blob: 57ee9c67de55a36278564c8274a05dbb2b4a6ecb [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 */
136};
137
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300138struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100139 enum drm_fb_type type;
140
Daniel Stone6e7a9612017-04-04 17:54:26 +0100141 int refcnt;
142
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200143 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100144 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000145 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200146 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200147 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200148
149 /* Used by gbm fbs */
150 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100151 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200152
153 /* Used by dumb fbs */
154 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300155};
156
Richard Hughes2b2092a2013-04-24 14:58:02 +0100157struct drm_edid {
158 char eisa_id[13];
159 char monitor_name[13];
160 char pnp_id[5];
161 char serial_number[13];
162};
163
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400164struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200165 struct weston_output base;
166 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400167
Daniel Stone17339232015-11-28 12:09:47 +0000168 uint32_t crtc_id; /* object ID to pass to DRM functions */
169 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400170 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700171 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100172 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300173 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100174 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200175
Daniel Stone36609c72015-06-18 07:49:02 +0100176 enum dpms_enum dpms;
177
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300178 int vblank_pending;
179 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800180 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200181 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300182
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100183 struct gbm_surface *gbm_surface;
184 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400185 struct weston_plane cursor_plane;
186 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500187 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400188 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300189 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200190 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200191
192 struct drm_fb *dumb[2];
193 pixman_image_t *image[2];
194 int current_image;
195 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300196
197 struct vaapi_recorder *recorder;
198 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000199
200 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400201};
202
Jesse Barnes58ef3792012-02-23 09:45:49 -0500203/*
204 * An output has a primary display plane plus zero or more sprites for
205 * blending display contents.
206 */
207struct drm_sprite {
208 struct wl_list link;
209
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400210 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500211
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200212 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300213 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300214 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500215
Jesse Barnes58ef3792012-02-23 09:45:49 -0500216 uint32_t possible_crtcs;
217 uint32_t plane_id;
218 uint32_t count_formats;
219
220 int32_t src_x, src_y;
221 uint32_t src_w, src_h;
222 uint32_t dest_x, dest_y;
223 uint32_t dest_w, dest_h;
224
225 uint32_t formats[];
226};
227
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300228static struct gl_renderer_interface *gl_renderer;
229
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500230static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400231
Armin Krezović545dba62016-08-05 15:54:18 +0200232static inline struct drm_output *
233to_drm_output(struct weston_output *base)
234{
235 return container_of(base, struct drm_output, base);
236}
237
238static inline struct drm_backend *
239to_drm_backend(struct weston_compositor *base)
240{
241 return container_of(base->backend, struct drm_backend, base);
242}
243
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000244static int
245pageflip_timeout(void *data) {
246 /*
247 * Our timer just went off, that means we're not receiving drm
248 * page flip events anymore for that output. Let's gracefully exit
249 * weston with a return value so devs can debug what's going on.
250 */
251 struct drm_output *output = data;
252 struct weston_compositor *compositor = output->base.compositor;
253
254 weston_log("Pageflip timeout reached on output %s, your "
255 "driver is probably buggy! Exiting.\n",
256 output->base.name);
257 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
258
259 return 0;
260}
261
262/* Creates the pageflip timer. Note that it isn't armed by default */
263static int
264drm_output_pageflip_timer_create(struct drm_output *output)
265{
266 struct wl_event_loop *loop = NULL;
267 struct weston_compositor *ec = output->base.compositor;
268
269 loop = wl_display_get_event_loop(ec->wl_display);
270 assert(loop);
271 output->pageflip_timer = wl_event_loop_add_timer(loop,
272 pageflip_timeout,
273 output);
274
275 if (output->pageflip_timer == NULL) {
276 weston_log("creating drm pageflip timer failed: %m\n");
277 return -1;
278 }
279
280 return 0;
281}
282
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400283static void
284drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400285
Mario Kleinerf507ec32015-06-21 21:25:14 +0200286static void
287drm_output_update_msc(struct drm_output *output, unsigned int seq);
288
Jesse Barnes58ef3792012-02-23 09:45:49 -0500289static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000290drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500291{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000292 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500293}
294
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000295static struct drm_output *
296drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
297{
298 struct drm_output *output;
299
300 wl_list_for_each(output, &b->compositor->output_list, base.link) {
301 if (output->crtc_id == crtc_id)
302 return output;
303 }
304
305 wl_list_for_each(output, &b->compositor->pending_output_list,
306 base.link) {
307 if (output->crtc_id == crtc_id)
308 return output;
309 }
310
311 return NULL;
312}
313
Daniel Stonec0ec7592017-02-09 13:58:35 +0000314static struct drm_output *
315drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
316{
317 struct drm_output *output;
318
319 wl_list_for_each(output, &b->compositor->output_list, base.link) {
320 if (output->connector_id == connector_id)
321 return output;
322 }
323
324 wl_list_for_each(output, &b->compositor->pending_output_list,
325 base.link) {
326 if (output->connector_id == connector_id)
327 return output;
328 }
329
330 return NULL;
331}
332
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300333static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100334drm_fb_destroy(struct drm_fb *fb)
335{
336 if (fb->fb_id != 0)
337 drmModeRmFB(fb->fd, fb->fb_id);
338 weston_buffer_reference(&fb->buffer_ref, NULL);
339 free(fb);
340}
341
342static void
343drm_fb_destroy_dumb(struct drm_fb *fb)
344{
345 struct drm_mode_destroy_dumb destroy_arg;
346
347 assert(fb->type == BUFFER_PIXMAN_DUMB);
348
349 if (fb->map && fb->size > 0)
350 munmap(fb->map, fb->size);
351
352 memset(&destroy_arg, 0, sizeof(destroy_arg));
353 destroy_arg.handle = fb->handle;
354 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
355
356 drm_fb_destroy(fb);
357}
358
359static void
360drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300361{
362 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300363
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100364 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT);
365 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300366}
367
368static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000369drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300370 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200371{
372 struct drm_fb *fb;
373 int ret;
374
375 struct drm_mode_create_dumb create_arg;
376 struct drm_mode_destroy_dumb destroy_arg;
377 struct drm_mode_map_dumb map_arg;
378
Peter Huttererf3d62272013-08-08 11:57:05 +1000379 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200380 if (!fb)
381 return NULL;
382
Daniel Stone6e7a9612017-04-04 17:54:26 +0100383 fb->refcnt = 1;
384
Daniel Stone0b70fa42017-04-04 17:54:23 +0100385 fb->format = pixel_format_get_info(format);
386 if (!fb->format) {
387 weston_log("failed to look up format 0x%lx\n",
388 (unsigned long) format);
389 goto err_fb;
390 }
391
392 if (!fb->format->depth || !fb->format->bpp) {
393 weston_log("format 0x%lx is not compatible with dumb buffers\n",
394 (unsigned long) format);
395 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300396 }
397
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700398 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100399 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200400 create_arg.width = width;
401 create_arg.height = height;
402
Giulio Camuffo954f1832014-10-11 18:27:30 +0300403 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200404 if (ret)
405 goto err_fb;
406
Daniel Stonefc175a72017-04-04 17:54:22 +0100407 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200408 fb->handle = create_arg.handle;
409 fb->stride = create_arg.pitch;
410 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000411 fb->width = width;
412 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300413 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200414
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300415 ret = -1;
416
417 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700418 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300419
420 handles[0] = fb->handle;
421 pitches[0] = fb->stride;
422 offsets[0] = 0;
423
424 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100425 fb->format->format,
426 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300427 &fb->fb_id, 0);
428 if (ret) {
429 weston_log("addfb2 failed: %m\n");
430 b->no_addfb2 = 1;
431 }
432 }
433
434 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100435 ret = drmModeAddFB(b->drm.fd, width, height,
436 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300437 fb->stride, fb->handle, &fb->fb_id);
438 }
439
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200440 if (ret)
441 goto err_bo;
442
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700443 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200444 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400445 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200446 if (ret)
447 goto err_add_fb;
448
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500449 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300450 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200451 if (fb->map == MAP_FAILED)
452 goto err_add_fb;
453
454 return fb;
455
456err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300457 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200458err_bo:
459 memset(&destroy_arg, 0, sizeof(destroy_arg));
460 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300461 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200462err_fb:
463 free(fb);
464 return NULL;
465}
466
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200467static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100468drm_fb_ref(struct drm_fb *fb)
469{
470 fb->refcnt++;
471 return fb;
472}
473
474static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100475drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
476 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300477{
478 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500479 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300480 int ret;
481
Daniel Stonefc175a72017-04-04 17:54:22 +0100482 if (fb) {
483 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100484 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100485 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300486
Bryce Harringtonde16d892014-11-20 22:21:57 -0800487 fb = zalloc(sizeof *fb);
488 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200489 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300490
Daniel Stonefc175a72017-04-04 17:54:22 +0100491 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100492 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300493 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300494
Daniel Stonec8c917c2016-11-14 17:45:58 +0000495 fb->width = gbm_bo_get_width(bo);
496 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200497 fb->stride = gbm_bo_get_stride(bo);
498 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100499 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000500 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300501 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300502
Daniel Stone0b70fa42017-04-04 17:54:23 +0100503 if (!fb->format) {
504 weston_log("couldn't look up format 0x%lx\n",
505 (unsigned long) format);
506 goto err_free;
507 }
508
Daniel Stonec8c917c2016-11-14 17:45:58 +0000509 if (backend->min_width > fb->width ||
510 fb->width > backend->max_width ||
511 backend->min_height > fb->height ||
512 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200513 weston_log("bo geometry out of bounds\n");
514 goto err_free;
515 }
516
517 ret = -1;
518
Giulio Camuffo954f1832014-10-11 18:27:30 +0300519 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200520 handles[0] = fb->handle;
521 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200522 offsets[0] = 0;
523
Daniel Stonec8c917c2016-11-14 17:45:58 +0000524 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200525 format, handles, pitches, offsets,
526 &fb->fb_id, 0);
527 if (ret) {
528 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300529 backend->no_addfb2 = 1;
530 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200531 }
532 }
533
Daniel Stone0b70fa42017-04-04 17:54:23 +0100534 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000535 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100536 fb->format->depth, fb->format->bpp,
537 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200538
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300539 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200540 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200541 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300542 }
543
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100544 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300545
546 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200547
548err_free:
549 free(fb);
550 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300551}
552
553static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500554drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200555{
Pekka Paalanende685b82012-12-04 15:58:12 +0200556 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100557 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200558 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200559}
560
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200561static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100562drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200563{
564 if (!fb)
565 return;
566
Daniel Stone6e7a9612017-04-04 17:54:26 +0100567 assert(fb->refcnt > 0);
568 if (--fb->refcnt > 0)
569 return;
570
Daniel Stonefc175a72017-04-04 17:54:22 +0100571 switch (fb->type) {
572 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100573 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100574 break;
575 case BUFFER_CLIENT:
576 gbm_bo_destroy(fb->bo);
577 break;
578 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100579 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100580 break;
581 default:
582 assert(NULL);
583 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200584 }
585}
586
Daniel Stone90648872016-10-21 18:08:37 +0100587static int
588drm_view_transform_supported(struct weston_view *ev)
589{
590 return !ev->transform.enabled ||
591 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
592}
593
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500594static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200595drm_output_check_scanout_format(struct drm_output *output,
596 struct weston_surface *es, struct gbm_bo *bo)
597{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200598 uint32_t format;
599 pixman_region32_t r;
600
601 format = gbm_bo_get_format(bo);
602
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700603 if (format == GBM_FORMAT_ARGB8888) {
604 /* We can scanout an ARGB buffer if the surface's
605 * opaque region covers the whole output, but we have
606 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800607 pixman_region32_init_rect(&r, 0, 0,
608 output->base.width,
609 output->base.height);
610 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200611
612 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500613 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200614
615 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500616 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700617
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100618 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700619 return format;
620
621 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200622}
623
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400624static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200625drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500626 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500627{
Armin Krezović545dba62016-08-05 15:54:18 +0200628 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200630 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300631 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500632 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500633
Daniel Stone90648872016-10-21 18:08:37 +0100634 /* Don't import buffers which span multiple outputs. */
635 if (ev->output_mask != (1u << output->base.id))
636 return NULL;
637
Daniel Stone296d7a92016-10-21 18:05:37 +0100638 /* We use GBM to import buffers. */
639 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400640 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500641
Daniel Stone296d7a92016-10-21 18:05:37 +0100642 if (buffer == NULL)
643 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100644 if (wl_shm_buffer_get(buffer->resource))
645 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100646
647 /* Make sure our view is exactly compatible with the output. */
648 if (ev->geometry.x != output->base.x ||
649 ev->geometry.y != output->base.y)
650 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100651 if (buffer->width != output->base.current_mode->width ||
652 buffer->height != output->base.current_mode->height)
653 return NULL;
654
Daniel Stone296d7a92016-10-21 18:05:37 +0100655 if (ev->transform.enabled)
656 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200657 if (ev->geometry.scissor_enabled)
658 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100659 if (viewport->buffer.transform != output->base.transform)
660 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100661 if (viewport->buffer.scale != output->base.current_scale)
662 return NULL;
663 if (!drm_view_transform_supported(ev))
664 return NULL;
665
666 if (ev->alpha != 1.0f)
667 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100668
Giulio Camuffo954f1832014-10-11 18:27:30 +0300669 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700670 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500671
Rob Bradford9b101872012-09-14 23:25:41 +0100672 /* Unable to use the buffer for scanout */
673 if (!bo)
674 return NULL;
675
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500677 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300678 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400679 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300680 }
681
Daniel Stonefc175a72017-04-04 17:54:22 +0100682 output->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300683 if (!output->next) {
684 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400685 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300686 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500687
Pekka Paalanende685b82012-12-04 15:58:12 +0200688 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500689
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400690 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500691}
692
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500693static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200694drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400695{
Armin Krezović545dba62016-08-05 15:54:18 +0200696 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300697 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400698
Giulio Camuffo954f1832014-10-11 18:27:30 +0300699 output->base.compositor->renderer->repaint_output(&output->base,
700 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400701
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100702 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300703 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200704 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400705 return;
706 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300707
Daniel Stonefc175a72017-04-04 17:54:22 +0100708 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format,
709 BUFFER_GBM_SURFACE);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300710 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200711 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100712 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300713 return;
714 }
Daniel Stone05a5ac22017-04-04 17:54:25 +0100715 output->next->gbm_surface = output->gbm_surface;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400716}
717
718static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200719drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
720{
721 struct weston_compositor *ec = output->base.compositor;
722 pixman_region32_t total_damage, previous_damage;
723
724 pixman_region32_init(&total_damage);
725 pixman_region32_init(&previous_damage);
726
727 pixman_region32_copy(&previous_damage, damage);
728
729 pixman_region32_union(&total_damage, damage, &output->previous_damage);
730 pixman_region32_copy(&output->previous_damage, &previous_damage);
731
732 output->current_image ^= 1;
733
Daniel Stone6e7a9612017-04-04 17:54:26 +0100734 output->next = drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200735 pixman_renderer_output_set_buffer(&output->base,
736 output->image[output->current_image]);
737
738 ec->renderer->repaint_output(&output->base, &total_damage);
739
740 pixman_region32_fini(&total_damage);
741 pixman_region32_fini(&previous_damage);
742}
743
744static void
745drm_output_render(struct drm_output *output, pixman_region32_t *damage)
746{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300747 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200748 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200749
Giulio Camuffo954f1832014-10-11 18:27:30 +0300750 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200751 drm_output_render_pixman(output, damage);
752 else
753 drm_output_render_gl(output, damage);
754
Giulio Camuffo954f1832014-10-11 18:27:30 +0300755 pixman_region32_subtract(&c->primary_plane.damage,
756 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200757}
758
759static void
Richard Hughese7299962013-05-01 21:52:12 +0100760drm_output_set_gamma(struct weston_output *output_base,
761 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
762{
763 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200764 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300765 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200766 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100767
768 /* check */
769 if (output_base->gamma_size != size)
770 return;
771 if (!output->original_crtc)
772 return;
773
Giulio Camuffo954f1832014-10-11 18:27:30 +0300774 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100775 output->crtc_id,
776 size, r, g, b);
777 if (rc)
778 weston_log("set gamma failed: %m\n");
779}
780
Bryce Harringtonada4f072015-06-30 13:25:46 -0700781/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200782 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700783 * The pipe parameter indicates which CRTC is in use. Knowing this, we
784 * can determine which vblank sequence type to use for it. Traditional
785 * cards had only two CRTCs, with CRTC 0 using no special flags, and
786 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
787 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200788 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700789 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
790 * 0-31. If this is non-zero it indicates we're dealing with a
791 * multi-gpu situation and we need to calculate the vblank sync
792 * using DRM_BLANK_HIGH_CRTC_MASK.
793 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300794static unsigned int
795drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200796{
797 if (output->pipe > 1)
798 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
799 DRM_VBLANK_HIGH_CRTC_MASK;
800 else if (output->pipe > 0)
801 return DRM_VBLANK_SECONDARY;
802 else
803 return 0;
804}
805
David Herrmann1edf44c2013-10-22 17:11:26 +0200806static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500807drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000808 pixman_region32_t *damage,
809 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100810{
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);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500814 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400815 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500816 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100817
Armin Krezović08368132016-09-30 14:11:05 +0200818 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200819 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800820
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300821 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400822 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300823 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200824 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100825
Hardeningff39efa2013-09-18 23:56:35 +0200826 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200827 if (!output->current ||
828 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300829 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300830 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400831 &output->connector_id, 1,
832 &mode->mode_info);
833 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200834 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200835 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400836 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300837 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200838 }
839
Giulio Camuffo954f1832014-10-11 18:27:30 +0300840 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300841 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500842 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200843 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200844 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500845 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100846
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300847 output->page_flip_pending = 1;
848
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000849 if (output->pageflip_timer)
850 wl_event_source_timer_update(output->pageflip_timer,
851 backend->pageflip_timeout);
852
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400853 drm_output_set_cursor(output);
854
Jesse Barnes58ef3792012-02-23 09:45:49 -0500855 /*
856 * Now, update all the sprite surfaces
857 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300858 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200859 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500860 drmVBlank vbl = {
861 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
862 .request.sequence = 1,
863 };
864
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200865 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000866 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500867 continue;
868
Giulio Camuffo954f1832014-10-11 18:27:30 +0300869 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200870 fb_id = s->next->fb_id;
871
Giulio Camuffo954f1832014-10-11 18:27:30 +0300872 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200873 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500874 s->dest_x, s->dest_y,
875 s->dest_w, s->dest_h,
876 s->src_x, s->src_y,
877 s->src_w, s->src_h);
878 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200879 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500880 ret, strerror(errno));
881
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200882 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500883
Jesse Barnes58ef3792012-02-23 09:45:49 -0500884 /*
885 * Queue a vblank signal so we know when the surface
886 * becomes active on the display or has been replaced.
887 */
888 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300889 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500890 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200891 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500892 ret, strerror(errno));
893 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300894
895 s->output = output;
896 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500897 }
898
David Herrmann1edf44c2013-10-22 17:11:26 +0200899 return 0;
900
901err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800902 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200903 if (output->next) {
Daniel Stone05a5ac22017-04-04 17:54:25 +0100904 drm_fb_unref(output->next);
David Herrmann1edf44c2013-10-22 17:11:26 +0200905 output->next = NULL;
906 }
907
908 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400909}
910
911static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200912drm_output_start_repaint_loop(struct weston_output *output_base)
913{
Armin Krezović545dba62016-08-05 15:54:18 +0200914 struct drm_output *output = to_drm_output(output_base);
915 struct drm_backend *backend =
916 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200917 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200918 struct timespec ts, tnow;
919 struct timespec vbl2now;
920 int64_t refresh_nsec;
921 int ret;
922 drmVBlank vbl = {
923 .request.type = DRM_VBLANK_RELATIVE,
924 .request.sequence = 0,
925 .request.signal = 0,
926 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300927
Armin Krezović08368132016-09-30 14:11:05 +0200928 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800929 return;
930
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300931 if (!output->current) {
932 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200933 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300934 }
935
Mario Kleinerf507ec32015-06-21 21:25:14 +0200936 /* Try to get current msc and timestamp via instant query */
937 vbl.request.type |= drm_waitvblank_pipe(output);
938 ret = drmWaitVBlank(backend->drm.fd, &vbl);
939
940 /* Error ret or zero timestamp means failure to get valid timestamp */
941 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
942 ts.tv_sec = vbl.reply.tval_sec;
943 ts.tv_nsec = vbl.reply.tval_usec * 1000;
944
945 /* Valid timestamp for most recent vblank - not stale?
946 * Stale ts could happen on Linux 3.17+, so make sure it
947 * is not older than 1 refresh duration since now.
948 */
949 weston_compositor_read_presentation_clock(backend->compositor,
950 &tnow);
951 timespec_sub(&vbl2now, &tnow, &ts);
952 refresh_nsec =
953 millihz_to_nsec(output->base.current_mode->refresh);
954 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
955 drm_output_update_msc(output, vbl.reply.sequence);
956 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200957 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200958 return;
959 }
960 }
961
962 /* Immediate query didn't provide valid timestamp.
963 * Use pageflip fallback.
964 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300965 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200966
Giulio Camuffo954f1832014-10-11 18:27:30 +0300967 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200968 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
969 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200970 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200971 }
David Herrmann3c688c52013-10-22 17:11:25 +0200972
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000973 if (output->pageflip_timer)
974 wl_event_source_timer_update(output->pageflip_timer,
975 backend->pageflip_timeout);
976
David Herrmann3c688c52013-10-22 17:11:25 +0200977 return;
978
979finish_frame:
980 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +0000981 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200982 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200983}
984
985static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400986drm_output_update_msc(struct drm_output *output, unsigned int seq)
987{
988 uint64_t msc_hi = output->base.msc >> 32;
989
990 if (seq < (output->base.msc & 0xffffffff))
991 msc_hi++;
992
993 output->base.msc = (msc_hi << 32) + seq;
994}
995
996static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500997vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
998 void *data)
999{
1000 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001001 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001002 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001003 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1004 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001005
Pekka Paalanen641307c2014-09-23 22:08:47 -04001006 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001007 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001008
Daniel Stone05a5ac22017-04-04 17:54:25 +01001009 drm_fb_unref(s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001010 s->current = s->next;
1011 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001012
1013 if (!output->page_flip_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001014 /* Stop the pageflip timer instead of rearming it here */
1015 if (output->pageflip_timer)
1016 wl_event_source_timer_update(output->pageflip_timer, 0);
1017
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001018 ts.tv_sec = sec;
1019 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001020 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001021 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001022}
1023
1024static void
Armin Krezović08368132016-09-30 14:11:05 +02001025drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001026
1027static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001028page_flip_handler(int fd, unsigned int frame,
1029 unsigned int sec, unsigned int usec, void *data)
1030{
Armin Krezović545dba62016-08-05 15:54:18 +02001031 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001032 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001033 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1034 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1035 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001036
Pekka Paalanen641307c2014-09-23 22:08:47 -04001037 drm_output_update_msc(output, frame);
1038
Jonas Ådahle5a12252013-04-05 23:07:11 +02001039 /* We don't set page_flip_pending on start_repaint_loop, in that case
1040 * we just want to page flip to the current buffer to get an accurate
1041 * timestamp */
1042 if (output->page_flip_pending) {
Daniel Stone05a5ac22017-04-04 17:54:25 +01001043 drm_fb_unref(output->current);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001044 output->current = output->next;
1045 output->next = NULL;
1046 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001047
Jonas Ådahle5a12252013-04-05 23:07:11 +02001048 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001049
Xiong Zhangabd5d472013-10-11 14:43:07 +08001050 if (output->destroy_pending)
1051 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001052 else if (output->disable_pending)
1053 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001054 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001055 /* Stop the pageflip timer instead of rearming it here */
1056 if (output->pageflip_timer)
1057 wl_event_source_timer_update(output->pageflip_timer, 0);
1058
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001059 ts.tv_sec = sec;
1060 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001061 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001062
1063 /* We can't call this from frame_notify, because the output's
1064 * repaint needed flag is cleared just after that */
1065 if (output->recorder)
1066 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001067 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001068}
1069
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001070static uint32_t
1071drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001073{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001074 uint32_t i, format;
1075
1076 format = gbm_bo_get_format(bo);
1077
1078 if (format == GBM_FORMAT_ARGB8888) {
1079 pixman_region32_t r;
1080
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001081 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001082 ev->surface->width,
1083 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001085
1086 if (!pixman_region32_not_empty(&r))
1087 format = GBM_FORMAT_XRGB8888;
1088
1089 pixman_region32_fini(&r);
1090 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001091
1092 for (i = 0; i < s->count_formats; i++)
1093 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001094 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001095
1096 return 0;
1097}
1098
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001099static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001100drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001102{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001103 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001104 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001105 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001106 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001107 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001108 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001109 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001110 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001111 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001112 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001113 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001114 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001115
Giulio Camuffo954f1832014-10-11 18:27:30 +03001116 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001117 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001118
Daniel Stone296d7a92016-10-21 18:05:37 +01001119 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001120 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001121 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001122
Daniel Stone296d7a92016-10-21 18:05:37 +01001123 /* We can only import GBM buffers. */
1124 if (b->gbm == NULL)
1125 return NULL;
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001128 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001129 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001130 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001131 return NULL;
1132
Daniel Stone296d7a92016-10-21 18:05:37 +01001133 if (viewport->buffer.transform != output->base.transform)
1134 return NULL;
1135 if (viewport->buffer.scale != output->base.current_scale)
1136 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001138 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001139
Daniel Stone296d7a92016-10-21 18:05:37 +01001140 if (ev->alpha != 1.0f)
1141 return NULL;
1142
Giulio Camuffo954f1832014-10-11 18:27:30 +03001143 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +00001144 if (!drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001145 continue;
1146
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001147 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001148 found = 1;
1149 break;
1150 }
1151 }
1152
1153 /* No sprites available */
1154 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001155 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001156
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001157 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001158#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001159 /* XXX: TODO:
1160 *
1161 * Use AddFB2 directly, do not go via GBM.
1162 * Add support for multiplanar formats.
1163 * Both require refactoring in the DRM-backend to
1164 * support a mix of gbm_bos and drmfbs.
1165 */
1166 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001167 .fd = dmabuf->attributes.fd[0],
1168 .width = dmabuf->attributes.width,
1169 .height = dmabuf->attributes.height,
1170 .stride = dmabuf->attributes.stride[0],
1171 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001172 };
1173
Micah Fedkec8890122017-02-01 15:28:23 -05001174 /* XXX: TODO:
1175 *
1176 * Currently the buffer is rejected if any dmabuf attribute
1177 * flag is set. This keeps us from passing an inverted /
1178 * interlaced / bottom-first buffer (or any other type that may
1179 * be added in the future) through to an overlay. Ultimately,
1180 * these types of buffers should be handled through buffer
1181 * transforms and not as spot-checks requiring specific
1182 * knowledge. */
1183 if (dmabuf->attributes.n_planes != 1 ||
1184 dmabuf->attributes.offset[0] != 0 ||
1185 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001186 return NULL;
1187
1188 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1189 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001190#else
1191 return NULL;
1192#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001193 } else {
1194 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1195 buffer_resource, GBM_BO_USE_SCANOUT);
1196 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001197 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001198 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001201 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001202 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001203 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001204 }
1205
Daniel Stonefc175a72017-04-04 17:54:22 +01001206 s->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001207 if (!s->next) {
1208 gbm_bo_destroy(bo);
1209 return NULL;
1210 }
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001215 s->plane.x = box->x1;
1216 s->plane.y = box->y1;
1217
Jesse Barnes58ef3792012-02-23 09:45:49 -05001218 /*
1219 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001220 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001221 * for us already).
1222 */
1223 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001225 &output->base.region);
1226 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001227 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001228 tbox = weston_transformed_rect(output->base.width,
1229 output->base.height,
1230 output->base.transform,
1231 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001232 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001233 s->dest_x = tbox.x1;
1234 s->dest_y = tbox.y1;
1235 s->dest_w = tbox.x2 - tbox.x1;
1236 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001237 pixman_region32_fini(&dest_rect);
1238
1239 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001241 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001242 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 weston_view_from_global_fixed(ev,
1245 wl_fixed_from_int(box->x1),
1246 wl_fixed_from_int(box->y1),
1247 &sx1, &sy1);
1248 weston_view_from_global_fixed(ev,
1249 wl_fixed_from_int(box->x2),
1250 wl_fixed_from_int(box->y2),
1251 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001252
1253 if (sx1 < 0)
1254 sx1 = 0;
1255 if (sy1 < 0)
1256 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001257 if (sx2 > wl_fixed_from_int(ev->surface->width))
1258 sx2 = wl_fixed_from_int(ev->surface->width);
1259 if (sy2 > wl_fixed_from_int(ev->surface->height))
1260 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001261
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001262 tbox.x1 = sx1;
1263 tbox.y1 = sy1;
1264 tbox.x2 = sx2;
1265 tbox.y2 = sy2;
1266
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001267 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1268 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001269 viewport->buffer.transform,
1270 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001271 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001272
1273 s->src_x = tbox.x1 << 8;
1274 s->src_y = tbox.y1 << 8;
1275 s->src_w = (tbox.x2 - tbox.x1) << 8;
1276 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001277 pixman_region32_fini(&src_rect);
1278
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001279 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001280}
1281
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001282static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001283drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001285{
Armin Krezović545dba62016-08-05 15:54:18 +02001286 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001287 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001288 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001289 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001290
Giulio Camuffo954f1832014-10-11 18:27:30 +03001291 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001292 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001293
1294 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001295 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001296
1297 /* Don't import buffers which span multiple outputs. */
1298 if (ev->output_mask != (1u << output->base.id))
1299 return NULL;
1300
1301 /* We use GBM to import SHM buffers. */
1302 if (b->gbm == NULL)
1303 return NULL;
1304
Derek Foreman6c19b692015-12-03 14:07:12 -06001305 if (ev->surface->buffer_ref.buffer == NULL)
1306 return NULL;
1307 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1308 if (!shmbuf)
1309 return NULL;
1310 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1311 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001312
1313 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1314 return NULL;
1315 if (ev->transform.enabled &&
1316 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1317 return NULL;
1318 if (viewport->buffer.scale != output->base.current_scale)
1319 return NULL;
1320 if (ev->geometry.scissor_enabled)
1321 return NULL;
1322
Derek Foreman6c19b692015-12-03 14:07:12 -06001323 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001324 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001325 return NULL;
1326
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001328 weston_view_to_global_float(ev, 0, 0, &x, &y);
1329 output->cursor_plane.x = x;
1330 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001331
1332 return &output->cursor_plane;
1333}
1334
Pekka Paalanend0ead482014-06-16 12:05:40 +03001335/**
1336 * Update the image for the current cursor surface
1337 *
1338 * @param b DRM backend structure
1339 * @param bo GBM buffer object to write into
1340 * @param ev View to use for cursor image
1341 */
1342static void
1343cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1344 struct weston_view *ev)
1345{
1346 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1347 uint32_t buf[b->cursor_width * b->cursor_height];
1348 int32_t stride;
1349 uint8_t *s;
1350 int i;
1351
1352 assert(buffer && buffer->shm_buffer);
1353 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1354 assert(ev->surface->width <= b->cursor_width);
1355 assert(ev->surface->height <= b->cursor_height);
1356
1357 memset(buf, 0, sizeof buf);
1358 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1359 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1360
1361 wl_shm_buffer_begin_access(buffer->shm_buffer);
1362 for (i = 0; i < ev->surface->height; i++)
1363 memcpy(buf + i * b->cursor_width,
1364 s + i * stride,
1365 ev->surface->width * 4);
1366 wl_shm_buffer_end_access(buffer->shm_buffer);
1367
1368 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1369 weston_log("failed update cursor: %m\n");
1370}
1371
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001372static void
1373drm_output_set_cursor(struct drm_output *output)
1374{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001376 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001377 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001378 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001379 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001382 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001383 return;
1384 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001385
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001386 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001387 pixman_region32_fini(&output->cursor_plane.damage);
1388 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001389 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001390 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001391
Pekka Paalanend0ead482014-06-16 12:05:40 +03001392 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001393 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001394 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1395 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001396 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001397 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001398 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001399 }
1400
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001401 x = (output->cursor_plane.x - output->base.x) *
1402 output->base.current_scale;
1403 y = (output->cursor_plane.y - output->base.y) *
1404 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001405
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001406 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1407 weston_log("failed to move cursor: %m\n");
1408 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001409 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001410}
1411
Jesse Barnes58ef3792012-02-23 09:45:49 -05001412static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001413drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001414{
Armin Krezović545dba62016-08-05 15:54:18 +02001415 struct drm_backend *b = to_drm_backend(output_base->compositor);
1416 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001418 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001419 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001420
1421 /*
1422 * Find a surface for each sprite in the output using some heuristics:
1423 * 1) size
1424 * 2) frequency of update
1425 * 3) opacity (though some hw might support alpha blending)
1426 * 4) clipping (this can be fixed with color keys)
1427 *
1428 * The idea is to save on blitting since this should save power.
1429 * If we can get a large video surface on the sprite for example,
1430 * the main display surface may not need to update at all, and
1431 * the client buffer can be used directly for the sprite surface
1432 * as we do for flipping full screen surfaces.
1433 */
1434 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001435 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001436
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001437 output->cursor_view = NULL;
1438 output->cursor_plane.x = INT32_MIN;
1439 output->cursor_plane.y = INT32_MIN;
1440
Giulio Camuffo954f1832014-10-11 18:27:30 +03001441 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001442 struct weston_surface *es = ev->surface;
1443
1444 /* Test whether this buffer can ever go into a plane:
1445 * non-shm, or small enough to be a cursor.
1446 *
1447 * Also, keep a reference when using the pixman renderer.
1448 * That makes it possible to do a seamless switch to the GL
1449 * renderer and since the pixman renderer keeps a reference
1450 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001451 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001452 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001453 (es->buffer_ref.buffer &&
1454 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001455 (ev->surface->width <= b->cursor_width &&
1456 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001457 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001458 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001459 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001460
Jesse Barnes58ef3792012-02-23 09:45:49 -05001461 pixman_region32_init(&surface_overlap);
1462 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001465 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001466 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001467 next_plane = primary;
1468 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001469 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001470 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001472 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001474 if (next_plane == NULL)
1475 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001476
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001478
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001479 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001480 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001482
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001483 if (next_plane == primary ||
1484 next_plane == &output->cursor_plane) {
1485 /* cursor plane involves a copy */
1486 ev->psf_flags = 0;
1487 } else {
1488 /* All other planes are a direct scanout of a
1489 * single client buffer.
1490 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001491 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001492 }
1493
Jesse Barnes58ef3792012-02-23 09:45:49 -05001494 pixman_region32_fini(&surface_overlap);
1495 }
1496 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001497}
1498
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001499/**
1500 * Find the closest-matching mode for a given target
1501 *
1502 * Given a target mode, find the most suitable mode amongst the output's
1503 * current mode list to use, preferring the current mode if possible, to
1504 * avoid an expensive mode switch.
1505 *
1506 * @param output DRM output
1507 * @param target_mode Mode to attempt to match
1508 * @returns Pointer to a mode from the output's mode list
1509 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001510static struct drm_mode *
1511choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1512{
1513 struct drm_mode *tmp_mode = NULL, *mode;
1514
Hardeningff39efa2013-09-18 23:56:35 +02001515 if (output->base.current_mode->width == target_mode->width &&
1516 output->base.current_mode->height == target_mode->height &&
1517 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001518 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001519 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001520
1521 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1522 if (mode->mode_info.hdisplay == target_mode->width &&
1523 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001524 if (mode->base.refresh == target_mode->refresh ||
1525 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001526 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001527 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001528 tmp_mode = mode;
1529 }
1530 }
1531
1532 return tmp_mode;
1533}
1534
1535static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001536drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001537static void
1538drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001539static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001540drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001541static void
1542drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001543
1544static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001545drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1546{
1547 struct drm_output *output;
1548 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001549 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001550
1551 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001552 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001553 return -1;
1554 }
1555
1556 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001557 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001558 return -1;
1559 }
1560
Armin Krezović545dba62016-08-05 15:54:18 +02001561 b = to_drm_backend(output_base->compositor);
1562 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001563 drm_mode = choose_mode (output, mode);
1564
1565 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001566 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001567 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001568 }
1569
Hardeningff39efa2013-09-18 23:56:35 +02001570 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001571 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001572
Hardeningff39efa2013-09-18 23:56:35 +02001573 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001574
Hardeningff39efa2013-09-18 23:56:35 +02001575 output->base.current_mode = &drm_mode->base;
1576 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001577 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1578
Alex Wub7b8bda2012-04-17 17:20:48 +08001579 /* reset rendering stuff. */
Daniel Stone05a5ac22017-04-04 17:54:25 +01001580 drm_fb_unref(output->current);
1581 drm_fb_unref(output->next);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001582 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001583
Giulio Camuffo954f1832014-10-11 18:27:30 +03001584 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001585 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001587 weston_log("failed to init output pixman state with "
1588 "new mode\n");
1589 return -1;
1590 }
1591 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001592 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001593 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001594 weston_log("failed to init output egl state with "
1595 "new mode");
1596 return -1;
1597 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001598 }
1599
Alex Wub7b8bda2012-04-17 17:20:48 +08001600 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001601}
1602
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001603static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001604on_drm_input(int fd, uint32_t mask, void *data)
1605{
1606 drmEventContext evctx;
1607
1608 memset(&evctx, 0, sizeof evctx);
1609 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1610 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001611 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001612 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001613
1614 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001615}
1616
1617static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001618init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001619{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001620 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001621 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001622 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001623
Daniel Stoneefa504f2016-12-19 16:48:20 +00001624 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001625
Daniel Stoneefa504f2016-12-19 16:48:20 +00001626 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001627 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001628 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001629 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001630 clk_id = CLOCK_REALTIME;
1631
Giulio Camuffo954f1832014-10-11 18:27:30 +03001632 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001633 weston_log("Error: failed to set presentation clock %d.\n",
1634 clk_id);
1635 return -1;
1636 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001637
Daniel Stoneefa504f2016-12-19 16:48:20 +00001638 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001639 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001640 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001641 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001642 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001643
Daniel Stoneefa504f2016-12-19 16:48:20 +00001644 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001645 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001646 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001647 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001648 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001649
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001650 return 0;
1651}
1652
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001653static struct gbm_device *
1654create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001655{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001656 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001657
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001658 gl_renderer = weston_load_module("gl-renderer.so",
1659 "gl_renderer_interface");
1660 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001661 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001662
1663 /* GBM will load a dri driver, but even though they need symbols from
1664 * libglapi, in some version of Mesa they are not linked to it. Since
1665 * only the gl-renderer module links to it, the call above won't make
1666 * these symbols globally available, and loading the DRI driver fails.
1667 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1668 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1669
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001670 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001671
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001672 return gbm;
1673}
1674
Bryce Harringtonc056a982015-05-19 15:25:18 -07001675/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001676 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001677 *
1678 * This returns 0 if substitution isn't possible, but 0 might be a
1679 * legitimate format for other EGL platforms, so the caller is
1680 * responsible for checking for 0 before calling gl_renderer->create().
1681 *
1682 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1683 * but it's entirely possible we'll see this again on other implementations.
1684 */
1685static int
1686fallback_format_for(uint32_t format)
1687{
1688 switch (format) {
1689 case GBM_FORMAT_XRGB8888:
1690 return GBM_FORMAT_ARGB8888;
1691 case GBM_FORMAT_XRGB2101010:
1692 return GBM_FORMAT_ARGB2101010;
1693 default:
1694 return 0;
1695 }
1696}
1697
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001698static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001699drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001700{
Derek Foreman6d556372015-11-04 14:47:33 -06001701 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001702 b->gbm_format,
1703 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001704 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001705 };
Derek Foreman6d556372015-11-04 14:47:33 -06001706 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001707
Derek Foremanc4cfe852015-05-15 12:12:40 -05001708 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001709 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001710 if (gl_renderer->display_create(b->compositor,
1711 EGL_PLATFORM_GBM_KHR,
1712 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001713 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001714 gl_renderer->opaque_attribs,
1715 format,
1716 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001717 return -1;
1718 }
1719
1720 return 0;
1721}
1722
1723static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001725{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001726 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001727
Giulio Camuffo954f1832014-10-11 18:27:30 +03001728 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001729 return -1;
1730
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 if (drm_backend_create_gl_renderer(b) < 0) {
1732 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001733 return -1;
1734 }
1735
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001736 return 0;
1737}
1738
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001739static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001740init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001741{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001742 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001743}
1744
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001745/**
1746 * Add a mode to output's mode list
1747 *
1748 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1749 * output's mode list.
1750 *
1751 * @param output DRM output to add mode to
1752 * @param info DRM mode structure to add
1753 * @returns Newly-allocated Weston/DRM mode structure
1754 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001755static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001756drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001757{
1758 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001759 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001760
1761 mode = malloc(sizeof *mode);
1762 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001763 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001764
1765 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001766 mode->base.width = info->hdisplay;
1767 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001768
1769 /* Calculate higher precision (mHz) refresh rate */
1770 refresh = (info->clock * 1000000LL / info->htotal +
1771 info->vtotal / 2) / info->vtotal;
1772
1773 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1774 refresh *= 2;
1775 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1776 refresh /= 2;
1777 if (info->vscan > 1)
1778 refresh /= info->vscan;
1779
1780 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001781 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001782
1783 if (info->type & DRM_MODE_TYPE_PREFERRED)
1784 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1785
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001786 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1787
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001788 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001789}
1790
1791static int
1792drm_subpixel_to_wayland(int drm_value)
1793{
1794 switch (drm_value) {
1795 default:
1796 case DRM_MODE_SUBPIXEL_UNKNOWN:
1797 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1798 case DRM_MODE_SUBPIXEL_NONE:
1799 return WL_OUTPUT_SUBPIXEL_NONE;
1800 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1801 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1802 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1803 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1804 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1805 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1806 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1807 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1808 }
1809}
1810
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001811/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001812static uint32_t
1813drm_get_backlight(struct drm_output *output)
1814{
1815 long brightness, max_brightness, norm;
1816
1817 brightness = backlight_get_brightness(output->backlight);
1818 max_brightness = backlight_get_max_brightness(output->backlight);
1819
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001820 /* convert it on a scale of 0 to 255 */
1821 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001822
1823 return (uint32_t) norm;
1824}
1825
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001826/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001827static void
1828drm_set_backlight(struct weston_output *output_base, uint32_t value)
1829{
Armin Krezović545dba62016-08-05 15:54:18 +02001830 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001831 long max_brightness, new_brightness;
1832
1833 if (!output->backlight)
1834 return;
1835
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001836 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001837 return;
1838
1839 max_brightness = backlight_get_max_brightness(output->backlight);
1840
1841 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001842 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001843
1844 backlight_set_brightness(output->backlight, new_brightness);
1845}
1846
1847static drmModePropertyPtr
1848drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1849{
1850 drmModePropertyPtr props;
1851 int i;
1852
1853 for (i = 0; i < connector->count_props; i++) {
1854 props = drmModeGetProperty(fd, connector->props[i]);
1855 if (!props)
1856 continue;
1857
1858 if (!strcmp(props->name, name))
1859 return props;
1860
1861 drmModeFreeProperty(props);
1862 }
1863
1864 return NULL;
1865}
1866
1867static void
1868drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1869{
Armin Krezović545dba62016-08-05 15:54:18 +02001870 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001871 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001872 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001873 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001874
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001875 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001876 return;
1877
Daniel Stone36609c72015-06-18 07:49:02 +01001878 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1879 output->dpms_prop->prop_id, level);
1880 if (ret) {
1881 weston_log("DRM: DPMS: failed property set for %s\n",
1882 output->base.name);
1883 return;
1884 }
1885
1886 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001887}
1888
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001889static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001890 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1891 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1892 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1893 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1894 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1895 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1896 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1897 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1898 [DRM_MODE_CONNECTOR_Component] = "Component",
1899 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1900 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1901 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1902 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1903 [DRM_MODE_CONNECTOR_TV] = "TV",
1904 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001905#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001906 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1907 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001908#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001909};
1910
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001911static char *
1912make_connector_name(const drmModeConnector *con)
1913{
1914 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001915 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001916
1917 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1918 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001919
1920 if (!type_name)
1921 type_name = "UNNAMED";
1922
1923 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001924
1925 return strdup(name);
1926}
1927
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001928static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001929find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001930 drmModeRes *resources, drmModeConnector *connector)
1931{
1932 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001933 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00001934 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001935
1936 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00001937 uint32_t possible_crtcs, encoder_id, crtc_id;
1938
Giulio Camuffo954f1832014-10-11 18:27:30 +03001939 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001940 if (encoder == NULL) {
1941 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00001942 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001943 }
Daniel Stone75487c22017-01-16 14:33:38 +00001944 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001945 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00001946 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001947 drmModeFreeEncoder(encoder);
1948
1949 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00001950 if (!(possible_crtcs & (1 << i)))
1951 continue;
1952
1953 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
1954 continue;
1955
Daniel Stone75487c22017-01-16 14:33:38 +00001956 /* Try to preserve the existing
1957 * CRTC -> encoder -> connector routing; it makes
1958 * initialisation faster, and also since we have a
1959 * very dumb picking algorithm, may preserve a better
1960 * choice. */
1961 if (!connector->encoder_id ||
1962 (encoder_id == connector->encoder_id &&
1963 crtc_id == resources->crtcs[i]))
1964 return i;
1965
1966 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001967 }
1968 }
1969
Daniel Stone75487c22017-01-16 14:33:38 +00001970 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001971}
1972
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001973/* Init output state that depends on gl or gbm */
1974static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001975drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001976{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001977 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001978 output->gbm_format,
1979 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001980 };
1981 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001982
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001983 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001984 output->base.current_mode->width,
1985 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001986 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001987 GBM_BO_USE_SCANOUT |
1988 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001989 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001990 weston_log("failed to create gbm surface\n");
1991 return -1;
1992 }
1993
Derek Foremanc4cfe852015-05-15 12:12:40 -05001994 if (format[1])
1995 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001996 if (gl_renderer->output_window_create(&output->base,
1997 (EGLNativeWindowType)output->gbm_surface,
1998 output->gbm_surface,
1999 gl_renderer->opaque_attribs,
2000 format,
2001 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002002 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002003 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002004 return -1;
2005 }
2006
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002007 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002008
2009 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002010 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002011 continue;
2012
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002013 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002014 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002015 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002016 }
2017
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002018 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002019 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002020 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002021 }
2022
2023 return 0;
2024}
2025
Daniel Stone3e661f72016-11-04 17:24:06 +00002026static void
2027drm_output_fini_egl(struct drm_output *output)
2028{
2029 gl_renderer->output_destroy(&output->base);
2030 gbm_surface_destroy(output->gbm_surface);
2031}
2032
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002033static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002034drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002035{
Hardeningff39efa2013-09-18 23:56:35 +02002036 int w = output->base.current_mode->width;
2037 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002038 uint32_t format = output->gbm_format;
2039 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002040 unsigned int i;
2041
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002042 switch (format) {
2043 case GBM_FORMAT_XRGB8888:
2044 pixman_format = PIXMAN_x8r8g8b8;
2045 break;
2046 case GBM_FORMAT_RGB565:
2047 pixman_format = PIXMAN_r5g6b5;
2048 break;
2049 default:
2050 weston_log("Unsupported pixman format 0x%x\n", format);
2051 return -1;
2052 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002053
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002054 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002055 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002056 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002057 if (!output->dumb[i])
2058 goto err;
2059
2060 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002061 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002062 output->dumb[i]->map,
2063 output->dumb[i]->stride);
2064 if (!output->image[i])
2065 goto err;
2066 }
2067
2068 if (pixman_renderer_output_create(&output->base) < 0)
2069 goto err;
2070
2071 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002072 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002073
2074 return 0;
2075
2076err:
2077 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2078 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002079 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002080 if (output->image[i])
2081 pixman_image_unref(output->image[i]);
2082
2083 output->dumb[i] = NULL;
2084 output->image[i] = NULL;
2085 }
2086
2087 return -1;
2088}
2089
2090static void
2091drm_output_fini_pixman(struct drm_output *output)
2092{
2093 unsigned int i;
2094
2095 pixman_renderer_output_destroy(&output->base);
2096 pixman_region32_fini(&output->previous_damage);
2097
2098 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002099 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002100 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002101 output->dumb[i] = NULL;
2102 output->image[i] = NULL;
2103 }
2104}
2105
Richard Hughes2b2092a2013-04-24 14:58:02 +01002106static void
2107edid_parse_string(const uint8_t *data, char text[])
2108{
2109 int i;
2110 int replaced = 0;
2111
2112 /* this is always 12 bytes, but we can't guarantee it's null
2113 * terminated or not junk. */
2114 strncpy(text, (const char *) data, 12);
2115
Bryce Harrington9c7de162015-08-28 13:04:26 -07002116 /* guarantee our new string is null-terminated */
2117 text[12] = '\0';
2118
Richard Hughes2b2092a2013-04-24 14:58:02 +01002119 /* remove insane chars */
2120 for (i = 0; text[i] != '\0'; i++) {
2121 if (text[i] == '\n' ||
2122 text[i] == '\r') {
2123 text[i] = '\0';
2124 break;
2125 }
2126 }
2127
2128 /* ensure string is printable */
2129 for (i = 0; text[i] != '\0'; i++) {
2130 if (!isprint(text[i])) {
2131 text[i] = '-';
2132 replaced++;
2133 }
2134 }
2135
2136 /* if the string is random junk, ignore the string */
2137 if (replaced > 4)
2138 text[0] = '\0';
2139}
2140
2141#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2142#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2143#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2144#define EDID_OFFSET_DATA_BLOCKS 0x36
2145#define EDID_OFFSET_LAST_BLOCK 0x6c
2146#define EDID_OFFSET_PNPID 0x08
2147#define EDID_OFFSET_SERIAL 0x0c
2148
2149static int
2150edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2151{
2152 int i;
2153 uint32_t serial_number;
2154
2155 /* check header */
2156 if (length < 128)
2157 return -1;
2158 if (data[0] != 0x00 || data[1] != 0xff)
2159 return -1;
2160
2161 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2162 * /--08--\/--09--\
2163 * 7654321076543210
2164 * |\---/\---/\---/
2165 * R C1 C2 C3 */
2166 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2167 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2168 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2169 edid->pnp_id[3] = '\0';
2170
2171 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2172 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2173 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2174 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2175 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2176 if (serial_number > 0)
2177 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2178
2179 /* parse EDID data */
2180 for (i = EDID_OFFSET_DATA_BLOCKS;
2181 i <= EDID_OFFSET_LAST_BLOCK;
2182 i += 18) {
2183 /* ignore pixel clock data */
2184 if (data[i] != 0)
2185 continue;
2186 if (data[i+2] != 0)
2187 continue;
2188
2189 /* any useful blocks? */
2190 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2191 edid_parse_string(&data[i+5],
2192 edid->monitor_name);
2193 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2194 edid_parse_string(&data[i+5],
2195 edid->serial_number);
2196 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2197 edid_parse_string(&data[i+5],
2198 edid->eisa_id);
2199 }
2200 }
2201 return 0;
2202}
2203
2204static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002205find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002206 struct drm_output *output,
2207 drmModeConnector *connector)
2208{
2209 drmModePropertyBlobPtr edid_blob = NULL;
2210 drmModePropertyPtr property;
2211 int i;
2212 int rc;
2213
2214 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002215 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002216 if (!property)
2217 continue;
2218 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2219 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002220 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002221 connector->prop_values[i]);
2222 }
2223 drmModeFreeProperty(property);
2224 }
2225 if (!edid_blob)
2226 return;
2227
2228 rc = edid_parse(&output->edid,
2229 edid_blob->data,
2230 edid_blob->length);
2231 if (!rc) {
2232 weston_log("EDID data '%s', '%s', '%s'\n",
2233 output->edid.pnp_id,
2234 output->edid.monitor_name,
2235 output->edid.serial_number);
2236 if (output->edid.pnp_id[0] != '\0')
2237 output->base.make = output->edid.pnp_id;
2238 if (output->edid.monitor_name[0] != '\0')
2239 output->base.model = output->edid.monitor_name;
2240 if (output->edid.serial_number[0] != '\0')
2241 output->base.serial_number = output->edid.serial_number;
2242 }
2243 drmModeFreePropertyBlob(edid_blob);
2244}
2245
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002246
2247
2248static int
2249parse_modeline(const char *s, drmModeModeInfo *mode)
2250{
2251 char hsync[16];
2252 char vsync[16];
2253 float fclock;
2254
2255 mode->type = DRM_MODE_TYPE_USERDEF;
2256 mode->hskew = 0;
2257 mode->vscan = 0;
2258 mode->vrefresh = 0;
2259 mode->flags = 0;
2260
Rob Bradford307e09e2013-07-26 16:29:40 +01002261 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002262 &fclock,
2263 &mode->hdisplay,
2264 &mode->hsync_start,
2265 &mode->hsync_end,
2266 &mode->htotal,
2267 &mode->vdisplay,
2268 &mode->vsync_start,
2269 &mode->vsync_end,
2270 &mode->vtotal, hsync, vsync) != 11)
2271 return -1;
2272
2273 mode->clock = fclock * 1000;
2274 if (strcmp(hsync, "+hsync") == 0)
2275 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2276 else if (strcmp(hsync, "-hsync") == 0)
2277 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2278 else
2279 return -1;
2280
2281 if (strcmp(vsync, "+vsync") == 0)
2282 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2283 else if (strcmp(vsync, "-vsync") == 0)
2284 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2285 else
2286 return -1;
2287
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002288 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2289 mode->hdisplay, mode->vdisplay, fclock);
2290
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002291 return 0;
2292}
2293
Rob Bradford66bd9f52013-06-25 18:56:42 +01002294static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002295setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002296 struct weston_output *output,
2297 const char *s)
2298{
2299 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002300 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002301 struct udev_seat *seat;
2302
Giulio Camuffo954f1832014-10-11 18:27:30 +03002303 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002304 if (!seat)
2305 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002306
Derek Foreman0720ea32015-07-15 13:00:35 -05002307 seat->base.output = output;
2308
Derek Foreman1281a362015-07-31 16:55:32 -05002309 pointer = weston_seat_get_pointer(&seat->base);
2310 if (pointer)
2311 weston_pointer_clamp(pointer,
2312 &pointer->x,
2313 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002314 }
2315}
2316
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002317static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002318parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002319{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002320 int ret = 0;
2321
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002322 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002323 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002324 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002325 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002326 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002327 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002328 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002329 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002330 else {
2331 weston_log("fatal: unrecognized pixel format: %s\n", s);
2332 ret = -1;
2333 }
2334
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002335 return ret;
2336}
2337
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002338/**
2339 * Choose suitable mode for an output
2340 *
2341 * Find the most suitable mode to use for initial setup (or reconfiguration on
2342 * hotplug etc) for a DRM output.
2343 *
2344 * @param output DRM output to choose mode for
2345 * @param kind Strategy and preference to use when choosing mode
2346 * @param width Desired width for this output
2347 * @param height Desired height for this output
2348 * @param current_mode Mode currently being displayed on this output
2349 * @param modeline Manually-entered mode (may be NULL)
2350 * @returns A mode from the output's mode list, or NULL if none available
2351 */
2352static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002353drm_output_choose_initial_mode(struct drm_backend *backend,
2354 struct drm_output *output,
2355 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002356 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002357 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002358{
2359 struct drm_mode *preferred = NULL;
2360 struct drm_mode *current = NULL;
2361 struct drm_mode *configured = NULL;
2362 struct drm_mode *best = NULL;
2363 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002364 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002365 int32_t width = 0;
2366 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002367 uint32_t refresh = 0;
2368 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002369
Armin Krezović08368132016-09-30 14:11:05 +02002370 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002371 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2372 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002373 width = -1;
2374
Armin Krezović08368132016-09-30 14:11:05 +02002375 if (parse_modeline(modeline, &drm_modeline) == 0) {
2376 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002377 if (!configured)
2378 return NULL;
2379 } else {
2380 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002381 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002382 }
2383 }
2384 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002385
2386 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002387 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002388 height == drm_mode->base.height &&
2389 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002390 configured = drm_mode;
2391
comic fans7a5c5622016-03-17 14:29:27 +02002392 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002393 sizeof *current_mode) == 0)
2394 current = drm_mode;
2395
2396 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2397 preferred = drm_mode;
2398
2399 best = drm_mode;
2400 }
2401
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002402 if (current == NULL && current_mode->clock != 0) {
2403 current = drm_output_add_mode(output, current_mode);
2404 if (!current)
2405 return NULL;
2406 }
2407
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002408 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002409 configured = current;
2410
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002411 if (configured)
2412 return configured;
2413
2414 if (preferred)
2415 return preferred;
2416
2417 if (current)
2418 return current;
2419
2420 if (best)
2421 return best;
2422
2423 weston_log("no available modes for %s\n", output->base.name);
2424 return NULL;
2425}
2426
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002427static int
2428connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2429 drmModeModeInfo *mode)
2430{
2431 drmModeEncoder *encoder;
2432 drmModeCrtc *crtc;
2433
2434 /* Get the current mode on the crtc that's currently driving
2435 * this connector. */
2436 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2437 memset(mode, 0, sizeof *mode);
2438 if (encoder != NULL) {
2439 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2440 drmModeFreeEncoder(encoder);
2441 if (crtc == NULL)
2442 return -1;
2443 if (crtc->mode_valid)
2444 *mode = crtc->mode;
2445 drmModeFreeCrtc(crtc);
2446 }
2447
2448 return 0;
2449}
2450
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002451static int
Armin Krezović08368132016-09-30 14:11:05 +02002452drm_output_set_mode(struct weston_output *base,
2453 enum weston_drm_backend_output_mode mode,
2454 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002455{
Armin Krezović08368132016-09-30 14:11:05 +02002456 struct drm_output *output = to_drm_output(base);
2457 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002458
Armin Krezović445b41b2016-10-09 23:48:16 +02002459 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002460 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002461
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002462 output->base.make = "unknown";
2463 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002464 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002465
Armin Krezović08368132016-09-30 14:11:05 +02002466 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002467 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002468
Armin Krezović08368132016-09-30 14:11:05 +02002469 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002470 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002471 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002472
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002473 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002474 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002475
Armin Krezović08368132016-09-30 14:11:05 +02002476 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2477 output->base.native_mode = output->base.current_mode;
2478 output->base.native_scale = output->base.current_scale;
2479
2480 output->base.mm_width = output->connector->mmWidth;
2481 output->base.mm_height = output->connector->mmHeight;
2482
2483 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02002484}
2485
2486static void
2487drm_output_set_gbm_format(struct weston_output *base,
2488 const char *gbm_format)
2489{
2490 struct drm_output *output = to_drm_output(base);
2491 struct drm_backend *b = to_drm_backend(base->compositor);
2492
2493 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2494 output->gbm_format = b->gbm_format;
2495}
2496
2497static void
2498drm_output_set_seat(struct weston_output *base,
2499 const char *seat)
2500{
2501 struct drm_output *output = to_drm_output(base);
2502 struct drm_backend *b = to_drm_backend(base->compositor);
2503
2504 setup_output_seat_constraint(b, &output->base,
2505 seat ? seat : "");
2506}
2507
2508static int
2509drm_output_enable(struct weston_output *base)
2510{
2511 struct drm_output *output = to_drm_output(base);
2512 struct drm_backend *b = to_drm_backend(base->compositor);
2513 struct weston_mode *m;
2514
2515 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002516
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002517 if (b->pageflip_timeout)
2518 drm_output_pageflip_timer_create(output);
2519
Giulio Camuffo954f1832014-10-11 18:27:30 +03002520 if (b->use_pixman) {
2521 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002522 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002523 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002524 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002526 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002527 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002528 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002529
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002530 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002531 weston_log("Initialized backlight, device %s\n",
2532 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002533 output->base.set_backlight = drm_set_backlight;
2534 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002535 } else {
2536 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002537 }
2538
Jonas Ådahle5a12252013-04-05 23:07:11 +02002539 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002540 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002541 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002542 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002543 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002544
Richard Hughese7299962013-05-01 21:52:12 +01002545 output->base.gamma_size = output->original_crtc->gamma_size;
2546 output->base.set_gamma = drm_output_set_gamma;
2547
Armin Krezović08368132016-09-30 14:11:05 +02002548 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2549
2550 find_and_parse_output_edid(b, output, output->connector);
Armin Krezović9bcf4c52017-02-08 13:55:26 +01002551 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2552 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Armin Krezović08368132016-09-30 14:11:05 +02002553 output->base.connection_internal = 1;
2554
Derek Foremanb0427562016-02-05 15:55:20 -06002555 weston_plane_init(&output->cursor_plane, b->compositor,
2556 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002557 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002558
Giulio Camuffo954f1832014-10-11 18:27:30 +03002559 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2560 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2561 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002562
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002563 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002564 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002565 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002566 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002567 m->width, m->height, m->refresh / 1000.0,
2568 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2569 ", preferred" : "",
2570 m->flags & WL_OUTPUT_MODE_CURRENT ?
2571 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002572 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002573 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002574
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002575 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002576
2577err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002578 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002579
David Herrmann0f0d54e2011-12-08 17:05:45 +01002580 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002581}
2582
Jesse Barnes58ef3792012-02-23 09:45:49 -05002583static void
Armin Krezović08368132016-09-30 14:11:05 +02002584drm_output_deinit(struct weston_output *base)
2585{
2586 struct drm_output *output = to_drm_output(base);
2587 struct drm_backend *b = to_drm_backend(base->compositor);
2588
Daniel Stone6e7a9612017-04-04 17:54:26 +01002589 /* output->next must not be set here;
2590 * destroy_pending/disable_pending exist to guarantee exactly this. */
2591 assert(!output->next);
2592 drm_fb_unref(output->current);
2593 output->current = NULL;
2594
Daniel Stone3e661f72016-11-04 17:24:06 +00002595 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002596 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002597 else
2598 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002599
2600 weston_plane_release(&output->fb_plane);
2601 weston_plane_release(&output->cursor_plane);
2602
2603 drmModeFreeProperty(output->dpms_prop);
2604
2605 /* Turn off hardware cursor */
2606 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2607}
2608
2609static void
2610drm_output_destroy(struct weston_output *base)
2611{
2612 struct drm_output *output = to_drm_output(base);
2613 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002614 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02002615 drmModeCrtcPtr origcrtc = output->original_crtc;
2616
2617 if (output->page_flip_pending) {
2618 output->destroy_pending = 1;
2619 weston_log("destroy output while page flip pending\n");
2620 return;
2621 }
2622
2623 if (output->base.enabled)
2624 drm_output_deinit(&output->base);
2625
Armin Krezović445b41b2016-10-09 23:48:16 +02002626 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2627 base.link) {
2628 wl_list_remove(&drm_mode->base.link);
2629 free(drm_mode);
2630 }
2631
Armin Krezović08368132016-09-30 14:11:05 +02002632 if (origcrtc) {
2633 /* Restore original CRTC state */
2634 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2635 origcrtc->x, origcrtc->y,
2636 &output->connector_id, 1, &origcrtc->mode);
2637 drmModeFreeCrtc(origcrtc);
2638 }
2639
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002640 if (output->pageflip_timer)
2641 wl_event_source_remove(output->pageflip_timer);
2642
Armin Krezović08368132016-09-30 14:11:05 +02002643 weston_output_destroy(&output->base);
2644
2645 drmModeFreeConnector(output->connector);
2646
2647 if (output->backlight)
2648 backlight_destroy(output->backlight);
2649
Armin Krezović08368132016-09-30 14:11:05 +02002650 free(output);
2651}
2652
2653static int
2654drm_output_disable(struct weston_output *base)
2655{
2656 struct drm_output *output = to_drm_output(base);
2657 struct drm_backend *b = to_drm_backend(base->compositor);
2658
2659 if (output->page_flip_pending) {
2660 output->disable_pending = 1;
2661 return -1;
2662 }
2663
2664 if (output->base.enabled)
2665 drm_output_deinit(&output->base);
2666
2667 output->disable_pending = 0;
2668
2669 weston_log("Disabling output %s\n", output->base.name);
2670 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2671 0, 0, 0, 0, 0, NULL);
2672
2673 return 0;
2674}
2675
2676/**
2677 * Create a Weston output structure
2678 *
2679 * Given a DRM connector, create a matching drm_output structure and add it
2680 * to Weston's output list. It also takes ownership of the connector, which
2681 * is released when output is destroyed.
2682 *
2683 * @param b Weston backend structure
2684 * @param resources DRM resources for this device
2685 * @param connector DRM connector to use for this new output
2686 * @param drm_device udev device pointer
2687 * @returns 0 on success, or -1 on failure
2688 */
2689static int
2690create_output_for_connector(struct drm_backend *b,
2691 drmModeRes *resources,
2692 drmModeConnector *connector,
2693 struct udev_device *drm_device)
2694{
2695 struct drm_output *output;
Armin Krezović445b41b2016-10-09 23:48:16 +02002696 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002697 int i;
2698
2699 i = find_crtc_for_connector(b, resources, connector);
2700 if (i < 0) {
2701 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02002702 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002703 }
2704
2705 output = zalloc(sizeof *output);
2706 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02002707 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002708
2709 output->connector = connector;
2710 output->crtc_id = resources->crtcs[i];
2711 output->pipe = i;
2712 output->connector_id = connector->connector_id;
2713
2714 output->backlight = backlight_init(drm_device,
2715 connector->connector_type);
2716
Armin Krezović445b41b2016-10-09 23:48:16 +02002717 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2718
Armin Krezović08368132016-09-30 14:11:05 +02002719 output->base.enable = drm_output_enable;
2720 output->base.destroy = drm_output_destroy;
2721 output->base.disable = drm_output_disable;
2722 output->base.name = make_connector_name(connector);
2723
2724 output->destroy_pending = 0;
2725 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02002726
Armin Krezović40087402016-09-30 14:11:12 +02002727 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002728
2729 wl_list_init(&output->base.mode_list);
2730
2731 for (i = 0; i < output->connector->count_modes; i++) {
2732 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
2733 if (!drm_mode) {
2734 drm_output_destroy(&output->base);
2735 return -1;
2736 }
2737 }
2738
Armin Krezović08368132016-09-30 14:11:05 +02002739 weston_compositor_add_pending_output(&output->base, b->compositor);
2740
2741 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02002742
2743err:
2744 drmModeFreeConnector(connector);
2745
2746 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002747}
2748
2749static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002750create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002751{
2752 struct drm_sprite *sprite;
2753 drmModePlaneRes *plane_res;
2754 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002755 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002756
Giulio Camuffo954f1832014-10-11 18:27:30 +03002757 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002758 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002759 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002760 strerror(errno));
2761 return;
2762 }
2763
2764 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002765 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002766 if (!plane)
2767 continue;
2768
Peter Huttererf3d62272013-08-08 11:57:05 +10002769 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002770 plane->count_formats));
2771 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002772 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002773 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002774 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002775 continue;
2776 }
2777
Jesse Barnes58ef3792012-02-23 09:45:49 -05002778 sprite->possible_crtcs = plane->possible_crtcs;
2779 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002780 sprite->current = NULL;
2781 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002782 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002783 sprite->count_formats = plane->count_formats;
2784 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002785 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002786 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2788 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2789 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002790
Giulio Camuffo954f1832014-10-11 18:27:30 +03002791 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002792 }
2793
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002794 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002795}
2796
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002797static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002798destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002799{
2800 struct drm_sprite *sprite, *next;
2801 struct drm_output *output;
2802
Giulio Camuffo954f1832014-10-11 18:27:30 +03002803 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002804 struct drm_output, base.link);
2805
Giulio Camuffo954f1832014-10-11 18:27:30 +03002806 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2807 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002808 sprite->plane_id,
2809 output->crtc_id, 0, 0,
2810 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone05a5ac22017-04-04 17:54:25 +01002811 drm_fb_unref(sprite->current);
2812 drm_fb_unref(sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002813 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002814 free(sprite);
2815 }
2816}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002817
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002818static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002819create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002820{
2821 drmModeConnector *connector;
2822 drmModeRes *resources;
2823 int i;
2824
Giulio Camuffo954f1832014-10-11 18:27:30 +03002825 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002826 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002827 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002828 return -1;
2829 }
2830
Giulio Camuffo954f1832014-10-11 18:27:30 +03002831 b->min_width = resources->min_width;
2832 b->max_width = resources->max_width;
2833 b->min_height = resources->min_height;
2834 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002835
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002836 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002837 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002838 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002839 if (connector == NULL)
2840 continue;
2841
2842 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002843 (b->connector == 0 ||
2844 connector->connector_id == b->connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002845 if (create_output_for_connector(b, resources,
Armin Krezović445b41b2016-10-09 23:48:16 +02002846 connector, drm_device) < 0)
Benjamin Franzke439d9862011-10-07 08:20:53 +02002847 continue;
Armin Krezović08368132016-09-30 14:11:05 +02002848 } else {
2849 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002850 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002851 }
2852
Armin Krezović08368132016-09-30 14:11:05 +02002853 if (wl_list_empty(&b->compositor->output_list) &&
2854 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002855 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002856
2857 drmModeFreeResources(resources);
2858
2859 return 0;
2860}
2861
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002862static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002863update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002864{
2865 drmModeConnector *connector;
2866 drmModeRes *resources;
2867 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002868 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002869 int i;
2870
Giulio Camuffo954f1832014-10-11 18:27:30 +03002871 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002872 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002873 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002874 return;
2875 }
2876
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002877 connected = calloc(resources->count_connectors, sizeof(uint32_t));
2878 if (!connected) {
2879 drmModeFreeResources(resources);
2880 return;
2881 }
2882
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002883 /* collect new connects */
2884 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002885 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002886
Giulio Camuffo954f1832014-10-11 18:27:30 +03002887 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002888 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002889 continue;
2890
David Herrmann7551cff2011-12-08 17:05:43 +01002891 if (connector->connection != DRM_MODE_CONNECTED) {
2892 drmModeFreeConnector(connector);
2893 continue;
2894 }
2895
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002896 if (b->connector && (b->connector != connector_id)) {
2897 drmModeFreeConnector(connector);
2898 continue;
2899 }
2900
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002901 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002902
Daniel Stonec0ec7592017-02-09 13:58:35 +00002903 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02002904 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00002905 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002906 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00002907
2908 create_output_for_connector(b, resources,
2909 connector, drm_device);
2910 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002911 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002912
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002913 wl_list_for_each_safe(output, next, &b->compositor->output_list,
2914 base.link) {
2915 bool disconnected = true;
2916
2917 for (i = 0; i < resources->count_connectors; i++) {
2918 if (connected[i] == output->connector_id) {
2919 disconnected = false;
2920 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002921 }
2922 }
Armin Krezović08368132016-09-30 14:11:05 +02002923
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002924 if (!disconnected)
2925 continue;
2926
2927 weston_log("connector %d disconnected\n", output->connector_id);
2928 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002929 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002930
2931 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2932 base.link) {
2933 bool disconnected = true;
2934
2935 for (i = 0; i < resources->count_connectors; i++) {
2936 if (connected[i] == output->connector_id) {
2937 disconnected = false;
2938 break;
2939 }
2940 }
2941
2942 if (!disconnected)
2943 continue;
2944
2945 weston_log("connector %d disconnected\n", output->connector_id);
2946 drm_output_destroy(&output->base);
2947 }
2948
2949 free(connected);
2950 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002951}
2952
2953static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002954udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002955{
David Herrmannd7488c22012-03-11 20:05:21 +01002956 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002957 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002958
2959 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002960 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002961 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002962
David Herrmann6ac52db2012-03-11 20:05:22 +01002963 val = udev_device_get_property_value(device, "HOTPLUG");
2964 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002965 return 0;
2966
David Herrmann6ac52db2012-03-11 20:05:22 +01002967 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002968}
2969
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002970static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002971udev_drm_event(int fd, uint32_t mask, void *data)
2972{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002973 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002974 struct udev_device *event;
2975
Giulio Camuffo954f1832014-10-11 18:27:30 +03002976 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002977
Giulio Camuffo954f1832014-10-11 18:27:30 +03002978 if (udev_event_is_hotplug(b, event))
2979 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002980
2981 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002982
2983 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002984}
2985
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002986static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002987drm_restore(struct weston_compositor *ec)
2988{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002989 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002990}
2991
2992static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002993drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002994{
Armin Krezović545dba62016-08-05 15:54:18 +02002995 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002996
Giulio Camuffo954f1832014-10-11 18:27:30 +03002997 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002998
Giulio Camuffo954f1832014-10-11 18:27:30 +03002999 wl_event_source_remove(b->udev_drm_source);
3000 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003001
Giulio Camuffo954f1832014-10-11 18:27:30 +03003002 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003003
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003004 weston_compositor_shutdown(ec);
3005
Giulio Camuffo954f1832014-10-11 18:27:30 +03003006 if (b->gbm)
3007 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003008
Giulio Camuffo954f1832014-10-11 18:27:30 +03003009 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003010
Giulio Camuffo954f1832014-10-11 18:27:30 +03003011 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003012 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003013}
3014
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003015static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003016session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003017{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003018 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003019 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003020 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003021 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003022
Giulio Camuffo954f1832014-10-11 18:27:30 +03003023 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003024 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003025 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003026 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003027 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003028 } else {
3029 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003031
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003032 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003033
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003034 /* If we have a repaint scheduled (either from a
3035 * pending pageflip or the idle handler), make sure we
3036 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003037 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003038 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003039 * back, we schedule a repaint, which will process
3040 * pending frame callbacks. */
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003043 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003044 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003045 }
3046
Giulio Camuffo954f1832014-10-11 18:27:30 +03003047 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003048 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003049
Giulio Camuffo954f1832014-10-11 18:27:30 +03003050 wl_list_for_each(sprite, &b->sprite_list, link)
3051 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003052 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003053 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003054 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003055 };
3056}
3057
Daniel Stoneefa504f2016-12-19 16:48:20 +00003058/**
3059 * Determines whether or not a device is capable of modesetting. If successful,
3060 * sets b->drm.fd and b->drm.filename to the opened device.
3061 */
3062static bool
3063drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3064{
3065 const char *filename = udev_device_get_devnode(device);
3066 const char *sysnum = udev_device_get_sysnum(device);
3067 drmModeRes *res;
3068 int id, fd;
3069
3070 if (!filename)
3071 return false;
3072
3073 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3074 if (fd < 0)
3075 return false;
3076
3077 res = drmModeGetResources(fd);
3078 if (!res)
3079 goto out_fd;
3080
3081 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3082 res->count_encoders <= 0)
3083 goto out_res;
3084
3085 if (sysnum)
3086 id = atoi(sysnum);
3087 if (!sysnum || id < 0) {
3088 weston_log("couldn't get sysnum for device %s\n", filename);
3089 goto out_res;
3090 }
3091
3092 /* We can be called successfully on multiple devices; if we have,
3093 * clean up old entries. */
3094 if (b->drm.fd >= 0)
3095 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3096 free(b->drm.filename);
3097
3098 b->drm.fd = fd;
3099 b->drm.id = id;
3100 b->drm.filename = strdup(filename);
3101
Sergi Granellceb59812017-03-28 12:44:04 +02003102 drmModeFreeResources(res);
3103
Daniel Stoneefa504f2016-12-19 16:48:20 +00003104 return true;
3105
3106out_res:
3107 drmModeFreeResources(res);
3108out_fd:
3109 weston_launcher_close(b->compositor->launcher, fd);
3110 return false;
3111}
3112
David Herrmann0af066f2012-10-29 19:21:16 +01003113/*
3114 * Find primary GPU
3115 * Some systems may have multiple DRM devices attached to a single seat. This
3116 * function loops over all devices and tries to find a PCI device with the
3117 * boot_vga sysfs attribute set to 1.
3118 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003119 * Devices are also vetted to make sure they are are capable of modesetting,
3120 * rather than pure render nodes (GPU with no display), or pure
3121 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003122 */
3123static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003124find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003125{
3126 struct udev_enumerate *e;
3127 struct udev_list_entry *entry;
3128 const char *path, *device_seat, *id;
3129 struct udev_device *device, *drm_device, *pci;
3130
Giulio Camuffo954f1832014-10-11 18:27:30 +03003131 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003132 udev_enumerate_add_match_subsystem(e, "drm");
3133 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3134
3135 udev_enumerate_scan_devices(e);
3136 drm_device = NULL;
3137 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003138 bool is_boot_vga = false;
3139
David Herrmann0af066f2012-10-29 19:21:16 +01003140 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003142 if (!device)
3143 continue;
3144 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3145 if (!device_seat)
3146 device_seat = default_seat;
3147 if (strcmp(device_seat, seat)) {
3148 udev_device_unref(device);
3149 continue;
3150 }
3151
3152 pci = udev_device_get_parent_with_subsystem_devtype(device,
3153 "pci", NULL);
3154 if (pci) {
3155 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003156 if (id && !strcmp(id, "1"))
3157 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003158 }
3159
Daniel Stoneefa504f2016-12-19 16:48:20 +00003160 /* If we already have a modesetting-capable device, and this
3161 * device isn't our boot-VGA device, we aren't going to use
3162 * it. */
3163 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003164 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003165 continue;
3166 }
3167
3168 /* Make sure this device is actually capable of modesetting;
3169 * if this call succeeds, b->drm.{fd,filename} will be set,
3170 * and any old values freed. */
3171 if (!drm_device_is_kms(b, device)) {
3172 udev_device_unref(device);
3173 continue;
3174 }
3175
3176 /* There can only be one boot_vga device, and we try to use it
3177 * at all costs. */
3178 if (is_boot_vga) {
3179 if (drm_device)
3180 udev_device_unref(drm_device);
3181 drm_device = device;
3182 break;
3183 }
3184
3185 /* Per the (!is_boot_vga && drm_device) test above, we only
3186 * trump existing saved devices with boot-VGA devices, so if
3187 * we end up here, this must be the first device we've seen. */
3188 assert(!drm_device);
3189 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003190 }
3191
Daniel Stoneefa504f2016-12-19 16:48:20 +00003192 /* If we're returning a device to use, we must have an open FD for
3193 * it. */
3194 assert(!!drm_device == (b->drm.fd >= 0));
3195
David Herrmann0af066f2012-10-29 19:21:16 +01003196 udev_enumerate_unref(e);
3197 return drm_device;
3198}
3199
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003200static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003201planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3202 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003203{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003205
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003206 switch (key) {
3207 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003208 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003209 break;
3210 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003211 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003212 break;
3213 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003214 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003215 break;
3216 default:
3217 break;
3218 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003219}
3220
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003221#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003222static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003223recorder_destroy(struct drm_output *output)
3224{
3225 vaapi_recorder_destroy(output->recorder);
3226 output->recorder = NULL;
3227
3228 output->base.disable_planes--;
3229
3230 wl_list_remove(&output->recorder_frame_listener.link);
3231 weston_log("[libva recorder] done\n");
3232}
3233
3234static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003235recorder_frame_notify(struct wl_listener *listener, void *data)
3236{
3237 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003238 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003239 int fd, ret;
3240
3241 output = container_of(listener, struct drm_output,
3242 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003243 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003244
3245 if (!output->recorder)
3246 return;
3247
Giulio Camuffo954f1832014-10-11 18:27:30 +03003248 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003249 DRM_CLOEXEC, &fd);
3250 if (ret) {
3251 weston_log("[libva recorder] "
3252 "failed to create prime fd for front buffer\n");
3253 return;
3254 }
3255
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003256 ret = vaapi_recorder_frame(output->recorder, fd,
3257 output->current->stride);
3258 if (ret < 0) {
3259 weston_log("[libva recorder] aborted: %m\n");
3260 recorder_destroy(output);
3261 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003262}
3263
3264static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003265create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003266 const char *filename)
3267{
3268 int fd;
3269 drm_magic_t magic;
3270
Giulio Camuffo954f1832014-10-11 18:27:30 +03003271 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003272 if (fd < 0)
3273 return NULL;
3274
3275 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003276 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003277
3278 return vaapi_recorder_create(fd, width, height, filename);
3279}
3280
3281static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003282recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003283 void *data)
3284{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003285 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003286 struct drm_output *output;
3287 int width, height;
3288
Giulio Camuffo954f1832014-10-11 18:27:30 +03003289 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003290 struct drm_output, base.link);
3291
3292 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003293 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003294 weston_log("failed to start vaapi recorder: "
3295 "output format not supported\n");
3296 return;
3297 }
3298
Hardeningff39efa2013-09-18 23:56:35 +02003299 width = output->base.current_mode->width;
3300 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003301
3302 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003303 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003304 if (!output->recorder) {
3305 weston_log("failed to create vaapi recorder\n");
3306 return;
3307 }
3308
3309 output->base.disable_planes++;
3310
3311 output->recorder_frame_listener.notify = recorder_frame_notify;
3312 wl_signal_add(&output->base.frame_signal,
3313 &output->recorder_frame_listener);
3314
3315 weston_output_schedule_repaint(&output->base);
3316
3317 weston_log("[libva recorder] initialized\n");
3318 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003319 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003320 }
3321}
3322#else
3323static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003324recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003325 void *data)
3326{
3327 weston_log("Compiled without libva support\n");
3328}
3329#endif
3330
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003331static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003332switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003333{
3334 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003335 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003336
Giulio Camuffo954f1832014-10-11 18:27:30 +03003337 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003338 return;
3339
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003340 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3341
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003342 weston_log("Switching to GL renderer\n");
3343
Giulio Camuffo954f1832014-10-11 18:27:30 +03003344 b->gbm = create_gbm_device(b->drm.fd);
3345 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003346 weston_log("Failed to create gbm device. "
3347 "Aborting renderer switch\n");
3348 return;
3349 }
3350
Giulio Camuffo954f1832014-10-11 18:27:30 +03003351 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003352 pixman_renderer_output_destroy(&output->base);
3353
Giulio Camuffo954f1832014-10-11 18:27:30 +03003354 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003355
Giulio Camuffo954f1832014-10-11 18:27:30 +03003356 if (drm_backend_create_gl_renderer(b) < 0) {
3357 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003358 weston_log("Failed to create GL renderer. Quitting.\n");
3359 /* FIXME: we need a function to shutdown cleanly */
3360 assert(0);
3361 }
3362
Giulio Camuffo954f1832014-10-11 18:27:30 +03003363 wl_list_for_each(output, &b->compositor->output_list, base.link)
3364 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003365
Giulio Camuffo954f1832014-10-11 18:27:30 +03003366 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003367
3368 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3369 if (linux_dmabuf_setup(b->compositor) < 0)
3370 weston_log("Error: initializing dmabuf "
3371 "support failed.\n");
3372 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003373}
3374
3375static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003376renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3377 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003378{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003379 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003380 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003381
Giulio Camuffo954f1832014-10-11 18:27:30 +03003382 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003383}
3384
Armin Krezović08368132016-09-30 14:11:05 +02003385static const struct weston_drm_output_api api = {
3386 drm_output_set_mode,
3387 drm_output_set_gbm_format,
3388 drm_output_set_seat,
3389};
3390
Giulio Camuffo954f1832014-10-11 18:27:30 +03003391static struct drm_backend *
3392drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003393 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003394{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003395 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003396 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003397 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003398 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003399 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003400
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003401 weston_log("initializing drm backend\n");
3402
Giulio Camuffo954f1832014-10-11 18:27:30 +03003403 b = zalloc(sizeof *b);
3404 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003405 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003406
Daniel Stoneefa504f2016-12-19 16:48:20 +00003407 b->drm.fd = -1;
3408
Pekka Paalanen68583832015-05-19 09:53:16 +03003409 /*
3410 * KMS support for hardware planes cannot properly synchronize
3411 * without nuclear page flip. Without nuclear/atomic, hw plane
3412 * and cursor plane updates would either tear or cause extra
3413 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003414 * to a fraction. For cursors, it's not so bad, so they are
3415 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003416 *
3417 * These can be enabled again when nuclear/atomic support lands.
3418 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003419 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003420 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003421 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003422 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003423
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003424 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3425 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003426
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003427 if (config->seat_id)
3428 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003429
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003430 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003431 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3432 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003433 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003434 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003435 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003436 goto err_compositor;
3437 }
3438
Giulio Camuffo954f1832014-10-11 18:27:30 +03003439 b->udev = udev_new();
3440 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003441 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003442 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003443 }
3444
Giulio Camuffo954f1832014-10-11 18:27:30 +03003445 b->session_listener.notify = session_notify;
3446 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003447
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003448 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003449 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003450 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003451 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003452 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003453
Daniel Stoneefa504f2016-12-19 16:48:20 +00003454 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003455 weston_log("failed to initialize kms\n");
3456 goto err_udev_dev;
3457 }
3458
Giulio Camuffo954f1832014-10-11 18:27:30 +03003459 if (b->use_pixman) {
3460 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003461 weston_log("failed to initialize pixman renderer\n");
3462 goto err_udev_dev;
3463 }
3464 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003465 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003466 weston_log("failed to initialize egl\n");
3467 goto err_udev_dev;
3468 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003469 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003470
Giulio Camuffo954f1832014-10-11 18:27:30 +03003471 b->base.destroy = drm_destroy;
3472 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003473
Bob Ham91880f12016-01-12 10:21:47 +00003474 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003475
Giulio Camuffo954f1832014-10-11 18:27:30 +03003476 wl_list_init(&b->sprite_list);
3477 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003478
Giulio Camuffo954f1832014-10-11 18:27:30 +03003479 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003480 compositor, b->udev, seat_id,
3481 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003482 weston_log("failed to create input devices\n");
3483 goto err_sprite;
3484 }
3485
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003486 b->connector = config->connector;
3487
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003488 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003489 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003490 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003491 }
3492
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003493 /* A this point we have some idea of whether or not we have a working
3494 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003495 if (!b->cursors_are_broken)
3496 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003497
Giulio Camuffo954f1832014-10-11 18:27:30 +03003498 loop = wl_display_get_event_loop(compositor->wl_display);
3499 b->drm_source =
3500 wl_event_loop_add_fd(loop, b->drm.fd,
3501 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003502
Giulio Camuffo954f1832014-10-11 18:27:30 +03003503 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3504 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003505 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003506 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003507 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003508 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003509 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003510 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003511 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003512 udev_monitor_get_fd(b->udev_monitor),
3513 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003514
Giulio Camuffo954f1832014-10-11 18:27:30 +03003515 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003516 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003517 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003518 }
3519
Daniel Stonea96b93c2012-06-22 14:04:37 +01003520 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003521
Giulio Camuffo954f1832014-10-11 18:27:30 +03003522 weston_compositor_add_debug_binding(compositor, KEY_O,
3523 planes_binding, b);
3524 weston_compositor_add_debug_binding(compositor, KEY_C,
3525 planes_binding, b);
3526 weston_compositor_add_debug_binding(compositor, KEY_V,
3527 planes_binding, b);
3528 weston_compositor_add_debug_binding(compositor, KEY_Q,
3529 recorder_binding, b);
3530 weston_compositor_add_debug_binding(compositor, KEY_W,
3531 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003532
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003533 if (compositor->renderer->import_dmabuf) {
3534 if (linux_dmabuf_setup(compositor) < 0)
3535 weston_log("Error: initializing dmabuf "
3536 "support failed.\n");
3537 }
3538
Giulio Camuffo954f1832014-10-11 18:27:30 +03003539 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003540
Armin Krezović08368132016-09-30 14:11:05 +02003541 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3542 &api, sizeof(api));
3543
3544 if (ret < 0) {
3545 weston_log("Failed to register output API.\n");
3546 goto err_udev_monitor;
3547 }
3548
Giulio Camuffo954f1832014-10-11 18:27:30 +03003549 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003550
3551err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003552 wl_event_source_remove(b->udev_drm_source);
3553 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003554err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003555 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003556err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003557 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003558err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003559 if (b->gbm)
3560 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003561 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003562err_udev_dev:
3563 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003564err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003565 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003566err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003567 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003568err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003569 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003570 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003571 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003572}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003573
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003574static void
3575config_init_to_defaults(struct weston_drm_backend_config *config)
3576{
3577}
3578
Giulio Camuffo954f1832014-10-11 18:27:30 +03003579WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003580weston_backend_init(struct weston_compositor *compositor,
3581 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003582{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003583 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003584 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003585
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003586 if (config_base == NULL ||
3587 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3588 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3589 weston_log("drm backend config structure is invalid\n");
3590 return -1;
3591 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003592
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003593 config_init_to_defaults(&config);
3594 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003595
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003596 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003597 if (b == NULL)
3598 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003599
Giulio Camuffo954f1832014-10-11 18:27:30 +03003600 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003601}