blob: 6ef706a8cec4d51fd814f0cbd51b61baa678daab [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>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010031#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040035#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070036#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030037#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020038#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030039#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030040#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041
Benjamin Franzkec649a922011-03-02 11:56:04 +010042#include <xf86drm.h>
43#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050044#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010045
Benjamin Franzke060cf802011-04-30 09:32:11 +020046#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040047#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020048
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070049#include "compositor.h"
50#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070051#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020052#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010053#include "gl-renderer.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020054#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070055#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100056#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010057#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030058#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020059#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030060#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040061
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030062#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
63#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
64#endif
65
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030066#ifndef DRM_CAP_CURSOR_WIDTH
67#define DRM_CAP_CURSOR_WIDTH 0x8
68#endif
69
70#ifndef DRM_CAP_CURSOR_HEIGHT
71#define DRM_CAP_CURSOR_HEIGHT 0x9
72#endif
73
74#ifndef GBM_BO_USE_CURSOR
75#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
76#endif
77
Giulio Camuffo954f1832014-10-11 18:27:30 +030078struct drm_backend {
79 struct weston_backend base;
80 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040081
82 struct udev *udev;
83 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040084
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010085 struct udev_monitor *udev_monitor;
86 struct wl_event_source *udev_drm_source;
87
Benjamin Franzke2af7f102011-03-02 11:14:59 +010088 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010089 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010090 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030091 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020093 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050094 uint32_t *crtcs;
95 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050096 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010097 uint32_t connector_allocator;
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 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200105 uint32_t min_width, max_width;
106 uint32_t min_height, max_height;
107 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
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200117 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300118
Rob Bradfordd355b802013-05-31 18:09:55 +0100119 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300120
Daniel Stone70d337d2015-06-16 18:42:23 +0100121 int32_t cursor_width;
122 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700123
124 /** Callback used to configure the outputs.
125 *
126 * This function will be called by the backend when a new DRM
127 * output needs to be configured.
128 */
129 enum weston_drm_backend_output_mode
130 (*configure_output)(struct weston_compositor *compositor,
131 bool use_current_mode,
132 const char *name,
133 struct weston_drm_backend_output_config *output_config);
134 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400135};
136
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400137struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400139 drmModeModeInfo mode_info;
140};
141
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300142struct drm_output;
143
144struct drm_fb {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300145 struct drm_output *output;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200146 uint32_t fb_id, stride, handle, size;
147 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300148 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200149 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200150
151 /* Used by gbm fbs */
152 struct gbm_bo *bo;
153
154 /* Used by dumb fbs */
155 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300156};
157
Richard Hughes2b2092a2013-04-24 14:58:02 +0100158struct drm_edid {
159 char eisa_id[13];
160 char monitor_name[13];
161 char pnp_id[5];
162 char serial_number[13];
163};
164
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500166 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400167
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500169 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400170 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700171 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100172 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300173 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100174 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200175
Daniel Stone36609c72015-06-18 07:49:02 +0100176 enum dpms_enum dpms;
177
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300178 int vblank_pending;
179 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800180 int destroy_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300181
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100182 struct gbm_surface *gbm_surface;
183 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400184 struct weston_plane cursor_plane;
185 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500186 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400187 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300188 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200189 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200190
191 struct drm_fb *dumb[2];
192 pixman_image_t *image[2];
193 int current_image;
194 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300195
196 struct vaapi_recorder *recorder;
197 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400198};
199
Jesse Barnes58ef3792012-02-23 09:45:49 -0500200/*
201 * An output has a primary display plane plus zero or more sprites for
202 * blending display contents.
203 */
204struct drm_sprite {
205 struct wl_list link;
206
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400207 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500208
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200209 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300210 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300211 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500212
Jesse Barnes58ef3792012-02-23 09:45:49 -0500213 uint32_t possible_crtcs;
214 uint32_t plane_id;
215 uint32_t count_formats;
216
217 int32_t src_x, src_y;
218 uint32_t src_w, src_h;
219 uint32_t dest_x, dest_y;
220 uint32_t dest_w, dest_h;
221
222 uint32_t formats[];
223};
224
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300225static struct gl_renderer_interface *gl_renderer;
226
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500227static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400228
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400229static void
230drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400231
Mario Kleinerf507ec32015-06-21 21:25:14 +0200232static void
233drm_output_update_msc(struct drm_output *output, unsigned int seq);
234
Jesse Barnes58ef3792012-02-23 09:45:49 -0500235static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200236drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500237{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200238 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300239 struct drm_backend *b =(struct drm_backend *)ec->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500240 int crtc;
241
Giulio Camuffo954f1832014-10-11 18:27:30 +0300242 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
243 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500244 continue;
245
246 if (supported & (1 << crtc))
247 return -1;
248 }
249
250 return 0;
251}
252
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300253static void
254drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
255{
256 struct drm_fb *fb = data;
257 struct gbm_device *gbm = gbm_bo_get_device(bo);
258
259 if (fb->fb_id)
260 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
261
Pekka Paalanende685b82012-12-04 15:58:12 +0200262 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300263
264 free(data);
265}
266
267static struct drm_fb *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300268drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200269{
270 struct drm_fb *fb;
271 int ret;
272
273 struct drm_mode_create_dumb create_arg;
274 struct drm_mode_destroy_dumb destroy_arg;
275 struct drm_mode_map_dumb map_arg;
276
Peter Huttererf3d62272013-08-08 11:57:05 +1000277 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200278 if (!fb)
279 return NULL;
280
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700281 memset(&create_arg, 0, sizeof create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200282 create_arg.bpp = 32;
283 create_arg.width = width;
284 create_arg.height = height;
285
Giulio Camuffo954f1832014-10-11 18:27:30 +0300286 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200287 if (ret)
288 goto err_fb;
289
290 fb->handle = create_arg.handle;
291 fb->stride = create_arg.pitch;
292 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300293 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200294
Giulio Camuffo954f1832014-10-11 18:27:30 +0300295 ret = drmModeAddFB(b->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200296 fb->stride, fb->handle, &fb->fb_id);
297 if (ret)
298 goto err_bo;
299
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700300 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200301 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400302 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200303 if (ret)
304 goto err_add_fb;
305
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500306 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300307 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200308 if (fb->map == MAP_FAILED)
309 goto err_add_fb;
310
311 return fb;
312
313err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300314 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200315err_bo:
316 memset(&destroy_arg, 0, sizeof(destroy_arg));
317 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300318 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200319err_fb:
320 free(fb);
321 return NULL;
322}
323
324static void
325drm_fb_destroy_dumb(struct drm_fb *fb)
326{
327 struct drm_mode_destroy_dumb destroy_arg;
328
329 if (!fb->map)
330 return;
331
332 if (fb->fb_id)
333 drmModeRmFB(fb->fd, fb->fb_id);
334
335 weston_buffer_reference(&fb->buffer_ref, NULL);
336
337 munmap(fb->map, fb->size);
338
339 memset(&destroy_arg, 0, sizeof(destroy_arg));
340 destroy_arg.handle = fb->handle;
341 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
342
343 free(fb);
344}
345
346static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500347drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300348 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300349{
350 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200351 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200352 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300353 int ret;
354
355 if (fb)
356 return fb;
357
Bryce Harringtonde16d892014-11-20 22:21:57 -0800358 fb = zalloc(sizeof *fb);
359 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200360 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300361
362 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300363
364 width = gbm_bo_get_width(bo);
365 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200366 fb->stride = gbm_bo_get_stride(bo);
367 fb->handle = gbm_bo_get_handle(bo).u32;
368 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300369 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300370
Giulio Camuffo954f1832014-10-11 18:27:30 +0300371 if (backend->min_width > width || width > backend->max_width ||
372 backend->min_height > height ||
373 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200374 weston_log("bo geometry out of bounds\n");
375 goto err_free;
376 }
377
378 ret = -1;
379
Giulio Camuffo954f1832014-10-11 18:27:30 +0300380 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200381 handles[0] = fb->handle;
382 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200383 offsets[0] = 0;
384
Giulio Camuffo954f1832014-10-11 18:27:30 +0300385 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200386 format, handles, pitches, offsets,
387 &fb->fb_id, 0);
388 if (ret) {
389 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300390 backend->no_addfb2 = 1;
391 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200392 }
393 }
394
395 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300396 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200397 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200398
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300399 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200400 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200401 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300402 }
403
404 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
405
406 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200407
408err_free:
409 free(fb);
410 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300411}
412
413static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500414drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200415{
Pekka Paalanende685b82012-12-04 15:58:12 +0200416 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200417
418 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200419
Pekka Paalanende685b82012-12-04 15:58:12 +0200420 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200421}
422
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200423static void
424drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
425{
426 if (!fb)
427 return;
428
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200429 if (fb->map &&
430 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200431 drm_fb_destroy_dumb(fb);
432 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200433 if (fb->is_client_buffer)
434 gbm_bo_destroy(fb->bo);
435 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100436 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600437 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200438 }
439}
440
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500441static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200442drm_output_check_scanout_format(struct drm_output *output,
443 struct weston_surface *es, struct gbm_bo *bo)
444{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200445 uint32_t format;
446 pixman_region32_t r;
447
448 format = gbm_bo_get_format(bo);
449
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700450 if (format == GBM_FORMAT_ARGB8888) {
451 /* We can scanout an ARGB buffer if the surface's
452 * opaque region covers the whole output, but we have
453 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800454 pixman_region32_init_rect(&r, 0, 0,
455 output->base.width,
456 output->base.height);
457 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200458
459 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500460 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200461
462 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500463 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700464
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100465 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700466 return format;
467
468 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200469}
470
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400471static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200472drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500474{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300475 struct drm_backend *b =
476 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500477 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200478 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300479 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500480 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500481
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482 if (ev->geometry.x != output->base.x ||
483 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300484 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200485 buffer->width != output->base.current_mode->width ||
486 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200487 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500488 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400489 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500490
Pekka Paalanen5580f222015-02-17 16:33:18 +0200491 if (ev->geometry.scissor_enabled)
492 return NULL;
493
Giulio Camuffo954f1832014-10-11 18:27:30 +0300494 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700495 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500496
Rob Bradford9b101872012-09-14 23:25:41 +0100497 /* Unable to use the buffer for scanout */
498 if (!bo)
499 return NULL;
500
Jason Ekstranda7af7042013-10-12 22:38:11 -0500501 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500502 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300503 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400504 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300505 }
506
Giulio Camuffo954f1832014-10-11 18:27:30 +0300507 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300508 if (!output->next) {
509 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400510 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300511 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500512
Pekka Paalanende685b82012-12-04 15:58:12 +0200513 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500514
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400515 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500516}
517
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500518static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200519drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400520{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300521 struct drm_backend *b =
522 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300523 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400524
Giulio Camuffo954f1832014-10-11 18:27:30 +0300525 output->base.compositor->renderer->repaint_output(&output->base,
526 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400527
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100528 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300529 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200530 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400531 return;
532 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300533
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100534 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300535 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200536 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100537 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300538 return;
539 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400540}
541
542static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200543drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
544{
545 struct weston_compositor *ec = output->base.compositor;
546 pixman_region32_t total_damage, previous_damage;
547
548 pixman_region32_init(&total_damage);
549 pixman_region32_init(&previous_damage);
550
551 pixman_region32_copy(&previous_damage, damage);
552
553 pixman_region32_union(&total_damage, damage, &output->previous_damage);
554 pixman_region32_copy(&output->previous_damage, &previous_damage);
555
556 output->current_image ^= 1;
557
558 output->next = output->dumb[output->current_image];
559 pixman_renderer_output_set_buffer(&output->base,
560 output->image[output->current_image]);
561
562 ec->renderer->repaint_output(&output->base, &total_damage);
563
564 pixman_region32_fini(&total_damage);
565 pixman_region32_fini(&previous_damage);
566}
567
568static void
569drm_output_render(struct drm_output *output, pixman_region32_t *damage)
570{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300571 struct weston_compositor *c = output->base.compositor;
572 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200573
Giulio Camuffo954f1832014-10-11 18:27:30 +0300574 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200575 drm_output_render_pixman(output, damage);
576 else
577 drm_output_render_gl(output, damage);
578
Giulio Camuffo954f1832014-10-11 18:27:30 +0300579 pixman_region32_subtract(&c->primary_plane.damage,
580 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200581}
582
583static void
Richard Hughese7299962013-05-01 21:52:12 +0100584drm_output_set_gamma(struct weston_output *output_base,
585 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
586{
587 int rc;
588 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300589 struct drm_backend *backend =
590 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100591
592 /* check */
593 if (output_base->gamma_size != size)
594 return;
595 if (!output->original_crtc)
596 return;
597
Giulio Camuffo954f1832014-10-11 18:27:30 +0300598 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100599 output->crtc_id,
600 size, r, g, b);
601 if (rc)
602 weston_log("set gamma failed: %m\n");
603}
604
Bryce Harringtonada4f072015-06-30 13:25:46 -0700605/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200606 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700607 * The pipe parameter indicates which CRTC is in use. Knowing this, we
608 * can determine which vblank sequence type to use for it. Traditional
609 * cards had only two CRTCs, with CRTC 0 using no special flags, and
610 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
611 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200612 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700613 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
614 * 0-31. If this is non-zero it indicates we're dealing with a
615 * multi-gpu situation and we need to calculate the vblank sync
616 * using DRM_BLANK_HIGH_CRTC_MASK.
617 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300618static unsigned int
619drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200620{
621 if (output->pipe > 1)
622 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
623 DRM_VBLANK_HIGH_CRTC_MASK;
624 else if (output->pipe > 0)
625 return DRM_VBLANK_SECONDARY;
626 else
627 return 0;
628}
629
David Herrmann1edf44c2013-10-22 17:11:26 +0200630static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500631drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400632 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100633{
634 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300635 struct drm_backend *backend =
636 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500637 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400638 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500639 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100640
Xiong Zhangabd5d472013-10-11 14:43:07 +0800641 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200642 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800643
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300644 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400645 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300646 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200647 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100648
Hardeningff39efa2013-09-18 23:56:35 +0200649 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200650 if (!output->current ||
651 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300652 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300653 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400654 &output->connector_id, 1,
655 &mode->mode_info);
656 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200657 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200658 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400659 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300660 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200661 }
662
Giulio Camuffo954f1832014-10-11 18:27:30 +0300663 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300664 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500665 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200666 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200667 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500668 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100669
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300670 output->page_flip_pending = 1;
671
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400672 drm_output_set_cursor(output);
673
Jesse Barnes58ef3792012-02-23 09:45:49 -0500674 /*
675 * Now, update all the sprite surfaces
676 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300677 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200678 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500679 drmVBlank vbl = {
680 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
681 .request.sequence = 1,
682 };
683
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200684 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200685 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500686 continue;
687
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200689 fb_id = s->next->fb_id;
690
Giulio Camuffo954f1832014-10-11 18:27:30 +0300691 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200692 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500693 s->dest_x, s->dest_y,
694 s->dest_w, s->dest_h,
695 s->src_x, s->src_y,
696 s->src_w, s->src_h);
697 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200698 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500699 ret, strerror(errno));
700
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200701 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500702
Jesse Barnes58ef3792012-02-23 09:45:49 -0500703 /*
704 * Queue a vblank signal so we know when the surface
705 * becomes active on the display or has been replaced.
706 */
707 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300708 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500709 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200710 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500711 ret, strerror(errno));
712 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300713
714 s->output = output;
715 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500716 }
717
David Herrmann1edf44c2013-10-22 17:11:26 +0200718 return 0;
719
720err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800721 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200722 if (output->next) {
723 drm_output_release_fb(output, output->next);
724 output->next = NULL;
725 }
726
727 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400728}
729
730static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200731drm_output_start_repaint_loop(struct weston_output *output_base)
732{
733 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300734 struct drm_backend *backend = (struct drm_backend *)
735 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200736 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200737 struct timespec ts, tnow;
738 struct timespec vbl2now;
739 int64_t refresh_nsec;
740 int ret;
741 drmVBlank vbl = {
742 .request.type = DRM_VBLANK_RELATIVE,
743 .request.sequence = 0,
744 .request.signal = 0,
745 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300746
Xiong Zhangabd5d472013-10-11 14:43:07 +0800747 if (output->destroy_pending)
748 return;
749
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300750 if (!output->current) {
751 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200752 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300753 }
754
Mario Kleinerf507ec32015-06-21 21:25:14 +0200755 /* Try to get current msc and timestamp via instant query */
756 vbl.request.type |= drm_waitvblank_pipe(output);
757 ret = drmWaitVBlank(backend->drm.fd, &vbl);
758
759 /* Error ret or zero timestamp means failure to get valid timestamp */
760 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
761 ts.tv_sec = vbl.reply.tval_sec;
762 ts.tv_nsec = vbl.reply.tval_usec * 1000;
763
764 /* Valid timestamp for most recent vblank - not stale?
765 * Stale ts could happen on Linux 3.17+, so make sure it
766 * is not older than 1 refresh duration since now.
767 */
768 weston_compositor_read_presentation_clock(backend->compositor,
769 &tnow);
770 timespec_sub(&vbl2now, &tnow, &ts);
771 refresh_nsec =
772 millihz_to_nsec(output->base.current_mode->refresh);
773 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
774 drm_output_update_msc(output, vbl.reply.sequence);
775 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200776 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200777 return;
778 }
779 }
780
781 /* Immediate query didn't provide valid timestamp.
782 * Use pageflip fallback.
783 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300784 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200785
Giulio Camuffo954f1832014-10-11 18:27:30 +0300786 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200787 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
788 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200789 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200790 }
David Herrmann3c688c52013-10-22 17:11:25 +0200791
792 return;
793
794finish_frame:
795 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300796 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200797 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200798 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200799}
800
801static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400802drm_output_update_msc(struct drm_output *output, unsigned int seq)
803{
804 uint64_t msc_hi = output->base.msc >> 32;
805
806 if (seq < (output->base.msc & 0xffffffff))
807 msc_hi++;
808
809 output->base.msc = (msc_hi << 32) + seq;
810}
811
812static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500813vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
814 void *data)
815{
816 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300817 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400818 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200819 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
820 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300821
Pekka Paalanen641307c2014-09-23 22:08:47 -0400822 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300823 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500824
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200825 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200826 s->current = s->next;
827 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300828
829 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400830 ts.tv_sec = sec;
831 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200832 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300833 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500834}
835
836static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800837drm_output_destroy(struct weston_output *output_base);
838
839static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400840page_flip_handler(int fd, unsigned int frame,
841 unsigned int sec, unsigned int usec, void *data)
842{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200843 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400844 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200845 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
846 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
847 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400848
Pekka Paalanen641307c2014-09-23 22:08:47 -0400849 drm_output_update_msc(output, frame);
850
Jonas Ådahle5a12252013-04-05 23:07:11 +0200851 /* We don't set page_flip_pending on start_repaint_loop, in that case
852 * we just want to page flip to the current buffer to get an accurate
853 * timestamp */
854 if (output->page_flip_pending) {
855 drm_output_release_fb(output, output->current);
856 output->current = output->next;
857 output->next = NULL;
858 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300859
Jonas Ådahle5a12252013-04-05 23:07:11 +0200860 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400861
Xiong Zhangabd5d472013-10-11 14:43:07 +0800862 if (output->destroy_pending)
863 drm_output_destroy(&output->base);
864 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400865 ts.tv_sec = sec;
866 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200867 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300868
869 /* We can't call this from frame_notify, because the output's
870 * repaint needed flag is cleared just after that */
871 if (output->recorder)
872 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300873 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200874}
875
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500876static uint32_t
877drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500879{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500880 uint32_t i, format;
881
882 format = gbm_bo_get_format(bo);
883
884 if (format == GBM_FORMAT_ARGB8888) {
885 pixman_region32_t r;
886
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500887 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600888 ev->surface->width,
889 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500891
892 if (!pixman_region32_not_empty(&r))
893 format = GBM_FORMAT_XRGB8888;
894
895 pixman_region32_fini(&r);
896 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500897
898 for (i = 0; i < s->count_formats; i++)
899 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500900 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500901
902 return 0;
903}
904
905static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500907{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 return !ev->transform.enabled ||
909 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500910}
911
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400912static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200913drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500915{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200916 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300917 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300919 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500920 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300921 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500922 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500923 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500924 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200925 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500926 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400927 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500928
Giulio Camuffo954f1832014-10-11 18:27:30 +0300929 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200930 return NULL;
931
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200932 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200933 return NULL;
934
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200935 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200936 return NULL;
937
Giulio Camuffo954f1832014-10-11 18:27:30 +0300938 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500940
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200941 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300943
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400945 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300946 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500947
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200949 return NULL;
950
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300951 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500952 return NULL;
953
Jason Ekstranda7af7042013-10-12 22:38:11 -0500954 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400955 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500956
Giulio Camuffo954f1832014-10-11 18:27:30 +0300957 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200958 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500959 continue;
960
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200961 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500962 found = 1;
963 break;
964 }
965 }
966
967 /* No sprites available */
968 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400969 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500970
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300971 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -0700972#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300973 /* XXX: TODO:
974 *
975 * Use AddFB2 directly, do not go via GBM.
976 * Add support for multiplanar formats.
977 * Both require refactoring in the DRM-backend to
978 * support a mix of gbm_bos and drmfbs.
979 */
980 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000981 .fd = dmabuf->attributes.fd[0],
982 .width = dmabuf->attributes.width,
983 .height = dmabuf->attributes.height,
984 .stride = dmabuf->attributes.stride[0],
985 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300986 };
987
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000988 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300989 return NULL;
990
991 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
992 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -0700993#else
994 return NULL;
995#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300996 } else {
997 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
998 buffer_resource, GBM_BO_USE_SCANOUT);
999 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001000 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001001 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001002
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001004 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001005 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001007 }
1008
Giulio Camuffo954f1832014-10-11 18:27:30 +03001009 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001010 if (!s->next) {
1011 gbm_bo_destroy(bo);
1012 return NULL;
1013 }
1014
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001016
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001018 s->plane.x = box->x1;
1019 s->plane.y = box->y1;
1020
Jesse Barnes58ef3792012-02-23 09:45:49 -05001021 /*
1022 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001023 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001024 * for us already).
1025 */
1026 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001028 &output->base.region);
1029 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001030 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001031 tbox = weston_transformed_rect(output->base.width,
1032 output->base.height,
1033 output->base.transform,
1034 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001035 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001036 s->dest_x = tbox.x1;
1037 s->dest_y = tbox.y1;
1038 s->dest_w = tbox.x2 - tbox.x1;
1039 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001040 pixman_region32_fini(&dest_rect);
1041
1042 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001044 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001045 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001046
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 weston_view_from_global_fixed(ev,
1048 wl_fixed_from_int(box->x1),
1049 wl_fixed_from_int(box->y1),
1050 &sx1, &sy1);
1051 weston_view_from_global_fixed(ev,
1052 wl_fixed_from_int(box->x2),
1053 wl_fixed_from_int(box->y2),
1054 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001055
1056 if (sx1 < 0)
1057 sx1 = 0;
1058 if (sy1 < 0)
1059 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001060 if (sx2 > wl_fixed_from_int(ev->surface->width))
1061 sx2 = wl_fixed_from_int(ev->surface->width);
1062 if (sy2 > wl_fixed_from_int(ev->surface->height))
1063 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001064
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001065 tbox.x1 = sx1;
1066 tbox.y1 = sy1;
1067 tbox.x2 = sx2;
1068 tbox.y2 = sy2;
1069
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001070 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1071 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001072 viewport->buffer.transform,
1073 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001074 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001075
1076 s->src_x = tbox.x1 << 8;
1077 s->src_y = tbox.y1 << 8;
1078 s->src_w = (tbox.x2 - tbox.x1) << 8;
1079 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001080 pixman_region32_fini(&src_rect);
1081
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001082 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001083}
1084
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001085static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001086drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001088{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001089 struct drm_backend *b =
1090 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001091 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001092 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001093
Derek Foremanbe428b32015-11-24 11:39:38 -06001094 if (ev->transform.enabled &&
1095 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1096 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001097 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001098 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001099 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1100 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001101 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001102 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001104 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001105 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001106 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001107 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001108 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001109 if (ev->geometry.scissor_enabled)
1110 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001111 if (ev->surface->buffer_ref.buffer == NULL)
1112 return NULL;
1113 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1114 if (!shmbuf)
1115 return NULL;
1116 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1117 return NULL;
1118 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001119 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001120 return NULL;
1121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001123
1124 return &output->cursor_plane;
1125}
1126
Pekka Paalanend0ead482014-06-16 12:05:40 +03001127/**
1128 * Update the image for the current cursor surface
1129 *
1130 * @param b DRM backend structure
1131 * @param bo GBM buffer object to write into
1132 * @param ev View to use for cursor image
1133 */
1134static void
1135cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1136 struct weston_view *ev)
1137{
1138 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1139 uint32_t buf[b->cursor_width * b->cursor_height];
1140 int32_t stride;
1141 uint8_t *s;
1142 int i;
1143
1144 assert(buffer && buffer->shm_buffer);
1145 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1146 assert(ev->surface->width <= b->cursor_width);
1147 assert(ev->surface->height <= b->cursor_height);
1148
1149 memset(buf, 0, sizeof buf);
1150 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1151 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1152
1153 wl_shm_buffer_begin_access(buffer->shm_buffer);
1154 for (i = 0; i < ev->surface->height; i++)
1155 memcpy(buf + i * b->cursor_width,
1156 s + i * stride,
1157 ev->surface->width * 4);
1158 wl_shm_buffer_end_access(buffer->shm_buffer);
1159
1160 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1161 weston_log("failed update cursor: %m\n");
1162}
1163
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001164static void
1165drm_output_set_cursor(struct drm_output *output)
1166{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001168 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001169 struct drm_backend *b =
1170 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001171 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001172 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001173 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001174
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 output->cursor_view = NULL;
1176 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001177 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001178 output->cursor_plane.x = INT32_MIN;
1179 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001180 return;
1181 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001182
Neil Robertse5051712013-11-13 15:44:06 +00001183 buffer = ev->surface->buffer_ref.buffer;
1184
1185 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001186 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001187 pixman_region32_fini(&output->cursor_plane.damage);
1188 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001189 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001190 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001191
Pekka Paalanend0ead482014-06-16 12:05:40 +03001192 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001193 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001194 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1195 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001196 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001197 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001198 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001199 }
1200
Derek Foremanbe428b32015-11-24 11:39:38 -06001201 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001202
1203 /* From global to output space, output transform is guaranteed to be
1204 * NORMAL by drm_output_prepare_cursor_view().
1205 */
1206 x = (x - output->base.x) * output->base.current_scale;
1207 y = (y - output->base.y) * output->base.current_scale;
1208
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001209 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001210 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001211 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001212 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001213 }
1214
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001215 output->cursor_plane.x = x;
1216 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001217 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001218}
1219
Jesse Barnes58ef3792012-02-23 09:45:49 -05001220static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001221drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001222{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001223 struct drm_backend *b =
1224 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001225 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001227 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001228 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001229
1230 /*
1231 * Find a surface for each sprite in the output using some heuristics:
1232 * 1) size
1233 * 2) frequency of update
1234 * 3) opacity (though some hw might support alpha blending)
1235 * 4) clipping (this can be fixed with color keys)
1236 *
1237 * The idea is to save on blitting since this should save power.
1238 * If we can get a large video surface on the sprite for example,
1239 * the main display surface may not need to update at all, and
1240 * the client buffer can be used directly for the sprite surface
1241 * as we do for flipping full screen surfaces.
1242 */
1243 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001244 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245
Giulio Camuffo954f1832014-10-11 18:27:30 +03001246 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001247 struct weston_surface *es = ev->surface;
1248
1249 /* Test whether this buffer can ever go into a plane:
1250 * non-shm, or small enough to be a cursor.
1251 *
1252 * Also, keep a reference when using the pixman renderer.
1253 * That makes it possible to do a seamless switch to the GL
1254 * renderer and since the pixman renderer keeps a reference
1255 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001256 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001257 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001258 (es->buffer_ref.buffer &&
1259 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001260 (ev->surface->width <= b->cursor_width &&
1261 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001262 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001263 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001264 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001265
Jesse Barnes58ef3792012-02-23 09:45:49 -05001266 pixman_region32_init(&surface_overlap);
1267 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001269
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001270 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001271 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001272 next_plane = primary;
1273 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001275 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001277 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001279 if (next_plane == NULL)
1280 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001283
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001284 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001285 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001287
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001288 if (next_plane == primary ||
1289 next_plane == &output->cursor_plane) {
1290 /* cursor plane involves a copy */
1291 ev->psf_flags = 0;
1292 } else {
1293 /* All other planes are a direct scanout of a
1294 * single client buffer.
1295 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001296 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001297 }
1298
Jesse Barnes58ef3792012-02-23 09:45:49 -05001299 pixman_region32_fini(&surface_overlap);
1300 }
1301 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001302}
1303
Matt Roper361d2ad2011-08-29 13:52:23 -07001304static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001305drm_output_fini_pixman(struct drm_output *output);
1306
1307static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001308drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001309{
1310 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001311 struct drm_backend *b =
1312 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001313 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001314
Xiong Zhangabd5d472013-10-11 14:43:07 +08001315 if (output->page_flip_pending) {
1316 output->destroy_pending = 1;
1317 weston_log("destroy output while page flip pending\n");
1318 return;
1319 }
1320
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001321 if (output->backlight)
1322 backlight_destroy(output->backlight);
1323
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001324 drmModeFreeProperty(output->dpms_prop);
1325
Matt Roper361d2ad2011-08-29 13:52:23 -07001326 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001327 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001328
1329 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001330 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001331 origcrtc->x, origcrtc->y,
1332 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001333 drmModeFreeCrtc(origcrtc);
1334
Giulio Camuffo954f1832014-10-11 18:27:30 +03001335 b->crtc_allocator &= ~(1 << output->crtc_id);
1336 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001337
Giulio Camuffo954f1832014-10-11 18:27:30 +03001338 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001339 drm_output_fini_pixman(output);
1340 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001341 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001342 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001343 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001344
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001345 weston_plane_release(&output->fb_plane);
1346 weston_plane_release(&output->cursor_plane);
1347
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001348 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001349
Matt Roper361d2ad2011-08-29 13:52:23 -07001350 free(output);
1351}
1352
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001353/**
1354 * Find the closest-matching mode for a given target
1355 *
1356 * Given a target mode, find the most suitable mode amongst the output's
1357 * current mode list to use, preferring the current mode if possible, to
1358 * avoid an expensive mode switch.
1359 *
1360 * @param output DRM output
1361 * @param target_mode Mode to attempt to match
1362 * @returns Pointer to a mode from the output's mode list
1363 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001364static struct drm_mode *
1365choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1366{
1367 struct drm_mode *tmp_mode = NULL, *mode;
1368
Hardeningff39efa2013-09-18 23:56:35 +02001369 if (output->base.current_mode->width == target_mode->width &&
1370 output->base.current_mode->height == target_mode->height &&
1371 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001372 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001373 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001374
1375 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1376 if (mode->mode_info.hdisplay == target_mode->width &&
1377 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001378 if (mode->base.refresh == target_mode->refresh ||
1379 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001380 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001381 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001382 tmp_mode = mode;
1383 }
1384 }
1385
1386 return tmp_mode;
1387}
1388
1389static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001390drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001391static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001392drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001393
1394static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001395drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1396{
1397 struct drm_output *output;
1398 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001399 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001400
1401 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001402 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001403 return -1;
1404 }
1405
1406 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001407 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001408 return -1;
1409 }
1410
Giulio Camuffo954f1832014-10-11 18:27:30 +03001411 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001412 output = (struct drm_output *)output_base;
1413 drm_mode = choose_mode (output, mode);
1414
1415 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001416 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001417 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001418 }
1419
Hardeningff39efa2013-09-18 23:56:35 +02001420 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001421 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001422
Hardeningff39efa2013-09-18 23:56:35 +02001423 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001424
Hardeningff39efa2013-09-18 23:56:35 +02001425 output->base.current_mode = &drm_mode->base;
1426 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001427 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1428
Alex Wub7b8bda2012-04-17 17:20:48 +08001429 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001430 drm_output_release_fb(output, output->current);
1431 drm_output_release_fb(output, output->next);
1432 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001433
Giulio Camuffo954f1832014-10-11 18:27:30 +03001434 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001435 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001437 weston_log("failed to init output pixman state with "
1438 "new mode\n");
1439 return -1;
1440 }
1441 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001442 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001443 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001444
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001446 weston_log("failed to init output egl state with "
1447 "new mode");
1448 return -1;
1449 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001450 }
1451
Alex Wub7b8bda2012-04-17 17:20:48 +08001452 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001453}
1454
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001455static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001456on_drm_input(int fd, uint32_t mask, void *data)
1457{
1458 drmEventContext evctx;
1459
1460 memset(&evctx, 0, sizeof evctx);
1461 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1462 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001463 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001464 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001465
1466 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001467}
1468
1469static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001470init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001471{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001472 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001473 uint64_t cap;
1474 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001475 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001476
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001477 sysnum = udev_device_get_sysnum(device);
1478 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001479 b->drm.id = atoi(sysnum);
1480 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001481 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001482 return -1;
1483 }
1484
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001485 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001486 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001487 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001488 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001489 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001490 udev_device_get_devnode(device));
1491 return -1;
1492 }
1493
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001494 weston_log("using %s\n", filename);
1495
Giulio Camuffo954f1832014-10-11 18:27:30 +03001496 b->drm.fd = fd;
1497 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001498
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001499 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1500 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001501 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001502 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001503 clk_id = CLOCK_REALTIME;
1504
Giulio Camuffo954f1832014-10-11 18:27:30 +03001505 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001506 weston_log("Error: failed to set presentation clock %d.\n",
1507 clk_id);
1508 return -1;
1509 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001510
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001511 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1512 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001513 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001514 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001515 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001516
1517 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1518 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001519 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001520 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001521 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001522
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001523 return 0;
1524}
1525
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001526static struct gbm_device *
1527create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001528{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001529 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001530
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001531 gl_renderer = weston_load_module("gl-renderer.so",
1532 "gl_renderer_interface");
1533 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001534 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001535
1536 /* GBM will load a dri driver, but even though they need symbols from
1537 * libglapi, in some version of Mesa they are not linked to it. Since
1538 * only the gl-renderer module links to it, the call above won't make
1539 * these symbols globally available, and loading the DRI driver fails.
1540 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1541 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1542
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001543 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001544
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001545 return gbm;
1546}
1547
Bryce Harringtonc056a982015-05-19 15:25:18 -07001548/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001549 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001550 *
1551 * This returns 0 if substitution isn't possible, but 0 might be a
1552 * legitimate format for other EGL platforms, so the caller is
1553 * responsible for checking for 0 before calling gl_renderer->create().
1554 *
1555 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1556 * but it's entirely possible we'll see this again on other implementations.
1557 */
1558static int
1559fallback_format_for(uint32_t format)
1560{
1561 switch (format) {
1562 case GBM_FORMAT_XRGB8888:
1563 return GBM_FORMAT_ARGB8888;
1564 case GBM_FORMAT_XRGB2101010:
1565 return GBM_FORMAT_ARGB2101010;
1566 default:
1567 return 0;
1568 }
1569}
1570
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001571static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001572drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001573{
Derek Foreman6d556372015-11-04 14:47:33 -06001574 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001575 b->gbm_format,
1576 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001577 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001578 };
Derek Foreman6d556372015-11-04 14:47:33 -06001579 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001580
Derek Foremanc4cfe852015-05-15 12:12:40 -05001581 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001582 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001583 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001584 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001585 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001586 gl_renderer->opaque_attribs,
1587 format,
1588 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001589 return -1;
1590 }
1591
1592 return 0;
1593}
1594
1595static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001596init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001597{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001598 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001599
Giulio Camuffo954f1832014-10-11 18:27:30 +03001600 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001601 return -1;
1602
Giulio Camuffo954f1832014-10-11 18:27:30 +03001603 if (drm_backend_create_gl_renderer(b) < 0) {
1604 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001605 return -1;
1606 }
1607
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001608 return 0;
1609}
1610
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001611static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001612init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001613{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001614 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001615}
1616
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001617/**
1618 * Add a mode to output's mode list
1619 *
1620 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1621 * output's mode list.
1622 *
1623 * @param output DRM output to add mode to
1624 * @param info DRM mode structure to add
1625 * @returns Newly-allocated Weston/DRM mode structure
1626 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001627static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001628drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001629{
1630 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001631 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001632
1633 mode = malloc(sizeof *mode);
1634 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001635 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001636
1637 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001638 mode->base.width = info->hdisplay;
1639 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001640
1641 /* Calculate higher precision (mHz) refresh rate */
1642 refresh = (info->clock * 1000000LL / info->htotal +
1643 info->vtotal / 2) / info->vtotal;
1644
1645 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1646 refresh *= 2;
1647 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1648 refresh /= 2;
1649 if (info->vscan > 1)
1650 refresh /= info->vscan;
1651
1652 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001653 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001654
1655 if (info->type & DRM_MODE_TYPE_PREFERRED)
1656 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1657
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001658 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1659
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001660 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001661}
1662
1663static int
1664drm_subpixel_to_wayland(int drm_value)
1665{
1666 switch (drm_value) {
1667 default:
1668 case DRM_MODE_SUBPIXEL_UNKNOWN:
1669 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1670 case DRM_MODE_SUBPIXEL_NONE:
1671 return WL_OUTPUT_SUBPIXEL_NONE;
1672 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1673 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1674 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1675 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1676 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1677 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1678 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1679 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1680 }
1681}
1682
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001683/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001684static uint32_t
1685drm_get_backlight(struct drm_output *output)
1686{
1687 long brightness, max_brightness, norm;
1688
1689 brightness = backlight_get_brightness(output->backlight);
1690 max_brightness = backlight_get_max_brightness(output->backlight);
1691
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001692 /* convert it on a scale of 0 to 255 */
1693 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001694
1695 return (uint32_t) norm;
1696}
1697
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001698/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001699static void
1700drm_set_backlight(struct weston_output *output_base, uint32_t value)
1701{
1702 struct drm_output *output = (struct drm_output *) output_base;
1703 long max_brightness, new_brightness;
1704
1705 if (!output->backlight)
1706 return;
1707
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001708 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001709 return;
1710
1711 max_brightness = backlight_get_max_brightness(output->backlight);
1712
1713 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001714 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001715
1716 backlight_set_brightness(output->backlight, new_brightness);
1717}
1718
1719static drmModePropertyPtr
1720drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1721{
1722 drmModePropertyPtr props;
1723 int i;
1724
1725 for (i = 0; i < connector->count_props; i++) {
1726 props = drmModeGetProperty(fd, connector->props[i]);
1727 if (!props)
1728 continue;
1729
1730 if (!strcmp(props->name, name))
1731 return props;
1732
1733 drmModeFreeProperty(props);
1734 }
1735
1736 return NULL;
1737}
1738
1739static void
1740drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1741{
1742 struct drm_output *output = (struct drm_output *) output_base;
1743 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001744 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001745 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001746
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001747 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001748 return;
1749
Daniel Stone36609c72015-06-18 07:49:02 +01001750 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1751 output->dpms_prop->prop_id, level);
1752 if (ret) {
1753 weston_log("DRM: DPMS: failed property set for %s\n",
1754 output->base.name);
1755 return;
1756 }
1757
1758 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001759}
1760
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001761static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001762 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1763 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1764 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1765 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1766 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1767 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1768 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1769 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1770 [DRM_MODE_CONNECTOR_Component] = "Component",
1771 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1772 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1773 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1774 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1775 [DRM_MODE_CONNECTOR_TV] = "TV",
1776 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001777#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001778 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1779 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001780#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001781};
1782
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001783static char *
1784make_connector_name(const drmModeConnector *con)
1785{
1786 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001787 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001788
1789 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1790 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001791
1792 if (!type_name)
1793 type_name = "UNNAMED";
1794
1795 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001796
1797 return strdup(name);
1798}
1799
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001800static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001801find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001802 drmModeRes *resources, drmModeConnector *connector)
1803{
1804 drmModeEncoder *encoder;
1805 uint32_t possible_crtcs;
1806 int i, j;
1807
1808 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001809 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001810 if (encoder == NULL) {
1811 weston_log("Failed to get encoder.\n");
1812 return -1;
1813 }
1814 possible_crtcs = encoder->possible_crtcs;
1815 drmModeFreeEncoder(encoder);
1816
1817 for (i = 0; i < resources->count_crtcs; i++) {
1818 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001819 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001820 return i;
1821 }
1822 }
1823
1824 return -1;
1825}
1826
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001827/* Init output state that depends on gl or gbm */
1828static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001829drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001830{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001831 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001832 output->gbm_format,
1833 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001834 };
1835 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001836
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001837 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001838 output->base.current_mode->width,
1839 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001840 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001841 GBM_BO_USE_SCANOUT |
1842 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001843 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001844 weston_log("failed to create gbm surface\n");
1845 return -1;
1846 }
1847
Derek Foremanc4cfe852015-05-15 12:12:40 -05001848 if (format[1])
1849 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001850 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001851 (EGLNativeWindowType)output->gbm_surface,
1852 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001853 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001854 format,
1855 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001856 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001857 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001858 return -1;
1859 }
1860
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001861 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001862
1863 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001864 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001865 continue;
1866
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001867 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001868 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001869 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001870 }
1871
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001872 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001873 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001874 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001875 }
1876
1877 return 0;
1878}
1879
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001880static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001881drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001882{
Hardeningff39efa2013-09-18 23:56:35 +02001883 int w = output->base.current_mode->width;
1884 int h = output->base.current_mode->height;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001885 unsigned int i;
1886
1887 /* FIXME error checking */
1888
1889 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001890 output->dumb[i] = drm_fb_create_dumb(b, w, h);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001891 if (!output->dumb[i])
1892 goto err;
1893
1894 output->image[i] =
Alexander Larsson0b135062013-05-28 16:23:36 +02001895 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001896 output->dumb[i]->map,
1897 output->dumb[i]->stride);
1898 if (!output->image[i])
1899 goto err;
1900 }
1901
1902 if (pixman_renderer_output_create(&output->base) < 0)
1903 goto err;
1904
1905 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001906 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001907
1908 return 0;
1909
1910err:
1911 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1912 if (output->dumb[i])
1913 drm_fb_destroy_dumb(output->dumb[i]);
1914 if (output->image[i])
1915 pixman_image_unref(output->image[i]);
1916
1917 output->dumb[i] = NULL;
1918 output->image[i] = NULL;
1919 }
1920
1921 return -1;
1922}
1923
1924static void
1925drm_output_fini_pixman(struct drm_output *output)
1926{
1927 unsigned int i;
1928
1929 pixman_renderer_output_destroy(&output->base);
1930 pixman_region32_fini(&output->previous_damage);
1931
1932 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1933 drm_fb_destroy_dumb(output->dumb[i]);
1934 pixman_image_unref(output->image[i]);
1935 output->dumb[i] = NULL;
1936 output->image[i] = NULL;
1937 }
1938}
1939
Richard Hughes2b2092a2013-04-24 14:58:02 +01001940static void
1941edid_parse_string(const uint8_t *data, char text[])
1942{
1943 int i;
1944 int replaced = 0;
1945
1946 /* this is always 12 bytes, but we can't guarantee it's null
1947 * terminated or not junk. */
1948 strncpy(text, (const char *) data, 12);
1949
Bryce Harrington9c7de162015-08-28 13:04:26 -07001950 /* guarantee our new string is null-terminated */
1951 text[12] = '\0';
1952
Richard Hughes2b2092a2013-04-24 14:58:02 +01001953 /* remove insane chars */
1954 for (i = 0; text[i] != '\0'; i++) {
1955 if (text[i] == '\n' ||
1956 text[i] == '\r') {
1957 text[i] = '\0';
1958 break;
1959 }
1960 }
1961
1962 /* ensure string is printable */
1963 for (i = 0; text[i] != '\0'; i++) {
1964 if (!isprint(text[i])) {
1965 text[i] = '-';
1966 replaced++;
1967 }
1968 }
1969
1970 /* if the string is random junk, ignore the string */
1971 if (replaced > 4)
1972 text[0] = '\0';
1973}
1974
1975#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1976#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1977#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1978#define EDID_OFFSET_DATA_BLOCKS 0x36
1979#define EDID_OFFSET_LAST_BLOCK 0x6c
1980#define EDID_OFFSET_PNPID 0x08
1981#define EDID_OFFSET_SERIAL 0x0c
1982
1983static int
1984edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1985{
1986 int i;
1987 uint32_t serial_number;
1988
1989 /* check header */
1990 if (length < 128)
1991 return -1;
1992 if (data[0] != 0x00 || data[1] != 0xff)
1993 return -1;
1994
1995 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1996 * /--08--\/--09--\
1997 * 7654321076543210
1998 * |\---/\---/\---/
1999 * R C1 C2 C3 */
2000 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2001 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2002 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2003 edid->pnp_id[3] = '\0';
2004
2005 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2006 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2007 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2008 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2009 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2010 if (serial_number > 0)
2011 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2012
2013 /* parse EDID data */
2014 for (i = EDID_OFFSET_DATA_BLOCKS;
2015 i <= EDID_OFFSET_LAST_BLOCK;
2016 i += 18) {
2017 /* ignore pixel clock data */
2018 if (data[i] != 0)
2019 continue;
2020 if (data[i+2] != 0)
2021 continue;
2022
2023 /* any useful blocks? */
2024 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2025 edid_parse_string(&data[i+5],
2026 edid->monitor_name);
2027 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2028 edid_parse_string(&data[i+5],
2029 edid->serial_number);
2030 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2031 edid_parse_string(&data[i+5],
2032 edid->eisa_id);
2033 }
2034 }
2035 return 0;
2036}
2037
2038static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002039find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002040 struct drm_output *output,
2041 drmModeConnector *connector)
2042{
2043 drmModePropertyBlobPtr edid_blob = NULL;
2044 drmModePropertyPtr property;
2045 int i;
2046 int rc;
2047
2048 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002049 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002050 if (!property)
2051 continue;
2052 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2053 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002054 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002055 connector->prop_values[i]);
2056 }
2057 drmModeFreeProperty(property);
2058 }
2059 if (!edid_blob)
2060 return;
2061
2062 rc = edid_parse(&output->edid,
2063 edid_blob->data,
2064 edid_blob->length);
2065 if (!rc) {
2066 weston_log("EDID data '%s', '%s', '%s'\n",
2067 output->edid.pnp_id,
2068 output->edid.monitor_name,
2069 output->edid.serial_number);
2070 if (output->edid.pnp_id[0] != '\0')
2071 output->base.make = output->edid.pnp_id;
2072 if (output->edid.monitor_name[0] != '\0')
2073 output->base.model = output->edid.monitor_name;
2074 if (output->edid.serial_number[0] != '\0')
2075 output->base.serial_number = output->edid.serial_number;
2076 }
2077 drmModeFreePropertyBlob(edid_blob);
2078}
2079
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002080
2081
2082static int
2083parse_modeline(const char *s, drmModeModeInfo *mode)
2084{
2085 char hsync[16];
2086 char vsync[16];
2087 float fclock;
2088
2089 mode->type = DRM_MODE_TYPE_USERDEF;
2090 mode->hskew = 0;
2091 mode->vscan = 0;
2092 mode->vrefresh = 0;
2093 mode->flags = 0;
2094
Rob Bradford307e09e2013-07-26 16:29:40 +01002095 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002096 &fclock,
2097 &mode->hdisplay,
2098 &mode->hsync_start,
2099 &mode->hsync_end,
2100 &mode->htotal,
2101 &mode->vdisplay,
2102 &mode->vsync_start,
2103 &mode->vsync_end,
2104 &mode->vtotal, hsync, vsync) != 11)
2105 return -1;
2106
2107 mode->clock = fclock * 1000;
2108 if (strcmp(hsync, "+hsync") == 0)
2109 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2110 else if (strcmp(hsync, "-hsync") == 0)
2111 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2112 else
2113 return -1;
2114
2115 if (strcmp(vsync, "+vsync") == 0)
2116 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2117 else if (strcmp(vsync, "-vsync") == 0)
2118 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2119 else
2120 return -1;
2121
2122 return 0;
2123}
2124
Rob Bradford66bd9f52013-06-25 18:56:42 +01002125static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002126setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002127 struct weston_output *output,
2128 const char *s)
2129{
2130 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002131 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002132 struct udev_seat *seat;
2133
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002135 if (!seat)
2136 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002137
Derek Foreman0720ea32015-07-15 13:00:35 -05002138 seat->base.output = output;
2139
Derek Foreman1281a362015-07-31 16:55:32 -05002140 pointer = weston_seat_get_pointer(&seat->base);
2141 if (pointer)
2142 weston_pointer_clamp(pointer,
2143 &pointer->x,
2144 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002145 }
2146}
2147
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002148static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002149parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002150{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002151 int ret = 0;
2152
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002153 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002154 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002155 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002156 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002157 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002158 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002159 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002160 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002161 else {
2162 weston_log("fatal: unrecognized pixel format: %s\n", s);
2163 ret = -1;
2164 }
2165
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002166 return ret;
2167}
2168
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002169/**
2170 * Choose suitable mode for an output
2171 *
2172 * Find the most suitable mode to use for initial setup (or reconfiguration on
2173 * hotplug etc) for a DRM output.
2174 *
2175 * @param output DRM output to choose mode for
2176 * @param kind Strategy and preference to use when choosing mode
2177 * @param width Desired width for this output
2178 * @param height Desired height for this output
2179 * @param current_mode Mode currently being displayed on this output
2180 * @param modeline Manually-entered mode (may be NULL)
2181 * @returns A mode from the output's mode list, or NULL if none available
2182 */
2183static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002184drm_output_choose_initial_mode(struct drm_backend *backend,
2185 struct drm_output *output,
2186 enum weston_drm_backend_output_mode mode,
2187 struct weston_drm_backend_output_config *config,
2188 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002189{
2190 struct drm_mode *preferred = NULL;
2191 struct drm_mode *current = NULL;
2192 struct drm_mode *configured = NULL;
2193 struct drm_mode *best = NULL;
2194 struct drm_mode *drm_mode;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002195 drmModeModeInfo modeline;
2196 int32_t width = 0;
2197 int32_t height = 0;
2198
2199 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
2200 if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
2201 width = -1;
2202
2203 if (parse_modeline(config->modeline, &modeline) == 0) {
2204 configured = drm_output_add_mode(output, &modeline);
2205 if (!configured)
2206 return NULL;
2207 } else {
2208 weston_log("Invalid modeline \"%s\" for output %s\n",
2209 config->modeline, output->base.name);
2210 }
2211 }
2212 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002213
2214 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002215 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002216 height == drm_mode->base.height)
2217 configured = drm_mode;
2218
comic fans7a5c5622016-03-17 14:29:27 +02002219 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002220 sizeof *current_mode) == 0)
2221 current = drm_mode;
2222
2223 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2224 preferred = drm_mode;
2225
2226 best = drm_mode;
2227 }
2228
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002229 if (current == NULL && current_mode->clock != 0) {
2230 current = drm_output_add_mode(output, current_mode);
2231 if (!current)
2232 return NULL;
2233 }
2234
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002235 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002236 configured = current;
2237
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002238 if (configured)
2239 return configured;
2240
2241 if (preferred)
2242 return preferred;
2243
2244 if (current)
2245 return current;
2246
2247 if (best)
2248 return best;
2249
2250 weston_log("no available modes for %s\n", output->base.name);
2251 return NULL;
2252}
2253
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002254static int
2255connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2256 drmModeModeInfo *mode)
2257{
2258 drmModeEncoder *encoder;
2259 drmModeCrtc *crtc;
2260
2261 /* Get the current mode on the crtc that's currently driving
2262 * this connector. */
2263 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2264 memset(mode, 0, sizeof *mode);
2265 if (encoder != NULL) {
2266 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2267 drmModeFreeEncoder(encoder);
2268 if (crtc == NULL)
2269 return -1;
2270 if (crtc->mode_valid)
2271 *mode = crtc->mode;
2272 drmModeFreeCrtc(crtc);
2273 }
2274
2275 return 0;
2276}
2277
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002278/**
2279 * Create and configure a Weston output structure
2280 *
2281 * Given a DRM connector, create a matching drm_output structure and add it
2282 * to Weston's output list.
2283 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002284 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002285 * @param resources DRM resources for this device
2286 * @param connector DRM connector to use for this new output
2287 * @param x Horizontal offset to use into global co-ordinate space
2288 * @param y Vertical offset to use into global co-ordinate space
2289 * @param drm_device udev device pointer
2290 * @returns 0 on success, or -1 on failure
2291 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002292static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002293create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002294 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002295 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002296 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002297{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002298 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002299 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002300 struct weston_mode *m;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002301
2302 drmModeModeInfo crtc_mode;
2303 int i;
2304 enum weston_drm_backend_output_mode mode;
2305 struct weston_drm_backend_output_config config = {{ 0 }};
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002306
Giulio Camuffo954f1832014-10-11 18:27:30 +03002307 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002308 if (i < 0) {
2309 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002310 return -1;
2311 }
2312
Peter Huttererf3d62272013-08-08 11:57:05 +10002313 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002314 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002315 return -1;
2316
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002317 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002318 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002319 output->base.make = "unknown";
2320 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002321 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002322 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002323
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002324 mode = b->configure_output(b->compositor, b->use_current_mode,
2325 output->base.name, &config);
2326 if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002327 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002328
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002329 setup_output_seat_constraint(b, &output->base,
2330 config.seat ? config.seat : "");
2331 free(config.seat);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002332
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002333 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002334 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002335 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002336 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002337 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002338
Giulio Camuffo954f1832014-10-11 18:27:30 +03002339 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2340 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002341
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002342 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2343 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002344
David Herrmann0f0d54e2011-12-08 17:05:45 +01002345 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002346 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002347 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002348 goto err_free;
2349 }
2350
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002351 if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002352 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002353 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002354 0, 0, 0, 0, 0, NULL);
2355 goto err_free;
2356 }
2357
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002358 current = drm_output_choose_initial_mode(b, output, mode, &config,
2359 &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002360 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002361 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002362 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002363 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002364
Giulio Camuffo954f1832014-10-11 18:27:30 +03002365 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002366 connector->mmWidth, connector->mmHeight,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002367 config.base.transform, config.base.scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002368
Giulio Camuffo954f1832014-10-11 18:27:30 +03002369 if (b->use_pixman) {
2370 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002371 weston_log("Failed to init output pixman state\n");
2372 goto err_output;
2373 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002374 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002375 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002376 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002377 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002378
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002379 output->backlight = backlight_init(drm_device,
2380 connector->connector_type);
2381 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002382 weston_log("Initialized backlight, device %s\n",
2383 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002384 output->base.set_backlight = drm_set_backlight;
2385 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002386 } else {
2387 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002388 }
2389
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002391
Giulio Camuffo954f1832014-10-11 18:27:30 +03002392 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002393 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2394 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002395
Jonas Ådahle5a12252013-04-05 23:07:11 +02002396 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002397 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002398 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002399 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002400 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002401 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002402
Richard Hughese7299962013-05-01 21:52:12 +01002403 output->base.gamma_size = output->original_crtc->gamma_size;
2404 output->base.set_gamma = drm_output_set_gamma;
2405
Derek Foremanb0427562016-02-05 15:55:20 -06002406 weston_plane_init(&output->cursor_plane, b->compositor,
2407 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002408 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002409
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2411 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2412 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002413
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002414 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002415 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002416 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002417 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002418 m->width, m->height, m->refresh / 1000.0,
2419 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2420 ", preferred" : "",
2421 m->flags & WL_OUTPUT_MODE_CURRENT ?
2422 ", current" : "",
2423 connector->count_modes == 0 ?
2424 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002425
Mario Kleiner80817042015-06-21 21:25:11 +02002426 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2427 output->base.native_mode = output->base.current_mode;
2428 output->base.native_scale = output->base.current_scale;
2429
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002430 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002431
John Kåre Alsaker94659272012-11-13 19:10:18 +01002432err_output:
2433 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002434err_free:
2435 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2436 base.link) {
2437 wl_list_remove(&drm_mode->base.link);
2438 free(drm_mode);
2439 }
2440
2441 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002442 b->crtc_allocator &= ~(1 << output->crtc_id);
2443 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002444 free(output);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002445 free(config.modeline);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002446
David Herrmann0f0d54e2011-12-08 17:05:45 +01002447 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002448}
2449
Jesse Barnes58ef3792012-02-23 09:45:49 -05002450static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002451create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002452{
2453 struct drm_sprite *sprite;
2454 drmModePlaneRes *plane_res;
2455 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002456 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002457
Giulio Camuffo954f1832014-10-11 18:27:30 +03002458 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002459 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002460 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002461 strerror(errno));
2462 return;
2463 }
2464
2465 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002466 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002467 if (!plane)
2468 continue;
2469
Peter Huttererf3d62272013-08-08 11:57:05 +10002470 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002471 plane->count_formats));
2472 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002473 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002474 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002475 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002476 continue;
2477 }
2478
Jesse Barnes58ef3792012-02-23 09:45:49 -05002479 sprite->possible_crtcs = plane->possible_crtcs;
2480 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002481 sprite->current = NULL;
2482 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002483 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002484 sprite->count_formats = plane->count_formats;
2485 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002486 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002487 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002488 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2489 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2490 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002491
Giulio Camuffo954f1832014-10-11 18:27:30 +03002492 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002493 }
2494
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002495 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002496}
2497
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002498static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002500{
2501 struct drm_sprite *sprite, *next;
2502 struct drm_output *output;
2503
Giulio Camuffo954f1832014-10-11 18:27:30 +03002504 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002505 struct drm_output, base.link);
2506
Giulio Camuffo954f1832014-10-11 18:27:30 +03002507 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2508 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002509 sprite->plane_id,
2510 output->crtc_id, 0, 0,
2511 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002512 drm_output_release_fb(output, sprite->current);
2513 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002514 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002515 free(sprite);
2516 }
2517}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002518
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002519static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002520create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002521 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002522{
2523 drmModeConnector *connector;
2524 drmModeRes *resources;
2525 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002526 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002527
Giulio Camuffo954f1832014-10-11 18:27:30 +03002528 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002529 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002530 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002531 return -1;
2532 }
2533
Giulio Camuffo954f1832014-10-11 18:27:30 +03002534 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2535 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002536 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002537 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002538 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002539
Giulio Camuffo954f1832014-10-11 18:27:30 +03002540 b->min_width = resources->min_width;
2541 b->max_width = resources->max_width;
2542 b->min_height = resources->min_height;
2543 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002544
Giulio Camuffo954f1832014-10-11 18:27:30 +03002545 b->num_crtcs = resources->count_crtcs;
2546 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002547
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002548 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002549 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002550 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002551 if (connector == NULL)
2552 continue;
2553
2554 if (connector->connection == DRM_MODE_CONNECTED &&
2555 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002556 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002557 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002558 connector, x, y,
2559 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002560 drmModeFreeConnector(connector);
2561 continue;
2562 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002563
Giulio Camuffo954f1832014-10-11 18:27:30 +03002564 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002565 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002566 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002567 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002568
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002569 drmModeFreeConnector(connector);
2570 }
2571
Giulio Camuffo954f1832014-10-11 18:27:30 +03002572 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002573 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002574 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002575 return -1;
2576 }
2577
2578 drmModeFreeResources(resources);
2579
2580 return 0;
2581}
2582
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002583static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002584update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002585{
2586 drmModeConnector *connector;
2587 drmModeRes *resources;
2588 struct drm_output *output, *next;
2589 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002590 uint32_t connected = 0, disconnects = 0;
2591 int i;
2592
Giulio Camuffo954f1832014-10-11 18:27:30 +03002593 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002594 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002595 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002596 return;
2597 }
2598
2599 /* collect new connects */
2600 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002601 int connector_id = resources->connectors[i];
2602
Giulio Camuffo954f1832014-10-11 18:27:30 +03002603 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002604 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002605 continue;
2606
David Herrmann7551cff2011-12-08 17:05:43 +01002607 if (connector->connection != DRM_MODE_CONNECTED) {
2608 drmModeFreeConnector(connector);
2609 continue;
2610 }
2611
Benjamin Franzke117483d2011-08-30 11:38:26 +02002612 connected |= (1 << connector_id);
2613
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002615 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002616 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002617 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002618
2619 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002620 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002621 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002622 else
2623 x = 0;
2624 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002625 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002626 connector, x, y,
2627 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002628 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002629
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002630 }
2631 drmModeFreeConnector(connector);
2632 }
2633 drmModeFreeResources(resources);
2634
Giulio Camuffo954f1832014-10-11 18:27:30 +03002635 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002636 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002637 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002638 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639 if (disconnects & (1 << output->connector_id)) {
2640 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002641 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002642 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002643 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002644 }
2645 }
2646 }
2647
Daniel Stonef556ebe2015-05-21 08:28:58 +01002648 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002649 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002650 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002651}
2652
2653static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002654udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002655{
David Herrmannd7488c22012-03-11 20:05:21 +01002656 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002657 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002658
2659 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002660 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002661 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002662
David Herrmann6ac52db2012-03-11 20:05:22 +01002663 val = udev_device_get_property_value(device, "HOTPLUG");
2664 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002665 return 0;
2666
David Herrmann6ac52db2012-03-11 20:05:22 +01002667 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002668}
2669
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002670static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002671udev_drm_event(int fd, uint32_t mask, void *data)
2672{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002673 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002674 struct udev_device *event;
2675
Giulio Camuffo954f1832014-10-11 18:27:30 +03002676 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002677
Giulio Camuffo954f1832014-10-11 18:27:30 +03002678 if (udev_event_is_hotplug(b, event))
2679 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002680
2681 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002682
2683 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002684}
2685
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002686static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002687drm_restore(struct weston_compositor *ec)
2688{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002689 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002690}
2691
2692static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002693drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002694{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002695 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002696
Giulio Camuffo954f1832014-10-11 18:27:30 +03002697 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002698
Giulio Camuffo954f1832014-10-11 18:27:30 +03002699 wl_event_source_remove(b->udev_drm_source);
2700 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002701
Giulio Camuffo954f1832014-10-11 18:27:30 +03002702 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002703
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002704 weston_compositor_shutdown(ec);
2705
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 if (b->gbm)
2707 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002708
Giulio Camuffo954f1832014-10-11 18:27:30 +03002709 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002710
Giulio Camuffo954f1832014-10-11 18:27:30 +03002711 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002712 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002713}
2714
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002715static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002716drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002717{
2718 struct drm_output *output;
2719 struct drm_mode *drm_mode;
2720 int ret;
2721
Giulio Camuffo954f1832014-10-11 18:27:30 +03002722 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002723 if (!output->current) {
2724 /* If something that would cause the output to
2725 * switch mode happened while in another vt, we
2726 * might not have a current drm_fb. In that case,
2727 * schedule a repaint and let drm_output_repaint
2728 * handle setting the mode. */
2729 weston_output_schedule_repaint(&output->base);
2730 continue;
2731 }
2732
Hardeningff39efa2013-09-18 23:56:35 +02002733 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002734 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002735 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002736 &output->connector_id, 1,
2737 &drm_mode->mode_info);
2738 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002739 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002740 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002741 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002742 output->base.x, output->base.y);
2743 }
2744 }
2745}
2746
2747static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002748session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002749{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002750 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002751 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002752 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002753 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002754
Giulio Camuffo954f1832014-10-11 18:27:30 +03002755 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002756 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002757 compositor->state = b->prev_state;
2758 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002759 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002760 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002761 } else {
2762 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002763 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002764
Giulio Camuffo954f1832014-10-11 18:27:30 +03002765 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002766 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002767
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002768 /* If we have a repaint scheduled (either from a
2769 * pending pageflip or the idle handler), make sure we
2770 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002771 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002772 * further attemps at repainting. When we switch
2773 * back, we schedule a repaint, which will process
2774 * pending frame callbacks. */
2775
Giulio Camuffo954f1832014-10-11 18:27:30 +03002776 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002777 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002778 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002779 }
2780
Giulio Camuffo954f1832014-10-11 18:27:30 +03002781 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002782 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002783
Giulio Camuffo954f1832014-10-11 18:27:30 +03002784 wl_list_for_each(sprite, &b->sprite_list, link)
2785 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002786 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002787 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002788 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002789 };
2790}
2791
David Herrmann0af066f2012-10-29 19:21:16 +01002792/*
2793 * Find primary GPU
2794 * Some systems may have multiple DRM devices attached to a single seat. This
2795 * function loops over all devices and tries to find a PCI device with the
2796 * boot_vga sysfs attribute set to 1.
2797 * If no such device is found, the first DRM device reported by udev is used.
2798 */
2799static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002801{
2802 struct udev_enumerate *e;
2803 struct udev_list_entry *entry;
2804 const char *path, *device_seat, *id;
2805 struct udev_device *device, *drm_device, *pci;
2806
Giulio Camuffo954f1832014-10-11 18:27:30 +03002807 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002808 udev_enumerate_add_match_subsystem(e, "drm");
2809 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2810
2811 udev_enumerate_scan_devices(e);
2812 drm_device = NULL;
2813 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2814 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002815 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002816 if (!device)
2817 continue;
2818 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2819 if (!device_seat)
2820 device_seat = default_seat;
2821 if (strcmp(device_seat, seat)) {
2822 udev_device_unref(device);
2823 continue;
2824 }
2825
2826 pci = udev_device_get_parent_with_subsystem_devtype(device,
2827 "pci", NULL);
2828 if (pci) {
2829 id = udev_device_get_sysattr_value(pci, "boot_vga");
2830 if (id && !strcmp(id, "1")) {
2831 if (drm_device)
2832 udev_device_unref(drm_device);
2833 drm_device = device;
2834 break;
2835 }
2836 }
2837
2838 if (!drm_device)
2839 drm_device = device;
2840 else
2841 udev_device_unref(device);
2842 }
2843
2844 udev_enumerate_unref(e);
2845 return drm_device;
2846}
2847
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002848static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002849planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2850 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002851{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002852 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002853
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002854 switch (key) {
2855 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002856 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002857 break;
2858 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002859 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002860 break;
2861 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002862 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002863 break;
2864 default:
2865 break;
2866 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002867}
2868
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002869#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002870static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002871recorder_destroy(struct drm_output *output)
2872{
2873 vaapi_recorder_destroy(output->recorder);
2874 output->recorder = NULL;
2875
2876 output->base.disable_planes--;
2877
2878 wl_list_remove(&output->recorder_frame_listener.link);
2879 weston_log("[libva recorder] done\n");
2880}
2881
2882static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002883recorder_frame_notify(struct wl_listener *listener, void *data)
2884{
2885 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002886 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002887 int fd, ret;
2888
2889 output = container_of(listener, struct drm_output,
2890 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002891 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002892
2893 if (!output->recorder)
2894 return;
2895
Giulio Camuffo954f1832014-10-11 18:27:30 +03002896 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002897 DRM_CLOEXEC, &fd);
2898 if (ret) {
2899 weston_log("[libva recorder] "
2900 "failed to create prime fd for front buffer\n");
2901 return;
2902 }
2903
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002904 ret = vaapi_recorder_frame(output->recorder, fd,
2905 output->current->stride);
2906 if (ret < 0) {
2907 weston_log("[libva recorder] aborted: %m\n");
2908 recorder_destroy(output);
2909 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002910}
2911
2912static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002913create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002914 const char *filename)
2915{
2916 int fd;
2917 drm_magic_t magic;
2918
Giulio Camuffo954f1832014-10-11 18:27:30 +03002919 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002920 if (fd < 0)
2921 return NULL;
2922
2923 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002924 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002925
2926 return vaapi_recorder_create(fd, width, height, filename);
2927}
2928
2929static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002930recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931 void *data)
2932{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002933 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002934 struct drm_output *output;
2935 int width, height;
2936
Giulio Camuffo954f1832014-10-11 18:27:30 +03002937 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002938 struct drm_output, base.link);
2939
2940 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002941 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002942 weston_log("failed to start vaapi recorder: "
2943 "output format not supported\n");
2944 return;
2945 }
2946
Hardeningff39efa2013-09-18 23:56:35 +02002947 width = output->base.current_mode->width;
2948 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002949
2950 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002951 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002952 if (!output->recorder) {
2953 weston_log("failed to create vaapi recorder\n");
2954 return;
2955 }
2956
2957 output->base.disable_planes++;
2958
2959 output->recorder_frame_listener.notify = recorder_frame_notify;
2960 wl_signal_add(&output->base.frame_signal,
2961 &output->recorder_frame_listener);
2962
2963 weston_output_schedule_repaint(&output->base);
2964
2965 weston_log("[libva recorder] initialized\n");
2966 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002967 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002968 }
2969}
2970#else
2971static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07002972recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002973 void *data)
2974{
2975 weston_log("Compiled without libva support\n");
2976}
2977#endif
2978
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002979static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002980switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002981{
2982 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03002983 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002984
Giulio Camuffo954f1832014-10-11 18:27:30 +03002985 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002986 return;
2987
Pekka Paalanene4d231e2014-06-12 15:12:48 +03002988 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
2989
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002990 weston_log("Switching to GL renderer\n");
2991
Giulio Camuffo954f1832014-10-11 18:27:30 +03002992 b->gbm = create_gbm_device(b->drm.fd);
2993 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002994 weston_log("Failed to create gbm device. "
2995 "Aborting renderer switch\n");
2996 return;
2997 }
2998
Giulio Camuffo954f1832014-10-11 18:27:30 +03002999 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003000 pixman_renderer_output_destroy(&output->base);
3001
Giulio Camuffo954f1832014-10-11 18:27:30 +03003002 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003003
Giulio Camuffo954f1832014-10-11 18:27:30 +03003004 if (drm_backend_create_gl_renderer(b) < 0) {
3005 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003006 weston_log("Failed to create GL renderer. Quitting.\n");
3007 /* FIXME: we need a function to shutdown cleanly */
3008 assert(0);
3009 }
3010
Giulio Camuffo954f1832014-10-11 18:27:30 +03003011 wl_list_for_each(output, &b->compositor->output_list, base.link)
3012 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003013
Giulio Camuffo954f1832014-10-11 18:27:30 +03003014 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003015
3016 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3017 if (linux_dmabuf_setup(b->compositor) < 0)
3018 weston_log("Error: initializing dmabuf "
3019 "support failed.\n");
3020 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003021}
3022
3023static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003024renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3025 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003026{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003027 struct drm_backend *b =
3028 (struct drm_backend *) keyboard->seat->compositor;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003029
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003031}
3032
Giulio Camuffo954f1832014-10-11 18:27:30 +03003033static struct drm_backend *
3034drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003035 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003036{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003037 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003038 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003039 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003040 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003041 const char *seat_id = default_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003042
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003043 weston_log("initializing drm backend\n");
3044
Giulio Camuffo954f1832014-10-11 18:27:30 +03003045 b = zalloc(sizeof *b);
3046 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003047 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003048
Pekka Paalanen68583832015-05-19 09:53:16 +03003049 /*
3050 * KMS support for hardware planes cannot properly synchronize
3051 * without nuclear page flip. Without nuclear/atomic, hw plane
3052 * and cursor plane updates would either tear or cause extra
3053 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003054 * to a fraction. For cursors, it's not so bad, so they are
3055 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003056 *
3057 * These can be enabled again when nuclear/atomic support lands.
3058 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003059 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003060 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003061 b->use_pixman = config->use_pixman;
3062 b->configure_output = config->configure_output;
3063 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003064
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003065 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3066 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003067
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003068 if (config->seat_id)
3069 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003070
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003071 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003072 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3073 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003074 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003075 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003076 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003077 goto err_compositor;
3078 }
3079
Giulio Camuffo954f1832014-10-11 18:27:30 +03003080 b->udev = udev_new();
3081 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003082 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003083 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003084 }
3085
Giulio Camuffo954f1832014-10-11 18:27:30 +03003086 b->session_listener.notify = session_notify;
3087 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003088
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003089 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003090 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003091 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003092 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003093 }
David Herrmann0af066f2012-10-29 19:21:16 +01003094 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003095
Giulio Camuffo954f1832014-10-11 18:27:30 +03003096 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003097 weston_log("failed to initialize kms\n");
3098 goto err_udev_dev;
3099 }
3100
Giulio Camuffo954f1832014-10-11 18:27:30 +03003101 if (b->use_pixman) {
3102 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003103 weston_log("failed to initialize pixman renderer\n");
3104 goto err_udev_dev;
3105 }
3106 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003107 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003108 weston_log("failed to initialize egl\n");
3109 goto err_udev_dev;
3110 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003111 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003112
Giulio Camuffo954f1832014-10-11 18:27:30 +03003113 b->base.destroy = drm_destroy;
3114 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003115
Giulio Camuffo954f1832014-10-11 18:27:30 +03003116 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003117
Bob Ham91880f12016-01-12 10:21:47 +00003118 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003119
Giulio Camuffo954f1832014-10-11 18:27:30 +03003120 wl_list_init(&b->sprite_list);
3121 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003122
Giulio Camuffo954f1832014-10-11 18:27:30 +03003123 if (udev_input_init(&b->input,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003124 compositor, b->udev, seat_id) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003125 weston_log("failed to create input devices\n");
3126 goto err_sprite;
3127 }
3128
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003129 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003130 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003131 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003132 }
3133
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003134 /* A this point we have some idea of whether or not we have a working
3135 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003136 if (!b->cursors_are_broken)
3137 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003138
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003139 path = NULL;
3140
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 loop = wl_display_get_event_loop(compositor->wl_display);
3142 b->drm_source =
3143 wl_event_loop_add_fd(loop, b->drm.fd,
3144 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003145
Giulio Camuffo954f1832014-10-11 18:27:30 +03003146 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3147 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003148 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003149 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003150 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003151 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003152 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003153 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003154 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003155 udev_monitor_get_fd(b->udev_monitor),
3156 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003157
Giulio Camuffo954f1832014-10-11 18:27:30 +03003158 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003159 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003160 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003161 }
3162
Daniel Stonea96b93c2012-06-22 14:04:37 +01003163 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003164
Giulio Camuffo954f1832014-10-11 18:27:30 +03003165 weston_compositor_add_debug_binding(compositor, KEY_O,
3166 planes_binding, b);
3167 weston_compositor_add_debug_binding(compositor, KEY_C,
3168 planes_binding, b);
3169 weston_compositor_add_debug_binding(compositor, KEY_V,
3170 planes_binding, b);
3171 weston_compositor_add_debug_binding(compositor, KEY_Q,
3172 recorder_binding, b);
3173 weston_compositor_add_debug_binding(compositor, KEY_W,
3174 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003175
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003176 if (compositor->renderer->import_dmabuf) {
3177 if (linux_dmabuf_setup(compositor) < 0)
3178 weston_log("Error: initializing dmabuf "
3179 "support failed.\n");
3180 }
3181
Giulio Camuffo954f1832014-10-11 18:27:30 +03003182 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003183
Giulio Camuffo954f1832014-10-11 18:27:30 +03003184 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003185
3186err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003187 wl_event_source_remove(b->udev_drm_source);
3188 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003189err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003190 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003191err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003192 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003193err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003194 gbm_device_destroy(b->gbm);
3195 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003196err_udev_dev:
3197 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003198err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003199 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003200err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003202err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003203 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003205 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003206}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003207
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003208static void
3209config_init_to_defaults(struct weston_drm_backend_config *config)
3210{
3211}
3212
Giulio Camuffo954f1832014-10-11 18:27:30 +03003213WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003214backend_init(struct weston_compositor *compositor,
3215 int *argc, char *argv[],
3216 struct weston_config *wc,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003217 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003218{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003219 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003220 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003221
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003222 if (config_base == NULL ||
3223 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3224 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3225 weston_log("drm backend config structure is invalid\n");
3226 return -1;
3227 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003228
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003229 config_init_to_defaults(&config);
3230 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003231
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003232 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003233 if (b == NULL)
3234 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003235
Giulio Camuffo954f1832014-10-11 18:27:30 +03003236 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003237}