blob: 28b1c7e07e267a2b89d38d77dda4bf902e4c1f97 [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 Paalanen363aa7b2014-12-17 16:20:40 +020058#include "presentation_timing-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040059
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030060#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
61#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
62#endif
63
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030064#ifndef DRM_CAP_CURSOR_WIDTH
65#define DRM_CAP_CURSOR_WIDTH 0x8
66#endif
67
68#ifndef DRM_CAP_CURSOR_HEIGHT
69#define DRM_CAP_CURSOR_HEIGHT 0x9
70#endif
71
72#ifndef GBM_BO_USE_CURSOR
73#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
74#endif
75
Kristian Høgsberg061c4252012-06-28 11:28:15 -040076static int option_current_mode = 0;
Scott Moreau8ab5d452012-07-30 19:51:08 -060077
78enum output_config {
79 OUTPUT_CONFIG_INVALID = 0,
80 OUTPUT_CONFIG_OFF,
81 OUTPUT_CONFIG_PREFERRED,
82 OUTPUT_CONFIG_CURRENT,
Scott Moreau8f37e0b2012-07-31 15:30:41 -060083 OUTPUT_CONFIG_MODE,
84 OUTPUT_CONFIG_MODELINE
Scott Moreau8ab5d452012-07-30 19:51:08 -060085};
86
Giulio Camuffo954f1832014-10-11 18:27:30 +030087struct drm_backend {
88 struct weston_backend base;
89 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040090
91 struct udev *udev;
92 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040093
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010094 struct udev_monitor *udev_monitor;
95 struct wl_event_source *udev_drm_source;
96
Benjamin Franzke2af7f102011-03-02 11:14:59 +010097 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010098 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010099 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300100 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100101 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200102 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500103 uint32_t *crtcs;
104 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -0500105 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100106 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700107 struct wl_listener session_listener;
Kristian Høgsbergc3ea26c2013-09-25 15:46:42 -0700108 uint32_t format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200109
Rob Clark4339add2012-08-09 14:18:28 -0500110 /* we need these parameters in order to not fail drmModeAddFB2()
111 * due to out of bounds dimensions, and then mistakenly set
112 * sprites_are_broken:
113 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200114 uint32_t min_width, max_width;
115 uint32_t min_height, max_height;
116 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500117
Jesse Barnes58ef3792012-02-23 09:45:49 -0500118 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500119 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200120 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500121
Rob Clarkab5b1e32012-08-09 13:24:45 -0500122 int cursors_are_broken;
123
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200124 int use_pixman;
125
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200126 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300127
Rob Bradfordd355b802013-05-31 18:09:55 +0100128 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300129
130 uint32_t cursor_width;
131 uint32_t cursor_height;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400132};
133
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400134struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500135 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400136 drmModeModeInfo mode_info;
137};
138
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300139struct drm_output;
140
141struct drm_fb {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300142 struct drm_output *output;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200143 uint32_t fb_id, stride, handle, size;
144 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300145 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200146 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200147
148 /* Used by gbm fbs */
149 struct gbm_bo *bo;
150
151 /* Used by dumb fbs */
152 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300153};
154
Richard Hughes2b2092a2013-04-24 14:58:02 +0100155struct drm_edid {
156 char eisa_id[13];
157 char monitor_name[13];
158 char pnp_id[5];
159 char serial_number[13];
160};
161
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400162struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500163 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400164
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500166 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400167 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700168 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100169 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300170 drmModePropertyPtr dpms_prop;
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000171 uint32_t format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200172
Daniel Stone36609c72015-06-18 07:49:02 +0100173 enum dpms_enum dpms;
174
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300175 int vblank_pending;
176 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800177 int destroy_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300178
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400179 struct gbm_surface *surface;
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -0400180 struct gbm_bo *cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400181 struct weston_plane cursor_plane;
182 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500183 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400184 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300185 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200186 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200187
188 struct drm_fb *dumb[2];
189 pixman_image_t *image[2];
190 int current_image;
191 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300192
193 struct vaapi_recorder *recorder;
194 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400195};
196
Jesse Barnes58ef3792012-02-23 09:45:49 -0500197/*
198 * An output has a primary display plane plus zero or more sprites for
199 * blending display contents.
200 */
201struct drm_sprite {
202 struct wl_list link;
203
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400204 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500205
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200206 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300207 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300208 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500209
Jesse Barnes58ef3792012-02-23 09:45:49 -0500210 uint32_t possible_crtcs;
211 uint32_t plane_id;
212 uint32_t count_formats;
213
214 int32_t src_x, src_y;
215 uint32_t src_w, src_h;
216 uint32_t dest_x, dest_y;
217 uint32_t dest_w, dest_h;
218
219 uint32_t formats[];
220};
221
Kristian Høgsbergd8e98332013-10-16 16:15:11 -0700222struct drm_parameters {
223 int connector;
224 int tty;
225 int use_pixman;
226 const char *seat_id;
227};
228
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300229static struct gl_renderer_interface *gl_renderer;
230
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500231static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400232
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400233static void
234drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400235
Mario Kleinerf507ec32015-06-21 21:25:14 +0200236static void
237drm_output_update_msc(struct drm_output *output, unsigned int seq);
238
Jesse Barnes58ef3792012-02-23 09:45:49 -0500239static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200240drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500241{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200242 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300243 struct drm_backend *b =(struct drm_backend *)ec->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500244 int crtc;
245
Giulio Camuffo954f1832014-10-11 18:27:30 +0300246 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
247 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500248 continue;
249
250 if (supported & (1 << crtc))
251 return -1;
252 }
253
254 return 0;
255}
256
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300257static void
258drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
259{
260 struct drm_fb *fb = data;
261 struct gbm_device *gbm = gbm_bo_get_device(bo);
262
263 if (fb->fb_id)
264 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
265
Pekka Paalanende685b82012-12-04 15:58:12 +0200266 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300267
268 free(data);
269}
270
271static struct drm_fb *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300272drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200273{
274 struct drm_fb *fb;
275 int ret;
276
277 struct drm_mode_create_dumb create_arg;
278 struct drm_mode_destroy_dumb destroy_arg;
279 struct drm_mode_map_dumb map_arg;
280
Peter Huttererf3d62272013-08-08 11:57:05 +1000281 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200282 if (!fb)
283 return NULL;
284
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700285 memset(&create_arg, 0, sizeof create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200286 create_arg.bpp = 32;
287 create_arg.width = width;
288 create_arg.height = height;
289
Giulio Camuffo954f1832014-10-11 18:27:30 +0300290 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200291 if (ret)
292 goto err_fb;
293
294 fb->handle = create_arg.handle;
295 fb->stride = create_arg.pitch;
296 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300297 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200298
Giulio Camuffo954f1832014-10-11 18:27:30 +0300299 ret = drmModeAddFB(b->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200300 fb->stride, fb->handle, &fb->fb_id);
301 if (ret)
302 goto err_bo;
303
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700304 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200305 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400306 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200307 if (ret)
308 goto err_add_fb;
309
310 fb->map = mmap(0, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300311 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200312 if (fb->map == MAP_FAILED)
313 goto err_add_fb;
314
315 return fb;
316
317err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300318 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200319err_bo:
320 memset(&destroy_arg, 0, sizeof(destroy_arg));
321 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300322 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200323err_fb:
324 free(fb);
325 return NULL;
326}
327
328static void
329drm_fb_destroy_dumb(struct drm_fb *fb)
330{
331 struct drm_mode_destroy_dumb destroy_arg;
332
333 if (!fb->map)
334 return;
335
336 if (fb->fb_id)
337 drmModeRmFB(fb->fd, fb->fb_id);
338
339 weston_buffer_reference(&fb->buffer_ref, NULL);
340
341 munmap(fb->map, fb->size);
342
343 memset(&destroy_arg, 0, sizeof(destroy_arg));
344 destroy_arg.handle = fb->handle;
345 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
346
347 free(fb);
348}
349
350static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500351drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300352 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300353{
354 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200355 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200356 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300357 int ret;
358
359 if (fb)
360 return fb;
361
Bryce Harringtonde16d892014-11-20 22:21:57 -0800362 fb = zalloc(sizeof *fb);
363 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200364 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300365
366 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300367
368 width = gbm_bo_get_width(bo);
369 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200370 fb->stride = gbm_bo_get_stride(bo);
371 fb->handle = gbm_bo_get_handle(bo).u32;
372 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300373 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300374
Giulio Camuffo954f1832014-10-11 18:27:30 +0300375 if (backend->min_width > width || width > backend->max_width ||
376 backend->min_height > height ||
377 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200378 weston_log("bo geometry out of bounds\n");
379 goto err_free;
380 }
381
382 ret = -1;
383
Giulio Camuffo954f1832014-10-11 18:27:30 +0300384 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200385 handles[0] = fb->handle;
386 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200387 offsets[0] = 0;
388
Giulio Camuffo954f1832014-10-11 18:27:30 +0300389 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200390 format, handles, pitches, offsets,
391 &fb->fb_id, 0);
392 if (ret) {
393 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300394 backend->no_addfb2 = 1;
395 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200396 }
397 }
398
399 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300400 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200401 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200402
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300403 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200404 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200405 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300406 }
407
408 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
409
410 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200411
412err_free:
413 free(fb);
414 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300415}
416
417static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500418drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200419{
Pekka Paalanende685b82012-12-04 15:58:12 +0200420 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200421
422 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200423
Pekka Paalanende685b82012-12-04 15:58:12 +0200424 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200425}
426
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200427static void
428drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
429{
430 if (!fb)
431 return;
432
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200433 if (fb->map &&
434 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200435 drm_fb_destroy_dumb(fb);
436 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200437 if (fb->is_client_buffer)
438 gbm_bo_destroy(fb->bo);
439 else
440 gbm_surface_release_buffer(output->surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600441 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200442 }
443}
444
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500445static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200446drm_output_check_scanout_format(struct drm_output *output,
447 struct weston_surface *es, struct gbm_bo *bo)
448{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200449 uint32_t format;
450 pixman_region32_t r;
451
452 format = gbm_bo_get_format(bo);
453
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700454 if (format == GBM_FORMAT_ARGB8888) {
455 /* We can scanout an ARGB buffer if the surface's
456 * opaque region covers the whole output, but we have
457 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800458 pixman_region32_init_rect(&r, 0, 0,
459 output->base.width,
460 output->base.height);
461 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200462
463 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500464 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200465
466 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500467 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700468
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000469 if (output->format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700470 return format;
471
472 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200473}
474
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400475static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200476drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500477 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500478{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300479 struct drm_backend *b =
480 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200482 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300483 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500484 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500485
Jason Ekstranda7af7042013-10-12 22:38:11 -0500486 if (ev->geometry.x != output->base.x ||
487 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300488 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200489 buffer->width != output->base.current_mode->width ||
490 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200491 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500492 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400493 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500494
Pekka Paalanen5580f222015-02-17 16:33:18 +0200495 if (ev->geometry.scissor_enabled)
496 return NULL;
497
Giulio Camuffo954f1832014-10-11 18:27:30 +0300498 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700499 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500500
Rob Bradford9b101872012-09-14 23:25:41 +0100501 /* Unable to use the buffer for scanout */
502 if (!bo)
503 return NULL;
504
Jason Ekstranda7af7042013-10-12 22:38:11 -0500505 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500506 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300507 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400508 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300509 }
510
Giulio Camuffo954f1832014-10-11 18:27:30 +0300511 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300512 if (!output->next) {
513 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400514 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300515 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500516
Pekka Paalanende685b82012-12-04 15:58:12 +0200517 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500518
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400519 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500520}
521
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500522static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200523drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400524{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300525 struct drm_backend *b =
526 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300527 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400528
Giulio Camuffo954f1832014-10-11 18:27:30 +0300529 output->base.compositor->renderer->repaint_output(&output->base,
530 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400531
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300532 bo = gbm_surface_lock_front_buffer(output->surface);
533 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200534 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400535 return;
536 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300537
Giulio Camuffo954f1832014-10-11 18:27:30 +0300538 output->next = drm_fb_get_from_bo(bo, b, output->format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300539 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200540 weston_log("failed to get drm_fb for bo\n");
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300541 gbm_surface_release_buffer(output->surface, bo);
542 return;
543 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400544}
545
546static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200547drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
548{
549 struct weston_compositor *ec = output->base.compositor;
550 pixman_region32_t total_damage, previous_damage;
551
552 pixman_region32_init(&total_damage);
553 pixman_region32_init(&previous_damage);
554
555 pixman_region32_copy(&previous_damage, damage);
556
557 pixman_region32_union(&total_damage, damage, &output->previous_damage);
558 pixman_region32_copy(&output->previous_damage, &previous_damage);
559
560 output->current_image ^= 1;
561
562 output->next = output->dumb[output->current_image];
563 pixman_renderer_output_set_buffer(&output->base,
564 output->image[output->current_image]);
565
566 ec->renderer->repaint_output(&output->base, &total_damage);
567
568 pixman_region32_fini(&total_damage);
569 pixman_region32_fini(&previous_damage);
570}
571
572static void
573drm_output_render(struct drm_output *output, pixman_region32_t *damage)
574{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300575 struct weston_compositor *c = output->base.compositor;
576 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200577
Giulio Camuffo954f1832014-10-11 18:27:30 +0300578 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200579 drm_output_render_pixman(output, damage);
580 else
581 drm_output_render_gl(output, damage);
582
Giulio Camuffo954f1832014-10-11 18:27:30 +0300583 pixman_region32_subtract(&c->primary_plane.damage,
584 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200585}
586
587static void
Richard Hughese7299962013-05-01 21:52:12 +0100588drm_output_set_gamma(struct weston_output *output_base,
589 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
590{
591 int rc;
592 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300593 struct drm_backend *backend =
594 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100595
596 /* check */
597 if (output_base->gamma_size != size)
598 return;
599 if (!output->original_crtc)
600 return;
601
Giulio Camuffo954f1832014-10-11 18:27:30 +0300602 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100603 output->crtc_id,
604 size, r, g, b);
605 if (rc)
606 weston_log("set gamma failed: %m\n");
607}
608
Bryce Harringtonada4f072015-06-30 13:25:46 -0700609/* Determine the type of vblank synchronization to use for the output.
610 *
611 * The pipe parameter indicates which CRTC is in use. Knowing this, we
612 * can determine which vblank sequence type to use for it. Traditional
613 * cards had only two CRTCs, with CRTC 0 using no special flags, and
614 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
615 * parameter indicates this.
616 *
617 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
618 * 0-31. If this is non-zero it indicates we're dealing with a
619 * multi-gpu situation and we need to calculate the vblank sync
620 * using DRM_BLANK_HIGH_CRTC_MASK.
621 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300622static unsigned int
623drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200624{
625 if (output->pipe > 1)
626 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
627 DRM_VBLANK_HIGH_CRTC_MASK;
628 else if (output->pipe > 0)
629 return DRM_VBLANK_SECONDARY;
630 else
631 return 0;
632}
633
David Herrmann1edf44c2013-10-22 17:11:26 +0200634static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500635drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400636 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100637{
638 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300639 struct drm_backend *backend =
640 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500641 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400642 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500643 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100644
Xiong Zhangabd5d472013-10-11 14:43:07 +0800645 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200646 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800647
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300648 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400649 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300650 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200651 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100652
Hardeningff39efa2013-09-18 23:56:35 +0200653 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200654 if (!output->current ||
655 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300656 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300657 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400658 &output->connector_id, 1,
659 &mode->mode_info);
660 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200661 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200662 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400663 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300664 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200665 }
666
Giulio Camuffo954f1832014-10-11 18:27:30 +0300667 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300668 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500669 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200670 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200671 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500672 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100673
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300674 output->page_flip_pending = 1;
675
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400676 drm_output_set_cursor(output);
677
Jesse Barnes58ef3792012-02-23 09:45:49 -0500678 /*
679 * Now, update all the sprite surfaces
680 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300681 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200682 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500683 drmVBlank vbl = {
684 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
685 .request.sequence = 1,
686 };
687
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200688 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200689 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500690 continue;
691
Giulio Camuffo954f1832014-10-11 18:27:30 +0300692 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200693 fb_id = s->next->fb_id;
694
Giulio Camuffo954f1832014-10-11 18:27:30 +0300695 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200696 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500697 s->dest_x, s->dest_y,
698 s->dest_w, s->dest_h,
699 s->src_x, s->src_y,
700 s->src_w, s->src_h);
701 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200702 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500703 ret, strerror(errno));
704
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200705 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500706
Jesse Barnes58ef3792012-02-23 09:45:49 -0500707 /*
708 * Queue a vblank signal so we know when the surface
709 * becomes active on the display or has been replaced.
710 */
711 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300712 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500713 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200714 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500715 ret, strerror(errno));
716 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300717
718 s->output = output;
719 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500720 }
721
David Herrmann1edf44c2013-10-22 17:11:26 +0200722 return 0;
723
724err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800725 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200726 if (output->next) {
727 drm_output_release_fb(output, output->next);
728 output->next = NULL;
729 }
730
731 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400732}
733
734static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200735drm_output_start_repaint_loop(struct weston_output *output_base)
736{
737 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300738 struct drm_backend *backend = (struct drm_backend *)
739 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200740 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200741 struct timespec ts, tnow;
742 struct timespec vbl2now;
743 int64_t refresh_nsec;
744 int ret;
745 drmVBlank vbl = {
746 .request.type = DRM_VBLANK_RELATIVE,
747 .request.sequence = 0,
748 .request.signal = 0,
749 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300750
Xiong Zhangabd5d472013-10-11 14:43:07 +0800751 if (output->destroy_pending)
752 return;
753
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300754 if (!output->current) {
755 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200756 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300757 }
758
Mario Kleinerf507ec32015-06-21 21:25:14 +0200759 /* Try to get current msc and timestamp via instant query */
760 vbl.request.type |= drm_waitvblank_pipe(output);
761 ret = drmWaitVBlank(backend->drm.fd, &vbl);
762
763 /* Error ret or zero timestamp means failure to get valid timestamp */
764 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
765 ts.tv_sec = vbl.reply.tval_sec;
766 ts.tv_nsec = vbl.reply.tval_usec * 1000;
767
768 /* Valid timestamp for most recent vblank - not stale?
769 * Stale ts could happen on Linux 3.17+, so make sure it
770 * is not older than 1 refresh duration since now.
771 */
772 weston_compositor_read_presentation_clock(backend->compositor,
773 &tnow);
774 timespec_sub(&vbl2now, &tnow, &ts);
775 refresh_nsec =
776 millihz_to_nsec(output->base.current_mode->refresh);
777 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
778 drm_output_update_msc(output, vbl.reply.sequence);
779 weston_output_finish_frame(output_base, &ts,
780 PRESENTATION_FEEDBACK_INVALID);
781 return;
782 }
783 }
784
785 /* Immediate query didn't provide valid timestamp.
786 * Use pageflip fallback.
787 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300788 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200789
Giulio Camuffo954f1832014-10-11 18:27:30 +0300790 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200791 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
792 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200793 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200794 }
David Herrmann3c688c52013-10-22 17:11:25 +0200795
796 return;
797
798finish_frame:
799 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300800 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200801 weston_output_finish_frame(output_base, &ts,
802 PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200803}
804
805static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400806drm_output_update_msc(struct drm_output *output, unsigned int seq)
807{
808 uint64_t msc_hi = output->base.msc >> 32;
809
810 if (seq < (output->base.msc & 0xffffffff))
811 msc_hi++;
812
813 output->base.msc = (msc_hi << 32) + seq;
814}
815
816static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500817vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
818 void *data)
819{
820 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300821 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400822 struct timespec ts;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200823 uint32_t flags = PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
824 PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300825
Pekka Paalanen641307c2014-09-23 22:08:47 -0400826 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300827 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500828
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200829 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200830 s->current = s->next;
831 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300832
833 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400834 ts.tv_sec = sec;
835 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200836 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300837 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500838}
839
840static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800841drm_output_destroy(struct weston_output *output_base);
842
843static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400844page_flip_handler(int fd, unsigned int frame,
845 unsigned int sec, unsigned int usec, void *data)
846{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200847 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400848 struct timespec ts;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200849 uint32_t flags = PRESENTATION_FEEDBACK_KIND_VSYNC |
850 PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
851 PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400852
Pekka Paalanen641307c2014-09-23 22:08:47 -0400853 drm_output_update_msc(output, frame);
854
Jonas Ådahle5a12252013-04-05 23:07:11 +0200855 /* We don't set page_flip_pending on start_repaint_loop, in that case
856 * we just want to page flip to the current buffer to get an accurate
857 * timestamp */
858 if (output->page_flip_pending) {
859 drm_output_release_fb(output, output->current);
860 output->current = output->next;
861 output->next = NULL;
862 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300863
Jonas Ådahle5a12252013-04-05 23:07:11 +0200864 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400865
Xiong Zhangabd5d472013-10-11 14:43:07 +0800866 if (output->destroy_pending)
867 drm_output_destroy(&output->base);
868 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400869 ts.tv_sec = sec;
870 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200871 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300872
873 /* We can't call this from frame_notify, because the output's
874 * repaint needed flag is cleared just after that */
875 if (output->recorder)
876 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300877 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200878}
879
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500880static uint32_t
881drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500883{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500884 uint32_t i, format;
885
886 format = gbm_bo_get_format(bo);
887
888 if (format == GBM_FORMAT_ARGB8888) {
889 pixman_region32_t r;
890
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500891 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600892 ev->surface->width,
893 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500895
896 if (!pixman_region32_not_empty(&r))
897 format = GBM_FORMAT_XRGB8888;
898
899 pixman_region32_fini(&r);
900 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500901
902 for (i = 0; i < s->count_formats; i++)
903 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500904 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500905
906 return 0;
907}
908
909static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500911{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912 return !ev->transform.enabled ||
913 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500914}
915
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400916static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200917drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500919{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200920 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300921 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200922 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500923 struct drm_sprite *s;
924 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500925 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500926 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200927 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500928 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400929 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500930
Giulio Camuffo954f1832014-10-11 18:27:30 +0300931 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200932 return NULL;
933
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200934 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200935 return NULL;
936
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200937 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200938 return NULL;
939
Giulio Camuffo954f1832014-10-11 18:27:30 +0300940 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400941 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500942
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200943 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300945
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400947 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200950 return NULL;
951
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 if (wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500953 return NULL;
954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400956 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500957
Giulio Camuffo954f1832014-10-11 18:27:30 +0300958 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200959 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960 continue;
961
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200962 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500963 found = 1;
964 break;
965 }
966 }
967
968 /* No sprites available */
969 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400970 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500971
Giulio Camuffo954f1832014-10-11 18:27:30 +0300972 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 ev->surface->buffer_ref.buffer->resource,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700974 GBM_BO_USE_SCANOUT);
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400975 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400976 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400977
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500979 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200980 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400981 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500982 }
983
Giulio Camuffo954f1832014-10-11 18:27:30 +0300984 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200985 if (!s->next) {
986 gbm_bo_destroy(bo);
987 return NULL;
988 }
989
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500991
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400993 s->plane.x = box->x1;
994 s->plane.y = box->y1;
995
Jesse Barnes58ef3792012-02-23 09:45:49 -0500996 /*
997 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -0500998 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -0500999 * for us already).
1000 */
1001 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001003 &output->base.region);
1004 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001005 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001006 tbox = weston_transformed_rect(output->base.width,
1007 output->base.height,
1008 output->base.transform,
1009 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001010 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001011 s->dest_x = tbox.x1;
1012 s->dest_y = tbox.y1;
1013 s->dest_w = tbox.x2 - tbox.x1;
1014 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001015 pixman_region32_fini(&dest_rect);
1016
1017 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001019 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001020 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_from_global_fixed(ev,
1023 wl_fixed_from_int(box->x1),
1024 wl_fixed_from_int(box->y1),
1025 &sx1, &sy1);
1026 weston_view_from_global_fixed(ev,
1027 wl_fixed_from_int(box->x2),
1028 wl_fixed_from_int(box->y2),
1029 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001030
1031 if (sx1 < 0)
1032 sx1 = 0;
1033 if (sy1 < 0)
1034 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001035 if (sx2 > wl_fixed_from_int(ev->surface->width))
1036 sx2 = wl_fixed_from_int(ev->surface->width);
1037 if (sy2 > wl_fixed_from_int(ev->surface->height))
1038 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001039
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001040 tbox.x1 = sx1;
1041 tbox.y1 = sy1;
1042 tbox.x2 = sx2;
1043 tbox.y2 = sy2;
1044
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001045 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1046 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001047 viewport->buffer.transform,
1048 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001049 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001050
1051 s->src_x = tbox.x1 << 8;
1052 s->src_y = tbox.y1 << 8;
1053 s->src_w = (tbox.x2 - tbox.x1) << 8;
1054 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001055 pixman_region32_fini(&src_rect);
1056
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001057 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001058}
1059
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001060static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001061drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001063{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001064 struct drm_backend *b =
1065 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001066 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001067
Giulio Camuffo954f1832014-10-11 18:27:30 +03001068 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001069 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001070 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1071 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001072 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001073 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001075 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001076 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001077 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001078 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001079 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001080 if (ev->geometry.scissor_enabled)
1081 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 if (ev->surface->buffer_ref.buffer == NULL ||
1083 !wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource) ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001084 ev->surface->width > 64 || ev->surface->height > 64)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001085 return NULL;
1086
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001088
1089 return &output->cursor_plane;
1090}
1091
1092static void
1093drm_output_set_cursor(struct drm_output *output)
1094{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001096 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001097 struct drm_backend *b =
1098 (struct drm_backend *) output->base.compositor->backend;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001099 EGLint handle, stride;
1100 struct gbm_bo *bo;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001101 uint32_t buf[b->cursor_width * b->cursor_height];
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001102 unsigned char *s;
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001103 int i, x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001104
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 output->cursor_view = NULL;
1106 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001107 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001108 return;
1109 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001110
Neil Robertse5051712013-11-13 15:44:06 +00001111 buffer = ev->surface->buffer_ref.buffer;
1112
1113 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001114 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001115 pixman_region32_fini(&output->cursor_plane.damage);
1116 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001117 output->current_cursor ^= 1;
1118 bo = output->cursor_bo[output->current_cursor];
1119 memset(buf, 0, sizeof buf);
Neil Robertse5051712013-11-13 15:44:06 +00001120 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1121 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1122 wl_shm_buffer_begin_access(buffer->shm_buffer);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001123 for (i = 0; i < ev->surface->height; i++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001124 memcpy(buf + i * b->cursor_width, s + i * stride,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001125 ev->surface->width * 4);
Neil Robertse5051712013-11-13 15:44:06 +00001126 wl_shm_buffer_end_access(buffer->shm_buffer);
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001127
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001128 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
Pekka Paalanenae29da22012-08-06 14:57:05 +03001129 weston_log("failed update cursor: %m\n");
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001130
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001131 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001132 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1133 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001134 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001135 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001136 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001137 }
1138
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 x = (ev->geometry.x - output->base.x) * output->base.current_scale;
1140 y = (ev->geometry.y - output->base.y) * output->base.current_scale;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001141 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001142 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001143 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001144 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001145 }
1146
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001147 output->cursor_plane.x = x;
1148 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001149 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001150}
1151
Jesse Barnes58ef3792012-02-23 09:45:49 -05001152static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001153drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001154{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001155 struct drm_backend *b =
1156 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001157 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001159 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001160 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001161
1162 /*
1163 * Find a surface for each sprite in the output using some heuristics:
1164 * 1) size
1165 * 2) frequency of update
1166 * 3) opacity (though some hw might support alpha blending)
1167 * 4) clipping (this can be fixed with color keys)
1168 *
1169 * The idea is to save on blitting since this should save power.
1170 * If we can get a large video surface on the sprite for example,
1171 * the main display surface may not need to update at all, and
1172 * the client buffer can be used directly for the sprite surface
1173 * as we do for flipping full screen surfaces.
1174 */
1175 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001176 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177
Giulio Camuffo954f1832014-10-11 18:27:30 +03001178 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001179 struct weston_surface *es = ev->surface;
1180
1181 /* Test whether this buffer can ever go into a plane:
1182 * non-shm, or small enough to be a cursor.
1183 *
1184 * Also, keep a reference when using the pixman renderer.
1185 * That makes it possible to do a seamless switch to the GL
1186 * renderer and since the pixman renderer keeps a reference
1187 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001188 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001189 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001190 (es->buffer_ref.buffer &&
1191 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001192 (ev->surface->width <= 64 && ev->surface->height <= 64))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001193 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001194 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001195 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001196
Jesse Barnes58ef3792012-02-23 09:45:49 -05001197 pixman_region32_init(&surface_overlap);
1198 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001200
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001201 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001202 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001203 next_plane = primary;
1204 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001206 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001208 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001210 if (next_plane == NULL)
1211 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001212
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001214
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001215 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001216 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001218
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001219 if (next_plane == primary ||
1220 next_plane == &output->cursor_plane) {
1221 /* cursor plane involves a copy */
1222 ev->psf_flags = 0;
1223 } else {
1224 /* All other planes are a direct scanout of a
1225 * single client buffer.
1226 */
1227 ev->psf_flags = PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1228 }
1229
Jesse Barnes58ef3792012-02-23 09:45:49 -05001230 pixman_region32_fini(&surface_overlap);
1231 }
1232 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001233}
1234
Matt Roper361d2ad2011-08-29 13:52:23 -07001235static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001236drm_output_fini_pixman(struct drm_output *output);
1237
1238static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001239drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001240{
1241 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001242 struct drm_backend *b =
1243 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001244 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001245
Xiong Zhangabd5d472013-10-11 14:43:07 +08001246 if (output->page_flip_pending) {
1247 output->destroy_pending = 1;
1248 weston_log("destroy output while page flip pending\n");
1249 return;
1250 }
1251
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001252 if (output->backlight)
1253 backlight_destroy(output->backlight);
1254
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001255 drmModeFreeProperty(output->dpms_prop);
1256
Matt Roper361d2ad2011-08-29 13:52:23 -07001257 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001258 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001259
1260 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001261 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001262 origcrtc->x, origcrtc->y,
1263 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001264 drmModeFreeCrtc(origcrtc);
1265
Giulio Camuffo954f1832014-10-11 18:27:30 +03001266 b->crtc_allocator &= ~(1 << output->crtc_id);
1267 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001268
Giulio Camuffo954f1832014-10-11 18:27:30 +03001269 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001270 drm_output_fini_pixman(output);
1271 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001272 gl_renderer->output_destroy(output_base);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001273 gbm_surface_destroy(output->surface);
1274 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001275
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001276 weston_plane_release(&output->fb_plane);
1277 weston_plane_release(&output->cursor_plane);
1278
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001280
Matt Roper361d2ad2011-08-29 13:52:23 -07001281 free(output);
1282}
1283
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001284/**
1285 * Find the closest-matching mode for a given target
1286 *
1287 * Given a target mode, find the most suitable mode amongst the output's
1288 * current mode list to use, preferring the current mode if possible, to
1289 * avoid an expensive mode switch.
1290 *
1291 * @param output DRM output
1292 * @param target_mode Mode to attempt to match
1293 * @returns Pointer to a mode from the output's mode list
1294 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001295static struct drm_mode *
1296choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1297{
1298 struct drm_mode *tmp_mode = NULL, *mode;
1299
Hardeningff39efa2013-09-18 23:56:35 +02001300 if (output->base.current_mode->width == target_mode->width &&
1301 output->base.current_mode->height == target_mode->height &&
1302 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001303 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001304 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001305
1306 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1307 if (mode->mode_info.hdisplay == target_mode->width &&
1308 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001309 if (mode->base.refresh == target_mode->refresh ||
1310 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001311 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001312 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001313 tmp_mode = mode;
1314 }
1315 }
1316
1317 return tmp_mode;
1318}
1319
1320static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001321drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001322static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001323drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001324
1325static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001326drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1327{
1328 struct drm_output *output;
1329 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001330 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001331
1332 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001333 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001334 return -1;
1335 }
1336
1337 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001338 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001339 return -1;
1340 }
1341
Giulio Camuffo954f1832014-10-11 18:27:30 +03001342 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001343 output = (struct drm_output *)output_base;
1344 drm_mode = choose_mode (output, mode);
1345
1346 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001347 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001348 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001349 }
1350
Hardeningff39efa2013-09-18 23:56:35 +02001351 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001352 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001353
Hardeningff39efa2013-09-18 23:56:35 +02001354 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001355
Hardeningff39efa2013-09-18 23:56:35 +02001356 output->base.current_mode = &drm_mode->base;
1357 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001358 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1359
Alex Wub7b8bda2012-04-17 17:20:48 +08001360 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001361 drm_output_release_fb(output, output->current);
1362 drm_output_release_fb(output, output->next);
1363 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001364
Giulio Camuffo954f1832014-10-11 18:27:30 +03001365 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001366 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001367 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001368 weston_log("failed to init output pixman state with "
1369 "new mode\n");
1370 return -1;
1371 }
1372 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001373 gl_renderer->output_destroy(&output->base);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001374 gbm_surface_destroy(output->surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001375
Giulio Camuffo954f1832014-10-11 18:27:30 +03001376 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001377 weston_log("failed to init output egl state with "
1378 "new mode");
1379 return -1;
1380 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001381 }
1382
Alex Wub7b8bda2012-04-17 17:20:48 +08001383 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001384}
1385
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001386static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001387on_drm_input(int fd, uint32_t mask, void *data)
1388{
1389 drmEventContext evctx;
1390
1391 memset(&evctx, 0, sizeof evctx);
1392 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1393 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001394 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001395 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001396
1397 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001398}
1399
1400static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001401init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001402{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001403 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001404 uint64_t cap;
1405 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001406 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001407
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001408 sysnum = udev_device_get_sysnum(device);
1409 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001410 b->drm.id = atoi(sysnum);
1411 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001412 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001413 return -1;
1414 }
1415
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001416 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001417 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001418 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001419 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001420 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001421 udev_device_get_devnode(device));
1422 return -1;
1423 }
1424
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001425 weston_log("using %s\n", filename);
1426
Giulio Camuffo954f1832014-10-11 18:27:30 +03001427 b->drm.fd = fd;
1428 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001429
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001430 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1431 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001432 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001433 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001434 clk_id = CLOCK_REALTIME;
1435
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001437 weston_log("Error: failed to set presentation clock %d.\n",
1438 clk_id);
1439 return -1;
1440 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001441
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001442 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1443 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001444 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001445 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001446 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001447
1448 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1449 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001450 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001451 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001452 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001453
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001454 return 0;
1455}
1456
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001457static struct gbm_device *
1458create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001459{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001460 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001461
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001462 gl_renderer = weston_load_module("gl-renderer.so",
1463 "gl_renderer_interface");
1464 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001465 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001466
1467 /* GBM will load a dri driver, but even though they need symbols from
1468 * libglapi, in some version of Mesa they are not linked to it. Since
1469 * only the gl-renderer module links to it, the call above won't make
1470 * these symbols globally available, and loading the DRI driver fails.
1471 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1472 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1473
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001474 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001475
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001476 return gbm;
1477}
1478
Bryce Harringtonc056a982015-05-19 15:25:18 -07001479/* When initializing EGL, if the preferred buffer format isn't available
Derek Foremanc4cfe852015-05-15 12:12:40 -05001480 * we may be able to susbstitute an ARGB format for an XRGB one.
1481 *
1482 * This returns 0 if substitution isn't possible, but 0 might be a
1483 * legitimate format for other EGL platforms, so the caller is
1484 * responsible for checking for 0 before calling gl_renderer->create().
1485 *
1486 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1487 * but it's entirely possible we'll see this again on other implementations.
1488 */
1489static int
1490fallback_format_for(uint32_t format)
1491{
1492 switch (format) {
1493 case GBM_FORMAT_XRGB8888:
1494 return GBM_FORMAT_ARGB8888;
1495 case GBM_FORMAT_XRGB2101010:
1496 return GBM_FORMAT_ARGB2101010;
1497 default:
1498 return 0;
1499 }
1500}
1501
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001502static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001503drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001504{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001505 EGLint format[2] = {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001506 b->format,
1507 fallback_format_for(b->format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001508 };
1509 int n_formats = 1;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001510
Derek Foremanc4cfe852015-05-15 12:12:40 -05001511 if (format[1])
1512 n_formats = 2;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001513 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001514 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001515 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001516 gl_renderer->opaque_attribs,
1517 format,
1518 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001519 return -1;
1520 }
1521
1522 return 0;
1523}
1524
1525static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001526init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001527{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001528 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001529
Giulio Camuffo954f1832014-10-11 18:27:30 +03001530 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001531 return -1;
1532
Giulio Camuffo954f1832014-10-11 18:27:30 +03001533 if (drm_backend_create_gl_renderer(b) < 0) {
1534 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001535 return -1;
1536 }
1537
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001538 return 0;
1539}
1540
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001541static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001542init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001543{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001544 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001545}
1546
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001547/**
1548 * Add a mode to output's mode list
1549 *
1550 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1551 * output's mode list.
1552 *
1553 * @param output DRM output to add mode to
1554 * @param info DRM mode structure to add
1555 * @returns Newly-allocated Weston/DRM mode structure
1556 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001557static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001558drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001559{
1560 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001561 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001562
1563 mode = malloc(sizeof *mode);
1564 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001565 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001566
1567 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001568 mode->base.width = info->hdisplay;
1569 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001570
1571 /* Calculate higher precision (mHz) refresh rate */
1572 refresh = (info->clock * 1000000LL / info->htotal +
1573 info->vtotal / 2) / info->vtotal;
1574
1575 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1576 refresh *= 2;
1577 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1578 refresh /= 2;
1579 if (info->vscan > 1)
1580 refresh /= info->vscan;
1581
1582 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001583 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001584
1585 if (info->type & DRM_MODE_TYPE_PREFERRED)
1586 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1587
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001588 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1589
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001590 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001591}
1592
1593static int
1594drm_subpixel_to_wayland(int drm_value)
1595{
1596 switch (drm_value) {
1597 default:
1598 case DRM_MODE_SUBPIXEL_UNKNOWN:
1599 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1600 case DRM_MODE_SUBPIXEL_NONE:
1601 return WL_OUTPUT_SUBPIXEL_NONE;
1602 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1603 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1604 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1605 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1606 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1607 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1608 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1609 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1610 }
1611}
1612
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001613/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001614static uint32_t
1615drm_get_backlight(struct drm_output *output)
1616{
1617 long brightness, max_brightness, norm;
1618
1619 brightness = backlight_get_brightness(output->backlight);
1620 max_brightness = backlight_get_max_brightness(output->backlight);
1621
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001622 /* convert it on a scale of 0 to 255 */
1623 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001624
1625 return (uint32_t) norm;
1626}
1627
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001628/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001629static void
1630drm_set_backlight(struct weston_output *output_base, uint32_t value)
1631{
1632 struct drm_output *output = (struct drm_output *) output_base;
1633 long max_brightness, new_brightness;
1634
1635 if (!output->backlight)
1636 return;
1637
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001638 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001639 return;
1640
1641 max_brightness = backlight_get_max_brightness(output->backlight);
1642
1643 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001644 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001645
1646 backlight_set_brightness(output->backlight, new_brightness);
1647}
1648
1649static drmModePropertyPtr
1650drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1651{
1652 drmModePropertyPtr props;
1653 int i;
1654
1655 for (i = 0; i < connector->count_props; i++) {
1656 props = drmModeGetProperty(fd, connector->props[i]);
1657 if (!props)
1658 continue;
1659
1660 if (!strcmp(props->name, name))
1661 return props;
1662
1663 drmModeFreeProperty(props);
1664 }
1665
1666 return NULL;
1667}
1668
1669static void
1670drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1671{
1672 struct drm_output *output = (struct drm_output *) output_base;
1673 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001674 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001675 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001676
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001677 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001678 return;
1679
Daniel Stone36609c72015-06-18 07:49:02 +01001680 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1681 output->dpms_prop->prop_id, level);
1682 if (ret) {
1683 weston_log("DRM: DPMS: failed property set for %s\n",
1684 output->base.name);
1685 return;
1686 }
1687
1688 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001689}
1690
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001691static const char * const connector_type_names[] = {
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001692 "None",
1693 "VGA",
1694 "DVI",
1695 "DVI",
1696 "DVI",
1697 "Composite",
1698 "TV",
1699 "LVDS",
1700 "CTV",
1701 "DIN",
1702 "DP",
1703 "HDMI",
1704 "HDMI",
1705 "TV",
1706 "eDP",
1707};
1708
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001709static char *
1710make_connector_name(const drmModeConnector *con)
1711{
1712 char name[32];
1713 const char *type_name;
1714
1715 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1716 type_name = connector_type_names[con->connector_type];
1717 else
1718 type_name = "UNKNOWN";
1719 snprintf(name, sizeof name, "%s%d", type_name, con->connector_type_id);
1720
1721 return strdup(name);
1722}
1723
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001724static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001725find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001726 drmModeRes *resources, drmModeConnector *connector)
1727{
1728 drmModeEncoder *encoder;
1729 uint32_t possible_crtcs;
1730 int i, j;
1731
1732 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001733 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001734 if (encoder == NULL) {
1735 weston_log("Failed to get encoder.\n");
1736 return -1;
1737 }
1738 possible_crtcs = encoder->possible_crtcs;
1739 drmModeFreeEncoder(encoder);
1740
1741 for (i = 0; i < resources->count_crtcs; i++) {
1742 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001743 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001744 return i;
1745 }
1746 }
1747
1748 return -1;
1749}
1750
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001751/* Init output state that depends on gl or gbm */
1752static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001753drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001754{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001755 EGLint format[2] = {
1756 output->format,
1757 fallback_format_for(output->format),
1758 };
1759 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001760
Giulio Camuffo954f1832014-10-11 18:27:30 +03001761 output->surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001762 output->base.current_mode->width,
1763 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001764 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001765 GBM_BO_USE_SCANOUT |
1766 GBM_BO_USE_RENDERING);
1767 if (!output->surface) {
1768 weston_log("failed to create gbm surface\n");
1769 return -1;
1770 }
1771
Derek Foremanc4cfe852015-05-15 12:12:40 -05001772 if (format[1])
1773 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001774 if (gl_renderer->output_create(&output->base,
Jonny Lamb445fb692015-03-24 13:12:01 +01001775 (EGLNativeDisplayType)output->surface,
1776 output->surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001777 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001778 format,
1779 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001780 weston_log("failed to create gl renderer output state\n");
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001781 gbm_surface_destroy(output->surface);
1782 return -1;
1783 }
1784
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001785 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001786
1787 for (i = 0; i < 2; i++) {
1788 if (output->cursor_bo[i])
1789 continue;
1790
1791 output->cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001792 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001793 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001794 }
1795
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001796 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1797 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001798 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001799 }
1800
1801 return 0;
1802}
1803
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001804static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001805drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001806{
Hardeningff39efa2013-09-18 23:56:35 +02001807 int w = output->base.current_mode->width;
1808 int h = output->base.current_mode->height;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001809 unsigned int i;
1810
1811 /* FIXME error checking */
1812
1813 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001814 output->dumb[i] = drm_fb_create_dumb(b, w, h);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001815 if (!output->dumb[i])
1816 goto err;
1817
1818 output->image[i] =
Alexander Larsson0b135062013-05-28 16:23:36 +02001819 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001820 output->dumb[i]->map,
1821 output->dumb[i]->stride);
1822 if (!output->image[i])
1823 goto err;
1824 }
1825
1826 if (pixman_renderer_output_create(&output->base) < 0)
1827 goto err;
1828
1829 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001830 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001831
1832 return 0;
1833
1834err:
1835 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1836 if (output->dumb[i])
1837 drm_fb_destroy_dumb(output->dumb[i]);
1838 if (output->image[i])
1839 pixman_image_unref(output->image[i]);
1840
1841 output->dumb[i] = NULL;
1842 output->image[i] = NULL;
1843 }
1844
1845 return -1;
1846}
1847
1848static void
1849drm_output_fini_pixman(struct drm_output *output)
1850{
1851 unsigned int i;
1852
1853 pixman_renderer_output_destroy(&output->base);
1854 pixman_region32_fini(&output->previous_damage);
1855
1856 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1857 drm_fb_destroy_dumb(output->dumb[i]);
1858 pixman_image_unref(output->image[i]);
1859 output->dumb[i] = NULL;
1860 output->image[i] = NULL;
1861 }
1862}
1863
Richard Hughes2b2092a2013-04-24 14:58:02 +01001864static void
1865edid_parse_string(const uint8_t *data, char text[])
1866{
1867 int i;
1868 int replaced = 0;
1869
1870 /* this is always 12 bytes, but we can't guarantee it's null
1871 * terminated or not junk. */
1872 strncpy(text, (const char *) data, 12);
1873
1874 /* remove insane chars */
1875 for (i = 0; text[i] != '\0'; i++) {
1876 if (text[i] == '\n' ||
1877 text[i] == '\r') {
1878 text[i] = '\0';
1879 break;
1880 }
1881 }
1882
1883 /* ensure string is printable */
1884 for (i = 0; text[i] != '\0'; i++) {
1885 if (!isprint(text[i])) {
1886 text[i] = '-';
1887 replaced++;
1888 }
1889 }
1890
1891 /* if the string is random junk, ignore the string */
1892 if (replaced > 4)
1893 text[0] = '\0';
1894}
1895
1896#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1897#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1898#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1899#define EDID_OFFSET_DATA_BLOCKS 0x36
1900#define EDID_OFFSET_LAST_BLOCK 0x6c
1901#define EDID_OFFSET_PNPID 0x08
1902#define EDID_OFFSET_SERIAL 0x0c
1903
1904static int
1905edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1906{
1907 int i;
1908 uint32_t serial_number;
1909
1910 /* check header */
1911 if (length < 128)
1912 return -1;
1913 if (data[0] != 0x00 || data[1] != 0xff)
1914 return -1;
1915
1916 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1917 * /--08--\/--09--\
1918 * 7654321076543210
1919 * |\---/\---/\---/
1920 * R C1 C2 C3 */
1921 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1922 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1923 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1924 edid->pnp_id[3] = '\0';
1925
1926 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1927 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1928 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1929 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1930 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1931 if (serial_number > 0)
1932 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1933
1934 /* parse EDID data */
1935 for (i = EDID_OFFSET_DATA_BLOCKS;
1936 i <= EDID_OFFSET_LAST_BLOCK;
1937 i += 18) {
1938 /* ignore pixel clock data */
1939 if (data[i] != 0)
1940 continue;
1941 if (data[i+2] != 0)
1942 continue;
1943
1944 /* any useful blocks? */
1945 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1946 edid_parse_string(&data[i+5],
1947 edid->monitor_name);
1948 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1949 edid_parse_string(&data[i+5],
1950 edid->serial_number);
1951 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1952 edid_parse_string(&data[i+5],
1953 edid->eisa_id);
1954 }
1955 }
1956 return 0;
1957}
1958
1959static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001960find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01001961 struct drm_output *output,
1962 drmModeConnector *connector)
1963{
1964 drmModePropertyBlobPtr edid_blob = NULL;
1965 drmModePropertyPtr property;
1966 int i;
1967 int rc;
1968
1969 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001970 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01001971 if (!property)
1972 continue;
1973 if ((property->flags & DRM_MODE_PROP_BLOB) &&
1974 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001975 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01001976 connector->prop_values[i]);
1977 }
1978 drmModeFreeProperty(property);
1979 }
1980 if (!edid_blob)
1981 return;
1982
1983 rc = edid_parse(&output->edid,
1984 edid_blob->data,
1985 edid_blob->length);
1986 if (!rc) {
1987 weston_log("EDID data '%s', '%s', '%s'\n",
1988 output->edid.pnp_id,
1989 output->edid.monitor_name,
1990 output->edid.serial_number);
1991 if (output->edid.pnp_id[0] != '\0')
1992 output->base.make = output->edid.pnp_id;
1993 if (output->edid.monitor_name[0] != '\0')
1994 output->base.model = output->edid.monitor_name;
1995 if (output->edid.serial_number[0] != '\0')
1996 output->base.serial_number = output->edid.serial_number;
1997 }
1998 drmModeFreePropertyBlob(edid_blob);
1999}
2000
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002001
2002
2003static int
2004parse_modeline(const char *s, drmModeModeInfo *mode)
2005{
2006 char hsync[16];
2007 char vsync[16];
2008 float fclock;
2009
2010 mode->type = DRM_MODE_TYPE_USERDEF;
2011 mode->hskew = 0;
2012 mode->vscan = 0;
2013 mode->vrefresh = 0;
2014 mode->flags = 0;
2015
Rob Bradford307e09e2013-07-26 16:29:40 +01002016 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002017 &fclock,
2018 &mode->hdisplay,
2019 &mode->hsync_start,
2020 &mode->hsync_end,
2021 &mode->htotal,
2022 &mode->vdisplay,
2023 &mode->vsync_start,
2024 &mode->vsync_end,
2025 &mode->vtotal, hsync, vsync) != 11)
2026 return -1;
2027
2028 mode->clock = fclock * 1000;
2029 if (strcmp(hsync, "+hsync") == 0)
2030 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2031 else if (strcmp(hsync, "-hsync") == 0)
2032 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2033 else
2034 return -1;
2035
2036 if (strcmp(vsync, "+vsync") == 0)
2037 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2038 else if (strcmp(vsync, "-vsync") == 0)
2039 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2040 else
2041 return -1;
2042
2043 return 0;
2044}
2045
Rob Bradford66bd9f52013-06-25 18:56:42 +01002046static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002047setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002048 struct weston_output *output,
2049 const char *s)
2050{
2051 if (strcmp(s, "") != 0) {
2052 struct udev_seat *seat;
2053
Giulio Camuffo954f1832014-10-11 18:27:30 +03002054 seat = udev_seat_get_named(&b->input, s);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002055 if (seat)
2056 seat->base.output = output;
2057
2058 if (seat && seat->base.pointer)
2059 weston_pointer_clamp(seat->base.pointer,
2060 &seat->base.pointer->x,
2061 &seat->base.pointer->y);
2062 }
2063}
2064
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002065static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002066get_gbm_format_from_section(struct weston_config_section *section,
2067 uint32_t default_value,
2068 uint32_t *format)
2069{
2070 char *s;
2071 int ret = 0;
2072
2073 weston_config_section_get_string(section,
2074 "gbm-format", &s, NULL);
2075
2076 if (s == NULL)
2077 *format = default_value;
2078 else if (strcmp(s, "xrgb8888") == 0)
2079 *format = GBM_FORMAT_XRGB8888;
2080 else if (strcmp(s, "rgb565") == 0)
2081 *format = GBM_FORMAT_RGB565;
2082 else if (strcmp(s, "xrgb2101010") == 0)
2083 *format = GBM_FORMAT_XRGB2101010;
2084 else {
2085 weston_log("fatal: unrecognized pixel format: %s\n", s);
2086 ret = -1;
2087 }
2088
2089 free(s);
2090
2091 return ret;
2092}
2093
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002094/**
2095 * Choose suitable mode for an output
2096 *
2097 * Find the most suitable mode to use for initial setup (or reconfiguration on
2098 * hotplug etc) for a DRM output.
2099 *
2100 * @param output DRM output to choose mode for
2101 * @param kind Strategy and preference to use when choosing mode
2102 * @param width Desired width for this output
2103 * @param height Desired height for this output
2104 * @param current_mode Mode currently being displayed on this output
2105 * @param modeline Manually-entered mode (may be NULL)
2106 * @returns A mode from the output's mode list, or NULL if none available
2107 */
2108static struct drm_mode *
2109drm_output_choose_initial_mode(struct drm_output *output,
2110 enum output_config kind,
2111 int width, int height,
2112 const drmModeModeInfo *current_mode,
2113 const drmModeModeInfo *modeline)
2114{
2115 struct drm_mode *preferred = NULL;
2116 struct drm_mode *current = NULL;
2117 struct drm_mode *configured = NULL;
2118 struct drm_mode *best = NULL;
2119 struct drm_mode *drm_mode;
2120
2121 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
2122 if (kind == OUTPUT_CONFIG_MODE &&
2123 width == drm_mode->base.width &&
2124 height == drm_mode->base.height)
2125 configured = drm_mode;
2126
2127 if (memcmp(&current_mode, &drm_mode->mode_info,
2128 sizeof *current_mode) == 0)
2129 current = drm_mode;
2130
2131 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2132 preferred = drm_mode;
2133
2134 best = drm_mode;
2135 }
2136
2137 if (kind == OUTPUT_CONFIG_MODELINE) {
2138 configured = drm_output_add_mode(output, modeline);
2139 if (!configured)
2140 return NULL;
2141 }
2142
2143 if (current == NULL && current_mode->clock != 0) {
2144 current = drm_output_add_mode(output, current_mode);
2145 if (!current)
2146 return NULL;
2147 }
2148
2149 if (kind == OUTPUT_CONFIG_CURRENT)
2150 configured = current;
2151
2152 if (option_current_mode && current)
2153 return current;
2154
2155 if (configured)
2156 return configured;
2157
2158 if (preferred)
2159 return preferred;
2160
2161 if (current)
2162 return current;
2163
2164 if (best)
2165 return best;
2166
2167 weston_log("no available modes for %s\n", output->base.name);
2168 return NULL;
2169}
2170
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002171static int
2172connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2173 drmModeModeInfo *mode)
2174{
2175 drmModeEncoder *encoder;
2176 drmModeCrtc *crtc;
2177
2178 /* Get the current mode on the crtc that's currently driving
2179 * this connector. */
2180 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2181 memset(mode, 0, sizeof *mode);
2182 if (encoder != NULL) {
2183 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2184 drmModeFreeEncoder(encoder);
2185 if (crtc == NULL)
2186 return -1;
2187 if (crtc->mode_valid)
2188 *mode = crtc->mode;
2189 drmModeFreeCrtc(crtc);
2190 }
2191
2192 return 0;
2193}
2194
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002195/**
2196 * Create and configure a Weston output structure
2197 *
2198 * Given a DRM connector, create a matching drm_output structure and add it
2199 * to Weston's output list.
2200 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002201 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002202 * @param resources DRM resources for this device
2203 * @param connector DRM connector to use for this new output
2204 * @param x Horizontal offset to use into global co-ordinate space
2205 * @param y Vertical offset to use into global co-ordinate space
2206 * @param drm_device udev device pointer
2207 * @returns 0 on success, or -1 on failure
2208 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002209static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002210create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002211 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002212 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002213 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002214{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002215 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002216 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002217 struct weston_mode *m;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002218 struct weston_config_section *section;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002219 drmModeModeInfo crtc_mode, modeline;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002220 int i, width, height, scale;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002221 char *s;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002222 enum output_config config;
2223 uint32_t transform;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002224
Giulio Camuffo954f1832014-10-11 18:27:30 +03002225 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002226 if (i < 0) {
2227 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002228 return -1;
2229 }
2230
Peter Huttererf3d62272013-08-08 11:57:05 +10002231 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002232 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002233 return -1;
2234
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002235 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002236 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002237 output->base.make = "unknown";
2238 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002239 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002240 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002241
Giulio Camuffo954f1832014-10-11 18:27:30 +03002242 section = weston_config_get_section(b->compositor->config, "output", "name",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002243 output->base.name);
2244 weston_config_section_get_string(section, "mode", &s, "preferred");
2245 if (strcmp(s, "off") == 0)
2246 config = OUTPUT_CONFIG_OFF;
2247 else if (strcmp(s, "preferred") == 0)
2248 config = OUTPUT_CONFIG_PREFERRED;
2249 else if (strcmp(s, "current") == 0)
2250 config = OUTPUT_CONFIG_CURRENT;
2251 else if (sscanf(s, "%dx%d", &width, &height) == 2)
2252 config = OUTPUT_CONFIG_MODE;
2253 else if (parse_modeline(s, &modeline) == 0)
2254 config = OUTPUT_CONFIG_MODELINE;
2255 else {
2256 weston_log("Invalid mode \"%s\" for output %s\n",
2257 s, output->base.name);
2258 config = OUTPUT_CONFIG_PREFERRED;
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002259 }
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002260 free(s);
2261
2262 weston_config_section_get_int(section, "scale", &scale, 1);
2263 weston_config_section_get_string(section, "transform", &s, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05002264 if (weston_parse_transform(s, &transform) < 0)
2265 weston_log("Invalid transform \"%s\" for output %s\n",
2266 s, output->base.name);
2267
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002268 free(s);
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002269
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002270 if (get_gbm_format_from_section(section,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002271 b->format,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002272 &output->format) == -1)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002273 output->format = b->format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002274
Rob Bradford66bd9f52013-06-25 18:56:42 +01002275 weston_config_section_get_string(section, "seat", &s, "");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002276 setup_output_seat_constraint(b, &output->base, s);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002277 free(s);
2278
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002279 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002280 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002281 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002282 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002283 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002284
Giulio Camuffo954f1832014-10-11 18:27:30 +03002285 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2286 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002287
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002288 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2289 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002290
David Herrmann0f0d54e2011-12-08 17:05:45 +01002291 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002292 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002293 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002294 goto err_free;
2295 }
2296
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002297 if (config == OUTPUT_CONFIG_OFF) {
2298 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002299 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002300 0, 0, 0, 0, 0, NULL);
2301 goto err_free;
2302 }
2303
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002304 current = drm_output_choose_initial_mode(output, config,
2305 width, height,
2306 &crtc_mode, &modeline);
2307 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002308 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002309 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002310 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002311
Giulio Camuffo954f1832014-10-11 18:27:30 +03002312 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002313 connector->mmWidth, connector->mmHeight,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002314 transform, scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002315
Giulio Camuffo954f1832014-10-11 18:27:30 +03002316 if (b->use_pixman) {
2317 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002318 weston_log("Failed to init output pixman state\n");
2319 goto err_output;
2320 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002321 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002322 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002323 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002324 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002325
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002326 output->backlight = backlight_init(drm_device,
2327 connector->connector_type);
2328 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002329 weston_log("Initialized backlight, device %s\n",
2330 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002331 output->base.set_backlight = drm_set_backlight;
2332 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002333 } else {
2334 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002335 }
2336
Giulio Camuffo954f1832014-10-11 18:27:30 +03002337 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002338
Giulio Camuffo954f1832014-10-11 18:27:30 +03002339 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002340 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2341 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002342
Jonas Ådahle5a12252013-04-05 23:07:11 +02002343 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002344 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002345 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002346 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002347 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002348 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002349
Richard Hughese7299962013-05-01 21:52:12 +01002350 output->base.gamma_size = output->original_crtc->gamma_size;
2351 output->base.set_gamma = drm_output_set_gamma;
2352
Giulio Camuffo954f1832014-10-11 18:27:30 +03002353 weston_plane_init(&output->cursor_plane, b->compositor, 0, 0);
2354 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002355
Giulio Camuffo954f1832014-10-11 18:27:30 +03002356 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2357 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2358 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002359
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002360 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002361 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002362 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002363 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002364 m->width, m->height, m->refresh / 1000.0,
2365 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2366 ", preferred" : "",
2367 m->flags & WL_OUTPUT_MODE_CURRENT ?
2368 ", current" : "",
2369 connector->count_modes == 0 ?
2370 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002371
Mario Kleiner80817042015-06-21 21:25:11 +02002372 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2373 output->base.native_mode = output->base.current_mode;
2374 output->base.native_scale = output->base.current_scale;
2375
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002376 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002377
John Kåre Alsaker94659272012-11-13 19:10:18 +01002378err_output:
2379 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002380err_free:
2381 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2382 base.link) {
2383 wl_list_remove(&drm_mode->base.link);
2384 free(drm_mode);
2385 }
2386
2387 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002388 b->crtc_allocator &= ~(1 << output->crtc_id);
2389 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002390 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002391
David Herrmann0f0d54e2011-12-08 17:05:45 +01002392 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002393}
2394
Jesse Barnes58ef3792012-02-23 09:45:49 -05002395static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002397{
2398 struct drm_sprite *sprite;
2399 drmModePlaneRes *plane_res;
2400 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002401 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002402
Giulio Camuffo954f1832014-10-11 18:27:30 +03002403 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002404 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002405 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002406 strerror(errno));
2407 return;
2408 }
2409
2410 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002411 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002412 if (!plane)
2413 continue;
2414
Peter Huttererf3d62272013-08-08 11:57:05 +10002415 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002416 plane->count_formats));
2417 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002418 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002419 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002420 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002421 continue;
2422 }
2423
Jesse Barnes58ef3792012-02-23 09:45:49 -05002424 sprite->possible_crtcs = plane->possible_crtcs;
2425 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002426 sprite->current = NULL;
2427 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002428 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002429 sprite->count_formats = plane->count_formats;
2430 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002431 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002432 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002433 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2434 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2435 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002436
Giulio Camuffo954f1832014-10-11 18:27:30 +03002437 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002438 }
2439
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002440 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002441}
2442
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002443static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002444destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002445{
2446 struct drm_sprite *sprite, *next;
2447 struct drm_output *output;
2448
Giulio Camuffo954f1832014-10-11 18:27:30 +03002449 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002450 struct drm_output, base.link);
2451
Giulio Camuffo954f1832014-10-11 18:27:30 +03002452 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2453 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002454 sprite->plane_id,
2455 output->crtc_id, 0, 0,
2456 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002457 drm_output_release_fb(output, sprite->current);
2458 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002459 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002460 free(sprite);
2461 }
2462}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002463
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002464static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002465create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002467{
2468 drmModeConnector *connector;
2469 drmModeRes *resources;
2470 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002471 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002472
Giulio Camuffo954f1832014-10-11 18:27:30 +03002473 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002474 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002475 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002476 return -1;
2477 }
2478
Giulio Camuffo954f1832014-10-11 18:27:30 +03002479 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2480 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002481 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002482 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002483 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002484
Giulio Camuffo954f1832014-10-11 18:27:30 +03002485 b->min_width = resources->min_width;
2486 b->max_width = resources->max_width;
2487 b->min_height = resources->min_height;
2488 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002489
Giulio Camuffo954f1832014-10-11 18:27:30 +03002490 b->num_crtcs = resources->count_crtcs;
2491 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002492
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002493 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002494 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002495 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002496 if (connector == NULL)
2497 continue;
2498
2499 if (connector->connection == DRM_MODE_CONNECTED &&
2500 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002501 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002502 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002503 connector, x, y,
2504 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002505 drmModeFreeConnector(connector);
2506 continue;
2507 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002508
Giulio Camuffo954f1832014-10-11 18:27:30 +03002509 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002510 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002511 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002512 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002513
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002514 drmModeFreeConnector(connector);
2515 }
2516
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002518 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002519 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002520 return -1;
2521 }
2522
2523 drmModeFreeResources(resources);
2524
2525 return 0;
2526}
2527
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002528static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002529update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002530{
2531 drmModeConnector *connector;
2532 drmModeRes *resources;
2533 struct drm_output *output, *next;
2534 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002535 uint32_t connected = 0, disconnects = 0;
2536 int i;
2537
Giulio Camuffo954f1832014-10-11 18:27:30 +03002538 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002539 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002540 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002541 return;
2542 }
2543
2544 /* collect new connects */
2545 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002546 int connector_id = resources->connectors[i];
2547
Giulio Camuffo954f1832014-10-11 18:27:30 +03002548 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002549 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002550 continue;
2551
David Herrmann7551cff2011-12-08 17:05:43 +01002552 if (connector->connection != DRM_MODE_CONNECTED) {
2553 drmModeFreeConnector(connector);
2554 continue;
2555 }
2556
Benjamin Franzke117483d2011-08-30 11:38:26 +02002557 connected |= (1 << connector_id);
2558
Giulio Camuffo954f1832014-10-11 18:27:30 +03002559 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002560 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002561 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002562 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002563
2564 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002565 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002566 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002567 else
2568 x = 0;
2569 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002570 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002571 connector, x, y,
2572 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002573 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002574
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002575 }
2576 drmModeFreeConnector(connector);
2577 }
2578 drmModeFreeResources(resources);
2579
Giulio Camuffo954f1832014-10-11 18:27:30 +03002580 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002581 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002582 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002583 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002584 if (disconnects & (1 << output->connector_id)) {
2585 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002586 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002587 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002588 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002589 }
2590 }
2591 }
2592
Daniel Stonef556ebe2015-05-21 08:28:58 +01002593 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002594 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002595 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002596}
2597
2598static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002599udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002600{
David Herrmannd7488c22012-03-11 20:05:21 +01002601 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002602 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002603
2604 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002605 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002606 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002607
David Herrmann6ac52db2012-03-11 20:05:22 +01002608 val = udev_device_get_property_value(device, "HOTPLUG");
2609 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002610 return 0;
2611
David Herrmann6ac52db2012-03-11 20:05:22 +01002612 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002613}
2614
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002615static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002616udev_drm_event(int fd, uint32_t mask, void *data)
2617{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002619 struct udev_device *event;
2620
Giulio Camuffo954f1832014-10-11 18:27:30 +03002621 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002622
Giulio Camuffo954f1832014-10-11 18:27:30 +03002623 if (udev_event_is_hotplug(b, event))
2624 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002625
2626 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002627
2628 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002629}
2630
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002631static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002632drm_restore(struct weston_compositor *ec)
2633{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002634 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002635}
2636
2637static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002638drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002639{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002640 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002641
Giulio Camuffo954f1832014-10-11 18:27:30 +03002642 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002643
Giulio Camuffo954f1832014-10-11 18:27:30 +03002644 wl_event_source_remove(b->udev_drm_source);
2645 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002646
Giulio Camuffo954f1832014-10-11 18:27:30 +03002647 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002648
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002649 weston_compositor_shutdown(ec);
2650
Giulio Camuffo954f1832014-10-11 18:27:30 +03002651 if (b->gbm)
2652 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002653
Giulio Camuffo954f1832014-10-11 18:27:30 +03002654 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002655
Giulio Camuffo954f1832014-10-11 18:27:30 +03002656 close(b->drm.fd);
Rob Bradford45c15b82013-07-26 16:29:35 +01002657
Giulio Camuffo954f1832014-10-11 18:27:30 +03002658 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002659}
2660
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002661static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002662drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002663{
2664 struct drm_output *output;
2665 struct drm_mode *drm_mode;
2666 int ret;
2667
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002669 if (!output->current) {
2670 /* If something that would cause the output to
2671 * switch mode happened while in another vt, we
2672 * might not have a current drm_fb. In that case,
2673 * schedule a repaint and let drm_output_repaint
2674 * handle setting the mode. */
2675 weston_output_schedule_repaint(&output->base);
2676 continue;
2677 }
2678
Hardeningff39efa2013-09-18 23:56:35 +02002679 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002680 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002681 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002682 &output->connector_id, 1,
2683 &drm_mode->mode_info);
2684 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002685 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002686 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002687 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002688 output->base.x, output->base.y);
2689 }
2690 }
2691}
2692
2693static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002694session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002695{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002696 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002697 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002698 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002699 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002700
Giulio Camuffo954f1832014-10-11 18:27:30 +03002701 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002702 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002703 compositor->state = b->prev_state;
2704 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002705 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002707 } else {
2708 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002709 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002710
Giulio Camuffo954f1832014-10-11 18:27:30 +03002711 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002712 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002713
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002714 /* If we have a repaint scheduled (either from a
2715 * pending pageflip or the idle handler), make sure we
2716 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002717 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002718 * further attemps at repainting. When we switch
2719 * back, we schedule a repaint, which will process
2720 * pending frame callbacks. */
2721
Giulio Camuffo954f1832014-10-11 18:27:30 +03002722 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002723 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002724 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002725 }
2726
Giulio Camuffo954f1832014-10-11 18:27:30 +03002727 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002728 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002729
Giulio Camuffo954f1832014-10-11 18:27:30 +03002730 wl_list_for_each(sprite, &b->sprite_list, link)
2731 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002732 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002733 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002734 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002735 };
2736}
2737
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002738static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002739switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002740{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002741 struct weston_compositor *compositor = data;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002742
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002743 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002744}
2745
David Herrmann0af066f2012-10-29 19:21:16 +01002746/*
2747 * Find primary GPU
2748 * Some systems may have multiple DRM devices attached to a single seat. This
2749 * function loops over all devices and tries to find a PCI device with the
2750 * boot_vga sysfs attribute set to 1.
2751 * If no such device is found, the first DRM device reported by udev is used.
2752 */
2753static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002754find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002755{
2756 struct udev_enumerate *e;
2757 struct udev_list_entry *entry;
2758 const char *path, *device_seat, *id;
2759 struct udev_device *device, *drm_device, *pci;
2760
Giulio Camuffo954f1832014-10-11 18:27:30 +03002761 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002762 udev_enumerate_add_match_subsystem(e, "drm");
2763 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2764
2765 udev_enumerate_scan_devices(e);
2766 drm_device = NULL;
2767 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2768 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002769 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002770 if (!device)
2771 continue;
2772 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2773 if (!device_seat)
2774 device_seat = default_seat;
2775 if (strcmp(device_seat, seat)) {
2776 udev_device_unref(device);
2777 continue;
2778 }
2779
2780 pci = udev_device_get_parent_with_subsystem_devtype(device,
2781 "pci", NULL);
2782 if (pci) {
2783 id = udev_device_get_sysattr_value(pci, "boot_vga");
2784 if (id && !strcmp(id, "1")) {
2785 if (drm_device)
2786 udev_device_unref(drm_device);
2787 drm_device = device;
2788 break;
2789 }
2790 }
2791
2792 if (!drm_device)
2793 drm_device = device;
2794 else
2795 udev_device_unref(device);
2796 }
2797
2798 udev_enumerate_unref(e);
2799 return drm_device;
2800}
2801
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002802static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002803planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002804{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002805 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002806
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002807 switch (key) {
2808 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002809 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002810 break;
2811 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002812 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002813 break;
2814 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002815 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002816 break;
2817 default:
2818 break;
2819 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002820}
2821
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002822#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002823static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002824recorder_destroy(struct drm_output *output)
2825{
2826 vaapi_recorder_destroy(output->recorder);
2827 output->recorder = NULL;
2828
2829 output->base.disable_planes--;
2830
2831 wl_list_remove(&output->recorder_frame_listener.link);
2832 weston_log("[libva recorder] done\n");
2833}
2834
2835static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002836recorder_frame_notify(struct wl_listener *listener, void *data)
2837{
2838 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002839 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002840 int fd, ret;
2841
2842 output = container_of(listener, struct drm_output,
2843 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002844 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002845
2846 if (!output->recorder)
2847 return;
2848
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002850 DRM_CLOEXEC, &fd);
2851 if (ret) {
2852 weston_log("[libva recorder] "
2853 "failed to create prime fd for front buffer\n");
2854 return;
2855 }
2856
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002857 ret = vaapi_recorder_frame(output->recorder, fd,
2858 output->current->stride);
2859 if (ret < 0) {
2860 weston_log("[libva recorder] aborted: %m\n");
2861 recorder_destroy(output);
2862 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002863}
2864
2865static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002866create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002867 const char *filename)
2868{
2869 int fd;
2870 drm_magic_t magic;
2871
Giulio Camuffo954f1832014-10-11 18:27:30 +03002872 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002873 if (fd < 0)
2874 return NULL;
2875
2876 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002877 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002878
2879 return vaapi_recorder_create(fd, width, height, filename);
2880}
2881
2882static void
2883recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2884 void *data)
2885{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002886 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002887 struct drm_output *output;
2888 int width, height;
2889
Giulio Camuffo954f1832014-10-11 18:27:30 +03002890 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002891 struct drm_output, base.link);
2892
2893 if (!output->recorder) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002894 if (output->format != GBM_FORMAT_XRGB8888) {
2895 weston_log("failed to start vaapi recorder: "
2896 "output format not supported\n");
2897 return;
2898 }
2899
Hardeningff39efa2013-09-18 23:56:35 +02002900 width = output->base.current_mode->width;
2901 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002902
2903 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002904 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002905 if (!output->recorder) {
2906 weston_log("failed to create vaapi recorder\n");
2907 return;
2908 }
2909
2910 output->base.disable_planes++;
2911
2912 output->recorder_frame_listener.notify = recorder_frame_notify;
2913 wl_signal_add(&output->base.frame_signal,
2914 &output->recorder_frame_listener);
2915
2916 weston_output_schedule_repaint(&output->base);
2917
2918 weston_log("[libva recorder] initialized\n");
2919 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002920 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002921 }
2922}
2923#else
2924static void
2925recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2926 void *data)
2927{
2928 weston_log("Compiled without libva support\n");
2929}
2930#endif
2931
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002932static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002933switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002934{
2935 struct drm_output *output;
2936
Giulio Camuffo954f1832014-10-11 18:27:30 +03002937 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002938 return;
2939
2940 weston_log("Switching to GL renderer\n");
2941
Giulio Camuffo954f1832014-10-11 18:27:30 +03002942 b->gbm = create_gbm_device(b->drm.fd);
2943 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002944 weston_log("Failed to create gbm device. "
2945 "Aborting renderer switch\n");
2946 return;
2947 }
2948
Giulio Camuffo954f1832014-10-11 18:27:30 +03002949 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002950 pixman_renderer_output_destroy(&output->base);
2951
Giulio Camuffo954f1832014-10-11 18:27:30 +03002952 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002953
Giulio Camuffo954f1832014-10-11 18:27:30 +03002954 if (drm_backend_create_gl_renderer(b) < 0) {
2955 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002956 weston_log("Failed to create GL renderer. Quitting.\n");
2957 /* FIXME: we need a function to shutdown cleanly */
2958 assert(0);
2959 }
2960
Giulio Camuffo954f1832014-10-11 18:27:30 +03002961 wl_list_for_each(output, &b->compositor->output_list, base.link)
2962 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002963
Giulio Camuffo954f1832014-10-11 18:27:30 +03002964 b->use_pixman = 0;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002965}
2966
2967static void
2968renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2969 void *data)
2970{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002971 struct drm_backend *b = (struct drm_backend *)seat->compositor->backend;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002972
Giulio Camuffo954f1832014-10-11 18:27:30 +03002973 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002974}
2975
Giulio Camuffo954f1832014-10-11 18:27:30 +03002976static struct drm_backend *
2977drm_backend_create(struct weston_compositor *compositor,
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07002978 struct drm_parameters *param,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002979 int *argc, char *argv[],
2980 struct weston_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002981{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002982 struct drm_backend *b;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07002983 struct weston_config_section *section;
David Herrmann0af066f2012-10-29 19:21:16 +01002984 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002985 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01002986 const char *path;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002987 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002988
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002989 weston_log("initializing drm backend\n");
2990
Giulio Camuffo954f1832014-10-11 18:27:30 +03002991 b = zalloc(sizeof *b);
2992 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002993 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01002994
Pekka Paalanen68583832015-05-19 09:53:16 +03002995 /*
2996 * KMS support for hardware planes cannot properly synchronize
2997 * without nuclear page flip. Without nuclear/atomic, hw plane
2998 * and cursor plane updates would either tear or cause extra
2999 * waits for vblanks which means dropping the compositor framerate
3000 * to a fraction.
3001 *
3002 * These can be enabled again when nuclear/atomic support lands.
3003 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003004 b->sprites_are_broken = 1;
3005 b->cursors_are_broken = 1;
3006 b->compositor = compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003007
3008 section = weston_config_get_section(config, "core", NULL, NULL);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003009 if (get_gbm_format_from_section(section,
3010 GBM_FORMAT_XRGB8888,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003011 &b->format) == -1)
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003012 goto err_base;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003013
Giulio Camuffo954f1832014-10-11 18:27:30 +03003014 b->use_pixman = param->use_pixman;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003015
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003016 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003017 compositor->launcher = weston_launcher_connect(compositor, param->tty,
3018 param->seat_id, true);
3019 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003020 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003021 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003022 goto err_compositor;
3023 }
3024
Giulio Camuffo954f1832014-10-11 18:27:30 +03003025 b->udev = udev_new();
3026 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003027 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003028 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003029 }
3030
Giulio Camuffo954f1832014-10-11 18:27:30 +03003031 b->session_listener.notify = session_notify;
3032 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003033
Giulio Camuffo954f1832014-10-11 18:27:30 +03003034 drm_device = find_primary_gpu(b, param->seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003035 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003036 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003037 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003038 }
David Herrmann0af066f2012-10-29 19:21:16 +01003039 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003040
Giulio Camuffo954f1832014-10-11 18:27:30 +03003041 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003042 weston_log("failed to initialize kms\n");
3043 goto err_udev_dev;
3044 }
3045
Giulio Camuffo954f1832014-10-11 18:27:30 +03003046 if (b->use_pixman) {
3047 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003048 weston_log("failed to initialize pixman renderer\n");
3049 goto err_udev_dev;
3050 }
3051 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003052 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003053 weston_log("failed to initialize egl\n");
3054 goto err_udev_dev;
3055 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003056 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003057
Giulio Camuffo954f1832014-10-11 18:27:30 +03003058 b->base.destroy = drm_destroy;
3059 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003060
Giulio Camuffo954f1832014-10-11 18:27:30 +03003061 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003062
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003063 for (key = KEY_F1; key < KEY_F9; key++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003064 weston_compositor_add_key_binding(compositor, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003065 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003066 switch_vt_binding, compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003067
Giulio Camuffo954f1832014-10-11 18:27:30 +03003068 wl_list_init(&b->sprite_list);
3069 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003070
Giulio Camuffo954f1832014-10-11 18:27:30 +03003071 if (udev_input_init(&b->input,
3072 compositor, b->udev, param->seat_id) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003073 weston_log("failed to create input devices\n");
3074 goto err_sprite;
3075 }
3076
Giulio Camuffo954f1832014-10-11 18:27:30 +03003077 if (create_outputs(b, param->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003078 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003079 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003080 }
3081
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003082 /* A this point we have some idea of whether or not we have a working
3083 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003084 if (!b->cursors_are_broken)
3085 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003086
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003087 path = NULL;
3088
Giulio Camuffo954f1832014-10-11 18:27:30 +03003089 loop = wl_display_get_event_loop(compositor->wl_display);
3090 b->drm_source =
3091 wl_event_loop_add_fd(loop, b->drm.fd,
3092 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003093
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3095 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003096 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003097 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003098 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003099 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003100 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003101 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003102 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003103 udev_monitor_get_fd(b->udev_monitor),
3104 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003105
Giulio Camuffo954f1832014-10-11 18:27:30 +03003106 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003107 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003108 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003109 }
3110
Daniel Stonea96b93c2012-06-22 14:04:37 +01003111 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003112
Giulio Camuffo954f1832014-10-11 18:27:30 +03003113 weston_compositor_add_debug_binding(compositor, KEY_O,
3114 planes_binding, b);
3115 weston_compositor_add_debug_binding(compositor, KEY_C,
3116 planes_binding, b);
3117 weston_compositor_add_debug_binding(compositor, KEY_V,
3118 planes_binding, b);
3119 weston_compositor_add_debug_binding(compositor, KEY_Q,
3120 recorder_binding, b);
3121 weston_compositor_add_debug_binding(compositor, KEY_W,
3122 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003123
Giulio Camuffo954f1832014-10-11 18:27:30 +03003124 compositor->backend = &b->base;
3125 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003126
3127err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003128 wl_event_source_remove(b->udev_drm_source);
3129 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003130err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003131 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003132err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003133 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003134err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003135 compositor->renderer->destroy(compositor);
3136 gbm_device_destroy(b->gbm);
3137 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003138err_udev_dev:
3139 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003140err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003142err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003143 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003144err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003145 weston_compositor_shutdown(compositor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003146err_base:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003147 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003148 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003149}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003150
Giulio Camuffo954f1832014-10-11 18:27:30 +03003151WL_EXPORT int
3152backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003153 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003154{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003155 struct drm_backend *b;
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003156 struct drm_parameters param = { 0, };
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003157
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003158 const struct weston_option drm_options[] = {
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003159 { WESTON_OPTION_INTEGER, "connector", 0, &param.connector },
3160 { WESTON_OPTION_STRING, "seat", 0, &param.seat_id },
3161 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003162 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003163 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003164 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02003165
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003166 param.seat_id = default_seat;
3167
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003168 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003169
Giulio Camuffo954f1832014-10-11 18:27:30 +03003170 b = drm_backend_create(compositor, &param, argc, argv, config);
3171 if (b == NULL)
3172 return -1;
3173 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003174}