blob: 491f171f7684eb85c217b95f9fd5fe20e5a237b6 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020055#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070056#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100057#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010058#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030059#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020060#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030061#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040062
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030063#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
64#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
65#endif
66
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030067#ifndef DRM_CAP_CURSOR_WIDTH
68#define DRM_CAP_CURSOR_WIDTH 0x8
69#endif
70
71#ifndef DRM_CAP_CURSOR_HEIGHT
72#define DRM_CAP_CURSOR_HEIGHT 0x9
73#endif
74
75#ifndef GBM_BO_USE_CURSOR
76#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
77#endif
78
Giulio Camuffo954f1832014-10-11 18:27:30 +030079struct drm_backend {
80 struct weston_backend base;
81 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040082
83 struct udev *udev;
84 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040085
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010086 struct udev_monitor *udev_monitor;
87 struct wl_event_source *udev_drm_source;
88
Benjamin Franzke2af7f102011-03-02 11:14:59 +010089 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010090 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010091 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030092 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010093 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020094 struct gbm_device *gbm;
Jesse Barnes58ef3792012-02-23 09:45:49 -050095 uint32_t *crtcs;
96 int num_crtcs;
Marty Jack13d9db22011-02-09 19:01:42 -050097 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010098 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070099 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100100 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200101
Rob Clark4339add2012-08-09 14:18:28 -0500102 /* we need these parameters in order to not fail drmModeAddFB2()
103 * due to out of bounds dimensions, and then mistakenly set
104 * sprites_are_broken:
105 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200106 uint32_t min_width, max_width;
107 uint32_t min_height, max_height;
108 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500109
Jesse Barnes58ef3792012-02-23 09:45:49 -0500110 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500111 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200112 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500113
Rob Clarkab5b1e32012-08-09 13:24:45 -0500114 int cursors_are_broken;
115
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200116 int use_pixman;
117
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200118 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300119
Rob Bradfordd355b802013-05-31 18:09:55 +0100120 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300121
Daniel Stone70d337d2015-06-16 18:42:23 +0100122 int32_t cursor_width;
123 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700124
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700125 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400126};
127
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400128struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500129 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400130 drmModeModeInfo mode_info;
131};
132
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300133struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200134 uint32_t fb_id, stride, handle, size;
135 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300136 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200137 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200138
139 /* Used by gbm fbs */
140 struct gbm_bo *bo;
141
142 /* Used by dumb fbs */
143 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300144};
145
Richard Hughes2b2092a2013-04-24 14:58:02 +0100146struct drm_edid {
147 char eisa_id[13];
148 char monitor_name[13];
149 char pnp_id[5];
150 char serial_number[13];
151};
152
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400153struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200154 struct weston_output base;
155 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400156
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400157 uint32_t crtc_id;
Rob Clark5ca1a472012-08-08 20:27:37 -0500158 int pipe;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400159 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700160 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100161 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300162 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100163 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200164
Daniel Stone36609c72015-06-18 07:49:02 +0100165 enum dpms_enum dpms;
166
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300167 int vblank_pending;
168 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800169 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200170 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300171
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100172 struct gbm_surface *gbm_surface;
173 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400174 struct weston_plane cursor_plane;
175 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500176 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400177 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300178 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200179 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200180
181 struct drm_fb *dumb[2];
182 pixman_image_t *image[2];
183 int current_image;
184 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300185
186 struct vaapi_recorder *recorder;
187 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400188};
189
Jesse Barnes58ef3792012-02-23 09:45:49 -0500190/*
191 * An output has a primary display plane plus zero or more sprites for
192 * blending display contents.
193 */
194struct drm_sprite {
195 struct wl_list link;
196
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400197 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500198
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200199 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300200 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300201 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500202
Jesse Barnes58ef3792012-02-23 09:45:49 -0500203 uint32_t possible_crtcs;
204 uint32_t plane_id;
205 uint32_t count_formats;
206
207 int32_t src_x, src_y;
208 uint32_t src_w, src_h;
209 uint32_t dest_x, dest_y;
210 uint32_t dest_w, dest_h;
211
212 uint32_t formats[];
213};
214
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300215static struct gl_renderer_interface *gl_renderer;
216
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500217static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400218
Armin Krezović545dba62016-08-05 15:54:18 +0200219static inline struct drm_output *
220to_drm_output(struct weston_output *base)
221{
222 return container_of(base, struct drm_output, base);
223}
224
225static inline struct drm_backend *
226to_drm_backend(struct weston_compositor *base)
227{
228 return container_of(base->backend, struct drm_backend, base);
229}
230
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400231static void
232drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400233
Mario Kleinerf507ec32015-06-21 21:25:14 +0200234static void
235drm_output_update_msc(struct drm_output *output, unsigned int seq);
236
Jesse Barnes58ef3792012-02-23 09:45:49 -0500237static int
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200238drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500239{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200240 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200241 struct drm_backend *b = to_drm_backend(ec);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500242 int crtc;
243
Giulio Camuffo954f1832014-10-11 18:27:30 +0300244 for (crtc = 0; crtc < b->num_crtcs; crtc++) {
245 if (b->crtcs[crtc] != output->crtc_id)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500246 continue;
247
248 if (supported & (1 << crtc))
249 return -1;
250 }
251
252 return 0;
253}
254
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300255static void
256drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
257{
258 struct drm_fb *fb = data;
259 struct gbm_device *gbm = gbm_bo_get_device(bo);
260
261 if (fb->fb_id)
262 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
263
Pekka Paalanende685b82012-12-04 15:58:12 +0200264 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300265
266 free(data);
267}
268
269static struct drm_fb *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300270drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
271 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200272{
273 struct drm_fb *fb;
274 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300275 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200276
277 struct drm_mode_create_dumb create_arg;
278 struct drm_mode_destroy_dumb destroy_arg;
279 struct drm_mode_map_dumb map_arg;
280
Peter Huttererf3d62272013-08-08 11:57:05 +1000281 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200282 if (!fb)
283 return NULL;
284
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300285 switch (format) {
286 case GBM_FORMAT_XRGB8888:
287 bpp = 32;
288 depth = 24;
289 break;
290 case GBM_FORMAT_RGB565:
291 bpp = depth = 16;
292 break;
293 default:
294 return NULL;
295 }
296
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700297 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300298 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200299 create_arg.width = width;
300 create_arg.height = height;
301
Giulio Camuffo954f1832014-10-11 18:27:30 +0300302 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200303 if (ret)
304 goto err_fb;
305
306 fb->handle = create_arg.handle;
307 fb->stride = create_arg.pitch;
308 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300309 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200310
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300311 ret = -1;
312
313 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700314 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300315
316 handles[0] = fb->handle;
317 pitches[0] = fb->stride;
318 offsets[0] = 0;
319
320 ret = drmModeAddFB2(b->drm.fd, width, height,
321 format, handles, pitches, offsets,
322 &fb->fb_id, 0);
323 if (ret) {
324 weston_log("addfb2 failed: %m\n");
325 b->no_addfb2 = 1;
326 }
327 }
328
329 if (ret) {
330 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
331 fb->stride, fb->handle, &fb->fb_id);
332 }
333
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200334 if (ret)
335 goto err_bo;
336
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700337 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200338 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400339 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200340 if (ret)
341 goto err_add_fb;
342
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500343 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300344 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200345 if (fb->map == MAP_FAILED)
346 goto err_add_fb;
347
348 return fb;
349
350err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300351 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200352err_bo:
353 memset(&destroy_arg, 0, sizeof(destroy_arg));
354 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300355 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200356err_fb:
357 free(fb);
358 return NULL;
359}
360
361static void
362drm_fb_destroy_dumb(struct drm_fb *fb)
363{
364 struct drm_mode_destroy_dumb destroy_arg;
365
366 if (!fb->map)
367 return;
368
369 if (fb->fb_id)
370 drmModeRmFB(fb->fd, fb->fb_id);
371
372 weston_buffer_reference(&fb->buffer_ref, NULL);
373
374 munmap(fb->map, fb->size);
375
376 memset(&destroy_arg, 0, sizeof(destroy_arg));
377 destroy_arg.handle = fb->handle;
378 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
379
380 free(fb);
381}
382
383static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500384drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300385 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300386{
387 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200388 uint32_t width, height;
Derek Foreman482ffdf2016-07-08 12:50:57 -0500389 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300390 int ret;
391
392 if (fb)
393 return fb;
394
Bryce Harringtonde16d892014-11-20 22:21:57 -0800395 fb = zalloc(sizeof *fb);
396 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200397 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300398
399 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300400
401 width = gbm_bo_get_width(bo);
402 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200403 fb->stride = gbm_bo_get_stride(bo);
404 fb->handle = gbm_bo_get_handle(bo).u32;
405 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300406 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300407
Giulio Camuffo954f1832014-10-11 18:27:30 +0300408 if (backend->min_width > width || width > backend->max_width ||
409 backend->min_height > height ||
410 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200411 weston_log("bo geometry out of bounds\n");
412 goto err_free;
413 }
414
415 ret = -1;
416
Giulio Camuffo954f1832014-10-11 18:27:30 +0300417 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200418 handles[0] = fb->handle;
419 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200420 offsets[0] = 0;
421
Giulio Camuffo954f1832014-10-11 18:27:30 +0300422 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200423 format, handles, pitches, offsets,
424 &fb->fb_id, 0);
425 if (ret) {
426 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300427 backend->no_addfb2 = 1;
428 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200429 }
430 }
431
432 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300433 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200434 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200435
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300436 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200437 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200438 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300439 }
440
441 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
442
443 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200444
445err_free:
446 free(fb);
447 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300448}
449
450static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500451drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200452{
Pekka Paalanende685b82012-12-04 15:58:12 +0200453 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200454
455 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200456
Pekka Paalanende685b82012-12-04 15:58:12 +0200457 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200458}
459
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200460static void
461drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
462{
463 if (!fb)
464 return;
465
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200466 if (fb->map &&
467 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200468 drm_fb_destroy_dumb(fb);
469 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200470 if (fb->is_client_buffer)
471 gbm_bo_destroy(fb->bo);
472 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100473 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600474 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200475 }
476}
477
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500478static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200479drm_output_check_scanout_format(struct drm_output *output,
480 struct weston_surface *es, struct gbm_bo *bo)
481{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200482 uint32_t format;
483 pixman_region32_t r;
484
485 format = gbm_bo_get_format(bo);
486
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700487 if (format == GBM_FORMAT_ARGB8888) {
488 /* We can scanout an ARGB buffer if the surface's
489 * opaque region covers the whole output, but we have
490 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800491 pixman_region32_init_rect(&r, 0, 0,
492 output->base.width,
493 output->base.height);
494 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200495
496 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500497 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200498
499 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500500 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700501
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100502 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700503 return format;
504
505 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200506}
507
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400508static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200509drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500511{
Armin Krezović545dba62016-08-05 15:54:18 +0200512 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200514 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300515 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500516 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500517
Jason Ekstranda7af7042013-10-12 22:38:11 -0500518 if (ev->geometry.x != output->base.x ||
519 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300520 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200521 buffer->width != output->base.current_mode->width ||
522 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200523 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400525 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500526
Pekka Paalanen5580f222015-02-17 16:33:18 +0200527 if (ev->geometry.scissor_enabled)
528 return NULL;
529
Giulio Camuffo954f1832014-10-11 18:27:30 +0300530 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700531 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500532
Rob Bradford9b101872012-09-14 23:25:41 +0100533 /* Unable to use the buffer for scanout */
534 if (!bo)
535 return NULL;
536
Jason Ekstranda7af7042013-10-12 22:38:11 -0500537 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500538 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300539 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400540 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300541 }
542
Giulio Camuffo954f1832014-10-11 18:27:30 +0300543 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300544 if (!output->next) {
545 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400546 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300547 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500548
Pekka Paalanende685b82012-12-04 15:58:12 +0200549 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500550
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400551 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500552}
553
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500554static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200555drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400556{
Armin Krezović545dba62016-08-05 15:54:18 +0200557 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300558 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400559
Giulio Camuffo954f1832014-10-11 18:27:30 +0300560 output->base.compositor->renderer->repaint_output(&output->base,
561 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400562
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100563 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300564 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200565 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400566 return;
567 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300568
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100569 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300570 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200571 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100572 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300573 return;
574 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400575}
576
577static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200578drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
579{
580 struct weston_compositor *ec = output->base.compositor;
581 pixman_region32_t total_damage, previous_damage;
582
583 pixman_region32_init(&total_damage);
584 pixman_region32_init(&previous_damage);
585
586 pixman_region32_copy(&previous_damage, damage);
587
588 pixman_region32_union(&total_damage, damage, &output->previous_damage);
589 pixman_region32_copy(&output->previous_damage, &previous_damage);
590
591 output->current_image ^= 1;
592
593 output->next = output->dumb[output->current_image];
594 pixman_renderer_output_set_buffer(&output->base,
595 output->image[output->current_image]);
596
597 ec->renderer->repaint_output(&output->base, &total_damage);
598
599 pixman_region32_fini(&total_damage);
600 pixman_region32_fini(&previous_damage);
601}
602
603static void
604drm_output_render(struct drm_output *output, pixman_region32_t *damage)
605{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300606 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200607 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200608
Giulio Camuffo954f1832014-10-11 18:27:30 +0300609 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200610 drm_output_render_pixman(output, damage);
611 else
612 drm_output_render_gl(output, damage);
613
Giulio Camuffo954f1832014-10-11 18:27:30 +0300614 pixman_region32_subtract(&c->primary_plane.damage,
615 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200616}
617
618static void
Richard Hughese7299962013-05-01 21:52:12 +0100619drm_output_set_gamma(struct weston_output *output_base,
620 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
621{
622 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200623 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300624 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200625 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100626
627 /* check */
628 if (output_base->gamma_size != size)
629 return;
630 if (!output->original_crtc)
631 return;
632
Giulio Camuffo954f1832014-10-11 18:27:30 +0300633 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100634 output->crtc_id,
635 size, r, g, b);
636 if (rc)
637 weston_log("set gamma failed: %m\n");
638}
639
Bryce Harringtonada4f072015-06-30 13:25:46 -0700640/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200641 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700642 * The pipe parameter indicates which CRTC is in use. Knowing this, we
643 * can determine which vblank sequence type to use for it. Traditional
644 * cards had only two CRTCs, with CRTC 0 using no special flags, and
645 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
646 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200647 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700648 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
649 * 0-31. If this is non-zero it indicates we're dealing with a
650 * multi-gpu situation and we need to calculate the vblank sync
651 * using DRM_BLANK_HIGH_CRTC_MASK.
652 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300653static unsigned int
654drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200655{
656 if (output->pipe > 1)
657 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
658 DRM_VBLANK_HIGH_CRTC_MASK;
659 else if (output->pipe > 0)
660 return DRM_VBLANK_SECONDARY;
661 else
662 return 0;
663}
664
David Herrmann1edf44c2013-10-22 17:11:26 +0200665static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500666drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400667 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100668{
Armin Krezović545dba62016-08-05 15:54:18 +0200669 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300670 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200671 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500672 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400673 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500674 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100675
Armin Krezović08368132016-09-30 14:11:05 +0200676 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200677 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800678
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300679 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400680 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300681 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200682 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100683
Hardeningff39efa2013-09-18 23:56:35 +0200684 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200685 if (!output->current ||
686 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300687 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300688 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400689 &output->connector_id, 1,
690 &mode->mode_info);
691 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200692 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200693 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400694 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300695 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200696 }
697
Giulio Camuffo954f1832014-10-11 18:27:30 +0300698 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300699 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500700 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200701 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200702 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500703 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100704
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300705 output->page_flip_pending = 1;
706
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400707 drm_output_set_cursor(output);
708
Jesse Barnes58ef3792012-02-23 09:45:49 -0500709 /*
710 * Now, update all the sprite surfaces
711 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300712 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200713 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500714 drmVBlank vbl = {
715 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
716 .request.sequence = 1,
717 };
718
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200719 if ((!s->current && !s->next) ||
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200720 !drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500721 continue;
722
Giulio Camuffo954f1832014-10-11 18:27:30 +0300723 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200724 fb_id = s->next->fb_id;
725
Giulio Camuffo954f1832014-10-11 18:27:30 +0300726 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200727 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500728 s->dest_x, s->dest_y,
729 s->dest_w, s->dest_h,
730 s->src_x, s->src_y,
731 s->src_w, s->src_h);
732 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200733 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500734 ret, strerror(errno));
735
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200736 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500737
Jesse Barnes58ef3792012-02-23 09:45:49 -0500738 /*
739 * Queue a vblank signal so we know when the surface
740 * becomes active on the display or has been replaced.
741 */
742 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300743 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500744 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200745 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500746 ret, strerror(errno));
747 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300748
749 s->output = output;
750 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500751 }
752
David Herrmann1edf44c2013-10-22 17:11:26 +0200753 return 0;
754
755err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800756 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200757 if (output->next) {
758 drm_output_release_fb(output, output->next);
759 output->next = NULL;
760 }
761
762 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400763}
764
765static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200766drm_output_start_repaint_loop(struct weston_output *output_base)
767{
Armin Krezović545dba62016-08-05 15:54:18 +0200768 struct drm_output *output = to_drm_output(output_base);
769 struct drm_backend *backend =
770 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200771 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200772 struct timespec ts, tnow;
773 struct timespec vbl2now;
774 int64_t refresh_nsec;
775 int ret;
776 drmVBlank vbl = {
777 .request.type = DRM_VBLANK_RELATIVE,
778 .request.sequence = 0,
779 .request.signal = 0,
780 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300781
Armin Krezović08368132016-09-30 14:11:05 +0200782 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800783 return;
784
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300785 if (!output->current) {
786 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200787 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300788 }
789
Mario Kleinerf507ec32015-06-21 21:25:14 +0200790 /* Try to get current msc and timestamp via instant query */
791 vbl.request.type |= drm_waitvblank_pipe(output);
792 ret = drmWaitVBlank(backend->drm.fd, &vbl);
793
794 /* Error ret or zero timestamp means failure to get valid timestamp */
795 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
796 ts.tv_sec = vbl.reply.tval_sec;
797 ts.tv_nsec = vbl.reply.tval_usec * 1000;
798
799 /* Valid timestamp for most recent vblank - not stale?
800 * Stale ts could happen on Linux 3.17+, so make sure it
801 * is not older than 1 refresh duration since now.
802 */
803 weston_compositor_read_presentation_clock(backend->compositor,
804 &tnow);
805 timespec_sub(&vbl2now, &tnow, &ts);
806 refresh_nsec =
807 millihz_to_nsec(output->base.current_mode->refresh);
808 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
809 drm_output_update_msc(output, vbl.reply.sequence);
810 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200811 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200812 return;
813 }
814 }
815
816 /* Immediate query didn't provide valid timestamp.
817 * Use pageflip fallback.
818 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300819 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200820
Giulio Camuffo954f1832014-10-11 18:27:30 +0300821 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200822 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
823 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200824 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200825 }
David Herrmann3c688c52013-10-22 17:11:25 +0200826
827 return;
828
829finish_frame:
830 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300831 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200832 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200833 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200834}
835
836static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400837drm_output_update_msc(struct drm_output *output, unsigned int seq)
838{
839 uint64_t msc_hi = output->base.msc >> 32;
840
841 if (seq < (output->base.msc & 0xffffffff))
842 msc_hi++;
843
844 output->base.msc = (msc_hi << 32) + seq;
845}
846
847static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500848vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
849 void *data)
850{
851 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300852 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400853 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200854 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
855 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300856
Pekka Paalanen641307c2014-09-23 22:08:47 -0400857 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300858 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500859
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200860 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200861 s->current = s->next;
862 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300863
864 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400865 ts.tv_sec = sec;
866 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200867 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300868 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500869}
870
871static void
Armin Krezović08368132016-09-30 14:11:05 +0200872drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800873
874static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400875page_flip_handler(int fd, unsigned int frame,
876 unsigned int sec, unsigned int usec, void *data)
877{
Armin Krezović545dba62016-08-05 15:54:18 +0200878 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400879 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200880 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
881 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
882 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400883
Pekka Paalanen641307c2014-09-23 22:08:47 -0400884 drm_output_update_msc(output, frame);
885
Jonas Ådahle5a12252013-04-05 23:07:11 +0200886 /* We don't set page_flip_pending on start_repaint_loop, in that case
887 * we just want to page flip to the current buffer to get an accurate
888 * timestamp */
889 if (output->page_flip_pending) {
890 drm_output_release_fb(output, output->current);
891 output->current = output->next;
892 output->next = NULL;
893 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300894
Jonas Ådahle5a12252013-04-05 23:07:11 +0200895 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400896
Xiong Zhangabd5d472013-10-11 14:43:07 +0800897 if (output->destroy_pending)
898 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +0200899 else if (output->disable_pending)
900 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800901 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400902 ts.tv_sec = sec;
903 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200904 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300905
906 /* We can't call this from frame_notify, because the output's
907 * repaint needed flag is cleared just after that */
908 if (output->recorder)
909 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300910 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200911}
912
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500913static uint32_t
914drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500916{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500917 uint32_t i, format;
918
919 format = gbm_bo_get_format(bo);
920
921 if (format == GBM_FORMAT_ARGB8888) {
922 pixman_region32_t r;
923
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500924 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600925 ev->surface->width,
926 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500928
929 if (!pixman_region32_not_empty(&r))
930 format = GBM_FORMAT_XRGB8888;
931
932 pixman_region32_fini(&r);
933 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500934
935 for (i = 0; i < s->count_formats; i++)
936 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500937 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500938
939 return 0;
940}
941
942static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500944{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 return !ev->transform.enabled ||
946 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500947}
948
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400949static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200950drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200953 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200954 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200955 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300956 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500957 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300958 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500959 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500960 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500961 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200962 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500963 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400964 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500965
Giulio Camuffo954f1832014-10-11 18:27:30 +0300966 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200967 return NULL;
968
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200969 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200970 return NULL;
971
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200972 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200973 return NULL;
974
Giulio Camuffo954f1832014-10-11 18:27:30 +0300975 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400976 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500977
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200978 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400979 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300980
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400982 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300983 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200986 return NULL;
987
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300988 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500989 return NULL;
990
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400992 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500993
Giulio Camuffo954f1832014-10-11 18:27:30 +0300994 wl_list_for_each(s, &b->sprite_list, link) {
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200995 if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500996 continue;
997
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200998 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500999 found = 1;
1000 break;
1001 }
1002 }
1003
1004 /* No sprites available */
1005 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001006 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001007
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001008 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001009#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001010 /* XXX: TODO:
1011 *
1012 * Use AddFB2 directly, do not go via GBM.
1013 * Add support for multiplanar formats.
1014 * Both require refactoring in the DRM-backend to
1015 * support a mix of gbm_bos and drmfbs.
1016 */
1017 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001018 .fd = dmabuf->attributes.fd[0],
1019 .width = dmabuf->attributes.width,
1020 .height = dmabuf->attributes.height,
1021 .stride = dmabuf->attributes.stride[0],
1022 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001023 };
1024
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001025 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001026 return NULL;
1027
1028 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1029 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001030#else
1031 return NULL;
1032#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001033 } else {
1034 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1035 buffer_resource, GBM_BO_USE_SCANOUT);
1036 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001037 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001038 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001039
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001041 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001042 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001043 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001044 }
1045
Giulio Camuffo954f1832014-10-11 18:27:30 +03001046 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001047 if (!s->next) {
1048 gbm_bo_destroy(bo);
1049 return NULL;
1050 }
1051
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001053
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001055 s->plane.x = box->x1;
1056 s->plane.y = box->y1;
1057
Jesse Barnes58ef3792012-02-23 09:45:49 -05001058 /*
1059 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001060 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001061 * for us already).
1062 */
1063 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001065 &output->base.region);
1066 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001067 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001068 tbox = weston_transformed_rect(output->base.width,
1069 output->base.height,
1070 output->base.transform,
1071 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001072 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001073 s->dest_x = tbox.x1;
1074 s->dest_y = tbox.y1;
1075 s->dest_w = tbox.x2 - tbox.x1;
1076 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001077 pixman_region32_fini(&dest_rect);
1078
1079 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001081 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001082 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001083
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 weston_view_from_global_fixed(ev,
1085 wl_fixed_from_int(box->x1),
1086 wl_fixed_from_int(box->y1),
1087 &sx1, &sy1);
1088 weston_view_from_global_fixed(ev,
1089 wl_fixed_from_int(box->x2),
1090 wl_fixed_from_int(box->y2),
1091 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001092
1093 if (sx1 < 0)
1094 sx1 = 0;
1095 if (sy1 < 0)
1096 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001097 if (sx2 > wl_fixed_from_int(ev->surface->width))
1098 sx2 = wl_fixed_from_int(ev->surface->width);
1099 if (sy2 > wl_fixed_from_int(ev->surface->height))
1100 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001101
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001102 tbox.x1 = sx1;
1103 tbox.y1 = sy1;
1104 tbox.x2 = sx2;
1105 tbox.y2 = sy2;
1106
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001107 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1108 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001109 viewport->buffer.transform,
1110 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001111 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001112
1113 s->src_x = tbox.x1 << 8;
1114 s->src_y = tbox.y1 << 8;
1115 s->src_w = (tbox.x2 - tbox.x1) << 8;
1116 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001117 pixman_region32_fini(&src_rect);
1118
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001119 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001120}
1121
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001122static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001123drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001125{
Armin Krezović545dba62016-08-05 15:54:18 +02001126 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001127 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001128 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001129
Derek Foremanbe428b32015-11-24 11:39:38 -06001130 if (ev->transform.enabled &&
1131 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1132 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001133 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001134 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001135 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1136 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001137 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001138 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001140 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001141 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001142 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001143 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001144 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001145 if (ev->geometry.scissor_enabled)
1146 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001147 if (ev->surface->buffer_ref.buffer == NULL)
1148 return NULL;
1149 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1150 if (!shmbuf)
1151 return NULL;
1152 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1153 return NULL;
1154 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001155 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001156 return NULL;
1157
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001159
1160 return &output->cursor_plane;
1161}
1162
Pekka Paalanend0ead482014-06-16 12:05:40 +03001163/**
1164 * Update the image for the current cursor surface
1165 *
1166 * @param b DRM backend structure
1167 * @param bo GBM buffer object to write into
1168 * @param ev View to use for cursor image
1169 */
1170static void
1171cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1172 struct weston_view *ev)
1173{
1174 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1175 uint32_t buf[b->cursor_width * b->cursor_height];
1176 int32_t stride;
1177 uint8_t *s;
1178 int i;
1179
1180 assert(buffer && buffer->shm_buffer);
1181 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1182 assert(ev->surface->width <= b->cursor_width);
1183 assert(ev->surface->height <= b->cursor_height);
1184
1185 memset(buf, 0, sizeof buf);
1186 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1187 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1188
1189 wl_shm_buffer_begin_access(buffer->shm_buffer);
1190 for (i = 0; i < ev->surface->height; i++)
1191 memcpy(buf + i * b->cursor_width,
1192 s + i * stride,
1193 ev->surface->width * 4);
1194 wl_shm_buffer_end_access(buffer->shm_buffer);
1195
1196 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1197 weston_log("failed update cursor: %m\n");
1198}
1199
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001200static void
1201drm_output_set_cursor(struct drm_output *output)
1202{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001204 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001205 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001206 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001207 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001208 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001209
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 output->cursor_view = NULL;
1211 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001212 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001213 output->cursor_plane.x = INT32_MIN;
1214 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001215 return;
1216 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001217
Neil Robertse5051712013-11-13 15:44:06 +00001218 buffer = ev->surface->buffer_ref.buffer;
1219
1220 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001221 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001222 pixman_region32_fini(&output->cursor_plane.damage);
1223 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001224 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001225 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001226
Pekka Paalanend0ead482014-06-16 12:05:40 +03001227 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001228 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001229 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1230 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001231 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001232 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001233 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001234 }
1235
Derek Foremanbe428b32015-11-24 11:39:38 -06001236 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001237
1238 /* From global to output space, output transform is guaranteed to be
1239 * NORMAL by drm_output_prepare_cursor_view().
1240 */
1241 x = (x - output->base.x) * output->base.current_scale;
1242 y = (y - output->base.y) * output->base.current_scale;
1243
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001244 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001245 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001246 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001247 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001248 }
1249
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001250 output->cursor_plane.x = x;
1251 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001252 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001253}
1254
Jesse Barnes58ef3792012-02-23 09:45:49 -05001255static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001256drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001257{
Armin Krezović545dba62016-08-05 15:54:18 +02001258 struct drm_backend *b = to_drm_backend(output_base->compositor);
1259 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001261 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001262 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001263
1264 /*
1265 * Find a surface for each sprite in the output using some heuristics:
1266 * 1) size
1267 * 2) frequency of update
1268 * 3) opacity (though some hw might support alpha blending)
1269 * 4) clipping (this can be fixed with color keys)
1270 *
1271 * The idea is to save on blitting since this should save power.
1272 * If we can get a large video surface on the sprite for example,
1273 * the main display surface may not need to update at all, and
1274 * the client buffer can be used directly for the sprite surface
1275 * as we do for flipping full screen surfaces.
1276 */
1277 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279
Giulio Camuffo954f1832014-10-11 18:27:30 +03001280 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001281 struct weston_surface *es = ev->surface;
1282
1283 /* Test whether this buffer can ever go into a plane:
1284 * non-shm, or small enough to be a cursor.
1285 *
1286 * Also, keep a reference when using the pixman renderer.
1287 * That makes it possible to do a seamless switch to the GL
1288 * renderer and since the pixman renderer keeps a reference
1289 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001290 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001291 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001292 (es->buffer_ref.buffer &&
1293 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001294 (ev->surface->width <= b->cursor_width &&
1295 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001296 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001297 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001298 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001299
Jesse Barnes58ef3792012-02-23 09:45:49 -05001300 pixman_region32_init(&surface_overlap);
1301 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001303
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001304 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001305 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001306 next_plane = primary;
1307 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001309 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001311 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001313 if (next_plane == NULL)
1314 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001317
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001318 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001319 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001321
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001322 if (next_plane == primary ||
1323 next_plane == &output->cursor_plane) {
1324 /* cursor plane involves a copy */
1325 ev->psf_flags = 0;
1326 } else {
1327 /* All other planes are a direct scanout of a
1328 * single client buffer.
1329 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001330 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001331 }
1332
Jesse Barnes58ef3792012-02-23 09:45:49 -05001333 pixman_region32_fini(&surface_overlap);
1334 }
1335 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001336}
1337
Matt Roper361d2ad2011-08-29 13:52:23 -07001338static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001339drm_output_fini_pixman(struct drm_output *output);
1340
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001341/**
1342 * Find the closest-matching mode for a given target
1343 *
1344 * Given a target mode, find the most suitable mode amongst the output's
1345 * current mode list to use, preferring the current mode if possible, to
1346 * avoid an expensive mode switch.
1347 *
1348 * @param output DRM output
1349 * @param target_mode Mode to attempt to match
1350 * @returns Pointer to a mode from the output's mode list
1351 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001352static struct drm_mode *
1353choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1354{
1355 struct drm_mode *tmp_mode = NULL, *mode;
1356
Hardeningff39efa2013-09-18 23:56:35 +02001357 if (output->base.current_mode->width == target_mode->width &&
1358 output->base.current_mode->height == target_mode->height &&
1359 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001360 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001361 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001362
1363 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1364 if (mode->mode_info.hdisplay == target_mode->width &&
1365 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001366 if (mode->base.refresh == target_mode->refresh ||
1367 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001368 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001369 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001370 tmp_mode = mode;
1371 }
1372 }
1373
1374 return tmp_mode;
1375}
1376
1377static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001378drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001379static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001380drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001381
1382static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001383drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1384{
1385 struct drm_output *output;
1386 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001387 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001388
1389 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001390 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001391 return -1;
1392 }
1393
1394 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001395 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001396 return -1;
1397 }
1398
Armin Krezović545dba62016-08-05 15:54:18 +02001399 b = to_drm_backend(output_base->compositor);
1400 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001401 drm_mode = choose_mode (output, mode);
1402
1403 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001404 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001405 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001406 }
1407
Hardeningff39efa2013-09-18 23:56:35 +02001408 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001409 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001410
Hardeningff39efa2013-09-18 23:56:35 +02001411 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001412
Hardeningff39efa2013-09-18 23:56:35 +02001413 output->base.current_mode = &drm_mode->base;
1414 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001415 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1416
Alex Wub7b8bda2012-04-17 17:20:48 +08001417 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001418 drm_output_release_fb(output, output->current);
1419 drm_output_release_fb(output, output->next);
1420 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001421
Giulio Camuffo954f1832014-10-11 18:27:30 +03001422 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001423 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001424 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001425 weston_log("failed to init output pixman state with "
1426 "new mode\n");
1427 return -1;
1428 }
1429 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001430 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001431 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001432
Giulio Camuffo954f1832014-10-11 18:27:30 +03001433 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001434 weston_log("failed to init output egl state with "
1435 "new mode");
1436 return -1;
1437 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001438 }
1439
Alex Wub7b8bda2012-04-17 17:20:48 +08001440 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001441}
1442
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001443static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001444on_drm_input(int fd, uint32_t mask, void *data)
1445{
1446 drmEventContext evctx;
1447
1448 memset(&evctx, 0, sizeof evctx);
1449 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1450 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001451 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001452 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001453
1454 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001455}
1456
1457static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001458init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001459{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001460 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001461 uint64_t cap;
1462 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001463 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001464
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001465 sysnum = udev_device_get_sysnum(device);
1466 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001467 b->drm.id = atoi(sysnum);
1468 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001469 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001470 return -1;
1471 }
1472
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001473 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001474 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001475 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001476 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001477 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001478 udev_device_get_devnode(device));
1479 return -1;
1480 }
1481
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001482 weston_log("using %s\n", filename);
1483
Giulio Camuffo954f1832014-10-11 18:27:30 +03001484 b->drm.fd = fd;
1485 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001486
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001487 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1488 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001489 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001490 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001491 clk_id = CLOCK_REALTIME;
1492
Giulio Camuffo954f1832014-10-11 18:27:30 +03001493 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001494 weston_log("Error: failed to set presentation clock %d.\n",
1495 clk_id);
1496 return -1;
1497 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001498
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001499 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1500 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001501 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001502 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001503 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001504
1505 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1506 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001507 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001508 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001509 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001510
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001511 return 0;
1512}
1513
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001514static struct gbm_device *
1515create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001516{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001517 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001518
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001519 gl_renderer = weston_load_module("gl-renderer.so",
1520 "gl_renderer_interface");
1521 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001522 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001523
1524 /* GBM will load a dri driver, but even though they need symbols from
1525 * libglapi, in some version of Mesa they are not linked to it. Since
1526 * only the gl-renderer module links to it, the call above won't make
1527 * these symbols globally available, and loading the DRI driver fails.
1528 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1529 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1530
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001531 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001532
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001533 return gbm;
1534}
1535
Bryce Harringtonc056a982015-05-19 15:25:18 -07001536/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001537 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001538 *
1539 * This returns 0 if substitution isn't possible, but 0 might be a
1540 * legitimate format for other EGL platforms, so the caller is
1541 * responsible for checking for 0 before calling gl_renderer->create().
1542 *
1543 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1544 * but it's entirely possible we'll see this again on other implementations.
1545 */
1546static int
1547fallback_format_for(uint32_t format)
1548{
1549 switch (format) {
1550 case GBM_FORMAT_XRGB8888:
1551 return GBM_FORMAT_ARGB8888;
1552 case GBM_FORMAT_XRGB2101010:
1553 return GBM_FORMAT_ARGB2101010;
1554 default:
1555 return 0;
1556 }
1557}
1558
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001559static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001560drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001561{
Derek Foreman6d556372015-11-04 14:47:33 -06001562 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001563 b->gbm_format,
1564 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001565 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001566 };
Derek Foreman6d556372015-11-04 14:47:33 -06001567 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001568
Derek Foremanc4cfe852015-05-15 12:12:40 -05001569 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001570 n_formats = 3;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001571 if (gl_renderer->create(b->compositor,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001572 EGL_PLATFORM_GBM_KHR,
Giulio Camuffo954f1832014-10-11 18:27:30 +03001573 (void *)b->gbm,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001574 gl_renderer->opaque_attribs,
1575 format,
1576 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001577 return -1;
1578 }
1579
1580 return 0;
1581}
1582
1583static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001584init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001585{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001586 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001587
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001589 return -1;
1590
Giulio Camuffo954f1832014-10-11 18:27:30 +03001591 if (drm_backend_create_gl_renderer(b) < 0) {
1592 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001593 return -1;
1594 }
1595
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001596 return 0;
1597}
1598
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001599static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001600init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001601{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001602 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001603}
1604
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001605/**
1606 * Add a mode to output's mode list
1607 *
1608 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1609 * output's mode list.
1610 *
1611 * @param output DRM output to add mode to
1612 * @param info DRM mode structure to add
1613 * @returns Newly-allocated Weston/DRM mode structure
1614 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001615static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001616drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001617{
1618 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001619 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001620
1621 mode = malloc(sizeof *mode);
1622 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001623 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001624
1625 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001626 mode->base.width = info->hdisplay;
1627 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001628
1629 /* Calculate higher precision (mHz) refresh rate */
1630 refresh = (info->clock * 1000000LL / info->htotal +
1631 info->vtotal / 2) / info->vtotal;
1632
1633 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1634 refresh *= 2;
1635 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1636 refresh /= 2;
1637 if (info->vscan > 1)
1638 refresh /= info->vscan;
1639
1640 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001641 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001642
1643 if (info->type & DRM_MODE_TYPE_PREFERRED)
1644 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1645
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001646 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1647
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001648 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001649}
1650
1651static int
1652drm_subpixel_to_wayland(int drm_value)
1653{
1654 switch (drm_value) {
1655 default:
1656 case DRM_MODE_SUBPIXEL_UNKNOWN:
1657 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1658 case DRM_MODE_SUBPIXEL_NONE:
1659 return WL_OUTPUT_SUBPIXEL_NONE;
1660 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1661 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1662 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1663 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1664 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1665 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1666 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1667 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1668 }
1669}
1670
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001671/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001672static uint32_t
1673drm_get_backlight(struct drm_output *output)
1674{
1675 long brightness, max_brightness, norm;
1676
1677 brightness = backlight_get_brightness(output->backlight);
1678 max_brightness = backlight_get_max_brightness(output->backlight);
1679
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001680 /* convert it on a scale of 0 to 255 */
1681 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001682
1683 return (uint32_t) norm;
1684}
1685
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001686/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001687static void
1688drm_set_backlight(struct weston_output *output_base, uint32_t value)
1689{
Armin Krezović545dba62016-08-05 15:54:18 +02001690 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001691 long max_brightness, new_brightness;
1692
1693 if (!output->backlight)
1694 return;
1695
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001696 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001697 return;
1698
1699 max_brightness = backlight_get_max_brightness(output->backlight);
1700
1701 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001702 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001703
1704 backlight_set_brightness(output->backlight, new_brightness);
1705}
1706
1707static drmModePropertyPtr
1708drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1709{
1710 drmModePropertyPtr props;
1711 int i;
1712
1713 for (i = 0; i < connector->count_props; i++) {
1714 props = drmModeGetProperty(fd, connector->props[i]);
1715 if (!props)
1716 continue;
1717
1718 if (!strcmp(props->name, name))
1719 return props;
1720
1721 drmModeFreeProperty(props);
1722 }
1723
1724 return NULL;
1725}
1726
1727static void
1728drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1729{
Armin Krezović545dba62016-08-05 15:54:18 +02001730 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001731 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001732 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001733 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001734
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001735 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001736 return;
1737
Daniel Stone36609c72015-06-18 07:49:02 +01001738 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1739 output->dpms_prop->prop_id, level);
1740 if (ret) {
1741 weston_log("DRM: DPMS: failed property set for %s\n",
1742 output->base.name);
1743 return;
1744 }
1745
1746 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001747}
1748
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001749static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001750 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1751 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1752 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1753 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1754 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1755 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1756 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1757 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1758 [DRM_MODE_CONNECTOR_Component] = "Component",
1759 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1760 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1761 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1762 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1763 [DRM_MODE_CONNECTOR_TV] = "TV",
1764 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001765#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001766 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1767 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001768#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001769};
1770
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001771static char *
1772make_connector_name(const drmModeConnector *con)
1773{
1774 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001775 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001776
1777 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1778 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001779
1780 if (!type_name)
1781 type_name = "UNNAMED";
1782
1783 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001784
1785 return strdup(name);
1786}
1787
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001788static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001789find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001790 drmModeRes *resources, drmModeConnector *connector)
1791{
1792 drmModeEncoder *encoder;
1793 uint32_t possible_crtcs;
1794 int i, j;
1795
1796 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001797 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001798 if (encoder == NULL) {
1799 weston_log("Failed to get encoder.\n");
1800 return -1;
1801 }
1802 possible_crtcs = encoder->possible_crtcs;
1803 drmModeFreeEncoder(encoder);
1804
1805 for (i = 0; i < resources->count_crtcs; i++) {
1806 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001807 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001808 return i;
1809 }
1810 }
1811
1812 return -1;
1813}
1814
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001815/* Init output state that depends on gl or gbm */
1816static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001817drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001818{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001819 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001820 output->gbm_format,
1821 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001822 };
1823 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001824
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001825 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001826 output->base.current_mode->width,
1827 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001828 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001829 GBM_BO_USE_SCANOUT |
1830 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001831 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001832 weston_log("failed to create gbm surface\n");
1833 return -1;
1834 }
1835
Derek Foremanc4cfe852015-05-15 12:12:40 -05001836 if (format[1])
1837 n_formats = 2;
Jonny Lamb671148f2015-03-20 15:26:52 +01001838 if (gl_renderer->output_create(&output->base,
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001839 (EGLNativeWindowType)output->gbm_surface,
1840 output->gbm_surface,
Neil Roberts77c1a5b2014-03-07 18:05:50 +00001841 gl_renderer->opaque_attribs,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001842 format,
1843 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001844 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001845 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001846 return -1;
1847 }
1848
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001849 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001850
1851 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001852 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001853 continue;
1854
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001855 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001856 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001857 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001858 }
1859
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001860 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001861 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001862 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001863 }
1864
1865 return 0;
1866}
1867
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001868static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001869drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001870{
Hardeningff39efa2013-09-18 23:56:35 +02001871 int w = output->base.current_mode->width;
1872 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001873 uint32_t format = output->gbm_format;
1874 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001875 unsigned int i;
1876
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001877 switch (format) {
1878 case GBM_FORMAT_XRGB8888:
1879 pixman_format = PIXMAN_x8r8g8b8;
1880 break;
1881 case GBM_FORMAT_RGB565:
1882 pixman_format = PIXMAN_r5g6b5;
1883 break;
1884 default:
1885 weston_log("Unsupported pixman format 0x%x\n", format);
1886 return -1;
1887 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001888
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001889 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001890 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001891 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001892 if (!output->dumb[i])
1893 goto err;
1894
1895 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001896 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001897 output->dumb[i]->map,
1898 output->dumb[i]->stride);
1899 if (!output->image[i])
1900 goto err;
1901 }
1902
1903 if (pixman_renderer_output_create(&output->base) < 0)
1904 goto err;
1905
1906 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001907 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001908
1909 return 0;
1910
1911err:
1912 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1913 if (output->dumb[i])
1914 drm_fb_destroy_dumb(output->dumb[i]);
1915 if (output->image[i])
1916 pixman_image_unref(output->image[i]);
1917
1918 output->dumb[i] = NULL;
1919 output->image[i] = NULL;
1920 }
1921
1922 return -1;
1923}
1924
1925static void
1926drm_output_fini_pixman(struct drm_output *output)
1927{
1928 unsigned int i;
1929
1930 pixman_renderer_output_destroy(&output->base);
1931 pixman_region32_fini(&output->previous_damage);
1932
1933 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1934 drm_fb_destroy_dumb(output->dumb[i]);
1935 pixman_image_unref(output->image[i]);
1936 output->dumb[i] = NULL;
1937 output->image[i] = NULL;
1938 }
1939}
1940
Richard Hughes2b2092a2013-04-24 14:58:02 +01001941static void
1942edid_parse_string(const uint8_t *data, char text[])
1943{
1944 int i;
1945 int replaced = 0;
1946
1947 /* this is always 12 bytes, but we can't guarantee it's null
1948 * terminated or not junk. */
1949 strncpy(text, (const char *) data, 12);
1950
Bryce Harrington9c7de162015-08-28 13:04:26 -07001951 /* guarantee our new string is null-terminated */
1952 text[12] = '\0';
1953
Richard Hughes2b2092a2013-04-24 14:58:02 +01001954 /* remove insane chars */
1955 for (i = 0; text[i] != '\0'; i++) {
1956 if (text[i] == '\n' ||
1957 text[i] == '\r') {
1958 text[i] = '\0';
1959 break;
1960 }
1961 }
1962
1963 /* ensure string is printable */
1964 for (i = 0; text[i] != '\0'; i++) {
1965 if (!isprint(text[i])) {
1966 text[i] = '-';
1967 replaced++;
1968 }
1969 }
1970
1971 /* if the string is random junk, ignore the string */
1972 if (replaced > 4)
1973 text[0] = '\0';
1974}
1975
1976#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1977#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1978#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1979#define EDID_OFFSET_DATA_BLOCKS 0x36
1980#define EDID_OFFSET_LAST_BLOCK 0x6c
1981#define EDID_OFFSET_PNPID 0x08
1982#define EDID_OFFSET_SERIAL 0x0c
1983
1984static int
1985edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1986{
1987 int i;
1988 uint32_t serial_number;
1989
1990 /* check header */
1991 if (length < 128)
1992 return -1;
1993 if (data[0] != 0x00 || data[1] != 0xff)
1994 return -1;
1995
1996 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1997 * /--08--\/--09--\
1998 * 7654321076543210
1999 * |\---/\---/\---/
2000 * R C1 C2 C3 */
2001 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2002 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2003 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2004 edid->pnp_id[3] = '\0';
2005
2006 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2007 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2008 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2009 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2010 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2011 if (serial_number > 0)
2012 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2013
2014 /* parse EDID data */
2015 for (i = EDID_OFFSET_DATA_BLOCKS;
2016 i <= EDID_OFFSET_LAST_BLOCK;
2017 i += 18) {
2018 /* ignore pixel clock data */
2019 if (data[i] != 0)
2020 continue;
2021 if (data[i+2] != 0)
2022 continue;
2023
2024 /* any useful blocks? */
2025 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2026 edid_parse_string(&data[i+5],
2027 edid->monitor_name);
2028 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2029 edid_parse_string(&data[i+5],
2030 edid->serial_number);
2031 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2032 edid_parse_string(&data[i+5],
2033 edid->eisa_id);
2034 }
2035 }
2036 return 0;
2037}
2038
2039static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002040find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002041 struct drm_output *output,
2042 drmModeConnector *connector)
2043{
2044 drmModePropertyBlobPtr edid_blob = NULL;
2045 drmModePropertyPtr property;
2046 int i;
2047 int rc;
2048
2049 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002050 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002051 if (!property)
2052 continue;
2053 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2054 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002055 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002056 connector->prop_values[i]);
2057 }
2058 drmModeFreeProperty(property);
2059 }
2060 if (!edid_blob)
2061 return;
2062
2063 rc = edid_parse(&output->edid,
2064 edid_blob->data,
2065 edid_blob->length);
2066 if (!rc) {
2067 weston_log("EDID data '%s', '%s', '%s'\n",
2068 output->edid.pnp_id,
2069 output->edid.monitor_name,
2070 output->edid.serial_number);
2071 if (output->edid.pnp_id[0] != '\0')
2072 output->base.make = output->edid.pnp_id;
2073 if (output->edid.monitor_name[0] != '\0')
2074 output->base.model = output->edid.monitor_name;
2075 if (output->edid.serial_number[0] != '\0')
2076 output->base.serial_number = output->edid.serial_number;
2077 }
2078 drmModeFreePropertyBlob(edid_blob);
2079}
2080
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002081
2082
2083static int
2084parse_modeline(const char *s, drmModeModeInfo *mode)
2085{
2086 char hsync[16];
2087 char vsync[16];
2088 float fclock;
2089
2090 mode->type = DRM_MODE_TYPE_USERDEF;
2091 mode->hskew = 0;
2092 mode->vscan = 0;
2093 mode->vrefresh = 0;
2094 mode->flags = 0;
2095
Rob Bradford307e09e2013-07-26 16:29:40 +01002096 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002097 &fclock,
2098 &mode->hdisplay,
2099 &mode->hsync_start,
2100 &mode->hsync_end,
2101 &mode->htotal,
2102 &mode->vdisplay,
2103 &mode->vsync_start,
2104 &mode->vsync_end,
2105 &mode->vtotal, hsync, vsync) != 11)
2106 return -1;
2107
2108 mode->clock = fclock * 1000;
2109 if (strcmp(hsync, "+hsync") == 0)
2110 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2111 else if (strcmp(hsync, "-hsync") == 0)
2112 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2113 else
2114 return -1;
2115
2116 if (strcmp(vsync, "+vsync") == 0)
2117 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2118 else if (strcmp(vsync, "-vsync") == 0)
2119 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2120 else
2121 return -1;
2122
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002123 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2124 mode->hdisplay, mode->vdisplay, fclock);
2125
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002126 return 0;
2127}
2128
Rob Bradford66bd9f52013-06-25 18:56:42 +01002129static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002130setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002131 struct weston_output *output,
2132 const char *s)
2133{
2134 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002135 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002136 struct udev_seat *seat;
2137
Giulio Camuffo954f1832014-10-11 18:27:30 +03002138 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002139 if (!seat)
2140 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002141
Derek Foreman0720ea32015-07-15 13:00:35 -05002142 seat->base.output = output;
2143
Derek Foreman1281a362015-07-31 16:55:32 -05002144 pointer = weston_seat_get_pointer(&seat->base);
2145 if (pointer)
2146 weston_pointer_clamp(pointer,
2147 &pointer->x,
2148 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002149 }
2150}
2151
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002152static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002153parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002154{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002155 int ret = 0;
2156
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002157 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002158 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002159 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002160 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002161 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002162 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002163 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002164 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002165 else {
2166 weston_log("fatal: unrecognized pixel format: %s\n", s);
2167 ret = -1;
2168 }
2169
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002170 return ret;
2171}
2172
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002173/**
2174 * Choose suitable mode for an output
2175 *
2176 * Find the most suitable mode to use for initial setup (or reconfiguration on
2177 * hotplug etc) for a DRM output.
2178 *
2179 * @param output DRM output to choose mode for
2180 * @param kind Strategy and preference to use when choosing mode
2181 * @param width Desired width for this output
2182 * @param height Desired height for this output
2183 * @param current_mode Mode currently being displayed on this output
2184 * @param modeline Manually-entered mode (may be NULL)
2185 * @returns A mode from the output's mode list, or NULL if none available
2186 */
2187static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002188drm_output_choose_initial_mode(struct drm_backend *backend,
2189 struct drm_output *output,
2190 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002191 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002192 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002193{
2194 struct drm_mode *preferred = NULL;
2195 struct drm_mode *current = NULL;
2196 struct drm_mode *configured = NULL;
2197 struct drm_mode *best = NULL;
2198 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002199 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002200 int32_t width = 0;
2201 int32_t height = 0;
2202
Armin Krezović08368132016-09-30 14:11:05 +02002203 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
2204 if (sscanf(modeline, "%dx%d", &width, &height) != 2) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002205 width = -1;
2206
Armin Krezović08368132016-09-30 14:11:05 +02002207 if (parse_modeline(modeline, &drm_modeline) == 0) {
2208 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002209 if (!configured)
2210 return NULL;
2211 } else {
2212 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002213 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002214 }
2215 }
2216 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002217
2218 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002219 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002220 height == drm_mode->base.height)
2221 configured = drm_mode;
2222
comic fans7a5c5622016-03-17 14:29:27 +02002223 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002224 sizeof *current_mode) == 0)
2225 current = drm_mode;
2226
2227 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2228 preferred = drm_mode;
2229
2230 best = drm_mode;
2231 }
2232
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002233 if (current == NULL && current_mode->clock != 0) {
2234 current = drm_output_add_mode(output, current_mode);
2235 if (!current)
2236 return NULL;
2237 }
2238
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002239 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002240 configured = current;
2241
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002242 if (configured)
2243 return configured;
2244
2245 if (preferred)
2246 return preferred;
2247
2248 if (current)
2249 return current;
2250
2251 if (best)
2252 return best;
2253
2254 weston_log("no available modes for %s\n", output->base.name);
2255 return NULL;
2256}
2257
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002258static int
2259connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2260 drmModeModeInfo *mode)
2261{
2262 drmModeEncoder *encoder;
2263 drmModeCrtc *crtc;
2264
2265 /* Get the current mode on the crtc that's currently driving
2266 * this connector. */
2267 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2268 memset(mode, 0, sizeof *mode);
2269 if (encoder != NULL) {
2270 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2271 drmModeFreeEncoder(encoder);
2272 if (crtc == NULL)
2273 return -1;
2274 if (crtc->mode_valid)
2275 *mode = crtc->mode;
2276 drmModeFreeCrtc(crtc);
2277 }
2278
2279 return 0;
2280}
2281
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002282static int
Armin Krezović08368132016-09-30 14:11:05 +02002283drm_output_set_mode(struct weston_output *base,
2284 enum weston_drm_backend_output_mode mode,
2285 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002286{
Armin Krezović08368132016-09-30 14:11:05 +02002287 struct drm_output *output = to_drm_output(base);
2288 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002289
Armin Krezović08368132016-09-30 14:11:05 +02002290 struct drm_mode *drm_mode, *next, *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002291 drmModeModeInfo crtc_mode;
2292 int i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002293
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002294 output->base.make = "unknown";
2295 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002296 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002297 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002298
Giulio Camuffo954f1832014-10-11 18:27:30 +03002299 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
Matt Roper361d2ad2011-08-29 13:52:23 -07002300
Armin Krezović08368132016-09-30 14:11:05 +02002301 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002302 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002303
Armin Krezović08368132016-09-30 14:11:05 +02002304 for (i = 0; i < output->connector->count_modes; i++) {
2305 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002306 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002307 goto err_free;
2308 }
2309
Armin Krezović08368132016-09-30 14:11:05 +02002310 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002311 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002312 goto err_free;
Armin Krezović08368132016-09-30 14:11:05 +02002313
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002314 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002315 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002316
Armin Krezović08368132016-09-30 14:11:05 +02002317 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2318 output->base.native_mode = output->base.current_mode;
2319 output->base.native_scale = output->base.current_scale;
2320
2321 output->base.mm_width = output->connector->mmWidth;
2322 output->base.mm_height = output->connector->mmHeight;
2323
2324 return 0;
2325
2326err_free:
2327 drmModeFreeCrtc(output->original_crtc);
2328 output->original_crtc = NULL;
2329
2330 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2331 base.link) {
2332 wl_list_remove(&drm_mode->base.link);
2333 free(drm_mode);
2334 }
2335
2336 return -1;
2337}
2338
2339static void
2340drm_output_set_gbm_format(struct weston_output *base,
2341 const char *gbm_format)
2342{
2343 struct drm_output *output = to_drm_output(base);
2344 struct drm_backend *b = to_drm_backend(base->compositor);
2345
2346 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2347 output->gbm_format = b->gbm_format;
2348}
2349
2350static void
2351drm_output_set_seat(struct weston_output *base,
2352 const char *seat)
2353{
2354 struct drm_output *output = to_drm_output(base);
2355 struct drm_backend *b = to_drm_backend(base->compositor);
2356
2357 setup_output_seat_constraint(b, &output->base,
2358 seat ? seat : "");
2359}
2360
2361static int
2362drm_output_enable(struct weston_output *base)
2363{
2364 struct drm_output *output = to_drm_output(base);
2365 struct drm_backend *b = to_drm_backend(base->compositor);
2366 struct weston_mode *m;
2367
2368 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002369
Giulio Camuffo954f1832014-10-11 18:27:30 +03002370 if (b->use_pixman) {
2371 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002372 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002373 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002374 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002375 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002376 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002377 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002378 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002379
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002380 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002381 weston_log("Initialized backlight, device %s\n",
2382 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002383 output->base.set_backlight = drm_set_backlight;
2384 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002385 } else {
2386 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002387 }
2388
Jonas Ådahle5a12252013-04-05 23:07:11 +02002389 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002390 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002391 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002392 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002393 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002394
Richard Hughese7299962013-05-01 21:52:12 +01002395 output->base.gamma_size = output->original_crtc->gamma_size;
2396 output->base.set_gamma = drm_output_set_gamma;
2397
Armin Krezović08368132016-09-30 14:11:05 +02002398 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2399
2400 find_and_parse_output_edid(b, output, output->connector);
2401 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2402 output->base.connection_internal = 1;
2403
Derek Foremanb0427562016-02-05 15:55:20 -06002404 weston_plane_init(&output->cursor_plane, b->compositor,
2405 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002406 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002407
Giulio Camuffo954f1832014-10-11 18:27:30 +03002408 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2409 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2410 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002411
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002412 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002413 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002414 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002415 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002416 m->width, m->height, m->refresh / 1000.0,
2417 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2418 ", preferred" : "",
2419 m->flags & WL_OUTPUT_MODE_CURRENT ?
2420 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002421 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002422 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002423
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002424 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002425
2426err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002427 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002428
David Herrmann0f0d54e2011-12-08 17:05:45 +01002429 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002430}
2431
Jesse Barnes58ef3792012-02-23 09:45:49 -05002432static void
Armin Krezović08368132016-09-30 14:11:05 +02002433drm_output_deinit(struct weston_output *base)
2434{
2435 struct drm_output *output = to_drm_output(base);
2436 struct drm_backend *b = to_drm_backend(base->compositor);
2437
2438 if (b->use_pixman) {
2439 drm_output_fini_pixman(output);
2440 } else {
2441 gl_renderer->output_destroy(&output->base);
2442 gbm_surface_destroy(output->gbm_surface);
2443 }
2444
2445 weston_plane_release(&output->fb_plane);
2446 weston_plane_release(&output->cursor_plane);
2447
2448 drmModeFreeProperty(output->dpms_prop);
2449
2450 /* Turn off hardware cursor */
2451 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2452}
2453
2454static void
2455drm_output_destroy(struct weston_output *base)
2456{
2457 struct drm_output *output = to_drm_output(base);
2458 struct drm_backend *b = to_drm_backend(base->compositor);
2459 drmModeCrtcPtr origcrtc = output->original_crtc;
2460
2461 if (output->page_flip_pending) {
2462 output->destroy_pending = 1;
2463 weston_log("destroy output while page flip pending\n");
2464 return;
2465 }
2466
2467 if (output->base.enabled)
2468 drm_output_deinit(&output->base);
2469
2470 if (origcrtc) {
2471 /* Restore original CRTC state */
2472 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2473 origcrtc->x, origcrtc->y,
2474 &output->connector_id, 1, &origcrtc->mode);
2475 drmModeFreeCrtc(origcrtc);
2476 }
2477
2478 weston_output_destroy(&output->base);
2479
2480 drmModeFreeConnector(output->connector);
2481
2482 if (output->backlight)
2483 backlight_destroy(output->backlight);
2484
2485 b->crtc_allocator &= ~(1 << output->crtc_id);
2486 b->connector_allocator &= ~(1 << output->connector_id);
2487
2488 free(output);
2489}
2490
2491static int
2492drm_output_disable(struct weston_output *base)
2493{
2494 struct drm_output *output = to_drm_output(base);
2495 struct drm_backend *b = to_drm_backend(base->compositor);
2496
2497 if (output->page_flip_pending) {
2498 output->disable_pending = 1;
2499 return -1;
2500 }
2501
2502 if (output->base.enabled)
2503 drm_output_deinit(&output->base);
2504
2505 output->disable_pending = 0;
2506
2507 weston_log("Disabling output %s\n", output->base.name);
2508 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2509 0, 0, 0, 0, 0, NULL);
2510
2511 return 0;
2512}
2513
2514/**
2515 * Create a Weston output structure
2516 *
2517 * Given a DRM connector, create a matching drm_output structure and add it
2518 * to Weston's output list. It also takes ownership of the connector, which
2519 * is released when output is destroyed.
2520 *
2521 * @param b Weston backend structure
2522 * @param resources DRM resources for this device
2523 * @param connector DRM connector to use for this new output
2524 * @param drm_device udev device pointer
2525 * @returns 0 on success, or -1 on failure
2526 */
2527static int
2528create_output_for_connector(struct drm_backend *b,
2529 drmModeRes *resources,
2530 drmModeConnector *connector,
2531 struct udev_device *drm_device)
2532{
2533 struct drm_output *output;
2534 int i;
2535
2536 i = find_crtc_for_connector(b, resources, connector);
2537 if (i < 0) {
2538 weston_log("No usable crtc/encoder pair for connector.\n");
2539 return -1;
2540 }
2541
2542 output = zalloc(sizeof *output);
2543 if (output == NULL)
2544 return -1;
2545
2546 output->connector = connector;
2547 output->crtc_id = resources->crtcs[i];
2548 output->pipe = i;
2549 output->connector_id = connector->connector_id;
2550
2551 output->backlight = backlight_init(drm_device,
2552 connector->connector_type);
2553
2554 output->base.enable = drm_output_enable;
2555 output->base.destroy = drm_output_destroy;
2556 output->base.disable = drm_output_disable;
2557 output->base.name = make_connector_name(connector);
2558
2559 output->destroy_pending = 0;
2560 output->disable_pending = 0;
2561 output->original_crtc = NULL;
2562
2563 b->crtc_allocator |= (1 << output->crtc_id);
2564 b->connector_allocator |= (1 << output->connector_id);
2565
2566 weston_output_init_pending(&output->base, b->compositor);
2567 weston_compositor_add_pending_output(&output->base, b->compositor);
2568
2569 return 0;
2570}
2571
2572static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002573create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002574{
2575 struct drm_sprite *sprite;
2576 drmModePlaneRes *plane_res;
2577 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002578 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002579
Giulio Camuffo954f1832014-10-11 18:27:30 +03002580 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002581 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002582 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002583 strerror(errno));
2584 return;
2585 }
2586
2587 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002588 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002589 if (!plane)
2590 continue;
2591
Peter Huttererf3d62272013-08-08 11:57:05 +10002592 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002593 plane->count_formats));
2594 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002595 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002596 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002597 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002598 continue;
2599 }
2600
Jesse Barnes58ef3792012-02-23 09:45:49 -05002601 sprite->possible_crtcs = plane->possible_crtcs;
2602 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002603 sprite->current = NULL;
2604 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002605 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002606 sprite->count_formats = plane->count_formats;
2607 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002608 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002609 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002610 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2611 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2612 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002613
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002615 }
2616
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002617 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002618}
2619
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002620static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002621destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002622{
2623 struct drm_sprite *sprite, *next;
2624 struct drm_output *output;
2625
Giulio Camuffo954f1832014-10-11 18:27:30 +03002626 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002627 struct drm_output, base.link);
2628
Giulio Camuffo954f1832014-10-11 18:27:30 +03002629 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2630 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002631 sprite->plane_id,
2632 output->crtc_id, 0, 0,
2633 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002634 drm_output_release_fb(output, sprite->current);
2635 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002636 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002637 free(sprite);
2638 }
2639}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002640
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002641static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002642create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002643 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002644{
2645 drmModeConnector *connector;
2646 drmModeRes *resources;
2647 int i;
2648
Giulio Camuffo954f1832014-10-11 18:27:30 +03002649 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002650 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002651 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002652 return -1;
2653 }
2654
Giulio Camuffo954f1832014-10-11 18:27:30 +03002655 b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
2656 if (!b->crtcs) {
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002657 drmModeFreeResources(resources);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002658 return -1;
Christopher Michaeleb866cd2012-03-07 14:55:21 -05002659 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05002660
Giulio Camuffo954f1832014-10-11 18:27:30 +03002661 b->min_width = resources->min_width;
2662 b->max_width = resources->max_width;
2663 b->min_height = resources->min_height;
2664 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002665
Giulio Camuffo954f1832014-10-11 18:27:30 +03002666 b->num_crtcs = resources->count_crtcs;
2667 memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002668
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002669 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002670 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002671 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002672 if (connector == NULL)
2673 continue;
2674
2675 if (connector->connection == DRM_MODE_CONNECTED &&
2676 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002677 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002678 if (create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002679 connector, drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002680 drmModeFreeConnector(connector);
2681 continue;
2682 }
Armin Krezović08368132016-09-30 14:11:05 +02002683 } else {
2684 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002685 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002686 }
2687
Armin Krezović08368132016-09-30 14:11:05 +02002688 if (wl_list_empty(&b->compositor->output_list) &&
2689 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002690 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002691
2692 drmModeFreeResources(resources);
2693
2694 return 0;
2695}
2696
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002697static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002698update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002699{
2700 drmModeConnector *connector;
2701 drmModeRes *resources;
2702 struct drm_output *output, *next;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002703 uint32_t connected = 0, disconnects = 0;
2704 int i;
2705
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002707 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002708 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002709 return;
2710 }
2711
2712 /* collect new connects */
2713 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002714 int connector_id = resources->connectors[i];
2715
Giulio Camuffo954f1832014-10-11 18:27:30 +03002716 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002717 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002718 continue;
2719
David Herrmann7551cff2011-12-08 17:05:43 +01002720 if (connector->connection != DRM_MODE_CONNECTED) {
2721 drmModeFreeConnector(connector);
2722 continue;
2723 }
2724
Benjamin Franzke117483d2011-08-30 11:38:26 +02002725 connected |= (1 << connector_id);
2726
Giulio Camuffo954f1832014-10-11 18:27:30 +03002727 if (!(b->connector_allocator & (1 << connector_id))) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002728 create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002729 connector, drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002730 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002731
Armin Krezović08368132016-09-30 14:11:05 +02002732 } else {
2733 drmModeFreeConnector(connector);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002734 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002735 }
2736 drmModeFreeResources(resources);
2737
Giulio Camuffo954f1832014-10-11 18:27:30 +03002738 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002739 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002740 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002741 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002742 if (disconnects & (1 << output->connector_id)) {
2743 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002744 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002745 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002746 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002747 }
2748 }
Armin Krezović08368132016-09-30 14:11:05 +02002749
2750 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2751 base.link) {
2752 if (disconnects & (1 << output->connector_id)) {
2753 disconnects &= ~(1 << output->connector_id);
2754 weston_log("connector %d disconnected\n",
2755 output->connector_id);
2756 drm_output_destroy(&output->base);
2757 }
2758 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002760}
2761
2762static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002763udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002764{
David Herrmannd7488c22012-03-11 20:05:21 +01002765 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002766 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002767
2768 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002769 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002770 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002771
David Herrmann6ac52db2012-03-11 20:05:22 +01002772 val = udev_device_get_property_value(device, "HOTPLUG");
2773 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002774 return 0;
2775
David Herrmann6ac52db2012-03-11 20:05:22 +01002776 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002777}
2778
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002779static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002780udev_drm_event(int fd, uint32_t mask, void *data)
2781{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002782 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002783 struct udev_device *event;
2784
Giulio Camuffo954f1832014-10-11 18:27:30 +03002785 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002786
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 if (udev_event_is_hotplug(b, event))
2788 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002789
2790 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002791
2792 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002793}
2794
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002795static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002796drm_restore(struct weston_compositor *ec)
2797{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002798 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002799}
2800
2801static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002802drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002803{
Armin Krezović545dba62016-08-05 15:54:18 +02002804 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002805
Giulio Camuffo954f1832014-10-11 18:27:30 +03002806 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002807
Giulio Camuffo954f1832014-10-11 18:27:30 +03002808 wl_event_source_remove(b->udev_drm_source);
2809 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002810
Giulio Camuffo954f1832014-10-11 18:27:30 +03002811 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002812
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002813 weston_compositor_shutdown(ec);
2814
Giulio Camuffo954f1832014-10-11 18:27:30 +03002815 if (b->gbm)
2816 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002817
Giulio Camuffo954f1832014-10-11 18:27:30 +03002818 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002819
Giulio Camuffo954f1832014-10-11 18:27:30 +03002820 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002821 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002822}
2823
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002824static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002825drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002826{
2827 struct drm_output *output;
2828 struct drm_mode *drm_mode;
2829 int ret;
2830
Giulio Camuffo954f1832014-10-11 18:27:30 +03002831 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002832 if (!output->current) {
2833 /* If something that would cause the output to
2834 * switch mode happened while in another vt, we
2835 * might not have a current drm_fb. In that case,
2836 * schedule a repaint and let drm_output_repaint
2837 * handle setting the mode. */
2838 weston_output_schedule_repaint(&output->base);
2839 continue;
2840 }
2841
Hardeningff39efa2013-09-18 23:56:35 +02002842 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002843 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002844 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002845 &output->connector_id, 1,
2846 &drm_mode->mode_info);
2847 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002848 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002849 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002850 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002851 output->base.x, output->base.y);
2852 }
2853 }
2854}
2855
2856static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002857session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002858{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002859 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002860 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002861 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002862 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002863
Giulio Camuffo954f1832014-10-11 18:27:30 +03002864 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002865 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002866 compositor->state = b->prev_state;
2867 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002868 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002869 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002870 } else {
2871 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002872 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002873
Giulio Camuffo954f1832014-10-11 18:27:30 +03002874 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002875 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002876
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002877 /* If we have a repaint scheduled (either from a
2878 * pending pageflip or the idle handler), make sure we
2879 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002880 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002881 * further attemps at repainting. When we switch
2882 * back, we schedule a repaint, which will process
2883 * pending frame callbacks. */
2884
Giulio Camuffo954f1832014-10-11 18:27:30 +03002885 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002886 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002887 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002888 }
2889
Giulio Camuffo954f1832014-10-11 18:27:30 +03002890 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002891 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002892
Giulio Camuffo954f1832014-10-11 18:27:30 +03002893 wl_list_for_each(sprite, &b->sprite_list, link)
2894 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002895 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002896 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002897 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002898 };
2899}
2900
David Herrmann0af066f2012-10-29 19:21:16 +01002901/*
2902 * Find primary GPU
2903 * Some systems may have multiple DRM devices attached to a single seat. This
2904 * function loops over all devices and tries to find a PCI device with the
2905 * boot_vga sysfs attribute set to 1.
2906 * If no such device is found, the first DRM device reported by udev is used.
2907 */
2908static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002909find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002910{
2911 struct udev_enumerate *e;
2912 struct udev_list_entry *entry;
2913 const char *path, *device_seat, *id;
2914 struct udev_device *device, *drm_device, *pci;
2915
Giulio Camuffo954f1832014-10-11 18:27:30 +03002916 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002917 udev_enumerate_add_match_subsystem(e, "drm");
2918 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2919
2920 udev_enumerate_scan_devices(e);
2921 drm_device = NULL;
2922 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2923 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002924 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002925 if (!device)
2926 continue;
2927 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2928 if (!device_seat)
2929 device_seat = default_seat;
2930 if (strcmp(device_seat, seat)) {
2931 udev_device_unref(device);
2932 continue;
2933 }
2934
2935 pci = udev_device_get_parent_with_subsystem_devtype(device,
2936 "pci", NULL);
2937 if (pci) {
2938 id = udev_device_get_sysattr_value(pci, "boot_vga");
2939 if (id && !strcmp(id, "1")) {
2940 if (drm_device)
2941 udev_device_unref(drm_device);
2942 drm_device = device;
2943 break;
2944 }
2945 }
2946
2947 if (!drm_device)
2948 drm_device = device;
2949 else
2950 udev_device_unref(device);
2951 }
2952
2953 udev_enumerate_unref(e);
2954 return drm_device;
2955}
2956
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002957static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002958planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2959 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002960{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002961 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002962
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002963 switch (key) {
2964 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002965 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002966 break;
2967 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002968 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002969 break;
2970 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002971 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002972 break;
2973 default:
2974 break;
2975 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002976}
2977
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002978#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002979static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002980recorder_destroy(struct drm_output *output)
2981{
2982 vaapi_recorder_destroy(output->recorder);
2983 output->recorder = NULL;
2984
2985 output->base.disable_planes--;
2986
2987 wl_list_remove(&output->recorder_frame_listener.link);
2988 weston_log("[libva recorder] done\n");
2989}
2990
2991static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002992recorder_frame_notify(struct wl_listener *listener, void *data)
2993{
2994 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002995 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002996 int fd, ret;
2997
2998 output = container_of(listener, struct drm_output,
2999 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003000 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003001
3002 if (!output->recorder)
3003 return;
3004
Giulio Camuffo954f1832014-10-11 18:27:30 +03003005 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003006 DRM_CLOEXEC, &fd);
3007 if (ret) {
3008 weston_log("[libva recorder] "
3009 "failed to create prime fd for front buffer\n");
3010 return;
3011 }
3012
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003013 ret = vaapi_recorder_frame(output->recorder, fd,
3014 output->current->stride);
3015 if (ret < 0) {
3016 weston_log("[libva recorder] aborted: %m\n");
3017 recorder_destroy(output);
3018 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003019}
3020
3021static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003022create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003023 const char *filename)
3024{
3025 int fd;
3026 drm_magic_t magic;
3027
Giulio Camuffo954f1832014-10-11 18:27:30 +03003028 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003029 if (fd < 0)
3030 return NULL;
3031
3032 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003033 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003034
3035 return vaapi_recorder_create(fd, width, height, filename);
3036}
3037
3038static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003039recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003040 void *data)
3041{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003042 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003043 struct drm_output *output;
3044 int width, height;
3045
Giulio Camuffo954f1832014-10-11 18:27:30 +03003046 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003047 struct drm_output, base.link);
3048
3049 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003050 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003051 weston_log("failed to start vaapi recorder: "
3052 "output format not supported\n");
3053 return;
3054 }
3055
Hardeningff39efa2013-09-18 23:56:35 +02003056 width = output->base.current_mode->width;
3057 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003058
3059 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003060 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003061 if (!output->recorder) {
3062 weston_log("failed to create vaapi recorder\n");
3063 return;
3064 }
3065
3066 output->base.disable_planes++;
3067
3068 output->recorder_frame_listener.notify = recorder_frame_notify;
3069 wl_signal_add(&output->base.frame_signal,
3070 &output->recorder_frame_listener);
3071
3072 weston_output_schedule_repaint(&output->base);
3073
3074 weston_log("[libva recorder] initialized\n");
3075 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003076 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003077 }
3078}
3079#else
3080static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003081recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003082 void *data)
3083{
3084 weston_log("Compiled without libva support\n");
3085}
3086#endif
3087
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003088static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003089switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003090{
3091 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003092 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003093
Giulio Camuffo954f1832014-10-11 18:27:30 +03003094 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003095 return;
3096
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003097 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3098
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003099 weston_log("Switching to GL renderer\n");
3100
Giulio Camuffo954f1832014-10-11 18:27:30 +03003101 b->gbm = create_gbm_device(b->drm.fd);
3102 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003103 weston_log("Failed to create gbm device. "
3104 "Aborting renderer switch\n");
3105 return;
3106 }
3107
Giulio Camuffo954f1832014-10-11 18:27:30 +03003108 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003109 pixman_renderer_output_destroy(&output->base);
3110
Giulio Camuffo954f1832014-10-11 18:27:30 +03003111 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003112
Giulio Camuffo954f1832014-10-11 18:27:30 +03003113 if (drm_backend_create_gl_renderer(b) < 0) {
3114 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003115 weston_log("Failed to create GL renderer. Quitting.\n");
3116 /* FIXME: we need a function to shutdown cleanly */
3117 assert(0);
3118 }
3119
Giulio Camuffo954f1832014-10-11 18:27:30 +03003120 wl_list_for_each(output, &b->compositor->output_list, base.link)
3121 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003122
Giulio Camuffo954f1832014-10-11 18:27:30 +03003123 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003124
3125 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3126 if (linux_dmabuf_setup(b->compositor) < 0)
3127 weston_log("Error: initializing dmabuf "
3128 "support failed.\n");
3129 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003130}
3131
3132static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003133renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3134 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003135{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003136 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003137 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003138
Giulio Camuffo954f1832014-10-11 18:27:30 +03003139 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003140}
3141
Armin Krezović08368132016-09-30 14:11:05 +02003142static const struct weston_drm_output_api api = {
3143 drm_output_set_mode,
3144 drm_output_set_gbm_format,
3145 drm_output_set_seat,
3146};
3147
Giulio Camuffo954f1832014-10-11 18:27:30 +03003148static struct drm_backend *
3149drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003150 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003151{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003152 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003153 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003154 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003155 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003156 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003157 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003158
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003159 weston_log("initializing drm backend\n");
3160
Giulio Camuffo954f1832014-10-11 18:27:30 +03003161 b = zalloc(sizeof *b);
3162 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003163 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003164
Pekka Paalanen68583832015-05-19 09:53:16 +03003165 /*
3166 * KMS support for hardware planes cannot properly synchronize
3167 * without nuclear page flip. Without nuclear/atomic, hw plane
3168 * and cursor plane updates would either tear or cause extra
3169 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003170 * to a fraction. For cursors, it's not so bad, so they are
3171 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003172 *
3173 * These can be enabled again when nuclear/atomic support lands.
3174 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003175 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003176 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003177 b->use_pixman = config->use_pixman;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003178 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003179
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003180 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3181 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003182
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003183 if (config->seat_id)
3184 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003185
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003186 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003187 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3188 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003189 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003190 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003191 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003192 goto err_compositor;
3193 }
3194
Giulio Camuffo954f1832014-10-11 18:27:30 +03003195 b->udev = udev_new();
3196 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003197 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003198 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003199 }
3200
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 b->session_listener.notify = session_notify;
3202 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003203
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003204 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003205 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003206 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003207 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003208 }
David Herrmann0af066f2012-10-29 19:21:16 +01003209 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003210
Giulio Camuffo954f1832014-10-11 18:27:30 +03003211 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003212 weston_log("failed to initialize kms\n");
3213 goto err_udev_dev;
3214 }
3215
Giulio Camuffo954f1832014-10-11 18:27:30 +03003216 if (b->use_pixman) {
3217 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003218 weston_log("failed to initialize pixman renderer\n");
3219 goto err_udev_dev;
3220 }
3221 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003222 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003223 weston_log("failed to initialize egl\n");
3224 goto err_udev_dev;
3225 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003226 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003227
Giulio Camuffo954f1832014-10-11 18:27:30 +03003228 b->base.destroy = drm_destroy;
3229 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003230
Giulio Camuffo954f1832014-10-11 18:27:30 +03003231 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003232
Bob Ham91880f12016-01-12 10:21:47 +00003233 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003234
Giulio Camuffo954f1832014-10-11 18:27:30 +03003235 wl_list_init(&b->sprite_list);
3236 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003237
Giulio Camuffo954f1832014-10-11 18:27:30 +03003238 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003239 compositor, b->udev, seat_id,
3240 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003241 weston_log("failed to create input devices\n");
3242 goto err_sprite;
3243 }
3244
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003245 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003246 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003247 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003248 }
3249
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003250 /* A this point we have some idea of whether or not we have a working
3251 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003252 if (!b->cursors_are_broken)
3253 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003254
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003255 path = NULL;
3256
Giulio Camuffo954f1832014-10-11 18:27:30 +03003257 loop = wl_display_get_event_loop(compositor->wl_display);
3258 b->drm_source =
3259 wl_event_loop_add_fd(loop, b->drm.fd,
3260 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003261
Giulio Camuffo954f1832014-10-11 18:27:30 +03003262 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3263 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003264 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003265 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003266 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003267 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003268 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003269 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003270 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003271 udev_monitor_get_fd(b->udev_monitor),
3272 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003273
Giulio Camuffo954f1832014-10-11 18:27:30 +03003274 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003275 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003276 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003277 }
3278
Daniel Stonea96b93c2012-06-22 14:04:37 +01003279 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003280
Giulio Camuffo954f1832014-10-11 18:27:30 +03003281 weston_compositor_add_debug_binding(compositor, KEY_O,
3282 planes_binding, b);
3283 weston_compositor_add_debug_binding(compositor, KEY_C,
3284 planes_binding, b);
3285 weston_compositor_add_debug_binding(compositor, KEY_V,
3286 planes_binding, b);
3287 weston_compositor_add_debug_binding(compositor, KEY_Q,
3288 recorder_binding, b);
3289 weston_compositor_add_debug_binding(compositor, KEY_W,
3290 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003291
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003292 if (compositor->renderer->import_dmabuf) {
3293 if (linux_dmabuf_setup(compositor) < 0)
3294 weston_log("Error: initializing dmabuf "
3295 "support failed.\n");
3296 }
3297
Giulio Camuffo954f1832014-10-11 18:27:30 +03003298 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003299
Armin Krezović08368132016-09-30 14:11:05 +02003300 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3301 &api, sizeof(api));
3302
3303 if (ret < 0) {
3304 weston_log("Failed to register output API.\n");
3305 goto err_udev_monitor;
3306 }
3307
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003309
3310err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003311 wl_event_source_remove(b->udev_drm_source);
3312 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003313err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003314 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003315err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003316 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003317err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003318 if (b->gbm)
3319 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003320 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003321err_udev_dev:
3322 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003323err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003324 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003325err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003326 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003327err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003328 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003329 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003330 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003331}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003332
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003333static void
3334config_init_to_defaults(struct weston_drm_backend_config *config)
3335{
3336}
3337
Giulio Camuffo954f1832014-10-11 18:27:30 +03003338WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003339backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003340 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003341{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003342 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003343 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003344
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003345 if (config_base == NULL ||
3346 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3347 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3348 weston_log("drm backend config structure is invalid\n");
3349 return -1;
3350 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003351
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003352 config_init_to_defaults(&config);
3353 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003354
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003355 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003356 if (b == NULL)
3357 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003358
Giulio Camuffo954f1832014-10-11 18:27:30 +03003359 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003360}