blob: 88e65bcd7a110a36cc7dbffcd78f098d916b6085 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040028
Jesse Barnes58ef3792012-02-23 09:45:49 -050029#include <errno.h>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040031#include <stdlib.h>
Richard Hughes2b2092a2013-04-24 14:58:02 +010032#include <ctype.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033#include <string.h>
34#include <fcntl.h>
35#include <unistd.h>
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -040036#include <linux/input.h>
Kristian Høgsberg3f495872013-09-18 23:00:17 -070037#include <linux/vt.h>
Ander Conselvan de Oliveirafd1f4c62012-06-26 17:09:14 +030038#include <assert.h>
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +020039#include <sys/mman.h>
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +030040#include <dlfcn.h>
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030041#include <time.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040042
Benjamin Franzkec649a922011-03-02 11:56:04 +010043#include <xf86drm.h>
44#include <xf86drmMode.h>
Jesse Barnes58ef3792012-02-23 09:45:49 -050045#include <drm_fourcc.h>
Benjamin Franzkec649a922011-03-02 11:56:04 +010046
Benjamin Franzke060cf802011-04-30 09:32:11 +020047#include <gbm.h>
Pekka Paalanen33156972012-08-03 13:30:30 -040048#include <libudev.h>
Benjamin Franzke060cf802011-04-30 09:32:11 +020049
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070050#include "compositor.h"
51#include "compositor-drm.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070052#include "shared/helpers.h"
Mario Kleinerf507ec32015-06-21 21:25:14 +020053#include "shared/timespec-util.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010054#include "gl-renderer.h"
Vincent Abriouc9506672016-10-05 16:14:07 +020055#include "weston-egl-ext.h"
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +020056#include "pixman-renderer.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070057#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100058#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010059#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030060#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020061#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030062#include "linux-dmabuf.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040063
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030064#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
65#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
66#endif
67
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030068#ifndef DRM_CAP_CURSOR_WIDTH
69#define DRM_CAP_CURSOR_WIDTH 0x8
70#endif
71
72#ifndef DRM_CAP_CURSOR_HEIGHT
73#define DRM_CAP_CURSOR_HEIGHT 0x9
74#endif
75
76#ifndef GBM_BO_USE_CURSOR
77#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
78#endif
79
Giulio Camuffo954f1832014-10-11 18:27:30 +030080struct drm_backend {
81 struct weston_backend base;
82 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040083
84 struct udev *udev;
85 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040086
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010087 struct udev_monitor *udev_monitor;
88 struct wl_event_source *udev_drm_source;
89
Benjamin Franzke2af7f102011-03-02 11:14:59 +010090 struct {
David Herrmannd7488c22012-03-11 20:05:21 +010091 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010092 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030093 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +010094 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +020095 struct gbm_device *gbm;
Marty Jack13d9db22011-02-09 19:01:42 -050096 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010097 uint32_t connector_allocator;
Kristian Høgsberg61741a22013-09-17 16:02:57 -070098 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +010099 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200100
Rob Clark4339add2012-08-09 14:18:28 -0500101 /* we need these parameters in order to not fail drmModeAddFB2()
102 * due to out of bounds dimensions, and then mistakenly set
103 * sprites_are_broken:
104 */
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200105 uint32_t min_width, max_width;
106 uint32_t min_height, max_height;
107 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500108
Jesse Barnes58ef3792012-02-23 09:45:49 -0500109 struct wl_list sprite_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500110 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200111 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500112
Rob Clarkab5b1e32012-08-09 13:24:45 -0500113 int cursors_are_broken;
114
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200115 int use_pixman;
116
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200117 uint32_t prev_state;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300118
Rob Bradfordd355b802013-05-31 18:09:55 +0100119 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300120
Daniel Stone70d337d2015-06-16 18:42:23 +0100121 int32_t cursor_width;
122 int32_t cursor_height;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700123
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -0700124 bool use_current_mode;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400125};
126
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400127struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500128 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400129 drmModeModeInfo mode_info;
130};
131
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300132struct drm_fb {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200133 uint32_t fb_id, stride, handle, size;
134 int fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300135 int is_client_buffer;
Pekka Paalanende685b82012-12-04 15:58:12 +0200136 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200137
138 /* Used by gbm fbs */
139 struct gbm_bo *bo;
140
141 /* Used by dumb fbs */
142 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300143};
144
Richard Hughes2b2092a2013-04-24 14:58:02 +0100145struct drm_edid {
146 char eisa_id[13];
147 char monitor_name[13];
148 char pnp_id[5];
149 char serial_number[13];
150};
151
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400152struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200153 struct weston_output base;
154 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400155
Daniel Stone17339232015-11-28 12:09:47 +0000156 uint32_t crtc_id; /* object ID to pass to DRM functions */
157 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400158 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700159 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100160 struct drm_edid edid;
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +0300161 drmModePropertyPtr dpms_prop;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100162 uint32_t gbm_format;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200163
Daniel Stone36609c72015-06-18 07:49:02 +0100164 enum dpms_enum dpms;
165
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300166 int vblank_pending;
167 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800168 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200169 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300170
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100171 struct gbm_surface *gbm_surface;
172 struct gbm_bo *gbm_cursor_bo[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400173 struct weston_plane cursor_plane;
174 struct weston_plane fb_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500175 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400176 int current_cursor;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300177 struct drm_fb *current, *next;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200178 struct backlight *backlight;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200179
180 struct drm_fb *dumb[2];
181 pixman_image_t *image[2];
182 int current_image;
183 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300184
185 struct vaapi_recorder *recorder;
186 struct wl_listener recorder_frame_listener;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400187};
188
Jesse Barnes58ef3792012-02-23 09:45:49 -0500189/*
190 * An output has a primary display plane plus zero or more sprites for
191 * blending display contents.
192 */
193struct drm_sprite {
194 struct wl_list link;
195
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400196 struct weston_plane plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500197
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200198 struct drm_fb *current, *next;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300199 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300200 struct drm_backend *backend;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500201
Jesse Barnes58ef3792012-02-23 09:45:49 -0500202 uint32_t possible_crtcs;
203 uint32_t plane_id;
204 uint32_t count_formats;
205
206 int32_t src_x, src_y;
207 uint32_t src_w, src_h;
208 uint32_t dest_x, dest_y;
209 uint32_t dest_w, dest_h;
210
211 uint32_t formats[];
212};
213
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300214static struct gl_renderer_interface *gl_renderer;
215
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500216static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400217
Armin Krezović545dba62016-08-05 15:54:18 +0200218static inline struct drm_output *
219to_drm_output(struct weston_output *base)
220{
221 return container_of(base, struct drm_output, base);
222}
223
224static inline struct drm_backend *
225to_drm_backend(struct weston_compositor *base)
226{
227 return container_of(base->backend, struct drm_backend, base);
228}
229
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400230static void
231drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400232
Mario Kleinerf507ec32015-06-21 21:25:14 +0200233static void
234drm_output_update_msc(struct drm_output *output, unsigned int seq);
235
Jesse Barnes58ef3792012-02-23 09:45:49 -0500236static int
Daniel Stonea3ae4762015-11-28 12:07:36 +0000237drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500238{
Daniel Stonea3ae4762015-11-28 12:07:36 +0000239 return !!(sprite->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500240}
241
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300242static void
243drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
244{
245 struct drm_fb *fb = data;
246 struct gbm_device *gbm = gbm_bo_get_device(bo);
247
248 if (fb->fb_id)
249 drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
250
Pekka Paalanende685b82012-12-04 15:58:12 +0200251 weston_buffer_reference(&fb->buffer_ref, NULL);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300252
253 free(data);
254}
255
256static struct drm_fb *
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300257drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
258 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200259{
260 struct drm_fb *fb;
261 int ret;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300262 uint32_t bpp, depth;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200263
264 struct drm_mode_create_dumb create_arg;
265 struct drm_mode_destroy_dumb destroy_arg;
266 struct drm_mode_map_dumb map_arg;
267
Peter Huttererf3d62272013-08-08 11:57:05 +1000268 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200269 if (!fb)
270 return NULL;
271
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300272 switch (format) {
273 case GBM_FORMAT_XRGB8888:
274 bpp = 32;
275 depth = 24;
276 break;
277 case GBM_FORMAT_RGB565:
278 bpp = depth = 16;
279 break;
280 default:
281 return NULL;
282 }
283
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700284 memset(&create_arg, 0, sizeof create_arg);
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300285 create_arg.bpp = bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200286 create_arg.width = width;
287 create_arg.height = height;
288
Giulio Camuffo954f1832014-10-11 18:27:30 +0300289 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200290 if (ret)
291 goto err_fb;
292
293 fb->handle = create_arg.handle;
294 fb->stride = create_arg.pitch;
295 fb->size = create_arg.size;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300296 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200297
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300298 ret = -1;
299
300 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700301 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300302
303 handles[0] = fb->handle;
304 pitches[0] = fb->stride;
305 offsets[0] = 0;
306
307 ret = drmModeAddFB2(b->drm.fd, width, height,
308 format, handles, pitches, offsets,
309 &fb->fb_id, 0);
310 if (ret) {
311 weston_log("addfb2 failed: %m\n");
312 b->no_addfb2 = 1;
313 }
314 }
315
316 if (ret) {
317 ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
318 fb->stride, fb->handle, &fb->fb_id);
319 }
320
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200321 if (ret)
322 goto err_bo;
323
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700324 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200325 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400326 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200327 if (ret)
328 goto err_add_fb;
329
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500330 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300331 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200332 if (fb->map == MAP_FAILED)
333 goto err_add_fb;
334
335 return fb;
336
337err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300338 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200339err_bo:
340 memset(&destroy_arg, 0, sizeof(destroy_arg));
341 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300342 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200343err_fb:
344 free(fb);
345 return NULL;
346}
347
348static void
349drm_fb_destroy_dumb(struct drm_fb *fb)
350{
351 struct drm_mode_destroy_dumb destroy_arg;
352
353 if (!fb->map)
354 return;
355
356 if (fb->fb_id)
357 drmModeRmFB(fb->fd, fb->fb_id);
358
359 weston_buffer_reference(&fb->buffer_ref, NULL);
360
361 munmap(fb->map, fb->size);
362
363 memset(&destroy_arg, 0, sizeof(destroy_arg));
364 destroy_arg.handle = fb->handle;
365 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
366
367 free(fb);
368}
369
370static struct drm_fb *
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500371drm_fb_get_from_bo(struct gbm_bo *bo,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300372 struct drm_backend *backend, uint32_t format)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300373{
374 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200375 uint32_t width, height;
Derek Foreman482ffdf2016-07-08 12:50:57 -0500376 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300377 int ret;
378
379 if (fb)
380 return fb;
381
Bryce Harringtonde16d892014-11-20 22:21:57 -0800382 fb = zalloc(sizeof *fb);
383 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200384 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300385
386 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300387
388 width = gbm_bo_get_width(bo);
389 height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200390 fb->stride = gbm_bo_get_stride(bo);
391 fb->handle = gbm_bo_get_handle(bo).u32;
392 fb->size = fb->stride * height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300393 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300394
Giulio Camuffo954f1832014-10-11 18:27:30 +0300395 if (backend->min_width > width || width > backend->max_width ||
396 backend->min_height > height ||
397 height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200398 weston_log("bo geometry out of bounds\n");
399 goto err_free;
400 }
401
402 ret = -1;
403
Giulio Camuffo954f1832014-10-11 18:27:30 +0300404 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200405 handles[0] = fb->handle;
406 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200407 offsets[0] = 0;
408
Giulio Camuffo954f1832014-10-11 18:27:30 +0300409 ret = drmModeAddFB2(backend->drm.fd, width, height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200410 format, handles, pitches, offsets,
411 &fb->fb_id, 0);
412 if (ret) {
413 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300414 backend->no_addfb2 = 1;
415 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200416 }
417 }
418
419 if (ret)
Giulio Camuffo954f1832014-10-11 18:27:30 +0300420 ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200421 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200422
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300423 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200424 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200425 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300426 }
427
428 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
429
430 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200431
432err_free:
433 free(fb);
434 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300435}
436
437static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500438drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200439{
Pekka Paalanende685b82012-12-04 15:58:12 +0200440 assert(fb->buffer_ref.buffer == NULL);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200441
442 fb->is_client_buffer = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200443
Pekka Paalanende685b82012-12-04 15:58:12 +0200444 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200445}
446
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200447static void
448drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
449{
450 if (!fb)
451 return;
452
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200453 if (fb->map &&
454 (fb != output->dumb[0] && fb != output->dumb[1])) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200455 drm_fb_destroy_dumb(fb);
456 } else if (fb->bo) {
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200457 if (fb->is_client_buffer)
458 gbm_bo_destroy(fb->bo);
459 else
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100460 gbm_surface_release_buffer(output->gbm_surface,
Jason Ekstrand3ec57f52013-11-14 20:52:35 -0600461 fb->bo);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200462 }
463}
464
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500465static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200466drm_output_check_scanout_format(struct drm_output *output,
467 struct weston_surface *es, struct gbm_bo *bo)
468{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200469 uint32_t format;
470 pixman_region32_t r;
471
472 format = gbm_bo_get_format(bo);
473
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700474 if (format == GBM_FORMAT_ARGB8888) {
475 /* We can scanout an ARGB buffer if the surface's
476 * opaque region covers the whole output, but we have
477 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800478 pixman_region32_init_rect(&r, 0, 0,
479 output->base.width,
480 output->base.height);
481 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200482
483 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500484 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200485
486 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500487 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700488
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100489 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700490 return format;
491
492 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200493}
494
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400495static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200496drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500498{
Armin Krezović545dba62016-08-05 15:54:18 +0200499 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200501 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300502 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500503 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500504
Jason Ekstranda7af7042013-10-12 22:38:11 -0500505 if (ev->geometry.x != output->base.x ||
506 ev->geometry.y != output->base.y ||
Giulio Camuffo954f1832014-10-11 18:27:30 +0300507 buffer == NULL || b->gbm == NULL ||
Hardeningff39efa2013-09-18 23:56:35 +0200508 buffer->width != output->base.current_mode->width ||
509 buffer->height != output->base.current_mode->height ||
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200510 output->base.transform != viewport->buffer.transform ||
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 ev->transform.enabled)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400512 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500513
Pekka Paalanen5580f222015-02-17 16:33:18 +0200514 if (ev->geometry.scissor_enabled)
515 return NULL;
516
Giulio Camuffo954f1832014-10-11 18:27:30 +0300517 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -0700518 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500519
Rob Bradford9b101872012-09-14 23:25:41 +0100520 /* Unable to use the buffer for scanout */
521 if (!bo)
522 return NULL;
523
Jason Ekstranda7af7042013-10-12 22:38:11 -0500524 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500525 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300526 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400527 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +0300528 }
529
Giulio Camuffo954f1832014-10-11 18:27:30 +0300530 output->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300531 if (!output->next) {
532 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400533 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300534 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500535
Pekka Paalanende685b82012-12-04 15:58:12 +0200536 drm_fb_set_buffer(output->next, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500537
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400538 return &output->fb_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500539}
540
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500541static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200542drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400543{
Armin Krezović545dba62016-08-05 15:54:18 +0200544 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300545 struct gbm_bo *bo;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400546
Giulio Camuffo954f1832014-10-11 18:27:30 +0300547 output->base.compositor->renderer->repaint_output(&output->base,
548 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400549
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100550 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300551 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +0200552 weston_log("failed to lock front buffer: %m\n");
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400553 return;
554 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300555
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100556 output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300557 if (!output->next) {
Martin Minarik6d118362012-06-07 18:01:59 +0200558 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100559 gbm_surface_release_buffer(output->gbm_surface, bo);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300560 return;
561 }
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400562}
563
564static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200565drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
566{
567 struct weston_compositor *ec = output->base.compositor;
568 pixman_region32_t total_damage, previous_damage;
569
570 pixman_region32_init(&total_damage);
571 pixman_region32_init(&previous_damage);
572
573 pixman_region32_copy(&previous_damage, damage);
574
575 pixman_region32_union(&total_damage, damage, &output->previous_damage);
576 pixman_region32_copy(&output->previous_damage, &previous_damage);
577
578 output->current_image ^= 1;
579
580 output->next = output->dumb[output->current_image];
581 pixman_renderer_output_set_buffer(&output->base,
582 output->image[output->current_image]);
583
584 ec->renderer->repaint_output(&output->base, &total_damage);
585
586 pixman_region32_fini(&total_damage);
587 pixman_region32_fini(&previous_damage);
588}
589
590static void
591drm_output_render(struct drm_output *output, pixman_region32_t *damage)
592{
Giulio Camuffo954f1832014-10-11 18:27:30 +0300593 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200594 struct drm_backend *b = to_drm_backend(c);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200595
Giulio Camuffo954f1832014-10-11 18:27:30 +0300596 if (b->use_pixman)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200597 drm_output_render_pixman(output, damage);
598 else
599 drm_output_render_gl(output, damage);
600
Giulio Camuffo954f1832014-10-11 18:27:30 +0300601 pixman_region32_subtract(&c->primary_plane.damage,
602 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200603}
604
605static void
Richard Hughese7299962013-05-01 21:52:12 +0100606drm_output_set_gamma(struct weston_output *output_base,
607 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
608{
609 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +0200610 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300611 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200612 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +0100613
614 /* check */
615 if (output_base->gamma_size != size)
616 return;
617 if (!output->original_crtc)
618 return;
619
Giulio Camuffo954f1832014-10-11 18:27:30 +0300620 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +0100621 output->crtc_id,
622 size, r, g, b);
623 if (rc)
624 weston_log("set gamma failed: %m\n");
625}
626
Bryce Harringtonada4f072015-06-30 13:25:46 -0700627/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200628 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700629 * The pipe parameter indicates which CRTC is in use. Knowing this, we
630 * can determine which vblank sequence type to use for it. Traditional
631 * cards had only two CRTCs, with CRTC 0 using no special flags, and
632 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
633 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200634 *
Bryce Harringtonada4f072015-06-30 13:25:46 -0700635 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
636 * 0-31. If this is non-zero it indicates we're dealing with a
637 * multi-gpu situation and we need to calculate the vblank sync
638 * using DRM_BLANK_HIGH_CRTC_MASK.
639 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +0300640static unsigned int
641drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200642{
643 if (output->pipe > 1)
644 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
645 DRM_VBLANK_HIGH_CRTC_MASK;
646 else if (output->pipe > 0)
647 return DRM_VBLANK_SECONDARY;
648 else
649 return 0;
650}
651
David Herrmann1edf44c2013-10-22 17:11:26 +0200652static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500653drm_output_repaint(struct weston_output *output_base,
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400654 pixman_region32_t *damage)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100655{
Armin Krezović545dba62016-08-05 15:54:18 +0200656 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +0300657 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +0200658 to_drm_backend(output->base.compositor);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500659 struct drm_sprite *s;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400660 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500661 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100662
Armin Krezović08368132016-09-30 14:11:05 +0200663 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +0200664 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800665
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300666 if (!output->next)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400667 drm_output_render(output, damage);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300668 if (!output->next)
David Herrmann1edf44c2013-10-22 17:11:26 +0200669 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100670
Hardeningff39efa2013-09-18 23:56:35 +0200671 mode = container_of(output->base.current_mode, struct drm_mode, base);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +0200672 if (!output->current ||
673 output->current->stride != output->next->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +0300674 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300675 output->next->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400676 &output->connector_id, 1,
677 &mode->mode_info);
678 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200679 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200680 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400681 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +0300682 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200683 }
684
Giulio Camuffo954f1832014-10-11 18:27:30 +0300685 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300686 output->next->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500687 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200688 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +0200689 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -0500690 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100691
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300692 output->page_flip_pending = 1;
693
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400694 drm_output_set_cursor(output);
695
Jesse Barnes58ef3792012-02-23 09:45:49 -0500696 /*
697 * Now, update all the sprite surfaces
698 */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300699 wl_list_for_each(s, &backend->sprite_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200700 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500701 drmVBlank vbl = {
702 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
703 .request.sequence = 1,
704 };
705
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200706 if ((!s->current && !s->next) ||
Daniel Stonea3ae4762015-11-28 12:07:36 +0000707 !drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500708 continue;
709
Giulio Camuffo954f1832014-10-11 18:27:30 +0300710 if (s->next && !backend->sprites_hidden)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200711 fb_id = s->next->fb_id;
712
Giulio Camuffo954f1832014-10-11 18:27:30 +0300713 ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200714 output->crtc_id, fb_id, flags,
Jesse Barnes58ef3792012-02-23 09:45:49 -0500715 s->dest_x, s->dest_y,
716 s->dest_w, s->dest_h,
717 s->src_x, s->src_y,
718 s->src_w, s->src_h);
719 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +0200720 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500721 ret, strerror(errno));
722
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +0200723 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -0500724
Jesse Barnes58ef3792012-02-23 09:45:49 -0500725 /*
726 * Queue a vblank signal so we know when the surface
727 * becomes active on the display or has been replaced.
728 */
729 vbl.request.signal = (unsigned long)s;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300730 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500731 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200732 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -0500733 ret, strerror(errno));
734 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300735
736 s->output = output;
737 output->vblank_pending = 1;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500738 }
739
David Herrmann1edf44c2013-10-22 17:11:26 +0200740 return 0;
741
742err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -0800743 output->cursor_view = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +0200744 if (output->next) {
745 drm_output_release_fb(output, output->next);
746 output->next = NULL;
747 }
748
749 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400750}
751
752static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200753drm_output_start_repaint_loop(struct weston_output *output_base)
754{
Armin Krezović545dba62016-08-05 15:54:18 +0200755 struct drm_output *output = to_drm_output(output_base);
756 struct drm_backend *backend =
757 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200758 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +0200759 struct timespec ts, tnow;
760 struct timespec vbl2now;
761 int64_t refresh_nsec;
762 int ret;
763 drmVBlank vbl = {
764 .request.type = DRM_VBLANK_RELATIVE,
765 .request.sequence = 0,
766 .request.signal = 0,
767 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300768
Armin Krezović08368132016-09-30 14:11:05 +0200769 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +0800770 return;
771
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300772 if (!output->current) {
773 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +0200774 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300775 }
776
Mario Kleinerf507ec32015-06-21 21:25:14 +0200777 /* Try to get current msc and timestamp via instant query */
778 vbl.request.type |= drm_waitvblank_pipe(output);
779 ret = drmWaitVBlank(backend->drm.fd, &vbl);
780
781 /* Error ret or zero timestamp means failure to get valid timestamp */
782 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
783 ts.tv_sec = vbl.reply.tval_sec;
784 ts.tv_nsec = vbl.reply.tval_usec * 1000;
785
786 /* Valid timestamp for most recent vblank - not stale?
787 * Stale ts could happen on Linux 3.17+, so make sure it
788 * is not older than 1 refresh duration since now.
789 */
790 weston_compositor_read_presentation_clock(backend->compositor,
791 &tnow);
792 timespec_sub(&vbl2now, &tnow, &ts);
793 refresh_nsec =
794 millihz_to_nsec(output->base.current_mode->refresh);
795 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
796 drm_output_update_msc(output, vbl.reply.sequence);
797 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200798 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +0200799 return;
800 }
801 }
802
803 /* Immediate query didn't provide valid timestamp.
804 * Use pageflip fallback.
805 */
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +0300806 fb_id = output->current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200807
Giulio Camuffo954f1832014-10-11 18:27:30 +0300808 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +0200809 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
810 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +0200811 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200812 }
David Herrmann3c688c52013-10-22 17:11:25 +0200813
814 return;
815
816finish_frame:
817 /* if we cannot page-flip, immediately finish frame */
Giulio Camuffo954f1832014-10-11 18:27:30 +0300818 weston_compositor_read_presentation_clock(output_base->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200819 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200820 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200821}
822
823static void
Pekka Paalanen641307c2014-09-23 22:08:47 -0400824drm_output_update_msc(struct drm_output *output, unsigned int seq)
825{
826 uint64_t msc_hi = output->base.msc >> 32;
827
828 if (seq < (output->base.msc & 0xffffffff))
829 msc_hi++;
830
831 output->base.msc = (msc_hi << 32) + seq;
832}
833
834static void
Jesse Barnes58ef3792012-02-23 09:45:49 -0500835vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
836 void *data)
837{
838 struct drm_sprite *s = (struct drm_sprite *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300839 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400840 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200841 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
842 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300843
Pekka Paalanen641307c2014-09-23 22:08:47 -0400844 drm_output_update_msc(output, frame);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300845 output->vblank_pending = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500846
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200847 drm_output_release_fb(output, s->current);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200848 s->current = s->next;
849 s->next = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300850
851 if (!output->page_flip_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400852 ts.tv_sec = sec;
853 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200854 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300855 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500856}
857
858static void
Armin Krezović08368132016-09-30 14:11:05 +0200859drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800860
861static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400862page_flip_handler(int fd, unsigned int frame,
863 unsigned int sec, unsigned int usec, void *data)
864{
Armin Krezović545dba62016-08-05 15:54:18 +0200865 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400866 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200867 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
868 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
869 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400870
Pekka Paalanen641307c2014-09-23 22:08:47 -0400871 drm_output_update_msc(output, frame);
872
Jonas Ådahle5a12252013-04-05 23:07:11 +0200873 /* We don't set page_flip_pending on start_repaint_loop, in that case
874 * we just want to page flip to the current buffer to get an accurate
875 * timestamp */
876 if (output->page_flip_pending) {
877 drm_output_release_fb(output, output->current);
878 output->current = output->next;
879 output->next = NULL;
880 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300881
Jonas Ådahle5a12252013-04-05 23:07:11 +0200882 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -0400883
Xiong Zhangabd5d472013-10-11 14:43:07 +0800884 if (output->destroy_pending)
885 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +0200886 else if (output->disable_pending)
887 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +0800888 else if (!output->vblank_pending) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400889 ts.tv_sec = sec;
890 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200891 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300892
893 /* We can't call this from frame_notify, because the output's
894 * repaint needed flag is cleared just after that */
895 if (output->recorder)
896 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300897 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200898}
899
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500900static uint32_t
901drm_output_check_sprite_format(struct drm_sprite *s,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500903{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500904 uint32_t i, format;
905
906 format = gbm_bo_get_format(bo);
907
908 if (format == GBM_FORMAT_ARGB8888) {
909 pixman_region32_t r;
910
Kristian Høgsberg63093a32013-03-01 14:29:16 -0500911 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600912 ev->surface->width,
913 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500915
916 if (!pixman_region32_not_empty(&r))
917 format = GBM_FORMAT_XRGB8888;
918
919 pixman_region32_fini(&r);
920 }
Jesse Barnes58ef3792012-02-23 09:45:49 -0500921
922 for (i = 0; i < s->count_formats; i++)
923 if (s->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500924 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500925
926 return 0;
927}
928
929static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930drm_view_transform_supported(struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500931{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 return !ev->transform.enabled ||
933 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
Jesse Barnes58ef3792012-02-23 09:45:49 -0500934}
935
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200937drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500939{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200940 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +0200941 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200942 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300943 struct wl_resource *buffer_resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500944 struct drm_sprite *s;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300945 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500946 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500947 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500948 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200949 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500950 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -0400951 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500952
Giulio Camuffo954f1832014-10-11 18:27:30 +0300953 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200954 return NULL;
955
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200956 if (viewport->buffer.transform != output->base.transform)
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +0200957 return NULL;
958
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200959 if (viewport->buffer.scale != output->base.current_scale)
Alexander Larssond9a7bb72013-05-22 14:41:39 +0200960 return NULL;
961
Giulio Camuffo954f1832014-10-11 18:27:30 +0300962 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400963 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500964
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200965 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400966 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +0300967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400969 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300970 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 if (ev->alpha != 1.0f)
Ville Syrjälä5a84f312012-11-16 11:48:46 +0200973 return NULL;
974
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300975 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -0500976 return NULL;
977
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400979 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500980
Giulio Camuffo954f1832014-10-11 18:27:30 +0300981 wl_list_for_each(s, &b->sprite_list, link) {
Daniel Stonea3ae4762015-11-28 12:07:36 +0000982 if (!drm_sprite_crtc_supported(output, s))
Jesse Barnes58ef3792012-02-23 09:45:49 -0500983 continue;
984
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200985 if (!s->next) {
Jesse Barnes58ef3792012-02-23 09:45:49 -0500986 found = 1;
987 break;
988 }
989 }
990
991 /* No sprites available */
992 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400993 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500994
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300995 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -0700996#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +0300997 /* XXX: TODO:
998 *
999 * Use AddFB2 directly, do not go via GBM.
1000 * Add support for multiplanar formats.
1001 * Both require refactoring in the DRM-backend to
1002 * support a mix of gbm_bos and drmfbs.
1003 */
1004 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001005 .fd = dmabuf->attributes.fd[0],
1006 .width = dmabuf->attributes.width,
1007 .height = dmabuf->attributes.height,
1008 .stride = dmabuf->attributes.stride[0],
1009 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001010 };
1011
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001012 if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001013 return NULL;
1014
1015 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1016 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001017#else
1018 return NULL;
1019#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001020 } else {
1021 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1022 buffer_resource, GBM_BO_USE_SCANOUT);
1023 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001024 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001025 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 format = drm_output_check_sprite_format(s, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001028 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001029 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001030 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001031 }
1032
Giulio Camuffo954f1832014-10-11 18:27:30 +03001033 s->next = drm_fb_get_from_bo(bo, b, format);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001034 if (!s->next) {
1035 gbm_bo_destroy(bo);
1036 return NULL;
1037 }
1038
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001040
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 box = pixman_region32_extents(&ev->transform.boundingbox);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001042 s->plane.x = box->x1;
1043 s->plane.y = box->y1;
1044
Jesse Barnes58ef3792012-02-23 09:45:49 -05001045 /*
1046 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001047 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001048 * for us already).
1049 */
1050 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001052 &output->base.region);
1053 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001054 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001055 tbox = weston_transformed_rect(output->base.width,
1056 output->base.height,
1057 output->base.transform,
1058 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001059 *box);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001060 s->dest_x = tbox.x1;
1061 s->dest_y = tbox.y1;
1062 s->dest_w = tbox.x2 - tbox.x1;
1063 s->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001064 pixman_region32_fini(&dest_rect);
1065
1066 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001068 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001069 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001070
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 weston_view_from_global_fixed(ev,
1072 wl_fixed_from_int(box->x1),
1073 wl_fixed_from_int(box->y1),
1074 &sx1, &sy1);
1075 weston_view_from_global_fixed(ev,
1076 wl_fixed_from_int(box->x2),
1077 wl_fixed_from_int(box->y2),
1078 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001079
1080 if (sx1 < 0)
1081 sx1 = 0;
1082 if (sy1 < 0)
1083 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001084 if (sx2 > wl_fixed_from_int(ev->surface->width))
1085 sx2 = wl_fixed_from_int(ev->surface->width);
1086 if (sy2 > wl_fixed_from_int(ev->surface->height))
1087 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001088
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001089 tbox.x1 = sx1;
1090 tbox.y1 = sy1;
1091 tbox.x2 = sx2;
1092 tbox.y2 = sy2;
1093
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001094 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1095 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001096 viewport->buffer.transform,
1097 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001098 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001099
1100 s->src_x = tbox.x1 << 8;
1101 s->src_y = tbox.y1 << 8;
1102 s->src_w = (tbox.x2 - tbox.x1) << 8;
1103 s->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001104 pixman_region32_fini(&src_rect);
1105
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001106 return &s->plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001107}
1108
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001109static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001110drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001112{
Armin Krezović545dba62016-08-05 15:54:18 +02001113 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001114 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001115 struct wl_shm_buffer *shmbuf;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001116
Derek Foremanbe428b32015-11-24 11:39:38 -06001117 if (ev->transform.enabled &&
1118 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1119 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001120 if (b->gbm == NULL)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001121 return NULL;
Ander Conselvan de Oliveira2908a3d2012-11-27 17:03:43 +02001122 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1123 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001124 if (viewport->buffer.scale != output->base.current_scale)
Neil Robertsf37f82c2014-05-01 18:00:41 +01001125 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 if (output->cursor_view)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001127 return NULL;
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001128 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001129 return NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001130 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001131 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001132 if (ev->geometry.scissor_enabled)
1133 return NULL;
Derek Foreman6c19b692015-12-03 14:07:12 -06001134 if (ev->surface->buffer_ref.buffer == NULL)
1135 return NULL;
1136 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1137 if (!shmbuf)
1138 return NULL;
1139 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1140 return NULL;
1141 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001142 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001143 return NULL;
1144
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 output->cursor_view = ev;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001146
1147 return &output->cursor_plane;
1148}
1149
Pekka Paalanend0ead482014-06-16 12:05:40 +03001150/**
1151 * Update the image for the current cursor surface
1152 *
1153 * @param b DRM backend structure
1154 * @param bo GBM buffer object to write into
1155 * @param ev View to use for cursor image
1156 */
1157static void
1158cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1159 struct weston_view *ev)
1160{
1161 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1162 uint32_t buf[b->cursor_width * b->cursor_height];
1163 int32_t stride;
1164 uint8_t *s;
1165 int i;
1166
1167 assert(buffer && buffer->shm_buffer);
1168 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1169 assert(ev->surface->width <= b->cursor_width);
1170 assert(ev->surface->height <= b->cursor_height);
1171
1172 memset(buf, 0, sizeof buf);
1173 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1174 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1175
1176 wl_shm_buffer_begin_access(buffer->shm_buffer);
1177 for (i = 0; i < ev->surface->height; i++)
1178 memcpy(buf + i * b->cursor_width,
1179 s + i * stride,
1180 ev->surface->width * 4);
1181 wl_shm_buffer_end_access(buffer->shm_buffer);
1182
1183 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1184 weston_log("failed update cursor: %m\n");
1185}
1186
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001187static void
1188drm_output_set_cursor(struct drm_output *output)
1189{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 struct weston_view *ev = output->cursor_view;
Neil Robertse5051712013-11-13 15:44:06 +00001191 struct weston_buffer *buffer;
Armin Krezović545dba62016-08-05 15:54:18 +02001192 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001193 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001194 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001195 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001196
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 output->cursor_view = NULL;
1198 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001199 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Derek Foremanb0427562016-02-05 15:55:20 -06001200 output->cursor_plane.x = INT32_MIN;
1201 output->cursor_plane.y = INT32_MIN;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001202 return;
1203 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001204
Neil Robertse5051712013-11-13 15:44:06 +00001205 buffer = ev->surface->buffer_ref.buffer;
1206
1207 if (buffer &&
Pekka Paalanende685b82012-12-04 15:58:12 +02001208 pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001209 pixman_region32_fini(&output->cursor_plane.damage);
1210 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001211 output->current_cursor ^= 1;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001212 bo = output->gbm_cursor_bo[output->current_cursor];
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001213
Pekka Paalanend0ead482014-06-16 12:05:40 +03001214 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001215 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001216 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1217 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001218 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001219 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001220 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001221 }
1222
Derek Foremanbe428b32015-11-24 11:39:38 -06001223 weston_view_to_global_float(ev, 0, 0, &x, &y);
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001224
1225 /* From global to output space, output transform is guaranteed to be
1226 * NORMAL by drm_output_prepare_cursor_view().
1227 */
1228 x = (x - output->base.x) * output->base.current_scale;
1229 y = (y - output->base.y) * output->base.current_scale;
1230
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001231 if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001232 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
Kristian Høgsberg24e42752012-07-18 12:08:37 -04001233 weston_log("failed to move cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001234 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001235 }
1236
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001237 output->cursor_plane.x = x;
1238 output->cursor_plane.y = y;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001239 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001240}
1241
Jesse Barnes58ef3792012-02-23 09:45:49 -05001242static void
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001243drm_assign_planes(struct weston_output *output_base)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001244{
Armin Krezović545dba62016-08-05 15:54:18 +02001245 struct drm_backend *b = to_drm_backend(output_base->compositor);
1246 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001248 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001249 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001250
1251 /*
1252 * Find a surface for each sprite in the output using some heuristics:
1253 * 1) size
1254 * 2) frequency of update
1255 * 3) opacity (though some hw might support alpha blending)
1256 * 4) clipping (this can be fixed with color keys)
1257 *
1258 * The idea is to save on blitting since this should save power.
1259 * If we can get a large video surface on the sprite for example,
1260 * the main display surface may not need to update at all, and
1261 * the client buffer can be used directly for the sprite surface
1262 * as we do for flipping full screen surfaces.
1263 */
1264 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001265 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266
Giulio Camuffo954f1832014-10-11 18:27:30 +03001267 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001268 struct weston_surface *es = ev->surface;
1269
1270 /* Test whether this buffer can ever go into a plane:
1271 * non-shm, or small enough to be a cursor.
1272 *
1273 * Also, keep a reference when using the pixman renderer.
1274 * That makes it possible to do a seamless switch to the GL
1275 * renderer and since the pixman renderer keeps a reference
1276 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001277 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001278 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001279 (es->buffer_ref.buffer &&
1280 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001281 (ev->surface->width <= b->cursor_width &&
1282 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001283 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001284 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001285 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001286
Jesse Barnes58ef3792012-02-23 09:45:49 -05001287 pixman_region32_init(&surface_overlap);
1288 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001290
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001291 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001292 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001293 next_plane = primary;
1294 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001296 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001298 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001300 if (next_plane == NULL)
1301 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001302
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001304
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001305 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001306 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001308
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001309 if (next_plane == primary ||
1310 next_plane == &output->cursor_plane) {
1311 /* cursor plane involves a copy */
1312 ev->psf_flags = 0;
1313 } else {
1314 /* All other planes are a direct scanout of a
1315 * single client buffer.
1316 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001317 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001318 }
1319
Jesse Barnes58ef3792012-02-23 09:45:49 -05001320 pixman_region32_fini(&surface_overlap);
1321 }
1322 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001323}
1324
Matt Roper361d2ad2011-08-29 13:52:23 -07001325static void
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001326drm_output_fini_pixman(struct drm_output *output);
1327
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001328/**
1329 * Find the closest-matching mode for a given target
1330 *
1331 * Given a target mode, find the most suitable mode amongst the output's
1332 * current mode list to use, preferring the current mode if possible, to
1333 * avoid an expensive mode switch.
1334 *
1335 * @param output DRM output
1336 * @param target_mode Mode to attempt to match
1337 * @returns Pointer to a mode from the output's mode list
1338 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001339static struct drm_mode *
1340choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1341{
1342 struct drm_mode *tmp_mode = NULL, *mode;
1343
Hardeningff39efa2013-09-18 23:56:35 +02001344 if (output->base.current_mode->width == target_mode->width &&
1345 output->base.current_mode->height == target_mode->height &&
1346 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001347 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001348 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001349
1350 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1351 if (mode->mode_info.hdisplay == target_mode->width &&
1352 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001353 if (mode->base.refresh == target_mode->refresh ||
1354 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001355 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001356 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001357 tmp_mode = mode;
1358 }
1359 }
1360
1361 return tmp_mode;
1362}
1363
1364static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001365drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001366static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001367drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001368
1369static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001370drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1371{
1372 struct drm_output *output;
1373 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001374 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08001375
1376 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001377 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001378 return -1;
1379 }
1380
1381 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001382 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08001383 return -1;
1384 }
1385
Armin Krezović545dba62016-08-05 15:54:18 +02001386 b = to_drm_backend(output_base->compositor);
1387 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08001388 drm_mode = choose_mode (output, mode);
1389
1390 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02001391 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08001392 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001393 }
1394
Hardeningff39efa2013-09-18 23:56:35 +02001395 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001396 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001397
Hardeningff39efa2013-09-18 23:56:35 +02001398 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001399
Hardeningff39efa2013-09-18 23:56:35 +02001400 output->base.current_mode = &drm_mode->base;
1401 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08001402 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
1403
Alex Wub7b8bda2012-04-17 17:20:48 +08001404 /* reset rendering stuff. */
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02001405 drm_output_release_fb(output, output->current);
1406 drm_output_release_fb(output, output->next);
1407 output->current = output->next = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08001408
Giulio Camuffo954f1832014-10-11 18:27:30 +03001409 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001410 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001411 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001412 weston_log("failed to init output pixman state with "
1413 "new mode\n");
1414 return -1;
1415 }
1416 } else {
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001417 gl_renderer->output_destroy(&output->base);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001418 gbm_surface_destroy(output->gbm_surface);
Alex Wub7b8bda2012-04-17 17:20:48 +08001419
Giulio Camuffo954f1832014-10-11 18:27:30 +03001420 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001421 weston_log("failed to init output egl state with "
1422 "new mode");
1423 return -1;
1424 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001425 }
1426
Alex Wub7b8bda2012-04-17 17:20:48 +08001427 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08001428}
1429
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001430static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001431on_drm_input(int fd, uint32_t mask, void *data)
1432{
1433 drmEventContext evctx;
1434
1435 memset(&evctx, 0, sizeof evctx);
1436 evctx.version = DRM_EVENT_CONTEXT_VERSION;
1437 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001438 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001439 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001440
1441 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001442}
1443
1444static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001445init_drm(struct drm_backend *b, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001446{
Kristian Høgsbergb5ef5912012-03-28 22:53:49 -04001447 const char *filename, *sysnum;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001448 uint64_t cap;
1449 int fd, ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001450 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001451
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001452 sysnum = udev_device_get_sysnum(device);
1453 if (sysnum)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001454 b->drm.id = atoi(sysnum);
1455 if (!sysnum || b->drm.id < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001456 weston_log("cannot get device sysnum\n");
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04001457 return -1;
1458 }
1459
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001460 filename = udev_device_get_devnode(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001461 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001462 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001463 /* Probably permissions error */
Martin Minarik6d118362012-06-07 18:01:59 +02001464 weston_log("couldn't open %s, skipping\n",
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001465 udev_device_get_devnode(device));
1466 return -1;
1467 }
1468
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04001469 weston_log("using %s\n", filename);
1470
Giulio Camuffo954f1832014-10-11 18:27:30 +03001471 b->drm.fd = fd;
1472 b->drm.filename = strdup(filename);
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001473
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001474 ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
1475 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001476 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001477 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001478 clk_id = CLOCK_REALTIME;
1479
Giulio Camuffo954f1832014-10-11 18:27:30 +03001480 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001481 weston_log("Error: failed to set presentation clock %d.\n",
1482 clk_id);
1483 return -1;
1484 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02001485
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001486 ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
1487 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001488 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001489 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001490 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001491
1492 ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
1493 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03001494 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001495 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03001496 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001497
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001498 return 0;
1499}
1500
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001501static struct gbm_device *
1502create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02001503{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001504 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01001505
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001506 gl_renderer = weston_load_module("gl-renderer.so",
1507 "gl_renderer_interface");
1508 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001509 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001510
1511 /* GBM will load a dri driver, but even though they need symbols from
1512 * libglapi, in some version of Mesa they are not linked to it. Since
1513 * only the gl-renderer module links to it, the call above won't make
1514 * these symbols globally available, and loading the DRI driver fails.
1515 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
1516 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
1517
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001518 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001519
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001520 return gbm;
1521}
1522
Bryce Harringtonc056a982015-05-19 15:25:18 -07001523/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07001524 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05001525 *
1526 * This returns 0 if substitution isn't possible, but 0 might be a
1527 * legitimate format for other EGL platforms, so the caller is
1528 * responsible for checking for 0 before calling gl_renderer->create().
1529 *
1530 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
1531 * but it's entirely possible we'll see this again on other implementations.
1532 */
1533static int
1534fallback_format_for(uint32_t format)
1535{
1536 switch (format) {
1537 case GBM_FORMAT_XRGB8888:
1538 return GBM_FORMAT_ARGB8888;
1539 case GBM_FORMAT_XRGB2101010:
1540 return GBM_FORMAT_ARGB2101010;
1541 default:
1542 return 0;
1543 }
1544}
1545
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001546static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001547drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001548{
Derek Foreman6d556372015-11-04 14:47:33 -06001549 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001550 b->gbm_format,
1551 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06001552 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001553 };
Derek Foreman6d556372015-11-04 14:47:33 -06001554 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01001555
Derek Foremanc4cfe852015-05-15 12:12:40 -05001556 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06001557 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001558 if (gl_renderer->display_create(b->compositor,
1559 EGL_PLATFORM_GBM_KHR,
1560 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02001561 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02001562 gl_renderer->opaque_attribs,
1563 format,
1564 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001565 return -1;
1566 }
1567
1568 return 0;
1569}
1570
1571static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001572init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001573{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001574 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001575
Giulio Camuffo954f1832014-10-11 18:27:30 +03001576 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02001577 return -1;
1578
Giulio Camuffo954f1832014-10-11 18:27:30 +03001579 if (drm_backend_create_gl_renderer(b) < 0) {
1580 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001581 return -1;
1582 }
1583
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001584 return 0;
1585}
1586
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001587static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001588init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001589{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001590 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001591}
1592
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001593/**
1594 * Add a mode to output's mode list
1595 *
1596 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
1597 * output's mode list.
1598 *
1599 * @param output DRM output to add mode to
1600 * @param info DRM mode structure to add
1601 * @returns Newly-allocated Weston/DRM mode structure
1602 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001603static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001604drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001605{
1606 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001607 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001608
1609 mode = malloc(sizeof *mode);
1610 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001611 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001612
1613 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02001614 mode->base.width = info->hdisplay;
1615 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04001616
1617 /* Calculate higher precision (mHz) refresh rate */
1618 refresh = (info->clock * 1000000LL / info->htotal +
1619 info->vtotal / 2) / info->vtotal;
1620
1621 if (info->flags & DRM_MODE_FLAG_INTERLACE)
1622 refresh *= 2;
1623 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
1624 refresh /= 2;
1625 if (info->vscan > 1)
1626 refresh /= info->vscan;
1627
1628 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001629 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04001630
1631 if (info->type & DRM_MODE_TYPE_PREFERRED)
1632 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
1633
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001634 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
1635
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03001636 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001637}
1638
1639static int
1640drm_subpixel_to_wayland(int drm_value)
1641{
1642 switch (drm_value) {
1643 default:
1644 case DRM_MODE_SUBPIXEL_UNKNOWN:
1645 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
1646 case DRM_MODE_SUBPIXEL_NONE:
1647 return WL_OUTPUT_SUBPIXEL_NONE;
1648 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
1649 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
1650 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
1651 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
1652 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
1653 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
1654 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
1655 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
1656 }
1657}
1658
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001659/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001660static uint32_t
1661drm_get_backlight(struct drm_output *output)
1662{
1663 long brightness, max_brightness, norm;
1664
1665 brightness = backlight_get_brightness(output->backlight);
1666 max_brightness = backlight_get_max_brightness(output->backlight);
1667
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001668 /* convert it on a scale of 0 to 255 */
1669 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001670
1671 return (uint32_t) norm;
1672}
1673
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001674/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001675static void
1676drm_set_backlight(struct weston_output *output_base, uint32_t value)
1677{
Armin Krezović545dba62016-08-05 15:54:18 +02001678 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001679 long max_brightness, new_brightness;
1680
1681 if (!output->backlight)
1682 return;
1683
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001684 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001685 return;
1686
1687 max_brightness = backlight_get_max_brightness(output->backlight);
1688
1689 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03001690 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001691
1692 backlight_set_brightness(output->backlight, new_brightness);
1693}
1694
1695static drmModePropertyPtr
1696drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
1697{
1698 drmModePropertyPtr props;
1699 int i;
1700
1701 for (i = 0; i < connector->count_props; i++) {
1702 props = drmModeGetProperty(fd, connector->props[i]);
1703 if (!props)
1704 continue;
1705
1706 if (!strcmp(props->name, name))
1707 return props;
1708
1709 drmModeFreeProperty(props);
1710 }
1711
1712 return NULL;
1713}
1714
1715static void
1716drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
1717{
Armin Krezović545dba62016-08-05 15:54:18 +02001718 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001719 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001720 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone36609c72015-06-18 07:49:02 +01001721 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001722
Ander Conselvan de Oliveiraa0a433a2013-06-04 16:24:04 +03001723 if (!output->dpms_prop)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001724 return;
1725
Daniel Stone36609c72015-06-18 07:49:02 +01001726 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
1727 output->dpms_prop->prop_id, level);
1728 if (ret) {
1729 weston_log("DRM: DPMS: failed property set for %s\n",
1730 output->base.name);
1731 return;
1732 }
1733
1734 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001735}
1736
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001737static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001738 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
1739 [DRM_MODE_CONNECTOR_VGA] = "VGA",
1740 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
1741 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
1742 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
1743 [DRM_MODE_CONNECTOR_Composite] = "Composite",
1744 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
1745 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
1746 [DRM_MODE_CONNECTOR_Component] = "Component",
1747 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
1748 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
1749 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
1750 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
1751 [DRM_MODE_CONNECTOR_TV] = "TV",
1752 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001753#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001754 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
1755 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03001756#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04001757};
1758
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001759static char *
1760make_connector_name(const drmModeConnector *con)
1761{
1762 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001763 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001764
1765 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
1766 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03001767
1768 if (!type_name)
1769 type_name = "UNNAMED";
1770
1771 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03001772
1773 return strdup(name);
1774}
1775
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001776static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001777find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001778 drmModeRes *resources, drmModeConnector *connector)
1779{
1780 drmModeEncoder *encoder;
1781 uint32_t possible_crtcs;
1782 int i, j;
1783
1784 for (j = 0; j < connector->count_encoders; j++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001785 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001786 if (encoder == NULL) {
1787 weston_log("Failed to get encoder.\n");
1788 return -1;
1789 }
1790 possible_crtcs = encoder->possible_crtcs;
1791 drmModeFreeEncoder(encoder);
1792
1793 for (i = 0; i < resources->count_crtcs; i++) {
1794 if (possible_crtcs & (1 << i) &&
Giulio Camuffo954f1832014-10-11 18:27:30 +03001795 !(b->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001796 return i;
1797 }
1798 }
1799
1800 return -1;
1801}
1802
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001803/* Init output state that depends on gl or gbm */
1804static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001805drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001806{
Derek Foremanc4cfe852015-05-15 12:12:40 -05001807 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001808 output->gbm_format,
1809 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05001810 };
1811 int i, flags, n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001812
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001813 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02001814 output->base.current_mode->width,
1815 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05001816 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001817 GBM_BO_USE_SCANOUT |
1818 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001819 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001820 weston_log("failed to create gbm surface\n");
1821 return -1;
1822 }
1823
Derek Foremanc4cfe852015-05-15 12:12:40 -05001824 if (format[1])
1825 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02001826 if (gl_renderer->output_window_create(&output->base,
1827 (EGLNativeWindowType)output->gbm_surface,
1828 output->gbm_surface,
1829 gl_renderer->opaque_attribs,
1830 format,
1831 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001832 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001833 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001834 return -1;
1835 }
1836
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001837 flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001838
1839 for (i = 0; i < 2; i++) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001840 if (output->gbm_cursor_bo[i])
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001841 continue;
1842
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001843 output->gbm_cursor_bo[i] =
Giulio Camuffo954f1832014-10-11 18:27:30 +03001844 gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03001845 GBM_FORMAT_ARGB8888, flags);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001846 }
1847
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001848 if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001849 weston_log("cursor buffers unavailable, using gl cursors\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001850 b->cursors_are_broken = 1;
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02001851 }
1852
1853 return 0;
1854}
1855
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04001856static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001857drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001858{
Hardeningff39efa2013-09-18 23:56:35 +02001859 int w = output->base.current_mode->width;
1860 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001861 uint32_t format = output->gbm_format;
1862 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001863 unsigned int i;
1864
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001865 switch (format) {
1866 case GBM_FORMAT_XRGB8888:
1867 pixman_format = PIXMAN_x8r8g8b8;
1868 break;
1869 case GBM_FORMAT_RGB565:
1870 pixman_format = PIXMAN_r5g6b5;
1871 break;
1872 default:
1873 weston_log("Unsupported pixman format 0x%x\n", format);
1874 return -1;
1875 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001876
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001877 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001878 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001879 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001880 if (!output->dumb[i])
1881 goto err;
1882
1883 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03001884 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001885 output->dumb[i]->map,
1886 output->dumb[i]->stride);
1887 if (!output->image[i])
1888 goto err;
1889 }
1890
1891 if (pixman_renderer_output_create(&output->base) < 0)
1892 goto err;
1893
1894 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02001895 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001896
1897 return 0;
1898
1899err:
1900 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1901 if (output->dumb[i])
1902 drm_fb_destroy_dumb(output->dumb[i]);
1903 if (output->image[i])
1904 pixman_image_unref(output->image[i]);
1905
1906 output->dumb[i] = NULL;
1907 output->image[i] = NULL;
1908 }
1909
1910 return -1;
1911}
1912
1913static void
1914drm_output_fini_pixman(struct drm_output *output)
1915{
1916 unsigned int i;
1917
1918 pixman_renderer_output_destroy(&output->base);
1919 pixman_region32_fini(&output->previous_damage);
1920
1921 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
1922 drm_fb_destroy_dumb(output->dumb[i]);
1923 pixman_image_unref(output->image[i]);
1924 output->dumb[i] = NULL;
1925 output->image[i] = NULL;
1926 }
1927}
1928
Richard Hughes2b2092a2013-04-24 14:58:02 +01001929static void
1930edid_parse_string(const uint8_t *data, char text[])
1931{
1932 int i;
1933 int replaced = 0;
1934
1935 /* this is always 12 bytes, but we can't guarantee it's null
1936 * terminated or not junk. */
1937 strncpy(text, (const char *) data, 12);
1938
Bryce Harrington9c7de162015-08-28 13:04:26 -07001939 /* guarantee our new string is null-terminated */
1940 text[12] = '\0';
1941
Richard Hughes2b2092a2013-04-24 14:58:02 +01001942 /* remove insane chars */
1943 for (i = 0; text[i] != '\0'; i++) {
1944 if (text[i] == '\n' ||
1945 text[i] == '\r') {
1946 text[i] = '\0';
1947 break;
1948 }
1949 }
1950
1951 /* ensure string is printable */
1952 for (i = 0; text[i] != '\0'; i++) {
1953 if (!isprint(text[i])) {
1954 text[i] = '-';
1955 replaced++;
1956 }
1957 }
1958
1959 /* if the string is random junk, ignore the string */
1960 if (replaced > 4)
1961 text[0] = '\0';
1962}
1963
1964#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
1965#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
1966#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
1967#define EDID_OFFSET_DATA_BLOCKS 0x36
1968#define EDID_OFFSET_LAST_BLOCK 0x6c
1969#define EDID_OFFSET_PNPID 0x08
1970#define EDID_OFFSET_SERIAL 0x0c
1971
1972static int
1973edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
1974{
1975 int i;
1976 uint32_t serial_number;
1977
1978 /* check header */
1979 if (length < 128)
1980 return -1;
1981 if (data[0] != 0x00 || data[1] != 0xff)
1982 return -1;
1983
1984 /* decode the PNP ID from three 5 bit words packed into 2 bytes
1985 * /--08--\/--09--\
1986 * 7654321076543210
1987 * |\---/\---/\---/
1988 * R C1 C2 C3 */
1989 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
1990 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
1991 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
1992 edid->pnp_id[3] = '\0';
1993
1994 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
1995 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
1996 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
1997 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
1998 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
1999 if (serial_number > 0)
2000 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2001
2002 /* parse EDID data */
2003 for (i = EDID_OFFSET_DATA_BLOCKS;
2004 i <= EDID_OFFSET_LAST_BLOCK;
2005 i += 18) {
2006 /* ignore pixel clock data */
2007 if (data[i] != 0)
2008 continue;
2009 if (data[i+2] != 0)
2010 continue;
2011
2012 /* any useful blocks? */
2013 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2014 edid_parse_string(&data[i+5],
2015 edid->monitor_name);
2016 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2017 edid_parse_string(&data[i+5],
2018 edid->serial_number);
2019 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2020 edid_parse_string(&data[i+5],
2021 edid->eisa_id);
2022 }
2023 }
2024 return 0;
2025}
2026
2027static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002028find_and_parse_output_edid(struct drm_backend *b,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002029 struct drm_output *output,
2030 drmModeConnector *connector)
2031{
2032 drmModePropertyBlobPtr edid_blob = NULL;
2033 drmModePropertyPtr property;
2034 int i;
2035 int rc;
2036
2037 for (i = 0; i < connector->count_props && !edid_blob; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002038 property = drmModeGetProperty(b->drm.fd, connector->props[i]);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002039 if (!property)
2040 continue;
2041 if ((property->flags & DRM_MODE_PROP_BLOB) &&
2042 !strcmp(property->name, "EDID")) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002043 edid_blob = drmModeGetPropertyBlob(b->drm.fd,
Richard Hughes2b2092a2013-04-24 14:58:02 +01002044 connector->prop_values[i]);
2045 }
2046 drmModeFreeProperty(property);
2047 }
2048 if (!edid_blob)
2049 return;
2050
2051 rc = edid_parse(&output->edid,
2052 edid_blob->data,
2053 edid_blob->length);
2054 if (!rc) {
2055 weston_log("EDID data '%s', '%s', '%s'\n",
2056 output->edid.pnp_id,
2057 output->edid.monitor_name,
2058 output->edid.serial_number);
2059 if (output->edid.pnp_id[0] != '\0')
2060 output->base.make = output->edid.pnp_id;
2061 if (output->edid.monitor_name[0] != '\0')
2062 output->base.model = output->edid.monitor_name;
2063 if (output->edid.serial_number[0] != '\0')
2064 output->base.serial_number = output->edid.serial_number;
2065 }
2066 drmModeFreePropertyBlob(edid_blob);
2067}
2068
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002069
2070
2071static int
2072parse_modeline(const char *s, drmModeModeInfo *mode)
2073{
2074 char hsync[16];
2075 char vsync[16];
2076 float fclock;
2077
2078 mode->type = DRM_MODE_TYPE_USERDEF;
2079 mode->hskew = 0;
2080 mode->vscan = 0;
2081 mode->vrefresh = 0;
2082 mode->flags = 0;
2083
Rob Bradford307e09e2013-07-26 16:29:40 +01002084 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002085 &fclock,
2086 &mode->hdisplay,
2087 &mode->hsync_start,
2088 &mode->hsync_end,
2089 &mode->htotal,
2090 &mode->vdisplay,
2091 &mode->vsync_start,
2092 &mode->vsync_end,
2093 &mode->vtotal, hsync, vsync) != 11)
2094 return -1;
2095
2096 mode->clock = fclock * 1000;
2097 if (strcmp(hsync, "+hsync") == 0)
2098 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2099 else if (strcmp(hsync, "-hsync") == 0)
2100 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2101 else
2102 return -1;
2103
2104 if (strcmp(vsync, "+vsync") == 0)
2105 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2106 else if (strcmp(vsync, "-vsync") == 0)
2107 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2108 else
2109 return -1;
2110
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002111 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2112 mode->hdisplay, mode->vdisplay, fclock);
2113
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002114 return 0;
2115}
2116
Rob Bradford66bd9f52013-06-25 18:56:42 +01002117static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002118setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002119 struct weston_output *output,
2120 const char *s)
2121{
2122 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002123 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002124 struct udev_seat *seat;
2125
Giulio Camuffo954f1832014-10-11 18:27:30 +03002126 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002127 if (!seat)
2128 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002129
Derek Foreman0720ea32015-07-15 13:00:35 -05002130 seat->base.output = output;
2131
Derek Foreman1281a362015-07-31 16:55:32 -05002132 pointer = weston_seat_get_pointer(&seat->base);
2133 if (pointer)
2134 weston_pointer_clamp(pointer,
2135 &pointer->x,
2136 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002137 }
2138}
2139
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002140static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002141parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002142{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002143 int ret = 0;
2144
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002145 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002146 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002147 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002148 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002149 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002150 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002151 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002152 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002153 else {
2154 weston_log("fatal: unrecognized pixel format: %s\n", s);
2155 ret = -1;
2156 }
2157
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002158 return ret;
2159}
2160
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002161/**
2162 * Choose suitable mode for an output
2163 *
2164 * Find the most suitable mode to use for initial setup (or reconfiguration on
2165 * hotplug etc) for a DRM output.
2166 *
2167 * @param output DRM output to choose mode for
2168 * @param kind Strategy and preference to use when choosing mode
2169 * @param width Desired width for this output
2170 * @param height Desired height for this output
2171 * @param current_mode Mode currently being displayed on this output
2172 * @param modeline Manually-entered mode (may be NULL)
2173 * @returns A mode from the output's mode list, or NULL if none available
2174 */
2175static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002176drm_output_choose_initial_mode(struct drm_backend *backend,
2177 struct drm_output *output,
2178 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002179 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002180 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002181{
2182 struct drm_mode *preferred = NULL;
2183 struct drm_mode *current = NULL;
2184 struct drm_mode *configured = NULL;
2185 struct drm_mode *best = NULL;
2186 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002187 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002188 int32_t width = 0;
2189 int32_t height = 0;
2190
Armin Krezović08368132016-09-30 14:11:05 +02002191 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
2192 if (sscanf(modeline, "%dx%d", &width, &height) != 2) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002193 width = -1;
2194
Armin Krezović08368132016-09-30 14:11:05 +02002195 if (parse_modeline(modeline, &drm_modeline) == 0) {
2196 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002197 if (!configured)
2198 return NULL;
2199 } else {
2200 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02002201 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002202 }
2203 }
2204 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002205
2206 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002207 if (width == drm_mode->base.width &&
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002208 height == drm_mode->base.height)
2209 configured = drm_mode;
2210
comic fans7a5c5622016-03-17 14:29:27 +02002211 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002212 sizeof *current_mode) == 0)
2213 current = drm_mode;
2214
2215 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
2216 preferred = drm_mode;
2217
2218 best = drm_mode;
2219 }
2220
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002221 if (current == NULL && current_mode->clock != 0) {
2222 current = drm_output_add_mode(output, current_mode);
2223 if (!current)
2224 return NULL;
2225 }
2226
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002227 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002228 configured = current;
2229
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002230 if (configured)
2231 return configured;
2232
2233 if (preferred)
2234 return preferred;
2235
2236 if (current)
2237 return current;
2238
2239 if (best)
2240 return best;
2241
2242 weston_log("no available modes for %s\n", output->base.name);
2243 return NULL;
2244}
2245
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002246static int
2247connector_get_current_mode(drmModeConnector *connector, int drm_fd,
2248 drmModeModeInfo *mode)
2249{
2250 drmModeEncoder *encoder;
2251 drmModeCrtc *crtc;
2252
2253 /* Get the current mode on the crtc that's currently driving
2254 * this connector. */
2255 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
2256 memset(mode, 0, sizeof *mode);
2257 if (encoder != NULL) {
2258 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
2259 drmModeFreeEncoder(encoder);
2260 if (crtc == NULL)
2261 return -1;
2262 if (crtc->mode_valid)
2263 *mode = crtc->mode;
2264 drmModeFreeCrtc(crtc);
2265 }
2266
2267 return 0;
2268}
2269
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002270static int
Armin Krezović08368132016-09-30 14:11:05 +02002271drm_output_set_mode(struct weston_output *base,
2272 enum weston_drm_backend_output_mode mode,
2273 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002274{
Armin Krezović08368132016-09-30 14:11:05 +02002275 struct drm_output *output = to_drm_output(base);
2276 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002277
Armin Krezović08368132016-09-30 14:11:05 +02002278 struct drm_mode *drm_mode, *next, *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002279 drmModeModeInfo crtc_mode;
2280 int i;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002281
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002282 output->base.make = "unknown";
2283 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01002284 output->base.serial_number = "unknown";
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002285 wl_list_init(&output->base.mode_list);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002286
Giulio Camuffo954f1832014-10-11 18:27:30 +03002287 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
Matt Roper361d2ad2011-08-29 13:52:23 -07002288
Armin Krezović08368132016-09-30 14:11:05 +02002289 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Pekka Paalaneneee580b2014-06-04 16:43:06 +03002290 goto err_free;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002291
Armin Krezović08368132016-09-30 14:11:05 +02002292 for (i = 0; i < output->connector->count_modes; i++) {
2293 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002294 if (!drm_mode)
David Herrmann0f0d54e2011-12-08 17:05:45 +01002295 goto err_free;
2296 }
2297
Armin Krezović08368132016-09-30 14:11:05 +02002298 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002299 if (!current)
Wang Quanxianacb805a2012-07-30 18:09:46 -04002300 goto err_free;
Armin Krezović08368132016-09-30 14:11:05 +02002301
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002302 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02002303 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04002304
Armin Krezović08368132016-09-30 14:11:05 +02002305 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
2306 output->base.native_mode = output->base.current_mode;
2307 output->base.native_scale = output->base.current_scale;
2308
2309 output->base.mm_width = output->connector->mmWidth;
2310 output->base.mm_height = output->connector->mmHeight;
2311
2312 return 0;
2313
2314err_free:
2315 drmModeFreeCrtc(output->original_crtc);
2316 output->original_crtc = NULL;
2317
2318 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
2319 base.link) {
2320 wl_list_remove(&drm_mode->base.link);
2321 free(drm_mode);
2322 }
2323
2324 return -1;
2325}
2326
2327static void
2328drm_output_set_gbm_format(struct weston_output *base,
2329 const char *gbm_format)
2330{
2331 struct drm_output *output = to_drm_output(base);
2332 struct drm_backend *b = to_drm_backend(base->compositor);
2333
2334 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
2335 output->gbm_format = b->gbm_format;
2336}
2337
2338static void
2339drm_output_set_seat(struct weston_output *base,
2340 const char *seat)
2341{
2342 struct drm_output *output = to_drm_output(base);
2343 struct drm_backend *b = to_drm_backend(base->compositor);
2344
2345 setup_output_seat_constraint(b, &output->base,
2346 seat ? seat : "");
2347}
2348
2349static int
2350drm_output_enable(struct weston_output *base)
2351{
2352 struct drm_output *output = to_drm_output(base);
2353 struct drm_backend *b = to_drm_backend(base->compositor);
2354 struct weston_mode *m;
2355
2356 output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS");
John Kåre Alsaker94659272012-11-13 19:10:18 +01002357
Giulio Camuffo954f1832014-10-11 18:27:30 +03002358 if (b->use_pixman) {
2359 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002360 weston_log("Failed to init output pixman state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002361 goto err_free;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002362 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03002363 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002364 weston_log("Failed to init output gl state\n");
Armin Krezović08368132016-09-30 14:11:05 +02002365 goto err_free;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04002366 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04002367
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002368 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002369 weston_log("Initialized backlight, device %s\n",
2370 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002371 output->base.set_backlight = drm_set_backlight;
2372 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04002373 } else {
2374 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002375 }
2376
Jonas Ådahle5a12252013-04-05 23:07:11 +02002377 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05002378 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002379 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002380 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08002381 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002382
Richard Hughese7299962013-05-01 21:52:12 +01002383 output->base.gamma_size = output->original_crtc->gamma_size;
2384 output->base.set_gamma = drm_output_set_gamma;
2385
Armin Krezović08368132016-09-30 14:11:05 +02002386 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
2387
2388 find_and_parse_output_edid(b, output, output->connector);
2389 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
2390 output->base.connection_internal = 1;
2391
Derek Foremanb0427562016-02-05 15:55:20 -06002392 weston_plane_init(&output->cursor_plane, b->compositor,
2393 INT32_MIN, INT32_MIN);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002394 weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002395
Giulio Camuffo954f1832014-10-11 18:27:30 +03002396 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
2397 weston_compositor_stack_plane(b->compositor, &output->fb_plane,
2398 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002399
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002400 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01002401 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002402 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08002403 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002404 m->width, m->height, m->refresh / 1000.0,
2405 m->flags & WL_OUTPUT_MODE_PREFERRED ?
2406 ", preferred" : "",
2407 m->flags & WL_OUTPUT_MODE_CURRENT ?
2408 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02002409 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002410 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002411
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002412 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01002413
2414err_free:
Armin Krezović08368132016-09-30 14:11:05 +02002415 drmModeFreeProperty(output->dpms_prop);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002416
David Herrmann0f0d54e2011-12-08 17:05:45 +01002417 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002418}
2419
Jesse Barnes58ef3792012-02-23 09:45:49 -05002420static void
Armin Krezović08368132016-09-30 14:11:05 +02002421drm_output_deinit(struct weston_output *base)
2422{
2423 struct drm_output *output = to_drm_output(base);
2424 struct drm_backend *b = to_drm_backend(base->compositor);
2425
2426 if (b->use_pixman) {
2427 drm_output_fini_pixman(output);
2428 } else {
2429 gl_renderer->output_destroy(&output->base);
2430 gbm_surface_destroy(output->gbm_surface);
2431 }
2432
2433 weston_plane_release(&output->fb_plane);
2434 weston_plane_release(&output->cursor_plane);
2435
2436 drmModeFreeProperty(output->dpms_prop);
2437
2438 /* Turn off hardware cursor */
2439 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
2440}
2441
2442static void
2443drm_output_destroy(struct weston_output *base)
2444{
2445 struct drm_output *output = to_drm_output(base);
2446 struct drm_backend *b = to_drm_backend(base->compositor);
2447 drmModeCrtcPtr origcrtc = output->original_crtc;
2448
2449 if (output->page_flip_pending) {
2450 output->destroy_pending = 1;
2451 weston_log("destroy output while page flip pending\n");
2452 return;
2453 }
2454
2455 if (output->base.enabled)
2456 drm_output_deinit(&output->base);
2457
2458 if (origcrtc) {
2459 /* Restore original CRTC state */
2460 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
2461 origcrtc->x, origcrtc->y,
2462 &output->connector_id, 1, &origcrtc->mode);
2463 drmModeFreeCrtc(origcrtc);
2464 }
2465
2466 weston_output_destroy(&output->base);
2467
2468 drmModeFreeConnector(output->connector);
2469
2470 if (output->backlight)
2471 backlight_destroy(output->backlight);
2472
2473 b->crtc_allocator &= ~(1 << output->crtc_id);
2474 b->connector_allocator &= ~(1 << output->connector_id);
2475
2476 free(output);
2477}
2478
2479static int
2480drm_output_disable(struct weston_output *base)
2481{
2482 struct drm_output *output = to_drm_output(base);
2483 struct drm_backend *b = to_drm_backend(base->compositor);
2484
2485 if (output->page_flip_pending) {
2486 output->disable_pending = 1;
2487 return -1;
2488 }
2489
2490 if (output->base.enabled)
2491 drm_output_deinit(&output->base);
2492
2493 output->disable_pending = 0;
2494
2495 weston_log("Disabling output %s\n", output->base.name);
2496 drmModeSetCrtc(b->drm.fd, output->crtc_id,
2497 0, 0, 0, 0, 0, NULL);
2498
2499 return 0;
2500}
2501
2502/**
2503 * Create a Weston output structure
2504 *
2505 * Given a DRM connector, create a matching drm_output structure and add it
2506 * to Weston's output list. It also takes ownership of the connector, which
2507 * is released when output is destroyed.
2508 *
2509 * @param b Weston backend structure
2510 * @param resources DRM resources for this device
2511 * @param connector DRM connector to use for this new output
2512 * @param drm_device udev device pointer
2513 * @returns 0 on success, or -1 on failure
2514 */
2515static int
2516create_output_for_connector(struct drm_backend *b,
2517 drmModeRes *resources,
2518 drmModeConnector *connector,
2519 struct udev_device *drm_device)
2520{
2521 struct drm_output *output;
2522 int i;
2523
2524 i = find_crtc_for_connector(b, resources, connector);
2525 if (i < 0) {
2526 weston_log("No usable crtc/encoder pair for connector.\n");
2527 return -1;
2528 }
2529
2530 output = zalloc(sizeof *output);
2531 if (output == NULL)
2532 return -1;
2533
2534 output->connector = connector;
2535 output->crtc_id = resources->crtcs[i];
2536 output->pipe = i;
2537 output->connector_id = connector->connector_id;
2538
2539 output->backlight = backlight_init(drm_device,
2540 connector->connector_type);
2541
2542 output->base.enable = drm_output_enable;
2543 output->base.destroy = drm_output_destroy;
2544 output->base.disable = drm_output_disable;
2545 output->base.name = make_connector_name(connector);
2546
2547 output->destroy_pending = 0;
2548 output->disable_pending = 0;
2549 output->original_crtc = NULL;
2550
2551 b->crtc_allocator |= (1 << output->crtc_id);
2552 b->connector_allocator |= (1 << output->connector_id);
2553
Armin Krezović40087402016-09-30 14:11:12 +02002554 weston_output_init(&output->base, b->compositor);
Armin Krezović08368132016-09-30 14:11:05 +02002555 weston_compositor_add_pending_output(&output->base, b->compositor);
2556
2557 return 0;
2558}
2559
2560static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002561create_sprites(struct drm_backend *b)
Jesse Barnes58ef3792012-02-23 09:45:49 -05002562{
2563 struct drm_sprite *sprite;
2564 drmModePlaneRes *plane_res;
2565 drmModePlane *plane;
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002566 uint32_t i;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002567
Giulio Camuffo954f1832014-10-11 18:27:30 +03002568 plane_res = drmModeGetPlaneResources(b->drm.fd);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002569 if (!plane_res) {
Martin Minarik6d118362012-06-07 18:01:59 +02002570 weston_log("failed to get plane resources: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002571 strerror(errno));
2572 return;
2573 }
2574
2575 for (i = 0; i < plane_res->count_planes; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002576 plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002577 if (!plane)
2578 continue;
2579
Peter Huttererf3d62272013-08-08 11:57:05 +10002580 sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
Jesse Barnes58ef3792012-02-23 09:45:49 -05002581 plane->count_formats));
2582 if (!sprite) {
Martin Minarik6d118362012-06-07 18:01:59 +02002583 weston_log("%s: out of memory\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05002584 __func__);
Chris Michael8b376872014-01-02 11:39:40 +00002585 drmModeFreePlane(plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002586 continue;
2587 }
2588
Jesse Barnes58ef3792012-02-23 09:45:49 -05002589 sprite->possible_crtcs = plane->possible_crtcs;
2590 sprite->plane_id = plane->plane_id;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02002591 sprite->current = NULL;
2592 sprite->next = NULL;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002593 sprite->backend = b;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002594 sprite->count_formats = plane->count_formats;
2595 memcpy(sprite->formats, plane->formats,
Rob Clark8efbc8e2012-03-11 19:48:44 -05002596 plane->count_formats * sizeof(plane->formats[0]));
Jesse Barnes58ef3792012-02-23 09:45:49 -05002597 drmModeFreePlane(plane);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002598 weston_plane_init(&sprite->plane, b->compositor, 0, 0);
2599 weston_compositor_stack_plane(b->compositor, &sprite->plane,
2600 &b->compositor->primary_plane);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002601
Giulio Camuffo954f1832014-10-11 18:27:30 +03002602 wl_list_insert(&b->sprite_list, &sprite->link);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002603 }
2604
Samuel Iglesias Gonsalvezde466732013-06-13 10:03:33 +02002605 drmModeFreePlaneResources(plane_res);
Jesse Barnes58ef3792012-02-23 09:45:49 -05002606}
2607
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002608static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002609destroy_sprites(struct drm_backend *backend)
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002610{
2611 struct drm_sprite *sprite, *next;
2612 struct drm_output *output;
2613
Giulio Camuffo954f1832014-10-11 18:27:30 +03002614 output = container_of(backend->compositor->output_list.next,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002615 struct drm_output, base.link);
2616
Giulio Camuffo954f1832014-10-11 18:27:30 +03002617 wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
2618 drmModeSetPlane(backend->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002619 sprite->plane_id,
2620 output->crtc_id, 0, 0,
2621 0, 0, 0, 0, 0, 0, 0, 0);
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +02002622 drm_output_release_fb(output, sprite->current);
2623 drm_output_release_fb(output, sprite->next);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002624 weston_plane_release(&sprite->plane);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002625 free(sprite);
2626 }
2627}
Jesse Barnes58ef3792012-02-23 09:45:49 -05002628
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002629static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002630create_outputs(struct drm_backend *b, uint32_t option_connector,
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002631 struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002632{
2633 drmModeConnector *connector;
2634 drmModeRes *resources;
2635 int i;
2636
Giulio Camuffo954f1832014-10-11 18:27:30 +03002637 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002638 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002639 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002640 return -1;
2641 }
2642
Giulio Camuffo954f1832014-10-11 18:27:30 +03002643 b->min_width = resources->min_width;
2644 b->max_width = resources->max_width;
2645 b->min_height = resources->min_height;
2646 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05002647
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002648 for (i = 0; i < resources->count_connectors; i++) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002649 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02002650 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002651 if (connector == NULL)
2652 continue;
2653
2654 if (connector->connection == DRM_MODE_CONNECTED &&
2655 (option_connector == 0 ||
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002656 connector->connector_id == option_connector)) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002657 if (create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002658 connector, drm_device) < 0) {
Benjamin Franzke439d9862011-10-07 08:20:53 +02002659 drmModeFreeConnector(connector);
2660 continue;
2661 }
Armin Krezović08368132016-09-30 14:11:05 +02002662 } else {
2663 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02002664 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002665 }
2666
Armin Krezović08368132016-09-30 14:11:05 +02002667 if (wl_list_empty(&b->compositor->output_list) &&
2668 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02002669 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002670
2671 drmModeFreeResources(resources);
2672
2673 return 0;
2674}
2675
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002676static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002677update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002678{
2679 drmModeConnector *connector;
2680 drmModeRes *resources;
2681 struct drm_output *output, *next;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002682 uint32_t connected = 0, disconnects = 0;
2683 int i;
2684
Giulio Camuffo954f1832014-10-11 18:27:30 +03002685 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002686 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02002687 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688 return;
2689 }
2690
2691 /* collect new connects */
2692 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke117483d2011-08-30 11:38:26 +02002693 int connector_id = resources->connectors[i];
2694
Giulio Camuffo954f1832014-10-11 18:27:30 +03002695 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01002696 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002697 continue;
2698
David Herrmann7551cff2011-12-08 17:05:43 +01002699 if (connector->connection != DRM_MODE_CONNECTED) {
2700 drmModeFreeConnector(connector);
2701 continue;
2702 }
2703
Benjamin Franzke117483d2011-08-30 11:38:26 +02002704 connected |= (1 << connector_id);
2705
Giulio Camuffo954f1832014-10-11 18:27:30 +03002706 if (!(b->connector_allocator & (1 << connector_id))) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002707 create_output_for_connector(b, resources,
Armin Krezović08368132016-09-30 14:11:05 +02002708 connector, drm_device);
Martin Minarik6d118362012-06-07 18:01:59 +02002709 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002710
Armin Krezović08368132016-09-30 14:11:05 +02002711 } else {
2712 drmModeFreeConnector(connector);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002713 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002714 }
2715 drmModeFreeResources(resources);
2716
Giulio Camuffo954f1832014-10-11 18:27:30 +03002717 disconnects = b->connector_allocator & ~connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002718 if (disconnects) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03002719 wl_list_for_each_safe(output, next, &b->compositor->output_list,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002720 base.link) {
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002721 if (disconnects & (1 << output->connector_id)) {
2722 disconnects &= ~(1 << output->connector_id);
Martin Minarik6d118362012-06-07 18:01:59 +02002723 weston_log("connector %d disconnected\n",
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002724 output->connector_id);
Benjamin Franzke48c4ea22011-08-30 11:44:56 +02002725 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002726 }
2727 }
Armin Krezović08368132016-09-30 14:11:05 +02002728
2729 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
2730 base.link) {
2731 if (disconnects & (1 << output->connector_id)) {
2732 disconnects &= ~(1 << output->connector_id);
2733 weston_log("connector %d disconnected\n",
2734 output->connector_id);
2735 drm_output_destroy(&output->base);
2736 }
2737 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002738 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002739}
2740
2741static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002742udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002743{
David Herrmannd7488c22012-03-11 20:05:21 +01002744 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01002745 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01002746
2747 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002748 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01002749 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02002750
David Herrmann6ac52db2012-03-11 20:05:22 +01002751 val = udev_device_get_property_value(device, "HOTPLUG");
2752 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002753 return 0;
2754
David Herrmann6ac52db2012-03-11 20:05:22 +01002755 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002756}
2757
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002758static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759udev_drm_event(int fd, uint32_t mask, void *data)
2760{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002761 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002762 struct udev_device *event;
2763
Giulio Camuffo954f1832014-10-11 18:27:30 +03002764 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02002765
Giulio Camuffo954f1832014-10-11 18:27:30 +03002766 if (udev_event_is_hotplug(b, event))
2767 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002768
2769 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002770
2771 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002772}
2773
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002774static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002775drm_restore(struct weston_compositor *ec)
2776{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07002777 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002778}
2779
2780static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002781drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002782{
Armin Krezović545dba62016-08-05 15:54:18 +02002783 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002784
Giulio Camuffo954f1832014-10-11 18:27:30 +03002785 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002786
Giulio Camuffo954f1832014-10-11 18:27:30 +03002787 wl_event_source_remove(b->udev_drm_source);
2788 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002789
Giulio Camuffo954f1832014-10-11 18:27:30 +03002790 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04002791
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03002792 weston_compositor_shutdown(ec);
2793
Giulio Camuffo954f1832014-10-11 18:27:30 +03002794 if (b->gbm)
2795 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002796
Giulio Camuffo954f1832014-10-11 18:27:30 +03002797 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002798
Giulio Camuffo954f1832014-10-11 18:27:30 +03002799 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002800 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05002801}
2802
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002803static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002804drm_backend_set_modes(struct drm_backend *backend)
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002805{
2806 struct drm_output *output;
2807 struct drm_mode *drm_mode;
2808 int ret;
2809
Giulio Camuffo954f1832014-10-11 18:27:30 +03002810 wl_list_for_each(output, &backend->compositor->output_list, base.link) {
Ander Conselvan de Oliveira2002f882013-02-26 13:44:58 +02002811 if (!output->current) {
2812 /* If something that would cause the output to
2813 * switch mode happened while in another vt, we
2814 * might not have a current drm_fb. In that case,
2815 * schedule a repaint and let drm_output_repaint
2816 * handle setting the mode. */
2817 weston_output_schedule_repaint(&output->base);
2818 continue;
2819 }
2820
Hardeningff39efa2013-09-18 23:56:35 +02002821 drm_mode = (struct drm_mode *) output->base.current_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002822 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03002823 output->current->fb_id, 0, 0,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002824 &output->connector_id, 1,
2825 &drm_mode->mode_info);
2826 if (ret < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002827 weston_log(
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002828 "failed to set mode %dx%d for output at %d,%d: %m\n",
Daniel Stonef556ebe2015-05-21 08:28:58 +01002829 drm_mode->base.width, drm_mode->base.height,
Kristian Høgsberg835cd492012-01-18 11:48:46 -05002830 output->base.x, output->base.y);
2831 }
2832 }
2833}
2834
2835static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002836session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002837{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002838 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02002839 struct drm_backend *b = to_drm_backend(compositor);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002840 struct drm_sprite *sprite;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002841 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002842
Giulio Camuffo954f1832014-10-11 18:27:30 +03002843 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002844 weston_log("activating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002845 compositor->state = b->prev_state;
2846 drm_backend_set_modes(b);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002847 weston_compositor_damage_all(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002848 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002849 } else {
2850 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03002851 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04002852
Giulio Camuffo954f1832014-10-11 18:27:30 +03002853 b->prev_state = compositor->state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002854 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04002855
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002856 /* If we have a repaint scheduled (either from a
2857 * pending pageflip or the idle handler), make sure we
2858 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002859 * vt switched away. The OFFSCREEN state will prevent
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002860 * further attemps at repainting. When we switch
2861 * back, we schedule a repaint, which will process
2862 * pending frame callbacks. */
2863
Giulio Camuffo954f1832014-10-11 18:27:30 +03002864 wl_list_for_each(output, &compositor->output_list, base.link) {
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002865 output->base.repaint_needed = 0;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002866 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002867 }
2868
Giulio Camuffo954f1832014-10-11 18:27:30 +03002869 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002870 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002871
Giulio Camuffo954f1832014-10-11 18:27:30 +03002872 wl_list_for_each(sprite, &b->sprite_list, link)
2873 drmModeSetPlane(b->drm.fd,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002874 sprite->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04002875 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05002876 0, 0, 0, 0, 0, 0, 0, 0);
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04002877 };
2878}
2879
David Herrmann0af066f2012-10-29 19:21:16 +01002880/*
2881 * Find primary GPU
2882 * Some systems may have multiple DRM devices attached to a single seat. This
2883 * function loops over all devices and tries to find a PCI device with the
2884 * boot_vga sysfs attribute set to 1.
2885 * If no such device is found, the first DRM device reported by udev is used.
2886 */
2887static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03002888find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01002889{
2890 struct udev_enumerate *e;
2891 struct udev_list_entry *entry;
2892 const char *path, *device_seat, *id;
2893 struct udev_device *device, *drm_device, *pci;
2894
Giulio Camuffo954f1832014-10-11 18:27:30 +03002895 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01002896 udev_enumerate_add_match_subsystem(e, "drm");
2897 udev_enumerate_add_match_sysname(e, "card[0-9]*");
2898
2899 udev_enumerate_scan_devices(e);
2900 drm_device = NULL;
2901 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
2902 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002903 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01002904 if (!device)
2905 continue;
2906 device_seat = udev_device_get_property_value(device, "ID_SEAT");
2907 if (!device_seat)
2908 device_seat = default_seat;
2909 if (strcmp(device_seat, seat)) {
2910 udev_device_unref(device);
2911 continue;
2912 }
2913
2914 pci = udev_device_get_parent_with_subsystem_devtype(device,
2915 "pci", NULL);
2916 if (pci) {
2917 id = udev_device_get_sysattr_value(pci, "boot_vga");
2918 if (id && !strcmp(id, "1")) {
2919 if (drm_device)
2920 udev_device_unref(drm_device);
2921 drm_device = device;
2922 break;
2923 }
2924 }
2925
2926 if (!drm_device)
2927 drm_device = device;
2928 else
2929 udev_device_unref(device);
2930 }
2931
2932 udev_enumerate_unref(e);
2933 return drm_device;
2934}
2935
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002936static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05002937planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
2938 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002939{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002940 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02002941
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002942 switch (key) {
2943 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002944 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002945 break;
2946 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002947 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002948 break;
2949 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03002950 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02002951 break;
2952 default:
2953 break;
2954 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02002955}
2956
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07002957#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002958static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002959recorder_destroy(struct drm_output *output)
2960{
2961 vaapi_recorder_destroy(output->recorder);
2962 output->recorder = NULL;
2963
2964 output->base.disable_planes--;
2965
2966 wl_list_remove(&output->recorder_frame_listener.link);
2967 weston_log("[libva recorder] done\n");
2968}
2969
2970static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002971recorder_frame_notify(struct wl_listener *listener, void *data)
2972{
2973 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002974 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002975 int fd, ret;
2976
2977 output = container_of(listener, struct drm_output,
2978 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02002979 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002980
2981 if (!output->recorder)
2982 return;
2983
Giulio Camuffo954f1832014-10-11 18:27:30 +03002984 ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002985 DRM_CLOEXEC, &fd);
2986 if (ret) {
2987 weston_log("[libva recorder] "
2988 "failed to create prime fd for front buffer\n");
2989 return;
2990 }
2991
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03002992 ret = vaapi_recorder_frame(output->recorder, fd,
2993 output->current->stride);
2994 if (ret < 0) {
2995 weston_log("[libva recorder] aborted: %m\n");
2996 recorder_destroy(output);
2997 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03002998}
2999
3000static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003001create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003002 const char *filename)
3003{
3004 int fd;
3005 drm_magic_t magic;
3006
Giulio Camuffo954f1832014-10-11 18:27:30 +03003007 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003008 if (fd < 0)
3009 return NULL;
3010
3011 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003012 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003013
3014 return vaapi_recorder_create(fd, width, height, filename);
3015}
3016
3017static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003018recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003019 void *data)
3020{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003021 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003022 struct drm_output *output;
3023 int width, height;
3024
Giulio Camuffo954f1832014-10-11 18:27:30 +03003025 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003026 struct drm_output, base.link);
3027
3028 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003029 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003030 weston_log("failed to start vaapi recorder: "
3031 "output format not supported\n");
3032 return;
3033 }
3034
Hardeningff39efa2013-09-18 23:56:35 +02003035 width = output->base.current_mode->width;
3036 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003037
3038 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003039 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003040 if (!output->recorder) {
3041 weston_log("failed to create vaapi recorder\n");
3042 return;
3043 }
3044
3045 output->base.disable_planes++;
3046
3047 output->recorder_frame_listener.notify = recorder_frame_notify;
3048 wl_signal_add(&output->base.frame_signal,
3049 &output->recorder_frame_listener);
3050
3051 weston_output_schedule_repaint(&output->base);
3052
3053 weston_log("[libva recorder] initialized\n");
3054 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003055 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003056 }
3057}
3058#else
3059static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003060recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003061 void *data)
3062{
3063 weston_log("Compiled without libva support\n");
3064}
3065#endif
3066
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003067static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003068switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003069{
3070 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003071 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003072
Giulio Camuffo954f1832014-10-11 18:27:30 +03003073 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003074 return;
3075
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003076 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3077
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003078 weston_log("Switching to GL renderer\n");
3079
Giulio Camuffo954f1832014-10-11 18:27:30 +03003080 b->gbm = create_gbm_device(b->drm.fd);
3081 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003082 weston_log("Failed to create gbm device. "
3083 "Aborting renderer switch\n");
3084 return;
3085 }
3086
Giulio Camuffo954f1832014-10-11 18:27:30 +03003087 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003088 pixman_renderer_output_destroy(&output->base);
3089
Giulio Camuffo954f1832014-10-11 18:27:30 +03003090 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003091
Giulio Camuffo954f1832014-10-11 18:27:30 +03003092 if (drm_backend_create_gl_renderer(b) < 0) {
3093 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003094 weston_log("Failed to create GL renderer. Quitting.\n");
3095 /* FIXME: we need a function to shutdown cleanly */
3096 assert(0);
3097 }
3098
Giulio Camuffo954f1832014-10-11 18:27:30 +03003099 wl_list_for_each(output, &b->compositor->output_list, base.link)
3100 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003101
Giulio Camuffo954f1832014-10-11 18:27:30 +03003102 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003103
3104 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3105 if (linux_dmabuf_setup(b->compositor) < 0)
3106 weston_log("Error: initializing dmabuf "
3107 "support failed.\n");
3108 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003109}
3110
3111static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003112renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3113 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003114{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003115 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003116 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003117
Giulio Camuffo954f1832014-10-11 18:27:30 +03003118 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003119}
3120
Armin Krezović08368132016-09-30 14:11:05 +02003121static const struct weston_drm_output_api api = {
3122 drm_output_set_mode,
3123 drm_output_set_gbm_format,
3124 drm_output_set_seat,
3125};
3126
Giulio Camuffo954f1832014-10-11 18:27:30 +03003127static struct drm_backend *
3128drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003129 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003130{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003131 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003132 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003133 struct wl_event_loop *loop;
David Herrmann0af066f2012-10-29 19:21:16 +01003134 const char *path;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003135 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003136 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003137
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003138 weston_log("initializing drm backend\n");
3139
Giulio Camuffo954f1832014-10-11 18:27:30 +03003140 b = zalloc(sizeof *b);
3141 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003142 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003143
Pekka Paalanen68583832015-05-19 09:53:16 +03003144 /*
3145 * KMS support for hardware planes cannot properly synchronize
3146 * without nuclear page flip. Without nuclear/atomic, hw plane
3147 * and cursor plane updates would either tear or cause extra
3148 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003149 * to a fraction. For cursors, it's not so bad, so they are
3150 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003151 *
3152 * These can be enabled again when nuclear/atomic support lands.
3153 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003154 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003155 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003156 b->use_pixman = config->use_pixman;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003157 b->use_current_mode = config->use_current_mode;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003158
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003159 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
3160 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07003161
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003162 if (config->seat_id)
3163 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003164
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003165 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003166 compositor->launcher = weston_launcher_connect(compositor, config->tty,
3167 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003168 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003169 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04003170 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01003171 goto err_compositor;
3172 }
3173
Giulio Camuffo954f1832014-10-11 18:27:30 +03003174 b->udev = udev_new();
3175 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003176 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003177 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003178 }
3179
Giulio Camuffo954f1832014-10-11 18:27:30 +03003180 b->session_listener.notify = session_notify;
3181 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05003182
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003183 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04003184 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003185 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003186 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003187 }
David Herrmann0af066f2012-10-29 19:21:16 +01003188 path = udev_device_get_syspath(drm_device);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003189
Giulio Camuffo954f1832014-10-11 18:27:30 +03003190 if (init_drm(b, drm_device) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02003191 weston_log("failed to initialize kms\n");
3192 goto err_udev_dev;
3193 }
3194
Giulio Camuffo954f1832014-10-11 18:27:30 +03003195 if (b->use_pixman) {
3196 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003197 weston_log("failed to initialize pixman renderer\n");
3198 goto err_udev_dev;
3199 }
3200 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03003201 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003202 weston_log("failed to initialize egl\n");
3203 goto err_udev_dev;
3204 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003205 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05003206
Giulio Camuffo954f1832014-10-11 18:27:30 +03003207 b->base.destroy = drm_destroy;
3208 b->base.restore = drm_restore;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02003209
Giulio Camuffo954f1832014-10-11 18:27:30 +03003210 b->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +02003211
Bob Ham91880f12016-01-12 10:21:47 +00003212 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04003213
Giulio Camuffo954f1832014-10-11 18:27:30 +03003214 wl_list_init(&b->sprite_list);
3215 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05003216
Giulio Camuffo954f1832014-10-11 18:27:30 +03003217 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03003218 compositor, b->udev, seat_id,
3219 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003220 weston_log("failed to create input devices\n");
3221 goto err_sprite;
3222 }
3223
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003224 if (create_outputs(b, config->connector, drm_device) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003225 weston_log("failed to create output for %s\n", path);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003226 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003227 }
3228
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003229 /* A this point we have some idea of whether or not we have a working
3230 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03003231 if (!b->cursors_are_broken)
3232 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05003233
Benjamin Franzke02dee2c2011-10-07 08:27:26 +02003234 path = NULL;
3235
Giulio Camuffo954f1832014-10-11 18:27:30 +03003236 loop = wl_display_get_event_loop(compositor->wl_display);
3237 b->drm_source =
3238 wl_event_loop_add_fd(loop, b->drm.fd,
3239 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003240
Giulio Camuffo954f1832014-10-11 18:27:30 +03003241 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
3242 if (b->udev_monitor == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003243 weston_log("failed to intialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003244 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003245 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003246 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003247 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003248 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02003249 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003250 udev_monitor_get_fd(b->udev_monitor),
3251 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003252
Giulio Camuffo954f1832014-10-11 18:27:30 +03003253 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02003254 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01003255 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003256 }
3257
Daniel Stonea96b93c2012-06-22 14:04:37 +01003258 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003259
Giulio Camuffo954f1832014-10-11 18:27:30 +03003260 weston_compositor_add_debug_binding(compositor, KEY_O,
3261 planes_binding, b);
3262 weston_compositor_add_debug_binding(compositor, KEY_C,
3263 planes_binding, b);
3264 weston_compositor_add_debug_binding(compositor, KEY_V,
3265 planes_binding, b);
3266 weston_compositor_add_debug_binding(compositor, KEY_Q,
3267 recorder_binding, b);
3268 weston_compositor_add_debug_binding(compositor, KEY_W,
3269 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003270
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003271 if (compositor->renderer->import_dmabuf) {
3272 if (linux_dmabuf_setup(compositor) < 0)
3273 weston_log("Error: initializing dmabuf "
3274 "support failed.\n");
3275 }
3276
Giulio Camuffo954f1832014-10-11 18:27:30 +03003277 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003278
Armin Krezović08368132016-09-30 14:11:05 +02003279 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
3280 &api, sizeof(api));
3281
3282 if (ret < 0) {
3283 weston_log("Failed to register output API.\n");
3284 goto err_udev_monitor;
3285 }
3286
Giulio Camuffo954f1832014-10-11 18:27:30 +03003287 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01003288
3289err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003290 wl_event_source_remove(b->udev_drm_source);
3291 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003292err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003293 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03003294err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003295 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04003296err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01003297 if (b->gbm)
3298 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003299 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003300err_udev_dev:
3301 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003302err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003303 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003304err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003305 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003306err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003307 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003308 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01003309 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003310}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003311
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003312static void
3313config_init_to_defaults(struct weston_drm_backend_config *config)
3314{
3315}
3316
Giulio Camuffo954f1832014-10-11 18:27:30 +03003317WL_EXPORT int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003318backend_init(struct weston_compositor *compositor,
Giulio Camuffo93daabb2015-10-17 19:24:14 +03003319 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003320{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003321 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003322 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003323
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003324 if (config_base == NULL ||
3325 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
3326 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
3327 weston_log("drm backend config structure is invalid\n");
3328 return -1;
3329 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02003330
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003331 config_init_to_defaults(&config);
3332 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07003333
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003334 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003335 if (b == NULL)
3336 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003337
Giulio Camuffo954f1832014-10-11 18:27:30 +03003338 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003339}