blob: 67408e7497bdd4e149665379ef90d95045d28228 [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
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200117 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300118
Rob Bradfordd355b802013-05-31 18:09:55 +0100119 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300120
Daniel Stone70d337d2015-06-16 18:42:23 +0100121 int32_t cursor_width;
122 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700123
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700124 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400125};
126
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400127struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400129 drmModeModeInfo mode_info;
130};
131
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300132struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200133 uint32_t fb_id, stride, handle, size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000134 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200135 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300136 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200137 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200138
139 /* Used by gbm fbs */
140 struct gbm_bo *bo;
141
142 /* Used by dumb fbs */
143 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300144};
145
Richard Hughes2b2092a2013-04-24 14:58:02 +0100146struct drm_edid {
147 char eisa_id[13];
148 char monitor_name[13];
149 char pnp_id[5];
150 char serial_number[13];
151};
152
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400153struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200154 struct weston_output base;
155 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400156
Daniel Stone17339232015-11-28 12:09:47 +0000157 uint32_t crtc_id; /* object ID to pass to DRM functions */
158 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400159 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700160 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100161 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300162 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100163 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200164
Daniel Stone36609c72015-06-18 07:49:02 +0100165 enum dpms_enum dpms;
166
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300167 int vblank_pending;
168 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800169 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200170 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300171
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100172 struct gbm_surface *gbm_surface;
173 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400174 struct weston_plane cursor_plane;
175 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500176 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400177 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300178 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200179 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200180
181 struct drm_fb *dumb[2];
182 pixman_image_t *image[2];
183 int current_image;
184 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300185
186 struct vaapi_recorder *recorder;
187 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400188};
189
Jesse Barnes58ef3792012-02-23 09:45:49 -0500190/*
191 * An output has a primary display plane plus zero or more sprites for
192 * blending display contents.
193 */
194struct drm_sprite {
195 struct wl_list link;
196
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400197 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500198
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200199 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300200 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300201 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500202
Jesse Barnes58ef3792012-02-23 09:45:49 -0500203 uint32_t possible_crtcs;
204 uint32_t plane_id;
205 uint32_t count_formats;
206
207 int32_t src_x, src_y;
208 uint32_t src_w, src_h;
209 uint32_t dest_x, dest_y;
210 uint32_t dest_w, dest_h;
211
212 uint32_t formats[];
213};
214
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300215static struct gl_renderer_interface *gl_renderer;
216
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500217static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400218
Armin Krezović545dba62016-08-05 15:54:18 +0200219static inline struct drm_output *
220to_drm_output(struct weston_output *base)
221{
222 return container_of(base, struct drm_output, base);
223}
224
225static inline struct drm_backend *
226to_drm_backend(struct weston_compositor *base)
227{
228 return container_of(base->backend, struct drm_backend, base);
229}
230
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400231static void
232drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400233
Mario Kleinerf507ec32015-06-21 21:25:14 +0200234static void
235drm_output_update_msc(struct drm_output *output, unsigned int seq);
236
Jesse Barnes58ef3792012-02-23 09:45:49 -0500237static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000238drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500239{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000240 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500241}
242
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300243static void
244drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
245{
246 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300247
248 if (fb->fb_id)
Daniel Stone4e5eceb2015-11-30 16:07:19 +0000249 drmModeRmFB(fb->fd, fb->fb_id);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300250
Pekka Paalanende685b82012-12-04 15:58:12 +0200251 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300252
253 free(data);
254}
255
256static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000257drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300258 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200259{
260 struct drm_fb *fb;
261 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300262 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200263
264 struct drm_mode_create_dumb create_arg;
265 struct drm_mode_destroy_dumb destroy_arg;
266 struct drm_mode_map_dumb map_arg;
267
Peter Huttererf3d62272013-08-08 11:57:05 +1000268 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200269 if (!fb)
270 return NULL;
271
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300272 switch (format) {
273 case GBM_FORMAT_XRGB8888:
274 bpp = 32;
275 depth = 24;
276 break;
277 case GBM_FORMAT_RGB565:
278 bpp = depth = 16;
279 break;
280 default:
281 return NULL;
282 }
283
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700284 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300285 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200286 create_arg.width = width;
287 create_arg.height = height;
288
Giulio Camuffo954f1832014-10-11 18:27:30 +0300289 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200290 if (ret)
291 goto err_fb;
292
293 fb->handle = create_arg.handle;
294 fb->stride = create_arg.pitch;
295 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000296 fb->width = width;
297 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300298 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300300 ret = -1;
301
302 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700303 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300304
305 handles[0] = fb->handle;
306 pitches[0] = fb->stride;
307 offsets[0] = 0;
308
309 ret = drmModeAddFB2(b->drm.fd, width, height,
310 format, handles, pitches, offsets,
311 &fb->fb_id, 0);
312 if (ret) {
313 weston_log("addfb2 failed: %m\n");
314 b->no_addfb2 = 1;
315 }
316 }
317
318 if (ret) {
319 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
320 fb->stride, fb->handle, &fb->fb_id);
321 }
322
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200323 if (ret)
324 goto err_bo;
325
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700326 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200327 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400328 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200329 if (ret)
330 goto err_add_fb;
331
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500332 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300333 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200334 if (fb->map == MAP_FAILED)
335 goto err_add_fb;
336
337 return fb;
338
339err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300340 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200341err_bo:
342 memset(&destroy_arg, 0, sizeof(destroy_arg));
343 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300344 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200345err_fb:
346 free(fb);
347 return NULL;
348}
349
350static void
351drm_fb_destroy_dumb(struct drm_fb *fb)
352{
353 struct drm_mode_destroy_dumb destroy_arg;
354
355 if (!fb->map)
356 return;
357
358 if (fb->fb_id)
359 drmModeRmFB(fb->fd, fb->fb_id);
360
361 weston_buffer_reference(&fb->buffer_ref, NULL);
362
363 munmap(fb->map, fb->size);
364
365 memset(&destroy_arg, 0, sizeof(destroy_arg));
366 destroy_arg.handle = fb->handle;
367 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
368
369 free(fb);
370}
371
372static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500373drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300374 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300375{
376 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500377 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300378 int ret;
379
380 if (fb)
381 return fb;
382
Bryce Harringtonde16d892014-11-20 22:21:57 -0800383 fb = zalloc(sizeof *fb);
384 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200385 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300386
387 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300388
Daniel Stonec8c917c2016-11-14 17:45:58 +0000389 fb->width = gbm_bo_get_width(bo);
390 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200391 fb->stride = gbm_bo_get_stride(bo);
392 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000393 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300394 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300395
Daniel Stonec8c917c2016-11-14 17:45:58 +0000396 if (backend->min_width > fb->width ||
397 fb->width > backend->max_width ||
398 backend->min_height > fb->height ||
399 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200400 weston_log("bo geometry out of bounds\n");
401 goto err_free;
402 }
403
404 ret = -1;
405
Giulio Camuffo954f1832014-10-11 18:27:30 +0300406 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200407 handles[0] = fb->handle;
408 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200409 offsets[0] = 0;
410
Daniel Stonec8c917c2016-11-14 17:45:58 +0000411 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200412 format, handles, pitches, offsets,
413 &fb->fb_id, 0);
414 if (ret) {
415 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300416 backend->no_addfb2 = 1;
417 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200418 }
419 }
420
421 if (ret)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000422 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
423 24, 32, fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200424
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300425 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200426 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200427 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300428 }
429
430 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
431
432 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200433
434err_free:
435 free(fb);
436 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300437}
438
439static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500440drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200441{
Pekka Paalanende685b82012-12-04 15:58:12 +0200442 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200443
444 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200445
Pekka Paalanende685b82012-12-04 15:58:12 +0200446 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200447}
448
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200449static void
450drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
451{
452 if (!fb)
453 return;
454
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200455 if (fb->map &&
456 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200457 drm_fb_destroy_dumb(fb);
458 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200459 if (fb->is_client_buffer)
460 gbm_bo_destroy(fb->bo);
461 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100462 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600463 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200464 }
465}
466
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500467static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200468drm_output_check_scanout_format(struct drm_output *output,
469 struct weston_surface *es, struct gbm_bo *bo)
470{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200471 uint32_t format;
472 pixman_region32_t r;
473
474 format = gbm_bo_get_format(bo);
475
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700476 if (format == GBM_FORMAT_ARGB8888) {
477 /* We can scanout an ARGB buffer if the surface's
478 * opaque region covers the whole output, but we have
479 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800480 pixman_region32_init_rect(&r, 0, 0,
481 output->base.width,
482 output->base.height);
483 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200484
485 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500486 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200487
488 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500489 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700490
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100491 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700492 return format;
493
494 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200495}
496
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400497static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200498drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500499 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500500{
Armin Krezović545dba62016-08-05 15:54:18 +0200501 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500502 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200503 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300504 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500505 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500506
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507 if (ev->geometry.x != output->base.x ||
508 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300509 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200510 buffer->width != output->base.current_mode->width ||
511 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200512 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400514 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500515
Pekka Paalanen5580f222015-02-17 16:33:18 +0200516 if (ev->geometry.scissor_enabled)
517 return NULL;
518
Giulio Camuffo954f1832014-10-11 18:27:30 +0300519 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700520 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500521
Rob Bradford9b101872012-09-14 23:25:41 +0100522 /* Unable to use the buffer for scanout */
523 if (!bo)
524 return NULL;
525
Jason Ekstranda7af7042013-10-12 22:38:11 -0500526 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500527 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300528 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400529 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300530 }
531
Giulio Camuffo954f1832014-10-11 18:27:30 +0300532 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300533 if (!output->next) {
534 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400535 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300536 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500537
Pekka Paalanende685b82012-12-04 15:58:12 +0200538 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500539
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400540 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500541}
542
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500543static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200544drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400545{
Armin Krezović545dba62016-08-05 15:54:18 +0200546 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300547 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400548
Giulio Camuffo954f1832014-10-11 18:27:30 +0300549 output->base.compositor->renderer->repaint_output(&output->base,
550 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400551
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100552 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300553 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200554 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400555 return;
556 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300557
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100558 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300559 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200560 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100561 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300562 return;
563 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400564}
565
566static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200567drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
568{
569 struct weston_compositor *ec = output->base.compositor;
570 pixman_region32_t total_damage, previous_damage;
571
572 pixman_region32_init(&total_damage);
573 pixman_region32_init(&previous_damage);
574
575 pixman_region32_copy(&previous_damage, damage);
576
577 pixman_region32_union(&total_damage, damage, &output->previous_damage);
578 pixman_region32_copy(&output->previous_damage, &previous_damage);
579
580 output->current_image ^= 1;
581
582 output->next = output->dumb[output->current_image];
583 pixman_renderer_output_set_buffer(&output->base,
584 output->image[output->current_image]);
585
586 ec->renderer->repaint_output(&output->base, &total_damage);
587
588 pixman_region32_fini(&total_damage);
589 pixman_region32_fini(&previous_damage);
590}
591
592static void
593drm_output_render(struct drm_output *output, pixman_region32_t *damage)
594{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300595 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200596 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200597
Giulio Camuffo954f1832014-10-11 18:27:30 +0300598 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200599 drm_output_render_pixman(output, damage);
600 else
601 drm_output_render_gl(output, damage);
602
Giulio Camuffo954f1832014-10-11 18:27:30 +0300603 pixman_region32_subtract(&c->primary_plane.damage,
604 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200605}
606
607static void
Richard Hughese7299962013-05-01 21:52:12 +0100608drm_output_set_gamma(struct weston_output *output_base,
609 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
610{
611 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200612 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300613 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200614 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100615
616 /* check */
617 if (output_base->gamma_size != size)
618 return;
619 if (!output->original_crtc)
620 return;
621
Giulio Camuffo954f1832014-10-11 18:27:30 +0300622 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100623 output->crtc_id,
624 size, r, g, b);
625 if (rc)
626 weston_log("set gamma failed: %m\n");
627}
628
Bryce Harringtonada4f072015-06-30 13:25:46 -0700629/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200630 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700631 * The pipe parameter indicates which CRTC is in use. Knowing this, we
632 * can determine which vblank sequence type to use for it. Traditional
633 * cards had only two CRTCs, with CRTC 0 using no special flags, and
634 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
635 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200636 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700637 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
638 * 0-31. If this is non-zero it indicates we're dealing with a
639 * multi-gpu situation and we need to calculate the vblank sync
640 * using DRM_BLANK_HIGH_CRTC_MASK.
641 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300642static unsigned int
643drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200644{
645 if (output->pipe > 1)
646 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
647 DRM_VBLANK_HIGH_CRTC_MASK;
648 else if (output->pipe > 0)
649 return DRM_VBLANK_SECONDARY;
650 else
651 return 0;
652}
653
David Herrmann1edf44c2013-10-22 17:11:26 +0200654static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500655drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400656 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100657{
Armin Krezović545dba62016-08-05 15:54:18 +0200658 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300659 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200660 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500661 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400662 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500663 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100664
Armin Krezović08368132016-09-30 14:11:05 +0200665 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200666 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800667
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300668 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400669 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300670 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200671 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100672
Hardeningff39efa2013-09-18 23:56:35 +0200673 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200674 if (!output->current ||
675 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300676 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300677 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400678 &output->connector_id, 1,
679 &mode->mode_info);
680 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200681 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200682 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400683 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300684 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200685 }
686
Giulio Camuffo954f1832014-10-11 18:27:30 +0300687 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300688 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500689 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200690 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200691 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500692 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100693
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300694 output->page_flip_pending = 1;
695
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400696 drm_output_set_cursor(output);
697
Jesse Barnes58ef3792012-02-23 09:45:49 -0500698 /*
699 * Now, update all the sprite surfaces
700 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300701 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200702 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500703 drmVBlank vbl = {
704 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
705 .request.sequence = 1,
706 };
707
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200708 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000709 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500710 continue;
711
Giulio Camuffo954f1832014-10-11 18:27:30 +0300712 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200713 fb_id = s->next->fb_id;
714
Giulio Camuffo954f1832014-10-11 18:27:30 +0300715 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200716 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500717 s->dest_x, s->dest_y,
718 s->dest_w, s->dest_h,
719 s->src_x, s->src_y,
720 s->src_w, s->src_h);
721 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200722 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500723 ret, strerror(errno));
724
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200725 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500726
Jesse Barnes58ef3792012-02-23 09:45:49 -0500727 /*
728 * Queue a vblank signal so we know when the surface
729 * becomes active on the display or has been replaced.
730 */
731 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300732 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500733 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200734 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500735 ret, strerror(errno));
736 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300737
738 s->output = output;
739 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500740 }
741
David Herrmann1edf44c2013-10-22 17:11:26 +0200742 return 0;
743
744err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800745 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200746 if (output->next) {
747 drm_output_release_fb(output, output->next);
748 output->next = NULL;
749 }
750
751 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400752}
753
754static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200755drm_output_start_repaint_loop(struct weston_output *output_base)
756{
Armin Krezović545dba62016-08-05 15:54:18 +0200757 struct drm_output *output = to_drm_output(output_base);
758 struct drm_backend *backend =
759 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200760 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200761 struct timespec ts, tnow;
762 struct timespec vbl2now;
763 int64_t refresh_nsec;
764 int ret;
765 drmVBlank vbl = {
766 .request.type = DRM_VBLANK_RELATIVE,
767 .request.sequence = 0,
768 .request.signal = 0,
769 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300770
Armin Krezović08368132016-09-30 14:11:05 +0200771 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800772 return;
773
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300774 if (!output->current) {
775 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200776 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300777 }
778
Mario Kleinerf507ec32015-06-21 21:25:14 +0200779 /* Try to get current msc and timestamp via instant query */
780 vbl.request.type |= drm_waitvblank_pipe(output);
781 ret = drmWaitVBlank(backend->drm.fd, &vbl);
782
783 /* Error ret or zero timestamp means failure to get valid timestamp */
784 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
785 ts.tv_sec = vbl.reply.tval_sec;
786 ts.tv_nsec = vbl.reply.tval_usec * 1000;
787
788 /* Valid timestamp for most recent vblank - not stale?
789 * Stale ts could happen on Linux 3.17+, so make sure it
790 * is not older than 1 refresh duration since now.
791 */
792 weston_compositor_read_presentation_clock(backend->compositor,
793 &tnow);
794 timespec_sub(&vbl2now, &tnow, &ts);
795 refresh_nsec =
796 millihz_to_nsec(output->base.current_mode->refresh);
797 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
798 drm_output_update_msc(output, vbl.reply.sequence);
799 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200800 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200801 return;
802 }
803 }
804
805 /* Immediate query didn't provide valid timestamp.
806 * Use pageflip fallback.
807 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300808 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200809
Giulio Camuffo954f1832014-10-11 18:27:30 +0300810 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200811 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
812 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200813 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200814 }
David Herrmann3c688c52013-10-22 17:11:25 +0200815
816 return;
817
818finish_frame:
819 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300820 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200821 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200822 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200823}
824
825static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400826drm_output_update_msc(struct drm_output *output, unsigned int seq)
827{
828 uint64_t msc_hi = output->base.msc >> 32;
829
830 if (seq < (output->base.msc & 0xffffffff))
831 msc_hi++;
832
833 output->base.msc = (msc_hi << 32) + seq;
834}
835
836static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500837vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
838 void *data)
839{
840 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300841 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400842 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200843 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
844 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300845
Pekka Paalanen641307c2014-09-23 22:08:47 -0400846 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300847 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500848
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200849 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200850 s->current = s->next;
851 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300852
853 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400854 ts.tv_sec = sec;
855 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200856 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300857 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500858}
859
860static void
Armin Krezović08368132016-09-30 14:11:05 +0200861drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800862
863static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400864page_flip_handler(int fd, unsigned int frame,
865 unsigned int sec, unsigned int usec, void *data)
866{
Armin Krezović545dba62016-08-05 15:54:18 +0200867 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400868 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200869 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
870 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
871 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400872
Pekka Paalanen641307c2014-09-23 22:08:47 -0400873 drm_output_update_msc(output, frame);
874
Jonas Ådahle5a12252013-04-05 23:07:11 +0200875 /* We don't set page_flip_pending on start_repaint_loop, in that case
876 * we just want to page flip to the current buffer to get an accurate
877 * timestamp */
878 if (output->page_flip_pending) {
879 drm_output_release_fb(output, output->current);
880 output->current = output->next;
881 output->next = NULL;
882 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300883
Jonas Ådahle5a12252013-04-05 23:07:11 +0200884 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400885
Xiong Zhangabd5d472013-10-11 14:43:07 +0800886 if (output->destroy_pending)
887 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +0200888 else if (output->disable_pending)
889 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800890 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400891 ts.tv_sec = sec;
892 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200893 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300894
895 /* We can't call this from frame_notify, because the output's
896 * repaint needed flag is cleared just after that */
897 if (output->recorder)
898 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300899 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200900}
901
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500902static uint32_t
903drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500905{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500906 uint32_t i, format;
907
908 format = gbm_bo_get_format(bo);
909
910 if (format == GBM_FORMAT_ARGB8888) {
911 pixman_region32_t r;
912
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500913 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600914 ev->surface->width,
915 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500917
918 if (!pixman_region32_not_empty(&r))
919 format = GBM_FORMAT_XRGB8888;
920
921 pixman_region32_fini(&r);
922 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500923
924 for (i = 0; i < s->count_formats; i++)
925 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500926 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500927
928 return 0;
929}
930
931static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500933{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 return !ev->transform.enabled ||
935 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500936}
937
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200939drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500941{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200942 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200943 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200944 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300945 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500946 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300947 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500949 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500950 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200951 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400953 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500954
Giulio Camuffo954f1832014-10-11 18:27:30 +0300955 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200956 return NULL;
957
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200958 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200959 return NULL;
960
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200961 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200962 return NULL;
963
Giulio Camuffo954f1832014-10-11 18:27:30 +0300964 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400965 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500966
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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400971 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300972 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500973
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200975 return NULL;
976
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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400981 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500982
Giulio Camuffo954f1832014-10-11 18:27:30 +0300983 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +0000984 if (!drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500985 continue;
986
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200987 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500988 found = 1;
989 break;
990 }
991 }
992
993 /* No sprites available */
994 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400995 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500996
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300997 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -0700998#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300999 /* XXX: TODO:
1000 *
1001 * Use AddFB2 directly, do not go via GBM.
1002 * Add support for multiplanar formats.
1003 * Both require refactoring in the DRM-backend to
1004 * support a mix of gbm_bos and drmfbs.
1005 */
1006 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001007 .fd = dmabuf->attributes.fd[0],
1008 .width = dmabuf->attributes.width,
1009 .height = dmabuf->attributes.height,
1010 .stride = dmabuf->attributes.stride[0],
1011 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001012 };
1013
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001014 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001015 return NULL;
1016
1017 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1018 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001019#else
1020 return NULL;
1021#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001022 } else {
1023 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1024 buffer_resource, GBM_BO_USE_SCANOUT);
1025 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001026 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001027 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001028
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001030 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001031 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001032 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001033 }
1034
Giulio Camuffo954f1832014-10-11 18:27:30 +03001035 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001036 if (!s->next) {
1037 gbm_bo_destroy(bo);
1038 return NULL;
1039 }
1040
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001042
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001044 s->plane.x = box->x1;
1045 s->plane.y = box->y1;
1046
Jesse Barnes58ef3792012-02-23 09:45:49 -05001047 /*
1048 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001049 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001050 * for us already).
1051 */
1052 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001054 &output->base.region);
1055 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001056 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001057 tbox = weston_transformed_rect(output->base.width,
1058 output->base.height,
1059 output->base.transform,
1060 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001061 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001062 s->dest_x = tbox.x1;
1063 s->dest_y = tbox.y1;
1064 s->dest_w = tbox.x2 - tbox.x1;
1065 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001066 pixman_region32_fini(&dest_rect);
1067
1068 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001070 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001071 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001072
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 weston_view_from_global_fixed(ev,
1074 wl_fixed_from_int(box->x1),
1075 wl_fixed_from_int(box->y1),
1076 &sx1, &sy1);
1077 weston_view_from_global_fixed(ev,
1078 wl_fixed_from_int(box->x2),
1079 wl_fixed_from_int(box->y2),
1080 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001081
1082 if (sx1 < 0)
1083 sx1 = 0;
1084 if (sy1 < 0)
1085 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001086 if (sx2 > wl_fixed_from_int(ev->surface->width))
1087 sx2 = wl_fixed_from_int(ev->surface->width);
1088 if (sy2 > wl_fixed_from_int(ev->surface->height))
1089 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001090
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001091 tbox.x1 = sx1;
1092 tbox.y1 = sy1;
1093 tbox.x2 = sx2;
1094 tbox.y2 = sy2;
1095
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001096 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1097 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001098 viewport->buffer.transform,
1099 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001100 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001101
1102 s->src_x = tbox.x1 << 8;
1103 s->src_y = tbox.y1 << 8;
1104 s->src_w = (tbox.x2 - tbox.x1) << 8;
1105 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001106 pixman_region32_fini(&src_rect);
1107
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001108 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001109}
1110
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001111static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001112drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001114{
Armin Krezović545dba62016-08-05 15:54:18 +02001115 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001116 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001117 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001118
Derek Foremanbe428b32015-11-24 11:39:38 -06001119 if (ev->transform.enabled &&
1120 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1121 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001122 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001123 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001124 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1125 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001126 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001127 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001129 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001130 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001131 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001132 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001133 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001134 if (ev->geometry.scissor_enabled)
1135 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001136 if (ev->surface->buffer_ref.buffer == NULL)
1137 return NULL;
1138 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1139 if (!shmbuf)
1140 return NULL;
1141 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1142 return NULL;
1143 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001144 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001145 return NULL;
1146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001148
1149 return &output->cursor_plane;
1150}
1151
Pekka Paalanend0ead482014-06-16 12:05:40 +03001152/**
1153 * Update the image for the current cursor surface
1154 *
1155 * @param b DRM backend structure
1156 * @param bo GBM buffer object to write into
1157 * @param ev View to use for cursor image
1158 */
1159static void
1160cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1161 struct weston_view *ev)
1162{
1163 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1164 uint32_t buf[b->cursor_width * b->cursor_height];
1165 int32_t stride;
1166 uint8_t *s;
1167 int i;
1168
1169 assert(buffer && buffer->shm_buffer);
1170 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1171 assert(ev->surface->width <= b->cursor_width);
1172 assert(ev->surface->height <= b->cursor_height);
1173
1174 memset(buf, 0, sizeof buf);
1175 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1176 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1177
1178 wl_shm_buffer_begin_access(buffer->shm_buffer);
1179 for (i = 0; i < ev->surface->height; i++)
1180 memcpy(buf + i * b->cursor_width,
1181 s + i * stride,
1182 ev->surface->width * 4);
1183 wl_shm_buffer_end_access(buffer->shm_buffer);
1184
1185 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1186 weston_log("failed update cursor: %m\n");
1187}
1188
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001189static void
1190drm_output_set_cursor(struct drm_output *output)
1191{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001193 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001194 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001195 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001196 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001197 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 output->cursor_view = NULL;
1200 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001201 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001202 output->cursor_plane.x = INT32_MIN;
1203 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001204 return;
1205 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001206
Neil Robertse5051712013-11-13 15:44:06 +00001207 buffer = ev->surface->buffer_ref.buffer;
1208
1209 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001210 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001211 pixman_region32_fini(&output->cursor_plane.damage);
1212 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001213 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001214 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001215
Pekka Paalanend0ead482014-06-16 12:05:40 +03001216 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001217 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001218 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1219 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001220 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001221 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001222 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001223 }
1224
Derek Foremanbe428b32015-11-24 11:39:38 -06001225 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001226
1227 /* From global to output space, output transform is guaranteed to be
1228 * NORMAL by drm_output_prepare_cursor_view().
1229 */
1230 x = (x - output->base.x) * output->base.current_scale;
1231 y = (y - output->base.y) * output->base.current_scale;
1232
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001233 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001234 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001235 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001236 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001237 }
1238
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001239 output->cursor_plane.x = x;
1240 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001241 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001242}
1243
Jesse Barnes58ef3792012-02-23 09:45:49 -05001244static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001245drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001246{
Armin Krezović545dba62016-08-05 15:54:18 +02001247 struct drm_backend *b = to_drm_backend(output_base->compositor);
1248 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001250 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001251 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001252
1253 /*
1254 * Find a surface for each sprite in the output using some heuristics:
1255 * 1) size
1256 * 2) frequency of update
1257 * 3) opacity (though some hw might support alpha blending)
1258 * 4) clipping (this can be fixed with color keys)
1259 *
1260 * The idea is to save on blitting since this should save power.
1261 * If we can get a large video surface on the sprite for example,
1262 * the main display surface may not need to update at all, and
1263 * the client buffer can be used directly for the sprite surface
1264 * as we do for flipping full screen surfaces.
1265 */
1266 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001267 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268
Giulio Camuffo954f1832014-10-11 18:27:30 +03001269 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001270 struct weston_surface *es = ev->surface;
1271
1272 /* Test whether this buffer can ever go into a plane:
1273 * non-shm, or small enough to be a cursor.
1274 *
1275 * Also, keep a reference when using the pixman renderer.
1276 * That makes it possible to do a seamless switch to the GL
1277 * renderer and since the pixman renderer keeps a reference
1278 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001279 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001280 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001281 (es->buffer_ref.buffer &&
1282 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001283 (ev->surface->width <= b->cursor_width &&
1284 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001285 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001286 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001287 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001288
Jesse Barnes58ef3792012-02-23 09:45:49 -05001289 pixman_region32_init(&surface_overlap);
1290 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001292
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001293 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001294 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001295 next_plane = primary;
1296 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001298 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001300 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001302 if (next_plane == NULL)
1303 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001306
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001307 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001308 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001310
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001311 if (next_plane == primary ||
1312 next_plane == &output->cursor_plane) {
1313 /* cursor plane involves a copy */
1314 ev->psf_flags = 0;
1315 } else {
1316 /* All other planes are a direct scanout of a
1317 * single client buffer.
1318 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001319 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001320 }
1321
Jesse Barnes58ef3792012-02-23 09:45:49 -05001322 pixman_region32_fini(&surface_overlap);
1323 }
1324 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001325}
1326
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001327/**
1328 * Find the closest-matching mode for a given target
1329 *
1330 * Given a target mode, find the most suitable mode amongst the output's
1331 * current mode list to use, preferring the current mode if possible, to
1332 * avoid an expensive mode switch.
1333 *
1334 * @param output DRM output
1335 * @param target_mode Mode to attempt to match
1336 * @returns Pointer to a mode from the output's mode list
1337 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001338static struct drm_mode *
1339choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1340{
1341 struct drm_mode *tmp_mode = NULL, *mode;
1342
Hardeningff39efa2013-09-18 23:56:35 +02001343 if (output->base.current_mode->width == target_mode->width &&
1344 output->base.current_mode->height == target_mode->height &&
1345 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001346 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001347 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001348
1349 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1350 if (mode->mode_info.hdisplay == target_mode->width &&
1351 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001352 if (mode->base.refresh == target_mode->refresh ||
1353 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001354 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001355 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001356 tmp_mode = mode;
1357 }
1358 }
1359
1360 return tmp_mode;
1361}
1362
1363static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001364drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001365static void
1366drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001367static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001368drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001369static void
1370drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001371
1372static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001373drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1374{
1375 struct drm_output *output;
1376 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001377 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001378
1379 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001380 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001381 return -1;
1382 }
1383
1384 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001385 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001386 return -1;
1387 }
1388
Armin Krezović545dba62016-08-05 15:54:18 +02001389 b = to_drm_backend(output_base->compositor);
1390 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001391 drm_mode = choose_mode (output, mode);
1392
1393 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001394 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001395 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001396 }
1397
Hardeningff39efa2013-09-18 23:56:35 +02001398 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001399 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001400
Hardeningff39efa2013-09-18 23:56:35 +02001401 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001402
Hardeningff39efa2013-09-18 23:56:35 +02001403 output->base.current_mode = &drm_mode->base;
1404 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001405 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1406
Alex Wub7b8bda2012-04-17 17:20:48 +08001407 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001408 drm_output_release_fb(output, output->current);
1409 drm_output_release_fb(output, output->next);
1410 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001411
Giulio Camuffo954f1832014-10-11 18:27:30 +03001412 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001413 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001414 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001415 weston_log("failed to init output pixman state with "
1416 "new mode\n");
1417 return -1;
1418 }
1419 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00001420 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001421 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001422 weston_log("failed to init output egl state with "
1423 "new mode");
1424 return -1;
1425 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001426 }
1427
Alex Wub7b8bda2012-04-17 17:20:48 +08001428 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001429}
1430
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001431static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001432on_drm_input(int fd, uint32_t mask, void *data)
1433{
1434 drmEventContext evctx;
1435
1436 memset(&evctx, 0, sizeof evctx);
1437 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1438 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001439 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001440 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001441
1442 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001443}
1444
1445static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001446init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001447{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001448 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001449 uint64_t cap;
1450 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001451 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001452
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001453 sysnum = udev_device_get_sysnum(device);
1454 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001455 b->drm.id = atoi(sysnum);
1456 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001457 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001458 return -1;
1459 }
1460
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001461 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001462 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001463 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001464 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001465 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001466 udev_device_get_devnode(device));
1467 return -1;
1468 }
1469
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001470 weston_log("using %s\n", filename);
1471
Giulio Camuffo954f1832014-10-11 18:27:30 +03001472 b->drm.fd = fd;
1473 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001474
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001475 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1476 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001477 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001478 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001479 clk_id = CLOCK_REALTIME;
1480
Giulio Camuffo954f1832014-10-11 18:27:30 +03001481 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001482 weston_log("Error: failed to set presentation clock %d.\n",
1483 clk_id);
1484 return -1;
1485 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001486
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001487 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1488 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001489 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001490 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001491 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001492
1493 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1494 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001495 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001496 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001497 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001498
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001499 return 0;
1500}
1501
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001502static struct gbm_device *
1503create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001504{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001505 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001506
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001507 gl_renderer = weston_load_module("gl-renderer.so",
1508 "gl_renderer_interface");
1509 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001510 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001511
1512 /* GBM will load a dri driver, but even though they need symbols from
1513 * libglapi, in some version of Mesa they are not linked to it. Since
1514 * only the gl-renderer module links to it, the call above won't make
1515 * these symbols globally available, and loading the DRI driver fails.
1516 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1517 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1518
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001519 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001520
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001521 return gbm;
1522}
1523
Bryce Harringtonc056a982015-05-19 15:25:18 -07001524/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001525 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001526 *
1527 * This returns 0 if substitution isn't possible, but 0 might be a
1528 * legitimate format for other EGL platforms, so the caller is
1529 * responsible for checking for 0 before calling gl_renderer->create().
1530 *
1531 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1532 * but it's entirely possible we'll see this again on other implementations.
1533 */
1534static int
1535fallback_format_for(uint32_t format)
1536{
1537 switch (format) {
1538 case GBM_FORMAT_XRGB8888:
1539 return GBM_FORMAT_ARGB8888;
1540 case GBM_FORMAT_XRGB2101010:
1541 return GBM_FORMAT_ARGB2101010;
1542 default:
1543 return 0;
1544 }
1545}
1546
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001547static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001548drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001549{
Derek Foreman6d556372015-11-04 14:47:33 -06001550 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001551 b->gbm_format,
1552 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001553 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001554 };
Derek Foreman6d556372015-11-04 14:47:33 -06001555 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001556
Derek Foremanc4cfe852015-05-15 12:12:40 -05001557 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001558 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001559 if (gl_renderer->display_create(b->compositor,
1560 EGL_PLATFORM_GBM_KHR,
1561 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001562 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001563 gl_renderer->opaque_attribs,
1564 format,
1565 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001566 return -1;
1567 }
1568
1569 return 0;
1570}
1571
1572static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001573init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001574{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001575 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001576
Giulio Camuffo954f1832014-10-11 18:27:30 +03001577 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001578 return -1;
1579
Giulio Camuffo954f1832014-10-11 18:27:30 +03001580 if (drm_backend_create_gl_renderer(b) < 0) {
1581 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001582 return -1;
1583 }
1584
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001585 return 0;
1586}
1587
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001588static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001589init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001590{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001592}
1593
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001594/**
1595 * Add a mode to output's mode list
1596 *
1597 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1598 * output's mode list.
1599 *
1600 * @param output DRM output to add mode to
1601 * @param info DRM mode structure to add
1602 * @returns Newly-allocated Weston/DRM mode structure
1603 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001604static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001605drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001606{
1607 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001608 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001609
1610 mode = malloc(sizeof *mode);
1611 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001612 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001613
1614 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001615 mode->base.width = info->hdisplay;
1616 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001617
1618 /* Calculate higher precision (mHz) refresh rate */
1619 refresh = (info->clock * 1000000LL / info->htotal +
1620 info->vtotal / 2) / info->vtotal;
1621
1622 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1623 refresh *= 2;
1624 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1625 refresh /= 2;
1626 if (info->vscan > 1)
1627 refresh /= info->vscan;
1628
1629 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001630 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001631
1632 if (info->type & DRM_MODE_TYPE_PREFERRED)
1633 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1634
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001635 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1636
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001637 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001638}
1639
1640static int
1641drm_subpixel_to_wayland(int drm_value)
1642{
1643 switch (drm_value) {
1644 default:
1645 case DRM_MODE_SUBPIXEL_UNKNOWN:
1646 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1647 case DRM_MODE_SUBPIXEL_NONE:
1648 return WL_OUTPUT_SUBPIXEL_NONE;
1649 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1650 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1651 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1652 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1653 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1654 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1655 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1656 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1657 }
1658}
1659
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001660/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001661static uint32_t
1662drm_get_backlight(struct drm_output *output)
1663{
1664 long brightness, max_brightness, norm;
1665
1666 brightness = backlight_get_brightness(output->backlight);
1667 max_brightness = backlight_get_max_brightness(output->backlight);
1668
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001669 /* convert it on a scale of 0 to 255 */
1670 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001671
1672 return (uint32_t) norm;
1673}
1674
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001675/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001676static void
1677drm_set_backlight(struct weston_output *output_base, uint32_t value)
1678{
Armin Krezović545dba62016-08-05 15:54:18 +02001679 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001680 long max_brightness, new_brightness;
1681
1682 if (!output->backlight)
1683 return;
1684
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001685 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001686 return;
1687
1688 max_brightness = backlight_get_max_brightness(output->backlight);
1689
1690 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001691 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001692
1693 backlight_set_brightness(output->backlight, new_brightness);
1694}
1695
1696static drmModePropertyPtr
1697drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1698{
1699 drmModePropertyPtr props;
1700 int i;
1701
1702 for (i = 0; i < connector->count_props; i++) {
1703 props = drmModeGetProperty(fd, connector->props[i]);
1704 if (!props)
1705 continue;
1706
1707 if (!strcmp(props->name, name))
1708 return props;
1709
1710 drmModeFreeProperty(props);
1711 }
1712
1713 return NULL;
1714}
1715
1716static void
1717drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1718{
Armin Krezović545dba62016-08-05 15:54:18 +02001719 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001720 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001721 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001722 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001723
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001724 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001725 return;
1726
Daniel Stone36609c72015-06-18 07:49:02 +01001727 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1728 output->dpms_prop->prop_id, level);
1729 if (ret) {
1730 weston_log("DRM: DPMS: failed property set for %s\n",
1731 output->base.name);
1732 return;
1733 }
1734
1735 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001736}
1737
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001738static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001739 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1740 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1741 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1742 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1743 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1744 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1745 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1746 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1747 [DRM_MODE_CONNECTOR_Component] = "Component",
1748 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1749 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1750 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1751 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1752 [DRM_MODE_CONNECTOR_TV] = "TV",
1753 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001754#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001755 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1756 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001757#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001758};
1759
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001760static char *
1761make_connector_name(const drmModeConnector *con)
1762{
1763 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001764 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001765
1766 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1767 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001768
1769 if (!type_name)
1770 type_name = "UNNAMED";
1771
1772 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001773
1774 return strdup(name);
1775}
1776
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001777static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001778find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001779 drmModeRes *resources, drmModeConnector *connector)
1780{
1781 drmModeEncoder *encoder;
1782 uint32_t possible_crtcs;
1783 int i, j;
1784
1785 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001786 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001787 if (encoder == NULL) {
1788 weston_log("Failed to get encoder.\n");
1789 return -1;
1790 }
1791 possible_crtcs = encoder->possible_crtcs;
1792 drmModeFreeEncoder(encoder);
1793
1794 for (i = 0; i < resources->count_crtcs; i++) {
1795 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001796 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001797 return i;
1798 }
1799 }
1800
1801 return -1;
1802}
1803
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001804/* Init output state that depends on gl or gbm */
1805static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001806drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001807{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001808 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001809 output->gbm_format,
1810 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001811 };
1812 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001813
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001814 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001815 output->base.current_mode->width,
1816 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001817 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001818 GBM_BO_USE_SCANOUT |
1819 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001820 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001821 weston_log("failed to create gbm surface\n");
1822 return -1;
1823 }
1824
Derek Foremanc4cfe852015-05-15 12:12:40 -05001825 if (format[1])
1826 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001827 if (gl_renderer->output_window_create(&output->base,
1828 (EGLNativeWindowType)output->gbm_surface,
1829 output->gbm_surface,
1830 gl_renderer->opaque_attribs,
1831 format,
1832 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001833 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001834 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001835 return -1;
1836 }
1837
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001838 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001839
1840 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001841 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001842 continue;
1843
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001844 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001845 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001846 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001847 }
1848
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001849 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001850 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001851 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001852 }
1853
1854 return 0;
1855}
1856
Daniel Stone3e661f72016-11-04 17:24:06 +00001857static void
1858drm_output_fini_egl(struct drm_output *output)
1859{
1860 gl_renderer->output_destroy(&output->base);
1861 gbm_surface_destroy(output->gbm_surface);
1862}
1863
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001864static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001865drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001866{
Hardeningff39efa2013-09-18 23:56:35 +02001867 int w = output->base.current_mode->width;
1868 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001869 uint32_t format = output->gbm_format;
1870 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001871 unsigned int i;
1872
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001873 switch (format) {
1874 case GBM_FORMAT_XRGB8888:
1875 pixman_format = PIXMAN_x8r8g8b8;
1876 break;
1877 case GBM_FORMAT_RGB565:
1878 pixman_format = PIXMAN_r5g6b5;
1879 break;
1880 default:
1881 weston_log("Unsupported pixman format 0x%x\n", format);
1882 return -1;
1883 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001884
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001885 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001886 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001887 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001888 if (!output->dumb[i])
1889 goto err;
1890
1891 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001892 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001893 output->dumb[i]->map,
1894 output->dumb[i]->stride);
1895 if (!output->image[i])
1896 goto err;
1897 }
1898
1899 if (pixman_renderer_output_create(&output->base) < 0)
1900 goto err;
1901
1902 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001903 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001904
1905 return 0;
1906
1907err:
1908 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1909 if (output->dumb[i])
1910 drm_fb_destroy_dumb(output->dumb[i]);
1911 if (output->image[i])
1912 pixman_image_unref(output->image[i]);
1913
1914 output->dumb[i] = NULL;
1915 output->image[i] = NULL;
1916 }
1917
1918 return -1;
1919}
1920
1921static void
1922drm_output_fini_pixman(struct drm_output *output)
1923{
1924 unsigned int i;
1925
1926 pixman_renderer_output_destroy(&output->base);
1927 pixman_region32_fini(&output->previous_damage);
1928
1929 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1930 drm_fb_destroy_dumb(output->dumb[i]);
1931 pixman_image_unref(output->image[i]);
1932 output->dumb[i] = NULL;
1933 output->image[i] = NULL;
1934 }
1935}
1936
Richard Hughes2b2092a2013-04-24 14:58:02 +01001937static void
1938edid_parse_string(const uint8_t *data, char text[])
1939{
1940 int i;
1941 int replaced = 0;
1942
1943 /* this is always 12 bytes, but we can't guarantee it's null
1944 * terminated or not junk. */
1945 strncpy(text, (const char *) data, 12);
1946
Bryce Harrington9c7de162015-08-28 13:04:26 -07001947 /* guarantee our new string is null-terminated */
1948 text[12] = '\0';
1949
Richard Hughes2b2092a2013-04-24 14:58:02 +01001950 /* remove insane chars */
1951 for (i = 0; text[i] != '\0'; i++) {
1952 if (text[i] == '\n' ||
1953 text[i] == '\r') {
1954 text[i] = '\0';
1955 break;
1956 }
1957 }
1958
1959 /* ensure string is printable */
1960 for (i = 0; text[i] != '\0'; i++) {
1961 if (!isprint(text[i])) {
1962 text[i] = '-';
1963 replaced++;
1964 }
1965 }
1966
1967 /* if the string is random junk, ignore the string */
1968 if (replaced > 4)
1969 text[0] = '\0';
1970}
1971
1972#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1973#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1974#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1975#define EDID_OFFSET_DATA_BLOCKS 0x36
1976#define EDID_OFFSET_LAST_BLOCK 0x6c
1977#define EDID_OFFSET_PNPID 0x08
1978#define EDID_OFFSET_SERIAL 0x0c
1979
1980static int
1981edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1982{
1983 int i;
1984 uint32_t serial_number;
1985
1986 /* check header */
1987 if (length < 128)
1988 return -1;
1989 if (data[0] != 0x00 || data[1] != 0xff)
1990 return -1;
1991
1992 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1993 * /--08--\/--09--\
1994 * 7654321076543210
1995 * |\---/\---/\---/
1996 * R C1 C2 C3 */
1997 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1998 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1999 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2000 edid->pnp_id[3] = '\0';
2001
2002 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2003 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2004 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2005 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2006 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2007 if (serial_number > 0)
2008 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2009
2010 /* parse EDID data */
2011 for (i = EDID_OFFSET_DATA_BLOCKS;
2012 i <= EDID_OFFSET_LAST_BLOCK;
2013 i += 18) {
2014 /* ignore pixel clock data */
2015 if (data[i] != 0)
2016 continue;
2017 if (data[i+2] != 0)
2018 continue;
2019
2020 /* any useful blocks? */
2021 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2022 edid_parse_string(&data[i+5],
2023 edid->monitor_name);
2024 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2025 edid_parse_string(&data[i+5],
2026 edid->serial_number);
2027 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2028 edid_parse_string(&data[i+5],
2029 edid->eisa_id);
2030 }
2031 }
2032 return 0;
2033}
2034
2035static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002036find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002037 struct drm_output *output,
2038 drmModeConnector *connector)
2039{
2040 drmModePropertyBlobPtr edid_blob = NULL;
2041 drmModePropertyPtr property;
2042 int i;
2043 int rc;
2044
2045 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002046 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002047 if (!property)
2048 continue;
2049 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2050 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002051 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002052 connector->prop_values[i]);
2053 }
2054 drmModeFreeProperty(property);
2055 }
2056 if (!edid_blob)
2057 return;
2058
2059 rc = edid_parse(&output->edid,
2060 edid_blob->data,
2061 edid_blob->length);
2062 if (!rc) {
2063 weston_log("EDID data '%s', '%s', '%s'\n",
2064 output->edid.pnp_id,
2065 output->edid.monitor_name,
2066 output->edid.serial_number);
2067 if (output->edid.pnp_id[0] != '\0')
2068 output->base.make = output->edid.pnp_id;
2069 if (output->edid.monitor_name[0] != '\0')
2070 output->base.model = output->edid.monitor_name;
2071 if (output->edid.serial_number[0] != '\0')
2072 output->base.serial_number = output->edid.serial_number;
2073 }
2074 drmModeFreePropertyBlob(edid_blob);
2075}
2076
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002077
2078
2079static int
2080parse_modeline(const char *s, drmModeModeInfo *mode)
2081{
2082 char hsync[16];
2083 char vsync[16];
2084 float fclock;
2085
2086 mode->type = DRM_MODE_TYPE_USERDEF;
2087 mode->hskew = 0;
2088 mode->vscan = 0;
2089 mode->vrefresh = 0;
2090 mode->flags = 0;
2091
Rob Bradford307e09e2013-07-26 16:29:40 +01002092 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002093 &fclock,
2094 &mode->hdisplay,
2095 &mode->hsync_start,
2096 &mode->hsync_end,
2097 &mode->htotal,
2098 &mode->vdisplay,
2099 &mode->vsync_start,
2100 &mode->vsync_end,
2101 &mode->vtotal, hsync, vsync) != 11)
2102 return -1;
2103
2104 mode->clock = fclock * 1000;
2105 if (strcmp(hsync, "+hsync") == 0)
2106 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2107 else if (strcmp(hsync, "-hsync") == 0)
2108 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2109 else
2110 return -1;
2111
2112 if (strcmp(vsync, "+vsync") == 0)
2113 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2114 else if (strcmp(vsync, "-vsync") == 0)
2115 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2116 else
2117 return -1;
2118
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002119 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2120 mode->hdisplay, mode->vdisplay, fclock);
2121
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002122 return 0;
2123}
2124
Rob Bradford66bd9f52013-06-25 18:56:42 +01002125static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002126setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002127 struct weston_output *output,
2128 const char *s)
2129{
2130 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002131 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002132 struct udev_seat *seat;
2133
Giulio Camuffo954f1832014-10-11 18:27:30 +03002134 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002135 if (!seat)
2136 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002137
Derek Foreman0720ea32015-07-15 13:00:35 -05002138 seat->base.output = output;
2139
Derek Foreman1281a362015-07-31 16:55:32 -05002140 pointer = weston_seat_get_pointer(&seat->base);
2141 if (pointer)
2142 weston_pointer_clamp(pointer,
2143 &pointer->x,
2144 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002145 }
2146}
2147
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002148static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002149parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002150{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002151 int ret = 0;
2152
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002153 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002154 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002155 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002156 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002157 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002158 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002159 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002160 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002161 else {
2162 weston_log("fatal: unrecognized pixel format: %s\n", s);
2163 ret = -1;
2164 }
2165
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002166 return ret;
2167}
2168
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002169/**
2170 * Choose suitable mode for an output
2171 *
2172 * Find the most suitable mode to use for initial setup (or reconfiguration on
2173 * hotplug etc) for a DRM output.
2174 *
2175 * @param output DRM output to choose mode for
2176 * @param kind Strategy and preference to use when choosing mode
2177 * @param width Desired width for this output
2178 * @param height Desired height for this output
2179 * @param current_mode Mode currently being displayed on this output
2180 * @param modeline Manually-entered mode (may be NULL)
2181 * @returns A mode from the output's mode list, or NULL if none available
2182 */
2183static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002184drm_output_choose_initial_mode(struct drm_backend *backend,
2185 struct drm_output *output,
2186 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002187 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002188 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002189{
2190 struct drm_mode *preferred = NULL;
2191 struct drm_mode *current = NULL;
2192 struct drm_mode *configured = NULL;
2193 struct drm_mode *best = NULL;
2194 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002195 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002196 int32_t width = 0;
2197 int32_t height = 0;
2198
Armin Krezović08368132016-09-30 14:11:05 +02002199 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
2200 if (sscanf(modeline, "%dx%d", &width, &height) != 2) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002201 width = -1;
2202
Armin Krezović08368132016-09-30 14:11:05 +02002203 if (parse_modeline(modeline, &drm_modeline) == 0) {
2204 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002205 if (!configured)
2206 return NULL;
2207 } else {
2208 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002209 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002210 }
2211 }
2212 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002213
2214 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002215 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002216 height == drm_mode->base.height)
2217 configured = drm_mode;
2218
comic fans7a5c5622016-03-17 14:29:27 +02002219 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002220 sizeof *current_mode) == 0)
2221 current = drm_mode;
2222
2223 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2224 preferred = drm_mode;
2225
2226 best = drm_mode;
2227 }
2228
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002229 if (current == NULL && current_mode->clock != 0) {
2230 current = drm_output_add_mode(output, current_mode);
2231 if (!current)
2232 return NULL;
2233 }
2234
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002235 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002236 configured = current;
2237
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002238 if (configured)
2239 return configured;
2240
2241 if (preferred)
2242 return preferred;
2243
2244 if (current)
2245 return current;
2246
2247 if (best)
2248 return best;
2249
2250 weston_log("no available modes for %s\n", output->base.name);
2251 return NULL;
2252}
2253
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002254static int
2255connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2256 drmModeModeInfo *mode)
2257{
2258 drmModeEncoder *encoder;
2259 drmModeCrtc *crtc;
2260
2261 /* Get the current mode on the crtc that's currently driving
2262 * this connector. */
2263 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2264 memset(mode, 0, sizeof *mode);
2265 if (encoder != NULL) {
2266 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2267 drmModeFreeEncoder(encoder);
2268 if (crtc == NULL)
2269 return -1;
2270 if (crtc->mode_valid)
2271 *mode = crtc->mode;
2272 drmModeFreeCrtc(crtc);
2273 }
2274
2275 return 0;
2276}
2277
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002278static int
Armin Krezović08368132016-09-30 14:11:05 +02002279drm_output_set_mode(struct weston_output *base,
2280 enum weston_drm_backend_output_mode mode,
2281 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002282{
Armin Krezović08368132016-09-30 14:11:05 +02002283 struct drm_output *output = to_drm_output(base);
2284 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002285
Armin Krezović08368132016-09-30 14:11:05 +02002286 struct drm_mode *drm_mode, *next, *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002287 drmModeModeInfo crtc_mode;
2288 int i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002289
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002290 output->base.make = "unknown";
2291 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002292 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002293 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002294
Giulio Camuffo954f1832014-10-11 18:27:30 +03002295 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
Matt Roper361d2ad2011-08-29 13:52:23 -07002296
Armin Krezović08368132016-09-30 14:11:05 +02002297 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002298 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002299
Armin Krezović08368132016-09-30 14:11:05 +02002300 for (i = 0; i < output->connector->count_modes; i++) {
2301 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002302 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002303 goto err_free;
2304 }
2305
Armin Krezović08368132016-09-30 14:11:05 +02002306 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002307 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002308 goto err_free;
Armin Krezović08368132016-09-30 14:11:05 +02002309
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002310 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002311 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002312
Armin Krezović08368132016-09-30 14:11:05 +02002313 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2314 output->base.native_mode = output->base.current_mode;
2315 output->base.native_scale = output->base.current_scale;
2316
2317 output->base.mm_width = output->connector->mmWidth;
2318 output->base.mm_height = output->connector->mmHeight;
2319
2320 return 0;
2321
2322err_free:
2323 drmModeFreeCrtc(output->original_crtc);
2324 output->original_crtc = NULL;
2325
2326 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2327 base.link) {
2328 wl_list_remove(&drm_mode->base.link);
2329 free(drm_mode);
2330 }
2331
2332 return -1;
2333}
2334
2335static void
2336drm_output_set_gbm_format(struct weston_output *base,
2337 const char *gbm_format)
2338{
2339 struct drm_output *output = to_drm_output(base);
2340 struct drm_backend *b = to_drm_backend(base->compositor);
2341
2342 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2343 output->gbm_format = b->gbm_format;
2344}
2345
2346static void
2347drm_output_set_seat(struct weston_output *base,
2348 const char *seat)
2349{
2350 struct drm_output *output = to_drm_output(base);
2351 struct drm_backend *b = to_drm_backend(base->compositor);
2352
2353 setup_output_seat_constraint(b, &output->base,
2354 seat ? seat : "");
2355}
2356
2357static int
2358drm_output_enable(struct weston_output *base)
2359{
2360 struct drm_output *output = to_drm_output(base);
2361 struct drm_backend *b = to_drm_backend(base->compositor);
2362 struct weston_mode *m;
2363
2364 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002365
Giulio Camuffo954f1832014-10-11 18:27:30 +03002366 if (b->use_pixman) {
2367 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002368 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002369 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002370 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002371 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002372 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002373 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002374 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002375
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002376 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002377 weston_log("Initialized backlight, device %s\n",
2378 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002379 output->base.set_backlight = drm_set_backlight;
2380 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002381 } else {
2382 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002383 }
2384
Jonas Ådahle5a12252013-04-05 23:07:11 +02002385 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002386 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002387 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002388 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002389 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002390
Richard Hughese7299962013-05-01 21:52:12 +01002391 output->base.gamma_size = output->original_crtc->gamma_size;
2392 output->base.set_gamma = drm_output_set_gamma;
2393
Armin Krezović08368132016-09-30 14:11:05 +02002394 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2395
2396 find_and_parse_output_edid(b, output, output->connector);
2397 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2398 output->base.connection_internal = 1;
2399
Derek Foremanb0427562016-02-05 15:55:20 -06002400 weston_plane_init(&output->cursor_plane, b->compositor,
2401 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002402 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002403
Giulio Camuffo954f1832014-10-11 18:27:30 +03002404 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2405 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2406 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002407
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002408 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002409 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002410 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002411 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002412 m->width, m->height, m->refresh / 1000.0,
2413 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2414 ", preferred" : "",
2415 m->flags & WL_OUTPUT_MODE_CURRENT ?
2416 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002417 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002418 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002419
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002420 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002421
2422err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002423 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002424
David Herrmann0f0d54e2011-12-08 17:05:45 +01002425 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002426}
2427
Jesse Barnes58ef3792012-02-23 09:45:49 -05002428static void
Armin Krezović08368132016-09-30 14:11:05 +02002429drm_output_deinit(struct weston_output *base)
2430{
2431 struct drm_output *output = to_drm_output(base);
2432 struct drm_backend *b = to_drm_backend(base->compositor);
2433
Daniel Stone3e661f72016-11-04 17:24:06 +00002434 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02002435 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002436 else
2437 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02002438
2439 weston_plane_release(&output->fb_plane);
2440 weston_plane_release(&output->cursor_plane);
2441
2442 drmModeFreeProperty(output->dpms_prop);
2443
2444 /* Turn off hardware cursor */
2445 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2446}
2447
2448static void
2449drm_output_destroy(struct weston_output *base)
2450{
2451 struct drm_output *output = to_drm_output(base);
2452 struct drm_backend *b = to_drm_backend(base->compositor);
2453 drmModeCrtcPtr origcrtc = output->original_crtc;
2454
2455 if (output->page_flip_pending) {
2456 output->destroy_pending = 1;
2457 weston_log("destroy output while page flip pending\n");
2458 return;
2459 }
2460
2461 if (output->base.enabled)
2462 drm_output_deinit(&output->base);
2463
2464 if (origcrtc) {
2465 /* Restore original CRTC state */
2466 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2467 origcrtc->x, origcrtc->y,
2468 &output->connector_id, 1, &origcrtc->mode);
2469 drmModeFreeCrtc(origcrtc);
2470 }
2471
2472 weston_output_destroy(&output->base);
2473
2474 drmModeFreeConnector(output->connector);
2475
2476 if (output->backlight)
2477 backlight_destroy(output->backlight);
2478
2479 b->crtc_allocator &= ~(1 << output->crtc_id);
2480 b->connector_allocator &= ~(1 << output->connector_id);
2481
2482 free(output);
2483}
2484
2485static int
2486drm_output_disable(struct weston_output *base)
2487{
2488 struct drm_output *output = to_drm_output(base);
2489 struct drm_backend *b = to_drm_backend(base->compositor);
2490
2491 if (output->page_flip_pending) {
2492 output->disable_pending = 1;
2493 return -1;
2494 }
2495
2496 if (output->base.enabled)
2497 drm_output_deinit(&output->base);
2498
2499 output->disable_pending = 0;
2500
2501 weston_log("Disabling output %s\n", output->base.name);
2502 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2503 0, 0, 0, 0, 0, NULL);
2504
2505 return 0;
2506}
2507
2508/**
2509 * Create a Weston output structure
2510 *
2511 * Given a DRM connector, create a matching drm_output structure and add it
2512 * to Weston's output list. It also takes ownership of the connector, which
2513 * is released when output is destroyed.
2514 *
2515 * @param b Weston backend structure
2516 * @param resources DRM resources for this device
2517 * @param connector DRM connector to use for this new output
2518 * @param drm_device udev device pointer
2519 * @returns 0 on success, or -1 on failure
2520 */
2521static int
2522create_output_for_connector(struct drm_backend *b,
2523 drmModeRes *resources,
2524 drmModeConnector *connector,
2525 struct udev_device *drm_device)
2526{
2527 struct drm_output *output;
2528 int i;
2529
2530 i = find_crtc_for_connector(b, resources, connector);
2531 if (i < 0) {
2532 weston_log("No usable crtc/encoder pair for connector.\n");
2533 return -1;
2534 }
2535
2536 output = zalloc(sizeof *output);
2537 if (output == NULL)
2538 return -1;
2539
2540 output->connector = connector;
2541 output->crtc_id = resources->crtcs[i];
2542 output->pipe = i;
2543 output->connector_id = connector->connector_id;
2544
2545 output->backlight = backlight_init(drm_device,
2546 connector->connector_type);
2547
2548 output->base.enable = drm_output_enable;
2549 output->base.destroy = drm_output_destroy;
2550 output->base.disable = drm_output_disable;
2551 output->base.name = make_connector_name(connector);
2552
2553 output->destroy_pending = 0;
2554 output->disable_pending = 0;
2555 output->original_crtc = NULL;
2556
2557 b->crtc_allocator |= (1 << output->crtc_id);
2558 b->connector_allocator |= (1 << output->connector_id);
2559
Armin Krezović40087402016-09-30 14:11:12 +02002560 weston_output_init(&output->base, b->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002561 weston_compositor_add_pending_output(&output->base, b->compositor);
2562
2563 return 0;
2564}
2565
2566static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002567create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002568{
2569 struct drm_sprite *sprite;
2570 drmModePlaneRes *plane_res;
2571 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002572 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002573
Giulio Camuffo954f1832014-10-11 18:27:30 +03002574 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002575 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002576 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002577 strerror(errno));
2578 return;
2579 }
2580
2581 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002582 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002583 if (!plane)
2584 continue;
2585
Peter Huttererf3d62272013-08-08 11:57:05 +10002586 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002587 plane->count_formats));
2588 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002589 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002590 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002591 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002592 continue;
2593 }
2594
Jesse Barnes58ef3792012-02-23 09:45:49 -05002595 sprite->possible_crtcs = plane->possible_crtcs;
2596 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002597 sprite->current = NULL;
2598 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002599 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002600 sprite->count_formats = plane->count_formats;
2601 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002602 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002603 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002604 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2605 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2606 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002607
Giulio Camuffo954f1832014-10-11 18:27:30 +03002608 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002609 }
2610
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002611 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002612}
2613
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002614static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002615destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002616{
2617 struct drm_sprite *sprite, *next;
2618 struct drm_output *output;
2619
Giulio Camuffo954f1832014-10-11 18:27:30 +03002620 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002621 struct drm_output, base.link);
2622
Giulio Camuffo954f1832014-10-11 18:27:30 +03002623 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2624 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002625 sprite->plane_id,
2626 output->crtc_id, 0, 0,
2627 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002628 drm_output_release_fb(output, sprite->current);
2629 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002630 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002631 free(sprite);
2632 }
2633}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002634
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002635static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002636create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002637 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002638{
2639 drmModeConnector *connector;
2640 drmModeRes *resources;
2641 int i;
2642
Giulio Camuffo954f1832014-10-11 18:27:30 +03002643 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002644 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002645 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002646 return -1;
2647 }
2648
Giulio Camuffo954f1832014-10-11 18:27:30 +03002649 b->min_width = resources->min_width;
2650 b->max_width = resources->max_width;
2651 b->min_height = resources->min_height;
2652 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002653
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002654 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002655 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002656 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002657 if (connector == NULL)
2658 continue;
2659
2660 if (connector->connection == DRM_MODE_CONNECTED &&
2661 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002662 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002663 if (create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002664 connector, drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002665 drmModeFreeConnector(connector);
2666 continue;
2667 }
Armin Krezović08368132016-09-30 14:11:05 +02002668 } else {
2669 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002670 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002671 }
2672
Armin Krezović08368132016-09-30 14:11:05 +02002673 if (wl_list_empty(&b->compositor->output_list) &&
2674 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002675 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002676
2677 drmModeFreeResources(resources);
2678
2679 return 0;
2680}
2681
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002682static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002683update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002684{
2685 drmModeConnector *connector;
2686 drmModeRes *resources;
2687 struct drm_output *output, *next;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688 uint32_t connected = 0, disconnects = 0;
2689 int i;
2690
Giulio Camuffo954f1832014-10-11 18:27:30 +03002691 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002692 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002693 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002694 return;
2695 }
2696
2697 /* collect new connects */
2698 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002699 int connector_id = resources->connectors[i];
2700
Giulio Camuffo954f1832014-10-11 18:27:30 +03002701 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002702 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002703 continue;
2704
David Herrmann7551cff2011-12-08 17:05:43 +01002705 if (connector->connection != DRM_MODE_CONNECTED) {
2706 drmModeFreeConnector(connector);
2707 continue;
2708 }
2709
Benjamin Franzke117483d2011-08-30 11:38:26 +02002710 connected |= (1 << connector_id);
2711
Giulio Camuffo954f1832014-10-11 18:27:30 +03002712 if (!(b->connector_allocator & (1 << connector_id))) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002713 create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002714 connector, drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002715 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002716
Armin Krezović08368132016-09-30 14:11:05 +02002717 } else {
2718 drmModeFreeConnector(connector);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002719 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002720 }
2721 drmModeFreeResources(resources);
2722
Giulio Camuffo954f1832014-10-11 18:27:30 +03002723 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002724 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002725 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002726 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002727 if (disconnects & (1 << output->connector_id)) {
2728 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002729 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002730 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002731 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002732 }
2733 }
Armin Krezović08368132016-09-30 14:11:05 +02002734
2735 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2736 base.link) {
2737 if (disconnects & (1 << output->connector_id)) {
2738 disconnects &= ~(1 << output->connector_id);
2739 weston_log("connector %d disconnected\n",
2740 output->connector_id);
2741 drm_output_destroy(&output->base);
2742 }
2743 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002744 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002745}
2746
2747static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002748udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002749{
David Herrmannd7488c22012-03-11 20:05:21 +01002750 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002751 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002752
2753 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002754 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002755 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002756
David Herrmann6ac52db2012-03-11 20:05:22 +01002757 val = udev_device_get_property_value(device, "HOTPLUG");
2758 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759 return 0;
2760
David Herrmann6ac52db2012-03-11 20:05:22 +01002761 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002762}
2763
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002764static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002765udev_drm_event(int fd, uint32_t mask, void *data)
2766{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002767 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002768 struct udev_device *event;
2769
Giulio Camuffo954f1832014-10-11 18:27:30 +03002770 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002771
Giulio Camuffo954f1832014-10-11 18:27:30 +03002772 if (udev_event_is_hotplug(b, event))
2773 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002774
2775 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002776
2777 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002778}
2779
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002780static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002781drm_restore(struct weston_compositor *ec)
2782{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002783 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002784}
2785
2786static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002787drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002788{
Armin Krezović545dba62016-08-05 15:54:18 +02002789 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002790
Giulio Camuffo954f1832014-10-11 18:27:30 +03002791 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002792
Giulio Camuffo954f1832014-10-11 18:27:30 +03002793 wl_event_source_remove(b->udev_drm_source);
2794 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002795
Giulio Camuffo954f1832014-10-11 18:27:30 +03002796 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002797
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002798 weston_compositor_shutdown(ec);
2799
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800 if (b->gbm)
2801 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002802
Giulio Camuffo954f1832014-10-11 18:27:30 +03002803 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002804
Giulio Camuffo954f1832014-10-11 18:27:30 +03002805 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002806 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002807}
2808
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002809static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002810drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002811{
2812 struct drm_output *output;
2813 struct drm_mode *drm_mode;
2814 int ret;
2815
Giulio Camuffo954f1832014-10-11 18:27:30 +03002816 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002817 if (!output->current) {
2818 /* If something that would cause the output to
2819 * switch mode happened while in another vt, we
2820 * might not have a current drm_fb. In that case,
2821 * schedule a repaint and let drm_output_repaint
2822 * handle setting the mode. */
2823 weston_output_schedule_repaint(&output->base);
2824 continue;
2825 }
2826
Hardeningff39efa2013-09-18 23:56:35 +02002827 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002828 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002829 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002830 &output->connector_id, 1,
2831 &drm_mode->mode_info);
2832 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002833 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002834 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002835 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002836 output->base.x, output->base.y);
2837 }
2838 }
2839}
2840
2841static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002842session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002843{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002844 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002845 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002846 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002847 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002848
Giulio Camuffo954f1832014-10-11 18:27:30 +03002849 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002850 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002851 compositor->state = b->prev_state;
2852 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002853 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002854 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002855 } else {
2856 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002857 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002858
Giulio Camuffo954f1832014-10-11 18:27:30 +03002859 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002860 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002861
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002862 /* If we have a repaint scheduled (either from a
2863 * pending pageflip or the idle handler), make sure we
2864 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002865 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002866 * further attemps at repainting. When we switch
2867 * back, we schedule a repaint, which will process
2868 * pending frame callbacks. */
2869
Giulio Camuffo954f1832014-10-11 18:27:30 +03002870 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002871 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002872 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002873 }
2874
Giulio Camuffo954f1832014-10-11 18:27:30 +03002875 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002876 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002877
Giulio Camuffo954f1832014-10-11 18:27:30 +03002878 wl_list_for_each(sprite, &b->sprite_list, link)
2879 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002880 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002881 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002882 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002883 };
2884}
2885
David Herrmann0af066f2012-10-29 19:21:16 +01002886/*
2887 * Find primary GPU
2888 * Some systems may have multiple DRM devices attached to a single seat. This
2889 * function loops over all devices and tries to find a PCI device with the
2890 * boot_vga sysfs attribute set to 1.
2891 * If no such device is found, the first DRM device reported by udev is used.
2892 */
2893static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002894find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002895{
2896 struct udev_enumerate *e;
2897 struct udev_list_entry *entry;
2898 const char *path, *device_seat, *id;
2899 struct udev_device *device, *drm_device, *pci;
2900
Giulio Camuffo954f1832014-10-11 18:27:30 +03002901 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002902 udev_enumerate_add_match_subsystem(e, "drm");
2903 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2904
2905 udev_enumerate_scan_devices(e);
2906 drm_device = NULL;
2907 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2908 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002909 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002910 if (!device)
2911 continue;
2912 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2913 if (!device_seat)
2914 device_seat = default_seat;
2915 if (strcmp(device_seat, seat)) {
2916 udev_device_unref(device);
2917 continue;
2918 }
2919
2920 pci = udev_device_get_parent_with_subsystem_devtype(device,
2921 "pci", NULL);
2922 if (pci) {
2923 id = udev_device_get_sysattr_value(pci, "boot_vga");
2924 if (id && !strcmp(id, "1")) {
2925 if (drm_device)
2926 udev_device_unref(drm_device);
2927 drm_device = device;
2928 break;
2929 }
2930 }
2931
2932 if (!drm_device)
2933 drm_device = device;
2934 else
2935 udev_device_unref(device);
2936 }
2937
2938 udev_enumerate_unref(e);
2939 return drm_device;
2940}
2941
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002942static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002943planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2944 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002945{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002946 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002947
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002948 switch (key) {
2949 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002950 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002951 break;
2952 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002953 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002954 break;
2955 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002956 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002957 break;
2958 default:
2959 break;
2960 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002961}
2962
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002963#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002964static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002965recorder_destroy(struct drm_output *output)
2966{
2967 vaapi_recorder_destroy(output->recorder);
2968 output->recorder = NULL;
2969
2970 output->base.disable_planes--;
2971
2972 wl_list_remove(&output->recorder_frame_listener.link);
2973 weston_log("[libva recorder] done\n");
2974}
2975
2976static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002977recorder_frame_notify(struct wl_listener *listener, void *data)
2978{
2979 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002980 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002981 int fd, ret;
2982
2983 output = container_of(listener, struct drm_output,
2984 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002985 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002986
2987 if (!output->recorder)
2988 return;
2989
Giulio Camuffo954f1832014-10-11 18:27:30 +03002990 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002991 DRM_CLOEXEC, &fd);
2992 if (ret) {
2993 weston_log("[libva recorder] "
2994 "failed to create prime fd for front buffer\n");
2995 return;
2996 }
2997
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002998 ret = vaapi_recorder_frame(output->recorder, fd,
2999 output->current->stride);
3000 if (ret < 0) {
3001 weston_log("[libva recorder] aborted: %m\n");
3002 recorder_destroy(output);
3003 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003004}
3005
3006static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003007create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008 const char *filename)
3009{
3010 int fd;
3011 drm_magic_t magic;
3012
Giulio Camuffo954f1832014-10-11 18:27:30 +03003013 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003014 if (fd < 0)
3015 return NULL;
3016
3017 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003018 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003019
3020 return vaapi_recorder_create(fd, width, height, filename);
3021}
3022
3023static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003024recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003025 void *data)
3026{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003027 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003028 struct drm_output *output;
3029 int width, height;
3030
Giulio Camuffo954f1832014-10-11 18:27:30 +03003031 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003032 struct drm_output, base.link);
3033
3034 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003035 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003036 weston_log("failed to start vaapi recorder: "
3037 "output format not supported\n");
3038 return;
3039 }
3040
Hardeningff39efa2013-09-18 23:56:35 +02003041 width = output->base.current_mode->width;
3042 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003043
3044 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003045 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003046 if (!output->recorder) {
3047 weston_log("failed to create vaapi recorder\n");
3048 return;
3049 }
3050
3051 output->base.disable_planes++;
3052
3053 output->recorder_frame_listener.notify = recorder_frame_notify;
3054 wl_signal_add(&output->base.frame_signal,
3055 &output->recorder_frame_listener);
3056
3057 weston_output_schedule_repaint(&output->base);
3058
3059 weston_log("[libva recorder] initialized\n");
3060 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003061 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003062 }
3063}
3064#else
3065static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003066recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003067 void *data)
3068{
3069 weston_log("Compiled without libva support\n");
3070}
3071#endif
3072
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003073static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003074switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003075{
3076 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003077 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003078
Giulio Camuffo954f1832014-10-11 18:27:30 +03003079 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003080 return;
3081
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003082 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3083
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003084 weston_log("Switching to GL renderer\n");
3085
Giulio Camuffo954f1832014-10-11 18:27:30 +03003086 b->gbm = create_gbm_device(b->drm.fd);
3087 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003088 weston_log("Failed to create gbm device. "
3089 "Aborting renderer switch\n");
3090 return;
3091 }
3092
Giulio Camuffo954f1832014-10-11 18:27:30 +03003093 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003094 pixman_renderer_output_destroy(&output->base);
3095
Giulio Camuffo954f1832014-10-11 18:27:30 +03003096 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003097
Giulio Camuffo954f1832014-10-11 18:27:30 +03003098 if (drm_backend_create_gl_renderer(b) < 0) {
3099 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003100 weston_log("Failed to create GL renderer. Quitting.\n");
3101 /* FIXME: we need a function to shutdown cleanly */
3102 assert(0);
3103 }
3104
Giulio Camuffo954f1832014-10-11 18:27:30 +03003105 wl_list_for_each(output, &b->compositor->output_list, base.link)
3106 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003107
Giulio Camuffo954f1832014-10-11 18:27:30 +03003108 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003109
3110 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3111 if (linux_dmabuf_setup(b->compositor) < 0)
3112 weston_log("Error: initializing dmabuf "
3113 "support failed.\n");
3114 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003115}
3116
3117static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003118renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3119 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003120{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003121 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003122 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003123
Giulio Camuffo954f1832014-10-11 18:27:30 +03003124 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003125}
3126
Armin Krezović08368132016-09-30 14:11:05 +02003127static const struct weston_drm_output_api api = {
3128 drm_output_set_mode,
3129 drm_output_set_gbm_format,
3130 drm_output_set_seat,
3131};
3132
Giulio Camuffo954f1832014-10-11 18:27:30 +03003133static struct drm_backend *
3134drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003135 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003136{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003137 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003138 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003139 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003140 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003141 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003142 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003143
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003144 weston_log("initializing drm backend\n");
3145
Giulio Camuffo954f1832014-10-11 18:27:30 +03003146 b = zalloc(sizeof *b);
3147 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003148 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003149
Pekka Paalanen68583832015-05-19 09:53:16 +03003150 /*
3151 * KMS support for hardware planes cannot properly synchronize
3152 * without nuclear page flip. Without nuclear/atomic, hw plane
3153 * and cursor plane updates would either tear or cause extra
3154 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003155 * to a fraction. For cursors, it's not so bad, so they are
3156 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003157 *
3158 * These can be enabled again when nuclear/atomic support lands.
3159 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003160 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003161 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003162 b->use_pixman = config->use_pixman;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003163 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003164
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003165 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3166 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003167
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003168 if (config->seat_id)
3169 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003170
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003171 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003172 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3173 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003174 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003175 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003176 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003177 goto err_compositor;
3178 }
3179
Giulio Camuffo954f1832014-10-11 18:27:30 +03003180 b->udev = udev_new();
3181 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003182 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003183 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003184 }
3185
Giulio Camuffo954f1832014-10-11 18:27:30 +03003186 b->session_listener.notify = session_notify;
3187 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003188
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003189 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003190 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003191 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003192 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003193 }
David Herrmann0af066f2012-10-29 19:21:16 +01003194 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003195
Giulio Camuffo954f1832014-10-11 18:27:30 +03003196 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003197 weston_log("failed to initialize kms\n");
3198 goto err_udev_dev;
3199 }
3200
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 if (b->use_pixman) {
3202 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003203 weston_log("failed to initialize pixman renderer\n");
3204 goto err_udev_dev;
3205 }
3206 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003207 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003208 weston_log("failed to initialize egl\n");
3209 goto err_udev_dev;
3210 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003211 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003212
Giulio Camuffo954f1832014-10-11 18:27:30 +03003213 b->base.destroy = drm_destroy;
3214 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003215
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003217
Bob Ham91880f12016-01-12 10:21:47 +00003218 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003219
Giulio Camuffo954f1832014-10-11 18:27:30 +03003220 wl_list_init(&b->sprite_list);
3221 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003222
Giulio Camuffo954f1832014-10-11 18:27:30 +03003223 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003224 compositor, b->udev, seat_id,
3225 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003226 weston_log("failed to create input devices\n");
3227 goto err_sprite;
3228 }
3229
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003230 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003231 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003232 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003233 }
3234
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003235 /* A this point we have some idea of whether or not we have a working
3236 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003237 if (!b->cursors_are_broken)
3238 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003239
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003240 path = NULL;
3241
Giulio Camuffo954f1832014-10-11 18:27:30 +03003242 loop = wl_display_get_event_loop(compositor->wl_display);
3243 b->drm_source =
3244 wl_event_loop_add_fd(loop, b->drm.fd,
3245 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003246
Giulio Camuffo954f1832014-10-11 18:27:30 +03003247 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3248 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003249 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003250 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003251 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003252 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003253 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003255 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003256 udev_monitor_get_fd(b->udev_monitor),
3257 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003258
Giulio Camuffo954f1832014-10-11 18:27:30 +03003259 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003260 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003261 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003262 }
3263
Daniel Stonea96b93c2012-06-22 14:04:37 +01003264 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003265
Giulio Camuffo954f1832014-10-11 18:27:30 +03003266 weston_compositor_add_debug_binding(compositor, KEY_O,
3267 planes_binding, b);
3268 weston_compositor_add_debug_binding(compositor, KEY_C,
3269 planes_binding, b);
3270 weston_compositor_add_debug_binding(compositor, KEY_V,
3271 planes_binding, b);
3272 weston_compositor_add_debug_binding(compositor, KEY_Q,
3273 recorder_binding, b);
3274 weston_compositor_add_debug_binding(compositor, KEY_W,
3275 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003276
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003277 if (compositor->renderer->import_dmabuf) {
3278 if (linux_dmabuf_setup(compositor) < 0)
3279 weston_log("Error: initializing dmabuf "
3280 "support failed.\n");
3281 }
3282
Giulio Camuffo954f1832014-10-11 18:27:30 +03003283 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003284
Armin Krezović08368132016-09-30 14:11:05 +02003285 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3286 &api, sizeof(api));
3287
3288 if (ret < 0) {
3289 weston_log("Failed to register output API.\n");
3290 goto err_udev_monitor;
3291 }
3292
Giulio Camuffo954f1832014-10-11 18:27:30 +03003293 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003294
3295err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003296 wl_event_source_remove(b->udev_drm_source);
3297 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003298err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003299 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003300err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003301 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003302err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003303 if (b->gbm)
3304 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003305 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003306err_udev_dev:
3307 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003308err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003309 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003310err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003311 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003312err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003313 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003314 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003315 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003316}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003317
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003318static void
3319config_init_to_defaults(struct weston_drm_backend_config *config)
3320{
3321}
3322
Giulio Camuffo954f1832014-10-11 18:27:30 +03003323WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003324backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003325 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003326{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003327 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003328 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003329
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003330 if (config_base == NULL ||
3331 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3332 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3333 weston_log("drm backend config structure is invalid\n");
3334 return -1;
3335 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003336
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003337 config_init_to_defaults(&config);
3338 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003339
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003340 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003341 if (b == NULL)
3342 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003343
Giulio Camuffo954f1832014-10-11 18:27:30 +03003344 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003345}