blob: c7f7f63691ffb25c2b3053127b2fa50ee9d3b41c [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"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070057#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100058#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010059#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030060#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020061#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030062#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040063
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030064#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
65#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
66#endif
67
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030068#ifndef DRM_CAP_CURSOR_WIDTH
69#define DRM_CAP_CURSOR_WIDTH 0x8
70#endif
71
72#ifndef DRM_CAP_CURSOR_HEIGHT
73#define DRM_CAP_CURSOR_HEIGHT 0x9
74#endif
75
76#ifndef GBM_BO_USE_CURSOR
77#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
78#endif
79
Giulio Camuffo954f1832014-10-11 18:27:30 +030080struct drm_backend {
81 struct weston_backend base;
82 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040083
84 struct udev *udev;
85 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040086
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010087 struct udev_monitor *udev_monitor;
88 struct wl_event_source *udev_drm_source;
89
Benjamin Franzke2af7f102011-03-02 11:14:59 +010090 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010091 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030093 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010094 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020095 struct gbm_device *gbm;
Marty Jack13d9db22011-02-09 19:01:42 -050096 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010097 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000105 int min_width, max_width;
106 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Rob Bradfordd355b802013-05-31 18:09:55 +0100117 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300118
Daniel Stone70d337d2015-06-16 18:42:23 +0100119 int32_t cursor_width;
120 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700121
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700122 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400123};
124
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400125struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500126 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400127 drmModeModeInfo mode_info;
128};
129
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300130struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200131 uint32_t fb_id, stride, handle, size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000132 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200133 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300134 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200135 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200136
137 /* Used by gbm fbs */
138 struct gbm_bo *bo;
139
140 /* Used by dumb fbs */
141 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300142};
143
Richard Hughes2b2092a2013-04-24 14:58:02 +0100144struct drm_edid {
145 char eisa_id[13];
146 char monitor_name[13];
147 char pnp_id[5];
148 char serial_number[13];
149};
150
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400151struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200152 struct weston_output base;
153 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400154
Daniel Stone17339232015-11-28 12:09:47 +0000155 uint32_t crtc_id; /* object ID to pass to DRM functions */
156 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400157 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700158 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100159 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300160 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100161 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200162
Daniel Stone36609c72015-06-18 07:49:02 +0100163 enum dpms_enum dpms;
164
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300165 int vblank_pending;
166 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800167 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200168 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300169
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100170 struct gbm_surface *gbm_surface;
171 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400172 struct weston_plane cursor_plane;
173 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500174 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400175 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300176 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200177 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200178
179 struct drm_fb *dumb[2];
180 pixman_image_t *image[2];
181 int current_image;
182 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300183
184 struct vaapi_recorder *recorder;
185 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400186};
187
Jesse Barnes58ef3792012-02-23 09:45:49 -0500188/*
189 * An output has a primary display plane plus zero or more sprites for
190 * blending display contents.
191 */
192struct drm_sprite {
193 struct wl_list link;
194
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400195 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500196
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200197 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300198 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300199 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500200
Jesse Barnes58ef3792012-02-23 09:45:49 -0500201 uint32_t possible_crtcs;
202 uint32_t plane_id;
203 uint32_t count_formats;
204
205 int32_t src_x, src_y;
206 uint32_t src_w, src_h;
207 uint32_t dest_x, dest_y;
208 uint32_t dest_w, dest_h;
209
210 uint32_t formats[];
211};
212
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300213static struct gl_renderer_interface *gl_renderer;
214
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500215static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400216
Armin Krezović545dba62016-08-05 15:54:18 +0200217static inline struct drm_output *
218to_drm_output(struct weston_output *base)
219{
220 return container_of(base, struct drm_output, base);
221}
222
223static inline struct drm_backend *
224to_drm_backend(struct weston_compositor *base)
225{
226 return container_of(base->backend, struct drm_backend, base);
227}
228
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400229static void
230drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400231
Mario Kleinerf507ec32015-06-21 21:25:14 +0200232static void
233drm_output_update_msc(struct drm_output *output, unsigned int seq);
234
Jesse Barnes58ef3792012-02-23 09:45:49 -0500235static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000236drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500237{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000238 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500239}
240
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300241static void
242drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
243{
244 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300245
246 if (fb->fb_id)
Daniel Stone4e5eceb2015-11-30 16:07:19 +0000247 drmModeRmFB(fb->fd, fb->fb_id);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300248
Pekka Paalanende685b82012-12-04 15:58:12 +0200249 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300250
251 free(data);
252}
253
254static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000255drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300256 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200257{
258 struct drm_fb *fb;
259 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300260 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200261
262 struct drm_mode_create_dumb create_arg;
263 struct drm_mode_destroy_dumb destroy_arg;
264 struct drm_mode_map_dumb map_arg;
265
Peter Huttererf3d62272013-08-08 11:57:05 +1000266 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200267 if (!fb)
268 return NULL;
269
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300270 switch (format) {
271 case GBM_FORMAT_XRGB8888:
272 bpp = 32;
273 depth = 24;
274 break;
275 case GBM_FORMAT_RGB565:
276 bpp = depth = 16;
277 break;
278 default:
279 return NULL;
280 }
281
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700282 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300283 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200284 create_arg.width = width;
285 create_arg.height = height;
286
Giulio Camuffo954f1832014-10-11 18:27:30 +0300287 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200288 if (ret)
289 goto err_fb;
290
291 fb->handle = create_arg.handle;
292 fb->stride = create_arg.pitch;
293 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000294 fb->width = width;
295 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300296 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200297
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300298 ret = -1;
299
300 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700301 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300302
303 handles[0] = fb->handle;
304 pitches[0] = fb->stride;
305 offsets[0] = 0;
306
307 ret = drmModeAddFB2(b->drm.fd, width, height,
308 format, handles, pitches, offsets,
309 &fb->fb_id, 0);
310 if (ret) {
311 weston_log("addfb2 failed: %m\n");
312 b->no_addfb2 = 1;
313 }
314 }
315
316 if (ret) {
317 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
318 fb->stride, fb->handle, &fb->fb_id);
319 }
320
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200321 if (ret)
322 goto err_bo;
323
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700324 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200325 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400326 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200327 if (ret)
328 goto err_add_fb;
329
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500330 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300331 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200332 if (fb->map == MAP_FAILED)
333 goto err_add_fb;
334
335 return fb;
336
337err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200339err_bo:
340 memset(&destroy_arg, 0, sizeof(destroy_arg));
341 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300342 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200343err_fb:
344 free(fb);
345 return NULL;
346}
347
348static void
349drm_fb_destroy_dumb(struct drm_fb *fb)
350{
351 struct drm_mode_destroy_dumb destroy_arg;
352
353 if (!fb->map)
354 return;
355
356 if (fb->fb_id)
357 drmModeRmFB(fb->fd, fb->fb_id);
358
359 weston_buffer_reference(&fb->buffer_ref, NULL);
360
361 munmap(fb->map, fb->size);
362
363 memset(&destroy_arg, 0, sizeof(destroy_arg));
364 destroy_arg.handle = fb->handle;
365 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
366
367 free(fb);
368}
369
370static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500371drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300372 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300373{
374 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500375 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300376 int ret;
377
378 if (fb)
379 return fb;
380
Bryce Harringtonde16d892014-11-20 22:21:57 -0800381 fb = zalloc(sizeof *fb);
382 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200383 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300384
385 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300386
Daniel Stonec8c917c2016-11-14 17:45:58 +0000387 fb->width = gbm_bo_get_width(bo);
388 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200389 fb->stride = gbm_bo_get_stride(bo);
390 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000391 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300392 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300393
Daniel Stonec8c917c2016-11-14 17:45:58 +0000394 if (backend->min_width > fb->width ||
395 fb->width > backend->max_width ||
396 backend->min_height > fb->height ||
397 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200398 weston_log("bo geometry out of bounds\n");
399 goto err_free;
400 }
401
402 ret = -1;
403
Giulio Camuffo954f1832014-10-11 18:27:30 +0300404 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200405 handles[0] = fb->handle;
406 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200407 offsets[0] = 0;
408
Daniel Stonec8c917c2016-11-14 17:45:58 +0000409 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200410 format, handles, pitches, offsets,
411 &fb->fb_id, 0);
412 if (ret) {
413 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300414 backend->no_addfb2 = 1;
415 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200416 }
417 }
418
419 if (ret)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000420 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
421 24, 32, fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200422
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300423 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200424 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200425 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300426 }
427
428 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
429
430 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200431
432err_free:
433 free(fb);
434 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300435}
436
437static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500438drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200439{
Pekka Paalanende685b82012-12-04 15:58:12 +0200440 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200441
442 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200443
Pekka Paalanende685b82012-12-04 15:58:12 +0200444 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200445}
446
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200447static void
448drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
449{
450 if (!fb)
451 return;
452
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200453 if (fb->map &&
454 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200455 drm_fb_destroy_dumb(fb);
456 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200457 if (fb->is_client_buffer)
458 gbm_bo_destroy(fb->bo);
459 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100460 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600461 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200462 }
463}
464
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500465static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200466drm_output_check_scanout_format(struct drm_output *output,
467 struct weston_surface *es, struct gbm_bo *bo)
468{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200469 uint32_t format;
470 pixman_region32_t r;
471
472 format = gbm_bo_get_format(bo);
473
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700474 if (format == GBM_FORMAT_ARGB8888) {
475 /* We can scanout an ARGB buffer if the surface's
476 * opaque region covers the whole output, but we have
477 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800478 pixman_region32_init_rect(&r, 0, 0,
479 output->base.width,
480 output->base.height);
481 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200482
483 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500484 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200485
486 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500487 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700488
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100489 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700490 return format;
491
492 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200493}
494
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400495static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200496drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500498{
Armin Krezović545dba62016-08-05 15:54:18 +0200499 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200501 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300502 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500503 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500504
Daniel Stone296d7a92016-10-21 18:05:37 +0100505 /* We use GBM to import buffers. */
506 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400507 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500508
Daniel Stone296d7a92016-10-21 18:05:37 +0100509 if (buffer == NULL)
510 return NULL;
511
512 /* Make sure our view is exactly compatible with the output. */
513 if (ev->geometry.x != output->base.x ||
514 ev->geometry.y != output->base.y)
515 return NULL;
516 if (ev->transform.enabled)
517 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +0200518 if (ev->geometry.scissor_enabled)
519 return NULL;
520
Daniel Stone296d7a92016-10-21 18:05:37 +0100521 if (buffer->width != output->base.current_mode->width ||
522 buffer->height != output->base.current_mode->height)
523 return NULL;
524 if (viewport->buffer.transform != output->base.transform)
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{
Armin Krezović545dba62016-08-05 15:54:18 +0200554 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300555 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400556
Giulio Camuffo954f1832014-10-11 18:27:30 +0300557 output->base.compositor->renderer->repaint_output(&output->base,
558 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400559
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100560 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300561 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200562 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400563 return;
564 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300565
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100566 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300567 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200568 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100569 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300570 return;
571 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400572}
573
574static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200575drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
576{
577 struct weston_compositor *ec = output->base.compositor;
578 pixman_region32_t total_damage, previous_damage;
579
580 pixman_region32_init(&total_damage);
581 pixman_region32_init(&previous_damage);
582
583 pixman_region32_copy(&previous_damage, damage);
584
585 pixman_region32_union(&total_damage, damage, &output->previous_damage);
586 pixman_region32_copy(&output->previous_damage, &previous_damage);
587
588 output->current_image ^= 1;
589
590 output->next = output->dumb[output->current_image];
591 pixman_renderer_output_set_buffer(&output->base,
592 output->image[output->current_image]);
593
594 ec->renderer->repaint_output(&output->base, &total_damage);
595
596 pixman_region32_fini(&total_damage);
597 pixman_region32_fini(&previous_damage);
598}
599
600static void
601drm_output_render(struct drm_output *output, pixman_region32_t *damage)
602{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200604 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200605
Giulio Camuffo954f1832014-10-11 18:27:30 +0300606 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200607 drm_output_render_pixman(output, damage);
608 else
609 drm_output_render_gl(output, damage);
610
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 pixman_region32_subtract(&c->primary_plane.damage,
612 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200613}
614
615static void
Richard Hughese7299962013-05-01 21:52:12 +0100616drm_output_set_gamma(struct weston_output *output_base,
617 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
618{
619 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200620 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300621 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200622 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100623
624 /* check */
625 if (output_base->gamma_size != size)
626 return;
627 if (!output->original_crtc)
628 return;
629
Giulio Camuffo954f1832014-10-11 18:27:30 +0300630 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100631 output->crtc_id,
632 size, r, g, b);
633 if (rc)
634 weston_log("set gamma failed: %m\n");
635}
636
Bryce Harringtonada4f072015-06-30 13:25:46 -0700637/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200638 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700639 * The pipe parameter indicates which CRTC is in use. Knowing this, we
640 * can determine which vblank sequence type to use for it. Traditional
641 * cards had only two CRTCs, with CRTC 0 using no special flags, and
642 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
643 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200644 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700645 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
646 * 0-31. If this is non-zero it indicates we're dealing with a
647 * multi-gpu situation and we need to calculate the vblank sync
648 * using DRM_BLANK_HIGH_CRTC_MASK.
649 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300650static unsigned int
651drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200652{
653 if (output->pipe > 1)
654 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
655 DRM_VBLANK_HIGH_CRTC_MASK;
656 else if (output->pipe > 0)
657 return DRM_VBLANK_SECONDARY;
658 else
659 return 0;
660}
661
David Herrmann1edf44c2013-10-22 17:11:26 +0200662static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500663drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400664 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100665{
Armin Krezović545dba62016-08-05 15:54:18 +0200666 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300667 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200668 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500669 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400670 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500671 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100672
Armin Krezović08368132016-09-30 14:11:05 +0200673 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200674 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800675
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300676 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400677 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300678 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200679 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100680
Hardeningff39efa2013-09-18 23:56:35 +0200681 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200682 if (!output->current ||
683 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300684 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300685 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400686 &output->connector_id, 1,
687 &mode->mode_info);
688 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200689 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200690 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400691 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300692 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200693 }
694
Giulio Camuffo954f1832014-10-11 18:27:30 +0300695 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300696 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500697 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200698 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200699 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500700 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100701
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300702 output->page_flip_pending = 1;
703
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400704 drm_output_set_cursor(output);
705
Jesse Barnes58ef3792012-02-23 09:45:49 -0500706 /*
707 * Now, update all the sprite surfaces
708 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300709 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200710 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500711 drmVBlank vbl = {
712 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
713 .request.sequence = 1,
714 };
715
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200716 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000717 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500718 continue;
719
Giulio Camuffo954f1832014-10-11 18:27:30 +0300720 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200721 fb_id = s->next->fb_id;
722
Giulio Camuffo954f1832014-10-11 18:27:30 +0300723 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200724 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500725 s->dest_x, s->dest_y,
726 s->dest_w, s->dest_h,
727 s->src_x, s->src_y,
728 s->src_w, s->src_h);
729 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200730 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500731 ret, strerror(errno));
732
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200733 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500734
Jesse Barnes58ef3792012-02-23 09:45:49 -0500735 /*
736 * Queue a vblank signal so we know when the surface
737 * becomes active on the display or has been replaced.
738 */
739 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300740 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500741 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200742 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500743 ret, strerror(errno));
744 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300745
746 s->output = output;
747 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500748 }
749
David Herrmann1edf44c2013-10-22 17:11:26 +0200750 return 0;
751
752err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800753 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200754 if (output->next) {
755 drm_output_release_fb(output, output->next);
756 output->next = NULL;
757 }
758
759 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400760}
761
762static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200763drm_output_start_repaint_loop(struct weston_output *output_base)
764{
Armin Krezović545dba62016-08-05 15:54:18 +0200765 struct drm_output *output = to_drm_output(output_base);
766 struct drm_backend *backend =
767 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200768 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200769 struct timespec ts, tnow;
770 struct timespec vbl2now;
771 int64_t refresh_nsec;
772 int ret;
773 drmVBlank vbl = {
774 .request.type = DRM_VBLANK_RELATIVE,
775 .request.sequence = 0,
776 .request.signal = 0,
777 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300778
Armin Krezović08368132016-09-30 14:11:05 +0200779 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800780 return;
781
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300782 if (!output->current) {
783 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200784 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300785 }
786
Mario Kleinerf507ec32015-06-21 21:25:14 +0200787 /* Try to get current msc and timestamp via instant query */
788 vbl.request.type |= drm_waitvblank_pipe(output);
789 ret = drmWaitVBlank(backend->drm.fd, &vbl);
790
791 /* Error ret or zero timestamp means failure to get valid timestamp */
792 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
793 ts.tv_sec = vbl.reply.tval_sec;
794 ts.tv_nsec = vbl.reply.tval_usec * 1000;
795
796 /* Valid timestamp for most recent vblank - not stale?
797 * Stale ts could happen on Linux 3.17+, so make sure it
798 * is not older than 1 refresh duration since now.
799 */
800 weston_compositor_read_presentation_clock(backend->compositor,
801 &tnow);
802 timespec_sub(&vbl2now, &tnow, &ts);
803 refresh_nsec =
804 millihz_to_nsec(output->base.current_mode->refresh);
805 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
806 drm_output_update_msc(output, vbl.reply.sequence);
807 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200808 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200809 return;
810 }
811 }
812
813 /* Immediate query didn't provide valid timestamp.
814 * Use pageflip fallback.
815 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300816 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200817
Giulio Camuffo954f1832014-10-11 18:27:30 +0300818 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200819 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
820 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200821 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200822 }
David Herrmann3c688c52013-10-22 17:11:25 +0200823
824 return;
825
826finish_frame:
827 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300828 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200829 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200830 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200831}
832
833static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400834drm_output_update_msc(struct drm_output *output, unsigned int seq)
835{
836 uint64_t msc_hi = output->base.msc >> 32;
837
838 if (seq < (output->base.msc & 0xffffffff))
839 msc_hi++;
840
841 output->base.msc = (msc_hi << 32) + seq;
842}
843
844static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500845vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
846 void *data)
847{
848 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300849 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400850 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200851 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
852 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300853
Pekka Paalanen641307c2014-09-23 22:08:47 -0400854 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300855 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500856
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200857 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200858 s->current = s->next;
859 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300860
861 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400862 ts.tv_sec = sec;
863 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200864 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300865 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500866}
867
868static void
Armin Krezović08368132016-09-30 14:11:05 +0200869drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800870
871static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400872page_flip_handler(int fd, unsigned int frame,
873 unsigned int sec, unsigned int usec, void *data)
874{
Armin Krezović545dba62016-08-05 15:54:18 +0200875 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400876 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200877 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
878 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
879 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400880
Pekka Paalanen641307c2014-09-23 22:08:47 -0400881 drm_output_update_msc(output, frame);
882
Jonas Ådahle5a12252013-04-05 23:07:11 +0200883 /* We don't set page_flip_pending on start_repaint_loop, in that case
884 * we just want to page flip to the current buffer to get an accurate
885 * timestamp */
886 if (output->page_flip_pending) {
887 drm_output_release_fb(output, output->current);
888 output->current = output->next;
889 output->next = NULL;
890 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300891
Jonas Ådahle5a12252013-04-05 23:07:11 +0200892 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400893
Xiong Zhangabd5d472013-10-11 14:43:07 +0800894 if (output->destroy_pending)
895 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +0200896 else if (output->disable_pending)
897 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800898 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400899 ts.tv_sec = sec;
900 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200901 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300902
903 /* We can't call this from frame_notify, because the output's
904 * repaint needed flag is cleared just after that */
905 if (output->recorder)
906 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300907 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200908}
909
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500910static uint32_t
911drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500913{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500914 uint32_t i, format;
915
916 format = gbm_bo_get_format(bo);
917
918 if (format == GBM_FORMAT_ARGB8888) {
919 pixman_region32_t r;
920
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500921 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600922 ev->surface->width,
923 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500925
926 if (!pixman_region32_not_empty(&r))
927 format = GBM_FORMAT_XRGB8888;
928
929 pixman_region32_fini(&r);
930 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500931
932 for (i = 0; i < s->count_formats; i++)
933 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500934 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500935
936 return 0;
937}
938
939static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500941{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 return !ev->transform.enabled ||
943 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500944}
945
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400946static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200947drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500949{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200950 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200951 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200952 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300953 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500954 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300955 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500956 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500957 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500958 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200959 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400961 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500962
Giulio Camuffo954f1832014-10-11 18:27:30 +0300963 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400964 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500965
Daniel Stone296d7a92016-10-21 18:05:37 +0100966 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200967 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400968 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300969
Daniel Stone296d7a92016-10-21 18:05:37 +0100970 /* We can only import GBM buffers. */
971 if (b->gbm == NULL)
972 return NULL;
973
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400975 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300976 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300977 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500978 return NULL;
979
Daniel Stone296d7a92016-10-21 18:05:37 +0100980 if (viewport->buffer.transform != output->base.transform)
981 return NULL;
982 if (viewport->buffer.scale != output->base.current_scale)
983 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400985 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500986
Daniel Stone296d7a92016-10-21 18:05:37 +0100987 if (ev->alpha != 1.0f)
988 return NULL;
989
Giulio Camuffo954f1832014-10-11 18:27:30 +0300990 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +0000991 if (!drm_sprite_crtc_supported(output, s))
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{
Armin Krezović545dba62016-08-05 15:54:18 +02001122 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001123 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001124 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001125
Giulio Camuffo954f1832014-10-11 18:27:30 +03001126 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001127 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001128
1129 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001130 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001131
1132 /* Don't import buffers which span multiple outputs. */
1133 if (ev->output_mask != (1u << output->base.id))
1134 return NULL;
1135
1136 /* We use GBM to import SHM buffers. */
1137 if (b->gbm == NULL)
1138 return NULL;
1139
Derek Foreman6c19b692015-12-03 14:07:12 -06001140 if (ev->surface->buffer_ref.buffer == NULL)
1141 return NULL;
1142 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1143 if (!shmbuf)
1144 return NULL;
1145 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1146 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001147
1148 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1149 return NULL;
1150 if (ev->transform.enabled &&
1151 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1152 return NULL;
1153 if (viewport->buffer.scale != output->base.current_scale)
1154 return NULL;
1155 if (ev->geometry.scissor_enabled)
1156 return NULL;
1157
Derek Foreman6c19b692015-12-03 14:07:12 -06001158 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001159 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001160 return NULL;
1161
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001163
1164 return &output->cursor_plane;
1165}
1166
Pekka Paalanend0ead482014-06-16 12:05:40 +03001167/**
1168 * Update the image for the current cursor surface
1169 *
1170 * @param b DRM backend structure
1171 * @param bo GBM buffer object to write into
1172 * @param ev View to use for cursor image
1173 */
1174static void
1175cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1176 struct weston_view *ev)
1177{
1178 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1179 uint32_t buf[b->cursor_width * b->cursor_height];
1180 int32_t stride;
1181 uint8_t *s;
1182 int i;
1183
1184 assert(buffer && buffer->shm_buffer);
1185 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1186 assert(ev->surface->width <= b->cursor_width);
1187 assert(ev->surface->height <= b->cursor_height);
1188
1189 memset(buf, 0, sizeof buf);
1190 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1191 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1192
1193 wl_shm_buffer_begin_access(buffer->shm_buffer);
1194 for (i = 0; i < ev->surface->height; i++)
1195 memcpy(buf + i * b->cursor_width,
1196 s + i * stride,
1197 ev->surface->width * 4);
1198 wl_shm_buffer_end_access(buffer->shm_buffer);
1199
1200 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1201 weston_log("failed update cursor: %m\n");
1202}
1203
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001204static void
1205drm_output_set_cursor(struct drm_output *output)
1206{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001208 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001209 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001210 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001211 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001212 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 output->cursor_view = NULL;
1215 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001216 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001217 output->cursor_plane.x = INT32_MIN;
1218 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001219 return;
1220 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001221
Neil Robertse5051712013-11-13 15:44:06 +00001222 buffer = ev->surface->buffer_ref.buffer;
1223
1224 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001225 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001226 pixman_region32_fini(&output->cursor_plane.damage);
1227 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001228 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001229 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001230
Pekka Paalanend0ead482014-06-16 12:05:40 +03001231 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001232 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001233 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1234 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001235 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001236 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001237 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001238 }
1239
Derek Foremanbe428b32015-11-24 11:39:38 -06001240 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001241
1242 /* From global to output space, output transform is guaranteed to be
1243 * NORMAL by drm_output_prepare_cursor_view().
1244 */
1245 x = (x - output->base.x) * output->base.current_scale;
1246 y = (y - output->base.y) * output->base.current_scale;
1247
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001248 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001249 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001250 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001251 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001252 }
1253
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001254 output->cursor_plane.x = x;
1255 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001256 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001257}
1258
Jesse Barnes58ef3792012-02-23 09:45:49 -05001259static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001260drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001261{
Armin Krezović545dba62016-08-05 15:54:18 +02001262 struct drm_backend *b = to_drm_backend(output_base->compositor);
1263 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001265 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001266 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001267
1268 /*
1269 * Find a surface for each sprite in the output using some heuristics:
1270 * 1) size
1271 * 2) frequency of update
1272 * 3) opacity (though some hw might support alpha blending)
1273 * 4) clipping (this can be fixed with color keys)
1274 *
1275 * The idea is to save on blitting since this should save power.
1276 * If we can get a large video surface on the sprite for example,
1277 * the main display surface may not need to update at all, and
1278 * the client buffer can be used directly for the sprite surface
1279 * as we do for flipping full screen surfaces.
1280 */
1281 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001282 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283
Giulio Camuffo954f1832014-10-11 18:27:30 +03001284 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001285 struct weston_surface *es = ev->surface;
1286
1287 /* Test whether this buffer can ever go into a plane:
1288 * non-shm, or small enough to be a cursor.
1289 *
1290 * Also, keep a reference when using the pixman renderer.
1291 * That makes it possible to do a seamless switch to the GL
1292 * renderer and since the pixman renderer keeps a reference
1293 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001294 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001295 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001296 (es->buffer_ref.buffer &&
1297 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001298 (ev->surface->width <= b->cursor_width &&
1299 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001300 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001301 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001302 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001303
Jesse Barnes58ef3792012-02-23 09:45:49 -05001304 pixman_region32_init(&surface_overlap);
1305 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001307
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001308 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001309 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001310 next_plane = primary;
1311 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001313 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001315 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001317 if (next_plane == NULL)
1318 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001321
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001322 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001323 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001325
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001326 if (next_plane == primary ||
1327 next_plane == &output->cursor_plane) {
1328 /* cursor plane involves a copy */
1329 ev->psf_flags = 0;
1330 } else {
1331 /* All other planes are a direct scanout of a
1332 * single client buffer.
1333 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001334 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001335 }
1336
Jesse Barnes58ef3792012-02-23 09:45:49 -05001337 pixman_region32_fini(&surface_overlap);
1338 }
1339 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001340}
1341
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001342/**
1343 * Find the closest-matching mode for a given target
1344 *
1345 * Given a target mode, find the most suitable mode amongst the output's
1346 * current mode list to use, preferring the current mode if possible, to
1347 * avoid an expensive mode switch.
1348 *
1349 * @param output DRM output
1350 * @param target_mode Mode to attempt to match
1351 * @returns Pointer to a mode from the output's mode list
1352 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001353static struct drm_mode *
1354choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1355{
1356 struct drm_mode *tmp_mode = NULL, *mode;
1357
Hardeningff39efa2013-09-18 23:56:35 +02001358 if (output->base.current_mode->width == target_mode->width &&
1359 output->base.current_mode->height == target_mode->height &&
1360 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001361 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001362 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001363
1364 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1365 if (mode->mode_info.hdisplay == target_mode->width &&
1366 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001367 if (mode->base.refresh == target_mode->refresh ||
1368 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001369 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001370 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001371 tmp_mode = mode;
1372 }
1373 }
1374
1375 return tmp_mode;
1376}
1377
1378static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001379drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001380static void
1381drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001382static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001383drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001384static void
1385drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001386
1387static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001388drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1389{
1390 struct drm_output *output;
1391 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001392 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001393
1394 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001395 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001396 return -1;
1397 }
1398
1399 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001400 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001401 return -1;
1402 }
1403
Armin Krezović545dba62016-08-05 15:54:18 +02001404 b = to_drm_backend(output_base->compositor);
1405 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001406 drm_mode = choose_mode (output, mode);
1407
1408 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001409 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001410 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001411 }
1412
Hardeningff39efa2013-09-18 23:56:35 +02001413 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001414 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001415
Hardeningff39efa2013-09-18 23:56:35 +02001416 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001417
Hardeningff39efa2013-09-18 23:56:35 +02001418 output->base.current_mode = &drm_mode->base;
1419 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001420 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1421
Alex Wub7b8bda2012-04-17 17:20:48 +08001422 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001423 drm_output_release_fb(output, output->current);
1424 drm_output_release_fb(output, output->next);
1425 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001426
Giulio Camuffo954f1832014-10-11 18:27:30 +03001427 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001428 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001429 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001430 weston_log("failed to init output pixman state with "
1431 "new mode\n");
1432 return -1;
1433 }
1434 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001435 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001436 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001437 weston_log("failed to init output egl state with "
1438 "new mode");
1439 return -1;
1440 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001441 }
1442
Alex Wub7b8bda2012-04-17 17:20:48 +08001443 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001444}
1445
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001446static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001447on_drm_input(int fd, uint32_t mask, void *data)
1448{
1449 drmEventContext evctx;
1450
1451 memset(&evctx, 0, sizeof evctx);
1452 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1453 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001454 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001455 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001456
1457 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001458}
1459
1460static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001461init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001462{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001463 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001464 uint64_t cap;
1465 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001466 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001467
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001468 sysnum = udev_device_get_sysnum(device);
1469 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001470 b->drm.id = atoi(sysnum);
1471 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001472 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001473 return -1;
1474 }
1475
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001476 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001477 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001478 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001479 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001480 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001481 udev_device_get_devnode(device));
1482 return -1;
1483 }
1484
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001485 weston_log("using %s\n", filename);
1486
Giulio Camuffo954f1832014-10-11 18:27:30 +03001487 b->drm.fd = fd;
1488 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001489
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001490 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1491 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001492 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001493 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001494 clk_id = CLOCK_REALTIME;
1495
Giulio Camuffo954f1832014-10-11 18:27:30 +03001496 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001497 weston_log("Error: failed to set presentation clock %d.\n",
1498 clk_id);
1499 return -1;
1500 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001501
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001502 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1503 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001504 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001505 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001506 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001507
1508 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1509 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001510 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001511 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001512 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001513
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001514 return 0;
1515}
1516
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001517static struct gbm_device *
1518create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001519{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001520 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001521
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001522 gl_renderer = weston_load_module("gl-renderer.so",
1523 "gl_renderer_interface");
1524 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001525 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001526
1527 /* GBM will load a dri driver, but even though they need symbols from
1528 * libglapi, in some version of Mesa they are not linked to it. Since
1529 * only the gl-renderer module links to it, the call above won't make
1530 * these symbols globally available, and loading the DRI driver fails.
1531 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1532 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1533
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001534 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001535
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001536 return gbm;
1537}
1538
Bryce Harringtonc056a982015-05-19 15:25:18 -07001539/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001540 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001541 *
1542 * This returns 0 if substitution isn't possible, but 0 might be a
1543 * legitimate format for other EGL platforms, so the caller is
1544 * responsible for checking for 0 before calling gl_renderer->create().
1545 *
1546 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1547 * but it's entirely possible we'll see this again on other implementations.
1548 */
1549static int
1550fallback_format_for(uint32_t format)
1551{
1552 switch (format) {
1553 case GBM_FORMAT_XRGB8888:
1554 return GBM_FORMAT_ARGB8888;
1555 case GBM_FORMAT_XRGB2101010:
1556 return GBM_FORMAT_ARGB2101010;
1557 default:
1558 return 0;
1559 }
1560}
1561
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001562static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001563drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001564{
Derek Foreman6d556372015-11-04 14:47:33 -06001565 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001566 b->gbm_format,
1567 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001568 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001569 };
Derek Foreman6d556372015-11-04 14:47:33 -06001570 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001571
Derek Foremanc4cfe852015-05-15 12:12:40 -05001572 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001573 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001574 if (gl_renderer->display_create(b->compositor,
1575 EGL_PLATFORM_GBM_KHR,
1576 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001577 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001578 gl_renderer->opaque_attribs,
1579 format,
1580 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001581 return -1;
1582 }
1583
1584 return 0;
1585}
1586
1587static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001589{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001591
Giulio Camuffo954f1832014-10-11 18:27:30 +03001592 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001593 return -1;
1594
Giulio Camuffo954f1832014-10-11 18:27:30 +03001595 if (drm_backend_create_gl_renderer(b) < 0) {
1596 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001597 return -1;
1598 }
1599
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001600 return 0;
1601}
1602
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001603static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001604init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001605{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001606 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001607}
1608
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001609/**
1610 * Add a mode to output's mode list
1611 *
1612 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1613 * output's mode list.
1614 *
1615 * @param output DRM output to add mode to
1616 * @param info DRM mode structure to add
1617 * @returns Newly-allocated Weston/DRM mode structure
1618 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001619static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001620drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001621{
1622 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001623 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001624
1625 mode = malloc(sizeof *mode);
1626 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001627 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001628
1629 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001630 mode->base.width = info->hdisplay;
1631 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001632
1633 /* Calculate higher precision (mHz) refresh rate */
1634 refresh = (info->clock * 1000000LL / info->htotal +
1635 info->vtotal / 2) / info->vtotal;
1636
1637 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1638 refresh *= 2;
1639 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1640 refresh /= 2;
1641 if (info->vscan > 1)
1642 refresh /= info->vscan;
1643
1644 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001645 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001646
1647 if (info->type & DRM_MODE_TYPE_PREFERRED)
1648 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1649
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001650 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1651
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001652 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001653}
1654
1655static int
1656drm_subpixel_to_wayland(int drm_value)
1657{
1658 switch (drm_value) {
1659 default:
1660 case DRM_MODE_SUBPIXEL_UNKNOWN:
1661 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1662 case DRM_MODE_SUBPIXEL_NONE:
1663 return WL_OUTPUT_SUBPIXEL_NONE;
1664 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1665 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1666 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1667 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1668 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1669 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1670 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1671 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1672 }
1673}
1674
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001675/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001676static uint32_t
1677drm_get_backlight(struct drm_output *output)
1678{
1679 long brightness, max_brightness, norm;
1680
1681 brightness = backlight_get_brightness(output->backlight);
1682 max_brightness = backlight_get_max_brightness(output->backlight);
1683
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001684 /* convert it on a scale of 0 to 255 */
1685 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001686
1687 return (uint32_t) norm;
1688}
1689
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001690/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001691static void
1692drm_set_backlight(struct weston_output *output_base, uint32_t value)
1693{
Armin Krezović545dba62016-08-05 15:54:18 +02001694 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001695 long max_brightness, new_brightness;
1696
1697 if (!output->backlight)
1698 return;
1699
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001700 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001701 return;
1702
1703 max_brightness = backlight_get_max_brightness(output->backlight);
1704
1705 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001706 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001707
1708 backlight_set_brightness(output->backlight, new_brightness);
1709}
1710
1711static drmModePropertyPtr
1712drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1713{
1714 drmModePropertyPtr props;
1715 int i;
1716
1717 for (i = 0; i < connector->count_props; i++) {
1718 props = drmModeGetProperty(fd, connector->props[i]);
1719 if (!props)
1720 continue;
1721
1722 if (!strcmp(props->name, name))
1723 return props;
1724
1725 drmModeFreeProperty(props);
1726 }
1727
1728 return NULL;
1729}
1730
1731static void
1732drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1733{
Armin Krezović545dba62016-08-05 15:54:18 +02001734 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001735 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001736 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001737 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001738
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001739 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001740 return;
1741
Daniel Stone36609c72015-06-18 07:49:02 +01001742 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1743 output->dpms_prop->prop_id, level);
1744 if (ret) {
1745 weston_log("DRM: DPMS: failed property set for %s\n",
1746 output->base.name);
1747 return;
1748 }
1749
1750 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001751}
1752
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001753static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001754 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1755 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1756 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1757 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1758 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1759 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1760 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1761 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1762 [DRM_MODE_CONNECTOR_Component] = "Component",
1763 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1764 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1765 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1766 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1767 [DRM_MODE_CONNECTOR_TV] = "TV",
1768 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001769#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001770 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1771 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001772#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001773};
1774
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001775static char *
1776make_connector_name(const drmModeConnector *con)
1777{
1778 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001779 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001780
1781 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1782 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001783
1784 if (!type_name)
1785 type_name = "UNNAMED";
1786
1787 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001788
1789 return strdup(name);
1790}
1791
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001792static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001793find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001794 drmModeRes *resources, drmModeConnector *connector)
1795{
1796 drmModeEncoder *encoder;
1797 uint32_t possible_crtcs;
1798 int i, j;
1799
1800 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001801 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001802 if (encoder == NULL) {
1803 weston_log("Failed to get encoder.\n");
1804 return -1;
1805 }
1806 possible_crtcs = encoder->possible_crtcs;
1807 drmModeFreeEncoder(encoder);
1808
1809 for (i = 0; i < resources->count_crtcs; i++) {
1810 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001811 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001812 return i;
1813 }
1814 }
1815
1816 return -1;
1817}
1818
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001819/* Init output state that depends on gl or gbm */
1820static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001821drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001822{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001823 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001824 output->gbm_format,
1825 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001826 };
1827 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001828
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001829 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001830 output->base.current_mode->width,
1831 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001832 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001833 GBM_BO_USE_SCANOUT |
1834 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001835 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001836 weston_log("failed to create gbm surface\n");
1837 return -1;
1838 }
1839
Derek Foremanc4cfe852015-05-15 12:12:40 -05001840 if (format[1])
1841 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001842 if (gl_renderer->output_window_create(&output->base,
1843 (EGLNativeWindowType)output->gbm_surface,
1844 output->gbm_surface,
1845 gl_renderer->opaque_attribs,
1846 format,
1847 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001848 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001849 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001850 return -1;
1851 }
1852
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001853 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001854
1855 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001856 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001857 continue;
1858
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001859 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001860 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001861 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001862 }
1863
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001864 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001865 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001866 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001867 }
1868
1869 return 0;
1870}
1871
Daniel Stone3e661f72016-11-04 17:24:06 +00001872static void
1873drm_output_fini_egl(struct drm_output *output)
1874{
1875 gl_renderer->output_destroy(&output->base);
1876 gbm_surface_destroy(output->gbm_surface);
1877}
1878
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001879static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001880drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001881{
Hardeningff39efa2013-09-18 23:56:35 +02001882 int w = output->base.current_mode->width;
1883 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001884 uint32_t format = output->gbm_format;
1885 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001886 unsigned int i;
1887
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001888 switch (format) {
1889 case GBM_FORMAT_XRGB8888:
1890 pixman_format = PIXMAN_x8r8g8b8;
1891 break;
1892 case GBM_FORMAT_RGB565:
1893 pixman_format = PIXMAN_r5g6b5;
1894 break;
1895 default:
1896 weston_log("Unsupported pixman format 0x%x\n", format);
1897 return -1;
1898 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001899
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001900 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001901 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001902 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001903 if (!output->dumb[i])
1904 goto err;
1905
1906 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001907 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001908 output->dumb[i]->map,
1909 output->dumb[i]->stride);
1910 if (!output->image[i])
1911 goto err;
1912 }
1913
1914 if (pixman_renderer_output_create(&output->base) < 0)
1915 goto err;
1916
1917 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001918 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001919
1920 return 0;
1921
1922err:
1923 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1924 if (output->dumb[i])
1925 drm_fb_destroy_dumb(output->dumb[i]);
1926 if (output->image[i])
1927 pixman_image_unref(output->image[i]);
1928
1929 output->dumb[i] = NULL;
1930 output->image[i] = NULL;
1931 }
1932
1933 return -1;
1934}
1935
1936static void
1937drm_output_fini_pixman(struct drm_output *output)
1938{
1939 unsigned int i;
1940
1941 pixman_renderer_output_destroy(&output->base);
1942 pixman_region32_fini(&output->previous_damage);
1943
1944 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1945 drm_fb_destroy_dumb(output->dumb[i]);
1946 pixman_image_unref(output->image[i]);
1947 output->dumb[i] = NULL;
1948 output->image[i] = NULL;
1949 }
1950}
1951
Richard Hughes2b2092a2013-04-24 14:58:02 +01001952static void
1953edid_parse_string(const uint8_t *data, char text[])
1954{
1955 int i;
1956 int replaced = 0;
1957
1958 /* this is always 12 bytes, but we can't guarantee it's null
1959 * terminated or not junk. */
1960 strncpy(text, (const char *) data, 12);
1961
Bryce Harrington9c7de162015-08-28 13:04:26 -07001962 /* guarantee our new string is null-terminated */
1963 text[12] = '\0';
1964
Richard Hughes2b2092a2013-04-24 14:58:02 +01001965 /* remove insane chars */
1966 for (i = 0; text[i] != '\0'; i++) {
1967 if (text[i] == '\n' ||
1968 text[i] == '\r') {
1969 text[i] = '\0';
1970 break;
1971 }
1972 }
1973
1974 /* ensure string is printable */
1975 for (i = 0; text[i] != '\0'; i++) {
1976 if (!isprint(text[i])) {
1977 text[i] = '-';
1978 replaced++;
1979 }
1980 }
1981
1982 /* if the string is random junk, ignore the string */
1983 if (replaced > 4)
1984 text[0] = '\0';
1985}
1986
1987#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1988#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1989#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1990#define EDID_OFFSET_DATA_BLOCKS 0x36
1991#define EDID_OFFSET_LAST_BLOCK 0x6c
1992#define EDID_OFFSET_PNPID 0x08
1993#define EDID_OFFSET_SERIAL 0x0c
1994
1995static int
1996edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1997{
1998 int i;
1999 uint32_t serial_number;
2000
2001 /* check header */
2002 if (length < 128)
2003 return -1;
2004 if (data[0] != 0x00 || data[1] != 0xff)
2005 return -1;
2006
2007 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2008 * /--08--\/--09--\
2009 * 7654321076543210
2010 * |\---/\---/\---/
2011 * R C1 C2 C3 */
2012 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2013 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2014 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2015 edid->pnp_id[3] = '\0';
2016
2017 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2018 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2019 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2020 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2021 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2022 if (serial_number > 0)
2023 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2024
2025 /* parse EDID data */
2026 for (i = EDID_OFFSET_DATA_BLOCKS;
2027 i <= EDID_OFFSET_LAST_BLOCK;
2028 i += 18) {
2029 /* ignore pixel clock data */
2030 if (data[i] != 0)
2031 continue;
2032 if (data[i+2] != 0)
2033 continue;
2034
2035 /* any useful blocks? */
2036 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2037 edid_parse_string(&data[i+5],
2038 edid->monitor_name);
2039 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2040 edid_parse_string(&data[i+5],
2041 edid->serial_number);
2042 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2043 edid_parse_string(&data[i+5],
2044 edid->eisa_id);
2045 }
2046 }
2047 return 0;
2048}
2049
2050static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002052 struct drm_output *output,
2053 drmModeConnector *connector)
2054{
2055 drmModePropertyBlobPtr edid_blob = NULL;
2056 drmModePropertyPtr property;
2057 int i;
2058 int rc;
2059
2060 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002061 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002062 if (!property)
2063 continue;
2064 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2065 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002066 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002067 connector->prop_values[i]);
2068 }
2069 drmModeFreeProperty(property);
2070 }
2071 if (!edid_blob)
2072 return;
2073
2074 rc = edid_parse(&output->edid,
2075 edid_blob->data,
2076 edid_blob->length);
2077 if (!rc) {
2078 weston_log("EDID data '%s', '%s', '%s'\n",
2079 output->edid.pnp_id,
2080 output->edid.monitor_name,
2081 output->edid.serial_number);
2082 if (output->edid.pnp_id[0] != '\0')
2083 output->base.make = output->edid.pnp_id;
2084 if (output->edid.monitor_name[0] != '\0')
2085 output->base.model = output->edid.monitor_name;
2086 if (output->edid.serial_number[0] != '\0')
2087 output->base.serial_number = output->edid.serial_number;
2088 }
2089 drmModeFreePropertyBlob(edid_blob);
2090}
2091
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002092
2093
2094static int
2095parse_modeline(const char *s, drmModeModeInfo *mode)
2096{
2097 char hsync[16];
2098 char vsync[16];
2099 float fclock;
2100
2101 mode->type = DRM_MODE_TYPE_USERDEF;
2102 mode->hskew = 0;
2103 mode->vscan = 0;
2104 mode->vrefresh = 0;
2105 mode->flags = 0;
2106
Rob Bradford307e09e2013-07-26 16:29:40 +01002107 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002108 &fclock,
2109 &mode->hdisplay,
2110 &mode->hsync_start,
2111 &mode->hsync_end,
2112 &mode->htotal,
2113 &mode->vdisplay,
2114 &mode->vsync_start,
2115 &mode->vsync_end,
2116 &mode->vtotal, hsync, vsync) != 11)
2117 return -1;
2118
2119 mode->clock = fclock * 1000;
2120 if (strcmp(hsync, "+hsync") == 0)
2121 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2122 else if (strcmp(hsync, "-hsync") == 0)
2123 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2124 else
2125 return -1;
2126
2127 if (strcmp(vsync, "+vsync") == 0)
2128 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2129 else if (strcmp(vsync, "-vsync") == 0)
2130 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2131 else
2132 return -1;
2133
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002134 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2135 mode->hdisplay, mode->vdisplay, fclock);
2136
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002137 return 0;
2138}
2139
Rob Bradford66bd9f52013-06-25 18:56:42 +01002140static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002141setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002142 struct weston_output *output,
2143 const char *s)
2144{
2145 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002146 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002147 struct udev_seat *seat;
2148
Giulio Camuffo954f1832014-10-11 18:27:30 +03002149 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002150 if (!seat)
2151 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002152
Derek Foreman0720ea32015-07-15 13:00:35 -05002153 seat->base.output = output;
2154
Derek Foreman1281a362015-07-31 16:55:32 -05002155 pointer = weston_seat_get_pointer(&seat->base);
2156 if (pointer)
2157 weston_pointer_clamp(pointer,
2158 &pointer->x,
2159 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002160 }
2161}
2162
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002163static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002164parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002165{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002166 int ret = 0;
2167
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002168 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002169 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002170 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002171 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002172 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002173 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002174 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002175 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002176 else {
2177 weston_log("fatal: unrecognized pixel format: %s\n", s);
2178 ret = -1;
2179 }
2180
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002181 return ret;
2182}
2183
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002184/**
2185 * Choose suitable mode for an output
2186 *
2187 * Find the most suitable mode to use for initial setup (or reconfiguration on
2188 * hotplug etc) for a DRM output.
2189 *
2190 * @param output DRM output to choose mode for
2191 * @param kind Strategy and preference to use when choosing mode
2192 * @param width Desired width for this output
2193 * @param height Desired height for this output
2194 * @param current_mode Mode currently being displayed on this output
2195 * @param modeline Manually-entered mode (may be NULL)
2196 * @returns A mode from the output's mode list, or NULL if none available
2197 */
2198static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002199drm_output_choose_initial_mode(struct drm_backend *backend,
2200 struct drm_output *output,
2201 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002202 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002203 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002204{
2205 struct drm_mode *preferred = NULL;
2206 struct drm_mode *current = NULL;
2207 struct drm_mode *configured = NULL;
2208 struct drm_mode *best = NULL;
2209 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002210 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002211 int32_t width = 0;
2212 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002213 uint32_t refresh = 0;
2214 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002215
Armin Krezović08368132016-09-30 14:11:05 +02002216 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002217 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2218 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002219 width = -1;
2220
Armin Krezović08368132016-09-30 14:11:05 +02002221 if (parse_modeline(modeline, &drm_modeline) == 0) {
2222 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002223 if (!configured)
2224 return NULL;
2225 } else {
2226 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002227 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002228 }
2229 }
2230 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002231
2232 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002233 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002234 height == drm_mode->base.height &&
2235 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002236 configured = drm_mode;
2237
comic fans7a5c5622016-03-17 14:29:27 +02002238 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002239 sizeof *current_mode) == 0)
2240 current = drm_mode;
2241
2242 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2243 preferred = drm_mode;
2244
2245 best = drm_mode;
2246 }
2247
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002248 if (current == NULL && current_mode->clock != 0) {
2249 current = drm_output_add_mode(output, current_mode);
2250 if (!current)
2251 return NULL;
2252 }
2253
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002254 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002255 configured = current;
2256
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002257 if (configured)
2258 return configured;
2259
2260 if (preferred)
2261 return preferred;
2262
2263 if (current)
2264 return current;
2265
2266 if (best)
2267 return best;
2268
2269 weston_log("no available modes for %s\n", output->base.name);
2270 return NULL;
2271}
2272
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002273static int
2274connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2275 drmModeModeInfo *mode)
2276{
2277 drmModeEncoder *encoder;
2278 drmModeCrtc *crtc;
2279
2280 /* Get the current mode on the crtc that's currently driving
2281 * this connector. */
2282 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2283 memset(mode, 0, sizeof *mode);
2284 if (encoder != NULL) {
2285 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2286 drmModeFreeEncoder(encoder);
2287 if (crtc == NULL)
2288 return -1;
2289 if (crtc->mode_valid)
2290 *mode = crtc->mode;
2291 drmModeFreeCrtc(crtc);
2292 }
2293
2294 return 0;
2295}
2296
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002297static int
Armin Krezović08368132016-09-30 14:11:05 +02002298drm_output_set_mode(struct weston_output *base,
2299 enum weston_drm_backend_output_mode mode,
2300 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002301{
Armin Krezović08368132016-09-30 14:11:05 +02002302 struct drm_output *output = to_drm_output(base);
2303 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002304
Armin Krezović08368132016-09-30 14:11:05 +02002305 struct drm_mode *drm_mode, *next, *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002306 drmModeModeInfo crtc_mode;
2307 int i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002308
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002309 output->base.make = "unknown";
2310 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002311 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002312 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002313
Giulio Camuffo954f1832014-10-11 18:27:30 +03002314 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
Matt Roper361d2ad2011-08-29 13:52:23 -07002315
Armin Krezović08368132016-09-30 14:11:05 +02002316 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002317 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002318
Armin Krezović08368132016-09-30 14:11:05 +02002319 for (i = 0; i < output->connector->count_modes; i++) {
2320 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002321 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002322 goto err_free;
2323 }
2324
Armin Krezović08368132016-09-30 14:11:05 +02002325 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002326 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002327 goto err_free;
Armin Krezović08368132016-09-30 14:11:05 +02002328
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002329 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002330 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002331
Armin Krezović08368132016-09-30 14:11:05 +02002332 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2333 output->base.native_mode = output->base.current_mode;
2334 output->base.native_scale = output->base.current_scale;
2335
2336 output->base.mm_width = output->connector->mmWidth;
2337 output->base.mm_height = output->connector->mmHeight;
2338
2339 return 0;
2340
2341err_free:
2342 drmModeFreeCrtc(output->original_crtc);
2343 output->original_crtc = NULL;
2344
2345 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2346 base.link) {
2347 wl_list_remove(&drm_mode->base.link);
2348 free(drm_mode);
2349 }
2350
2351 return -1;
2352}
2353
2354static void
2355drm_output_set_gbm_format(struct weston_output *base,
2356 const char *gbm_format)
2357{
2358 struct drm_output *output = to_drm_output(base);
2359 struct drm_backend *b = to_drm_backend(base->compositor);
2360
2361 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2362 output->gbm_format = b->gbm_format;
2363}
2364
2365static void
2366drm_output_set_seat(struct weston_output *base,
2367 const char *seat)
2368{
2369 struct drm_output *output = to_drm_output(base);
2370 struct drm_backend *b = to_drm_backend(base->compositor);
2371
2372 setup_output_seat_constraint(b, &output->base,
2373 seat ? seat : "");
2374}
2375
2376static int
2377drm_output_enable(struct weston_output *base)
2378{
2379 struct drm_output *output = to_drm_output(base);
2380 struct drm_backend *b = to_drm_backend(base->compositor);
2381 struct weston_mode *m;
2382
2383 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002384
Giulio Camuffo954f1832014-10-11 18:27:30 +03002385 if (b->use_pixman) {
2386 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002387 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002388 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002389 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002390 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002391 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002392 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002393 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002394
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002395 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002396 weston_log("Initialized backlight, device %s\n",
2397 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002398 output->base.set_backlight = drm_set_backlight;
2399 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002400 } else {
2401 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002402 }
2403
Jonas Ådahle5a12252013-04-05 23:07:11 +02002404 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002405 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002406 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002407 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002408 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002409
Richard Hughese7299962013-05-01 21:52:12 +01002410 output->base.gamma_size = output->original_crtc->gamma_size;
2411 output->base.set_gamma = drm_output_set_gamma;
2412
Armin Krezović08368132016-09-30 14:11:05 +02002413 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2414
2415 find_and_parse_output_edid(b, output, output->connector);
2416 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2417 output->base.connection_internal = 1;
2418
Derek Foremanb0427562016-02-05 15:55:20 -06002419 weston_plane_init(&output->cursor_plane, b->compositor,
2420 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002421 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002422
Giulio Camuffo954f1832014-10-11 18:27:30 +03002423 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2424 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2425 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002426
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002427 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002428 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002429 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002430 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002431 m->width, m->height, m->refresh / 1000.0,
2432 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2433 ", preferred" : "",
2434 m->flags & WL_OUTPUT_MODE_CURRENT ?
2435 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002436 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002437 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002438
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002439 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002440
2441err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002442 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002443
David Herrmann0f0d54e2011-12-08 17:05:45 +01002444 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002445}
2446
Jesse Barnes58ef3792012-02-23 09:45:49 -05002447static void
Armin Krezović08368132016-09-30 14:11:05 +02002448drm_output_deinit(struct weston_output *base)
2449{
2450 struct drm_output *output = to_drm_output(base);
2451 struct drm_backend *b = to_drm_backend(base->compositor);
2452
Daniel Stone3e661f72016-11-04 17:24:06 +00002453 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002454 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002455 else
2456 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002457
2458 weston_plane_release(&output->fb_plane);
2459 weston_plane_release(&output->cursor_plane);
2460
2461 drmModeFreeProperty(output->dpms_prop);
2462
2463 /* Turn off hardware cursor */
2464 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2465}
2466
2467static void
2468drm_output_destroy(struct weston_output *base)
2469{
2470 struct drm_output *output = to_drm_output(base);
2471 struct drm_backend *b = to_drm_backend(base->compositor);
2472 drmModeCrtcPtr origcrtc = output->original_crtc;
2473
2474 if (output->page_flip_pending) {
2475 output->destroy_pending = 1;
2476 weston_log("destroy output while page flip pending\n");
2477 return;
2478 }
2479
2480 if (output->base.enabled)
2481 drm_output_deinit(&output->base);
2482
2483 if (origcrtc) {
2484 /* Restore original CRTC state */
2485 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2486 origcrtc->x, origcrtc->y,
2487 &output->connector_id, 1, &origcrtc->mode);
2488 drmModeFreeCrtc(origcrtc);
2489 }
2490
2491 weston_output_destroy(&output->base);
2492
2493 drmModeFreeConnector(output->connector);
2494
2495 if (output->backlight)
2496 backlight_destroy(output->backlight);
2497
2498 b->crtc_allocator &= ~(1 << output->crtc_id);
2499 b->connector_allocator &= ~(1 << output->connector_id);
2500
2501 free(output);
2502}
2503
2504static int
2505drm_output_disable(struct weston_output *base)
2506{
2507 struct drm_output *output = to_drm_output(base);
2508 struct drm_backend *b = to_drm_backend(base->compositor);
2509
2510 if (output->page_flip_pending) {
2511 output->disable_pending = 1;
2512 return -1;
2513 }
2514
2515 if (output->base.enabled)
2516 drm_output_deinit(&output->base);
2517
2518 output->disable_pending = 0;
2519
2520 weston_log("Disabling output %s\n", output->base.name);
2521 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2522 0, 0, 0, 0, 0, NULL);
2523
2524 return 0;
2525}
2526
2527/**
2528 * Create a Weston output structure
2529 *
2530 * Given a DRM connector, create a matching drm_output structure and add it
2531 * to Weston's output list. It also takes ownership of the connector, which
2532 * is released when output is destroyed.
2533 *
2534 * @param b Weston backend structure
2535 * @param resources DRM resources for this device
2536 * @param connector DRM connector to use for this new output
2537 * @param drm_device udev device pointer
2538 * @returns 0 on success, or -1 on failure
2539 */
2540static int
2541create_output_for_connector(struct drm_backend *b,
2542 drmModeRes *resources,
2543 drmModeConnector *connector,
2544 struct udev_device *drm_device)
2545{
2546 struct drm_output *output;
2547 int i;
2548
2549 i = find_crtc_for_connector(b, resources, connector);
2550 if (i < 0) {
2551 weston_log("No usable crtc/encoder pair for connector.\n");
2552 return -1;
2553 }
2554
2555 output = zalloc(sizeof *output);
2556 if (output == NULL)
2557 return -1;
2558
2559 output->connector = connector;
2560 output->crtc_id = resources->crtcs[i];
2561 output->pipe = i;
2562 output->connector_id = connector->connector_id;
2563
2564 output->backlight = backlight_init(drm_device,
2565 connector->connector_type);
2566
2567 output->base.enable = drm_output_enable;
2568 output->base.destroy = drm_output_destroy;
2569 output->base.disable = drm_output_disable;
2570 output->base.name = make_connector_name(connector);
2571
2572 output->destroy_pending = 0;
2573 output->disable_pending = 0;
2574 output->original_crtc = NULL;
2575
2576 b->crtc_allocator |= (1 << output->crtc_id);
2577 b->connector_allocator |= (1 << output->connector_id);
2578
Armin Krezović40087402016-09-30 14:11:12 +02002579 weston_output_init(&output->base, b->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002580 weston_compositor_add_pending_output(&output->base, b->compositor);
2581
2582 return 0;
2583}
2584
2585static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002586create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002587{
2588 struct drm_sprite *sprite;
2589 drmModePlaneRes *plane_res;
2590 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002591 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002592
Giulio Camuffo954f1832014-10-11 18:27:30 +03002593 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002594 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002595 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002596 strerror(errno));
2597 return;
2598 }
2599
2600 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002601 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002602 if (!plane)
2603 continue;
2604
Peter Huttererf3d62272013-08-08 11:57:05 +10002605 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002606 plane->count_formats));
2607 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002608 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002609 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002610 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002611 continue;
2612 }
2613
Jesse Barnes58ef3792012-02-23 09:45:49 -05002614 sprite->possible_crtcs = plane->possible_crtcs;
2615 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002616 sprite->current = NULL;
2617 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002619 sprite->count_formats = plane->count_formats;
2620 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002621 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002622 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002623 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2624 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2625 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002626
Giulio Camuffo954f1832014-10-11 18:27:30 +03002627 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002628 }
2629
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002630 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002631}
2632
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002633static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002634destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002635{
2636 struct drm_sprite *sprite, *next;
2637 struct drm_output *output;
2638
Giulio Camuffo954f1832014-10-11 18:27:30 +03002639 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002640 struct drm_output, base.link);
2641
Giulio Camuffo954f1832014-10-11 18:27:30 +03002642 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2643 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002644 sprite->plane_id,
2645 output->crtc_id, 0, 0,
2646 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002647 drm_output_release_fb(output, sprite->current);
2648 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002649 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002650 free(sprite);
2651 }
2652}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002653
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002654static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002655create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002656 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002657{
2658 drmModeConnector *connector;
2659 drmModeRes *resources;
2660 int i;
2661
Giulio Camuffo954f1832014-10-11 18:27:30 +03002662 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002663 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002664 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002665 return -1;
2666 }
2667
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 b->min_width = resources->min_width;
2669 b->max_width = resources->max_width;
2670 b->min_height = resources->min_height;
2671 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002672
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002673 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002674 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002675 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002676 if (connector == NULL)
2677 continue;
2678
2679 if (connector->connection == DRM_MODE_CONNECTED &&
2680 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002681 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002682 if (create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002683 connector, drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002684 drmModeFreeConnector(connector);
2685 continue;
2686 }
Armin Krezović08368132016-09-30 14:11:05 +02002687 } else {
2688 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002689 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002690 }
2691
Armin Krezović08368132016-09-30 14:11:05 +02002692 if (wl_list_empty(&b->compositor->output_list) &&
2693 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002694 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002695
2696 drmModeFreeResources(resources);
2697
2698 return 0;
2699}
2700
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002701static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002702update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002703{
2704 drmModeConnector *connector;
2705 drmModeRes *resources;
2706 struct drm_output *output, *next;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002707 uint32_t connected = 0, disconnects = 0;
2708 int i;
2709
Giulio Camuffo954f1832014-10-11 18:27:30 +03002710 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002711 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002712 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002713 return;
2714 }
2715
2716 /* collect new connects */
2717 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002718 int connector_id = resources->connectors[i];
2719
Giulio Camuffo954f1832014-10-11 18:27:30 +03002720 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002721 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002722 continue;
2723
David Herrmann7551cff2011-12-08 17:05:43 +01002724 if (connector->connection != DRM_MODE_CONNECTED) {
2725 drmModeFreeConnector(connector);
2726 continue;
2727 }
2728
Benjamin Franzke117483d2011-08-30 11:38:26 +02002729 connected |= (1 << connector_id);
2730
Giulio Camuffo954f1832014-10-11 18:27:30 +03002731 if (!(b->connector_allocator & (1 << connector_id))) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002732 create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002733 connector, drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002734 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002735
Armin Krezović08368132016-09-30 14:11:05 +02002736 } else {
2737 drmModeFreeConnector(connector);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002738 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002739 }
2740 drmModeFreeResources(resources);
2741
Giulio Camuffo954f1832014-10-11 18:27:30 +03002742 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002743 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002744 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002745 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002746 if (disconnects & (1 << output->connector_id)) {
2747 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002748 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002749 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002750 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002751 }
2752 }
Armin Krezović08368132016-09-30 14:11:05 +02002753
2754 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2755 base.link) {
2756 if (disconnects & (1 << output->connector_id)) {
2757 disconnects &= ~(1 << output->connector_id);
2758 weston_log("connector %d disconnected\n",
2759 output->connector_id);
2760 drm_output_destroy(&output->base);
2761 }
2762 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002763 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002764}
2765
2766static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002767udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002768{
David Herrmannd7488c22012-03-11 20:05:21 +01002769 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002770 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002771
2772 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002773 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002774 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002775
David Herrmann6ac52db2012-03-11 20:05:22 +01002776 val = udev_device_get_property_value(device, "HOTPLUG");
2777 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002778 return 0;
2779
David Herrmann6ac52db2012-03-11 20:05:22 +01002780 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002781}
2782
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002783static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002784udev_drm_event(int fd, uint32_t mask, void *data)
2785{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002786 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002787 struct udev_device *event;
2788
Giulio Camuffo954f1832014-10-11 18:27:30 +03002789 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002790
Giulio Camuffo954f1832014-10-11 18:27:30 +03002791 if (udev_event_is_hotplug(b, event))
2792 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002793
2794 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002795
2796 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002797}
2798
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002799static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002800drm_restore(struct weston_compositor *ec)
2801{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002802 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002803}
2804
2805static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002806drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002807{
Armin Krezović545dba62016-08-05 15:54:18 +02002808 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002809
Giulio Camuffo954f1832014-10-11 18:27:30 +03002810 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002811
Giulio Camuffo954f1832014-10-11 18:27:30 +03002812 wl_event_source_remove(b->udev_drm_source);
2813 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002814
Giulio Camuffo954f1832014-10-11 18:27:30 +03002815 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002816
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002817 weston_compositor_shutdown(ec);
2818
Giulio Camuffo954f1832014-10-11 18:27:30 +03002819 if (b->gbm)
2820 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002821
Giulio Camuffo954f1832014-10-11 18:27:30 +03002822 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002823
Giulio Camuffo954f1832014-10-11 18:27:30 +03002824 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002825 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002826}
2827
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002828static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002829session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002830{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002831 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002832 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002833 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002834 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002835
Giulio Camuffo954f1832014-10-11 18:27:30 +03002836 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002837 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00002838 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002839 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002840 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002841 } else {
2842 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002843 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002844
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002845 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002846
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002847 /* If we have a repaint scheduled (either from a
2848 * pending pageflip or the idle handler), make sure we
2849 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002850 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05002851 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002852 * back, we schedule a repaint, which will process
2853 * pending frame callbacks. */
2854
Giulio Camuffo954f1832014-10-11 18:27:30 +03002855 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002856 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002857 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002858 }
2859
Giulio Camuffo954f1832014-10-11 18:27:30 +03002860 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002861 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002862
Giulio Camuffo954f1832014-10-11 18:27:30 +03002863 wl_list_for_each(sprite, &b->sprite_list, link)
2864 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002865 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002866 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002867 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002868 };
2869}
2870
David Herrmann0af066f2012-10-29 19:21:16 +01002871/*
2872 * Find primary GPU
2873 * Some systems may have multiple DRM devices attached to a single seat. This
2874 * function loops over all devices and tries to find a PCI device with the
2875 * boot_vga sysfs attribute set to 1.
2876 * If no such device is found, the first DRM device reported by udev is used.
2877 */
2878static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002879find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002880{
2881 struct udev_enumerate *e;
2882 struct udev_list_entry *entry;
2883 const char *path, *device_seat, *id;
2884 struct udev_device *device, *drm_device, *pci;
2885
Giulio Camuffo954f1832014-10-11 18:27:30 +03002886 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002887 udev_enumerate_add_match_subsystem(e, "drm");
2888 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2889
2890 udev_enumerate_scan_devices(e);
2891 drm_device = NULL;
2892 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2893 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002894 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002895 if (!device)
2896 continue;
2897 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2898 if (!device_seat)
2899 device_seat = default_seat;
2900 if (strcmp(device_seat, seat)) {
2901 udev_device_unref(device);
2902 continue;
2903 }
2904
2905 pci = udev_device_get_parent_with_subsystem_devtype(device,
2906 "pci", NULL);
2907 if (pci) {
2908 id = udev_device_get_sysattr_value(pci, "boot_vga");
2909 if (id && !strcmp(id, "1")) {
2910 if (drm_device)
2911 udev_device_unref(drm_device);
2912 drm_device = device;
2913 break;
2914 }
2915 }
2916
2917 if (!drm_device)
2918 drm_device = device;
2919 else
2920 udev_device_unref(device);
2921 }
2922
2923 udev_enumerate_unref(e);
2924 return drm_device;
2925}
2926
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002927static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002928planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2929 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002930{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002931 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002932
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002933 switch (key) {
2934 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002935 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002936 break;
2937 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002938 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002939 break;
2940 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002941 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002942 break;
2943 default:
2944 break;
2945 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002946}
2947
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002948#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002949static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002950recorder_destroy(struct drm_output *output)
2951{
2952 vaapi_recorder_destroy(output->recorder);
2953 output->recorder = NULL;
2954
2955 output->base.disable_planes--;
2956
2957 wl_list_remove(&output->recorder_frame_listener.link);
2958 weston_log("[libva recorder] done\n");
2959}
2960
2961static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002962recorder_frame_notify(struct wl_listener *listener, void *data)
2963{
2964 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002965 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002966 int fd, ret;
2967
2968 output = container_of(listener, struct drm_output,
2969 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002970 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002971
2972 if (!output->recorder)
2973 return;
2974
Giulio Camuffo954f1832014-10-11 18:27:30 +03002975 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002976 DRM_CLOEXEC, &fd);
2977 if (ret) {
2978 weston_log("[libva recorder] "
2979 "failed to create prime fd for front buffer\n");
2980 return;
2981 }
2982
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002983 ret = vaapi_recorder_frame(output->recorder, fd,
2984 output->current->stride);
2985 if (ret < 0) {
2986 weston_log("[libva recorder] aborted: %m\n");
2987 recorder_destroy(output);
2988 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002989}
2990
2991static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03002992create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002993 const char *filename)
2994{
2995 int fd;
2996 drm_magic_t magic;
2997
Giulio Camuffo954f1832014-10-11 18:27:30 +03002998 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002999 if (fd < 0)
3000 return NULL;
3001
3002 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003003 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003004
3005 return vaapi_recorder_create(fd, width, height, filename);
3006}
3007
3008static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003009recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003010 void *data)
3011{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003012 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003013 struct drm_output *output;
3014 int width, height;
3015
Giulio Camuffo954f1832014-10-11 18:27:30 +03003016 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003017 struct drm_output, base.link);
3018
3019 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003020 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003021 weston_log("failed to start vaapi recorder: "
3022 "output format not supported\n");
3023 return;
3024 }
3025
Hardeningff39efa2013-09-18 23:56:35 +02003026 width = output->base.current_mode->width;
3027 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003028
3029 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003031 if (!output->recorder) {
3032 weston_log("failed to create vaapi recorder\n");
3033 return;
3034 }
3035
3036 output->base.disable_planes++;
3037
3038 output->recorder_frame_listener.notify = recorder_frame_notify;
3039 wl_signal_add(&output->base.frame_signal,
3040 &output->recorder_frame_listener);
3041
3042 weston_output_schedule_repaint(&output->base);
3043
3044 weston_log("[libva recorder] initialized\n");
3045 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003046 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003047 }
3048}
3049#else
3050static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003051recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003052 void *data)
3053{
3054 weston_log("Compiled without libva support\n");
3055}
3056#endif
3057
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003058static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003059switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003060{
3061 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003062 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003063
Giulio Camuffo954f1832014-10-11 18:27:30 +03003064 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003065 return;
3066
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003067 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3068
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003069 weston_log("Switching to GL renderer\n");
3070
Giulio Camuffo954f1832014-10-11 18:27:30 +03003071 b->gbm = create_gbm_device(b->drm.fd);
3072 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003073 weston_log("Failed to create gbm device. "
3074 "Aborting renderer switch\n");
3075 return;
3076 }
3077
Giulio Camuffo954f1832014-10-11 18:27:30 +03003078 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003079 pixman_renderer_output_destroy(&output->base);
3080
Giulio Camuffo954f1832014-10-11 18:27:30 +03003081 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003082
Giulio Camuffo954f1832014-10-11 18:27:30 +03003083 if (drm_backend_create_gl_renderer(b) < 0) {
3084 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003085 weston_log("Failed to create GL renderer. Quitting.\n");
3086 /* FIXME: we need a function to shutdown cleanly */
3087 assert(0);
3088 }
3089
Giulio Camuffo954f1832014-10-11 18:27:30 +03003090 wl_list_for_each(output, &b->compositor->output_list, base.link)
3091 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003092
Giulio Camuffo954f1832014-10-11 18:27:30 +03003093 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003094
3095 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3096 if (linux_dmabuf_setup(b->compositor) < 0)
3097 weston_log("Error: initializing dmabuf "
3098 "support failed.\n");
3099 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003100}
3101
3102static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003103renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3104 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003105{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003106 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003107 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003108
Giulio Camuffo954f1832014-10-11 18:27:30 +03003109 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003110}
3111
Armin Krezović08368132016-09-30 14:11:05 +02003112static const struct weston_drm_output_api api = {
3113 drm_output_set_mode,
3114 drm_output_set_gbm_format,
3115 drm_output_set_seat,
3116};
3117
Giulio Camuffo954f1832014-10-11 18:27:30 +03003118static struct drm_backend *
3119drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003120 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003121{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003122 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003123 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003124 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003125 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003126 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003127 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003128
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003129 weston_log("initializing drm backend\n");
3130
Giulio Camuffo954f1832014-10-11 18:27:30 +03003131 b = zalloc(sizeof *b);
3132 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003133 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003134
Pekka Paalanen68583832015-05-19 09:53:16 +03003135 /*
3136 * KMS support for hardware planes cannot properly synchronize
3137 * without nuclear page flip. Without nuclear/atomic, hw plane
3138 * and cursor plane updates would either tear or cause extra
3139 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003140 * to a fraction. For cursors, it's not so bad, so they are
3141 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003142 *
3143 * These can be enabled again when nuclear/atomic support lands.
3144 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003145 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003146 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003147 b->use_pixman = config->use_pixman;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003148 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003149
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003150 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3151 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003152
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003153 if (config->seat_id)
3154 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003155
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003156 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003157 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3158 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003159 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003160 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003161 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003162 goto err_compositor;
3163 }
3164
Giulio Camuffo954f1832014-10-11 18:27:30 +03003165 b->udev = udev_new();
3166 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003167 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003168 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003169 }
3170
Giulio Camuffo954f1832014-10-11 18:27:30 +03003171 b->session_listener.notify = session_notify;
3172 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003173
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003174 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003175 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003176 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003177 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003178 }
David Herrmann0af066f2012-10-29 19:21:16 +01003179 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003180
Giulio Camuffo954f1832014-10-11 18:27:30 +03003181 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003182 weston_log("failed to initialize kms\n");
3183 goto err_udev_dev;
3184 }
3185
Giulio Camuffo954f1832014-10-11 18:27:30 +03003186 if (b->use_pixman) {
3187 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003188 weston_log("failed to initialize pixman renderer\n");
3189 goto err_udev_dev;
3190 }
3191 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003192 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003193 weston_log("failed to initialize egl\n");
3194 goto err_udev_dev;
3195 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003196 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003197
Giulio Camuffo954f1832014-10-11 18:27:30 +03003198 b->base.destroy = drm_destroy;
3199 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003200
Bob Ham91880f12016-01-12 10:21:47 +00003201 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003202
Giulio Camuffo954f1832014-10-11 18:27:30 +03003203 wl_list_init(&b->sprite_list);
3204 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003205
Giulio Camuffo954f1832014-10-11 18:27:30 +03003206 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003207 compositor, b->udev, seat_id,
3208 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003209 weston_log("failed to create input devices\n");
3210 goto err_sprite;
3211 }
3212
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003213 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003214 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003215 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003216 }
3217
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003218 /* A this point we have some idea of whether or not we have a working
3219 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003220 if (!b->cursors_are_broken)
3221 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003222
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003223 path = NULL;
3224
Giulio Camuffo954f1832014-10-11 18:27:30 +03003225 loop = wl_display_get_event_loop(compositor->wl_display);
3226 b->drm_source =
3227 wl_event_loop_add_fd(loop, b->drm.fd,
3228 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003229
Giulio Camuffo954f1832014-10-11 18:27:30 +03003230 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3231 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003232 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003233 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003234 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003235 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003236 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003237 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003238 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003239 udev_monitor_get_fd(b->udev_monitor),
3240 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003241
Giulio Camuffo954f1832014-10-11 18:27:30 +03003242 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003243 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003244 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003245 }
3246
Daniel Stonea96b93c2012-06-22 14:04:37 +01003247 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003248
Giulio Camuffo954f1832014-10-11 18:27:30 +03003249 weston_compositor_add_debug_binding(compositor, KEY_O,
3250 planes_binding, b);
3251 weston_compositor_add_debug_binding(compositor, KEY_C,
3252 planes_binding, b);
3253 weston_compositor_add_debug_binding(compositor, KEY_V,
3254 planes_binding, b);
3255 weston_compositor_add_debug_binding(compositor, KEY_Q,
3256 recorder_binding, b);
3257 weston_compositor_add_debug_binding(compositor, KEY_W,
3258 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003259
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003260 if (compositor->renderer->import_dmabuf) {
3261 if (linux_dmabuf_setup(compositor) < 0)
3262 weston_log("Error: initializing dmabuf "
3263 "support failed.\n");
3264 }
3265
Giulio Camuffo954f1832014-10-11 18:27:30 +03003266 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003267
Armin Krezović08368132016-09-30 14:11:05 +02003268 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3269 &api, sizeof(api));
3270
3271 if (ret < 0) {
3272 weston_log("Failed to register output API.\n");
3273 goto err_udev_monitor;
3274 }
3275
Giulio Camuffo954f1832014-10-11 18:27:30 +03003276 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003277
3278err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003279 wl_event_source_remove(b->udev_drm_source);
3280 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003281err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003282 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003283err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003284 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003285err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003286 if (b->gbm)
3287 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003288 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003289err_udev_dev:
3290 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003291err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003292 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003293err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003294 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003295err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003296 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003297 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003298 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003299}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003300
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003301static void
3302config_init_to_defaults(struct weston_drm_backend_config *config)
3303{
3304}
3305
Giulio Camuffo954f1832014-10-11 18:27:30 +03003306WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003307backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003308 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003309{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003311 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003312
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003313 if (config_base == NULL ||
3314 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3315 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3316 weston_log("drm backend config structure is invalid\n");
3317 return -1;
3318 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003319
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003320 config_init_to_defaults(&config);
3321 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003322
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003323 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003324 if (b == NULL)
3325 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003326
Giulio Camuffo954f1832014-10-11 18:27:30 +03003327 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003328}