blob: b707a01fc4829a6db0e8eb699cbe8e008af732a2 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010057#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100059#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010060#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030061#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030063#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050064#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040065
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030066#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
68#endif
69
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030070#ifndef DRM_CAP_CURSOR_WIDTH
71#define DRM_CAP_CURSOR_WIDTH 0x8
72#endif
73
74#ifndef DRM_CAP_CURSOR_HEIGHT
75#define DRM_CAP_CURSOR_HEIGHT 0x9
76#endif
77
78#ifndef GBM_BO_USE_CURSOR
79#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
80#endif
81
Giulio Camuffo954f1832014-10-11 18:27:30 +030082struct drm_backend {
83 struct weston_backend base;
84 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085
86 struct udev *udev;
87 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010089 struct udev_monitor *udev_monitor;
90 struct wl_event_source *udev_drm_source;
91
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010093 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010094 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030095 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010096 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020097 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000105 int min_width, max_width;
106 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Rob Bradfordd355b802013-05-31 18:09:55 +0100117 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300118
Daniel Stone70d337d2015-06-16 18:42:23 +0100119 int32_t cursor_width;
120 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000121
122 uint32_t connector;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000123 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400124};
125
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400126struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400128 drmModeModeInfo mode_info;
129};
130
Daniel Stonefc175a72017-04-04 17:54:22 +0100131enum drm_fb_type {
132 BUFFER_INVALID = 0, /**< never used */
133 BUFFER_CLIENT, /**< directly sourced from client */
134 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
135 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100136 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100137};
138
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300139struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100140 enum drm_fb_type type;
141
Daniel Stone6e7a9612017-04-04 17:54:26 +0100142 int refcnt;
143
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200144 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100145 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000146 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200147 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200148 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200149
150 /* Used by gbm fbs */
151 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100152 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200153
154 /* Used by dumb fbs */
155 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300156};
157
Richard Hughes2b2092a2013-04-24 14:58:02 +0100158struct drm_edid {
159 char eisa_id[13];
160 char monitor_name[13];
161 char pnp_id[5];
162 char serial_number[13];
163};
164
Daniel Stone08d4edf2017-04-04 17:54:34 +0100165/**
166 * A plane represents one buffer, positioned within a CRTC, and stacked
167 * relative to other planes on the same CRTC.
168 *
169 * Each CRTC has a 'primary plane', which use used to display the classic
170 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
171 * call (which combines setting the CRTC's actual physical mode, and the
172 * properties of the primary plane).
173 *
174 * The cursor plane also has its own alternate legacy API.
175 *
176 * Other planes are used opportunistically to display content we do not
177 * wish to blit into the primary plane. These non-primary/cursor planes
178 * are referred to as 'sprites'.
179 */
180struct drm_plane {
181 struct wl_list link;
182
183 struct weston_plane base;
184
185 struct drm_output *output;
186 struct drm_backend *backend;
187
188 uint32_t possible_crtcs;
189 uint32_t plane_id;
190 uint32_t count_formats;
191
192 /* The last framebuffer submitted to the kernel for this plane. */
193 struct drm_fb *fb_current;
194 /* The previously-submitted framebuffer, where the hardware has not
195 * yet acknowledged display of fb_current. */
196 struct drm_fb *fb_last;
197 /* Framebuffer we are going to submit to the kernel when the current
198 * repaint is flushed. */
199 struct drm_fb *fb_pending;
200
201 int32_t src_x, src_y;
202 uint32_t src_w, src_h;
203 uint32_t dest_x, dest_y;
204 uint32_t dest_w, dest_h;
205
206 uint32_t formats[];
207};
208
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400209struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200210 struct weston_output base;
211 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400212
Daniel Stone17339232015-11-28 12:09:47 +0000213 uint32_t crtc_id; /* object ID to pass to DRM functions */
214 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400215 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700216 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100217 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300218 drmModePropertyPtr dpms_prop;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200219
Daniel Stone36609c72015-06-18 07:49:02 +0100220 enum dpms_enum dpms;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100221 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100222
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300223 bool state_invalid;
224
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300225 int vblank_pending;
226 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800227 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200228 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300229
Daniel Stonee4256832017-04-04 17:54:27 +0100230 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400231 struct weston_plane cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500232 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400233 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100234
235 struct gbm_surface *gbm_surface;
236 uint32_t gbm_format;
237
Daniel Stone4202e502016-10-24 13:26:42 +0100238 /* Plane for a fullscreen direct scanout view */
239 struct weston_plane scanout_plane;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100240
241 /* The last framebuffer submitted to the kernel for this CRTC. */
242 struct drm_fb *fb_current;
243 /* The previously-submitted framebuffer, where the hardware has not
244 * yet acknowledged display of fb_current. */
245 struct drm_fb *fb_last;
246 /* Framebuffer we are going to submit to the kernel when the current
247 * repaint is flushed. */
248 struct drm_fb *fb_pending;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200249
250 struct drm_fb *dumb[2];
251 pixman_image_t *image[2];
252 int current_image;
253 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300254
255 struct vaapi_recorder *recorder;
256 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000257
258 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400259};
260
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300261static struct gl_renderer_interface *gl_renderer;
262
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500263static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400264
Armin Krezović545dba62016-08-05 15:54:18 +0200265static inline struct drm_output *
266to_drm_output(struct weston_output *base)
267{
268 return container_of(base, struct drm_output, base);
269}
270
271static inline struct drm_backend *
272to_drm_backend(struct weston_compositor *base)
273{
274 return container_of(base->backend, struct drm_backend, base);
275}
276
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000277static int
278pageflip_timeout(void *data) {
279 /*
280 * Our timer just went off, that means we're not receiving drm
281 * page flip events anymore for that output. Let's gracefully exit
282 * weston with a return value so devs can debug what's going on.
283 */
284 struct drm_output *output = data;
285 struct weston_compositor *compositor = output->base.compositor;
286
287 weston_log("Pageflip timeout reached on output %s, your "
288 "driver is probably buggy! Exiting.\n",
289 output->base.name);
290 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
291
292 return 0;
293}
294
295/* Creates the pageflip timer. Note that it isn't armed by default */
296static int
297drm_output_pageflip_timer_create(struct drm_output *output)
298{
299 struct wl_event_loop *loop = NULL;
300 struct weston_compositor *ec = output->base.compositor;
301
302 loop = wl_display_get_event_loop(ec->wl_display);
303 assert(loop);
304 output->pageflip_timer = wl_event_loop_add_timer(loop,
305 pageflip_timeout,
306 output);
307
308 if (output->pageflip_timer == NULL) {
309 weston_log("creating drm pageflip timer failed: %m\n");
310 return -1;
311 }
312
313 return 0;
314}
315
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400316static void
317drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400318
Mario Kleinerf507ec32015-06-21 21:25:14 +0200319static void
320drm_output_update_msc(struct drm_output *output, unsigned int seq);
321
Jesse Barnes58ef3792012-02-23 09:45:49 -0500322static int
Daniel Stone08d4edf2017-04-04 17:54:34 +0100323drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500324{
Daniel Stone08d4edf2017-04-04 17:54:34 +0100325 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500326}
327
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000328static struct drm_output *
329drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
330{
331 struct drm_output *output;
332
333 wl_list_for_each(output, &b->compositor->output_list, base.link) {
334 if (output->crtc_id == crtc_id)
335 return output;
336 }
337
338 wl_list_for_each(output, &b->compositor->pending_output_list,
339 base.link) {
340 if (output->crtc_id == crtc_id)
341 return output;
342 }
343
344 return NULL;
345}
346
Daniel Stonec0ec7592017-02-09 13:58:35 +0000347static struct drm_output *
348drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
349{
350 struct drm_output *output;
351
352 wl_list_for_each(output, &b->compositor->output_list, base.link) {
353 if (output->connector_id == connector_id)
354 return output;
355 }
356
357 wl_list_for_each(output, &b->compositor->pending_output_list,
358 base.link) {
359 if (output->connector_id == connector_id)
360 return output;
361 }
362
363 return NULL;
364}
365
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300366static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100367drm_fb_destroy(struct drm_fb *fb)
368{
369 if (fb->fb_id != 0)
370 drmModeRmFB(fb->fd, fb->fb_id);
371 weston_buffer_reference(&fb->buffer_ref, NULL);
372 free(fb);
373}
374
375static void
376drm_fb_destroy_dumb(struct drm_fb *fb)
377{
378 struct drm_mode_destroy_dumb destroy_arg;
379
380 assert(fb->type == BUFFER_PIXMAN_DUMB);
381
382 if (fb->map && fb->size > 0)
383 munmap(fb->map, fb->size);
384
385 memset(&destroy_arg, 0, sizeof(destroy_arg));
386 destroy_arg.handle = fb->handle;
387 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
388
389 drm_fb_destroy(fb);
390}
391
392static void
393drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300394{
395 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300396
Daniel Stonee4256832017-04-04 17:54:27 +0100397 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
398 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100399 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300400}
401
402static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000403drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300404 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200405{
406 struct drm_fb *fb;
407 int ret;
408
409 struct drm_mode_create_dumb create_arg;
410 struct drm_mode_destroy_dumb destroy_arg;
411 struct drm_mode_map_dumb map_arg;
412
Peter Huttererf3d62272013-08-08 11:57:05 +1000413 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200414 if (!fb)
415 return NULL;
416
Daniel Stone6e7a9612017-04-04 17:54:26 +0100417 fb->refcnt = 1;
418
Daniel Stone0b70fa42017-04-04 17:54:23 +0100419 fb->format = pixel_format_get_info(format);
420 if (!fb->format) {
421 weston_log("failed to look up format 0x%lx\n",
422 (unsigned long) format);
423 goto err_fb;
424 }
425
426 if (!fb->format->depth || !fb->format->bpp) {
427 weston_log("format 0x%lx is not compatible with dumb buffers\n",
428 (unsigned long) format);
429 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300430 }
431
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700432 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100433 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200434 create_arg.width = width;
435 create_arg.height = height;
436
Giulio Camuffo954f1832014-10-11 18:27:30 +0300437 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200438 if (ret)
439 goto err_fb;
440
Daniel Stonefc175a72017-04-04 17:54:22 +0100441 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200442 fb->handle = create_arg.handle;
443 fb->stride = create_arg.pitch;
444 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000445 fb->width = width;
446 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300447 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200448
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300449 ret = -1;
450
451 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700452 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300453
454 handles[0] = fb->handle;
455 pitches[0] = fb->stride;
456 offsets[0] = 0;
457
458 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100459 fb->format->format,
460 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300461 &fb->fb_id, 0);
462 if (ret) {
463 weston_log("addfb2 failed: %m\n");
464 b->no_addfb2 = 1;
465 }
466 }
467
468 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100469 ret = drmModeAddFB(b->drm.fd, width, height,
470 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300471 fb->stride, fb->handle, &fb->fb_id);
472 }
473
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200474 if (ret)
475 goto err_bo;
476
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700477 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200478 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400479 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200480 if (ret)
481 goto err_add_fb;
482
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500483 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300484 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200485 if (fb->map == MAP_FAILED)
486 goto err_add_fb;
487
488 return fb;
489
490err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300491 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200492err_bo:
493 memset(&destroy_arg, 0, sizeof(destroy_arg));
494 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300495 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200496err_fb:
497 free(fb);
498 return NULL;
499}
500
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200501static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100502drm_fb_ref(struct drm_fb *fb)
503{
504 fb->refcnt++;
505 return fb;
506}
507
508static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100509drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
510 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300511{
512 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500513 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300514 int ret;
515
Daniel Stonefc175a72017-04-04 17:54:22 +0100516 if (fb) {
517 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100518 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100519 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300520
Bryce Harringtonde16d892014-11-20 22:21:57 -0800521 fb = zalloc(sizeof *fb);
522 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200523 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300524
Daniel Stonefc175a72017-04-04 17:54:22 +0100525 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100526 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300527 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300528
Daniel Stonec8c917c2016-11-14 17:45:58 +0000529 fb->width = gbm_bo_get_width(bo);
530 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200531 fb->stride = gbm_bo_get_stride(bo);
532 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100533 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000534 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300535 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300536
Daniel Stone0b70fa42017-04-04 17:54:23 +0100537 if (!fb->format) {
538 weston_log("couldn't look up format 0x%lx\n",
539 (unsigned long) format);
540 goto err_free;
541 }
542
Daniel Stonec8c917c2016-11-14 17:45:58 +0000543 if (backend->min_width > fb->width ||
544 fb->width > backend->max_width ||
545 backend->min_height > fb->height ||
546 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200547 weston_log("bo geometry out of bounds\n");
548 goto err_free;
549 }
550
551 ret = -1;
552
Giulio Camuffo954f1832014-10-11 18:27:30 +0300553 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200554 handles[0] = fb->handle;
555 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200556 offsets[0] = 0;
557
Daniel Stonec8c917c2016-11-14 17:45:58 +0000558 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200559 format, handles, pitches, offsets,
560 &fb->fb_id, 0);
561 if (ret) {
562 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300563 backend->no_addfb2 = 1;
564 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200565 }
566 }
567
Daniel Stone0b70fa42017-04-04 17:54:23 +0100568 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000569 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100570 fb->format->depth, fb->format->bpp,
571 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200572
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300573 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200574 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200575 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300576 }
577
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100578 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300579
580 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200581
582err_free:
583 free(fb);
584 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300585}
586
587static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500588drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200589{
Pekka Paalanende685b82012-12-04 15:58:12 +0200590 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100591 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200592 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200593}
594
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200595static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100596drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200597{
598 if (!fb)
599 return;
600
Daniel Stone6e7a9612017-04-04 17:54:26 +0100601 assert(fb->refcnt > 0);
602 if (--fb->refcnt > 0)
603 return;
604
Daniel Stonefc175a72017-04-04 17:54:22 +0100605 switch (fb->type) {
606 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100607 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100608 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100609 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100610 case BUFFER_CLIENT:
611 gbm_bo_destroy(fb->bo);
612 break;
613 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100614 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100615 break;
616 default:
617 assert(NULL);
618 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200619 }
620}
621
Daniel Stone90648872016-10-21 18:08:37 +0100622static int
623drm_view_transform_supported(struct weston_view *ev)
624{
625 return !ev->transform.enabled ||
626 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
627}
628
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500629static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200630drm_output_check_scanout_format(struct drm_output *output,
631 struct weston_surface *es, struct gbm_bo *bo)
632{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200633 uint32_t format;
634 pixman_region32_t r;
635
636 format = gbm_bo_get_format(bo);
637
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700638 if (format == GBM_FORMAT_ARGB8888) {
639 /* We can scanout an ARGB buffer if the surface's
640 * opaque region covers the whole output, but we have
641 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800642 pixman_region32_init_rect(&r, 0, 0,
643 output->base.width,
644 output->base.height);
645 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200646
647 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500648 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200649
650 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500651 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700652
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100653 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700654 return format;
655
656 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200657}
658
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400659static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200660drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500661 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500662{
Armin Krezović545dba62016-08-05 15:54:18 +0200663 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500664 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200665 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300666 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500667 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500668
Daniel Stone90648872016-10-21 18:08:37 +0100669 /* Don't import buffers which span multiple outputs. */
670 if (ev->output_mask != (1u << output->base.id))
671 return NULL;
672
Daniel Stone296d7a92016-10-21 18:05:37 +0100673 /* We use GBM to import buffers. */
674 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400675 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500676
Daniel Stone296d7a92016-10-21 18:05:37 +0100677 if (buffer == NULL)
678 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100679 if (wl_shm_buffer_get(buffer->resource))
680 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100681
682 /* Make sure our view is exactly compatible with the output. */
683 if (ev->geometry.x != output->base.x ||
684 ev->geometry.y != output->base.y)
685 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100686 if (buffer->width != output->base.current_mode->width ||
687 buffer->height != output->base.current_mode->height)
688 return NULL;
689
Daniel Stone296d7a92016-10-21 18:05:37 +0100690 if (ev->transform.enabled)
691 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200692 if (ev->geometry.scissor_enabled)
693 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100694 if (viewport->buffer.transform != output->base.transform)
695 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100696 if (viewport->buffer.scale != output->base.current_scale)
697 return NULL;
698 if (!drm_view_transform_supported(ev))
699 return NULL;
700
701 if (ev->alpha != 1.0f)
702 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100703
Giulio Camuffo954f1832014-10-11 18:27:30 +0300704 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700705 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500706
Rob Bradford9b101872012-09-14 23:25:41 +0100707 /* Unable to use the buffer for scanout */
708 if (!bo)
709 return NULL;
710
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500712 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300713 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400714 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300715 }
716
Daniel Stone5bb8f582017-04-04 17:54:28 +0100717 output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
718 if (!output->fb_pending) {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300719 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400720 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300721 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500722
Daniel Stone5bb8f582017-04-04 17:54:28 +0100723 drm_fb_set_buffer(output->fb_pending, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500724
Daniel Stone4202e502016-10-24 13:26:42 +0100725 return &output->scanout_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500726}
727
Daniel Stone95d48a22017-04-04 17:54:30 +0100728static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200729drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400730{
Armin Krezović545dba62016-08-05 15:54:18 +0200731 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300732 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +0100733 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400734
Giulio Camuffo954f1832014-10-11 18:27:30 +0300735 output->base.compositor->renderer->repaint_output(&output->base,
736 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400737
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100738 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300739 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200740 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +0100741 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400742 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300743
Daniel Stone95d48a22017-04-04 17:54:30 +0100744 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
745 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200746 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100747 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +0100748 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300749 }
Daniel Stone95d48a22017-04-04 17:54:30 +0100750 ret->gbm_surface = output->gbm_surface;
751
752 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400753}
754
Daniel Stone95d48a22017-04-04 17:54:30 +0100755static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200756drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
757{
758 struct weston_compositor *ec = output->base.compositor;
759 pixman_region32_t total_damage, previous_damage;
760
761 pixman_region32_init(&total_damage);
762 pixman_region32_init(&previous_damage);
763
764 pixman_region32_copy(&previous_damage, damage);
765
766 pixman_region32_union(&total_damage, damage, &output->previous_damage);
767 pixman_region32_copy(&output->previous_damage, &previous_damage);
768
769 output->current_image ^= 1;
770
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200771 pixman_renderer_output_set_buffer(&output->base,
772 output->image[output->current_image]);
773
774 ec->renderer->repaint_output(&output->base, &total_damage);
775
776 pixman_region32_fini(&total_damage);
777 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +0100778
779 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200780}
781
782static void
783drm_output_render(struct drm_output *output, pixman_region32_t *damage)
784{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300785 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200786 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +0100787 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200788
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100789 /* If we already have a client buffer promoted to scanout, then we don't
790 * want to render. */
791 if (output->fb_pending)
792 return;
793
Giulio Camuffo954f1832014-10-11 18:27:30 +0300794 if (b->use_pixman)
Daniel Stone95d48a22017-04-04 17:54:30 +0100795 fb = drm_output_render_pixman(output, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200796 else
Daniel Stone95d48a22017-04-04 17:54:30 +0100797 fb = drm_output_render_gl(output, damage);
798
799 if (!fb)
800 return;
801 output->fb_pending = fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200802
Giulio Camuffo954f1832014-10-11 18:27:30 +0300803 pixman_region32_subtract(&c->primary_plane.damage,
804 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200805}
806
807static void
Richard Hughese7299962013-05-01 21:52:12 +0100808drm_output_set_gamma(struct weston_output *output_base,
809 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
810{
811 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200812 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300813 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200814 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100815
816 /* check */
817 if (output_base->gamma_size != size)
818 return;
819 if (!output->original_crtc)
820 return;
821
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100823 output->crtc_id,
824 size, r, g, b);
825 if (rc)
826 weston_log("set gamma failed: %m\n");
827}
828
Bryce Harringtonada4f072015-06-30 13:25:46 -0700829/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200830 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700831 * The pipe parameter indicates which CRTC is in use. Knowing this, we
832 * can determine which vblank sequence type to use for it. Traditional
833 * cards had only two CRTCs, with CRTC 0 using no special flags, and
834 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
835 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200836 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700837 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
838 * 0-31. If this is non-zero it indicates we're dealing with a
839 * multi-gpu situation and we need to calculate the vblank sync
840 * using DRM_BLANK_HIGH_CRTC_MASK.
841 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300842static unsigned int
843drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200844{
845 if (output->pipe > 1)
846 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
847 DRM_VBLANK_HIGH_CRTC_MASK;
848 else if (output->pipe > 0)
849 return DRM_VBLANK_SECONDARY;
850 else
851 return 0;
852}
853
David Herrmann1edf44c2013-10-22 17:11:26 +0200854static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500855drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000856 pixman_region32_t *damage,
857 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100858{
Armin Krezović545dba62016-08-05 15:54:18 +0200859 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300860 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200861 to_drm_backend(output->base.compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +0100862 struct drm_plane *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400863 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500864 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100865
Armin Krezović08368132016-09-30 14:11:05 +0200866 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200867 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800868
Daniel Stonef30a18c2017-04-04 17:54:31 +0100869 assert(!output->fb_last);
870
Derek Foreman2cd87fe2017-04-13 13:48:48 -0500871 /* If disable_planes is set then assign_planes() wasn't
872 * called for this render, so we could still have a stale
873 * cursor plane set up.
874 */
875 if (output->base.disable_planes) {
876 output->cursor_view = NULL;
877 output->cursor_plane.x = INT32_MIN;
878 output->cursor_plane.y = INT32_MIN;
879 }
880
Daniel Stone4e84f7d2017-04-04 17:54:29 +0100881 drm_output_render(output, damage);
Daniel Stone5bb8f582017-04-04 17:54:28 +0100882 if (!output->fb_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200883 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100884
Hardeningff39efa2013-09-18 23:56:35 +0200885 mode = container_of(output->base.current_mode, struct drm_mode, base);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300886 if (output->state_invalid || !output->fb_current ||
Daniel Stone5bb8f582017-04-04 17:54:28 +0100887 output->fb_current->stride != output->fb_pending->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300888 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +0100889 output->fb_pending->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400890 &output->connector_id, 1,
891 &mode->mode_info);
892 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200893 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200894 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400895 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300896 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300897
898 output->state_invalid = false;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200899 }
900
Giulio Camuffo954f1832014-10-11 18:27:30 +0300901 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +0100902 output->fb_pending->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500903 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200904 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200905 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500906 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100907
Daniel Stonef30a18c2017-04-04 17:54:31 +0100908 output->fb_last = output->fb_current;
909 output->fb_current = output->fb_pending;
910 output->fb_pending = NULL;
911
Daniel Stone205c0a02017-04-04 17:54:33 +0100912 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300913 output->page_flip_pending = 1;
914
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000915 if (output->pageflip_timer)
916 wl_event_source_timer_update(output->pageflip_timer,
917 backend->pageflip_timeout);
918
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400919 drm_output_set_cursor(output);
920
Jesse Barnes58ef3792012-02-23 09:45:49 -0500921 /*
922 * Now, update all the sprite surfaces
923 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300924 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200925 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500926 drmVBlank vbl = {
927 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
928 .request.sequence = 1,
929 };
930
Daniel Stonef30a18c2017-04-04 17:54:31 +0100931 /* XXX: Set output much earlier, so we don't attempt to place
932 * planes on entirely the wrong output. */
Daniel Stone5bb8f582017-04-04 17:54:28 +0100933 if ((!s->fb_current && !s->fb_pending) ||
Daniel Stone08d4edf2017-04-04 17:54:34 +0100934 !drm_plane_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500935 continue;
936
Daniel Stone5bb8f582017-04-04 17:54:28 +0100937 if (s->fb_pending && !backend->sprites_hidden)
938 fb_id = s->fb_pending->fb_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200939
Giulio Camuffo954f1832014-10-11 18:27:30 +0300940 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200941 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500942 s->dest_x, s->dest_y,
943 s->dest_w, s->dest_h,
944 s->src_x, s->src_y,
945 s->src_w, s->src_h);
946 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200947 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948 ret, strerror(errno));
949
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200950 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500951
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952 /*
953 * Queue a vblank signal so we know when the surface
954 * becomes active on the display or has been replaced.
955 */
956 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300957 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500958 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200959 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960 ret, strerror(errno));
961 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300962
963 s->output = output;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100964 s->fb_last = s->fb_current;
965 s->fb_current = s->fb_pending;
966 s->fb_pending = NULL;
Daniel Stone65d87d02017-04-04 17:54:32 +0100967 output->vblank_pending++;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500968 }
969
David Herrmann1edf44c2013-10-22 17:11:26 +0200970 return 0;
971
972err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800973 output->cursor_view = NULL;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100974 if (output->fb_pending) {
975 drm_fb_unref(output->fb_pending);
976 output->fb_pending = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200977 }
978
979 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400980}
981
982static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200983drm_output_start_repaint_loop(struct weston_output *output_base)
984{
Armin Krezović545dba62016-08-05 15:54:18 +0200985 struct drm_output *output = to_drm_output(output_base);
986 struct drm_backend *backend =
987 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200988 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200989 struct timespec ts, tnow;
990 struct timespec vbl2now;
991 int64_t refresh_nsec;
992 int ret;
993 drmVBlank vbl = {
994 .request.type = DRM_VBLANK_RELATIVE,
995 .request.sequence = 0,
996 .request.signal = 0,
997 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300998
Armin Krezović08368132016-09-30 14:11:05 +0200999 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08001000 return;
1001
Daniel Stone5bb8f582017-04-04 17:54:28 +01001002 if (!output->fb_current) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001003 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02001004 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001005 }
1006
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001007 /* Need to smash all state in from scratch; current timings might not
1008 * be what we want, page flip might not work, etc.
1009 */
1010 if (output->state_invalid)
1011 goto finish_frame;
1012
Mario Kleinerf507ec32015-06-21 21:25:14 +02001013 /* Try to get current msc and timestamp via instant query */
1014 vbl.request.type |= drm_waitvblank_pipe(output);
1015 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1016
1017 /* Error ret or zero timestamp means failure to get valid timestamp */
1018 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
1019 ts.tv_sec = vbl.reply.tval_sec;
1020 ts.tv_nsec = vbl.reply.tval_usec * 1000;
1021
1022 /* Valid timestamp for most recent vblank - not stale?
1023 * Stale ts could happen on Linux 3.17+, so make sure it
1024 * is not older than 1 refresh duration since now.
1025 */
1026 weston_compositor_read_presentation_clock(backend->compositor,
1027 &tnow);
1028 timespec_sub(&vbl2now, &tnow, &ts);
1029 refresh_nsec =
1030 millihz_to_nsec(output->base.current_mode->refresh);
1031 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
1032 drm_output_update_msc(output, vbl.reply.sequence);
1033 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001034 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02001035 return;
1036 }
1037 }
1038
1039 /* Immediate query didn't provide valid timestamp.
1040 * Use pageflip fallback.
1041 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001042 fb_id = output->fb_current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001043
Daniel Stone205c0a02017-04-04 17:54:33 +01001044 assert(!output->page_flip_pending);
1045 assert(!output->fb_last);
1046
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +02001048 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1049 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02001050 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001051 }
David Herrmann3c688c52013-10-22 17:11:25 +02001052
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001053 if (output->pageflip_timer)
1054 wl_event_source_timer_update(output->pageflip_timer,
1055 backend->pageflip_timeout);
1056
Daniel Stone205c0a02017-04-04 17:54:33 +01001057 output->fb_last = drm_fb_ref(output->fb_current);
1058 output->page_flip_pending = 1;
1059
David Herrmann3c688c52013-10-22 17:11:25 +02001060 return;
1061
1062finish_frame:
1063 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00001064 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001065 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001066}
1067
1068static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04001069drm_output_update_msc(struct drm_output *output, unsigned int seq)
1070{
1071 uint64_t msc_hi = output->base.msc >> 32;
1072
1073 if (seq < (output->base.msc & 0xffffffff))
1074 msc_hi++;
1075
1076 output->base.msc = (msc_hi << 32) + seq;
1077}
1078
1079static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001080vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1081 void *data)
1082{
Daniel Stone08d4edf2017-04-04 17:54:34 +01001083 struct drm_plane *s = (struct drm_plane *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001084 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001085 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001086 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1087 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001088
Pekka Paalanen641307c2014-09-23 22:08:47 -04001089 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01001090 output->vblank_pending--;
1091 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001092
Daniel Stonef30a18c2017-04-04 17:54:31 +01001093 assert(s->fb_last || s->fb_current);
1094 drm_fb_unref(s->fb_last);
1095 s->fb_last = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001096
Daniel Stone65d87d02017-04-04 17:54:32 +01001097 if (!output->page_flip_pending && !output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001098 /* Stop the pageflip timer instead of rearming it here */
1099 if (output->pageflip_timer)
1100 wl_event_source_timer_update(output->pageflip_timer, 0);
1101
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001102 ts.tv_sec = sec;
1103 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001104 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001105 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001106}
1107
1108static void
Armin Krezović08368132016-09-30 14:11:05 +02001109drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001110
1111static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001112page_flip_handler(int fd, unsigned int frame,
1113 unsigned int sec, unsigned int usec, void *data)
1114{
Armin Krezović545dba62016-08-05 15:54:18 +02001115 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001116 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001117 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1118 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1119 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001120
Pekka Paalanen641307c2014-09-23 22:08:47 -04001121 drm_output_update_msc(output, frame);
1122
Daniel Stone205c0a02017-04-04 17:54:33 +01001123 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001124 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001125
Daniel Stone205c0a02017-04-04 17:54:33 +01001126 drm_fb_unref(output->fb_last);
1127 output->fb_last = NULL;
1128
Xiong Zhangabd5d472013-10-11 14:43:07 +08001129 if (output->destroy_pending)
1130 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001131 else if (output->disable_pending)
1132 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001133 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001134 /* Stop the pageflip timer instead of rearming it here */
1135 if (output->pageflip_timer)
1136 wl_event_source_timer_update(output->pageflip_timer, 0);
1137
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001138 ts.tv_sec = sec;
1139 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001140 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001141
1142 /* We can't call this from frame_notify, because the output's
1143 * repaint needed flag is cleared just after that */
1144 if (output->recorder)
1145 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001146 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001147}
1148
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001149static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01001150drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001152{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001153 uint32_t i, format;
1154
1155 format = gbm_bo_get_format(bo);
1156
1157 if (format == GBM_FORMAT_ARGB8888) {
1158 pixman_region32_t r;
1159
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001160 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001161 ev->surface->width,
1162 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001164
1165 if (!pixman_region32_not_empty(&r))
1166 format = GBM_FORMAT_XRGB8888;
1167
1168 pixman_region32_fini(&r);
1169 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001170
Daniel Stone08d4edf2017-04-04 17:54:34 +01001171 for (i = 0; i < p->count_formats; i++)
1172 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001173 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001174
1175 return 0;
1176}
1177
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001178static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001179drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001181{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001182 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001183 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001184 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001185 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01001186 struct drm_plane *p;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001187 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001188 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001189 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001190 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001191 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001192 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001193 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001194
Giulio Camuffo954f1832014-10-11 18:27:30 +03001195 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001196 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001197
Daniel Stone296d7a92016-10-21 18:05:37 +01001198 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001199 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001200 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001201
Daniel Stone296d7a92016-10-21 18:05:37 +01001202 /* We can only import GBM buffers. */
1203 if (b->gbm == NULL)
1204 return NULL;
1205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001207 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001208 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001209 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001210 return NULL;
1211
Daniel Stone296d7a92016-10-21 18:05:37 +01001212 if (viewport->buffer.transform != output->base.transform)
1213 return NULL;
1214 if (viewport->buffer.scale != output->base.current_scale)
1215 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001217 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001218
Daniel Stone296d7a92016-10-21 18:05:37 +01001219 if (ev->alpha != 1.0f)
1220 return NULL;
1221
Daniel Stone08d4edf2017-04-04 17:54:34 +01001222 wl_list_for_each(p, &b->sprite_list, link) {
1223 if (!drm_plane_crtc_supported(output, p))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001224 continue;
1225
Daniel Stone08d4edf2017-04-04 17:54:34 +01001226 if (!p->fb_pending) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001227 found = 1;
1228 break;
1229 }
1230 }
1231
1232 /* No sprites available */
1233 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001234 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001235
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001236 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001237#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001238 /* XXX: TODO:
1239 *
1240 * Use AddFB2 directly, do not go via GBM.
1241 * Add support for multiplanar formats.
1242 * Both require refactoring in the DRM-backend to
1243 * support a mix of gbm_bos and drmfbs.
1244 */
1245 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001246 .fd = dmabuf->attributes.fd[0],
1247 .width = dmabuf->attributes.width,
1248 .height = dmabuf->attributes.height,
1249 .stride = dmabuf->attributes.stride[0],
1250 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001251 };
1252
Micah Fedkec8890122017-02-01 15:28:23 -05001253 /* XXX: TODO:
1254 *
1255 * Currently the buffer is rejected if any dmabuf attribute
1256 * flag is set. This keeps us from passing an inverted /
1257 * interlaced / bottom-first buffer (or any other type that may
1258 * be added in the future) through to an overlay. Ultimately,
1259 * these types of buffers should be handled through buffer
1260 * transforms and not as spot-checks requiring specific
1261 * knowledge. */
1262 if (dmabuf->attributes.n_planes != 1 ||
1263 dmabuf->attributes.offset[0] != 0 ||
1264 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001265 return NULL;
1266
1267 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1268 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001269#else
1270 return NULL;
1271#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001272 } else {
1273 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1274 buffer_resource, GBM_BO_USE_SCANOUT);
1275 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001276 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001277 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001278
Daniel Stone08d4edf2017-04-04 17:54:34 +01001279 format = drm_output_check_plane_format(p, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001280 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001281 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001282 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001283 }
1284
Daniel Stone08d4edf2017-04-04 17:54:34 +01001285 p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1286 if (!p->fb_pending) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001287 gbm_bo_destroy(bo);
1288 return NULL;
1289 }
1290
Daniel Stone08d4edf2017-04-04 17:54:34 +01001291 drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001294 p->base.x = box->x1;
1295 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001296
Jesse Barnes58ef3792012-02-23 09:45:49 -05001297 /*
1298 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001299 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001300 * for us already).
1301 */
1302 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001304 &output->base.region);
1305 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001306 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001307 tbox = weston_transformed_rect(output->base.width,
1308 output->base.height,
1309 output->base.transform,
1310 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001311 *box);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001312 p->dest_x = tbox.x1;
1313 p->dest_y = tbox.y1;
1314 p->dest_w = tbox.x2 - tbox.x1;
1315 p->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001316 pixman_region32_fini(&dest_rect);
1317
1318 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001320 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001321 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 weston_view_from_global_fixed(ev,
1324 wl_fixed_from_int(box->x1),
1325 wl_fixed_from_int(box->y1),
1326 &sx1, &sy1);
1327 weston_view_from_global_fixed(ev,
1328 wl_fixed_from_int(box->x2),
1329 wl_fixed_from_int(box->y2),
1330 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001331
1332 if (sx1 < 0)
1333 sx1 = 0;
1334 if (sy1 < 0)
1335 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001336 if (sx2 > wl_fixed_from_int(ev->surface->width))
1337 sx2 = wl_fixed_from_int(ev->surface->width);
1338 if (sy2 > wl_fixed_from_int(ev->surface->height))
1339 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001340
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001341 tbox.x1 = sx1;
1342 tbox.y1 = sy1;
1343 tbox.x2 = sx2;
1344 tbox.y2 = sy2;
1345
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001346 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1347 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001348 viewport->buffer.transform,
1349 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001350 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001351
Daniel Stone08d4edf2017-04-04 17:54:34 +01001352 p->src_x = tbox.x1 << 8;
1353 p->src_y = tbox.y1 << 8;
1354 p->src_w = (tbox.x2 - tbox.x1) << 8;
1355 p->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001356 pixman_region32_fini(&src_rect);
1357
Daniel Stone08d4edf2017-04-04 17:54:34 +01001358 return &p->base;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001359}
1360
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001361static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001362drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001364{
Armin Krezović545dba62016-08-05 15:54:18 +02001365 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001366 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001367 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001368 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001369
Giulio Camuffo954f1832014-10-11 18:27:30 +03001370 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001371 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001372
1373 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001374 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001375
1376 /* Don't import buffers which span multiple outputs. */
1377 if (ev->output_mask != (1u << output->base.id))
1378 return NULL;
1379
1380 /* We use GBM to import SHM buffers. */
1381 if (b->gbm == NULL)
1382 return NULL;
1383
Derek Foreman6c19b692015-12-03 14:07:12 -06001384 if (ev->surface->buffer_ref.buffer == NULL)
1385 return NULL;
1386 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1387 if (!shmbuf)
1388 return NULL;
1389 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1390 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001391
1392 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1393 return NULL;
1394 if (ev->transform.enabled &&
1395 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1396 return NULL;
1397 if (viewport->buffer.scale != output->base.current_scale)
1398 return NULL;
1399 if (ev->geometry.scissor_enabled)
1400 return NULL;
1401
Derek Foreman6c19b692015-12-03 14:07:12 -06001402 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001403 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001404 return NULL;
1405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001407 weston_view_to_global_float(ev, 0, 0, &x, &y);
1408 output->cursor_plane.x = x;
1409 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001410
1411 return &output->cursor_plane;
1412}
1413
Pekka Paalanend0ead482014-06-16 12:05:40 +03001414/**
1415 * Update the image for the current cursor surface
1416 *
1417 * @param b DRM backend structure
1418 * @param bo GBM buffer object to write into
1419 * @param ev View to use for cursor image
1420 */
1421static void
1422cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1423 struct weston_view *ev)
1424{
1425 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1426 uint32_t buf[b->cursor_width * b->cursor_height];
1427 int32_t stride;
1428 uint8_t *s;
1429 int i;
1430
1431 assert(buffer && buffer->shm_buffer);
1432 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1433 assert(ev->surface->width <= b->cursor_width);
1434 assert(ev->surface->height <= b->cursor_height);
1435
1436 memset(buf, 0, sizeof buf);
1437 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1438 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1439
1440 wl_shm_buffer_begin_access(buffer->shm_buffer);
1441 for (i = 0; i < ev->surface->height; i++)
1442 memcpy(buf + i * b->cursor_width,
1443 s + i * stride,
1444 ev->surface->width * 4);
1445 wl_shm_buffer_end_access(buffer->shm_buffer);
1446
1447 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1448 weston_log("failed update cursor: %m\n");
1449}
1450
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001451static void
1452drm_output_set_cursor(struct drm_output *output)
1453{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001455 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001456 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001457 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001458 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001459
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001461 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001462 return;
1463 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001464
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001465 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001466 pixman_region32_fini(&output->cursor_plane.damage);
1467 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001468 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001469 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001470
Pekka Paalanend0ead482014-06-16 12:05:40 +03001471 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001472 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001473 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1474 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001475 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001476 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001477 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001478 }
1479
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001480 x = (output->cursor_plane.x - output->base.x) *
1481 output->base.current_scale;
1482 y = (output->cursor_plane.y - output->base.y) *
1483 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001484
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001485 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1486 weston_log("failed to move cursor: %m\n");
1487 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001488 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001489}
1490
Jesse Barnes58ef3792012-02-23 09:45:49 -05001491static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001492drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001493{
Armin Krezović545dba62016-08-05 15:54:18 +02001494 struct drm_backend *b = to_drm_backend(output_base->compositor);
1495 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001497 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001498 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001499
1500 /*
1501 * Find a surface for each sprite in the output using some heuristics:
1502 * 1) size
1503 * 2) frequency of update
1504 * 3) opacity (though some hw might support alpha blending)
1505 * 4) clipping (this can be fixed with color keys)
1506 *
1507 * The idea is to save on blitting since this should save power.
1508 * If we can get a large video surface on the sprite for example,
1509 * the main display surface may not need to update at all, and
1510 * the client buffer can be used directly for the sprite surface
1511 * as we do for flipping full screen surfaces.
1512 */
1513 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001514 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001516 output->cursor_view = NULL;
1517 output->cursor_plane.x = INT32_MIN;
1518 output->cursor_plane.y = INT32_MIN;
1519
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001521 struct weston_surface *es = ev->surface;
1522
1523 /* Test whether this buffer can ever go into a plane:
1524 * non-shm, or small enough to be a cursor.
1525 *
1526 * Also, keep a reference when using the pixman renderer.
1527 * That makes it possible to do a seamless switch to the GL
1528 * renderer and since the pixman renderer keeps a reference
1529 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001530 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001531 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001532 (es->buffer_ref.buffer &&
1533 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001534 (ev->surface->width <= b->cursor_width &&
1535 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001536 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001537 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001538 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001539
Jesse Barnes58ef3792012-02-23 09:45:49 -05001540 pixman_region32_init(&surface_overlap);
1541 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001544 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001545 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001546 next_plane = primary;
1547 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001549 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001551 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001553 if (next_plane == NULL)
1554 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001557
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001558 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001559 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001561
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001562 if (next_plane == primary ||
1563 next_plane == &output->cursor_plane) {
1564 /* cursor plane involves a copy */
1565 ev->psf_flags = 0;
1566 } else {
1567 /* All other planes are a direct scanout of a
1568 * single client buffer.
1569 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001570 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001571 }
1572
Jesse Barnes58ef3792012-02-23 09:45:49 -05001573 pixman_region32_fini(&surface_overlap);
1574 }
1575 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001576}
1577
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001578/**
1579 * Find the closest-matching mode for a given target
1580 *
1581 * Given a target mode, find the most suitable mode amongst the output's
1582 * current mode list to use, preferring the current mode if possible, to
1583 * avoid an expensive mode switch.
1584 *
1585 * @param output DRM output
1586 * @param target_mode Mode to attempt to match
1587 * @returns Pointer to a mode from the output's mode list
1588 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001589static struct drm_mode *
1590choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1591{
1592 struct drm_mode *tmp_mode = NULL, *mode;
1593
Hardeningff39efa2013-09-18 23:56:35 +02001594 if (output->base.current_mode->width == target_mode->width &&
1595 output->base.current_mode->height == target_mode->height &&
1596 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001597 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001598 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001599
1600 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1601 if (mode->mode_info.hdisplay == target_mode->width &&
1602 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001603 if (mode->base.refresh == target_mode->refresh ||
1604 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001605 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001606 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001607 tmp_mode = mode;
1608 }
1609 }
1610
1611 return tmp_mode;
1612}
1613
1614static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001615drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001616static void
1617drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001618static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001619drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001620static void
1621drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001622
1623static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001624drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1625{
1626 struct drm_output *output;
1627 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001629
1630 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001631 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001632 return -1;
1633 }
1634
1635 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001636 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001637 return -1;
1638 }
1639
Armin Krezović545dba62016-08-05 15:54:18 +02001640 b = to_drm_backend(output_base->compositor);
1641 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001642 drm_mode = choose_mode (output, mode);
1643
1644 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001645 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001646 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001647 }
1648
Hardeningff39efa2013-09-18 23:56:35 +02001649 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001650 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001651
Hardeningff39efa2013-09-18 23:56:35 +02001652 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001653
Hardeningff39efa2013-09-18 23:56:35 +02001654 output->base.current_mode = &drm_mode->base;
1655 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001656 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1657
Daniel Stonef30a18c2017-04-04 17:54:31 +01001658 /* XXX: This drops our current buffer too early, before we've started
1659 * displaying it. Ideally this should be much more atomic and
1660 * integrated with a full repaint cycle, rather than doing a
1661 * sledgehammer modeswitch first, and only later showing new
1662 * content.
1663 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001664 drm_fb_unref(output->fb_current);
Daniel Stonef30a18c2017-04-04 17:54:31 +01001665 assert(!output->fb_last);
1666 assert(!output->fb_pending);
1667 output->fb_last = output->fb_current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001668
Giulio Camuffo954f1832014-10-11 18:27:30 +03001669 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001670 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001671 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001672 weston_log("failed to init output pixman state with "
1673 "new mode\n");
1674 return -1;
1675 }
1676 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001677 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001678 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001679 weston_log("failed to init output egl state with "
1680 "new mode");
1681 return -1;
1682 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001683 }
1684
Alex Wub7b8bda2012-04-17 17:20:48 +08001685 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001686}
1687
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001688static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001689on_drm_input(int fd, uint32_t mask, void *data)
1690{
1691 drmEventContext evctx;
1692
1693 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01001694 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001695 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001696 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001697 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001698
1699 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001700}
1701
1702static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001703init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001704{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001705 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001706 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001707 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001708
Daniel Stoneefa504f2016-12-19 16:48:20 +00001709 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001710
Daniel Stoneefa504f2016-12-19 16:48:20 +00001711 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001712 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001713 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001714 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001715 clk_id = CLOCK_REALTIME;
1716
Giulio Camuffo954f1832014-10-11 18:27:30 +03001717 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001718 weston_log("Error: failed to set presentation clock %d.\n",
1719 clk_id);
1720 return -1;
1721 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001722
Daniel Stoneefa504f2016-12-19 16:48:20 +00001723 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001724 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001725 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001726 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001727 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001728
Daniel Stoneefa504f2016-12-19 16:48:20 +00001729 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001730 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001731 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001732 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001733 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001734
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001735 return 0;
1736}
1737
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001738static struct gbm_device *
1739create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001740{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001741 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001742
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001743 gl_renderer = weston_load_module("gl-renderer.so",
1744 "gl_renderer_interface");
1745 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001746 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001747
1748 /* GBM will load a dri driver, but even though they need symbols from
1749 * libglapi, in some version of Mesa they are not linked to it. Since
1750 * only the gl-renderer module links to it, the call above won't make
1751 * these symbols globally available, and loading the DRI driver fails.
1752 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1753 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1754
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001755 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001756
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001757 return gbm;
1758}
1759
Bryce Harringtonc056a982015-05-19 15:25:18 -07001760/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001761 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001762 *
1763 * This returns 0 if substitution isn't possible, but 0 might be a
1764 * legitimate format for other EGL platforms, so the caller is
1765 * responsible for checking for 0 before calling gl_renderer->create().
1766 *
1767 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1768 * but it's entirely possible we'll see this again on other implementations.
1769 */
1770static int
1771fallback_format_for(uint32_t format)
1772{
1773 switch (format) {
1774 case GBM_FORMAT_XRGB8888:
1775 return GBM_FORMAT_ARGB8888;
1776 case GBM_FORMAT_XRGB2101010:
1777 return GBM_FORMAT_ARGB2101010;
1778 default:
1779 return 0;
1780 }
1781}
1782
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001783static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001784drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001785{
Derek Foreman6d556372015-11-04 14:47:33 -06001786 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001787 b->gbm_format,
1788 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001789 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001790 };
Derek Foreman6d556372015-11-04 14:47:33 -06001791 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001792
Derek Foremanc4cfe852015-05-15 12:12:40 -05001793 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001794 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001795 if (gl_renderer->display_create(b->compositor,
1796 EGL_PLATFORM_GBM_KHR,
1797 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001798 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001799 gl_renderer->opaque_attribs,
1800 format,
1801 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001802 return -1;
1803 }
1804
1805 return 0;
1806}
1807
1808static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001809init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001810{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001811 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001812
Giulio Camuffo954f1832014-10-11 18:27:30 +03001813 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001814 return -1;
1815
Giulio Camuffo954f1832014-10-11 18:27:30 +03001816 if (drm_backend_create_gl_renderer(b) < 0) {
1817 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001818 return -1;
1819 }
1820
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001821 return 0;
1822}
1823
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001824static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001825init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001826{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001827 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001828}
1829
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001830/**
Pekka Paalanenec272712014-06-05 11:22:25 +03001831 * Create a drm_plane for a hardware plane
1832 *
1833 * Creates one drm_plane structure for a hardware plane, and initialises its
1834 * properties and formats.
1835 *
1836 * This function does not add the plane to the list of usable planes in Weston
1837 * itself; the caller is responsible for this.
1838 *
1839 * Call drm_plane_destroy to clean up the plane.
1840 *
1841 * @param b DRM compositor backend
1842 * @param kplane DRM plane to create
1843 */
1844static struct drm_plane *
1845drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
1846{
1847 struct drm_plane *plane;
1848
1849 plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) *
1850 kplane->count_formats));
1851 if (!plane) {
1852 weston_log("%s: out of memory\n", __func__);
1853 return NULL;
1854 }
1855
1856 plane->backend = b;
1857 plane->possible_crtcs = kplane->possible_crtcs;
1858 plane->plane_id = kplane->plane_id;
1859 plane->count_formats = kplane->count_formats;
1860 memcpy(plane->formats, kplane->formats,
1861 kplane->count_formats * sizeof(kplane->formats[0]));
1862
1863 weston_plane_init(&plane->base, b->compositor, 0, 0);
1864 wl_list_insert(&b->sprite_list, &plane->link);
1865
1866 return plane;
1867}
1868
1869/**
1870 * Destroy one DRM plane
1871 *
1872 * Destroy a DRM plane, removing it from screen and releasing its retained
1873 * buffers in the process. The counterpart to drm_plane_create.
1874 *
1875 * @param plane Plane to deallocate (will be freed)
1876 */
1877static void
1878drm_plane_destroy(struct drm_plane *plane)
1879{
1880 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, 0, 0, 0,
1881 0, 0, 0, 0, 0, 0, 0, 0);
1882 assert(!plane->fb_last);
1883 assert(!plane->fb_pending);
1884 drm_fb_unref(plane->fb_current);
1885 weston_plane_release(&plane->base);
1886 wl_list_remove(&plane->link);
1887 free(plane);
1888}
1889
1890/**
1891 * Initialise sprites (overlay planes)
1892 *
1893 * Walk the list of provided DRM planes, and add overlay planes.
1894 *
1895 * Call destroy_sprites to free these planes.
1896 *
1897 * @param b DRM compositor backend
1898 */
1899static void
1900create_sprites(struct drm_backend *b)
1901{
1902 drmModePlaneRes *kplane_res;
1903 drmModePlane *kplane;
1904 struct drm_plane *drm_plane;
1905 uint32_t i;
1906
1907 kplane_res = drmModeGetPlaneResources(b->drm.fd);
1908 if (!kplane_res) {
1909 weston_log("failed to get plane resources: %s\n",
1910 strerror(errno));
1911 return;
1912 }
1913
1914 for (i = 0; i < kplane_res->count_planes; i++) {
1915 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
1916 if (!kplane)
1917 continue;
1918
1919 drm_plane = drm_plane_create(b, kplane);
1920 drmModeFreePlane(kplane);
1921 if (!drm_plane)
1922 continue;
1923
1924 weston_compositor_stack_plane(b->compositor, &drm_plane->base,
1925 &b->compositor->primary_plane);
1926 }
1927
1928 drmModeFreePlaneResources(kplane_res);
1929}
1930
1931/**
1932 * Clean up sprites (overlay planes)
1933 *
1934 * The counterpart to create_sprites.
1935 *
1936 * @param b DRM compositor backend
1937 */
1938static void
1939destroy_sprites(struct drm_backend *b)
1940{
1941 struct drm_plane *plane, *next;
1942
1943 wl_list_for_each_safe(plane, next, &b->sprite_list, link)
1944 drm_plane_destroy(plane);
1945}
1946
1947/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001948 * Add a mode to output's mode list
1949 *
1950 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1951 * output's mode list.
1952 *
1953 * @param output DRM output to add mode to
1954 * @param info DRM mode structure to add
1955 * @returns Newly-allocated Weston/DRM mode structure
1956 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001957static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001958drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001959{
1960 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001961 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001962
1963 mode = malloc(sizeof *mode);
1964 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001965 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001966
1967 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001968 mode->base.width = info->hdisplay;
1969 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001970
1971 /* Calculate higher precision (mHz) refresh rate */
1972 refresh = (info->clock * 1000000LL / info->htotal +
1973 info->vtotal / 2) / info->vtotal;
1974
1975 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1976 refresh *= 2;
1977 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1978 refresh /= 2;
1979 if (info->vscan > 1)
1980 refresh /= info->vscan;
1981
1982 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001983 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001984
1985 if (info->type & DRM_MODE_TYPE_PREFERRED)
1986 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1987
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001988 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1989
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001990 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001991}
1992
1993static int
1994drm_subpixel_to_wayland(int drm_value)
1995{
1996 switch (drm_value) {
1997 default:
1998 case DRM_MODE_SUBPIXEL_UNKNOWN:
1999 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
2000 case DRM_MODE_SUBPIXEL_NONE:
2001 return WL_OUTPUT_SUBPIXEL_NONE;
2002 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
2003 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
2004 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
2005 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
2006 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
2007 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
2008 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
2009 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
2010 }
2011}
2012
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002013/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002014static uint32_t
2015drm_get_backlight(struct drm_output *output)
2016{
2017 long brightness, max_brightness, norm;
2018
2019 brightness = backlight_get_brightness(output->backlight);
2020 max_brightness = backlight_get_max_brightness(output->backlight);
2021
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002022 /* convert it on a scale of 0 to 255 */
2023 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002024
2025 return (uint32_t) norm;
2026}
2027
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002028/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002029static void
2030drm_set_backlight(struct weston_output *output_base, uint32_t value)
2031{
Armin Krezović545dba62016-08-05 15:54:18 +02002032 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002033 long max_brightness, new_brightness;
2034
2035 if (!output->backlight)
2036 return;
2037
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002038 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002039 return;
2040
2041 max_brightness = backlight_get_max_brightness(output->backlight);
2042
2043 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002044 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002045
2046 backlight_set_brightness(output->backlight, new_brightness);
2047}
2048
2049static drmModePropertyPtr
2050drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
2051{
2052 drmModePropertyPtr props;
2053 int i;
2054
2055 for (i = 0; i < connector->count_props; i++) {
2056 props = drmModeGetProperty(fd, connector->props[i]);
2057 if (!props)
2058 continue;
2059
2060 if (!strcmp(props->name, name))
2061 return props;
2062
2063 drmModeFreeProperty(props);
2064 }
2065
2066 return NULL;
2067}
2068
2069static void
2070drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
2071{
Armin Krezović545dba62016-08-05 15:54:18 +02002072 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002073 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002074 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01002075 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002076
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03002077 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002078 return;
2079
Daniel Stone36609c72015-06-18 07:49:02 +01002080 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
2081 output->dpms_prop->prop_id, level);
2082 if (ret) {
2083 weston_log("DRM: DPMS: failed property set for %s\n",
2084 output->base.name);
2085 return;
2086 }
2087
2088 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002089}
2090
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002091static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002092 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
2093 [DRM_MODE_CONNECTOR_VGA] = "VGA",
2094 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
2095 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
2096 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
2097 [DRM_MODE_CONNECTOR_Composite] = "Composite",
2098 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
2099 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
2100 [DRM_MODE_CONNECTOR_Component] = "Component",
2101 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
2102 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
2103 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
2104 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
2105 [DRM_MODE_CONNECTOR_TV] = "TV",
2106 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002107#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002108 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
2109 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002110#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002111};
2112
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002113static char *
2114make_connector_name(const drmModeConnector *con)
2115{
2116 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002117 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002118
2119 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
2120 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002121
2122 if (!type_name)
2123 type_name = "UNNAMED";
2124
2125 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002126
2127 return strdup(name);
2128}
2129
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002130static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002132 drmModeRes *resources, drmModeConnector *connector)
2133{
2134 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002135 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00002136 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002137
2138 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00002139 uint32_t possible_crtcs, encoder_id, crtc_id;
2140
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002142 if (encoder == NULL) {
2143 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00002144 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002145 }
Daniel Stone75487c22017-01-16 14:33:38 +00002146 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002147 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00002148 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002149 drmModeFreeEncoder(encoder);
2150
2151 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00002152 if (!(possible_crtcs & (1 << i)))
2153 continue;
2154
2155 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
2156 continue;
2157
Daniel Stone75487c22017-01-16 14:33:38 +00002158 /* Try to preserve the existing
2159 * CRTC -> encoder -> connector routing; it makes
2160 * initialisation faster, and also since we have a
2161 * very dumb picking algorithm, may preserve a better
2162 * choice. */
2163 if (!connector->encoder_id ||
2164 (encoder_id == connector->encoder_id &&
2165 crtc_id == resources->crtcs[i]))
2166 return i;
2167
2168 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002169 }
2170 }
2171
Daniel Stone75487c22017-01-16 14:33:38 +00002172 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002173}
2174
Daniel Stonee4256832017-04-04 17:54:27 +01002175static void drm_output_fini_cursor_egl(struct drm_output *output)
2176{
2177 unsigned int i;
2178
2179 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2180 drm_fb_unref(output->gbm_cursor_fb[i]);
2181 output->gbm_cursor_fb[i] = NULL;
2182 }
2183}
2184
2185static int
2186drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
2187{
2188 unsigned int i;
2189
2190 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2191 struct gbm_bo *bo;
2192
2193 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
2194 GBM_FORMAT_ARGB8888,
2195 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
2196 if (!bo)
2197 goto err;
2198
2199 output->gbm_cursor_fb[i] =
2200 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2201 BUFFER_CURSOR);
2202 if (!output->gbm_cursor_fb[i]) {
2203 gbm_bo_destroy(bo);
2204 goto err;
2205 }
2206 }
2207
2208 return 0;
2209
2210err:
2211 weston_log("cursor buffers unavailable, using gl cursors\n");
2212 b->cursors_are_broken = 1;
2213 drm_output_fini_cursor_egl(output);
2214 return -1;
2215}
2216
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002217/* Init output state that depends on gl or gbm */
2218static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002219drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002220{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002221 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002222 output->gbm_format,
2223 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002224 };
Daniel Stonee4256832017-04-04 17:54:27 +01002225 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002226
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002227 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002228 output->base.current_mode->width,
2229 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002230 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002231 GBM_BO_USE_SCANOUT |
2232 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002233 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002234 weston_log("failed to create gbm surface\n");
2235 return -1;
2236 }
2237
Derek Foremanc4cfe852015-05-15 12:12:40 -05002238 if (format[1])
2239 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002240 if (gl_renderer->output_window_create(&output->base,
2241 (EGLNativeWindowType)output->gbm_surface,
2242 output->gbm_surface,
2243 gl_renderer->opaque_attribs,
2244 format,
2245 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002246 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002247 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002248 return -1;
2249 }
2250
Daniel Stonee4256832017-04-04 17:54:27 +01002251 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002252
2253 return 0;
2254}
2255
Daniel Stone3e661f72016-11-04 17:24:06 +00002256static void
2257drm_output_fini_egl(struct drm_output *output)
2258{
2259 gl_renderer->output_destroy(&output->base);
2260 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002261 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002262}
2263
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002264static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002265drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002266{
Hardeningff39efa2013-09-18 23:56:35 +02002267 int w = output->base.current_mode->width;
2268 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002269 uint32_t format = output->gbm_format;
2270 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002271 unsigned int i;
2272
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002273 switch (format) {
2274 case GBM_FORMAT_XRGB8888:
2275 pixman_format = PIXMAN_x8r8g8b8;
2276 break;
2277 case GBM_FORMAT_RGB565:
2278 pixman_format = PIXMAN_r5g6b5;
2279 break;
2280 default:
2281 weston_log("Unsupported pixman format 0x%x\n", format);
2282 return -1;
2283 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002284
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002285 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002286 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002287 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002288 if (!output->dumb[i])
2289 goto err;
2290
2291 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002292 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002293 output->dumb[i]->map,
2294 output->dumb[i]->stride);
2295 if (!output->image[i])
2296 goto err;
2297 }
2298
2299 if (pixman_renderer_output_create(&output->base) < 0)
2300 goto err;
2301
2302 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002303 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002304
2305 return 0;
2306
2307err:
2308 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2309 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002310 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002311 if (output->image[i])
2312 pixman_image_unref(output->image[i]);
2313
2314 output->dumb[i] = NULL;
2315 output->image[i] = NULL;
2316 }
2317
2318 return -1;
2319}
2320
2321static void
2322drm_output_fini_pixman(struct drm_output *output)
2323{
2324 unsigned int i;
2325
2326 pixman_renderer_output_destroy(&output->base);
2327 pixman_region32_fini(&output->previous_damage);
2328
2329 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002330 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002331 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002332 output->dumb[i] = NULL;
2333 output->image[i] = NULL;
2334 }
2335}
2336
Richard Hughes2b2092a2013-04-24 14:58:02 +01002337static void
2338edid_parse_string(const uint8_t *data, char text[])
2339{
2340 int i;
2341 int replaced = 0;
2342
2343 /* this is always 12 bytes, but we can't guarantee it's null
2344 * terminated or not junk. */
2345 strncpy(text, (const char *) data, 12);
2346
Bryce Harrington9c7de162015-08-28 13:04:26 -07002347 /* guarantee our new string is null-terminated */
2348 text[12] = '\0';
2349
Richard Hughes2b2092a2013-04-24 14:58:02 +01002350 /* remove insane chars */
2351 for (i = 0; text[i] != '\0'; i++) {
2352 if (text[i] == '\n' ||
2353 text[i] == '\r') {
2354 text[i] = '\0';
2355 break;
2356 }
2357 }
2358
2359 /* ensure string is printable */
2360 for (i = 0; text[i] != '\0'; i++) {
2361 if (!isprint(text[i])) {
2362 text[i] = '-';
2363 replaced++;
2364 }
2365 }
2366
2367 /* if the string is random junk, ignore the string */
2368 if (replaced > 4)
2369 text[0] = '\0';
2370}
2371
2372#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2373#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2374#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2375#define EDID_OFFSET_DATA_BLOCKS 0x36
2376#define EDID_OFFSET_LAST_BLOCK 0x6c
2377#define EDID_OFFSET_PNPID 0x08
2378#define EDID_OFFSET_SERIAL 0x0c
2379
2380static int
2381edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2382{
2383 int i;
2384 uint32_t serial_number;
2385
2386 /* check header */
2387 if (length < 128)
2388 return -1;
2389 if (data[0] != 0x00 || data[1] != 0xff)
2390 return -1;
2391
2392 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2393 * /--08--\/--09--\
2394 * 7654321076543210
2395 * |\---/\---/\---/
2396 * R C1 C2 C3 */
2397 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2398 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2399 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2400 edid->pnp_id[3] = '\0';
2401
2402 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2403 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2404 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2405 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2406 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2407 if (serial_number > 0)
2408 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2409
2410 /* parse EDID data */
2411 for (i = EDID_OFFSET_DATA_BLOCKS;
2412 i <= EDID_OFFSET_LAST_BLOCK;
2413 i += 18) {
2414 /* ignore pixel clock data */
2415 if (data[i] != 0)
2416 continue;
2417 if (data[i+2] != 0)
2418 continue;
2419
2420 /* any useful blocks? */
2421 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2422 edid_parse_string(&data[i+5],
2423 edid->monitor_name);
2424 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2425 edid_parse_string(&data[i+5],
2426 edid->serial_number);
2427 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2428 edid_parse_string(&data[i+5],
2429 edid->eisa_id);
2430 }
2431 }
2432 return 0;
2433}
2434
2435static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002436find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002437 struct drm_output *output,
2438 drmModeConnector *connector)
2439{
2440 drmModePropertyBlobPtr edid_blob = NULL;
2441 drmModePropertyPtr property;
2442 int i;
2443 int rc;
2444
2445 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002446 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002447 if (!property)
2448 continue;
2449 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2450 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002452 connector->prop_values[i]);
2453 }
2454 drmModeFreeProperty(property);
2455 }
2456 if (!edid_blob)
2457 return;
2458
2459 rc = edid_parse(&output->edid,
2460 edid_blob->data,
2461 edid_blob->length);
2462 if (!rc) {
2463 weston_log("EDID data '%s', '%s', '%s'\n",
2464 output->edid.pnp_id,
2465 output->edid.monitor_name,
2466 output->edid.serial_number);
2467 if (output->edid.pnp_id[0] != '\0')
2468 output->base.make = output->edid.pnp_id;
2469 if (output->edid.monitor_name[0] != '\0')
2470 output->base.model = output->edid.monitor_name;
2471 if (output->edid.serial_number[0] != '\0')
2472 output->base.serial_number = output->edid.serial_number;
2473 }
2474 drmModeFreePropertyBlob(edid_blob);
2475}
2476
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002477
2478
2479static int
2480parse_modeline(const char *s, drmModeModeInfo *mode)
2481{
2482 char hsync[16];
2483 char vsync[16];
2484 float fclock;
2485
2486 mode->type = DRM_MODE_TYPE_USERDEF;
2487 mode->hskew = 0;
2488 mode->vscan = 0;
2489 mode->vrefresh = 0;
2490 mode->flags = 0;
2491
Rob Bradford307e09e2013-07-26 16:29:40 +01002492 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002493 &fclock,
2494 &mode->hdisplay,
2495 &mode->hsync_start,
2496 &mode->hsync_end,
2497 &mode->htotal,
2498 &mode->vdisplay,
2499 &mode->vsync_start,
2500 &mode->vsync_end,
2501 &mode->vtotal, hsync, vsync) != 11)
2502 return -1;
2503
2504 mode->clock = fclock * 1000;
2505 if (strcmp(hsync, "+hsync") == 0)
2506 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2507 else if (strcmp(hsync, "-hsync") == 0)
2508 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2509 else
2510 return -1;
2511
2512 if (strcmp(vsync, "+vsync") == 0)
2513 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2514 else if (strcmp(vsync, "-vsync") == 0)
2515 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2516 else
2517 return -1;
2518
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002519 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2520 mode->hdisplay, mode->vdisplay, fclock);
2521
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002522 return 0;
2523}
2524
Rob Bradford66bd9f52013-06-25 18:56:42 +01002525static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002526setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002527 struct weston_output *output,
2528 const char *s)
2529{
2530 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002531 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002532 struct udev_seat *seat;
2533
Giulio Camuffo954f1832014-10-11 18:27:30 +03002534 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002535 if (!seat)
2536 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002537
Derek Foreman0720ea32015-07-15 13:00:35 -05002538 seat->base.output = output;
2539
Derek Foreman1281a362015-07-31 16:55:32 -05002540 pointer = weston_seat_get_pointer(&seat->base);
2541 if (pointer)
2542 weston_pointer_clamp(pointer,
2543 &pointer->x,
2544 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002545 }
2546}
2547
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002548static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002549parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002550{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002551 int ret = 0;
2552
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002553 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002554 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002555 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002556 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002557 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002558 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002559 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002560 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002561 else {
2562 weston_log("fatal: unrecognized pixel format: %s\n", s);
2563 ret = -1;
2564 }
2565
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002566 return ret;
2567}
2568
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002569/**
2570 * Choose suitable mode for an output
2571 *
2572 * Find the most suitable mode to use for initial setup (or reconfiguration on
2573 * hotplug etc) for a DRM output.
2574 *
2575 * @param output DRM output to choose mode for
2576 * @param kind Strategy and preference to use when choosing mode
2577 * @param width Desired width for this output
2578 * @param height Desired height for this output
2579 * @param current_mode Mode currently being displayed on this output
2580 * @param modeline Manually-entered mode (may be NULL)
2581 * @returns A mode from the output's mode list, or NULL if none available
2582 */
2583static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002584drm_output_choose_initial_mode(struct drm_backend *backend,
2585 struct drm_output *output,
2586 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002587 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002588 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002589{
2590 struct drm_mode *preferred = NULL;
2591 struct drm_mode *current = NULL;
2592 struct drm_mode *configured = NULL;
2593 struct drm_mode *best = NULL;
2594 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002595 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002596 int32_t width = 0;
2597 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002598 uint32_t refresh = 0;
2599 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002600
Armin Krezović08368132016-09-30 14:11:05 +02002601 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002602 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2603 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002604 width = -1;
2605
Armin Krezović08368132016-09-30 14:11:05 +02002606 if (parse_modeline(modeline, &drm_modeline) == 0) {
2607 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002608 if (!configured)
2609 return NULL;
2610 } else {
2611 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002612 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002613 }
2614 }
2615 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002616
2617 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002618 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002619 height == drm_mode->base.height &&
2620 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002621 configured = drm_mode;
2622
comic fans7a5c5622016-03-17 14:29:27 +02002623 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002624 sizeof *current_mode) == 0)
2625 current = drm_mode;
2626
2627 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2628 preferred = drm_mode;
2629
2630 best = drm_mode;
2631 }
2632
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002633 if (current == NULL && current_mode->clock != 0) {
2634 current = drm_output_add_mode(output, current_mode);
2635 if (!current)
2636 return NULL;
2637 }
2638
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002639 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002640 configured = current;
2641
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002642 if (configured)
2643 return configured;
2644
2645 if (preferred)
2646 return preferred;
2647
2648 if (current)
2649 return current;
2650
2651 if (best)
2652 return best;
2653
2654 weston_log("no available modes for %s\n", output->base.name);
2655 return NULL;
2656}
2657
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002658static int
2659connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2660 drmModeModeInfo *mode)
2661{
2662 drmModeEncoder *encoder;
2663 drmModeCrtc *crtc;
2664
2665 /* Get the current mode on the crtc that's currently driving
2666 * this connector. */
2667 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2668 memset(mode, 0, sizeof *mode);
2669 if (encoder != NULL) {
2670 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2671 drmModeFreeEncoder(encoder);
2672 if (crtc == NULL)
2673 return -1;
2674 if (crtc->mode_valid)
2675 *mode = crtc->mode;
2676 drmModeFreeCrtc(crtc);
2677 }
2678
2679 return 0;
2680}
2681
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002682static int
Armin Krezović08368132016-09-30 14:11:05 +02002683drm_output_set_mode(struct weston_output *base,
2684 enum weston_drm_backend_output_mode mode,
2685 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002686{
Armin Krezović08368132016-09-30 14:11:05 +02002687 struct drm_output *output = to_drm_output(base);
2688 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002689
Armin Krezović445b41b2016-10-09 23:48:16 +02002690 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002691 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002692
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002693 output->base.make = "unknown";
2694 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002695 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002696
Armin Krezović08368132016-09-30 14:11:05 +02002697 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002698 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002699
Armin Krezović08368132016-09-30 14:11:05 +02002700 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002701 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002702 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002703
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002704 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002705 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002706
Armin Krezović08368132016-09-30 14:11:05 +02002707 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2708 output->base.native_mode = output->base.current_mode;
2709 output->base.native_scale = output->base.current_scale;
2710
2711 output->base.mm_width = output->connector->mmWidth;
2712 output->base.mm_height = output->connector->mmHeight;
2713
2714 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02002715}
2716
2717static void
2718drm_output_set_gbm_format(struct weston_output *base,
2719 const char *gbm_format)
2720{
2721 struct drm_output *output = to_drm_output(base);
2722 struct drm_backend *b = to_drm_backend(base->compositor);
2723
2724 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2725 output->gbm_format = b->gbm_format;
2726}
2727
2728static void
2729drm_output_set_seat(struct weston_output *base,
2730 const char *seat)
2731{
2732 struct drm_output *output = to_drm_output(base);
2733 struct drm_backend *b = to_drm_backend(base->compositor);
2734
2735 setup_output_seat_constraint(b, &output->base,
2736 seat ? seat : "");
2737}
2738
2739static int
2740drm_output_enable(struct weston_output *base)
2741{
2742 struct drm_output *output = to_drm_output(base);
2743 struct drm_backend *b = to_drm_backend(base->compositor);
2744 struct weston_mode *m;
2745
2746 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002747
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002748 if (b->pageflip_timeout)
2749 drm_output_pageflip_timer_create(output);
2750
Giulio Camuffo954f1832014-10-11 18:27:30 +03002751 if (b->use_pixman) {
2752 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002753 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002754 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002755 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002756 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002757 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002758 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002759 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002760
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002761 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002762 weston_log("Initialized backlight, device %s\n",
2763 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002764 output->base.set_backlight = drm_set_backlight;
2765 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002766 } else {
2767 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002768 }
2769
Jonas Ådahle5a12252013-04-05 23:07:11 +02002770 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002771 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002772 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002773 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002774 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002775
Richard Hughese7299962013-05-01 21:52:12 +01002776 output->base.gamma_size = output->original_crtc->gamma_size;
2777 output->base.set_gamma = drm_output_set_gamma;
2778
Armin Krezović08368132016-09-30 14:11:05 +02002779 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2780
2781 find_and_parse_output_edid(b, output, output->connector);
Armin Krezović9bcf4c52017-02-08 13:55:26 +01002782 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2783 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanen0079a942017-03-24 15:46:23 +02002784 output->base.connection_internal = true;
Armin Krezović08368132016-09-30 14:11:05 +02002785
Derek Foremanb0427562016-02-05 15:55:20 -06002786 weston_plane_init(&output->cursor_plane, b->compositor,
2787 INT32_MIN, INT32_MIN);
Daniel Stone4202e502016-10-24 13:26:42 +01002788 weston_plane_init(&output->scanout_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002789
Giulio Camuffo954f1832014-10-11 18:27:30 +03002790 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
Daniel Stone4202e502016-10-24 13:26:42 +01002791 weston_compositor_stack_plane(b->compositor, &output->scanout_plane,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002792 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002793
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002794 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002795 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002796 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002797 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002798 m->width, m->height, m->refresh / 1000.0,
2799 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2800 ", preferred" : "",
2801 m->flags & WL_OUTPUT_MODE_CURRENT ?
2802 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002803 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002804 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002805
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03002806 output->state_invalid = true;
2807
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002808 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002809
2810err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002811 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002812
David Herrmann0f0d54e2011-12-08 17:05:45 +01002813 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002814}
2815
Jesse Barnes58ef3792012-02-23 09:45:49 -05002816static void
Armin Krezović08368132016-09-30 14:11:05 +02002817drm_output_deinit(struct weston_output *base)
2818{
2819 struct drm_output *output = to_drm_output(base);
2820 struct drm_backend *b = to_drm_backend(base->compositor);
2821
Daniel Stonef30a18c2017-04-04 17:54:31 +01002822 /* output->fb_last and output->fb_pending must not be set here;
Daniel Stone6e7a9612017-04-04 17:54:26 +01002823 * destroy_pending/disable_pending exist to guarantee exactly this. */
Daniel Stonef30a18c2017-04-04 17:54:31 +01002824 assert(!output->fb_last);
Daniel Stone5bb8f582017-04-04 17:54:28 +01002825 assert(!output->fb_pending);
2826 drm_fb_unref(output->fb_current);
2827 output->fb_current = NULL;
Daniel Stone6e7a9612017-04-04 17:54:26 +01002828
Daniel Stone3e661f72016-11-04 17:24:06 +00002829 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002830 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002831 else
2832 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002833
Daniel Stone4202e502016-10-24 13:26:42 +01002834 weston_plane_release(&output->scanout_plane);
Armin Krezović08368132016-09-30 14:11:05 +02002835 weston_plane_release(&output->cursor_plane);
2836
2837 drmModeFreeProperty(output->dpms_prop);
2838
2839 /* Turn off hardware cursor */
2840 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2841}
2842
2843static void
2844drm_output_destroy(struct weston_output *base)
2845{
2846 struct drm_output *output = to_drm_output(base);
2847 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002848 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02002849 drmModeCrtcPtr origcrtc = output->original_crtc;
2850
2851 if (output->page_flip_pending) {
2852 output->destroy_pending = 1;
2853 weston_log("destroy output while page flip pending\n");
2854 return;
2855 }
2856
2857 if (output->base.enabled)
2858 drm_output_deinit(&output->base);
2859
Armin Krezović445b41b2016-10-09 23:48:16 +02002860 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2861 base.link) {
2862 wl_list_remove(&drm_mode->base.link);
2863 free(drm_mode);
2864 }
2865
Armin Krezović08368132016-09-30 14:11:05 +02002866 if (origcrtc) {
2867 /* Restore original CRTC state */
2868 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2869 origcrtc->x, origcrtc->y,
2870 &output->connector_id, 1, &origcrtc->mode);
2871 drmModeFreeCrtc(origcrtc);
2872 }
2873
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002874 if (output->pageflip_timer)
2875 wl_event_source_remove(output->pageflip_timer);
2876
Armin Krezović08368132016-09-30 14:11:05 +02002877 weston_output_destroy(&output->base);
2878
2879 drmModeFreeConnector(output->connector);
2880
2881 if (output->backlight)
2882 backlight_destroy(output->backlight);
2883
Armin Krezović08368132016-09-30 14:11:05 +02002884 free(output);
2885}
2886
2887static int
2888drm_output_disable(struct weston_output *base)
2889{
2890 struct drm_output *output = to_drm_output(base);
2891 struct drm_backend *b = to_drm_backend(base->compositor);
2892
2893 if (output->page_flip_pending) {
2894 output->disable_pending = 1;
2895 return -1;
2896 }
2897
2898 if (output->base.enabled)
2899 drm_output_deinit(&output->base);
2900
2901 output->disable_pending = 0;
2902
2903 weston_log("Disabling output %s\n", output->base.name);
2904 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2905 0, 0, 0, 0, 0, NULL);
2906
2907 return 0;
2908}
2909
2910/**
2911 * Create a Weston output structure
2912 *
2913 * Given a DRM connector, create a matching drm_output structure and add it
2914 * to Weston's output list. It also takes ownership of the connector, which
2915 * is released when output is destroyed.
2916 *
2917 * @param b Weston backend structure
2918 * @param resources DRM resources for this device
2919 * @param connector DRM connector to use for this new output
2920 * @param drm_device udev device pointer
2921 * @returns 0 on success, or -1 on failure
2922 */
2923static int
2924create_output_for_connector(struct drm_backend *b,
2925 drmModeRes *resources,
2926 drmModeConnector *connector,
2927 struct udev_device *drm_device)
2928{
2929 struct drm_output *output;
Armin Krezović445b41b2016-10-09 23:48:16 +02002930 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002931 int i;
2932
2933 i = find_crtc_for_connector(b, resources, connector);
2934 if (i < 0) {
2935 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02002936 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002937 }
2938
2939 output = zalloc(sizeof *output);
2940 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02002941 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002942
2943 output->connector = connector;
2944 output->crtc_id = resources->crtcs[i];
2945 output->pipe = i;
2946 output->connector_id = connector->connector_id;
2947
2948 output->backlight = backlight_init(drm_device,
2949 connector->connector_type);
2950
Armin Krezović445b41b2016-10-09 23:48:16 +02002951 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2952
Armin Krezović08368132016-09-30 14:11:05 +02002953 output->base.enable = drm_output_enable;
2954 output->base.destroy = drm_output_destroy;
2955 output->base.disable = drm_output_disable;
2956 output->base.name = make_connector_name(connector);
2957
2958 output->destroy_pending = 0;
2959 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02002960
Armin Krezović40087402016-09-30 14:11:12 +02002961 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002962
2963 wl_list_init(&output->base.mode_list);
2964
2965 for (i = 0; i < output->connector->count_modes; i++) {
2966 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
2967 if (!drm_mode) {
2968 drm_output_destroy(&output->base);
2969 return -1;
2970 }
2971 }
2972
Armin Krezović08368132016-09-30 14:11:05 +02002973 weston_compositor_add_pending_output(&output->base, b->compositor);
2974
2975 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02002976
2977err:
2978 drmModeFreeConnector(connector);
2979
2980 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002981}
2982
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002983static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002984create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002985{
2986 drmModeConnector *connector;
2987 drmModeRes *resources;
2988 int i;
2989
Giulio Camuffo954f1832014-10-11 18:27:30 +03002990 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002991 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002992 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002993 return -1;
2994 }
2995
Giulio Camuffo954f1832014-10-11 18:27:30 +03002996 b->min_width = resources->min_width;
2997 b->max_width = resources->max_width;
2998 b->min_height = resources->min_height;
2999 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05003000
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003001 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003002 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02003003 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003004 if (connector == NULL)
3005 continue;
3006
3007 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003008 (b->connector == 0 ||
3009 connector->connector_id == b->connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003010 if (create_output_for_connector(b, resources,
Armin Krezović445b41b2016-10-09 23:48:16 +02003011 connector, drm_device) < 0)
Benjamin Franzke439d9862011-10-07 08:20:53 +02003012 continue;
Armin Krezović08368132016-09-30 14:11:05 +02003013 } else {
3014 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02003015 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003016 }
3017
Armin Krezović08368132016-09-30 14:11:05 +02003018 if (wl_list_empty(&b->compositor->output_list) &&
3019 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02003020 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003021
3022 drmModeFreeResources(resources);
3023
3024 return 0;
3025}
3026
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003027static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003028update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003029{
3030 drmModeConnector *connector;
3031 drmModeRes *resources;
3032 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003033 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003034 int i;
3035
Giulio Camuffo954f1832014-10-11 18:27:30 +03003036 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003037 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003038 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003039 return;
3040 }
3041
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003042 connected = calloc(resources->count_connectors, sizeof(uint32_t));
3043 if (!connected) {
3044 drmModeFreeResources(resources);
3045 return;
3046 }
3047
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003048 /* collect new connects */
3049 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003050 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02003051
Giulio Camuffo954f1832014-10-11 18:27:30 +03003052 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01003053 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003054 continue;
3055
David Herrmann7551cff2011-12-08 17:05:43 +01003056 if (connector->connection != DRM_MODE_CONNECTED) {
3057 drmModeFreeConnector(connector);
3058 continue;
3059 }
3060
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003061 if (b->connector && (b->connector != connector_id)) {
3062 drmModeFreeConnector(connector);
3063 continue;
3064 }
3065
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003066 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003067
Daniel Stonec0ec7592017-02-09 13:58:35 +00003068 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02003069 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00003070 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003071 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00003072
3073 create_output_for_connector(b, resources,
3074 connector, drm_device);
3075 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003076 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003077
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003078 wl_list_for_each_safe(output, next, &b->compositor->output_list,
3079 base.link) {
3080 bool disconnected = true;
3081
3082 for (i = 0; i < resources->count_connectors; i++) {
3083 if (connected[i] == output->connector_id) {
3084 disconnected = false;
3085 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003086 }
3087 }
Armin Krezović08368132016-09-30 14:11:05 +02003088
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003089 if (!disconnected)
3090 continue;
3091
3092 weston_log("connector %d disconnected\n", output->connector_id);
3093 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003094 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003095
3096 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
3097 base.link) {
3098 bool disconnected = true;
3099
3100 for (i = 0; i < resources->count_connectors; i++) {
3101 if (connected[i] == output->connector_id) {
3102 disconnected = false;
3103 break;
3104 }
3105 }
3106
3107 if (!disconnected)
3108 continue;
3109
3110 weston_log("connector %d disconnected\n", output->connector_id);
3111 drm_output_destroy(&output->base);
3112 }
3113
3114 free(connected);
3115 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003116}
3117
3118static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003119udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003120{
David Herrmannd7488c22012-03-11 20:05:21 +01003121 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01003122 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01003123
3124 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003125 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01003126 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003127
David Herrmann6ac52db2012-03-11 20:05:22 +01003128 val = udev_device_get_property_value(device, "HOTPLUG");
3129 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003130 return 0;
3131
David Herrmann6ac52db2012-03-11 20:05:22 +01003132 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003133}
3134
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003135static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003136udev_drm_event(int fd, uint32_t mask, void *data)
3137{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003138 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003139 struct udev_device *event;
3140
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003142
Giulio Camuffo954f1832014-10-11 18:27:30 +03003143 if (udev_event_is_hotplug(b, event))
3144 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003145
3146 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003147
3148 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003149}
3150
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003151static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003152drm_restore(struct weston_compositor *ec)
3153{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003154 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003155}
3156
3157static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003158drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003159{
Armin Krezović545dba62016-08-05 15:54:18 +02003160 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003161
Giulio Camuffo954f1832014-10-11 18:27:30 +03003162 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003163
Giulio Camuffo954f1832014-10-11 18:27:30 +03003164 wl_event_source_remove(b->udev_drm_source);
3165 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003166
Giulio Camuffo954f1832014-10-11 18:27:30 +03003167 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003168
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003169 weston_compositor_shutdown(ec);
3170
Giulio Camuffo954f1832014-10-11 18:27:30 +03003171 if (b->gbm)
3172 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003173
Giulio Camuffo954f1832014-10-11 18:27:30 +03003174 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003175
Giulio Camuffo954f1832014-10-11 18:27:30 +03003176 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003177 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003178}
3179
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003180static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003181session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003182{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003183 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003184 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone08d4edf2017-04-04 17:54:34 +01003185 struct drm_plane *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003186 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003187
Giulio Camuffo954f1832014-10-11 18:27:30 +03003188 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003189 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003190 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003191 weston_compositor_damage_all(compositor);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003192
3193 wl_list_for_each(output, &compositor->output_list, base.link)
3194 output->state_invalid = true;
3195
Giulio Camuffo954f1832014-10-11 18:27:30 +03003196 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003197 } else {
3198 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003199 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003200
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003201 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003202
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003203 /* If we have a repaint scheduled (either from a
3204 * pending pageflip or the idle handler), make sure we
3205 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003206 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003207 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003208 * back, we schedule a repaint, which will process
3209 * pending frame callbacks. */
3210
Giulio Camuffo954f1832014-10-11 18:27:30 +03003211 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003212 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003213 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003214 }
3215
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003217 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003218
Giulio Camuffo954f1832014-10-11 18:27:30 +03003219 wl_list_for_each(sprite, &b->sprite_list, link)
3220 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003221 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003222 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003223 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003224 };
3225}
3226
Daniel Stoneefa504f2016-12-19 16:48:20 +00003227/**
3228 * Determines whether or not a device is capable of modesetting. If successful,
3229 * sets b->drm.fd and b->drm.filename to the opened device.
3230 */
3231static bool
3232drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3233{
3234 const char *filename = udev_device_get_devnode(device);
3235 const char *sysnum = udev_device_get_sysnum(device);
3236 drmModeRes *res;
3237 int id, fd;
3238
3239 if (!filename)
3240 return false;
3241
3242 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3243 if (fd < 0)
3244 return false;
3245
3246 res = drmModeGetResources(fd);
3247 if (!res)
3248 goto out_fd;
3249
3250 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3251 res->count_encoders <= 0)
3252 goto out_res;
3253
3254 if (sysnum)
3255 id = atoi(sysnum);
3256 if (!sysnum || id < 0) {
3257 weston_log("couldn't get sysnum for device %s\n", filename);
3258 goto out_res;
3259 }
3260
3261 /* We can be called successfully on multiple devices; if we have,
3262 * clean up old entries. */
3263 if (b->drm.fd >= 0)
3264 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3265 free(b->drm.filename);
3266
3267 b->drm.fd = fd;
3268 b->drm.id = id;
3269 b->drm.filename = strdup(filename);
3270
Sergi Granellceb59812017-03-28 12:44:04 +02003271 drmModeFreeResources(res);
3272
Daniel Stoneefa504f2016-12-19 16:48:20 +00003273 return true;
3274
3275out_res:
3276 drmModeFreeResources(res);
3277out_fd:
3278 weston_launcher_close(b->compositor->launcher, fd);
3279 return false;
3280}
3281
David Herrmann0af066f2012-10-29 19:21:16 +01003282/*
3283 * Find primary GPU
3284 * Some systems may have multiple DRM devices attached to a single seat. This
3285 * function loops over all devices and tries to find a PCI device with the
3286 * boot_vga sysfs attribute set to 1.
3287 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003288 * Devices are also vetted to make sure they are are capable of modesetting,
3289 * rather than pure render nodes (GPU with no display), or pure
3290 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003291 */
3292static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003293find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003294{
3295 struct udev_enumerate *e;
3296 struct udev_list_entry *entry;
3297 const char *path, *device_seat, *id;
3298 struct udev_device *device, *drm_device, *pci;
3299
Giulio Camuffo954f1832014-10-11 18:27:30 +03003300 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003301 udev_enumerate_add_match_subsystem(e, "drm");
3302 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3303
3304 udev_enumerate_scan_devices(e);
3305 drm_device = NULL;
3306 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003307 bool is_boot_vga = false;
3308
David Herrmann0af066f2012-10-29 19:21:16 +01003309 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003311 if (!device)
3312 continue;
3313 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3314 if (!device_seat)
3315 device_seat = default_seat;
3316 if (strcmp(device_seat, seat)) {
3317 udev_device_unref(device);
3318 continue;
3319 }
3320
3321 pci = udev_device_get_parent_with_subsystem_devtype(device,
3322 "pci", NULL);
3323 if (pci) {
3324 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003325 if (id && !strcmp(id, "1"))
3326 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003327 }
3328
Daniel Stoneefa504f2016-12-19 16:48:20 +00003329 /* If we already have a modesetting-capable device, and this
3330 * device isn't our boot-VGA device, we aren't going to use
3331 * it. */
3332 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003333 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003334 continue;
3335 }
3336
3337 /* Make sure this device is actually capable of modesetting;
3338 * if this call succeeds, b->drm.{fd,filename} will be set,
3339 * and any old values freed. */
3340 if (!drm_device_is_kms(b, device)) {
3341 udev_device_unref(device);
3342 continue;
3343 }
3344
3345 /* There can only be one boot_vga device, and we try to use it
3346 * at all costs. */
3347 if (is_boot_vga) {
3348 if (drm_device)
3349 udev_device_unref(drm_device);
3350 drm_device = device;
3351 break;
3352 }
3353
3354 /* Per the (!is_boot_vga && drm_device) test above, we only
3355 * trump existing saved devices with boot-VGA devices, so if
3356 * we end up here, this must be the first device we've seen. */
3357 assert(!drm_device);
3358 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003359 }
3360
Daniel Stoneefa504f2016-12-19 16:48:20 +00003361 /* If we're returning a device to use, we must have an open FD for
3362 * it. */
3363 assert(!!drm_device == (b->drm.fd >= 0));
3364
David Herrmann0af066f2012-10-29 19:21:16 +01003365 udev_enumerate_unref(e);
3366 return drm_device;
3367}
3368
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003369static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003370planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3371 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003372{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003373 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003374
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003375 switch (key) {
3376 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003377 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003378 break;
3379 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003380 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003381 break;
3382 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003383 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003384 break;
3385 default:
3386 break;
3387 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003388}
3389
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003390#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003391static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003392recorder_destroy(struct drm_output *output)
3393{
3394 vaapi_recorder_destroy(output->recorder);
3395 output->recorder = NULL;
3396
3397 output->base.disable_planes--;
3398
3399 wl_list_remove(&output->recorder_frame_listener.link);
3400 weston_log("[libva recorder] done\n");
3401}
3402
3403static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003404recorder_frame_notify(struct wl_listener *listener, void *data)
3405{
3406 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003407 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003408 int fd, ret;
3409
3410 output = container_of(listener, struct drm_output,
3411 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003412 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003413
3414 if (!output->recorder)
3415 return;
3416
Daniel Stone5bb8f582017-04-04 17:54:28 +01003417 ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003418 DRM_CLOEXEC, &fd);
3419 if (ret) {
3420 weston_log("[libva recorder] "
3421 "failed to create prime fd for front buffer\n");
3422 return;
3423 }
3424
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003425 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone5bb8f582017-04-04 17:54:28 +01003426 output->fb_current->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003427 if (ret < 0) {
3428 weston_log("[libva recorder] aborted: %m\n");
3429 recorder_destroy(output);
3430 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003431}
3432
3433static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003434create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003435 const char *filename)
3436{
3437 int fd;
3438 drm_magic_t magic;
3439
Giulio Camuffo954f1832014-10-11 18:27:30 +03003440 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003441 if (fd < 0)
3442 return NULL;
3443
3444 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003445 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003446
3447 return vaapi_recorder_create(fd, width, height, filename);
3448}
3449
3450static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003451recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003452 void *data)
3453{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003454 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003455 struct drm_output *output;
3456 int width, height;
3457
Giulio Camuffo954f1832014-10-11 18:27:30 +03003458 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003459 struct drm_output, base.link);
3460
3461 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003462 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003463 weston_log("failed to start vaapi recorder: "
3464 "output format not supported\n");
3465 return;
3466 }
3467
Hardeningff39efa2013-09-18 23:56:35 +02003468 width = output->base.current_mode->width;
3469 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003470
3471 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003472 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003473 if (!output->recorder) {
3474 weston_log("failed to create vaapi recorder\n");
3475 return;
3476 }
3477
3478 output->base.disable_planes++;
3479
3480 output->recorder_frame_listener.notify = recorder_frame_notify;
3481 wl_signal_add(&output->base.frame_signal,
3482 &output->recorder_frame_listener);
3483
3484 weston_output_schedule_repaint(&output->base);
3485
3486 weston_log("[libva recorder] initialized\n");
3487 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003488 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003489 }
3490}
3491#else
3492static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003493recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003494 void *data)
3495{
3496 weston_log("Compiled without libva support\n");
3497}
3498#endif
3499
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003500static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003501switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003502{
3503 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003504 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003505
Giulio Camuffo954f1832014-10-11 18:27:30 +03003506 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003507 return;
3508
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003509 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3510
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003511 weston_log("Switching to GL renderer\n");
3512
Giulio Camuffo954f1832014-10-11 18:27:30 +03003513 b->gbm = create_gbm_device(b->drm.fd);
3514 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003515 weston_log("Failed to create gbm device. "
3516 "Aborting renderer switch\n");
3517 return;
3518 }
3519
Giulio Camuffo954f1832014-10-11 18:27:30 +03003520 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003521 pixman_renderer_output_destroy(&output->base);
3522
Giulio Camuffo954f1832014-10-11 18:27:30 +03003523 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003524
Giulio Camuffo954f1832014-10-11 18:27:30 +03003525 if (drm_backend_create_gl_renderer(b) < 0) {
3526 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003527 weston_log("Failed to create GL renderer. Quitting.\n");
3528 /* FIXME: we need a function to shutdown cleanly */
3529 assert(0);
3530 }
3531
Giulio Camuffo954f1832014-10-11 18:27:30 +03003532 wl_list_for_each(output, &b->compositor->output_list, base.link)
3533 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003534
Giulio Camuffo954f1832014-10-11 18:27:30 +03003535 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003536
3537 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3538 if (linux_dmabuf_setup(b->compositor) < 0)
3539 weston_log("Error: initializing dmabuf "
3540 "support failed.\n");
3541 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003542}
3543
3544static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003545renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3546 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003547{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003548 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003549 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003550
Giulio Camuffo954f1832014-10-11 18:27:30 +03003551 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003552}
3553
Armin Krezović08368132016-09-30 14:11:05 +02003554static const struct weston_drm_output_api api = {
3555 drm_output_set_mode,
3556 drm_output_set_gbm_format,
3557 drm_output_set_seat,
3558};
3559
Giulio Camuffo954f1832014-10-11 18:27:30 +03003560static struct drm_backend *
3561drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003562 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003563{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003564 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003565 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003566 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003567 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003568 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003569
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003570 weston_log("initializing drm backend\n");
3571
Giulio Camuffo954f1832014-10-11 18:27:30 +03003572 b = zalloc(sizeof *b);
3573 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003574 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003575
Daniel Stoneefa504f2016-12-19 16:48:20 +00003576 b->drm.fd = -1;
3577
Pekka Paalanen68583832015-05-19 09:53:16 +03003578 /*
3579 * KMS support for hardware planes cannot properly synchronize
3580 * without nuclear page flip. Without nuclear/atomic, hw plane
3581 * and cursor plane updates would either tear or cause extra
3582 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003583 * to a fraction. For cursors, it's not so bad, so they are
3584 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003585 *
3586 * These can be enabled again when nuclear/atomic support lands.
3587 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003588 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003589 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003590 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003591 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003592
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003593 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3594 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003595
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003596 if (config->seat_id)
3597 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003598
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003599 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003600 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3601 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003602 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003603 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003604 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003605 goto err_compositor;
3606 }
3607
Giulio Camuffo954f1832014-10-11 18:27:30 +03003608 b->udev = udev_new();
3609 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003610 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003611 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003612 }
3613
Giulio Camuffo954f1832014-10-11 18:27:30 +03003614 b->session_listener.notify = session_notify;
3615 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003616
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003617 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003618 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003619 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003620 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003621 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003622
Daniel Stoneefa504f2016-12-19 16:48:20 +00003623 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003624 weston_log("failed to initialize kms\n");
3625 goto err_udev_dev;
3626 }
3627
Giulio Camuffo954f1832014-10-11 18:27:30 +03003628 if (b->use_pixman) {
3629 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003630 weston_log("failed to initialize pixman renderer\n");
3631 goto err_udev_dev;
3632 }
3633 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003634 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003635 weston_log("failed to initialize egl\n");
3636 goto err_udev_dev;
3637 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003638 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003639
Giulio Camuffo954f1832014-10-11 18:27:30 +03003640 b->base.destroy = drm_destroy;
3641 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003642
Bob Ham91880f12016-01-12 10:21:47 +00003643 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003644
Giulio Camuffo954f1832014-10-11 18:27:30 +03003645 wl_list_init(&b->sprite_list);
3646 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003647
Giulio Camuffo954f1832014-10-11 18:27:30 +03003648 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003649 compositor, b->udev, seat_id,
3650 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003651 weston_log("failed to create input devices\n");
3652 goto err_sprite;
3653 }
3654
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003655 b->connector = config->connector;
3656
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003657 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003658 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003659 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003660 }
3661
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003662 /* A this point we have some idea of whether or not we have a working
3663 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003664 if (!b->cursors_are_broken)
3665 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003666
Giulio Camuffo954f1832014-10-11 18:27:30 +03003667 loop = wl_display_get_event_loop(compositor->wl_display);
3668 b->drm_source =
3669 wl_event_loop_add_fd(loop, b->drm.fd,
3670 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003671
Giulio Camuffo954f1832014-10-11 18:27:30 +03003672 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3673 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003674 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003675 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003676 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003677 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003678 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003679 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003680 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003681 udev_monitor_get_fd(b->udev_monitor),
3682 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003683
Giulio Camuffo954f1832014-10-11 18:27:30 +03003684 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003685 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003686 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003687 }
3688
Daniel Stonea96b93c2012-06-22 14:04:37 +01003689 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003690
Giulio Camuffo954f1832014-10-11 18:27:30 +03003691 weston_compositor_add_debug_binding(compositor, KEY_O,
3692 planes_binding, b);
3693 weston_compositor_add_debug_binding(compositor, KEY_C,
3694 planes_binding, b);
3695 weston_compositor_add_debug_binding(compositor, KEY_V,
3696 planes_binding, b);
3697 weston_compositor_add_debug_binding(compositor, KEY_Q,
3698 recorder_binding, b);
3699 weston_compositor_add_debug_binding(compositor, KEY_W,
3700 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003701
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003702 if (compositor->renderer->import_dmabuf) {
3703 if (linux_dmabuf_setup(compositor) < 0)
3704 weston_log("Error: initializing dmabuf "
3705 "support failed.\n");
3706 }
3707
Giulio Camuffo954f1832014-10-11 18:27:30 +03003708 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003709
Armin Krezović08368132016-09-30 14:11:05 +02003710 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3711 &api, sizeof(api));
3712
3713 if (ret < 0) {
3714 weston_log("Failed to register output API.\n");
3715 goto err_udev_monitor;
3716 }
3717
Giulio Camuffo954f1832014-10-11 18:27:30 +03003718 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003719
3720err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003721 wl_event_source_remove(b->udev_drm_source);
3722 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003723err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003724 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003725err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003726 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003727err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003728 if (b->gbm)
3729 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003730 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003731err_udev_dev:
3732 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003733err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003734 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003735err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003736 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003737err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003738 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003739 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003740 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003741}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003742
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003743static void
3744config_init_to_defaults(struct weston_drm_backend_config *config)
3745{
3746}
3747
Giulio Camuffo954f1832014-10-11 18:27:30 +03003748WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003749weston_backend_init(struct weston_compositor *compositor,
3750 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003751{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003752 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003753 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003754
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003755 if (config_base == NULL ||
3756 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3757 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3758 weston_log("drm backend config structure is invalid\n");
3759 return -1;
3760 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003761
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003762 config_init_to_defaults(&config);
3763 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003764
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003765 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003766 if (b == NULL)
3767 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003768
Giulio Camuffo954f1832014-10-11 18:27:30 +03003769 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003770}