blob: db498a63726af2ad0d9a17a76c891e0a08fd4390 [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
Daniel Stone70d337d2015-06-16 18:42:23 +0100130 int32_t cursor_width;
131 int32_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) ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001084 ev->surface->width > b->cursor_width ||
1085 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001086 return NULL;
1087
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001089
1090 return &output->cursor_plane;
1091}
1092
Pekka Paalanend0ead482014-06-16 12:05:40 +03001093/**
1094 * Update the image for the current cursor surface
1095 *
1096 * @param b DRM backend structure
1097 * @param bo GBM buffer object to write into
1098 * @param ev View to use for cursor image
1099 */
1100static void
1101cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1102 struct weston_view *ev)
1103{
1104 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1105 uint32_t buf[b->cursor_width * b->cursor_height];
1106 int32_t stride;
1107 uint8_t *s;
1108 int i;
1109
1110 assert(buffer && buffer->shm_buffer);
1111 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1112 assert(ev->surface->width <= b->cursor_width);
1113 assert(ev->surface->height <= b->cursor_height);
1114
1115 memset(buf, 0, sizeof buf);
1116 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1117 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1118
1119 wl_shm_buffer_begin_access(buffer->shm_buffer);
1120 for (i = 0; i < ev->surface->height; i++)
1121 memcpy(buf + i * b->cursor_width,
1122 s + i * stride,
1123 ev->surface->width * 4);
1124 wl_shm_buffer_end_access(buffer->shm_buffer);
1125
1126 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1127 weston_log("failed update cursor: %m\n");
1128}
1129
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001130static void
1131drm_output_set_cursor(struct drm_output *output)
1132{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001134 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001135 struct drm_backend *b =
1136 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001137 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001138 struct gbm_bo *bo;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001139 int x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 output->cursor_view = NULL;
1142 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001143 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001144 return;
1145 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001146
Neil Robertse5051712013-11-13 15:44:06 +00001147 buffer = ev->surface->buffer_ref.buffer;
1148
1149 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001150 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001151 pixman_region32_fini(&output->cursor_plane.damage);
1152 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001153 output->current_cursor ^= 1;
1154 bo = output->cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001155
Pekka Paalanend0ead482014-06-16 12:05:40 +03001156 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001157 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001158 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1159 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001160 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001161 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001162 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001163 }
1164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 x = (ev->geometry.x - output->base.x) * output->base.current_scale;
1166 y = (ev->geometry.y - output->base.y) * output->base.current_scale;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001167 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001168 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001169 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001170 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001171 }
1172
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001173 output->cursor_plane.x = x;
1174 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001175 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001176}
1177
Jesse Barnes58ef3792012-02-23 09:45:49 -05001178static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001179drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001180{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001181 struct drm_backend *b =
1182 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001183 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001185 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001186 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001187
1188 /*
1189 * Find a surface for each sprite in the output using some heuristics:
1190 * 1) size
1191 * 2) frequency of update
1192 * 3) opacity (though some hw might support alpha blending)
1193 * 4) clipping (this can be fixed with color keys)
1194 *
1195 * The idea is to save on blitting since this should save power.
1196 * If we can get a large video surface on the sprite for example,
1197 * the main display surface may not need to update at all, and
1198 * the client buffer can be used directly for the sprite surface
1199 * as we do for flipping full screen surfaces.
1200 */
1201 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203
Giulio Camuffo954f1832014-10-11 18:27:30 +03001204 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001205 struct weston_surface *es = ev->surface;
1206
1207 /* Test whether this buffer can ever go into a plane:
1208 * non-shm, or small enough to be a cursor.
1209 *
1210 * Also, keep a reference when using the pixman renderer.
1211 * That makes it possible to do a seamless switch to the GL
1212 * renderer and since the pixman renderer keeps a reference
1213 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001214 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001215 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001216 (es->buffer_ref.buffer &&
1217 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001218 (ev->surface->width <= 64 && ev->surface->height <= 64))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001219 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001220 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001221 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001222
Jesse Barnes58ef3792012-02-23 09:45:49 -05001223 pixman_region32_init(&surface_overlap);
1224 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001226
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001227 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001228 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001229 next_plane = primary;
1230 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001232 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001234 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001236 if (next_plane == NULL)
1237 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001240
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001241 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001242 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001244
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001245 if (next_plane == primary ||
1246 next_plane == &output->cursor_plane) {
1247 /* cursor plane involves a copy */
1248 ev->psf_flags = 0;
1249 } else {
1250 /* All other planes are a direct scanout of a
1251 * single client buffer.
1252 */
1253 ev->psf_flags = PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
1254 }
1255
Jesse Barnes58ef3792012-02-23 09:45:49 -05001256 pixman_region32_fini(&surface_overlap);
1257 }
1258 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001259}
1260
Matt Roper361d2ad2011-08-29 13:52:23 -07001261static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001262drm_output_fini_pixman(struct drm_output *output);
1263
1264static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001265drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001266{
1267 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001268 struct drm_backend *b =
1269 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001270 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001271
Xiong Zhangabd5d472013-10-11 14:43:07 +08001272 if (output->page_flip_pending) {
1273 output->destroy_pending = 1;
1274 weston_log("destroy output while page flip pending\n");
1275 return;
1276 }
1277
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001278 if (output->backlight)
1279 backlight_destroy(output->backlight);
1280
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001281 drmModeFreeProperty(output->dpms_prop);
1282
Matt Roper361d2ad2011-08-29 13:52:23 -07001283 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001284 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001285
1286 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001287 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001288 origcrtc->x, origcrtc->y,
1289 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001290 drmModeFreeCrtc(origcrtc);
1291
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 b->crtc_allocator &= ~(1 << output->crtc_id);
1293 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001294
Giulio Camuffo954f1832014-10-11 18:27:30 +03001295 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001296 drm_output_fini_pixman(output);
1297 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001298 gl_renderer->output_destroy(output_base);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001299 gbm_surface_destroy(output->surface);
1300 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001301
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001302 weston_plane_release(&output->fb_plane);
1303 weston_plane_release(&output->cursor_plane);
1304
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001305 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001306
Matt Roper361d2ad2011-08-29 13:52:23 -07001307 free(output);
1308}
1309
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001310/**
1311 * Find the closest-matching mode for a given target
1312 *
1313 * Given a target mode, find the most suitable mode amongst the output's
1314 * current mode list to use, preferring the current mode if possible, to
1315 * avoid an expensive mode switch.
1316 *
1317 * @param output DRM output
1318 * @param target_mode Mode to attempt to match
1319 * @returns Pointer to a mode from the output's mode list
1320 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001321static struct drm_mode *
1322choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1323{
1324 struct drm_mode *tmp_mode = NULL, *mode;
1325
Hardeningff39efa2013-09-18 23:56:35 +02001326 if (output->base.current_mode->width == target_mode->width &&
1327 output->base.current_mode->height == target_mode->height &&
1328 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001329 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001330 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001331
1332 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1333 if (mode->mode_info.hdisplay == target_mode->width &&
1334 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001335 if (mode->base.refresh == target_mode->refresh ||
1336 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001337 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001338 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001339 tmp_mode = mode;
1340 }
1341 }
1342
1343 return tmp_mode;
1344}
1345
1346static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001347drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001348static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001349drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001350
1351static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001352drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1353{
1354 struct drm_output *output;
1355 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001356 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001357
1358 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001359 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001360 return -1;
1361 }
1362
1363 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001364 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001365 return -1;
1366 }
1367
Giulio Camuffo954f1832014-10-11 18:27:30 +03001368 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001369 output = (struct drm_output *)output_base;
1370 drm_mode = choose_mode (output, mode);
1371
1372 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001373 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001374 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001375 }
1376
Hardeningff39efa2013-09-18 23:56:35 +02001377 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001378 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001379
Hardeningff39efa2013-09-18 23:56:35 +02001380 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001381
Hardeningff39efa2013-09-18 23:56:35 +02001382 output->base.current_mode = &drm_mode->base;
1383 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001384 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1385
Alex Wub7b8bda2012-04-17 17:20:48 +08001386 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001387 drm_output_release_fb(output, output->current);
1388 drm_output_release_fb(output, output->next);
1389 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001390
Giulio Camuffo954f1832014-10-11 18:27:30 +03001391 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001392 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001393 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001394 weston_log("failed to init output pixman state with "
1395 "new mode\n");
1396 return -1;
1397 }
1398 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001399 gl_renderer->output_destroy(&output->base);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001400 gbm_surface_destroy(output->surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001401
Giulio Camuffo954f1832014-10-11 18:27:30 +03001402 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001403 weston_log("failed to init output egl state with "
1404 "new mode");
1405 return -1;
1406 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001407 }
1408
Alex Wub7b8bda2012-04-17 17:20:48 +08001409 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001410}
1411
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001412static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001413on_drm_input(int fd, uint32_t mask, void *data)
1414{
1415 drmEventContext evctx;
1416
1417 memset(&evctx, 0, sizeof evctx);
1418 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1419 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001420 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001421 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001422
1423 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001424}
1425
1426static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001427init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001428{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001429 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001430 uint64_t cap;
1431 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001432 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001433
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001434 sysnum = udev_device_get_sysnum(device);
1435 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 b->drm.id = atoi(sysnum);
1437 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001438 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001439 return -1;
1440 }
1441
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001442 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001443 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001444 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001445 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001446 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001447 udev_device_get_devnode(device));
1448 return -1;
1449 }
1450
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001451 weston_log("using %s\n", filename);
1452
Giulio Camuffo954f1832014-10-11 18:27:30 +03001453 b->drm.fd = fd;
1454 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001455
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001456 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1457 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001458 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001459 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001460 clk_id = CLOCK_REALTIME;
1461
Giulio Camuffo954f1832014-10-11 18:27:30 +03001462 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001463 weston_log("Error: failed to set presentation clock %d.\n",
1464 clk_id);
1465 return -1;
1466 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001467
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001468 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1469 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001470 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001471 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001472 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001473
1474 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1475 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001476 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001477 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001478 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001479
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001480 return 0;
1481}
1482
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001483static struct gbm_device *
1484create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001485{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001486 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001487
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001488 gl_renderer = weston_load_module("gl-renderer.so",
1489 "gl_renderer_interface");
1490 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001491 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001492
1493 /* GBM will load a dri driver, but even though they need symbols from
1494 * libglapi, in some version of Mesa they are not linked to it. Since
1495 * only the gl-renderer module links to it, the call above won't make
1496 * these symbols globally available, and loading the DRI driver fails.
1497 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1498 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1499
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001500 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001501
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001502 return gbm;
1503}
1504
Bryce Harringtonc056a982015-05-19 15:25:18 -07001505/* When initializing EGL, if the preferred buffer format isn't available
Derek Foremanc4cfe852015-05-15 12:12:40 -05001506 * we may be able to susbstitute an ARGB format for an XRGB one.
1507 *
1508 * This returns 0 if substitution isn't possible, but 0 might be a
1509 * legitimate format for other EGL platforms, so the caller is
1510 * responsible for checking for 0 before calling gl_renderer->create().
1511 *
1512 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1513 * but it's entirely possible we'll see this again on other implementations.
1514 */
1515static int
1516fallback_format_for(uint32_t format)
1517{
1518 switch (format) {
1519 case GBM_FORMAT_XRGB8888:
1520 return GBM_FORMAT_ARGB8888;
1521 case GBM_FORMAT_XRGB2101010:
1522 return GBM_FORMAT_ARGB2101010;
1523 default:
1524 return 0;
1525 }
1526}
1527
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001528static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001529drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001530{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001531 EGLint format[2] = {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001532 b->format,
1533 fallback_format_for(b->format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001534 };
1535 int n_formats = 1;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001536
Derek Foremanc4cfe852015-05-15 12:12:40 -05001537 if (format[1])
1538 n_formats = 2;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001539 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001540 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001541 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001542 gl_renderer->opaque_attribs,
1543 format,
1544 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001545 return -1;
1546 }
1547
1548 return 0;
1549}
1550
1551static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001553{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001554 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001555
Giulio Camuffo954f1832014-10-11 18:27:30 +03001556 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001557 return -1;
1558
Giulio Camuffo954f1832014-10-11 18:27:30 +03001559 if (drm_backend_create_gl_renderer(b) < 0) {
1560 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001561 return -1;
1562 }
1563
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001564 return 0;
1565}
1566
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001567static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001568init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001569{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001570 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001571}
1572
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001573/**
1574 * Add a mode to output's mode list
1575 *
1576 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1577 * output's mode list.
1578 *
1579 * @param output DRM output to add mode to
1580 * @param info DRM mode structure to add
1581 * @returns Newly-allocated Weston/DRM mode structure
1582 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001583static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001584drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001585{
1586 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001587 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001588
1589 mode = malloc(sizeof *mode);
1590 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001591 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001592
1593 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001594 mode->base.width = info->hdisplay;
1595 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001596
1597 /* Calculate higher precision (mHz) refresh rate */
1598 refresh = (info->clock * 1000000LL / info->htotal +
1599 info->vtotal / 2) / info->vtotal;
1600
1601 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1602 refresh *= 2;
1603 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1604 refresh /= 2;
1605 if (info->vscan > 1)
1606 refresh /= info->vscan;
1607
1608 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001609 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001610
1611 if (info->type & DRM_MODE_TYPE_PREFERRED)
1612 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1613
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001614 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1615
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001616 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001617}
1618
1619static int
1620drm_subpixel_to_wayland(int drm_value)
1621{
1622 switch (drm_value) {
1623 default:
1624 case DRM_MODE_SUBPIXEL_UNKNOWN:
1625 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1626 case DRM_MODE_SUBPIXEL_NONE:
1627 return WL_OUTPUT_SUBPIXEL_NONE;
1628 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1629 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1630 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1631 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1632 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1633 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1634 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1635 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1636 }
1637}
1638
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001639/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001640static uint32_t
1641drm_get_backlight(struct drm_output *output)
1642{
1643 long brightness, max_brightness, norm;
1644
1645 brightness = backlight_get_brightness(output->backlight);
1646 max_brightness = backlight_get_max_brightness(output->backlight);
1647
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001648 /* convert it on a scale of 0 to 255 */
1649 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001650
1651 return (uint32_t) norm;
1652}
1653
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001654/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001655static void
1656drm_set_backlight(struct weston_output *output_base, uint32_t value)
1657{
1658 struct drm_output *output = (struct drm_output *) output_base;
1659 long max_brightness, new_brightness;
1660
1661 if (!output->backlight)
1662 return;
1663
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001664 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001665 return;
1666
1667 max_brightness = backlight_get_max_brightness(output->backlight);
1668
1669 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001670 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001671
1672 backlight_set_brightness(output->backlight, new_brightness);
1673}
1674
1675static drmModePropertyPtr
1676drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1677{
1678 drmModePropertyPtr props;
1679 int i;
1680
1681 for (i = 0; i < connector->count_props; i++) {
1682 props = drmModeGetProperty(fd, connector->props[i]);
1683 if (!props)
1684 continue;
1685
1686 if (!strcmp(props->name, name))
1687 return props;
1688
1689 drmModeFreeProperty(props);
1690 }
1691
1692 return NULL;
1693}
1694
1695static void
1696drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1697{
1698 struct drm_output *output = (struct drm_output *) output_base;
1699 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001700 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001701 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001702
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001703 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001704 return;
1705
Daniel Stone36609c72015-06-18 07:49:02 +01001706 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1707 output->dpms_prop->prop_id, level);
1708 if (ret) {
1709 weston_log("DRM: DPMS: failed property set for %s\n",
1710 output->base.name);
1711 return;
1712 }
1713
1714 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001715}
1716
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001717static const char * const connector_type_names[] = {
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001718 "None",
1719 "VGA",
1720 "DVI",
1721 "DVI",
1722 "DVI",
1723 "Composite",
1724 "TV",
1725 "LVDS",
1726 "CTV",
1727 "DIN",
1728 "DP",
1729 "HDMI",
1730 "HDMI",
1731 "TV",
1732 "eDP",
1733};
1734
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001735static char *
1736make_connector_name(const drmModeConnector *con)
1737{
1738 char name[32];
1739 const char *type_name;
1740
1741 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1742 type_name = connector_type_names[con->connector_type];
1743 else
1744 type_name = "UNKNOWN";
1745 snprintf(name, sizeof name, "%s%d", type_name, con->connector_type_id);
1746
1747 return strdup(name);
1748}
1749
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001750static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001751find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001752 drmModeRes *resources, drmModeConnector *connector)
1753{
1754 drmModeEncoder *encoder;
1755 uint32_t possible_crtcs;
1756 int i, j;
1757
1758 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001759 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001760 if (encoder == NULL) {
1761 weston_log("Failed to get encoder.\n");
1762 return -1;
1763 }
1764 possible_crtcs = encoder->possible_crtcs;
1765 drmModeFreeEncoder(encoder);
1766
1767 for (i = 0; i < resources->count_crtcs; i++) {
1768 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001769 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001770 return i;
1771 }
1772 }
1773
1774 return -1;
1775}
1776
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001777/* Init output state that depends on gl or gbm */
1778static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001779drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001780{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001781 EGLint format[2] = {
1782 output->format,
1783 fallback_format_for(output->format),
1784 };
1785 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001786
Giulio Camuffo954f1832014-10-11 18:27:30 +03001787 output->surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001788 output->base.current_mode->width,
1789 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001790 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001791 GBM_BO_USE_SCANOUT |
1792 GBM_BO_USE_RENDERING);
1793 if (!output->surface) {
1794 weston_log("failed to create gbm surface\n");
1795 return -1;
1796 }
1797
Derek Foremanc4cfe852015-05-15 12:12:40 -05001798 if (format[1])
1799 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001800 if (gl_renderer->output_create(&output->base,
Jonny Lamb445fb692015-03-24 13:12:01 +01001801 (EGLNativeDisplayType)output->surface,
1802 output->surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001803 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001804 format,
1805 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001806 weston_log("failed to create gl renderer output state\n");
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001807 gbm_surface_destroy(output->surface);
1808 return -1;
1809 }
1810
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001811 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001812
1813 for (i = 0; i < 2; i++) {
1814 if (output->cursor_bo[i])
1815 continue;
1816
1817 output->cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001818 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001819 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001820 }
1821
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001822 if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) {
1823 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001824 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001825 }
1826
1827 return 0;
1828}
1829
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001830static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001831drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001832{
Hardeningff39efa2013-09-18 23:56:35 +02001833 int w = output->base.current_mode->width;
1834 int h = output->base.current_mode->height;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001835 unsigned int i;
1836
1837 /* FIXME error checking */
1838
1839 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001840 output->dumb[i] = drm_fb_create_dumb(b, w, h);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001841 if (!output->dumb[i])
1842 goto err;
1843
1844 output->image[i] =
Alexander Larsson0b135062013-05-28 16:23:36 +02001845 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001846 output->dumb[i]->map,
1847 output->dumb[i]->stride);
1848 if (!output->image[i])
1849 goto err;
1850 }
1851
1852 if (pixman_renderer_output_create(&output->base) < 0)
1853 goto err;
1854
1855 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001856 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001857
1858 return 0;
1859
1860err:
1861 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1862 if (output->dumb[i])
1863 drm_fb_destroy_dumb(output->dumb[i]);
1864 if (output->image[i])
1865 pixman_image_unref(output->image[i]);
1866
1867 output->dumb[i] = NULL;
1868 output->image[i] = NULL;
1869 }
1870
1871 return -1;
1872}
1873
1874static void
1875drm_output_fini_pixman(struct drm_output *output)
1876{
1877 unsigned int i;
1878
1879 pixman_renderer_output_destroy(&output->base);
1880 pixman_region32_fini(&output->previous_damage);
1881
1882 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1883 drm_fb_destroy_dumb(output->dumb[i]);
1884 pixman_image_unref(output->image[i]);
1885 output->dumb[i] = NULL;
1886 output->image[i] = NULL;
1887 }
1888}
1889
Richard Hughes2b2092a2013-04-24 14:58:02 +01001890static void
1891edid_parse_string(const uint8_t *data, char text[])
1892{
1893 int i;
1894 int replaced = 0;
1895
1896 /* this is always 12 bytes, but we can't guarantee it's null
1897 * terminated or not junk. */
1898 strncpy(text, (const char *) data, 12);
1899
1900 /* remove insane chars */
1901 for (i = 0; text[i] != '\0'; i++) {
1902 if (text[i] == '\n' ||
1903 text[i] == '\r') {
1904 text[i] = '\0';
1905 break;
1906 }
1907 }
1908
1909 /* ensure string is printable */
1910 for (i = 0; text[i] != '\0'; i++) {
1911 if (!isprint(text[i])) {
1912 text[i] = '-';
1913 replaced++;
1914 }
1915 }
1916
1917 /* if the string is random junk, ignore the string */
1918 if (replaced > 4)
1919 text[0] = '\0';
1920}
1921
1922#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1923#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1924#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1925#define EDID_OFFSET_DATA_BLOCKS 0x36
1926#define EDID_OFFSET_LAST_BLOCK 0x6c
1927#define EDID_OFFSET_PNPID 0x08
1928#define EDID_OFFSET_SERIAL 0x0c
1929
1930static int
1931edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1932{
1933 int i;
1934 uint32_t serial_number;
1935
1936 /* check header */
1937 if (length < 128)
1938 return -1;
1939 if (data[0] != 0x00 || data[1] != 0xff)
1940 return -1;
1941
1942 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1943 * /--08--\/--09--\
1944 * 7654321076543210
1945 * |\---/\---/\---/
1946 * R C1 C2 C3 */
1947 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1948 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1949 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1950 edid->pnp_id[3] = '\0';
1951
1952 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1953 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1954 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1955 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1956 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1957 if (serial_number > 0)
1958 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
1959
1960 /* parse EDID data */
1961 for (i = EDID_OFFSET_DATA_BLOCKS;
1962 i <= EDID_OFFSET_LAST_BLOCK;
1963 i += 18) {
1964 /* ignore pixel clock data */
1965 if (data[i] != 0)
1966 continue;
1967 if (data[i+2] != 0)
1968 continue;
1969
1970 /* any useful blocks? */
1971 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
1972 edid_parse_string(&data[i+5],
1973 edid->monitor_name);
1974 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
1975 edid_parse_string(&data[i+5],
1976 edid->serial_number);
1977 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
1978 edid_parse_string(&data[i+5],
1979 edid->eisa_id);
1980 }
1981 }
1982 return 0;
1983}
1984
1985static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03001986find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01001987 struct drm_output *output,
1988 drmModeConnector *connector)
1989{
1990 drmModePropertyBlobPtr edid_blob = NULL;
1991 drmModePropertyPtr property;
1992 int i;
1993 int rc;
1994
1995 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001996 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01001997 if (!property)
1998 continue;
1999 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2000 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002001 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002002 connector->prop_values[i]);
2003 }
2004 drmModeFreeProperty(property);
2005 }
2006 if (!edid_blob)
2007 return;
2008
2009 rc = edid_parse(&output->edid,
2010 edid_blob->data,
2011 edid_blob->length);
2012 if (!rc) {
2013 weston_log("EDID data '%s', '%s', '%s'\n",
2014 output->edid.pnp_id,
2015 output->edid.monitor_name,
2016 output->edid.serial_number);
2017 if (output->edid.pnp_id[0] != '\0')
2018 output->base.make = output->edid.pnp_id;
2019 if (output->edid.monitor_name[0] != '\0')
2020 output->base.model = output->edid.monitor_name;
2021 if (output->edid.serial_number[0] != '\0')
2022 output->base.serial_number = output->edid.serial_number;
2023 }
2024 drmModeFreePropertyBlob(edid_blob);
2025}
2026
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002027
2028
2029static int
2030parse_modeline(const char *s, drmModeModeInfo *mode)
2031{
2032 char hsync[16];
2033 char vsync[16];
2034 float fclock;
2035
2036 mode->type = DRM_MODE_TYPE_USERDEF;
2037 mode->hskew = 0;
2038 mode->vscan = 0;
2039 mode->vrefresh = 0;
2040 mode->flags = 0;
2041
Rob Bradford307e09e2013-07-26 16:29:40 +01002042 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002043 &fclock,
2044 &mode->hdisplay,
2045 &mode->hsync_start,
2046 &mode->hsync_end,
2047 &mode->htotal,
2048 &mode->vdisplay,
2049 &mode->vsync_start,
2050 &mode->vsync_end,
2051 &mode->vtotal, hsync, vsync) != 11)
2052 return -1;
2053
2054 mode->clock = fclock * 1000;
2055 if (strcmp(hsync, "+hsync") == 0)
2056 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2057 else if (strcmp(hsync, "-hsync") == 0)
2058 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2059 else
2060 return -1;
2061
2062 if (strcmp(vsync, "+vsync") == 0)
2063 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2064 else if (strcmp(vsync, "-vsync") == 0)
2065 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2066 else
2067 return -1;
2068
2069 return 0;
2070}
2071
Rob Bradford66bd9f52013-06-25 18:56:42 +01002072static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002073setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002074 struct weston_output *output,
2075 const char *s)
2076{
2077 if (strcmp(s, "") != 0) {
2078 struct udev_seat *seat;
2079
Giulio Camuffo954f1832014-10-11 18:27:30 +03002080 seat = udev_seat_get_named(&b->input, s);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002081 if (seat)
2082 seat->base.output = output;
2083
2084 if (seat && seat->base.pointer)
2085 weston_pointer_clamp(seat->base.pointer,
2086 &seat->base.pointer->x,
2087 &seat->base.pointer->y);
2088 }
2089}
2090
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002091static int
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002092get_gbm_format_from_section(struct weston_config_section *section,
2093 uint32_t default_value,
2094 uint32_t *format)
2095{
2096 char *s;
2097 int ret = 0;
2098
2099 weston_config_section_get_string(section,
2100 "gbm-format", &s, NULL);
2101
2102 if (s == NULL)
2103 *format = default_value;
2104 else if (strcmp(s, "xrgb8888") == 0)
2105 *format = GBM_FORMAT_XRGB8888;
2106 else if (strcmp(s, "rgb565") == 0)
2107 *format = GBM_FORMAT_RGB565;
2108 else if (strcmp(s, "xrgb2101010") == 0)
2109 *format = GBM_FORMAT_XRGB2101010;
2110 else {
2111 weston_log("fatal: unrecognized pixel format: %s\n", s);
2112 ret = -1;
2113 }
2114
2115 free(s);
2116
2117 return ret;
2118}
2119
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002120/**
2121 * Choose suitable mode for an output
2122 *
2123 * Find the most suitable mode to use for initial setup (or reconfiguration on
2124 * hotplug etc) for a DRM output.
2125 *
2126 * @param output DRM output to choose mode for
2127 * @param kind Strategy and preference to use when choosing mode
2128 * @param width Desired width for this output
2129 * @param height Desired height for this output
2130 * @param current_mode Mode currently being displayed on this output
2131 * @param modeline Manually-entered mode (may be NULL)
2132 * @returns A mode from the output's mode list, or NULL if none available
2133 */
2134static struct drm_mode *
2135drm_output_choose_initial_mode(struct drm_output *output,
2136 enum output_config kind,
2137 int width, int height,
2138 const drmModeModeInfo *current_mode,
2139 const drmModeModeInfo *modeline)
2140{
2141 struct drm_mode *preferred = NULL;
2142 struct drm_mode *current = NULL;
2143 struct drm_mode *configured = NULL;
2144 struct drm_mode *best = NULL;
2145 struct drm_mode *drm_mode;
2146
2147 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
2148 if (kind == OUTPUT_CONFIG_MODE &&
2149 width == drm_mode->base.width &&
2150 height == drm_mode->base.height)
2151 configured = drm_mode;
2152
2153 if (memcmp(&current_mode, &drm_mode->mode_info,
2154 sizeof *current_mode) == 0)
2155 current = drm_mode;
2156
2157 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2158 preferred = drm_mode;
2159
2160 best = drm_mode;
2161 }
2162
2163 if (kind == OUTPUT_CONFIG_MODELINE) {
2164 configured = drm_output_add_mode(output, modeline);
2165 if (!configured)
2166 return NULL;
2167 }
2168
2169 if (current == NULL && current_mode->clock != 0) {
2170 current = drm_output_add_mode(output, current_mode);
2171 if (!current)
2172 return NULL;
2173 }
2174
2175 if (kind == OUTPUT_CONFIG_CURRENT)
2176 configured = current;
2177
2178 if (option_current_mode && current)
2179 return current;
2180
2181 if (configured)
2182 return configured;
2183
2184 if (preferred)
2185 return preferred;
2186
2187 if (current)
2188 return current;
2189
2190 if (best)
2191 return best;
2192
2193 weston_log("no available modes for %s\n", output->base.name);
2194 return NULL;
2195}
2196
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002197static int
2198connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2199 drmModeModeInfo *mode)
2200{
2201 drmModeEncoder *encoder;
2202 drmModeCrtc *crtc;
2203
2204 /* Get the current mode on the crtc that's currently driving
2205 * this connector. */
2206 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2207 memset(mode, 0, sizeof *mode);
2208 if (encoder != NULL) {
2209 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2210 drmModeFreeEncoder(encoder);
2211 if (crtc == NULL)
2212 return -1;
2213 if (crtc->mode_valid)
2214 *mode = crtc->mode;
2215 drmModeFreeCrtc(crtc);
2216 }
2217
2218 return 0;
2219}
2220
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002221/**
2222 * Create and configure a Weston output structure
2223 *
2224 * Given a DRM connector, create a matching drm_output structure and add it
2225 * to Weston's output list.
2226 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002227 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002228 * @param resources DRM resources for this device
2229 * @param connector DRM connector to use for this new output
2230 * @param x Horizontal offset to use into global co-ordinate space
2231 * @param y Vertical offset to use into global co-ordinate space
2232 * @param drm_device udev device pointer
2233 * @returns 0 on success, or -1 on failure
2234 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002235static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002236create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002237 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002238 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002239 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002240{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002241 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002242 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002243 struct weston_mode *m;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002244 struct weston_config_section *section;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002245 drmModeModeInfo crtc_mode, modeline;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002246 int i, width, height, scale;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002247 char *s;
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002248 enum output_config config;
2249 uint32_t transform;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002250
Giulio Camuffo954f1832014-10-11 18:27:30 +03002251 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002252 if (i < 0) {
2253 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002254 return -1;
2255 }
2256
Peter Huttererf3d62272013-08-08 11:57:05 +10002257 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002258 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002259 return -1;
2260
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002261 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002262 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002263 output->base.make = "unknown";
2264 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002265 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002266 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002267
Giulio Camuffo954f1832014-10-11 18:27:30 +03002268 section = weston_config_get_section(b->compositor->config, "output", "name",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002269 output->base.name);
2270 weston_config_section_get_string(section, "mode", &s, "preferred");
2271 if (strcmp(s, "off") == 0)
2272 config = OUTPUT_CONFIG_OFF;
2273 else if (strcmp(s, "preferred") == 0)
2274 config = OUTPUT_CONFIG_PREFERRED;
2275 else if (strcmp(s, "current") == 0)
2276 config = OUTPUT_CONFIG_CURRENT;
2277 else if (sscanf(s, "%dx%d", &width, &height) == 2)
2278 config = OUTPUT_CONFIG_MODE;
2279 else if (parse_modeline(s, &modeline) == 0)
2280 config = OUTPUT_CONFIG_MODELINE;
2281 else {
2282 weston_log("Invalid mode \"%s\" for output %s\n",
2283 s, output->base.name);
2284 config = OUTPUT_CONFIG_PREFERRED;
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002285 }
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002286 free(s);
2287
2288 weston_config_section_get_int(section, "scale", &scale, 1);
2289 weston_config_section_get_string(section, "transform", &s, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05002290 if (weston_parse_transform(s, &transform) < 0)
2291 weston_log("Invalid transform \"%s\" for output %s\n",
2292 s, output->base.name);
2293
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002294 free(s);
Alexander Larssond9a7bb72013-05-22 14:41:39 +02002295
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002296 if (get_gbm_format_from_section(section,
Giulio Camuffo954f1832014-10-11 18:27:30 +03002297 b->format,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002298 &output->format) == -1)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002299 output->format = b->format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002300
Rob Bradford66bd9f52013-06-25 18:56:42 +01002301 weston_config_section_get_string(section, "seat", &s, "");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002302 setup_output_seat_constraint(b, &output->base, s);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002303 free(s);
2304
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002305 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002306 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002307 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002308 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002309 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002310
Giulio Camuffo954f1832014-10-11 18:27:30 +03002311 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2312 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002313
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002314 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2315 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002316
David Herrmann0f0d54e2011-12-08 17:05:45 +01002317 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002318 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002319 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002320 goto err_free;
2321 }
2322
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002323 if (config == OUTPUT_CONFIG_OFF) {
2324 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002325 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002326 0, 0, 0, 0, 0, NULL);
2327 goto err_free;
2328 }
2329
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002330 current = drm_output_choose_initial_mode(output, config,
2331 width, height,
2332 &crtc_mode, &modeline);
2333 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002334 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002335 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002336 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002337
Giulio Camuffo954f1832014-10-11 18:27:30 +03002338 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002339 connector->mmWidth, connector->mmHeight,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002340 transform, scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002341
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342 if (b->use_pixman) {
2343 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002344 weston_log("Failed to init output pixman state\n");
2345 goto err_output;
2346 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002347 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002348 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002349 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002350 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002351
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002352 output->backlight = backlight_init(drm_device,
2353 connector->connector_type);
2354 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002355 weston_log("Initialized backlight, device %s\n",
2356 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002357 output->base.set_backlight = drm_set_backlight;
2358 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002359 } else {
2360 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002361 }
2362
Giulio Camuffo954f1832014-10-11 18:27:30 +03002363 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002364
Giulio Camuffo954f1832014-10-11 18:27:30 +03002365 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002366 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2367 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002368
Jonas Ådahle5a12252013-04-05 23:07:11 +02002369 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002370 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002371 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002372 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002373 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002374 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002375
Richard Hughese7299962013-05-01 21:52:12 +01002376 output->base.gamma_size = output->original_crtc->gamma_size;
2377 output->base.set_gamma = drm_output_set_gamma;
2378
Giulio Camuffo954f1832014-10-11 18:27:30 +03002379 weston_plane_init(&output->cursor_plane, b->compositor, 0, 0);
2380 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002381
Giulio Camuffo954f1832014-10-11 18:27:30 +03002382 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2383 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2384 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002385
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002386 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002387 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002388 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002389 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002390 m->width, m->height, m->refresh / 1000.0,
2391 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2392 ", preferred" : "",
2393 m->flags & WL_OUTPUT_MODE_CURRENT ?
2394 ", current" : "",
2395 connector->count_modes == 0 ?
2396 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002397
Mario Kleiner80817042015-06-21 21:25:11 +02002398 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2399 output->base.native_mode = output->base.current_mode;
2400 output->base.native_scale = output->base.current_scale;
2401
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002402 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002403
John Kåre Alsaker94659272012-11-13 19:10:18 +01002404err_output:
2405 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002406err_free:
2407 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2408 base.link) {
2409 wl_list_remove(&drm_mode->base.link);
2410 free(drm_mode);
2411 }
2412
2413 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002414 b->crtc_allocator &= ~(1 << output->crtc_id);
2415 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002416 free(output);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002417
David Herrmann0f0d54e2011-12-08 17:05:45 +01002418 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002419}
2420
Jesse Barnes58ef3792012-02-23 09:45:49 -05002421static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002422create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002423{
2424 struct drm_sprite *sprite;
2425 drmModePlaneRes *plane_res;
2426 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002427 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002428
Giulio Camuffo954f1832014-10-11 18:27:30 +03002429 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002430 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002431 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002432 strerror(errno));
2433 return;
2434 }
2435
2436 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002437 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002438 if (!plane)
2439 continue;
2440
Peter Huttererf3d62272013-08-08 11:57:05 +10002441 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002442 plane->count_formats));
2443 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002444 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002445 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002446 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002447 continue;
2448 }
2449
Jesse Barnes58ef3792012-02-23 09:45:49 -05002450 sprite->possible_crtcs = plane->possible_crtcs;
2451 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002452 sprite->current = NULL;
2453 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002454 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002455 sprite->count_formats = plane->count_formats;
2456 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002457 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002458 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002459 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2460 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2461 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002462
Giulio Camuffo954f1832014-10-11 18:27:30 +03002463 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002464 }
2465
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002466 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002467}
2468
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002469static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002470destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002471{
2472 struct drm_sprite *sprite, *next;
2473 struct drm_output *output;
2474
Giulio Camuffo954f1832014-10-11 18:27:30 +03002475 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002476 struct drm_output, base.link);
2477
Giulio Camuffo954f1832014-10-11 18:27:30 +03002478 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2479 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002480 sprite->plane_id,
2481 output->crtc_id, 0, 0,
2482 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002483 drm_output_release_fb(output, sprite->current);
2484 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002485 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002486 free(sprite);
2487 }
2488}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002489
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002490static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002491create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002492 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002493{
2494 drmModeConnector *connector;
2495 drmModeRes *resources;
2496 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002497 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002498
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002500 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002501 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002502 return -1;
2503 }
2504
Giulio Camuffo954f1832014-10-11 18:27:30 +03002505 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2506 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002507 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002508 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002509 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002510
Giulio Camuffo954f1832014-10-11 18:27:30 +03002511 b->min_width = resources->min_width;
2512 b->max_width = resources->max_width;
2513 b->min_height = resources->min_height;
2514 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002515
Giulio Camuffo954f1832014-10-11 18:27:30 +03002516 b->num_crtcs = resources->count_crtcs;
2517 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002518
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002519 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002520 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002521 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002522 if (connector == NULL)
2523 continue;
2524
2525 if (connector->connection == DRM_MODE_CONNECTED &&
2526 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002527 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002528 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002529 connector, x, y,
2530 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002531 drmModeFreeConnector(connector);
2532 continue;
2533 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002534
Giulio Camuffo954f1832014-10-11 18:27:30 +03002535 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002536 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002537 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002538 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002539
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002540 drmModeFreeConnector(connector);
2541 }
2542
Giulio Camuffo954f1832014-10-11 18:27:30 +03002543 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002544 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002545 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002546 return -1;
2547 }
2548
2549 drmModeFreeResources(resources);
2550
2551 return 0;
2552}
2553
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002554static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002555update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002556{
2557 drmModeConnector *connector;
2558 drmModeRes *resources;
2559 struct drm_output *output, *next;
2560 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002561 uint32_t connected = 0, disconnects = 0;
2562 int i;
2563
Giulio Camuffo954f1832014-10-11 18:27:30 +03002564 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002565 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002566 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002567 return;
2568 }
2569
2570 /* collect new connects */
2571 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002572 int connector_id = resources->connectors[i];
2573
Giulio Camuffo954f1832014-10-11 18:27:30 +03002574 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002575 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002576 continue;
2577
David Herrmann7551cff2011-12-08 17:05:43 +01002578 if (connector->connection != DRM_MODE_CONNECTED) {
2579 drmModeFreeConnector(connector);
2580 continue;
2581 }
2582
Benjamin Franzke117483d2011-08-30 11:38:26 +02002583 connected |= (1 << connector_id);
2584
Giulio Camuffo954f1832014-10-11 18:27:30 +03002585 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002586 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002587 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002588 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002589
2590 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002591 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002592 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002593 else
2594 x = 0;
2595 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002596 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002597 connector, x, y,
2598 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002599 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002600
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002601 }
2602 drmModeFreeConnector(connector);
2603 }
2604 drmModeFreeResources(resources);
2605
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002607 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002608 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002609 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002610 if (disconnects & (1 << output->connector_id)) {
2611 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002612 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002613 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002614 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002615 }
2616 }
2617 }
2618
Daniel Stonef556ebe2015-05-21 08:28:58 +01002619 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002620 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002621 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002622}
2623
2624static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002625udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002626{
David Herrmannd7488c22012-03-11 20:05:21 +01002627 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002628 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002629
2630 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002631 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002632 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002633
David Herrmann6ac52db2012-03-11 20:05:22 +01002634 val = udev_device_get_property_value(device, "HOTPLUG");
2635 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002636 return 0;
2637
David Herrmann6ac52db2012-03-11 20:05:22 +01002638 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639}
2640
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002641static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002642udev_drm_event(int fd, uint32_t mask, void *data)
2643{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002644 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002645 struct udev_device *event;
2646
Giulio Camuffo954f1832014-10-11 18:27:30 +03002647 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002648
Giulio Camuffo954f1832014-10-11 18:27:30 +03002649 if (udev_event_is_hotplug(b, event))
2650 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002651
2652 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002653
2654 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002655}
2656
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002657static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002658drm_restore(struct weston_compositor *ec)
2659{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002660 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002661}
2662
2663static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002664drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002665{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002666 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002667
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002669
Giulio Camuffo954f1832014-10-11 18:27:30 +03002670 wl_event_source_remove(b->udev_drm_source);
2671 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002672
Giulio Camuffo954f1832014-10-11 18:27:30 +03002673 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002674
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002675 weston_compositor_shutdown(ec);
2676
Giulio Camuffo954f1832014-10-11 18:27:30 +03002677 if (b->gbm)
2678 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002679
Giulio Camuffo954f1832014-10-11 18:27:30 +03002680 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002681
Giulio Camuffo954f1832014-10-11 18:27:30 +03002682 close(b->drm.fd);
Rob Bradford45c15b82013-07-26 16:29:35 +01002683
Giulio Camuffo954f1832014-10-11 18:27:30 +03002684 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002685}
2686
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002687static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002688drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002689{
2690 struct drm_output *output;
2691 struct drm_mode *drm_mode;
2692 int ret;
2693
Giulio Camuffo954f1832014-10-11 18:27:30 +03002694 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002695 if (!output->current) {
2696 /* If something that would cause the output to
2697 * switch mode happened while in another vt, we
2698 * might not have a current drm_fb. In that case,
2699 * schedule a repaint and let drm_output_repaint
2700 * handle setting the mode. */
2701 weston_output_schedule_repaint(&output->base);
2702 continue;
2703 }
2704
Hardeningff39efa2013-09-18 23:56:35 +02002705 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002707 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002708 &output->connector_id, 1,
2709 &drm_mode->mode_info);
2710 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002711 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002712 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002713 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002714 output->base.x, output->base.y);
2715 }
2716 }
2717}
2718
2719static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002720session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002721{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002722 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002723 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002724 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002725 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002726
Giulio Camuffo954f1832014-10-11 18:27:30 +03002727 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002728 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002729 compositor->state = b->prev_state;
2730 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002731 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002732 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002733 } else {
2734 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002735 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002736
Giulio Camuffo954f1832014-10-11 18:27:30 +03002737 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002738 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002739
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002740 /* If we have a repaint scheduled (either from a
2741 * pending pageflip or the idle handler), make sure we
2742 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002743 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002744 * further attemps at repainting. When we switch
2745 * back, we schedule a repaint, which will process
2746 * pending frame callbacks. */
2747
Giulio Camuffo954f1832014-10-11 18:27:30 +03002748 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002749 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002750 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002751 }
2752
Giulio Camuffo954f1832014-10-11 18:27:30 +03002753 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002754 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002755
Giulio Camuffo954f1832014-10-11 18:27:30 +03002756 wl_list_for_each(sprite, &b->sprite_list, link)
2757 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002758 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002759 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002760 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002761 };
2762}
2763
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002764static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002765switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002766{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002767 struct weston_compositor *compositor = data;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002768
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002769 weston_launcher_activate_vt(compositor->launcher, key - KEY_F1 + 1);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04002770}
2771
David Herrmann0af066f2012-10-29 19:21:16 +01002772/*
2773 * Find primary GPU
2774 * Some systems may have multiple DRM devices attached to a single seat. This
2775 * function loops over all devices and tries to find a PCI device with the
2776 * boot_vga sysfs attribute set to 1.
2777 * If no such device is found, the first DRM device reported by udev is used.
2778 */
2779static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002780find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002781{
2782 struct udev_enumerate *e;
2783 struct udev_list_entry *entry;
2784 const char *path, *device_seat, *id;
2785 struct udev_device *device, *drm_device, *pci;
2786
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002788 udev_enumerate_add_match_subsystem(e, "drm");
2789 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2790
2791 udev_enumerate_scan_devices(e);
2792 drm_device = NULL;
2793 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2794 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002795 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002796 if (!device)
2797 continue;
2798 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2799 if (!device_seat)
2800 device_seat = default_seat;
2801 if (strcmp(device_seat, seat)) {
2802 udev_device_unref(device);
2803 continue;
2804 }
2805
2806 pci = udev_device_get_parent_with_subsystem_devtype(device,
2807 "pci", NULL);
2808 if (pci) {
2809 id = udev_device_get_sysattr_value(pci, "boot_vga");
2810 if (id && !strcmp(id, "1")) {
2811 if (drm_device)
2812 udev_device_unref(drm_device);
2813 drm_device = device;
2814 break;
2815 }
2816 }
2817
2818 if (!drm_device)
2819 drm_device = device;
2820 else
2821 udev_device_unref(device);
2822 }
2823
2824 udev_enumerate_unref(e);
2825 return drm_device;
2826}
2827
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002828static void
Kristian Høgsberge3148752013-05-06 23:19:49 -04002829planes_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002830{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002831 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002832
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002833 switch (key) {
2834 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002835 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002836 break;
2837 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002838 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002839 break;
2840 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002841 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002842 break;
2843 default:
2844 break;
2845 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002846}
2847
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002848#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002849static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002850recorder_destroy(struct drm_output *output)
2851{
2852 vaapi_recorder_destroy(output->recorder);
2853 output->recorder = NULL;
2854
2855 output->base.disable_planes--;
2856
2857 wl_list_remove(&output->recorder_frame_listener.link);
2858 weston_log("[libva recorder] done\n");
2859}
2860
2861static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002862recorder_frame_notify(struct wl_listener *listener, void *data)
2863{
2864 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002865 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002866 int fd, ret;
2867
2868 output = container_of(listener, struct drm_output,
2869 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002870 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002871
2872 if (!output->recorder)
2873 return;
2874
Giulio Camuffo954f1832014-10-11 18:27:30 +03002875 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002876 DRM_CLOEXEC, &fd);
2877 if (ret) {
2878 weston_log("[libva recorder] "
2879 "failed to create prime fd for front buffer\n");
2880 return;
2881 }
2882
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002883 ret = vaapi_recorder_frame(output->recorder, fd,
2884 output->current->stride);
2885 if (ret < 0) {
2886 weston_log("[libva recorder] aborted: %m\n");
2887 recorder_destroy(output);
2888 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002889}
2890
2891static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002892create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002893 const char *filename)
2894{
2895 int fd;
2896 drm_magic_t magic;
2897
Giulio Camuffo954f1832014-10-11 18:27:30 +03002898 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002899 if (fd < 0)
2900 return NULL;
2901
2902 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002903 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002904
2905 return vaapi_recorder_create(fd, width, height, filename);
2906}
2907
2908static void
2909recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2910 void *data)
2911{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002912 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002913 struct drm_output *output;
2914 int width, height;
2915
Giulio Camuffo954f1832014-10-11 18:27:30 +03002916 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002917 struct drm_output, base.link);
2918
2919 if (!output->recorder) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002920 if (output->format != GBM_FORMAT_XRGB8888) {
2921 weston_log("failed to start vaapi recorder: "
2922 "output format not supported\n");
2923 return;
2924 }
2925
Hardeningff39efa2013-09-18 23:56:35 +02002926 width = output->base.current_mode->width;
2927 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002928
2929 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002930 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931 if (!output->recorder) {
2932 weston_log("failed to create vaapi recorder\n");
2933 return;
2934 }
2935
2936 output->base.disable_planes++;
2937
2938 output->recorder_frame_listener.notify = recorder_frame_notify;
2939 wl_signal_add(&output->base.frame_signal,
2940 &output->recorder_frame_listener);
2941
2942 weston_output_schedule_repaint(&output->base);
2943
2944 weston_log("[libva recorder] initialized\n");
2945 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002946 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002947 }
2948}
2949#else
2950static void
2951recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2952 void *data)
2953{
2954 weston_log("Compiled without libva support\n");
2955}
2956#endif
2957
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002958static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002959switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002960{
2961 struct drm_output *output;
2962
Giulio Camuffo954f1832014-10-11 18:27:30 +03002963 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002964 return;
2965
2966 weston_log("Switching to GL renderer\n");
2967
Giulio Camuffo954f1832014-10-11 18:27:30 +03002968 b->gbm = create_gbm_device(b->drm.fd);
2969 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002970 weston_log("Failed to create gbm device. "
2971 "Aborting renderer switch\n");
2972 return;
2973 }
2974
Giulio Camuffo954f1832014-10-11 18:27:30 +03002975 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002976 pixman_renderer_output_destroy(&output->base);
2977
Giulio Camuffo954f1832014-10-11 18:27:30 +03002978 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002979
Giulio Camuffo954f1832014-10-11 18:27:30 +03002980 if (drm_backend_create_gl_renderer(b) < 0) {
2981 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002982 weston_log("Failed to create GL renderer. Quitting.\n");
2983 /* FIXME: we need a function to shutdown cleanly */
2984 assert(0);
2985 }
2986
Giulio Camuffo954f1832014-10-11 18:27:30 +03002987 wl_list_for_each(output, &b->compositor->output_list, base.link)
2988 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002989
Giulio Camuffo954f1832014-10-11 18:27:30 +03002990 b->use_pixman = 0;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002991}
2992
2993static void
2994renderer_switch_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
2995 void *data)
2996{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002997 struct drm_backend *b = (struct drm_backend *)seat->compositor->backend;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002998
Giulio Camuffo954f1832014-10-11 18:27:30 +03002999 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003000}
3001
Giulio Camuffo954f1832014-10-11 18:27:30 +03003002static struct drm_backend *
3003drm_backend_create(struct weston_compositor *compositor,
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003004 struct drm_parameters *param,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003005 int *argc, char *argv[],
3006 struct weston_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003007{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003008 struct drm_backend *b;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003009 struct weston_config_section *section;
David Herrmann0af066f2012-10-29 19:21:16 +01003010 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003011 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003012 const char *path;
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003013 uint32_t key;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003014
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003015 weston_log("initializing drm backend\n");
3016
Giulio Camuffo954f1832014-10-11 18:27:30 +03003017 b = zalloc(sizeof *b);
3018 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003019 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003020
Pekka Paalanen68583832015-05-19 09:53:16 +03003021 /*
3022 * KMS support for hardware planes cannot properly synchronize
3023 * without nuclear page flip. Without nuclear/atomic, hw plane
3024 * and cursor plane updates would either tear or cause extra
3025 * waits for vblanks which means dropping the compositor framerate
3026 * to a fraction.
3027 *
3028 * These can be enabled again when nuclear/atomic support lands.
3029 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 b->sprites_are_broken = 1;
3031 b->cursors_are_broken = 1;
3032 b->compositor = compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003033
3034 section = weston_config_get_section(config, "core", NULL, NULL);
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003035 if (get_gbm_format_from_section(section,
3036 GBM_FORMAT_XRGB8888,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003037 &b->format) == -1)
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003038 goto err_base;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003039
Giulio Camuffo954f1832014-10-11 18:27:30 +03003040 b->use_pixman = param->use_pixman;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003041
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003042 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003043 compositor->launcher = weston_launcher_connect(compositor, param->tty,
3044 param->seat_id, true);
3045 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003046 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003047 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003048 goto err_compositor;
3049 }
3050
Giulio Camuffo954f1832014-10-11 18:27:30 +03003051 b->udev = udev_new();
3052 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003053 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003054 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003055 }
3056
Giulio Camuffo954f1832014-10-11 18:27:30 +03003057 b->session_listener.notify = session_notify;
3058 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003059
Giulio Camuffo954f1832014-10-11 18:27:30 +03003060 drm_device = find_primary_gpu(b, param->seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003061 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003062 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003063 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003064 }
David Herrmann0af066f2012-10-29 19:21:16 +01003065 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003066
Giulio Camuffo954f1832014-10-11 18:27:30 +03003067 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003068 weston_log("failed to initialize kms\n");
3069 goto err_udev_dev;
3070 }
3071
Giulio Camuffo954f1832014-10-11 18:27:30 +03003072 if (b->use_pixman) {
3073 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003074 weston_log("failed to initialize pixman renderer\n");
3075 goto err_udev_dev;
3076 }
3077 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003078 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003079 weston_log("failed to initialize egl\n");
3080 goto err_udev_dev;
3081 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003082 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003083
Giulio Camuffo954f1832014-10-11 18:27:30 +03003084 b->base.destroy = drm_destroy;
3085 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003086
Giulio Camuffo954f1832014-10-11 18:27:30 +03003087 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003088
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003089 for (key = KEY_F1; key < KEY_F9; key++)
Giulio Camuffo954f1832014-10-11 18:27:30 +03003090 weston_compositor_add_key_binding(compositor, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01003091 MODIFIER_CTRL | MODIFIER_ALT,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003092 switch_vt_binding, compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003093
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 wl_list_init(&b->sprite_list);
3095 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003096
Giulio Camuffo954f1832014-10-11 18:27:30 +03003097 if (udev_input_init(&b->input,
3098 compositor, b->udev, param->seat_id) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003099 weston_log("failed to create input devices\n");
3100 goto err_sprite;
3101 }
3102
Giulio Camuffo954f1832014-10-11 18:27:30 +03003103 if (create_outputs(b, param->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003104 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003105 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003106 }
3107
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003108 /* A this point we have some idea of whether or not we have a working
3109 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003110 if (!b->cursors_are_broken)
3111 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003112
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003113 path = NULL;
3114
Giulio Camuffo954f1832014-10-11 18:27:30 +03003115 loop = wl_display_get_event_loop(compositor->wl_display);
3116 b->drm_source =
3117 wl_event_loop_add_fd(loop, b->drm.fd,
3118 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003119
Giulio Camuffo954f1832014-10-11 18:27:30 +03003120 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3121 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003122 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003123 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003124 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003125 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003126 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003127 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003128 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003129 udev_monitor_get_fd(b->udev_monitor),
3130 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003131
Giulio Camuffo954f1832014-10-11 18:27:30 +03003132 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003133 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003134 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003135 }
3136
Daniel Stonea96b93c2012-06-22 14:04:37 +01003137 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003138
Giulio Camuffo954f1832014-10-11 18:27:30 +03003139 weston_compositor_add_debug_binding(compositor, KEY_O,
3140 planes_binding, b);
3141 weston_compositor_add_debug_binding(compositor, KEY_C,
3142 planes_binding, b);
3143 weston_compositor_add_debug_binding(compositor, KEY_V,
3144 planes_binding, b);
3145 weston_compositor_add_debug_binding(compositor, KEY_Q,
3146 recorder_binding, b);
3147 weston_compositor_add_debug_binding(compositor, KEY_W,
3148 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003149
Giulio Camuffo954f1832014-10-11 18:27:30 +03003150 compositor->backend = &b->base;
3151 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003152
3153err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003154 wl_event_source_remove(b->udev_drm_source);
3155 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003156err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003157 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003158err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003159 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003160err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003161 compositor->renderer->destroy(compositor);
3162 gbm_device_destroy(b->gbm);
3163 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003164err_udev_dev:
3165 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003166err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003167 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003168err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003169 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003170err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003171 weston_compositor_shutdown(compositor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003172err_base:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003173 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003174 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003175}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003176
Giulio Camuffo954f1832014-10-11 18:27:30 +03003177WL_EXPORT int
3178backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003179 struct weston_config *config)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003180{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003181 struct drm_backend *b;
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003182 struct drm_parameters param = { 0, };
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003183
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003184 const struct weston_option drm_options[] = {
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003185 { WESTON_OPTION_INTEGER, "connector", 0, &param.connector },
3186 { WESTON_OPTION_STRING, "seat", 0, &param.seat_id },
3187 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003188 { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode },
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003189 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &param.use_pixman },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003190 };
Benjamin Franzke117483d2011-08-30 11:38:26 +02003191
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003192 param.seat_id = default_seat;
3193
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003194 parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003195
Giulio Camuffo954f1832014-10-11 18:27:30 +03003196 b = drm_backend_create(compositor, &param, argc, argv, config);
3197 if (b == NULL)
3198 return -1;
3199 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003200}