blob: 0259c630a426f68146da389ce1cd07001d2c6a11 [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"
Daniel Stone0b70fa42017-04-04 17:54:23 +010057#include "pixel-formats.h"
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -070058#include "libbacklight.h"
Peter Hutterer823ad332014-11-26 07:06:31 +100059#include "libinput-seat.h"
Benjamin Franzkebfeda132012-01-30 14:04:04 +010060#include "launcher-util.h"
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +030061#include "vaapi-recorder.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020062#include "presentation-time-server-protocol.h"
Pekka Paalanene4d231e2014-06-12 15:12:48 +030063#include "linux-dmabuf.h"
Micah Fedkec8890122017-02-01 15:28:23 -050064#include "linux-dmabuf-unstable-v1-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040065
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +030066#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
67#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
68#endif
69
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010070#ifndef DRM_CLIENT_CAP_UNIVERSAL_PLANES
71#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2
72#endif
73
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -030074#ifndef DRM_CAP_CURSOR_WIDTH
75#define DRM_CAP_CURSOR_WIDTH 0x8
76#endif
77
78#ifndef DRM_CAP_CURSOR_HEIGHT
79#define DRM_CAP_CURSOR_HEIGHT 0x9
80#endif
81
82#ifndef GBM_BO_USE_CURSOR
83#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
84#endif
85
Daniel Stone02cf4662017-03-03 16:19:39 +000086/**
Pekka Paalanenc5de57f2015-05-20 23:01:44 +010087 * List of properties attached to DRM planes
88 */
89enum wdrm_plane_property {
90 WDRM_PLANE_TYPE = 0,
91 WDRM_PLANE__COUNT
92};
93
94/**
95 * Possible values for the WDRM_PLANE_TYPE property.
96 */
97enum wdrm_plane_type {
98 WDRM_PLANE_TYPE_PRIMARY = 0,
99 WDRM_PLANE_TYPE_CURSOR,
100 WDRM_PLANE_TYPE_OVERLAY,
101 WDRM_PLANE_TYPE__COUNT
102};
103
104/**
Daniel Stone02cf4662017-03-03 16:19:39 +0000105 * List of properties attached to a DRM connector
106 */
107enum wdrm_connector_property {
108 WDRM_CONNECTOR_EDID = 0,
109 WDRM_CONNECTOR_DPMS,
110 WDRM_CONNECTOR__COUNT
111};
112
113/**
114 * Represents the values of an enum-type KMS property
115 */
116struct drm_property_enum_info {
117 const char *name; /**< name as string (static, not freed) */
118 bool valid; /**< true if value is supported; ignore if false */
119 uint64_t value; /**< raw value */
120};
121
122/**
123 * Holds information on a DRM property, including its ID and the enum
124 * values it holds.
125 *
126 * DRM properties are allocated dynamically, and maintained as DRM objects
127 * within the normal object ID space; they thus do not have a stable ID
128 * to refer to. This includes enum values, which must be referred to by
129 * integer values, but these are not stable.
130 *
131 * drm_property_info allows a cache to be maintained where Weston can use
132 * enum values internally to refer to properties, with the mapping to DRM
133 * ID values being maintained internally.
134 */
135struct drm_property_info {
136 const char *name; /**< name as string (static, not freed) */
137 uint32_t prop_id; /**< KMS property object ID */
138 unsigned int num_enum_values; /**< number of enum values */
139 struct drm_property_enum_info *enum_values; /**< array of enum values */
140};
141
Giulio Camuffo954f1832014-10-11 18:27:30 +0300142struct drm_backend {
143 struct weston_backend base;
144 struct weston_compositor *compositor;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400145
146 struct udev *udev;
147 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400148
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100149 struct udev_monitor *udev_monitor;
150 struct wl_event_source *udev_drm_source;
151
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100152 struct {
David Herrmannd7488c22012-03-11 20:05:21 +0100153 int id;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100154 int fd;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300155 char *filename;
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100156 } drm;
Benjamin Franzke060cf802011-04-30 09:32:11 +0200157 struct gbm_device *gbm;
Kristian Høgsberg61741a22013-09-17 16:02:57 -0700158 struct wl_listener session_listener;
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100159 uint32_t gbm_format;
Pekka Paalanenbce2d3f2011-12-02 13:07:27 +0200160
Rob Clark4339add2012-08-09 14:18:28 -0500161 /* we need these parameters in order to not fail drmModeAddFB2()
162 * due to out of bounds dimensions, and then mistakenly set
163 * sprites_are_broken:
164 */
Daniel Stonef214fdc2016-11-14 17:43:57 +0000165 int min_width, max_width;
166 int min_height, max_height;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200167 int no_addfb2;
Rob Clark4339add2012-08-09 14:18:28 -0500168
Daniel Stone085d2b92015-05-21 00:00:57 +0100169 struct wl_list plane_list;
Kristian Høgsberg65bec242012-03-05 19:57:35 -0500170 int sprites_are_broken;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +0200171 int sprites_hidden;
Jesse Barnes58ef3792012-02-23 09:45:49 -0500172
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000173 void *repaint_data;
174
Rob Clarkab5b1e32012-08-09 13:24:45 -0500175 int cursors_are_broken;
176
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100177 bool universal_planes;
178
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200179 int use_pixman;
180
Rob Bradfordd355b802013-05-31 18:09:55 +0100181 struct udev_input input;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -0300182
Daniel Stone70d337d2015-06-16 18:42:23 +0100183 int32_t cursor_width;
184 int32_t cursor_height;
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +0000185
186 uint32_t connector;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000187 uint32_t pageflip_timeout;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400188};
189
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400190struct drm_mode {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500191 struct weston_mode base;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400192 drmModeModeInfo mode_info;
193};
194
Daniel Stonefc175a72017-04-04 17:54:22 +0100195enum drm_fb_type {
196 BUFFER_INVALID = 0, /**< never used */
197 BUFFER_CLIENT, /**< directly sourced from client */
198 BUFFER_PIXMAN_DUMB, /**< internal Pixman rendering */
199 BUFFER_GBM_SURFACE, /**< internal EGL rendering */
Daniel Stonee4256832017-04-04 17:54:27 +0100200 BUFFER_CURSOR, /**< internal cursor buffer */
Daniel Stonefc175a72017-04-04 17:54:22 +0100201};
202
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300203struct drm_fb {
Daniel Stonefc175a72017-04-04 17:54:22 +0100204 enum drm_fb_type type;
205
Daniel Stone6e7a9612017-04-04 17:54:26 +0100206 int refcnt;
207
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200208 uint32_t fb_id, stride, handle, size;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100209 const struct pixel_format_info *format;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000210 int width, height;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200211 int fd;
Pekka Paalanende685b82012-12-04 15:58:12 +0200212 struct weston_buffer_reference buffer_ref;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200213
214 /* Used by gbm fbs */
215 struct gbm_bo *bo;
Daniel Stone05a5ac22017-04-04 17:54:25 +0100216 struct gbm_surface *gbm_surface;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200217
218 /* Used by dumb fbs */
219 void *map;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300220};
221
Richard Hughes2b2092a2013-04-24 14:58:02 +0100222struct drm_edid {
223 char eisa_id[13];
224 char monitor_name[13];
225 char pnp_id[5];
226 char serial_number[13];
227};
228
Daniel Stone08d4edf2017-04-04 17:54:34 +0100229/**
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000230 * Pending state holds one or more drm_output_state structures, collected from
231 * performing repaint. This pending state is transient, and only lives between
232 * beginning a repaint group and flushing the results: after flush, each
233 * output state will complete and be retired separately.
234 */
235struct drm_pending_state {
236 struct drm_backend *backend;
237};
238
239/**
Daniel Stone08d4edf2017-04-04 17:54:34 +0100240 * A plane represents one buffer, positioned within a CRTC, and stacked
241 * relative to other planes on the same CRTC.
242 *
243 * Each CRTC has a 'primary plane', which use used to display the classic
244 * framebuffer contents, as accessed through the legacy drmModeSetCrtc
245 * call (which combines setting the CRTC's actual physical mode, and the
246 * properties of the primary plane).
247 *
248 * The cursor plane also has its own alternate legacy API.
249 *
250 * Other planes are used opportunistically to display content we do not
251 * wish to blit into the primary plane. These non-primary/cursor planes
252 * are referred to as 'sprites'.
253 */
254struct drm_plane {
255 struct wl_list link;
256
257 struct weston_plane base;
258
259 struct drm_output *output;
260 struct drm_backend *backend;
261
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100262 enum wdrm_plane_type type;
263
Daniel Stone08d4edf2017-04-04 17:54:34 +0100264 uint32_t possible_crtcs;
265 uint32_t plane_id;
266 uint32_t count_formats;
267
Pekka Paalanenc5de57f2015-05-20 23:01:44 +0100268 struct drm_property_info props[WDRM_PLANE__COUNT];
269
Daniel Stone08d4edf2017-04-04 17:54:34 +0100270 /* The last framebuffer submitted to the kernel for this plane. */
271 struct drm_fb *fb_current;
272 /* The previously-submitted framebuffer, where the hardware has not
273 * yet acknowledged display of fb_current. */
274 struct drm_fb *fb_last;
275 /* Framebuffer we are going to submit to the kernel when the current
276 * repaint is flushed. */
277 struct drm_fb *fb_pending;
278
279 int32_t src_x, src_y;
280 uint32_t src_w, src_h;
281 uint32_t dest_x, dest_y;
282 uint32_t dest_w, dest_h;
283
284 uint32_t formats[];
285};
286
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400287struct drm_output {
Armin Krezović08368132016-09-30 14:11:05 +0200288 struct weston_output base;
289 drmModeConnector *connector;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400290
Daniel Stone17339232015-11-28 12:09:47 +0000291 uint32_t crtc_id; /* object ID to pass to DRM functions */
292 int pipe; /* index of CRTC in resource array / bitmasks */
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400293 uint32_t connector_id;
Matt Roper361d2ad2011-08-29 13:52:23 -0700294 drmModeCrtcPtr original_crtc;
Richard Hughes2b2092a2013-04-24 14:58:02 +0100295 struct drm_edid edid;
Daniel Stone02cf4662017-03-03 16:19:39 +0000296
297 /* Holds the properties for the connector */
298 struct drm_property_info props_conn[WDRM_CONNECTOR__COUNT];
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200299
Daniel Stone36609c72015-06-18 07:49:02 +0100300 enum dpms_enum dpms;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100301 struct backlight *backlight;
Daniel Stone36609c72015-06-18 07:49:02 +0100302
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +0300303 bool state_invalid;
304
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300305 int vblank_pending;
306 int page_flip_pending;
Xiong Zhangabd5d472013-10-11 14:43:07 +0800307 int destroy_pending;
Armin Krezović08368132016-09-30 14:11:05 +0200308 int disable_pending;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +0300309
Daniel Stonee4256832017-04-04 17:54:27 +0100310 struct drm_fb *gbm_cursor_fb[2];
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400311 struct weston_plane cursor_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500312 struct weston_view *cursor_view;
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400313 int current_cursor;
Daniel Stone5bb8f582017-04-04 17:54:28 +0100314
315 struct gbm_surface *gbm_surface;
316 uint32_t gbm_format;
317
Daniel Stone4202e502016-10-24 13:26:42 +0100318 /* Plane for a fullscreen direct scanout view */
319 struct weston_plane scanout_plane;
Daniel Stonef30a18c2017-04-04 17:54:31 +0100320
321 /* The last framebuffer submitted to the kernel for this CRTC. */
322 struct drm_fb *fb_current;
323 /* The previously-submitted framebuffer, where the hardware has not
324 * yet acknowledged display of fb_current. */
325 struct drm_fb *fb_last;
326 /* Framebuffer we are going to submit to the kernel when the current
327 * repaint is flushed. */
328 struct drm_fb *fb_pending;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +0200329
330 struct drm_fb *dumb[2];
331 pixman_image_t *image[2];
332 int current_image;
333 pixman_region32_t previous_damage;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +0300334
335 struct vaapi_recorder *recorder;
336 struct wl_listener recorder_frame_listener;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000337
338 struct wl_event_source *pageflip_timer;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400339};
340
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300341static struct gl_renderer_interface *gl_renderer;
342
Kristian Høgsberg98cfea62013-02-18 16:15:53 -0500343static const char default_seat[] = "seat0";
Pekka Paalanen33156972012-08-03 13:30:30 -0400344
Armin Krezović545dba62016-08-05 15:54:18 +0200345static inline struct drm_output *
346to_drm_output(struct weston_output *base)
347{
348 return container_of(base, struct drm_output, base);
349}
350
351static inline struct drm_backend *
352to_drm_backend(struct weston_compositor *base)
353{
354 return container_of(base->backend, struct drm_backend, base);
355}
356
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +0000357static int
358pageflip_timeout(void *data) {
359 /*
360 * Our timer just went off, that means we're not receiving drm
361 * page flip events anymore for that output. Let's gracefully exit
362 * weston with a return value so devs can debug what's going on.
363 */
364 struct drm_output *output = data;
365 struct weston_compositor *compositor = output->base.compositor;
366
367 weston_log("Pageflip timeout reached on output %s, your "
368 "driver is probably buggy! Exiting.\n",
369 output->base.name);
370 weston_compositor_exit_with_code(compositor, EXIT_FAILURE);
371
372 return 0;
373}
374
375/* Creates the pageflip timer. Note that it isn't armed by default */
376static int
377drm_output_pageflip_timer_create(struct drm_output *output)
378{
379 struct wl_event_loop *loop = NULL;
380 struct weston_compositor *ec = output->base.compositor;
381
382 loop = wl_display_get_event_loop(ec->wl_display);
383 assert(loop);
384 output->pageflip_timer = wl_event_loop_add_timer(loop,
385 pageflip_timeout,
386 output);
387
388 if (output->pageflip_timer == NULL) {
389 weston_log("creating drm pageflip timer failed: %m\n");
390 return -1;
391 }
392
393 return 0;
394}
395
Daniel Stone02cf4662017-03-03 16:19:39 +0000396/**
397 * Get the current value of a KMS property
398 *
399 * Given a drmModeObjectGetProperties return, as well as the drm_property_info
400 * for the target property, return the current value of that property,
401 * with an optional default. If the property is a KMS enum type, the return
402 * value will be translated into the appropriate internal enum.
403 *
404 * If the property is not present, the default value will be returned.
405 *
406 * @param info Internal structure for property to look up
407 * @param props Raw KMS properties for the target object
408 * @param def Value to return if property is not found
409 */
410static uint64_t
411drm_property_get_value(struct drm_property_info *info,
412 drmModeObjectPropertiesPtr props,
413 uint64_t def)
414{
415 unsigned int i;
416
417 if (info->prop_id == 0)
418 return def;
419
420 for (i = 0; i < props->count_props; i++) {
421 unsigned int j;
422
423 if (props->props[i] != info->prop_id)
424 continue;
425
426 /* Simple (non-enum) types can return the value directly */
427 if (info->num_enum_values == 0)
428 return props->prop_values[i];
429
430 /* Map from raw value to enum value */
431 for (j = 0; j < info->num_enum_values; j++) {
432 if (!info->enum_values[j].valid)
433 continue;
434 if (info->enum_values[j].value != props->prop_values[i])
435 continue;
436
437 return j;
438 }
439
440 /* We don't have a mapping for this enum; return default. */
441 break;
442 }
443
444 return def;
445}
446
447/**
448 * Cache DRM property values
449 *
450 * Update a per-object array of drm_property_info structures, given the
451 * DRM properties of the object.
452 *
453 * Call this every time an object newly appears (note that only connectors
454 * can be hotplugged), the first time it is seen, or when its status changes
455 * in a way which invalidates the potential property values (currently, the
456 * only case for this is connector hotplug).
457 *
458 * This updates the property IDs and enum values within the drm_property_info
459 * array.
460 *
461 * DRM property enum values are dynamic at runtime; the user must query the
462 * property to find out the desired runtime value for a requested string
463 * name. Using the 'type' field on planes as an example, there is no single
464 * hardcoded constant for primary plane types; instead, the property must be
465 * queried at runtime to find the value associated with the string "Primary".
466 *
467 * This helper queries and caches the enum values, to allow us to use a set
468 * of compile-time-constant enums portably across various implementations.
469 * The values given in enum_names are searched for, and stored in the
470 * same-indexed field of the map array.
471 *
472 * @param b DRM backend object
473 * @param src DRM property info array to source from
474 * @param info DRM property info array to copy into
475 * @param num_infos Number of entries in the source array
476 * @param props DRM object properties for the object
477 */
478static void
479drm_property_info_populate(struct drm_backend *b,
480 const struct drm_property_info *src,
481 struct drm_property_info *info,
482 unsigned int num_infos,
483 drmModeObjectProperties *props)
484{
485 drmModePropertyRes *prop;
486 unsigned i, j;
487
488 for (i = 0; i < num_infos; i++) {
489 unsigned int j;
490
491 info[i].name = src[i].name;
492 info[i].prop_id = 0;
493 info[i].num_enum_values = src[i].num_enum_values;
494
495 if (src[i].num_enum_values == 0)
496 continue;
497
498 info[i].enum_values =
499 malloc(src[i].num_enum_values *
500 sizeof(*info[i].enum_values));
501 assert(info[i].enum_values);
502 for (j = 0; j < info[i].num_enum_values; j++) {
503 info[i].enum_values[j].name = src[i].enum_values[j].name;
504 info[i].enum_values[j].valid = false;
505 }
506 }
507
508 for (i = 0; i < props->count_props; i++) {
509 unsigned int k;
510
511 prop = drmModeGetProperty(b->drm.fd, props->props[i]);
512 if (!prop)
513 continue;
514
515 for (j = 0; j < num_infos; j++) {
516 if (!strcmp(prop->name, info[j].name))
517 break;
518 }
519
520 /* We don't know/care about this property. */
521 if (j == num_infos) {
522#ifdef DEBUG
523 weston_log("DRM debug: unrecognized property %u '%s'\n",
524 prop->prop_id, prop->name);
525#endif
526 drmModeFreeProperty(prop);
527 continue;
528 }
529
530 if (info[j].num_enum_values == 0 &&
531 (prop->flags & DRM_MODE_PROP_ENUM)) {
532 weston_log("DRM: expected property %s to not be an"
533 " enum, but it is; ignoring\n", prop->name);
534 drmModeFreeProperty(prop);
535 continue;
536 }
537
538 info[j].prop_id = props->props[i];
539
540 if (info[j].num_enum_values == 0) {
541 drmModeFreeProperty(prop);
542 continue;
543 }
544
545 if (!(prop->flags & DRM_MODE_PROP_ENUM)) {
546 weston_log("DRM: expected property %s to be an enum,"
547 " but it is not; ignoring\n", prop->name);
548 drmModeFreeProperty(prop);
549 info[j].prop_id = 0;
550 continue;
551 }
552
553 for (k = 0; k < info[j].num_enum_values; k++) {
554 int l;
555
556 for (l = 0; l < prop->count_enums; l++) {
557 if (!strcmp(prop->enums[l].name,
558 info[j].enum_values[k].name))
559 break;
560 }
561
562 if (l == prop->count_enums)
563 continue;
564
565 info[j].enum_values[k].valid = true;
566 info[j].enum_values[k].value = prop->enums[l].value;
567 }
568
569 drmModeFreeProperty(prop);
570 }
571
572#ifdef DEBUG
573 for (i = 0; i < num_infos; i++) {
574 if (info[i].prop_id == 0)
575 weston_log("DRM warning: property '%s' missing\n",
576 info[i].name);
577 }
578#endif
579}
580
581/**
582 * Free DRM property information
583 *
584 * Frees all memory associated with a DRM property info array.
585 *
586 * @param info DRM property info array
587 * @param num_props Number of entries in array to free
588 */
589static void
590drm_property_info_free(struct drm_property_info *info, int num_props)
591{
592 int i;
593
594 for (i = 0; i < num_props; i++)
595 free(info[i].enum_values);
596}
597
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400598static void
599drm_output_set_cursor(struct drm_output *output);
Kristian Høgsberg5626d342012-08-03 11:50:05 -0400600
Mario Kleinerf507ec32015-06-21 21:25:14 +0200601static void
602drm_output_update_msc(struct drm_output *output, unsigned int seq);
603
Jesse Barnes58ef3792012-02-23 09:45:49 -0500604static int
Daniel Stone08d4edf2017-04-04 17:54:34 +0100605drm_plane_crtc_supported(struct drm_output *output, struct drm_plane *plane)
Jesse Barnes58ef3792012-02-23 09:45:49 -0500606{
Daniel Stone08d4edf2017-04-04 17:54:34 +0100607 return !!(plane->possible_crtcs & (1 << output->pipe));
Jesse Barnes58ef3792012-02-23 09:45:49 -0500608}
609
Daniel Stone72c0e1b2017-02-09 13:49:15 +0000610static struct drm_output *
611drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id)
612{
613 struct drm_output *output;
614
615 wl_list_for_each(output, &b->compositor->output_list, base.link) {
616 if (output->crtc_id == crtc_id)
617 return output;
618 }
619
620 wl_list_for_each(output, &b->compositor->pending_output_list,
621 base.link) {
622 if (output->crtc_id == crtc_id)
623 return output;
624 }
625
626 return NULL;
627}
628
Daniel Stonec0ec7592017-02-09 13:58:35 +0000629static struct drm_output *
630drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id)
631{
632 struct drm_output *output;
633
634 wl_list_for_each(output, &b->compositor->output_list, base.link) {
635 if (output->connector_id == connector_id)
636 return output;
637 }
638
639 wl_list_for_each(output, &b->compositor->pending_output_list,
640 base.link) {
641 if (output->connector_id == connector_id)
642 return output;
643 }
644
645 return NULL;
646}
647
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300648static void
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100649drm_fb_destroy(struct drm_fb *fb)
650{
651 if (fb->fb_id != 0)
652 drmModeRmFB(fb->fd, fb->fb_id);
653 weston_buffer_reference(&fb->buffer_ref, NULL);
654 free(fb);
655}
656
657static void
658drm_fb_destroy_dumb(struct drm_fb *fb)
659{
660 struct drm_mode_destroy_dumb destroy_arg;
661
662 assert(fb->type == BUFFER_PIXMAN_DUMB);
663
664 if (fb->map && fb->size > 0)
665 munmap(fb->map, fb->size);
666
667 memset(&destroy_arg, 0, sizeof(destroy_arg));
668 destroy_arg.handle = fb->handle;
669 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
670
671 drm_fb_destroy(fb);
672}
673
674static void
675drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300676{
677 struct drm_fb *fb = data;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300678
Daniel Stonee4256832017-04-04 17:54:27 +0100679 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
680 fb->type == BUFFER_CURSOR);
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100681 drm_fb_destroy(fb);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300682}
683
684static struct drm_fb *
Daniel Stonef214fdc2016-11-14 17:43:57 +0000685drm_fb_create_dumb(struct drm_backend *b, int width, int height,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300686 uint32_t format)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200687{
688 struct drm_fb *fb;
689 int ret;
690
691 struct drm_mode_create_dumb create_arg;
692 struct drm_mode_destroy_dumb destroy_arg;
693 struct drm_mode_map_dumb map_arg;
694
Peter Huttererf3d62272013-08-08 11:57:05 +1000695 fb = zalloc(sizeof *fb);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200696 if (!fb)
697 return NULL;
698
Daniel Stone6e7a9612017-04-04 17:54:26 +0100699 fb->refcnt = 1;
700
Daniel Stone0b70fa42017-04-04 17:54:23 +0100701 fb->format = pixel_format_get_info(format);
702 if (!fb->format) {
703 weston_log("failed to look up format 0x%lx\n",
704 (unsigned long) format);
705 goto err_fb;
706 }
707
708 if (!fb->format->depth || !fb->format->bpp) {
709 weston_log("format 0x%lx is not compatible with dumb buffers\n",
710 (unsigned long) format);
711 goto err_fb;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300712 }
713
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700714 memset(&create_arg, 0, sizeof create_arg);
Daniel Stone0b70fa42017-04-04 17:54:23 +0100715 create_arg.bpp = fb->format->bpp;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200716 create_arg.width = width;
717 create_arg.height = height;
718
Giulio Camuffo954f1832014-10-11 18:27:30 +0300719 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200720 if (ret)
721 goto err_fb;
722
Daniel Stonefc175a72017-04-04 17:54:22 +0100723 fb->type = BUFFER_PIXMAN_DUMB;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200724 fb->handle = create_arg.handle;
725 fb->stride = create_arg.pitch;
726 fb->size = create_arg.size;
Daniel Stonec8c917c2016-11-14 17:45:58 +0000727 fb->width = width;
728 fb->height = height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300729 fb->fd = b->drm.fd;
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200730
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300731 ret = -1;
732
733 if (!b->no_addfb2) {
Yong Bakos4b6321f2016-08-17 17:37:55 -0700734 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300735
736 handles[0] = fb->handle;
737 pitches[0] = fb->stride;
738 offsets[0] = 0;
739
740 ret = drmModeAddFB2(b->drm.fd, width, height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100741 fb->format->format,
742 handles, pitches, offsets,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300743 &fb->fb_id, 0);
744 if (ret) {
745 weston_log("addfb2 failed: %m\n");
746 b->no_addfb2 = 1;
747 }
748 }
749
750 if (ret) {
Daniel Stone0b70fa42017-04-04 17:54:23 +0100751 ret = drmModeAddFB(b->drm.fd, width, height,
752 fb->format->depth, fb->format->bpp,
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +0300753 fb->stride, fb->handle, &fb->fb_id);
754 }
755
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200756 if (ret)
757 goto err_bo;
758
Kristian Høgsbergac6104e2013-08-21 22:14:14 -0700759 memset(&map_arg, 0, sizeof map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200760 map_arg.handle = fb->handle;
Chris Michaeleb2074a2013-05-01 21:26:02 -0400761 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200762 if (ret)
763 goto err_add_fb;
764
Chris Michael4a7ce1f2015-11-10 10:40:37 -0500765 fb->map = mmap(NULL, fb->size, PROT_WRITE,
Giulio Camuffo954f1832014-10-11 18:27:30 +0300766 MAP_SHARED, b->drm.fd, map_arg.offset);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200767 if (fb->map == MAP_FAILED)
768 goto err_add_fb;
769
770 return fb;
771
772err_add_fb:
Giulio Camuffo954f1832014-10-11 18:27:30 +0300773 drmModeRmFB(b->drm.fd, fb->fb_id);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200774err_bo:
775 memset(&destroy_arg, 0, sizeof(destroy_arg));
776 destroy_arg.handle = create_arg.handle;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300777 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200778err_fb:
779 free(fb);
780 return NULL;
781}
782
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200783static struct drm_fb *
Daniel Stone6e7a9612017-04-04 17:54:26 +0100784drm_fb_ref(struct drm_fb *fb)
785{
786 fb->refcnt++;
787 return fb;
788}
789
790static struct drm_fb *
Daniel Stonefc175a72017-04-04 17:54:22 +0100791drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
792 uint32_t format, enum drm_fb_type type)
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300793{
794 struct drm_fb *fb = gbm_bo_get_user_data(bo);
Derek Foreman482ffdf2016-07-08 12:50:57 -0500795 uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300796 int ret;
797
Daniel Stonefc175a72017-04-04 17:54:22 +0100798 if (fb) {
799 assert(fb->type == type);
Daniel Stone6e7a9612017-04-04 17:54:26 +0100800 return drm_fb_ref(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100801 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300802
Bryce Harringtonde16d892014-11-20 22:21:57 -0800803 fb = zalloc(sizeof *fb);
804 if (fb == NULL)
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200805 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300806
Daniel Stonefc175a72017-04-04 17:54:22 +0100807 fb->type = type;
Daniel Stone6e7a9612017-04-04 17:54:26 +0100808 fb->refcnt = 1;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300809 fb->bo = bo;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300810
Daniel Stonec8c917c2016-11-14 17:45:58 +0000811 fb->width = gbm_bo_get_width(bo);
812 fb->height = gbm_bo_get_height(bo);
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200813 fb->stride = gbm_bo_get_stride(bo);
814 fb->handle = gbm_bo_get_handle(bo).u32;
Daniel Stone0b70fa42017-04-04 17:54:23 +0100815 fb->format = pixel_format_get_info(format);
Daniel Stonec8c917c2016-11-14 17:45:58 +0000816 fb->size = fb->stride * fb->height;
Giulio Camuffo954f1832014-10-11 18:27:30 +0300817 fb->fd = backend->drm.fd;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300818
Daniel Stone0b70fa42017-04-04 17:54:23 +0100819 if (!fb->format) {
820 weston_log("couldn't look up format 0x%lx\n",
821 (unsigned long) format);
822 goto err_free;
823 }
824
Daniel Stonec8c917c2016-11-14 17:45:58 +0000825 if (backend->min_width > fb->width ||
826 fb->width > backend->max_width ||
827 backend->min_height > fb->height ||
828 fb->height > backend->max_height) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200829 weston_log("bo geometry out of bounds\n");
830 goto err_free;
831 }
832
833 ret = -1;
834
Giulio Camuffo954f1832014-10-11 18:27:30 +0300835 if (format && !backend->no_addfb2) {
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +0200836 handles[0] = fb->handle;
837 pitches[0] = fb->stride;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200838 offsets[0] = 0;
839
Daniel Stonec8c917c2016-11-14 17:45:58 +0000840 ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200841 format, handles, pitches, offsets,
842 &fb->fb_id, 0);
843 if (ret) {
844 weston_log("addfb2 failed: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +0300845 backend->no_addfb2 = 1;
846 backend->sprites_are_broken = 1;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200847 }
848 }
849
Daniel Stone0b70fa42017-04-04 17:54:23 +0100850 if (ret && fb->format->depth && fb->format->bpp)
Daniel Stonec8c917c2016-11-14 17:45:58 +0000851 ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
Daniel Stone0b70fa42017-04-04 17:54:23 +0100852 fb->format->depth, fb->format->bpp,
853 fb->stride, fb->handle, &fb->fb_id);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200854
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300855 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +0200856 weston_log("failed to create kms fb: %m\n");
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200857 goto err_free;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300858 }
859
Tomohito Esaki576f42e2017-04-04 17:54:24 +0100860 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300861
862 return fb;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200863
864err_free:
865 free(fb);
866 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300867}
868
869static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -0500870drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200871{
Pekka Paalanende685b82012-12-04 15:58:12 +0200872 assert(fb->buffer_ref.buffer == NULL);
Daniel Stonefc175a72017-04-04 17:54:22 +0100873 assert(fb->type == BUFFER_CLIENT);
Pekka Paalanende685b82012-12-04 15:58:12 +0200874 weston_buffer_reference(&fb->buffer_ref, buffer);
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +0200875}
876
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200877static void
Daniel Stone05a5ac22017-04-04 17:54:25 +0100878drm_fb_unref(struct drm_fb *fb)
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200879{
880 if (!fb)
881 return;
882
Daniel Stone6e7a9612017-04-04 17:54:26 +0100883 assert(fb->refcnt > 0);
884 if (--fb->refcnt > 0)
885 return;
886
Daniel Stonefc175a72017-04-04 17:54:22 +0100887 switch (fb->type) {
888 case BUFFER_PIXMAN_DUMB:
Daniel Stone6e7a9612017-04-04 17:54:26 +0100889 drm_fb_destroy_dumb(fb);
Daniel Stonefc175a72017-04-04 17:54:22 +0100890 break;
Daniel Stonee4256832017-04-04 17:54:27 +0100891 case BUFFER_CURSOR:
Daniel Stonefc175a72017-04-04 17:54:22 +0100892 case BUFFER_CLIENT:
893 gbm_bo_destroy(fb->bo);
894 break;
895 case BUFFER_GBM_SURFACE:
Daniel Stone05a5ac22017-04-04 17:54:25 +0100896 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
Daniel Stonefc175a72017-04-04 17:54:22 +0100897 break;
898 default:
899 assert(NULL);
900 break;
Ander Conselvan de Oliveira526d4612013-01-25 15:13:03 +0200901 }
902}
903
Daniel Stone90648872016-10-21 18:08:37 +0100904static int
905drm_view_transform_supported(struct weston_view *ev)
906{
907 return !ev->transform.enabled ||
908 (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
909}
910
Daniel Stoneeedf84c2017-02-10 18:06:04 +0000911/**
912 * Allocate a new drm_pending_state
913 *
914 * Allocate a new, empty, 'pending state' structure to be used across a
915 * repaint cycle or similar.
916 *
917 * @param backend DRM backend
918 * @returns Newly-allocated pending state structure
919 */
920static struct drm_pending_state *
921drm_pending_state_alloc(struct drm_backend *backend)
922{
923 struct drm_pending_state *ret;
924
925 ret = calloc(1, sizeof(*ret));
926 if (!ret)
927 return NULL;
928
929 ret->backend = backend;
930
931 return ret;
932}
933
934/**
935 * Free a drm_pending_state structure
936 *
937 * Frees a pending_state structure.
938 *
939 * @param pending_state Pending state structure to free
940 */
941static void
942drm_pending_state_free(struct drm_pending_state *pending_state)
943{
944 if (!pending_state)
945 return;
946
947 free(pending_state);
948}
949
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500950static uint32_t
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200951drm_output_check_scanout_format(struct drm_output *output,
952 struct weston_surface *es, struct gbm_bo *bo)
953{
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200954 uint32_t format;
955 pixman_region32_t r;
956
957 format = gbm_bo_get_format(bo);
958
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700959 if (format == GBM_FORMAT_ARGB8888) {
960 /* We can scanout an ARGB buffer if the surface's
961 * opaque region covers the whole output, but we have
962 * to use XRGB as the KMS format code. */
Kristian Høgsberg1be87e32014-01-17 14:22:41 -0800963 pixman_region32_init_rect(&r, 0, 0,
964 output->base.width,
965 output->base.height);
966 pixman_region32_subtract(&r, &r, &es->opaque);
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200967
968 if (!pixman_region32_not_empty(&r))
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500969 format = GBM_FORMAT_XRGB8888;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200970
971 pixman_region32_fini(&r);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500972 }
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700973
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +0100974 if (output->gbm_format == format)
Kristian Høgsberg3f97b342013-10-16 16:08:57 -0700975 return format;
976
977 return 0;
Ander Conselvan de Oliveirae9209412012-11-30 17:34:22 +0200978}
979
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400980static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +0200981drm_output_prepare_scanout_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 struct weston_view *ev)
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500983{
Armin Krezović545dba62016-08-05 15:54:18 +0200984 struct drm_backend *b = to_drm_backend(output->base.compositor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200986 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +0300987 struct gbm_bo *bo;
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -0500988 uint32_t format;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500989
Daniel Stone90648872016-10-21 18:08:37 +0100990 /* Don't import buffers which span multiple outputs. */
991 if (ev->output_mask != (1u << output->base.id))
992 return NULL;
993
Daniel Stone296d7a92016-10-21 18:05:37 +0100994 /* We use GBM to import buffers. */
995 if (b->gbm == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400996 return NULL;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -0500997
Daniel Stone296d7a92016-10-21 18:05:37 +0100998 if (buffer == NULL)
999 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001000 if (wl_shm_buffer_get(buffer->resource))
1001 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001002
1003 /* Make sure our view is exactly compatible with the output. */
1004 if (ev->geometry.x != output->base.x ||
1005 ev->geometry.y != output->base.y)
1006 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001007 if (buffer->width != output->base.current_mode->width ||
1008 buffer->height != output->base.current_mode->height)
1009 return NULL;
1010
Daniel Stone296d7a92016-10-21 18:05:37 +01001011 if (ev->transform.enabled)
1012 return NULL;
Pekka Paalanen5580f222015-02-17 16:33:18 +02001013 if (ev->geometry.scissor_enabled)
1014 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001015 if (viewport->buffer.transform != output->base.transform)
1016 return NULL;
Daniel Stone90648872016-10-21 18:08:37 +01001017 if (viewport->buffer.scale != output->base.current_scale)
1018 return NULL;
1019 if (!drm_view_transform_supported(ev))
1020 return NULL;
1021
1022 if (ev->alpha != 1.0f)
1023 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001024
Giulio Camuffo954f1832014-10-11 18:27:30 +03001025 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
Kristian Høgsberg63996462013-09-03 22:27:08 -07001026 buffer->resource, GBM_BO_USE_SCANOUT);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001027
Rob Bradford9b101872012-09-14 23:25:41 +01001028 /* Unable to use the buffer for scanout */
1029 if (!bo)
1030 return NULL;
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 format = drm_output_check_scanout_format(output, ev->surface, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001033 if (format == 0) {
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001034 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001035 return NULL;
Ander Conselvan de Oliveiraa64b15d2012-05-02 16:42:22 +03001036 }
1037
Daniel Stone5bb8f582017-04-04 17:54:28 +01001038 output->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1039 if (!output->fb_pending) {
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001040 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001041 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001042 }
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001043
Daniel Stone5bb8f582017-04-04 17:54:28 +01001044 drm_fb_set_buffer(output->fb_pending, buffer);
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001045
Daniel Stone4202e502016-10-24 13:26:42 +01001046 return &output->scanout_plane;
Kristian Høgsberg5f5e42e2012-01-25 23:59:42 -05001047}
1048
Daniel Stone95d48a22017-04-04 17:54:30 +01001049static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001050drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001051{
Armin Krezović545dba62016-08-05 15:54:18 +02001052 struct drm_backend *b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001053 struct gbm_bo *bo;
Daniel Stone95d48a22017-04-04 17:54:30 +01001054 struct drm_fb *ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001055
Giulio Camuffo954f1832014-10-11 18:27:30 +03001056 output->base.compositor->renderer->repaint_output(&output->base,
1057 damage);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001058
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001059 bo = gbm_surface_lock_front_buffer(output->gbm_surface);
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001060 if (!bo) {
Martin Minarik6d118362012-06-07 18:01:59 +02001061 weston_log("failed to lock front buffer: %m\n");
Daniel Stone95d48a22017-04-04 17:54:30 +01001062 return NULL;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001063 }
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001064
Daniel Stone95d48a22017-04-04 17:54:30 +01001065 ret = drm_fb_get_from_bo(bo, b, output->gbm_format, BUFFER_GBM_SURFACE);
1066 if (!ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001067 weston_log("failed to get drm_fb for bo\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01001068 gbm_surface_release_buffer(output->gbm_surface, bo);
Daniel Stone95d48a22017-04-04 17:54:30 +01001069 return NULL;
Ander Conselvan de Oliveira555c17d2012-05-02 16:42:21 +03001070 }
Daniel Stone95d48a22017-04-04 17:54:30 +01001071 ret->gbm_surface = output->gbm_surface;
1072
1073 return ret;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001074}
1075
Daniel Stone95d48a22017-04-04 17:54:30 +01001076static struct drm_fb *
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001077drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
1078{
1079 struct weston_compositor *ec = output->base.compositor;
1080 pixman_region32_t total_damage, previous_damage;
1081
1082 pixman_region32_init(&total_damage);
1083 pixman_region32_init(&previous_damage);
1084
1085 pixman_region32_copy(&previous_damage, damage);
1086
1087 pixman_region32_union(&total_damage, damage, &output->previous_damage);
1088 pixman_region32_copy(&output->previous_damage, &previous_damage);
1089
1090 output->current_image ^= 1;
1091
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001092 pixman_renderer_output_set_buffer(&output->base,
1093 output->image[output->current_image]);
1094
1095 ec->renderer->repaint_output(&output->base, &total_damage);
1096
1097 pixman_region32_fini(&total_damage);
1098 pixman_region32_fini(&previous_damage);
Daniel Stone95d48a22017-04-04 17:54:30 +01001099
1100 return drm_fb_ref(output->dumb[output->current_image]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001101}
1102
1103static void
1104drm_output_render(struct drm_output *output, pixman_region32_t *damage)
1105{
Giulio Camuffo954f1832014-10-11 18:27:30 +03001106 struct weston_compositor *c = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001107 struct drm_backend *b = to_drm_backend(c);
Daniel Stone95d48a22017-04-04 17:54:30 +01001108 struct drm_fb *fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001109
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001110 /* If we already have a client buffer promoted to scanout, then we don't
1111 * want to render. */
1112 if (output->fb_pending)
1113 return;
1114
Giulio Camuffo954f1832014-10-11 18:27:30 +03001115 if (b->use_pixman)
Daniel Stone95d48a22017-04-04 17:54:30 +01001116 fb = drm_output_render_pixman(output, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001117 else
Daniel Stone95d48a22017-04-04 17:54:30 +01001118 fb = drm_output_render_gl(output, damage);
1119
1120 if (!fb)
1121 return;
1122 output->fb_pending = fb;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001123
Giulio Camuffo954f1832014-10-11 18:27:30 +03001124 pixman_region32_subtract(&c->primary_plane.damage,
1125 &c->primary_plane.damage, damage);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001126}
1127
1128static void
Richard Hughese7299962013-05-01 21:52:12 +01001129drm_output_set_gamma(struct weston_output *output_base,
1130 uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
1131{
1132 int rc;
Armin Krezović545dba62016-08-05 15:54:18 +02001133 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001134 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001135 to_drm_backend(output->base.compositor);
Richard Hughese7299962013-05-01 21:52:12 +01001136
1137 /* check */
1138 if (output_base->gamma_size != size)
1139 return;
1140 if (!output->original_crtc)
1141 return;
1142
Giulio Camuffo954f1832014-10-11 18:27:30 +03001143 rc = drmModeCrtcSetGamma(backend->drm.fd,
Richard Hughese7299962013-05-01 21:52:12 +01001144 output->crtc_id,
1145 size, r, g, b);
1146 if (rc)
1147 weston_log("set gamma failed: %m\n");
1148}
1149
Bryce Harringtonada4f072015-06-30 13:25:46 -07001150/* Determine the type of vblank synchronization to use for the output.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001151 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001152 * The pipe parameter indicates which CRTC is in use. Knowing this, we
1153 * can determine which vblank sequence type to use for it. Traditional
1154 * cards had only two CRTCs, with CRTC 0 using no special flags, and
1155 * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
1156 * parameter indicates this.
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001157 *
Bryce Harringtonada4f072015-06-30 13:25:46 -07001158 * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
1159 * 0-31. If this is non-zero it indicates we're dealing with a
1160 * multi-gpu situation and we need to calculate the vblank sync
1161 * using DRM_BLANK_HIGH_CRTC_MASK.
1162 */
Pekka Paalanenc8a1ff02015-07-02 15:06:08 +03001163static unsigned int
1164drm_waitvblank_pipe(struct drm_output *output)
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001165{
1166 if (output->pipe > 1)
1167 return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
1168 DRM_VBLANK_HIGH_CRTC_MASK;
1169 else if (output->pipe > 0)
1170 return DRM_VBLANK_SECONDARY;
1171 else
1172 return 0;
1173}
1174
David Herrmann1edf44c2013-10-22 17:11:26 +02001175static int
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001176drm_output_repaint(struct weston_output *output_base,
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001177 pixman_region32_t *damage,
1178 void *repaint_data)
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001179{
Armin Krezović545dba62016-08-05 15:54:18 +02001180 struct drm_output *output = to_drm_output(output_base);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001181 struct drm_backend *backend =
Armin Krezović545dba62016-08-05 15:54:18 +02001182 to_drm_backend(output->base.compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01001183 struct drm_plane *p;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001184 struct drm_mode *mode;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001185 int ret = 0;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001186
Armin Krezović08368132016-09-30 14:11:05 +02001187 if (output->disable_pending || output->destroy_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001188 return -1;
Xiong Zhangabd5d472013-10-11 14:43:07 +08001189
Daniel Stonef30a18c2017-04-04 17:54:31 +01001190 assert(!output->fb_last);
1191
Derek Foreman2cd87fe2017-04-13 13:48:48 -05001192 /* If disable_planes is set then assign_planes() wasn't
1193 * called for this render, so we could still have a stale
1194 * cursor plane set up.
1195 */
1196 if (output->base.disable_planes) {
1197 output->cursor_view = NULL;
1198 output->cursor_plane.x = INT32_MIN;
1199 output->cursor_plane.y = INT32_MIN;
1200 }
1201
Daniel Stone4e84f7d2017-04-04 17:54:29 +01001202 drm_output_render(output, damage);
Daniel Stone5bb8f582017-04-04 17:54:28 +01001203 if (!output->fb_pending)
David Herrmann1edf44c2013-10-22 17:11:26 +02001204 return -1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001205
Hardeningff39efa2013-09-18 23:56:35 +02001206 mode = container_of(output->base.current_mode, struct drm_mode, base);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001207 if (output->state_invalid || !output->fb_current ||
Daniel Stone5bb8f582017-04-04 17:54:28 +01001208 output->fb_current->stride != output->fb_pending->stride) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001209 ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001210 output->fb_pending->fb_id, 0, 0,
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001211 &output->connector_id, 1,
1212 &mode->mode_info);
1213 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001214 weston_log("set mode failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001215 goto err_pageflip;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001216 }
Ander Conselvan de Oliveira722a2d52013-06-04 16:24:05 +03001217 output_base->set_dpms(output_base, WESTON_DPMS_ON);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001218
1219 output->state_invalid = false;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001220 }
1221
Giulio Camuffo954f1832014-10-11 18:27:30 +03001222 if (drmModePageFlip(backend->drm.fd, output->crtc_id,
Daniel Stone5bb8f582017-04-04 17:54:28 +01001223 output->fb_pending->fb_id,
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001224 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02001225 weston_log("queueing pageflip failed: %m\n");
David Herrmann1edf44c2013-10-22 17:11:26 +02001226 goto err_pageflip;
Kristian Høgsberg54f14c32012-01-18 11:47:41 -05001227 }
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001228
Daniel Stonef30a18c2017-04-04 17:54:31 +01001229 output->fb_last = output->fb_current;
1230 output->fb_current = output->fb_pending;
1231 output->fb_pending = NULL;
1232
Daniel Stone205c0a02017-04-04 17:54:33 +01001233 assert(!output->page_flip_pending);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001234 output->page_flip_pending = 1;
1235
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001236 if (output->pageflip_timer)
1237 wl_event_source_timer_update(output->pageflip_timer,
1238 backend->pageflip_timeout);
1239
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001240 drm_output_set_cursor(output);
1241
Jesse Barnes58ef3792012-02-23 09:45:49 -05001242 /*
1243 * Now, update all the sprite surfaces
1244 */
Daniel Stone085d2b92015-05-21 00:00:57 +01001245 wl_list_for_each(p, &backend->plane_list, link) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001246 uint32_t flags = 0, fb_id = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001247 drmVBlank vbl = {
1248 .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1249 .request.sequence = 1,
1250 };
1251
Daniel Stone085d2b92015-05-21 00:00:57 +01001252 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001253 continue;
1254
Daniel Stone085d2b92015-05-21 00:00:57 +01001255 if ((!p->fb_current && !p->fb_pending) ||
1256 !drm_plane_crtc_supported(output, p))
1257 continue;
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001258
Daniel Stone085d2b92015-05-21 00:00:57 +01001259 if (p->fb_pending && !backend->sprites_hidden)
1260 fb_id = p->fb_pending->fb_id;
1261
1262 ret = drmModeSetPlane(backend->drm.fd, p->plane_id,
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001263 output->crtc_id, fb_id, flags,
Daniel Stone085d2b92015-05-21 00:00:57 +01001264 p->dest_x, p->dest_y,
1265 p->dest_w, p->dest_h,
1266 p->src_x, p->src_y,
1267 p->src_w, p->src_h);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001268 if (ret)
Martin Minarik6d118362012-06-07 18:01:59 +02001269 weston_log("setplane failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001270 ret, strerror(errno));
1271
Mario Kleiner2ab4f4e2015-06-21 21:25:13 +02001272 vbl.request.type |= drm_waitvblank_pipe(output);
Rob Clark5ca1a472012-08-08 20:27:37 -05001273
Jesse Barnes58ef3792012-02-23 09:45:49 -05001274 /*
1275 * Queue a vblank signal so we know when the surface
1276 * becomes active on the display or has been replaced.
1277 */
Daniel Stone085d2b92015-05-21 00:00:57 +01001278 vbl.request.signal = (unsigned long) p;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001279 ret = drmWaitVBlank(backend->drm.fd, &vbl);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001280 if (ret) {
Martin Minarik6d118362012-06-07 18:01:59 +02001281 weston_log("vblank event request failed: %d: %s\n",
Jesse Barnes58ef3792012-02-23 09:45:49 -05001282 ret, strerror(errno));
1283 }
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001284
Daniel Stone085d2b92015-05-21 00:00:57 +01001285 p->output = output;
1286 p->fb_last = p->fb_current;
1287 p->fb_current = p->fb_pending;
1288 p->fb_pending = NULL;
Daniel Stone65d87d02017-04-04 17:54:32 +01001289 output->vblank_pending++;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001290 }
1291
David Herrmann1edf44c2013-10-22 17:11:26 +02001292 return 0;
1293
1294err_pageflip:
Kristian Høgsbergb3955b02014-01-23 16:25:06 -08001295 output->cursor_view = NULL;
Daniel Stone5bb8f582017-04-04 17:54:28 +01001296 if (output->fb_pending) {
1297 drm_fb_unref(output->fb_pending);
1298 output->fb_pending = NULL;
David Herrmann1edf44c2013-10-22 17:11:26 +02001299 }
1300
1301 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001302}
1303
1304static void
Jonas Ådahle5a12252013-04-05 23:07:11 +02001305drm_output_start_repaint_loop(struct weston_output *output_base)
1306{
Armin Krezović545dba62016-08-05 15:54:18 +02001307 struct drm_output *output = to_drm_output(output_base);
1308 struct drm_backend *backend =
1309 to_drm_backend(output_base->compositor);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001310 uint32_t fb_id;
Mario Kleinerf507ec32015-06-21 21:25:14 +02001311 struct timespec ts, tnow;
1312 struct timespec vbl2now;
1313 int64_t refresh_nsec;
1314 int ret;
1315 drmVBlank vbl = {
1316 .request.type = DRM_VBLANK_RELATIVE,
1317 .request.sequence = 0,
1318 .request.signal = 0,
1319 };
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001320
Armin Krezović08368132016-09-30 14:11:05 +02001321 if (output->disable_pending || output->destroy_pending)
Xiong Zhangabd5d472013-10-11 14:43:07 +08001322 return;
1323
Daniel Stone5bb8f582017-04-04 17:54:28 +01001324 if (!output->fb_current) {
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001325 /* We can't page flip if there's no mode set */
David Herrmann3c688c52013-10-22 17:11:25 +02001326 goto finish_frame;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03001327 }
1328
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03001329 /* Need to smash all state in from scratch; current timings might not
1330 * be what we want, page flip might not work, etc.
1331 */
1332 if (output->state_invalid)
1333 goto finish_frame;
1334
Mario Kleinerf507ec32015-06-21 21:25:14 +02001335 /* Try to get current msc and timestamp via instant query */
1336 vbl.request.type |= drm_waitvblank_pipe(output);
1337 ret = drmWaitVBlank(backend->drm.fd, &vbl);
1338
1339 /* Error ret or zero timestamp means failure to get valid timestamp */
1340 if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
1341 ts.tv_sec = vbl.reply.tval_sec;
1342 ts.tv_nsec = vbl.reply.tval_usec * 1000;
1343
1344 /* Valid timestamp for most recent vblank - not stale?
1345 * Stale ts could happen on Linux 3.17+, so make sure it
1346 * is not older than 1 refresh duration since now.
1347 */
1348 weston_compositor_read_presentation_clock(backend->compositor,
1349 &tnow);
1350 timespec_sub(&vbl2now, &tnow, &ts);
1351 refresh_nsec =
1352 millihz_to_nsec(output->base.current_mode->refresh);
1353 if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
1354 drm_output_update_msc(output, vbl.reply.sequence);
1355 weston_output_finish_frame(output_base, &ts,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001356 WP_PRESENTATION_FEEDBACK_INVALID);
Mario Kleinerf507ec32015-06-21 21:25:14 +02001357 return;
1358 }
1359 }
1360
1361 /* Immediate query didn't provide valid timestamp.
1362 * Use pageflip fallback.
1363 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01001364 fb_id = output->fb_current->fb_id;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001365
Daniel Stone205c0a02017-04-04 17:54:33 +01001366 assert(!output->page_flip_pending);
1367 assert(!output->fb_last);
1368
Giulio Camuffo954f1832014-10-11 18:27:30 +03001369 if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
Jonas Ådahle5a12252013-04-05 23:07:11 +02001370 DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
1371 weston_log("queueing pageflip failed: %m\n");
David Herrmann3c688c52013-10-22 17:11:25 +02001372 goto finish_frame;
Jonas Ådahle5a12252013-04-05 23:07:11 +02001373 }
David Herrmann3c688c52013-10-22 17:11:25 +02001374
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001375 if (output->pageflip_timer)
1376 wl_event_source_timer_update(output->pageflip_timer,
1377 backend->pageflip_timeout);
1378
Daniel Stone205c0a02017-04-04 17:54:33 +01001379 output->fb_last = drm_fb_ref(output->fb_current);
1380 output->page_flip_pending = 1;
1381
David Herrmann3c688c52013-10-22 17:11:25 +02001382 return;
1383
1384finish_frame:
1385 /* if we cannot page-flip, immediately finish frame */
Daniel Stone3615ce12017-03-01 11:34:05 +00001386 weston_output_finish_frame(output_base, NULL,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001387 WP_PRESENTATION_FEEDBACK_INVALID);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001388}
1389
1390static void
Pekka Paalanen641307c2014-09-23 22:08:47 -04001391drm_output_update_msc(struct drm_output *output, unsigned int seq)
1392{
1393 uint64_t msc_hi = output->base.msc >> 32;
1394
1395 if (seq < (output->base.msc & 0xffffffff))
1396 msc_hi++;
1397
1398 output->base.msc = (msc_hi << 32) + seq;
1399}
1400
1401static void
Jesse Barnes58ef3792012-02-23 09:45:49 -05001402vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
1403 void *data)
1404{
Daniel Stone08d4edf2017-04-04 17:54:34 +01001405 struct drm_plane *s = (struct drm_plane *)data;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001406 struct drm_output *output = s->output;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001407 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001408 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1409 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001410
Pekka Paalanen641307c2014-09-23 22:08:47 -04001411 drm_output_update_msc(output, frame);
Daniel Stone65d87d02017-04-04 17:54:32 +01001412 output->vblank_pending--;
1413 assert(output->vblank_pending >= 0);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001414
Daniel Stonef30a18c2017-04-04 17:54:31 +01001415 assert(s->fb_last || s->fb_current);
1416 drm_fb_unref(s->fb_last);
1417 s->fb_last = NULL;
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001418
Daniel Stone65d87d02017-04-04 17:54:32 +01001419 if (!output->page_flip_pending && !output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001420 /* Stop the pageflip timer instead of rearming it here */
1421 if (output->pageflip_timer)
1422 wl_event_source_timer_update(output->pageflip_timer, 0);
1423
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001424 ts.tv_sec = sec;
1425 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001426 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001427 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001428}
1429
1430static void
Armin Krezović08368132016-09-30 14:11:05 +02001431drm_output_destroy(struct weston_output *base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001432
1433static void
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001434page_flip_handler(int fd, unsigned int frame,
1435 unsigned int sec, unsigned int usec, void *data)
1436{
Armin Krezović545dba62016-08-05 15:54:18 +02001437 struct drm_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001438 struct timespec ts;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001439 uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
1440 WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
1441 WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001442
Pekka Paalanen641307c2014-09-23 22:08:47 -04001443 drm_output_update_msc(output, frame);
1444
Daniel Stone205c0a02017-04-04 17:54:33 +01001445 assert(output->page_flip_pending);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001446 output->page_flip_pending = 0;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04001447
Daniel Stone205c0a02017-04-04 17:54:33 +01001448 drm_fb_unref(output->fb_last);
1449 output->fb_last = NULL;
1450
Xiong Zhangabd5d472013-10-11 14:43:07 +08001451 if (output->destroy_pending)
1452 drm_output_destroy(&output->base);
Armin Krezović08368132016-09-30 14:11:05 +02001453 else if (output->disable_pending)
1454 weston_output_disable(&output->base);
Xiong Zhangabd5d472013-10-11 14:43:07 +08001455 else if (!output->vblank_pending) {
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00001456 /* Stop the pageflip timer instead of rearming it here */
1457 if (output->pageflip_timer)
1458 wl_event_source_timer_update(output->pageflip_timer, 0);
1459
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001460 ts.tv_sec = sec;
1461 ts.tv_nsec = usec * 1000;
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02001462 weston_output_finish_frame(&output->base, &ts, flags);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03001463
1464 /* We can't call this from frame_notify, because the output's
1465 * repaint needed flag is cleared just after that */
1466 if (output->recorder)
1467 weston_output_schedule_repaint(&output->base);
Ander Conselvan de Oliveiraa7326962012-06-26 17:09:13 +03001468 }
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001469}
1470
Daniel Stoneeedf84c2017-02-10 18:06:04 +00001471/**
1472 * Begin a new repaint cycle
1473 *
1474 * Called by the core compositor at the beginning of a repaint cycle.
1475 */
1476static void *
1477drm_repaint_begin(struct weston_compositor *compositor)
1478{
1479 struct drm_backend *b = to_drm_backend(compositor);
1480 struct drm_pending_state *ret;
1481
1482 ret = drm_pending_state_alloc(b);
1483 b->repaint_data = ret;
1484
1485 return ret;
1486}
1487
1488/**
1489 * Flush a repaint set
1490 *
1491 * Called by the core compositor when a repaint cycle has been completed
1492 * and should be flushed.
1493 */
1494static void
1495drm_repaint_flush(struct weston_compositor *compositor, void *repaint_data)
1496{
1497 struct drm_backend *b = to_drm_backend(compositor);
1498 struct drm_pending_state *pending_state = repaint_data;
1499
1500 drm_pending_state_free(pending_state);
1501 b->repaint_data = NULL;
1502}
1503
1504/**
1505 * Cancel a repaint set
1506 *
1507 * Called by the core compositor when a repaint has finished, so the data
1508 * held across the repaint cycle should be discarded.
1509 */
1510static void
1511drm_repaint_cancel(struct weston_compositor *compositor, void *repaint_data)
1512{
1513 struct drm_backend *b = to_drm_backend(compositor);
1514 struct drm_pending_state *pending_state = repaint_data;
1515
1516 drm_pending_state_free(pending_state);
1517 b->repaint_data = NULL;
1518}
1519
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001520static uint32_t
Daniel Stone08d4edf2017-04-04 17:54:34 +01001521drm_output_check_plane_format(struct drm_plane *p,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 struct weston_view *ev, struct gbm_bo *bo)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001523{
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001524 uint32_t i, format;
1525
1526 format = gbm_bo_get_format(bo);
1527
1528 if (format == GBM_FORMAT_ARGB8888) {
1529 pixman_region32_t r;
1530
Kristian Høgsberg63093a32013-03-01 14:29:16 -05001531 pixman_region32_init_rect(&r, 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001532 ev->surface->width,
1533 ev->surface->height);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 pixman_region32_subtract(&r, &r, &ev->surface->opaque);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001535
1536 if (!pixman_region32_not_empty(&r))
1537 format = GBM_FORMAT_XRGB8888;
1538
1539 pixman_region32_fini(&r);
1540 }
Jesse Barnes58ef3792012-02-23 09:45:49 -05001541
Daniel Stone08d4edf2017-04-04 17:54:34 +01001542 for (i = 0; i < p->count_formats; i++)
1543 if (p->formats[i] == format)
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001544 return format;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001545
1546 return 0;
1547}
1548
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001549static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001550drm_output_prepare_overlay_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 struct weston_view *ev)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001552{
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001553 struct weston_compositor *ec = output->base.compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02001554 struct drm_backend *b = to_drm_backend(ec);
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001555 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001556 struct wl_resource *buffer_resource;
Daniel Stone08d4edf2017-04-04 17:54:34 +01001557 struct drm_plane *p;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001558 struct linux_dmabuf_buffer *dmabuf;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001559 int found = 0;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001560 struct gbm_bo *bo;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001561 pixman_region32_t dest_rect, src_rect;
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001562 pixman_box32_t *box, tbox;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001563 uint32_t format;
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001564 wl_fixed_t sx1, sy1, sx2, sy2;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001565
Giulio Camuffo954f1832014-10-11 18:27:30 +03001566 if (b->sprites_are_broken)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001567 return NULL;
Kristian Høgsberg65bec242012-03-05 19:57:35 -05001568
Daniel Stone296d7a92016-10-21 18:05:37 +01001569 /* Don't import buffers which span multiple outputs. */
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001570 if (ev->output_mask != (1u << output->base.id))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001571 return NULL;
Ander Conselvan de Oliveirad450b192012-06-26 17:09:12 +03001572
Daniel Stone296d7a92016-10-21 18:05:37 +01001573 /* We can only import GBM buffers. */
1574 if (b->gbm == NULL)
1575 return NULL;
1576
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 if (ev->surface->buffer_ref.buffer == NULL)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001578 return NULL;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001579 buffer_resource = ev->surface->buffer_ref.buffer->resource;
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001580 if (wl_shm_buffer_get(buffer_resource))
Rob Clark702ffae2012-08-09 14:18:27 -05001581 return NULL;
1582
Daniel Stone296d7a92016-10-21 18:05:37 +01001583 if (viewport->buffer.transform != output->base.transform)
1584 return NULL;
1585 if (viewport->buffer.scale != output->base.current_scale)
1586 return NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 if (!drm_view_transform_supported(ev))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001588 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001589
Daniel Stone296d7a92016-10-21 18:05:37 +01001590 if (ev->alpha != 1.0f)
1591 return NULL;
1592
Daniel Stone085d2b92015-05-21 00:00:57 +01001593 wl_list_for_each(p, &b->plane_list, link) {
1594 if (p->type != WDRM_PLANE_TYPE_OVERLAY)
1595 continue;
1596
Daniel Stone08d4edf2017-04-04 17:54:34 +01001597 if (!drm_plane_crtc_supported(output, p))
Jesse Barnes58ef3792012-02-23 09:45:49 -05001598 continue;
1599
Daniel Stone08d4edf2017-04-04 17:54:34 +01001600 if (!p->fb_pending) {
Jesse Barnes58ef3792012-02-23 09:45:49 -05001601 found = 1;
1602 break;
1603 }
1604 }
1605
1606 /* No sprites available */
1607 if (!found)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001608 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001609
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001610 if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
Bryce Harringtona3582072015-08-14 12:23:13 -07001611#ifdef HAVE_GBM_FD_IMPORT
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001612 /* XXX: TODO:
1613 *
1614 * Use AddFB2 directly, do not go via GBM.
1615 * Add support for multiplanar formats.
1616 * Both require refactoring in the DRM-backend to
1617 * support a mix of gbm_bos and drmfbs.
1618 */
1619 struct gbm_import_fd_data gbm_dmabuf = {
Emmanuel Gil Peyrotc3996922015-11-24 19:28:24 +00001620 .fd = dmabuf->attributes.fd[0],
1621 .width = dmabuf->attributes.width,
1622 .height = dmabuf->attributes.height,
1623 .stride = dmabuf->attributes.stride[0],
1624 .format = dmabuf->attributes.format
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001625 };
1626
Micah Fedkec8890122017-02-01 15:28:23 -05001627 /* XXX: TODO:
1628 *
1629 * Currently the buffer is rejected if any dmabuf attribute
1630 * flag is set. This keeps us from passing an inverted /
1631 * interlaced / bottom-first buffer (or any other type that may
1632 * be added in the future) through to an overlay. Ultimately,
1633 * these types of buffers should be handled through buffer
1634 * transforms and not as spot-checks requiring specific
1635 * knowledge. */
1636 if (dmabuf->attributes.n_planes != 1 ||
1637 dmabuf->attributes.offset[0] != 0 ||
1638 dmabuf->attributes.flags)
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001639 return NULL;
1640
1641 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
1642 GBM_BO_USE_SCANOUT);
Bryce Harringtona3582072015-08-14 12:23:13 -07001643#else
1644 return NULL;
1645#endif
Pekka Paalanenbf8cc6f2014-06-12 17:12:12 +03001646 } else {
1647 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
1648 buffer_resource, GBM_BO_USE_SCANOUT);
1649 }
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001650 if (!bo)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001651 return NULL;
Kristian Høgsberg2763a2e2012-07-13 22:54:43 -04001652
Daniel Stone08d4edf2017-04-04 17:54:34 +01001653 format = drm_output_check_plane_format(p, ev, bo);
Kristian Høgsberga2f84cc2012-12-07 12:37:58 -05001654 if (format == 0) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001655 gbm_bo_destroy(bo);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001656 return NULL;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001657 }
1658
Daniel Stone08d4edf2017-04-04 17:54:34 +01001659 p->fb_pending = drm_fb_get_from_bo(bo, b, format, BUFFER_CLIENT);
1660 if (!p->fb_pending) {
Ander Conselvan de Oliveira8d360b42012-11-09 14:19:05 +02001661 gbm_bo_destroy(bo);
1662 return NULL;
1663 }
1664
Daniel Stone08d4edf2017-04-04 17:54:34 +01001665 drm_fb_set_buffer(p->fb_pending, ev->surface->buffer_ref.buffer);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001666
Jason Ekstranda7af7042013-10-12 22:38:11 -05001667 box = pixman_region32_extents(&ev->transform.boundingbox);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001668 p->base.x = box->x1;
1669 p->base.y = box->y1;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001670
Jesse Barnes58ef3792012-02-23 09:45:49 -05001671 /*
1672 * Calculate the source & dest rects properly based on actual
Derek Foreman4b1a0a12014-09-10 15:37:33 -05001673 * position (note the caller has called weston_view_update_transform()
Jesse Barnes58ef3792012-02-23 09:45:49 -05001674 * for us already).
1675 */
1676 pixman_region32_init(&dest_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001678 &output->base.region);
1679 pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001680 box = pixman_region32_extents(&dest_rect);
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001681 tbox = weston_transformed_rect(output->base.width,
1682 output->base.height,
1683 output->base.transform,
1684 output->base.current_scale,
Alexander Larssond9a7bb72013-05-22 14:41:39 +02001685 *box);
Daniel Stone08d4edf2017-04-04 17:54:34 +01001686 p->dest_x = tbox.x1;
1687 p->dest_y = tbox.y1;
1688 p->dest_w = tbox.x2 - tbox.x1;
1689 p->dest_h = tbox.y2 - tbox.y1;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001690 pixman_region32_fini(&dest_rect);
1691
1692 pixman_region32_init(&src_rect);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001694 &output->base.region);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001695 box = pixman_region32_extents(&src_rect);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001696
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 weston_view_from_global_fixed(ev,
1698 wl_fixed_from_int(box->x1),
1699 wl_fixed_from_int(box->y1),
1700 &sx1, &sy1);
1701 weston_view_from_global_fixed(ev,
1702 wl_fixed_from_int(box->x2),
1703 wl_fixed_from_int(box->y2),
1704 &sx2, &sy2);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001705
1706 if (sx1 < 0)
1707 sx1 = 0;
1708 if (sy1 < 0)
1709 sy1 = 0;
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001710 if (sx2 > wl_fixed_from_int(ev->surface->width))
1711 sx2 = wl_fixed_from_int(ev->surface->width);
1712 if (sy2 > wl_fixed_from_int(ev->surface->height))
1713 sy2 = wl_fixed_from_int(ev->surface->height);
Kristian Høgsberg3b00bae2012-07-13 15:25:07 -04001714
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001715 tbox.x1 = sx1;
1716 tbox.y1 = sy1;
1717 tbox.x2 = sx2;
1718 tbox.y2 = sy2;
1719
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001720 tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
1721 wl_fixed_from_int(ev->surface->height),
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001722 viewport->buffer.transform,
1723 viewport->buffer.scale,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001724 tbox);
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +02001725
Daniel Stone08d4edf2017-04-04 17:54:34 +01001726 p->src_x = tbox.x1 << 8;
1727 p->src_y = tbox.y1 << 8;
1728 p->src_w = (tbox.x2 - tbox.x1) << 8;
1729 p->src_h = (tbox.y2 - tbox.y1) << 8;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001730 pixman_region32_fini(&src_rect);
1731
Daniel Stone08d4edf2017-04-04 17:54:34 +01001732 return &p->base;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001733}
1734
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001735static struct weston_plane *
Pekka Paalanen050c1ba2014-12-17 16:20:38 +02001736drm_output_prepare_cursor_view(struct drm_output *output,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 struct weston_view *ev)
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001738{
Armin Krezović545dba62016-08-05 15:54:18 +02001739 struct drm_backend *b = to_drm_backend(output->base.compositor);
Neil Robertsf37f82c2014-05-01 18:00:41 +01001740 struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
Derek Foreman6c19b692015-12-03 14:07:12 -06001741 struct wl_shm_buffer *shmbuf;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001742 float x, y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001743
Giulio Camuffo954f1832014-10-11 18:27:30 +03001744 if (b->cursors_are_broken)
Kristian Høgsberg8a015802012-08-09 17:19:23 -04001745 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001746
1747 if (output->cursor_view)
Pekka Paalanen5580f222015-02-17 16:33:18 +02001748 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001749
1750 /* Don't import buffers which span multiple outputs. */
1751 if (ev->output_mask != (1u << output->base.id))
1752 return NULL;
1753
1754 /* We use GBM to import SHM buffers. */
1755 if (b->gbm == NULL)
1756 return NULL;
1757
Derek Foreman6c19b692015-12-03 14:07:12 -06001758 if (ev->surface->buffer_ref.buffer == NULL)
1759 return NULL;
1760 shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
1761 if (!shmbuf)
1762 return NULL;
1763 if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
1764 return NULL;
Daniel Stone296d7a92016-10-21 18:05:37 +01001765
1766 if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
1767 return NULL;
1768 if (ev->transform.enabled &&
1769 (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
1770 return NULL;
1771 if (viewport->buffer.scale != output->base.current_scale)
1772 return NULL;
1773 if (ev->geometry.scissor_enabled)
1774 return NULL;
1775
Derek Foreman6c19b692015-12-03 14:07:12 -06001776 if (ev->surface->width > b->cursor_width ||
Daniel Stone70d337d2015-06-16 18:42:23 +01001777 ev->surface->height > b->cursor_height)
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001778 return NULL;
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 output->cursor_view = ev;
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001781 weston_view_to_global_float(ev, 0, 0, &x, &y);
1782 output->cursor_plane.x = x;
1783 output->cursor_plane.y = y;
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001784
1785 return &output->cursor_plane;
1786}
1787
Pekka Paalanend0ead482014-06-16 12:05:40 +03001788/**
1789 * Update the image for the current cursor surface
1790 *
1791 * @param b DRM backend structure
1792 * @param bo GBM buffer object to write into
1793 * @param ev View to use for cursor image
1794 */
1795static void
1796cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
1797 struct weston_view *ev)
1798{
1799 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
1800 uint32_t buf[b->cursor_width * b->cursor_height];
1801 int32_t stride;
1802 uint8_t *s;
1803 int i;
1804
1805 assert(buffer && buffer->shm_buffer);
1806 assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
1807 assert(ev->surface->width <= b->cursor_width);
1808 assert(ev->surface->height <= b->cursor_height);
1809
1810 memset(buf, 0, sizeof buf);
1811 stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
1812 s = wl_shm_buffer_get_data(buffer->shm_buffer);
1813
1814 wl_shm_buffer_begin_access(buffer->shm_buffer);
1815 for (i = 0; i < ev->surface->height; i++)
1816 memcpy(buf + i * b->cursor_width,
1817 s + i * stride,
1818 ev->surface->width * 4);
1819 wl_shm_buffer_end_access(buffer->shm_buffer);
1820
1821 if (gbm_bo_write(bo, buf, sizeof buf) < 0)
1822 weston_log("failed update cursor: %m\n");
1823}
1824
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001825static void
1826drm_output_set_cursor(struct drm_output *output)
1827{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *ev = output->cursor_view;
Armin Krezović545dba62016-08-05 15:54:18 +02001829 struct drm_backend *b = to_drm_backend(output->base.compositor);
Pekka Paalanend0ead482014-06-16 12:05:40 +03001830 EGLint handle;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001831 struct gbm_bo *bo;
Derek Foremanbe428b32015-11-24 11:39:38 -06001832 float x, y;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001833
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 if (ev == NULL) {
Giulio Camuffo954f1832014-10-11 18:27:30 +03001835 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001836 return;
1837 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001838
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001839 if (pixman_region32_not_empty(&output->cursor_plane.damage)) {
Kristian Høgsberg5626d342012-08-03 11:50:05 -04001840 pixman_region32_fini(&output->cursor_plane.damage);
1841 pixman_region32_init(&output->cursor_plane.damage);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001842 output->current_cursor ^= 1;
Daniel Stonee4256832017-04-04 17:54:27 +01001843 bo = output->gbm_cursor_fb[output->current_cursor]->bo;
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001844
Pekka Paalanend0ead482014-06-16 12:05:40 +03001845 cursor_bo_update(b, bo, ev);
Kristian Høgsberg1f5de352012-07-18 12:09:58 -04001846 handle = gbm_bo_get_handle(bo).s32;
Giulio Camuffo954f1832014-10-11 18:27:30 +03001847 if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
1848 b->cursor_width, b->cursor_height)) {
Pekka Paalanenae29da22012-08-06 14:57:05 +03001849 weston_log("failed to set cursor: %m\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03001850 b->cursors_are_broken = 1;
Rob Clarkab5b1e32012-08-09 13:24:45 -05001851 }
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001852 }
1853
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001854 x = (output->cursor_plane.x - output->base.x) *
1855 output->base.current_scale;
1856 y = (output->cursor_plane.y - output->base.y) *
1857 output->base.current_scale;
Pekka Paalanen7eaed402015-11-27 14:20:58 +02001858
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001859 if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
1860 weston_log("failed to move cursor: %m\n");
1861 b->cursors_are_broken = 1;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04001862 }
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -05001863}
1864
Jesse Barnes58ef3792012-02-23 09:45:49 -05001865static void
Daniel Stoneb1f166d2017-03-01 11:34:10 +00001866drm_assign_planes(struct weston_output *output_base, void *repaint_data)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001867{
Armin Krezović545dba62016-08-05 15:54:18 +02001868 struct drm_backend *b = to_drm_backend(output_base->compositor);
1869 struct drm_output *output = to_drm_output(output_base);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 struct weston_view *ev, *next;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001871 pixman_region32_t overlap, surface_overlap;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001872 struct weston_plane *primary, *next_plane;
Jesse Barnes58ef3792012-02-23 09:45:49 -05001873
1874 /*
1875 * Find a surface for each sprite in the output using some heuristics:
1876 * 1) size
1877 * 2) frequency of update
1878 * 3) opacity (though some hw might support alpha blending)
1879 * 4) clipping (this can be fixed with color keys)
1880 *
1881 * The idea is to save on blitting since this should save power.
1882 * If we can get a large video surface on the sprite for example,
1883 * the main display surface may not need to update at all, and
1884 * the client buffer can be used directly for the sprite surface
1885 * as we do for flipping full screen surfaces.
1886 */
1887 pixman_region32_init(&overlap);
Giulio Camuffo954f1832014-10-11 18:27:30 +03001888 primary = &output_base->compositor->primary_plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889
Daniel Stonea7cba1d2017-04-04 17:54:21 +01001890 output->cursor_view = NULL;
1891 output->cursor_plane.x = INT32_MIN;
1892 output->cursor_plane.y = INT32_MIN;
1893
Giulio Camuffo954f1832014-10-11 18:27:30 +03001894 wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001895 struct weston_surface *es = ev->surface;
1896
1897 /* Test whether this buffer can ever go into a plane:
1898 * non-shm, or small enough to be a cursor.
1899 *
1900 * Also, keep a reference when using the pixman renderer.
1901 * That makes it possible to do a seamless switch to the GL
1902 * renderer and since the pixman renderer keeps a reference
1903 * to the buffer anyway, there is no side effects.
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001904 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03001905 if (b->use_pixman ||
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001906 (es->buffer_ref.buffer &&
1907 (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
Derek Foreman87430472015-10-15 10:24:48 -05001908 (ev->surface->width <= b->cursor_width &&
1909 ev->surface->height <= b->cursor_height))))
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001910 es->keep_buffer = true;
Ander Conselvan de Oliveira895b1fd2013-11-19 15:22:05 +02001911 else
Derek Foreman0fd6d4e2014-10-10 09:36:45 -05001912 es->keep_buffer = false;
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001913
Jesse Barnes58ef3792012-02-23 09:45:49 -05001914 pixman_region32_init(&surface_overlap);
1915 pixman_region32_intersect(&surface_overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 &ev->transform.boundingbox);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001917
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001918 next_plane = NULL;
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001919 if (pixman_region32_not_empty(&surface_overlap))
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001920 next_plane = primary;
1921 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 next_plane = drm_output_prepare_cursor_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001923 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001924 next_plane = drm_output_prepare_scanout_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001925 if (next_plane == NULL)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926 next_plane = drm_output_prepare_overlay_view(output, ev);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001927 if (next_plane == NULL)
1928 next_plane = primary;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001929
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 weston_view_move_to_plane(ev, next_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001931
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001932 if (next_plane == primary)
Jesse Barnes58ef3792012-02-23 09:45:49 -05001933 pixman_region32_union(&overlap, &overlap,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934 &ev->transform.boundingbox);
Kristian Høgsberg6143f7d2012-07-14 00:31:32 -04001935
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001936 if (next_plane == primary ||
1937 next_plane == &output->cursor_plane) {
1938 /* cursor plane involves a copy */
1939 ev->psf_flags = 0;
1940 } else {
1941 /* All other planes are a direct scanout of a
1942 * single client buffer.
1943 */
Pekka Paalanenb00c79b2016-02-18 16:53:27 +02001944 ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02001945 }
1946
Jesse Barnes58ef3792012-02-23 09:45:49 -05001947 pixman_region32_fini(&surface_overlap);
1948 }
1949 pixman_region32_fini(&overlap);
Jesse Barnes58ef3792012-02-23 09:45:49 -05001950}
1951
Pekka Paalanen7b36b422014-06-04 14:00:53 +03001952/**
1953 * Find the closest-matching mode for a given target
1954 *
1955 * Given a target mode, find the most suitable mode amongst the output's
1956 * current mode list to use, preferring the current mode if possible, to
1957 * avoid an expensive mode switch.
1958 *
1959 * @param output DRM output
1960 * @param target_mode Mode to attempt to match
1961 * @returns Pointer to a mode from the output's mode list
1962 */
Alex Wub7b8bda2012-04-17 17:20:48 +08001963static struct drm_mode *
1964choose_mode (struct drm_output *output, struct weston_mode *target_mode)
1965{
1966 struct drm_mode *tmp_mode = NULL, *mode;
1967
Hardeningff39efa2013-09-18 23:56:35 +02001968 if (output->base.current_mode->width == target_mode->width &&
1969 output->base.current_mode->height == target_mode->height &&
1970 (output->base.current_mode->refresh == target_mode->refresh ||
Alex Wub7b8bda2012-04-17 17:20:48 +08001971 target_mode->refresh == 0))
Hardeningff39efa2013-09-18 23:56:35 +02001972 return (struct drm_mode *)output->base.current_mode;
Alex Wub7b8bda2012-04-17 17:20:48 +08001973
1974 wl_list_for_each(mode, &output->base.mode_list, base.link) {
1975 if (mode->mode_info.hdisplay == target_mode->width &&
1976 mode->mode_info.vdisplay == target_mode->height) {
Mario Kleiner872797c2015-06-21 21:25:09 +02001977 if (mode->base.refresh == target_mode->refresh ||
1978 target_mode->refresh == 0) {
Alex Wub7b8bda2012-04-17 17:20:48 +08001979 return mode;
Daniel Stonef556ebe2015-05-21 08:28:58 +01001980 } else if (!tmp_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08001981 tmp_mode = mode;
1982 }
1983 }
1984
1985 return tmp_mode;
1986}
1987
1988static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001989drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001990static void
1991drm_output_fini_egl(struct drm_output *output);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02001992static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03001993drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
Daniel Stone3e661f72016-11-04 17:24:06 +00001994static void
1995drm_output_fini_pixman(struct drm_output *output);
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02001996
1997static int
Alex Wub7b8bda2012-04-17 17:20:48 +08001998drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
1999{
2000 struct drm_output *output;
2001 struct drm_mode *drm_mode;
Giulio Camuffo954f1832014-10-11 18:27:30 +03002002 struct drm_backend *b;
Alex Wub7b8bda2012-04-17 17:20:48 +08002003
2004 if (output_base == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002005 weston_log("output is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08002006 return -1;
2007 }
2008
2009 if (mode == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002010 weston_log("mode is NULL.\n");
Alex Wub7b8bda2012-04-17 17:20:48 +08002011 return -1;
2012 }
2013
Armin Krezović545dba62016-08-05 15:54:18 +02002014 b = to_drm_backend(output_base->compositor);
2015 output = to_drm_output(output_base);
Alex Wub7b8bda2012-04-17 17:20:48 +08002016 drm_mode = choose_mode (output, mode);
2017
2018 if (!drm_mode) {
Martin Minarik6d118362012-06-07 18:01:59 +02002019 weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
Alex Wub7b8bda2012-04-17 17:20:48 +08002020 return -1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002021 }
2022
Hardeningff39efa2013-09-18 23:56:35 +02002023 if (&drm_mode->base == output->base.current_mode)
Alex Wub7b8bda2012-04-17 17:20:48 +08002024 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002025
Hardeningff39efa2013-09-18 23:56:35 +02002026 output->base.current_mode->flags = 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002027
Hardeningff39efa2013-09-18 23:56:35 +02002028 output->base.current_mode = &drm_mode->base;
2029 output->base.current_mode->flags =
Alex Wub7b8bda2012-04-17 17:20:48 +08002030 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
2031
Daniel Stonef30a18c2017-04-04 17:54:31 +01002032 /* XXX: This drops our current buffer too early, before we've started
2033 * displaying it. Ideally this should be much more atomic and
2034 * integrated with a full repaint cycle, rather than doing a
2035 * sledgehammer modeswitch first, and only later showing new
2036 * content.
2037 */
Daniel Stone5bb8f582017-04-04 17:54:28 +01002038 drm_fb_unref(output->fb_current);
Daniel Stonef30a18c2017-04-04 17:54:31 +01002039 assert(!output->fb_last);
2040 assert(!output->fb_pending);
2041 output->fb_last = output->fb_current = NULL;
Alex Wub7b8bda2012-04-17 17:20:48 +08002042
Giulio Camuffo954f1832014-10-11 18:27:30 +03002043 if (b->use_pixman) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002044 drm_output_fini_pixman(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002045 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002046 weston_log("failed to init output pixman state with "
2047 "new mode\n");
2048 return -1;
2049 }
2050 } else {
Daniel Stone3e661f72016-11-04 17:24:06 +00002051 drm_output_fini_egl(output);
Giulio Camuffo954f1832014-10-11 18:27:30 +03002052 if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002053 weston_log("failed to init output egl state with "
2054 "new mode");
2055 return -1;
2056 }
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002057 }
2058
Alex Wub7b8bda2012-04-17 17:20:48 +08002059 return 0;
Alex Wub7b8bda2012-04-17 17:20:48 +08002060}
2061
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002062static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002063on_drm_input(int fd, uint32_t mask, void *data)
2064{
2065 drmEventContext evctx;
2066
2067 memset(&evctx, 0, sizeof evctx);
Emil Velikov863e66b2017-04-04 18:07:34 +01002068 evctx.version = 2;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002069 evctx.page_flip_handler = page_flip_handler;
Jesse Barnes58ef3792012-02-23 09:45:49 -05002070 evctx.vblank_handler = vblank_handler;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002071 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002072
2073 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002074}
2075
2076static int
Daniel Stoneefa504f2016-12-19 16:48:20 +00002077init_kms_caps(struct drm_backend *b)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002078{
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002079 uint64_t cap;
Daniel Stoneefa504f2016-12-19 16:48:20 +00002080 int ret;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002081 clockid_t clk_id;
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002082
Daniel Stoneefa504f2016-12-19 16:48:20 +00002083 weston_log("using %s\n", b->drm.filename);
Kristian Høgsbergb71302e2012-05-10 12:28:35 -04002084
Daniel Stoneefa504f2016-12-19 16:48:20 +00002085 ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002086 if (ret == 0 && cap == 1)
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002087 clk_id = CLOCK_MONOTONIC;
Ander Conselvan de Oliveira95eb3a22013-05-07 14:16:59 +03002088 else
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002089 clk_id = CLOCK_REALTIME;
2090
Giulio Camuffo954f1832014-10-11 18:27:30 +03002091 if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002092 weston_log("Error: failed to set presentation clock %d.\n",
2093 clk_id);
2094 return -1;
2095 }
Ander Conselvan de Oliveira1d41ad42013-01-25 15:13:04 +02002096
Daniel Stoneefa504f2016-12-19 16:48:20 +00002097 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002098 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002099 b->cursor_width = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002100 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002101 b->cursor_width = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002102
Daniel Stoneefa504f2016-12-19 16:48:20 +00002103 ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap);
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002104 if (ret == 0)
Giulio Camuffo954f1832014-10-11 18:27:30 +03002105 b->cursor_height = cap;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002106 else
Giulio Camuffo954f1832014-10-11 18:27:30 +03002107 b->cursor_height = 64;
Alvaro Fernando Garcíadce7c6e2014-07-28 18:30:17 -03002108
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002109 ret = drmSetClientCap(b->drm.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
2110 b->universal_planes = (ret == 0);
2111 weston_log("DRM: %s universal planes\n",
2112 b->universal_planes ? "supports" : "does not support");
2113
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002114 return 0;
2115}
2116
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002117static struct gbm_device *
2118create_gbm_device(int fd)
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02002119{
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002120 struct gbm_device *gbm;
Alexandru DAMIANbe0ac5b2013-10-02 17:51:05 +01002121
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002122 gl_renderer = weston_load_module("gl-renderer.so",
2123 "gl_renderer_interface");
2124 if (!gl_renderer)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002125 return NULL;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03002126
2127 /* GBM will load a dri driver, but even though they need symbols from
2128 * libglapi, in some version of Mesa they are not linked to it. Since
2129 * only the gl-renderer module links to it, the call above won't make
2130 * these symbols globally available, and loading the DRI driver fails.
2131 * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
2132 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
2133
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002134 gbm = gbm_create_device(fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002135
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002136 return gbm;
2137}
2138
Bryce Harringtonc056a982015-05-19 15:25:18 -07002139/* When initializing EGL, if the preferred buffer format isn't available
Bryce Harringtonb9939982016-04-15 20:28:26 -07002140 * we may be able to substitute an ARGB format for an XRGB one.
Derek Foremanc4cfe852015-05-15 12:12:40 -05002141 *
2142 * This returns 0 if substitution isn't possible, but 0 might be a
2143 * legitimate format for other EGL platforms, so the caller is
2144 * responsible for checking for 0 before calling gl_renderer->create().
2145 *
2146 * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
2147 * but it's entirely possible we'll see this again on other implementations.
2148 */
2149static int
2150fallback_format_for(uint32_t format)
2151{
2152 switch (format) {
2153 case GBM_FORMAT_XRGB8888:
2154 return GBM_FORMAT_ARGB8888;
2155 case GBM_FORMAT_XRGB2101010:
2156 return GBM_FORMAT_ARGB2101010;
2157 default:
2158 return 0;
2159 }
2160}
2161
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002162static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002163drm_backend_create_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002164{
Derek Foreman6d556372015-11-04 14:47:33 -06002165 EGLint format[3] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002166 b->gbm_format,
2167 fallback_format_for(b->gbm_format),
Derek Foreman6d556372015-11-04 14:47:33 -06002168 0,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002169 };
Derek Foreman6d556372015-11-04 14:47:33 -06002170 int n_formats = 2;
John Kåre Alsakeref591aa2013-03-02 12:27:39 +01002171
Derek Foremanc4cfe852015-05-15 12:12:40 -05002172 if (format[1])
Derek Foreman6d556372015-11-04 14:47:33 -06002173 n_formats = 3;
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002174 if (gl_renderer->display_create(b->compositor,
2175 EGL_PLATFORM_GBM_KHR,
2176 (void *)b->gbm,
Miguel A. Vico41700e32016-05-18 17:47:59 +02002177 NULL,
Miguel A. Vicodddc6702016-05-18 17:41:07 +02002178 gl_renderer->opaque_attribs,
2179 format,
2180 n_formats) < 0) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002181 return -1;
2182 }
2183
2184 return 0;
2185}
2186
2187static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002188init_egl(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002189{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002190 b->gbm = create_gbm_device(b->drm.fd);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002191
Giulio Camuffo954f1832014-10-11 18:27:30 +03002192 if (!b->gbm)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02002193 return -1;
2194
Giulio Camuffo954f1832014-10-11 18:27:30 +03002195 if (drm_backend_create_gl_renderer(b) < 0) {
2196 gbm_device_destroy(b->gbm);
Kristian Høgsbergcbcd0472012-03-11 18:27:41 -04002197 return -1;
2198 }
2199
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002200 return 0;
2201}
2202
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002203static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002204init_pixman(struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002205{
Giulio Camuffo954f1832014-10-11 18:27:30 +03002206 return pixman_renderer_init(b->compositor);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002207}
2208
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002209/**
Pekka Paalanenec272712014-06-05 11:22:25 +03002210 * Create a drm_plane for a hardware plane
2211 *
2212 * Creates one drm_plane structure for a hardware plane, and initialises its
2213 * properties and formats.
2214 *
2215 * This function does not add the plane to the list of usable planes in Weston
2216 * itself; the caller is responsible for this.
2217 *
2218 * Call drm_plane_destroy to clean up the plane.
2219 *
2220 * @param b DRM compositor backend
2221 * @param kplane DRM plane to create
2222 */
2223static struct drm_plane *
2224drm_plane_create(struct drm_backend *b, const drmModePlane *kplane)
2225{
2226 struct drm_plane *plane;
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002227 drmModeObjectProperties *props;
2228
2229 static struct drm_property_enum_info plane_type_enums[] = {
2230 [WDRM_PLANE_TYPE_PRIMARY] = {
2231 .name = "Primary",
2232 },
2233 [WDRM_PLANE_TYPE_OVERLAY] = {
2234 .name = "Overlay",
2235 },
2236 [WDRM_PLANE_TYPE_CURSOR] = {
2237 .name = "Cursor",
2238 },
2239 };
2240 static const struct drm_property_info plane_props[] = {
2241 [WDRM_PLANE_TYPE] = {
2242 .name = "type",
2243 .enum_values = plane_type_enums,
2244 .num_enum_values = WDRM_PLANE_TYPE__COUNT,
2245 },
2246 };
Pekka Paalanenec272712014-06-05 11:22:25 +03002247
2248 plane = zalloc(sizeof(*plane) + ((sizeof(uint32_t)) *
2249 kplane->count_formats));
2250 if (!plane) {
2251 weston_log("%s: out of memory\n", __func__);
2252 return NULL;
2253 }
2254
2255 plane->backend = b;
2256 plane->possible_crtcs = kplane->possible_crtcs;
2257 plane->plane_id = kplane->plane_id;
2258 plane->count_formats = kplane->count_formats;
2259 memcpy(plane->formats, kplane->formats,
2260 kplane->count_formats * sizeof(kplane->formats[0]));
2261
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002262 props = drmModeObjectGetProperties(b->drm.fd, kplane->plane_id,
2263 DRM_MODE_OBJECT_PLANE);
2264 if (!props) {
2265 weston_log("couldn't get plane properties\n");
2266 free(plane);
2267 return NULL;
2268 }
2269 drm_property_info_populate(b, plane_props, plane->props,
2270 WDRM_PLANE__COUNT, props);
2271 plane->type =
2272 drm_property_get_value(&plane->props[WDRM_PLANE_TYPE],
2273 props,
2274 WDRM_PLANE_TYPE_OVERLAY);
2275 drmModeFreeObjectProperties(props);
2276
Pekka Paalanenec272712014-06-05 11:22:25 +03002277 weston_plane_init(&plane->base, b->compositor, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01002278 wl_list_insert(&b->plane_list, &plane->link);
Pekka Paalanenec272712014-06-05 11:22:25 +03002279
2280 return plane;
2281}
2282
2283/**
2284 * Destroy one DRM plane
2285 *
2286 * Destroy a DRM plane, removing it from screen and releasing its retained
2287 * buffers in the process. The counterpart to drm_plane_create.
2288 *
2289 * @param plane Plane to deallocate (will be freed)
2290 */
2291static void
2292drm_plane_destroy(struct drm_plane *plane)
2293{
2294 drmModeSetPlane(plane->backend->drm.fd, plane->plane_id, 0, 0, 0,
2295 0, 0, 0, 0, 0, 0, 0, 0);
2296 assert(!plane->fb_last);
2297 assert(!plane->fb_pending);
Pekka Paalanenc5de57f2015-05-20 23:01:44 +01002298 drm_property_info_free(plane->props, WDRM_PLANE__COUNT);
Pekka Paalanenec272712014-06-05 11:22:25 +03002299 drm_fb_unref(plane->fb_current);
2300 weston_plane_release(&plane->base);
2301 wl_list_remove(&plane->link);
2302 free(plane);
2303}
2304
2305/**
2306 * Initialise sprites (overlay planes)
2307 *
2308 * Walk the list of provided DRM planes, and add overlay planes.
2309 *
2310 * Call destroy_sprites to free these planes.
2311 *
2312 * @param b DRM compositor backend
2313 */
2314static void
2315create_sprites(struct drm_backend *b)
2316{
2317 drmModePlaneRes *kplane_res;
2318 drmModePlane *kplane;
2319 struct drm_plane *drm_plane;
2320 uint32_t i;
2321
2322 kplane_res = drmModeGetPlaneResources(b->drm.fd);
2323 if (!kplane_res) {
2324 weston_log("failed to get plane resources: %s\n",
2325 strerror(errno));
2326 return;
2327 }
2328
2329 for (i = 0; i < kplane_res->count_planes; i++) {
2330 kplane = drmModeGetPlane(b->drm.fd, kplane_res->planes[i]);
2331 if (!kplane)
2332 continue;
2333
2334 drm_plane = drm_plane_create(b, kplane);
2335 drmModeFreePlane(kplane);
2336 if (!drm_plane)
2337 continue;
2338
Daniel Stone085d2b92015-05-21 00:00:57 +01002339 if (drm_plane->type == WDRM_PLANE_TYPE_OVERLAY)
2340 weston_compositor_stack_plane(b->compositor,
2341 &drm_plane->base,
2342 &b->compositor->primary_plane);
Pekka Paalanenec272712014-06-05 11:22:25 +03002343 }
2344
2345 drmModeFreePlaneResources(kplane_res);
2346}
2347
2348/**
2349 * Clean up sprites (overlay planes)
2350 *
2351 * The counterpart to create_sprites.
2352 *
2353 * @param b DRM compositor backend
2354 */
2355static void
2356destroy_sprites(struct drm_backend *b)
2357{
2358 struct drm_plane *plane, *next;
2359
Daniel Stone085d2b92015-05-21 00:00:57 +01002360 wl_list_for_each_safe(plane, next, &b->plane_list, link)
Pekka Paalanenec272712014-06-05 11:22:25 +03002361 drm_plane_destroy(plane);
2362}
2363
2364/**
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002365 * Add a mode to output's mode list
2366 *
2367 * Copy the supplied DRM mode into a Weston mode structure, and add it to the
2368 * output's mode list.
2369 *
2370 * @param output DRM output to add mode to
2371 * @param info DRM mode structure to add
2372 * @returns Newly-allocated Weston/DRM mode structure
2373 */
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002374static struct drm_mode *
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002375drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002376{
2377 struct drm_mode *mode;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002378 uint64_t refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002379
2380 mode = malloc(sizeof *mode);
2381 if (mode == NULL)
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002382 return NULL;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002383
2384 mode->base.flags = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002385 mode->base.width = info->hdisplay;
2386 mode->base.height = info->vdisplay;
Kristian Høgsbergc4621b02012-05-10 12:23:53 -04002387
2388 /* Calculate higher precision (mHz) refresh rate */
2389 refresh = (info->clock * 1000000LL / info->htotal +
2390 info->vtotal / 2) / info->vtotal;
2391
2392 if (info->flags & DRM_MODE_FLAG_INTERLACE)
2393 refresh *= 2;
2394 if (info->flags & DRM_MODE_FLAG_DBLSCAN)
2395 refresh /= 2;
2396 if (info->vscan > 1)
2397 refresh /= info->vscan;
2398
2399 mode->base.refresh = refresh;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002400 mode->mode_info = *info;
Kristian Høgsberg061c4252012-06-28 11:28:15 -04002401
2402 if (info->type & DRM_MODE_TYPE_PREFERRED)
2403 mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
2404
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002405 wl_list_insert(output->base.mode_list.prev, &mode->base.link);
2406
Ander Conselvan de Oliveira42c46462012-08-09 16:45:00 +03002407 return mode;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002408}
2409
2410static int
2411drm_subpixel_to_wayland(int drm_value)
2412{
2413 switch (drm_value) {
2414 default:
2415 case DRM_MODE_SUBPIXEL_UNKNOWN:
2416 return WL_OUTPUT_SUBPIXEL_UNKNOWN;
2417 case DRM_MODE_SUBPIXEL_NONE:
2418 return WL_OUTPUT_SUBPIXEL_NONE;
2419 case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
2420 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
2421 case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
2422 return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
2423 case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
2424 return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
2425 case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
2426 return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
2427 }
2428}
2429
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002430/* returns a value between 0-255 range, where higher is brighter */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002431static uint32_t
2432drm_get_backlight(struct drm_output *output)
2433{
2434 long brightness, max_brightness, norm;
2435
2436 brightness = backlight_get_brightness(output->backlight);
2437 max_brightness = backlight_get_max_brightness(output->backlight);
2438
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002439 /* convert it on a scale of 0 to 255 */
2440 norm = (brightness * 255)/(max_brightness);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002441
2442 return (uint32_t) norm;
2443}
2444
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002445/* values accepted are between 0-255 range */
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002446static void
2447drm_set_backlight(struct weston_output *output_base, uint32_t value)
2448{
Armin Krezović545dba62016-08-05 15:54:18 +02002449 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002450 long max_brightness, new_brightness;
2451
2452 if (!output->backlight)
2453 return;
2454
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002455 if (value > 255)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002456 return;
2457
2458 max_brightness = backlight_get_max_brightness(output->backlight);
2459
2460 /* get denormalized value */
Tiago Vignatti5ab91ad2012-03-12 19:40:09 -03002461 new_brightness = (value * max_brightness) / 255;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002462
2463 backlight_set_brightness(output->backlight, new_brightness);
2464}
2465
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466static void
2467drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
2468{
Armin Krezović545dba62016-08-05 15:54:18 +02002469 struct drm_output *output = to_drm_output(output_base);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002470 struct weston_compositor *ec = output_base->compositor;
Armin Krezović545dba62016-08-05 15:54:18 +02002471 struct drm_backend *b = to_drm_backend(ec);
Daniel Stone02cf4662017-03-03 16:19:39 +00002472 struct drm_property_info *prop =
2473 &output->props_conn[WDRM_CONNECTOR_DPMS];
Daniel Stone36609c72015-06-18 07:49:02 +01002474 int ret;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002475
Daniel Stone02cf4662017-03-03 16:19:39 +00002476 if (!prop->prop_id)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002477 return;
2478
Daniel Stone36609c72015-06-18 07:49:02 +01002479 ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
Daniel Stone02cf4662017-03-03 16:19:39 +00002480 prop->prop_id, level);
Daniel Stone36609c72015-06-18 07:49:02 +01002481 if (ret) {
2482 weston_log("DRM: DPMS: failed property set for %s\n",
2483 output->base.name);
2484 return;
2485 }
2486
2487 output->dpms = level;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002488}
2489
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002490static const char * const connector_type_names[] = {
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002491 [DRM_MODE_CONNECTOR_Unknown] = "Unknown",
2492 [DRM_MODE_CONNECTOR_VGA] = "VGA",
2493 [DRM_MODE_CONNECTOR_DVII] = "DVI-I",
2494 [DRM_MODE_CONNECTOR_DVID] = "DVI-D",
2495 [DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
2496 [DRM_MODE_CONNECTOR_Composite] = "Composite",
2497 [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
2498 [DRM_MODE_CONNECTOR_LVDS] = "LVDS",
2499 [DRM_MODE_CONNECTOR_Component] = "Component",
2500 [DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
2501 [DRM_MODE_CONNECTOR_DisplayPort] = "DP",
2502 [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
2503 [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
2504 [DRM_MODE_CONNECTOR_TV] = "TV",
2505 [DRM_MODE_CONNECTOR_eDP] = "eDP",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002506#ifdef DRM_MODE_CONNECTOR_DSI
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002507 [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
2508 [DRM_MODE_CONNECTOR_DSI] = "DSI",
Pekka Paalanenab81f152015-08-24 14:27:07 +03002509#endif
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04002510};
2511
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002512static char *
2513make_connector_name(const drmModeConnector *con)
2514{
2515 char name[32];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002516 const char *type_name = NULL;
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002517
2518 if (con->connector_type < ARRAY_LENGTH(connector_type_names))
2519 type_name = connector_type_names[con->connector_type];
Pekka Paalanen89c49b32015-08-19 15:25:57 +03002520
2521 if (!type_name)
2522 type_name = "UNNAMED";
2523
2524 snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
Pekka Paalanen3ce63622014-06-04 16:29:49 +03002525
2526 return strdup(name);
2527}
2528
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04002529static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002530find_crtc_for_connector(struct drm_backend *b,
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002531 drmModeRes *resources, drmModeConnector *connector)
2532{
2533 drmModeEncoder *encoder;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002534 int i, j;
Daniel Stone75487c22017-01-16 14:33:38 +00002535 int ret = -1;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002536
2537 for (j = 0; j < connector->count_encoders; j++) {
Daniel Stone75487c22017-01-16 14:33:38 +00002538 uint32_t possible_crtcs, encoder_id, crtc_id;
2539
Giulio Camuffo954f1832014-10-11 18:27:30 +03002540 encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002541 if (encoder == NULL) {
2542 weston_log("Failed to get encoder.\n");
Daniel Stone75487c22017-01-16 14:33:38 +00002543 continue;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002544 }
Daniel Stone75487c22017-01-16 14:33:38 +00002545 encoder_id = encoder->encoder_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002546 possible_crtcs = encoder->possible_crtcs;
Daniel Stone75487c22017-01-16 14:33:38 +00002547 crtc_id = encoder->crtc_id;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002548 drmModeFreeEncoder(encoder);
2549
2550 for (i = 0; i < resources->count_crtcs; i++) {
Daniel Stone72c0e1b2017-02-09 13:49:15 +00002551 if (!(possible_crtcs & (1 << i)))
2552 continue;
2553
2554 if (drm_output_find_by_crtc(b, resources->crtcs[i]))
2555 continue;
2556
Daniel Stone75487c22017-01-16 14:33:38 +00002557 /* Try to preserve the existing
2558 * CRTC -> encoder -> connector routing; it makes
2559 * initialisation faster, and also since we have a
2560 * very dumb picking algorithm, may preserve a better
2561 * choice. */
2562 if (!connector->encoder_id ||
2563 (encoder_id == connector->encoder_id &&
2564 crtc_id == resources->crtcs[i]))
2565 return i;
2566
2567 ret = i;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002568 }
2569 }
2570
Daniel Stone75487c22017-01-16 14:33:38 +00002571 return ret;
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002572}
2573
Daniel Stonee4256832017-04-04 17:54:27 +01002574static void drm_output_fini_cursor_egl(struct drm_output *output)
2575{
2576 unsigned int i;
2577
2578 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2579 drm_fb_unref(output->gbm_cursor_fb[i]);
2580 output->gbm_cursor_fb[i] = NULL;
2581 }
2582}
2583
2584static int
2585drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
2586{
2587 unsigned int i;
2588
2589 for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
2590 struct gbm_bo *bo;
2591
2592 bo = gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
2593 GBM_FORMAT_ARGB8888,
2594 GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
2595 if (!bo)
2596 goto err;
2597
2598 output->gbm_cursor_fb[i] =
2599 drm_fb_get_from_bo(bo, b, GBM_FORMAT_ARGB8888,
2600 BUFFER_CURSOR);
2601 if (!output->gbm_cursor_fb[i]) {
2602 gbm_bo_destroy(bo);
2603 goto err;
2604 }
2605 }
2606
2607 return 0;
2608
2609err:
2610 weston_log("cursor buffers unavailable, using gl cursors\n");
2611 b->cursors_are_broken = 1;
2612 drm_output_fini_cursor_egl(output);
2613 return -1;
2614}
2615
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002616/* Init output state that depends on gl or gbm */
2617static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002618drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002619{
Derek Foremanc4cfe852015-05-15 12:12:40 -05002620 EGLint format[2] = {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002621 output->gbm_format,
2622 fallback_format_for(output->gbm_format),
Derek Foremanc4cfe852015-05-15 12:12:40 -05002623 };
Daniel Stonee4256832017-04-04 17:54:27 +01002624 int n_formats = 1;
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002625
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002626 output->gbm_surface = gbm_surface_create(b->gbm,
Hardeningff39efa2013-09-18 23:56:35 +02002627 output->base.current_mode->width,
2628 output->base.current_mode->height,
Derek Foremanc4cfe852015-05-15 12:12:40 -05002629 format[0],
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002630 GBM_BO_USE_SCANOUT |
2631 GBM_BO_USE_RENDERING);
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002632 if (!output->gbm_surface) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002633 weston_log("failed to create gbm surface\n");
2634 return -1;
2635 }
2636
Derek Foremanc4cfe852015-05-15 12:12:40 -05002637 if (format[1])
2638 n_formats = 2;
Miguel A. Vicoc095cde2016-05-18 17:43:00 +02002639 if (gl_renderer->output_window_create(&output->base,
2640 (EGLNativeWindowType)output->gbm_surface,
2641 output->gbm_surface,
2642 gl_renderer->opaque_attribs,
2643 format,
2644 n_formats) < 0) {
Ander Conselvan de Oliveira6c01c9c2012-12-14 13:37:30 -02002645 weston_log("failed to create gl renderer output state\n");
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01002646 gbm_surface_destroy(output->gbm_surface);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002647 return -1;
2648 }
2649
Daniel Stonee4256832017-04-04 17:54:27 +01002650 drm_output_init_cursor_egl(output, b);
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02002651
2652 return 0;
2653}
2654
Daniel Stone3e661f72016-11-04 17:24:06 +00002655static void
2656drm_output_fini_egl(struct drm_output *output)
2657{
2658 gl_renderer->output_destroy(&output->base);
2659 gbm_surface_destroy(output->gbm_surface);
Daniel Stonee4256832017-04-04 17:54:27 +01002660 drm_output_fini_cursor_egl(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00002661}
2662
Kristian Høgsberg9ca38462012-07-26 22:44:55 -04002663static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03002664drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002665{
Hardeningff39efa2013-09-18 23:56:35 +02002666 int w = output->base.current_mode->width;
2667 int h = output->base.current_mode->height;
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002668 uint32_t format = output->gbm_format;
2669 uint32_t pixman_format;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002670 unsigned int i;
2671
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002672 switch (format) {
2673 case GBM_FORMAT_XRGB8888:
2674 pixman_format = PIXMAN_x8r8g8b8;
2675 break;
2676 case GBM_FORMAT_RGB565:
2677 pixman_format = PIXMAN_r5g6b5;
2678 break;
2679 default:
2680 weston_log("Unsupported pixman format 0x%x\n", format);
2681 return -1;
2682 }
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002683
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002684 /* FIXME error checking */
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002685 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002686 output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002687 if (!output->dumb[i])
2688 goto err;
2689
2690 output->image[i] =
Tomi Valkeinenf8da0c22016-06-20 14:18:45 +03002691 pixman_image_create_bits(pixman_format, w, h,
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002692 output->dumb[i]->map,
2693 output->dumb[i]->stride);
2694 if (!output->image[i])
2695 goto err;
2696 }
2697
2698 if (pixman_renderer_output_create(&output->base) < 0)
2699 goto err;
2700
2701 pixman_region32_init_rect(&output->previous_damage,
Alexander Larsson0b135062013-05-28 16:23:36 +02002702 output->base.x, output->base.y, output->base.width, output->base.height);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002703
2704 return 0;
2705
2706err:
2707 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
2708 if (output->dumb[i])
Daniel Stone6e7a9612017-04-04 17:54:26 +01002709 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002710 if (output->image[i])
2711 pixman_image_unref(output->image[i]);
2712
2713 output->dumb[i] = NULL;
2714 output->image[i] = NULL;
2715 }
2716
2717 return -1;
2718}
2719
2720static void
2721drm_output_fini_pixman(struct drm_output *output)
2722{
2723 unsigned int i;
2724
2725 pixman_renderer_output_destroy(&output->base);
2726 pixman_region32_fini(&output->previous_damage);
2727
2728 for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002729 pixman_image_unref(output->image[i]);
Daniel Stone6e7a9612017-04-04 17:54:26 +01002730 drm_fb_unref(output->dumb[i]);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002731 output->dumb[i] = NULL;
2732 output->image[i] = NULL;
2733 }
2734}
2735
Richard Hughes2b2092a2013-04-24 14:58:02 +01002736static void
2737edid_parse_string(const uint8_t *data, char text[])
2738{
2739 int i;
2740 int replaced = 0;
2741
2742 /* this is always 12 bytes, but we can't guarantee it's null
2743 * terminated or not junk. */
2744 strncpy(text, (const char *) data, 12);
2745
Bryce Harrington9c7de162015-08-28 13:04:26 -07002746 /* guarantee our new string is null-terminated */
2747 text[12] = '\0';
2748
Richard Hughes2b2092a2013-04-24 14:58:02 +01002749 /* remove insane chars */
2750 for (i = 0; text[i] != '\0'; i++) {
2751 if (text[i] == '\n' ||
2752 text[i] == '\r') {
2753 text[i] = '\0';
2754 break;
2755 }
2756 }
2757
2758 /* ensure string is printable */
2759 for (i = 0; text[i] != '\0'; i++) {
2760 if (!isprint(text[i])) {
2761 text[i] = '-';
2762 replaced++;
2763 }
2764 }
2765
2766 /* if the string is random junk, ignore the string */
2767 if (replaced > 4)
2768 text[0] = '\0';
2769}
2770
2771#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
2772#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
2773#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
2774#define EDID_OFFSET_DATA_BLOCKS 0x36
2775#define EDID_OFFSET_LAST_BLOCK 0x6c
2776#define EDID_OFFSET_PNPID 0x08
2777#define EDID_OFFSET_SERIAL 0x0c
2778
2779static int
2780edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
2781{
2782 int i;
2783 uint32_t serial_number;
2784
2785 /* check header */
2786 if (length < 128)
2787 return -1;
2788 if (data[0] != 0x00 || data[1] != 0xff)
2789 return -1;
2790
2791 /* decode the PNP ID from three 5 bit words packed into 2 bytes
2792 * /--08--\/--09--\
2793 * 7654321076543210
2794 * |\---/\---/\---/
2795 * R C1 C2 C3 */
2796 edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
2797 edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
2798 edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
2799 edid->pnp_id[3] = '\0';
2800
2801 /* maybe there isn't a ASCII serial number descriptor, so use this instead */
2802 serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
2803 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
2804 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
2805 serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
2806 if (serial_number > 0)
2807 sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
2808
2809 /* parse EDID data */
2810 for (i = EDID_OFFSET_DATA_BLOCKS;
2811 i <= EDID_OFFSET_LAST_BLOCK;
2812 i += 18) {
2813 /* ignore pixel clock data */
2814 if (data[i] != 0)
2815 continue;
2816 if (data[i+2] != 0)
2817 continue;
2818
2819 /* any useful blocks? */
2820 if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
2821 edid_parse_string(&data[i+5],
2822 edid->monitor_name);
2823 } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
2824 edid_parse_string(&data[i+5],
2825 edid->serial_number);
2826 } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
2827 edid_parse_string(&data[i+5],
2828 edid->eisa_id);
2829 }
2830 }
2831 return 0;
2832}
2833
2834static void
Daniel Stone02cf4662017-03-03 16:19:39 +00002835find_and_parse_output_edid(struct drm_backend *b, struct drm_output *output,
2836 drmModeObjectPropertiesPtr props)
Richard Hughes2b2092a2013-04-24 14:58:02 +01002837{
2838 drmModePropertyBlobPtr edid_blob = NULL;
Daniel Stone02cf4662017-03-03 16:19:39 +00002839 uint32_t blob_id;
Richard Hughes2b2092a2013-04-24 14:58:02 +01002840 int rc;
2841
Daniel Stone02cf4662017-03-03 16:19:39 +00002842 blob_id =
2843 drm_property_get_value(&output->props_conn[WDRM_CONNECTOR_EDID],
2844 props, 0);
2845 if (!blob_id)
2846 return;
2847
2848 edid_blob = drmModeGetPropertyBlob(b->drm.fd, blob_id);
Richard Hughes2b2092a2013-04-24 14:58:02 +01002849 if (!edid_blob)
2850 return;
2851
2852 rc = edid_parse(&output->edid,
2853 edid_blob->data,
2854 edid_blob->length);
2855 if (!rc) {
2856 weston_log("EDID data '%s', '%s', '%s'\n",
2857 output->edid.pnp_id,
2858 output->edid.monitor_name,
2859 output->edid.serial_number);
2860 if (output->edid.pnp_id[0] != '\0')
2861 output->base.make = output->edid.pnp_id;
2862 if (output->edid.monitor_name[0] != '\0')
2863 output->base.model = output->edid.monitor_name;
2864 if (output->edid.serial_number[0] != '\0')
2865 output->base.serial_number = output->edid.serial_number;
2866 }
2867 drmModeFreePropertyBlob(edid_blob);
2868}
2869
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002870
2871
2872static int
2873parse_modeline(const char *s, drmModeModeInfo *mode)
2874{
2875 char hsync[16];
2876 char vsync[16];
2877 float fclock;
2878
2879 mode->type = DRM_MODE_TYPE_USERDEF;
2880 mode->hskew = 0;
2881 mode->vscan = 0;
2882 mode->vrefresh = 0;
2883 mode->flags = 0;
2884
Rob Bradford307e09e2013-07-26 16:29:40 +01002885 if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002886 &fclock,
2887 &mode->hdisplay,
2888 &mode->hsync_start,
2889 &mode->hsync_end,
2890 &mode->htotal,
2891 &mode->vdisplay,
2892 &mode->vsync_start,
2893 &mode->vsync_end,
2894 &mode->vtotal, hsync, vsync) != 11)
2895 return -1;
2896
2897 mode->clock = fclock * 1000;
2898 if (strcmp(hsync, "+hsync") == 0)
2899 mode->flags |= DRM_MODE_FLAG_PHSYNC;
2900 else if (strcmp(hsync, "-hsync") == 0)
2901 mode->flags |= DRM_MODE_FLAG_NHSYNC;
2902 else
2903 return -1;
2904
2905 if (strcmp(vsync, "+vsync") == 0)
2906 mode->flags |= DRM_MODE_FLAG_PVSYNC;
2907 else if (strcmp(vsync, "-vsync") == 0)
2908 mode->flags |= DRM_MODE_FLAG_NVSYNC;
2909 else
2910 return -1;
2911
Emmanuel Gil Peyrota62138b2016-05-02 22:40:11 +01002912 snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
2913 mode->hdisplay, mode->vdisplay, fclock);
2914
Kristian Høgsberga30989a2013-05-23 17:23:15 -04002915 return 0;
2916}
2917
Rob Bradford66bd9f52013-06-25 18:56:42 +01002918static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03002919setup_output_seat_constraint(struct drm_backend *b,
Rob Bradford66bd9f52013-06-25 18:56:42 +01002920 struct weston_output *output,
2921 const char *s)
2922{
2923 if (strcmp(s, "") != 0) {
Derek Foreman1281a362015-07-31 16:55:32 -05002924 struct weston_pointer *pointer;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002925 struct udev_seat *seat;
2926
Giulio Camuffo954f1832014-10-11 18:27:30 +03002927 seat = udev_seat_get_named(&b->input, s);
Derek Foreman0720ea32015-07-15 13:00:35 -05002928 if (!seat)
2929 return;
Rob Bradford66bd9f52013-06-25 18:56:42 +01002930
Derek Foreman0720ea32015-07-15 13:00:35 -05002931 seat->base.output = output;
2932
Derek Foreman1281a362015-07-31 16:55:32 -05002933 pointer = weston_seat_get_pointer(&seat->base);
2934 if (pointer)
2935 weston_pointer_clamp(pointer,
2936 &pointer->x,
2937 &pointer->y);
Rob Bradford66bd9f52013-06-25 18:56:42 +01002938 }
2939}
2940
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02002941static int
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002942parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002943{
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002944 int ret = 0;
2945
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002946 if (s == NULL)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002947 *gbm_format = default_value;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002948 else if (strcmp(s, "xrgb8888") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002949 *gbm_format = GBM_FORMAT_XRGB8888;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002950 else if (strcmp(s, "rgb565") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002951 *gbm_format = GBM_FORMAT_RGB565;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002952 else if (strcmp(s, "xrgb2101010") == 0)
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002953 *gbm_format = GBM_FORMAT_XRGB2101010;
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002954 else {
2955 weston_log("fatal: unrecognized pixel format: %s\n", s);
2956 ret = -1;
2957 }
2958
Neil Roberts77c1a5b2014-03-07 18:05:50 +00002959 return ret;
2960}
2961
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002962/**
2963 * Choose suitable mode for an output
2964 *
2965 * Find the most suitable mode to use for initial setup (or reconfiguration on
2966 * hotplug etc) for a DRM output.
2967 *
2968 * @param output DRM output to choose mode for
2969 * @param kind Strategy and preference to use when choosing mode
2970 * @param width Desired width for this output
2971 * @param height Desired height for this output
2972 * @param current_mode Mode currently being displayed on this output
2973 * @param modeline Manually-entered mode (may be NULL)
2974 * @returns A mode from the output's mode list, or NULL if none available
2975 */
2976static struct drm_mode *
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002977drm_output_choose_initial_mode(struct drm_backend *backend,
2978 struct drm_output *output,
2979 enum weston_drm_backend_output_mode mode,
Armin Krezović08368132016-09-30 14:11:05 +02002980 const char *modeline,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002981 const drmModeModeInfo *current_mode)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03002982{
2983 struct drm_mode *preferred = NULL;
2984 struct drm_mode *current = NULL;
2985 struct drm_mode *configured = NULL;
2986 struct drm_mode *best = NULL;
2987 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02002988 drmModeModeInfo drm_modeline;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002989 int32_t width = 0;
2990 int32_t height = 0;
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002991 uint32_t refresh = 0;
2992 int n;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002993
Armin Krezović08368132016-09-30 14:11:05 +02002994 if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) {
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01002995 n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh);
2996 if (n != 2 && n != 3) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07002997 width = -1;
2998
Armin Krezović08368132016-09-30 14:11:05 +02002999 if (parse_modeline(modeline, &drm_modeline) == 0) {
3000 configured = drm_output_add_mode(output, &drm_modeline);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003001 if (!configured)
3002 return NULL;
3003 } else {
3004 weston_log("Invalid modeline \"%s\" for output %s\n",
Armin Krezović08368132016-09-30 14:11:05 +02003005 modeline, output->base.name);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003006 }
3007 }
3008 }
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003009
3010 wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003011 if (width == drm_mode->base.width &&
Fabien Dessenne2d66a7d2017-01-17 17:17:21 +01003012 height == drm_mode->base.height &&
3013 (refresh == 0 || refresh == drm_mode->mode_info.vrefresh))
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003014 configured = drm_mode;
3015
comic fans7a5c5622016-03-17 14:29:27 +02003016 if (memcmp(current_mode, &drm_mode->mode_info,
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003017 sizeof *current_mode) == 0)
3018 current = drm_mode;
3019
3020 if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
3021 preferred = drm_mode;
3022
3023 best = drm_mode;
3024 }
3025
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003026 if (current == NULL && current_mode->clock != 0) {
3027 current = drm_output_add_mode(output, current_mode);
3028 if (!current)
3029 return NULL;
3030 }
3031
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003032 if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003033 configured = current;
3034
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003035 if (configured)
3036 return configured;
3037
3038 if (preferred)
3039 return preferred;
3040
3041 if (current)
3042 return current;
3043
3044 if (best)
3045 return best;
3046
3047 weston_log("no available modes for %s\n", output->base.name);
3048 return NULL;
3049}
3050
Pekka Paalaneneee580b2014-06-04 16:43:06 +03003051static int
3052connector_get_current_mode(drmModeConnector *connector, int drm_fd,
3053 drmModeModeInfo *mode)
3054{
3055 drmModeEncoder *encoder;
3056 drmModeCrtc *crtc;
3057
3058 /* Get the current mode on the crtc that's currently driving
3059 * this connector. */
3060 encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
3061 memset(mode, 0, sizeof *mode);
3062 if (encoder != NULL) {
3063 crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
3064 drmModeFreeEncoder(encoder);
3065 if (crtc == NULL)
3066 return -1;
3067 if (crtc->mode_valid)
3068 *mode = crtc->mode;
3069 drmModeFreeCrtc(crtc);
3070 }
3071
3072 return 0;
3073}
3074
Neil Roberts77c1a5b2014-03-07 18:05:50 +00003075static int
Armin Krezović08368132016-09-30 14:11:05 +02003076drm_output_set_mode(struct weston_output *base,
3077 enum weston_drm_backend_output_mode mode,
3078 const char *modeline)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003079{
Armin Krezović08368132016-09-30 14:11:05 +02003080 struct drm_output *output = to_drm_output(base);
3081 struct drm_backend *b = to_drm_backend(base->compositor);
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003082
Armin Krezović445b41b2016-10-09 23:48:16 +02003083 struct drm_mode *current;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003084 drmModeModeInfo crtc_mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003085
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003086 output->base.make = "unknown";
3087 output->base.model = "unknown";
Richard Hughes2b2092a2013-04-24 14:58:02 +01003088 output->base.serial_number = "unknown";
Matt Roper361d2ad2011-08-29 13:52:23 -07003089
Armin Krezović08368132016-09-30 14:11:05 +02003090 if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0)
Armin Krezović445b41b2016-10-09 23:48:16 +02003091 return -1;
David Herrmann0f0d54e2011-12-08 17:05:45 +01003092
Armin Krezović08368132016-09-30 14:11:05 +02003093 current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode);
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003094 if (!current)
Armin Krezović445b41b2016-10-09 23:48:16 +02003095 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02003096
Pekka Paalanen7b36b422014-06-04 14:00:53 +03003097 output->base.current_mode = &current->base;
Hardeningff39efa2013-09-18 23:56:35 +02003098 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
Wang Quanxianacb805a2012-07-30 18:09:46 -04003099
Armin Krezović08368132016-09-30 14:11:05 +02003100 /* Set native_ fields, so weston_output_mode_switch_to_native() works */
3101 output->base.native_mode = output->base.current_mode;
3102 output->base.native_scale = output->base.current_scale;
3103
3104 output->base.mm_width = output->connector->mmWidth;
3105 output->base.mm_height = output->connector->mmHeight;
3106
3107 return 0;
Armin Krezović08368132016-09-30 14:11:05 +02003108}
3109
3110static void
3111drm_output_set_gbm_format(struct weston_output *base,
3112 const char *gbm_format)
3113{
3114 struct drm_output *output = to_drm_output(base);
3115 struct drm_backend *b = to_drm_backend(base->compositor);
3116
3117 if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1)
3118 output->gbm_format = b->gbm_format;
3119}
3120
3121static void
3122drm_output_set_seat(struct weston_output *base,
3123 const char *seat)
3124{
3125 struct drm_output *output = to_drm_output(base);
3126 struct drm_backend *b = to_drm_backend(base->compositor);
3127
3128 setup_output_seat_constraint(b, &output->base,
3129 seat ? seat : "");
3130}
3131
3132static int
3133drm_output_enable(struct weston_output *base)
3134{
3135 struct drm_output *output = to_drm_output(base);
3136 struct drm_backend *b = to_drm_backend(base->compositor);
3137 struct weston_mode *m;
3138
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003139 if (b->pageflip_timeout)
3140 drm_output_pageflip_timer_create(output);
3141
Giulio Camuffo954f1832014-10-11 18:27:30 +03003142 if (b->use_pixman) {
3143 if (drm_output_init_pixman(output, b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003144 weston_log("Failed to init output pixman state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003145 goto err;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003146 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03003147 } else if (drm_output_init_egl(output, b) < 0) {
Ander Conselvan de Oliveira475cf152012-12-14 13:37:29 -02003148 weston_log("Failed to init output gl state\n");
Daniel Stone02cf4662017-03-03 16:19:39 +00003149 goto err;
Kristian Høgsberg1d1e0a52012-10-21 13:29:26 -04003150 }
Kristian Høgsberg8e1f77f2012-05-03 11:39:35 -04003151
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003152 if (output->backlight) {
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003153 weston_log("Initialized backlight, device %s\n",
3154 output->backlight->path);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003155 output->base.set_backlight = drm_set_backlight;
3156 output->base.backlight_current = drm_get_backlight(output);
Kristian Høgsberg220819f2013-05-23 20:29:40 -04003157 } else {
3158 weston_log("Failed to initialize backlight\n");
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003159 }
3160
Jonas Ådahle5a12252013-04-05 23:07:11 +02003161 output->base.start_repaint_loop = drm_output_start_repaint_loop;
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05003162 output->base.repaint = drm_output_repaint;
Jesse Barnes58ef3792012-02-23 09:45:49 -05003163 output->base.assign_planes = drm_assign_planes;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003164 output->base.set_dpms = drm_set_dpms;
Alex Wub7b8bda2012-04-17 17:20:48 +08003165 output->base.switch_mode = drm_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01003166
Richard Hughese7299962013-05-01 21:52:12 +01003167 output->base.gamma_size = output->original_crtc->gamma_size;
3168 output->base.set_gamma = drm_output_set_gamma;
3169
Armin Krezović08368132016-09-30 14:11:05 +02003170 output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel);
3171
Armin Krezović9bcf4c52017-02-08 13:55:26 +01003172 if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
3173 output->connector->connector_type == DRM_MODE_CONNECTOR_eDP)
Pekka Paalanen0079a942017-03-24 15:46:23 +02003174 output->base.connection_internal = true;
Armin Krezović08368132016-09-30 14:11:05 +02003175
Derek Foremanb0427562016-02-05 15:55:20 -06003176 weston_plane_init(&output->cursor_plane, b->compositor,
3177 INT32_MIN, INT32_MIN);
Daniel Stone4202e502016-10-24 13:26:42 +01003178 weston_plane_init(&output->scanout_plane, b->compositor, 0, 0);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003179
Giulio Camuffo954f1832014-10-11 18:27:30 +03003180 weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
Daniel Stone4202e502016-10-24 13:26:42 +01003181 weston_compositor_stack_plane(b->compositor, &output->scanout_plane,
Giulio Camuffo954f1832014-10-11 18:27:30 +03003182 &b->compositor->primary_plane);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003183
Kristian Høgsberg2f9ed712012-07-26 17:57:15 -04003184 weston_log("Output %s, (connector %d, crtc %d)\n",
Richard Hughesafe690c2013-05-02 10:10:04 +01003185 output->base.name, output->connector_id, output->crtc_id);
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003186 wl_list_for_each(m, &output->base.mode_list, link)
U. Artie Eoffd3ed6cb2014-01-10 10:15:17 -08003187 weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003188 m->width, m->height, m->refresh / 1000.0,
3189 m->flags & WL_OUTPUT_MODE_PREFERRED ?
3190 ", preferred" : "",
3191 m->flags & WL_OUTPUT_MODE_CURRENT ?
3192 ", current" : "",
Armin Krezović08368132016-09-30 14:11:05 +02003193 output->connector->count_modes == 0 ?
Kristian Høgsberg061c4252012-06-28 11:28:15 -04003194 ", built-in" : "");
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003195
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003196 output->state_invalid = true;
3197
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003198 return 0;
David Herrmann0f0d54e2011-12-08 17:05:45 +01003199
Daniel Stone02cf4662017-03-03 16:19:39 +00003200err:
David Herrmann0f0d54e2011-12-08 17:05:45 +01003201 return -1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003202}
3203
Jesse Barnes58ef3792012-02-23 09:45:49 -05003204static void
Armin Krezović08368132016-09-30 14:11:05 +02003205drm_output_deinit(struct weston_output *base)
3206{
3207 struct drm_output *output = to_drm_output(base);
3208 struct drm_backend *b = to_drm_backend(base->compositor);
3209
Daniel Stonef30a18c2017-04-04 17:54:31 +01003210 /* output->fb_last and output->fb_pending must not be set here;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003211 * destroy_pending/disable_pending exist to guarantee exactly this. */
Daniel Stonef30a18c2017-04-04 17:54:31 +01003212 assert(!output->fb_last);
Daniel Stone5bb8f582017-04-04 17:54:28 +01003213 assert(!output->fb_pending);
3214 drm_fb_unref(output->fb_current);
3215 output->fb_current = NULL;
Daniel Stone6e7a9612017-04-04 17:54:26 +01003216
Daniel Stone3e661f72016-11-04 17:24:06 +00003217 if (b->use_pixman)
Armin Krezović08368132016-09-30 14:11:05 +02003218 drm_output_fini_pixman(output);
Daniel Stone3e661f72016-11-04 17:24:06 +00003219 else
3220 drm_output_fini_egl(output);
Armin Krezović08368132016-09-30 14:11:05 +02003221
Daniel Stone4202e502016-10-24 13:26:42 +01003222 weston_plane_release(&output->scanout_plane);
Armin Krezović08368132016-09-30 14:11:05 +02003223 weston_plane_release(&output->cursor_plane);
3224
Armin Krezović08368132016-09-30 14:11:05 +02003225 /* Turn off hardware cursor */
3226 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
3227}
3228
3229static void
3230drm_output_destroy(struct weston_output *base)
3231{
3232 struct drm_output *output = to_drm_output(base);
3233 struct drm_backend *b = to_drm_backend(base->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02003234 struct drm_mode *drm_mode, *next;
Armin Krezović08368132016-09-30 14:11:05 +02003235 drmModeCrtcPtr origcrtc = output->original_crtc;
3236
3237 if (output->page_flip_pending) {
3238 output->destroy_pending = 1;
3239 weston_log("destroy output while page flip pending\n");
3240 return;
3241 }
3242
3243 if (output->base.enabled)
3244 drm_output_deinit(&output->base);
3245
Armin Krezović445b41b2016-10-09 23:48:16 +02003246 wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
3247 base.link) {
3248 wl_list_remove(&drm_mode->base.link);
3249 free(drm_mode);
3250 }
3251
Armin Krezović08368132016-09-30 14:11:05 +02003252 if (origcrtc) {
3253 /* Restore original CRTC state */
3254 drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
3255 origcrtc->x, origcrtc->y,
3256 &output->connector_id, 1, &origcrtc->mode);
3257 drmModeFreeCrtc(origcrtc);
3258 }
3259
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00003260 if (output->pageflip_timer)
3261 wl_event_source_remove(output->pageflip_timer);
3262
Armin Krezović08368132016-09-30 14:11:05 +02003263 weston_output_destroy(&output->base);
3264
Daniel Stone02cf4662017-03-03 16:19:39 +00003265 drm_property_info_free(output->props_conn, WDRM_CONNECTOR__COUNT);
3266
Armin Krezović08368132016-09-30 14:11:05 +02003267 drmModeFreeConnector(output->connector);
3268
3269 if (output->backlight)
3270 backlight_destroy(output->backlight);
3271
Armin Krezović08368132016-09-30 14:11:05 +02003272 free(output);
3273}
3274
3275static int
3276drm_output_disable(struct weston_output *base)
3277{
3278 struct drm_output *output = to_drm_output(base);
3279 struct drm_backend *b = to_drm_backend(base->compositor);
3280
3281 if (output->page_flip_pending) {
3282 output->disable_pending = 1;
3283 return -1;
3284 }
3285
3286 if (output->base.enabled)
3287 drm_output_deinit(&output->base);
3288
3289 output->disable_pending = 0;
3290
3291 weston_log("Disabling output %s\n", output->base.name);
3292 drmModeSetCrtc(b->drm.fd, output->crtc_id,
3293 0, 0, 0, 0, 0, NULL);
3294
3295 return 0;
3296}
3297
3298/**
3299 * Create a Weston output structure
3300 *
3301 * Given a DRM connector, create a matching drm_output structure and add it
3302 * to Weston's output list. It also takes ownership of the connector, which
3303 * is released when output is destroyed.
3304 *
3305 * @param b Weston backend structure
3306 * @param resources DRM resources for this device
3307 * @param connector DRM connector to use for this new output
3308 * @param drm_device udev device pointer
3309 * @returns 0 on success, or -1 on failure
3310 */
3311static int
3312create_output_for_connector(struct drm_backend *b,
3313 drmModeRes *resources,
3314 drmModeConnector *connector,
3315 struct udev_device *drm_device)
3316{
3317 struct drm_output *output;
Daniel Stone02cf4662017-03-03 16:19:39 +00003318 drmModeObjectPropertiesPtr props;
Armin Krezović445b41b2016-10-09 23:48:16 +02003319 struct drm_mode *drm_mode;
Armin Krezović08368132016-09-30 14:11:05 +02003320 int i;
3321
Daniel Stone02cf4662017-03-03 16:19:39 +00003322 static const struct drm_property_info connector_props[] = {
3323 [WDRM_CONNECTOR_EDID] = { .name = "EDID" },
3324 [WDRM_CONNECTOR_DPMS] = { .name = "DPMS" },
3325 };
3326
Armin Krezović08368132016-09-30 14:11:05 +02003327 i = find_crtc_for_connector(b, resources, connector);
3328 if (i < 0) {
3329 weston_log("No usable crtc/encoder pair for connector.\n");
Armin Krezović445b41b2016-10-09 23:48:16 +02003330 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003331 }
3332
3333 output = zalloc(sizeof *output);
3334 if (output == NULL)
Armin Krezović445b41b2016-10-09 23:48:16 +02003335 goto err;
Armin Krezović08368132016-09-30 14:11:05 +02003336
3337 output->connector = connector;
3338 output->crtc_id = resources->crtcs[i];
3339 output->pipe = i;
3340 output->connector_id = connector->connector_id;
3341
3342 output->backlight = backlight_init(drm_device,
3343 connector->connector_type);
3344
Armin Krezović445b41b2016-10-09 23:48:16 +02003345 output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
3346
Armin Krezović08368132016-09-30 14:11:05 +02003347 output->base.enable = drm_output_enable;
3348 output->base.destroy = drm_output_destroy;
3349 output->base.disable = drm_output_disable;
3350 output->base.name = make_connector_name(connector);
3351
3352 output->destroy_pending = 0;
3353 output->disable_pending = 0;
Armin Krezović08368132016-09-30 14:11:05 +02003354
Daniel Stone02cf4662017-03-03 16:19:39 +00003355 props = drmModeObjectGetProperties(b->drm.fd, connector->connector_id,
3356 DRM_MODE_OBJECT_CONNECTOR);
3357 if (!props) {
3358 weston_log("failed to get connector properties\n");
3359 goto err;
3360 }
3361 drm_property_info_populate(b, connector_props, output->props_conn,
3362 WDRM_CONNECTOR__COUNT, props);
3363 find_and_parse_output_edid(b, output, props);
3364 drmModeFreeObjectProperties(props);
3365
Armin Krezović40087402016-09-30 14:11:12 +02003366 weston_output_init(&output->base, b->compositor);
Armin Krezović445b41b2016-10-09 23:48:16 +02003367
3368 wl_list_init(&output->base.mode_list);
3369
3370 for (i = 0; i < output->connector->count_modes; i++) {
3371 drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
3372 if (!drm_mode) {
3373 drm_output_destroy(&output->base);
3374 return -1;
3375 }
3376 }
3377
Armin Krezović08368132016-09-30 14:11:05 +02003378 weston_compositor_add_pending_output(&output->base, b->compositor);
3379
3380 return 0;
Armin Krezović445b41b2016-10-09 23:48:16 +02003381
3382err:
3383 drmModeFreeConnector(connector);
3384
3385 return -1;
Armin Krezović08368132016-09-30 14:11:05 +02003386}
3387
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003388static int
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003389create_outputs(struct drm_backend *b, struct udev_device *drm_device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003390{
3391 drmModeConnector *connector;
3392 drmModeRes *resources;
3393 int i;
3394
Giulio Camuffo954f1832014-10-11 18:27:30 +03003395 resources = drmModeGetResources(b->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003396 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003397 weston_log("drmModeGetResources failed\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003398 return -1;
3399 }
3400
Giulio Camuffo954f1832014-10-11 18:27:30 +03003401 b->min_width = resources->min_width;
3402 b->max_width = resources->max_width;
3403 b->min_height = resources->min_height;
3404 b->max_height = resources->max_height;
Rob Clark4339add2012-08-09 14:18:28 -05003405
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003406 for (i = 0; i < resources->count_connectors; i++) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003407 int ret;
3408
Giulio Camuffo954f1832014-10-11 18:27:30 +03003409 connector = drmModeGetConnector(b->drm.fd,
Benjamin Franzke117483d2011-08-30 11:38:26 +02003410 resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003411 if (connector == NULL)
3412 continue;
3413
3414 if (connector->connection == DRM_MODE_CONNECTED &&
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00003415 (b->connector == 0 ||
3416 connector->connector_id == b->connector)) {
Daniel Stone02cf4662017-03-03 16:19:39 +00003417 ret = create_output_for_connector(b, resources,
3418 connector, drm_device);
3419 if (ret < 0)
3420 weston_log("failed to create new connector\n");
Armin Krezović08368132016-09-30 14:11:05 +02003421 } else {
3422 drmModeFreeConnector(connector);
Benjamin Franzke9eaee352011-08-02 13:03:54 +02003423 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003424 }
3425
Armin Krezović08368132016-09-30 14:11:05 +02003426 if (wl_list_empty(&b->compositor->output_list) &&
3427 wl_list_empty(&b->compositor->pending_output_list))
Martin Minarik6d118362012-06-07 18:01:59 +02003428 weston_log("No currently active connector found.\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003429
3430 drmModeFreeResources(resources);
3431
3432 return 0;
3433}
3434
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003435static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003436update_outputs(struct drm_backend *b, struct udev_device *drm_device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003437{
3438 drmModeConnector *connector;
3439 drmModeRes *resources;
3440 struct drm_output *output, *next;
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003441 uint32_t *connected;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003442 int i;
3443
Giulio Camuffo954f1832014-10-11 18:27:30 +03003444 resources = drmModeGetResources(b->drm.fd);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003445 if (!resources) {
Martin Minarik6d118362012-06-07 18:01:59 +02003446 weston_log("drmModeGetResources failed\n");
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003447 return;
3448 }
3449
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003450 connected = calloc(resources->count_connectors, sizeof(uint32_t));
3451 if (!connected) {
3452 drmModeFreeResources(resources);
3453 return;
3454 }
3455
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003456 /* collect new connects */
3457 for (i = 0; i < resources->count_connectors; i++) {
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003458 uint32_t connector_id = resources->connectors[i];
Benjamin Franzke117483d2011-08-30 11:38:26 +02003459
Giulio Camuffo954f1832014-10-11 18:27:30 +03003460 connector = drmModeGetConnector(b->drm.fd, connector_id);
David Herrmann7551cff2011-12-08 17:05:43 +01003461 if (connector == NULL)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003462 continue;
3463
David Herrmann7551cff2011-12-08 17:05:43 +01003464 if (connector->connection != DRM_MODE_CONNECTED) {
3465 drmModeFreeConnector(connector);
3466 continue;
3467 }
3468
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00003469 if (b->connector && (b->connector != connector_id)) {
3470 drmModeFreeConnector(connector);
3471 continue;
3472 }
3473
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003474 connected[i] = connector_id;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003475
Daniel Stonec0ec7592017-02-09 13:58:35 +00003476 if (drm_output_find_by_connector(b, connector_id)) {
Armin Krezović08368132016-09-30 14:11:05 +02003477 drmModeFreeConnector(connector);
Daniel Stonec0ec7592017-02-09 13:58:35 +00003478 continue;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003479 }
Daniel Stonec0ec7592017-02-09 13:58:35 +00003480
3481 create_output_for_connector(b, resources,
3482 connector, drm_device);
3483 weston_log("connector %d connected\n", connector_id);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003484 }
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003485
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003486 wl_list_for_each_safe(output, next, &b->compositor->output_list,
3487 base.link) {
3488 bool disconnected = true;
3489
3490 for (i = 0; i < resources->count_connectors; i++) {
3491 if (connected[i] == output->connector_id) {
3492 disconnected = false;
3493 break;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003494 }
3495 }
Armin Krezović08368132016-09-30 14:11:05 +02003496
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003497 if (!disconnected)
3498 continue;
3499
3500 weston_log("connector %d disconnected\n", output->connector_id);
3501 drm_output_destroy(&output->base);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003502 }
Daniel Stoneefc2b1d2017-02-09 14:06:31 +00003503
3504 wl_list_for_each_safe(output, next, &b->compositor->pending_output_list,
3505 base.link) {
3506 bool disconnected = true;
3507
3508 for (i = 0; i < resources->count_connectors; i++) {
3509 if (connected[i] == output->connector_id) {
3510 disconnected = false;
3511 break;
3512 }
3513 }
3514
3515 if (!disconnected)
3516 continue;
3517
3518 weston_log("connector %d disconnected\n", output->connector_id);
3519 drm_output_destroy(&output->base);
3520 }
3521
3522 free(connected);
3523 drmModeFreeResources(resources);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003524}
3525
3526static int
Giulio Camuffo954f1832014-10-11 18:27:30 +03003527udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003528{
David Herrmannd7488c22012-03-11 20:05:21 +01003529 const char *sysnum;
David Herrmann6ac52db2012-03-11 20:05:22 +01003530 const char *val;
David Herrmannd7488c22012-03-11 20:05:21 +01003531
3532 sysnum = udev_device_get_sysnum(device);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003533 if (!sysnum || atoi(sysnum) != b->drm.id)
David Herrmannd7488c22012-03-11 20:05:21 +01003534 return 0;
Benjamin Franzke117483d2011-08-30 11:38:26 +02003535
David Herrmann6ac52db2012-03-11 20:05:22 +01003536 val = udev_device_get_property_value(device, "HOTPLUG");
3537 if (!val)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003538 return 0;
3539
David Herrmann6ac52db2012-03-11 20:05:22 +01003540 return strcmp(val, "1") == 0;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003541}
3542
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003543static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003544udev_drm_event(int fd, uint32_t mask, void *data)
3545{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003546 struct drm_backend *b = data;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003547 struct udev_device *event;
3548
Giulio Camuffo954f1832014-10-11 18:27:30 +03003549 event = udev_monitor_receive_device(b->udev_monitor);
Benjamin Franzke117483d2011-08-30 11:38:26 +02003550
Giulio Camuffo954f1832014-10-11 18:27:30 +03003551 if (udev_event_is_hotplug(b, event))
3552 update_outputs(b, event);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003553
3554 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003555
3556 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003557}
3558
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003559static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003560drm_restore(struct weston_compositor *ec)
3561{
Kristian Høgsberg3f495872013-09-18 23:00:17 -07003562 weston_launcher_restore(ec->launcher);
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003563}
3564
3565static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003566drm_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003567{
Armin Krezović545dba62016-08-05 15:54:18 +02003568 struct drm_backend *b = to_drm_backend(ec);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003569
Giulio Camuffo954f1832014-10-11 18:27:30 +03003570 udev_input_destroy(&b->input);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003571
Giulio Camuffo954f1832014-10-11 18:27:30 +03003572 wl_event_source_remove(b->udev_drm_source);
3573 wl_event_source_remove(b->drm_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003574
Giulio Camuffo954f1832014-10-11 18:27:30 +03003575 destroy_sprites(b);
Kristian Høgsberg3d64a3e2013-05-10 12:36:04 -04003576
Ander Conselvan de Oliveira6b162142013-10-25 16:26:32 +03003577 weston_compositor_shutdown(ec);
3578
Giulio Camuffo954f1832014-10-11 18:27:30 +03003579 if (b->gbm)
3580 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02003581
Giulio Camuffo954f1832014-10-11 18:27:30 +03003582 weston_launcher_destroy(ec->launcher);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003583
Giulio Camuffo954f1832014-10-11 18:27:30 +03003584 close(b->drm.fd);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003585 free(b);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003586}
3587
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003588static void
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003589session_notify(struct wl_listener *listener, void *data)
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003590{
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003591 struct weston_compositor *compositor = data;
Armin Krezović545dba62016-08-05 15:54:18 +02003592 struct drm_backend *b = to_drm_backend(compositor);
Daniel Stone085d2b92015-05-21 00:00:57 +01003593 struct drm_plane *plane;
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003594 struct drm_output *output;
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003595
Giulio Camuffo954f1832014-10-11 18:27:30 +03003596 if (compositor->session_active) {
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003597 weston_log("activating session\n");
Daniel Stonef33e1042016-11-05 08:10:13 +00003598 weston_compositor_wake(compositor);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003599 weston_compositor_damage_all(compositor);
Pekka Paalanen6b65d8f2017-07-27 13:44:32 +03003600
3601 wl_list_for_each(output, &compositor->output_list, base.link)
3602 output->state_invalid = true;
3603
Giulio Camuffo954f1832014-10-11 18:27:30 +03003604 udev_input_enable(&b->input);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003605 } else {
3606 weston_log("deactivating session\n");
Giulio Camuffo954f1832014-10-11 18:27:30 +03003607 udev_input_disable(&b->input);
Kristian Høgsberg4014a6b2012-04-10 00:08:45 -04003608
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003609 weston_compositor_offscreen(compositor);
Kristian Høgsbergd8e181b2011-05-06 15:38:28 -04003610
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003611 /* If we have a repaint scheduled (either from a
3612 * pending pageflip or the idle handler), make sure we
3613 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003614 * vt switched away. The OFFSCREEN state will prevent
Abdur Rehman4dca0e12017-01-01 19:46:35 +05003615 * further attempts at repainting. When we switch
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003616 * back, we schedule a repaint, which will process
3617 * pending frame callbacks. */
3618
Giulio Camuffo954f1832014-10-11 18:27:30 +03003619 wl_list_for_each(output, &compositor->output_list, base.link) {
Daniel Stone09a97e22017-03-01 11:34:06 +00003620 output->base.repaint_needed = false;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003621 drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05003622 }
3623
Giulio Camuffo954f1832014-10-11 18:27:30 +03003624 output = container_of(compositor->output_list.next,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003625 struct drm_output, base.link);
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003626
Daniel Stone085d2b92015-05-21 00:00:57 +01003627 wl_list_for_each(plane, &b->plane_list, link) {
3628 if (plane->type != WDRM_PLANE_TYPE_OVERLAY)
3629 continue;
3630
Giulio Camuffo954f1832014-10-11 18:27:30 +03003631 drmModeSetPlane(b->drm.fd,
Daniel Stone085d2b92015-05-21 00:00:57 +01003632 plane->plane_id,
Kristian Høgsberga6edab32012-07-14 01:06:28 -04003633 output->crtc_id, 0, 0,
Kristian Høgsberg85fd3272012-02-23 21:45:32 -05003634 0, 0, 0, 0, 0, 0, 0, 0);
Daniel Stone085d2b92015-05-21 00:00:57 +01003635 }
3636 }
Kristian Høgsberg9396fc52011-05-06 15:15:37 -04003637}
3638
Daniel Stoneefa504f2016-12-19 16:48:20 +00003639/**
3640 * Determines whether or not a device is capable of modesetting. If successful,
3641 * sets b->drm.fd and b->drm.filename to the opened device.
3642 */
3643static bool
3644drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
3645{
3646 const char *filename = udev_device_get_devnode(device);
3647 const char *sysnum = udev_device_get_sysnum(device);
3648 drmModeRes *res;
3649 int id, fd;
3650
3651 if (!filename)
3652 return false;
3653
3654 fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
3655 if (fd < 0)
3656 return false;
3657
3658 res = drmModeGetResources(fd);
3659 if (!res)
3660 goto out_fd;
3661
3662 if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
3663 res->count_encoders <= 0)
3664 goto out_res;
3665
3666 if (sysnum)
3667 id = atoi(sysnum);
3668 if (!sysnum || id < 0) {
3669 weston_log("couldn't get sysnum for device %s\n", filename);
3670 goto out_res;
3671 }
3672
3673 /* We can be called successfully on multiple devices; if we have,
3674 * clean up old entries. */
3675 if (b->drm.fd >= 0)
3676 weston_launcher_close(b->compositor->launcher, b->drm.fd);
3677 free(b->drm.filename);
3678
3679 b->drm.fd = fd;
3680 b->drm.id = id;
3681 b->drm.filename = strdup(filename);
3682
Sergi Granellceb59812017-03-28 12:44:04 +02003683 drmModeFreeResources(res);
3684
Daniel Stoneefa504f2016-12-19 16:48:20 +00003685 return true;
3686
3687out_res:
3688 drmModeFreeResources(res);
3689out_fd:
3690 weston_launcher_close(b->compositor->launcher, fd);
3691 return false;
3692}
3693
David Herrmann0af066f2012-10-29 19:21:16 +01003694/*
3695 * Find primary GPU
3696 * Some systems may have multiple DRM devices attached to a single seat. This
3697 * function loops over all devices and tries to find a PCI device with the
3698 * boot_vga sysfs attribute set to 1.
3699 * If no such device is found, the first DRM device reported by udev is used.
Daniel Stoneefa504f2016-12-19 16:48:20 +00003700 * Devices are also vetted to make sure they are are capable of modesetting,
3701 * rather than pure render nodes (GPU with no display), or pure
3702 * memory-allocation devices (VGEM).
David Herrmann0af066f2012-10-29 19:21:16 +01003703 */
3704static struct udev_device*
Giulio Camuffo954f1832014-10-11 18:27:30 +03003705find_primary_gpu(struct drm_backend *b, const char *seat)
David Herrmann0af066f2012-10-29 19:21:16 +01003706{
3707 struct udev_enumerate *e;
3708 struct udev_list_entry *entry;
3709 const char *path, *device_seat, *id;
3710 struct udev_device *device, *drm_device, *pci;
3711
Giulio Camuffo954f1832014-10-11 18:27:30 +03003712 e = udev_enumerate_new(b->udev);
David Herrmann0af066f2012-10-29 19:21:16 +01003713 udev_enumerate_add_match_subsystem(e, "drm");
3714 udev_enumerate_add_match_sysname(e, "card[0-9]*");
3715
3716 udev_enumerate_scan_devices(e);
3717 drm_device = NULL;
3718 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00003719 bool is_boot_vga = false;
3720
David Herrmann0af066f2012-10-29 19:21:16 +01003721 path = udev_list_entry_get_name(entry);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003722 device = udev_device_new_from_syspath(b->udev, path);
David Herrmann0af066f2012-10-29 19:21:16 +01003723 if (!device)
3724 continue;
3725 device_seat = udev_device_get_property_value(device, "ID_SEAT");
3726 if (!device_seat)
3727 device_seat = default_seat;
3728 if (strcmp(device_seat, seat)) {
3729 udev_device_unref(device);
3730 continue;
3731 }
3732
3733 pci = udev_device_get_parent_with_subsystem_devtype(device,
3734 "pci", NULL);
3735 if (pci) {
3736 id = udev_device_get_sysattr_value(pci, "boot_vga");
Daniel Stoneefa504f2016-12-19 16:48:20 +00003737 if (id && !strcmp(id, "1"))
3738 is_boot_vga = true;
David Herrmann0af066f2012-10-29 19:21:16 +01003739 }
3740
Daniel Stoneefa504f2016-12-19 16:48:20 +00003741 /* If we already have a modesetting-capable device, and this
3742 * device isn't our boot-VGA device, we aren't going to use
3743 * it. */
3744 if (!is_boot_vga && drm_device) {
David Herrmann0af066f2012-10-29 19:21:16 +01003745 udev_device_unref(device);
Daniel Stoneefa504f2016-12-19 16:48:20 +00003746 continue;
3747 }
3748
3749 /* Make sure this device is actually capable of modesetting;
3750 * if this call succeeds, b->drm.{fd,filename} will be set,
3751 * and any old values freed. */
3752 if (!drm_device_is_kms(b, device)) {
3753 udev_device_unref(device);
3754 continue;
3755 }
3756
3757 /* There can only be one boot_vga device, and we try to use it
3758 * at all costs. */
3759 if (is_boot_vga) {
3760 if (drm_device)
3761 udev_device_unref(drm_device);
3762 drm_device = device;
3763 break;
3764 }
3765
3766 /* Per the (!is_boot_vga && drm_device) test above, we only
3767 * trump existing saved devices with boot-VGA devices, so if
3768 * we end up here, this must be the first device we've seen. */
3769 assert(!drm_device);
3770 drm_device = device;
David Herrmann0af066f2012-10-29 19:21:16 +01003771 }
3772
Daniel Stoneefa504f2016-12-19 16:48:20 +00003773 /* If we're returning a device to use, we must have an open FD for
3774 * it. */
3775 assert(!!drm_device == (b->drm.fd >= 0));
3776
David Herrmann0af066f2012-10-29 19:21:16 +01003777 udev_enumerate_unref(e);
3778 return drm_device;
3779}
3780
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003781static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003782planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
3783 void *data)
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003784{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003785 struct drm_backend *b = data;
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02003786
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003787 switch (key) {
3788 case KEY_C:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003789 b->cursors_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003790 break;
3791 case KEY_V:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003792 b->sprites_are_broken ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003793 break;
3794 case KEY_O:
Giulio Camuffo954f1832014-10-11 18:27:30 +03003795 b->sprites_hidden ^= 1;
Ander Conselvan de Oliveira7e918da2012-11-22 15:56:59 +02003796 break;
3797 default:
3798 break;
3799 }
Ander Conselvan de Oliveira180f42a2012-11-21 15:11:37 +02003800}
3801
Kristian Høgsberg0eac34a2013-08-30 14:28:22 -07003802#ifdef BUILD_VAAPI_RECORDER
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003803static void
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003804recorder_destroy(struct drm_output *output)
3805{
3806 vaapi_recorder_destroy(output->recorder);
3807 output->recorder = NULL;
3808
3809 output->base.disable_planes--;
3810
3811 wl_list_remove(&output->recorder_frame_listener.link);
3812 weston_log("[libva recorder] done\n");
3813}
3814
3815static void
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003816recorder_frame_notify(struct wl_listener *listener, void *data)
3817{
3818 struct drm_output *output;
Giulio Camuffo954f1832014-10-11 18:27:30 +03003819 struct drm_backend *b;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003820 int fd, ret;
3821
3822 output = container_of(listener, struct drm_output,
3823 recorder_frame_listener);
Armin Krezović545dba62016-08-05 15:54:18 +02003824 b = to_drm_backend(output->base.compositor);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003825
3826 if (!output->recorder)
3827 return;
3828
Daniel Stone5bb8f582017-04-04 17:54:28 +01003829 ret = drmPrimeHandleToFD(b->drm.fd, output->fb_current->handle,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003830 DRM_CLOEXEC, &fd);
3831 if (ret) {
3832 weston_log("[libva recorder] "
3833 "failed to create prime fd for front buffer\n");
3834 return;
3835 }
3836
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003837 ret = vaapi_recorder_frame(output->recorder, fd,
Daniel Stone5bb8f582017-04-04 17:54:28 +01003838 output->fb_current->stride);
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003839 if (ret < 0) {
3840 weston_log("[libva recorder] aborted: %m\n");
3841 recorder_destroy(output);
3842 }
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003843}
3844
3845static void *
Giulio Camuffo954f1832014-10-11 18:27:30 +03003846create_recorder(struct drm_backend *b, int width, int height,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003847 const char *filename)
3848{
3849 int fd;
3850 drm_magic_t magic;
3851
Giulio Camuffo954f1832014-10-11 18:27:30 +03003852 fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003853 if (fd < 0)
3854 return NULL;
3855
3856 drmGetMagic(fd, &magic);
Giulio Camuffo954f1832014-10-11 18:27:30 +03003857 drmAuthMagic(b->drm.fd, magic);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003858
3859 return vaapi_recorder_create(fd, width, height, filename);
3860}
3861
3862static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003863recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003864 void *data)
3865{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003866 struct drm_backend *b = data;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003867 struct drm_output *output;
3868 int width, height;
3869
Giulio Camuffo954f1832014-10-11 18:27:30 +03003870 output = container_of(b->compositor->output_list.next,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003871 struct drm_output, base.link);
3872
3873 if (!output->recorder) {
Miguel A. Vicofcf4b6c2016-03-21 17:41:03 +01003874 if (output->gbm_format != GBM_FORMAT_XRGB8888) {
Ander Conselvan de Oliveira2ef1cd12014-05-06 16:49:06 +03003875 weston_log("failed to start vaapi recorder: "
3876 "output format not supported\n");
3877 return;
3878 }
3879
Hardeningff39efa2013-09-18 23:56:35 +02003880 width = output->base.current_mode->width;
3881 height = output->base.current_mode->height;
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003882
3883 output->recorder =
Giulio Camuffo954f1832014-10-11 18:27:30 +03003884 create_recorder(b, width, height, "capture.h264");
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003885 if (!output->recorder) {
3886 weston_log("failed to create vaapi recorder\n");
3887 return;
3888 }
3889
3890 output->base.disable_planes++;
3891
3892 output->recorder_frame_listener.notify = recorder_frame_notify;
3893 wl_signal_add(&output->base.frame_signal,
3894 &output->recorder_frame_listener);
3895
3896 weston_output_schedule_repaint(&output->base);
3897
3898 weston_log("[libva recorder] initialized\n");
3899 } else {
Ander Conselvan de Oliveira2d13fde2014-05-09 15:57:38 +03003900 recorder_destroy(output);
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003901 }
3902}
3903#else
3904static void
Bryce Harrington4a8a3a12015-07-16 19:12:26 -07003905recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
Ander Conselvan de Oliveira6aae4d32013-08-23 17:15:48 +03003906 void *data)
3907{
3908 weston_log("Compiled without libva support\n");
3909}
3910#endif
3911
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003912static void
Giulio Camuffo954f1832014-10-11 18:27:30 +03003913switch_to_gl_renderer(struct drm_backend *b)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003914{
3915 struct drm_output *output;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003916 bool dmabuf_support_inited;
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003917
Giulio Camuffo954f1832014-10-11 18:27:30 +03003918 if (!b->use_pixman)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003919 return;
3920
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003921 dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
3922
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003923 weston_log("Switching to GL renderer\n");
3924
Giulio Camuffo954f1832014-10-11 18:27:30 +03003925 b->gbm = create_gbm_device(b->drm.fd);
3926 if (!b->gbm) {
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003927 weston_log("Failed to create gbm device. "
3928 "Aborting renderer switch\n");
3929 return;
3930 }
3931
Giulio Camuffo954f1832014-10-11 18:27:30 +03003932 wl_list_for_each(output, &b->compositor->output_list, base.link)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003933 pixman_renderer_output_destroy(&output->base);
3934
Giulio Camuffo954f1832014-10-11 18:27:30 +03003935 b->compositor->renderer->destroy(b->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003936
Giulio Camuffo954f1832014-10-11 18:27:30 +03003937 if (drm_backend_create_gl_renderer(b) < 0) {
3938 gbm_device_destroy(b->gbm);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003939 weston_log("Failed to create GL renderer. Quitting.\n");
3940 /* FIXME: we need a function to shutdown cleanly */
3941 assert(0);
3942 }
3943
Giulio Camuffo954f1832014-10-11 18:27:30 +03003944 wl_list_for_each(output, &b->compositor->output_list, base.link)
3945 drm_output_init_egl(output, b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003946
Giulio Camuffo954f1832014-10-11 18:27:30 +03003947 b->use_pixman = 0;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03003948
3949 if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
3950 if (linux_dmabuf_setup(b->compositor) < 0)
3951 weston_log("Error: initializing dmabuf "
3952 "support failed.\n");
3953 }
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003954}
3955
3956static void
Derek Foreman8ae2db52015-07-15 13:00:36 -05003957renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
3958 uint32_t key, void *data)
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003959{
Derek Foreman8ae2db52015-07-15 13:00:36 -05003960 struct drm_backend *b =
Armin Krezović545dba62016-08-05 15:54:18 +02003961 to_drm_backend(keyboard->seat->compositor);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003962
Giulio Camuffo954f1832014-10-11 18:27:30 +03003963 switch_to_gl_renderer(b);
Ander Conselvan de Oliveira65796812013-11-19 15:22:04 +02003964}
3965
Armin Krezović08368132016-09-30 14:11:05 +02003966static const struct weston_drm_output_api api = {
3967 drm_output_set_mode,
3968 drm_output_set_gbm_format,
3969 drm_output_set_seat,
3970};
3971
Giulio Camuffo954f1832014-10-11 18:27:30 +03003972static struct drm_backend *
3973drm_backend_create(struct weston_compositor *compositor,
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003974 struct weston_drm_backend_config *config)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003975{
Giulio Camuffo954f1832014-10-11 18:27:30 +03003976 struct drm_backend *b;
David Herrmann0af066f2012-10-29 19:21:16 +01003977 struct udev_device *drm_device;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003978 struct wl_event_loop *loop;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07003979 const char *seat_id = default_seat;
Armin Krezović08368132016-09-30 14:11:05 +02003980 int ret;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003981
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003982 weston_log("initializing drm backend\n");
3983
Giulio Camuffo954f1832014-10-11 18:27:30 +03003984 b = zalloc(sizeof *b);
3985 if (b == NULL)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003986 return NULL;
Daniel Stone725c2c32012-06-22 14:04:36 +01003987
Daniel Stoneefa504f2016-12-19 16:48:20 +00003988 b->drm.fd = -1;
3989
Pekka Paalanen68583832015-05-19 09:53:16 +03003990 /*
3991 * KMS support for hardware planes cannot properly synchronize
3992 * without nuclear page flip. Without nuclear/atomic, hw plane
3993 * and cursor plane updates would either tear or cause extra
3994 * waits for vblanks which means dropping the compositor framerate
Pekka Paalanen3f32a132015-09-07 15:38:43 +03003995 * to a fraction. For cursors, it's not so bad, so they are
3996 * enabled.
Pekka Paalanen68583832015-05-19 09:53:16 +03003997 *
3998 * These can be enabled again when nuclear/atomic support lands.
3999 */
Giulio Camuffo954f1832014-10-11 18:27:30 +03004000 b->sprites_are_broken = 1;
Giulio Camuffo954f1832014-10-11 18:27:30 +03004001 b->compositor = compositor;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004002 b->use_pixman = config->use_pixman;
Emmanuel Gil Peyrot11ae2a32017-03-07 13:27:54 +00004003 b->pageflip_timeout = config->pageflip_timeout;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07004004
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004005 if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
4006 goto err_compositor;
Kristian Høgsberg8e6f3762013-10-16 16:31:42 -07004007
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004008 if (config->seat_id)
4009 seat_id = config->seat_id;
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004010
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004011 /* Check if we run drm-backend using weston-launch */
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004012 compositor->launcher = weston_launcher_connect(compositor, config->tty,
4013 seat_id, true);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004014 if (compositor->launcher == NULL) {
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004015 weston_log("fatal: drm backend should be run "
Kristian Høgsbergb76237e2013-04-04 21:36:20 -04004016 "using weston-launch binary or as root\n");
Rafal Mielniczuk32069c02013-03-27 18:39:28 +01004017 goto err_compositor;
4018 }
4019
Giulio Camuffo954f1832014-10-11 18:27:30 +03004020 b->udev = udev_new();
4021 if (b->udev == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02004022 weston_log("failed to initialize udev context\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004023 goto err_launcher;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004024 }
4025
Giulio Camuffo954f1832014-10-11 18:27:30 +03004026 b->session_listener.notify = session_notify;
4027 wl_signal_add(&compositor->session_signal, &b->session_listener);
Kristian Høgsbergc5b9ddb2012-01-15 14:29:09 -05004028
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004029 drm_device = find_primary_gpu(b, seat_id);
Kristian Høgsberg8d51f142011-07-15 21:28:38 -04004030 if (drm_device == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02004031 weston_log("no drm device found\n");
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004032 goto err_udev;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004033 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004034
Daniel Stoneefa504f2016-12-19 16:48:20 +00004035 if (init_kms_caps(b) < 0) {
Ander Conselvan de Oliveira22929172013-01-25 15:13:02 +02004036 weston_log("failed to initialize kms\n");
4037 goto err_udev_dev;
4038 }
4039
Giulio Camuffo954f1832014-10-11 18:27:30 +03004040 if (b->use_pixman) {
4041 if (init_pixman(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004042 weston_log("failed to initialize pixman renderer\n");
4043 goto err_udev_dev;
4044 }
4045 } else {
Giulio Camuffo954f1832014-10-11 18:27:30 +03004046 if (init_egl(b) < 0) {
Ander Conselvan de Oliveira5fb44142013-01-25 15:13:05 +02004047 weston_log("failed to initialize egl\n");
4048 goto err_udev_dev;
4049 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004050 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -05004051
Giulio Camuffo954f1832014-10-11 18:27:30 +03004052 b->base.destroy = drm_destroy;
4053 b->base.restore = drm_restore;
Daniel Stoneeedf84c2017-02-10 18:06:04 +00004054 b->base.repaint_begin = drm_repaint_begin;
4055 b->base.repaint_flush = drm_repaint_flush;
4056 b->base.repaint_cancel = drm_repaint_cancel;
Benjamin Franzke431da9a2011-04-20 11:02:58 +02004057
Bob Ham91880f12016-01-12 10:21:47 +00004058 weston_setup_vt_switch_bindings(compositor);
Kristian Høgsberg5d1c0c52012-04-10 00:11:50 -04004059
Daniel Stone085d2b92015-05-21 00:00:57 +01004060 wl_list_init(&b->plane_list);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004061 create_sprites(b);
Jesse Barnes58ef3792012-02-23 09:45:49 -05004062
Giulio Camuffo954f1832014-10-11 18:27:30 +03004063 if (udev_input_init(&b->input,
Giulio Camuffo8aedf7b2016-06-02 21:48:12 +03004064 compositor, b->udev, seat_id,
4065 config->configure_device) < 0) {
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004066 weston_log("failed to create input devices\n");
4067 goto err_sprite;
4068 }
4069
Ucan, Emre (ADITG/SW1)21e49442017-02-02 14:06:55 +00004070 b->connector = config->connector;
4071
Ucan, Emre (ADITG/SW1)9a200d72017-02-02 14:06:56 +00004072 if (create_outputs(b, drm_device) < 0) {
Daniel Stoneefa504f2016-12-19 16:48:20 +00004073 weston_log("failed to create output for %s\n", b->drm.filename);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004074 goto err_udev_input;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004075 }
4076
Jason Ekstrand9fc71512014-04-02 19:53:46 -05004077 /* A this point we have some idea of whether or not we have a working
4078 * cursor plane. */
Giulio Camuffo954f1832014-10-11 18:27:30 +03004079 if (!b->cursors_are_broken)
4080 compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
Jason Ekstrand9fc71512014-04-02 19:53:46 -05004081
Giulio Camuffo954f1832014-10-11 18:27:30 +03004082 loop = wl_display_get_event_loop(compositor->wl_display);
4083 b->drm_source =
4084 wl_event_loop_add_fd(loop, b->drm.fd,
4085 WL_EVENT_READABLE, on_drm_input, b);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004086
Giulio Camuffo954f1832014-10-11 18:27:30 +03004087 b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
4088 if (b->udev_monitor == NULL) {
Abdur Rehman4dca0e12017-01-01 19:46:35 +05004089 weston_log("failed to initialize udev monitor\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01004090 goto err_drm_source;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004091 }
Giulio Camuffo954f1832014-10-11 18:27:30 +03004092 udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004093 "drm", NULL);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004094 b->udev_drm_source =
Benjamin Franzke117483d2011-08-30 11:38:26 +02004095 wl_event_loop_add_fd(loop,
Giulio Camuffo954f1832014-10-11 18:27:30 +03004096 udev_monitor_get_fd(b->udev_monitor),
4097 WL_EVENT_READABLE, udev_drm_event, b);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004098
Giulio Camuffo954f1832014-10-11 18:27:30 +03004099 if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02004100 weston_log("failed to enable udev-monitor receiving\n");
Daniel Stonea96b93c2012-06-22 14:04:37 +01004101 goto err_udev_monitor;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004102 }
4103
Daniel Stonea96b93c2012-06-22 14:04:37 +01004104 udev_device_unref(drm_device);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004105
Giulio Camuffo954f1832014-10-11 18:27:30 +03004106 weston_compositor_add_debug_binding(compositor, KEY_O,
4107 planes_binding, b);
4108 weston_compositor_add_debug_binding(compositor, KEY_C,
4109 planes_binding, b);
4110 weston_compositor_add_debug_binding(compositor, KEY_V,
4111 planes_binding, b);
4112 weston_compositor_add_debug_binding(compositor, KEY_Q,
4113 recorder_binding, b);
4114 weston_compositor_add_debug_binding(compositor, KEY_W,
4115 renderer_switch_binding, b);
Ander Conselvan de Oliveirada1c9082012-10-31 17:55:46 +02004116
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004117 if (compositor->renderer->import_dmabuf) {
4118 if (linux_dmabuf_setup(compositor) < 0)
4119 weston_log("Error: initializing dmabuf "
4120 "support failed.\n");
4121 }
4122
Giulio Camuffo954f1832014-10-11 18:27:30 +03004123 compositor->backend = &b->base;
Pekka Paalanene4d231e2014-06-12 15:12:48 +03004124
Armin Krezović08368132016-09-30 14:11:05 +02004125 ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME,
4126 &api, sizeof(api));
4127
4128 if (ret < 0) {
4129 weston_log("Failed to register output API.\n");
4130 goto err_udev_monitor;
4131 }
4132
Giulio Camuffo954f1832014-10-11 18:27:30 +03004133 return b;
Daniel Stonea96b93c2012-06-22 14:04:37 +01004134
4135err_udev_monitor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004136 wl_event_source_remove(b->udev_drm_source);
4137 udev_monitor_unref(b->udev_monitor);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004138err_drm_source:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004139 wl_event_source_remove(b->drm_source);
Ander Conselvan de Oliveira4ade0e42014-04-17 13:08:45 +03004140err_udev_input:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004141 udev_input_destroy(&b->input);
Kristian Høgsberg2bc5e8e2012-09-06 20:51:00 -04004142err_sprite:
Emmanuel Gil Peyrotb8347e32016-05-02 22:40:13 +01004143 if (b->gbm)
4144 gbm_device_destroy(b->gbm);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004145 destroy_sprites(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004146err_udev_dev:
4147 udev_device_unref(drm_device);
Kristian Høgsberg3f495872013-09-18 23:00:17 -07004148err_launcher:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004149 weston_launcher_destroy(compositor->launcher);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004150err_udev:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004151 udev_unref(b->udev);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004152err_compositor:
Giulio Camuffo954f1832014-10-11 18:27:30 +03004153 weston_compositor_shutdown(compositor);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004154 free(b);
Daniel Stonea96b93c2012-06-22 14:04:37 +01004155 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04004156}
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004157
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004158static void
4159config_init_to_defaults(struct weston_drm_backend_config *config)
4160{
4161}
4162
Giulio Camuffo954f1832014-10-11 18:27:30 +03004163WL_EXPORT int
Quentin Glidic23e1d6f2016-12-02 14:08:44 +01004164weston_backend_init(struct weston_compositor *compositor,
4165 struct weston_backend_config *config_base)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004166{
Giulio Camuffo954f1832014-10-11 18:27:30 +03004167 struct drm_backend *b;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004168 struct weston_drm_backend_config config = {{ 0, }};
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004169
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004170 if (config_base == NULL ||
4171 config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
4172 config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
4173 weston_log("drm backend config structure is invalid\n");
4174 return -1;
4175 }
Benjamin Franzke117483d2011-08-30 11:38:26 +02004176
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004177 config_init_to_defaults(&config);
4178 memcpy(&config, config_base, config_base->struct_size);
Kristian Høgsbergd8e98332013-10-16 16:15:11 -07004179
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004180 b = drm_backend_create(compositor, &config);
Giulio Camuffo954f1832014-10-11 18:27:30 +03004181 if (b == NULL)
4182 return -1;
Giulio Camuffo1c0e40d2016-04-29 15:40:34 -07004183
Giulio Camuffo954f1832014-10-11 18:27:30 +03004184 return 0;
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004185}