blob: 136d8eb3d67a02776766ec14e474e129a843b131 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010031#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040032#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040035#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070036#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030037#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020038#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030039#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030040#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041
Benjamin Franzkec649a922011-03-02 11:56:04 +010042#include <xf86drm.h>
43#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050044#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010045
Benjamin Franzke060cf802011-04-30 09:32:11 +020046#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040047#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020048
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070049#include "compositor.h"
50#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070051#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020052#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010053#include "gl-renderer.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020054#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070055#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100056#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010057#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030058#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020059#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030060#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040061
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030062#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
63#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
64#endif
65
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030066#ifndef DRM_CAP_CURSOR_WIDTH
67#define DRM_CAP_CURSOR_WIDTH 0x8
68#endif
69
70#ifndef DRM_CAP_CURSOR_HEIGHT
71#define DRM_CAP_CURSOR_HEIGHT 0x9
72#endif
73
74#ifndef GBM_BO_USE_CURSOR
75#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
76#endif
77
Giulio Camuffo954f1832014-10-11 18:27:30 +030078struct drm_backend {
79 struct weston_backend base;
80 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040081
82 struct udev *udev;
83 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040084
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010085 struct udev_monitor *udev_monitor;
86 struct wl_event_source *udev_drm_source;
87
Benjamin Franzke2af7f102011-03-02 11:14:59 +010088 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010089 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010090 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030091 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020093 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050094 uint32_t *crtcs;
95 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050096 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010097 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200105 uint32_t min_width, max_width;
106 uint32_t min_height, max_height;
107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200117 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300118
Rob Bradfordd355b802013-05-31 18:09:55 +0100119 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300120
Daniel Stone70d337d2015-06-16 18:42:23 +0100121 int32_t cursor_width;
122 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700123
124 /** Callback used to configure the outputs.
125 *
126 * This function will be called by the backend when a new DRM
127 * output needs to be configured.
128 */
129 enum weston_drm_backend_output_mode
130 (*configure_output)(struct weston_compositor *compositor,
131 bool use_current_mode,
132 const char *name,
133 struct weston_drm_backend_output_config *output_config);
134 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400135};
136
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400137struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500138 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400139 drmModeModeInfo mode_info;
140};
141
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300142struct drm_fb {
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;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100171 uint32_t gbm_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
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100179 struct gbm_surface *gbm_surface;
180 struct gbm_bo *gbm_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
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300222static struct gl_renderer_interface *gl_renderer;
223
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500224static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400225
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400226static void
227drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400228
Mario Kleinerf507ec32015-06-21 21:25:14 +0200229static void
230drm_output_update_msc(struct drm_output *output, unsigned int seq);
231
Jesse Barnes58ef3792012-02-23 09:45:49 -0500232static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200233drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500234{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200235 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300236 struct drm_backend *b =(struct drm_backend *)ec->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500237 int crtc;
238
Giulio Camuffo954f1832014-10-11 18:27:30 +0300239 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
240 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500241 continue;
242
243 if (supported & (1 << crtc))
244 return -1;
245 }
246
247 return 0;
248}
249
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300250static void
251drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
252{
253 struct drm_fb *fb = data;
254 struct gbm_device *gbm = gbm_bo_get_device(bo);
255
256 if (fb->fb_id)
257 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
258
Pekka Paalanende685b82012-12-04 15:58:12 +0200259 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300260
261 free(data);
262}
263
264static struct drm_fb *
Giulio Camuffo954f1832014-10-11 18:27:30 +0300265drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200266{
267 struct drm_fb *fb;
268 int ret;
269
270 struct drm_mode_create_dumb create_arg;
271 struct drm_mode_destroy_dumb destroy_arg;
272 struct drm_mode_map_dumb map_arg;
273
Peter Huttererf3d62272013-08-08 11:57:05 +1000274 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200275 if (!fb)
276 return NULL;
277
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700278 memset(&create_arg, 0, sizeof create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200279 create_arg.bpp = 32;
280 create_arg.width = width;
281 create_arg.height = height;
282
Giulio Camuffo954f1832014-10-11 18:27:30 +0300283 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200284 if (ret)
285 goto err_fb;
286
287 fb->handle = create_arg.handle;
288 fb->stride = create_arg.pitch;
289 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300290 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200291
Giulio Camuffo954f1832014-10-11 18:27:30 +0300292 ret = drmModeAddFB(b->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200293 fb->stride, fb->handle, &fb->fb_id);
294 if (ret)
295 goto err_bo;
296
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700297 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200298 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400299 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200300 if (ret)
301 goto err_add_fb;
302
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500303 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300304 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200305 if (fb->map == MAP_FAILED)
306 goto err_add_fb;
307
308 return fb;
309
310err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300311 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200312err_bo:
313 memset(&destroy_arg, 0, sizeof(destroy_arg));
314 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300315 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200316err_fb:
317 free(fb);
318 return NULL;
319}
320
321static void
322drm_fb_destroy_dumb(struct drm_fb *fb)
323{
324 struct drm_mode_destroy_dumb destroy_arg;
325
326 if (!fb->map)
327 return;
328
329 if (fb->fb_id)
330 drmModeRmFB(fb->fd, fb->fb_id);
331
332 weston_buffer_reference(&fb->buffer_ref, NULL);
333
334 munmap(fb->map, fb->size);
335
336 memset(&destroy_arg, 0, sizeof(destroy_arg));
337 destroy_arg.handle = fb->handle;
338 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
339
340 free(fb);
341}
342
343static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500344drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300345 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300346{
347 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200348 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200349 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300350 int ret;
351
352 if (fb)
353 return fb;
354
Bryce Harringtonde16d892014-11-20 22:21:57 -0800355 fb = zalloc(sizeof *fb);
356 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200357 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300358
359 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300360
361 width = gbm_bo_get_width(bo);
362 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200363 fb->stride = gbm_bo_get_stride(bo);
364 fb->handle = gbm_bo_get_handle(bo).u32;
365 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300366 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300367
Giulio Camuffo954f1832014-10-11 18:27:30 +0300368 if (backend->min_width > width || width > backend->max_width ||
369 backend->min_height > height ||
370 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200371 weston_log("bo geometry out of bounds\n");
372 goto err_free;
373 }
374
375 ret = -1;
376
Giulio Camuffo954f1832014-10-11 18:27:30 +0300377 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200378 handles[0] = fb->handle;
379 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200380 offsets[0] = 0;
381
Giulio Camuffo954f1832014-10-11 18:27:30 +0300382 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200383 format, handles, pitches, offsets,
384 &fb->fb_id, 0);
385 if (ret) {
386 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300387 backend->no_addfb2 = 1;
388 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200389 }
390 }
391
392 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300393 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200394 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200395
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300396 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200397 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200398 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300399 }
400
401 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
402
403 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200404
405err_free:
406 free(fb);
407 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300408}
409
410static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500411drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200412{
Pekka Paalanende685b82012-12-04 15:58:12 +0200413 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200414
415 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200416
Pekka Paalanende685b82012-12-04 15:58:12 +0200417 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200418}
419
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200420static void
421drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
422{
423 if (!fb)
424 return;
425
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200426 if (fb->map &&
427 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200428 drm_fb_destroy_dumb(fb);
429 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200430 if (fb->is_client_buffer)
431 gbm_bo_destroy(fb->bo);
432 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100433 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600434 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200435 }
436}
437
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500438static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200439drm_output_check_scanout_format(struct drm_output *output,
440 struct weston_surface *es, struct gbm_bo *bo)
441{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200442 uint32_t format;
443 pixman_region32_t r;
444
445 format = gbm_bo_get_format(bo);
446
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700447 if (format == GBM_FORMAT_ARGB8888) {
448 /* We can scanout an ARGB buffer if the surface's
449 * opaque region covers the whole output, but we have
450 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800451 pixman_region32_init_rect(&r, 0, 0,
452 output->base.width,
453 output->base.height);
454 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200455
456 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500457 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200458
459 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500460 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700461
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100462 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700463 return format;
464
465 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200466}
467
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400468static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200469drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500470 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500471{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300472 struct drm_backend *b =
473 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500474 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200475 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300476 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500477 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500478
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 if (ev->geometry.x != output->base.x ||
480 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300481 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200482 buffer->width != output->base.current_mode->width ||
483 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200484 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400486 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500487
Pekka Paalanen5580f222015-02-17 16:33:18 +0200488 if (ev->geometry.scissor_enabled)
489 return NULL;
490
Giulio Camuffo954f1832014-10-11 18:27:30 +0300491 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700492 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500493
Rob Bradford9b101872012-09-14 23:25:41 +0100494 /* Unable to use the buffer for scanout */
495 if (!bo)
496 return NULL;
497
Jason Ekstranda7af7042013-10-12 22:38:11 -0500498 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500499 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300500 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400501 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300502 }
503
Giulio Camuffo954f1832014-10-11 18:27:30 +0300504 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300505 if (!output->next) {
506 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400507 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300508 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500509
Pekka Paalanende685b82012-12-04 15:58:12 +0200510 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500511
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400512 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500513}
514
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500515static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200516drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400517{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300518 struct drm_backend *b =
519 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300520 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400521
Giulio Camuffo954f1832014-10-11 18:27:30 +0300522 output->base.compositor->renderer->repaint_output(&output->base,
523 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400524
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100525 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300526 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200527 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400528 return;
529 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300530
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100531 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300532 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200533 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100534 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300535 return;
536 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400537}
538
539static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200540drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
541{
542 struct weston_compositor *ec = output->base.compositor;
543 pixman_region32_t total_damage, previous_damage;
544
545 pixman_region32_init(&total_damage);
546 pixman_region32_init(&previous_damage);
547
548 pixman_region32_copy(&previous_damage, damage);
549
550 pixman_region32_union(&total_damage, damage, &output->previous_damage);
551 pixman_region32_copy(&output->previous_damage, &previous_damage);
552
553 output->current_image ^= 1;
554
555 output->next = output->dumb[output->current_image];
556 pixman_renderer_output_set_buffer(&output->base,
557 output->image[output->current_image]);
558
559 ec->renderer->repaint_output(&output->base, &total_damage);
560
561 pixman_region32_fini(&total_damage);
562 pixman_region32_fini(&previous_damage);
563}
564
565static void
566drm_output_render(struct drm_output *output, pixman_region32_t *damage)
567{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300568 struct weston_compositor *c = output->base.compositor;
569 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200570
Giulio Camuffo954f1832014-10-11 18:27:30 +0300571 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200572 drm_output_render_pixman(output, damage);
573 else
574 drm_output_render_gl(output, damage);
575
Giulio Camuffo954f1832014-10-11 18:27:30 +0300576 pixman_region32_subtract(&c->primary_plane.damage,
577 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200578}
579
580static void
Richard Hughese7299962013-05-01 21:52:12 +0100581drm_output_set_gamma(struct weston_output *output_base,
582 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
583{
584 int rc;
585 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300586 struct drm_backend *backend =
587 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100588
589 /* check */
590 if (output_base->gamma_size != size)
591 return;
592 if (!output->original_crtc)
593 return;
594
Giulio Camuffo954f1832014-10-11 18:27:30 +0300595 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100596 output->crtc_id,
597 size, r, g, b);
598 if (rc)
599 weston_log("set gamma failed: %m\n");
600}
601
Bryce Harringtonada4f072015-06-30 13:25:46 -0700602/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200603 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700604 * The pipe parameter indicates which CRTC is in use. Knowing this, we
605 * can determine which vblank sequence type to use for it. Traditional
606 * cards had only two CRTCs, with CRTC 0 using no special flags, and
607 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
608 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200609 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700610 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
611 * 0-31. If this is non-zero it indicates we're dealing with a
612 * multi-gpu situation and we need to calculate the vblank sync
613 * using DRM_BLANK_HIGH_CRTC_MASK.
614 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300615static unsigned int
616drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200617{
618 if (output->pipe > 1)
619 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
620 DRM_VBLANK_HIGH_CRTC_MASK;
621 else if (output->pipe > 0)
622 return DRM_VBLANK_SECONDARY;
623 else
624 return 0;
625}
626
David Herrmann1edf44c2013-10-22 17:11:26 +0200627static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500628drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400629 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100630{
631 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300632 struct drm_backend *backend =
633 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500634 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400635 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500636 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100637
Xiong Zhangabd5d472013-10-11 14:43:07 +0800638 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200639 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800640
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300641 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400642 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300643 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200644 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100645
Hardeningff39efa2013-09-18 23:56:35 +0200646 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200647 if (!output->current ||
648 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300649 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300650 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400651 &output->connector_id, 1,
652 &mode->mode_info);
653 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200654 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200655 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400656 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300657 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200658 }
659
Giulio Camuffo954f1832014-10-11 18:27:30 +0300660 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300661 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500662 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200663 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200664 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500665 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100666
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300667 output->page_flip_pending = 1;
668
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400669 drm_output_set_cursor(output);
670
Jesse Barnes58ef3792012-02-23 09:45:49 -0500671 /*
672 * Now, update all the sprite surfaces
673 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300674 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200675 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500676 drmVBlank vbl = {
677 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
678 .request.sequence = 1,
679 };
680
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200681 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200682 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500683 continue;
684
Giulio Camuffo954f1832014-10-11 18:27:30 +0300685 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200686 fb_id = s->next->fb_id;
687
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200689 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500690 s->dest_x, s->dest_y,
691 s->dest_w, s->dest_h,
692 s->src_x, s->src_y,
693 s->src_w, s->src_h);
694 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200695 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500696 ret, strerror(errno));
697
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200698 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500699
Jesse Barnes58ef3792012-02-23 09:45:49 -0500700 /*
701 * Queue a vblank signal so we know when the surface
702 * becomes active on the display or has been replaced.
703 */
704 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300705 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500706 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200707 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500708 ret, strerror(errno));
709 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300710
711 s->output = output;
712 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500713 }
714
David Herrmann1edf44c2013-10-22 17:11:26 +0200715 return 0;
716
717err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800718 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200719 if (output->next) {
720 drm_output_release_fb(output, output->next);
721 output->next = NULL;
722 }
723
724 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400725}
726
727static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200728drm_output_start_repaint_loop(struct weston_output *output_base)
729{
730 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300731 struct drm_backend *backend = (struct drm_backend *)
732 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200733 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200734 struct timespec ts, tnow;
735 struct timespec vbl2now;
736 int64_t refresh_nsec;
737 int ret;
738 drmVBlank vbl = {
739 .request.type = DRM_VBLANK_RELATIVE,
740 .request.sequence = 0,
741 .request.signal = 0,
742 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300743
Xiong Zhangabd5d472013-10-11 14:43:07 +0800744 if (output->destroy_pending)
745 return;
746
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300747 if (!output->current) {
748 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200749 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300750 }
751
Mario Kleinerf507ec32015-06-21 21:25:14 +0200752 /* Try to get current msc and timestamp via instant query */
753 vbl.request.type |= drm_waitvblank_pipe(output);
754 ret = drmWaitVBlank(backend->drm.fd, &vbl);
755
756 /* Error ret or zero timestamp means failure to get valid timestamp */
757 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
758 ts.tv_sec = vbl.reply.tval_sec;
759 ts.tv_nsec = vbl.reply.tval_usec * 1000;
760
761 /* Valid timestamp for most recent vblank - not stale?
762 * Stale ts could happen on Linux 3.17+, so make sure it
763 * is not older than 1 refresh duration since now.
764 */
765 weston_compositor_read_presentation_clock(backend->compositor,
766 &tnow);
767 timespec_sub(&vbl2now, &tnow, &ts);
768 refresh_nsec =
769 millihz_to_nsec(output->base.current_mode->refresh);
770 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
771 drm_output_update_msc(output, vbl.reply.sequence);
772 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200773 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200774 return;
775 }
776 }
777
778 /* Immediate query didn't provide valid timestamp.
779 * Use pageflip fallback.
780 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300781 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200782
Giulio Camuffo954f1832014-10-11 18:27:30 +0300783 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200784 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
785 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200786 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200787 }
David Herrmann3c688c52013-10-22 17:11:25 +0200788
789 return;
790
791finish_frame:
792 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300793 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200794 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200795 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200796}
797
798static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400799drm_output_update_msc(struct drm_output *output, unsigned int seq)
800{
801 uint64_t msc_hi = output->base.msc >> 32;
802
803 if (seq < (output->base.msc & 0xffffffff))
804 msc_hi++;
805
806 output->base.msc = (msc_hi << 32) + seq;
807}
808
809static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500810vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
811 void *data)
812{
813 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300814 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400815 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200816 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
817 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300818
Pekka Paalanen641307c2014-09-23 22:08:47 -0400819 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300820 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500821
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200822 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200823 s->current = s->next;
824 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300825
826 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400827 ts.tv_sec = sec;
828 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200829 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300830 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500831}
832
833static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800834drm_output_destroy(struct weston_output *output_base);
835
836static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400837page_flip_handler(int fd, unsigned int frame,
838 unsigned int sec, unsigned int usec, void *data)
839{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200840 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400841 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200842 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
843 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
844 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400845
Pekka Paalanen641307c2014-09-23 22:08:47 -0400846 drm_output_update_msc(output, frame);
847
Jonas Ådahle5a12252013-04-05 23:07:11 +0200848 /* We don't set page_flip_pending on start_repaint_loop, in that case
849 * we just want to page flip to the current buffer to get an accurate
850 * timestamp */
851 if (output->page_flip_pending) {
852 drm_output_release_fb(output, output->current);
853 output->current = output->next;
854 output->next = NULL;
855 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300856
Jonas Ådahle5a12252013-04-05 23:07:11 +0200857 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400858
Xiong Zhangabd5d472013-10-11 14:43:07 +0800859 if (output->destroy_pending)
860 drm_output_destroy(&output->base);
861 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400862 ts.tv_sec = sec;
863 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200864 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300865
866 /* We can't call this from frame_notify, because the output's
867 * repaint needed flag is cleared just after that */
868 if (output->recorder)
869 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300870 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200871}
872
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500873static uint32_t
874drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500876{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500877 uint32_t i, format;
878
879 format = gbm_bo_get_format(bo);
880
881 if (format == GBM_FORMAT_ARGB8888) {
882 pixman_region32_t r;
883
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500884 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600885 ev->surface->width,
886 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500888
889 if (!pixman_region32_not_empty(&r))
890 format = GBM_FORMAT_XRGB8888;
891
892 pixman_region32_fini(&r);
893 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500894
895 for (i = 0; i < s->count_formats; i++)
896 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500897 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500898
899 return 0;
900}
901
902static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500904{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 return !ev->transform.enabled ||
906 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500907}
908
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400909static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200910drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500912{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200913 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300914 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200915 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300916 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500917 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300918 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500919 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500920 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500921 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200922 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500923 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400924 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500925
Giulio Camuffo954f1832014-10-11 18:27:30 +0300926 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200927 return NULL;
928
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200929 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200930 return NULL;
931
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200932 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200933 return NULL;
934
Giulio Camuffo954f1832014-10-11 18:27:30 +0300935 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500937
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200938 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300940
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300943 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500944
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200946 return NULL;
947
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300948 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500949 return NULL;
950
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400952 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500953
Giulio Camuffo954f1832014-10-11 18:27:30 +0300954 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200955 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500956 continue;
957
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200958 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500959 found = 1;
960 break;
961 }
962 }
963
964 /* No sprites available */
965 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400966 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500967
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300968 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -0700969#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300970 /* XXX: TODO:
971 *
972 * Use AddFB2 directly, do not go via GBM.
973 * Add support for multiplanar formats.
974 * Both require refactoring in the DRM-backend to
975 * support a mix of gbm_bos and drmfbs.
976 */
977 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000978 .fd = dmabuf->attributes.fd[0],
979 .width = dmabuf->attributes.width,
980 .height = dmabuf->attributes.height,
981 .stride = dmabuf->attributes.stride[0],
982 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300983 };
984
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +0000985 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300986 return NULL;
987
988 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
989 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -0700990#else
991 return NULL;
992#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300993 } else {
994 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
995 buffer_resource, GBM_BO_USE_SCANOUT);
996 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400997 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400998 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -0400999
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001001 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001002 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001003 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001004 }
1005
Giulio Camuffo954f1832014-10-11 18:27:30 +03001006 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001007 if (!s->next) {
1008 gbm_bo_destroy(bo);
1009 return NULL;
1010 }
1011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001013
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001015 s->plane.x = box->x1;
1016 s->plane.y = box->y1;
1017
Jesse Barnes58ef3792012-02-23 09:45:49 -05001018 /*
1019 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001020 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001021 * for us already).
1022 */
1023 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001025 &output->base.region);
1026 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001027 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001028 tbox = weston_transformed_rect(output->base.width,
1029 output->base.height,
1030 output->base.transform,
1031 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001032 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001033 s->dest_x = tbox.x1;
1034 s->dest_y = tbox.y1;
1035 s->dest_w = tbox.x2 - tbox.x1;
1036 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001037 pixman_region32_fini(&dest_rect);
1038
1039 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001041 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001042 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001043
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 weston_view_from_global_fixed(ev,
1045 wl_fixed_from_int(box->x1),
1046 wl_fixed_from_int(box->y1),
1047 &sx1, &sy1);
1048 weston_view_from_global_fixed(ev,
1049 wl_fixed_from_int(box->x2),
1050 wl_fixed_from_int(box->y2),
1051 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001052
1053 if (sx1 < 0)
1054 sx1 = 0;
1055 if (sy1 < 0)
1056 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001057 if (sx2 > wl_fixed_from_int(ev->surface->width))
1058 sx2 = wl_fixed_from_int(ev->surface->width);
1059 if (sy2 > wl_fixed_from_int(ev->surface->height))
1060 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001061
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001062 tbox.x1 = sx1;
1063 tbox.y1 = sy1;
1064 tbox.x2 = sx2;
1065 tbox.y2 = sy2;
1066
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001067 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1068 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001069 viewport->buffer.transform,
1070 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001071 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001072
1073 s->src_x = tbox.x1 << 8;
1074 s->src_y = tbox.y1 << 8;
1075 s->src_w = (tbox.x2 - tbox.x1) << 8;
1076 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001077 pixman_region32_fini(&src_rect);
1078
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001079 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001080}
1081
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001082static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001083drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001085{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001086 struct drm_backend *b =
1087 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001088 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001089 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001090
Derek Foremanbe428b32015-11-24 11:39:38 -06001091 if (ev->transform.enabled &&
1092 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1093 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001094 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001095 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001096 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1097 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001098 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001099 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001101 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001102 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001103 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001104 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001105 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001106 if (ev->geometry.scissor_enabled)
1107 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001108 if (ev->surface->buffer_ref.buffer == NULL)
1109 return NULL;
1110 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1111 if (!shmbuf)
1112 return NULL;
1113 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1114 return NULL;
1115 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001116 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001117 return NULL;
1118
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001120
1121 return &output->cursor_plane;
1122}
1123
Pekka Paalanend0ead482014-06-16 12:05:40 +03001124/**
1125 * Update the image for the current cursor surface
1126 *
1127 * @param b DRM backend structure
1128 * @param bo GBM buffer object to write into
1129 * @param ev View to use for cursor image
1130 */
1131static void
1132cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1133 struct weston_view *ev)
1134{
1135 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1136 uint32_t buf[b->cursor_width * b->cursor_height];
1137 int32_t stride;
1138 uint8_t *s;
1139 int i;
1140
1141 assert(buffer && buffer->shm_buffer);
1142 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1143 assert(ev->surface->width <= b->cursor_width);
1144 assert(ev->surface->height <= b->cursor_height);
1145
1146 memset(buf, 0, sizeof buf);
1147 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1148 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1149
1150 wl_shm_buffer_begin_access(buffer->shm_buffer);
1151 for (i = 0; i < ev->surface->height; i++)
1152 memcpy(buf + i * b->cursor_width,
1153 s + i * stride,
1154 ev->surface->width * 4);
1155 wl_shm_buffer_end_access(buffer->shm_buffer);
1156
1157 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1158 weston_log("failed update cursor: %m\n");
1159}
1160
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001161static void
1162drm_output_set_cursor(struct drm_output *output)
1163{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001165 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001166 struct drm_backend *b =
1167 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001168 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001169 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001170 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001171
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 output->cursor_view = NULL;
1173 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001174 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001175 output->cursor_plane.x = INT32_MIN;
1176 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001177 return;
1178 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001179
Neil Robertse5051712013-11-13 15:44:06 +00001180 buffer = ev->surface->buffer_ref.buffer;
1181
1182 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001183 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001184 pixman_region32_fini(&output->cursor_plane.damage);
1185 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001186 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001187 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001188
Pekka Paalanend0ead482014-06-16 12:05:40 +03001189 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001190 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001191 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1192 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001193 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001194 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001195 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001196 }
1197
Derek Foremanbe428b32015-11-24 11:39:38 -06001198 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001199
1200 /* From global to output space, output transform is guaranteed to be
1201 * NORMAL by drm_output_prepare_cursor_view().
1202 */
1203 x = (x - output->base.x) * output->base.current_scale;
1204 y = (y - output->base.y) * output->base.current_scale;
1205
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001206 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001207 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001208 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001209 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001210 }
1211
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001212 output->cursor_plane.x = x;
1213 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001214 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001215}
1216
Jesse Barnes58ef3792012-02-23 09:45:49 -05001217static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001218drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001219{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001220 struct drm_backend *b =
1221 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001222 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001224 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001225 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001226
1227 /*
1228 * Find a surface for each sprite in the output using some heuristics:
1229 * 1) size
1230 * 2) frequency of update
1231 * 3) opacity (though some hw might support alpha blending)
1232 * 4) clipping (this can be fixed with color keys)
1233 *
1234 * The idea is to save on blitting since this should save power.
1235 * If we can get a large video surface on the sprite for example,
1236 * the main display surface may not need to update at all, and
1237 * the client buffer can be used directly for the sprite surface
1238 * as we do for flipping full screen surfaces.
1239 */
1240 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001241 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242
Giulio Camuffo954f1832014-10-11 18:27:30 +03001243 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001244 struct weston_surface *es = ev->surface;
1245
1246 /* Test whether this buffer can ever go into a plane:
1247 * non-shm, or small enough to be a cursor.
1248 *
1249 * Also, keep a reference when using the pixman renderer.
1250 * That makes it possible to do a seamless switch to the GL
1251 * renderer and since the pixman renderer keeps a reference
1252 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001253 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001254 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001255 (es->buffer_ref.buffer &&
1256 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001257 (ev->surface->width <= b->cursor_width &&
1258 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001259 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001260 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001261 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001262
Jesse Barnes58ef3792012-02-23 09:45:49 -05001263 pixman_region32_init(&surface_overlap);
1264 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001266
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001267 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001268 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001269 next_plane = primary;
1270 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001272 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001274 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001276 if (next_plane == NULL)
1277 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001278
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001280
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001281 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001282 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001284
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001285 if (next_plane == primary ||
1286 next_plane == &output->cursor_plane) {
1287 /* cursor plane involves a copy */
1288 ev->psf_flags = 0;
1289 } else {
1290 /* All other planes are a direct scanout of a
1291 * single client buffer.
1292 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001293 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001294 }
1295
Jesse Barnes58ef3792012-02-23 09:45:49 -05001296 pixman_region32_fini(&surface_overlap);
1297 }
1298 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001299}
1300
Matt Roper361d2ad2011-08-29 13:52:23 -07001301static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001302drm_output_fini_pixman(struct drm_output *output);
1303
1304static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001305drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001306{
1307 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001308 struct drm_backend *b =
1309 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001310 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001311
Xiong Zhangabd5d472013-10-11 14:43:07 +08001312 if (output->page_flip_pending) {
1313 output->destroy_pending = 1;
1314 weston_log("destroy output while page flip pending\n");
1315 return;
1316 }
1317
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001318 if (output->backlight)
1319 backlight_destroy(output->backlight);
1320
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001321 drmModeFreeProperty(output->dpms_prop);
1322
Matt Roper361d2ad2011-08-29 13:52:23 -07001323 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001324 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001325
1326 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001327 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001328 origcrtc->x, origcrtc->y,
1329 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001330 drmModeFreeCrtc(origcrtc);
1331
Giulio Camuffo954f1832014-10-11 18:27:30 +03001332 b->crtc_allocator &= ~(1 << output->crtc_id);
1333 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001334
Giulio Camuffo954f1832014-10-11 18:27:30 +03001335 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001336 drm_output_fini_pixman(output);
1337 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001338 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001339 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001340 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001341
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001342 weston_plane_release(&output->fb_plane);
1343 weston_plane_release(&output->cursor_plane);
1344
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001345 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001346
Matt Roper361d2ad2011-08-29 13:52:23 -07001347 free(output);
1348}
1349
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001350/**
1351 * Find the closest-matching mode for a given target
1352 *
1353 * Given a target mode, find the most suitable mode amongst the output's
1354 * current mode list to use, preferring the current mode if possible, to
1355 * avoid an expensive mode switch.
1356 *
1357 * @param output DRM output
1358 * @param target_mode Mode to attempt to match
1359 * @returns Pointer to a mode from the output's mode list
1360 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001361static struct drm_mode *
1362choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1363{
1364 struct drm_mode *tmp_mode = NULL, *mode;
1365
Hardeningff39efa2013-09-18 23:56:35 +02001366 if (output->base.current_mode->width == target_mode->width &&
1367 output->base.current_mode->height == target_mode->height &&
1368 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001369 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001370 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001371
1372 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1373 if (mode->mode_info.hdisplay == target_mode->width &&
1374 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001375 if (mode->base.refresh == target_mode->refresh ||
1376 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001377 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001378 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001379 tmp_mode = mode;
1380 }
1381 }
1382
1383 return tmp_mode;
1384}
1385
1386static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001387drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001388static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001389drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001390
1391static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001392drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1393{
1394 struct drm_output *output;
1395 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001396 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001397
1398 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001399 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001400 return -1;
1401 }
1402
1403 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001404 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001405 return -1;
1406 }
1407
Giulio Camuffo954f1832014-10-11 18:27:30 +03001408 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001409 output = (struct drm_output *)output_base;
1410 drm_mode = choose_mode (output, mode);
1411
1412 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001413 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001414 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001415 }
1416
Hardeningff39efa2013-09-18 23:56:35 +02001417 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001418 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001419
Hardeningff39efa2013-09-18 23:56:35 +02001420 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001421
Hardeningff39efa2013-09-18 23:56:35 +02001422 output->base.current_mode = &drm_mode->base;
1423 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001424 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1425
Alex Wub7b8bda2012-04-17 17:20:48 +08001426 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001427 drm_output_release_fb(output, output->current);
1428 drm_output_release_fb(output, output->next);
1429 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001430
Giulio Camuffo954f1832014-10-11 18:27:30 +03001431 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001432 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001433 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001434 weston_log("failed to init output pixman state with "
1435 "new mode\n");
1436 return -1;
1437 }
1438 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001439 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001440 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001441
Giulio Camuffo954f1832014-10-11 18:27:30 +03001442 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001443 weston_log("failed to init output egl state with "
1444 "new mode");
1445 return -1;
1446 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001447 }
1448
Alex Wub7b8bda2012-04-17 17:20:48 +08001449 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001450}
1451
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001452static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001453on_drm_input(int fd, uint32_t mask, void *data)
1454{
1455 drmEventContext evctx;
1456
1457 memset(&evctx, 0, sizeof evctx);
1458 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1459 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001460 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001461 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001462
1463 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001464}
1465
1466static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001467init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001468{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001469 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001470 uint64_t cap;
1471 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001472 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001473
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001474 sysnum = udev_device_get_sysnum(device);
1475 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001476 b->drm.id = atoi(sysnum);
1477 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001478 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001479 return -1;
1480 }
1481
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001482 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001483 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001484 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001485 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001486 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001487 udev_device_get_devnode(device));
1488 return -1;
1489 }
1490
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001491 weston_log("using %s\n", filename);
1492
Giulio Camuffo954f1832014-10-11 18:27:30 +03001493 b->drm.fd = fd;
1494 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001495
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001496 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1497 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001498 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001499 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001500 clk_id = CLOCK_REALTIME;
1501
Giulio Camuffo954f1832014-10-11 18:27:30 +03001502 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001503 weston_log("Error: failed to set presentation clock %d.\n",
1504 clk_id);
1505 return -1;
1506 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001507
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001508 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1509 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001510 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001511 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001512 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001513
1514 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1515 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001516 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001517 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001518 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001519
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001520 return 0;
1521}
1522
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001523static struct gbm_device *
1524create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001525{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001526 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001527
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001528 gl_renderer = weston_load_module("gl-renderer.so",
1529 "gl_renderer_interface");
1530 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001531 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001532
1533 /* GBM will load a dri driver, but even though they need symbols from
1534 * libglapi, in some version of Mesa they are not linked to it. Since
1535 * only the gl-renderer module links to it, the call above won't make
1536 * these symbols globally available, and loading the DRI driver fails.
1537 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1538 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1539
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001540 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001541
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001542 return gbm;
1543}
1544
Bryce Harringtonc056a982015-05-19 15:25:18 -07001545/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001546 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001547 *
1548 * This returns 0 if substitution isn't possible, but 0 might be a
1549 * legitimate format for other EGL platforms, so the caller is
1550 * responsible for checking for 0 before calling gl_renderer->create().
1551 *
1552 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1553 * but it's entirely possible we'll see this again on other implementations.
1554 */
1555static int
1556fallback_format_for(uint32_t format)
1557{
1558 switch (format) {
1559 case GBM_FORMAT_XRGB8888:
1560 return GBM_FORMAT_ARGB8888;
1561 case GBM_FORMAT_XRGB2101010:
1562 return GBM_FORMAT_ARGB2101010;
1563 default:
1564 return 0;
1565 }
1566}
1567
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001568static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001569drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001570{
Derek Foreman6d556372015-11-04 14:47:33 -06001571 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001572 b->gbm_format,
1573 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001574 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001575 };
Derek Foreman6d556372015-11-04 14:47:33 -06001576 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001577
Derek Foremanc4cfe852015-05-15 12:12:40 -05001578 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001579 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001580 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001581 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001582 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001583 gl_renderer->opaque_attribs,
1584 format,
1585 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001586 return -1;
1587 }
1588
1589 return 0;
1590}
1591
1592static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001593init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001594{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001595 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001596
Giulio Camuffo954f1832014-10-11 18:27:30 +03001597 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001598 return -1;
1599
Giulio Camuffo954f1832014-10-11 18:27:30 +03001600 if (drm_backend_create_gl_renderer(b) < 0) {
1601 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001602 return -1;
1603 }
1604
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001605 return 0;
1606}
1607
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001608static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001609init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001610{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001611 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001612}
1613
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001614/**
1615 * Add a mode to output's mode list
1616 *
1617 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1618 * output's mode list.
1619 *
1620 * @param output DRM output to add mode to
1621 * @param info DRM mode structure to add
1622 * @returns Newly-allocated Weston/DRM mode structure
1623 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001624static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001625drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001626{
1627 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001628 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001629
1630 mode = malloc(sizeof *mode);
1631 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001632 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001633
1634 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001635 mode->base.width = info->hdisplay;
1636 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001637
1638 /* Calculate higher precision (mHz) refresh rate */
1639 refresh = (info->clock * 1000000LL / info->htotal +
1640 info->vtotal / 2) / info->vtotal;
1641
1642 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1643 refresh *= 2;
1644 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1645 refresh /= 2;
1646 if (info->vscan > 1)
1647 refresh /= info->vscan;
1648
1649 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001650 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001651
1652 if (info->type & DRM_MODE_TYPE_PREFERRED)
1653 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1654
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001655 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1656
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001657 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001658}
1659
1660static int
1661drm_subpixel_to_wayland(int drm_value)
1662{
1663 switch (drm_value) {
1664 default:
1665 case DRM_MODE_SUBPIXEL_UNKNOWN:
1666 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1667 case DRM_MODE_SUBPIXEL_NONE:
1668 return WL_OUTPUT_SUBPIXEL_NONE;
1669 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1670 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1671 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1672 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1673 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1674 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1675 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1676 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1677 }
1678}
1679
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001680/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001681static uint32_t
1682drm_get_backlight(struct drm_output *output)
1683{
1684 long brightness, max_brightness, norm;
1685
1686 brightness = backlight_get_brightness(output->backlight);
1687 max_brightness = backlight_get_max_brightness(output->backlight);
1688
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001689 /* convert it on a scale of 0 to 255 */
1690 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001691
1692 return (uint32_t) norm;
1693}
1694
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001695/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001696static void
1697drm_set_backlight(struct weston_output *output_base, uint32_t value)
1698{
1699 struct drm_output *output = (struct drm_output *) output_base;
1700 long max_brightness, new_brightness;
1701
1702 if (!output->backlight)
1703 return;
1704
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001705 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001706 return;
1707
1708 max_brightness = backlight_get_max_brightness(output->backlight);
1709
1710 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001711 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001712
1713 backlight_set_brightness(output->backlight, new_brightness);
1714}
1715
1716static drmModePropertyPtr
1717drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1718{
1719 drmModePropertyPtr props;
1720 int i;
1721
1722 for (i = 0; i < connector->count_props; i++) {
1723 props = drmModeGetProperty(fd, connector->props[i]);
1724 if (!props)
1725 continue;
1726
1727 if (!strcmp(props->name, name))
1728 return props;
1729
1730 drmModeFreeProperty(props);
1731 }
1732
1733 return NULL;
1734}
1735
1736static void
1737drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1738{
1739 struct drm_output *output = (struct drm_output *) output_base;
1740 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001741 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001742 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001743
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001744 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001745 return;
1746
Daniel Stone36609c72015-06-18 07:49:02 +01001747 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1748 output->dpms_prop->prop_id, level);
1749 if (ret) {
1750 weston_log("DRM: DPMS: failed property set for %s\n",
1751 output->base.name);
1752 return;
1753 }
1754
1755 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001756}
1757
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001758static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001759 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1760 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1761 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1762 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1763 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1764 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1765 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1766 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1767 [DRM_MODE_CONNECTOR_Component] = "Component",
1768 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1769 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1770 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1771 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1772 [DRM_MODE_CONNECTOR_TV] = "TV",
1773 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001774#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001775 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1776 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001777#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001778};
1779
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001780static char *
1781make_connector_name(const drmModeConnector *con)
1782{
1783 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001784 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001785
1786 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1787 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001788
1789 if (!type_name)
1790 type_name = "UNNAMED";
1791
1792 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001793
1794 return strdup(name);
1795}
1796
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001797static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001798find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001799 drmModeRes *resources, drmModeConnector *connector)
1800{
1801 drmModeEncoder *encoder;
1802 uint32_t possible_crtcs;
1803 int i, j;
1804
1805 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001806 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001807 if (encoder == NULL) {
1808 weston_log("Failed to get encoder.\n");
1809 return -1;
1810 }
1811 possible_crtcs = encoder->possible_crtcs;
1812 drmModeFreeEncoder(encoder);
1813
1814 for (i = 0; i < resources->count_crtcs; i++) {
1815 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001816 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001817 return i;
1818 }
1819 }
1820
1821 return -1;
1822}
1823
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001824/* Init output state that depends on gl or gbm */
1825static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001826drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001827{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001828 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001829 output->gbm_format,
1830 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001831 };
1832 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001833
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001834 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001835 output->base.current_mode->width,
1836 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001837 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001838 GBM_BO_USE_SCANOUT |
1839 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001840 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001841 weston_log("failed to create gbm surface\n");
1842 return -1;
1843 }
1844
Derek Foremanc4cfe852015-05-15 12:12:40 -05001845 if (format[1])
1846 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001847 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001848 (EGLNativeWindowType)output->gbm_surface,
1849 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001850 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001851 format,
1852 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001853 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001854 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001855 return -1;
1856 }
1857
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001858 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001859
1860 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001861 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001862 continue;
1863
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001864 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001866 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001867 }
1868
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001869 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001870 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001871 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001872 }
1873
1874 return 0;
1875}
1876
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001877static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001878drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001879{
Hardeningff39efa2013-09-18 23:56:35 +02001880 int w = output->base.current_mode->width;
1881 int h = output->base.current_mode->height;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001882 unsigned int i;
1883
1884 /* FIXME error checking */
1885
1886 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001887 output->dumb[i] = drm_fb_create_dumb(b, w, h);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001888 if (!output->dumb[i])
1889 goto err;
1890
1891 output->image[i] =
Alexander Larsson0b135062013-05-28 16:23:36 +02001892 pixman_image_create_bits(PIXMAN_x8r8g8b8, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001893 output->dumb[i]->map,
1894 output->dumb[i]->stride);
1895 if (!output->image[i])
1896 goto err;
1897 }
1898
1899 if (pixman_renderer_output_create(&output->base) < 0)
1900 goto err;
1901
1902 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001903 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001904
1905 return 0;
1906
1907err:
1908 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1909 if (output->dumb[i])
1910 drm_fb_destroy_dumb(output->dumb[i]);
1911 if (output->image[i])
1912 pixman_image_unref(output->image[i]);
1913
1914 output->dumb[i] = NULL;
1915 output->image[i] = NULL;
1916 }
1917
1918 return -1;
1919}
1920
1921static void
1922drm_output_fini_pixman(struct drm_output *output)
1923{
1924 unsigned int i;
1925
1926 pixman_renderer_output_destroy(&output->base);
1927 pixman_region32_fini(&output->previous_damage);
1928
1929 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1930 drm_fb_destroy_dumb(output->dumb[i]);
1931 pixman_image_unref(output->image[i]);
1932 output->dumb[i] = NULL;
1933 output->image[i] = NULL;
1934 }
1935}
1936
Richard Hughes2b2092a2013-04-24 14:58:02 +01001937static void
1938edid_parse_string(const uint8_t *data, char text[])
1939{
1940 int i;
1941 int replaced = 0;
1942
1943 /* this is always 12 bytes, but we can't guarantee it's null
1944 * terminated or not junk. */
1945 strncpy(text, (const char *) data, 12);
1946
Bryce Harrington9c7de162015-08-28 13:04:26 -07001947 /* guarantee our new string is null-terminated */
1948 text[12] = '\0';
1949
Richard Hughes2b2092a2013-04-24 14:58:02 +01001950 /* remove insane chars */
1951 for (i = 0; text[i] != '\0'; i++) {
1952 if (text[i] == '\n' ||
1953 text[i] == '\r') {
1954 text[i] = '\0';
1955 break;
1956 }
1957 }
1958
1959 /* ensure string is printable */
1960 for (i = 0; text[i] != '\0'; i++) {
1961 if (!isprint(text[i])) {
1962 text[i] = '-';
1963 replaced++;
1964 }
1965 }
1966
1967 /* if the string is random junk, ignore the string */
1968 if (replaced > 4)
1969 text[0] = '\0';
1970}
1971
1972#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1973#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1974#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1975#define EDID_OFFSET_DATA_BLOCKS 0x36
1976#define EDID_OFFSET_LAST_BLOCK 0x6c
1977#define EDID_OFFSET_PNPID 0x08
1978#define EDID_OFFSET_SERIAL 0x0c
1979
1980static int
1981edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1982{
1983 int i;
1984 uint32_t serial_number;
1985
1986 /* check header */
1987 if (length < 128)
1988 return -1;
1989 if (data[0] != 0x00 || data[1] != 0xff)
1990 return -1;
1991
1992 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1993 * /--08--\/--09--\
1994 * 7654321076543210
1995 * |\---/\---/\---/
1996 * R C1 C2 C3 */
1997 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1998 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1999 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2000 edid->pnp_id[3] = '\0';
2001
2002 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2003 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2004 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2005 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2006 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2007 if (serial_number > 0)
2008 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2009
2010 /* parse EDID data */
2011 for (i = EDID_OFFSET_DATA_BLOCKS;
2012 i <= EDID_OFFSET_LAST_BLOCK;
2013 i += 18) {
2014 /* ignore pixel clock data */
2015 if (data[i] != 0)
2016 continue;
2017 if (data[i+2] != 0)
2018 continue;
2019
2020 /* any useful blocks? */
2021 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2022 edid_parse_string(&data[i+5],
2023 edid->monitor_name);
2024 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2025 edid_parse_string(&data[i+5],
2026 edid->serial_number);
2027 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2028 edid_parse_string(&data[i+5],
2029 edid->eisa_id);
2030 }
2031 }
2032 return 0;
2033}
2034
2035static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002036find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002037 struct drm_output *output,
2038 drmModeConnector *connector)
2039{
2040 drmModePropertyBlobPtr edid_blob = NULL;
2041 drmModePropertyPtr property;
2042 int i;
2043 int rc;
2044
2045 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002046 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002047 if (!property)
2048 continue;
2049 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2050 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002052 connector->prop_values[i]);
2053 }
2054 drmModeFreeProperty(property);
2055 }
2056 if (!edid_blob)
2057 return;
2058
2059 rc = edid_parse(&output->edid,
2060 edid_blob->data,
2061 edid_blob->length);
2062 if (!rc) {
2063 weston_log("EDID data '%s', '%s', '%s'\n",
2064 output->edid.pnp_id,
2065 output->edid.monitor_name,
2066 output->edid.serial_number);
2067 if (output->edid.pnp_id[0] != '\0')
2068 output->base.make = output->edid.pnp_id;
2069 if (output->edid.monitor_name[0] != '\0')
2070 output->base.model = output->edid.monitor_name;
2071 if (output->edid.serial_number[0] != '\0')
2072 output->base.serial_number = output->edid.serial_number;
2073 }
2074 drmModeFreePropertyBlob(edid_blob);
2075}
2076
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002077
2078
2079static int
2080parse_modeline(const char *s, drmModeModeInfo *mode)
2081{
2082 char hsync[16];
2083 char vsync[16];
2084 float fclock;
2085
2086 mode->type = DRM_MODE_TYPE_USERDEF;
2087 mode->hskew = 0;
2088 mode->vscan = 0;
2089 mode->vrefresh = 0;
2090 mode->flags = 0;
2091
Rob Bradford307e09e2013-07-26 16:29:40 +01002092 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002093 &fclock,
2094 &mode->hdisplay,
2095 &mode->hsync_start,
2096 &mode->hsync_end,
2097 &mode->htotal,
2098 &mode->vdisplay,
2099 &mode->vsync_start,
2100 &mode->vsync_end,
2101 &mode->vtotal, hsync, vsync) != 11)
2102 return -1;
2103
2104 mode->clock = fclock * 1000;
2105 if (strcmp(hsync, "+hsync") == 0)
2106 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2107 else if (strcmp(hsync, "-hsync") == 0)
2108 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2109 else
2110 return -1;
2111
2112 if (strcmp(vsync, "+vsync") == 0)
2113 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2114 else if (strcmp(vsync, "-vsync") == 0)
2115 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2116 else
2117 return -1;
2118
2119 return 0;
2120}
2121
Rob Bradford66bd9f52013-06-25 18:56:42 +01002122static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002123setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002124 struct weston_output *output,
2125 const char *s)
2126{
2127 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002128 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002129 struct udev_seat *seat;
2130
Giulio Camuffo954f1832014-10-11 18:27:30 +03002131 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002132 if (!seat)
2133 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002134
Derek Foreman0720ea32015-07-15 13:00:35 -05002135 seat->base.output = output;
2136
Derek Foreman1281a362015-07-31 16:55:32 -05002137 pointer = weston_seat_get_pointer(&seat->base);
2138 if (pointer)
2139 weston_pointer_clamp(pointer,
2140 &pointer->x,
2141 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002142 }
2143}
2144
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002145static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002146parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002147{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002148 int ret = 0;
2149
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002150 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002151 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002152 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002153 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002154 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002155 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002156 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002157 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002158 else {
2159 weston_log("fatal: unrecognized pixel format: %s\n", s);
2160 ret = -1;
2161 }
2162
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002163 return ret;
2164}
2165
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002166/**
2167 * Choose suitable mode for an output
2168 *
2169 * Find the most suitable mode to use for initial setup (or reconfiguration on
2170 * hotplug etc) for a DRM output.
2171 *
2172 * @param output DRM output to choose mode for
2173 * @param kind Strategy and preference to use when choosing mode
2174 * @param width Desired width for this output
2175 * @param height Desired height for this output
2176 * @param current_mode Mode currently being displayed on this output
2177 * @param modeline Manually-entered mode (may be NULL)
2178 * @returns A mode from the output's mode list, or NULL if none available
2179 */
2180static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002181drm_output_choose_initial_mode(struct drm_backend *backend,
2182 struct drm_output *output,
2183 enum weston_drm_backend_output_mode mode,
2184 struct weston_drm_backend_output_config *config,
2185 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002186{
2187 struct drm_mode *preferred = NULL;
2188 struct drm_mode *current = NULL;
2189 struct drm_mode *configured = NULL;
2190 struct drm_mode *best = NULL;
2191 struct drm_mode *drm_mode;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002192 drmModeModeInfo modeline;
2193 int32_t width = 0;
2194 int32_t height = 0;
2195
2196 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
2197 if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
2198 width = -1;
2199
2200 if (parse_modeline(config->modeline, &modeline) == 0) {
2201 configured = drm_output_add_mode(output, &modeline);
2202 if (!configured)
2203 return NULL;
2204 } else {
2205 weston_log("Invalid modeline \"%s\" for output %s\n",
2206 config->modeline, output->base.name);
2207 }
2208 }
2209 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002210
2211 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002212 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002213 height == drm_mode->base.height)
2214 configured = drm_mode;
2215
comic fans7a5c5622016-03-17 14:29:27 +02002216 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002217 sizeof *current_mode) == 0)
2218 current = drm_mode;
2219
2220 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2221 preferred = drm_mode;
2222
2223 best = drm_mode;
2224 }
2225
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002226 if (current == NULL && current_mode->clock != 0) {
2227 current = drm_output_add_mode(output, current_mode);
2228 if (!current)
2229 return NULL;
2230 }
2231
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002232 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002233 configured = current;
2234
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002235 if (configured)
2236 return configured;
2237
2238 if (preferred)
2239 return preferred;
2240
2241 if (current)
2242 return current;
2243
2244 if (best)
2245 return best;
2246
2247 weston_log("no available modes for %s\n", output->base.name);
2248 return NULL;
2249}
2250
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002251static int
2252connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2253 drmModeModeInfo *mode)
2254{
2255 drmModeEncoder *encoder;
2256 drmModeCrtc *crtc;
2257
2258 /* Get the current mode on the crtc that's currently driving
2259 * this connector. */
2260 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2261 memset(mode, 0, sizeof *mode);
2262 if (encoder != NULL) {
2263 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2264 drmModeFreeEncoder(encoder);
2265 if (crtc == NULL)
2266 return -1;
2267 if (crtc->mode_valid)
2268 *mode = crtc->mode;
2269 drmModeFreeCrtc(crtc);
2270 }
2271
2272 return 0;
2273}
2274
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002275/**
2276 * Create and configure a Weston output structure
2277 *
2278 * Given a DRM connector, create a matching drm_output structure and add it
2279 * to Weston's output list.
2280 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002281 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002282 * @param resources DRM resources for this device
2283 * @param connector DRM connector to use for this new output
2284 * @param x Horizontal offset to use into global co-ordinate space
2285 * @param y Vertical offset to use into global co-ordinate space
2286 * @param drm_device udev device pointer
2287 * @returns 0 on success, or -1 on failure
2288 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002289static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002290create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002291 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002292 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002293 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002294{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002295 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002296 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002297 struct weston_mode *m;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002298
2299 drmModeModeInfo crtc_mode;
2300 int i;
2301 enum weston_drm_backend_output_mode mode;
2302 struct weston_drm_backend_output_config config = {{ 0 }};
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002303
Giulio Camuffo954f1832014-10-11 18:27:30 +03002304 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002305 if (i < 0) {
2306 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002307 return -1;
2308 }
2309
Peter Huttererf3d62272013-08-08 11:57:05 +10002310 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002311 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002312 return -1;
2313
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002314 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002315 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002316 output->base.make = "unknown";
2317 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002318 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002319 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002320
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002321 mode = b->configure_output(b->compositor, b->use_current_mode,
2322 output->base.name, &config);
2323 if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002324 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002325
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002326 setup_output_seat_constraint(b, &output->base,
2327 config.seat ? config.seat : "");
2328 free(config.seat);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002329
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002330 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002331 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002332 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002333 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002334 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002335
Giulio Camuffo954f1832014-10-11 18:27:30 +03002336 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2337 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002338
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002339 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2340 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002341
David Herrmann0f0d54e2011-12-08 17:05:45 +01002342 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002343 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002344 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002345 goto err_free;
2346 }
2347
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002348 if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002349 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002350 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002351 0, 0, 0, 0, 0, NULL);
2352 goto err_free;
2353 }
2354
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002355 current = drm_output_choose_initial_mode(b, output, mode, &config,
2356 &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002357 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002358 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002359 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002360 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002361
Giulio Camuffo954f1832014-10-11 18:27:30 +03002362 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002363 connector->mmWidth, connector->mmHeight,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002364 config.base.transform, config.base.scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002365
Giulio Camuffo954f1832014-10-11 18:27:30 +03002366 if (b->use_pixman) {
2367 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002368 weston_log("Failed to init output pixman state\n");
2369 goto err_output;
2370 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002371 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002372 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002373 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002374 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002375
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002376 output->backlight = backlight_init(drm_device,
2377 connector->connector_type);
2378 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002379 weston_log("Initialized backlight, device %s\n",
2380 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002381 output->base.set_backlight = drm_set_backlight;
2382 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002383 } else {
2384 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002385 }
2386
Giulio Camuffo954f1832014-10-11 18:27:30 +03002387 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002388
Giulio Camuffo954f1832014-10-11 18:27:30 +03002389 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002390 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2391 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002392
Jonas Ådahle5a12252013-04-05 23:07:11 +02002393 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002394 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002395 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002396 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002397 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002398 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002399
Richard Hughese7299962013-05-01 21:52:12 +01002400 output->base.gamma_size = output->original_crtc->gamma_size;
2401 output->base.set_gamma = drm_output_set_gamma;
2402
Derek Foremanb0427562016-02-05 15:55:20 -06002403 weston_plane_init(&output->cursor_plane, b->compositor,
2404 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002405 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002406
Giulio Camuffo954f1832014-10-11 18:27:30 +03002407 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2408 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2409 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002410
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002411 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002412 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002413 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002414 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002415 m->width, m->height, m->refresh / 1000.0,
2416 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2417 ", preferred" : "",
2418 m->flags & WL_OUTPUT_MODE_CURRENT ?
2419 ", current" : "",
2420 connector->count_modes == 0 ?
2421 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002422
Mario Kleiner80817042015-06-21 21:25:11 +02002423 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2424 output->base.native_mode = output->base.current_mode;
2425 output->base.native_scale = output->base.current_scale;
2426
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002427 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002428
John Kåre Alsaker94659272012-11-13 19:10:18 +01002429err_output:
2430 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002431err_free:
2432 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2433 base.link) {
2434 wl_list_remove(&drm_mode->base.link);
2435 free(drm_mode);
2436 }
2437
2438 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002439 b->crtc_allocator &= ~(1 << output->crtc_id);
2440 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002441 free(output);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002442 free(config.modeline);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002443
David Herrmann0f0d54e2011-12-08 17:05:45 +01002444 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002445}
2446
Jesse Barnes58ef3792012-02-23 09:45:49 -05002447static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002448create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002449{
2450 struct drm_sprite *sprite;
2451 drmModePlaneRes *plane_res;
2452 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002453 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002454
Giulio Camuffo954f1832014-10-11 18:27:30 +03002455 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002456 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002457 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002458 strerror(errno));
2459 return;
2460 }
2461
2462 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002463 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002464 if (!plane)
2465 continue;
2466
Peter Huttererf3d62272013-08-08 11:57:05 +10002467 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002468 plane->count_formats));
2469 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002470 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002471 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002472 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002473 continue;
2474 }
2475
Jesse Barnes58ef3792012-02-23 09:45:49 -05002476 sprite->possible_crtcs = plane->possible_crtcs;
2477 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002478 sprite->current = NULL;
2479 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002480 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002481 sprite->count_formats = plane->count_formats;
2482 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002483 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002484 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002485 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2486 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2487 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002488
Giulio Camuffo954f1832014-10-11 18:27:30 +03002489 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002490 }
2491
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002492 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002493}
2494
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002495static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002496destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002497{
2498 struct drm_sprite *sprite, *next;
2499 struct drm_output *output;
2500
Giulio Camuffo954f1832014-10-11 18:27:30 +03002501 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002502 struct drm_output, base.link);
2503
Giulio Camuffo954f1832014-10-11 18:27:30 +03002504 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2505 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002506 sprite->plane_id,
2507 output->crtc_id, 0, 0,
2508 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002509 drm_output_release_fb(output, sprite->current);
2510 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002511 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002512 free(sprite);
2513 }
2514}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002515
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002516static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002517create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002518 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002519{
2520 drmModeConnector *connector;
2521 drmModeRes *resources;
2522 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002523 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002524
Giulio Camuffo954f1832014-10-11 18:27:30 +03002525 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002526 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002527 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002528 return -1;
2529 }
2530
Giulio Camuffo954f1832014-10-11 18:27:30 +03002531 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2532 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002533 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002534 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002535 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002536
Giulio Camuffo954f1832014-10-11 18:27:30 +03002537 b->min_width = resources->min_width;
2538 b->max_width = resources->max_width;
2539 b->min_height = resources->min_height;
2540 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002541
Giulio Camuffo954f1832014-10-11 18:27:30 +03002542 b->num_crtcs = resources->count_crtcs;
2543 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002544
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002545 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002546 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002547 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002548 if (connector == NULL)
2549 continue;
2550
2551 if (connector->connection == DRM_MODE_CONNECTED &&
2552 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002553 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002554 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002555 connector, x, y,
2556 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002557 drmModeFreeConnector(connector);
2558 continue;
2559 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002560
Giulio Camuffo954f1832014-10-11 18:27:30 +03002561 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002562 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002563 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002564 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002565
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002566 drmModeFreeConnector(connector);
2567 }
2568
Giulio Camuffo954f1832014-10-11 18:27:30 +03002569 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002570 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002571 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002572 return -1;
2573 }
2574
2575 drmModeFreeResources(resources);
2576
2577 return 0;
2578}
2579
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002580static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002581update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002582{
2583 drmModeConnector *connector;
2584 drmModeRes *resources;
2585 struct drm_output *output, *next;
2586 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002587 uint32_t connected = 0, disconnects = 0;
2588 int i;
2589
Giulio Camuffo954f1832014-10-11 18:27:30 +03002590 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002591 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002592 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002593 return;
2594 }
2595
2596 /* collect new connects */
2597 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002598 int connector_id = resources->connectors[i];
2599
Giulio Camuffo954f1832014-10-11 18:27:30 +03002600 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002601 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002602 continue;
2603
David Herrmann7551cff2011-12-08 17:05:43 +01002604 if (connector->connection != DRM_MODE_CONNECTED) {
2605 drmModeFreeConnector(connector);
2606 continue;
2607 }
2608
Benjamin Franzke117483d2011-08-30 11:38:26 +02002609 connected |= (1 << connector_id);
2610
Giulio Camuffo954f1832014-10-11 18:27:30 +03002611 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002612 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002613 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002614 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002615
2616 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002617 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002618 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002619 else
2620 x = 0;
2621 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002622 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002623 connector, x, y,
2624 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002625 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002626
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002627 }
2628 drmModeFreeConnector(connector);
2629 }
2630 drmModeFreeResources(resources);
2631
Giulio Camuffo954f1832014-10-11 18:27:30 +03002632 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002633 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002635 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002636 if (disconnects & (1 << output->connector_id)) {
2637 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002638 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002640 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002641 }
2642 }
2643 }
2644
Daniel Stonef556ebe2015-05-21 08:28:58 +01002645 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002646 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002647 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002648}
2649
2650static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002651udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002652{
David Herrmannd7488c22012-03-11 20:05:21 +01002653 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002654 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002655
2656 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002657 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002658 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002659
David Herrmann6ac52db2012-03-11 20:05:22 +01002660 val = udev_device_get_property_value(device, "HOTPLUG");
2661 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002662 return 0;
2663
David Herrmann6ac52db2012-03-11 20:05:22 +01002664 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002665}
2666
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002667static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002668udev_drm_event(int fd, uint32_t mask, void *data)
2669{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002670 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002671 struct udev_device *event;
2672
Giulio Camuffo954f1832014-10-11 18:27:30 +03002673 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002674
Giulio Camuffo954f1832014-10-11 18:27:30 +03002675 if (udev_event_is_hotplug(b, event))
2676 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002677
2678 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002679
2680 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002681}
2682
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002683static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002684drm_restore(struct weston_compositor *ec)
2685{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002686 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002687}
2688
2689static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002690drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002691{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002692 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002693
Giulio Camuffo954f1832014-10-11 18:27:30 +03002694 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002695
Giulio Camuffo954f1832014-10-11 18:27:30 +03002696 wl_event_source_remove(b->udev_drm_source);
2697 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002698
Giulio Camuffo954f1832014-10-11 18:27:30 +03002699 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002700
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002701 weston_compositor_shutdown(ec);
2702
Giulio Camuffo954f1832014-10-11 18:27:30 +03002703 if (b->gbm)
2704 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002705
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002707
Giulio Camuffo954f1832014-10-11 18:27:30 +03002708 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002709 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002710}
2711
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002712static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002713drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002714{
2715 struct drm_output *output;
2716 struct drm_mode *drm_mode;
2717 int ret;
2718
Giulio Camuffo954f1832014-10-11 18:27:30 +03002719 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002720 if (!output->current) {
2721 /* If something that would cause the output to
2722 * switch mode happened while in another vt, we
2723 * might not have a current drm_fb. In that case,
2724 * schedule a repaint and let drm_output_repaint
2725 * handle setting the mode. */
2726 weston_output_schedule_repaint(&output->base);
2727 continue;
2728 }
2729
Hardeningff39efa2013-09-18 23:56:35 +02002730 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002731 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002732 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002733 &output->connector_id, 1,
2734 &drm_mode->mode_info);
2735 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002736 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002737 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002738 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002739 output->base.x, output->base.y);
2740 }
2741 }
2742}
2743
2744static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002745session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002746{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002747 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002748 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002749 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002750 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002751
Giulio Camuffo954f1832014-10-11 18:27:30 +03002752 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002753 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002754 compositor->state = b->prev_state;
2755 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002756 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002757 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002758 } else {
2759 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002760 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002761
Giulio Camuffo954f1832014-10-11 18:27:30 +03002762 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002763 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002764
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002765 /* If we have a repaint scheduled (either from a
2766 * pending pageflip or the idle handler), make sure we
2767 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002768 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002769 * further attemps at repainting. When we switch
2770 * back, we schedule a repaint, which will process
2771 * pending frame callbacks. */
2772
Giulio Camuffo954f1832014-10-11 18:27:30 +03002773 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002774 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002775 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002776 }
2777
Giulio Camuffo954f1832014-10-11 18:27:30 +03002778 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002779 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002780
Giulio Camuffo954f1832014-10-11 18:27:30 +03002781 wl_list_for_each(sprite, &b->sprite_list, link)
2782 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002783 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002784 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002785 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002786 };
2787}
2788
David Herrmann0af066f2012-10-29 19:21:16 +01002789/*
2790 * Find primary GPU
2791 * Some systems may have multiple DRM devices attached to a single seat. This
2792 * function loops over all devices and tries to find a PCI device with the
2793 * boot_vga sysfs attribute set to 1.
2794 * If no such device is found, the first DRM device reported by udev is used.
2795 */
2796static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002797find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002798{
2799 struct udev_enumerate *e;
2800 struct udev_list_entry *entry;
2801 const char *path, *device_seat, *id;
2802 struct udev_device *device, *drm_device, *pci;
2803
Giulio Camuffo954f1832014-10-11 18:27:30 +03002804 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002805 udev_enumerate_add_match_subsystem(e, "drm");
2806 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2807
2808 udev_enumerate_scan_devices(e);
2809 drm_device = NULL;
2810 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2811 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002812 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002813 if (!device)
2814 continue;
2815 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2816 if (!device_seat)
2817 device_seat = default_seat;
2818 if (strcmp(device_seat, seat)) {
2819 udev_device_unref(device);
2820 continue;
2821 }
2822
2823 pci = udev_device_get_parent_with_subsystem_devtype(device,
2824 "pci", NULL);
2825 if (pci) {
2826 id = udev_device_get_sysattr_value(pci, "boot_vga");
2827 if (id && !strcmp(id, "1")) {
2828 if (drm_device)
2829 udev_device_unref(drm_device);
2830 drm_device = device;
2831 break;
2832 }
2833 }
2834
2835 if (!drm_device)
2836 drm_device = device;
2837 else
2838 udev_device_unref(device);
2839 }
2840
2841 udev_enumerate_unref(e);
2842 return drm_device;
2843}
2844
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002845static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002846planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2847 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002848{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002850
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002851 switch (key) {
2852 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002853 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002854 break;
2855 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002856 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002857 break;
2858 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002859 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002860 break;
2861 default:
2862 break;
2863 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002864}
2865
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002866#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002867static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002868recorder_destroy(struct drm_output *output)
2869{
2870 vaapi_recorder_destroy(output->recorder);
2871 output->recorder = NULL;
2872
2873 output->base.disable_planes--;
2874
2875 wl_list_remove(&output->recorder_frame_listener.link);
2876 weston_log("[libva recorder] done\n");
2877}
2878
2879static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002880recorder_frame_notify(struct wl_listener *listener, void *data)
2881{
2882 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002883 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002884 int fd, ret;
2885
2886 output = container_of(listener, struct drm_output,
2887 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002888 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002889
2890 if (!output->recorder)
2891 return;
2892
Giulio Camuffo954f1832014-10-11 18:27:30 +03002893 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002894 DRM_CLOEXEC, &fd);
2895 if (ret) {
2896 weston_log("[libva recorder] "
2897 "failed to create prime fd for front buffer\n");
2898 return;
2899 }
2900
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002901 ret = vaapi_recorder_frame(output->recorder, fd,
2902 output->current->stride);
2903 if (ret < 0) {
2904 weston_log("[libva recorder] aborted: %m\n");
2905 recorder_destroy(output);
2906 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002907}
2908
2909static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002910create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002911 const char *filename)
2912{
2913 int fd;
2914 drm_magic_t magic;
2915
Giulio Camuffo954f1832014-10-11 18:27:30 +03002916 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002917 if (fd < 0)
2918 return NULL;
2919
2920 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002921 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002922
2923 return vaapi_recorder_create(fd, width, height, filename);
2924}
2925
2926static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002927recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002928 void *data)
2929{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002930 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931 struct drm_output *output;
2932 int width, height;
2933
Giulio Camuffo954f1832014-10-11 18:27:30 +03002934 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002935 struct drm_output, base.link);
2936
2937 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002938 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002939 weston_log("failed to start vaapi recorder: "
2940 "output format not supported\n");
2941 return;
2942 }
2943
Hardeningff39efa2013-09-18 23:56:35 +02002944 width = output->base.current_mode->width;
2945 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002946
2947 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002948 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002949 if (!output->recorder) {
2950 weston_log("failed to create vaapi recorder\n");
2951 return;
2952 }
2953
2954 output->base.disable_planes++;
2955
2956 output->recorder_frame_listener.notify = recorder_frame_notify;
2957 wl_signal_add(&output->base.frame_signal,
2958 &output->recorder_frame_listener);
2959
2960 weston_output_schedule_repaint(&output->base);
2961
2962 weston_log("[libva recorder] initialized\n");
2963 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002964 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002965 }
2966}
2967#else
2968static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07002969recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002970 void *data)
2971{
2972 weston_log("Compiled without libva support\n");
2973}
2974#endif
2975
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002976static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002977switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002978{
2979 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03002980 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002981
Giulio Camuffo954f1832014-10-11 18:27:30 +03002982 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002983 return;
2984
Pekka Paalanene4d231e2014-06-12 15:12:48 +03002985 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
2986
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002987 weston_log("Switching to GL renderer\n");
2988
Giulio Camuffo954f1832014-10-11 18:27:30 +03002989 b->gbm = create_gbm_device(b->drm.fd);
2990 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002991 weston_log("Failed to create gbm device. "
2992 "Aborting renderer switch\n");
2993 return;
2994 }
2995
Giulio Camuffo954f1832014-10-11 18:27:30 +03002996 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002997 pixman_renderer_output_destroy(&output->base);
2998
Giulio Camuffo954f1832014-10-11 18:27:30 +03002999 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003000
Giulio Camuffo954f1832014-10-11 18:27:30 +03003001 if (drm_backend_create_gl_renderer(b) < 0) {
3002 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003003 weston_log("Failed to create GL renderer. Quitting.\n");
3004 /* FIXME: we need a function to shutdown cleanly */
3005 assert(0);
3006 }
3007
Giulio Camuffo954f1832014-10-11 18:27:30 +03003008 wl_list_for_each(output, &b->compositor->output_list, base.link)
3009 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003010
Giulio Camuffo954f1832014-10-11 18:27:30 +03003011 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003012
3013 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3014 if (linux_dmabuf_setup(b->compositor) < 0)
3015 weston_log("Error: initializing dmabuf "
3016 "support failed.\n");
3017 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003018}
3019
3020static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003021renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3022 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003023{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003024 struct drm_backend *b =
3025 (struct drm_backend *) keyboard->seat->compositor;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003026
Giulio Camuffo954f1832014-10-11 18:27:30 +03003027 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003028}
3029
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030static struct drm_backend *
3031drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003032 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003033{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003034 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003035 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003036 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003037 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003038 const char *seat_id = default_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003039
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003040 weston_log("initializing drm backend\n");
3041
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 b = zalloc(sizeof *b);
3043 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003044 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003045
Pekka Paalanen68583832015-05-19 09:53:16 +03003046 /*
3047 * KMS support for hardware planes cannot properly synchronize
3048 * without nuclear page flip. Without nuclear/atomic, hw plane
3049 * and cursor plane updates would either tear or cause extra
3050 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003051 * to a fraction. For cursors, it's not so bad, so they are
3052 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003053 *
3054 * These can be enabled again when nuclear/atomic support lands.
3055 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003056 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003057 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003058 b->use_pixman = config->use_pixman;
3059 b->configure_output = config->configure_output;
3060 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003061
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003062 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3063 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003064
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003065 if (config->seat_id)
3066 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003067
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003068 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003069 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3070 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003071 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003072 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003073 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003074 goto err_compositor;
3075 }
3076
Giulio Camuffo954f1832014-10-11 18:27:30 +03003077 b->udev = udev_new();
3078 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003079 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003080 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003081 }
3082
Giulio Camuffo954f1832014-10-11 18:27:30 +03003083 b->session_listener.notify = session_notify;
3084 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003085
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003086 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003087 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003088 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003089 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003090 }
David Herrmann0af066f2012-10-29 19:21:16 +01003091 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003092
Giulio Camuffo954f1832014-10-11 18:27:30 +03003093 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003094 weston_log("failed to initialize kms\n");
3095 goto err_udev_dev;
3096 }
3097
Giulio Camuffo954f1832014-10-11 18:27:30 +03003098 if (b->use_pixman) {
3099 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003100 weston_log("failed to initialize pixman renderer\n");
3101 goto err_udev_dev;
3102 }
3103 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003104 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003105 weston_log("failed to initialize egl\n");
3106 goto err_udev_dev;
3107 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003108 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003109
Giulio Camuffo954f1832014-10-11 18:27:30 +03003110 b->base.destroy = drm_destroy;
3111 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003112
Giulio Camuffo954f1832014-10-11 18:27:30 +03003113 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003114
Bob Ham91880f12016-01-12 10:21:47 +00003115 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003116
Giulio Camuffo954f1832014-10-11 18:27:30 +03003117 wl_list_init(&b->sprite_list);
3118 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003119
Giulio Camuffo954f1832014-10-11 18:27:30 +03003120 if (udev_input_init(&b->input,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003121 compositor, b->udev, seat_id) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003122 weston_log("failed to create input devices\n");
3123 goto err_sprite;
3124 }
3125
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003126 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003127 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003128 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003129 }
3130
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003131 /* A this point we have some idea of whether or not we have a working
3132 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003133 if (!b->cursors_are_broken)
3134 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003135
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003136 path = NULL;
3137
Giulio Camuffo954f1832014-10-11 18:27:30 +03003138 loop = wl_display_get_event_loop(compositor->wl_display);
3139 b->drm_source =
3140 wl_event_loop_add_fd(loop, b->drm.fd,
3141 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003142
Giulio Camuffo954f1832014-10-11 18:27:30 +03003143 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3144 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003145 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003146 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003147 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003148 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003149 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003150 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003151 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003152 udev_monitor_get_fd(b->udev_monitor),
3153 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003154
Giulio Camuffo954f1832014-10-11 18:27:30 +03003155 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003156 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003157 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003158 }
3159
Daniel Stonea96b93c2012-06-22 14:04:37 +01003160 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003161
Giulio Camuffo954f1832014-10-11 18:27:30 +03003162 weston_compositor_add_debug_binding(compositor, KEY_O,
3163 planes_binding, b);
3164 weston_compositor_add_debug_binding(compositor, KEY_C,
3165 planes_binding, b);
3166 weston_compositor_add_debug_binding(compositor, KEY_V,
3167 planes_binding, b);
3168 weston_compositor_add_debug_binding(compositor, KEY_Q,
3169 recorder_binding, b);
3170 weston_compositor_add_debug_binding(compositor, KEY_W,
3171 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003172
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003173 if (compositor->renderer->import_dmabuf) {
3174 if (linux_dmabuf_setup(compositor) < 0)
3175 weston_log("Error: initializing dmabuf "
3176 "support failed.\n");
3177 }
3178
Giulio Camuffo954f1832014-10-11 18:27:30 +03003179 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003180
Giulio Camuffo954f1832014-10-11 18:27:30 +03003181 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003182
3183err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003184 wl_event_source_remove(b->udev_drm_source);
3185 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003186err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003187 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003188err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003189 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003190err_sprite:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003191 gbm_device_destroy(b->gbm);
3192 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003193err_udev_dev:
3194 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003195err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003196 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003197err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003198 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003199err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003200 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003202 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003203}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003204
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003205static void
3206config_init_to_defaults(struct weston_drm_backend_config *config)
3207{
3208}
3209
Giulio Camuffo954f1832014-10-11 18:27:30 +03003210WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003211backend_init(struct weston_compositor *compositor,
3212 int *argc, char *argv[],
3213 struct weston_config *wc,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003214 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003215{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003217 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003218
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003219 if (config_base == NULL ||
3220 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3221 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3222 weston_log("drm backend config structure is invalid\n");
3223 return -1;
3224 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003225
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003226 config_init_to_defaults(&config);
3227 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003228
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003229 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003230 if (b == NULL)
3231 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003232
Giulio Camuffo954f1832014-10-11 18:27:30 +03003233 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003234}