blob: 34922af170d572eee1d22b4f3c933fe4e0c692e6 [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
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400227static void
228drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400229
Mario Kleinerf507ec32015-06-21 21:25:14 +0200230static void
231drm_output_update_msc(struct drm_output *output, unsigned int seq);
232
Jesse Barnes58ef3792012-02-23 09:45:49 -0500233static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200234drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500235{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200236 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300237 struct drm_backend *b =(struct drm_backend *)ec->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500238 int crtc;
239
Giulio Camuffo954f1832014-10-11 18:27:30 +0300240 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
241 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500242 continue;
243
244 if (supported & (1 << crtc))
245 return -1;
246 }
247
248 return 0;
249}
250
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300251static void
252drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
253{
254 struct drm_fb *fb = data;
255 struct gbm_device *gbm = gbm_bo_get_device(bo);
256
257 if (fb->fb_id)
258 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
259
Pekka Paalanende685b82012-12-04 15:58:12 +0200260 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300261
262 free(data);
263}
264
265static struct drm_fb *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300266drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
267 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200268{
269 struct drm_fb *fb;
270 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300271 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200272
273 struct drm_mode_create_dumb create_arg;
274 struct drm_mode_destroy_dumb destroy_arg;
275 struct drm_mode_map_dumb map_arg;
276
Peter Huttererf3d62272013-08-08 11:57:05 +1000277 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200278 if (!fb)
279 return NULL;
280
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300281 switch (format) {
282 case GBM_FORMAT_XRGB8888:
283 bpp = 32;
284 depth = 24;
285 break;
286 case GBM_FORMAT_RGB565:
287 bpp = depth = 16;
288 break;
289 default:
290 return NULL;
291 }
292
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700293 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300294 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200295 create_arg.width = width;
296 create_arg.height = height;
297
Giulio Camuffo954f1832014-10-11 18:27:30 +0300298 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299 if (ret)
300 goto err_fb;
301
302 fb->handle = create_arg.handle;
303 fb->stride = create_arg.pitch;
304 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300305 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200306
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300307 ret = -1;
308
309 if (!b->no_addfb2) {
310 uint32_t handles[4], pitches[4], offsets[4];
311
312 handles[0] = fb->handle;
313 pitches[0] = fb->stride;
314 offsets[0] = 0;
315
316 ret = drmModeAddFB2(b->drm.fd, width, height,
317 format, handles, pitches, offsets,
318 &fb->fb_id, 0);
319 if (ret) {
320 weston_log("addfb2 failed: %m\n");
321 b->no_addfb2 = 1;
322 }
323 }
324
325 if (ret) {
326 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
327 fb->stride, fb->handle, &fb->fb_id);
328 }
329
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200330 if (ret)
331 goto err_bo;
332
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700333 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200334 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400335 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200336 if (ret)
337 goto err_add_fb;
338
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500339 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300340 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200341 if (fb->map == MAP_FAILED)
342 goto err_add_fb;
343
344 return fb;
345
346err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300347 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200348err_bo:
349 memset(&destroy_arg, 0, sizeof(destroy_arg));
350 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300351 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200352err_fb:
353 free(fb);
354 return NULL;
355}
356
357static void
358drm_fb_destroy_dumb(struct drm_fb *fb)
359{
360 struct drm_mode_destroy_dumb destroy_arg;
361
362 if (!fb->map)
363 return;
364
365 if (fb->fb_id)
366 drmModeRmFB(fb->fd, fb->fb_id);
367
368 weston_buffer_reference(&fb->buffer_ref, NULL);
369
370 munmap(fb->map, fb->size);
371
372 memset(&destroy_arg, 0, sizeof(destroy_arg));
373 destroy_arg.handle = fb->handle;
374 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
375
376 free(fb);
377}
378
379static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500380drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300381 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300382{
383 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200384 uint32_t width, height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200385 uint32_t handles[4], pitches[4], offsets[4];
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300386 int ret;
387
388 if (fb)
389 return fb;
390
Bryce Harringtonde16d892014-11-20 22:21:57 -0800391 fb = zalloc(sizeof *fb);
392 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200393 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300394
395 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300396
397 width = gbm_bo_get_width(bo);
398 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200399 fb->stride = gbm_bo_get_stride(bo);
400 fb->handle = gbm_bo_get_handle(bo).u32;
401 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300402 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300403
Giulio Camuffo954f1832014-10-11 18:27:30 +0300404 if (backend->min_width > width || width > backend->max_width ||
405 backend->min_height > height ||
406 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200407 weston_log("bo geometry out of bounds\n");
408 goto err_free;
409 }
410
411 ret = -1;
412
Giulio Camuffo954f1832014-10-11 18:27:30 +0300413 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200414 handles[0] = fb->handle;
415 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200416 offsets[0] = 0;
417
Giulio Camuffo954f1832014-10-11 18:27:30 +0300418 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200419 format, handles, pitches, offsets,
420 &fb->fb_id, 0);
421 if (ret) {
422 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300423 backend->no_addfb2 = 1;
424 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200425 }
426 }
427
428 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300429 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200430 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200431
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300432 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200433 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200434 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300435 }
436
437 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
438
439 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200440
441err_free:
442 free(fb);
443 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300444}
445
446static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500447drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200448{
Pekka Paalanende685b82012-12-04 15:58:12 +0200449 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200450
451 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200452
Pekka Paalanende685b82012-12-04 15:58:12 +0200453 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200454}
455
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200456static void
457drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
458{
459 if (!fb)
460 return;
461
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200462 if (fb->map &&
463 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200464 drm_fb_destroy_dumb(fb);
465 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200466 if (fb->is_client_buffer)
467 gbm_bo_destroy(fb->bo);
468 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100469 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600470 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200471 }
472}
473
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500474static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200475drm_output_check_scanout_format(struct drm_output *output,
476 struct weston_surface *es, struct gbm_bo *bo)
477{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200478 uint32_t format;
479 pixman_region32_t r;
480
481 format = gbm_bo_get_format(bo);
482
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700483 if (format == GBM_FORMAT_ARGB8888) {
484 /* We can scanout an ARGB buffer if the surface's
485 * opaque region covers the whole output, but we have
486 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800487 pixman_region32_init_rect(&r, 0, 0,
488 output->base.width,
489 output->base.height);
490 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200491
492 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500493 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200494
495 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500496 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700497
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100498 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700499 return format;
500
501 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200502}
503
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400504static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200505drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500506 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500507{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300508 struct drm_backend *b =
509 (struct drm_backend *)output->base.compositor->backend;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200511 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300512 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500513 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500514
Jason Ekstranda7af7042013-10-12 22:38:11 -0500515 if (ev->geometry.x != output->base.x ||
516 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300517 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200518 buffer->width != output->base.current_mode->width ||
519 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200520 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500521 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400522 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500523
Pekka Paalanen5580f222015-02-17 16:33:18 +0200524 if (ev->geometry.scissor_enabled)
525 return NULL;
526
Giulio Camuffo954f1832014-10-11 18:27:30 +0300527 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700528 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500529
Rob Bradford9b101872012-09-14 23:25:41 +0100530 /* Unable to use the buffer for scanout */
531 if (!bo)
532 return NULL;
533
Jason Ekstranda7af7042013-10-12 22:38:11 -0500534 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500535 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300536 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400537 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300538 }
539
Giulio Camuffo954f1832014-10-11 18:27:30 +0300540 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300541 if (!output->next) {
542 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400543 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300544 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500545
Pekka Paalanende685b82012-12-04 15:58:12 +0200546 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500547
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400548 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500549}
550
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500551static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200552drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400553{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300554 struct drm_backend *b =
555 (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300556 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400557
Giulio Camuffo954f1832014-10-11 18:27:30 +0300558 output->base.compositor->renderer->repaint_output(&output->base,
559 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400560
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100561 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300562 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200563 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400564 return;
565 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300566
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100567 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300568 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200569 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100570 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300571 return;
572 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400573}
574
575static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200576drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
577{
578 struct weston_compositor *ec = output->base.compositor;
579 pixman_region32_t total_damage, previous_damage;
580
581 pixman_region32_init(&total_damage);
582 pixman_region32_init(&previous_damage);
583
584 pixman_region32_copy(&previous_damage, damage);
585
586 pixman_region32_union(&total_damage, damage, &output->previous_damage);
587 pixman_region32_copy(&output->previous_damage, &previous_damage);
588
589 output->current_image ^= 1;
590
591 output->next = output->dumb[output->current_image];
592 pixman_renderer_output_set_buffer(&output->base,
593 output->image[output->current_image]);
594
595 ec->renderer->repaint_output(&output->base, &total_damage);
596
597 pixman_region32_fini(&total_damage);
598 pixman_region32_fini(&previous_damage);
599}
600
601static void
602drm_output_render(struct drm_output *output, pixman_region32_t *damage)
603{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300604 struct weston_compositor *c = output->base.compositor;
605 struct drm_backend *b = (struct drm_backend *)c->backend;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200606
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200608 drm_output_render_pixman(output, damage);
609 else
610 drm_output_render_gl(output, damage);
611
Giulio Camuffo954f1832014-10-11 18:27:30 +0300612 pixman_region32_subtract(&c->primary_plane.damage,
613 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200614}
615
616static void
Richard Hughese7299962013-05-01 21:52:12 +0100617drm_output_set_gamma(struct weston_output *output_base,
618 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
619{
620 int rc;
621 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300622 struct drm_backend *backend =
623 (struct drm_backend *) output->base.compositor->backend;
Richard Hughese7299962013-05-01 21:52:12 +0100624
625 /* check */
626 if (output_base->gamma_size != size)
627 return;
628 if (!output->original_crtc)
629 return;
630
Giulio Camuffo954f1832014-10-11 18:27:30 +0300631 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100632 output->crtc_id,
633 size, r, g, b);
634 if (rc)
635 weston_log("set gamma failed: %m\n");
636}
637
Bryce Harringtonada4f072015-06-30 13:25:46 -0700638/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200639 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700640 * The pipe parameter indicates which CRTC is in use. Knowing this, we
641 * can determine which vblank sequence type to use for it. Traditional
642 * cards had only two CRTCs, with CRTC 0 using no special flags, and
643 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
644 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200645 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700646 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
647 * 0-31. If this is non-zero it indicates we're dealing with a
648 * multi-gpu situation and we need to calculate the vblank sync
649 * using DRM_BLANK_HIGH_CRTC_MASK.
650 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300651static unsigned int
652drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200653{
654 if (output->pipe > 1)
655 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
656 DRM_VBLANK_HIGH_CRTC_MASK;
657 else if (output->pipe > 0)
658 return DRM_VBLANK_SECONDARY;
659 else
660 return 0;
661}
662
David Herrmann1edf44c2013-10-22 17:11:26 +0200663static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500664drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400665 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100666{
667 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300668 struct drm_backend *backend =
669 (struct drm_backend *)output->base.compositor->backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500670 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400671 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500672 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100673
Xiong Zhangabd5d472013-10-11 14:43:07 +0800674 if (output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200675 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800676
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300677 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400678 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300679 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200680 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100681
Hardeningff39efa2013-09-18 23:56:35 +0200682 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200683 if (!output->current ||
684 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300685 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300686 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400687 &output->connector_id, 1,
688 &mode->mode_info);
689 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200690 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200691 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400692 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300693 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200694 }
695
Giulio Camuffo954f1832014-10-11 18:27:30 +0300696 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300697 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500698 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200699 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200700 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500701 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100702
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300703 output->page_flip_pending = 1;
704
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400705 drm_output_set_cursor(output);
706
Jesse Barnes58ef3792012-02-23 09:45:49 -0500707 /*
708 * Now, update all the sprite surfaces
709 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300710 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200711 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500712 drmVBlank vbl = {
713 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
714 .request.sequence = 1,
715 };
716
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200717 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200718 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500719 continue;
720
Giulio Camuffo954f1832014-10-11 18:27:30 +0300721 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200722 fb_id = s->next->fb_id;
723
Giulio Camuffo954f1832014-10-11 18:27:30 +0300724 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200725 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500726 s->dest_x, s->dest_y,
727 s->dest_w, s->dest_h,
728 s->src_x, s->src_y,
729 s->src_w, s->src_h);
730 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200731 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500732 ret, strerror(errno));
733
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200734 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500735
Jesse Barnes58ef3792012-02-23 09:45:49 -0500736 /*
737 * Queue a vblank signal so we know when the surface
738 * becomes active on the display or has been replaced.
739 */
740 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300741 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500742 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200743 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500744 ret, strerror(errno));
745 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300746
747 s->output = output;
748 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500749 }
750
David Herrmann1edf44c2013-10-22 17:11:26 +0200751 return 0;
752
753err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800754 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200755 if (output->next) {
756 drm_output_release_fb(output, output->next);
757 output->next = NULL;
758 }
759
760 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400761}
762
763static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200764drm_output_start_repaint_loop(struct weston_output *output_base)
765{
766 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300767 struct drm_backend *backend = (struct drm_backend *)
768 output_base->compositor->backend;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200769 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200770 struct timespec ts, tnow;
771 struct timespec vbl2now;
772 int64_t refresh_nsec;
773 int ret;
774 drmVBlank vbl = {
775 .request.type = DRM_VBLANK_RELATIVE,
776 .request.sequence = 0,
777 .request.signal = 0,
778 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300779
Xiong Zhangabd5d472013-10-11 14:43:07 +0800780 if (output->destroy_pending)
781 return;
782
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300783 if (!output->current) {
784 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200785 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300786 }
787
Mario Kleinerf507ec32015-06-21 21:25:14 +0200788 /* Try to get current msc and timestamp via instant query */
789 vbl.request.type |= drm_waitvblank_pipe(output);
790 ret = drmWaitVBlank(backend->drm.fd, &vbl);
791
792 /* Error ret or zero timestamp means failure to get valid timestamp */
793 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
794 ts.tv_sec = vbl.reply.tval_sec;
795 ts.tv_nsec = vbl.reply.tval_usec * 1000;
796
797 /* Valid timestamp for most recent vblank - not stale?
798 * Stale ts could happen on Linux 3.17+, so make sure it
799 * is not older than 1 refresh duration since now.
800 */
801 weston_compositor_read_presentation_clock(backend->compositor,
802 &tnow);
803 timespec_sub(&vbl2now, &tnow, &ts);
804 refresh_nsec =
805 millihz_to_nsec(output->base.current_mode->refresh);
806 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
807 drm_output_update_msc(output, vbl.reply.sequence);
808 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200809 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200810 return;
811 }
812 }
813
814 /* Immediate query didn't provide valid timestamp.
815 * Use pageflip fallback.
816 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300817 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200818
Giulio Camuffo954f1832014-10-11 18:27:30 +0300819 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200820 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
821 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200822 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200823 }
David Herrmann3c688c52013-10-22 17:11:25 +0200824
825 return;
826
827finish_frame:
828 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300829 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200830 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200831 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200832}
833
834static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400835drm_output_update_msc(struct drm_output *output, unsigned int seq)
836{
837 uint64_t msc_hi = output->base.msc >> 32;
838
839 if (seq < (output->base.msc & 0xffffffff))
840 msc_hi++;
841
842 output->base.msc = (msc_hi << 32) + seq;
843}
844
845static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500846vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
847 void *data)
848{
849 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300850 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400851 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200852 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
853 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300854
Pekka Paalanen641307c2014-09-23 22:08:47 -0400855 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300856 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500857
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200858 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200859 s->current = s->next;
860 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300861
862 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400863 ts.tv_sec = sec;
864 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200865 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300866 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500867}
868
869static void
Xiong Zhangabd5d472013-10-11 14:43:07 +0800870drm_output_destroy(struct weston_output *output_base);
871
872static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400873page_flip_handler(int fd, unsigned int frame,
874 unsigned int sec, unsigned int usec, void *data)
875{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200876 struct drm_output *output = (struct drm_output *) data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400877 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200878 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
879 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
880 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400881
Pekka Paalanen641307c2014-09-23 22:08:47 -0400882 drm_output_update_msc(output, frame);
883
Jonas Ådahle5a12252013-04-05 23:07:11 +0200884 /* We don't set page_flip_pending on start_repaint_loop, in that case
885 * we just want to page flip to the current buffer to get an accurate
886 * timestamp */
887 if (output->page_flip_pending) {
888 drm_output_release_fb(output, output->current);
889 output->current = output->next;
890 output->next = NULL;
891 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300892
Jonas Ådahle5a12252013-04-05 23:07:11 +0200893 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400894
Xiong Zhangabd5d472013-10-11 14:43:07 +0800895 if (output->destroy_pending)
896 drm_output_destroy(&output->base);
897 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400898 ts.tv_sec = sec;
899 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200900 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300901
902 /* We can't call this from frame_notify, because the output's
903 * repaint needed flag is cleared just after that */
904 if (output->recorder)
905 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300906 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200907}
908
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500909static uint32_t
910drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500912{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500913 uint32_t i, format;
914
915 format = gbm_bo_get_format(bo);
916
917 if (format == GBM_FORMAT_ARGB8888) {
918 pixman_region32_t r;
919
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500920 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600921 ev->surface->width,
922 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500924
925 if (!pixman_region32_not_empty(&r))
926 format = GBM_FORMAT_XRGB8888;
927
928 pixman_region32_fini(&r);
929 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500930
931 for (i = 0; i < s->count_formats; i++)
932 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500933 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500934
935 return 0;
936}
937
938static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500940{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 return !ev->transform.enabled ||
942 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500943}
944
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400945static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200946drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200949 struct weston_compositor *ec = output->base.compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300950 struct drm_backend *b = (struct drm_backend *)ec->backend;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200951 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300952 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500953 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300954 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500955 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500956 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500957 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200958 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500959 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400960 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200963 return NULL;
964
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200965 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200966 return NULL;
967
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200968 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200969 return NULL;
970
Giulio Camuffo954f1832014-10-11 18:27:30 +0300971 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400972 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500973
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200974 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400975 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300976
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400978 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300979 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500980
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200982 return NULL;
983
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300984 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500985 return NULL;
986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400988 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500989
Giulio Camuffo954f1832014-10-11 18:27:30 +0300990 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200991 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500992 continue;
993
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200994 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500995 found = 1;
996 break;
997 }
998 }
999
1000 /* No sprites available */
1001 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001002 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001003
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001004 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001005#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001006 /* XXX: TODO:
1007 *
1008 * Use AddFB2 directly, do not go via GBM.
1009 * Add support for multiplanar formats.
1010 * Both require refactoring in the DRM-backend to
1011 * support a mix of gbm_bos and drmfbs.
1012 */
1013 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001014 .fd = dmabuf->attributes.fd[0],
1015 .width = dmabuf->attributes.width,
1016 .height = dmabuf->attributes.height,
1017 .stride = dmabuf->attributes.stride[0],
1018 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001019 };
1020
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001021 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001022 return NULL;
1023
1024 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1025 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001026#else
1027 return NULL;
1028#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001029 } else {
1030 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1031 buffer_resource, GBM_BO_USE_SCANOUT);
1032 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001033 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001034 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001037 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001038 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001039 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001040 }
1041
Giulio Camuffo954f1832014-10-11 18:27:30 +03001042 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001043 if (!s->next) {
1044 gbm_bo_destroy(bo);
1045 return NULL;
1046 }
1047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001049
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001051 s->plane.x = box->x1;
1052 s->plane.y = box->y1;
1053
Jesse Barnes58ef3792012-02-23 09:45:49 -05001054 /*
1055 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001056 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001057 * for us already).
1058 */
1059 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001061 &output->base.region);
1062 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001063 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001064 tbox = weston_transformed_rect(output->base.width,
1065 output->base.height,
1066 output->base.transform,
1067 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001068 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001069 s->dest_x = tbox.x1;
1070 s->dest_y = tbox.y1;
1071 s->dest_w = tbox.x2 - tbox.x1;
1072 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001073 pixman_region32_fini(&dest_rect);
1074
1075 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001077 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001078 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_view_from_global_fixed(ev,
1081 wl_fixed_from_int(box->x1),
1082 wl_fixed_from_int(box->y1),
1083 &sx1, &sy1);
1084 weston_view_from_global_fixed(ev,
1085 wl_fixed_from_int(box->x2),
1086 wl_fixed_from_int(box->y2),
1087 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001088
1089 if (sx1 < 0)
1090 sx1 = 0;
1091 if (sy1 < 0)
1092 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001093 if (sx2 > wl_fixed_from_int(ev->surface->width))
1094 sx2 = wl_fixed_from_int(ev->surface->width);
1095 if (sy2 > wl_fixed_from_int(ev->surface->height))
1096 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001097
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001098 tbox.x1 = sx1;
1099 tbox.y1 = sy1;
1100 tbox.x2 = sx2;
1101 tbox.y2 = sy2;
1102
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001103 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1104 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001105 viewport->buffer.transform,
1106 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001107 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001108
1109 s->src_x = tbox.x1 << 8;
1110 s->src_y = tbox.y1 << 8;
1111 s->src_w = (tbox.x2 - tbox.x1) << 8;
1112 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001113 pixman_region32_fini(&src_rect);
1114
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001115 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001116}
1117
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001118static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001119drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001121{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001122 struct drm_backend *b =
1123 (struct drm_backend *)output->base.compositor->backend;
Neil Robertsf37f82c2014-05-01 18:00:41 +01001124 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001125 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001126
Derek Foremanbe428b32015-11-24 11:39:38 -06001127 if (ev->transform.enabled &&
1128 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1129 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001130 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001131 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001132 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1133 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001134 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001135 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001137 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001138 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001139 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001140 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001141 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001142 if (ev->geometry.scissor_enabled)
1143 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001144 if (ev->surface->buffer_ref.buffer == NULL)
1145 return NULL;
1146 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1147 if (!shmbuf)
1148 return NULL;
1149 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1150 return NULL;
1151 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001152 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001153 return NULL;
1154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001156
1157 return &output->cursor_plane;
1158}
1159
Pekka Paalanend0ead482014-06-16 12:05:40 +03001160/**
1161 * Update the image for the current cursor surface
1162 *
1163 * @param b DRM backend structure
1164 * @param bo GBM buffer object to write into
1165 * @param ev View to use for cursor image
1166 */
1167static void
1168cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1169 struct weston_view *ev)
1170{
1171 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1172 uint32_t buf[b->cursor_width * b->cursor_height];
1173 int32_t stride;
1174 uint8_t *s;
1175 int i;
1176
1177 assert(buffer && buffer->shm_buffer);
1178 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1179 assert(ev->surface->width <= b->cursor_width);
1180 assert(ev->surface->height <= b->cursor_height);
1181
1182 memset(buf, 0, sizeof buf);
1183 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1184 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1185
1186 wl_shm_buffer_begin_access(buffer->shm_buffer);
1187 for (i = 0; i < ev->surface->height; i++)
1188 memcpy(buf + i * b->cursor_width,
1189 s + i * stride,
1190 ev->surface->width * 4);
1191 wl_shm_buffer_end_access(buffer->shm_buffer);
1192
1193 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1194 weston_log("failed update cursor: %m\n");
1195}
1196
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001197static void
1198drm_output_set_cursor(struct drm_output *output)
1199{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001201 struct weston_buffer *buffer;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001202 struct drm_backend *b =
1203 (struct drm_backend *) output->base.compositor->backend;
Pekka Paalanend0ead482014-06-16 12:05:40 +03001204 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001205 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001206 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 output->cursor_view = NULL;
1209 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001210 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001211 output->cursor_plane.x = INT32_MIN;
1212 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001213 return;
1214 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001215
Neil Robertse5051712013-11-13 15:44:06 +00001216 buffer = ev->surface->buffer_ref.buffer;
1217
1218 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001219 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001220 pixman_region32_fini(&output->cursor_plane.damage);
1221 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001222 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001223 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001224
Pekka Paalanend0ead482014-06-16 12:05:40 +03001225 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001226 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001227 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1228 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001229 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001230 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001231 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001232 }
1233
Derek Foremanbe428b32015-11-24 11:39:38 -06001234 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001235
1236 /* From global to output space, output transform is guaranteed to be
1237 * NORMAL by drm_output_prepare_cursor_view().
1238 */
1239 x = (x - output->base.x) * output->base.current_scale;
1240 y = (y - output->base.y) * output->base.current_scale;
1241
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001242 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001243 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001244 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001245 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001246 }
1247
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001248 output->cursor_plane.x = x;
1249 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001250 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001251}
1252
Jesse Barnes58ef3792012-02-23 09:45:49 -05001253static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001254drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001255{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001256 struct drm_backend *b =
1257 (struct drm_backend *)output_base->compositor->backend;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001258 struct drm_output *output = (struct drm_output *)output_base;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001260 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001261 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001262
1263 /*
1264 * Find a surface for each sprite in the output using some heuristics:
1265 * 1) size
1266 * 2) frequency of update
1267 * 3) opacity (though some hw might support alpha blending)
1268 * 4) clipping (this can be fixed with color keys)
1269 *
1270 * The idea is to save on blitting since this should save power.
1271 * If we can get a large video surface on the sprite for example,
1272 * the main display surface may not need to update at all, and
1273 * the client buffer can be used directly for the sprite surface
1274 * as we do for flipping full screen surfaces.
1275 */
1276 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001277 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278
Giulio Camuffo954f1832014-10-11 18:27:30 +03001279 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001280 struct weston_surface *es = ev->surface;
1281
1282 /* Test whether this buffer can ever go into a plane:
1283 * non-shm, or small enough to be a cursor.
1284 *
1285 * Also, keep a reference when using the pixman renderer.
1286 * That makes it possible to do a seamless switch to the GL
1287 * renderer and since the pixman renderer keeps a reference
1288 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001289 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001290 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001291 (es->buffer_ref.buffer &&
1292 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001293 (ev->surface->width <= b->cursor_width &&
1294 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001295 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001296 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001297 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001298
Jesse Barnes58ef3792012-02-23 09:45:49 -05001299 pixman_region32_init(&surface_overlap);
1300 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001302
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001303 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001304 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001305 next_plane = primary;
1306 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001308 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001310 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001312 if (next_plane == NULL)
1313 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001316
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001317 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001318 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001320
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001321 if (next_plane == primary ||
1322 next_plane == &output->cursor_plane) {
1323 /* cursor plane involves a copy */
1324 ev->psf_flags = 0;
1325 } else {
1326 /* All other planes are a direct scanout of a
1327 * single client buffer.
1328 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001329 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001330 }
1331
Jesse Barnes58ef3792012-02-23 09:45:49 -05001332 pixman_region32_fini(&surface_overlap);
1333 }
1334 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001335}
1336
Matt Roper361d2ad2011-08-29 13:52:23 -07001337static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001338drm_output_fini_pixman(struct drm_output *output);
1339
1340static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001341drm_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -07001342{
1343 struct drm_output *output = (struct drm_output *) output_base;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001344 struct drm_backend *b =
1345 (struct drm_backend *)output->base.compositor->backend;
Matt Roper361d2ad2011-08-29 13:52:23 -07001346 drmModeCrtcPtr origcrtc = output->original_crtc;
Matt Roper361d2ad2011-08-29 13:52:23 -07001347
Xiong Zhangabd5d472013-10-11 14:43:07 +08001348 if (output->page_flip_pending) {
1349 output->destroy_pending = 1;
1350 weston_log("destroy output while page flip pending\n");
1351 return;
1352 }
1353
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001354 if (output->backlight)
1355 backlight_destroy(output->backlight);
1356
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001357 drmModeFreeProperty(output->dpms_prop);
1358
Matt Roper361d2ad2011-08-29 13:52:23 -07001359 /* Turn off hardware cursor */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001360 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Matt Roper361d2ad2011-08-29 13:52:23 -07001361
1362 /* Restore original CRTC state */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001363 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
Benjamin Franzke117483d2011-08-30 11:38:26 +02001364 origcrtc->x, origcrtc->y,
1365 &output->connector_id, 1, &origcrtc->mode);
Matt Roper361d2ad2011-08-29 13:52:23 -07001366 drmModeFreeCrtc(origcrtc);
1367
Giulio Camuffo954f1832014-10-11 18:27:30 +03001368 b->crtc_allocator &= ~(1 << output->crtc_id);
1369 b->connector_allocator &= ~(1 << output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001370
Giulio Camuffo954f1832014-10-11 18:27:30 +03001371 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001372 drm_output_fini_pixman(output);
1373 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001374 gl_renderer->output_destroy(output_base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001375 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001376 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001377
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001378 weston_plane_release(&output->fb_plane);
1379 weston_plane_release(&output->cursor_plane);
1380
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001381 weston_output_destroy(&output->base);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02001382
Matt Roper361d2ad2011-08-29 13:52:23 -07001383 free(output);
1384}
1385
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001386/**
1387 * Find the closest-matching mode for a given target
1388 *
1389 * Given a target mode, find the most suitable mode amongst the output's
1390 * current mode list to use, preferring the current mode if possible, to
1391 * avoid an expensive mode switch.
1392 *
1393 * @param output DRM output
1394 * @param target_mode Mode to attempt to match
1395 * @returns Pointer to a mode from the output's mode list
1396 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001397static struct drm_mode *
1398choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1399{
1400 struct drm_mode *tmp_mode = NULL, *mode;
1401
Hardeningff39efa2013-09-18 23:56:35 +02001402 if (output->base.current_mode->width == target_mode->width &&
1403 output->base.current_mode->height == target_mode->height &&
1404 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001405 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001406 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001407
1408 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1409 if (mode->mode_info.hdisplay == target_mode->width &&
1410 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001411 if (mode->base.refresh == target_mode->refresh ||
1412 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001413 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001414 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001415 tmp_mode = mode;
1416 }
1417 }
1418
1419 return tmp_mode;
1420}
1421
1422static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001423drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001424static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001425drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001426
1427static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001428drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1429{
1430 struct drm_output *output;
1431 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001432 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001433
1434 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001435 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001436 return -1;
1437 }
1438
1439 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001440 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001441 return -1;
1442 }
1443
Giulio Camuffo954f1832014-10-11 18:27:30 +03001444 b = (struct drm_backend *)output_base->compositor->backend;
Alex Wub7b8bda2012-04-17 17:20:48 +08001445 output = (struct drm_output *)output_base;
1446 drm_mode = choose_mode (output, mode);
1447
1448 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001449 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001450 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001451 }
1452
Hardeningff39efa2013-09-18 23:56:35 +02001453 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001454 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001455
Hardeningff39efa2013-09-18 23:56:35 +02001456 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001457
Hardeningff39efa2013-09-18 23:56:35 +02001458 output->base.current_mode = &drm_mode->base;
1459 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001460 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1461
Alex Wub7b8bda2012-04-17 17:20:48 +08001462 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001463 drm_output_release_fb(output, output->current);
1464 drm_output_release_fb(output, output->next);
1465 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001466
Giulio Camuffo954f1832014-10-11 18:27:30 +03001467 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001468 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001469 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001470 weston_log("failed to init output pixman state with "
1471 "new mode\n");
1472 return -1;
1473 }
1474 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001475 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001476 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001477
Giulio Camuffo954f1832014-10-11 18:27:30 +03001478 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001479 weston_log("failed to init output egl state with "
1480 "new mode");
1481 return -1;
1482 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001483 }
1484
Alex Wub7b8bda2012-04-17 17:20:48 +08001485 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001486}
1487
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001488static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001489on_drm_input(int fd, uint32_t mask, void *data)
1490{
1491 drmEventContext evctx;
1492
1493 memset(&evctx, 0, sizeof evctx);
1494 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1495 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001496 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001497 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001498
1499 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001500}
1501
1502static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001503init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001504{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001505 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001506 uint64_t cap;
1507 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001508 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001509
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001510 sysnum = udev_device_get_sysnum(device);
1511 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001512 b->drm.id = atoi(sysnum);
1513 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001514 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001515 return -1;
1516 }
1517
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001518 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001519 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001520 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001521 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001522 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001523 udev_device_get_devnode(device));
1524 return -1;
1525 }
1526
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001527 weston_log("using %s\n", filename);
1528
Giulio Camuffo954f1832014-10-11 18:27:30 +03001529 b->drm.fd = fd;
1530 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001531
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001532 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1533 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001534 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001535 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001536 clk_id = CLOCK_REALTIME;
1537
Giulio Camuffo954f1832014-10-11 18:27:30 +03001538 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001539 weston_log("Error: failed to set presentation clock %d.\n",
1540 clk_id);
1541 return -1;
1542 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001543
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001544 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1545 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001546 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001547 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001548 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001549
1550 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1551 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001552 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001553 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001554 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001555
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001556 return 0;
1557}
1558
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001559static struct gbm_device *
1560create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001561{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001562 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001563
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001564 gl_renderer = weston_load_module("gl-renderer.so",
1565 "gl_renderer_interface");
1566 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001567 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001568
1569 /* GBM will load a dri driver, but even though they need symbols from
1570 * libglapi, in some version of Mesa they are not linked to it. Since
1571 * only the gl-renderer module links to it, the call above won't make
1572 * these symbols globally available, and loading the DRI driver fails.
1573 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1574 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1575
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001576 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001577
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001578 return gbm;
1579}
1580
Bryce Harringtonc056a982015-05-19 15:25:18 -07001581/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001582 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001583 *
1584 * This returns 0 if substitution isn't possible, but 0 might be a
1585 * legitimate format for other EGL platforms, so the caller is
1586 * responsible for checking for 0 before calling gl_renderer->create().
1587 *
1588 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1589 * but it's entirely possible we'll see this again on other implementations.
1590 */
1591static int
1592fallback_format_for(uint32_t format)
1593{
1594 switch (format) {
1595 case GBM_FORMAT_XRGB8888:
1596 return GBM_FORMAT_ARGB8888;
1597 case GBM_FORMAT_XRGB2101010:
1598 return GBM_FORMAT_ARGB2101010;
1599 default:
1600 return 0;
1601 }
1602}
1603
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001604static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001605drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001606{
Derek Foreman6d556372015-11-04 14:47:33 -06001607 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001608 b->gbm_format,
1609 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001610 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001611 };
Derek Foreman6d556372015-11-04 14:47:33 -06001612 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001613
Derek Foremanc4cfe852015-05-15 12:12:40 -05001614 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001615 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001616 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001617 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001618 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001619 gl_renderer->opaque_attribs,
1620 format,
1621 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001622 return -1;
1623 }
1624
1625 return 0;
1626}
1627
1628static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001629init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001630{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001631 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001632
Giulio Camuffo954f1832014-10-11 18:27:30 +03001633 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001634 return -1;
1635
Giulio Camuffo954f1832014-10-11 18:27:30 +03001636 if (drm_backend_create_gl_renderer(b) < 0) {
1637 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001638 return -1;
1639 }
1640
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001641 return 0;
1642}
1643
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001644static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001645init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001646{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001647 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001648}
1649
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001650/**
1651 * Add a mode to output's mode list
1652 *
1653 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1654 * output's mode list.
1655 *
1656 * @param output DRM output to add mode to
1657 * @param info DRM mode structure to add
1658 * @returns Newly-allocated Weston/DRM mode structure
1659 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001660static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001661drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001662{
1663 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001664 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001665
1666 mode = malloc(sizeof *mode);
1667 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001668 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001669
1670 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001671 mode->base.width = info->hdisplay;
1672 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001673
1674 /* Calculate higher precision (mHz) refresh rate */
1675 refresh = (info->clock * 1000000LL / info->htotal +
1676 info->vtotal / 2) / info->vtotal;
1677
1678 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1679 refresh *= 2;
1680 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1681 refresh /= 2;
1682 if (info->vscan > 1)
1683 refresh /= info->vscan;
1684
1685 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001686 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001687
1688 if (info->type & DRM_MODE_TYPE_PREFERRED)
1689 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1690
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001691 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1692
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001693 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001694}
1695
1696static int
1697drm_subpixel_to_wayland(int drm_value)
1698{
1699 switch (drm_value) {
1700 default:
1701 case DRM_MODE_SUBPIXEL_UNKNOWN:
1702 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1703 case DRM_MODE_SUBPIXEL_NONE:
1704 return WL_OUTPUT_SUBPIXEL_NONE;
1705 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1706 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1707 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1708 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1709 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1710 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1711 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1712 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1713 }
1714}
1715
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001716/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001717static uint32_t
1718drm_get_backlight(struct drm_output *output)
1719{
1720 long brightness, max_brightness, norm;
1721
1722 brightness = backlight_get_brightness(output->backlight);
1723 max_brightness = backlight_get_max_brightness(output->backlight);
1724
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001725 /* convert it on a scale of 0 to 255 */
1726 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001727
1728 return (uint32_t) norm;
1729}
1730
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001731/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001732static void
1733drm_set_backlight(struct weston_output *output_base, uint32_t value)
1734{
1735 struct drm_output *output = (struct drm_output *) output_base;
1736 long max_brightness, new_brightness;
1737
1738 if (!output->backlight)
1739 return;
1740
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001741 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001742 return;
1743
1744 max_brightness = backlight_get_max_brightness(output->backlight);
1745
1746 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001747 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001748
1749 backlight_set_brightness(output->backlight, new_brightness);
1750}
1751
1752static drmModePropertyPtr
1753drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1754{
1755 drmModePropertyPtr props;
1756 int i;
1757
1758 for (i = 0; i < connector->count_props; i++) {
1759 props = drmModeGetProperty(fd, connector->props[i]);
1760 if (!props)
1761 continue;
1762
1763 if (!strcmp(props->name, name))
1764 return props;
1765
1766 drmModeFreeProperty(props);
1767 }
1768
1769 return NULL;
1770}
1771
1772static void
1773drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1774{
1775 struct drm_output *output = (struct drm_output *) output_base;
1776 struct weston_compositor *ec = output_base->compositor;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001777 struct drm_backend *b = (struct drm_backend *)ec->backend;
Daniel Stone36609c72015-06-18 07:49:02 +01001778 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001779
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001780 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001781 return;
1782
Daniel Stone36609c72015-06-18 07:49:02 +01001783 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1784 output->dpms_prop->prop_id, level);
1785 if (ret) {
1786 weston_log("DRM: DPMS: failed property set for %s\n",
1787 output->base.name);
1788 return;
1789 }
1790
1791 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001792}
1793
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001794static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001795 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1796 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1797 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1798 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1799 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1800 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1801 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1802 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1803 [DRM_MODE_CONNECTOR_Component] = "Component",
1804 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1805 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1806 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1807 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1808 [DRM_MODE_CONNECTOR_TV] = "TV",
1809 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001810#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001811 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1812 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001813#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001814};
1815
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001816static char *
1817make_connector_name(const drmModeConnector *con)
1818{
1819 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001820 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001821
1822 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1823 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001824
1825 if (!type_name)
1826 type_name = "UNNAMED";
1827
1828 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001829
1830 return strdup(name);
1831}
1832
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001833static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001834find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001835 drmModeRes *resources, drmModeConnector *connector)
1836{
1837 drmModeEncoder *encoder;
1838 uint32_t possible_crtcs;
1839 int i, j;
1840
1841 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001842 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001843 if (encoder == NULL) {
1844 weston_log("Failed to get encoder.\n");
1845 return -1;
1846 }
1847 possible_crtcs = encoder->possible_crtcs;
1848 drmModeFreeEncoder(encoder);
1849
1850 for (i = 0; i < resources->count_crtcs; i++) {
1851 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001852 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001853 return i;
1854 }
1855 }
1856
1857 return -1;
1858}
1859
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001860/* Init output state that depends on gl or gbm */
1861static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001862drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001863{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001864 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001865 output->gbm_format,
1866 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001867 };
1868 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001869
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001870 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001871 output->base.current_mode->width,
1872 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001873 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001874 GBM_BO_USE_SCANOUT |
1875 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001876 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001877 weston_log("failed to create gbm surface\n");
1878 return -1;
1879 }
1880
Derek Foremanc4cfe852015-05-15 12:12:40 -05001881 if (format[1])
1882 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001883 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001884 (EGLNativeWindowType)output->gbm_surface,
1885 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001886 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001887 format,
1888 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001889 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001890 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001891 return -1;
1892 }
1893
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001894 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001895
1896 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001897 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001898 continue;
1899
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001900 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001901 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001902 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001903 }
1904
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001905 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001906 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001907 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001908 }
1909
1910 return 0;
1911}
1912
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001913static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001914drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001915{
Hardeningff39efa2013-09-18 23:56:35 +02001916 int w = output->base.current_mode->width;
1917 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001918 uint32_t format = output->gbm_format;
1919 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001920 unsigned int i;
1921
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001922 switch (format) {
1923 case GBM_FORMAT_XRGB8888:
1924 pixman_format = PIXMAN_x8r8g8b8;
1925 break;
1926 case GBM_FORMAT_RGB565:
1927 pixman_format = PIXMAN_r5g6b5;
1928 break;
1929 default:
1930 weston_log("Unsupported pixman format 0x%x\n", format);
1931 return -1;
1932 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001933
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001934 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001935 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001936 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001937 if (!output->dumb[i])
1938 goto err;
1939
1940 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001941 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001942 output->dumb[i]->map,
1943 output->dumb[i]->stride);
1944 if (!output->image[i])
1945 goto err;
1946 }
1947
1948 if (pixman_renderer_output_create(&output->base) < 0)
1949 goto err;
1950
1951 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001952 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001953
1954 return 0;
1955
1956err:
1957 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1958 if (output->dumb[i])
1959 drm_fb_destroy_dumb(output->dumb[i]);
1960 if (output->image[i])
1961 pixman_image_unref(output->image[i]);
1962
1963 output->dumb[i] = NULL;
1964 output->image[i] = NULL;
1965 }
1966
1967 return -1;
1968}
1969
1970static void
1971drm_output_fini_pixman(struct drm_output *output)
1972{
1973 unsigned int i;
1974
1975 pixman_renderer_output_destroy(&output->base);
1976 pixman_region32_fini(&output->previous_damage);
1977
1978 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1979 drm_fb_destroy_dumb(output->dumb[i]);
1980 pixman_image_unref(output->image[i]);
1981 output->dumb[i] = NULL;
1982 output->image[i] = NULL;
1983 }
1984}
1985
Richard Hughes2b2092a2013-04-24 14:58:02 +01001986static void
1987edid_parse_string(const uint8_t *data, char text[])
1988{
1989 int i;
1990 int replaced = 0;
1991
1992 /* this is always 12 bytes, but we can't guarantee it's null
1993 * terminated or not junk. */
1994 strncpy(text, (const char *) data, 12);
1995
Bryce Harrington9c7de162015-08-28 13:04:26 -07001996 /* guarantee our new string is null-terminated */
1997 text[12] = '\0';
1998
Richard Hughes2b2092a2013-04-24 14:58:02 +01001999 /* remove insane chars */
2000 for (i = 0; text[i] != '\0'; i++) {
2001 if (text[i] == '\n' ||
2002 text[i] == '\r') {
2003 text[i] = '\0';
2004 break;
2005 }
2006 }
2007
2008 /* ensure string is printable */
2009 for (i = 0; text[i] != '\0'; i++) {
2010 if (!isprint(text[i])) {
2011 text[i] = '-';
2012 replaced++;
2013 }
2014 }
2015
2016 /* if the string is random junk, ignore the string */
2017 if (replaced > 4)
2018 text[0] = '\0';
2019}
2020
2021#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2022#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2023#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2024#define EDID_OFFSET_DATA_BLOCKS 0x36
2025#define EDID_OFFSET_LAST_BLOCK 0x6c
2026#define EDID_OFFSET_PNPID 0x08
2027#define EDID_OFFSET_SERIAL 0x0c
2028
2029static int
2030edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2031{
2032 int i;
2033 uint32_t serial_number;
2034
2035 /* check header */
2036 if (length < 128)
2037 return -1;
2038 if (data[0] != 0x00 || data[1] != 0xff)
2039 return -1;
2040
2041 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2042 * /--08--\/--09--\
2043 * 7654321076543210
2044 * |\---/\---/\---/
2045 * R C1 C2 C3 */
2046 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2047 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2048 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2049 edid->pnp_id[3] = '\0';
2050
2051 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2052 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2053 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2054 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2055 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2056 if (serial_number > 0)
2057 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2058
2059 /* parse EDID data */
2060 for (i = EDID_OFFSET_DATA_BLOCKS;
2061 i <= EDID_OFFSET_LAST_BLOCK;
2062 i += 18) {
2063 /* ignore pixel clock data */
2064 if (data[i] != 0)
2065 continue;
2066 if (data[i+2] != 0)
2067 continue;
2068
2069 /* any useful blocks? */
2070 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2071 edid_parse_string(&data[i+5],
2072 edid->monitor_name);
2073 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2074 edid_parse_string(&data[i+5],
2075 edid->serial_number);
2076 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2077 edid_parse_string(&data[i+5],
2078 edid->eisa_id);
2079 }
2080 }
2081 return 0;
2082}
2083
2084static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002085find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002086 struct drm_output *output,
2087 drmModeConnector *connector)
2088{
2089 drmModePropertyBlobPtr edid_blob = NULL;
2090 drmModePropertyPtr property;
2091 int i;
2092 int rc;
2093
2094 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002095 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002096 if (!property)
2097 continue;
2098 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2099 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002100 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002101 connector->prop_values[i]);
2102 }
2103 drmModeFreeProperty(property);
2104 }
2105 if (!edid_blob)
2106 return;
2107
2108 rc = edid_parse(&output->edid,
2109 edid_blob->data,
2110 edid_blob->length);
2111 if (!rc) {
2112 weston_log("EDID data '%s', '%s', '%s'\n",
2113 output->edid.pnp_id,
2114 output->edid.monitor_name,
2115 output->edid.serial_number);
2116 if (output->edid.pnp_id[0] != '\0')
2117 output->base.make = output->edid.pnp_id;
2118 if (output->edid.monitor_name[0] != '\0')
2119 output->base.model = output->edid.monitor_name;
2120 if (output->edid.serial_number[0] != '\0')
2121 output->base.serial_number = output->edid.serial_number;
2122 }
2123 drmModeFreePropertyBlob(edid_blob);
2124}
2125
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002126
2127
2128static int
2129parse_modeline(const char *s, drmModeModeInfo *mode)
2130{
2131 char hsync[16];
2132 char vsync[16];
2133 float fclock;
2134
2135 mode->type = DRM_MODE_TYPE_USERDEF;
2136 mode->hskew = 0;
2137 mode->vscan = 0;
2138 mode->vrefresh = 0;
2139 mode->flags = 0;
2140
Rob Bradford307e09e2013-07-26 16:29:40 +01002141 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002142 &fclock,
2143 &mode->hdisplay,
2144 &mode->hsync_start,
2145 &mode->hsync_end,
2146 &mode->htotal,
2147 &mode->vdisplay,
2148 &mode->vsync_start,
2149 &mode->vsync_end,
2150 &mode->vtotal, hsync, vsync) != 11)
2151 return -1;
2152
2153 mode->clock = fclock * 1000;
2154 if (strcmp(hsync, "+hsync") == 0)
2155 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2156 else if (strcmp(hsync, "-hsync") == 0)
2157 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2158 else
2159 return -1;
2160
2161 if (strcmp(vsync, "+vsync") == 0)
2162 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2163 else if (strcmp(vsync, "-vsync") == 0)
2164 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2165 else
2166 return -1;
2167
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002168 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2169 mode->hdisplay, mode->vdisplay, fclock);
2170
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002171 return 0;
2172}
2173
Rob Bradford66bd9f52013-06-25 18:56:42 +01002174static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002175setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002176 struct weston_output *output,
2177 const char *s)
2178{
2179 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002180 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002181 struct udev_seat *seat;
2182
Giulio Camuffo954f1832014-10-11 18:27:30 +03002183 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002184 if (!seat)
2185 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002186
Derek Foreman0720ea32015-07-15 13:00:35 -05002187 seat->base.output = output;
2188
Derek Foreman1281a362015-07-31 16:55:32 -05002189 pointer = weston_seat_get_pointer(&seat->base);
2190 if (pointer)
2191 weston_pointer_clamp(pointer,
2192 &pointer->x,
2193 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002194 }
2195}
2196
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002197static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002198parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002199{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002200 int ret = 0;
2201
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002202 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002203 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002204 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002205 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002206 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002207 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002208 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002209 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002210 else {
2211 weston_log("fatal: unrecognized pixel format: %s\n", s);
2212 ret = -1;
2213 }
2214
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002215 return ret;
2216}
2217
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002218/**
2219 * Choose suitable mode for an output
2220 *
2221 * Find the most suitable mode to use for initial setup (or reconfiguration on
2222 * hotplug etc) for a DRM output.
2223 *
2224 * @param output DRM output to choose mode for
2225 * @param kind Strategy and preference to use when choosing mode
2226 * @param width Desired width for this output
2227 * @param height Desired height for this output
2228 * @param current_mode Mode currently being displayed on this output
2229 * @param modeline Manually-entered mode (may be NULL)
2230 * @returns A mode from the output's mode list, or NULL if none available
2231 */
2232static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002233drm_output_choose_initial_mode(struct drm_backend *backend,
2234 struct drm_output *output,
2235 enum weston_drm_backend_output_mode mode,
2236 struct weston_drm_backend_output_config *config,
2237 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002238{
2239 struct drm_mode *preferred = NULL;
2240 struct drm_mode *current = NULL;
2241 struct drm_mode *configured = NULL;
2242 struct drm_mode *best = NULL;
2243 struct drm_mode *drm_mode;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002244 drmModeModeInfo modeline;
2245 int32_t width = 0;
2246 int32_t height = 0;
2247
2248 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
2249 if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
2250 width = -1;
2251
2252 if (parse_modeline(config->modeline, &modeline) == 0) {
2253 configured = drm_output_add_mode(output, &modeline);
2254 if (!configured)
2255 return NULL;
2256 } else {
2257 weston_log("Invalid modeline \"%s\" for output %s\n",
2258 config->modeline, output->base.name);
2259 }
2260 }
2261 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002262
2263 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002264 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002265 height == drm_mode->base.height)
2266 configured = drm_mode;
2267
comic fans7a5c5622016-03-17 14:29:27 +02002268 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002269 sizeof *current_mode) == 0)
2270 current = drm_mode;
2271
2272 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2273 preferred = drm_mode;
2274
2275 best = drm_mode;
2276 }
2277
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002278 if (current == NULL && current_mode->clock != 0) {
2279 current = drm_output_add_mode(output, current_mode);
2280 if (!current)
2281 return NULL;
2282 }
2283
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002284 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002285 configured = current;
2286
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002287 if (configured)
2288 return configured;
2289
2290 if (preferred)
2291 return preferred;
2292
2293 if (current)
2294 return current;
2295
2296 if (best)
2297 return best;
2298
2299 weston_log("no available modes for %s\n", output->base.name);
2300 return NULL;
2301}
2302
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002303static int
2304connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2305 drmModeModeInfo *mode)
2306{
2307 drmModeEncoder *encoder;
2308 drmModeCrtc *crtc;
2309
2310 /* Get the current mode on the crtc that's currently driving
2311 * this connector. */
2312 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2313 memset(mode, 0, sizeof *mode);
2314 if (encoder != NULL) {
2315 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2316 drmModeFreeEncoder(encoder);
2317 if (crtc == NULL)
2318 return -1;
2319 if (crtc->mode_valid)
2320 *mode = crtc->mode;
2321 drmModeFreeCrtc(crtc);
2322 }
2323
2324 return 0;
2325}
2326
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002327/**
2328 * Create and configure a Weston output structure
2329 *
2330 * Given a DRM connector, create a matching drm_output structure and add it
2331 * to Weston's output list.
2332 *
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002333 * @param b Weston backend structure structure
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002334 * @param resources DRM resources for this device
2335 * @param connector DRM connector to use for this new output
2336 * @param x Horizontal offset to use into global co-ordinate space
2337 * @param y Vertical offset to use into global co-ordinate space
2338 * @param drm_device udev device pointer
2339 * @returns 0 on success, or -1 on failure
2340 */
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002341static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002342create_output_for_connector(struct drm_backend *b,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002343 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002344 drmModeConnector *connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002345 int x, int y, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002346{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002347 struct drm_output *output;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002348 struct drm_mode *drm_mode, *next, *current;
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002349 struct weston_mode *m;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002350
2351 drmModeModeInfo crtc_mode;
2352 int i;
2353 enum weston_drm_backend_output_mode mode;
2354 struct weston_drm_backend_output_config config = {{ 0 }};
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002355
Giulio Camuffo954f1832014-10-11 18:27:30 +03002356 i = find_crtc_for_connector(b, resources, connector);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002357 if (i < 0) {
2358 weston_log("No usable crtc/encoder pair for connector.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002359 return -1;
2360 }
2361
Peter Huttererf3d62272013-08-08 11:57:05 +10002362 output = zalloc(sizeof *output);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002363 if (output == NULL)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002364 return -1;
2365
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002366 output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002367 output->base.name = make_connector_name(connector);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002368 output->base.make = "unknown";
2369 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002370 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002371 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002372
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002373 mode = b->configure_output(b->compositor, b->use_current_mode,
2374 output->base.name, &config);
2375 if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002376 output->gbm_format = b->gbm_format;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002377
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002378 setup_output_seat_constraint(b, &output->base,
2379 config.seat ? config.seat : "");
2380 free(config.seat);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002381
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002382 output->crtc_id = resources->crtcs[i];
Rob Clark5ca1a472012-08-08 20:27:37 -05002383 output->pipe = i;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002384 b->crtc_allocator |= (1 << output->crtc_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002385 output->connector_id = connector->connector_id;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002386 b->connector_allocator |= (1 << output->connector_id);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002387
Giulio Camuffo954f1832014-10-11 18:27:30 +03002388 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
2389 output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
Matt Roper361d2ad2011-08-29 13:52:23 -07002390
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002391 if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
2392 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002393
David Herrmann0f0d54e2011-12-08 17:05:45 +01002394 for (i = 0; i < connector->count_modes; i++) {
Alexander Larsson0b135062013-05-28 16:23:36 +02002395 drm_mode = drm_output_add_mode(output, &connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002396 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002397 goto err_free;
2398 }
2399
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002400 if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002401 weston_log("Disabling output %s\n", output->base.name);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002402 drmModeSetCrtc(b->drm.fd, output->crtc_id,
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002403 0, 0, 0, 0, 0, NULL);
2404 goto err_free;
2405 }
2406
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002407 current = drm_output_choose_initial_mode(b, output, mode, &config,
2408 &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002409 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002410 goto err_free;
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002411 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002412 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002413
Giulio Camuffo954f1832014-10-11 18:27:30 +03002414 weston_output_init(&output->base, b->compositor, x, y,
John Kåre Alsaker94659272012-11-13 19:10:18 +01002415 connector->mmWidth, connector->mmHeight,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002416 config.base.transform, config.base.scale);
John Kåre Alsaker94659272012-11-13 19:10:18 +01002417
Giulio Camuffo954f1832014-10-11 18:27:30 +03002418 if (b->use_pixman) {
2419 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002420 weston_log("Failed to init output pixman state\n");
2421 goto err_output;
2422 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002423 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002424 weston_log("Failed to init output gl state\n");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002425 goto err_output;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002426 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002427
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002428 output->backlight = backlight_init(drm_device,
2429 connector->connector_type);
2430 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002431 weston_log("Initialized backlight, device %s\n",
2432 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002433 output->base.set_backlight = drm_set_backlight;
2434 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002435 } else {
2436 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002437 }
2438
Giulio Camuffo954f1832014-10-11 18:27:30 +03002439 weston_compositor_add_output(b->compositor, &output->base);
Kristian Høgsberga4b7e202011-10-24 13:26:32 -04002440
Giulio Camuffo954f1832014-10-11 18:27:30 +03002441 find_and_parse_output_edid(b, output, connector);
Richard Hughesb24e48e2013-05-09 20:31:09 +01002442 if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2443 output->base.connection_internal = 1;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002444
Jonas Ådahle5a12252013-04-05 23:07:11 +02002445 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002446 output->base.repaint = drm_output_repaint;
Matt Roper361d2ad2011-08-29 13:52:23 -07002447 output->base.destroy = drm_output_destroy;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002448 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002449 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002450 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002451
Richard Hughese7299962013-05-01 21:52:12 +01002452 output->base.gamma_size = output->original_crtc->gamma_size;
2453 output->base.set_gamma = drm_output_set_gamma;
2454
Derek Foremanb0427562016-02-05 15:55:20 -06002455 weston_plane_init(&output->cursor_plane, b->compositor,
2456 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002457 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002458
Giulio Camuffo954f1832014-10-11 18:27:30 +03002459 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2460 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2461 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002462
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002463 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002464 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002465 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002466 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002467 m->width, m->height, m->refresh / 1000.0,
2468 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2469 ", preferred" : "",
2470 m->flags & WL_OUTPUT_MODE_CURRENT ?
2471 ", current" : "",
2472 connector->count_modes == 0 ?
2473 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002474
Mario Kleiner80817042015-06-21 21:25:11 +02002475 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2476 output->base.native_mode = output->base.current_mode;
2477 output->base.native_scale = output->base.current_scale;
2478
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002479 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002480
John Kåre Alsaker94659272012-11-13 19:10:18 +01002481err_output:
2482 weston_output_destroy(&output->base);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002483err_free:
2484 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2485 base.link) {
2486 wl_list_remove(&drm_mode->base.link);
2487 free(drm_mode);
2488 }
2489
2490 drmModeFreeCrtc(output->original_crtc);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002491 b->crtc_allocator &= ~(1 << output->crtc_id);
2492 b->connector_allocator &= ~(1 << output->connector_id);
David Herrmann0f0d54e2011-12-08 17:05:45 +01002493 free(output);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002494 free(config.modeline);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002495
David Herrmann0f0d54e2011-12-08 17:05:45 +01002496 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002497}
2498
Jesse Barnes58ef3792012-02-23 09:45:49 -05002499static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002500create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002501{
2502 struct drm_sprite *sprite;
2503 drmModePlaneRes *plane_res;
2504 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002505 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002506
Giulio Camuffo954f1832014-10-11 18:27:30 +03002507 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002508 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002509 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002510 strerror(errno));
2511 return;
2512 }
2513
2514 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002515 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002516 if (!plane)
2517 continue;
2518
Peter Huttererf3d62272013-08-08 11:57:05 +10002519 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002520 plane->count_formats));
2521 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002522 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002523 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002524 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002525 continue;
2526 }
2527
Jesse Barnes58ef3792012-02-23 09:45:49 -05002528 sprite->possible_crtcs = plane->possible_crtcs;
2529 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002530 sprite->current = NULL;
2531 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002532 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002533 sprite->count_formats = plane->count_formats;
2534 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002535 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002536 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002537 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2538 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2539 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002540
Giulio Camuffo954f1832014-10-11 18:27:30 +03002541 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002542 }
2543
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002544 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002545}
2546
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002547static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002548destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002549{
2550 struct drm_sprite *sprite, *next;
2551 struct drm_output *output;
2552
Giulio Camuffo954f1832014-10-11 18:27:30 +03002553 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002554 struct drm_output, base.link);
2555
Giulio Camuffo954f1832014-10-11 18:27:30 +03002556 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2557 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002558 sprite->plane_id,
2559 output->crtc_id, 0, 0,
2560 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002561 drm_output_release_fb(output, sprite->current);
2562 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002563 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002564 free(sprite);
2565 }
2566}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002567
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002568static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002569create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002570 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002571{
2572 drmModeConnector *connector;
2573 drmModeRes *resources;
2574 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002575 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002576
Giulio Camuffo954f1832014-10-11 18:27:30 +03002577 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002578 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002579 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002580 return -1;
2581 }
2582
Giulio Camuffo954f1832014-10-11 18:27:30 +03002583 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2584 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002585 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002586 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002587 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002588
Giulio Camuffo954f1832014-10-11 18:27:30 +03002589 b->min_width = resources->min_width;
2590 b->max_width = resources->max_width;
2591 b->min_height = resources->min_height;
2592 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002593
Giulio Camuffo954f1832014-10-11 18:27:30 +03002594 b->num_crtcs = resources->count_crtcs;
2595 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002596
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002597 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002598 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002599 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002600 if (connector == NULL)
2601 continue;
2602
2603 if (connector->connection == DRM_MODE_CONNECTED &&
2604 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002605 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002606 if (create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002607 connector, x, y,
2608 drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002609 drmModeFreeConnector(connector);
2610 continue;
2611 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002612
Giulio Camuffo954f1832014-10-11 18:27:30 +03002613 x += container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002614 struct weston_output,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002615 link)->width;
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002616 }
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002617
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002618 drmModeFreeConnector(connector);
2619 }
2620
Giulio Camuffo954f1832014-10-11 18:27:30 +03002621 if (wl_list_empty(&b->compositor->output_list)) {
Martin Minarik6d118362012-06-07 18:01:59 +02002622 weston_log("No currently active connector found.\n");
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002623 drmModeFreeResources(resources);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002624 return -1;
2625 }
2626
2627 drmModeFreeResources(resources);
2628
2629 return 0;
2630}
2631
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002632static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002633update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002634{
2635 drmModeConnector *connector;
2636 drmModeRes *resources;
2637 struct drm_output *output, *next;
2638 int x = 0, y = 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002639 uint32_t connected = 0, disconnects = 0;
2640 int i;
2641
Giulio Camuffo954f1832014-10-11 18:27:30 +03002642 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002643 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002644 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002645 return;
2646 }
2647
2648 /* collect new connects */
2649 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002650 int connector_id = resources->connectors[i];
2651
Giulio Camuffo954f1832014-10-11 18:27:30 +03002652 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002653 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002654 continue;
2655
David Herrmann7551cff2011-12-08 17:05:43 +01002656 if (connector->connection != DRM_MODE_CONNECTED) {
2657 drmModeFreeConnector(connector);
2658 continue;
2659 }
2660
Benjamin Franzke117483d2011-08-30 11:38:26 +02002661 connected |= (1 << connector_id);
2662
Giulio Camuffo954f1832014-10-11 18:27:30 +03002663 if (!(b->connector_allocator & (1 << connector_id))) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002664 struct weston_output *last =
Giulio Camuffo954f1832014-10-11 18:27:30 +03002665 container_of(b->compositor->output_list.prev,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002666 struct weston_output, link);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002667
2668 /* XXX: not yet needed, we die with 0 outputs */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002669 if (!wl_list_empty(&b->compositor->output_list))
Scott Moreau1bad5db2012-08-18 01:04:05 -06002670 x = last->x + last->width;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002671 else
2672 x = 0;
2673 y = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002674 create_output_for_connector(b, resources,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002675 connector, x, y,
2676 drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002677 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002678
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002679 }
2680 drmModeFreeConnector(connector);
2681 }
2682 drmModeFreeResources(resources);
2683
Giulio Camuffo954f1832014-10-11 18:27:30 +03002684 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002685 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002686 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002687 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688 if (disconnects & (1 << output->connector_id)) {
2689 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002690 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002691 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002692 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002693 }
2694 }
2695 }
2696
Daniel Stonef556ebe2015-05-21 08:28:58 +01002697 /* FIXME: handle zero outputs, without terminating */
Giulio Camuffo954f1832014-10-11 18:27:30 +03002698 if (b->connector_allocator == 0)
Giulio Camuffo459137b2014-10-11 23:56:24 +03002699 weston_compositor_exit(b->compositor);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002700}
2701
2702static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002703udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002704{
David Herrmannd7488c22012-03-11 20:05:21 +01002705 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002706 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002707
2708 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002709 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002710 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002711
David Herrmann6ac52db2012-03-11 20:05:22 +01002712 val = udev_device_get_property_value(device, "HOTPLUG");
2713 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002714 return 0;
2715
David Herrmann6ac52db2012-03-11 20:05:22 +01002716 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002717}
2718
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002719static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002720udev_drm_event(int fd, uint32_t mask, void *data)
2721{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002722 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002723 struct udev_device *event;
2724
Giulio Camuffo954f1832014-10-11 18:27:30 +03002725 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002726
Giulio Camuffo954f1832014-10-11 18:27:30 +03002727 if (udev_event_is_hotplug(b, event))
2728 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002729
2730 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002731
2732 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002733}
2734
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002735static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002736drm_restore(struct weston_compositor *ec)
2737{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002738 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002739}
2740
2741static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002742drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002743{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002744 struct drm_backend *b = (struct drm_backend *) ec->backend;
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002745
Giulio Camuffo954f1832014-10-11 18:27:30 +03002746 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002747
Giulio Camuffo954f1832014-10-11 18:27:30 +03002748 wl_event_source_remove(b->udev_drm_source);
2749 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002750
Giulio Camuffo954f1832014-10-11 18:27:30 +03002751 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002752
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002753 weston_compositor_shutdown(ec);
2754
Giulio Camuffo954f1832014-10-11 18:27:30 +03002755 if (b->gbm)
2756 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002757
Giulio Camuffo954f1832014-10-11 18:27:30 +03002758 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002759
Giulio Camuffo954f1832014-10-11 18:27:30 +03002760 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002761 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002762}
2763
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002764static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002765drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002766{
2767 struct drm_output *output;
2768 struct drm_mode *drm_mode;
2769 int ret;
2770
Giulio Camuffo954f1832014-10-11 18:27:30 +03002771 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002772 if (!output->current) {
2773 /* If something that would cause the output to
2774 * switch mode happened while in another vt, we
2775 * might not have a current drm_fb. In that case,
2776 * schedule a repaint and let drm_output_repaint
2777 * handle setting the mode. */
2778 weston_output_schedule_repaint(&output->base);
2779 continue;
2780 }
2781
Hardeningff39efa2013-09-18 23:56:35 +02002782 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002783 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002784 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002785 &output->connector_id, 1,
2786 &drm_mode->mode_info);
2787 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002788 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002789 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002790 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002791 output->base.x, output->base.y);
2792 }
2793 }
2794}
2795
2796static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002797session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002798{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002799 struct weston_compositor *compositor = data;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800 struct drm_backend *b = (struct drm_backend *)compositor->backend;
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002801 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002802 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002803
Giulio Camuffo954f1832014-10-11 18:27:30 +03002804 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002805 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002806 compositor->state = b->prev_state;
2807 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002808 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002809 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002810 } else {
2811 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002812 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002813
Giulio Camuffo954f1832014-10-11 18:27:30 +03002814 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002815 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002816
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002817 /* If we have a repaint scheduled (either from a
2818 * pending pageflip or the idle handler), make sure we
2819 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002820 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002821 * further attemps at repainting. When we switch
2822 * back, we schedule a repaint, which will process
2823 * pending frame callbacks. */
2824
Giulio Camuffo954f1832014-10-11 18:27:30 +03002825 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002826 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002827 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002828 }
2829
Giulio Camuffo954f1832014-10-11 18:27:30 +03002830 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002831 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002832
Giulio Camuffo954f1832014-10-11 18:27:30 +03002833 wl_list_for_each(sprite, &b->sprite_list, link)
2834 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002835 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002836 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002837 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002838 };
2839}
2840
David Herrmann0af066f2012-10-29 19:21:16 +01002841/*
2842 * Find primary GPU
2843 * Some systems may have multiple DRM devices attached to a single seat. This
2844 * function loops over all devices and tries to find a PCI device with the
2845 * boot_vga sysfs attribute set to 1.
2846 * If no such device is found, the first DRM device reported by udev is used.
2847 */
2848static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002850{
2851 struct udev_enumerate *e;
2852 struct udev_list_entry *entry;
2853 const char *path, *device_seat, *id;
2854 struct udev_device *device, *drm_device, *pci;
2855
Giulio Camuffo954f1832014-10-11 18:27:30 +03002856 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002857 udev_enumerate_add_match_subsystem(e, "drm");
2858 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2859
2860 udev_enumerate_scan_devices(e);
2861 drm_device = NULL;
2862 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2863 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002864 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002865 if (!device)
2866 continue;
2867 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2868 if (!device_seat)
2869 device_seat = default_seat;
2870 if (strcmp(device_seat, seat)) {
2871 udev_device_unref(device);
2872 continue;
2873 }
2874
2875 pci = udev_device_get_parent_with_subsystem_devtype(device,
2876 "pci", NULL);
2877 if (pci) {
2878 id = udev_device_get_sysattr_value(pci, "boot_vga");
2879 if (id && !strcmp(id, "1")) {
2880 if (drm_device)
2881 udev_device_unref(drm_device);
2882 drm_device = device;
2883 break;
2884 }
2885 }
2886
2887 if (!drm_device)
2888 drm_device = device;
2889 else
2890 udev_device_unref(device);
2891 }
2892
2893 udev_enumerate_unref(e);
2894 return drm_device;
2895}
2896
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002897static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002898planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2899 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002900{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002901 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002902
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002903 switch (key) {
2904 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002905 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002906 break;
2907 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002908 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002909 break;
2910 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002911 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002912 break;
2913 default:
2914 break;
2915 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002916}
2917
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002918#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002919static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002920recorder_destroy(struct drm_output *output)
2921{
2922 vaapi_recorder_destroy(output->recorder);
2923 output->recorder = NULL;
2924
2925 output->base.disable_planes--;
2926
2927 wl_list_remove(&output->recorder_frame_listener.link);
2928 weston_log("[libva recorder] done\n");
2929}
2930
2931static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002932recorder_frame_notify(struct wl_listener *listener, void *data)
2933{
2934 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002935 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002936 int fd, ret;
2937
2938 output = container_of(listener, struct drm_output,
2939 recorder_frame_listener);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002940 b = (struct drm_backend *)output->base.compositor->backend;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002941
2942 if (!output->recorder)
2943 return;
2944
Giulio Camuffo954f1832014-10-11 18:27:30 +03002945 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002946 DRM_CLOEXEC, &fd);
2947 if (ret) {
2948 weston_log("[libva recorder] "
2949 "failed to create prime fd for front buffer\n");
2950 return;
2951 }
2952
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002953 ret = vaapi_recorder_frame(output->recorder, fd,
2954 output->current->stride);
2955 if (ret < 0) {
2956 weston_log("[libva recorder] aborted: %m\n");
2957 recorder_destroy(output);
2958 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002959}
2960
2961static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002962create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002963 const char *filename)
2964{
2965 int fd;
2966 drm_magic_t magic;
2967
Giulio Camuffo954f1832014-10-11 18:27:30 +03002968 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002969 if (fd < 0)
2970 return NULL;
2971
2972 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002973 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002974
2975 return vaapi_recorder_create(fd, width, height, filename);
2976}
2977
2978static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002979recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002980 void *data)
2981{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002982 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002983 struct drm_output *output;
2984 int width, height;
2985
Giulio Camuffo954f1832014-10-11 18:27:30 +03002986 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002987 struct drm_output, base.link);
2988
2989 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002990 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03002991 weston_log("failed to start vaapi recorder: "
2992 "output format not supported\n");
2993 return;
2994 }
2995
Hardeningff39efa2013-09-18 23:56:35 +02002996 width = output->base.current_mode->width;
2997 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002998
2999 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003000 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003001 if (!output->recorder) {
3002 weston_log("failed to create vaapi recorder\n");
3003 return;
3004 }
3005
3006 output->base.disable_planes++;
3007
3008 output->recorder_frame_listener.notify = recorder_frame_notify;
3009 wl_signal_add(&output->base.frame_signal,
3010 &output->recorder_frame_listener);
3011
3012 weston_output_schedule_repaint(&output->base);
3013
3014 weston_log("[libva recorder] initialized\n");
3015 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003016 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003017 }
3018}
3019#else
3020static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003021recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003022 void *data)
3023{
3024 weston_log("Compiled without libva support\n");
3025}
3026#endif
3027
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003028static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003029switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003030{
3031 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003032 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003033
Giulio Camuffo954f1832014-10-11 18:27:30 +03003034 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003035 return;
3036
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003037 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3038
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003039 weston_log("Switching to GL renderer\n");
3040
Giulio Camuffo954f1832014-10-11 18:27:30 +03003041 b->gbm = create_gbm_device(b->drm.fd);
3042 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003043 weston_log("Failed to create gbm device. "
3044 "Aborting renderer switch\n");
3045 return;
3046 }
3047
Giulio Camuffo954f1832014-10-11 18:27:30 +03003048 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003049 pixman_renderer_output_destroy(&output->base);
3050
Giulio Camuffo954f1832014-10-11 18:27:30 +03003051 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003052
Giulio Camuffo954f1832014-10-11 18:27:30 +03003053 if (drm_backend_create_gl_renderer(b) < 0) {
3054 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003055 weston_log("Failed to create GL renderer. Quitting.\n");
3056 /* FIXME: we need a function to shutdown cleanly */
3057 assert(0);
3058 }
3059
Giulio Camuffo954f1832014-10-11 18:27:30 +03003060 wl_list_for_each(output, &b->compositor->output_list, base.link)
3061 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003062
Giulio Camuffo954f1832014-10-11 18:27:30 +03003063 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003064
3065 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3066 if (linux_dmabuf_setup(b->compositor) < 0)
3067 weston_log("Error: initializing dmabuf "
3068 "support failed.\n");
3069 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003070}
3071
3072static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003073renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3074 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003075{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003076 struct drm_backend *b =
3077 (struct drm_backend *) keyboard->seat->compositor;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003078
Giulio Camuffo954f1832014-10-11 18:27:30 +03003079 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003080}
3081
Giulio Camuffo954f1832014-10-11 18:27:30 +03003082static struct drm_backend *
3083drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003084 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003085{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003086 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003087 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003088 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003089 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003090 const char *seat_id = default_seat;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003091
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003092 weston_log("initializing drm backend\n");
3093
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 b = zalloc(sizeof *b);
3095 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003096 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003097
Pekka Paalanen68583832015-05-19 09:53:16 +03003098 /*
3099 * KMS support for hardware planes cannot properly synchronize
3100 * without nuclear page flip. Without nuclear/atomic, hw plane
3101 * and cursor plane updates would either tear or cause extra
3102 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003103 * to a fraction. For cursors, it's not so bad, so they are
3104 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003105 *
3106 * These can be enabled again when nuclear/atomic support lands.
3107 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003108 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003109 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003110 b->use_pixman = config->use_pixman;
3111 b->configure_output = config->configure_output;
3112 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003113
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003114 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3115 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003116
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003117 if (config->seat_id)
3118 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003119
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003120 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003121 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3122 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003123 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003124 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003125 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003126 goto err_compositor;
3127 }
3128
Giulio Camuffo954f1832014-10-11 18:27:30 +03003129 b->udev = udev_new();
3130 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003131 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003132 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003133 }
3134
Giulio Camuffo954f1832014-10-11 18:27:30 +03003135 b->session_listener.notify = session_notify;
3136 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003137
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003138 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003139 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003140 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003141 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003142 }
David Herrmann0af066f2012-10-29 19:21:16 +01003143 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003144
Giulio Camuffo954f1832014-10-11 18:27:30 +03003145 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003146 weston_log("failed to initialize kms\n");
3147 goto err_udev_dev;
3148 }
3149
Giulio Camuffo954f1832014-10-11 18:27:30 +03003150 if (b->use_pixman) {
3151 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003152 weston_log("failed to initialize pixman renderer\n");
3153 goto err_udev_dev;
3154 }
3155 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003156 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003157 weston_log("failed to initialize egl\n");
3158 goto err_udev_dev;
3159 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003160 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003161
Giulio Camuffo954f1832014-10-11 18:27:30 +03003162 b->base.destroy = drm_destroy;
3163 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003164
Giulio Camuffo954f1832014-10-11 18:27:30 +03003165 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003166
Bob Ham91880f12016-01-12 10:21:47 +00003167 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003168
Giulio Camuffo954f1832014-10-11 18:27:30 +03003169 wl_list_init(&b->sprite_list);
3170 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003171
Giulio Camuffo954f1832014-10-11 18:27:30 +03003172 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003173 compositor, b->udev, seat_id,
3174 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003175 weston_log("failed to create input devices\n");
3176 goto err_sprite;
3177 }
3178
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003179 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003180 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003181 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003182 }
3183
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003184 /* A this point we have some idea of whether or not we have a working
3185 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003186 if (!b->cursors_are_broken)
3187 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003188
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003189 path = NULL;
3190
Giulio Camuffo954f1832014-10-11 18:27:30 +03003191 loop = wl_display_get_event_loop(compositor->wl_display);
3192 b->drm_source =
3193 wl_event_loop_add_fd(loop, b->drm.fd,
3194 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003195
Giulio Camuffo954f1832014-10-11 18:27:30 +03003196 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3197 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003198 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003199 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003200 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003202 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003203 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003204 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003205 udev_monitor_get_fd(b->udev_monitor),
3206 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003207
Giulio Camuffo954f1832014-10-11 18:27:30 +03003208 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003209 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003210 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003211 }
3212
Daniel Stonea96b93c2012-06-22 14:04:37 +01003213 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003214
Giulio Camuffo954f1832014-10-11 18:27:30 +03003215 weston_compositor_add_debug_binding(compositor, KEY_O,
3216 planes_binding, b);
3217 weston_compositor_add_debug_binding(compositor, KEY_C,
3218 planes_binding, b);
3219 weston_compositor_add_debug_binding(compositor, KEY_V,
3220 planes_binding, b);
3221 weston_compositor_add_debug_binding(compositor, KEY_Q,
3222 recorder_binding, b);
3223 weston_compositor_add_debug_binding(compositor, KEY_W,
3224 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003225
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003226 if (compositor->renderer->import_dmabuf) {
3227 if (linux_dmabuf_setup(compositor) < 0)
3228 weston_log("Error: initializing dmabuf "
3229 "support failed.\n");
3230 }
3231
Giulio Camuffo954f1832014-10-11 18:27:30 +03003232 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003233
Giulio Camuffo954f1832014-10-11 18:27:30 +03003234 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003235
3236err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003237 wl_event_source_remove(b->udev_drm_source);
3238 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003239err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003240 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003241err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003242 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003243err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003244 if (b->gbm)
3245 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003246 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003247err_udev_dev:
3248 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003249err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003250 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003251err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003252 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003253err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003255 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003256 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003257}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003258
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003259static void
3260config_init_to_defaults(struct weston_drm_backend_config *config)
3261{
3262}
3263
Giulio Camuffo954f1832014-10-11 18:27:30 +03003264WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003265backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003266 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003267{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003268 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003269 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003270
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003271 if (config_base == NULL ||
3272 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3273 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3274 weston_log("drm backend config structure is invalid\n");
3275 return -1;
3276 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003277
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003278 config_init_to_defaults(&config);
3279 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003280
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003281 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003282 if (b == NULL)
3283 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003284
Giulio Camuffo954f1832014-10-11 18:27:30 +03003285 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003286}