blob: a47b453477e70e1b049e9f49a06bfe4417445e81 [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
Jon Cruz35b2eaa2015-06-15 15:37:08 -070049#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020050#include "shared/timespec-util.h"
Kristian Høgsberg36d5fac2014-01-27 23:02:35 -080051#include "libbacklight.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040052#include "compositor.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"
Peter Hutterer823ad332014-11-26 07:06:31 +100055#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010056#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030057#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020058#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030059#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040060
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030061#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
62#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
63#endif
64
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030065#ifndef DRM_CAP_CURSOR_WIDTH
66#define DRM_CAP_CURSOR_WIDTH 0x8
67#endif
68
69#ifndef DRM_CAP_CURSOR_HEIGHT
70#define DRM_CAP_CURSOR_HEIGHT 0x9
71#endif
72
73#ifndef GBM_BO_USE_CURSOR
74#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
75#endif
76
Kristian Høgsberg061c4252012-06-28 11:28:15 -040077static int option_current_mode = 0;
Scott Moreau8ab5d452012-07-30 19:51:08 -060078
79enum output_config {
80 OUTPUT_CONFIG_INVALID = 0,
81 OUTPUT_CONFIG_OFF,
82 OUTPUT_CONFIG_PREFERRED,
83 OUTPUT_CONFIG_CURRENT,
Scott Moreau8f37e0b2012-07-31 15:30:41 -060084 OUTPUT_CONFIG_MODE,
85 OUTPUT_CONFIG_MODELINE
Scott Moreau8ab5d452012-07-30 19:51:08 -060086};
87
Giulio Camuffo954f1832014-10-11 18:27:30 +030088struct drm_backend {
89 struct weston_backend base;
90 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091
92 struct udev *udev;
93 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040094
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010095 struct udev_monitor *udev_monitor;
96 struct wl_event_source *udev_drm_source;
97
Benjamin Franzke2af7f102011-03-02 11:14:59 +010098 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010099 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100100 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300101 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100102 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200103 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500104 uint32_t *crtcs;
105 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -0500106 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100107 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700108 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100109 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200110
Rob Clark4339add2012-08-09 14:18:28 -0500111 /* we need these parameters in order to not fail drmModeAddFB2()
112 * due to out of bounds dimensions, and then mistakenly set
113 * sprites_are_broken:
114 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200115 uint32_t min_width, max_width;
116 uint32_t min_height, max_height;
117 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500118
Jesse Barnes58ef3792012-02-23 09:45:49 -0500119 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500120 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200121 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500122
Rob Clarkab5b1e32012-08-09 13:24:45 -0500123 int cursors_are_broken;
124
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200125 int use_pixman;
126
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200127 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300128
Rob Bradfordd355b802013-05-31 18:09:55 +0100129 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300130
Daniel Stone70d337d2015-06-16 18:42:23 +0100131 int32_t cursor_width;
132 int32_t cursor_height;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400133};
134
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400135struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500136 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400137 drmModeModeInfo mode_info;
138};
139
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300140struct drm_output;
141
142struct drm_fb {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300143 struct drm_output *output;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200144 uint32_t fb_id, stride, handle, size;
145 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300146 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200147 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200148
149 /* Used by gbm fbs */
150 struct gbm_bo *bo;
151
152 /* Used by dumb fbs */
153 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300154};
155
Richard Hughes2b2092a2013-04-24 14:58:02 +0100156struct drm_edid {
157 char eisa_id[13];
158 char monitor_name[13];
159 char pnp_id[5];
160 char serial_number[13];
161};
162
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400163struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400166 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500167 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700169 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100170 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300171 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100172 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200173
Daniel Stone36609c72015-06-18 07:49:02 +0100174 enum dpms_enum dpms;
175
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300176 int vblank_pending;
177 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800178 int destroy_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300179
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100180 struct gbm_surface *gbm_surface;
181 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400182 struct weston_plane cursor_plane;
183 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500184 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400185 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300186 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200187 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200188
189 struct drm_fb *dumb[2];
190 pixman_image_t *image[2];
191 int current_image;
192 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300193
194 struct vaapi_recorder *recorder;
195 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400196};
197
Jesse Barnes58ef3792012-02-23 09:45:49 -0500198/*
199 * An output has a primary display plane plus zero or more sprites for
200 * blending display contents.
201 */
202struct drm_sprite {
203 struct wl_list link;
204
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400205 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500206
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200207 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300208 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300209 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500210
Jesse Barnes58ef3792012-02-23 09:45:49 -0500211 uint32_t possible_crtcs;
212 uint32_t plane_id;
213 uint32_t count_formats;
214
215 int32_t src_x, src_y;
216 uint32_t src_w, src_h;
217 uint32_t dest_x, dest_y;
218 uint32_t dest_w, dest_h;
219
220 uint32_t formats[];
221};
222
Kristian Høgsbergd8e98332013-10-16 16:15:11 -0700223struct drm_parameters {
224 int connector;
225 int tty;
226 int use_pixman;
227 const char *seat_id;
228};
229
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300230static struct gl_renderer_interface *gl_renderer;
231
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500232static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400233
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400234static void
235drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400236
Mario Kleinerf507ec32015-06-21 21:25:14 +0200237static void
238drm_output_update_msc(struct drm_output *output, unsigned int seq);
239
Jesse Barnes58ef3792012-02-23 09:45:49 -0500240static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200241drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500242{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200243 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300244 struct drm_backend *b =(struct drm_backend *)ec->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500245 int crtc;
246
Giulio Camuffo954f1832014-10-11 18:27:30 +0300247 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
248 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500249 continue;
250
251 if (supported & (1 << crtc))
252 return -1;
253 }
254
255 return 0;
256}
257
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300258static void
259drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
260{
261 struct drm_fb *fb = data;
262 struct gbm_device *gbm = gbm_bo_get_device(bo);
263
264 if (fb->fb_id)
265 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
266
Pekka Paalanende685b82012-12-04 15:58:12 +0200267 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300268
269 free(data);
270}
271
272static struct drm_fb *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300273drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200274{
275 struct drm_fb *fb;
276 int ret;
277
278 struct drm_mode_create_dumb create_arg;
279 struct drm_mode_destroy_dumb destroy_arg;
280 struct drm_mode_map_dumb map_arg;
281
Peter Huttererf3d62272013-08-08 11:57:05 +1000282 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200283 if (!fb)
284 return NULL;
285
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700286 memset(&create_arg, 0, sizeof create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200287 create_arg.bpp = 32;
288 create_arg.width = width;
289 create_arg.height = height;
290
Giulio Camuffo954f1832014-10-11 18:27:30 +0300291 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200292 if (ret)
293 goto err_fb;
294
295 fb->handle = create_arg.handle;
296 fb->stride = create_arg.pitch;
297 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300298 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299
Giulio Camuffo954f1832014-10-11 18:27:30 +0300300 ret = drmModeAddFB(b->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200301 fb->stride, fb->handle, &fb->fb_id);
302 if (ret)
303 goto err_bo;
304
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700305 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200306 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400307 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200308 if (ret)
309 goto err_add_fb;
310
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500311 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300312 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200313 if (fb->map == MAP_FAILED)
314 goto err_add_fb;
315
316 return fb;
317
318err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300319 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200320err_bo:
321 memset(&destroy_arg, 0, sizeof(destroy_arg));
322 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300323 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200324err_fb:
325 free(fb);
326 return NULL;
327}
328
329static void
330drm_fb_destroy_dumb(struct drm_fb *fb)
331{
332 struct drm_mode_destroy_dumb destroy_arg;
333
334 if (!fb->map)
335 return;
336
337 if (fb->fb_id)
338 drmModeRmFB(fb->fd, fb->fb_id);
339
340 weston_buffer_reference(&fb->buffer_ref, NULL);
341
342 munmap(fb->map, fb->size);
343
344 memset(&destroy_arg, 0, sizeof(destroy_arg));
345 destroy_arg.handle = fb->handle;
346 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
347
348 free(fb);
349}
350
351static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500352drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300353 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300354{
355 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200356 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200357 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300358 int ret;
359
360 if (fb)
361 return fb;
362
Bryce Harringtonde16d892014-11-20 22:21:57 -0800363 fb = zalloc(sizeof *fb);
364 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200365 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300366
367 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300368
369 width = gbm_bo_get_width(bo);
370 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200371 fb->stride = gbm_bo_get_stride(bo);
372 fb->handle = gbm_bo_get_handle(bo).u32;
373 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300374 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300375
Giulio Camuffo954f1832014-10-11 18:27:30 +0300376 if (backend->min_width > width || width > backend->max_width ||
377 backend->min_height > height ||
378 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200379 weston_log("bo geometry out of bounds\n");
380 goto err_free;
381 }
382
383 ret = -1;
384
Giulio Camuffo954f1832014-10-11 18:27:30 +0300385 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200386 handles[0] = fb->handle;
387 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200388 offsets[0] = 0;
389
Giulio Camuffo954f1832014-10-11 18:27:30 +0300390 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200391 format, handles, pitches, offsets,
392 &fb->fb_id, 0);
393 if (ret) {
394 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300395 backend->no_addfb2 = 1;
396 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200397 }
398 }
399
400 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300401 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200402 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200403
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300404 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200405 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200406 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300407 }
408
409 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
410
411 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200412
413err_free:
414 free(fb);
415 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300416}
417
418static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500419drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200420{
Pekka Paalanende685b82012-12-04 15:58:12 +0200421 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200422
423 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200424
Pekka Paalanende685b82012-12-04 15:58:12 +0200425 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200426}
427
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200428static void
429drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
430{
431 if (!fb)
432 return;
433
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200434 if (fb->map &&
435 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200436 drm_fb_destroy_dumb(fb);
437 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200438 if (fb->is_client_buffer)
439 gbm_bo_destroy(fb->bo);
440 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100441 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600442 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200443 }
444}
445
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500446static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200447drm_output_check_scanout_format(struct drm_output *output,
448 struct weston_surface *es, struct gbm_bo *bo)
449{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200450 uint32_t format;
451 pixman_region32_t r;
452
453 format = gbm_bo_get_format(bo);
454
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700455 if (format == GBM_FORMAT_ARGB8888) {
456 /* We can scanout an ARGB buffer if the surface's
457 * opaque region covers the whole output, but we have
458 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800459 pixman_region32_init_rect(&r, 0, 0,
460 output->base.width,
461 output->base.height);
462 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200463
464 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500465 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200466
467 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500468 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700469
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100470 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700471 return format;
472
473 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200474}
475
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400476static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200477drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500478 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500479{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300480 struct drm_backend *b =
481 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200483 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300484 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500485 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500486
Jason Ekstranda7af7042013-10-12 22:38:11 -0500487 if (ev->geometry.x != output->base.x ||
488 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300489 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200490 buffer->width != output->base.current_mode->width ||
491 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200492 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500493 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400494 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500495
Pekka Paalanen5580f222015-02-17 16:33:18 +0200496 if (ev->geometry.scissor_enabled)
497 return NULL;
498
Giulio Camuffo954f1832014-10-11 18:27:30 +0300499 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700500 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500501
Rob Bradford9b101872012-09-14 23:25:41 +0100502 /* Unable to use the buffer for scanout */
503 if (!bo)
504 return NULL;
505
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500507 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300508 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400509 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300510 }
511
Giulio Camuffo954f1832014-10-11 18:27:30 +0300512 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300513 if (!output->next) {
514 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400515 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300516 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500517
Pekka Paalanende685b82012-12-04 15:58:12 +0200518 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500519
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400520 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500521}
522
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500523static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200524drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400525{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300526 struct drm_backend *b =
527 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300528 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400529
Giulio Camuffo954f1832014-10-11 18:27:30 +0300530 output->base.compositor->renderer->repaint_output(&output->base,
531 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400532
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100533 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300534 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200535 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400536 return;
537 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300538
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100539 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300540 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200541 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100542 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300543 return;
544 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400545}
546
547static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200548drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
549{
550 struct weston_compositor *ec = output->base.compositor;
551 pixman_region32_t total_damage, previous_damage;
552
553 pixman_region32_init(&total_damage);
554 pixman_region32_init(&previous_damage);
555
556 pixman_region32_copy(&previous_damage, damage);
557
558 pixman_region32_union(&total_damage, damage, &output->previous_damage);
559 pixman_region32_copy(&output->previous_damage, &previous_damage);
560
561 output->current_image ^= 1;
562
563 output->next = output->dumb[output->current_image];
564 pixman_renderer_output_set_buffer(&output->base,
565 output->image[output->current_image]);
566
567 ec->renderer->repaint_output(&output->base, &total_damage);
568
569 pixman_region32_fini(&total_damage);
570 pixman_region32_fini(&previous_damage);
571}
572
573static void
574drm_output_render(struct drm_output *output, pixman_region32_t *damage)
575{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300576 struct weston_compositor *c = output->base.compositor;
577 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200578
Giulio Camuffo954f1832014-10-11 18:27:30 +0300579 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200580 drm_output_render_pixman(output, damage);
581 else
582 drm_output_render_gl(output, damage);
583
Giulio Camuffo954f1832014-10-11 18:27:30 +0300584 pixman_region32_subtract(&c->primary_plane.damage,
585 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200586}
587
588static void
Richard Hughese7299962013-05-01 21:52:12 +0100589drm_output_set_gamma(struct weston_output *output_base,
590 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
591{
592 int rc;
593 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300594 struct drm_backend *backend =
595 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100596
597 /* check */
598 if (output_base->gamma_size != size)
599 return;
600 if (!output->original_crtc)
601 return;
602
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100604 output->crtc_id,
605 size, r, g, b);
606 if (rc)
607 weston_log("set gamma failed: %m\n");
608}
609
Bryce Harringtonada4f072015-06-30 13:25:46 -0700610/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200611 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700612 * The pipe parameter indicates which CRTC is in use. Knowing this, we
613 * can determine which vblank sequence type to use for it. Traditional
614 * cards had only two CRTCs, with CRTC 0 using no special flags, and
615 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
616 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200617 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700618 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
619 * 0-31. If this is non-zero it indicates we're dealing with a
620 * multi-gpu situation and we need to calculate the vblank sync
621 * using DRM_BLANK_HIGH_CRTC_MASK.
622 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300623static unsigned int
624drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200625{
626 if (output->pipe > 1)
627 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
628 DRM_VBLANK_HIGH_CRTC_MASK;
629 else if (output->pipe > 0)
630 return DRM_VBLANK_SECONDARY;
631 else
632 return 0;
633}
634
David Herrmann1edf44c2013-10-22 17:11:26 +0200635static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500636drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400637 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100638{
639 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300640 struct drm_backend *backend =
641 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500642 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400643 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500644 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100645
Xiong Zhangabd5d472013-10-11 14:43:07 +0800646 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200647 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800648
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300649 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400650 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300651 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200652 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100653
Hardeningff39efa2013-09-18 23:56:35 +0200654 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200655 if (!output->current ||
656 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300657 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300658 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400659 &output->connector_id, 1,
660 &mode->mode_info);
661 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200662 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200663 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400664 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300665 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200666 }
667
Giulio Camuffo954f1832014-10-11 18:27:30 +0300668 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300669 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500670 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200671 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200672 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500673 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100674
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300675 output->page_flip_pending = 1;
676
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400677 drm_output_set_cursor(output);
678
Jesse Barnes58ef3792012-02-23 09:45:49 -0500679 /*
680 * Now, update all the sprite surfaces
681 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300682 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200683 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500684 drmVBlank vbl = {
685 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
686 .request.sequence = 1,
687 };
688
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200689 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200690 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500691 continue;
692
Giulio Camuffo954f1832014-10-11 18:27:30 +0300693 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200694 fb_id = s->next->fb_id;
695
Giulio Camuffo954f1832014-10-11 18:27:30 +0300696 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200697 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500698 s->dest_x, s->dest_y,
699 s->dest_w, s->dest_h,
700 s->src_x, s->src_y,
701 s->src_w, s->src_h);
702 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200703 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500704 ret, strerror(errno));
705
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200706 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500707
Jesse Barnes58ef3792012-02-23 09:45:49 -0500708 /*
709 * Queue a vblank signal so we know when the surface
710 * becomes active on the display or has been replaced.
711 */
712 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300713 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500714 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200715 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500716 ret, strerror(errno));
717 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300718
719 s->output = output;
720 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500721 }
722
David Herrmann1edf44c2013-10-22 17:11:26 +0200723 return 0;
724
725err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800726 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200727 if (output->next) {
728 drm_output_release_fb(output, output->next);
729 output->next = NULL;
730 }
731
732 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400733}
734
735static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200736drm_output_start_repaint_loop(struct weston_output *output_base)
737{
738 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300739 struct drm_backend *backend = (struct drm_backend *)
740 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200741 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200742 struct timespec ts, tnow;
743 struct timespec vbl2now;
744 int64_t refresh_nsec;
745 int ret;
746 drmVBlank vbl = {
747 .request.type = DRM_VBLANK_RELATIVE,
748 .request.sequence = 0,
749 .request.signal = 0,
750 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300751
Xiong Zhangabd5d472013-10-11 14:43:07 +0800752 if (output->destroy_pending)
753 return;
754
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300755 if (!output->current) {
756 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200757 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300758 }
759
Mario Kleinerf507ec32015-06-21 21:25:14 +0200760 /* Try to get current msc and timestamp via instant query */
761 vbl.request.type |= drm_waitvblank_pipe(output);
762 ret = drmWaitVBlank(backend->drm.fd, &vbl);
763
764 /* Error ret or zero timestamp means failure to get valid timestamp */
765 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
766 ts.tv_sec = vbl.reply.tval_sec;
767 ts.tv_nsec = vbl.reply.tval_usec * 1000;
768
769 /* Valid timestamp for most recent vblank - not stale?
770 * Stale ts could happen on Linux 3.17+, so make sure it
771 * is not older than 1 refresh duration since now.
772 */
773 weston_compositor_read_presentation_clock(backend->compositor,
774 &tnow);
775 timespec_sub(&vbl2now, &tnow, &ts);
776 refresh_nsec =
777 millihz_to_nsec(output->base.current_mode->refresh);
778 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
779 drm_output_update_msc(output, vbl.reply.sequence);
780 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200781 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200782 return;
783 }
784 }
785
786 /* Immediate query didn't provide valid timestamp.
787 * Use pageflip fallback.
788 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300789 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200790
Giulio Camuffo954f1832014-10-11 18:27:30 +0300791 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200792 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
793 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200794 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200795 }
David Herrmann3c688c52013-10-22 17:11:25 +0200796
797 return;
798
799finish_frame:
800 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300801 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200802 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200803 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200804}
805
806static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400807drm_output_update_msc(struct drm_output *output, unsigned int seq)
808{
809 uint64_t msc_hi = output->base.msc >> 32;
810
811 if (seq < (output->base.msc & 0xffffffff))
812 msc_hi++;
813
814 output->base.msc = (msc_hi << 32) + seq;
815}
816
817static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500818vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
819 void *data)
820{
821 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300822 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400823 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200824 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
825 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300826
Pekka Paalanen641307c2014-09-23 22:08:47 -0400827 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300828 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500829
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200830 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200831 s->current = s->next;
832 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300833
834 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400835 ts.tv_sec = sec;
836 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200837 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300838 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500839}
840
841static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800842drm_output_destroy(struct weston_output *output_base);
843
844static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400845page_flip_handler(int fd, unsigned int frame,
846 unsigned int sec, unsigned int usec, void *data)
847{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200848 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400849 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200850 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
851 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
852 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400853
Pekka Paalanen641307c2014-09-23 22:08:47 -0400854 drm_output_update_msc(output, frame);
855
Jonas Ådahle5a12252013-04-05 23:07:11 +0200856 /* We don't set page_flip_pending on start_repaint_loop, in that case
857 * we just want to page flip to the current buffer to get an accurate
858 * timestamp */
859 if (output->page_flip_pending) {
860 drm_output_release_fb(output, output->current);
861 output->current = output->next;
862 output->next = NULL;
863 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300864
Jonas Ådahle5a12252013-04-05 23:07:11 +0200865 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400866
Xiong Zhangabd5d472013-10-11 14:43:07 +0800867 if (output->destroy_pending)
868 drm_output_destroy(&output->base);
869 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400870 ts.tv_sec = sec;
871 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200872 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300873
874 /* We can't call this from frame_notify, because the output's
875 * repaint needed flag is cleared just after that */
876 if (output->recorder)
877 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300878 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200879}
880
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500881static uint32_t
882drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500884{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500885 uint32_t i, format;
886
887 format = gbm_bo_get_format(bo);
888
889 if (format == GBM_FORMAT_ARGB8888) {
890 pixman_region32_t r;
891
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500892 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600893 ev->surface->width,
894 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500896
897 if (!pixman_region32_not_empty(&r))
898 format = GBM_FORMAT_XRGB8888;
899
900 pixman_region32_fini(&r);
901 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500902
903 for (i = 0; i < s->count_formats; i++)
904 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500905 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500906
907 return 0;
908}
909
910static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500912{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 return !ev->transform.enabled ||
914 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500915}
916
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400917static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200918drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500920{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200921 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300922 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200923 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300924 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500925 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300926 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500927 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500928 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500929 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200930 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500931 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400932 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500933
Giulio Camuffo954f1832014-10-11 18:27:30 +0300934 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200935 return NULL;
936
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200937 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200938 return NULL;
939
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200940 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200941 return NULL;
942
Giulio Camuffo954f1832014-10-11 18:27:30 +0300943 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500945
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200946 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400950 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300951 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200954 return NULL;
955
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300956 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500957 return NULL;
958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400960 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200963 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500964 continue;
965
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200966 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500967 found = 1;
968 break;
969 }
970 }
971
972 /* No sprites available */
973 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400974 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500975
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300976 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -0700977#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300978 /* XXX: TODO:
979 *
980 * Use AddFB2 directly, do not go via GBM.
981 * Add support for multiplanar formats.
982 * Both require refactoring in the DRM-backend to
983 * support a mix of gbm_bos and drmfbs.
984 */
985 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000986 .fd = dmabuf->attributes.fd[0],
987 .width = dmabuf->attributes.width,
988 .height = dmabuf->attributes.height,
989 .stride = dmabuf->attributes.stride[0],
990 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300991 };
992
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000993 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300994 return NULL;
995
996 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
997 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -0700998#else
999 return NULL;
1000#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001001 } else {
1002 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1003 buffer_resource, GBM_BO_USE_SCANOUT);
1004 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001005 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001009 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001010 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001011 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001012 }
1013
Giulio Camuffo954f1832014-10-11 18:27:30 +03001014 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001015 if (!s->next) {
1016 gbm_bo_destroy(bo);
1017 return NULL;
1018 }
1019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001023 s->plane.x = box->x1;
1024 s->plane.y = box->y1;
1025
Jesse Barnes58ef3792012-02-23 09:45:49 -05001026 /*
1027 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001028 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001029 * for us already).
1030 */
1031 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001033 &output->base.region);
1034 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001035 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001036 tbox = weston_transformed_rect(output->base.width,
1037 output->base.height,
1038 output->base.transform,
1039 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001040 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001041 s->dest_x = tbox.x1;
1042 s->dest_y = tbox.y1;
1043 s->dest_w = tbox.x2 - tbox.x1;
1044 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001045 pixman_region32_fini(&dest_rect);
1046
1047 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001049 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001050 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001051
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 weston_view_from_global_fixed(ev,
1053 wl_fixed_from_int(box->x1),
1054 wl_fixed_from_int(box->y1),
1055 &sx1, &sy1);
1056 weston_view_from_global_fixed(ev,
1057 wl_fixed_from_int(box->x2),
1058 wl_fixed_from_int(box->y2),
1059 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001060
1061 if (sx1 < 0)
1062 sx1 = 0;
1063 if (sy1 < 0)
1064 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001065 if (sx2 > wl_fixed_from_int(ev->surface->width))
1066 sx2 = wl_fixed_from_int(ev->surface->width);
1067 if (sy2 > wl_fixed_from_int(ev->surface->height))
1068 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001069
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001070 tbox.x1 = sx1;
1071 tbox.y1 = sy1;
1072 tbox.x2 = sx2;
1073 tbox.y2 = sy2;
1074
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001075 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1076 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001077 viewport->buffer.transform,
1078 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001079 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001080
1081 s->src_x = tbox.x1 << 8;
1082 s->src_y = tbox.y1 << 8;
1083 s->src_w = (tbox.x2 - tbox.x1) << 8;
1084 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001085 pixman_region32_fini(&src_rect);
1086
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001087 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001088}
1089
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001090static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001091drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001093{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001094 struct drm_backend *b =
1095 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001096 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001097 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001098
Derek Foremanbe428b32015-11-24 11:39:38 -06001099 if (ev->transform.enabled &&
1100 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1101 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001102 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001103 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001104 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1105 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001106 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001107 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001109 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001110 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001111 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001112 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001113 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001114 if (ev->geometry.scissor_enabled)
1115 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001116 if (ev->surface->buffer_ref.buffer == NULL)
1117 return NULL;
1118 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1119 if (!shmbuf)
1120 return NULL;
1121 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1122 return NULL;
1123 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001124 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001125 return NULL;
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001128
1129 return &output->cursor_plane;
1130}
1131
Pekka Paalanend0ead482014-06-16 12:05:40 +03001132/**
1133 * Update the image for the current cursor surface
1134 *
1135 * @param b DRM backend structure
1136 * @param bo GBM buffer object to write into
1137 * @param ev View to use for cursor image
1138 */
1139static void
1140cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1141 struct weston_view *ev)
1142{
1143 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1144 uint32_t buf[b->cursor_width * b->cursor_height];
1145 int32_t stride;
1146 uint8_t *s;
1147 int i;
1148
1149 assert(buffer && buffer->shm_buffer);
1150 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1151 assert(ev->surface->width <= b->cursor_width);
1152 assert(ev->surface->height <= b->cursor_height);
1153
1154 memset(buf, 0, sizeof buf);
1155 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1156 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1157
1158 wl_shm_buffer_begin_access(buffer->shm_buffer);
1159 for (i = 0; i < ev->surface->height; i++)
1160 memcpy(buf + i * b->cursor_width,
1161 s + i * stride,
1162 ev->surface->width * 4);
1163 wl_shm_buffer_end_access(buffer->shm_buffer);
1164
1165 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1166 weston_log("failed update cursor: %m\n");
1167}
1168
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001169static void
1170drm_output_set_cursor(struct drm_output *output)
1171{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001173 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001174 struct drm_backend *b =
1175 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001176 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001177 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001178 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 output->cursor_view = NULL;
1181 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001182 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001183 output->cursor_plane.x = INT32_MIN;
1184 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001185 return;
1186 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001187
Neil Robertse5051712013-11-13 15:44:06 +00001188 buffer = ev->surface->buffer_ref.buffer;
1189
1190 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001191 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001192 pixman_region32_fini(&output->cursor_plane.damage);
1193 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001194 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001195 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001196
Pekka Paalanend0ead482014-06-16 12:05:40 +03001197 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001198 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001199 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1200 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001201 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001203 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001204 }
1205
Derek Foremanbe428b32015-11-24 11:39:38 -06001206 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001207
1208 /* From global to output space, output transform is guaranteed to be
1209 * NORMAL by drm_output_prepare_cursor_view().
1210 */
1211 x = (x - output->base.x) * output->base.current_scale;
1212 y = (y - output->base.y) * output->base.current_scale;
1213
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001214 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001215 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001216 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001217 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001218 }
1219
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001220 output->cursor_plane.x = x;
1221 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001222 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001223}
1224
Jesse Barnes58ef3792012-02-23 09:45:49 -05001225static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001226drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001227{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001228 struct drm_backend *b =
1229 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001230 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001232 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001233 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001234
1235 /*
1236 * Find a surface for each sprite in the output using some heuristics:
1237 * 1) size
1238 * 2) frequency of update
1239 * 3) opacity (though some hw might support alpha blending)
1240 * 4) clipping (this can be fixed with color keys)
1241 *
1242 * The idea is to save on blitting since this should save power.
1243 * If we can get a large video surface on the sprite for example,
1244 * the main display surface may not need to update at all, and
1245 * the client buffer can be used directly for the sprite surface
1246 * as we do for flipping full screen surfaces.
1247 */
1248 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001249 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001252 struct weston_surface *es = ev->surface;
1253
1254 /* Test whether this buffer can ever go into a plane:
1255 * non-shm, or small enough to be a cursor.
1256 *
1257 * Also, keep a reference when using the pixman renderer.
1258 * That makes it possible to do a seamless switch to the GL
1259 * renderer and since the pixman renderer keeps a reference
1260 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001261 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001262 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001263 (es->buffer_ref.buffer &&
1264 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001265 (ev->surface->width <= b->cursor_width &&
1266 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001267 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001268 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001269 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001270
Jesse Barnes58ef3792012-02-23 09:45:49 -05001271 pixman_region32_init(&surface_overlap);
1272 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001274
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001275 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001276 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001277 next_plane = primary;
1278 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001280 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001282 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001284 if (next_plane == NULL)
1285 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001288
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001289 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001290 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001292
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001293 if (next_plane == primary ||
1294 next_plane == &output->cursor_plane) {
1295 /* cursor plane involves a copy */
1296 ev->psf_flags = 0;
1297 } else {
1298 /* All other planes are a direct scanout of a
1299 * single client buffer.
1300 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001301 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001302 }
1303
Jesse Barnes58ef3792012-02-23 09:45:49 -05001304 pixman_region32_fini(&surface_overlap);
1305 }
1306 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001307}
1308
Matt Roper361d2ad2011-08-29 13:52:23 -07001309static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001310drm_output_fini_pixman(struct drm_output *output);
1311
1312static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001313drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001314{
1315 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001316 struct drm_backend *b =
1317 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001318 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001319
Xiong Zhangabd5d472013-10-11 14:43:07 +08001320 if (output->page_flip_pending) {
1321 output->destroy_pending = 1;
1322 weston_log("destroy output while page flip pending\n");
1323 return;
1324 }
1325
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001326 if (output->backlight)
1327 backlight_destroy(output->backlight);
1328
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001329 drmModeFreeProperty(output->dpms_prop);
1330
Matt Roper361d2ad2011-08-29 13:52:23 -07001331 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001332 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001333
1334 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001335 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001336 origcrtc->x, origcrtc->y,
1337 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001338 drmModeFreeCrtc(origcrtc);
1339
Giulio Camuffo954f1832014-10-11 18:27:30 +03001340 b->crtc_allocator &= ~(1 << output->crtc_id);
1341 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001342
Giulio Camuffo954f1832014-10-11 18:27:30 +03001343 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001344 drm_output_fini_pixman(output);
1345 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001346 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001347 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001348 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001349
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001350 weston_plane_release(&output->fb_plane);
1351 weston_plane_release(&output->cursor_plane);
1352
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001353 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001354
Matt Roper361d2ad2011-08-29 13:52:23 -07001355 free(output);
1356}
1357
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001358/**
1359 * Find the closest-matching mode for a given target
1360 *
1361 * Given a target mode, find the most suitable mode amongst the output's
1362 * current mode list to use, preferring the current mode if possible, to
1363 * avoid an expensive mode switch.
1364 *
1365 * @param output DRM output
1366 * @param target_mode Mode to attempt to match
1367 * @returns Pointer to a mode from the output's mode list
1368 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001369static struct drm_mode *
1370choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1371{
1372 struct drm_mode *tmp_mode = NULL, *mode;
1373
Hardeningff39efa2013-09-18 23:56:35 +02001374 if (output->base.current_mode->width == target_mode->width &&
1375 output->base.current_mode->height == target_mode->height &&
1376 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001377 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001378 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001379
1380 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1381 if (mode->mode_info.hdisplay == target_mode->width &&
1382 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001383 if (mode->base.refresh == target_mode->refresh ||
1384 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001385 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001386 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001387 tmp_mode = mode;
1388 }
1389 }
1390
1391 return tmp_mode;
1392}
1393
1394static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001395drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001396static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001397drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001398
1399static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001400drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1401{
1402 struct drm_output *output;
1403 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001404 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001405
1406 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001407 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001408 return -1;
1409 }
1410
1411 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001412 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001413 return -1;
1414 }
1415
Giulio Camuffo954f1832014-10-11 18:27:30 +03001416 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001417 output = (struct drm_output *)output_base;
1418 drm_mode = choose_mode (output, mode);
1419
1420 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001421 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001422 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001423 }
1424
Hardeningff39efa2013-09-18 23:56:35 +02001425 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001426 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001427
Hardeningff39efa2013-09-18 23:56:35 +02001428 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001429
Hardeningff39efa2013-09-18 23:56:35 +02001430 output->base.current_mode = &drm_mode->base;
1431 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001432 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1433
Alex Wub7b8bda2012-04-17 17:20:48 +08001434 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001435 drm_output_release_fb(output, output->current);
1436 drm_output_release_fb(output, output->next);
1437 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001438
Giulio Camuffo954f1832014-10-11 18:27:30 +03001439 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001440 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001441 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001442 weston_log("failed to init output pixman state with "
1443 "new mode\n");
1444 return -1;
1445 }
1446 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001447 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001448 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001449
Giulio Camuffo954f1832014-10-11 18:27:30 +03001450 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001451 weston_log("failed to init output egl state with "
1452 "new mode");
1453 return -1;
1454 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001455 }
1456
Alex Wub7b8bda2012-04-17 17:20:48 +08001457 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001458}
1459
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001460static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001461on_drm_input(int fd, uint32_t mask, void *data)
1462{
1463 drmEventContext evctx;
1464
1465 memset(&evctx, 0, sizeof evctx);
1466 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1467 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001468 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001469 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001470
1471 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001472}
1473
1474static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001475init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001476{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001477 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001478 uint64_t cap;
1479 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001480 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001481
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001482 sysnum = udev_device_get_sysnum(device);
1483 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001484 b->drm.id = atoi(sysnum);
1485 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001486 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001487 return -1;
1488 }
1489
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001490 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001491 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001492 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001493 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001494 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001495 udev_device_get_devnode(device));
1496 return -1;
1497 }
1498
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001499 weston_log("using %s\n", filename);
1500
Giulio Camuffo954f1832014-10-11 18:27:30 +03001501 b->drm.fd = fd;
1502 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001503
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001504 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1505 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001506 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001507 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001508 clk_id = CLOCK_REALTIME;
1509
Giulio Camuffo954f1832014-10-11 18:27:30 +03001510 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001511 weston_log("Error: failed to set presentation clock %d.\n",
1512 clk_id);
1513 return -1;
1514 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001515
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001516 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1517 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001518 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001519 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001520 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001521
1522 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1523 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001524 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001525 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001526 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001527
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001528 return 0;
1529}
1530
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001531static struct gbm_device *
1532create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001533{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001534 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001535
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001536 gl_renderer = weston_load_module("gl-renderer.so",
1537 "gl_renderer_interface");
1538 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001539 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001540
1541 /* GBM will load a dri driver, but even though they need symbols from
1542 * libglapi, in some version of Mesa they are not linked to it. Since
1543 * only the gl-renderer module links to it, the call above won't make
1544 * these symbols globally available, and loading the DRI driver fails.
1545 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1546 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1547
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001548 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001549
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001550 return gbm;
1551}
1552
Bryce Harringtonc056a982015-05-19 15:25:18 -07001553/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001554 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001555 *
1556 * This returns 0 if substitution isn't possible, but 0 might be a
1557 * legitimate format for other EGL platforms, so the caller is
1558 * responsible for checking for 0 before calling gl_renderer->create().
1559 *
1560 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1561 * but it's entirely possible we'll see this again on other implementations.
1562 */
1563static int
1564fallback_format_for(uint32_t format)
1565{
1566 switch (format) {
1567 case GBM_FORMAT_XRGB8888:
1568 return GBM_FORMAT_ARGB8888;
1569 case GBM_FORMAT_XRGB2101010:
1570 return GBM_FORMAT_ARGB2101010;
1571 default:
1572 return 0;
1573 }
1574}
1575
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001576static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001578{
Derek Foreman6d556372015-11-04 14:47:33 -06001579 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001580 b->gbm_format,
1581 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001582 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001583 };
Derek Foreman6d556372015-11-04 14:47:33 -06001584 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001585
Derek Foremanc4cfe852015-05-15 12:12:40 -05001586 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001587 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001589 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001591 gl_renderer->opaque_attribs,
1592 format,
1593 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001594 return -1;
1595 }
1596
1597 return 0;
1598}
1599
1600static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001601init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001602{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001603 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001604
Giulio Camuffo954f1832014-10-11 18:27:30 +03001605 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001606 return -1;
1607
Giulio Camuffo954f1832014-10-11 18:27:30 +03001608 if (drm_backend_create_gl_renderer(b) < 0) {
1609 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001610 return -1;
1611 }
1612
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001613 return 0;
1614}
1615
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001616static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001617init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001618{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001619 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001620}
1621
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001622/**
1623 * Add a mode to output's mode list
1624 *
1625 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1626 * output's mode list.
1627 *
1628 * @param output DRM output to add mode to
1629 * @param info DRM mode structure to add
1630 * @returns Newly-allocated Weston/DRM mode structure
1631 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001632static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001633drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001634{
1635 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001636 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001637
1638 mode = malloc(sizeof *mode);
1639 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001640 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001641
1642 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001643 mode->base.width = info->hdisplay;
1644 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001645
1646 /* Calculate higher precision (mHz) refresh rate */
1647 refresh = (info->clock * 1000000LL / info->htotal +
1648 info->vtotal / 2) / info->vtotal;
1649
1650 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1651 refresh *= 2;
1652 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1653 refresh /= 2;
1654 if (info->vscan > 1)
1655 refresh /= info->vscan;
1656
1657 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001658 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001659
1660 if (info->type & DRM_MODE_TYPE_PREFERRED)
1661 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1662
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001663 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1664
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001665 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001666}
1667
1668static int
1669drm_subpixel_to_wayland(int drm_value)
1670{
1671 switch (drm_value) {
1672 default:
1673 case DRM_MODE_SUBPIXEL_UNKNOWN:
1674 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1675 case DRM_MODE_SUBPIXEL_NONE:
1676 return WL_OUTPUT_SUBPIXEL_NONE;
1677 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1678 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1679 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1680 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1681 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1682 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1683 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1684 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1685 }
1686}
1687
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001688/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001689static uint32_t
1690drm_get_backlight(struct drm_output *output)
1691{
1692 long brightness, max_brightness, norm;
1693
1694 brightness = backlight_get_brightness(output->backlight);
1695 max_brightness = backlight_get_max_brightness(output->backlight);
1696
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001697 /* convert it on a scale of 0 to 255 */
1698 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001699
1700 return (uint32_t) norm;
1701}
1702
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001703/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001704static void
1705drm_set_backlight(struct weston_output *output_base, uint32_t value)
1706{
1707 struct drm_output *output = (struct drm_output *) output_base;
1708 long max_brightness, new_brightness;
1709
1710 if (!output->backlight)
1711 return;
1712
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001713 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001714 return;
1715
1716 max_brightness = backlight_get_max_brightness(output->backlight);
1717
1718 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001719 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001720
1721 backlight_set_brightness(output->backlight, new_brightness);
1722}
1723
1724static drmModePropertyPtr
1725drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1726{
1727 drmModePropertyPtr props;
1728 int i;
1729
1730 for (i = 0; i < connector->count_props; i++) {
1731 props = drmModeGetProperty(fd, connector->props[i]);
1732 if (!props)
1733 continue;
1734
1735 if (!strcmp(props->name, name))
1736 return props;
1737
1738 drmModeFreeProperty(props);
1739 }
1740
1741 return NULL;
1742}
1743
1744static void
1745drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1746{
1747 struct drm_output *output = (struct drm_output *) output_base;
1748 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001749 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001750 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001751
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001752 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001753 return;
1754
Daniel Stone36609c72015-06-18 07:49:02 +01001755 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1756 output->dpms_prop->prop_id, level);
1757 if (ret) {
1758 weston_log("DRM: DPMS: failed property set for %s\n",
1759 output->base.name);
1760 return;
1761 }
1762
1763 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001764}
1765
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001766static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001767 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1768 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1769 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1770 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1771 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1772 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1773 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1774 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1775 [DRM_MODE_CONNECTOR_Component] = "Component",
1776 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1777 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1778 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1779 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1780 [DRM_MODE_CONNECTOR_TV] = "TV",
1781 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001782#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001783 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1784 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001785#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001786};
1787
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001788static char *
1789make_connector_name(const drmModeConnector *con)
1790{
1791 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001792 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001793
1794 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1795 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001796
1797 if (!type_name)
1798 type_name = "UNNAMED";
1799
1800 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001801
1802 return strdup(name);
1803}
1804
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001805static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001806find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001807 drmModeRes *resources, drmModeConnector *connector)
1808{
1809 drmModeEncoder *encoder;
1810 uint32_t possible_crtcs;
1811 int i, j;
1812
1813 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001814 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001815 if (encoder == NULL) {
1816 weston_log("Failed to get encoder.\n");
1817 return -1;
1818 }
1819 possible_crtcs = encoder->possible_crtcs;
1820 drmModeFreeEncoder(encoder);
1821
1822 for (i = 0; i < resources->count_crtcs; i++) {
1823 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001824 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001825 return i;
1826 }
1827 }
1828
1829 return -1;
1830}
1831
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001832/* Init output state that depends on gl or gbm */
1833static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001834drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001835{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001836 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001837 output->gbm_format,
1838 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001839 };
1840 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001841
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001842 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001843 output->base.current_mode->width,
1844 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001845 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001846 GBM_BO_USE_SCANOUT |
1847 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001848 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001849 weston_log("failed to create gbm surface\n");
1850 return -1;
1851 }
1852
Derek Foremanc4cfe852015-05-15 12:12:40 -05001853 if (format[1])
1854 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001855 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001856 (EGLNativeWindowType)output->gbm_surface,
1857 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001858 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001859 format,
1860 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001861 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001862 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001863 return -1;
1864 }
1865
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001866 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001867
1868 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001869 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001870 continue;
1871
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001872 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001873 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001874 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001875 }
1876
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001877 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001878 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001879 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001880 }
1881
1882 return 0;
1883}
1884
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001885static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001886drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001887{
Hardeningff39efa2013-09-18 23:56:35 +02001888 int w = output->base.current_mode->width;
1889 int h = output->base.current_mode->height;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001890 unsigned int i;
1891
1892 /* FIXME error checking */
1893
1894 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001895 output->dumb[i] = drm_fb_create_dumb(b, w, h);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001896 if (!output->dumb[i])
1897 goto err;
1898
1899 output->image[i] =
Alexander Larsson0b135062013-05-28 16:23:36 +02001900 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001901 output->dumb[i]->map,
1902 output->dumb[i]->stride);
1903 if (!output->image[i])
1904 goto err;
1905 }
1906
1907 if (pixman_renderer_output_create(&output->base) < 0)
1908 goto err;
1909
1910 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001911 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001912
1913 return 0;
1914
1915err:
1916 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1917 if (output->dumb[i])
1918 drm_fb_destroy_dumb(output->dumb[i]);
1919 if (output->image[i])
1920 pixman_image_unref(output->image[i]);
1921
1922 output->dumb[i] = NULL;
1923 output->image[i] = NULL;
1924 }
1925
1926 return -1;
1927}
1928
1929static void
1930drm_output_fini_pixman(struct drm_output *output)
1931{
1932 unsigned int i;
1933
1934 pixman_renderer_output_destroy(&output->base);
1935 pixman_region32_fini(&output->previous_damage);
1936
1937 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1938 drm_fb_destroy_dumb(output->dumb[i]);
1939 pixman_image_unref(output->image[i]);
1940 output->dumb[i] = NULL;
1941 output->image[i] = NULL;
1942 }
1943}
1944
Richard Hughes2b2092a2013-04-24 14:58:02 +01001945static void
1946edid_parse_string(const uint8_t *data, char text[])
1947{
1948 int i;
1949 int replaced = 0;
1950
1951 /* this is always 12 bytes, but we can't guarantee it's null
1952 * terminated or not junk. */
1953 strncpy(text, (const char *) data, 12);
1954
Bryce Harrington9c7de162015-08-28 13:04:26 -07001955 /* guarantee our new string is null-terminated */
1956 text[12] = '\0';
1957
Richard Hughes2b2092a2013-04-24 14:58:02 +01001958 /* remove insane chars */
1959 for (i = 0; text[i] != '\0'; i++) {
1960 if (text[i] == '\n' ||
1961 text[i] == '\r') {
1962 text[i] = '\0';
1963 break;
1964 }
1965 }
1966
1967 /* ensure string is printable */
1968 for (i = 0; text[i] != '\0'; i++) {
1969 if (!isprint(text[i])) {
1970 text[i] = '-';
1971 replaced++;
1972 }
1973 }
1974
1975 /* if the string is random junk, ignore the string */
1976 if (replaced > 4)
1977 text[0] = '\0';
1978}
1979
1980#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1981#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1982#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1983#define EDID_OFFSET_DATA_BLOCKS 0x36
1984#define EDID_OFFSET_LAST_BLOCK 0x6c
1985#define EDID_OFFSET_PNPID 0x08
1986#define EDID_OFFSET_SERIAL 0x0c
1987
1988static int
1989edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1990{
1991 int i;
1992 uint32_t serial_number;
1993
1994 /* check header */
1995 if (length < 128)
1996 return -1;
1997 if (data[0] != 0x00 || data[1] != 0xff)
1998 return -1;
1999
2000 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2001 * /--08--\/--09--\
2002 * 7654321076543210
2003 * |\---/\---/\---/
2004 * R C1 C2 C3 */
2005 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2006 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2007 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2008 edid->pnp_id[3] = '\0';
2009
2010 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2011 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2012 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2013 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2014 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2015 if (serial_number > 0)
2016 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2017
2018 /* parse EDID data */
2019 for (i = EDID_OFFSET_DATA_BLOCKS;
2020 i <= EDID_OFFSET_LAST_BLOCK;
2021 i += 18) {
2022 /* ignore pixel clock data */
2023 if (data[i] != 0)
2024 continue;
2025 if (data[i+2] != 0)
2026 continue;
2027
2028 /* any useful blocks? */
2029 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2030 edid_parse_string(&data[i+5],
2031 edid->monitor_name);
2032 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2033 edid_parse_string(&data[i+5],
2034 edid->serial_number);
2035 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2036 edid_parse_string(&data[i+5],
2037 edid->eisa_id);
2038 }
2039 }
2040 return 0;
2041}
2042
2043static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002044find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002045 struct drm_output *output,
2046 drmModeConnector *connector)
2047{
2048 drmModePropertyBlobPtr edid_blob = NULL;
2049 drmModePropertyPtr property;
2050 int i;
2051 int rc;
2052
2053 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002054 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002055 if (!property)
2056 continue;
2057 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2058 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002059 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002060 connector->prop_values[i]);
2061 }
2062 drmModeFreeProperty(property);
2063 }
2064 if (!edid_blob)
2065 return;
2066
2067 rc = edid_parse(&output->edid,
2068 edid_blob->data,
2069 edid_blob->length);
2070 if (!rc) {
2071 weston_log("EDID data '%s', '%s', '%s'\n",
2072 output->edid.pnp_id,
2073 output->edid.monitor_name,
2074 output->edid.serial_number);
2075 if (output->edid.pnp_id[0] != '\0')
2076 output->base.make = output->edid.pnp_id;
2077 if (output->edid.monitor_name[0] != '\0')
2078 output->base.model = output->edid.monitor_name;
2079 if (output->edid.serial_number[0] != '\0')
2080 output->base.serial_number = output->edid.serial_number;
2081 }
2082 drmModeFreePropertyBlob(edid_blob);
2083}
2084
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002085
2086
2087static int
2088parse_modeline(const char *s, drmModeModeInfo *mode)
2089{
2090 char hsync[16];
2091 char vsync[16];
2092 float fclock;
2093
2094 mode->type = DRM_MODE_TYPE_USERDEF;
2095 mode->hskew = 0;
2096 mode->vscan = 0;
2097 mode->vrefresh = 0;
2098 mode->flags = 0;
2099
Rob Bradford307e09e2013-07-26 16:29:40 +01002100 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002101 &fclock,
2102 &mode->hdisplay,
2103 &mode->hsync_start,
2104 &mode->hsync_end,
2105 &mode->htotal,
2106 &mode->vdisplay,
2107 &mode->vsync_start,
2108 &mode->vsync_end,
2109 &mode->vtotal, hsync, vsync) != 11)
2110 return -1;
2111
2112 mode->clock = fclock * 1000;
2113 if (strcmp(hsync, "+hsync") == 0)
2114 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2115 else if (strcmp(hsync, "-hsync") == 0)
2116 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2117 else
2118 return -1;
2119
2120 if (strcmp(vsync, "+vsync") == 0)
2121 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2122 else if (strcmp(vsync, "-vsync") == 0)
2123 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2124 else
2125 return -1;
2126
2127 return 0;
2128}
2129
Rob Bradford66bd9f52013-06-25 18:56:42 +01002130static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002132 struct weston_output *output,
2133 const char *s)
2134{
2135 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002136 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002137 struct udev_seat *seat;
2138
Giulio Camuffo954f1832014-10-11 18:27:30 +03002139 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002140 if (!seat)
2141 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002142
Derek Foreman0720ea32015-07-15 13:00:35 -05002143 seat->base.output = output;
2144
Derek Foreman1281a362015-07-31 16:55:32 -05002145 pointer = weston_seat_get_pointer(&seat->base);
2146 if (pointer)
2147 weston_pointer_clamp(pointer,
2148 &pointer->x,
2149 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002150 }
2151}
2152
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002153static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002154get_gbm_format_from_section(struct weston_config_section *section,
2155 uint32_t default_value,
2156 uint32_t *format)
2157{
2158 char *s;
2159 int ret = 0;
2160
2161 weston_config_section_get_string(section,
2162 "gbm-format", &s, NULL);
2163
2164 if (s == NULL)
2165 *format = default_value;
2166 else if (strcmp(s, "xrgb8888") == 0)
2167 *format = GBM_FORMAT_XRGB8888;
2168 else if (strcmp(s, "rgb565") == 0)
2169 *format = GBM_FORMAT_RGB565;
2170 else if (strcmp(s, "xrgb2101010") == 0)
2171 *format = GBM_FORMAT_XRGB2101010;
2172 else {
2173 weston_log("fatal: unrecognized pixel format: %s\n", s);
2174 ret = -1;
2175 }
2176
2177 free(s);
2178
2179 return ret;
2180}
2181
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002182/**
2183 * Choose suitable mode for an output
2184 *
2185 * Find the most suitable mode to use for initial setup (or reconfiguration on
2186 * hotplug etc) for a DRM output.
2187 *
2188 * @param output DRM output to choose mode for
2189 * @param kind Strategy and preference to use when choosing mode
2190 * @param width Desired width for this output
2191 * @param height Desired height for this output
2192 * @param current_mode Mode currently being displayed on this output
2193 * @param modeline Manually-entered mode (may be NULL)
2194 * @returns A mode from the output's mode list, or NULL if none available
2195 */
2196static struct drm_mode *
2197drm_output_choose_initial_mode(struct drm_output *output,
2198 enum output_config kind,
2199 int width, int height,
2200 const drmModeModeInfo *current_mode,
2201 const drmModeModeInfo *modeline)
2202{
2203 struct drm_mode *preferred = NULL;
2204 struct drm_mode *current = NULL;
2205 struct drm_mode *configured = NULL;
2206 struct drm_mode *best = NULL;
2207 struct drm_mode *drm_mode;
2208
2209 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
2210 if (kind == OUTPUT_CONFIG_MODE &&
2211 width == drm_mode->base.width &&
2212 height == drm_mode->base.height)
2213 configured = drm_mode;
2214
comic fans7a5c5622016-03-17 14:29:27 +02002215 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002216 sizeof *current_mode) == 0)
2217 current = drm_mode;
2218
2219 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2220 preferred = drm_mode;
2221
2222 best = drm_mode;
2223 }
2224
2225 if (kind == OUTPUT_CONFIG_MODELINE) {
2226 configured = drm_output_add_mode(output, modeline);
2227 if (!configured)
2228 return NULL;
2229 }
2230
2231 if (current == NULL && current_mode->clock != 0) {
2232 current = drm_output_add_mode(output, current_mode);
2233 if (!current)
2234 return NULL;
2235 }
2236
2237 if (kind == OUTPUT_CONFIG_CURRENT)
2238 configured = current;
2239
2240 if (option_current_mode && current)
2241 return current;
2242
2243 if (configured)
2244 return configured;
2245
2246 if (preferred)
2247 return preferred;
2248
2249 if (current)
2250 return current;
2251
2252 if (best)
2253 return best;
2254
2255 weston_log("no available modes for %s\n", output->base.name);
2256 return NULL;
2257}
2258
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002259static int
2260connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2261 drmModeModeInfo *mode)
2262{
2263 drmModeEncoder *encoder;
2264 drmModeCrtc *crtc;
2265
2266 /* Get the current mode on the crtc that's currently driving
2267 * this connector. */
2268 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2269 memset(mode, 0, sizeof *mode);
2270 if (encoder != NULL) {
2271 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2272 drmModeFreeEncoder(encoder);
2273 if (crtc == NULL)
2274 return -1;
2275 if (crtc->mode_valid)
2276 *mode = crtc->mode;
2277 drmModeFreeCrtc(crtc);
2278 }
2279
2280 return 0;
2281}
2282
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002283/**
2284 * Create and configure a Weston output structure
2285 *
2286 * Given a DRM connector, create a matching drm_output structure and add it
2287 * to Weston's output list.
2288 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002289 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002290 * @param resources DRM resources for this device
2291 * @param connector DRM connector to use for this new output
2292 * @param x Horizontal offset to use into global co-ordinate space
2293 * @param y Vertical offset to use into global co-ordinate space
2294 * @param drm_device udev device pointer
2295 * @returns 0 on success, or -1 on failure
2296 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002297static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002298create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002299 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002300 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002301 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002302{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002303 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002304 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002305 struct weston_mode *m;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002306 struct weston_config_section *section;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002307 drmModeModeInfo crtc_mode, modeline;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002308 int i, width, height, scale;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002309 char *s;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002310 enum output_config config;
2311 uint32_t transform;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002312
Giulio Camuffo954f1832014-10-11 18:27:30 +03002313 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002314 if (i < 0) {
2315 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002316 return -1;
2317 }
2318
Peter Huttererf3d62272013-08-08 11:57:05 +10002319 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002320 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002321 return -1;
2322
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002323 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002324 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002325 output->base.make = "unknown";
2326 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002327 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002328 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002329
Giulio Camuffo954f1832014-10-11 18:27:30 +03002330 section = weston_config_get_section(b->compositor->config, "output", "name",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002331 output->base.name);
2332 weston_config_section_get_string(section, "mode", &s, "preferred");
2333 if (strcmp(s, "off") == 0)
2334 config = OUTPUT_CONFIG_OFF;
2335 else if (strcmp(s, "preferred") == 0)
2336 config = OUTPUT_CONFIG_PREFERRED;
2337 else if (strcmp(s, "current") == 0)
2338 config = OUTPUT_CONFIG_CURRENT;
2339 else if (sscanf(s, "%dx%d", &width, &height) == 2)
2340 config = OUTPUT_CONFIG_MODE;
2341 else if (parse_modeline(s, &modeline) == 0)
2342 config = OUTPUT_CONFIG_MODELINE;
2343 else {
2344 weston_log("Invalid mode \"%s\" for output %s\n",
2345 s, output->base.name);
2346 config = OUTPUT_CONFIG_PREFERRED;
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002347 }
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002348 free(s);
2349
2350 weston_config_section_get_int(section, "scale", &scale, 1);
2351 weston_config_section_get_string(section, "transform", &s, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05002352 if (weston_parse_transform(s, &transform) < 0)
2353 weston_log("Invalid transform \"%s\" for output %s\n",
2354 s, output->base.name);
2355
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002356 free(s);
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002357
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002358 if (get_gbm_format_from_section(section,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002359 b->gbm_format,
2360 &output->gbm_format) == -1)
2361 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002362
Rob Bradford66bd9f52013-06-25 18:56:42 +01002363 weston_config_section_get_string(section, "seat", &s, "");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002364 setup_output_seat_constraint(b, &output->base, s);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002365 free(s);
2366
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002367 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002368 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002369 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002370 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002371 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002372
Giulio Camuffo954f1832014-10-11 18:27:30 +03002373 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2374 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002375
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002376 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2377 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002378
David Herrmann0f0d54e2011-12-08 17:05:45 +01002379 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002380 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002381 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002382 goto err_free;
2383 }
2384
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002385 if (config == OUTPUT_CONFIG_OFF) {
2386 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002387 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002388 0, 0, 0, 0, 0, NULL);
2389 goto err_free;
2390 }
2391
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002392 current = drm_output_choose_initial_mode(output, config,
2393 width, height,
2394 &crtc_mode, &modeline);
2395 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002396 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002397 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002398 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002399
Giulio Camuffo954f1832014-10-11 18:27:30 +03002400 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002401 connector->mmWidth, connector->mmHeight,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002402 transform, scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002403
Giulio Camuffo954f1832014-10-11 18:27:30 +03002404 if (b->use_pixman) {
2405 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002406 weston_log("Failed to init output pixman state\n");
2407 goto err_output;
2408 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002409 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002410 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002411 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002412 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002413
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002414 output->backlight = backlight_init(drm_device,
2415 connector->connector_type);
2416 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002417 weston_log("Initialized backlight, device %s\n",
2418 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002419 output->base.set_backlight = drm_set_backlight;
2420 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002421 } else {
2422 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002423 }
2424
Giulio Camuffo954f1832014-10-11 18:27:30 +03002425 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002426
Giulio Camuffo954f1832014-10-11 18:27:30 +03002427 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002428 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2429 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002430
Jonas Ådahle5a12252013-04-05 23:07:11 +02002431 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002432 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002433 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002434 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002435 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002436 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002437
Richard Hughese7299962013-05-01 21:52:12 +01002438 output->base.gamma_size = output->original_crtc->gamma_size;
2439 output->base.set_gamma = drm_output_set_gamma;
2440
Derek Foremanb0427562016-02-05 15:55:20 -06002441 weston_plane_init(&output->cursor_plane, b->compositor,
2442 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002443 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002444
Giulio Camuffo954f1832014-10-11 18:27:30 +03002445 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2446 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2447 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002448
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002449 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002450 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002451 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002452 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002453 m->width, m->height, m->refresh / 1000.0,
2454 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2455 ", preferred" : "",
2456 m->flags & WL_OUTPUT_MODE_CURRENT ?
2457 ", current" : "",
2458 connector->count_modes == 0 ?
2459 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002460
Mario Kleiner80817042015-06-21 21:25:11 +02002461 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2462 output->base.native_mode = output->base.current_mode;
2463 output->base.native_scale = output->base.current_scale;
2464
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002465 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002466
John Kåre Alsaker94659272012-11-13 19:10:18 +01002467err_output:
2468 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002469err_free:
2470 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2471 base.link) {
2472 wl_list_remove(&drm_mode->base.link);
2473 free(drm_mode);
2474 }
2475
2476 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002477 b->crtc_allocator &= ~(1 << output->crtc_id);
2478 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002479 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002480
David Herrmann0f0d54e2011-12-08 17:05:45 +01002481 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002482}
2483
Jesse Barnes58ef3792012-02-23 09:45:49 -05002484static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002485create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002486{
2487 struct drm_sprite *sprite;
2488 drmModePlaneRes *plane_res;
2489 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002490 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002491
Giulio Camuffo954f1832014-10-11 18:27:30 +03002492 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002493 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002494 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002495 strerror(errno));
2496 return;
2497 }
2498
2499 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002500 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002501 if (!plane)
2502 continue;
2503
Peter Huttererf3d62272013-08-08 11:57:05 +10002504 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002505 plane->count_formats));
2506 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002507 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002508 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002509 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002510 continue;
2511 }
2512
Jesse Barnes58ef3792012-02-23 09:45:49 -05002513 sprite->possible_crtcs = plane->possible_crtcs;
2514 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002515 sprite->current = NULL;
2516 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002518 sprite->count_formats = plane->count_formats;
2519 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002520 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002521 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002522 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2523 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2524 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002525
Giulio Camuffo954f1832014-10-11 18:27:30 +03002526 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002527 }
2528
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002529 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002530}
2531
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002532static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002533destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002534{
2535 struct drm_sprite *sprite, *next;
2536 struct drm_output *output;
2537
Giulio Camuffo954f1832014-10-11 18:27:30 +03002538 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002539 struct drm_output, base.link);
2540
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2542 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002543 sprite->plane_id,
2544 output->crtc_id, 0, 0,
2545 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002546 drm_output_release_fb(output, sprite->current);
2547 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002548 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002549 free(sprite);
2550 }
2551}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002552
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002553static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002554create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002555 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002556{
2557 drmModeConnector *connector;
2558 drmModeRes *resources;
2559 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002560 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002561
Giulio Camuffo954f1832014-10-11 18:27:30 +03002562 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002563 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002564 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002565 return -1;
2566 }
2567
Giulio Camuffo954f1832014-10-11 18:27:30 +03002568 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2569 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002570 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002571 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002572 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002573
Giulio Camuffo954f1832014-10-11 18:27:30 +03002574 b->min_width = resources->min_width;
2575 b->max_width = resources->max_width;
2576 b->min_height = resources->min_height;
2577 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002578
Giulio Camuffo954f1832014-10-11 18:27:30 +03002579 b->num_crtcs = resources->count_crtcs;
2580 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002581
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002582 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002583 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002584 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002585 if (connector == NULL)
2586 continue;
2587
2588 if (connector->connection == DRM_MODE_CONNECTED &&
2589 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002590 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002591 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002592 connector, x, y,
2593 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002594 drmModeFreeConnector(connector);
2595 continue;
2596 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002597
Giulio Camuffo954f1832014-10-11 18:27:30 +03002598 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002599 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002600 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002601 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002602
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002603 drmModeFreeConnector(connector);
2604 }
2605
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002607 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002608 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002609 return -1;
2610 }
2611
2612 drmModeFreeResources(resources);
2613
2614 return 0;
2615}
2616
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002617static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002619{
2620 drmModeConnector *connector;
2621 drmModeRes *resources;
2622 struct drm_output *output, *next;
2623 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002624 uint32_t connected = 0, disconnects = 0;
2625 int i;
2626
Giulio Camuffo954f1832014-10-11 18:27:30 +03002627 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002628 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002629 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002630 return;
2631 }
2632
2633 /* collect new connects */
2634 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002635 int connector_id = resources->connectors[i];
2636
Giulio Camuffo954f1832014-10-11 18:27:30 +03002637 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002638 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639 continue;
2640
David Herrmann7551cff2011-12-08 17:05:43 +01002641 if (connector->connection != DRM_MODE_CONNECTED) {
2642 drmModeFreeConnector(connector);
2643 continue;
2644 }
2645
Benjamin Franzke117483d2011-08-30 11:38:26 +02002646 connected |= (1 << connector_id);
2647
Giulio Camuffo954f1832014-10-11 18:27:30 +03002648 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002649 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002650 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002651 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002652
2653 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002654 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002655 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002656 else
2657 x = 0;
2658 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002659 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002660 connector, x, y,
2661 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002662 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002663
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002664 }
2665 drmModeFreeConnector(connector);
2666 }
2667 drmModeFreeResources(resources);
2668
Giulio Camuffo954f1832014-10-11 18:27:30 +03002669 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002670 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002671 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002672 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002673 if (disconnects & (1 << output->connector_id)) {
2674 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002675 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002676 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002677 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002678 }
2679 }
2680 }
2681
Daniel Stonef556ebe2015-05-21 08:28:58 +01002682 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002683 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002684 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002685}
2686
2687static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002688udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002689{
David Herrmannd7488c22012-03-11 20:05:21 +01002690 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002691 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002692
2693 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002694 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002695 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002696
David Herrmann6ac52db2012-03-11 20:05:22 +01002697 val = udev_device_get_property_value(device, "HOTPLUG");
2698 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002699 return 0;
2700
David Herrmann6ac52db2012-03-11 20:05:22 +01002701 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002702}
2703
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002704static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002705udev_drm_event(int fd, uint32_t mask, void *data)
2706{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002707 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002708 struct udev_device *event;
2709
Giulio Camuffo954f1832014-10-11 18:27:30 +03002710 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002711
Giulio Camuffo954f1832014-10-11 18:27:30 +03002712 if (udev_event_is_hotplug(b, event))
2713 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002714
2715 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002716
2717 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002718}
2719
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002720static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002721drm_restore(struct weston_compositor *ec)
2722{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002723 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002724}
2725
2726static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002727drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002728{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002729 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002730
Giulio Camuffo954f1832014-10-11 18:27:30 +03002731 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002732
Giulio Camuffo954f1832014-10-11 18:27:30 +03002733 wl_event_source_remove(b->udev_drm_source);
2734 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002735
Giulio Camuffo954f1832014-10-11 18:27:30 +03002736 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002737
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002738 weston_compositor_shutdown(ec);
2739
Giulio Camuffo954f1832014-10-11 18:27:30 +03002740 if (b->gbm)
2741 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002742
Giulio Camuffo954f1832014-10-11 18:27:30 +03002743 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002744
Giulio Camuffo954f1832014-10-11 18:27:30 +03002745 close(b->drm.fd);
Rob Bradford45c15b82013-07-26 16:29:35 +01002746
Giulio Camuffo954f1832014-10-11 18:27:30 +03002747 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002748}
2749
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002750static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002751drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002752{
2753 struct drm_output *output;
2754 struct drm_mode *drm_mode;
2755 int ret;
2756
Giulio Camuffo954f1832014-10-11 18:27:30 +03002757 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002758 if (!output->current) {
2759 /* If something that would cause the output to
2760 * switch mode happened while in another vt, we
2761 * might not have a current drm_fb. In that case,
2762 * schedule a repaint and let drm_output_repaint
2763 * handle setting the mode. */
2764 weston_output_schedule_repaint(&output->base);
2765 continue;
2766 }
2767
Hardeningff39efa2013-09-18 23:56:35 +02002768 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002769 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002770 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002771 &output->connector_id, 1,
2772 &drm_mode->mode_info);
2773 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002774 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002775 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002776 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002777 output->base.x, output->base.y);
2778 }
2779 }
2780}
2781
2782static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002783session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002784{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002785 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002786 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002787 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002788 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002789
Giulio Camuffo954f1832014-10-11 18:27:30 +03002790 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002791 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002792 compositor->state = b->prev_state;
2793 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002794 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002795 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002796 } else {
2797 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002798 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002799
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002801 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002802
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002803 /* If we have a repaint scheduled (either from a
2804 * pending pageflip or the idle handler), make sure we
2805 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002806 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002807 * further attemps at repainting. When we switch
2808 * back, we schedule a repaint, which will process
2809 * pending frame callbacks. */
2810
Giulio Camuffo954f1832014-10-11 18:27:30 +03002811 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002812 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002813 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002814 }
2815
Giulio Camuffo954f1832014-10-11 18:27:30 +03002816 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002817 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002818
Giulio Camuffo954f1832014-10-11 18:27:30 +03002819 wl_list_for_each(sprite, &b->sprite_list, link)
2820 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002821 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002822 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002823 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002824 };
2825}
2826
David Herrmann0af066f2012-10-29 19:21:16 +01002827/*
2828 * Find primary GPU
2829 * Some systems may have multiple DRM devices attached to a single seat. This
2830 * function loops over all devices and tries to find a PCI device with the
2831 * boot_vga sysfs attribute set to 1.
2832 * If no such device is found, the first DRM device reported by udev is used.
2833 */
2834static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002835find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002836{
2837 struct udev_enumerate *e;
2838 struct udev_list_entry *entry;
2839 const char *path, *device_seat, *id;
2840 struct udev_device *device, *drm_device, *pci;
2841
Giulio Camuffo954f1832014-10-11 18:27:30 +03002842 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002843 udev_enumerate_add_match_subsystem(e, "drm");
2844 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2845
2846 udev_enumerate_scan_devices(e);
2847 drm_device = NULL;
2848 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2849 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002850 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002851 if (!device)
2852 continue;
2853 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2854 if (!device_seat)
2855 device_seat = default_seat;
2856 if (strcmp(device_seat, seat)) {
2857 udev_device_unref(device);
2858 continue;
2859 }
2860
2861 pci = udev_device_get_parent_with_subsystem_devtype(device,
2862 "pci", NULL);
2863 if (pci) {
2864 id = udev_device_get_sysattr_value(pci, "boot_vga");
2865 if (id && !strcmp(id, "1")) {
2866 if (drm_device)
2867 udev_device_unref(drm_device);
2868 drm_device = device;
2869 break;
2870 }
2871 }
2872
2873 if (!drm_device)
2874 drm_device = device;
2875 else
2876 udev_device_unref(device);
2877 }
2878
2879 udev_enumerate_unref(e);
2880 return drm_device;
2881}
2882
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002883static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002884planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2885 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002886{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002887 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002888
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002889 switch (key) {
2890 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002891 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002892 break;
2893 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002894 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002895 break;
2896 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002897 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002898 break;
2899 default:
2900 break;
2901 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002902}
2903
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002904#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002905static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002906recorder_destroy(struct drm_output *output)
2907{
2908 vaapi_recorder_destroy(output->recorder);
2909 output->recorder = NULL;
2910
2911 output->base.disable_planes--;
2912
2913 wl_list_remove(&output->recorder_frame_listener.link);
2914 weston_log("[libva recorder] done\n");
2915}
2916
2917static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002918recorder_frame_notify(struct wl_listener *listener, void *data)
2919{
2920 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002921 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002922 int fd, ret;
2923
2924 output = container_of(listener, struct drm_output,
2925 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002926 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002927
2928 if (!output->recorder)
2929 return;
2930
Giulio Camuffo954f1832014-10-11 18:27:30 +03002931 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002932 DRM_CLOEXEC, &fd);
2933 if (ret) {
2934 weston_log("[libva recorder] "
2935 "failed to create prime fd for front buffer\n");
2936 return;
2937 }
2938
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002939 ret = vaapi_recorder_frame(output->recorder, fd,
2940 output->current->stride);
2941 if (ret < 0) {
2942 weston_log("[libva recorder] aborted: %m\n");
2943 recorder_destroy(output);
2944 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002945}
2946
2947static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002948create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002949 const char *filename)
2950{
2951 int fd;
2952 drm_magic_t magic;
2953
Giulio Camuffo954f1832014-10-11 18:27:30 +03002954 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002955 if (fd < 0)
2956 return NULL;
2957
2958 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002959 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002960
2961 return vaapi_recorder_create(fd, width, height, filename);
2962}
2963
2964static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002965recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002966 void *data)
2967{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002968 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002969 struct drm_output *output;
2970 int width, height;
2971
Giulio Camuffo954f1832014-10-11 18:27:30 +03002972 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002973 struct drm_output, base.link);
2974
2975 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002976 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002977 weston_log("failed to start vaapi recorder: "
2978 "output format not supported\n");
2979 return;
2980 }
2981
Hardeningff39efa2013-09-18 23:56:35 +02002982 width = output->base.current_mode->width;
2983 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002984
2985 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002986 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002987 if (!output->recorder) {
2988 weston_log("failed to create vaapi recorder\n");
2989 return;
2990 }
2991
2992 output->base.disable_planes++;
2993
2994 output->recorder_frame_listener.notify = recorder_frame_notify;
2995 wl_signal_add(&output->base.frame_signal,
2996 &output->recorder_frame_listener);
2997
2998 weston_output_schedule_repaint(&output->base);
2999
3000 weston_log("[libva recorder] initialized\n");
3001 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003002 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003003 }
3004}
3005#else
3006static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003007recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008 void *data)
3009{
3010 weston_log("Compiled without libva support\n");
3011}
3012#endif
3013
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003014static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003015switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003016{
3017 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003018 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003019
Giulio Camuffo954f1832014-10-11 18:27:30 +03003020 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003021 return;
3022
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003023 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3024
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003025 weston_log("Switching to GL renderer\n");
3026
Giulio Camuffo954f1832014-10-11 18:27:30 +03003027 b->gbm = create_gbm_device(b->drm.fd);
3028 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003029 weston_log("Failed to create gbm device. "
3030 "Aborting renderer switch\n");
3031 return;
3032 }
3033
Giulio Camuffo954f1832014-10-11 18:27:30 +03003034 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003035 pixman_renderer_output_destroy(&output->base);
3036
Giulio Camuffo954f1832014-10-11 18:27:30 +03003037 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003038
Giulio Camuffo954f1832014-10-11 18:27:30 +03003039 if (drm_backend_create_gl_renderer(b) < 0) {
3040 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003041 weston_log("Failed to create GL renderer. Quitting.\n");
3042 /* FIXME: we need a function to shutdown cleanly */
3043 assert(0);
3044 }
3045
Giulio Camuffo954f1832014-10-11 18:27:30 +03003046 wl_list_for_each(output, &b->compositor->output_list, base.link)
3047 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003048
Giulio Camuffo954f1832014-10-11 18:27:30 +03003049 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003050
3051 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3052 if (linux_dmabuf_setup(b->compositor) < 0)
3053 weston_log("Error: initializing dmabuf "
3054 "support failed.\n");
3055 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003056}
3057
3058static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003059renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3060 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003061{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003062 struct drm_backend *b =
3063 (struct drm_backend *) keyboard->seat->compositor;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003064
Giulio Camuffo954f1832014-10-11 18:27:30 +03003065 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003066}
3067
Giulio Camuffo954f1832014-10-11 18:27:30 +03003068static struct drm_backend *
3069drm_backend_create(struct weston_compositor *compositor,
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003070 struct drm_parameters *param,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003071 int *argc, char *argv[],
3072 struct weston_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003073{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003074 struct drm_backend *b;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003075 struct weston_config_section *section;
David Herrmann0af066f2012-10-29 19:21:16 +01003076 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003077 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003078 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003079
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003080 weston_log("initializing drm backend\n");
3081
Giulio Camuffo954f1832014-10-11 18:27:30 +03003082 b = zalloc(sizeof *b);
3083 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003084 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003085
Pekka Paalanen68583832015-05-19 09:53:16 +03003086 /*
3087 * KMS support for hardware planes cannot properly synchronize
3088 * without nuclear page flip. Without nuclear/atomic, hw plane
3089 * and cursor plane updates would either tear or cause extra
3090 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003091 * to a fraction. For cursors, it's not so bad, so they are
3092 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003093 *
3094 * These can be enabled again when nuclear/atomic support lands.
3095 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003096 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003097 b->compositor = compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003098
3099 section = weston_config_get_section(config, "core", NULL, NULL);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003100 if (get_gbm_format_from_section(section,
3101 GBM_FORMAT_XRGB8888,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003102 &b->gbm_format) == -1)
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003103 goto err_base;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003104
Giulio Camuffo954f1832014-10-11 18:27:30 +03003105 b->use_pixman = param->use_pixman;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003106
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003107 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003108 compositor->launcher = weston_launcher_connect(compositor, param->tty,
3109 param->seat_id, true);
3110 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003111 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003112 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003113 goto err_compositor;
3114 }
3115
Giulio Camuffo954f1832014-10-11 18:27:30 +03003116 b->udev = udev_new();
3117 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003118 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003119 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003120 }
3121
Giulio Camuffo954f1832014-10-11 18:27:30 +03003122 b->session_listener.notify = session_notify;
3123 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003124
Giulio Camuffo954f1832014-10-11 18:27:30 +03003125 drm_device = find_primary_gpu(b, param->seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003126 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003127 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003128 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003129 }
David Herrmann0af066f2012-10-29 19:21:16 +01003130 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003131
Giulio Camuffo954f1832014-10-11 18:27:30 +03003132 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003133 weston_log("failed to initialize kms\n");
3134 goto err_udev_dev;
3135 }
3136
Giulio Camuffo954f1832014-10-11 18:27:30 +03003137 if (b->use_pixman) {
3138 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003139 weston_log("failed to initialize pixman renderer\n");
3140 goto err_udev_dev;
3141 }
3142 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003143 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003144 weston_log("failed to initialize egl\n");
3145 goto err_udev_dev;
3146 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003147 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003148
Giulio Camuffo954f1832014-10-11 18:27:30 +03003149 b->base.destroy = drm_destroy;
3150 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003151
Giulio Camuffo954f1832014-10-11 18:27:30 +03003152 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003153
Bob Ham91880f12016-01-12 10:21:47 +00003154 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003155
Giulio Camuffo954f1832014-10-11 18:27:30 +03003156 wl_list_init(&b->sprite_list);
3157 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003158
Giulio Camuffo954f1832014-10-11 18:27:30 +03003159 if (udev_input_init(&b->input,
3160 compositor, b->udev, param->seat_id) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003161 weston_log("failed to create input devices\n");
3162 goto err_sprite;
3163 }
3164
Giulio Camuffo954f1832014-10-11 18:27:30 +03003165 if (create_outputs(b, param->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003166 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003167 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003168 }
3169
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003170 /* A this point we have some idea of whether or not we have a working
3171 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003172 if (!b->cursors_are_broken)
3173 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003174
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003175 path = NULL;
3176
Giulio Camuffo954f1832014-10-11 18:27:30 +03003177 loop = wl_display_get_event_loop(compositor->wl_display);
3178 b->drm_source =
3179 wl_event_loop_add_fd(loop, b->drm.fd,
3180 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003181
Giulio Camuffo954f1832014-10-11 18:27:30 +03003182 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3183 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003184 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003185 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003186 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003187 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003188 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003189 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003190 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003191 udev_monitor_get_fd(b->udev_monitor),
3192 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003193
Giulio Camuffo954f1832014-10-11 18:27:30 +03003194 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003195 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003196 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003197 }
3198
Daniel Stonea96b93c2012-06-22 14:04:37 +01003199 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003200
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 weston_compositor_add_debug_binding(compositor, KEY_O,
3202 planes_binding, b);
3203 weston_compositor_add_debug_binding(compositor, KEY_C,
3204 planes_binding, b);
3205 weston_compositor_add_debug_binding(compositor, KEY_V,
3206 planes_binding, b);
3207 weston_compositor_add_debug_binding(compositor, KEY_Q,
3208 recorder_binding, b);
3209 weston_compositor_add_debug_binding(compositor, KEY_W,
3210 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003211
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003212 if (compositor->renderer->import_dmabuf) {
3213 if (linux_dmabuf_setup(compositor) < 0)
3214 weston_log("Error: initializing dmabuf "
3215 "support failed.\n");
3216 }
3217
Giulio Camuffo954f1832014-10-11 18:27:30 +03003218 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003219
Giulio Camuffo954f1832014-10-11 18:27:30 +03003220 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003221
3222err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003223 wl_event_source_remove(b->udev_drm_source);
3224 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003225err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003226 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003227err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003228 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003229err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003230 gbm_device_destroy(b->gbm);
3231 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003232err_udev_dev:
3233 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003234err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003235 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003236err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003237 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003238err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003239 weston_compositor_shutdown(compositor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003240err_base:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003241 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003242 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003243}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003244
Giulio Camuffo954f1832014-10-11 18:27:30 +03003245WL_EXPORT int
3246backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003247 struct weston_config *config,
3248 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003249{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003250 struct drm_backend *b;
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003251 struct drm_parameters param = { 0, };
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003252
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003253 const struct weston_option drm_options[] = {
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003254 { WESTON_OPTION_INTEGER, "connector", 0, &param.connector },
3255 { WESTON_OPTION_STRING, "seat", 0, &param.seat_id },
3256 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003257 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003258 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003259 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02003260
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003261 param.seat_id = default_seat;
3262
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003263 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003264
Giulio Camuffo954f1832014-10-11 18:27:30 +03003265 b = drm_backend_create(compositor, &param, argc, argv, config);
3266 if (b == NULL)
3267 return -1;
3268 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003269}