blob: 4825b46639f2f1865597d829099d95b27b411b76 [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020055#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070056#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100057#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010058#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030059#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020060#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030061#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040062
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030063#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
64#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
65#endif
66
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030067#ifndef DRM_CAP_CURSOR_WIDTH
68#define DRM_CAP_CURSOR_WIDTH 0x8
69#endif
70
71#ifndef DRM_CAP_CURSOR_HEIGHT
72#define DRM_CAP_CURSOR_HEIGHT 0x9
73#endif
74
75#ifndef GBM_BO_USE_CURSOR
76#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
77#endif
78
Giulio Camuffo954f1832014-10-11 18:27:30 +030079struct drm_backend {
80 struct weston_backend base;
81 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040082
83 struct udev *udev;
84 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010086 struct udev_monitor *udev_monitor;
87 struct wl_event_source *udev_drm_source;
88
Benjamin Franzke2af7f102011-03-02 11:14:59 +010089 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010090 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010091 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030092 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010093 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020094 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050095 uint32_t *crtcs;
96 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050097 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010098 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070099 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100100 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200101
Rob Clark4339add2012-08-09 14:18:28 -0500102 /* we need these parameters in order to not fail drmModeAddFB2()
103 * due to out of bounds dimensions, and then mistakenly set
104 * sprites_are_broken:
105 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200106 uint32_t min_width, max_width;
107 uint32_t min_height, max_height;
108 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500109
Jesse Barnes58ef3792012-02-23 09:45:49 -0500110 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500111 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200112 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500113
Rob Clarkab5b1e32012-08-09 13:24:45 -0500114 int cursors_are_broken;
115
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200116 int use_pixman;
117
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200118 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300119
Rob Bradfordd355b802013-05-31 18:09:55 +0100120 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300121
Daniel Stone70d337d2015-06-16 18:42:23 +0100122 int32_t cursor_width;
123 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700124
125 /** Callback used to configure the outputs.
126 *
127 * This function will be called by the backend when a new DRM
128 * output needs to be configured.
129 */
130 enum weston_drm_backend_output_mode
131 (*configure_output)(struct weston_compositor *compositor,
132 bool use_current_mode,
133 const char *name,
134 struct weston_drm_backend_output_config *output_config);
135 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400136};
137
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400138struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500139 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400140 drmModeModeInfo mode_info;
141};
142
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300143struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200144 uint32_t fb_id, stride, handle, size;
145 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300146 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200147 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200148
149 /* Used by gbm fbs */
150 struct gbm_bo *bo;
151
152 /* Used by dumb fbs */
153 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300154};
155
Richard Hughes2b2092a2013-04-24 14:58:02 +0100156struct drm_edid {
157 char eisa_id[13];
158 char monitor_name[13];
159 char pnp_id[5];
160 char serial_number[13];
161};
162
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400163struct drm_output {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500164 struct weston_output base;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400165
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400166 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500167 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400168 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700169 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100170 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300171 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100172 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200173
Daniel Stone36609c72015-06-18 07:49:02 +0100174 enum dpms_enum dpms;
175
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300176 int vblank_pending;
177 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800178 int destroy_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300179
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100180 struct gbm_surface *gbm_surface;
181 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400182 struct weston_plane cursor_plane;
183 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500184 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400185 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300186 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200187 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200188
189 struct drm_fb *dumb[2];
190 pixman_image_t *image[2];
191 int current_image;
192 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300193
194 struct vaapi_recorder *recorder;
195 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400196};
197
Jesse Barnes58ef3792012-02-23 09:45:49 -0500198/*
199 * An output has a primary display plane plus zero or more sprites for
200 * blending display contents.
201 */
202struct drm_sprite {
203 struct wl_list link;
204
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400205 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500206
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200207 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300208 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300209 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500210
Jesse Barnes58ef3792012-02-23 09:45:49 -0500211 uint32_t possible_crtcs;
212 uint32_t plane_id;
213 uint32_t count_formats;
214
215 int32_t src_x, src_y;
216 uint32_t src_w, src_h;
217 uint32_t dest_x, dest_y;
218 uint32_t dest_w, dest_h;
219
220 uint32_t formats[];
221};
222
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300223static struct gl_renderer_interface *gl_renderer;
224
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500225static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400226
Armin Krezović545dba62016-08-05 15:54:18 +0200227static inline struct drm_output *
228to_drm_output(struct weston_output *base)
229{
230 return container_of(base, struct drm_output, base);
231}
232
233static inline struct drm_backend *
234to_drm_backend(struct weston_compositor *base)
235{
236 return container_of(base->backend, struct drm_backend, base);
237}
238
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400239static void
240drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400241
Mario Kleinerf507ec32015-06-21 21:25:14 +0200242static void
243drm_output_update_msc(struct drm_output *output, unsigned int seq);
244
Jesse Barnes58ef3792012-02-23 09:45:49 -0500245static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200246drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500247{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200248 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200249 struct drm_backend *b = to_drm_backend(ec);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500250 int crtc;
251
Giulio Camuffo954f1832014-10-11 18:27:30 +0300252 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
253 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500254 continue;
255
256 if (supported & (1 << crtc))
257 return -1;
258 }
259
260 return 0;
261}
262
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300263static void
264drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
265{
266 struct drm_fb *fb = data;
267 struct gbm_device *gbm = gbm_bo_get_device(bo);
268
269 if (fb->fb_id)
270 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
271
Pekka Paalanende685b82012-12-04 15:58:12 +0200272 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300273
274 free(data);
275}
276
277static struct drm_fb *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300278drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
279 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200280{
281 struct drm_fb *fb;
282 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300283 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200284
285 struct drm_mode_create_dumb create_arg;
286 struct drm_mode_destroy_dumb destroy_arg;
287 struct drm_mode_map_dumb map_arg;
288
Peter Huttererf3d62272013-08-08 11:57:05 +1000289 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200290 if (!fb)
291 return NULL;
292
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300293 switch (format) {
294 case GBM_FORMAT_XRGB8888:
295 bpp = 32;
296 depth = 24;
297 break;
298 case GBM_FORMAT_RGB565:
299 bpp = depth = 16;
300 break;
301 default:
302 return NULL;
303 }
304
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700305 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300306 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200307 create_arg.width = width;
308 create_arg.height = height;
309
Giulio Camuffo954f1832014-10-11 18:27:30 +0300310 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200311 if (ret)
312 goto err_fb;
313
314 fb->handle = create_arg.handle;
315 fb->stride = create_arg.pitch;
316 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300317 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200318
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300319 ret = -1;
320
321 if (!b->no_addfb2) {
322 uint32_t handles[4], pitches[4], offsets[4];
323
324 handles[0] = fb->handle;
325 pitches[0] = fb->stride;
326 offsets[0] = 0;
327
328 ret = drmModeAddFB2(b->drm.fd, width, height,
329 format, handles, pitches, offsets,
330 &fb->fb_id, 0);
331 if (ret) {
332 weston_log("addfb2 failed: %m\n");
333 b->no_addfb2 = 1;
334 }
335 }
336
337 if (ret) {
338 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
339 fb->stride, fb->handle, &fb->fb_id);
340 }
341
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200342 if (ret)
343 goto err_bo;
344
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700345 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200346 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400347 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200348 if (ret)
349 goto err_add_fb;
350
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500351 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300352 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200353 if (fb->map == MAP_FAILED)
354 goto err_add_fb;
355
356 return fb;
357
358err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300359 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200360err_bo:
361 memset(&destroy_arg, 0, sizeof(destroy_arg));
362 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300363 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200364err_fb:
365 free(fb);
366 return NULL;
367}
368
369static void
370drm_fb_destroy_dumb(struct drm_fb *fb)
371{
372 struct drm_mode_destroy_dumb destroy_arg;
373
374 if (!fb->map)
375 return;
376
377 if (fb->fb_id)
378 drmModeRmFB(fb->fd, fb->fb_id);
379
380 weston_buffer_reference(&fb->buffer_ref, NULL);
381
382 munmap(fb->map, fb->size);
383
384 memset(&destroy_arg, 0, sizeof(destroy_arg));
385 destroy_arg.handle = fb->handle;
386 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
387
388 free(fb);
389}
390
391static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500392drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300393 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300394{
395 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200396 uint32_t width, height;
Derek Foreman482ffdf2016-07-08 12:50:57 -0500397 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300398 int ret;
399
400 if (fb)
401 return fb;
402
Bryce Harringtonde16d892014-11-20 22:21:57 -0800403 fb = zalloc(sizeof *fb);
404 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200405 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300406
407 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300408
409 width = gbm_bo_get_width(bo);
410 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200411 fb->stride = gbm_bo_get_stride(bo);
412 fb->handle = gbm_bo_get_handle(bo).u32;
413 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300414 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300415
Giulio Camuffo954f1832014-10-11 18:27:30 +0300416 if (backend->min_width > width || width > backend->max_width ||
417 backend->min_height > height ||
418 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200419 weston_log("bo geometry out of bounds\n");
420 goto err_free;
421 }
422
423 ret = -1;
424
Giulio Camuffo954f1832014-10-11 18:27:30 +0300425 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200426 handles[0] = fb->handle;
427 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200428 offsets[0] = 0;
429
Giulio Camuffo954f1832014-10-11 18:27:30 +0300430 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200431 format, handles, pitches, offsets,
432 &fb->fb_id, 0);
433 if (ret) {
434 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300435 backend->no_addfb2 = 1;
436 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200437 }
438 }
439
440 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300441 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200442 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200443
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300444 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200445 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200446 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300447 }
448
449 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
450
451 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200452
453err_free:
454 free(fb);
455 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300456}
457
458static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500459drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200460{
Pekka Paalanende685b82012-12-04 15:58:12 +0200461 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200462
463 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200464
Pekka Paalanende685b82012-12-04 15:58:12 +0200465 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200466}
467
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200468static void
469drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
470{
471 if (!fb)
472 return;
473
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200474 if (fb->map &&
475 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200476 drm_fb_destroy_dumb(fb);
477 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200478 if (fb->is_client_buffer)
479 gbm_bo_destroy(fb->bo);
480 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100481 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600482 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200483 }
484}
485
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500486static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200487drm_output_check_scanout_format(struct drm_output *output,
488 struct weston_surface *es, struct gbm_bo *bo)
489{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200490 uint32_t format;
491 pixman_region32_t r;
492
493 format = gbm_bo_get_format(bo);
494
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700495 if (format == GBM_FORMAT_ARGB8888) {
496 /* We can scanout an ARGB buffer if the surface's
497 * opaque region covers the whole output, but we have
498 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800499 pixman_region32_init_rect(&r, 0, 0,
500 output->base.width,
501 output->base.height);
502 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200503
504 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500505 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200506
507 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500508 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700509
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100510 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700511 return format;
512
513 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200514}
515
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400516static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200517drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500519{
Armin Krezović545dba62016-08-05 15:54:18 +0200520 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200522 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300523 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500524 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500525
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 if (ev->geometry.x != output->base.x ||
527 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300528 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200529 buffer->width != output->base.current_mode->width ||
530 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200531 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500532 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400533 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500534
Pekka Paalanen5580f222015-02-17 16:33:18 +0200535 if (ev->geometry.scissor_enabled)
536 return NULL;
537
Giulio Camuffo954f1832014-10-11 18:27:30 +0300538 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700539 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500540
Rob Bradford9b101872012-09-14 23:25:41 +0100541 /* Unable to use the buffer for scanout */
542 if (!bo)
543 return NULL;
544
Jason Ekstranda7af7042013-10-12 22:38:11 -0500545 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500546 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300547 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400548 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300549 }
550
Giulio Camuffo954f1832014-10-11 18:27:30 +0300551 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300552 if (!output->next) {
553 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400554 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300555 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500556
Pekka Paalanende685b82012-12-04 15:58:12 +0200557 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500558
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400559 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500560}
561
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500562static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200563drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400564{
Armin Krezović545dba62016-08-05 15:54:18 +0200565 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300566 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400567
Giulio Camuffo954f1832014-10-11 18:27:30 +0300568 output->base.compositor->renderer->repaint_output(&output->base,
569 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400570
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100571 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300572 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200573 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400574 return;
575 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300576
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100577 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300578 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200579 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100580 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300581 return;
582 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400583}
584
585static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200586drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
587{
588 struct weston_compositor *ec = output->base.compositor;
589 pixman_region32_t total_damage, previous_damage;
590
591 pixman_region32_init(&total_damage);
592 pixman_region32_init(&previous_damage);
593
594 pixman_region32_copy(&previous_damage, damage);
595
596 pixman_region32_union(&total_damage, damage, &output->previous_damage);
597 pixman_region32_copy(&output->previous_damage, &previous_damage);
598
599 output->current_image ^= 1;
600
601 output->next = output->dumb[output->current_image];
602 pixman_renderer_output_set_buffer(&output->base,
603 output->image[output->current_image]);
604
605 ec->renderer->repaint_output(&output->base, &total_damage);
606
607 pixman_region32_fini(&total_damage);
608 pixman_region32_fini(&previous_damage);
609}
610
611static void
612drm_output_render(struct drm_output *output, pixman_region32_t *damage)
613{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300614 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200615 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200616
Giulio Camuffo954f1832014-10-11 18:27:30 +0300617 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200618 drm_output_render_pixman(output, damage);
619 else
620 drm_output_render_gl(output, damage);
621
Giulio Camuffo954f1832014-10-11 18:27:30 +0300622 pixman_region32_subtract(&c->primary_plane.damage,
623 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200624}
625
626static void
Richard Hughese7299962013-05-01 21:52:12 +0100627drm_output_set_gamma(struct weston_output *output_base,
628 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
629{
630 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200631 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300632 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200633 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100634
635 /* check */
636 if (output_base->gamma_size != size)
637 return;
638 if (!output->original_crtc)
639 return;
640
Giulio Camuffo954f1832014-10-11 18:27:30 +0300641 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100642 output->crtc_id,
643 size, r, g, b);
644 if (rc)
645 weston_log("set gamma failed: %m\n");
646}
647
Bryce Harringtonada4f072015-06-30 13:25:46 -0700648/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200649 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700650 * The pipe parameter indicates which CRTC is in use. Knowing this, we
651 * can determine which vblank sequence type to use for it. Traditional
652 * cards had only two CRTCs, with CRTC 0 using no special flags, and
653 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
654 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200655 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700656 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
657 * 0-31. If this is non-zero it indicates we're dealing with a
658 * multi-gpu situation and we need to calculate the vblank sync
659 * using DRM_BLANK_HIGH_CRTC_MASK.
660 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300661static unsigned int
662drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200663{
664 if (output->pipe > 1)
665 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
666 DRM_VBLANK_HIGH_CRTC_MASK;
667 else if (output->pipe > 0)
668 return DRM_VBLANK_SECONDARY;
669 else
670 return 0;
671}
672
David Herrmann1edf44c2013-10-22 17:11:26 +0200673static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500674drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400675 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100676{
Armin Krezović545dba62016-08-05 15:54:18 +0200677 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300678 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200679 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500680 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400681 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500682 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100683
Xiong Zhangabd5d472013-10-11 14:43:07 +0800684 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200685 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800686
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300687 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400688 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300689 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200690 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100691
Hardeningff39efa2013-09-18 23:56:35 +0200692 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200693 if (!output->current ||
694 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300695 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300696 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400697 &output->connector_id, 1,
698 &mode->mode_info);
699 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200700 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200701 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400702 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300703 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200704 }
705
Giulio Camuffo954f1832014-10-11 18:27:30 +0300706 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300707 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500708 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200709 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200710 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500711 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100712
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300713 output->page_flip_pending = 1;
714
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400715 drm_output_set_cursor(output);
716
Jesse Barnes58ef3792012-02-23 09:45:49 -0500717 /*
718 * Now, update all the sprite surfaces
719 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300720 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200721 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500722 drmVBlank vbl = {
723 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
724 .request.sequence = 1,
725 };
726
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200727 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200728 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500729 continue;
730
Giulio Camuffo954f1832014-10-11 18:27:30 +0300731 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200732 fb_id = s->next->fb_id;
733
Giulio Camuffo954f1832014-10-11 18:27:30 +0300734 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200735 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500736 s->dest_x, s->dest_y,
737 s->dest_w, s->dest_h,
738 s->src_x, s->src_y,
739 s->src_w, s->src_h);
740 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200741 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500742 ret, strerror(errno));
743
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200744 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500745
Jesse Barnes58ef3792012-02-23 09:45:49 -0500746 /*
747 * Queue a vblank signal so we know when the surface
748 * becomes active on the display or has been replaced.
749 */
750 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300751 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500752 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200753 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500754 ret, strerror(errno));
755 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300756
757 s->output = output;
758 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500759 }
760
David Herrmann1edf44c2013-10-22 17:11:26 +0200761 return 0;
762
763err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800764 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200765 if (output->next) {
766 drm_output_release_fb(output, output->next);
767 output->next = NULL;
768 }
769
770 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400771}
772
773static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200774drm_output_start_repaint_loop(struct weston_output *output_base)
775{
Armin Krezović545dba62016-08-05 15:54:18 +0200776 struct drm_output *output = to_drm_output(output_base);
777 struct drm_backend *backend =
778 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200779 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200780 struct timespec ts, tnow;
781 struct timespec vbl2now;
782 int64_t refresh_nsec;
783 int ret;
784 drmVBlank vbl = {
785 .request.type = DRM_VBLANK_RELATIVE,
786 .request.sequence = 0,
787 .request.signal = 0,
788 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300789
Xiong Zhangabd5d472013-10-11 14:43:07 +0800790 if (output->destroy_pending)
791 return;
792
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300793 if (!output->current) {
794 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200795 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300796 }
797
Mario Kleinerf507ec32015-06-21 21:25:14 +0200798 /* Try to get current msc and timestamp via instant query */
799 vbl.request.type |= drm_waitvblank_pipe(output);
800 ret = drmWaitVBlank(backend->drm.fd, &vbl);
801
802 /* Error ret or zero timestamp means failure to get valid timestamp */
803 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
804 ts.tv_sec = vbl.reply.tval_sec;
805 ts.tv_nsec = vbl.reply.tval_usec * 1000;
806
807 /* Valid timestamp for most recent vblank - not stale?
808 * Stale ts could happen on Linux 3.17+, so make sure it
809 * is not older than 1 refresh duration since now.
810 */
811 weston_compositor_read_presentation_clock(backend->compositor,
812 &tnow);
813 timespec_sub(&vbl2now, &tnow, &ts);
814 refresh_nsec =
815 millihz_to_nsec(output->base.current_mode->refresh);
816 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
817 drm_output_update_msc(output, vbl.reply.sequence);
818 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200819 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200820 return;
821 }
822 }
823
824 /* Immediate query didn't provide valid timestamp.
825 * Use pageflip fallback.
826 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300827 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200828
Giulio Camuffo954f1832014-10-11 18:27:30 +0300829 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200830 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
831 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200832 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200833 }
David Herrmann3c688c52013-10-22 17:11:25 +0200834
835 return;
836
837finish_frame:
838 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300839 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200840 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200841 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200842}
843
844static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400845drm_output_update_msc(struct drm_output *output, unsigned int seq)
846{
847 uint64_t msc_hi = output->base.msc >> 32;
848
849 if (seq < (output->base.msc & 0xffffffff))
850 msc_hi++;
851
852 output->base.msc = (msc_hi << 32) + seq;
853}
854
855static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500856vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
857 void *data)
858{
859 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300860 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400861 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200862 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
863 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300864
Pekka Paalanen641307c2014-09-23 22:08:47 -0400865 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300866 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500867
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200868 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200869 s->current = s->next;
870 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300871
872 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400873 ts.tv_sec = sec;
874 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200875 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300876 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500877}
878
879static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800880drm_output_destroy(struct weston_output *output_base);
881
882static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400883page_flip_handler(int fd, unsigned int frame,
884 unsigned int sec, unsigned int usec, void *data)
885{
Armin Krezović545dba62016-08-05 15:54:18 +0200886 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400887 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200888 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
889 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
890 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400891
Pekka Paalanen641307c2014-09-23 22:08:47 -0400892 drm_output_update_msc(output, frame);
893
Jonas Ådahle5a12252013-04-05 23:07:11 +0200894 /* We don't set page_flip_pending on start_repaint_loop, in that case
895 * we just want to page flip to the current buffer to get an accurate
896 * timestamp */
897 if (output->page_flip_pending) {
898 drm_output_release_fb(output, output->current);
899 output->current = output->next;
900 output->next = NULL;
901 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300902
Jonas Ådahle5a12252013-04-05 23:07:11 +0200903 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400904
Xiong Zhangabd5d472013-10-11 14:43:07 +0800905 if (output->destroy_pending)
906 drm_output_destroy(&output->base);
907 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400908 ts.tv_sec = sec;
909 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200910 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300911
912 /* We can't call this from frame_notify, because the output's
913 * repaint needed flag is cleared just after that */
914 if (output->recorder)
915 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300916 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200917}
918
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500919static uint32_t
920drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500922{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500923 uint32_t i, format;
924
925 format = gbm_bo_get_format(bo);
926
927 if (format == GBM_FORMAT_ARGB8888) {
928 pixman_region32_t r;
929
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500930 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600931 ev->surface->width,
932 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500934
935 if (!pixman_region32_not_empty(&r))
936 format = GBM_FORMAT_XRGB8888;
937
938 pixman_region32_fini(&r);
939 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500940
941 for (i = 0; i < s->count_formats; i++)
942 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500943 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500944
945 return 0;
946}
947
948static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500950{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 return !ev->transform.enabled ||
952 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500953}
954
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400955static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200956drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500958{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200959 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200960 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200961 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300962 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500963 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300964 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500965 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500966 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500967 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200968 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500969 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400970 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500971
Giulio Camuffo954f1832014-10-11 18:27:30 +0300972 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200973 return NULL;
974
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200975 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200976 return NULL;
977
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200978 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200979 return NULL;
980
Giulio Camuffo954f1832014-10-11 18:27:30 +0300981 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400982 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500983
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200984 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400985 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400988 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300989 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200992 return NULL;
993
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300994 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500995 return NULL;
996
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400998 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500999
Giulio Camuffo954f1832014-10-11 18:27:30 +03001000 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001001 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001002 continue;
1003
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001004 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001005 found = 1;
1006 break;
1007 }
1008 }
1009
1010 /* No sprites available */
1011 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001012 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001013
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001014 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001015#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001016 /* XXX: TODO:
1017 *
1018 * Use AddFB2 directly, do not go via GBM.
1019 * Add support for multiplanar formats.
1020 * Both require refactoring in the DRM-backend to
1021 * support a mix of gbm_bos and drmfbs.
1022 */
1023 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001024 .fd = dmabuf->attributes.fd[0],
1025 .width = dmabuf->attributes.width,
1026 .height = dmabuf->attributes.height,
1027 .stride = dmabuf->attributes.stride[0],
1028 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001029 };
1030
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001031 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001032 return NULL;
1033
1034 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1035 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001036#else
1037 return NULL;
1038#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001039 } else {
1040 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1041 buffer_resource, GBM_BO_USE_SCANOUT);
1042 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001043 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001044 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001045
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001047 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001048 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001049 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001050 }
1051
Giulio Camuffo954f1832014-10-11 18:27:30 +03001052 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001053 if (!s->next) {
1054 gbm_bo_destroy(bo);
1055 return NULL;
1056 }
1057
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001061 s->plane.x = box->x1;
1062 s->plane.y = box->y1;
1063
Jesse Barnes58ef3792012-02-23 09:45:49 -05001064 /*
1065 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001066 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001067 * for us already).
1068 */
1069 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001071 &output->base.region);
1072 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001073 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001074 tbox = weston_transformed_rect(output->base.width,
1075 output->base.height,
1076 output->base.transform,
1077 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001078 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001079 s->dest_x = tbox.x1;
1080 s->dest_y = tbox.y1;
1081 s->dest_w = tbox.x2 - tbox.x1;
1082 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001083 pixman_region32_fini(&dest_rect);
1084
1085 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001087 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001088 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001089
Jason Ekstranda7af7042013-10-12 22:38:11 -05001090 weston_view_from_global_fixed(ev,
1091 wl_fixed_from_int(box->x1),
1092 wl_fixed_from_int(box->y1),
1093 &sx1, &sy1);
1094 weston_view_from_global_fixed(ev,
1095 wl_fixed_from_int(box->x2),
1096 wl_fixed_from_int(box->y2),
1097 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001098
1099 if (sx1 < 0)
1100 sx1 = 0;
1101 if (sy1 < 0)
1102 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001103 if (sx2 > wl_fixed_from_int(ev->surface->width))
1104 sx2 = wl_fixed_from_int(ev->surface->width);
1105 if (sy2 > wl_fixed_from_int(ev->surface->height))
1106 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001107
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001108 tbox.x1 = sx1;
1109 tbox.y1 = sy1;
1110 tbox.x2 = sx2;
1111 tbox.y2 = sy2;
1112
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001113 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1114 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001115 viewport->buffer.transform,
1116 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001117 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001118
1119 s->src_x = tbox.x1 << 8;
1120 s->src_y = tbox.y1 << 8;
1121 s->src_w = (tbox.x2 - tbox.x1) << 8;
1122 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001123 pixman_region32_fini(&src_rect);
1124
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001125 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001126}
1127
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001128static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001129drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001131{
Armin Krezović545dba62016-08-05 15:54:18 +02001132 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001133 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001134 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001135
Derek Foremanbe428b32015-11-24 11:39:38 -06001136 if (ev->transform.enabled &&
1137 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1138 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001139 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001140 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001141 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1142 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001143 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001144 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001146 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001147 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001148 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001149 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001150 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001151 if (ev->geometry.scissor_enabled)
1152 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001153 if (ev->surface->buffer_ref.buffer == NULL)
1154 return NULL;
1155 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1156 if (!shmbuf)
1157 return NULL;
1158 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1159 return NULL;
1160 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001161 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001162 return NULL;
1163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001165
1166 return &output->cursor_plane;
1167}
1168
Pekka Paalanend0ead482014-06-16 12:05:40 +03001169/**
1170 * Update the image for the current cursor surface
1171 *
1172 * @param b DRM backend structure
1173 * @param bo GBM buffer object to write into
1174 * @param ev View to use for cursor image
1175 */
1176static void
1177cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1178 struct weston_view *ev)
1179{
1180 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1181 uint32_t buf[b->cursor_width * b->cursor_height];
1182 int32_t stride;
1183 uint8_t *s;
1184 int i;
1185
1186 assert(buffer && buffer->shm_buffer);
1187 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1188 assert(ev->surface->width <= b->cursor_width);
1189 assert(ev->surface->height <= b->cursor_height);
1190
1191 memset(buf, 0, sizeof buf);
1192 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1193 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1194
1195 wl_shm_buffer_begin_access(buffer->shm_buffer);
1196 for (i = 0; i < ev->surface->height; i++)
1197 memcpy(buf + i * b->cursor_width,
1198 s + i * stride,
1199 ev->surface->width * 4);
1200 wl_shm_buffer_end_access(buffer->shm_buffer);
1201
1202 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1203 weston_log("failed update cursor: %m\n");
1204}
1205
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001206static void
1207drm_output_set_cursor(struct drm_output *output)
1208{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001210 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001211 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001212 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001213 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001214 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001215
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 output->cursor_view = NULL;
1217 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001218 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001219 output->cursor_plane.x = INT32_MIN;
1220 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001221 return;
1222 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001223
Neil Robertse5051712013-11-13 15:44:06 +00001224 buffer = ev->surface->buffer_ref.buffer;
1225
1226 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001227 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001228 pixman_region32_fini(&output->cursor_plane.damage);
1229 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001230 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001231 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001232
Pekka Paalanend0ead482014-06-16 12:05:40 +03001233 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001234 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001235 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1236 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001237 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001238 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001239 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001240 }
1241
Derek Foremanbe428b32015-11-24 11:39:38 -06001242 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001243
1244 /* From global to output space, output transform is guaranteed to be
1245 * NORMAL by drm_output_prepare_cursor_view().
1246 */
1247 x = (x - output->base.x) * output->base.current_scale;
1248 y = (y - output->base.y) * output->base.current_scale;
1249
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001250 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001252 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001253 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001254 }
1255
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001256 output->cursor_plane.x = x;
1257 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001258 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001259}
1260
Jesse Barnes58ef3792012-02-23 09:45:49 -05001261static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001262drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001263{
Armin Krezović545dba62016-08-05 15:54:18 +02001264 struct drm_backend *b = to_drm_backend(output_base->compositor);
1265 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001267 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001268 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001269
1270 /*
1271 * Find a surface for each sprite in the output using some heuristics:
1272 * 1) size
1273 * 2) frequency of update
1274 * 3) opacity (though some hw might support alpha blending)
1275 * 4) clipping (this can be fixed with color keys)
1276 *
1277 * The idea is to save on blitting since this should save power.
1278 * If we can get a large video surface on the sprite for example,
1279 * the main display surface may not need to update at all, and
1280 * the client buffer can be used directly for the sprite surface
1281 * as we do for flipping full screen surfaces.
1282 */
1283 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001284 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285
Giulio Camuffo954f1832014-10-11 18:27:30 +03001286 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001287 struct weston_surface *es = ev->surface;
1288
1289 /* Test whether this buffer can ever go into a plane:
1290 * non-shm, or small enough to be a cursor.
1291 *
1292 * Also, keep a reference when using the pixman renderer.
1293 * That makes it possible to do a seamless switch to the GL
1294 * renderer and since the pixman renderer keeps a reference
1295 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001296 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001297 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001298 (es->buffer_ref.buffer &&
1299 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001300 (ev->surface->width <= b->cursor_width &&
1301 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001302 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001303 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001304 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001305
Jesse Barnes58ef3792012-02-23 09:45:49 -05001306 pixman_region32_init(&surface_overlap);
1307 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001309
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001310 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001311 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001312 next_plane = primary;
1313 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001315 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001317 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001319 if (next_plane == NULL)
1320 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001323
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001324 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001325 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001327
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001328 if (next_plane == primary ||
1329 next_plane == &output->cursor_plane) {
1330 /* cursor plane involves a copy */
1331 ev->psf_flags = 0;
1332 } else {
1333 /* All other planes are a direct scanout of a
1334 * single client buffer.
1335 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001336 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001337 }
1338
Jesse Barnes58ef3792012-02-23 09:45:49 -05001339 pixman_region32_fini(&surface_overlap);
1340 }
1341 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001342}
1343
Matt Roper361d2ad2011-08-29 13:52:23 -07001344static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001345drm_output_fini_pixman(struct drm_output *output);
1346
1347static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001348drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001349{
Armin Krezović545dba62016-08-05 15:54:18 +02001350 struct drm_output *output = to_drm_output(output_base);
1351 struct drm_backend *b = to_drm_backend(output->base.compositor);
Matt Roper361d2ad2011-08-29 13:52:23 -07001352 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001353
Xiong Zhangabd5d472013-10-11 14:43:07 +08001354 if (output->page_flip_pending) {
1355 output->destroy_pending = 1;
1356 weston_log("destroy output while page flip pending\n");
1357 return;
1358 }
1359
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001360 if (output->backlight)
1361 backlight_destroy(output->backlight);
1362
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001363 drmModeFreeProperty(output->dpms_prop);
1364
Matt Roper361d2ad2011-08-29 13:52:23 -07001365 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001366 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001367
1368 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001369 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001370 origcrtc->x, origcrtc->y,
1371 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001372 drmModeFreeCrtc(origcrtc);
1373
Giulio Camuffo954f1832014-10-11 18:27:30 +03001374 b->crtc_allocator &= ~(1 << output->crtc_id);
1375 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001376
Giulio Camuffo954f1832014-10-11 18:27:30 +03001377 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001378 drm_output_fini_pixman(output);
1379 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001380 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001381 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001382 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001383
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001384 weston_plane_release(&output->fb_plane);
1385 weston_plane_release(&output->cursor_plane);
1386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001387 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001388
Matt Roper361d2ad2011-08-29 13:52:23 -07001389 free(output);
1390}
1391
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001392/**
1393 * Find the closest-matching mode for a given target
1394 *
1395 * Given a target mode, find the most suitable mode amongst the output's
1396 * current mode list to use, preferring the current mode if possible, to
1397 * avoid an expensive mode switch.
1398 *
1399 * @param output DRM output
1400 * @param target_mode Mode to attempt to match
1401 * @returns Pointer to a mode from the output's mode list
1402 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001403static struct drm_mode *
1404choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1405{
1406 struct drm_mode *tmp_mode = NULL, *mode;
1407
Hardeningff39efa2013-09-18 23:56:35 +02001408 if (output->base.current_mode->width == target_mode->width &&
1409 output->base.current_mode->height == target_mode->height &&
1410 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001411 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001412 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001413
1414 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1415 if (mode->mode_info.hdisplay == target_mode->width &&
1416 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001417 if (mode->base.refresh == target_mode->refresh ||
1418 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001419 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001420 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001421 tmp_mode = mode;
1422 }
1423 }
1424
1425 return tmp_mode;
1426}
1427
1428static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001429drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001430static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001431drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001432
1433static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001434drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1435{
1436 struct drm_output *output;
1437 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001438 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001439
1440 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001441 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001442 return -1;
1443 }
1444
1445 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001446 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001447 return -1;
1448 }
1449
Armin Krezović545dba62016-08-05 15:54:18 +02001450 b = to_drm_backend(output_base->compositor);
1451 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001452 drm_mode = choose_mode (output, mode);
1453
1454 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001455 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001456 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001457 }
1458
Hardeningff39efa2013-09-18 23:56:35 +02001459 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001460 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001461
Hardeningff39efa2013-09-18 23:56:35 +02001462 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001463
Hardeningff39efa2013-09-18 23:56:35 +02001464 output->base.current_mode = &drm_mode->base;
1465 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001466 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1467
Alex Wub7b8bda2012-04-17 17:20:48 +08001468 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001469 drm_output_release_fb(output, output->current);
1470 drm_output_release_fb(output, output->next);
1471 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001472
Giulio Camuffo954f1832014-10-11 18:27:30 +03001473 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001474 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001475 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001476 weston_log("failed to init output pixman state with "
1477 "new mode\n");
1478 return -1;
1479 }
1480 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001481 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001482 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001483
Giulio Camuffo954f1832014-10-11 18:27:30 +03001484 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001485 weston_log("failed to init output egl state with "
1486 "new mode");
1487 return -1;
1488 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001489 }
1490
Alex Wub7b8bda2012-04-17 17:20:48 +08001491 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001492}
1493
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001494static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001495on_drm_input(int fd, uint32_t mask, void *data)
1496{
1497 drmEventContext evctx;
1498
1499 memset(&evctx, 0, sizeof evctx);
1500 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1501 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001502 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001503 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001504
1505 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001506}
1507
1508static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001510{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001511 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001512 uint64_t cap;
1513 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001514 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001515
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001516 sysnum = udev_device_get_sysnum(device);
1517 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001518 b->drm.id = atoi(sysnum);
1519 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001520 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001521 return -1;
1522 }
1523
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001524 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001525 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001526 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001527 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001528 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001529 udev_device_get_devnode(device));
1530 return -1;
1531 }
1532
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001533 weston_log("using %s\n", filename);
1534
Giulio Camuffo954f1832014-10-11 18:27:30 +03001535 b->drm.fd = fd;
1536 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001537
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001538 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1539 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001540 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001541 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001542 clk_id = CLOCK_REALTIME;
1543
Giulio Camuffo954f1832014-10-11 18:27:30 +03001544 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001545 weston_log("Error: failed to set presentation clock %d.\n",
1546 clk_id);
1547 return -1;
1548 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001549
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001550 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1551 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001553 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001554 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001555
1556 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1557 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001558 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001559 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001560 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001561
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001562 return 0;
1563}
1564
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001565static struct gbm_device *
1566create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001567{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001568 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001569
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001570 gl_renderer = weston_load_module("gl-renderer.so",
1571 "gl_renderer_interface");
1572 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001573 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001574
1575 /* GBM will load a dri driver, but even though they need symbols from
1576 * libglapi, in some version of Mesa they are not linked to it. Since
1577 * only the gl-renderer module links to it, the call above won't make
1578 * these symbols globally available, and loading the DRI driver fails.
1579 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1580 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1581
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001582 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001583
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001584 return gbm;
1585}
1586
Bryce Harringtonc056a982015-05-19 15:25:18 -07001587/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001588 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001589 *
1590 * This returns 0 if substitution isn't possible, but 0 might be a
1591 * legitimate format for other EGL platforms, so the caller is
1592 * responsible for checking for 0 before calling gl_renderer->create().
1593 *
1594 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1595 * but it's entirely possible we'll see this again on other implementations.
1596 */
1597static int
1598fallback_format_for(uint32_t format)
1599{
1600 switch (format) {
1601 case GBM_FORMAT_XRGB8888:
1602 return GBM_FORMAT_ARGB8888;
1603 case GBM_FORMAT_XRGB2101010:
1604 return GBM_FORMAT_ARGB2101010;
1605 default:
1606 return 0;
1607 }
1608}
1609
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001610static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001611drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001612{
Derek Foreman6d556372015-11-04 14:47:33 -06001613 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001614 b->gbm_format,
1615 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001616 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001617 };
Derek Foreman6d556372015-11-04 14:47:33 -06001618 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001619
Derek Foremanc4cfe852015-05-15 12:12:40 -05001620 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001621 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001622 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001623 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001624 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001625 gl_renderer->opaque_attribs,
1626 format,
1627 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001628 return -1;
1629 }
1630
1631 return 0;
1632}
1633
1634static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001635init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001636{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001637 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001638
Giulio Camuffo954f1832014-10-11 18:27:30 +03001639 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001640 return -1;
1641
Giulio Camuffo954f1832014-10-11 18:27:30 +03001642 if (drm_backend_create_gl_renderer(b) < 0) {
1643 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001644 return -1;
1645 }
1646
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001647 return 0;
1648}
1649
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001650static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001651init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001652{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001653 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001654}
1655
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001656/**
1657 * Add a mode to output's mode list
1658 *
1659 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1660 * output's mode list.
1661 *
1662 * @param output DRM output to add mode to
1663 * @param info DRM mode structure to add
1664 * @returns Newly-allocated Weston/DRM mode structure
1665 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001666static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001667drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001668{
1669 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001670 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001671
1672 mode = malloc(sizeof *mode);
1673 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001674 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001675
1676 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001677 mode->base.width = info->hdisplay;
1678 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001679
1680 /* Calculate higher precision (mHz) refresh rate */
1681 refresh = (info->clock * 1000000LL / info->htotal +
1682 info->vtotal / 2) / info->vtotal;
1683
1684 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1685 refresh *= 2;
1686 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1687 refresh /= 2;
1688 if (info->vscan > 1)
1689 refresh /= info->vscan;
1690
1691 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001692 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001693
1694 if (info->type & DRM_MODE_TYPE_PREFERRED)
1695 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1696
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001697 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1698
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001699 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001700}
1701
1702static int
1703drm_subpixel_to_wayland(int drm_value)
1704{
1705 switch (drm_value) {
1706 default:
1707 case DRM_MODE_SUBPIXEL_UNKNOWN:
1708 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1709 case DRM_MODE_SUBPIXEL_NONE:
1710 return WL_OUTPUT_SUBPIXEL_NONE;
1711 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1712 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1713 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1714 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1715 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1716 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1717 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1718 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1719 }
1720}
1721
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001722/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001723static uint32_t
1724drm_get_backlight(struct drm_output *output)
1725{
1726 long brightness, max_brightness, norm;
1727
1728 brightness = backlight_get_brightness(output->backlight);
1729 max_brightness = backlight_get_max_brightness(output->backlight);
1730
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001731 /* convert it on a scale of 0 to 255 */
1732 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001733
1734 return (uint32_t) norm;
1735}
1736
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001737/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001738static void
1739drm_set_backlight(struct weston_output *output_base, uint32_t value)
1740{
Armin Krezović545dba62016-08-05 15:54:18 +02001741 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001742 long max_brightness, new_brightness;
1743
1744 if (!output->backlight)
1745 return;
1746
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001747 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001748 return;
1749
1750 max_brightness = backlight_get_max_brightness(output->backlight);
1751
1752 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001753 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001754
1755 backlight_set_brightness(output->backlight, new_brightness);
1756}
1757
1758static drmModePropertyPtr
1759drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1760{
1761 drmModePropertyPtr props;
1762 int i;
1763
1764 for (i = 0; i < connector->count_props; i++) {
1765 props = drmModeGetProperty(fd, connector->props[i]);
1766 if (!props)
1767 continue;
1768
1769 if (!strcmp(props->name, name))
1770 return props;
1771
1772 drmModeFreeProperty(props);
1773 }
1774
1775 return NULL;
1776}
1777
1778static void
1779drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1780{
Armin Krezović545dba62016-08-05 15:54:18 +02001781 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001782 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001783 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001784 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001785
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001786 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001787 return;
1788
Daniel Stone36609c72015-06-18 07:49:02 +01001789 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1790 output->dpms_prop->prop_id, level);
1791 if (ret) {
1792 weston_log("DRM: DPMS: failed property set for %s\n",
1793 output->base.name);
1794 return;
1795 }
1796
1797 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001798}
1799
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001800static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001801 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1802 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1803 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1804 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1805 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1806 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1807 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1808 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1809 [DRM_MODE_CONNECTOR_Component] = "Component",
1810 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1811 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1812 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1813 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1814 [DRM_MODE_CONNECTOR_TV] = "TV",
1815 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001816#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001817 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1818 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001819#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001820};
1821
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001822static char *
1823make_connector_name(const drmModeConnector *con)
1824{
1825 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001826 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001827
1828 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1829 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001830
1831 if (!type_name)
1832 type_name = "UNNAMED";
1833
1834 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001835
1836 return strdup(name);
1837}
1838
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001839static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001840find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001841 drmModeRes *resources, drmModeConnector *connector)
1842{
1843 drmModeEncoder *encoder;
1844 uint32_t possible_crtcs;
1845 int i, j;
1846
1847 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001848 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001849 if (encoder == NULL) {
1850 weston_log("Failed to get encoder.\n");
1851 return -1;
1852 }
1853 possible_crtcs = encoder->possible_crtcs;
1854 drmModeFreeEncoder(encoder);
1855
1856 for (i = 0; i < resources->count_crtcs; i++) {
1857 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001858 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001859 return i;
1860 }
1861 }
1862
1863 return -1;
1864}
1865
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001866/* Init output state that depends on gl or gbm */
1867static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001868drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001869{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001870 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001871 output->gbm_format,
1872 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001873 };
1874 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001875
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001876 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001877 output->base.current_mode->width,
1878 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001879 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001880 GBM_BO_USE_SCANOUT |
1881 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001882 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001883 weston_log("failed to create gbm surface\n");
1884 return -1;
1885 }
1886
Derek Foremanc4cfe852015-05-15 12:12:40 -05001887 if (format[1])
1888 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001889 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001890 (EGLNativeWindowType)output->gbm_surface,
1891 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001892 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001893 format,
1894 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001895 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001896 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001897 return -1;
1898 }
1899
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001900 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001901
1902 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001903 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001904 continue;
1905
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001906 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001907 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001908 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001909 }
1910
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001911 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001912 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001913 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001914 }
1915
1916 return 0;
1917}
1918
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001919static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001920drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001921{
Hardeningff39efa2013-09-18 23:56:35 +02001922 int w = output->base.current_mode->width;
1923 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001924 uint32_t format = output->gbm_format;
1925 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001926 unsigned int i;
1927
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001928 switch (format) {
1929 case GBM_FORMAT_XRGB8888:
1930 pixman_format = PIXMAN_x8r8g8b8;
1931 break;
1932 case GBM_FORMAT_RGB565:
1933 pixman_format = PIXMAN_r5g6b5;
1934 break;
1935 default:
1936 weston_log("Unsupported pixman format 0x%x\n", format);
1937 return -1;
1938 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001939
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001940 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001941 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001942 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001943 if (!output->dumb[i])
1944 goto err;
1945
1946 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001947 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001948 output->dumb[i]->map,
1949 output->dumb[i]->stride);
1950 if (!output->image[i])
1951 goto err;
1952 }
1953
1954 if (pixman_renderer_output_create(&output->base) < 0)
1955 goto err;
1956
1957 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001958 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001959
1960 return 0;
1961
1962err:
1963 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1964 if (output->dumb[i])
1965 drm_fb_destroy_dumb(output->dumb[i]);
1966 if (output->image[i])
1967 pixman_image_unref(output->image[i]);
1968
1969 output->dumb[i] = NULL;
1970 output->image[i] = NULL;
1971 }
1972
1973 return -1;
1974}
1975
1976static void
1977drm_output_fini_pixman(struct drm_output *output)
1978{
1979 unsigned int i;
1980
1981 pixman_renderer_output_destroy(&output->base);
1982 pixman_region32_fini(&output->previous_damage);
1983
1984 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1985 drm_fb_destroy_dumb(output->dumb[i]);
1986 pixman_image_unref(output->image[i]);
1987 output->dumb[i] = NULL;
1988 output->image[i] = NULL;
1989 }
1990}
1991
Richard Hughes2b2092a2013-04-24 14:58:02 +01001992static void
1993edid_parse_string(const uint8_t *data, char text[])
1994{
1995 int i;
1996 int replaced = 0;
1997
1998 /* this is always 12 bytes, but we can't guarantee it's null
1999 * terminated or not junk. */
2000 strncpy(text, (const char *) data, 12);
2001
Bryce Harrington9c7de162015-08-28 13:04:26 -07002002 /* guarantee our new string is null-terminated */
2003 text[12] = '\0';
2004
Richard Hughes2b2092a2013-04-24 14:58:02 +01002005 /* remove insane chars */
2006 for (i = 0; text[i] != '\0'; i++) {
2007 if (text[i] == '\n' ||
2008 text[i] == '\r') {
2009 text[i] = '\0';
2010 break;
2011 }
2012 }
2013
2014 /* ensure string is printable */
2015 for (i = 0; text[i] != '\0'; i++) {
2016 if (!isprint(text[i])) {
2017 text[i] = '-';
2018 replaced++;
2019 }
2020 }
2021
2022 /* if the string is random junk, ignore the string */
2023 if (replaced > 4)
2024 text[0] = '\0';
2025}
2026
2027#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2028#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2029#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2030#define EDID_OFFSET_DATA_BLOCKS 0x36
2031#define EDID_OFFSET_LAST_BLOCK 0x6c
2032#define EDID_OFFSET_PNPID 0x08
2033#define EDID_OFFSET_SERIAL 0x0c
2034
2035static int
2036edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2037{
2038 int i;
2039 uint32_t serial_number;
2040
2041 /* check header */
2042 if (length < 128)
2043 return -1;
2044 if (data[0] != 0x00 || data[1] != 0xff)
2045 return -1;
2046
2047 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2048 * /--08--\/--09--\
2049 * 7654321076543210
2050 * |\---/\---/\---/
2051 * R C1 C2 C3 */
2052 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2053 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2054 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2055 edid->pnp_id[3] = '\0';
2056
2057 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2058 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2059 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2060 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2061 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2062 if (serial_number > 0)
2063 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2064
2065 /* parse EDID data */
2066 for (i = EDID_OFFSET_DATA_BLOCKS;
2067 i <= EDID_OFFSET_LAST_BLOCK;
2068 i += 18) {
2069 /* ignore pixel clock data */
2070 if (data[i] != 0)
2071 continue;
2072 if (data[i+2] != 0)
2073 continue;
2074
2075 /* any useful blocks? */
2076 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2077 edid_parse_string(&data[i+5],
2078 edid->monitor_name);
2079 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2080 edid_parse_string(&data[i+5],
2081 edid->serial_number);
2082 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2083 edid_parse_string(&data[i+5],
2084 edid->eisa_id);
2085 }
2086 }
2087 return 0;
2088}
2089
2090static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002091find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002092 struct drm_output *output,
2093 drmModeConnector *connector)
2094{
2095 drmModePropertyBlobPtr edid_blob = NULL;
2096 drmModePropertyPtr property;
2097 int i;
2098 int rc;
2099
2100 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002101 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002102 if (!property)
2103 continue;
2104 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2105 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002106 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002107 connector->prop_values[i]);
2108 }
2109 drmModeFreeProperty(property);
2110 }
2111 if (!edid_blob)
2112 return;
2113
2114 rc = edid_parse(&output->edid,
2115 edid_blob->data,
2116 edid_blob->length);
2117 if (!rc) {
2118 weston_log("EDID data '%s', '%s', '%s'\n",
2119 output->edid.pnp_id,
2120 output->edid.monitor_name,
2121 output->edid.serial_number);
2122 if (output->edid.pnp_id[0] != '\0')
2123 output->base.make = output->edid.pnp_id;
2124 if (output->edid.monitor_name[0] != '\0')
2125 output->base.model = output->edid.monitor_name;
2126 if (output->edid.serial_number[0] != '\0')
2127 output->base.serial_number = output->edid.serial_number;
2128 }
2129 drmModeFreePropertyBlob(edid_blob);
2130}
2131
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002132
2133
2134static int
2135parse_modeline(const char *s, drmModeModeInfo *mode)
2136{
2137 char hsync[16];
2138 char vsync[16];
2139 float fclock;
2140
2141 mode->type = DRM_MODE_TYPE_USERDEF;
2142 mode->hskew = 0;
2143 mode->vscan = 0;
2144 mode->vrefresh = 0;
2145 mode->flags = 0;
2146
Rob Bradford307e09e2013-07-26 16:29:40 +01002147 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002148 &fclock,
2149 &mode->hdisplay,
2150 &mode->hsync_start,
2151 &mode->hsync_end,
2152 &mode->htotal,
2153 &mode->vdisplay,
2154 &mode->vsync_start,
2155 &mode->vsync_end,
2156 &mode->vtotal, hsync, vsync) != 11)
2157 return -1;
2158
2159 mode->clock = fclock * 1000;
2160 if (strcmp(hsync, "+hsync") == 0)
2161 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2162 else if (strcmp(hsync, "-hsync") == 0)
2163 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2164 else
2165 return -1;
2166
2167 if (strcmp(vsync, "+vsync") == 0)
2168 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2169 else if (strcmp(vsync, "-vsync") == 0)
2170 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2171 else
2172 return -1;
2173
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002174 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2175 mode->hdisplay, mode->vdisplay, fclock);
2176
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002177 return 0;
2178}
2179
Rob Bradford66bd9f52013-06-25 18:56:42 +01002180static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002181setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002182 struct weston_output *output,
2183 const char *s)
2184{
2185 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002186 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002187 struct udev_seat *seat;
2188
Giulio Camuffo954f1832014-10-11 18:27:30 +03002189 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002190 if (!seat)
2191 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002192
Derek Foreman0720ea32015-07-15 13:00:35 -05002193 seat->base.output = output;
2194
Derek Foreman1281a362015-07-31 16:55:32 -05002195 pointer = weston_seat_get_pointer(&seat->base);
2196 if (pointer)
2197 weston_pointer_clamp(pointer,
2198 &pointer->x,
2199 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002200 }
2201}
2202
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002203static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002204parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002205{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002206 int ret = 0;
2207
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002208 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002209 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002210 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002211 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002212 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002213 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002214 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002215 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002216 else {
2217 weston_log("fatal: unrecognized pixel format: %s\n", s);
2218 ret = -1;
2219 }
2220
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002221 return ret;
2222}
2223
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002224/**
2225 * Choose suitable mode for an output
2226 *
2227 * Find the most suitable mode to use for initial setup (or reconfiguration on
2228 * hotplug etc) for a DRM output.
2229 *
2230 * @param output DRM output to choose mode for
2231 * @param kind Strategy and preference to use when choosing mode
2232 * @param width Desired width for this output
2233 * @param height Desired height for this output
2234 * @param current_mode Mode currently being displayed on this output
2235 * @param modeline Manually-entered mode (may be NULL)
2236 * @returns A mode from the output's mode list, or NULL if none available
2237 */
2238static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002239drm_output_choose_initial_mode(struct drm_backend *backend,
2240 struct drm_output *output,
2241 enum weston_drm_backend_output_mode mode,
2242 struct weston_drm_backend_output_config *config,
2243 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002244{
2245 struct drm_mode *preferred = NULL;
2246 struct drm_mode *current = NULL;
2247 struct drm_mode *configured = NULL;
2248 struct drm_mode *best = NULL;
2249 struct drm_mode *drm_mode;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002250 drmModeModeInfo modeline;
2251 int32_t width = 0;
2252 int32_t height = 0;
2253
2254 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
2255 if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
2256 width = -1;
2257
2258 if (parse_modeline(config->modeline, &modeline) == 0) {
2259 configured = drm_output_add_mode(output, &modeline);
2260 if (!configured)
2261 return NULL;
2262 } else {
2263 weston_log("Invalid modeline \"%s\" for output %s\n",
2264 config->modeline, output->base.name);
2265 }
2266 }
2267 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002268
2269 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002270 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002271 height == drm_mode->base.height)
2272 configured = drm_mode;
2273
comic fans7a5c5622016-03-17 14:29:27 +02002274 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002275 sizeof *current_mode) == 0)
2276 current = drm_mode;
2277
2278 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2279 preferred = drm_mode;
2280
2281 best = drm_mode;
2282 }
2283
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002284 if (current == NULL && current_mode->clock != 0) {
2285 current = drm_output_add_mode(output, current_mode);
2286 if (!current)
2287 return NULL;
2288 }
2289
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002290 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002291 configured = current;
2292
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002293 if (configured)
2294 return configured;
2295
2296 if (preferred)
2297 return preferred;
2298
2299 if (current)
2300 return current;
2301
2302 if (best)
2303 return best;
2304
2305 weston_log("no available modes for %s\n", output->base.name);
2306 return NULL;
2307}
2308
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002309static int
2310connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2311 drmModeModeInfo *mode)
2312{
2313 drmModeEncoder *encoder;
2314 drmModeCrtc *crtc;
2315
2316 /* Get the current mode on the crtc that's currently driving
2317 * this connector. */
2318 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2319 memset(mode, 0, sizeof *mode);
2320 if (encoder != NULL) {
2321 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2322 drmModeFreeEncoder(encoder);
2323 if (crtc == NULL)
2324 return -1;
2325 if (crtc->mode_valid)
2326 *mode = crtc->mode;
2327 drmModeFreeCrtc(crtc);
2328 }
2329
2330 return 0;
2331}
2332
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002333/**
2334 * Create and configure a Weston output structure
2335 *
2336 * Given a DRM connector, create a matching drm_output structure and add it
2337 * to Weston's output list.
2338 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002339 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002340 * @param resources DRM resources for this device
2341 * @param connector DRM connector to use for this new output
2342 * @param x Horizontal offset to use into global co-ordinate space
2343 * @param y Vertical offset to use into global co-ordinate space
2344 * @param drm_device udev device pointer
2345 * @returns 0 on success, or -1 on failure
2346 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002347static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002348create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002349 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002350 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002351 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002352{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002353 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002354 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002355 struct weston_mode *m;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002356
2357 drmModeModeInfo crtc_mode;
2358 int i;
2359 enum weston_drm_backend_output_mode mode;
2360 struct weston_drm_backend_output_config config = {{ 0 }};
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002361
Giulio Camuffo954f1832014-10-11 18:27:30 +03002362 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002363 if (i < 0) {
2364 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002365 return -1;
2366 }
2367
Peter Huttererf3d62272013-08-08 11:57:05 +10002368 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002369 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002370 return -1;
2371
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002372 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002373 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002374 output->base.make = "unknown";
2375 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002376 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002377 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002378
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002379 mode = b->configure_output(b->compositor, b->use_current_mode,
2380 output->base.name, &config);
2381 if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002382 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002383
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002384 setup_output_seat_constraint(b, &output->base,
2385 config.seat ? config.seat : "");
2386 free(config.seat);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002387
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002388 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002389 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002391 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002392 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002393
Giulio Camuffo954f1832014-10-11 18:27:30 +03002394 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2395 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002396
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002397 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2398 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002399
David Herrmann0f0d54e2011-12-08 17:05:45 +01002400 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002401 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002402 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002403 goto err_free;
2404 }
2405
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002406 if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002407 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002408 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002409 0, 0, 0, 0, 0, NULL);
2410 goto err_free;
2411 }
2412
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002413 current = drm_output_choose_initial_mode(b, output, mode, &config,
2414 &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002415 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002416 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002417 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002418 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002419
Giulio Camuffo954f1832014-10-11 18:27:30 +03002420 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002421 connector->mmWidth, connector->mmHeight,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002422 config.base.transform, config.base.scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002423
Giulio Camuffo954f1832014-10-11 18:27:30 +03002424 if (b->use_pixman) {
2425 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002426 weston_log("Failed to init output pixman state\n");
2427 goto err_output;
2428 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002429 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002430 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002431 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002432 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002433
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002434 output->backlight = backlight_init(drm_device,
2435 connector->connector_type);
2436 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002437 weston_log("Initialized backlight, device %s\n",
2438 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002439 output->base.set_backlight = drm_set_backlight;
2440 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002441 } else {
2442 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002443 }
2444
Giulio Camuffo954f1832014-10-11 18:27:30 +03002445 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002446
Giulio Camuffo954f1832014-10-11 18:27:30 +03002447 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002448 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2449 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002450
Jonas Ådahle5a12252013-04-05 23:07:11 +02002451 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002452 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002453 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002454 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002455 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002456 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002457
Richard Hughese7299962013-05-01 21:52:12 +01002458 output->base.gamma_size = output->original_crtc->gamma_size;
2459 output->base.set_gamma = drm_output_set_gamma;
2460
Derek Foremanb0427562016-02-05 15:55:20 -06002461 weston_plane_init(&output->cursor_plane, b->compositor,
2462 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002463 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002464
Giulio Camuffo954f1832014-10-11 18:27:30 +03002465 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2466 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2467 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002468
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002469 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002470 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002471 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002472 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002473 m->width, m->height, m->refresh / 1000.0,
2474 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2475 ", preferred" : "",
2476 m->flags & WL_OUTPUT_MODE_CURRENT ?
2477 ", current" : "",
2478 connector->count_modes == 0 ?
2479 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002480
Mario Kleiner80817042015-06-21 21:25:11 +02002481 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2482 output->base.native_mode = output->base.current_mode;
2483 output->base.native_scale = output->base.current_scale;
2484
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002485 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002486
John Kåre Alsaker94659272012-11-13 19:10:18 +01002487err_output:
2488 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002489err_free:
2490 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2491 base.link) {
2492 wl_list_remove(&drm_mode->base.link);
2493 free(drm_mode);
2494 }
2495
2496 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002497 b->crtc_allocator &= ~(1 << output->crtc_id);
2498 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002499 free(output);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002500 free(config.modeline);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002501
David Herrmann0f0d54e2011-12-08 17:05:45 +01002502 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002503}
2504
Jesse Barnes58ef3792012-02-23 09:45:49 -05002505static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002506create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002507{
2508 struct drm_sprite *sprite;
2509 drmModePlaneRes *plane_res;
2510 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002511 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002512
Giulio Camuffo954f1832014-10-11 18:27:30 +03002513 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002514 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002515 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002516 strerror(errno));
2517 return;
2518 }
2519
2520 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002521 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002522 if (!plane)
2523 continue;
2524
Peter Huttererf3d62272013-08-08 11:57:05 +10002525 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002526 plane->count_formats));
2527 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002528 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002529 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002530 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002531 continue;
2532 }
2533
Jesse Barnes58ef3792012-02-23 09:45:49 -05002534 sprite->possible_crtcs = plane->possible_crtcs;
2535 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002536 sprite->current = NULL;
2537 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002538 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002539 sprite->count_formats = plane->count_formats;
2540 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002541 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002542 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002543 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2544 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2545 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002546
Giulio Camuffo954f1832014-10-11 18:27:30 +03002547 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002548 }
2549
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002550 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002551}
2552
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002553static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002554destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002555{
2556 struct drm_sprite *sprite, *next;
2557 struct drm_output *output;
2558
Giulio Camuffo954f1832014-10-11 18:27:30 +03002559 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002560 struct drm_output, base.link);
2561
Giulio Camuffo954f1832014-10-11 18:27:30 +03002562 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2563 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002564 sprite->plane_id,
2565 output->crtc_id, 0, 0,
2566 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002567 drm_output_release_fb(output, sprite->current);
2568 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002569 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002570 free(sprite);
2571 }
2572}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002573
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002574static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002575create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002576 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002577{
2578 drmModeConnector *connector;
2579 drmModeRes *resources;
2580 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002581 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002582
Giulio Camuffo954f1832014-10-11 18:27:30 +03002583 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002584 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002585 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002586 return -1;
2587 }
2588
Giulio Camuffo954f1832014-10-11 18:27:30 +03002589 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2590 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002591 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002592 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002593 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002594
Giulio Camuffo954f1832014-10-11 18:27:30 +03002595 b->min_width = resources->min_width;
2596 b->max_width = resources->max_width;
2597 b->min_height = resources->min_height;
2598 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002599
Giulio Camuffo954f1832014-10-11 18:27:30 +03002600 b->num_crtcs = resources->count_crtcs;
2601 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002602
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002603 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002604 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002605 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002606 if (connector == NULL)
2607 continue;
2608
2609 if (connector->connection == DRM_MODE_CONNECTED &&
2610 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002611 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002612 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002613 connector, x, y,
2614 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002615 drmModeFreeConnector(connector);
2616 continue;
2617 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002618
Giulio Camuffo954f1832014-10-11 18:27:30 +03002619 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002620 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002621 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002622 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002623
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002624 drmModeFreeConnector(connector);
2625 }
2626
Armin Krezovićb12a7542016-07-29 13:26:22 +02002627 if (wl_list_empty(&b->compositor->output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002628 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002629
2630 drmModeFreeResources(resources);
2631
2632 return 0;
2633}
2634
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002635static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002636update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002637{
2638 drmModeConnector *connector;
2639 drmModeRes *resources;
2640 struct drm_output *output, *next;
2641 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002642 uint32_t connected = 0, disconnects = 0;
2643 int i;
2644
Giulio Camuffo954f1832014-10-11 18:27:30 +03002645 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002646 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002647 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002648 return;
2649 }
2650
2651 /* collect new connects */
2652 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002653 int connector_id = resources->connectors[i];
2654
Giulio Camuffo954f1832014-10-11 18:27:30 +03002655 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002656 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002657 continue;
2658
David Herrmann7551cff2011-12-08 17:05:43 +01002659 if (connector->connection != DRM_MODE_CONNECTED) {
2660 drmModeFreeConnector(connector);
2661 continue;
2662 }
2663
Benjamin Franzke117483d2011-08-30 11:38:26 +02002664 connected |= (1 << connector_id);
2665
Giulio Camuffo954f1832014-10-11 18:27:30 +03002666 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002667 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002669 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002670
2671 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002672 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002673 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002674 else
2675 x = 0;
2676 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002677 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002678 connector, x, y,
2679 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002680 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002681
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002682 }
2683 drmModeFreeConnector(connector);
2684 }
2685 drmModeFreeResources(resources);
2686
Giulio Camuffo954f1832014-10-11 18:27:30 +03002687 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002689 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002690 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002691 if (disconnects & (1 << output->connector_id)) {
2692 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002693 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002694 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002695 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002696 }
2697 }
2698 }
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{
Armin Krezović545dba62016-08-05 15:54:18 +02002743 struct drm_backend *b = to_drm_backend(ec);
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;
Armin Krezović545dba62016-08-05 15:54:18 +02002799 struct drm_backend *b = to_drm_backend(compositor);
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);
Armin Krezović545dba62016-08-05 15:54:18 +02002939 b = to_drm_backend(output->base.compositor);
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 =
Armin Krezović545dba62016-08-05 15:54:18 +02003076 to_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}