blob: e4c91f22107720f59b96c419acbe863425323ea6 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Daniel Stone0b70fa42017-04-04 17:54:23 +010057#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100059#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010060#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030061#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030063#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050064#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040065
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030066#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
68#endif
69
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030070#ifndef DRM_CAP_CURSOR_WIDTH
71#define DRM_CAP_CURSOR_WIDTH 0x8
72#endif
73
74#ifndef DRM_CAP_CURSOR_HEIGHT
75#define DRM_CAP_CURSOR_HEIGHT 0x9
76#endif
77
78#ifndef GBM_BO_USE_CURSOR
79#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
80#endif
81
Giulio Camuffo954f1832014-10-11 18:27:30 +030082struct drm_backend {
83 struct weston_backend base;
84 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085
86 struct udev *udev;
87 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010089 struct udev_monitor *udev_monitor;
90 struct wl_event_source *udev_drm_source;
91
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010093 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010094 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030095 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010096 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020097 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000105 int min_width, max_width;
106 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Rob Bradfordd355b802013-05-31 18:09:55 +0100117 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300118
Daniel Stone70d337d2015-06-16 18:42:23 +0100119 int32_t cursor_width;
120 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000121
122 uint32_t connector;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000123 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400124};
125
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400126struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500127 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400128 drmModeModeInfo mode_info;
129};
130
Daniel Stonefc175a72017-04-04 17:54:22 +0100131enum drm_fb_type {
132 BUFFER_INVALID = 0, /**< never used */
133 BUFFER_CLIENT, /**< directly sourced from client */
134 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
135 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
136};
137
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300138struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100139 enum drm_fb_type type;
140
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200141 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100142 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000143 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200144 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200145 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200146
147 /* Used by gbm fbs */
148 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100149 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200150
151 /* Used by dumb fbs */
152 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300153};
154
Richard Hughes2b2092a2013-04-24 14:58:02 +0100155struct drm_edid {
156 char eisa_id[13];
157 char monitor_name[13];
158 char pnp_id[5];
159 char serial_number[13];
160};
161
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400162struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200163 struct weston_output base;
164 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165
Daniel Stone17339232015-11-28 12:09:47 +0000166 uint32_t crtc_id; /* object ID to pass to DRM functions */
167 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700169 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100170 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300171 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100172 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200173
Daniel Stone36609c72015-06-18 07:49:02 +0100174 enum dpms_enum dpms;
175
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300176 int vblank_pending;
177 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800178 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200179 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300180
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100181 struct gbm_surface *gbm_surface;
182 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400183 struct weston_plane cursor_plane;
184 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500185 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400186 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300187 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200188 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200189
190 struct drm_fb *dumb[2];
191 pixman_image_t *image[2];
192 int current_image;
193 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300194
195 struct vaapi_recorder *recorder;
196 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000197
198 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400199};
200
Jesse Barnes58ef3792012-02-23 09:45:49 -0500201/*
202 * An output has a primary display plane plus zero or more sprites for
203 * blending display contents.
204 */
205struct drm_sprite {
206 struct wl_list link;
207
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400208 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500209
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200210 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300211 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300212 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500213
Jesse Barnes58ef3792012-02-23 09:45:49 -0500214 uint32_t possible_crtcs;
215 uint32_t plane_id;
216 uint32_t count_formats;
217
218 int32_t src_x, src_y;
219 uint32_t src_w, src_h;
220 uint32_t dest_x, dest_y;
221 uint32_t dest_w, dest_h;
222
223 uint32_t formats[];
224};
225
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300226static struct gl_renderer_interface *gl_renderer;
227
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500228static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400229
Armin Krezović545dba62016-08-05 15:54:18 +0200230static inline struct drm_output *
231to_drm_output(struct weston_output *base)
232{
233 return container_of(base, struct drm_output, base);
234}
235
236static inline struct drm_backend *
237to_drm_backend(struct weston_compositor *base)
238{
239 return container_of(base->backend, struct drm_backend, base);
240}
241
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000242static int
243pageflip_timeout(void *data) {
244 /*
245 * Our timer just went off, that means we're not receiving drm
246 * page flip events anymore for that output. Let's gracefully exit
247 * weston with a return value so devs can debug what's going on.
248 */
249 struct drm_output *output = data;
250 struct weston_compositor *compositor = output->base.compositor;
251
252 weston_log("Pageflip timeout reached on output %s, your "
253 "driver is probably buggy! Exiting.\n",
254 output->base.name);
255 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
256
257 return 0;
258}
259
260/* Creates the pageflip timer. Note that it isn't armed by default */
261static int
262drm_output_pageflip_timer_create(struct drm_output *output)
263{
264 struct wl_event_loop *loop = NULL;
265 struct weston_compositor *ec = output->base.compositor;
266
267 loop = wl_display_get_event_loop(ec->wl_display);
268 assert(loop);
269 output->pageflip_timer = wl_event_loop_add_timer(loop,
270 pageflip_timeout,
271 output);
272
273 if (output->pageflip_timer == NULL) {
274 weston_log("creating drm pageflip timer failed: %m\n");
275 return -1;
276 }
277
278 return 0;
279}
280
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400281static void
282drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400283
Mario Kleinerf507ec32015-06-21 21:25:14 +0200284static void
285drm_output_update_msc(struct drm_output *output, unsigned int seq);
286
Jesse Barnes58ef3792012-02-23 09:45:49 -0500287static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000288drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500289{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000290 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500291}
292
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000293static struct drm_output *
294drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
295{
296 struct drm_output *output;
297
298 wl_list_for_each(output, &b->compositor->output_list, base.link) {
299 if (output->crtc_id == crtc_id)
300 return output;
301 }
302
303 wl_list_for_each(output, &b->compositor->pending_output_list,
304 base.link) {
305 if (output->crtc_id == crtc_id)
306 return output;
307 }
308
309 return NULL;
310}
311
Daniel Stonec0ec7592017-02-09 13:58:35 +0000312static struct drm_output *
313drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
314{
315 struct drm_output *output;
316
317 wl_list_for_each(output, &b->compositor->output_list, base.link) {
318 if (output->connector_id == connector_id)
319 return output;
320 }
321
322 wl_list_for_each(output, &b->compositor->pending_output_list,
323 base.link) {
324 if (output->connector_id == connector_id)
325 return output;
326 }
327
328 return NULL;
329}
330
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300331static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100332drm_fb_destroy(struct drm_fb *fb)
333{
334 if (fb->fb_id != 0)
335 drmModeRmFB(fb->fd, fb->fb_id);
336 weston_buffer_reference(&fb->buffer_ref, NULL);
337 free(fb);
338}
339
340static void
341drm_fb_destroy_dumb(struct drm_fb *fb)
342{
343 struct drm_mode_destroy_dumb destroy_arg;
344
345 assert(fb->type == BUFFER_PIXMAN_DUMB);
346
347 if (fb->map && fb->size > 0)
348 munmap(fb->map, fb->size);
349
350 memset(&destroy_arg, 0, sizeof(destroy_arg));
351 destroy_arg.handle = fb->handle;
352 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
353
354 drm_fb_destroy(fb);
355}
356
357static void
358drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300359{
360 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300361
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100362 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT);
363 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300364}
365
366static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000367drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300368 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200369{
370 struct drm_fb *fb;
371 int ret;
372
373 struct drm_mode_create_dumb create_arg;
374 struct drm_mode_destroy_dumb destroy_arg;
375 struct drm_mode_map_dumb map_arg;
376
Peter Huttererf3d62272013-08-08 11:57:05 +1000377 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200378 if (!fb)
379 return NULL;
380
Daniel Stone0b70fa42017-04-04 17:54:23 +0100381 fb->format = pixel_format_get_info(format);
382 if (!fb->format) {
383 weston_log("failed to look up format 0x%lx\n",
384 (unsigned long) format);
385 goto err_fb;
386 }
387
388 if (!fb->format->depth || !fb->format->bpp) {
389 weston_log("format 0x%lx is not compatible with dumb buffers\n",
390 (unsigned long) format);
391 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300392 }
393
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700394 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100395 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200396 create_arg.width = width;
397 create_arg.height = height;
398
Giulio Camuffo954f1832014-10-11 18:27:30 +0300399 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200400 if (ret)
401 goto err_fb;
402
Daniel Stonefc175a72017-04-04 17:54:22 +0100403 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200404 fb->handle = create_arg.handle;
405 fb->stride = create_arg.pitch;
406 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000407 fb->width = width;
408 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300409 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200410
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300411 ret = -1;
412
413 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700414 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300415
416 handles[0] = fb->handle;
417 pitches[0] = fb->stride;
418 offsets[0] = 0;
419
420 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100421 fb->format->format,
422 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300423 &fb->fb_id, 0);
424 if (ret) {
425 weston_log("addfb2 failed: %m\n");
426 b->no_addfb2 = 1;
427 }
428 }
429
430 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100431 ret = drmModeAddFB(b->drm.fd, width, height,
432 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300433 fb->stride, fb->handle, &fb->fb_id);
434 }
435
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200436 if (ret)
437 goto err_bo;
438
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700439 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200440 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400441 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200442 if (ret)
443 goto err_add_fb;
444
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500445 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300446 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200447 if (fb->map == MAP_FAILED)
448 goto err_add_fb;
449
450 return fb;
451
452err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300453 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200454err_bo:
455 memset(&destroy_arg, 0, sizeof(destroy_arg));
456 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300457 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200458err_fb:
459 free(fb);
460 return NULL;
461}
462
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200463static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100464drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
465 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300466{
467 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500468 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300469 int ret;
470
Daniel Stonefc175a72017-04-04 17:54:22 +0100471 if (fb) {
472 assert(fb->type == type);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300473 return fb;
Daniel Stonefc175a72017-04-04 17:54:22 +0100474 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300475
Bryce Harringtonde16d892014-11-20 22:21:57 -0800476 fb = zalloc(sizeof *fb);
477 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200478 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300479
Daniel Stonefc175a72017-04-04 17:54:22 +0100480 fb->type = type;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300481 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300482
Daniel Stonec8c917c2016-11-14 17:45:58 +0000483 fb->width = gbm_bo_get_width(bo);
484 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200485 fb->stride = gbm_bo_get_stride(bo);
486 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100487 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000488 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300489 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300490
Daniel Stone0b70fa42017-04-04 17:54:23 +0100491 if (!fb->format) {
492 weston_log("couldn't look up format 0x%lx\n",
493 (unsigned long) format);
494 goto err_free;
495 }
496
Daniel Stonec8c917c2016-11-14 17:45:58 +0000497 if (backend->min_width > fb->width ||
498 fb->width > backend->max_width ||
499 backend->min_height > fb->height ||
500 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200501 weston_log("bo geometry out of bounds\n");
502 goto err_free;
503 }
504
505 ret = -1;
506
Giulio Camuffo954f1832014-10-11 18:27:30 +0300507 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200508 handles[0] = fb->handle;
509 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200510 offsets[0] = 0;
511
Daniel Stonec8c917c2016-11-14 17:45:58 +0000512 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200513 format, handles, pitches, offsets,
514 &fb->fb_id, 0);
515 if (ret) {
516 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300517 backend->no_addfb2 = 1;
518 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200519 }
520 }
521
Daniel Stone0b70fa42017-04-04 17:54:23 +0100522 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000523 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100524 fb->format->depth, fb->format->bpp,
525 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200526
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300527 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200528 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200529 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300530 }
531
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100532 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300533
534 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200535
536err_free:
537 free(fb);
538 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300539}
540
541static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500542drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200543{
Pekka Paalanende685b82012-12-04 15:58:12 +0200544 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100545 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200546 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200547}
548
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200549static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100550drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200551{
552 if (!fb)
553 return;
554
Daniel Stonefc175a72017-04-04 17:54:22 +0100555 switch (fb->type) {
556 case BUFFER_PIXMAN_DUMB:
557 /* nothing: pixman buffers are destroyed manually */
558 break;
559 case BUFFER_CLIENT:
560 gbm_bo_destroy(fb->bo);
561 break;
562 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100563 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100564 break;
565 default:
566 assert(NULL);
567 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200568 }
569}
570
Daniel Stone90648872016-10-21 18:08:37 +0100571static int
572drm_view_transform_supported(struct weston_view *ev)
573{
574 return !ev->transform.enabled ||
575 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
576}
577
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500578static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200579drm_output_check_scanout_format(struct drm_output *output,
580 struct weston_surface *es, struct gbm_bo *bo)
581{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200582 uint32_t format;
583 pixman_region32_t r;
584
585 format = gbm_bo_get_format(bo);
586
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700587 if (format == GBM_FORMAT_ARGB8888) {
588 /* We can scanout an ARGB buffer if the surface's
589 * opaque region covers the whole output, but we have
590 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800591 pixman_region32_init_rect(&r, 0, 0,
592 output->base.width,
593 output->base.height);
594 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200595
596 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500597 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200598
599 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500600 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700601
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100602 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700603 return format;
604
605 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200606}
607
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400608static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200609drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500611{
Armin Krezović545dba62016-08-05 15:54:18 +0200612 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500613 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200614 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300615 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500616 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500617
Daniel Stone90648872016-10-21 18:08:37 +0100618 /* Don't import buffers which span multiple outputs. */
619 if (ev->output_mask != (1u << output->base.id))
620 return NULL;
621
Daniel Stone296d7a92016-10-21 18:05:37 +0100622 /* We use GBM to import buffers. */
623 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400624 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500625
Daniel Stone296d7a92016-10-21 18:05:37 +0100626 if (buffer == NULL)
627 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100628 if (wl_shm_buffer_get(buffer->resource))
629 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100630
631 /* Make sure our view is exactly compatible with the output. */
632 if (ev->geometry.x != output->base.x ||
633 ev->geometry.y != output->base.y)
634 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100635 if (buffer->width != output->base.current_mode->width ||
636 buffer->height != output->base.current_mode->height)
637 return NULL;
638
Daniel Stone296d7a92016-10-21 18:05:37 +0100639 if (ev->transform.enabled)
640 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200641 if (ev->geometry.scissor_enabled)
642 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100643 if (viewport->buffer.transform != output->base.transform)
644 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +0100645 if (viewport->buffer.scale != output->base.current_scale)
646 return NULL;
647 if (!drm_view_transform_supported(ev))
648 return NULL;
649
650 if (ev->alpha != 1.0f)
651 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +0100652
Giulio Camuffo954f1832014-10-11 18:27:30 +0300653 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700654 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500655
Rob Bradford9b101872012-09-14 23:25:41 +0100656 /* Unable to use the buffer for scanout */
657 if (!bo)
658 return NULL;
659
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500661 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300662 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400663 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300664 }
665
Daniel Stonefc175a72017-04-04 17:54:22 +0100666 output->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300667 if (!output->next) {
668 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400669 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300670 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500671
Pekka Paalanende685b82012-12-04 15:58:12 +0200672 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500673
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400674 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500675}
676
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500677static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200678drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400679{
Armin Krezović545dba62016-08-05 15:54:18 +0200680 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300681 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400682
Giulio Camuffo954f1832014-10-11 18:27:30 +0300683 output->base.compositor->renderer->repaint_output(&output->base,
684 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400685
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100686 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300687 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200688 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400689 return;
690 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300691
Daniel Stonefc175a72017-04-04 17:54:22 +0100692 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format,
693 BUFFER_GBM_SURFACE);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300694 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200695 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100696 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300697 return;
698 }
Daniel Stone05a5ac22017-04-04 17:54:25 +0100699 output->next->gbm_surface = output->gbm_surface;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400700}
701
702static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200703drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
704{
705 struct weston_compositor *ec = output->base.compositor;
706 pixman_region32_t total_damage, previous_damage;
707
708 pixman_region32_init(&total_damage);
709 pixman_region32_init(&previous_damage);
710
711 pixman_region32_copy(&previous_damage, damage);
712
713 pixman_region32_union(&total_damage, damage, &output->previous_damage);
714 pixman_region32_copy(&output->previous_damage, &previous_damage);
715
716 output->current_image ^= 1;
717
718 output->next = output->dumb[output->current_image];
719 pixman_renderer_output_set_buffer(&output->base,
720 output->image[output->current_image]);
721
722 ec->renderer->repaint_output(&output->base, &total_damage);
723
724 pixman_region32_fini(&total_damage);
725 pixman_region32_fini(&previous_damage);
726}
727
728static void
729drm_output_render(struct drm_output *output, pixman_region32_t *damage)
730{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300731 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200732 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200733
Giulio Camuffo954f1832014-10-11 18:27:30 +0300734 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200735 drm_output_render_pixman(output, damage);
736 else
737 drm_output_render_gl(output, damage);
738
Giulio Camuffo954f1832014-10-11 18:27:30 +0300739 pixman_region32_subtract(&c->primary_plane.damage,
740 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200741}
742
743static void
Richard Hughese7299962013-05-01 21:52:12 +0100744drm_output_set_gamma(struct weston_output *output_base,
745 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
746{
747 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200748 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300749 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200750 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100751
752 /* check */
753 if (output_base->gamma_size != size)
754 return;
755 if (!output->original_crtc)
756 return;
757
Giulio Camuffo954f1832014-10-11 18:27:30 +0300758 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100759 output->crtc_id,
760 size, r, g, b);
761 if (rc)
762 weston_log("set gamma failed: %m\n");
763}
764
Bryce Harringtonada4f072015-06-30 13:25:46 -0700765/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200766 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700767 * The pipe parameter indicates which CRTC is in use. Knowing this, we
768 * can determine which vblank sequence type to use for it. Traditional
769 * cards had only two CRTCs, with CRTC 0 using no special flags, and
770 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
771 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200772 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700773 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
774 * 0-31. If this is non-zero it indicates we're dealing with a
775 * multi-gpu situation and we need to calculate the vblank sync
776 * using DRM_BLANK_HIGH_CRTC_MASK.
777 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300778static unsigned int
779drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200780{
781 if (output->pipe > 1)
782 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
783 DRM_VBLANK_HIGH_CRTC_MASK;
784 else if (output->pipe > 0)
785 return DRM_VBLANK_SECONDARY;
786 else
787 return 0;
788}
789
David Herrmann1edf44c2013-10-22 17:11:26 +0200790static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500791drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +0000792 pixman_region32_t *damage,
793 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100794{
Armin Krezović545dba62016-08-05 15:54:18 +0200795 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300796 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200797 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500798 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400799 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500800 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100801
Armin Krezović08368132016-09-30 14:11:05 +0200802 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200803 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800804
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300805 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400806 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300807 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200808 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100809
Hardeningff39efa2013-09-18 23:56:35 +0200810 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200811 if (!output->current ||
812 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300813 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300814 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400815 &output->connector_id, 1,
816 &mode->mode_info);
817 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200818 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200819 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400820 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300821 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200822 }
823
Giulio Camuffo954f1832014-10-11 18:27:30 +0300824 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300825 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500826 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200827 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200828 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500829 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100830
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300831 output->page_flip_pending = 1;
832
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000833 if (output->pageflip_timer)
834 wl_event_source_timer_update(output->pageflip_timer,
835 backend->pageflip_timeout);
836
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400837 drm_output_set_cursor(output);
838
Jesse Barnes58ef3792012-02-23 09:45:49 -0500839 /*
840 * Now, update all the sprite surfaces
841 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300842 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200843 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500844 drmVBlank vbl = {
845 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
846 .request.sequence = 1,
847 };
848
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200849 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000850 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500851 continue;
852
Giulio Camuffo954f1832014-10-11 18:27:30 +0300853 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200854 fb_id = s->next->fb_id;
855
Giulio Camuffo954f1832014-10-11 18:27:30 +0300856 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200857 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500858 s->dest_x, s->dest_y,
859 s->dest_w, s->dest_h,
860 s->src_x, s->src_y,
861 s->src_w, s->src_h);
862 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200863 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500864 ret, strerror(errno));
865
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200866 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500867
Jesse Barnes58ef3792012-02-23 09:45:49 -0500868 /*
869 * Queue a vblank signal so we know when the surface
870 * becomes active on the display or has been replaced.
871 */
872 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300873 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500874 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200875 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500876 ret, strerror(errno));
877 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300878
879 s->output = output;
880 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500881 }
882
David Herrmann1edf44c2013-10-22 17:11:26 +0200883 return 0;
884
885err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800886 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200887 if (output->next) {
Daniel Stone05a5ac22017-04-04 17:54:25 +0100888 drm_fb_unref(output->next);
David Herrmann1edf44c2013-10-22 17:11:26 +0200889 output->next = NULL;
890 }
891
892 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400893}
894
895static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200896drm_output_start_repaint_loop(struct weston_output *output_base)
897{
Armin Krezović545dba62016-08-05 15:54:18 +0200898 struct drm_output *output = to_drm_output(output_base);
899 struct drm_backend *backend =
900 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200901 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200902 struct timespec ts, tnow;
903 struct timespec vbl2now;
904 int64_t refresh_nsec;
905 int ret;
906 drmVBlank vbl = {
907 .request.type = DRM_VBLANK_RELATIVE,
908 .request.sequence = 0,
909 .request.signal = 0,
910 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300911
Armin Krezović08368132016-09-30 14:11:05 +0200912 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800913 return;
914
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300915 if (!output->current) {
916 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200917 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300918 }
919
Mario Kleinerf507ec32015-06-21 21:25:14 +0200920 /* Try to get current msc and timestamp via instant query */
921 vbl.request.type |= drm_waitvblank_pipe(output);
922 ret = drmWaitVBlank(backend->drm.fd, &vbl);
923
924 /* Error ret or zero timestamp means failure to get valid timestamp */
925 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
926 ts.tv_sec = vbl.reply.tval_sec;
927 ts.tv_nsec = vbl.reply.tval_usec * 1000;
928
929 /* Valid timestamp for most recent vblank - not stale?
930 * Stale ts could happen on Linux 3.17+, so make sure it
931 * is not older than 1 refresh duration since now.
932 */
933 weston_compositor_read_presentation_clock(backend->compositor,
934 &tnow);
935 timespec_sub(&vbl2now, &tnow, &ts);
936 refresh_nsec =
937 millihz_to_nsec(output->base.current_mode->refresh);
938 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
939 drm_output_update_msc(output, vbl.reply.sequence);
940 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200941 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200942 return;
943 }
944 }
945
946 /* Immediate query didn't provide valid timestamp.
947 * Use pageflip fallback.
948 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300949 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200950
Giulio Camuffo954f1832014-10-11 18:27:30 +0300951 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200952 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
953 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200954 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200955 }
David Herrmann3c688c52013-10-22 17:11:25 +0200956
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000957 if (output->pageflip_timer)
958 wl_event_source_timer_update(output->pageflip_timer,
959 backend->pageflip_timeout);
960
David Herrmann3c688c52013-10-22 17:11:25 +0200961 return;
962
963finish_frame:
964 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +0000965 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200966 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200967}
968
969static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400970drm_output_update_msc(struct drm_output *output, unsigned int seq)
971{
972 uint64_t msc_hi = output->base.msc >> 32;
973
974 if (seq < (output->base.msc & 0xffffffff))
975 msc_hi++;
976
977 output->base.msc = (msc_hi << 32) + seq;
978}
979
980static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500981vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
982 void *data)
983{
984 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300985 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400986 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200987 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
988 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300989
Pekka Paalanen641307c2014-09-23 22:08:47 -0400990 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300991 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500992
Daniel Stone05a5ac22017-04-04 17:54:25 +0100993 drm_fb_unref(s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200994 s->current = s->next;
995 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300996
997 if (!output->page_flip_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000998 /* Stop the pageflip timer instead of rearming it here */
999 if (output->pageflip_timer)
1000 wl_event_source_timer_update(output->pageflip_timer, 0);
1001
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001002 ts.tv_sec = sec;
1003 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001004 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001005 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001006}
1007
1008static void
Armin Krezović08368132016-09-30 14:11:05 +02001009drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001010
1011static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001012page_flip_handler(int fd, unsigned int frame,
1013 unsigned int sec, unsigned int usec, void *data)
1014{
Armin Krezović545dba62016-08-05 15:54:18 +02001015 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001016 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001017 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1018 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1019 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001020
Pekka Paalanen641307c2014-09-23 22:08:47 -04001021 drm_output_update_msc(output, frame);
1022
Jonas Ådahle5a12252013-04-05 23:07:11 +02001023 /* We don't set page_flip_pending on start_repaint_loop, in that case
1024 * we just want to page flip to the current buffer to get an accurate
1025 * timestamp */
1026 if (output->page_flip_pending) {
Daniel Stone05a5ac22017-04-04 17:54:25 +01001027 drm_fb_unref(output->current);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001028 output->current = output->next;
1029 output->next = NULL;
1030 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001031
Jonas Ådahle5a12252013-04-05 23:07:11 +02001032 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001033
Xiong Zhangabd5d472013-10-11 14:43:07 +08001034 if (output->destroy_pending)
1035 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001036 else if (output->disable_pending)
1037 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001038 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001039 /* Stop the pageflip timer instead of rearming it here */
1040 if (output->pageflip_timer)
1041 wl_event_source_timer_update(output->pageflip_timer, 0);
1042
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001043 ts.tv_sec = sec;
1044 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001045 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001046
1047 /* We can't call this from frame_notify, because the output's
1048 * repaint needed flag is cleared just after that */
1049 if (output->recorder)
1050 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001051 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001052}
1053
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001054static uint32_t
1055drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001057{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001058 uint32_t i, format;
1059
1060 format = gbm_bo_get_format(bo);
1061
1062 if (format == GBM_FORMAT_ARGB8888) {
1063 pixman_region32_t r;
1064
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001065 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001066 ev->surface->width,
1067 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001069
1070 if (!pixman_region32_not_empty(&r))
1071 format = GBM_FORMAT_XRGB8888;
1072
1073 pixman_region32_fini(&r);
1074 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001075
1076 for (i = 0; i < s->count_formats; i++)
1077 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001078 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001079
1080 return 0;
1081}
1082
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001083static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001084drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001086{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001087 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001088 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001089 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001090 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001091 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001092 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001093 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001094 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001095 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001096 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001097 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001098 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001099
Giulio Camuffo954f1832014-10-11 18:27:30 +03001100 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001101 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001102
Daniel Stone296d7a92016-10-21 18:05:37 +01001103 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001104 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001105 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001106
Daniel Stone296d7a92016-10-21 18:05:37 +01001107 /* We can only import GBM buffers. */
1108 if (b->gbm == NULL)
1109 return NULL;
1110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001112 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001113 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001114 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001115 return NULL;
1116
Daniel Stone296d7a92016-10-21 18:05:37 +01001117 if (viewport->buffer.transform != output->base.transform)
1118 return NULL;
1119 if (viewport->buffer.scale != output->base.current_scale)
1120 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001122 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001123
Daniel Stone296d7a92016-10-21 18:05:37 +01001124 if (ev->alpha != 1.0f)
1125 return NULL;
1126
Giulio Camuffo954f1832014-10-11 18:27:30 +03001127 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +00001128 if (!drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001129 continue;
1130
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001131 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001132 found = 1;
1133 break;
1134 }
1135 }
1136
1137 /* No sprites available */
1138 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001139 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001140
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001141 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001142#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001143 /* XXX: TODO:
1144 *
1145 * Use AddFB2 directly, do not go via GBM.
1146 * Add support for multiplanar formats.
1147 * Both require refactoring in the DRM-backend to
1148 * support a mix of gbm_bos and drmfbs.
1149 */
1150 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001151 .fd = dmabuf->attributes.fd[0],
1152 .width = dmabuf->attributes.width,
1153 .height = dmabuf->attributes.height,
1154 .stride = dmabuf->attributes.stride[0],
1155 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001156 };
1157
Micah Fedkec8890122017-02-01 15:28:23 -05001158 /* XXX: TODO:
1159 *
1160 * Currently the buffer is rejected if any dmabuf attribute
1161 * flag is set. This keeps us from passing an inverted /
1162 * interlaced / bottom-first buffer (or any other type that may
1163 * be added in the future) through to an overlay. Ultimately,
1164 * these types of buffers should be handled through buffer
1165 * transforms and not as spot-checks requiring specific
1166 * knowledge. */
1167 if (dmabuf->attributes.n_planes != 1 ||
1168 dmabuf->attributes.offset[0] != 0 ||
1169 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001170 return NULL;
1171
1172 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1173 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001174#else
1175 return NULL;
1176#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001177 } else {
1178 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1179 buffer_resource, GBM_BO_USE_SCANOUT);
1180 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001181 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001182 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001185 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001186 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001187 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001188 }
1189
Daniel Stonefc175a72017-04-04 17:54:22 +01001190 s->next = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001191 if (!s->next) {
1192 gbm_bo_destroy(bo);
1193 return NULL;
1194 }
1195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001199 s->plane.x = box->x1;
1200 s->plane.y = box->y1;
1201
Jesse Barnes58ef3792012-02-23 09:45:49 -05001202 /*
1203 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001204 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001205 * for us already).
1206 */
1207 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001209 &output->base.region);
1210 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001211 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001212 tbox = weston_transformed_rect(output->base.width,
1213 output->base.height,
1214 output->base.transform,
1215 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001216 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001217 s->dest_x = tbox.x1;
1218 s->dest_y = tbox.y1;
1219 s->dest_w = tbox.x2 - tbox.x1;
1220 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001221 pixman_region32_fini(&dest_rect);
1222
1223 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001225 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001226 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_from_global_fixed(ev,
1229 wl_fixed_from_int(box->x1),
1230 wl_fixed_from_int(box->y1),
1231 &sx1, &sy1);
1232 weston_view_from_global_fixed(ev,
1233 wl_fixed_from_int(box->x2),
1234 wl_fixed_from_int(box->y2),
1235 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001236
1237 if (sx1 < 0)
1238 sx1 = 0;
1239 if (sy1 < 0)
1240 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001241 if (sx2 > wl_fixed_from_int(ev->surface->width))
1242 sx2 = wl_fixed_from_int(ev->surface->width);
1243 if (sy2 > wl_fixed_from_int(ev->surface->height))
1244 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001245
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001246 tbox.x1 = sx1;
1247 tbox.y1 = sy1;
1248 tbox.x2 = sx2;
1249 tbox.y2 = sy2;
1250
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001251 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1252 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001253 viewport->buffer.transform,
1254 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001255 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001256
1257 s->src_x = tbox.x1 << 8;
1258 s->src_y = tbox.y1 << 8;
1259 s->src_w = (tbox.x2 - tbox.x1) << 8;
1260 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001261 pixman_region32_fini(&src_rect);
1262
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001263 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001264}
1265
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001266static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001267drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001269{
Armin Krezović545dba62016-08-05 15:54:18 +02001270 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001271 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001272 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001273 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001274
Giulio Camuffo954f1832014-10-11 18:27:30 +03001275 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001276 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001277
1278 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001279 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001280
1281 /* Don't import buffers which span multiple outputs. */
1282 if (ev->output_mask != (1u << output->base.id))
1283 return NULL;
1284
1285 /* We use GBM to import SHM buffers. */
1286 if (b->gbm == NULL)
1287 return NULL;
1288
Derek Foreman6c19b692015-12-03 14:07:12 -06001289 if (ev->surface->buffer_ref.buffer == NULL)
1290 return NULL;
1291 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1292 if (!shmbuf)
1293 return NULL;
1294 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1295 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001296
1297 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1298 return NULL;
1299 if (ev->transform.enabled &&
1300 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1301 return NULL;
1302 if (viewport->buffer.scale != output->base.current_scale)
1303 return NULL;
1304 if (ev->geometry.scissor_enabled)
1305 return NULL;
1306
Derek Foreman6c19b692015-12-03 14:07:12 -06001307 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001308 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001309 return NULL;
1310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001312 weston_view_to_global_float(ev, 0, 0, &x, &y);
1313 output->cursor_plane.x = x;
1314 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001315
1316 return &output->cursor_plane;
1317}
1318
Pekka Paalanend0ead482014-06-16 12:05:40 +03001319/**
1320 * Update the image for the current cursor surface
1321 *
1322 * @param b DRM backend structure
1323 * @param bo GBM buffer object to write into
1324 * @param ev View to use for cursor image
1325 */
1326static void
1327cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1328 struct weston_view *ev)
1329{
1330 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1331 uint32_t buf[b->cursor_width * b->cursor_height];
1332 int32_t stride;
1333 uint8_t *s;
1334 int i;
1335
1336 assert(buffer && buffer->shm_buffer);
1337 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1338 assert(ev->surface->width <= b->cursor_width);
1339 assert(ev->surface->height <= b->cursor_height);
1340
1341 memset(buf, 0, sizeof buf);
1342 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1343 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1344
1345 wl_shm_buffer_begin_access(buffer->shm_buffer);
1346 for (i = 0; i < ev->surface->height; i++)
1347 memcpy(buf + i * b->cursor_width,
1348 s + i * stride,
1349 ev->surface->width * 4);
1350 wl_shm_buffer_end_access(buffer->shm_buffer);
1351
1352 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1353 weston_log("failed update cursor: %m\n");
1354}
1355
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001356static void
1357drm_output_set_cursor(struct drm_output *output)
1358{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001360 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001361 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001362 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001363 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001366 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001367 return;
1368 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001369
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001370 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001371 pixman_region32_fini(&output->cursor_plane.damage);
1372 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001373 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001374 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001375
Pekka Paalanend0ead482014-06-16 12:05:40 +03001376 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001377 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001378 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1379 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001380 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001381 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001382 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001383 }
1384
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001385 x = (output->cursor_plane.x - output->base.x) *
1386 output->base.current_scale;
1387 y = (output->cursor_plane.y - output->base.y) *
1388 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001389
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001390 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1391 weston_log("failed to move cursor: %m\n");
1392 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001393 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001394}
1395
Jesse Barnes58ef3792012-02-23 09:45:49 -05001396static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001397drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001398{
Armin Krezović545dba62016-08-05 15:54:18 +02001399 struct drm_backend *b = to_drm_backend(output_base->compositor);
1400 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001402 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001403 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001404
1405 /*
1406 * Find a surface for each sprite in the output using some heuristics:
1407 * 1) size
1408 * 2) frequency of update
1409 * 3) opacity (though some hw might support alpha blending)
1410 * 4) clipping (this can be fixed with color keys)
1411 *
1412 * The idea is to save on blitting since this should save power.
1413 * If we can get a large video surface on the sprite for example,
1414 * the main display surface may not need to update at all, and
1415 * the client buffer can be used directly for the sprite surface
1416 * as we do for flipping full screen surfaces.
1417 */
1418 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001419 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001421 output->cursor_view = NULL;
1422 output->cursor_plane.x = INT32_MIN;
1423 output->cursor_plane.y = INT32_MIN;
1424
Giulio Camuffo954f1832014-10-11 18:27:30 +03001425 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001426 struct weston_surface *es = ev->surface;
1427
1428 /* Test whether this buffer can ever go into a plane:
1429 * non-shm, or small enough to be a cursor.
1430 *
1431 * Also, keep a reference when using the pixman renderer.
1432 * That makes it possible to do a seamless switch to the GL
1433 * renderer and since the pixman renderer keeps a reference
1434 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001435 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001437 (es->buffer_ref.buffer &&
1438 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001439 (ev->surface->width <= b->cursor_width &&
1440 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001441 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001442 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001443 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001444
Jesse Barnes58ef3792012-02-23 09:45:49 -05001445 pixman_region32_init(&surface_overlap);
1446 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001448
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001449 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001450 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001451 next_plane = primary;
1452 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001454 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001456 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001458 if (next_plane == NULL)
1459 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001462
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001463 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001464 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001466
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001467 if (next_plane == primary ||
1468 next_plane == &output->cursor_plane) {
1469 /* cursor plane involves a copy */
1470 ev->psf_flags = 0;
1471 } else {
1472 /* All other planes are a direct scanout of a
1473 * single client buffer.
1474 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001475 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001476 }
1477
Jesse Barnes58ef3792012-02-23 09:45:49 -05001478 pixman_region32_fini(&surface_overlap);
1479 }
1480 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001481}
1482
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001483/**
1484 * Find the closest-matching mode for a given target
1485 *
1486 * Given a target mode, find the most suitable mode amongst the output's
1487 * current mode list to use, preferring the current mode if possible, to
1488 * avoid an expensive mode switch.
1489 *
1490 * @param output DRM output
1491 * @param target_mode Mode to attempt to match
1492 * @returns Pointer to a mode from the output's mode list
1493 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001494static struct drm_mode *
1495choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1496{
1497 struct drm_mode *tmp_mode = NULL, *mode;
1498
Hardeningff39efa2013-09-18 23:56:35 +02001499 if (output->base.current_mode->width == target_mode->width &&
1500 output->base.current_mode->height == target_mode->height &&
1501 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001502 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001503 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001504
1505 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1506 if (mode->mode_info.hdisplay == target_mode->width &&
1507 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001508 if (mode->base.refresh == target_mode->refresh ||
1509 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001510 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001511 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001512 tmp_mode = mode;
1513 }
1514 }
1515
1516 return tmp_mode;
1517}
1518
1519static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001521static void
1522drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001523static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001524drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001525static void
1526drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001527
1528static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001529drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1530{
1531 struct drm_output *output;
1532 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001533 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001534
1535 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001536 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001537 return -1;
1538 }
1539
1540 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001541 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001542 return -1;
1543 }
1544
Armin Krezović545dba62016-08-05 15:54:18 +02001545 b = to_drm_backend(output_base->compositor);
1546 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001547 drm_mode = choose_mode (output, mode);
1548
1549 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001550 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001551 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001552 }
1553
Hardeningff39efa2013-09-18 23:56:35 +02001554 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001555 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001556
Hardeningff39efa2013-09-18 23:56:35 +02001557 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001558
Hardeningff39efa2013-09-18 23:56:35 +02001559 output->base.current_mode = &drm_mode->base;
1560 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001561 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1562
Alex Wub7b8bda2012-04-17 17:20:48 +08001563 /* reset rendering stuff. */
Daniel Stone05a5ac22017-04-04 17:54:25 +01001564 drm_fb_unref(output->current);
1565 drm_fb_unref(output->next);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001566 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001567
Giulio Camuffo954f1832014-10-11 18:27:30 +03001568 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001569 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001570 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001571 weston_log("failed to init output pixman state with "
1572 "new mode\n");
1573 return -1;
1574 }
1575 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001576 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001578 weston_log("failed to init output egl state with "
1579 "new mode");
1580 return -1;
1581 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001582 }
1583
Alex Wub7b8bda2012-04-17 17:20:48 +08001584 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001585}
1586
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001587static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001588on_drm_input(int fd, uint32_t mask, void *data)
1589{
1590 drmEventContext evctx;
1591
1592 memset(&evctx, 0, sizeof evctx);
1593 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1594 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001595 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001596 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001597
1598 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001599}
1600
1601static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00001602init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001603{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001604 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00001605 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001606 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001607
Daniel Stoneefa504f2016-12-19 16:48:20 +00001608 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001609
Daniel Stoneefa504f2016-12-19 16:48:20 +00001610 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001611 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001612 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001613 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001614 clk_id = CLOCK_REALTIME;
1615
Giulio Camuffo954f1832014-10-11 18:27:30 +03001616 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001617 weston_log("Error: failed to set presentation clock %d.\n",
1618 clk_id);
1619 return -1;
1620 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001621
Daniel Stoneefa504f2016-12-19 16:48:20 +00001622 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001623 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001624 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001625 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001626 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001627
Daniel Stoneefa504f2016-12-19 16:48:20 +00001628 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001629 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001630 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001631 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001632 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001633
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001634 return 0;
1635}
1636
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001637static struct gbm_device *
1638create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001639{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001640 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001641
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001642 gl_renderer = weston_load_module("gl-renderer.so",
1643 "gl_renderer_interface");
1644 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001645 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001646
1647 /* GBM will load a dri driver, but even though they need symbols from
1648 * libglapi, in some version of Mesa they are not linked to it. Since
1649 * only the gl-renderer module links to it, the call above won't make
1650 * these symbols globally available, and loading the DRI driver fails.
1651 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1652 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1653
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001654 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001655
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001656 return gbm;
1657}
1658
Bryce Harringtonc056a982015-05-19 15:25:18 -07001659/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001660 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001661 *
1662 * This returns 0 if substitution isn't possible, but 0 might be a
1663 * legitimate format for other EGL platforms, so the caller is
1664 * responsible for checking for 0 before calling gl_renderer->create().
1665 *
1666 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1667 * but it's entirely possible we'll see this again on other implementations.
1668 */
1669static int
1670fallback_format_for(uint32_t format)
1671{
1672 switch (format) {
1673 case GBM_FORMAT_XRGB8888:
1674 return GBM_FORMAT_ARGB8888;
1675 case GBM_FORMAT_XRGB2101010:
1676 return GBM_FORMAT_ARGB2101010;
1677 default:
1678 return 0;
1679 }
1680}
1681
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001682static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001683drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001684{
Derek Foreman6d556372015-11-04 14:47:33 -06001685 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001686 b->gbm_format,
1687 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001688 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001689 };
Derek Foreman6d556372015-11-04 14:47:33 -06001690 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001691
Derek Foremanc4cfe852015-05-15 12:12:40 -05001692 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001693 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001694 if (gl_renderer->display_create(b->compositor,
1695 EGL_PLATFORM_GBM_KHR,
1696 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001697 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001698 gl_renderer->opaque_attribs,
1699 format,
1700 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001701 return -1;
1702 }
1703
1704 return 0;
1705}
1706
1707static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001708init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001709{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001710 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001711
Giulio Camuffo954f1832014-10-11 18:27:30 +03001712 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001713 return -1;
1714
Giulio Camuffo954f1832014-10-11 18:27:30 +03001715 if (drm_backend_create_gl_renderer(b) < 0) {
1716 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001717 return -1;
1718 }
1719
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001720 return 0;
1721}
1722
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001723static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001724init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001725{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001726 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001727}
1728
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001729/**
1730 * Add a mode to output's mode list
1731 *
1732 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1733 * output's mode list.
1734 *
1735 * @param output DRM output to add mode to
1736 * @param info DRM mode structure to add
1737 * @returns Newly-allocated Weston/DRM mode structure
1738 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001739static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001740drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001741{
1742 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001743 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001744
1745 mode = malloc(sizeof *mode);
1746 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001747 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001748
1749 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001750 mode->base.width = info->hdisplay;
1751 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001752
1753 /* Calculate higher precision (mHz) refresh rate */
1754 refresh = (info->clock * 1000000LL / info->htotal +
1755 info->vtotal / 2) / info->vtotal;
1756
1757 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1758 refresh *= 2;
1759 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1760 refresh /= 2;
1761 if (info->vscan > 1)
1762 refresh /= info->vscan;
1763
1764 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001765 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001766
1767 if (info->type & DRM_MODE_TYPE_PREFERRED)
1768 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1769
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001770 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1771
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001772 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001773}
1774
1775static int
1776drm_subpixel_to_wayland(int drm_value)
1777{
1778 switch (drm_value) {
1779 default:
1780 case DRM_MODE_SUBPIXEL_UNKNOWN:
1781 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1782 case DRM_MODE_SUBPIXEL_NONE:
1783 return WL_OUTPUT_SUBPIXEL_NONE;
1784 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1785 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1786 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1787 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1788 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1789 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1790 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1791 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1792 }
1793}
1794
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001795/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001796static uint32_t
1797drm_get_backlight(struct drm_output *output)
1798{
1799 long brightness, max_brightness, norm;
1800
1801 brightness = backlight_get_brightness(output->backlight);
1802 max_brightness = backlight_get_max_brightness(output->backlight);
1803
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001804 /* convert it on a scale of 0 to 255 */
1805 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001806
1807 return (uint32_t) norm;
1808}
1809
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001810/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001811static void
1812drm_set_backlight(struct weston_output *output_base, uint32_t value)
1813{
Armin Krezović545dba62016-08-05 15:54:18 +02001814 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001815 long max_brightness, new_brightness;
1816
1817 if (!output->backlight)
1818 return;
1819
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001820 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001821 return;
1822
1823 max_brightness = backlight_get_max_brightness(output->backlight);
1824
1825 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001826 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001827
1828 backlight_set_brightness(output->backlight, new_brightness);
1829}
1830
1831static drmModePropertyPtr
1832drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1833{
1834 drmModePropertyPtr props;
1835 int i;
1836
1837 for (i = 0; i < connector->count_props; i++) {
1838 props = drmModeGetProperty(fd, connector->props[i]);
1839 if (!props)
1840 continue;
1841
1842 if (!strcmp(props->name, name))
1843 return props;
1844
1845 drmModeFreeProperty(props);
1846 }
1847
1848 return NULL;
1849}
1850
1851static void
1852drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1853{
Armin Krezović545dba62016-08-05 15:54:18 +02001854 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001855 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001856 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001857 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001858
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001859 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001860 return;
1861
Daniel Stone36609c72015-06-18 07:49:02 +01001862 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1863 output->dpms_prop->prop_id, level);
1864 if (ret) {
1865 weston_log("DRM: DPMS: failed property set for %s\n",
1866 output->base.name);
1867 return;
1868 }
1869
1870 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001871}
1872
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001873static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001874 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1875 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1876 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1877 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1878 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1879 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1880 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1881 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1882 [DRM_MODE_CONNECTOR_Component] = "Component",
1883 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1884 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1885 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1886 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1887 [DRM_MODE_CONNECTOR_TV] = "TV",
1888 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001889#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001890 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1891 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001892#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001893};
1894
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001895static char *
1896make_connector_name(const drmModeConnector *con)
1897{
1898 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001899 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001900
1901 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1902 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001903
1904 if (!type_name)
1905 type_name = "UNNAMED";
1906
1907 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001908
1909 return strdup(name);
1910}
1911
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001912static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001913find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001914 drmModeRes *resources, drmModeConnector *connector)
1915{
1916 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001917 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00001918 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001919
1920 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00001921 uint32_t possible_crtcs, encoder_id, crtc_id;
1922
Giulio Camuffo954f1832014-10-11 18:27:30 +03001923 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001924 if (encoder == NULL) {
1925 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00001926 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001927 }
Daniel Stone75487c22017-01-16 14:33:38 +00001928 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001929 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00001930 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001931 drmModeFreeEncoder(encoder);
1932
1933 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00001934 if (!(possible_crtcs & (1 << i)))
1935 continue;
1936
1937 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
1938 continue;
1939
Daniel Stone75487c22017-01-16 14:33:38 +00001940 /* Try to preserve the existing
1941 * CRTC -> encoder -> connector routing; it makes
1942 * initialisation faster, and also since we have a
1943 * very dumb picking algorithm, may preserve a better
1944 * choice. */
1945 if (!connector->encoder_id ||
1946 (encoder_id == connector->encoder_id &&
1947 crtc_id == resources->crtcs[i]))
1948 return i;
1949
1950 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001951 }
1952 }
1953
Daniel Stone75487c22017-01-16 14:33:38 +00001954 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001955}
1956
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001957/* Init output state that depends on gl or gbm */
1958static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001959drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001960{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001961 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001962 output->gbm_format,
1963 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001964 };
1965 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001966
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001967 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001968 output->base.current_mode->width,
1969 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001970 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001971 GBM_BO_USE_SCANOUT |
1972 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001973 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001974 weston_log("failed to create gbm surface\n");
1975 return -1;
1976 }
1977
Derek Foremanc4cfe852015-05-15 12:12:40 -05001978 if (format[1])
1979 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001980 if (gl_renderer->output_window_create(&output->base,
1981 (EGLNativeWindowType)output->gbm_surface,
1982 output->gbm_surface,
1983 gl_renderer->opaque_attribs,
1984 format,
1985 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001986 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001987 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001988 return -1;
1989 }
1990
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001991 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001992
1993 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001994 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001995 continue;
1996
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001997 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001998 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001999 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002000 }
2001
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002002 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002003 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002004 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002005 }
2006
2007 return 0;
2008}
2009
Daniel Stone3e661f72016-11-04 17:24:06 +00002010static void
2011drm_output_fini_egl(struct drm_output *output)
2012{
2013 gl_renderer->output_destroy(&output->base);
2014 gbm_surface_destroy(output->gbm_surface);
2015}
2016
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002017static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002018drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002019{
Hardeningff39efa2013-09-18 23:56:35 +02002020 int w = output->base.current_mode->width;
2021 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002022 uint32_t format = output->gbm_format;
2023 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002024 unsigned int i;
2025
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002026 switch (format) {
2027 case GBM_FORMAT_XRGB8888:
2028 pixman_format = PIXMAN_x8r8g8b8;
2029 break;
2030 case GBM_FORMAT_RGB565:
2031 pixman_format = PIXMAN_r5g6b5;
2032 break;
2033 default:
2034 weston_log("Unsupported pixman format 0x%x\n", format);
2035 return -1;
2036 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002037
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002038 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002039 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002040 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002041 if (!output->dumb[i])
2042 goto err;
2043
2044 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002045 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002046 output->dumb[i]->map,
2047 output->dumb[i]->stride);
2048 if (!output->image[i])
2049 goto err;
2050 }
2051
2052 if (pixman_renderer_output_create(&output->base) < 0)
2053 goto err;
2054
2055 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002056 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002057
2058 return 0;
2059
2060err:
2061 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2062 if (output->dumb[i])
2063 drm_fb_destroy_dumb(output->dumb[i]);
2064 if (output->image[i])
2065 pixman_image_unref(output->image[i]);
2066
2067 output->dumb[i] = NULL;
2068 output->image[i] = NULL;
2069 }
2070
2071 return -1;
2072}
2073
2074static void
2075drm_output_fini_pixman(struct drm_output *output)
2076{
2077 unsigned int i;
2078
2079 pixman_renderer_output_destroy(&output->base);
2080 pixman_region32_fini(&output->previous_damage);
2081
2082 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2083 drm_fb_destroy_dumb(output->dumb[i]);
2084 pixman_image_unref(output->image[i]);
2085 output->dumb[i] = NULL;
2086 output->image[i] = NULL;
2087 }
2088}
2089
Richard Hughes2b2092a2013-04-24 14:58:02 +01002090static void
2091edid_parse_string(const uint8_t *data, char text[])
2092{
2093 int i;
2094 int replaced = 0;
2095
2096 /* this is always 12 bytes, but we can't guarantee it's null
2097 * terminated or not junk. */
2098 strncpy(text, (const char *) data, 12);
2099
Bryce Harrington9c7de162015-08-28 13:04:26 -07002100 /* guarantee our new string is null-terminated */
2101 text[12] = '\0';
2102
Richard Hughes2b2092a2013-04-24 14:58:02 +01002103 /* remove insane chars */
2104 for (i = 0; text[i] != '\0'; i++) {
2105 if (text[i] == '\n' ||
2106 text[i] == '\r') {
2107 text[i] = '\0';
2108 break;
2109 }
2110 }
2111
2112 /* ensure string is printable */
2113 for (i = 0; text[i] != '\0'; i++) {
2114 if (!isprint(text[i])) {
2115 text[i] = '-';
2116 replaced++;
2117 }
2118 }
2119
2120 /* if the string is random junk, ignore the string */
2121 if (replaced > 4)
2122 text[0] = '\0';
2123}
2124
2125#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2126#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2127#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2128#define EDID_OFFSET_DATA_BLOCKS 0x36
2129#define EDID_OFFSET_LAST_BLOCK 0x6c
2130#define EDID_OFFSET_PNPID 0x08
2131#define EDID_OFFSET_SERIAL 0x0c
2132
2133static int
2134edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2135{
2136 int i;
2137 uint32_t serial_number;
2138
2139 /* check header */
2140 if (length < 128)
2141 return -1;
2142 if (data[0] != 0x00 || data[1] != 0xff)
2143 return -1;
2144
2145 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2146 * /--08--\/--09--\
2147 * 7654321076543210
2148 * |\---/\---/\---/
2149 * R C1 C2 C3 */
2150 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2151 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2152 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2153 edid->pnp_id[3] = '\0';
2154
2155 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2156 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2157 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2158 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2159 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2160 if (serial_number > 0)
2161 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2162
2163 /* parse EDID data */
2164 for (i = EDID_OFFSET_DATA_BLOCKS;
2165 i <= EDID_OFFSET_LAST_BLOCK;
2166 i += 18) {
2167 /* ignore pixel clock data */
2168 if (data[i] != 0)
2169 continue;
2170 if (data[i+2] != 0)
2171 continue;
2172
2173 /* any useful blocks? */
2174 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2175 edid_parse_string(&data[i+5],
2176 edid->monitor_name);
2177 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2178 edid_parse_string(&data[i+5],
2179 edid->serial_number);
2180 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2181 edid_parse_string(&data[i+5],
2182 edid->eisa_id);
2183 }
2184 }
2185 return 0;
2186}
2187
2188static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002190 struct drm_output *output,
2191 drmModeConnector *connector)
2192{
2193 drmModePropertyBlobPtr edid_blob = NULL;
2194 drmModePropertyPtr property;
2195 int i;
2196 int rc;
2197
2198 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002199 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002200 if (!property)
2201 continue;
2202 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2203 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002204 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002205 connector->prop_values[i]);
2206 }
2207 drmModeFreeProperty(property);
2208 }
2209 if (!edid_blob)
2210 return;
2211
2212 rc = edid_parse(&output->edid,
2213 edid_blob->data,
2214 edid_blob->length);
2215 if (!rc) {
2216 weston_log("EDID data '%s', '%s', '%s'\n",
2217 output->edid.pnp_id,
2218 output->edid.monitor_name,
2219 output->edid.serial_number);
2220 if (output->edid.pnp_id[0] != '\0')
2221 output->base.make = output->edid.pnp_id;
2222 if (output->edid.monitor_name[0] != '\0')
2223 output->base.model = output->edid.monitor_name;
2224 if (output->edid.serial_number[0] != '\0')
2225 output->base.serial_number = output->edid.serial_number;
2226 }
2227 drmModeFreePropertyBlob(edid_blob);
2228}
2229
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002230
2231
2232static int
2233parse_modeline(const char *s, drmModeModeInfo *mode)
2234{
2235 char hsync[16];
2236 char vsync[16];
2237 float fclock;
2238
2239 mode->type = DRM_MODE_TYPE_USERDEF;
2240 mode->hskew = 0;
2241 mode->vscan = 0;
2242 mode->vrefresh = 0;
2243 mode->flags = 0;
2244
Rob Bradford307e09e2013-07-26 16:29:40 +01002245 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002246 &fclock,
2247 &mode->hdisplay,
2248 &mode->hsync_start,
2249 &mode->hsync_end,
2250 &mode->htotal,
2251 &mode->vdisplay,
2252 &mode->vsync_start,
2253 &mode->vsync_end,
2254 &mode->vtotal, hsync, vsync) != 11)
2255 return -1;
2256
2257 mode->clock = fclock * 1000;
2258 if (strcmp(hsync, "+hsync") == 0)
2259 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2260 else if (strcmp(hsync, "-hsync") == 0)
2261 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2262 else
2263 return -1;
2264
2265 if (strcmp(vsync, "+vsync") == 0)
2266 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2267 else if (strcmp(vsync, "-vsync") == 0)
2268 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2269 else
2270 return -1;
2271
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002272 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2273 mode->hdisplay, mode->vdisplay, fclock);
2274
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002275 return 0;
2276}
2277
Rob Bradford66bd9f52013-06-25 18:56:42 +01002278static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002279setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002280 struct weston_output *output,
2281 const char *s)
2282{
2283 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002284 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002285 struct udev_seat *seat;
2286
Giulio Camuffo954f1832014-10-11 18:27:30 +03002287 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002288 if (!seat)
2289 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002290
Derek Foreman0720ea32015-07-15 13:00:35 -05002291 seat->base.output = output;
2292
Derek Foreman1281a362015-07-31 16:55:32 -05002293 pointer = weston_seat_get_pointer(&seat->base);
2294 if (pointer)
2295 weston_pointer_clamp(pointer,
2296 &pointer->x,
2297 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002298 }
2299}
2300
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002301static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002302parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002303{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002304 int ret = 0;
2305
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002306 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002307 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002308 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002309 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002310 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002311 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002312 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002313 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002314 else {
2315 weston_log("fatal: unrecognized pixel format: %s\n", s);
2316 ret = -1;
2317 }
2318
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002319 return ret;
2320}
2321
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002322/**
2323 * Choose suitable mode for an output
2324 *
2325 * Find the most suitable mode to use for initial setup (or reconfiguration on
2326 * hotplug etc) for a DRM output.
2327 *
2328 * @param output DRM output to choose mode for
2329 * @param kind Strategy and preference to use when choosing mode
2330 * @param width Desired width for this output
2331 * @param height Desired height for this output
2332 * @param current_mode Mode currently being displayed on this output
2333 * @param modeline Manually-entered mode (may be NULL)
2334 * @returns A mode from the output's mode list, or NULL if none available
2335 */
2336static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002337drm_output_choose_initial_mode(struct drm_backend *backend,
2338 struct drm_output *output,
2339 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002340 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002341 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002342{
2343 struct drm_mode *preferred = NULL;
2344 struct drm_mode *current = NULL;
2345 struct drm_mode *configured = NULL;
2346 struct drm_mode *best = NULL;
2347 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002348 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002349 int32_t width = 0;
2350 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002351 uint32_t refresh = 0;
2352 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002353
Armin Krezović08368132016-09-30 14:11:05 +02002354 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002355 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2356 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002357 width = -1;
2358
Armin Krezović08368132016-09-30 14:11:05 +02002359 if (parse_modeline(modeline, &drm_modeline) == 0) {
2360 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002361 if (!configured)
2362 return NULL;
2363 } else {
2364 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002365 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002366 }
2367 }
2368 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002369
2370 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002371 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002372 height == drm_mode->base.height &&
2373 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002374 configured = drm_mode;
2375
comic fans7a5c5622016-03-17 14:29:27 +02002376 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002377 sizeof *current_mode) == 0)
2378 current = drm_mode;
2379
2380 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2381 preferred = drm_mode;
2382
2383 best = drm_mode;
2384 }
2385
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002386 if (current == NULL && current_mode->clock != 0) {
2387 current = drm_output_add_mode(output, current_mode);
2388 if (!current)
2389 return NULL;
2390 }
2391
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002392 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002393 configured = current;
2394
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002395 if (configured)
2396 return configured;
2397
2398 if (preferred)
2399 return preferred;
2400
2401 if (current)
2402 return current;
2403
2404 if (best)
2405 return best;
2406
2407 weston_log("no available modes for %s\n", output->base.name);
2408 return NULL;
2409}
2410
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002411static int
2412connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2413 drmModeModeInfo *mode)
2414{
2415 drmModeEncoder *encoder;
2416 drmModeCrtc *crtc;
2417
2418 /* Get the current mode on the crtc that's currently driving
2419 * this connector. */
2420 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2421 memset(mode, 0, sizeof *mode);
2422 if (encoder != NULL) {
2423 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2424 drmModeFreeEncoder(encoder);
2425 if (crtc == NULL)
2426 return -1;
2427 if (crtc->mode_valid)
2428 *mode = crtc->mode;
2429 drmModeFreeCrtc(crtc);
2430 }
2431
2432 return 0;
2433}
2434
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002435static int
Armin Krezović08368132016-09-30 14:11:05 +02002436drm_output_set_mode(struct weston_output *base,
2437 enum weston_drm_backend_output_mode mode,
2438 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002439{
Armin Krezović08368132016-09-30 14:11:05 +02002440 struct drm_output *output = to_drm_output(base);
2441 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002442
Armin Krezović445b41b2016-10-09 23:48:16 +02002443 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002444 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002445
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002446 output->base.make = "unknown";
2447 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002448 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07002449
Armin Krezović08368132016-09-30 14:11:05 +02002450 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02002451 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002452
Armin Krezović08368132016-09-30 14:11:05 +02002453 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002454 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02002455 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002456
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002457 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002458 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002459
Armin Krezović08368132016-09-30 14:11:05 +02002460 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2461 output->base.native_mode = output->base.current_mode;
2462 output->base.native_scale = output->base.current_scale;
2463
2464 output->base.mm_width = output->connector->mmWidth;
2465 output->base.mm_height = output->connector->mmHeight;
2466
2467 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02002468}
2469
2470static void
2471drm_output_set_gbm_format(struct weston_output *base,
2472 const char *gbm_format)
2473{
2474 struct drm_output *output = to_drm_output(base);
2475 struct drm_backend *b = to_drm_backend(base->compositor);
2476
2477 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2478 output->gbm_format = b->gbm_format;
2479}
2480
2481static void
2482drm_output_set_seat(struct weston_output *base,
2483 const char *seat)
2484{
2485 struct drm_output *output = to_drm_output(base);
2486 struct drm_backend *b = to_drm_backend(base->compositor);
2487
2488 setup_output_seat_constraint(b, &output->base,
2489 seat ? seat : "");
2490}
2491
2492static int
2493drm_output_enable(struct weston_output *base)
2494{
2495 struct drm_output *output = to_drm_output(base);
2496 struct drm_backend *b = to_drm_backend(base->compositor);
2497 struct weston_mode *m;
2498
2499 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002500
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002501 if (b->pageflip_timeout)
2502 drm_output_pageflip_timer_create(output);
2503
Giulio Camuffo954f1832014-10-11 18:27:30 +03002504 if (b->use_pixman) {
2505 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002506 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002507 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002508 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002509 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002510 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002511 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002512 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002513
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002514 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002515 weston_log("Initialized backlight, device %s\n",
2516 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002517 output->base.set_backlight = drm_set_backlight;
2518 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002519 } else {
2520 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002521 }
2522
Jonas Ådahle5a12252013-04-05 23:07:11 +02002523 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002524 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002525 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002526 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002527 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002528
Richard Hughese7299962013-05-01 21:52:12 +01002529 output->base.gamma_size = output->original_crtc->gamma_size;
2530 output->base.set_gamma = drm_output_set_gamma;
2531
Armin Krezović08368132016-09-30 14:11:05 +02002532 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2533
2534 find_and_parse_output_edid(b, output, output->connector);
Armin Krezović9bcf4c52017-02-08 13:55:26 +01002535 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
2536 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Armin Krezović08368132016-09-30 14:11:05 +02002537 output->base.connection_internal = 1;
2538
Derek Foremanb0427562016-02-05 15:55:20 -06002539 weston_plane_init(&output->cursor_plane, b->compositor,
2540 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002542
Giulio Camuffo954f1832014-10-11 18:27:30 +03002543 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2544 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2545 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002546
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002547 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002548 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002549 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002550 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002551 m->width, m->height, m->refresh / 1000.0,
2552 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2553 ", preferred" : "",
2554 m->flags & WL_OUTPUT_MODE_CURRENT ?
2555 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002556 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002557 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002558
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002559 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002560
2561err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002562 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002563
David Herrmann0f0d54e2011-12-08 17:05:45 +01002564 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002565}
2566
Jesse Barnes58ef3792012-02-23 09:45:49 -05002567static void
Armin Krezović08368132016-09-30 14:11:05 +02002568drm_output_deinit(struct weston_output *base)
2569{
2570 struct drm_output *output = to_drm_output(base);
2571 struct drm_backend *b = to_drm_backend(base->compositor);
2572
Daniel Stone3e661f72016-11-04 17:24:06 +00002573 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002574 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002575 else
2576 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002577
2578 weston_plane_release(&output->fb_plane);
2579 weston_plane_release(&output->cursor_plane);
2580
2581 drmModeFreeProperty(output->dpms_prop);
2582
2583 /* Turn off hardware cursor */
2584 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2585}
2586
2587static void
2588drm_output_destroy(struct weston_output *base)
2589{
2590 struct drm_output *output = to_drm_output(base);
2591 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002592 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02002593 drmModeCrtcPtr origcrtc = output->original_crtc;
2594
2595 if (output->page_flip_pending) {
2596 output->destroy_pending = 1;
2597 weston_log("destroy output while page flip pending\n");
2598 return;
2599 }
2600
2601 if (output->base.enabled)
2602 drm_output_deinit(&output->base);
2603
Armin Krezović445b41b2016-10-09 23:48:16 +02002604 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2605 base.link) {
2606 wl_list_remove(&drm_mode->base.link);
2607 free(drm_mode);
2608 }
2609
Armin Krezović08368132016-09-30 14:11:05 +02002610 if (origcrtc) {
2611 /* Restore original CRTC state */
2612 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2613 origcrtc->x, origcrtc->y,
2614 &output->connector_id, 1, &origcrtc->mode);
2615 drmModeFreeCrtc(origcrtc);
2616 }
2617
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00002618 if (output->pageflip_timer)
2619 wl_event_source_remove(output->pageflip_timer);
2620
Armin Krezović08368132016-09-30 14:11:05 +02002621 weston_output_destroy(&output->base);
2622
2623 drmModeFreeConnector(output->connector);
2624
2625 if (output->backlight)
2626 backlight_destroy(output->backlight);
2627
Armin Krezović08368132016-09-30 14:11:05 +02002628 free(output);
2629}
2630
2631static int
2632drm_output_disable(struct weston_output *base)
2633{
2634 struct drm_output *output = to_drm_output(base);
2635 struct drm_backend *b = to_drm_backend(base->compositor);
2636
2637 if (output->page_flip_pending) {
2638 output->disable_pending = 1;
2639 return -1;
2640 }
2641
2642 if (output->base.enabled)
2643 drm_output_deinit(&output->base);
2644
2645 output->disable_pending = 0;
2646
2647 weston_log("Disabling output %s\n", output->base.name);
2648 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2649 0, 0, 0, 0, 0, NULL);
2650
2651 return 0;
2652}
2653
2654/**
2655 * Create a Weston output structure
2656 *
2657 * Given a DRM connector, create a matching drm_output structure and add it
2658 * to Weston's output list. It also takes ownership of the connector, which
2659 * is released when output is destroyed.
2660 *
2661 * @param b Weston backend structure
2662 * @param resources DRM resources for this device
2663 * @param connector DRM connector to use for this new output
2664 * @param drm_device udev device pointer
2665 * @returns 0 on success, or -1 on failure
2666 */
2667static int
2668create_output_for_connector(struct drm_backend *b,
2669 drmModeRes *resources,
2670 drmModeConnector *connector,
2671 struct udev_device *drm_device)
2672{
2673 struct drm_output *output;
Armin Krezović445b41b2016-10-09 23:48:16 +02002674 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002675 int i;
2676
2677 i = find_crtc_for_connector(b, resources, connector);
2678 if (i < 0) {
2679 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02002680 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002681 }
2682
2683 output = zalloc(sizeof *output);
2684 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02002685 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02002686
2687 output->connector = connector;
2688 output->crtc_id = resources->crtcs[i];
2689 output->pipe = i;
2690 output->connector_id = connector->connector_id;
2691
2692 output->backlight = backlight_init(drm_device,
2693 connector->connector_type);
2694
Armin Krezović445b41b2016-10-09 23:48:16 +02002695 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2696
Armin Krezović08368132016-09-30 14:11:05 +02002697 output->base.enable = drm_output_enable;
2698 output->base.destroy = drm_output_destroy;
2699 output->base.disable = drm_output_disable;
2700 output->base.name = make_connector_name(connector);
2701
2702 output->destroy_pending = 0;
2703 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02002704
Armin Krezović40087402016-09-30 14:11:12 +02002705 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02002706
2707 wl_list_init(&output->base.mode_list);
2708
2709 for (i = 0; i < output->connector->count_modes; i++) {
2710 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
2711 if (!drm_mode) {
2712 drm_output_destroy(&output->base);
2713 return -1;
2714 }
2715 }
2716
Armin Krezović08368132016-09-30 14:11:05 +02002717 weston_compositor_add_pending_output(&output->base, b->compositor);
2718
2719 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02002720
2721err:
2722 drmModeFreeConnector(connector);
2723
2724 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02002725}
2726
2727static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002728create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002729{
2730 struct drm_sprite *sprite;
2731 drmModePlaneRes *plane_res;
2732 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002733 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002734
Giulio Camuffo954f1832014-10-11 18:27:30 +03002735 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002736 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002737 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002738 strerror(errno));
2739 return;
2740 }
2741
2742 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002743 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002744 if (!plane)
2745 continue;
2746
Peter Huttererf3d62272013-08-08 11:57:05 +10002747 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002748 plane->count_formats));
2749 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002750 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002751 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002752 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002753 continue;
2754 }
2755
Jesse Barnes58ef3792012-02-23 09:45:49 -05002756 sprite->possible_crtcs = plane->possible_crtcs;
2757 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002758 sprite->current = NULL;
2759 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002760 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002761 sprite->count_formats = plane->count_formats;
2762 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002763 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002764 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002765 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2766 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2767 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002768
Giulio Camuffo954f1832014-10-11 18:27:30 +03002769 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002770 }
2771
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002772 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002773}
2774
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002775static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002776destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002777{
2778 struct drm_sprite *sprite, *next;
2779 struct drm_output *output;
2780
Giulio Camuffo954f1832014-10-11 18:27:30 +03002781 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002782 struct drm_output, base.link);
2783
Giulio Camuffo954f1832014-10-11 18:27:30 +03002784 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2785 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002786 sprite->plane_id,
2787 output->crtc_id, 0, 0,
2788 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone05a5ac22017-04-04 17:54:25 +01002789 drm_fb_unref(sprite->current);
2790 drm_fb_unref(sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002791 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002792 free(sprite);
2793 }
2794}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002795
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002796static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002797create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002798{
2799 drmModeConnector *connector;
2800 drmModeRes *resources;
2801 int i;
2802
Giulio Camuffo954f1832014-10-11 18:27:30 +03002803 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002804 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002805 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002806 return -1;
2807 }
2808
Giulio Camuffo954f1832014-10-11 18:27:30 +03002809 b->min_width = resources->min_width;
2810 b->max_width = resources->max_width;
2811 b->min_height = resources->min_height;
2812 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002813
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002814 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002815 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002816 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002817 if (connector == NULL)
2818 continue;
2819
2820 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00002821 (b->connector == 0 ||
2822 connector->connector_id == b->connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002823 if (create_output_for_connector(b, resources,
Armin Krezović445b41b2016-10-09 23:48:16 +02002824 connector, drm_device) < 0)
Benjamin Franzke439d9862011-10-07 08:20:53 +02002825 continue;
Armin Krezović08368132016-09-30 14:11:05 +02002826 } else {
2827 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002828 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002829 }
2830
Armin Krezović08368132016-09-30 14:11:05 +02002831 if (wl_list_empty(&b->compositor->output_list) &&
2832 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002833 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002834
2835 drmModeFreeResources(resources);
2836
2837 return 0;
2838}
2839
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002840static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002841update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002842{
2843 drmModeConnector *connector;
2844 drmModeRes *resources;
2845 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002846 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002847 int i;
2848
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002850 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002851 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002852 return;
2853 }
2854
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002855 connected = calloc(resources->count_connectors, sizeof(uint32_t));
2856 if (!connected) {
2857 drmModeFreeResources(resources);
2858 return;
2859 }
2860
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002861 /* collect new connects */
2862 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002863 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02002864
Giulio Camuffo954f1832014-10-11 18:27:30 +03002865 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002866 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002867 continue;
2868
David Herrmann7551cff2011-12-08 17:05:43 +01002869 if (connector->connection != DRM_MODE_CONNECTED) {
2870 drmModeFreeConnector(connector);
2871 continue;
2872 }
2873
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00002874 if (b->connector && (b->connector != connector_id)) {
2875 drmModeFreeConnector(connector);
2876 continue;
2877 }
2878
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002879 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002880
Daniel Stonec0ec7592017-02-09 13:58:35 +00002881 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02002882 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00002883 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002884 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00002885
2886 create_output_for_connector(b, resources,
2887 connector, drm_device);
2888 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002889 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002890
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002891 wl_list_for_each_safe(output, next, &b->compositor->output_list,
2892 base.link) {
2893 bool disconnected = true;
2894
2895 for (i = 0; i < resources->count_connectors; i++) {
2896 if (connected[i] == output->connector_id) {
2897 disconnected = false;
2898 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002899 }
2900 }
Armin Krezović08368132016-09-30 14:11:05 +02002901
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002902 if (!disconnected)
2903 continue;
2904
2905 weston_log("connector %d disconnected\n", output->connector_id);
2906 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002907 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00002908
2909 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2910 base.link) {
2911 bool disconnected = true;
2912
2913 for (i = 0; i < resources->count_connectors; i++) {
2914 if (connected[i] == output->connector_id) {
2915 disconnected = false;
2916 break;
2917 }
2918 }
2919
2920 if (!disconnected)
2921 continue;
2922
2923 weston_log("connector %d disconnected\n", output->connector_id);
2924 drm_output_destroy(&output->base);
2925 }
2926
2927 free(connected);
2928 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002929}
2930
2931static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002932udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002933{
David Herrmannd7488c22012-03-11 20:05:21 +01002934 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002935 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002936
2937 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002938 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002939 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002940
David Herrmann6ac52db2012-03-11 20:05:22 +01002941 val = udev_device_get_property_value(device, "HOTPLUG");
2942 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002943 return 0;
2944
David Herrmann6ac52db2012-03-11 20:05:22 +01002945 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002946}
2947
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002948static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002949udev_drm_event(int fd, uint32_t mask, void *data)
2950{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002951 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002952 struct udev_device *event;
2953
Giulio Camuffo954f1832014-10-11 18:27:30 +03002954 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002955
Giulio Camuffo954f1832014-10-11 18:27:30 +03002956 if (udev_event_is_hotplug(b, event))
2957 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002958
2959 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002960
2961 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002962}
2963
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002964static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002965drm_restore(struct weston_compositor *ec)
2966{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002967 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002968}
2969
2970static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002971drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002972{
Armin Krezović545dba62016-08-05 15:54:18 +02002973 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002974
Giulio Camuffo954f1832014-10-11 18:27:30 +03002975 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002976
Giulio Camuffo954f1832014-10-11 18:27:30 +03002977 wl_event_source_remove(b->udev_drm_source);
2978 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002979
Giulio Camuffo954f1832014-10-11 18:27:30 +03002980 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002981
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002982 weston_compositor_shutdown(ec);
2983
Giulio Camuffo954f1832014-10-11 18:27:30 +03002984 if (b->gbm)
2985 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002986
Giulio Camuffo954f1832014-10-11 18:27:30 +03002987 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002988
Giulio Camuffo954f1832014-10-11 18:27:30 +03002989 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002990 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002991}
2992
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002993static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002994session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002995{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002996 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002997 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002998 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002999 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003000
Giulio Camuffo954f1832014-10-11 18:27:30 +03003001 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003002 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003003 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003004 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003005 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003006 } else {
3007 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003008 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003009
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003010 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003011
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003012 /* If we have a repaint scheduled (either from a
3013 * pending pageflip or the idle handler), make sure we
3014 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003015 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003016 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003017 * back, we schedule a repaint, which will process
3018 * pending frame callbacks. */
3019
Giulio Camuffo954f1832014-10-11 18:27:30 +03003020 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003021 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003022 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003023 }
3024
Giulio Camuffo954f1832014-10-11 18:27:30 +03003025 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003026 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003027
Giulio Camuffo954f1832014-10-11 18:27:30 +03003028 wl_list_for_each(sprite, &b->sprite_list, link)
3029 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003030 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003031 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003032 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003033 };
3034}
3035
Daniel Stoneefa504f2016-12-19 16:48:20 +00003036/**
3037 * Determines whether or not a device is capable of modesetting. If successful,
3038 * sets b->drm.fd and b->drm.filename to the opened device.
3039 */
3040static bool
3041drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3042{
3043 const char *filename = udev_device_get_devnode(device);
3044 const char *sysnum = udev_device_get_sysnum(device);
3045 drmModeRes *res;
3046 int id, fd;
3047
3048 if (!filename)
3049 return false;
3050
3051 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3052 if (fd < 0)
3053 return false;
3054
3055 res = drmModeGetResources(fd);
3056 if (!res)
3057 goto out_fd;
3058
3059 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3060 res->count_encoders <= 0)
3061 goto out_res;
3062
3063 if (sysnum)
3064 id = atoi(sysnum);
3065 if (!sysnum || id < 0) {
3066 weston_log("couldn't get sysnum for device %s\n", filename);
3067 goto out_res;
3068 }
3069
3070 /* We can be called successfully on multiple devices; if we have,
3071 * clean up old entries. */
3072 if (b->drm.fd >= 0)
3073 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3074 free(b->drm.filename);
3075
3076 b->drm.fd = fd;
3077 b->drm.id = id;
3078 b->drm.filename = strdup(filename);
3079
Sergi Granellceb59812017-03-28 12:44:04 +02003080 drmModeFreeResources(res);
3081
Daniel Stoneefa504f2016-12-19 16:48:20 +00003082 return true;
3083
3084out_res:
3085 drmModeFreeResources(res);
3086out_fd:
3087 weston_launcher_close(b->compositor->launcher, fd);
3088 return false;
3089}
3090
David Herrmann0af066f2012-10-29 19:21:16 +01003091/*
3092 * Find primary GPU
3093 * Some systems may have multiple DRM devices attached to a single seat. This
3094 * function loops over all devices and tries to find a PCI device with the
3095 * boot_vga sysfs attribute set to 1.
3096 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003097 * Devices are also vetted to make sure they are are capable of modesetting,
3098 * rather than pure render nodes (GPU with no display), or pure
3099 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003100 */
3101static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003102find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003103{
3104 struct udev_enumerate *e;
3105 struct udev_list_entry *entry;
3106 const char *path, *device_seat, *id;
3107 struct udev_device *device, *drm_device, *pci;
3108
Giulio Camuffo954f1832014-10-11 18:27:30 +03003109 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003110 udev_enumerate_add_match_subsystem(e, "drm");
3111 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3112
3113 udev_enumerate_scan_devices(e);
3114 drm_device = NULL;
3115 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003116 bool is_boot_vga = false;
3117
David Herrmann0af066f2012-10-29 19:21:16 +01003118 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003119 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003120 if (!device)
3121 continue;
3122 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3123 if (!device_seat)
3124 device_seat = default_seat;
3125 if (strcmp(device_seat, seat)) {
3126 udev_device_unref(device);
3127 continue;
3128 }
3129
3130 pci = udev_device_get_parent_with_subsystem_devtype(device,
3131 "pci", NULL);
3132 if (pci) {
3133 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003134 if (id && !strcmp(id, "1"))
3135 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003136 }
3137
Daniel Stoneefa504f2016-12-19 16:48:20 +00003138 /* If we already have a modesetting-capable device, and this
3139 * device isn't our boot-VGA device, we aren't going to use
3140 * it. */
3141 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003142 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003143 continue;
3144 }
3145
3146 /* Make sure this device is actually capable of modesetting;
3147 * if this call succeeds, b->drm.{fd,filename} will be set,
3148 * and any old values freed. */
3149 if (!drm_device_is_kms(b, device)) {
3150 udev_device_unref(device);
3151 continue;
3152 }
3153
3154 /* There can only be one boot_vga device, and we try to use it
3155 * at all costs. */
3156 if (is_boot_vga) {
3157 if (drm_device)
3158 udev_device_unref(drm_device);
3159 drm_device = device;
3160 break;
3161 }
3162
3163 /* Per the (!is_boot_vga && drm_device) test above, we only
3164 * trump existing saved devices with boot-VGA devices, so if
3165 * we end up here, this must be the first device we've seen. */
3166 assert(!drm_device);
3167 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003168 }
3169
Daniel Stoneefa504f2016-12-19 16:48:20 +00003170 /* If we're returning a device to use, we must have an open FD for
3171 * it. */
3172 assert(!!drm_device == (b->drm.fd >= 0));
3173
David Herrmann0af066f2012-10-29 19:21:16 +01003174 udev_enumerate_unref(e);
3175 return drm_device;
3176}
3177
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003178static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003179planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3180 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003181{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003182 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003183
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003184 switch (key) {
3185 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003186 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003187 break;
3188 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003189 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003190 break;
3191 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003192 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003193 break;
3194 default:
3195 break;
3196 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003197}
3198
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003199#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003200static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003201recorder_destroy(struct drm_output *output)
3202{
3203 vaapi_recorder_destroy(output->recorder);
3204 output->recorder = NULL;
3205
3206 output->base.disable_planes--;
3207
3208 wl_list_remove(&output->recorder_frame_listener.link);
3209 weston_log("[libva recorder] done\n");
3210}
3211
3212static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003213recorder_frame_notify(struct wl_listener *listener, void *data)
3214{
3215 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003217 int fd, ret;
3218
3219 output = container_of(listener, struct drm_output,
3220 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003221 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003222
3223 if (!output->recorder)
3224 return;
3225
Giulio Camuffo954f1832014-10-11 18:27:30 +03003226 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003227 DRM_CLOEXEC, &fd);
3228 if (ret) {
3229 weston_log("[libva recorder] "
3230 "failed to create prime fd for front buffer\n");
3231 return;
3232 }
3233
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003234 ret = vaapi_recorder_frame(output->recorder, fd,
3235 output->current->stride);
3236 if (ret < 0) {
3237 weston_log("[libva recorder] aborted: %m\n");
3238 recorder_destroy(output);
3239 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003240}
3241
3242static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003243create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003244 const char *filename)
3245{
3246 int fd;
3247 drm_magic_t magic;
3248
Giulio Camuffo954f1832014-10-11 18:27:30 +03003249 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003250 if (fd < 0)
3251 return NULL;
3252
3253 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003255
3256 return vaapi_recorder_create(fd, width, height, filename);
3257}
3258
3259static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003260recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003261 void *data)
3262{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003263 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003264 struct drm_output *output;
3265 int width, height;
3266
Giulio Camuffo954f1832014-10-11 18:27:30 +03003267 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003268 struct drm_output, base.link);
3269
3270 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003271 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003272 weston_log("failed to start vaapi recorder: "
3273 "output format not supported\n");
3274 return;
3275 }
3276
Hardeningff39efa2013-09-18 23:56:35 +02003277 width = output->base.current_mode->width;
3278 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003279
3280 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003281 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003282 if (!output->recorder) {
3283 weston_log("failed to create vaapi recorder\n");
3284 return;
3285 }
3286
3287 output->base.disable_planes++;
3288
3289 output->recorder_frame_listener.notify = recorder_frame_notify;
3290 wl_signal_add(&output->base.frame_signal,
3291 &output->recorder_frame_listener);
3292
3293 weston_output_schedule_repaint(&output->base);
3294
3295 weston_log("[libva recorder] initialized\n");
3296 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003297 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003298 }
3299}
3300#else
3301static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003302recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003303 void *data)
3304{
3305 weston_log("Compiled without libva support\n");
3306}
3307#endif
3308
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003309static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003311{
3312 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003313 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003314
Giulio Camuffo954f1832014-10-11 18:27:30 +03003315 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003316 return;
3317
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003318 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3319
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003320 weston_log("Switching to GL renderer\n");
3321
Giulio Camuffo954f1832014-10-11 18:27:30 +03003322 b->gbm = create_gbm_device(b->drm.fd);
3323 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003324 weston_log("Failed to create gbm device. "
3325 "Aborting renderer switch\n");
3326 return;
3327 }
3328
Giulio Camuffo954f1832014-10-11 18:27:30 +03003329 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003330 pixman_renderer_output_destroy(&output->base);
3331
Giulio Camuffo954f1832014-10-11 18:27:30 +03003332 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003333
Giulio Camuffo954f1832014-10-11 18:27:30 +03003334 if (drm_backend_create_gl_renderer(b) < 0) {
3335 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003336 weston_log("Failed to create GL renderer. Quitting.\n");
3337 /* FIXME: we need a function to shutdown cleanly */
3338 assert(0);
3339 }
3340
Giulio Camuffo954f1832014-10-11 18:27:30 +03003341 wl_list_for_each(output, &b->compositor->output_list, base.link)
3342 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003343
Giulio Camuffo954f1832014-10-11 18:27:30 +03003344 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003345
3346 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3347 if (linux_dmabuf_setup(b->compositor) < 0)
3348 weston_log("Error: initializing dmabuf "
3349 "support failed.\n");
3350 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003351}
3352
3353static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003354renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3355 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003356{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003357 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003358 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003359
Giulio Camuffo954f1832014-10-11 18:27:30 +03003360 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003361}
3362
Armin Krezović08368132016-09-30 14:11:05 +02003363static const struct weston_drm_output_api api = {
3364 drm_output_set_mode,
3365 drm_output_set_gbm_format,
3366 drm_output_set_seat,
3367};
3368
Giulio Camuffo954f1832014-10-11 18:27:30 +03003369static struct drm_backend *
3370drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003371 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003372{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003373 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003374 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003375 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003376 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003377 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003378
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003379 weston_log("initializing drm backend\n");
3380
Giulio Camuffo954f1832014-10-11 18:27:30 +03003381 b = zalloc(sizeof *b);
3382 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003383 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003384
Daniel Stoneefa504f2016-12-19 16:48:20 +00003385 b->drm.fd = -1;
3386
Pekka Paalanen68583832015-05-19 09:53:16 +03003387 /*
3388 * KMS support for hardware planes cannot properly synchronize
3389 * without nuclear page flip. Without nuclear/atomic, hw plane
3390 * and cursor plane updates would either tear or cause extra
3391 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003392 * to a fraction. For cursors, it's not so bad, so they are
3393 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003394 *
3395 * These can be enabled again when nuclear/atomic support lands.
3396 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003397 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003398 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003399 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003400 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003401
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003402 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3403 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003404
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003405 if (config->seat_id)
3406 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003407
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003408 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003409 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3410 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003411 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003412 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003413 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003414 goto err_compositor;
3415 }
3416
Giulio Camuffo954f1832014-10-11 18:27:30 +03003417 b->udev = udev_new();
3418 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003419 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003420 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003421 }
3422
Giulio Camuffo954f1832014-10-11 18:27:30 +03003423 b->session_listener.notify = session_notify;
3424 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003425
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003426 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003427 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003428 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003429 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003430 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003431
Daniel Stoneefa504f2016-12-19 16:48:20 +00003432 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003433 weston_log("failed to initialize kms\n");
3434 goto err_udev_dev;
3435 }
3436
Giulio Camuffo954f1832014-10-11 18:27:30 +03003437 if (b->use_pixman) {
3438 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003439 weston_log("failed to initialize pixman renderer\n");
3440 goto err_udev_dev;
3441 }
3442 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003443 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003444 weston_log("failed to initialize egl\n");
3445 goto err_udev_dev;
3446 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003447 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003448
Giulio Camuffo954f1832014-10-11 18:27:30 +03003449 b->base.destroy = drm_destroy;
3450 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003451
Bob Ham91880f12016-01-12 10:21:47 +00003452 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003453
Giulio Camuffo954f1832014-10-11 18:27:30 +03003454 wl_list_init(&b->sprite_list);
3455 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003456
Giulio Camuffo954f1832014-10-11 18:27:30 +03003457 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003458 compositor, b->udev, seat_id,
3459 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003460 weston_log("failed to create input devices\n");
3461 goto err_sprite;
3462 }
3463
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003464 b->connector = config->connector;
3465
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003466 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003467 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003468 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003469 }
3470
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003471 /* A this point we have some idea of whether or not we have a working
3472 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003473 if (!b->cursors_are_broken)
3474 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003475
Giulio Camuffo954f1832014-10-11 18:27:30 +03003476 loop = wl_display_get_event_loop(compositor->wl_display);
3477 b->drm_source =
3478 wl_event_loop_add_fd(loop, b->drm.fd,
3479 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003480
Giulio Camuffo954f1832014-10-11 18:27:30 +03003481 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3482 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003483 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003484 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003485 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003486 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003487 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003488 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003489 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003490 udev_monitor_get_fd(b->udev_monitor),
3491 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003492
Giulio Camuffo954f1832014-10-11 18:27:30 +03003493 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003494 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003495 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003496 }
3497
Daniel Stonea96b93c2012-06-22 14:04:37 +01003498 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003499
Giulio Camuffo954f1832014-10-11 18:27:30 +03003500 weston_compositor_add_debug_binding(compositor, KEY_O,
3501 planes_binding, b);
3502 weston_compositor_add_debug_binding(compositor, KEY_C,
3503 planes_binding, b);
3504 weston_compositor_add_debug_binding(compositor, KEY_V,
3505 planes_binding, b);
3506 weston_compositor_add_debug_binding(compositor, KEY_Q,
3507 recorder_binding, b);
3508 weston_compositor_add_debug_binding(compositor, KEY_W,
3509 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003510
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003511 if (compositor->renderer->import_dmabuf) {
3512 if (linux_dmabuf_setup(compositor) < 0)
3513 weston_log("Error: initializing dmabuf "
3514 "support failed.\n");
3515 }
3516
Giulio Camuffo954f1832014-10-11 18:27:30 +03003517 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003518
Armin Krezović08368132016-09-30 14:11:05 +02003519 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3520 &api, sizeof(api));
3521
3522 if (ret < 0) {
3523 weston_log("Failed to register output API.\n");
3524 goto err_udev_monitor;
3525 }
3526
Giulio Camuffo954f1832014-10-11 18:27:30 +03003527 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003528
3529err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003530 wl_event_source_remove(b->udev_drm_source);
3531 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003532err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003533 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003534err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003535 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003536err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003537 if (b->gbm)
3538 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003539 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003540err_udev_dev:
3541 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003542err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003543 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003544err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003545 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003546err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003547 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003548 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003549 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003550}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003551
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003552static void
3553config_init_to_defaults(struct weston_drm_backend_config *config)
3554{
3555}
3556
Giulio Camuffo954f1832014-10-11 18:27:30 +03003557WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01003558weston_backend_init(struct weston_compositor *compositor,
3559 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003560{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003561 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003562 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003563
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003564 if (config_base == NULL ||
3565 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3566 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3567 weston_log("drm backend config structure is invalid\n");
3568 return -1;
3569 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003570
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003571 config_init_to_defaults(&config);
3572 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003573
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003574 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003575 if (b == NULL)
3576 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003577
Giulio Camuffo954f1832014-10-11 18:27:30 +03003578 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003579}