blob: f903a3b4a0454e279b4afd22870a98f925551a12 [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 *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300265drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
266 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200267{
268 struct drm_fb *fb;
269 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300270 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200271
272 struct drm_mode_create_dumb create_arg;
273 struct drm_mode_destroy_dumb destroy_arg;
274 struct drm_mode_map_dumb map_arg;
275
Peter Huttererf3d62272013-08-08 11:57:05 +1000276 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200277 if (!fb)
278 return NULL;
279
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300280 switch (format) {
281 case GBM_FORMAT_XRGB8888:
282 bpp = 32;
283 depth = 24;
284 break;
285 case GBM_FORMAT_RGB565:
286 bpp = depth = 16;
287 break;
288 default:
289 return NULL;
290 }
291
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700292 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300293 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200294 create_arg.width = width;
295 create_arg.height = height;
296
Giulio Camuffo954f1832014-10-11 18:27:30 +0300297 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200298 if (ret)
299 goto err_fb;
300
301 fb->handle = create_arg.handle;
302 fb->stride = create_arg.pitch;
303 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300304 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200305
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300306 ret = -1;
307
308 if (!b->no_addfb2) {
309 uint32_t handles[4], pitches[4], offsets[4];
310
311 handles[0] = fb->handle;
312 pitches[0] = fb->stride;
313 offsets[0] = 0;
314
315 ret = drmModeAddFB2(b->drm.fd, width, height,
316 format, handles, pitches, offsets,
317 &fb->fb_id, 0);
318 if (ret) {
319 weston_log("addfb2 failed: %m\n");
320 b->no_addfb2 = 1;
321 }
322 }
323
324 if (ret) {
325 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
326 fb->stride, fb->handle, &fb->fb_id);
327 }
328
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200329 if (ret)
330 goto err_bo;
331
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700332 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200333 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400334 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200335 if (ret)
336 goto err_add_fb;
337
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500338 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300339 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200340 if (fb->map == MAP_FAILED)
341 goto err_add_fb;
342
343 return fb;
344
345err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300346 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200347err_bo:
348 memset(&destroy_arg, 0, sizeof(destroy_arg));
349 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300350 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200351err_fb:
352 free(fb);
353 return NULL;
354}
355
356static void
357drm_fb_destroy_dumb(struct drm_fb *fb)
358{
359 struct drm_mode_destroy_dumb destroy_arg;
360
361 if (!fb->map)
362 return;
363
364 if (fb->fb_id)
365 drmModeRmFB(fb->fd, fb->fb_id);
366
367 weston_buffer_reference(&fb->buffer_ref, NULL);
368
369 munmap(fb->map, fb->size);
370
371 memset(&destroy_arg, 0, sizeof(destroy_arg));
372 destroy_arg.handle = fb->handle;
373 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
374
375 free(fb);
376}
377
378static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500379drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300380 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300381{
382 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200383 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200384 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300385 int ret;
386
387 if (fb)
388 return fb;
389
Bryce Harringtonde16d892014-11-20 22:21:57 -0800390 fb = zalloc(sizeof *fb);
391 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200392 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300393
394 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300395
396 width = gbm_bo_get_width(bo);
397 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200398 fb->stride = gbm_bo_get_stride(bo);
399 fb->handle = gbm_bo_get_handle(bo).u32;
400 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300401 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300402
Giulio Camuffo954f1832014-10-11 18:27:30 +0300403 if (backend->min_width > width || width > backend->max_width ||
404 backend->min_height > height ||
405 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200406 weston_log("bo geometry out of bounds\n");
407 goto err_free;
408 }
409
410 ret = -1;
411
Giulio Camuffo954f1832014-10-11 18:27:30 +0300412 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200413 handles[0] = fb->handle;
414 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200415 offsets[0] = 0;
416
Giulio Camuffo954f1832014-10-11 18:27:30 +0300417 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200418 format, handles, pitches, offsets,
419 &fb->fb_id, 0);
420 if (ret) {
421 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300422 backend->no_addfb2 = 1;
423 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200424 }
425 }
426
427 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300428 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200429 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200430
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300431 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200432 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200433 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300434 }
435
436 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
437
438 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200439
440err_free:
441 free(fb);
442 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300443}
444
445static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500446drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200447{
Pekka Paalanende685b82012-12-04 15:58:12 +0200448 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200449
450 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200451
Pekka Paalanende685b82012-12-04 15:58:12 +0200452 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200453}
454
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200455static void
456drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
457{
458 if (!fb)
459 return;
460
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200461 if (fb->map &&
462 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200463 drm_fb_destroy_dumb(fb);
464 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200465 if (fb->is_client_buffer)
466 gbm_bo_destroy(fb->bo);
467 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100468 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600469 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200470 }
471}
472
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500473static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200474drm_output_check_scanout_format(struct drm_output *output,
475 struct weston_surface *es, struct gbm_bo *bo)
476{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200477 uint32_t format;
478 pixman_region32_t r;
479
480 format = gbm_bo_get_format(bo);
481
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700482 if (format == GBM_FORMAT_ARGB8888) {
483 /* We can scanout an ARGB buffer if the surface's
484 * opaque region covers the whole output, but we have
485 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800486 pixman_region32_init_rect(&r, 0, 0,
487 output->base.width,
488 output->base.height);
489 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200490
491 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500492 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200493
494 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500495 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700496
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100497 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700498 return format;
499
500 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200501}
502
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400503static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200504drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500505 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500506{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300507 struct drm_backend *b =
508 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500509 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200510 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300511 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500512 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500513
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 if (ev->geometry.x != output->base.x ||
515 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300516 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200517 buffer->width != output->base.current_mode->width ||
518 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200519 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500520 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400521 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500522
Pekka Paalanen5580f222015-02-17 16:33:18 +0200523 if (ev->geometry.scissor_enabled)
524 return NULL;
525
Giulio Camuffo954f1832014-10-11 18:27:30 +0300526 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700527 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500528
Rob Bradford9b101872012-09-14 23:25:41 +0100529 /* Unable to use the buffer for scanout */
530 if (!bo)
531 return NULL;
532
Jason Ekstranda7af7042013-10-12 22:38:11 -0500533 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500534 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300535 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400536 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300537 }
538
Giulio Camuffo954f1832014-10-11 18:27:30 +0300539 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300540 if (!output->next) {
541 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400542 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300543 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500544
Pekka Paalanende685b82012-12-04 15:58:12 +0200545 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500546
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400547 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500548}
549
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500550static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200551drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400552{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300553 struct drm_backend *b =
554 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300555 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400556
Giulio Camuffo954f1832014-10-11 18:27:30 +0300557 output->base.compositor->renderer->repaint_output(&output->base,
558 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400559
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100560 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300561 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200562 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400563 return;
564 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300565
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100566 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300567 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200568 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100569 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300570 return;
571 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400572}
573
574static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200575drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
576{
577 struct weston_compositor *ec = output->base.compositor;
578 pixman_region32_t total_damage, previous_damage;
579
580 pixman_region32_init(&total_damage);
581 pixman_region32_init(&previous_damage);
582
583 pixman_region32_copy(&previous_damage, damage);
584
585 pixman_region32_union(&total_damage, damage, &output->previous_damage);
586 pixman_region32_copy(&output->previous_damage, &previous_damage);
587
588 output->current_image ^= 1;
589
590 output->next = output->dumb[output->current_image];
591 pixman_renderer_output_set_buffer(&output->base,
592 output->image[output->current_image]);
593
594 ec->renderer->repaint_output(&output->base, &total_damage);
595
596 pixman_region32_fini(&total_damage);
597 pixman_region32_fini(&previous_damage);
598}
599
600static void
601drm_output_render(struct drm_output *output, pixman_region32_t *damage)
602{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 struct weston_compositor *c = output->base.compositor;
604 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200605
Giulio Camuffo954f1832014-10-11 18:27:30 +0300606 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200607 drm_output_render_pixman(output, damage);
608 else
609 drm_output_render_gl(output, damage);
610
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 pixman_region32_subtract(&c->primary_plane.damage,
612 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200613}
614
615static void
Richard Hughese7299962013-05-01 21:52:12 +0100616drm_output_set_gamma(struct weston_output *output_base,
617 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
618{
619 int rc;
620 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300621 struct drm_backend *backend =
622 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100623
624 /* check */
625 if (output_base->gamma_size != size)
626 return;
627 if (!output->original_crtc)
628 return;
629
Giulio Camuffo954f1832014-10-11 18:27:30 +0300630 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100631 output->crtc_id,
632 size, r, g, b);
633 if (rc)
634 weston_log("set gamma failed: %m\n");
635}
636
Bryce Harringtonada4f072015-06-30 13:25:46 -0700637/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200638 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700639 * The pipe parameter indicates which CRTC is in use. Knowing this, we
640 * can determine which vblank sequence type to use for it. Traditional
641 * cards had only two CRTCs, with CRTC 0 using no special flags, and
642 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
643 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200644 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700645 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
646 * 0-31. If this is non-zero it indicates we're dealing with a
647 * multi-gpu situation and we need to calculate the vblank sync
648 * using DRM_BLANK_HIGH_CRTC_MASK.
649 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300650static unsigned int
651drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200652{
653 if (output->pipe > 1)
654 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
655 DRM_VBLANK_HIGH_CRTC_MASK;
656 else if (output->pipe > 0)
657 return DRM_VBLANK_SECONDARY;
658 else
659 return 0;
660}
661
David Herrmann1edf44c2013-10-22 17:11:26 +0200662static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500663drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400664 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100665{
666 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300667 struct drm_backend *backend =
668 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500669 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400670 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500671 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100672
Xiong Zhangabd5d472013-10-11 14:43:07 +0800673 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200674 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800675
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300676 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400677 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300678 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200679 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100680
Hardeningff39efa2013-09-18 23:56:35 +0200681 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200682 if (!output->current ||
683 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300684 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300685 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400686 &output->connector_id, 1,
687 &mode->mode_info);
688 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200689 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200690 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400691 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300692 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200693 }
694
Giulio Camuffo954f1832014-10-11 18:27:30 +0300695 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300696 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500697 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200698 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200699 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500700 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100701
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300702 output->page_flip_pending = 1;
703
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400704 drm_output_set_cursor(output);
705
Jesse Barnes58ef3792012-02-23 09:45:49 -0500706 /*
707 * Now, update all the sprite surfaces
708 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300709 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200710 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500711 drmVBlank vbl = {
712 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
713 .request.sequence = 1,
714 };
715
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200716 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200717 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500718 continue;
719
Giulio Camuffo954f1832014-10-11 18:27:30 +0300720 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200721 fb_id = s->next->fb_id;
722
Giulio Camuffo954f1832014-10-11 18:27:30 +0300723 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200724 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500725 s->dest_x, s->dest_y,
726 s->dest_w, s->dest_h,
727 s->src_x, s->src_y,
728 s->src_w, s->src_h);
729 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200730 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500731 ret, strerror(errno));
732
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200733 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500734
Jesse Barnes58ef3792012-02-23 09:45:49 -0500735 /*
736 * Queue a vblank signal so we know when the surface
737 * becomes active on the display or has been replaced.
738 */
739 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500741 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200742 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500743 ret, strerror(errno));
744 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300745
746 s->output = output;
747 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500748 }
749
David Herrmann1edf44c2013-10-22 17:11:26 +0200750 return 0;
751
752err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800753 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200754 if (output->next) {
755 drm_output_release_fb(output, output->next);
756 output->next = NULL;
757 }
758
759 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400760}
761
762static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200763drm_output_start_repaint_loop(struct weston_output *output_base)
764{
765 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300766 struct drm_backend *backend = (struct drm_backend *)
767 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200768 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200769 struct timespec ts, tnow;
770 struct timespec vbl2now;
771 int64_t refresh_nsec;
772 int ret;
773 drmVBlank vbl = {
774 .request.type = DRM_VBLANK_RELATIVE,
775 .request.sequence = 0,
776 .request.signal = 0,
777 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300778
Xiong Zhangabd5d472013-10-11 14:43:07 +0800779 if (output->destroy_pending)
780 return;
781
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300782 if (!output->current) {
783 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200784 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300785 }
786
Mario Kleinerf507ec32015-06-21 21:25:14 +0200787 /* Try to get current msc and timestamp via instant query */
788 vbl.request.type |= drm_waitvblank_pipe(output);
789 ret = drmWaitVBlank(backend->drm.fd, &vbl);
790
791 /* Error ret or zero timestamp means failure to get valid timestamp */
792 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
793 ts.tv_sec = vbl.reply.tval_sec;
794 ts.tv_nsec = vbl.reply.tval_usec * 1000;
795
796 /* Valid timestamp for most recent vblank - not stale?
797 * Stale ts could happen on Linux 3.17+, so make sure it
798 * is not older than 1 refresh duration since now.
799 */
800 weston_compositor_read_presentation_clock(backend->compositor,
801 &tnow);
802 timespec_sub(&vbl2now, &tnow, &ts);
803 refresh_nsec =
804 millihz_to_nsec(output->base.current_mode->refresh);
805 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
806 drm_output_update_msc(output, vbl.reply.sequence);
807 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200808 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200809 return;
810 }
811 }
812
813 /* Immediate query didn't provide valid timestamp.
814 * Use pageflip fallback.
815 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300816 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200817
Giulio Camuffo954f1832014-10-11 18:27:30 +0300818 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200819 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
820 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200821 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200822 }
David Herrmann3c688c52013-10-22 17:11:25 +0200823
824 return;
825
826finish_frame:
827 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300828 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200829 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200830 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200831}
832
833static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400834drm_output_update_msc(struct drm_output *output, unsigned int seq)
835{
836 uint64_t msc_hi = output->base.msc >> 32;
837
838 if (seq < (output->base.msc & 0xffffffff))
839 msc_hi++;
840
841 output->base.msc = (msc_hi << 32) + seq;
842}
843
844static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500845vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
846 void *data)
847{
848 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300849 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400850 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200851 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
852 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300853
Pekka Paalanen641307c2014-09-23 22:08:47 -0400854 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300855 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500856
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200857 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200858 s->current = s->next;
859 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300860
861 if (!output->page_flip_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 Oliveiraa7326962012-06-26 17:09:13 +0300865 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500866}
867
868static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800869drm_output_destroy(struct weston_output *output_base);
870
871static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400872page_flip_handler(int fd, unsigned int frame,
873 unsigned int sec, unsigned int usec, void *data)
874{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200875 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400876 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200877 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
878 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
879 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400880
Pekka Paalanen641307c2014-09-23 22:08:47 -0400881 drm_output_update_msc(output, frame);
882
Jonas Ådahle5a12252013-04-05 23:07:11 +0200883 /* We don't set page_flip_pending on start_repaint_loop, in that case
884 * we just want to page flip to the current buffer to get an accurate
885 * timestamp */
886 if (output->page_flip_pending) {
887 drm_output_release_fb(output, output->current);
888 output->current = output->next;
889 output->next = NULL;
890 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300891
Jonas Ådahle5a12252013-04-05 23:07:11 +0200892 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400893
Xiong Zhangabd5d472013-10-11 14:43:07 +0800894 if (output->destroy_pending)
895 drm_output_destroy(&output->base);
896 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400897 ts.tv_sec = sec;
898 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200899 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300900
901 /* We can't call this from frame_notify, because the output's
902 * repaint needed flag is cleared just after that */
903 if (output->recorder)
904 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300905 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200906}
907
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500908static uint32_t
909drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500911{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500912 uint32_t i, format;
913
914 format = gbm_bo_get_format(bo);
915
916 if (format == GBM_FORMAT_ARGB8888) {
917 pixman_region32_t r;
918
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500919 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600920 ev->surface->width,
921 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500923
924 if (!pixman_region32_not_empty(&r))
925 format = GBM_FORMAT_XRGB8888;
926
927 pixman_region32_fini(&r);
928 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500929
930 for (i = 0; i < s->count_formats; i++)
931 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500932 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500933
934 return 0;
935}
936
937static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500939{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 return !ev->transform.enabled ||
941 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500942}
943
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200945drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500947{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200948 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300949 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200950 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300951 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300953 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500954 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500955 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500956 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200957 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500958 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400959 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960
Giulio Camuffo954f1832014-10-11 18:27:30 +0300961 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200962 return NULL;
963
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200964 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200965 return NULL;
966
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200967 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200968 return NULL;
969
Giulio Camuffo954f1832014-10-11 18:27:30 +0300970 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400971 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500972
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200973 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400974 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300975
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400977 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300978 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500979
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200981 return NULL;
982
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300983 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500984 return NULL;
985
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400987 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500988
Giulio Camuffo954f1832014-10-11 18:27:30 +0300989 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200990 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500991 continue;
992
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200993 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500994 found = 1;
995 break;
996 }
997 }
998
999 /* No sprites available */
1000 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001001 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001002
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001003 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001004#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001005 /* XXX: TODO:
1006 *
1007 * Use AddFB2 directly, do not go via GBM.
1008 * Add support for multiplanar formats.
1009 * Both require refactoring in the DRM-backend to
1010 * support a mix of gbm_bos and drmfbs.
1011 */
1012 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001013 .fd = dmabuf->attributes.fd[0],
1014 .width = dmabuf->attributes.width,
1015 .height = dmabuf->attributes.height,
1016 .stride = dmabuf->attributes.stride[0],
1017 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001018 };
1019
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001020 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001021 return NULL;
1022
1023 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1024 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001025#else
1026 return NULL;
1027#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001028 } else {
1029 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1030 buffer_resource, GBM_BO_USE_SCANOUT);
1031 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001032 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001033 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001034
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001036 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001037 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001038 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001039 }
1040
Giulio Camuffo954f1832014-10-11 18:27:30 +03001041 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001042 if (!s->next) {
1043 gbm_bo_destroy(bo);
1044 return NULL;
1045 }
1046
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001048
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001050 s->plane.x = box->x1;
1051 s->plane.y = box->y1;
1052
Jesse Barnes58ef3792012-02-23 09:45:49 -05001053 /*
1054 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001055 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001056 * for us already).
1057 */
1058 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001060 &output->base.region);
1061 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001062 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001063 tbox = weston_transformed_rect(output->base.width,
1064 output->base.height,
1065 output->base.transform,
1066 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001067 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001068 s->dest_x = tbox.x1;
1069 s->dest_y = tbox.y1;
1070 s->dest_w = tbox.x2 - tbox.x1;
1071 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001072 pixman_region32_fini(&dest_rect);
1073
1074 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001076 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001077 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001078
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 weston_view_from_global_fixed(ev,
1080 wl_fixed_from_int(box->x1),
1081 wl_fixed_from_int(box->y1),
1082 &sx1, &sy1);
1083 weston_view_from_global_fixed(ev,
1084 wl_fixed_from_int(box->x2),
1085 wl_fixed_from_int(box->y2),
1086 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001087
1088 if (sx1 < 0)
1089 sx1 = 0;
1090 if (sy1 < 0)
1091 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001092 if (sx2 > wl_fixed_from_int(ev->surface->width))
1093 sx2 = wl_fixed_from_int(ev->surface->width);
1094 if (sy2 > wl_fixed_from_int(ev->surface->height))
1095 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001096
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001097 tbox.x1 = sx1;
1098 tbox.y1 = sy1;
1099 tbox.x2 = sx2;
1100 tbox.y2 = sy2;
1101
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001102 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1103 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001104 viewport->buffer.transform,
1105 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001106 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001107
1108 s->src_x = tbox.x1 << 8;
1109 s->src_y = tbox.y1 << 8;
1110 s->src_w = (tbox.x2 - tbox.x1) << 8;
1111 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001112 pixman_region32_fini(&src_rect);
1113
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001114 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001115}
1116
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001117static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001118drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001120{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001121 struct drm_backend *b =
1122 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001123 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001124 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001125
Derek Foremanbe428b32015-11-24 11:39:38 -06001126 if (ev->transform.enabled &&
1127 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1128 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001129 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001130 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001131 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1132 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001133 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001134 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001136 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001137 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001138 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001139 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001140 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001141 if (ev->geometry.scissor_enabled)
1142 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001143 if (ev->surface->buffer_ref.buffer == NULL)
1144 return NULL;
1145 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1146 if (!shmbuf)
1147 return NULL;
1148 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1149 return NULL;
1150 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001151 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001152 return NULL;
1153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001155
1156 return &output->cursor_plane;
1157}
1158
Pekka Paalanend0ead482014-06-16 12:05:40 +03001159/**
1160 * Update the image for the current cursor surface
1161 *
1162 * @param b DRM backend structure
1163 * @param bo GBM buffer object to write into
1164 * @param ev View to use for cursor image
1165 */
1166static void
1167cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1168 struct weston_view *ev)
1169{
1170 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1171 uint32_t buf[b->cursor_width * b->cursor_height];
1172 int32_t stride;
1173 uint8_t *s;
1174 int i;
1175
1176 assert(buffer && buffer->shm_buffer);
1177 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1178 assert(ev->surface->width <= b->cursor_width);
1179 assert(ev->surface->height <= b->cursor_height);
1180
1181 memset(buf, 0, sizeof buf);
1182 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1183 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1184
1185 wl_shm_buffer_begin_access(buffer->shm_buffer);
1186 for (i = 0; i < ev->surface->height; i++)
1187 memcpy(buf + i * b->cursor_width,
1188 s + i * stride,
1189 ev->surface->width * 4);
1190 wl_shm_buffer_end_access(buffer->shm_buffer);
1191
1192 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1193 weston_log("failed update cursor: %m\n");
1194}
1195
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001196static void
1197drm_output_set_cursor(struct drm_output *output)
1198{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001200 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001201 struct drm_backend *b =
1202 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001203 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001204 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001205 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 output->cursor_view = NULL;
1208 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001209 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001210 output->cursor_plane.x = INT32_MIN;
1211 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001212 return;
1213 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001214
Neil Robertse5051712013-11-13 15:44:06 +00001215 buffer = ev->surface->buffer_ref.buffer;
1216
1217 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001218 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001219 pixman_region32_fini(&output->cursor_plane.damage);
1220 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001221 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001222 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001223
Pekka Paalanend0ead482014-06-16 12:05:40 +03001224 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001225 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001226 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1227 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001228 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001229 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001230 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001231 }
1232
Derek Foremanbe428b32015-11-24 11:39:38 -06001233 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001234
1235 /* From global to output space, output transform is guaranteed to be
1236 * NORMAL by drm_output_prepare_cursor_view().
1237 */
1238 x = (x - output->base.x) * output->base.current_scale;
1239 y = (y - output->base.y) * output->base.current_scale;
1240
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001241 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001242 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001243 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001244 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001245 }
1246
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001247 output->cursor_plane.x = x;
1248 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001249 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001250}
1251
Jesse Barnes58ef3792012-02-23 09:45:49 -05001252static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001253drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001254{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001255 struct drm_backend *b =
1256 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001257 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001259 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001260 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001261
1262 /*
1263 * Find a surface for each sprite in the output using some heuristics:
1264 * 1) size
1265 * 2) frequency of update
1266 * 3) opacity (though some hw might support alpha blending)
1267 * 4) clipping (this can be fixed with color keys)
1268 *
1269 * The idea is to save on blitting since this should save power.
1270 * If we can get a large video surface on the sprite for example,
1271 * the main display surface may not need to update at all, and
1272 * the client buffer can be used directly for the sprite surface
1273 * as we do for flipping full screen surfaces.
1274 */
1275 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001276 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001279 struct weston_surface *es = ev->surface;
1280
1281 /* Test whether this buffer can ever go into a plane:
1282 * non-shm, or small enough to be a cursor.
1283 *
1284 * Also, keep a reference when using the pixman renderer.
1285 * That makes it possible to do a seamless switch to the GL
1286 * renderer and since the pixman renderer keeps a reference
1287 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001288 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001289 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001290 (es->buffer_ref.buffer &&
1291 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001292 (ev->surface->width <= b->cursor_width &&
1293 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001294 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001295 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001296 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001297
Jesse Barnes58ef3792012-02-23 09:45:49 -05001298 pixman_region32_init(&surface_overlap);
1299 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001301
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001302 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001303 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001304 next_plane = primary;
1305 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001307 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001309 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001311 if (next_plane == NULL)
1312 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001315
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001316 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001317 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001319
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001320 if (next_plane == primary ||
1321 next_plane == &output->cursor_plane) {
1322 /* cursor plane involves a copy */
1323 ev->psf_flags = 0;
1324 } else {
1325 /* All other planes are a direct scanout of a
1326 * single client buffer.
1327 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001328 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001329 }
1330
Jesse Barnes58ef3792012-02-23 09:45:49 -05001331 pixman_region32_fini(&surface_overlap);
1332 }
1333 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001334}
1335
Matt Roper361d2ad2011-08-29 13:52:23 -07001336static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001337drm_output_fini_pixman(struct drm_output *output);
1338
1339static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001340drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001341{
1342 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001343 struct drm_backend *b =
1344 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001345 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001346
Xiong Zhangabd5d472013-10-11 14:43:07 +08001347 if (output->page_flip_pending) {
1348 output->destroy_pending = 1;
1349 weston_log("destroy output while page flip pending\n");
1350 return;
1351 }
1352
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001353 if (output->backlight)
1354 backlight_destroy(output->backlight);
1355
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001356 drmModeFreeProperty(output->dpms_prop);
1357
Matt Roper361d2ad2011-08-29 13:52:23 -07001358 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001359 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001360
1361 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001362 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001363 origcrtc->x, origcrtc->y,
1364 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001365 drmModeFreeCrtc(origcrtc);
1366
Giulio Camuffo954f1832014-10-11 18:27:30 +03001367 b->crtc_allocator &= ~(1 << output->crtc_id);
1368 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001369
Giulio Camuffo954f1832014-10-11 18:27:30 +03001370 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001371 drm_output_fini_pixman(output);
1372 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001373 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001374 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001375 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001376
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001377 weston_plane_release(&output->fb_plane);
1378 weston_plane_release(&output->cursor_plane);
1379
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001380 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001381
Matt Roper361d2ad2011-08-29 13:52:23 -07001382 free(output);
1383}
1384
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001385/**
1386 * Find the closest-matching mode for a given target
1387 *
1388 * Given a target mode, find the most suitable mode amongst the output's
1389 * current mode list to use, preferring the current mode if possible, to
1390 * avoid an expensive mode switch.
1391 *
1392 * @param output DRM output
1393 * @param target_mode Mode to attempt to match
1394 * @returns Pointer to a mode from the output's mode list
1395 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001396static struct drm_mode *
1397choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1398{
1399 struct drm_mode *tmp_mode = NULL, *mode;
1400
Hardeningff39efa2013-09-18 23:56:35 +02001401 if (output->base.current_mode->width == target_mode->width &&
1402 output->base.current_mode->height == target_mode->height &&
1403 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001404 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001405 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001406
1407 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1408 if (mode->mode_info.hdisplay == target_mode->width &&
1409 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001410 if (mode->base.refresh == target_mode->refresh ||
1411 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001412 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001413 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001414 tmp_mode = mode;
1415 }
1416 }
1417
1418 return tmp_mode;
1419}
1420
1421static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001422drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001423static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001424drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001425
1426static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001427drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1428{
1429 struct drm_output *output;
1430 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001431 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001432
1433 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001434 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001435 return -1;
1436 }
1437
1438 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001439 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001440 return -1;
1441 }
1442
Giulio Camuffo954f1832014-10-11 18:27:30 +03001443 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001444 output = (struct drm_output *)output_base;
1445 drm_mode = choose_mode (output, mode);
1446
1447 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001448 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001449 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001450 }
1451
Hardeningff39efa2013-09-18 23:56:35 +02001452 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001453 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001454
Hardeningff39efa2013-09-18 23:56:35 +02001455 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001456
Hardeningff39efa2013-09-18 23:56:35 +02001457 output->base.current_mode = &drm_mode->base;
1458 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001459 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1460
Alex Wub7b8bda2012-04-17 17:20:48 +08001461 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001462 drm_output_release_fb(output, output->current);
1463 drm_output_release_fb(output, output->next);
1464 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001465
Giulio Camuffo954f1832014-10-11 18:27:30 +03001466 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001467 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001468 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001469 weston_log("failed to init output pixman state with "
1470 "new mode\n");
1471 return -1;
1472 }
1473 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001474 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001475 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001476
Giulio Camuffo954f1832014-10-11 18:27:30 +03001477 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001478 weston_log("failed to init output egl state with "
1479 "new mode");
1480 return -1;
1481 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001482 }
1483
Alex Wub7b8bda2012-04-17 17:20:48 +08001484 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001485}
1486
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001487static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001488on_drm_input(int fd, uint32_t mask, void *data)
1489{
1490 drmEventContext evctx;
1491
1492 memset(&evctx, 0, sizeof evctx);
1493 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1494 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001495 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001496 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001497
1498 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001499}
1500
1501static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001502init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001503{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001504 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001505 uint64_t cap;
1506 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001507 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001508
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001509 sysnum = udev_device_get_sysnum(device);
1510 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001511 b->drm.id = atoi(sysnum);
1512 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001513 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001514 return -1;
1515 }
1516
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001517 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001518 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001519 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001520 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001521 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001522 udev_device_get_devnode(device));
1523 return -1;
1524 }
1525
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001526 weston_log("using %s\n", filename);
1527
Giulio Camuffo954f1832014-10-11 18:27:30 +03001528 b->drm.fd = fd;
1529 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001530
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001531 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1532 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001533 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001534 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001535 clk_id = CLOCK_REALTIME;
1536
Giulio Camuffo954f1832014-10-11 18:27:30 +03001537 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001538 weston_log("Error: failed to set presentation clock %d.\n",
1539 clk_id);
1540 return -1;
1541 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001542
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001543 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1544 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001545 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001546 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001547 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001548
1549 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1550 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001551 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001552 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001553 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001554
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001555 return 0;
1556}
1557
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001558static struct gbm_device *
1559create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001560{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001561 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001562
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001563 gl_renderer = weston_load_module("gl-renderer.so",
1564 "gl_renderer_interface");
1565 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001566 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001567
1568 /* GBM will load a dri driver, but even though they need symbols from
1569 * libglapi, in some version of Mesa they are not linked to it. Since
1570 * only the gl-renderer module links to it, the call above won't make
1571 * these symbols globally available, and loading the DRI driver fails.
1572 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1573 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1574
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001575 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001576
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001577 return gbm;
1578}
1579
Bryce Harringtonc056a982015-05-19 15:25:18 -07001580/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001581 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001582 *
1583 * This returns 0 if substitution isn't possible, but 0 might be a
1584 * legitimate format for other EGL platforms, so the caller is
1585 * responsible for checking for 0 before calling gl_renderer->create().
1586 *
1587 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1588 * but it's entirely possible we'll see this again on other implementations.
1589 */
1590static int
1591fallback_format_for(uint32_t format)
1592{
1593 switch (format) {
1594 case GBM_FORMAT_XRGB8888:
1595 return GBM_FORMAT_ARGB8888;
1596 case GBM_FORMAT_XRGB2101010:
1597 return GBM_FORMAT_ARGB2101010;
1598 default:
1599 return 0;
1600 }
1601}
1602
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001603static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001604drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001605{
Derek Foreman6d556372015-11-04 14:47:33 -06001606 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001607 b->gbm_format,
1608 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001609 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001610 };
Derek Foreman6d556372015-11-04 14:47:33 -06001611 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001612
Derek Foremanc4cfe852015-05-15 12:12:40 -05001613 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001614 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001615 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001616 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001617 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001618 gl_renderer->opaque_attribs,
1619 format,
1620 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001621 return -1;
1622 }
1623
1624 return 0;
1625}
1626
1627static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001628init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001629{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001630 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001631
Giulio Camuffo954f1832014-10-11 18:27:30 +03001632 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001633 return -1;
1634
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635 if (drm_backend_create_gl_renderer(b) < 0) {
1636 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001637 return -1;
1638 }
1639
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001640 return 0;
1641}
1642
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001643static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001644init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001645{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001646 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001647}
1648
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001649/**
1650 * Add a mode to output's mode list
1651 *
1652 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1653 * output's mode list.
1654 *
1655 * @param output DRM output to add mode to
1656 * @param info DRM mode structure to add
1657 * @returns Newly-allocated Weston/DRM mode structure
1658 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001659static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001660drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001661{
1662 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001663 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001664
1665 mode = malloc(sizeof *mode);
1666 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001667 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001668
1669 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001670 mode->base.width = info->hdisplay;
1671 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001672
1673 /* Calculate higher precision (mHz) refresh rate */
1674 refresh = (info->clock * 1000000LL / info->htotal +
1675 info->vtotal / 2) / info->vtotal;
1676
1677 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1678 refresh *= 2;
1679 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1680 refresh /= 2;
1681 if (info->vscan > 1)
1682 refresh /= info->vscan;
1683
1684 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001685 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001686
1687 if (info->type & DRM_MODE_TYPE_PREFERRED)
1688 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1689
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001690 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1691
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001692 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001693}
1694
1695static int
1696drm_subpixel_to_wayland(int drm_value)
1697{
1698 switch (drm_value) {
1699 default:
1700 case DRM_MODE_SUBPIXEL_UNKNOWN:
1701 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1702 case DRM_MODE_SUBPIXEL_NONE:
1703 return WL_OUTPUT_SUBPIXEL_NONE;
1704 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1705 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1706 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1707 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1708 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1709 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1710 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1711 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1712 }
1713}
1714
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001715/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001716static uint32_t
1717drm_get_backlight(struct drm_output *output)
1718{
1719 long brightness, max_brightness, norm;
1720
1721 brightness = backlight_get_brightness(output->backlight);
1722 max_brightness = backlight_get_max_brightness(output->backlight);
1723
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001724 /* convert it on a scale of 0 to 255 */
1725 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001726
1727 return (uint32_t) norm;
1728}
1729
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001730/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001731static void
1732drm_set_backlight(struct weston_output *output_base, uint32_t value)
1733{
1734 struct drm_output *output = (struct drm_output *) output_base;
1735 long max_brightness, new_brightness;
1736
1737 if (!output->backlight)
1738 return;
1739
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001740 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001741 return;
1742
1743 max_brightness = backlight_get_max_brightness(output->backlight);
1744
1745 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001746 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001747
1748 backlight_set_brightness(output->backlight, new_brightness);
1749}
1750
1751static drmModePropertyPtr
1752drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1753{
1754 drmModePropertyPtr props;
1755 int i;
1756
1757 for (i = 0; i < connector->count_props; i++) {
1758 props = drmModeGetProperty(fd, connector->props[i]);
1759 if (!props)
1760 continue;
1761
1762 if (!strcmp(props->name, name))
1763 return props;
1764
1765 drmModeFreeProperty(props);
1766 }
1767
1768 return NULL;
1769}
1770
1771static void
1772drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1773{
1774 struct drm_output *output = (struct drm_output *) output_base;
1775 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001776 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001777 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001778
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001779 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001780 return;
1781
Daniel Stone36609c72015-06-18 07:49:02 +01001782 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1783 output->dpms_prop->prop_id, level);
1784 if (ret) {
1785 weston_log("DRM: DPMS: failed property set for %s\n",
1786 output->base.name);
1787 return;
1788 }
1789
1790 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001791}
1792
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001793static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001794 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1795 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1796 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1797 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1798 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1799 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1800 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1801 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1802 [DRM_MODE_CONNECTOR_Component] = "Component",
1803 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1804 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1805 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1806 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1807 [DRM_MODE_CONNECTOR_TV] = "TV",
1808 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001809#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001810 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1811 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001812#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001813};
1814
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001815static char *
1816make_connector_name(const drmModeConnector *con)
1817{
1818 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001819 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001820
1821 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1822 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001823
1824 if (!type_name)
1825 type_name = "UNNAMED";
1826
1827 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001828
1829 return strdup(name);
1830}
1831
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001832static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001833find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001834 drmModeRes *resources, drmModeConnector *connector)
1835{
1836 drmModeEncoder *encoder;
1837 uint32_t possible_crtcs;
1838 int i, j;
1839
1840 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001841 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001842 if (encoder == NULL) {
1843 weston_log("Failed to get encoder.\n");
1844 return -1;
1845 }
1846 possible_crtcs = encoder->possible_crtcs;
1847 drmModeFreeEncoder(encoder);
1848
1849 for (i = 0; i < resources->count_crtcs; i++) {
1850 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001851 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001852 return i;
1853 }
1854 }
1855
1856 return -1;
1857}
1858
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001859/* Init output state that depends on gl or gbm */
1860static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001861drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001862{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001863 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001864 output->gbm_format,
1865 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001866 };
1867 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001868
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001869 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001870 output->base.current_mode->width,
1871 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001872 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001873 GBM_BO_USE_SCANOUT |
1874 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001875 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001876 weston_log("failed to create gbm surface\n");
1877 return -1;
1878 }
1879
Derek Foremanc4cfe852015-05-15 12:12:40 -05001880 if (format[1])
1881 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001882 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001883 (EGLNativeWindowType)output->gbm_surface,
1884 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001885 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001886 format,
1887 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001888 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001889 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001890 return -1;
1891 }
1892
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001893 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001894
1895 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001896 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001897 continue;
1898
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001899 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001900 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001901 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001902 }
1903
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001904 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001905 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001906 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001907 }
1908
1909 return 0;
1910}
1911
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001912static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001913drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001914{
Hardeningff39efa2013-09-18 23:56:35 +02001915 int w = output->base.current_mode->width;
1916 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001917 uint32_t format = output->gbm_format;
1918 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001919 unsigned int i;
1920
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001921 switch (format) {
1922 case GBM_FORMAT_XRGB8888:
1923 pixman_format = PIXMAN_x8r8g8b8;
1924 break;
1925 case GBM_FORMAT_RGB565:
1926 pixman_format = PIXMAN_r5g6b5;
1927 break;
1928 default:
1929 weston_log("Unsupported pixman format 0x%x\n", format);
1930 return -1;
1931 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001932
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001933 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001934 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001935 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001936 if (!output->dumb[i])
1937 goto err;
1938
1939 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001940 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001941 output->dumb[i]->map,
1942 output->dumb[i]->stride);
1943 if (!output->image[i])
1944 goto err;
1945 }
1946
1947 if (pixman_renderer_output_create(&output->base) < 0)
1948 goto err;
1949
1950 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001951 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001952
1953 return 0;
1954
1955err:
1956 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1957 if (output->dumb[i])
1958 drm_fb_destroy_dumb(output->dumb[i]);
1959 if (output->image[i])
1960 pixman_image_unref(output->image[i]);
1961
1962 output->dumb[i] = NULL;
1963 output->image[i] = NULL;
1964 }
1965
1966 return -1;
1967}
1968
1969static void
1970drm_output_fini_pixman(struct drm_output *output)
1971{
1972 unsigned int i;
1973
1974 pixman_renderer_output_destroy(&output->base);
1975 pixman_region32_fini(&output->previous_damage);
1976
1977 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1978 drm_fb_destroy_dumb(output->dumb[i]);
1979 pixman_image_unref(output->image[i]);
1980 output->dumb[i] = NULL;
1981 output->image[i] = NULL;
1982 }
1983}
1984
Richard Hughes2b2092a2013-04-24 14:58:02 +01001985static void
1986edid_parse_string(const uint8_t *data, char text[])
1987{
1988 int i;
1989 int replaced = 0;
1990
1991 /* this is always 12 bytes, but we can't guarantee it's null
1992 * terminated or not junk. */
1993 strncpy(text, (const char *) data, 12);
1994
Bryce Harrington9c7de162015-08-28 13:04:26 -07001995 /* guarantee our new string is null-terminated */
1996 text[12] = '\0';
1997
Richard Hughes2b2092a2013-04-24 14:58:02 +01001998 /* remove insane chars */
1999 for (i = 0; text[i] != '\0'; i++) {
2000 if (text[i] == '\n' ||
2001 text[i] == '\r') {
2002 text[i] = '\0';
2003 break;
2004 }
2005 }
2006
2007 /* ensure string is printable */
2008 for (i = 0; text[i] != '\0'; i++) {
2009 if (!isprint(text[i])) {
2010 text[i] = '-';
2011 replaced++;
2012 }
2013 }
2014
2015 /* if the string is random junk, ignore the string */
2016 if (replaced > 4)
2017 text[0] = '\0';
2018}
2019
2020#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2021#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2022#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2023#define EDID_OFFSET_DATA_BLOCKS 0x36
2024#define EDID_OFFSET_LAST_BLOCK 0x6c
2025#define EDID_OFFSET_PNPID 0x08
2026#define EDID_OFFSET_SERIAL 0x0c
2027
2028static int
2029edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2030{
2031 int i;
2032 uint32_t serial_number;
2033
2034 /* check header */
2035 if (length < 128)
2036 return -1;
2037 if (data[0] != 0x00 || data[1] != 0xff)
2038 return -1;
2039
2040 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2041 * /--08--\/--09--\
2042 * 7654321076543210
2043 * |\---/\---/\---/
2044 * R C1 C2 C3 */
2045 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2046 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2047 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2048 edid->pnp_id[3] = '\0';
2049
2050 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2051 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2052 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2053 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2054 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2055 if (serial_number > 0)
2056 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2057
2058 /* parse EDID data */
2059 for (i = EDID_OFFSET_DATA_BLOCKS;
2060 i <= EDID_OFFSET_LAST_BLOCK;
2061 i += 18) {
2062 /* ignore pixel clock data */
2063 if (data[i] != 0)
2064 continue;
2065 if (data[i+2] != 0)
2066 continue;
2067
2068 /* any useful blocks? */
2069 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2070 edid_parse_string(&data[i+5],
2071 edid->monitor_name);
2072 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2073 edid_parse_string(&data[i+5],
2074 edid->serial_number);
2075 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2076 edid_parse_string(&data[i+5],
2077 edid->eisa_id);
2078 }
2079 }
2080 return 0;
2081}
2082
2083static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002084find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002085 struct drm_output *output,
2086 drmModeConnector *connector)
2087{
2088 drmModePropertyBlobPtr edid_blob = NULL;
2089 drmModePropertyPtr property;
2090 int i;
2091 int rc;
2092
2093 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002094 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002095 if (!property)
2096 continue;
2097 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2098 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002099 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002100 connector->prop_values[i]);
2101 }
2102 drmModeFreeProperty(property);
2103 }
2104 if (!edid_blob)
2105 return;
2106
2107 rc = edid_parse(&output->edid,
2108 edid_blob->data,
2109 edid_blob->length);
2110 if (!rc) {
2111 weston_log("EDID data '%s', '%s', '%s'\n",
2112 output->edid.pnp_id,
2113 output->edid.monitor_name,
2114 output->edid.serial_number);
2115 if (output->edid.pnp_id[0] != '\0')
2116 output->base.make = output->edid.pnp_id;
2117 if (output->edid.monitor_name[0] != '\0')
2118 output->base.model = output->edid.monitor_name;
2119 if (output->edid.serial_number[0] != '\0')
2120 output->base.serial_number = output->edid.serial_number;
2121 }
2122 drmModeFreePropertyBlob(edid_blob);
2123}
2124
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002125
2126
2127static int
2128parse_modeline(const char *s, drmModeModeInfo *mode)
2129{
2130 char hsync[16];
2131 char vsync[16];
2132 float fclock;
2133
2134 mode->type = DRM_MODE_TYPE_USERDEF;
2135 mode->hskew = 0;
2136 mode->vscan = 0;
2137 mode->vrefresh = 0;
2138 mode->flags = 0;
2139
Rob Bradford307e09e2013-07-26 16:29:40 +01002140 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002141 &fclock,
2142 &mode->hdisplay,
2143 &mode->hsync_start,
2144 &mode->hsync_end,
2145 &mode->htotal,
2146 &mode->vdisplay,
2147 &mode->vsync_start,
2148 &mode->vsync_end,
2149 &mode->vtotal, hsync, vsync) != 11)
2150 return -1;
2151
2152 mode->clock = fclock * 1000;
2153 if (strcmp(hsync, "+hsync") == 0)
2154 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2155 else if (strcmp(hsync, "-hsync") == 0)
2156 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2157 else
2158 return -1;
2159
2160 if (strcmp(vsync, "+vsync") == 0)
2161 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2162 else if (strcmp(vsync, "-vsync") == 0)
2163 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2164 else
2165 return -1;
2166
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002167 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2168 mode->hdisplay, mode->vdisplay, fclock);
2169
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002170 return 0;
2171}
2172
Rob Bradford66bd9f52013-06-25 18:56:42 +01002173static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002174setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002175 struct weston_output *output,
2176 const char *s)
2177{
2178 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002179 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002180 struct udev_seat *seat;
2181
Giulio Camuffo954f1832014-10-11 18:27:30 +03002182 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002183 if (!seat)
2184 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002185
Derek Foreman0720ea32015-07-15 13:00:35 -05002186 seat->base.output = output;
2187
Derek Foreman1281a362015-07-31 16:55:32 -05002188 pointer = weston_seat_get_pointer(&seat->base);
2189 if (pointer)
2190 weston_pointer_clamp(pointer,
2191 &pointer->x,
2192 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002193 }
2194}
2195
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002196static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002197parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002198{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002199 int ret = 0;
2200
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002201 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002202 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002203 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002204 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002205 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002206 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002207 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002208 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002209 else {
2210 weston_log("fatal: unrecognized pixel format: %s\n", s);
2211 ret = -1;
2212 }
2213
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002214 return ret;
2215}
2216
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002217/**
2218 * Choose suitable mode for an output
2219 *
2220 * Find the most suitable mode to use for initial setup (or reconfiguration on
2221 * hotplug etc) for a DRM output.
2222 *
2223 * @param output DRM output to choose mode for
2224 * @param kind Strategy and preference to use when choosing mode
2225 * @param width Desired width for this output
2226 * @param height Desired height for this output
2227 * @param current_mode Mode currently being displayed on this output
2228 * @param modeline Manually-entered mode (may be NULL)
2229 * @returns A mode from the output's mode list, or NULL if none available
2230 */
2231static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002232drm_output_choose_initial_mode(struct drm_backend *backend,
2233 struct drm_output *output,
2234 enum weston_drm_backend_output_mode mode,
2235 struct weston_drm_backend_output_config *config,
2236 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002237{
2238 struct drm_mode *preferred = NULL;
2239 struct drm_mode *current = NULL;
2240 struct drm_mode *configured = NULL;
2241 struct drm_mode *best = NULL;
2242 struct drm_mode *drm_mode;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002243 drmModeModeInfo modeline;
2244 int32_t width = 0;
2245 int32_t height = 0;
2246
2247 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
2248 if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
2249 width = -1;
2250
2251 if (parse_modeline(config->modeline, &modeline) == 0) {
2252 configured = drm_output_add_mode(output, &modeline);
2253 if (!configured)
2254 return NULL;
2255 } else {
2256 weston_log("Invalid modeline \"%s\" for output %s\n",
2257 config->modeline, output->base.name);
2258 }
2259 }
2260 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002261
2262 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002263 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002264 height == drm_mode->base.height)
2265 configured = drm_mode;
2266
comic fans7a5c5622016-03-17 14:29:27 +02002267 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002268 sizeof *current_mode) == 0)
2269 current = drm_mode;
2270
2271 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2272 preferred = drm_mode;
2273
2274 best = drm_mode;
2275 }
2276
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002277 if (current == NULL && current_mode->clock != 0) {
2278 current = drm_output_add_mode(output, current_mode);
2279 if (!current)
2280 return NULL;
2281 }
2282
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002283 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002284 configured = current;
2285
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002286 if (configured)
2287 return configured;
2288
2289 if (preferred)
2290 return preferred;
2291
2292 if (current)
2293 return current;
2294
2295 if (best)
2296 return best;
2297
2298 weston_log("no available modes for %s\n", output->base.name);
2299 return NULL;
2300}
2301
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002302static int
2303connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2304 drmModeModeInfo *mode)
2305{
2306 drmModeEncoder *encoder;
2307 drmModeCrtc *crtc;
2308
2309 /* Get the current mode on the crtc that's currently driving
2310 * this connector. */
2311 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2312 memset(mode, 0, sizeof *mode);
2313 if (encoder != NULL) {
2314 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2315 drmModeFreeEncoder(encoder);
2316 if (crtc == NULL)
2317 return -1;
2318 if (crtc->mode_valid)
2319 *mode = crtc->mode;
2320 drmModeFreeCrtc(crtc);
2321 }
2322
2323 return 0;
2324}
2325
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002326/**
2327 * Create and configure a Weston output structure
2328 *
2329 * Given a DRM connector, create a matching drm_output structure and add it
2330 * to Weston's output list.
2331 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002332 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002333 * @param resources DRM resources for this device
2334 * @param connector DRM connector to use for this new output
2335 * @param x Horizontal offset to use into global co-ordinate space
2336 * @param y Vertical offset to use into global co-ordinate space
2337 * @param drm_device udev device pointer
2338 * @returns 0 on success, or -1 on failure
2339 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002340static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002341create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002342 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002343 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002344 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002345{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002346 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002347 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002348 struct weston_mode *m;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002349
2350 drmModeModeInfo crtc_mode;
2351 int i;
2352 enum weston_drm_backend_output_mode mode;
2353 struct weston_drm_backend_output_config config = {{ 0 }};
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002354
Giulio Camuffo954f1832014-10-11 18:27:30 +03002355 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002356 if (i < 0) {
2357 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002358 return -1;
2359 }
2360
Peter Huttererf3d62272013-08-08 11:57:05 +10002361 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002362 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002363 return -1;
2364
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002365 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002366 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002367 output->base.make = "unknown";
2368 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002369 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002370 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002371
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002372 mode = b->configure_output(b->compositor, b->use_current_mode,
2373 output->base.name, &config);
2374 if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002375 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002376
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002377 setup_output_seat_constraint(b, &output->base,
2378 config.seat ? config.seat : "");
2379 free(config.seat);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002380
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002381 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002382 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002383 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002384 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002385 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002386
Giulio Camuffo954f1832014-10-11 18:27:30 +03002387 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2388 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002389
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002390 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2391 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002392
David Herrmann0f0d54e2011-12-08 17:05:45 +01002393 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002394 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002395 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002396 goto err_free;
2397 }
2398
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002399 if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002400 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002401 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002402 0, 0, 0, 0, 0, NULL);
2403 goto err_free;
2404 }
2405
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002406 current = drm_output_choose_initial_mode(b, output, mode, &config,
2407 &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002408 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002409 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002410 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002411 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002412
Giulio Camuffo954f1832014-10-11 18:27:30 +03002413 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002414 connector->mmWidth, connector->mmHeight,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002415 config.base.transform, config.base.scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002416
Giulio Camuffo954f1832014-10-11 18:27:30 +03002417 if (b->use_pixman) {
2418 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002419 weston_log("Failed to init output pixman state\n");
2420 goto err_output;
2421 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002422 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002423 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002424 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002425 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002426
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002427 output->backlight = backlight_init(drm_device,
2428 connector->connector_type);
2429 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002430 weston_log("Initialized backlight, device %s\n",
2431 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002432 output->base.set_backlight = drm_set_backlight;
2433 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002434 } else {
2435 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002436 }
2437
Giulio Camuffo954f1832014-10-11 18:27:30 +03002438 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002439
Giulio Camuffo954f1832014-10-11 18:27:30 +03002440 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002441 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2442 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002443
Jonas Ådahle5a12252013-04-05 23:07:11 +02002444 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002445 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002446 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002447 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002448 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002449 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002450
Richard Hughese7299962013-05-01 21:52:12 +01002451 output->base.gamma_size = output->original_crtc->gamma_size;
2452 output->base.set_gamma = drm_output_set_gamma;
2453
Derek Foremanb0427562016-02-05 15:55:20 -06002454 weston_plane_init(&output->cursor_plane, b->compositor,
2455 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002456 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002457
Giulio Camuffo954f1832014-10-11 18:27:30 +03002458 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2459 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2460 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002461
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002462 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002463 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002464 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002465 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002466 m->width, m->height, m->refresh / 1000.0,
2467 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2468 ", preferred" : "",
2469 m->flags & WL_OUTPUT_MODE_CURRENT ?
2470 ", current" : "",
2471 connector->count_modes == 0 ?
2472 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002473
Mario Kleiner80817042015-06-21 21:25:11 +02002474 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2475 output->base.native_mode = output->base.current_mode;
2476 output->base.native_scale = output->base.current_scale;
2477
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002478 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002479
John Kåre Alsaker94659272012-11-13 19:10:18 +01002480err_output:
2481 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002482err_free:
2483 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2484 base.link) {
2485 wl_list_remove(&drm_mode->base.link);
2486 free(drm_mode);
2487 }
2488
2489 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002490 b->crtc_allocator &= ~(1 << output->crtc_id);
2491 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002492 free(output);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002493 free(config.modeline);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002494
David Herrmann0f0d54e2011-12-08 17:05:45 +01002495 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002496}
2497
Jesse Barnes58ef3792012-02-23 09:45:49 -05002498static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002499create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002500{
2501 struct drm_sprite *sprite;
2502 drmModePlaneRes *plane_res;
2503 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002504 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002505
Giulio Camuffo954f1832014-10-11 18:27:30 +03002506 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002507 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002508 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002509 strerror(errno));
2510 return;
2511 }
2512
2513 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002514 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002515 if (!plane)
2516 continue;
2517
Peter Huttererf3d62272013-08-08 11:57:05 +10002518 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002519 plane->count_formats));
2520 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002521 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002522 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002523 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002524 continue;
2525 }
2526
Jesse Barnes58ef3792012-02-23 09:45:49 -05002527 sprite->possible_crtcs = plane->possible_crtcs;
2528 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002529 sprite->current = NULL;
2530 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002531 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002532 sprite->count_formats = plane->count_formats;
2533 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002534 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002535 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002536 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2537 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2538 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002539
Giulio Camuffo954f1832014-10-11 18:27:30 +03002540 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002541 }
2542
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002543 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002544}
2545
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002546static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002547destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002548{
2549 struct drm_sprite *sprite, *next;
2550 struct drm_output *output;
2551
Giulio Camuffo954f1832014-10-11 18:27:30 +03002552 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002553 struct drm_output, base.link);
2554
Giulio Camuffo954f1832014-10-11 18:27:30 +03002555 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2556 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002557 sprite->plane_id,
2558 output->crtc_id, 0, 0,
2559 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002560 drm_output_release_fb(output, sprite->current);
2561 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002562 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002563 free(sprite);
2564 }
2565}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002566
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002567static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002568create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002569 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002570{
2571 drmModeConnector *connector;
2572 drmModeRes *resources;
2573 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002574 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002575
Giulio Camuffo954f1832014-10-11 18:27:30 +03002576 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002577 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002578 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002579 return -1;
2580 }
2581
Giulio Camuffo954f1832014-10-11 18:27:30 +03002582 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2583 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002584 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002585 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002586 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002587
Giulio Camuffo954f1832014-10-11 18:27:30 +03002588 b->min_width = resources->min_width;
2589 b->max_width = resources->max_width;
2590 b->min_height = resources->min_height;
2591 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002592
Giulio Camuffo954f1832014-10-11 18:27:30 +03002593 b->num_crtcs = resources->count_crtcs;
2594 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002595
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002596 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002597 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002598 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002599 if (connector == NULL)
2600 continue;
2601
2602 if (connector->connection == DRM_MODE_CONNECTED &&
2603 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002604 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002605 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002606 connector, x, y,
2607 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002608 drmModeFreeConnector(connector);
2609 continue;
2610 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002611
Giulio Camuffo954f1832014-10-11 18:27:30 +03002612 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002613 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002614 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002615 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002616
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002617 drmModeFreeConnector(connector);
2618 }
2619
Giulio Camuffo954f1832014-10-11 18:27:30 +03002620 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002621 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002622 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002623 return -1;
2624 }
2625
2626 drmModeFreeResources(resources);
2627
2628 return 0;
2629}
2630
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002631static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002632update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002633{
2634 drmModeConnector *connector;
2635 drmModeRes *resources;
2636 struct drm_output *output, *next;
2637 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002638 uint32_t connected = 0, disconnects = 0;
2639 int i;
2640
Giulio Camuffo954f1832014-10-11 18:27:30 +03002641 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002642 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002643 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002644 return;
2645 }
2646
2647 /* collect new connects */
2648 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002649 int connector_id = resources->connectors[i];
2650
Giulio Camuffo954f1832014-10-11 18:27:30 +03002651 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002652 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002653 continue;
2654
David Herrmann7551cff2011-12-08 17:05:43 +01002655 if (connector->connection != DRM_MODE_CONNECTED) {
2656 drmModeFreeConnector(connector);
2657 continue;
2658 }
2659
Benjamin Franzke117483d2011-08-30 11:38:26 +02002660 connected |= (1 << connector_id);
2661
Giulio Camuffo954f1832014-10-11 18:27:30 +03002662 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002663 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002664 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002665 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002666
2667 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002669 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002670 else
2671 x = 0;
2672 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002673 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002674 connector, x, y,
2675 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002676 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002677
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002678 }
2679 drmModeFreeConnector(connector);
2680 }
2681 drmModeFreeResources(resources);
2682
Giulio Camuffo954f1832014-10-11 18:27:30 +03002683 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002684 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002685 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002686 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002687 if (disconnects & (1 << output->connector_id)) {
2688 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002689 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002690 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002691 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002692 }
2693 }
2694 }
2695
Daniel Stonef556ebe2015-05-21 08:28:58 +01002696 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002697 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002698 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002699}
2700
2701static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002702udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002703{
David Herrmannd7488c22012-03-11 20:05:21 +01002704 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002705 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002706
2707 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002708 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002709 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002710
David Herrmann6ac52db2012-03-11 20:05:22 +01002711 val = udev_device_get_property_value(device, "HOTPLUG");
2712 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002713 return 0;
2714
David Herrmann6ac52db2012-03-11 20:05:22 +01002715 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002716}
2717
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002718static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002719udev_drm_event(int fd, uint32_t mask, void *data)
2720{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002721 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002722 struct udev_device *event;
2723
Giulio Camuffo954f1832014-10-11 18:27:30 +03002724 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002725
Giulio Camuffo954f1832014-10-11 18:27:30 +03002726 if (udev_event_is_hotplug(b, event))
2727 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002728
2729 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002730
2731 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002732}
2733
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002734static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002735drm_restore(struct weston_compositor *ec)
2736{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002737 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002738}
2739
2740static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002741drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002742{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002743 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002744
Giulio Camuffo954f1832014-10-11 18:27:30 +03002745 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002746
Giulio Camuffo954f1832014-10-11 18:27:30 +03002747 wl_event_source_remove(b->udev_drm_source);
2748 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002749
Giulio Camuffo954f1832014-10-11 18:27:30 +03002750 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002751
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002752 weston_compositor_shutdown(ec);
2753
Giulio Camuffo954f1832014-10-11 18:27:30 +03002754 if (b->gbm)
2755 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002756
Giulio Camuffo954f1832014-10-11 18:27:30 +03002757 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002758
Giulio Camuffo954f1832014-10-11 18:27:30 +03002759 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002760 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002761}
2762
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002763static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002764drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002765{
2766 struct drm_output *output;
2767 struct drm_mode *drm_mode;
2768 int ret;
2769
Giulio Camuffo954f1832014-10-11 18:27:30 +03002770 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002771 if (!output->current) {
2772 /* If something that would cause the output to
2773 * switch mode happened while in another vt, we
2774 * might not have a current drm_fb. In that case,
2775 * schedule a repaint and let drm_output_repaint
2776 * handle setting the mode. */
2777 weston_output_schedule_repaint(&output->base);
2778 continue;
2779 }
2780
Hardeningff39efa2013-09-18 23:56:35 +02002781 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002782 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002783 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002784 &output->connector_id, 1,
2785 &drm_mode->mode_info);
2786 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002787 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002788 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002789 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002790 output->base.x, output->base.y);
2791 }
2792 }
2793}
2794
2795static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002796session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002797{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002798 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002799 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002800 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002801 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002802
Giulio Camuffo954f1832014-10-11 18:27:30 +03002803 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002804 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002805 compositor->state = b->prev_state;
2806 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002807 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002808 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002809 } else {
2810 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002811 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002812
Giulio Camuffo954f1832014-10-11 18:27:30 +03002813 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002814 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002815
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002816 /* If we have a repaint scheduled (either from a
2817 * pending pageflip or the idle handler), make sure we
2818 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002819 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002820 * further attemps at repainting. When we switch
2821 * back, we schedule a repaint, which will process
2822 * pending frame callbacks. */
2823
Giulio Camuffo954f1832014-10-11 18:27:30 +03002824 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002825 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002826 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002827 }
2828
Giulio Camuffo954f1832014-10-11 18:27:30 +03002829 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002830 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002831
Giulio Camuffo954f1832014-10-11 18:27:30 +03002832 wl_list_for_each(sprite, &b->sprite_list, link)
2833 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002834 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002835 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002836 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002837 };
2838}
2839
David Herrmann0af066f2012-10-29 19:21:16 +01002840/*
2841 * Find primary GPU
2842 * Some systems may have multiple DRM devices attached to a single seat. This
2843 * function loops over all devices and tries to find a PCI device with the
2844 * boot_vga sysfs attribute set to 1.
2845 * If no such device is found, the first DRM device reported by udev is used.
2846 */
2847static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002848find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002849{
2850 struct udev_enumerate *e;
2851 struct udev_list_entry *entry;
2852 const char *path, *device_seat, *id;
2853 struct udev_device *device, *drm_device, *pci;
2854
Giulio Camuffo954f1832014-10-11 18:27:30 +03002855 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002856 udev_enumerate_add_match_subsystem(e, "drm");
2857 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2858
2859 udev_enumerate_scan_devices(e);
2860 drm_device = NULL;
2861 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2862 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002863 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002864 if (!device)
2865 continue;
2866 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2867 if (!device_seat)
2868 device_seat = default_seat;
2869 if (strcmp(device_seat, seat)) {
2870 udev_device_unref(device);
2871 continue;
2872 }
2873
2874 pci = udev_device_get_parent_with_subsystem_devtype(device,
2875 "pci", NULL);
2876 if (pci) {
2877 id = udev_device_get_sysattr_value(pci, "boot_vga");
2878 if (id && !strcmp(id, "1")) {
2879 if (drm_device)
2880 udev_device_unref(drm_device);
2881 drm_device = device;
2882 break;
2883 }
2884 }
2885
2886 if (!drm_device)
2887 drm_device = device;
2888 else
2889 udev_device_unref(device);
2890 }
2891
2892 udev_enumerate_unref(e);
2893 return drm_device;
2894}
2895
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002896static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002897planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2898 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002899{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002900 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002901
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002902 switch (key) {
2903 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002904 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002905 break;
2906 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002907 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002908 break;
2909 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002910 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002911 break;
2912 default:
2913 break;
2914 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002915}
2916
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002917#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002918static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002919recorder_destroy(struct drm_output *output)
2920{
2921 vaapi_recorder_destroy(output->recorder);
2922 output->recorder = NULL;
2923
2924 output->base.disable_planes--;
2925
2926 wl_list_remove(&output->recorder_frame_listener.link);
2927 weston_log("[libva recorder] done\n");
2928}
2929
2930static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002931recorder_frame_notify(struct wl_listener *listener, void *data)
2932{
2933 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002934 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002935 int fd, ret;
2936
2937 output = container_of(listener, struct drm_output,
2938 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002939 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002940
2941 if (!output->recorder)
2942 return;
2943
Giulio Camuffo954f1832014-10-11 18:27:30 +03002944 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002945 DRM_CLOEXEC, &fd);
2946 if (ret) {
2947 weston_log("[libva recorder] "
2948 "failed to create prime fd for front buffer\n");
2949 return;
2950 }
2951
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002952 ret = vaapi_recorder_frame(output->recorder, fd,
2953 output->current->stride);
2954 if (ret < 0) {
2955 weston_log("[libva recorder] aborted: %m\n");
2956 recorder_destroy(output);
2957 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002958}
2959
2960static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002961create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002962 const char *filename)
2963{
2964 int fd;
2965 drm_magic_t magic;
2966
Giulio Camuffo954f1832014-10-11 18:27:30 +03002967 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002968 if (fd < 0)
2969 return NULL;
2970
2971 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002972 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002973
2974 return vaapi_recorder_create(fd, width, height, filename);
2975}
2976
2977static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002978recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002979 void *data)
2980{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002981 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002982 struct drm_output *output;
2983 int width, height;
2984
Giulio Camuffo954f1832014-10-11 18:27:30 +03002985 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002986 struct drm_output, base.link);
2987
2988 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002989 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002990 weston_log("failed to start vaapi recorder: "
2991 "output format not supported\n");
2992 return;
2993 }
2994
Hardeningff39efa2013-09-18 23:56:35 +02002995 width = output->base.current_mode->width;
2996 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002997
2998 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002999 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003000 if (!output->recorder) {
3001 weston_log("failed to create vaapi recorder\n");
3002 return;
3003 }
3004
3005 output->base.disable_planes++;
3006
3007 output->recorder_frame_listener.notify = recorder_frame_notify;
3008 wl_signal_add(&output->base.frame_signal,
3009 &output->recorder_frame_listener);
3010
3011 weston_output_schedule_repaint(&output->base);
3012
3013 weston_log("[libva recorder] initialized\n");
3014 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003015 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003016 }
3017}
3018#else
3019static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003020recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003021 void *data)
3022{
3023 weston_log("Compiled without libva support\n");
3024}
3025#endif
3026
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003027static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003028switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003029{
3030 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003031 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003032
Giulio Camuffo954f1832014-10-11 18:27:30 +03003033 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003034 return;
3035
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003036 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3037
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003038 weston_log("Switching to GL renderer\n");
3039
Giulio Camuffo954f1832014-10-11 18:27:30 +03003040 b->gbm = create_gbm_device(b->drm.fd);
3041 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003042 weston_log("Failed to create gbm device. "
3043 "Aborting renderer switch\n");
3044 return;
3045 }
3046
Giulio Camuffo954f1832014-10-11 18:27:30 +03003047 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003048 pixman_renderer_output_destroy(&output->base);
3049
Giulio Camuffo954f1832014-10-11 18:27:30 +03003050 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003051
Giulio Camuffo954f1832014-10-11 18:27:30 +03003052 if (drm_backend_create_gl_renderer(b) < 0) {
3053 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003054 weston_log("Failed to create GL renderer. Quitting.\n");
3055 /* FIXME: we need a function to shutdown cleanly */
3056 assert(0);
3057 }
3058
Giulio Camuffo954f1832014-10-11 18:27:30 +03003059 wl_list_for_each(output, &b->compositor->output_list, base.link)
3060 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003061
Giulio Camuffo954f1832014-10-11 18:27:30 +03003062 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003063
3064 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3065 if (linux_dmabuf_setup(b->compositor) < 0)
3066 weston_log("Error: initializing dmabuf "
3067 "support failed.\n");
3068 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003069}
3070
3071static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003072renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3073 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003074{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003075 struct drm_backend *b =
3076 (struct drm_backend *) keyboard->seat->compositor;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003077
Giulio Camuffo954f1832014-10-11 18:27:30 +03003078 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003079}
3080
Giulio Camuffo954f1832014-10-11 18:27:30 +03003081static struct drm_backend *
3082drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003083 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003084{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003085 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003086 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003087 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003088 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003089 const char *seat_id = default_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003090
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003091 weston_log("initializing drm backend\n");
3092
Giulio Camuffo954f1832014-10-11 18:27:30 +03003093 b = zalloc(sizeof *b);
3094 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003095 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003096
Pekka Paalanen68583832015-05-19 09:53:16 +03003097 /*
3098 * KMS support for hardware planes cannot properly synchronize
3099 * without nuclear page flip. Without nuclear/atomic, hw plane
3100 * and cursor plane updates would either tear or cause extra
3101 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003102 * to a fraction. For cursors, it's not so bad, so they are
3103 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003104 *
3105 * These can be enabled again when nuclear/atomic support lands.
3106 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003107 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003108 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003109 b->use_pixman = config->use_pixman;
3110 b->configure_output = config->configure_output;
3111 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003112
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003113 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3114 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003115
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003116 if (config->seat_id)
3117 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003118
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003119 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003120 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3121 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003122 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003123 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003124 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003125 goto err_compositor;
3126 }
3127
Giulio Camuffo954f1832014-10-11 18:27:30 +03003128 b->udev = udev_new();
3129 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003130 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003131 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003132 }
3133
Giulio Camuffo954f1832014-10-11 18:27:30 +03003134 b->session_listener.notify = session_notify;
3135 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003136
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003137 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003138 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003139 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003140 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003141 }
David Herrmann0af066f2012-10-29 19:21:16 +01003142 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003143
Giulio Camuffo954f1832014-10-11 18:27:30 +03003144 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003145 weston_log("failed to initialize kms\n");
3146 goto err_udev_dev;
3147 }
3148
Giulio Camuffo954f1832014-10-11 18:27:30 +03003149 if (b->use_pixman) {
3150 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003151 weston_log("failed to initialize pixman renderer\n");
3152 goto err_udev_dev;
3153 }
3154 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003155 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003156 weston_log("failed to initialize egl\n");
3157 goto err_udev_dev;
3158 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003159 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003160
Giulio Camuffo954f1832014-10-11 18:27:30 +03003161 b->base.destroy = drm_destroy;
3162 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003163
Giulio Camuffo954f1832014-10-11 18:27:30 +03003164 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003165
Bob Ham91880f12016-01-12 10:21:47 +00003166 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003167
Giulio Camuffo954f1832014-10-11 18:27:30 +03003168 wl_list_init(&b->sprite_list);
3169 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003170
Giulio Camuffo954f1832014-10-11 18:27:30 +03003171 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003172 compositor, b->udev, seat_id,
3173 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003174 weston_log("failed to create input devices\n");
3175 goto err_sprite;
3176 }
3177
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003178 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003179 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003180 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003181 }
3182
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003183 /* A this point we have some idea of whether or not we have a working
3184 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003185 if (!b->cursors_are_broken)
3186 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003187
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003188 path = NULL;
3189
Giulio Camuffo954f1832014-10-11 18:27:30 +03003190 loop = wl_display_get_event_loop(compositor->wl_display);
3191 b->drm_source =
3192 wl_event_loop_add_fd(loop, b->drm.fd,
3193 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003194
Giulio Camuffo954f1832014-10-11 18:27:30 +03003195 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3196 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003197 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003198 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003199 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003200 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003201 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003202 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003203 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003204 udev_monitor_get_fd(b->udev_monitor),
3205 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003206
Giulio Camuffo954f1832014-10-11 18:27:30 +03003207 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003208 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003209 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003210 }
3211
Daniel Stonea96b93c2012-06-22 14:04:37 +01003212 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003213
Giulio Camuffo954f1832014-10-11 18:27:30 +03003214 weston_compositor_add_debug_binding(compositor, KEY_O,
3215 planes_binding, b);
3216 weston_compositor_add_debug_binding(compositor, KEY_C,
3217 planes_binding, b);
3218 weston_compositor_add_debug_binding(compositor, KEY_V,
3219 planes_binding, b);
3220 weston_compositor_add_debug_binding(compositor, KEY_Q,
3221 recorder_binding, b);
3222 weston_compositor_add_debug_binding(compositor, KEY_W,
3223 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003224
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003225 if (compositor->renderer->import_dmabuf) {
3226 if (linux_dmabuf_setup(compositor) < 0)
3227 weston_log("Error: initializing dmabuf "
3228 "support failed.\n");
3229 }
3230
Giulio Camuffo954f1832014-10-11 18:27:30 +03003231 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003232
Giulio Camuffo954f1832014-10-11 18:27:30 +03003233 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003234
3235err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003236 wl_event_source_remove(b->udev_drm_source);
3237 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003238err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003239 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003240err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003241 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003242err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003243 if (b->gbm)
3244 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003245 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003246err_udev_dev:
3247 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003248err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003249 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003250err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003251 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003252err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003253 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003255 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003256}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003257
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003258static void
3259config_init_to_defaults(struct weston_drm_backend_config *config)
3260{
3261}
3262
Giulio Camuffo954f1832014-10-11 18:27:30 +03003263WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003264backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003265 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003266{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003267 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003268 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003269
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003270 if (config_base == NULL ||
3271 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3272 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3273 weston_log("drm backend config structure is invalid\n");
3274 return -1;
3275 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003276
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003277 config_init_to_defaults(&config);
3278 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003279
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003280 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003281 if (b == NULL)
3282 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003283
Giulio Camuffo954f1832014-10-11 18:27:30 +03003284 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003285}