blob: a8992138ffb4f9b65aa58a5aad111171b180e042 [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;
Jesse Barnes58ef3792012-02-23 09:45:49 -050096 uint32_t *crtcs;
97 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050098 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010099 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700100 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100101 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200102
Rob Clark4339add2012-08-09 14:18:28 -0500103 /* we need these parameters in order to not fail drmModeAddFB2()
104 * due to out of bounds dimensions, and then mistakenly set
105 * sprites_are_broken:
106 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200107 uint32_t min_width, max_width;
108 uint32_t min_height, max_height;
109 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500110
Jesse Barnes58ef3792012-02-23 09:45:49 -0500111 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500112 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200113 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500114
Rob Clarkab5b1e32012-08-09 13:24:45 -0500115 int cursors_are_broken;
116
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200117 int use_pixman;
118
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200119 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300120
Rob Bradfordd355b802013-05-31 18:09:55 +0100121 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300122
Daniel Stone70d337d2015-06-16 18:42:23 +0100123 int32_t cursor_width;
124 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700125
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700126 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400127};
128
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400129struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500130 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400131 drmModeModeInfo mode_info;
132};
133
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300134struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200135 uint32_t fb_id, stride, handle, size;
136 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300137 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200138 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200139
140 /* Used by gbm fbs */
141 struct gbm_bo *bo;
142
143 /* Used by dumb fbs */
144 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300145};
146
Richard Hughes2b2092a2013-04-24 14:58:02 +0100147struct drm_edid {
148 char eisa_id[13];
149 char monitor_name[13];
150 char pnp_id[5];
151 char serial_number[13];
152};
153
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400154struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200155 struct weston_output base;
156 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400157
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400158 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500159 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400160 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700161 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100162 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300163 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100164 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200165
Daniel Stone36609c72015-06-18 07:49:02 +0100166 enum dpms_enum dpms;
167
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300168 int vblank_pending;
169 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800170 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200171 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300172
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100173 struct gbm_surface *gbm_surface;
174 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400175 struct weston_plane cursor_plane;
176 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500177 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400178 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300179 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200180 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200181
182 struct drm_fb *dumb[2];
183 pixman_image_t *image[2];
184 int current_image;
185 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300186
187 struct vaapi_recorder *recorder;
188 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400189};
190
Jesse Barnes58ef3792012-02-23 09:45:49 -0500191/*
192 * An output has a primary display plane plus zero or more sprites for
193 * blending display contents.
194 */
195struct drm_sprite {
196 struct wl_list link;
197
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400198 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500199
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200200 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300201 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300202 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500203
Jesse Barnes58ef3792012-02-23 09:45:49 -0500204 uint32_t possible_crtcs;
205 uint32_t plane_id;
206 uint32_t count_formats;
207
208 int32_t src_x, src_y;
209 uint32_t src_w, src_h;
210 uint32_t dest_x, dest_y;
211 uint32_t dest_w, dest_h;
212
213 uint32_t formats[];
214};
215
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300216static struct gl_renderer_interface *gl_renderer;
217
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500218static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400219
Armin Krezović545dba62016-08-05 15:54:18 +0200220static inline struct drm_output *
221to_drm_output(struct weston_output *base)
222{
223 return container_of(base, struct drm_output, base);
224}
225
226static inline struct drm_backend *
227to_drm_backend(struct weston_compositor *base)
228{
229 return container_of(base->backend, struct drm_backend, base);
230}
231
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400232static void
233drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400234
Mario Kleinerf507ec32015-06-21 21:25:14 +0200235static void
236drm_output_update_msc(struct drm_output *output, unsigned int seq);
237
Jesse Barnes58ef3792012-02-23 09:45:49 -0500238static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200239drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500240{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200241 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200242 struct drm_backend *b = to_drm_backend(ec);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500243 int crtc;
244
Giulio Camuffo954f1832014-10-11 18:27:30 +0300245 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
246 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500247 continue;
248
249 if (supported & (1 << crtc))
250 return -1;
251 }
252
253 return 0;
254}
255
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300256static void
257drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
258{
259 struct drm_fb *fb = data;
260 struct gbm_device *gbm = gbm_bo_get_device(bo);
261
262 if (fb->fb_id)
263 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
264
Pekka Paalanende685b82012-12-04 15:58:12 +0200265 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300266
267 free(data);
268}
269
270static struct drm_fb *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300271drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
272 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200273{
274 struct drm_fb *fb;
275 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300276 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200277
278 struct drm_mode_create_dumb create_arg;
279 struct drm_mode_destroy_dumb destroy_arg;
280 struct drm_mode_map_dumb map_arg;
281
Peter Huttererf3d62272013-08-08 11:57:05 +1000282 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200283 if (!fb)
284 return NULL;
285
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300286 switch (format) {
287 case GBM_FORMAT_XRGB8888:
288 bpp = 32;
289 depth = 24;
290 break;
291 case GBM_FORMAT_RGB565:
292 bpp = depth = 16;
293 break;
294 default:
295 return NULL;
296 }
297
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700298 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300299 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200300 create_arg.width = width;
301 create_arg.height = height;
302
Giulio Camuffo954f1832014-10-11 18:27:30 +0300303 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200304 if (ret)
305 goto err_fb;
306
307 fb->handle = create_arg.handle;
308 fb->stride = create_arg.pitch;
309 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300310 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200311
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300312 ret = -1;
313
314 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700315 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300316
317 handles[0] = fb->handle;
318 pitches[0] = fb->stride;
319 offsets[0] = 0;
320
321 ret = drmModeAddFB2(b->drm.fd, width, height,
322 format, handles, pitches, offsets,
323 &fb->fb_id, 0);
324 if (ret) {
325 weston_log("addfb2 failed: %m\n");
326 b->no_addfb2 = 1;
327 }
328 }
329
330 if (ret) {
331 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
332 fb->stride, fb->handle, &fb->fb_id);
333 }
334
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200335 if (ret)
336 goto err_bo;
337
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700338 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200339 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400340 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200341 if (ret)
342 goto err_add_fb;
343
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500344 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300345 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200346 if (fb->map == MAP_FAILED)
347 goto err_add_fb;
348
349 return fb;
350
351err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300352 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200353err_bo:
354 memset(&destroy_arg, 0, sizeof(destroy_arg));
355 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300356 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200357err_fb:
358 free(fb);
359 return NULL;
360}
361
362static void
363drm_fb_destroy_dumb(struct drm_fb *fb)
364{
365 struct drm_mode_destroy_dumb destroy_arg;
366
367 if (!fb->map)
368 return;
369
370 if (fb->fb_id)
371 drmModeRmFB(fb->fd, fb->fb_id);
372
373 weston_buffer_reference(&fb->buffer_ref, NULL);
374
375 munmap(fb->map, fb->size);
376
377 memset(&destroy_arg, 0, sizeof(destroy_arg));
378 destroy_arg.handle = fb->handle;
379 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
380
381 free(fb);
382}
383
384static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500385drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300386 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300387{
388 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200389 uint32_t width, height;
Derek Foreman482ffdf2016-07-08 12:50:57 -0500390 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300391 int ret;
392
393 if (fb)
394 return fb;
395
Bryce Harringtonde16d892014-11-20 22:21:57 -0800396 fb = zalloc(sizeof *fb);
397 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200398 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300399
400 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300401
402 width = gbm_bo_get_width(bo);
403 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200404 fb->stride = gbm_bo_get_stride(bo);
405 fb->handle = gbm_bo_get_handle(bo).u32;
406 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300407 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300408
Giulio Camuffo954f1832014-10-11 18:27:30 +0300409 if (backend->min_width > width || width > backend->max_width ||
410 backend->min_height > height ||
411 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200412 weston_log("bo geometry out of bounds\n");
413 goto err_free;
414 }
415
416 ret = -1;
417
Giulio Camuffo954f1832014-10-11 18:27:30 +0300418 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200419 handles[0] = fb->handle;
420 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200421 offsets[0] = 0;
422
Giulio Camuffo954f1832014-10-11 18:27:30 +0300423 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200424 format, handles, pitches, offsets,
425 &fb->fb_id, 0);
426 if (ret) {
427 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300428 backend->no_addfb2 = 1;
429 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200430 }
431 }
432
433 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300434 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200435 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200436
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300437 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200438 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200439 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300440 }
441
442 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
443
444 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200445
446err_free:
447 free(fb);
448 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300449}
450
451static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500452drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200453{
Pekka Paalanende685b82012-12-04 15:58:12 +0200454 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200455
456 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200457
Pekka Paalanende685b82012-12-04 15:58:12 +0200458 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200459}
460
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200461static void
462drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
463{
464 if (!fb)
465 return;
466
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200467 if (fb->map &&
468 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200469 drm_fb_destroy_dumb(fb);
470 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200471 if (fb->is_client_buffer)
472 gbm_bo_destroy(fb->bo);
473 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100474 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600475 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200476 }
477}
478
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500479static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200480drm_output_check_scanout_format(struct drm_output *output,
481 struct weston_surface *es, struct gbm_bo *bo)
482{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200483 uint32_t format;
484 pixman_region32_t r;
485
486 format = gbm_bo_get_format(bo);
487
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700488 if (format == GBM_FORMAT_ARGB8888) {
489 /* We can scanout an ARGB buffer if the surface's
490 * opaque region covers the whole output, but we have
491 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800492 pixman_region32_init_rect(&r, 0, 0,
493 output->base.width,
494 output->base.height);
495 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200496
497 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500498 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200499
500 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500501 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700502
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100503 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700504 return format;
505
506 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200507}
508
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400509static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200510drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500512{
Armin Krezović545dba62016-08-05 15:54:18 +0200513 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200515 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300516 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500517 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500518
Jason Ekstranda7af7042013-10-12 22:38:11 -0500519 if (ev->geometry.x != output->base.x ||
520 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300521 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200522 buffer->width != output->base.current_mode->width ||
523 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200524 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400526 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500527
Pekka Paalanen5580f222015-02-17 16:33:18 +0200528 if (ev->geometry.scissor_enabled)
529 return NULL;
530
Giulio Camuffo954f1832014-10-11 18:27:30 +0300531 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700532 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500533
Rob Bradford9b101872012-09-14 23:25:41 +0100534 /* Unable to use the buffer for scanout */
535 if (!bo)
536 return NULL;
537
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500539 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300540 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400541 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300542 }
543
Giulio Camuffo954f1832014-10-11 18:27:30 +0300544 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300545 if (!output->next) {
546 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400547 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300548 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500549
Pekka Paalanende685b82012-12-04 15:58:12 +0200550 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500551
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400552 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500553}
554
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500555static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200556drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400557{
Armin Krezović545dba62016-08-05 15:54:18 +0200558 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300559 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400560
Giulio Camuffo954f1832014-10-11 18:27:30 +0300561 output->base.compositor->renderer->repaint_output(&output->base,
562 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400563
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100564 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300565 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200566 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400567 return;
568 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300569
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100570 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300571 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200572 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100573 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300574 return;
575 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400576}
577
578static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200579drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
580{
581 struct weston_compositor *ec = output->base.compositor;
582 pixman_region32_t total_damage, previous_damage;
583
584 pixman_region32_init(&total_damage);
585 pixman_region32_init(&previous_damage);
586
587 pixman_region32_copy(&previous_damage, damage);
588
589 pixman_region32_union(&total_damage, damage, &output->previous_damage);
590 pixman_region32_copy(&output->previous_damage, &previous_damage);
591
592 output->current_image ^= 1;
593
594 output->next = output->dumb[output->current_image];
595 pixman_renderer_output_set_buffer(&output->base,
596 output->image[output->current_image]);
597
598 ec->renderer->repaint_output(&output->base, &total_damage);
599
600 pixman_region32_fini(&total_damage);
601 pixman_region32_fini(&previous_damage);
602}
603
604static void
605drm_output_render(struct drm_output *output, pixman_region32_t *damage)
606{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300607 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200608 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200609
Giulio Camuffo954f1832014-10-11 18:27:30 +0300610 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200611 drm_output_render_pixman(output, damage);
612 else
613 drm_output_render_gl(output, damage);
614
Giulio Camuffo954f1832014-10-11 18:27:30 +0300615 pixman_region32_subtract(&c->primary_plane.damage,
616 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200617}
618
619static void
Richard Hughese7299962013-05-01 21:52:12 +0100620drm_output_set_gamma(struct weston_output *output_base,
621 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
622{
623 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200624 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300625 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200626 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100627
628 /* check */
629 if (output_base->gamma_size != size)
630 return;
631 if (!output->original_crtc)
632 return;
633
Giulio Camuffo954f1832014-10-11 18:27:30 +0300634 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100635 output->crtc_id,
636 size, r, g, b);
637 if (rc)
638 weston_log("set gamma failed: %m\n");
639}
640
Bryce Harringtonada4f072015-06-30 13:25:46 -0700641/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200642 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700643 * The pipe parameter indicates which CRTC is in use. Knowing this, we
644 * can determine which vblank sequence type to use for it. Traditional
645 * cards had only two CRTCs, with CRTC 0 using no special flags, and
646 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
647 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200648 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700649 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
650 * 0-31. If this is non-zero it indicates we're dealing with a
651 * multi-gpu situation and we need to calculate the vblank sync
652 * using DRM_BLANK_HIGH_CRTC_MASK.
653 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300654static unsigned int
655drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200656{
657 if (output->pipe > 1)
658 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
659 DRM_VBLANK_HIGH_CRTC_MASK;
660 else if (output->pipe > 0)
661 return DRM_VBLANK_SECONDARY;
662 else
663 return 0;
664}
665
David Herrmann1edf44c2013-10-22 17:11:26 +0200666static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500667drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400668 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100669{
Armin Krezović545dba62016-08-05 15:54:18 +0200670 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300671 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200672 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500673 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400674 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500675 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100676
Armin Krezović08368132016-09-30 14:11:05 +0200677 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200678 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800679
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300680 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400681 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300682 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200683 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100684
Hardeningff39efa2013-09-18 23:56:35 +0200685 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200686 if (!output->current ||
687 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300688 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300689 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400690 &output->connector_id, 1,
691 &mode->mode_info);
692 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200693 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200694 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400695 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300696 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200697 }
698
Giulio Camuffo954f1832014-10-11 18:27:30 +0300699 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300700 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500701 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200702 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200703 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500704 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100705
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300706 output->page_flip_pending = 1;
707
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400708 drm_output_set_cursor(output);
709
Jesse Barnes58ef3792012-02-23 09:45:49 -0500710 /*
711 * Now, update all the sprite surfaces
712 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300713 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200714 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500715 drmVBlank vbl = {
716 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
717 .request.sequence = 1,
718 };
719
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200720 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200721 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500722 continue;
723
Giulio Camuffo954f1832014-10-11 18:27:30 +0300724 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200725 fb_id = s->next->fb_id;
726
Giulio Camuffo954f1832014-10-11 18:27:30 +0300727 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200728 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500729 s->dest_x, s->dest_y,
730 s->dest_w, s->dest_h,
731 s->src_x, s->src_y,
732 s->src_w, s->src_h);
733 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200734 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500735 ret, strerror(errno));
736
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200737 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500738
Jesse Barnes58ef3792012-02-23 09:45:49 -0500739 /*
740 * Queue a vblank signal so we know when the surface
741 * becomes active on the display or has been replaced.
742 */
743 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300744 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500745 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200746 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500747 ret, strerror(errno));
748 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300749
750 s->output = output;
751 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500752 }
753
David Herrmann1edf44c2013-10-22 17:11:26 +0200754 return 0;
755
756err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800757 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200758 if (output->next) {
759 drm_output_release_fb(output, output->next);
760 output->next = NULL;
761 }
762
763 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400764}
765
766static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200767drm_output_start_repaint_loop(struct weston_output *output_base)
768{
Armin Krezović545dba62016-08-05 15:54:18 +0200769 struct drm_output *output = to_drm_output(output_base);
770 struct drm_backend *backend =
771 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200772 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200773 struct timespec ts, tnow;
774 struct timespec vbl2now;
775 int64_t refresh_nsec;
776 int ret;
777 drmVBlank vbl = {
778 .request.type = DRM_VBLANK_RELATIVE,
779 .request.sequence = 0,
780 .request.signal = 0,
781 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300782
Armin Krezović08368132016-09-30 14:11:05 +0200783 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800784 return;
785
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300786 if (!output->current) {
787 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200788 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300789 }
790
Mario Kleinerf507ec32015-06-21 21:25:14 +0200791 /* Try to get current msc and timestamp via instant query */
792 vbl.request.type |= drm_waitvblank_pipe(output);
793 ret = drmWaitVBlank(backend->drm.fd, &vbl);
794
795 /* Error ret or zero timestamp means failure to get valid timestamp */
796 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
797 ts.tv_sec = vbl.reply.tval_sec;
798 ts.tv_nsec = vbl.reply.tval_usec * 1000;
799
800 /* Valid timestamp for most recent vblank - not stale?
801 * Stale ts could happen on Linux 3.17+, so make sure it
802 * is not older than 1 refresh duration since now.
803 */
804 weston_compositor_read_presentation_clock(backend->compositor,
805 &tnow);
806 timespec_sub(&vbl2now, &tnow, &ts);
807 refresh_nsec =
808 millihz_to_nsec(output->base.current_mode->refresh);
809 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
810 drm_output_update_msc(output, vbl.reply.sequence);
811 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200812 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200813 return;
814 }
815 }
816
817 /* Immediate query didn't provide valid timestamp.
818 * Use pageflip fallback.
819 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300820 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200821
Giulio Camuffo954f1832014-10-11 18:27:30 +0300822 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200823 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
824 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200825 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200826 }
David Herrmann3c688c52013-10-22 17:11:25 +0200827
828 return;
829
830finish_frame:
831 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300832 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200833 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200834 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200835}
836
837static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400838drm_output_update_msc(struct drm_output *output, unsigned int seq)
839{
840 uint64_t msc_hi = output->base.msc >> 32;
841
842 if (seq < (output->base.msc & 0xffffffff))
843 msc_hi++;
844
845 output->base.msc = (msc_hi << 32) + seq;
846}
847
848static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500849vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
850 void *data)
851{
852 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300853 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400854 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200855 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
856 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300857
Pekka Paalanen641307c2014-09-23 22:08:47 -0400858 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300859 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500860
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200861 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200862 s->current = s->next;
863 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300864
865 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400866 ts.tv_sec = sec;
867 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200868 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300869 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500870}
871
872static void
Armin Krezović08368132016-09-30 14:11:05 +0200873drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800874
875static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400876page_flip_handler(int fd, unsigned int frame,
877 unsigned int sec, unsigned int usec, void *data)
878{
Armin Krezović545dba62016-08-05 15:54:18 +0200879 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400880 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200881 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
882 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
883 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400884
Pekka Paalanen641307c2014-09-23 22:08:47 -0400885 drm_output_update_msc(output, frame);
886
Jonas Ådahle5a12252013-04-05 23:07:11 +0200887 /* We don't set page_flip_pending on start_repaint_loop, in that case
888 * we just want to page flip to the current buffer to get an accurate
889 * timestamp */
890 if (output->page_flip_pending) {
891 drm_output_release_fb(output, output->current);
892 output->current = output->next;
893 output->next = NULL;
894 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300895
Jonas Ådahle5a12252013-04-05 23:07:11 +0200896 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400897
Xiong Zhangabd5d472013-10-11 14:43:07 +0800898 if (output->destroy_pending)
899 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +0200900 else if (output->disable_pending)
901 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800902 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400903 ts.tv_sec = sec;
904 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200905 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300906
907 /* We can't call this from frame_notify, because the output's
908 * repaint needed flag is cleared just after that */
909 if (output->recorder)
910 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300911 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200912}
913
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500914static uint32_t
915drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500917{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500918 uint32_t i, format;
919
920 format = gbm_bo_get_format(bo);
921
922 if (format == GBM_FORMAT_ARGB8888) {
923 pixman_region32_t r;
924
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500925 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600926 ev->surface->width,
927 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500929
930 if (!pixman_region32_not_empty(&r))
931 format = GBM_FORMAT_XRGB8888;
932
933 pixman_region32_fini(&r);
934 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500935
936 for (i = 0; i < s->count_formats; i++)
937 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500938 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500939
940 return 0;
941}
942
943static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500945{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 return !ev->transform.enabled ||
947 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948}
949
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400950static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200951drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500953{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200954 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200955 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200956 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300957 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500958 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300959 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500961 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500962 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200963 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500964 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400965 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500966
Giulio Camuffo954f1832014-10-11 18:27:30 +0300967 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200968 return NULL;
969
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200970 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200971 return NULL;
972
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200973 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200974 return NULL;
975
Giulio Camuffo954f1832014-10-11 18:27:30 +0300976 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400977 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500978
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200979 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400980 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400983 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300984 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500985
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200987 return NULL;
988
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300989 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500990 return NULL;
991
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400993 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500994
Giulio Camuffo954f1832014-10-11 18:27:30 +0300995 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200996 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500997 continue;
998
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200999 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001000 found = 1;
1001 break;
1002 }
1003 }
1004
1005 /* No sprites available */
1006 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001007 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001008
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001009 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001010#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001011 /* XXX: TODO:
1012 *
1013 * Use AddFB2 directly, do not go via GBM.
1014 * Add support for multiplanar formats.
1015 * Both require refactoring in the DRM-backend to
1016 * support a mix of gbm_bos and drmfbs.
1017 */
1018 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001019 .fd = dmabuf->attributes.fd[0],
1020 .width = dmabuf->attributes.width,
1021 .height = dmabuf->attributes.height,
1022 .stride = dmabuf->attributes.stride[0],
1023 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001024 };
1025
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001026 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001027 return NULL;
1028
1029 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1030 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001031#else
1032 return NULL;
1033#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001034 } else {
1035 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1036 buffer_resource, GBM_BO_USE_SCANOUT);
1037 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001038 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001039 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001040
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001042 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001043 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001044 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001045 }
1046
Giulio Camuffo954f1832014-10-11 18:27:30 +03001047 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001048 if (!s->next) {
1049 gbm_bo_destroy(bo);
1050 return NULL;
1051 }
1052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001054
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001056 s->plane.x = box->x1;
1057 s->plane.y = box->y1;
1058
Jesse Barnes58ef3792012-02-23 09:45:49 -05001059 /*
1060 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001061 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001062 * for us already).
1063 */
1064 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001066 &output->base.region);
1067 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001068 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001069 tbox = weston_transformed_rect(output->base.width,
1070 output->base.height,
1071 output->base.transform,
1072 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001073 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001074 s->dest_x = tbox.x1;
1075 s->dest_y = tbox.y1;
1076 s->dest_w = tbox.x2 - tbox.x1;
1077 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001078 pixman_region32_fini(&dest_rect);
1079
1080 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001082 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001083 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001084
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 weston_view_from_global_fixed(ev,
1086 wl_fixed_from_int(box->x1),
1087 wl_fixed_from_int(box->y1),
1088 &sx1, &sy1);
1089 weston_view_from_global_fixed(ev,
1090 wl_fixed_from_int(box->x2),
1091 wl_fixed_from_int(box->y2),
1092 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001093
1094 if (sx1 < 0)
1095 sx1 = 0;
1096 if (sy1 < 0)
1097 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001098 if (sx2 > wl_fixed_from_int(ev->surface->width))
1099 sx2 = wl_fixed_from_int(ev->surface->width);
1100 if (sy2 > wl_fixed_from_int(ev->surface->height))
1101 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001102
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001103 tbox.x1 = sx1;
1104 tbox.y1 = sy1;
1105 tbox.x2 = sx2;
1106 tbox.y2 = sy2;
1107
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001108 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1109 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001110 viewport->buffer.transform,
1111 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001112 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001113
1114 s->src_x = tbox.x1 << 8;
1115 s->src_y = tbox.y1 << 8;
1116 s->src_w = (tbox.x2 - tbox.x1) << 8;
1117 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001118 pixman_region32_fini(&src_rect);
1119
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001120 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001121}
1122
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001123static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001124drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001126{
Armin Krezović545dba62016-08-05 15:54:18 +02001127 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001128 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001129 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001130
Derek Foremanbe428b32015-11-24 11:39:38 -06001131 if (ev->transform.enabled &&
1132 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1133 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001134 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001135 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001136 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1137 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001138 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001139 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001141 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001142 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001143 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001144 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001145 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001146 if (ev->geometry.scissor_enabled)
1147 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001148 if (ev->surface->buffer_ref.buffer == NULL)
1149 return NULL;
1150 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1151 if (!shmbuf)
1152 return NULL;
1153 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1154 return NULL;
1155 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001156 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001157 return NULL;
1158
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001160
1161 return &output->cursor_plane;
1162}
1163
Pekka Paalanend0ead482014-06-16 12:05:40 +03001164/**
1165 * Update the image for the current cursor surface
1166 *
1167 * @param b DRM backend structure
1168 * @param bo GBM buffer object to write into
1169 * @param ev View to use for cursor image
1170 */
1171static void
1172cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1173 struct weston_view *ev)
1174{
1175 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1176 uint32_t buf[b->cursor_width * b->cursor_height];
1177 int32_t stride;
1178 uint8_t *s;
1179 int i;
1180
1181 assert(buffer && buffer->shm_buffer);
1182 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1183 assert(ev->surface->width <= b->cursor_width);
1184 assert(ev->surface->height <= b->cursor_height);
1185
1186 memset(buf, 0, sizeof buf);
1187 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1188 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1189
1190 wl_shm_buffer_begin_access(buffer->shm_buffer);
1191 for (i = 0; i < ev->surface->height; i++)
1192 memcpy(buf + i * b->cursor_width,
1193 s + i * stride,
1194 ev->surface->width * 4);
1195 wl_shm_buffer_end_access(buffer->shm_buffer);
1196
1197 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1198 weston_log("failed update cursor: %m\n");
1199}
1200
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001201static void
1202drm_output_set_cursor(struct drm_output *output)
1203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001205 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001206 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001207 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001208 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001209 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001210
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 output->cursor_view = NULL;
1212 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001213 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001214 output->cursor_plane.x = INT32_MIN;
1215 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001216 return;
1217 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001218
Neil Robertse5051712013-11-13 15:44:06 +00001219 buffer = ev->surface->buffer_ref.buffer;
1220
1221 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001222 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001223 pixman_region32_fini(&output->cursor_plane.damage);
1224 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001225 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001226 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001227
Pekka Paalanend0ead482014-06-16 12:05:40 +03001228 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001229 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001230 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1231 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001232 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001233 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001234 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001235 }
1236
Derek Foremanbe428b32015-11-24 11:39:38 -06001237 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001238
1239 /* From global to output space, output transform is guaranteed to be
1240 * NORMAL by drm_output_prepare_cursor_view().
1241 */
1242 x = (x - output->base.x) * output->base.current_scale;
1243 y = (y - output->base.y) * output->base.current_scale;
1244
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001245 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001246 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001247 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001248 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001249 }
1250
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001251 output->cursor_plane.x = x;
1252 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001253 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001254}
1255
Jesse Barnes58ef3792012-02-23 09:45:49 -05001256static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001257drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001258{
Armin Krezović545dba62016-08-05 15:54:18 +02001259 struct drm_backend *b = to_drm_backend(output_base->compositor);
1260 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001262 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001263 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001264
1265 /*
1266 * Find a surface for each sprite in the output using some heuristics:
1267 * 1) size
1268 * 2) frequency of update
1269 * 3) opacity (though some hw might support alpha blending)
1270 * 4) clipping (this can be fixed with color keys)
1271 *
1272 * The idea is to save on blitting since this should save power.
1273 * If we can get a large video surface on the sprite for example,
1274 * the main display surface may not need to update at all, and
1275 * the client buffer can be used directly for the sprite surface
1276 * as we do for flipping full screen surfaces.
1277 */
1278 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001279 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280
Giulio Camuffo954f1832014-10-11 18:27:30 +03001281 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001282 struct weston_surface *es = ev->surface;
1283
1284 /* Test whether this buffer can ever go into a plane:
1285 * non-shm, or small enough to be a cursor.
1286 *
1287 * Also, keep a reference when using the pixman renderer.
1288 * That makes it possible to do a seamless switch to the GL
1289 * renderer and since the pixman renderer keeps a reference
1290 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001291 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001292 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001293 (es->buffer_ref.buffer &&
1294 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001295 (ev->surface->width <= b->cursor_width &&
1296 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001297 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001298 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001299 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001300
Jesse Barnes58ef3792012-02-23 09:45:49 -05001301 pixman_region32_init(&surface_overlap);
1302 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001304
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001305 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001306 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001307 next_plane = primary;
1308 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001310 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001312 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001314 if (next_plane == NULL)
1315 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001316
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001318
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001319 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001320 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001322
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001323 if (next_plane == primary ||
1324 next_plane == &output->cursor_plane) {
1325 /* cursor plane involves a copy */
1326 ev->psf_flags = 0;
1327 } else {
1328 /* All other planes are a direct scanout of a
1329 * single client buffer.
1330 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001331 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001332 }
1333
Jesse Barnes58ef3792012-02-23 09:45:49 -05001334 pixman_region32_fini(&surface_overlap);
1335 }
1336 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001337}
1338
Matt Roper361d2ad2011-08-29 13:52:23 -07001339static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001340drm_output_fini_pixman(struct drm_output *output);
1341
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001342/**
1343 * Find the closest-matching mode for a given target
1344 *
1345 * Given a target mode, find the most suitable mode amongst the output's
1346 * current mode list to use, preferring the current mode if possible, to
1347 * avoid an expensive mode switch.
1348 *
1349 * @param output DRM output
1350 * @param target_mode Mode to attempt to match
1351 * @returns Pointer to a mode from the output's mode list
1352 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001353static struct drm_mode *
1354choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1355{
1356 struct drm_mode *tmp_mode = NULL, *mode;
1357
Hardeningff39efa2013-09-18 23:56:35 +02001358 if (output->base.current_mode->width == target_mode->width &&
1359 output->base.current_mode->height == target_mode->height &&
1360 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001361 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001362 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001363
1364 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1365 if (mode->mode_info.hdisplay == target_mode->width &&
1366 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001367 if (mode->base.refresh == target_mode->refresh ||
1368 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001369 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001370 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001371 tmp_mode = mode;
1372 }
1373 }
1374
1375 return tmp_mode;
1376}
1377
1378static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001379drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001380static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001381drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001382
1383static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001384drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1385{
1386 struct drm_output *output;
1387 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001388 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001389
1390 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001391 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001392 return -1;
1393 }
1394
1395 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001396 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001397 return -1;
1398 }
1399
Armin Krezović545dba62016-08-05 15:54:18 +02001400 b = to_drm_backend(output_base->compositor);
1401 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001402 drm_mode = choose_mode (output, mode);
1403
1404 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001405 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001406 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001407 }
1408
Hardeningff39efa2013-09-18 23:56:35 +02001409 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001410 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001411
Hardeningff39efa2013-09-18 23:56:35 +02001412 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001413
Hardeningff39efa2013-09-18 23:56:35 +02001414 output->base.current_mode = &drm_mode->base;
1415 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001416 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1417
Alex Wub7b8bda2012-04-17 17:20:48 +08001418 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001419 drm_output_release_fb(output, output->current);
1420 drm_output_release_fb(output, output->next);
1421 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001422
Giulio Camuffo954f1832014-10-11 18:27:30 +03001423 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001424 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001425 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001426 weston_log("failed to init output pixman state with "
1427 "new mode\n");
1428 return -1;
1429 }
1430 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001431 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001432 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001433
Giulio Camuffo954f1832014-10-11 18:27:30 +03001434 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001435 weston_log("failed to init output egl state with "
1436 "new mode");
1437 return -1;
1438 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001439 }
1440
Alex Wub7b8bda2012-04-17 17:20:48 +08001441 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001442}
1443
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001444static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001445on_drm_input(int fd, uint32_t mask, void *data)
1446{
1447 drmEventContext evctx;
1448
1449 memset(&evctx, 0, sizeof evctx);
1450 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1451 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001452 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001453 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001454
1455 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001456}
1457
1458static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001459init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001460{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001461 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001462 uint64_t cap;
1463 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001464 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001465
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001466 sysnum = udev_device_get_sysnum(device);
1467 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001468 b->drm.id = atoi(sysnum);
1469 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001470 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001471 return -1;
1472 }
1473
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001474 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001475 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001476 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001477 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001478 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001479 udev_device_get_devnode(device));
1480 return -1;
1481 }
1482
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001483 weston_log("using %s\n", filename);
1484
Giulio Camuffo954f1832014-10-11 18:27:30 +03001485 b->drm.fd = fd;
1486 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001487
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001488 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1489 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001490 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001491 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001492 clk_id = CLOCK_REALTIME;
1493
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001495 weston_log("Error: failed to set presentation clock %d.\n",
1496 clk_id);
1497 return -1;
1498 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001499
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001500 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1501 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001502 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001503 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001504 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001505
1506 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1507 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001508 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001509 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001510 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001511
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001512 return 0;
1513}
1514
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001515static struct gbm_device *
1516create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001517{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001518 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001519
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001520 gl_renderer = weston_load_module("gl-renderer.so",
1521 "gl_renderer_interface");
1522 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001523 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001524
1525 /* GBM will load a dri driver, but even though they need symbols from
1526 * libglapi, in some version of Mesa they are not linked to it. Since
1527 * only the gl-renderer module links to it, the call above won't make
1528 * these symbols globally available, and loading the DRI driver fails.
1529 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1530 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1531
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001532 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001533
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001534 return gbm;
1535}
1536
Bryce Harringtonc056a982015-05-19 15:25:18 -07001537/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001538 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001539 *
1540 * This returns 0 if substitution isn't possible, but 0 might be a
1541 * legitimate format for other EGL platforms, so the caller is
1542 * responsible for checking for 0 before calling gl_renderer->create().
1543 *
1544 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1545 * but it's entirely possible we'll see this again on other implementations.
1546 */
1547static int
1548fallback_format_for(uint32_t format)
1549{
1550 switch (format) {
1551 case GBM_FORMAT_XRGB8888:
1552 return GBM_FORMAT_ARGB8888;
1553 case GBM_FORMAT_XRGB2101010:
1554 return GBM_FORMAT_ARGB2101010;
1555 default:
1556 return 0;
1557 }
1558}
1559
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001560static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001561drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001562{
Derek Foreman6d556372015-11-04 14:47:33 -06001563 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001564 b->gbm_format,
1565 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001566 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001567 };
Derek Foreman6d556372015-11-04 14:47:33 -06001568 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001569
Derek Foremanc4cfe852015-05-15 12:12:40 -05001570 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001571 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001572 if (gl_renderer->display_create(b->compositor,
1573 EGL_PLATFORM_GBM_KHR,
1574 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001575 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001576 gl_renderer->opaque_attribs,
1577 format,
1578 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001579 return -1;
1580 }
1581
1582 return 0;
1583}
1584
1585static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001587{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001589
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001591 return -1;
1592
Giulio Camuffo954f1832014-10-11 18:27:30 +03001593 if (drm_backend_create_gl_renderer(b) < 0) {
1594 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001595 return -1;
1596 }
1597
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001598 return 0;
1599}
1600
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001601static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001602init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001603{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001604 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001605}
1606
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001607/**
1608 * Add a mode to output's mode list
1609 *
1610 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1611 * output's mode list.
1612 *
1613 * @param output DRM output to add mode to
1614 * @param info DRM mode structure to add
1615 * @returns Newly-allocated Weston/DRM mode structure
1616 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001617static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001618drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001619{
1620 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001621 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001622
1623 mode = malloc(sizeof *mode);
1624 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001625 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001626
1627 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001628 mode->base.width = info->hdisplay;
1629 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001630
1631 /* Calculate higher precision (mHz) refresh rate */
1632 refresh = (info->clock * 1000000LL / info->htotal +
1633 info->vtotal / 2) / info->vtotal;
1634
1635 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1636 refresh *= 2;
1637 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1638 refresh /= 2;
1639 if (info->vscan > 1)
1640 refresh /= info->vscan;
1641
1642 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001643 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001644
1645 if (info->type & DRM_MODE_TYPE_PREFERRED)
1646 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1647
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001648 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1649
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001650 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001651}
1652
1653static int
1654drm_subpixel_to_wayland(int drm_value)
1655{
1656 switch (drm_value) {
1657 default:
1658 case DRM_MODE_SUBPIXEL_UNKNOWN:
1659 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1660 case DRM_MODE_SUBPIXEL_NONE:
1661 return WL_OUTPUT_SUBPIXEL_NONE;
1662 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1663 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1664 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1665 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1666 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1667 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1668 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1669 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1670 }
1671}
1672
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001673/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001674static uint32_t
1675drm_get_backlight(struct drm_output *output)
1676{
1677 long brightness, max_brightness, norm;
1678
1679 brightness = backlight_get_brightness(output->backlight);
1680 max_brightness = backlight_get_max_brightness(output->backlight);
1681
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001682 /* convert it on a scale of 0 to 255 */
1683 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001684
1685 return (uint32_t) norm;
1686}
1687
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001688/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001689static void
1690drm_set_backlight(struct weston_output *output_base, uint32_t value)
1691{
Armin Krezović545dba62016-08-05 15:54:18 +02001692 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001693 long max_brightness, new_brightness;
1694
1695 if (!output->backlight)
1696 return;
1697
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001698 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001699 return;
1700
1701 max_brightness = backlight_get_max_brightness(output->backlight);
1702
1703 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001704 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001705
1706 backlight_set_brightness(output->backlight, new_brightness);
1707}
1708
1709static drmModePropertyPtr
1710drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1711{
1712 drmModePropertyPtr props;
1713 int i;
1714
1715 for (i = 0; i < connector->count_props; i++) {
1716 props = drmModeGetProperty(fd, connector->props[i]);
1717 if (!props)
1718 continue;
1719
1720 if (!strcmp(props->name, name))
1721 return props;
1722
1723 drmModeFreeProperty(props);
1724 }
1725
1726 return NULL;
1727}
1728
1729static void
1730drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1731{
Armin Krezović545dba62016-08-05 15:54:18 +02001732 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001733 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001734 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001735 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001736
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001737 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001738 return;
1739
Daniel Stone36609c72015-06-18 07:49:02 +01001740 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1741 output->dpms_prop->prop_id, level);
1742 if (ret) {
1743 weston_log("DRM: DPMS: failed property set for %s\n",
1744 output->base.name);
1745 return;
1746 }
1747
1748 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001749}
1750
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001751static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001752 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1753 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1754 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1755 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1756 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1757 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1758 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1759 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1760 [DRM_MODE_CONNECTOR_Component] = "Component",
1761 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1762 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1763 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1764 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1765 [DRM_MODE_CONNECTOR_TV] = "TV",
1766 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001767#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001768 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1769 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001770#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001771};
1772
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001773static char *
1774make_connector_name(const drmModeConnector *con)
1775{
1776 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001777 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001778
1779 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1780 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001781
1782 if (!type_name)
1783 type_name = "UNNAMED";
1784
1785 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001786
1787 return strdup(name);
1788}
1789
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001790static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001791find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001792 drmModeRes *resources, drmModeConnector *connector)
1793{
1794 drmModeEncoder *encoder;
1795 uint32_t possible_crtcs;
1796 int i, j;
1797
1798 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001799 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001800 if (encoder == NULL) {
1801 weston_log("Failed to get encoder.\n");
1802 return -1;
1803 }
1804 possible_crtcs = encoder->possible_crtcs;
1805 drmModeFreeEncoder(encoder);
1806
1807 for (i = 0; i < resources->count_crtcs; i++) {
1808 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001809 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001810 return i;
1811 }
1812 }
1813
1814 return -1;
1815}
1816
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001817/* Init output state that depends on gl or gbm */
1818static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001819drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001820{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001821 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001822 output->gbm_format,
1823 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001824 };
1825 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001826
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001827 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001828 output->base.current_mode->width,
1829 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001830 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001831 GBM_BO_USE_SCANOUT |
1832 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001833 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001834 weston_log("failed to create gbm surface\n");
1835 return -1;
1836 }
1837
Derek Foremanc4cfe852015-05-15 12:12:40 -05001838 if (format[1])
1839 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001840 if (gl_renderer->output_window_create(&output->base,
1841 (EGLNativeWindowType)output->gbm_surface,
1842 output->gbm_surface,
1843 gl_renderer->opaque_attribs,
1844 format,
1845 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001846 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001847 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001848 return -1;
1849 }
1850
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001851 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001852
1853 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001854 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001855 continue;
1856
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001857 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001858 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001859 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001860 }
1861
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001862 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001863 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001864 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001865 }
1866
1867 return 0;
1868}
1869
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001870static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001871drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001872{
Hardeningff39efa2013-09-18 23:56:35 +02001873 int w = output->base.current_mode->width;
1874 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001875 uint32_t format = output->gbm_format;
1876 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001877 unsigned int i;
1878
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001879 switch (format) {
1880 case GBM_FORMAT_XRGB8888:
1881 pixman_format = PIXMAN_x8r8g8b8;
1882 break;
1883 case GBM_FORMAT_RGB565:
1884 pixman_format = PIXMAN_r5g6b5;
1885 break;
1886 default:
1887 weston_log("Unsupported pixman format 0x%x\n", format);
1888 return -1;
1889 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001890
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001891 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001892 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001893 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001894 if (!output->dumb[i])
1895 goto err;
1896
1897 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001898 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001899 output->dumb[i]->map,
1900 output->dumb[i]->stride);
1901 if (!output->image[i])
1902 goto err;
1903 }
1904
1905 if (pixman_renderer_output_create(&output->base) < 0)
1906 goto err;
1907
1908 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001909 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001910
1911 return 0;
1912
1913err:
1914 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1915 if (output->dumb[i])
1916 drm_fb_destroy_dumb(output->dumb[i]);
1917 if (output->image[i])
1918 pixman_image_unref(output->image[i]);
1919
1920 output->dumb[i] = NULL;
1921 output->image[i] = NULL;
1922 }
1923
1924 return -1;
1925}
1926
1927static void
1928drm_output_fini_pixman(struct drm_output *output)
1929{
1930 unsigned int i;
1931
1932 pixman_renderer_output_destroy(&output->base);
1933 pixman_region32_fini(&output->previous_damage);
1934
1935 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1936 drm_fb_destroy_dumb(output->dumb[i]);
1937 pixman_image_unref(output->image[i]);
1938 output->dumb[i] = NULL;
1939 output->image[i] = NULL;
1940 }
1941}
1942
Richard Hughes2b2092a2013-04-24 14:58:02 +01001943static void
1944edid_parse_string(const uint8_t *data, char text[])
1945{
1946 int i;
1947 int replaced = 0;
1948
1949 /* this is always 12 bytes, but we can't guarantee it's null
1950 * terminated or not junk. */
1951 strncpy(text, (const char *) data, 12);
1952
Bryce Harrington9c7de162015-08-28 13:04:26 -07001953 /* guarantee our new string is null-terminated */
1954 text[12] = '\0';
1955
Richard Hughes2b2092a2013-04-24 14:58:02 +01001956 /* remove insane chars */
1957 for (i = 0; text[i] != '\0'; i++) {
1958 if (text[i] == '\n' ||
1959 text[i] == '\r') {
1960 text[i] = '\0';
1961 break;
1962 }
1963 }
1964
1965 /* ensure string is printable */
1966 for (i = 0; text[i] != '\0'; i++) {
1967 if (!isprint(text[i])) {
1968 text[i] = '-';
1969 replaced++;
1970 }
1971 }
1972
1973 /* if the string is random junk, ignore the string */
1974 if (replaced > 4)
1975 text[0] = '\0';
1976}
1977
1978#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1979#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1980#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1981#define EDID_OFFSET_DATA_BLOCKS 0x36
1982#define EDID_OFFSET_LAST_BLOCK 0x6c
1983#define EDID_OFFSET_PNPID 0x08
1984#define EDID_OFFSET_SERIAL 0x0c
1985
1986static int
1987edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1988{
1989 int i;
1990 uint32_t serial_number;
1991
1992 /* check header */
1993 if (length < 128)
1994 return -1;
1995 if (data[0] != 0x00 || data[1] != 0xff)
1996 return -1;
1997
1998 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1999 * /--08--\/--09--\
2000 * 7654321076543210
2001 * |\---/\---/\---/
2002 * R C1 C2 C3 */
2003 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2004 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2005 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2006 edid->pnp_id[3] = '\0';
2007
2008 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2009 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2010 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2011 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2012 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2013 if (serial_number > 0)
2014 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2015
2016 /* parse EDID data */
2017 for (i = EDID_OFFSET_DATA_BLOCKS;
2018 i <= EDID_OFFSET_LAST_BLOCK;
2019 i += 18) {
2020 /* ignore pixel clock data */
2021 if (data[i] != 0)
2022 continue;
2023 if (data[i+2] != 0)
2024 continue;
2025
2026 /* any useful blocks? */
2027 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2028 edid_parse_string(&data[i+5],
2029 edid->monitor_name);
2030 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2031 edid_parse_string(&data[i+5],
2032 edid->serial_number);
2033 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2034 edid_parse_string(&data[i+5],
2035 edid->eisa_id);
2036 }
2037 }
2038 return 0;
2039}
2040
2041static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002042find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002043 struct drm_output *output,
2044 drmModeConnector *connector)
2045{
2046 drmModePropertyBlobPtr edid_blob = NULL;
2047 drmModePropertyPtr property;
2048 int i;
2049 int rc;
2050
2051 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002052 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002053 if (!property)
2054 continue;
2055 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2056 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002057 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002058 connector->prop_values[i]);
2059 }
2060 drmModeFreeProperty(property);
2061 }
2062 if (!edid_blob)
2063 return;
2064
2065 rc = edid_parse(&output->edid,
2066 edid_blob->data,
2067 edid_blob->length);
2068 if (!rc) {
2069 weston_log("EDID data '%s', '%s', '%s'\n",
2070 output->edid.pnp_id,
2071 output->edid.monitor_name,
2072 output->edid.serial_number);
2073 if (output->edid.pnp_id[0] != '\0')
2074 output->base.make = output->edid.pnp_id;
2075 if (output->edid.monitor_name[0] != '\0')
2076 output->base.model = output->edid.monitor_name;
2077 if (output->edid.serial_number[0] != '\0')
2078 output->base.serial_number = output->edid.serial_number;
2079 }
2080 drmModeFreePropertyBlob(edid_blob);
2081}
2082
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002083
2084
2085static int
2086parse_modeline(const char *s, drmModeModeInfo *mode)
2087{
2088 char hsync[16];
2089 char vsync[16];
2090 float fclock;
2091
2092 mode->type = DRM_MODE_TYPE_USERDEF;
2093 mode->hskew = 0;
2094 mode->vscan = 0;
2095 mode->vrefresh = 0;
2096 mode->flags = 0;
2097
Rob Bradford307e09e2013-07-26 16:29:40 +01002098 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002099 &fclock,
2100 &mode->hdisplay,
2101 &mode->hsync_start,
2102 &mode->hsync_end,
2103 &mode->htotal,
2104 &mode->vdisplay,
2105 &mode->vsync_start,
2106 &mode->vsync_end,
2107 &mode->vtotal, hsync, vsync) != 11)
2108 return -1;
2109
2110 mode->clock = fclock * 1000;
2111 if (strcmp(hsync, "+hsync") == 0)
2112 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2113 else if (strcmp(hsync, "-hsync") == 0)
2114 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2115 else
2116 return -1;
2117
2118 if (strcmp(vsync, "+vsync") == 0)
2119 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2120 else if (strcmp(vsync, "-vsync") == 0)
2121 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2122 else
2123 return -1;
2124
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002125 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2126 mode->hdisplay, mode->vdisplay, fclock);
2127
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002128 return 0;
2129}
2130
Rob Bradford66bd9f52013-06-25 18:56:42 +01002131static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002132setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002133 struct weston_output *output,
2134 const char *s)
2135{
2136 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002137 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002138 struct udev_seat *seat;
2139
Giulio Camuffo954f1832014-10-11 18:27:30 +03002140 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002141 if (!seat)
2142 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002143
Derek Foreman0720ea32015-07-15 13:00:35 -05002144 seat->base.output = output;
2145
Derek Foreman1281a362015-07-31 16:55:32 -05002146 pointer = weston_seat_get_pointer(&seat->base);
2147 if (pointer)
2148 weston_pointer_clamp(pointer,
2149 &pointer->x,
2150 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002151 }
2152}
2153
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002154static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002155parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002156{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002157 int ret = 0;
2158
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002159 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002160 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002161 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002162 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002163 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002164 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002165 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002166 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002167 else {
2168 weston_log("fatal: unrecognized pixel format: %s\n", s);
2169 ret = -1;
2170 }
2171
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002172 return ret;
2173}
2174
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002175/**
2176 * Choose suitable mode for an output
2177 *
2178 * Find the most suitable mode to use for initial setup (or reconfiguration on
2179 * hotplug etc) for a DRM output.
2180 *
2181 * @param output DRM output to choose mode for
2182 * @param kind Strategy and preference to use when choosing mode
2183 * @param width Desired width for this output
2184 * @param height Desired height for this output
2185 * @param current_mode Mode currently being displayed on this output
2186 * @param modeline Manually-entered mode (may be NULL)
2187 * @returns A mode from the output's mode list, or NULL if none available
2188 */
2189static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002190drm_output_choose_initial_mode(struct drm_backend *backend,
2191 struct drm_output *output,
2192 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002193 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002194 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002195{
2196 struct drm_mode *preferred = NULL;
2197 struct drm_mode *current = NULL;
2198 struct drm_mode *configured = NULL;
2199 struct drm_mode *best = NULL;
2200 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002201 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002202 int32_t width = 0;
2203 int32_t height = 0;
2204
Armin Krezović08368132016-09-30 14:11:05 +02002205 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
2206 if (sscanf(modeline, "%dx%d", &width, &height) != 2) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002207 width = -1;
2208
Armin Krezović08368132016-09-30 14:11:05 +02002209 if (parse_modeline(modeline, &drm_modeline) == 0) {
2210 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002211 if (!configured)
2212 return NULL;
2213 } else {
2214 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002215 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002216 }
2217 }
2218 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002219
2220 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002221 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002222 height == drm_mode->base.height)
2223 configured = drm_mode;
2224
comic fans7a5c5622016-03-17 14:29:27 +02002225 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002226 sizeof *current_mode) == 0)
2227 current = drm_mode;
2228
2229 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2230 preferred = drm_mode;
2231
2232 best = drm_mode;
2233 }
2234
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002235 if (current == NULL && current_mode->clock != 0) {
2236 current = drm_output_add_mode(output, current_mode);
2237 if (!current)
2238 return NULL;
2239 }
2240
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002241 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002242 configured = current;
2243
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002244 if (configured)
2245 return configured;
2246
2247 if (preferred)
2248 return preferred;
2249
2250 if (current)
2251 return current;
2252
2253 if (best)
2254 return best;
2255
2256 weston_log("no available modes for %s\n", output->base.name);
2257 return NULL;
2258}
2259
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002260static int
2261connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2262 drmModeModeInfo *mode)
2263{
2264 drmModeEncoder *encoder;
2265 drmModeCrtc *crtc;
2266
2267 /* Get the current mode on the crtc that's currently driving
2268 * this connector. */
2269 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2270 memset(mode, 0, sizeof *mode);
2271 if (encoder != NULL) {
2272 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2273 drmModeFreeEncoder(encoder);
2274 if (crtc == NULL)
2275 return -1;
2276 if (crtc->mode_valid)
2277 *mode = crtc->mode;
2278 drmModeFreeCrtc(crtc);
2279 }
2280
2281 return 0;
2282}
2283
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002284static int
Armin Krezović08368132016-09-30 14:11:05 +02002285drm_output_set_mode(struct weston_output *base,
2286 enum weston_drm_backend_output_mode mode,
2287 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002288{
Armin Krezović08368132016-09-30 14:11:05 +02002289 struct drm_output *output = to_drm_output(base);
2290 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002291
Armin Krezović08368132016-09-30 14:11:05 +02002292 struct drm_mode *drm_mode, *next, *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002293 drmModeModeInfo crtc_mode;
2294 int i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002295
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002296 output->base.make = "unknown";
2297 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002298 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002299 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002300
Giulio Camuffo954f1832014-10-11 18:27:30 +03002301 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
Matt Roper361d2ad2011-08-29 13:52:23 -07002302
Armin Krezović08368132016-09-30 14:11:05 +02002303 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002304 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002305
Armin Krezović08368132016-09-30 14:11:05 +02002306 for (i = 0; i < output->connector->count_modes; i++) {
2307 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002308 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002309 goto err_free;
2310 }
2311
Armin Krezović08368132016-09-30 14:11:05 +02002312 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002313 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002314 goto err_free;
Armin Krezović08368132016-09-30 14:11:05 +02002315
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002316 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002317 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002318
Armin Krezović08368132016-09-30 14:11:05 +02002319 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2320 output->base.native_mode = output->base.current_mode;
2321 output->base.native_scale = output->base.current_scale;
2322
2323 output->base.mm_width = output->connector->mmWidth;
2324 output->base.mm_height = output->connector->mmHeight;
2325
2326 return 0;
2327
2328err_free:
2329 drmModeFreeCrtc(output->original_crtc);
2330 output->original_crtc = NULL;
2331
2332 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2333 base.link) {
2334 wl_list_remove(&drm_mode->base.link);
2335 free(drm_mode);
2336 }
2337
2338 return -1;
2339}
2340
2341static void
2342drm_output_set_gbm_format(struct weston_output *base,
2343 const char *gbm_format)
2344{
2345 struct drm_output *output = to_drm_output(base);
2346 struct drm_backend *b = to_drm_backend(base->compositor);
2347
2348 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2349 output->gbm_format = b->gbm_format;
2350}
2351
2352static void
2353drm_output_set_seat(struct weston_output *base,
2354 const char *seat)
2355{
2356 struct drm_output *output = to_drm_output(base);
2357 struct drm_backend *b = to_drm_backend(base->compositor);
2358
2359 setup_output_seat_constraint(b, &output->base,
2360 seat ? seat : "");
2361}
2362
2363static int
2364drm_output_enable(struct weston_output *base)
2365{
2366 struct drm_output *output = to_drm_output(base);
2367 struct drm_backend *b = to_drm_backend(base->compositor);
2368 struct weston_mode *m;
2369
2370 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002371
Giulio Camuffo954f1832014-10-11 18:27:30 +03002372 if (b->use_pixman) {
2373 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002374 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002375 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002376 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002377 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002378 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002379 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002380 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002381
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002382 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002383 weston_log("Initialized backlight, device %s\n",
2384 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002385 output->base.set_backlight = drm_set_backlight;
2386 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002387 } else {
2388 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002389 }
2390
Jonas Ådahle5a12252013-04-05 23:07:11 +02002391 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002392 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002393 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002394 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002395 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002396
Richard Hughese7299962013-05-01 21:52:12 +01002397 output->base.gamma_size = output->original_crtc->gamma_size;
2398 output->base.set_gamma = drm_output_set_gamma;
2399
Armin Krezović08368132016-09-30 14:11:05 +02002400 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2401
2402 find_and_parse_output_edid(b, output, output->connector);
2403 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2404 output->base.connection_internal = 1;
2405
Derek Foremanb0427562016-02-05 15:55:20 -06002406 weston_plane_init(&output->cursor_plane, b->compositor,
2407 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002408 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002409
Giulio Camuffo954f1832014-10-11 18:27:30 +03002410 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2411 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2412 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002413
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002414 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002415 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002416 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002417 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002418 m->width, m->height, m->refresh / 1000.0,
2419 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2420 ", preferred" : "",
2421 m->flags & WL_OUTPUT_MODE_CURRENT ?
2422 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002423 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002424 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002425
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002426 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002427
2428err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002429 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002430
David Herrmann0f0d54e2011-12-08 17:05:45 +01002431 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002432}
2433
Jesse Barnes58ef3792012-02-23 09:45:49 -05002434static void
Armin Krezović08368132016-09-30 14:11:05 +02002435drm_output_deinit(struct weston_output *base)
2436{
2437 struct drm_output *output = to_drm_output(base);
2438 struct drm_backend *b = to_drm_backend(base->compositor);
2439
2440 if (b->use_pixman) {
2441 drm_output_fini_pixman(output);
2442 } else {
2443 gl_renderer->output_destroy(&output->base);
2444 gbm_surface_destroy(output->gbm_surface);
2445 }
2446
2447 weston_plane_release(&output->fb_plane);
2448 weston_plane_release(&output->cursor_plane);
2449
2450 drmModeFreeProperty(output->dpms_prop);
2451
2452 /* Turn off hardware cursor */
2453 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2454}
2455
2456static void
2457drm_output_destroy(struct weston_output *base)
2458{
2459 struct drm_output *output = to_drm_output(base);
2460 struct drm_backend *b = to_drm_backend(base->compositor);
2461 drmModeCrtcPtr origcrtc = output->original_crtc;
2462
2463 if (output->page_flip_pending) {
2464 output->destroy_pending = 1;
2465 weston_log("destroy output while page flip pending\n");
2466 return;
2467 }
2468
2469 if (output->base.enabled)
2470 drm_output_deinit(&output->base);
2471
2472 if (origcrtc) {
2473 /* Restore original CRTC state */
2474 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2475 origcrtc->x, origcrtc->y,
2476 &output->connector_id, 1, &origcrtc->mode);
2477 drmModeFreeCrtc(origcrtc);
2478 }
2479
2480 weston_output_destroy(&output->base);
2481
2482 drmModeFreeConnector(output->connector);
2483
2484 if (output->backlight)
2485 backlight_destroy(output->backlight);
2486
2487 b->crtc_allocator &= ~(1 << output->crtc_id);
2488 b->connector_allocator &= ~(1 << output->connector_id);
2489
2490 free(output);
2491}
2492
2493static int
2494drm_output_disable(struct weston_output *base)
2495{
2496 struct drm_output *output = to_drm_output(base);
2497 struct drm_backend *b = to_drm_backend(base->compositor);
2498
2499 if (output->page_flip_pending) {
2500 output->disable_pending = 1;
2501 return -1;
2502 }
2503
2504 if (output->base.enabled)
2505 drm_output_deinit(&output->base);
2506
2507 output->disable_pending = 0;
2508
2509 weston_log("Disabling output %s\n", output->base.name);
2510 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2511 0, 0, 0, 0, 0, NULL);
2512
2513 return 0;
2514}
2515
2516/**
2517 * Create a Weston output structure
2518 *
2519 * Given a DRM connector, create a matching drm_output structure and add it
2520 * to Weston's output list. It also takes ownership of the connector, which
2521 * is released when output is destroyed.
2522 *
2523 * @param b Weston backend structure
2524 * @param resources DRM resources for this device
2525 * @param connector DRM connector to use for this new output
2526 * @param drm_device udev device pointer
2527 * @returns 0 on success, or -1 on failure
2528 */
2529static int
2530create_output_for_connector(struct drm_backend *b,
2531 drmModeRes *resources,
2532 drmModeConnector *connector,
2533 struct udev_device *drm_device)
2534{
2535 struct drm_output *output;
2536 int i;
2537
2538 i = find_crtc_for_connector(b, resources, connector);
2539 if (i < 0) {
2540 weston_log("No usable crtc/encoder pair for connector.\n");
2541 return -1;
2542 }
2543
2544 output = zalloc(sizeof *output);
2545 if (output == NULL)
2546 return -1;
2547
2548 output->connector = connector;
2549 output->crtc_id = resources->crtcs[i];
2550 output->pipe = i;
2551 output->connector_id = connector->connector_id;
2552
2553 output->backlight = backlight_init(drm_device,
2554 connector->connector_type);
2555
2556 output->base.enable = drm_output_enable;
2557 output->base.destroy = drm_output_destroy;
2558 output->base.disable = drm_output_disable;
2559 output->base.name = make_connector_name(connector);
2560
2561 output->destroy_pending = 0;
2562 output->disable_pending = 0;
2563 output->original_crtc = NULL;
2564
2565 b->crtc_allocator |= (1 << output->crtc_id);
2566 b->connector_allocator |= (1 << output->connector_id);
2567
Armin Krezović40087402016-09-30 14:11:12 +02002568 weston_output_init(&output->base, b->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002569 weston_compositor_add_pending_output(&output->base, b->compositor);
2570
2571 return 0;
2572}
2573
2574static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002575create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002576{
2577 struct drm_sprite *sprite;
2578 drmModePlaneRes *plane_res;
2579 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002580 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002581
Giulio Camuffo954f1832014-10-11 18:27:30 +03002582 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002583 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002584 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002585 strerror(errno));
2586 return;
2587 }
2588
2589 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002590 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002591 if (!plane)
2592 continue;
2593
Peter Huttererf3d62272013-08-08 11:57:05 +10002594 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002595 plane->count_formats));
2596 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002597 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002598 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002599 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002600 continue;
2601 }
2602
Jesse Barnes58ef3792012-02-23 09:45:49 -05002603 sprite->possible_crtcs = plane->possible_crtcs;
2604 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002605 sprite->current = NULL;
2606 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002607 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002608 sprite->count_formats = plane->count_formats;
2609 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002610 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002611 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002612 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2613 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2614 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002615
Giulio Camuffo954f1832014-10-11 18:27:30 +03002616 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002617 }
2618
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002619 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002620}
2621
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002622static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002623destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002624{
2625 struct drm_sprite *sprite, *next;
2626 struct drm_output *output;
2627
Giulio Camuffo954f1832014-10-11 18:27:30 +03002628 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002629 struct drm_output, base.link);
2630
Giulio Camuffo954f1832014-10-11 18:27:30 +03002631 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2632 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002633 sprite->plane_id,
2634 output->crtc_id, 0, 0,
2635 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002636 drm_output_release_fb(output, sprite->current);
2637 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002638 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002639 free(sprite);
2640 }
2641}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002642
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002643static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002644create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002645 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002646{
2647 drmModeConnector *connector;
2648 drmModeRes *resources;
2649 int i;
2650
Giulio Camuffo954f1832014-10-11 18:27:30 +03002651 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002652 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002653 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002654 return -1;
2655 }
2656
Giulio Camuffo954f1832014-10-11 18:27:30 +03002657 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2658 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002659 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002660 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002661 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002662
Giulio Camuffo954f1832014-10-11 18:27:30 +03002663 b->min_width = resources->min_width;
2664 b->max_width = resources->max_width;
2665 b->min_height = resources->min_height;
2666 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002667
Giulio Camuffo954f1832014-10-11 18:27:30 +03002668 b->num_crtcs = resources->count_crtcs;
2669 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002670
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002671 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002672 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002673 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002674 if (connector == NULL)
2675 continue;
2676
2677 if (connector->connection == DRM_MODE_CONNECTED &&
2678 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002679 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002680 if (create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002681 connector, drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002682 drmModeFreeConnector(connector);
2683 continue;
2684 }
Armin Krezović08368132016-09-30 14:11:05 +02002685 } else {
2686 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002687 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002688 }
2689
Armin Krezović08368132016-09-30 14:11:05 +02002690 if (wl_list_empty(&b->compositor->output_list) &&
2691 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002692 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002693
2694 drmModeFreeResources(resources);
2695
2696 return 0;
2697}
2698
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002699static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002700update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002701{
2702 drmModeConnector *connector;
2703 drmModeRes *resources;
2704 struct drm_output *output, *next;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002705 uint32_t connected = 0, disconnects = 0;
2706 int i;
2707
Giulio Camuffo954f1832014-10-11 18:27:30 +03002708 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002709 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002710 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002711 return;
2712 }
2713
2714 /* collect new connects */
2715 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002716 int connector_id = resources->connectors[i];
2717
Giulio Camuffo954f1832014-10-11 18:27:30 +03002718 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002719 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002720 continue;
2721
David Herrmann7551cff2011-12-08 17:05:43 +01002722 if (connector->connection != DRM_MODE_CONNECTED) {
2723 drmModeFreeConnector(connector);
2724 continue;
2725 }
2726
Benjamin Franzke117483d2011-08-30 11:38:26 +02002727 connected |= (1 << connector_id);
2728
Giulio Camuffo954f1832014-10-11 18:27:30 +03002729 if (!(b->connector_allocator & (1 << connector_id))) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002730 create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002731 connector, drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002732 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002733
Armin Krezović08368132016-09-30 14:11:05 +02002734 } else {
2735 drmModeFreeConnector(connector);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002736 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002737 }
2738 drmModeFreeResources(resources);
2739
Giulio Camuffo954f1832014-10-11 18:27:30 +03002740 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002741 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002742 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002743 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002744 if (disconnects & (1 << output->connector_id)) {
2745 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002746 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002747 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002748 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002749 }
2750 }
Armin Krezović08368132016-09-30 14:11:05 +02002751
2752 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2753 base.link) {
2754 if (disconnects & (1 << output->connector_id)) {
2755 disconnects &= ~(1 << output->connector_id);
2756 weston_log("connector %d disconnected\n",
2757 output->connector_id);
2758 drm_output_destroy(&output->base);
2759 }
2760 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002761 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002762}
2763
2764static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002765udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002766{
David Herrmannd7488c22012-03-11 20:05:21 +01002767 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002768 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002769
2770 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002771 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002772 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002773
David Herrmann6ac52db2012-03-11 20:05:22 +01002774 val = udev_device_get_property_value(device, "HOTPLUG");
2775 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002776 return 0;
2777
David Herrmann6ac52db2012-03-11 20:05:22 +01002778 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002779}
2780
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002781static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002782udev_drm_event(int fd, uint32_t mask, void *data)
2783{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002784 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002785 struct udev_device *event;
2786
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002788
Giulio Camuffo954f1832014-10-11 18:27:30 +03002789 if (udev_event_is_hotplug(b, event))
2790 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002791
2792 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002793
2794 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002795}
2796
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002797static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002798drm_restore(struct weston_compositor *ec)
2799{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002800 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002801}
2802
2803static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002804drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002805{
Armin Krezović545dba62016-08-05 15:54:18 +02002806 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002807
Giulio Camuffo954f1832014-10-11 18:27:30 +03002808 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002809
Giulio Camuffo954f1832014-10-11 18:27:30 +03002810 wl_event_source_remove(b->udev_drm_source);
2811 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002812
Giulio Camuffo954f1832014-10-11 18:27:30 +03002813 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002814
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002815 weston_compositor_shutdown(ec);
2816
Giulio Camuffo954f1832014-10-11 18:27:30 +03002817 if (b->gbm)
2818 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002819
Giulio Camuffo954f1832014-10-11 18:27:30 +03002820 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002821
Giulio Camuffo954f1832014-10-11 18:27:30 +03002822 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002823 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002824}
2825
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002826static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002827drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002828{
2829 struct drm_output *output;
2830 struct drm_mode *drm_mode;
2831 int ret;
2832
Giulio Camuffo954f1832014-10-11 18:27:30 +03002833 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002834 if (!output->current) {
2835 /* If something that would cause the output to
2836 * switch mode happened while in another vt, we
2837 * might not have a current drm_fb. In that case,
2838 * schedule a repaint and let drm_output_repaint
2839 * handle setting the mode. */
2840 weston_output_schedule_repaint(&output->base);
2841 continue;
2842 }
2843
Hardeningff39efa2013-09-18 23:56:35 +02002844 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002845 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002846 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002847 &output->connector_id, 1,
2848 &drm_mode->mode_info);
2849 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002850 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002851 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002852 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002853 output->base.x, output->base.y);
2854 }
2855 }
2856}
2857
2858static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002859session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002860{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002861 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002862 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002863 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002864 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002865
Giulio Camuffo954f1832014-10-11 18:27:30 +03002866 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002867 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002868 compositor->state = b->prev_state;
2869 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002870 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002871 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002872 } else {
2873 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002874 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002875
Giulio Camuffo954f1832014-10-11 18:27:30 +03002876 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002877 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002878
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002879 /* If we have a repaint scheduled (either from a
2880 * pending pageflip or the idle handler), make sure we
2881 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002882 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002883 * further attemps at repainting. When we switch
2884 * back, we schedule a repaint, which will process
2885 * pending frame callbacks. */
2886
Giulio Camuffo954f1832014-10-11 18:27:30 +03002887 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002888 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002889 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002890 }
2891
Giulio Camuffo954f1832014-10-11 18:27:30 +03002892 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002893 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002894
Giulio Camuffo954f1832014-10-11 18:27:30 +03002895 wl_list_for_each(sprite, &b->sprite_list, link)
2896 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002897 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002898 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002899 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002900 };
2901}
2902
David Herrmann0af066f2012-10-29 19:21:16 +01002903/*
2904 * Find primary GPU
2905 * Some systems may have multiple DRM devices attached to a single seat. This
2906 * function loops over all devices and tries to find a PCI device with the
2907 * boot_vga sysfs attribute set to 1.
2908 * If no such device is found, the first DRM device reported by udev is used.
2909 */
2910static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002911find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002912{
2913 struct udev_enumerate *e;
2914 struct udev_list_entry *entry;
2915 const char *path, *device_seat, *id;
2916 struct udev_device *device, *drm_device, *pci;
2917
Giulio Camuffo954f1832014-10-11 18:27:30 +03002918 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002919 udev_enumerate_add_match_subsystem(e, "drm");
2920 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2921
2922 udev_enumerate_scan_devices(e);
2923 drm_device = NULL;
2924 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2925 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002926 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002927 if (!device)
2928 continue;
2929 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2930 if (!device_seat)
2931 device_seat = default_seat;
2932 if (strcmp(device_seat, seat)) {
2933 udev_device_unref(device);
2934 continue;
2935 }
2936
2937 pci = udev_device_get_parent_with_subsystem_devtype(device,
2938 "pci", NULL);
2939 if (pci) {
2940 id = udev_device_get_sysattr_value(pci, "boot_vga");
2941 if (id && !strcmp(id, "1")) {
2942 if (drm_device)
2943 udev_device_unref(drm_device);
2944 drm_device = device;
2945 break;
2946 }
2947 }
2948
2949 if (!drm_device)
2950 drm_device = device;
2951 else
2952 udev_device_unref(device);
2953 }
2954
2955 udev_enumerate_unref(e);
2956 return drm_device;
2957}
2958
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002959static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002960planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2961 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002962{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002963 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002964
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002965 switch (key) {
2966 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002967 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002968 break;
2969 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002970 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002971 break;
2972 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002973 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002974 break;
2975 default:
2976 break;
2977 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002978}
2979
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002980#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002981static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002982recorder_destroy(struct drm_output *output)
2983{
2984 vaapi_recorder_destroy(output->recorder);
2985 output->recorder = NULL;
2986
2987 output->base.disable_planes--;
2988
2989 wl_list_remove(&output->recorder_frame_listener.link);
2990 weston_log("[libva recorder] done\n");
2991}
2992
2993static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002994recorder_frame_notify(struct wl_listener *listener, void *data)
2995{
2996 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002997 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002998 int fd, ret;
2999
3000 output = container_of(listener, struct drm_output,
3001 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003002 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003003
3004 if (!output->recorder)
3005 return;
3006
Giulio Camuffo954f1832014-10-11 18:27:30 +03003007 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008 DRM_CLOEXEC, &fd);
3009 if (ret) {
3010 weston_log("[libva recorder] "
3011 "failed to create prime fd for front buffer\n");
3012 return;
3013 }
3014
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003015 ret = vaapi_recorder_frame(output->recorder, fd,
3016 output->current->stride);
3017 if (ret < 0) {
3018 weston_log("[libva recorder] aborted: %m\n");
3019 recorder_destroy(output);
3020 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003021}
3022
3023static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003024create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003025 const char *filename)
3026{
3027 int fd;
3028 drm_magic_t magic;
3029
Giulio Camuffo954f1832014-10-11 18:27:30 +03003030 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003031 if (fd < 0)
3032 return NULL;
3033
3034 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003035 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003036
3037 return vaapi_recorder_create(fd, width, height, filename);
3038}
3039
3040static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003041recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003042 void *data)
3043{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003044 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003045 struct drm_output *output;
3046 int width, height;
3047
Giulio Camuffo954f1832014-10-11 18:27:30 +03003048 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003049 struct drm_output, base.link);
3050
3051 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003052 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003053 weston_log("failed to start vaapi recorder: "
3054 "output format not supported\n");
3055 return;
3056 }
3057
Hardeningff39efa2013-09-18 23:56:35 +02003058 width = output->base.current_mode->width;
3059 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003060
3061 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003062 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003063 if (!output->recorder) {
3064 weston_log("failed to create vaapi recorder\n");
3065 return;
3066 }
3067
3068 output->base.disable_planes++;
3069
3070 output->recorder_frame_listener.notify = recorder_frame_notify;
3071 wl_signal_add(&output->base.frame_signal,
3072 &output->recorder_frame_listener);
3073
3074 weston_output_schedule_repaint(&output->base);
3075
3076 weston_log("[libva recorder] initialized\n");
3077 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003078 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003079 }
3080}
3081#else
3082static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003083recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003084 void *data)
3085{
3086 weston_log("Compiled without libva support\n");
3087}
3088#endif
3089
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003090static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003091switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003092{
3093 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003094 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003095
Giulio Camuffo954f1832014-10-11 18:27:30 +03003096 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003097 return;
3098
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003099 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3100
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003101 weston_log("Switching to GL renderer\n");
3102
Giulio Camuffo954f1832014-10-11 18:27:30 +03003103 b->gbm = create_gbm_device(b->drm.fd);
3104 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003105 weston_log("Failed to create gbm device. "
3106 "Aborting renderer switch\n");
3107 return;
3108 }
3109
Giulio Camuffo954f1832014-10-11 18:27:30 +03003110 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003111 pixman_renderer_output_destroy(&output->base);
3112
Giulio Camuffo954f1832014-10-11 18:27:30 +03003113 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003114
Giulio Camuffo954f1832014-10-11 18:27:30 +03003115 if (drm_backend_create_gl_renderer(b) < 0) {
3116 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003117 weston_log("Failed to create GL renderer. Quitting.\n");
3118 /* FIXME: we need a function to shutdown cleanly */
3119 assert(0);
3120 }
3121
Giulio Camuffo954f1832014-10-11 18:27:30 +03003122 wl_list_for_each(output, &b->compositor->output_list, base.link)
3123 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003124
Giulio Camuffo954f1832014-10-11 18:27:30 +03003125 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003126
3127 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3128 if (linux_dmabuf_setup(b->compositor) < 0)
3129 weston_log("Error: initializing dmabuf "
3130 "support failed.\n");
3131 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003132}
3133
3134static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003135renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3136 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003137{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003138 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003139 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003140
Giulio Camuffo954f1832014-10-11 18:27:30 +03003141 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003142}
3143
Armin Krezović08368132016-09-30 14:11:05 +02003144static const struct weston_drm_output_api api = {
3145 drm_output_set_mode,
3146 drm_output_set_gbm_format,
3147 drm_output_set_seat,
3148};
3149
Giulio Camuffo954f1832014-10-11 18:27:30 +03003150static struct drm_backend *
3151drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003152 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003153{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003154 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003155 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003156 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003157 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003158 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003159 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003160
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003161 weston_log("initializing drm backend\n");
3162
Giulio Camuffo954f1832014-10-11 18:27:30 +03003163 b = zalloc(sizeof *b);
3164 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003165 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003166
Pekka Paalanen68583832015-05-19 09:53:16 +03003167 /*
3168 * KMS support for hardware planes cannot properly synchronize
3169 * without nuclear page flip. Without nuclear/atomic, hw plane
3170 * and cursor plane updates would either tear or cause extra
3171 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003172 * to a fraction. For cursors, it's not so bad, so they are
3173 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003174 *
3175 * These can be enabled again when nuclear/atomic support lands.
3176 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003177 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003178 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003179 b->use_pixman = config->use_pixman;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003180 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003181
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003182 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3183 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003184
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003185 if (config->seat_id)
3186 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003187
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003188 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003189 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3190 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003191 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003192 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003193 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003194 goto err_compositor;
3195 }
3196
Giulio Camuffo954f1832014-10-11 18:27:30 +03003197 b->udev = udev_new();
3198 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003199 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003200 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003201 }
3202
Giulio Camuffo954f1832014-10-11 18:27:30 +03003203 b->session_listener.notify = session_notify;
3204 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003205
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003206 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003207 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003208 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003209 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003210 }
David Herrmann0af066f2012-10-29 19:21:16 +01003211 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003212
Giulio Camuffo954f1832014-10-11 18:27:30 +03003213 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003214 weston_log("failed to initialize kms\n");
3215 goto err_udev_dev;
3216 }
3217
Giulio Camuffo954f1832014-10-11 18:27:30 +03003218 if (b->use_pixman) {
3219 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003220 weston_log("failed to initialize pixman renderer\n");
3221 goto err_udev_dev;
3222 }
3223 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003224 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003225 weston_log("failed to initialize egl\n");
3226 goto err_udev_dev;
3227 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003228 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003229
Giulio Camuffo954f1832014-10-11 18:27:30 +03003230 b->base.destroy = drm_destroy;
3231 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003232
Giulio Camuffo954f1832014-10-11 18:27:30 +03003233 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003234
Bob Ham91880f12016-01-12 10:21:47 +00003235 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003236
Giulio Camuffo954f1832014-10-11 18:27:30 +03003237 wl_list_init(&b->sprite_list);
3238 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003239
Giulio Camuffo954f1832014-10-11 18:27:30 +03003240 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003241 compositor, b->udev, seat_id,
3242 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003243 weston_log("failed to create input devices\n");
3244 goto err_sprite;
3245 }
3246
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003247 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003248 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003249 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003250 }
3251
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003252 /* A this point we have some idea of whether or not we have a working
3253 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003254 if (!b->cursors_are_broken)
3255 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003256
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003257 path = NULL;
3258
Giulio Camuffo954f1832014-10-11 18:27:30 +03003259 loop = wl_display_get_event_loop(compositor->wl_display);
3260 b->drm_source =
3261 wl_event_loop_add_fd(loop, b->drm.fd,
3262 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003263
Giulio Camuffo954f1832014-10-11 18:27:30 +03003264 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3265 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003266 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003267 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003268 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003269 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003270 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003271 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003272 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003273 udev_monitor_get_fd(b->udev_monitor),
3274 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003275
Giulio Camuffo954f1832014-10-11 18:27:30 +03003276 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003277 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003278 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003279 }
3280
Daniel Stonea96b93c2012-06-22 14:04:37 +01003281 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003282
Giulio Camuffo954f1832014-10-11 18:27:30 +03003283 weston_compositor_add_debug_binding(compositor, KEY_O,
3284 planes_binding, b);
3285 weston_compositor_add_debug_binding(compositor, KEY_C,
3286 planes_binding, b);
3287 weston_compositor_add_debug_binding(compositor, KEY_V,
3288 planes_binding, b);
3289 weston_compositor_add_debug_binding(compositor, KEY_Q,
3290 recorder_binding, b);
3291 weston_compositor_add_debug_binding(compositor, KEY_W,
3292 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003293
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003294 if (compositor->renderer->import_dmabuf) {
3295 if (linux_dmabuf_setup(compositor) < 0)
3296 weston_log("Error: initializing dmabuf "
3297 "support failed.\n");
3298 }
3299
Giulio Camuffo954f1832014-10-11 18:27:30 +03003300 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003301
Armin Krezović08368132016-09-30 14:11:05 +02003302 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3303 &api, sizeof(api));
3304
3305 if (ret < 0) {
3306 weston_log("Failed to register output API.\n");
3307 goto err_udev_monitor;
3308 }
3309
Giulio Camuffo954f1832014-10-11 18:27:30 +03003310 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003311
3312err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003313 wl_event_source_remove(b->udev_drm_source);
3314 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003315err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003316 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003317err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003318 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003319err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003320 if (b->gbm)
3321 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003322 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003323err_udev_dev:
3324 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003325err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003326 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003327err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003328 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003329err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003330 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003331 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003332 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003333}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003334
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003335static void
3336config_init_to_defaults(struct weston_drm_backend_config *config)
3337{
3338}
3339
Giulio Camuffo954f1832014-10-11 18:27:30 +03003340WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003341backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003342 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003343{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003344 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003345 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003346
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003347 if (config_base == NULL ||
3348 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3349 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3350 weston_log("drm backend config structure is invalid\n");
3351 return -1;
3352 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003353
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003354 config_init_to_defaults(&config);
3355 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003356
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003357 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003358 if (b == NULL)
3359 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003360
Giulio Camuffo954f1832014-10-11 18:27:30 +03003361 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003362}