Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 5 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 12 | * |
Bryce Harrington | a0bbfea | 2015-06-11 15:35:43 -0700 | [diff] [blame] | 13 | * 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øgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
Daniel Stone | c228e23 | 2013-05-22 18:03:19 +0300 | [diff] [blame] | 27 | #include "config.h" |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 28 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 29 | #include <errno.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 30 | #include <stdint.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 31 | #include <stdlib.h> |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 32 | #include <ctype.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 36 | #include <linux/input.h> |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 37 | #include <linux/vt.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 38 | #include <assert.h> |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 39 | #include <sys/mman.h> |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 40 | #include <dlfcn.h> |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 41 | #include <time.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 43 | #include <xf86drm.h> |
| 44 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 45 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 46 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 47 | #include <gbm.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 48 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 49 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 50 | #include "compositor.h" |
| 51 | #include "compositor-drm.h" |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 52 | #include "shared/helpers.h" |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 53 | #include "shared/timespec-util.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 54 | #include "gl-renderer.h" |
Vincent Abriou | c950667 | 2016-10-05 16:14:07 +0200 | [diff] [blame] | 55 | #include "weston-egl-ext.h" |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 56 | #include "pixman-renderer.h" |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 57 | #include "libbacklight.h" |
Peter Hutterer | 823ad33 | 2014-11-26 07:06:31 +1000 | [diff] [blame] | 58 | #include "libinput-seat.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 59 | #include "launcher-util.h" |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 60 | #include "vaapi-recorder.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 61 | #include "presentation-time-server-protocol.h" |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 62 | #include "linux-dmabuf.h" |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 63 | #include "linux-dmabuf-unstable-v1-server-protocol.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 64 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 65 | #ifndef DRM_CAP_TIMESTAMP_MONOTONIC |
| 66 | #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 |
| 67 | #endif |
| 68 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 69 | #ifndef DRM_CAP_CURSOR_WIDTH |
| 70 | #define DRM_CAP_CURSOR_WIDTH 0x8 |
| 71 | #endif |
| 72 | |
| 73 | #ifndef DRM_CAP_CURSOR_HEIGHT |
| 74 | #define DRM_CAP_CURSOR_HEIGHT 0x9 |
| 75 | #endif |
| 76 | |
| 77 | #ifndef GBM_BO_USE_CURSOR |
| 78 | #define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64 |
| 79 | #endif |
| 80 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 81 | struct drm_backend { |
| 82 | struct weston_backend base; |
| 83 | struct weston_compositor *compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 84 | |
| 85 | struct udev *udev; |
| 86 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 87 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 88 | struct udev_monitor *udev_monitor; |
| 89 | struct wl_event_source *udev_drm_source; |
| 90 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 91 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 92 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 93 | int fd; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 94 | char *filename; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 95 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 96 | struct gbm_device *gbm; |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 97 | struct wl_listener session_listener; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 98 | uint32_t gbm_format; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 99 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 100 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 101 | * due to out of bounds dimensions, and then mistakenly set |
| 102 | * sprites_are_broken: |
| 103 | */ |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 104 | int min_width, max_width; |
| 105 | int min_height, max_height; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 106 | int no_addfb2; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 107 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 108 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 109 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 110 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 111 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 112 | int cursors_are_broken; |
| 113 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 114 | int use_pixman; |
| 115 | |
Rob Bradford | d355b80 | 2013-05-31 18:09:55 +0100 | [diff] [blame] | 116 | struct udev_input input; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 117 | |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 118 | int32_t cursor_width; |
| 119 | int32_t cursor_height; |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 120 | |
| 121 | uint32_t connector; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 122 | uint32_t pageflip_timeout; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 123 | }; |
| 124 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 125 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 126 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 127 | drmModeModeInfo mode_info; |
| 128 | }; |
| 129 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 130 | struct drm_fb { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 131 | uint32_t fb_id, stride, handle, size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 132 | int width, height; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 133 | int fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 134 | int is_client_buffer; |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 135 | struct weston_buffer_reference buffer_ref; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 136 | |
| 137 | /* Used by gbm fbs */ |
| 138 | struct gbm_bo *bo; |
| 139 | |
| 140 | /* Used by dumb fbs */ |
| 141 | void *map; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 142 | }; |
| 143 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 144 | struct drm_edid { |
| 145 | char eisa_id[13]; |
| 146 | char monitor_name[13]; |
| 147 | char pnp_id[5]; |
| 148 | char serial_number[13]; |
| 149 | }; |
| 150 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 151 | struct drm_output { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 152 | struct weston_output base; |
| 153 | drmModeConnector *connector; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 154 | |
Daniel Stone | 1733923 | 2015-11-28 12:09:47 +0000 | [diff] [blame] | 155 | uint32_t crtc_id; /* object ID to pass to DRM functions */ |
| 156 | int pipe; /* index of CRTC in resource array / bitmasks */ |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 157 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 158 | drmModeCrtcPtr original_crtc; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 159 | struct drm_edid edid; |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 160 | drmModePropertyPtr dpms_prop; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 161 | uint32_t gbm_format; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 162 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 163 | enum dpms_enum dpms; |
| 164 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 165 | int vblank_pending; |
| 166 | int page_flip_pending; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 167 | int destroy_pending; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 168 | int disable_pending; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 169 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 170 | struct gbm_surface *gbm_surface; |
| 171 | struct gbm_bo *gbm_cursor_bo[2]; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 172 | struct weston_plane cursor_plane; |
| 173 | struct weston_plane fb_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 174 | struct weston_view *cursor_view; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 175 | int current_cursor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 176 | struct drm_fb *current, *next; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 177 | struct backlight *backlight; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 178 | |
| 179 | struct drm_fb *dumb[2]; |
| 180 | pixman_image_t *image[2]; |
| 181 | int current_image; |
| 182 | pixman_region32_t previous_damage; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 183 | |
| 184 | struct vaapi_recorder *recorder; |
| 185 | struct wl_listener recorder_frame_listener; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 186 | |
| 187 | struct wl_event_source *pageflip_timer; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 188 | }; |
| 189 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 190 | /* |
| 191 | * An output has a primary display plane plus zero or more sprites for |
| 192 | * blending display contents. |
| 193 | */ |
| 194 | struct drm_sprite { |
| 195 | struct wl_list link; |
| 196 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 197 | struct weston_plane plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 198 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 199 | struct drm_fb *current, *next; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 200 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 201 | struct drm_backend *backend; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 202 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 203 | uint32_t possible_crtcs; |
| 204 | uint32_t plane_id; |
| 205 | uint32_t count_formats; |
| 206 | |
| 207 | int32_t src_x, src_y; |
| 208 | uint32_t src_w, src_h; |
| 209 | uint32_t dest_x, dest_y; |
| 210 | uint32_t dest_w, dest_h; |
| 211 | |
| 212 | uint32_t formats[]; |
| 213 | }; |
| 214 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 215 | static struct gl_renderer_interface *gl_renderer; |
| 216 | |
Kristian Høgsberg | 98cfea6 | 2013-02-18 16:15:53 -0500 | [diff] [blame] | 217 | static const char default_seat[] = "seat0"; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 218 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 219 | static inline struct drm_output * |
| 220 | to_drm_output(struct weston_output *base) |
| 221 | { |
| 222 | return container_of(base, struct drm_output, base); |
| 223 | } |
| 224 | |
| 225 | static inline struct drm_backend * |
| 226 | to_drm_backend(struct weston_compositor *base) |
| 227 | { |
| 228 | return container_of(base->backend, struct drm_backend, base); |
| 229 | } |
| 230 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 231 | static int |
| 232 | pageflip_timeout(void *data) { |
| 233 | /* |
| 234 | * Our timer just went off, that means we're not receiving drm |
| 235 | * page flip events anymore for that output. Let's gracefully exit |
| 236 | * weston with a return value so devs can debug what's going on. |
| 237 | */ |
| 238 | struct drm_output *output = data; |
| 239 | struct weston_compositor *compositor = output->base.compositor; |
| 240 | |
| 241 | weston_log("Pageflip timeout reached on output %s, your " |
| 242 | "driver is probably buggy! Exiting.\n", |
| 243 | output->base.name); |
| 244 | weston_compositor_exit_with_code(compositor, EXIT_FAILURE); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /* Creates the pageflip timer. Note that it isn't armed by default */ |
| 250 | static int |
| 251 | drm_output_pageflip_timer_create(struct drm_output *output) |
| 252 | { |
| 253 | struct wl_event_loop *loop = NULL; |
| 254 | struct weston_compositor *ec = output->base.compositor; |
| 255 | |
| 256 | loop = wl_display_get_event_loop(ec->wl_display); |
| 257 | assert(loop); |
| 258 | output->pageflip_timer = wl_event_loop_add_timer(loop, |
| 259 | pageflip_timeout, |
| 260 | output); |
| 261 | |
| 262 | if (output->pageflip_timer == NULL) { |
| 263 | weston_log("creating drm pageflip timer failed: %m\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 270 | static void |
| 271 | drm_output_set_cursor(struct drm_output *output); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 272 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 273 | static void |
| 274 | drm_output_update_msc(struct drm_output *output, unsigned int seq); |
| 275 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 276 | static int |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 277 | drm_sprite_crtc_supported(struct drm_output *output, struct drm_sprite *sprite) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 278 | { |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 279 | return !!(sprite->possible_crtcs & (1 << output->pipe)); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 280 | } |
| 281 | |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 282 | static struct drm_output * |
| 283 | drm_output_find_by_crtc(struct drm_backend *b, uint32_t crtc_id) |
| 284 | { |
| 285 | struct drm_output *output; |
| 286 | |
| 287 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 288 | if (output->crtc_id == crtc_id) |
| 289 | return output; |
| 290 | } |
| 291 | |
| 292 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 293 | base.link) { |
| 294 | if (output->crtc_id == crtc_id) |
| 295 | return output; |
| 296 | } |
| 297 | |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 301 | static struct drm_output * |
| 302 | drm_output_find_by_connector(struct drm_backend *b, uint32_t connector_id) |
| 303 | { |
| 304 | struct drm_output *output; |
| 305 | |
| 306 | wl_list_for_each(output, &b->compositor->output_list, base.link) { |
| 307 | if (output->connector_id == connector_id) |
| 308 | return output; |
| 309 | } |
| 310 | |
| 311 | wl_list_for_each(output, &b->compositor->pending_output_list, |
| 312 | base.link) { |
| 313 | if (output->connector_id == connector_id) |
| 314 | return output; |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 320 | static void |
| 321 | drm_fb_destroy_callback(struct gbm_bo *bo, void *data) |
| 322 | { |
| 323 | struct drm_fb *fb = data; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 324 | |
| 325 | if (fb->fb_id) |
Daniel Stone | 4e5eceb | 2015-11-30 16:07:19 +0000 | [diff] [blame] | 326 | drmModeRmFB(fb->fd, fb->fb_id); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 327 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 328 | weston_buffer_reference(&fb->buffer_ref, NULL); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 329 | |
| 330 | free(data); |
| 331 | } |
| 332 | |
| 333 | static struct drm_fb * |
Daniel Stone | f214fdc | 2016-11-14 17:43:57 +0000 | [diff] [blame] | 334 | drm_fb_create_dumb(struct drm_backend *b, int width, int height, |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 335 | uint32_t format) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 336 | { |
| 337 | struct drm_fb *fb; |
| 338 | int ret; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 339 | uint32_t bpp, depth; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 340 | |
| 341 | struct drm_mode_create_dumb create_arg; |
| 342 | struct drm_mode_destroy_dumb destroy_arg; |
| 343 | struct drm_mode_map_dumb map_arg; |
| 344 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 345 | fb = zalloc(sizeof *fb); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 346 | if (!fb) |
| 347 | return NULL; |
| 348 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 349 | switch (format) { |
| 350 | case GBM_FORMAT_XRGB8888: |
| 351 | bpp = 32; |
| 352 | depth = 24; |
| 353 | break; |
| 354 | case GBM_FORMAT_RGB565: |
| 355 | bpp = depth = 16; |
| 356 | break; |
| 357 | default: |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 361 | memset(&create_arg, 0, sizeof create_arg); |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 362 | create_arg.bpp = bpp; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 363 | create_arg.width = width; |
| 364 | create_arg.height = height; |
| 365 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 366 | ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 367 | if (ret) |
| 368 | goto err_fb; |
| 369 | |
| 370 | fb->handle = create_arg.handle; |
| 371 | fb->stride = create_arg.pitch; |
| 372 | fb->size = create_arg.size; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 373 | fb->width = width; |
| 374 | fb->height = height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 375 | fb->fd = b->drm.fd; |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 376 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 377 | ret = -1; |
| 378 | |
| 379 | if (!b->no_addfb2) { |
Yong Bakos | 4b6321f | 2016-08-17 17:37:55 -0700 | [diff] [blame] | 380 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 381 | |
| 382 | handles[0] = fb->handle; |
| 383 | pitches[0] = fb->stride; |
| 384 | offsets[0] = 0; |
| 385 | |
| 386 | ret = drmModeAddFB2(b->drm.fd, width, height, |
| 387 | format, handles, pitches, offsets, |
| 388 | &fb->fb_id, 0); |
| 389 | if (ret) { |
| 390 | weston_log("addfb2 failed: %m\n"); |
| 391 | b->no_addfb2 = 1; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | if (ret) { |
| 396 | ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp, |
| 397 | fb->stride, fb->handle, &fb->fb_id); |
| 398 | } |
| 399 | |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 400 | if (ret) |
| 401 | goto err_bo; |
| 402 | |
Kristian Høgsberg | ac6104e | 2013-08-21 22:14:14 -0700 | [diff] [blame] | 403 | memset(&map_arg, 0, sizeof map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 404 | map_arg.handle = fb->handle; |
Chris Michael | eb2074a | 2013-05-01 21:26:02 -0400 | [diff] [blame] | 405 | ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 406 | if (ret) |
| 407 | goto err_add_fb; |
| 408 | |
Chris Michael | 4a7ce1f | 2015-11-10 10:40:37 -0500 | [diff] [blame] | 409 | fb->map = mmap(NULL, fb->size, PROT_WRITE, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 410 | MAP_SHARED, b->drm.fd, map_arg.offset); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 411 | if (fb->map == MAP_FAILED) |
| 412 | goto err_add_fb; |
| 413 | |
| 414 | return fb; |
| 415 | |
| 416 | err_add_fb: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 417 | drmModeRmFB(b->drm.fd, fb->fb_id); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 418 | err_bo: |
| 419 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 420 | destroy_arg.handle = create_arg.handle; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 421 | drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 422 | err_fb: |
| 423 | free(fb); |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | static void |
| 428 | drm_fb_destroy_dumb(struct drm_fb *fb) |
| 429 | { |
| 430 | struct drm_mode_destroy_dumb destroy_arg; |
| 431 | |
| 432 | if (!fb->map) |
| 433 | return; |
| 434 | |
| 435 | if (fb->fb_id) |
| 436 | drmModeRmFB(fb->fd, fb->fb_id); |
| 437 | |
| 438 | weston_buffer_reference(&fb->buffer_ref, NULL); |
| 439 | |
| 440 | munmap(fb->map, fb->size); |
| 441 | |
| 442 | memset(&destroy_arg, 0, sizeof(destroy_arg)); |
| 443 | destroy_arg.handle = fb->handle; |
| 444 | drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); |
| 445 | |
| 446 | free(fb); |
| 447 | } |
| 448 | |
| 449 | static struct drm_fb * |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 450 | drm_fb_get_from_bo(struct gbm_bo *bo, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 451 | struct drm_backend *backend, uint32_t format) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 452 | { |
| 453 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Derek Foreman | 482ffdf | 2016-07-08 12:50:57 -0500 | [diff] [blame] | 454 | uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 }; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 455 | int ret; |
| 456 | |
| 457 | if (fb) |
| 458 | return fb; |
| 459 | |
Bryce Harrington | de16d89 | 2014-11-20 22:21:57 -0800 | [diff] [blame] | 460 | fb = zalloc(sizeof *fb); |
| 461 | if (fb == NULL) |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 462 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 463 | |
| 464 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 465 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 466 | fb->width = gbm_bo_get_width(bo); |
| 467 | fb->height = gbm_bo_get_height(bo); |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 468 | fb->stride = gbm_bo_get_stride(bo); |
| 469 | fb->handle = gbm_bo_get_handle(bo).u32; |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 470 | fb->size = fb->stride * fb->height; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 471 | fb->fd = backend->drm.fd; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 472 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 473 | if (backend->min_width > fb->width || |
| 474 | fb->width > backend->max_width || |
| 475 | backend->min_height > fb->height || |
| 476 | fb->height > backend->max_height) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 477 | weston_log("bo geometry out of bounds\n"); |
| 478 | goto err_free; |
| 479 | } |
| 480 | |
| 481 | ret = -1; |
| 482 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 483 | if (format && !backend->no_addfb2) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 484 | handles[0] = fb->handle; |
| 485 | pitches[0] = fb->stride; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 486 | offsets[0] = 0; |
| 487 | |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 488 | ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 489 | format, handles, pitches, offsets, |
| 490 | &fb->fb_id, 0); |
| 491 | if (ret) { |
| 492 | weston_log("addfb2 failed: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 493 | backend->no_addfb2 = 1; |
| 494 | backend->sprites_are_broken = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | if (ret) |
Daniel Stone | c8c917c | 2016-11-14 17:45:58 +0000 | [diff] [blame] | 499 | ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height, |
| 500 | 24, 32, fb->stride, fb->handle, &fb->fb_id); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 501 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 502 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 503 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 504 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); |
| 508 | |
| 509 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 510 | |
| 511 | err_free: |
| 512 | free(fb); |
| 513 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void |
Jason Ekstrand | 6bd6294 | 2013-06-20 20:38:23 -0500 | [diff] [blame] | 517 | drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer) |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 518 | { |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 519 | assert(fb->buffer_ref.buffer == NULL); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 520 | |
| 521 | fb->is_client_buffer = 1; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 522 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 523 | weston_buffer_reference(&fb->buffer_ref, buffer); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 526 | static void |
| 527 | drm_output_release_fb(struct drm_output *output, struct drm_fb *fb) |
| 528 | { |
| 529 | if (!fb) |
| 530 | return; |
| 531 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 532 | if (fb->map && |
| 533 | (fb != output->dumb[0] && fb != output->dumb[1])) { |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 534 | drm_fb_destroy_dumb(fb); |
| 535 | } else if (fb->bo) { |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 536 | if (fb->is_client_buffer) |
| 537 | gbm_bo_destroy(fb->bo); |
| 538 | else |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 539 | gbm_surface_release_buffer(output->gbm_surface, |
Jason Ekstrand | 3ec57f5 | 2013-11-14 20:52:35 -0600 | [diff] [blame] | 540 | fb->bo); |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 544 | static int |
| 545 | drm_view_transform_supported(struct weston_view *ev) |
| 546 | { |
| 547 | return !ev->transform.enabled || |
| 548 | (ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE); |
| 549 | } |
| 550 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 551 | static uint32_t |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 552 | drm_output_check_scanout_format(struct drm_output *output, |
| 553 | struct weston_surface *es, struct gbm_bo *bo) |
| 554 | { |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 555 | uint32_t format; |
| 556 | pixman_region32_t r; |
| 557 | |
| 558 | format = gbm_bo_get_format(bo); |
| 559 | |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 560 | if (format == GBM_FORMAT_ARGB8888) { |
| 561 | /* We can scanout an ARGB buffer if the surface's |
| 562 | * opaque region covers the whole output, but we have |
| 563 | * to use XRGB as the KMS format code. */ |
Kristian Høgsberg | 1be87e3 | 2014-01-17 14:22:41 -0800 | [diff] [blame] | 564 | pixman_region32_init_rect(&r, 0, 0, |
| 565 | output->base.width, |
| 566 | output->base.height); |
| 567 | pixman_region32_subtract(&r, &r, &es->opaque); |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 568 | |
| 569 | if (!pixman_region32_not_empty(&r)) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 570 | format = GBM_FORMAT_XRGB8888; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 571 | |
| 572 | pixman_region32_fini(&r); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 573 | } |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 574 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 575 | if (output->gbm_format == format) |
Kristian Høgsberg | 3f97b34 | 2013-10-16 16:08:57 -0700 | [diff] [blame] | 576 | return format; |
| 577 | |
| 578 | return 0; |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 581 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 582 | drm_output_prepare_scanout_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 583 | struct weston_view *ev) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 584 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 585 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 586 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 587 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 588 | struct gbm_bo *bo; |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 589 | uint32_t format; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 590 | |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 591 | /* Don't import buffers which span multiple outputs. */ |
| 592 | if (ev->output_mask != (1u << output->base.id)) |
| 593 | return NULL; |
| 594 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 595 | /* We use GBM to import buffers. */ |
| 596 | if (b->gbm == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 597 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 598 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 599 | if (buffer == NULL) |
| 600 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 601 | if (wl_shm_buffer_get(buffer->resource)) |
| 602 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 603 | |
| 604 | /* Make sure our view is exactly compatible with the output. */ |
| 605 | if (ev->geometry.x != output->base.x || |
| 606 | ev->geometry.y != output->base.y) |
| 607 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 608 | if (buffer->width != output->base.current_mode->width || |
| 609 | buffer->height != output->base.current_mode->height) |
| 610 | return NULL; |
| 611 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 612 | if (ev->transform.enabled) |
| 613 | return NULL; |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 614 | if (ev->geometry.scissor_enabled) |
| 615 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 616 | if (viewport->buffer.transform != output->base.transform) |
| 617 | return NULL; |
Daniel Stone | 9064887 | 2016-10-21 18:08:37 +0100 | [diff] [blame] | 618 | if (viewport->buffer.scale != output->base.current_scale) |
| 619 | return NULL; |
| 620 | if (!drm_view_transform_supported(ev)) |
| 621 | return NULL; |
| 622 | |
| 623 | if (ev->alpha != 1.0f) |
| 624 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 625 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 626 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
Kristian Høgsberg | 6399646 | 2013-09-03 22:27:08 -0700 | [diff] [blame] | 627 | buffer->resource, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 628 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 629 | /* Unable to use the buffer for scanout */ |
| 630 | if (!bo) |
| 631 | return NULL; |
| 632 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 633 | format = drm_output_check_scanout_format(output, ev->surface, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 634 | if (format == 0) { |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 635 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 636 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 637 | } |
| 638 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 639 | output->next = drm_fb_get_from_bo(bo, b, format); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 640 | if (!output->next) { |
| 641 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 642 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 643 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 644 | |
Pekka Paalanen | de685b8 | 2012-12-04 15:58:12 +0200 | [diff] [blame] | 645 | drm_fb_set_buffer(output->next, buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 646 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 647 | return &output->fb_plane; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 648 | } |
| 649 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 650 | static void |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 651 | drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 652 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 653 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 654 | struct gbm_bo *bo; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 655 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 656 | output->base.compositor->renderer->repaint_output(&output->base, |
| 657 | damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 658 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 659 | bo = gbm_surface_lock_front_buffer(output->gbm_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 660 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 661 | weston_log("failed to lock front buffer: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 662 | return; |
| 663 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 664 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 665 | output->next = drm_fb_get_from_bo(bo, b, output->gbm_format); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 666 | if (!output->next) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 667 | weston_log("failed to get drm_fb for bo\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 668 | gbm_surface_release_buffer(output->gbm_surface, bo); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 669 | return; |
| 670 | } |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | static void |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 674 | drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage) |
| 675 | { |
| 676 | struct weston_compositor *ec = output->base.compositor; |
| 677 | pixman_region32_t total_damage, previous_damage; |
| 678 | |
| 679 | pixman_region32_init(&total_damage); |
| 680 | pixman_region32_init(&previous_damage); |
| 681 | |
| 682 | pixman_region32_copy(&previous_damage, damage); |
| 683 | |
| 684 | pixman_region32_union(&total_damage, damage, &output->previous_damage); |
| 685 | pixman_region32_copy(&output->previous_damage, &previous_damage); |
| 686 | |
| 687 | output->current_image ^= 1; |
| 688 | |
| 689 | output->next = output->dumb[output->current_image]; |
| 690 | pixman_renderer_output_set_buffer(&output->base, |
| 691 | output->image[output->current_image]); |
| 692 | |
| 693 | ec->renderer->repaint_output(&output->base, &total_damage); |
| 694 | |
| 695 | pixman_region32_fini(&total_damage); |
| 696 | pixman_region32_fini(&previous_damage); |
| 697 | } |
| 698 | |
| 699 | static void |
| 700 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
| 701 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 702 | struct weston_compositor *c = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 703 | struct drm_backend *b = to_drm_backend(c); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 704 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 705 | if (b->use_pixman) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 706 | drm_output_render_pixman(output, damage); |
| 707 | else |
| 708 | drm_output_render_gl(output, damage); |
| 709 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 710 | pixman_region32_subtract(&c->primary_plane.damage, |
| 711 | &c->primary_plane.damage, damage); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 715 | drm_output_set_gamma(struct weston_output *output_base, |
| 716 | uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b) |
| 717 | { |
| 718 | int rc; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 719 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 720 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 721 | to_drm_backend(output->base.compositor); |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 722 | |
| 723 | /* check */ |
| 724 | if (output_base->gamma_size != size) |
| 725 | return; |
| 726 | if (!output->original_crtc) |
| 727 | return; |
| 728 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 729 | rc = drmModeCrtcSetGamma(backend->drm.fd, |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 730 | output->crtc_id, |
| 731 | size, r, g, b); |
| 732 | if (rc) |
| 733 | weston_log("set gamma failed: %m\n"); |
| 734 | } |
| 735 | |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 736 | /* Determine the type of vblank synchronization to use for the output. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 737 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 738 | * The pipe parameter indicates which CRTC is in use. Knowing this, we |
| 739 | * can determine which vblank sequence type to use for it. Traditional |
| 740 | * cards had only two CRTCs, with CRTC 0 using no special flags, and |
| 741 | * CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe |
| 742 | * parameter indicates this. |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 743 | * |
Bryce Harrington | ada4f07 | 2015-06-30 13:25:46 -0700 | [diff] [blame] | 744 | * Bits 1-5 of the pipe parameter are 5 bit wide pipe number between |
| 745 | * 0-31. If this is non-zero it indicates we're dealing with a |
| 746 | * multi-gpu situation and we need to calculate the vblank sync |
| 747 | * using DRM_BLANK_HIGH_CRTC_MASK. |
| 748 | */ |
Pekka Paalanen | c8a1ff0 | 2015-07-02 15:06:08 +0300 | [diff] [blame] | 749 | static unsigned int |
| 750 | drm_waitvblank_pipe(struct drm_output *output) |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 751 | { |
| 752 | if (output->pipe > 1) |
| 753 | return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) & |
| 754 | DRM_VBLANK_HIGH_CRTC_MASK; |
| 755 | else if (output->pipe > 0) |
| 756 | return DRM_VBLANK_SECONDARY; |
| 757 | else |
| 758 | return 0; |
| 759 | } |
| 760 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 761 | static int |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 762 | drm_output_repaint(struct weston_output *output_base, |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 763 | pixman_region32_t *damage, |
| 764 | void *repaint_data) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 765 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 766 | struct drm_output *output = to_drm_output(output_base); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 767 | struct drm_backend *backend = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 768 | to_drm_backend(output->base.compositor); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 769 | struct drm_sprite *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 770 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 771 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 772 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 773 | if (output->disable_pending || output->destroy_pending) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 774 | return -1; |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 775 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 776 | if (!output->next) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 777 | drm_output_render(output, damage); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 778 | if (!output->next) |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 779 | return -1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 780 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 781 | mode = container_of(output->base.current_mode, struct drm_mode, base); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 782 | if (!output->current || |
| 783 | output->current->stride != output->next->stride) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 784 | ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 785 | output->next->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 786 | &output->connector_id, 1, |
| 787 | &mode->mode_info); |
| 788 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 789 | weston_log("set mode failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 790 | goto err_pageflip; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 791 | } |
Ander Conselvan de Oliveira | 722a2d5 | 2013-06-04 16:24:05 +0300 | [diff] [blame] | 792 | output_base->set_dpms(output_base, WESTON_DPMS_ON); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 795 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 796 | output->next->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 797 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 798 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 799 | goto err_pageflip; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 800 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 801 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 802 | output->page_flip_pending = 1; |
| 803 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 804 | if (output->pageflip_timer) |
| 805 | wl_event_source_timer_update(output->pageflip_timer, |
| 806 | backend->pageflip_timeout); |
| 807 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 808 | drm_output_set_cursor(output); |
| 809 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 810 | /* |
| 811 | * Now, update all the sprite surfaces |
| 812 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 813 | wl_list_for_each(s, &backend->sprite_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 814 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 815 | drmVBlank vbl = { |
| 816 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 817 | .request.sequence = 1, |
| 818 | }; |
| 819 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 820 | if ((!s->current && !s->next) || |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 821 | !drm_sprite_crtc_supported(output, s)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 822 | continue; |
| 823 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 824 | if (s->next && !backend->sprites_hidden) |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 825 | fb_id = s->next->fb_id; |
| 826 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 827 | ret = drmModeSetPlane(backend->drm.fd, s->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 828 | output->crtc_id, fb_id, flags, |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 829 | s->dest_x, s->dest_y, |
| 830 | s->dest_w, s->dest_h, |
| 831 | s->src_x, s->src_y, |
| 832 | s->src_w, s->src_h); |
| 833 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 834 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 835 | ret, strerror(errno)); |
| 836 | |
Mario Kleiner | 2ab4f4e | 2015-06-21 21:25:13 +0200 | [diff] [blame] | 837 | vbl.request.type |= drm_waitvblank_pipe(output); |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 838 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 839 | /* |
| 840 | * Queue a vblank signal so we know when the surface |
| 841 | * becomes active on the display or has been replaced. |
| 842 | */ |
| 843 | vbl.request.signal = (unsigned long)s; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 844 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 845 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 846 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 847 | ret, strerror(errno)); |
| 848 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 849 | |
| 850 | s->output = output; |
| 851 | output->vblank_pending = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 852 | } |
| 853 | |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 854 | return 0; |
| 855 | |
| 856 | err_pageflip: |
Kristian Høgsberg | b3955b0 | 2014-01-23 16:25:06 -0800 | [diff] [blame] | 857 | output->cursor_view = NULL; |
David Herrmann | 1edf44c | 2013-10-22 17:11:26 +0200 | [diff] [blame] | 858 | if (output->next) { |
| 859 | drm_output_release_fb(output, output->next); |
| 860 | output->next = NULL; |
| 861 | } |
| 862 | |
| 863 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | static void |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 867 | drm_output_start_repaint_loop(struct weston_output *output_base) |
| 868 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 869 | struct drm_output *output = to_drm_output(output_base); |
| 870 | struct drm_backend *backend = |
| 871 | to_drm_backend(output_base->compositor); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 872 | uint32_t fb_id; |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 873 | struct timespec ts, tnow; |
| 874 | struct timespec vbl2now; |
| 875 | int64_t refresh_nsec; |
| 876 | int ret; |
| 877 | drmVBlank vbl = { |
| 878 | .request.type = DRM_VBLANK_RELATIVE, |
| 879 | .request.sequence = 0, |
| 880 | .request.signal = 0, |
| 881 | }; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 882 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 883 | if (output->disable_pending || output->destroy_pending) |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 884 | return; |
| 885 | |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 886 | if (!output->current) { |
| 887 | /* We can't page flip if there's no mode set */ |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 888 | goto finish_frame; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 889 | } |
| 890 | |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 891 | /* Try to get current msc and timestamp via instant query */ |
| 892 | vbl.request.type |= drm_waitvblank_pipe(output); |
| 893 | ret = drmWaitVBlank(backend->drm.fd, &vbl); |
| 894 | |
| 895 | /* Error ret or zero timestamp means failure to get valid timestamp */ |
| 896 | if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) { |
| 897 | ts.tv_sec = vbl.reply.tval_sec; |
| 898 | ts.tv_nsec = vbl.reply.tval_usec * 1000; |
| 899 | |
| 900 | /* Valid timestamp for most recent vblank - not stale? |
| 901 | * Stale ts could happen on Linux 3.17+, so make sure it |
| 902 | * is not older than 1 refresh duration since now. |
| 903 | */ |
| 904 | weston_compositor_read_presentation_clock(backend->compositor, |
| 905 | &tnow); |
| 906 | timespec_sub(&vbl2now, &tnow, &ts); |
| 907 | refresh_nsec = |
| 908 | millihz_to_nsec(output->base.current_mode->refresh); |
| 909 | if (timespec_to_nsec(&vbl2now) < refresh_nsec) { |
| 910 | drm_output_update_msc(output, vbl.reply.sequence); |
| 911 | weston_output_finish_frame(output_base, &ts, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 912 | WP_PRESENTATION_FEEDBACK_INVALID); |
Mario Kleiner | f507ec3 | 2015-06-21 21:25:14 +0200 | [diff] [blame] | 913 | return; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* Immediate query didn't provide valid timestamp. |
| 918 | * Use pageflip fallback. |
| 919 | */ |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 920 | fb_id = output->current->fb_id; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 921 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 922 | if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id, |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 923 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 924 | weston_log("queueing pageflip failed: %m\n"); |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 925 | goto finish_frame; |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 926 | } |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 927 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 928 | if (output->pageflip_timer) |
| 929 | wl_event_source_timer_update(output->pageflip_timer, |
| 930 | backend->pageflip_timeout); |
| 931 | |
David Herrmann | 3c688c5 | 2013-10-22 17:11:25 +0200 | [diff] [blame] | 932 | return; |
| 933 | |
| 934 | finish_frame: |
| 935 | /* if we cannot page-flip, immediately finish frame */ |
Daniel Stone | 3615ce1 | 2017-03-01 11:34:05 +0000 | [diff] [blame] | 936 | weston_output_finish_frame(output_base, NULL, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 937 | WP_PRESENTATION_FEEDBACK_INVALID); |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static void |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 941 | drm_output_update_msc(struct drm_output *output, unsigned int seq) |
| 942 | { |
| 943 | uint64_t msc_hi = output->base.msc >> 32; |
| 944 | |
| 945 | if (seq < (output->base.msc & 0xffffffff)) |
| 946 | msc_hi++; |
| 947 | |
| 948 | output->base.msc = (msc_hi << 32) + seq; |
| 949 | } |
| 950 | |
| 951 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 952 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 953 | void *data) |
| 954 | { |
| 955 | struct drm_sprite *s = (struct drm_sprite *)data; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 956 | struct drm_output *output = s->output; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 957 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 958 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 959 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 960 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 961 | drm_output_update_msc(output, frame); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 962 | output->vblank_pending = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 963 | |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 964 | drm_output_release_fb(output, s->current); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 965 | s->current = s->next; |
| 966 | s->next = NULL; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 967 | |
| 968 | if (!output->page_flip_pending) { |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 969 | /* Stop the pageflip timer instead of rearming it here */ |
| 970 | if (output->pageflip_timer) |
| 971 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 972 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 973 | ts.tv_sec = sec; |
| 974 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 975 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 976 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 980 | drm_output_destroy(struct weston_output *base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 981 | |
| 982 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 983 | page_flip_handler(int fd, unsigned int frame, |
| 984 | unsigned int sec, unsigned int usec, void *data) |
| 985 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 986 | struct drm_output *output = data; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 987 | struct timespec ts; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 988 | uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC | |
| 989 | WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION | |
| 990 | WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 991 | |
Pekka Paalanen | 641307c | 2014-09-23 22:08:47 -0400 | [diff] [blame] | 992 | drm_output_update_msc(output, frame); |
| 993 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 994 | /* We don't set page_flip_pending on start_repaint_loop, in that case |
| 995 | * we just want to page flip to the current buffer to get an accurate |
| 996 | * timestamp */ |
| 997 | if (output->page_flip_pending) { |
| 998 | drm_output_release_fb(output, output->current); |
| 999 | output->current = output->next; |
| 1000 | output->next = NULL; |
| 1001 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1002 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 1003 | output->page_flip_pending = 0; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1004 | |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 1005 | if (output->destroy_pending) |
| 1006 | drm_output_destroy(&output->base); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 1007 | else if (output->disable_pending) |
| 1008 | weston_output_disable(&output->base); |
Xiong Zhang | abd5d47 | 2013-10-11 14:43:07 +0800 | [diff] [blame] | 1009 | else if (!output->vblank_pending) { |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 1010 | /* Stop the pageflip timer instead of rearming it here */ |
| 1011 | if (output->pageflip_timer) |
| 1012 | wl_event_source_timer_update(output->pageflip_timer, 0); |
| 1013 | |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1014 | ts.tv_sec = sec; |
| 1015 | ts.tv_nsec = usec * 1000; |
Pekka Paalanen | 363aa7b | 2014-12-17 16:20:40 +0200 | [diff] [blame] | 1016 | weston_output_finish_frame(&output->base, &ts, flags); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 1017 | |
| 1018 | /* We can't call this from frame_notify, because the output's |
| 1019 | * repaint needed flag is cleared just after that */ |
| 1020 | if (output->recorder) |
| 1021 | weston_output_schedule_repaint(&output->base); |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 1022 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1025 | static uint32_t |
| 1026 | drm_output_check_sprite_format(struct drm_sprite *s, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1027 | struct weston_view *ev, struct gbm_bo *bo) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1028 | { |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1029 | uint32_t i, format; |
| 1030 | |
| 1031 | format = gbm_bo_get_format(bo); |
| 1032 | |
| 1033 | if (format == GBM_FORMAT_ARGB8888) { |
| 1034 | pixman_region32_t r; |
| 1035 | |
Kristian Høgsberg | 63093a3 | 2013-03-01 14:29:16 -0500 | [diff] [blame] | 1036 | pixman_region32_init_rect(&r, 0, 0, |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1037 | ev->surface->width, |
| 1038 | ev->surface->height); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1039 | pixman_region32_subtract(&r, &r, &ev->surface->opaque); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1040 | |
| 1041 | if (!pixman_region32_not_empty(&r)) |
| 1042 | format = GBM_FORMAT_XRGB8888; |
| 1043 | |
| 1044 | pixman_region32_fini(&r); |
| 1045 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1046 | |
| 1047 | for (i = 0; i < s->count_formats; i++) |
| 1048 | if (s->formats[i] == format) |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1049 | return format; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1050 | |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1054 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1055 | drm_output_prepare_overlay_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1056 | struct weston_view *ev) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1057 | { |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1058 | struct weston_compositor *ec = output->base.compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1059 | struct drm_backend *b = to_drm_backend(ec); |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1060 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1061 | struct wl_resource *buffer_resource; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1062 | struct drm_sprite *s; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1063 | struct linux_dmabuf_buffer *dmabuf; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1064 | int found = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1065 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1066 | pixman_region32_t dest_rect, src_rect; |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1067 | pixman_box32_t *box, tbox; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1068 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1069 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1070 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1071 | if (b->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1072 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 1073 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1074 | /* Don't import buffers which span multiple outputs. */ |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1075 | if (ev->output_mask != (1u << output->base.id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1076 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 1077 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1078 | /* We can only import GBM buffers. */ |
| 1079 | if (b->gbm == NULL) |
| 1080 | return NULL; |
| 1081 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1082 | if (ev->surface->buffer_ref.buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1083 | return NULL; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1084 | buffer_resource = ev->surface->buffer_ref.buffer->resource; |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1085 | if (wl_shm_buffer_get(buffer_resource)) |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 1086 | return NULL; |
| 1087 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1088 | if (viewport->buffer.transform != output->base.transform) |
| 1089 | return NULL; |
| 1090 | if (viewport->buffer.scale != output->base.current_scale) |
| 1091 | return NULL; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1092 | if (!drm_view_transform_supported(ev)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1093 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1094 | |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1095 | if (ev->alpha != 1.0f) |
| 1096 | return NULL; |
| 1097 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1098 | wl_list_for_each(s, &b->sprite_list, link) { |
Daniel Stone | a3ae476 | 2015-11-28 12:07:36 +0000 | [diff] [blame] | 1099 | if (!drm_sprite_crtc_supported(output, s)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1100 | continue; |
| 1101 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1102 | if (!s->next) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1103 | found = 1; |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | /* No sprites available */ |
| 1109 | if (!found) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1110 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1111 | |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1112 | if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) { |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 1113 | #ifdef HAVE_GBM_FD_IMPORT |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1114 | /* XXX: TODO: |
| 1115 | * |
| 1116 | * Use AddFB2 directly, do not go via GBM. |
| 1117 | * Add support for multiplanar formats. |
| 1118 | * Both require refactoring in the DRM-backend to |
| 1119 | * support a mix of gbm_bos and drmfbs. |
| 1120 | */ |
| 1121 | struct gbm_import_fd_data gbm_dmabuf = { |
Emmanuel Gil Peyrot | c399692 | 2015-11-24 19:28:24 +0000 | [diff] [blame] | 1122 | .fd = dmabuf->attributes.fd[0], |
| 1123 | .width = dmabuf->attributes.width, |
| 1124 | .height = dmabuf->attributes.height, |
| 1125 | .stride = dmabuf->attributes.stride[0], |
| 1126 | .format = dmabuf->attributes.format |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1127 | }; |
| 1128 | |
Micah Fedke | c889012 | 2017-02-01 15:28:23 -0500 | [diff] [blame] | 1129 | /* XXX: TODO: |
| 1130 | * |
| 1131 | * Currently the buffer is rejected if any dmabuf attribute |
| 1132 | * flag is set. This keeps us from passing an inverted / |
| 1133 | * interlaced / bottom-first buffer (or any other type that may |
| 1134 | * be added in the future) through to an overlay. Ultimately, |
| 1135 | * these types of buffers should be handled through buffer |
| 1136 | * transforms and not as spot-checks requiring specific |
| 1137 | * knowledge. */ |
| 1138 | if (dmabuf->attributes.n_planes != 1 || |
| 1139 | dmabuf->attributes.offset[0] != 0 || |
| 1140 | dmabuf->attributes.flags) |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1141 | return NULL; |
| 1142 | |
| 1143 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf, |
| 1144 | GBM_BO_USE_SCANOUT); |
Bryce Harrington | a358207 | 2015-08-14 12:23:13 -0700 | [diff] [blame] | 1145 | #else |
| 1146 | return NULL; |
| 1147 | #endif |
Pekka Paalanen | bf8cc6f | 2014-06-12 17:12:12 +0300 | [diff] [blame] | 1148 | } else { |
| 1149 | bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 1150 | buffer_resource, GBM_BO_USE_SCANOUT); |
| 1151 | } |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1152 | if (!bo) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1153 | return NULL; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 1154 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1155 | format = drm_output_check_sprite_format(s, ev, bo); |
Kristian Høgsberg | a2f84cc | 2012-12-07 12:37:58 -0500 | [diff] [blame] | 1156 | if (format == 0) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1157 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1158 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1161 | s->next = drm_fb_get_from_bo(bo, b, format); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1162 | if (!s->next) { |
| 1163 | gbm_bo_destroy(bo); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1167 | drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1168 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1169 | box = pixman_region32_extents(&ev->transform.boundingbox); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1170 | s->plane.x = box->x1; |
| 1171 | s->plane.y = box->y1; |
| 1172 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1173 | /* |
| 1174 | * Calculate the source & dest rects properly based on actual |
Derek Foreman | 4b1a0a1 | 2014-09-10 15:37:33 -0500 | [diff] [blame] | 1175 | * position (note the caller has called weston_view_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1176 | * for us already). |
| 1177 | */ |
| 1178 | pixman_region32_init(&dest_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1179 | pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1180 | &output->base.region); |
| 1181 | pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1182 | box = pixman_region32_extents(&dest_rect); |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1183 | tbox = weston_transformed_rect(output->base.width, |
| 1184 | output->base.height, |
| 1185 | output->base.transform, |
| 1186 | output->base.current_scale, |
Alexander Larsson | d9a7bb7 | 2013-05-22 14:41:39 +0200 | [diff] [blame] | 1187 | *box); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1188 | s->dest_x = tbox.x1; |
| 1189 | s->dest_y = tbox.y1; |
| 1190 | s->dest_w = tbox.x2 - tbox.x1; |
| 1191 | s->dest_h = tbox.y2 - tbox.y1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1192 | pixman_region32_fini(&dest_rect); |
| 1193 | |
| 1194 | pixman_region32_init(&src_rect); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1195 | pixman_region32_intersect(&src_rect, &ev->transform.boundingbox, |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1196 | &output->base.region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1197 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1198 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1199 | weston_view_from_global_fixed(ev, |
| 1200 | wl_fixed_from_int(box->x1), |
| 1201 | wl_fixed_from_int(box->y1), |
| 1202 | &sx1, &sy1); |
| 1203 | weston_view_from_global_fixed(ev, |
| 1204 | wl_fixed_from_int(box->x2), |
| 1205 | wl_fixed_from_int(box->y2), |
| 1206 | &sx2, &sy2); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1207 | |
| 1208 | if (sx1 < 0) |
| 1209 | sx1 = 0; |
| 1210 | if (sy1 < 0) |
| 1211 | sy1 = 0; |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1212 | if (sx2 > wl_fixed_from_int(ev->surface->width)) |
| 1213 | sx2 = wl_fixed_from_int(ev->surface->width); |
| 1214 | if (sy2 > wl_fixed_from_int(ev->surface->height)) |
| 1215 | sy2 = wl_fixed_from_int(ev->surface->height); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 1216 | |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1217 | tbox.x1 = sx1; |
| 1218 | tbox.y1 = sy1; |
| 1219 | tbox.x2 = sx2; |
| 1220 | tbox.y2 = sy2; |
| 1221 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 1222 | tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width), |
| 1223 | wl_fixed_from_int(ev->surface->height), |
Pekka Paalanen | 952b6c8 | 2014-03-14 14:38:15 +0200 | [diff] [blame] | 1224 | viewport->buffer.transform, |
| 1225 | viewport->buffer.scale, |
Pekka Paalanen | 1fd9c0f | 2013-11-26 18:19:41 +0100 | [diff] [blame] | 1226 | tbox); |
Ander Conselvan de Oliveira | 409eebf | 2012-12-05 15:14:04 +0200 | [diff] [blame] | 1227 | |
| 1228 | s->src_x = tbox.x1 << 8; |
| 1229 | s->src_y = tbox.y1 << 8; |
| 1230 | s->src_w = (tbox.x2 - tbox.x1) << 8; |
| 1231 | s->src_h = (tbox.y2 - tbox.y1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1232 | pixman_region32_fini(&src_rect); |
| 1233 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1234 | return &s->plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1237 | static struct weston_plane * |
Pekka Paalanen | 050c1ba | 2014-12-17 16:20:38 +0200 | [diff] [blame] | 1238 | drm_output_prepare_cursor_view(struct drm_output *output, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1239 | struct weston_view *ev) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1240 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1241 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Neil Roberts | f37f82c | 2014-05-01 18:00:41 +0100 | [diff] [blame] | 1242 | struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport; |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1243 | struct wl_shm_buffer *shmbuf; |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1244 | float x, y; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1245 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1246 | if (b->cursors_are_broken) |
Kristian Høgsberg | 8a01580 | 2012-08-09 17:19:23 -0400 | [diff] [blame] | 1247 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1248 | |
| 1249 | if (output->cursor_view) |
Pekka Paalanen | 5580f22 | 2015-02-17 16:33:18 +0200 | [diff] [blame] | 1250 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1251 | |
| 1252 | /* Don't import buffers which span multiple outputs. */ |
| 1253 | if (ev->output_mask != (1u << output->base.id)) |
| 1254 | return NULL; |
| 1255 | |
| 1256 | /* We use GBM to import SHM buffers. */ |
| 1257 | if (b->gbm == NULL) |
| 1258 | return NULL; |
| 1259 | |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1260 | if (ev->surface->buffer_ref.buffer == NULL) |
| 1261 | return NULL; |
| 1262 | shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource); |
| 1263 | if (!shmbuf) |
| 1264 | return NULL; |
| 1265 | if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888) |
| 1266 | return NULL; |
Daniel Stone | 296d7a9 | 2016-10-21 18:05:37 +0100 | [diff] [blame] | 1267 | |
| 1268 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 1269 | return NULL; |
| 1270 | if (ev->transform.enabled && |
| 1271 | (ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE)) |
| 1272 | return NULL; |
| 1273 | if (viewport->buffer.scale != output->base.current_scale) |
| 1274 | return NULL; |
| 1275 | if (ev->geometry.scissor_enabled) |
| 1276 | return NULL; |
| 1277 | |
Derek Foreman | 6c19b69 | 2015-12-03 14:07:12 -0600 | [diff] [blame] | 1278 | if (ev->surface->width > b->cursor_width || |
Daniel Stone | 70d337d | 2015-06-16 18:42:23 +0100 | [diff] [blame] | 1279 | ev->surface->height > b->cursor_height) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1280 | return NULL; |
| 1281 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1282 | output->cursor_view = ev; |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1283 | weston_view_to_global_float(ev, 0, 0, &x, &y); |
| 1284 | output->cursor_plane.x = x; |
| 1285 | output->cursor_plane.y = y; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1286 | |
| 1287 | return &output->cursor_plane; |
| 1288 | } |
| 1289 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1290 | /** |
| 1291 | * Update the image for the current cursor surface |
| 1292 | * |
| 1293 | * @param b DRM backend structure |
| 1294 | * @param bo GBM buffer object to write into |
| 1295 | * @param ev View to use for cursor image |
| 1296 | */ |
| 1297 | static void |
| 1298 | cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo, |
| 1299 | struct weston_view *ev) |
| 1300 | { |
| 1301 | struct weston_buffer *buffer = ev->surface->buffer_ref.buffer; |
| 1302 | uint32_t buf[b->cursor_width * b->cursor_height]; |
| 1303 | int32_t stride; |
| 1304 | uint8_t *s; |
| 1305 | int i; |
| 1306 | |
| 1307 | assert(buffer && buffer->shm_buffer); |
| 1308 | assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource)); |
| 1309 | assert(ev->surface->width <= b->cursor_width); |
| 1310 | assert(ev->surface->height <= b->cursor_height); |
| 1311 | |
| 1312 | memset(buf, 0, sizeof buf); |
| 1313 | stride = wl_shm_buffer_get_stride(buffer->shm_buffer); |
| 1314 | s = wl_shm_buffer_get_data(buffer->shm_buffer); |
| 1315 | |
| 1316 | wl_shm_buffer_begin_access(buffer->shm_buffer); |
| 1317 | for (i = 0; i < ev->surface->height; i++) |
| 1318 | memcpy(buf + i * b->cursor_width, |
| 1319 | s + i * stride, |
| 1320 | ev->surface->width * 4); |
| 1321 | wl_shm_buffer_end_access(buffer->shm_buffer); |
| 1322 | |
| 1323 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 1324 | weston_log("failed update cursor: %m\n"); |
| 1325 | } |
| 1326 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1327 | static void |
| 1328 | drm_output_set_cursor(struct drm_output *output) |
| 1329 | { |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1330 | struct weston_view *ev = output->cursor_view; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1331 | struct drm_backend *b = to_drm_backend(output->base.compositor); |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1332 | EGLint handle; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1333 | struct gbm_bo *bo; |
Derek Foreman | be428b3 | 2015-11-24 11:39:38 -0600 | [diff] [blame] | 1334 | float x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1335 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1336 | if (ev == NULL) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1337 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1338 | return; |
| 1339 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1340 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1341 | if (pixman_region32_not_empty(&output->cursor_plane.damage)) { |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 1342 | pixman_region32_fini(&output->cursor_plane.damage); |
| 1343 | pixman_region32_init(&output->cursor_plane.damage); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1344 | output->current_cursor ^= 1; |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1345 | bo = output->gbm_cursor_bo[output->current_cursor]; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1346 | |
Pekka Paalanen | d0ead48 | 2014-06-16 12:05:40 +0300 | [diff] [blame] | 1347 | cursor_bo_update(b, bo, ev); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 1348 | handle = gbm_bo_get_handle(bo).s32; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1349 | if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle, |
| 1350 | b->cursor_width, b->cursor_height)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 1351 | weston_log("failed to set cursor: %m\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1352 | b->cursors_are_broken = 1; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 1353 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1354 | } |
| 1355 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1356 | x = (output->cursor_plane.x - output->base.x) * |
| 1357 | output->base.current_scale; |
| 1358 | y = (output->cursor_plane.y - output->base.y) * |
| 1359 | output->base.current_scale; |
Pekka Paalanen | 7eaed40 | 2015-11-27 14:20:58 +0200 | [diff] [blame] | 1360 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1361 | if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) { |
| 1362 | weston_log("failed to move cursor: %m\n"); |
| 1363 | b->cursors_are_broken = 1; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1364 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 1365 | } |
| 1366 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1367 | static void |
Daniel Stone | b1f166d | 2017-03-01 11:34:10 +0000 | [diff] [blame] | 1368 | drm_assign_planes(struct weston_output *output_base, void *repaint_data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1369 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1370 | struct drm_backend *b = to_drm_backend(output_base->compositor); |
| 1371 | struct drm_output *output = to_drm_output(output_base); |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1372 | struct weston_view *ev, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1373 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1374 | struct weston_plane *primary, *next_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * Find a surface for each sprite in the output using some heuristics: |
| 1378 | * 1) size |
| 1379 | * 2) frequency of update |
| 1380 | * 3) opacity (though some hw might support alpha blending) |
| 1381 | * 4) clipping (this can be fixed with color keys) |
| 1382 | * |
| 1383 | * The idea is to save on blitting since this should save power. |
| 1384 | * If we can get a large video surface on the sprite for example, |
| 1385 | * the main display surface may not need to update at all, and |
| 1386 | * the client buffer can be used directly for the sprite surface |
| 1387 | * as we do for flipping full screen surfaces. |
| 1388 | */ |
| 1389 | pixman_region32_init(&overlap); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1390 | primary = &output_base->compositor->primary_plane; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1391 | |
Daniel Stone | a7cba1d | 2017-04-04 17:54:21 +0100 | [diff] [blame^] | 1392 | output->cursor_view = NULL; |
| 1393 | output->cursor_plane.x = INT32_MIN; |
| 1394 | output->cursor_plane.y = INT32_MIN; |
| 1395 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1396 | wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) { |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1397 | struct weston_surface *es = ev->surface; |
| 1398 | |
| 1399 | /* Test whether this buffer can ever go into a plane: |
| 1400 | * non-shm, or small enough to be a cursor. |
| 1401 | * |
| 1402 | * Also, keep a reference when using the pixman renderer. |
| 1403 | * That makes it possible to do a seamless switch to the GL |
| 1404 | * renderer and since the pixman renderer keeps a reference |
| 1405 | * to the buffer anyway, there is no side effects. |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1406 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1407 | if (b->use_pixman || |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1408 | (es->buffer_ref.buffer && |
| 1409 | (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) || |
Derek Foreman | 8743047 | 2015-10-15 10:24:48 -0500 | [diff] [blame] | 1410 | (ev->surface->width <= b->cursor_width && |
| 1411 | ev->surface->height <= b->cursor_height)))) |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1412 | es->keep_buffer = true; |
Ander Conselvan de Oliveira | 895b1fd | 2013-11-19 15:22:05 +0200 | [diff] [blame] | 1413 | else |
Derek Foreman | 0fd6d4e | 2014-10-10 09:36:45 -0500 | [diff] [blame] | 1414 | es->keep_buffer = false; |
Pekka Paalanen | ccfeae2 | 2012-12-04 15:58:14 +0200 | [diff] [blame] | 1415 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1416 | pixman_region32_init(&surface_overlap); |
| 1417 | pixman_region32_intersect(&surface_overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1418 | &ev->transform.boundingbox); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1419 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1420 | next_plane = NULL; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1421 | if (pixman_region32_not_empty(&surface_overlap)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1422 | next_plane = primary; |
| 1423 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1424 | next_plane = drm_output_prepare_cursor_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1425 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1426 | next_plane = drm_output_prepare_scanout_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1427 | if (next_plane == NULL) |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1428 | next_plane = drm_output_prepare_overlay_view(output, ev); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1429 | if (next_plane == NULL) |
| 1430 | next_plane = primary; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1431 | |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1432 | weston_view_move_to_plane(ev, next_plane); |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1433 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1434 | if (next_plane == primary) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1435 | pixman_region32_union(&overlap, &overlap, |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 1436 | &ev->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 1437 | |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1438 | if (next_plane == primary || |
| 1439 | next_plane == &output->cursor_plane) { |
| 1440 | /* cursor plane involves a copy */ |
| 1441 | ev->psf_flags = 0; |
| 1442 | } else { |
| 1443 | /* All other planes are a direct scanout of a |
| 1444 | * single client buffer. |
| 1445 | */ |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 1446 | ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY; |
Pekka Paalanen | bf0e031 | 2014-12-17 16:20:41 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1449 | pixman_region32_fini(&surface_overlap); |
| 1450 | } |
| 1451 | pixman_region32_fini(&overlap); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1452 | } |
| 1453 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1454 | /** |
| 1455 | * Find the closest-matching mode for a given target |
| 1456 | * |
| 1457 | * Given a target mode, find the most suitable mode amongst the output's |
| 1458 | * current mode list to use, preferring the current mode if possible, to |
| 1459 | * avoid an expensive mode switch. |
| 1460 | * |
| 1461 | * @param output DRM output |
| 1462 | * @param target_mode Mode to attempt to match |
| 1463 | * @returns Pointer to a mode from the output's mode list |
| 1464 | */ |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1465 | static struct drm_mode * |
| 1466 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 1467 | { |
| 1468 | struct drm_mode *tmp_mode = NULL, *mode; |
| 1469 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1470 | if (output->base.current_mode->width == target_mode->width && |
| 1471 | output->base.current_mode->height == target_mode->height && |
| 1472 | (output->base.current_mode->refresh == target_mode->refresh || |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1473 | target_mode->refresh == 0)) |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1474 | return (struct drm_mode *)output->base.current_mode; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1475 | |
| 1476 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 1477 | if (mode->mode_info.hdisplay == target_mode->width && |
| 1478 | mode->mode_info.vdisplay == target_mode->height) { |
Mario Kleiner | 872797c | 2015-06-21 21:25:09 +0200 | [diff] [blame] | 1479 | if (mode->base.refresh == target_mode->refresh || |
| 1480 | target_mode->refresh == 0) { |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1481 | return mode; |
Daniel Stone | f556ebe | 2015-05-21 08:28:58 +0100 | [diff] [blame] | 1482 | } else if (!tmp_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1483 | tmp_mode = mode; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | return tmp_mode; |
| 1488 | } |
| 1489 | |
| 1490 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1491 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1492 | static void |
| 1493 | drm_output_fini_egl(struct drm_output *output); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1494 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1495 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1496 | static void |
| 1497 | drm_output_fini_pixman(struct drm_output *output); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1498 | |
| 1499 | static int |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1500 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 1501 | { |
| 1502 | struct drm_output *output; |
| 1503 | struct drm_mode *drm_mode; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1504 | struct drm_backend *b; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1505 | |
| 1506 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1507 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1508 | return -1; |
| 1509 | } |
| 1510 | |
| 1511 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1512 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1513 | return -1; |
| 1514 | } |
| 1515 | |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1516 | b = to_drm_backend(output_base->compositor); |
| 1517 | output = to_drm_output(output_base); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1518 | drm_mode = choose_mode (output, mode); |
| 1519 | |
| 1520 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1521 | weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1522 | return -1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1523 | } |
| 1524 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1525 | if (&drm_mode->base == output->base.current_mode) |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1526 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1527 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1528 | output->base.current_mode->flags = 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1529 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1530 | output->base.current_mode = &drm_mode->base; |
| 1531 | output->base.current_mode->flags = |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1532 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1533 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1534 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 1535 | drm_output_release_fb(output, output->current); |
| 1536 | drm_output_release_fb(output, output->next); |
| 1537 | output->current = output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1538 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1539 | if (b->use_pixman) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1540 | drm_output_fini_pixman(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1541 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1542 | weston_log("failed to init output pixman state with " |
| 1543 | "new mode\n"); |
| 1544 | return -1; |
| 1545 | } |
| 1546 | } else { |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1547 | drm_output_fini_egl(output); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1548 | if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1549 | weston_log("failed to init output egl state with " |
| 1550 | "new mode"); |
| 1551 | return -1; |
| 1552 | } |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1555 | return 0; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1558 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1559 | on_drm_input(int fd, uint32_t mask, void *data) |
| 1560 | { |
| 1561 | drmEventContext evctx; |
| 1562 | |
| 1563 | memset(&evctx, 0, sizeof evctx); |
| 1564 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 1565 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1566 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1567 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1568 | |
| 1569 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static int |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1573 | init_kms_caps(struct drm_backend *b) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1574 | { |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1575 | uint64_t cap; |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1576 | int ret; |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1577 | clockid_t clk_id; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1578 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1579 | weston_log("using %s\n", b->drm.filename); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1580 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1581 | ret = drmGetCap(b->drm.fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap); |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1582 | if (ret == 0 && cap == 1) |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1583 | clk_id = CLOCK_MONOTONIC; |
Ander Conselvan de Oliveira | 95eb3a2 | 2013-05-07 14:16:59 +0300 | [diff] [blame] | 1584 | else |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1585 | clk_id = CLOCK_REALTIME; |
| 1586 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1587 | if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) { |
Pekka Paalanen | b5eedad | 2014-09-23 22:08:45 -0400 | [diff] [blame] | 1588 | weston_log("Error: failed to set presentation clock %d.\n", |
| 1589 | clk_id); |
| 1590 | return -1; |
| 1591 | } |
Ander Conselvan de Oliveira | 1d41ad4 | 2013-01-25 15:13:04 +0200 | [diff] [blame] | 1592 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1593 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_WIDTH, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1594 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1595 | b->cursor_width = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1596 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1597 | b->cursor_width = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1598 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 1599 | ret = drmGetCap(b->drm.fd, DRM_CAP_CURSOR_HEIGHT, &cap); |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1600 | if (ret == 0) |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1601 | b->cursor_height = cap; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1602 | else |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1603 | b->cursor_height = 64; |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1604 | |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1605 | return 0; |
| 1606 | } |
| 1607 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1608 | static struct gbm_device * |
| 1609 | create_gbm_device(int fd) |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 1610 | { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1611 | struct gbm_device *gbm; |
Alexandru DAMIAN | be0ac5b | 2013-10-02 17:51:05 +0100 | [diff] [blame] | 1612 | |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1613 | gl_renderer = weston_load_module("gl-renderer.so", |
| 1614 | "gl_renderer_interface"); |
| 1615 | if (!gl_renderer) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1616 | return NULL; |
Ander Conselvan de Oliveira | 97f2952 | 2013-10-14 15:57:11 +0300 | [diff] [blame] | 1617 | |
| 1618 | /* GBM will load a dri driver, but even though they need symbols from |
| 1619 | * libglapi, in some version of Mesa they are not linked to it. Since |
| 1620 | * only the gl-renderer module links to it, the call above won't make |
| 1621 | * these symbols globally available, and loading the DRI driver fails. |
| 1622 | * Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */ |
| 1623 | dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL); |
| 1624 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1625 | gbm = gbm_create_device(fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1626 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1627 | return gbm; |
| 1628 | } |
| 1629 | |
Bryce Harrington | c056a98 | 2015-05-19 15:25:18 -0700 | [diff] [blame] | 1630 | /* When initializing EGL, if the preferred buffer format isn't available |
Bryce Harrington | b993998 | 2016-04-15 20:28:26 -0700 | [diff] [blame] | 1631 | * we may be able to substitute an ARGB format for an XRGB one. |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1632 | * |
| 1633 | * This returns 0 if substitution isn't possible, but 0 might be a |
| 1634 | * legitimate format for other EGL platforms, so the caller is |
| 1635 | * responsible for checking for 0 before calling gl_renderer->create(). |
| 1636 | * |
| 1637 | * This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689 |
| 1638 | * but it's entirely possible we'll see this again on other implementations. |
| 1639 | */ |
| 1640 | static int |
| 1641 | fallback_format_for(uint32_t format) |
| 1642 | { |
| 1643 | switch (format) { |
| 1644 | case GBM_FORMAT_XRGB8888: |
| 1645 | return GBM_FORMAT_ARGB8888; |
| 1646 | case GBM_FORMAT_XRGB2101010: |
| 1647 | return GBM_FORMAT_ARGB2101010; |
| 1648 | default: |
| 1649 | return 0; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1653 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1654 | drm_backend_create_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1655 | { |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1656 | EGLint format[3] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1657 | b->gbm_format, |
| 1658 | fallback_format_for(b->gbm_format), |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1659 | 0, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1660 | }; |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1661 | int n_formats = 2; |
John Kåre Alsaker | ef591aa | 2013-03-02 12:27:39 +0100 | [diff] [blame] | 1662 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1663 | if (format[1]) |
Derek Foreman | 6d55637 | 2015-11-04 14:47:33 -0600 | [diff] [blame] | 1664 | n_formats = 3; |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1665 | if (gl_renderer->display_create(b->compositor, |
| 1666 | EGL_PLATFORM_GBM_KHR, |
| 1667 | (void *)b->gbm, |
Miguel A. Vico | 41700e3 | 2016-05-18 17:47:59 +0200 | [diff] [blame] | 1668 | NULL, |
Miguel A. Vico | dddc670 | 2016-05-18 17:41:07 +0200 | [diff] [blame] | 1669 | gl_renderer->opaque_attribs, |
| 1670 | format, |
| 1671 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1672 | return -1; |
| 1673 | } |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1679 | init_egl(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1680 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1681 | b->gbm = create_gbm_device(b->drm.fd); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1682 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1683 | if (!b->gbm) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 1684 | return -1; |
| 1685 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1686 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 1687 | gbm_device_destroy(b->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1688 | return -1; |
| 1689 | } |
| 1690 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1694 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1695 | init_pixman(struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1696 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1697 | return pixman_renderer_init(b->compositor); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1698 | } |
| 1699 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1700 | /** |
| 1701 | * Add a mode to output's mode list |
| 1702 | * |
| 1703 | * Copy the supplied DRM mode into a Weston mode structure, and add it to the |
| 1704 | * output's mode list. |
| 1705 | * |
| 1706 | * @param output DRM output to add mode to |
| 1707 | * @param info DRM mode structure to add |
| 1708 | * @returns Newly-allocated Weston/DRM mode structure |
| 1709 | */ |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1710 | static struct drm_mode * |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 1711 | drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1712 | { |
| 1713 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1714 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1715 | |
| 1716 | mode = malloc(sizeof *mode); |
| 1717 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1718 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1719 | |
| 1720 | mode->base.flags = 0; |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 1721 | mode->base.width = info->hdisplay; |
| 1722 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1723 | |
| 1724 | /* Calculate higher precision (mHz) refresh rate */ |
| 1725 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1726 | info->vtotal / 2) / info->vtotal; |
| 1727 | |
| 1728 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1729 | refresh *= 2; |
| 1730 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1731 | refresh /= 2; |
| 1732 | if (info->vscan > 1) |
| 1733 | refresh /= info->vscan; |
| 1734 | |
| 1735 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1736 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1737 | |
| 1738 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1739 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1740 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1741 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1742 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1743 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static int |
| 1747 | drm_subpixel_to_wayland(int drm_value) |
| 1748 | { |
| 1749 | switch (drm_value) { |
| 1750 | default: |
| 1751 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1752 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1753 | case DRM_MODE_SUBPIXEL_NONE: |
| 1754 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1755 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1756 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1757 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1758 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1759 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1760 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1761 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1762 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1763 | } |
| 1764 | } |
| 1765 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1766 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1767 | static uint32_t |
| 1768 | drm_get_backlight(struct drm_output *output) |
| 1769 | { |
| 1770 | long brightness, max_brightness, norm; |
| 1771 | |
| 1772 | brightness = backlight_get_brightness(output->backlight); |
| 1773 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1774 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1775 | /* convert it on a scale of 0 to 255 */ |
| 1776 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1777 | |
| 1778 | return (uint32_t) norm; |
| 1779 | } |
| 1780 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1781 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1782 | static void |
| 1783 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1784 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1785 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1786 | long max_brightness, new_brightness; |
| 1787 | |
| 1788 | if (!output->backlight) |
| 1789 | return; |
| 1790 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1791 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1792 | return; |
| 1793 | |
| 1794 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1795 | |
| 1796 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1797 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1798 | |
| 1799 | backlight_set_brightness(output->backlight, new_brightness); |
| 1800 | } |
| 1801 | |
| 1802 | static drmModePropertyPtr |
| 1803 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1804 | { |
| 1805 | drmModePropertyPtr props; |
| 1806 | int i; |
| 1807 | |
| 1808 | for (i = 0; i < connector->count_props; i++) { |
| 1809 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1810 | if (!props) |
| 1811 | continue; |
| 1812 | |
| 1813 | if (!strcmp(props->name, name)) |
| 1814 | return props; |
| 1815 | |
| 1816 | drmModeFreeProperty(props); |
| 1817 | } |
| 1818 | |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | |
| 1822 | static void |
| 1823 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1824 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1825 | struct drm_output *output = to_drm_output(output_base); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1826 | struct weston_compositor *ec = output_base->compositor; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 1827 | struct drm_backend *b = to_drm_backend(ec); |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1828 | int ret; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1829 | |
Ander Conselvan de Oliveira | a0a433a | 2013-06-04 16:24:04 +0300 | [diff] [blame] | 1830 | if (!output->dpms_prop) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1831 | return; |
| 1832 | |
Daniel Stone | 36609c7 | 2015-06-18 07:49:02 +0100 | [diff] [blame] | 1833 | ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id, |
| 1834 | output->dpms_prop->prop_id, level); |
| 1835 | if (ret) { |
| 1836 | weston_log("DRM: DPMS: failed property set for %s\n", |
| 1837 | output->base.name); |
| 1838 | return; |
| 1839 | } |
| 1840 | |
| 1841 | output->dpms = level; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1842 | } |
| 1843 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1844 | static const char * const connector_type_names[] = { |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1845 | [DRM_MODE_CONNECTOR_Unknown] = "Unknown", |
| 1846 | [DRM_MODE_CONNECTOR_VGA] = "VGA", |
| 1847 | [DRM_MODE_CONNECTOR_DVII] = "DVI-I", |
| 1848 | [DRM_MODE_CONNECTOR_DVID] = "DVI-D", |
| 1849 | [DRM_MODE_CONNECTOR_DVIA] = "DVI-A", |
| 1850 | [DRM_MODE_CONNECTOR_Composite] = "Composite", |
| 1851 | [DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO", |
| 1852 | [DRM_MODE_CONNECTOR_LVDS] = "LVDS", |
| 1853 | [DRM_MODE_CONNECTOR_Component] = "Component", |
| 1854 | [DRM_MODE_CONNECTOR_9PinDIN] = "DIN", |
| 1855 | [DRM_MODE_CONNECTOR_DisplayPort] = "DP", |
| 1856 | [DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A", |
| 1857 | [DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B", |
| 1858 | [DRM_MODE_CONNECTOR_TV] = "TV", |
| 1859 | [DRM_MODE_CONNECTOR_eDP] = "eDP", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1860 | #ifdef DRM_MODE_CONNECTOR_DSI |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1861 | [DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual", |
| 1862 | [DRM_MODE_CONNECTOR_DSI] = "DSI", |
Pekka Paalanen | ab81f15 | 2015-08-24 14:27:07 +0300 | [diff] [blame] | 1863 | #endif |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1864 | }; |
| 1865 | |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1866 | static char * |
| 1867 | make_connector_name(const drmModeConnector *con) |
| 1868 | { |
| 1869 | char name[32]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1870 | const char *type_name = NULL; |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1871 | |
| 1872 | if (con->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1873 | type_name = connector_type_names[con->connector_type]; |
Pekka Paalanen | 89c49b3 | 2015-08-19 15:25:57 +0300 | [diff] [blame] | 1874 | |
| 1875 | if (!type_name) |
| 1876 | type_name = "UNNAMED"; |
| 1877 | |
| 1878 | snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id); |
Pekka Paalanen | 3ce6362 | 2014-06-04 16:29:49 +0300 | [diff] [blame] | 1879 | |
| 1880 | return strdup(name); |
| 1881 | } |
| 1882 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1883 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1884 | find_crtc_for_connector(struct drm_backend *b, |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1885 | drmModeRes *resources, drmModeConnector *connector) |
| 1886 | { |
| 1887 | drmModeEncoder *encoder; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1888 | int i, j; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1889 | int ret = -1; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1890 | |
| 1891 | for (j = 0; j < connector->count_encoders; j++) { |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1892 | uint32_t possible_crtcs, encoder_id, crtc_id; |
| 1893 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1894 | encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1895 | if (encoder == NULL) { |
| 1896 | weston_log("Failed to get encoder.\n"); |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1897 | continue; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1898 | } |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1899 | encoder_id = encoder->encoder_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1900 | possible_crtcs = encoder->possible_crtcs; |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1901 | crtc_id = encoder->crtc_id; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1902 | drmModeFreeEncoder(encoder); |
| 1903 | |
| 1904 | for (i = 0; i < resources->count_crtcs; i++) { |
Daniel Stone | 72c0e1b | 2017-02-09 13:49:15 +0000 | [diff] [blame] | 1905 | if (!(possible_crtcs & (1 << i))) |
| 1906 | continue; |
| 1907 | |
| 1908 | if (drm_output_find_by_crtc(b, resources->crtcs[i])) |
| 1909 | continue; |
| 1910 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1911 | /* Try to preserve the existing |
| 1912 | * CRTC -> encoder -> connector routing; it makes |
| 1913 | * initialisation faster, and also since we have a |
| 1914 | * very dumb picking algorithm, may preserve a better |
| 1915 | * choice. */ |
| 1916 | if (!connector->encoder_id || |
| 1917 | (encoder_id == connector->encoder_id && |
| 1918 | crtc_id == resources->crtcs[i])) |
| 1919 | return i; |
| 1920 | |
| 1921 | ret = i; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
Daniel Stone | 75487c2 | 2017-01-16 14:33:38 +0000 | [diff] [blame] | 1925 | return ret; |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1926 | } |
| 1927 | |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1928 | /* Init output state that depends on gl or gbm */ |
| 1929 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1930 | drm_output_init_egl(struct drm_output *output, struct drm_backend *b) |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1931 | { |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1932 | EGLint format[2] = { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1933 | output->gbm_format, |
| 1934 | fallback_format_for(output->gbm_format), |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1935 | }; |
| 1936 | int i, flags, n_formats = 1; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1937 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1938 | output->gbm_surface = gbm_surface_create(b->gbm, |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1939 | output->base.current_mode->width, |
| 1940 | output->base.current_mode->height, |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1941 | format[0], |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1942 | GBM_BO_USE_SCANOUT | |
| 1943 | GBM_BO_USE_RENDERING); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1944 | if (!output->gbm_surface) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1945 | weston_log("failed to create gbm surface\n"); |
| 1946 | return -1; |
| 1947 | } |
| 1948 | |
Derek Foreman | c4cfe85 | 2015-05-15 12:12:40 -0500 | [diff] [blame] | 1949 | if (format[1]) |
| 1950 | n_formats = 2; |
Miguel A. Vico | c095cde | 2016-05-18 17:43:00 +0200 | [diff] [blame] | 1951 | if (gl_renderer->output_window_create(&output->base, |
| 1952 | (EGLNativeWindowType)output->gbm_surface, |
| 1953 | output->gbm_surface, |
| 1954 | gl_renderer->opaque_attribs, |
| 1955 | format, |
| 1956 | n_formats) < 0) { |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1957 | weston_log("failed to create gl renderer output state\n"); |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1958 | gbm_surface_destroy(output->gbm_surface); |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1959 | return -1; |
| 1960 | } |
| 1961 | |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1962 | flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE; |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1963 | |
| 1964 | for (i = 0; i < 2; i++) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1965 | if (output->gbm_cursor_bo[i]) |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1966 | continue; |
| 1967 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1968 | output->gbm_cursor_bo[i] = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1969 | gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height, |
Alvaro Fernando García | dce7c6e | 2014-07-28 18:30:17 -0300 | [diff] [blame] | 1970 | GBM_FORMAT_ARGB8888, flags); |
Ander Conselvan de Oliveira | 6c01c9c | 2012-12-14 13:37:30 -0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 1973 | if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1974 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1975 | b->cursors_are_broken = 1; |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 1981 | static void |
| 1982 | drm_output_fini_egl(struct drm_output *output) |
| 1983 | { |
| 1984 | gl_renderer->output_destroy(&output->base); |
| 1985 | gbm_surface_destroy(output->gbm_surface); |
| 1986 | } |
| 1987 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1988 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 1989 | drm_output_init_pixman(struct drm_output *output, struct drm_backend *b) |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1990 | { |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 1991 | int w = output->base.current_mode->width; |
| 1992 | int h = output->base.current_mode->height; |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1993 | uint32_t format = output->gbm_format; |
| 1994 | uint32_t pixman_format; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 1995 | unsigned int i; |
| 1996 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 1997 | switch (format) { |
| 1998 | case GBM_FORMAT_XRGB8888: |
| 1999 | pixman_format = PIXMAN_x8r8g8b8; |
| 2000 | break; |
| 2001 | case GBM_FORMAT_RGB565: |
| 2002 | pixman_format = PIXMAN_r5g6b5; |
| 2003 | break; |
| 2004 | default: |
| 2005 | weston_log("Unsupported pixman format 0x%x\n", format); |
| 2006 | return -1; |
| 2007 | } |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2008 | |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2009 | /* FIXME error checking */ |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2010 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2011 | output->dumb[i] = drm_fb_create_dumb(b, w, h, format); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2012 | if (!output->dumb[i]) |
| 2013 | goto err; |
| 2014 | |
| 2015 | output->image[i] = |
Tomi Valkeinen | f8da0c2 | 2016-06-20 14:18:45 +0300 | [diff] [blame] | 2016 | pixman_image_create_bits(pixman_format, w, h, |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2017 | output->dumb[i]->map, |
| 2018 | output->dumb[i]->stride); |
| 2019 | if (!output->image[i]) |
| 2020 | goto err; |
| 2021 | } |
| 2022 | |
| 2023 | if (pixman_renderer_output_create(&output->base) < 0) |
| 2024 | goto err; |
| 2025 | |
| 2026 | pixman_region32_init_rect(&output->previous_damage, |
Alexander Larsson | 0b13506 | 2013-05-28 16:23:36 +0200 | [diff] [blame] | 2027 | output->base.x, output->base.y, output->base.width, output->base.height); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2028 | |
| 2029 | return 0; |
| 2030 | |
| 2031 | err: |
| 2032 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 2033 | if (output->dumb[i]) |
| 2034 | drm_fb_destroy_dumb(output->dumb[i]); |
| 2035 | if (output->image[i]) |
| 2036 | pixman_image_unref(output->image[i]); |
| 2037 | |
| 2038 | output->dumb[i] = NULL; |
| 2039 | output->image[i] = NULL; |
| 2040 | } |
| 2041 | |
| 2042 | return -1; |
| 2043 | } |
| 2044 | |
| 2045 | static void |
| 2046 | drm_output_fini_pixman(struct drm_output *output) |
| 2047 | { |
| 2048 | unsigned int i; |
| 2049 | |
| 2050 | pixman_renderer_output_destroy(&output->base); |
| 2051 | pixman_region32_fini(&output->previous_damage); |
| 2052 | |
| 2053 | for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) { |
| 2054 | drm_fb_destroy_dumb(output->dumb[i]); |
| 2055 | pixman_image_unref(output->image[i]); |
| 2056 | output->dumb[i] = NULL; |
| 2057 | output->image[i] = NULL; |
| 2058 | } |
| 2059 | } |
| 2060 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2061 | static void |
| 2062 | edid_parse_string(const uint8_t *data, char text[]) |
| 2063 | { |
| 2064 | int i; |
| 2065 | int replaced = 0; |
| 2066 | |
| 2067 | /* this is always 12 bytes, but we can't guarantee it's null |
| 2068 | * terminated or not junk. */ |
| 2069 | strncpy(text, (const char *) data, 12); |
| 2070 | |
Bryce Harrington | 9c7de16 | 2015-08-28 13:04:26 -0700 | [diff] [blame] | 2071 | /* guarantee our new string is null-terminated */ |
| 2072 | text[12] = '\0'; |
| 2073 | |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2074 | /* remove insane chars */ |
| 2075 | for (i = 0; text[i] != '\0'; i++) { |
| 2076 | if (text[i] == '\n' || |
| 2077 | text[i] == '\r') { |
| 2078 | text[i] = '\0'; |
| 2079 | break; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | /* ensure string is printable */ |
| 2084 | for (i = 0; text[i] != '\0'; i++) { |
| 2085 | if (!isprint(text[i])) { |
| 2086 | text[i] = '-'; |
| 2087 | replaced++; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | /* if the string is random junk, ignore the string */ |
| 2092 | if (replaced > 4) |
| 2093 | text[0] = '\0'; |
| 2094 | } |
| 2095 | |
| 2096 | #define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe |
| 2097 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc |
| 2098 | #define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff |
| 2099 | #define EDID_OFFSET_DATA_BLOCKS 0x36 |
| 2100 | #define EDID_OFFSET_LAST_BLOCK 0x6c |
| 2101 | #define EDID_OFFSET_PNPID 0x08 |
| 2102 | #define EDID_OFFSET_SERIAL 0x0c |
| 2103 | |
| 2104 | static int |
| 2105 | edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length) |
| 2106 | { |
| 2107 | int i; |
| 2108 | uint32_t serial_number; |
| 2109 | |
| 2110 | /* check header */ |
| 2111 | if (length < 128) |
| 2112 | return -1; |
| 2113 | if (data[0] != 0x00 || data[1] != 0xff) |
| 2114 | return -1; |
| 2115 | |
| 2116 | /* decode the PNP ID from three 5 bit words packed into 2 bytes |
| 2117 | * /--08--\/--09--\ |
| 2118 | * 7654321076543210 |
| 2119 | * |\---/\---/\---/ |
| 2120 | * R C1 C2 C3 */ |
| 2121 | edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1; |
| 2122 | edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1; |
| 2123 | edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1; |
| 2124 | edid->pnp_id[3] = '\0'; |
| 2125 | |
| 2126 | /* maybe there isn't a ASCII serial number descriptor, so use this instead */ |
| 2127 | serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0]; |
| 2128 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100; |
| 2129 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000; |
| 2130 | serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000; |
| 2131 | if (serial_number > 0) |
| 2132 | sprintf(edid->serial_number, "%lu", (unsigned long) serial_number); |
| 2133 | |
| 2134 | /* parse EDID data */ |
| 2135 | for (i = EDID_OFFSET_DATA_BLOCKS; |
| 2136 | i <= EDID_OFFSET_LAST_BLOCK; |
| 2137 | i += 18) { |
| 2138 | /* ignore pixel clock data */ |
| 2139 | if (data[i] != 0) |
| 2140 | continue; |
| 2141 | if (data[i+2] != 0) |
| 2142 | continue; |
| 2143 | |
| 2144 | /* any useful blocks? */ |
| 2145 | if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) { |
| 2146 | edid_parse_string(&data[i+5], |
| 2147 | edid->monitor_name); |
| 2148 | } else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) { |
| 2149 | edid_parse_string(&data[i+5], |
| 2150 | edid->serial_number); |
| 2151 | } else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) { |
| 2152 | edid_parse_string(&data[i+5], |
| 2153 | edid->eisa_id); |
| 2154 | } |
| 2155 | } |
| 2156 | return 0; |
| 2157 | } |
| 2158 | |
| 2159 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2160 | find_and_parse_output_edid(struct drm_backend *b, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2161 | struct drm_output *output, |
| 2162 | drmModeConnector *connector) |
| 2163 | { |
| 2164 | drmModePropertyBlobPtr edid_blob = NULL; |
| 2165 | drmModePropertyPtr property; |
| 2166 | int i; |
| 2167 | int rc; |
| 2168 | |
| 2169 | for (i = 0; i < connector->count_props && !edid_blob; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2170 | property = drmModeGetProperty(b->drm.fd, connector->props[i]); |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2171 | if (!property) |
| 2172 | continue; |
| 2173 | if ((property->flags & DRM_MODE_PROP_BLOB) && |
| 2174 | !strcmp(property->name, "EDID")) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2175 | edid_blob = drmModeGetPropertyBlob(b->drm.fd, |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2176 | connector->prop_values[i]); |
| 2177 | } |
| 2178 | drmModeFreeProperty(property); |
| 2179 | } |
| 2180 | if (!edid_blob) |
| 2181 | return; |
| 2182 | |
| 2183 | rc = edid_parse(&output->edid, |
| 2184 | edid_blob->data, |
| 2185 | edid_blob->length); |
| 2186 | if (!rc) { |
| 2187 | weston_log("EDID data '%s', '%s', '%s'\n", |
| 2188 | output->edid.pnp_id, |
| 2189 | output->edid.monitor_name, |
| 2190 | output->edid.serial_number); |
| 2191 | if (output->edid.pnp_id[0] != '\0') |
| 2192 | output->base.make = output->edid.pnp_id; |
| 2193 | if (output->edid.monitor_name[0] != '\0') |
| 2194 | output->base.model = output->edid.monitor_name; |
| 2195 | if (output->edid.serial_number[0] != '\0') |
| 2196 | output->base.serial_number = output->edid.serial_number; |
| 2197 | } |
| 2198 | drmModeFreePropertyBlob(edid_blob); |
| 2199 | } |
| 2200 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2201 | |
| 2202 | |
| 2203 | static int |
| 2204 | parse_modeline(const char *s, drmModeModeInfo *mode) |
| 2205 | { |
| 2206 | char hsync[16]; |
| 2207 | char vsync[16]; |
| 2208 | float fclock; |
| 2209 | |
| 2210 | mode->type = DRM_MODE_TYPE_USERDEF; |
| 2211 | mode->hskew = 0; |
| 2212 | mode->vscan = 0; |
| 2213 | mode->vrefresh = 0; |
| 2214 | mode->flags = 0; |
| 2215 | |
Rob Bradford | 307e09e | 2013-07-26 16:29:40 +0100 | [diff] [blame] | 2216 | if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s", |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2217 | &fclock, |
| 2218 | &mode->hdisplay, |
| 2219 | &mode->hsync_start, |
| 2220 | &mode->hsync_end, |
| 2221 | &mode->htotal, |
| 2222 | &mode->vdisplay, |
| 2223 | &mode->vsync_start, |
| 2224 | &mode->vsync_end, |
| 2225 | &mode->vtotal, hsync, vsync) != 11) |
| 2226 | return -1; |
| 2227 | |
| 2228 | mode->clock = fclock * 1000; |
| 2229 | if (strcmp(hsync, "+hsync") == 0) |
| 2230 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 2231 | else if (strcmp(hsync, "-hsync") == 0) |
| 2232 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 2233 | else |
| 2234 | return -1; |
| 2235 | |
| 2236 | if (strcmp(vsync, "+vsync") == 0) |
| 2237 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 2238 | else if (strcmp(vsync, "-vsync") == 0) |
| 2239 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 2240 | else |
| 2241 | return -1; |
| 2242 | |
Emmanuel Gil Peyrot | a62138b | 2016-05-02 22:40:11 +0100 | [diff] [blame] | 2243 | snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f", |
| 2244 | mode->hdisplay, mode->vdisplay, fclock); |
| 2245 | |
Kristian Høgsberg | a30989a | 2013-05-23 17:23:15 -0400 | [diff] [blame] | 2246 | return 0; |
| 2247 | } |
| 2248 | |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2249 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2250 | setup_output_seat_constraint(struct drm_backend *b, |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2251 | struct weston_output *output, |
| 2252 | const char *s) |
| 2253 | { |
| 2254 | if (strcmp(s, "") != 0) { |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2255 | struct weston_pointer *pointer; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2256 | struct udev_seat *seat; |
| 2257 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2258 | seat = udev_seat_get_named(&b->input, s); |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2259 | if (!seat) |
| 2260 | return; |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2261 | |
Derek Foreman | 0720ea3 | 2015-07-15 13:00:35 -0500 | [diff] [blame] | 2262 | seat->base.output = output; |
| 2263 | |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 2264 | pointer = weston_seat_get_pointer(&seat->base); |
| 2265 | if (pointer) |
| 2266 | weston_pointer_clamp(pointer, |
| 2267 | &pointer->x, |
| 2268 | &pointer->y); |
Rob Bradford | 66bd9f5 | 2013-06-25 18:56:42 +0100 | [diff] [blame] | 2269 | } |
| 2270 | } |
| 2271 | |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2272 | static int |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2273 | parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format) |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2274 | { |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2275 | int ret = 0; |
| 2276 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2277 | if (s == NULL) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2278 | *gbm_format = default_value; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2279 | else if (strcmp(s, "xrgb8888") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2280 | *gbm_format = GBM_FORMAT_XRGB8888; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2281 | else if (strcmp(s, "rgb565") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2282 | *gbm_format = GBM_FORMAT_RGB565; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2283 | else if (strcmp(s, "xrgb2101010") == 0) |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2284 | *gbm_format = GBM_FORMAT_XRGB2101010; |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2285 | else { |
| 2286 | weston_log("fatal: unrecognized pixel format: %s\n", s); |
| 2287 | ret = -1; |
| 2288 | } |
| 2289 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2290 | return ret; |
| 2291 | } |
| 2292 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2293 | /** |
| 2294 | * Choose suitable mode for an output |
| 2295 | * |
| 2296 | * Find the most suitable mode to use for initial setup (or reconfiguration on |
| 2297 | * hotplug etc) for a DRM output. |
| 2298 | * |
| 2299 | * @param output DRM output to choose mode for |
| 2300 | * @param kind Strategy and preference to use when choosing mode |
| 2301 | * @param width Desired width for this output |
| 2302 | * @param height Desired height for this output |
| 2303 | * @param current_mode Mode currently being displayed on this output |
| 2304 | * @param modeline Manually-entered mode (may be NULL) |
| 2305 | * @returns A mode from the output's mode list, or NULL if none available |
| 2306 | */ |
| 2307 | static struct drm_mode * |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2308 | drm_output_choose_initial_mode(struct drm_backend *backend, |
| 2309 | struct drm_output *output, |
| 2310 | enum weston_drm_backend_output_mode mode, |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2311 | const char *modeline, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2312 | const drmModeModeInfo *current_mode) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2313 | { |
| 2314 | struct drm_mode *preferred = NULL; |
| 2315 | struct drm_mode *current = NULL; |
| 2316 | struct drm_mode *configured = NULL; |
| 2317 | struct drm_mode *best = NULL; |
| 2318 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2319 | drmModeModeInfo drm_modeline; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2320 | int32_t width = 0; |
| 2321 | int32_t height = 0; |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2322 | uint32_t refresh = 0; |
| 2323 | int n; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2324 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2325 | if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && modeline) { |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2326 | n = sscanf(modeline, "%dx%d@%d", &width, &height, &refresh); |
| 2327 | if (n != 2 && n != 3) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2328 | width = -1; |
| 2329 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2330 | if (parse_modeline(modeline, &drm_modeline) == 0) { |
| 2331 | configured = drm_output_add_mode(output, &drm_modeline); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2332 | if (!configured) |
| 2333 | return NULL; |
| 2334 | } else { |
| 2335 | weston_log("Invalid modeline \"%s\" for output %s\n", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2336 | modeline, output->base.name); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | } |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2340 | |
| 2341 | wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) { |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2342 | if (width == drm_mode->base.width && |
Fabien Dessenne | 2d66a7d | 2017-01-17 17:17:21 +0100 | [diff] [blame] | 2343 | height == drm_mode->base.height && |
| 2344 | (refresh == 0 || refresh == drm_mode->mode_info.vrefresh)) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2345 | configured = drm_mode; |
| 2346 | |
comic fans | 7a5c562 | 2016-03-17 14:29:27 +0200 | [diff] [blame] | 2347 | if (memcmp(current_mode, &drm_mode->mode_info, |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2348 | sizeof *current_mode) == 0) |
| 2349 | current = drm_mode; |
| 2350 | |
| 2351 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 2352 | preferred = drm_mode; |
| 2353 | |
| 2354 | best = drm_mode; |
| 2355 | } |
| 2356 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2357 | if (current == NULL && current_mode->clock != 0) { |
| 2358 | current = drm_output_add_mode(output, current_mode); |
| 2359 | if (!current) |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2363 | if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT) |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2364 | configured = current; |
| 2365 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2366 | if (configured) |
| 2367 | return configured; |
| 2368 | |
| 2369 | if (preferred) |
| 2370 | return preferred; |
| 2371 | |
| 2372 | if (current) |
| 2373 | return current; |
| 2374 | |
| 2375 | if (best) |
| 2376 | return best; |
| 2377 | |
| 2378 | weston_log("no available modes for %s\n", output->base.name); |
| 2379 | return NULL; |
| 2380 | } |
| 2381 | |
Pekka Paalanen | eee580b | 2014-06-04 16:43:06 +0300 | [diff] [blame] | 2382 | static int |
| 2383 | connector_get_current_mode(drmModeConnector *connector, int drm_fd, |
| 2384 | drmModeModeInfo *mode) |
| 2385 | { |
| 2386 | drmModeEncoder *encoder; |
| 2387 | drmModeCrtc *crtc; |
| 2388 | |
| 2389 | /* Get the current mode on the crtc that's currently driving |
| 2390 | * this connector. */ |
| 2391 | encoder = drmModeGetEncoder(drm_fd, connector->encoder_id); |
| 2392 | memset(mode, 0, sizeof *mode); |
| 2393 | if (encoder != NULL) { |
| 2394 | crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id); |
| 2395 | drmModeFreeEncoder(encoder); |
| 2396 | if (crtc == NULL) |
| 2397 | return -1; |
| 2398 | if (crtc->mode_valid) |
| 2399 | *mode = crtc->mode; |
| 2400 | drmModeFreeCrtc(crtc); |
| 2401 | } |
| 2402 | |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
Neil Roberts | 77c1a5b | 2014-03-07 18:05:50 +0000 | [diff] [blame] | 2406 | static int |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2407 | drm_output_set_mode(struct weston_output *base, |
| 2408 | enum weston_drm_backend_output_mode mode, |
| 2409 | const char *modeline) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2410 | { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2411 | struct drm_output *output = to_drm_output(base); |
| 2412 | struct drm_backend *b = to_drm_backend(base->compositor); |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2413 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2414 | struct drm_mode *current; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 2415 | drmModeModeInfo crtc_mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2416 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 2417 | output->base.make = "unknown"; |
| 2418 | output->base.model = "unknown"; |
Richard Hughes | 2b2092a | 2013-04-24 14:58:02 +0100 | [diff] [blame] | 2419 | output->base.serial_number = "unknown"; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 2420 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2421 | if (connector_get_current_mode(output->connector, b->drm.fd, &crtc_mode) < 0) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2422 | return -1; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2423 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2424 | current = drm_output_choose_initial_mode(b, output, mode, modeline, &crtc_mode); |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2425 | if (!current) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2426 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2427 | |
Pekka Paalanen | 7b36b42 | 2014-06-04 14:00:53 +0300 | [diff] [blame] | 2428 | output->base.current_mode = ¤t->base; |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 2429 | output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 2430 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2431 | /* Set native_ fields, so weston_output_mode_switch_to_native() works */ |
| 2432 | output->base.native_mode = output->base.current_mode; |
| 2433 | output->base.native_scale = output->base.current_scale; |
| 2434 | |
| 2435 | output->base.mm_width = output->connector->mmWidth; |
| 2436 | output->base.mm_height = output->connector->mmHeight; |
| 2437 | |
| 2438 | return 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | static void |
| 2442 | drm_output_set_gbm_format(struct weston_output *base, |
| 2443 | const char *gbm_format) |
| 2444 | { |
| 2445 | struct drm_output *output = to_drm_output(base); |
| 2446 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2447 | |
| 2448 | if (parse_gbm_format(gbm_format, b->gbm_format, &output->gbm_format) == -1) |
| 2449 | output->gbm_format = b->gbm_format; |
| 2450 | } |
| 2451 | |
| 2452 | static void |
| 2453 | drm_output_set_seat(struct weston_output *base, |
| 2454 | const char *seat) |
| 2455 | { |
| 2456 | struct drm_output *output = to_drm_output(base); |
| 2457 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2458 | |
| 2459 | setup_output_seat_constraint(b, &output->base, |
| 2460 | seat ? seat : ""); |
| 2461 | } |
| 2462 | |
| 2463 | static int |
| 2464 | drm_output_enable(struct weston_output *base) |
| 2465 | { |
| 2466 | struct drm_output *output = to_drm_output(base); |
| 2467 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2468 | struct weston_mode *m; |
| 2469 | |
| 2470 | output->dpms_prop = drm_get_prop(b->drm.fd, output->connector, "DPMS"); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 2471 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2472 | if (b->pageflip_timeout) |
| 2473 | drm_output_pageflip_timer_create(output); |
| 2474 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2475 | if (b->use_pixman) { |
| 2476 | if (drm_output_init_pixman(output, b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2477 | weston_log("Failed to init output pixman state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2478 | goto err_free; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2479 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2480 | } else if (drm_output_init_egl(output, b) < 0) { |
Ander Conselvan de Oliveira | 475cf15 | 2012-12-14 13:37:29 -0200 | [diff] [blame] | 2481 | weston_log("Failed to init output gl state\n"); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2482 | goto err_free; |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 2483 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 2484 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2485 | if (output->backlight) { |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2486 | weston_log("Initialized backlight, device %s\n", |
| 2487 | output->backlight->path); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2488 | output->base.set_backlight = drm_set_backlight; |
| 2489 | output->base.backlight_current = drm_get_backlight(output); |
Kristian Høgsberg | 220819f | 2013-05-23 20:29:40 -0400 | [diff] [blame] | 2490 | } else { |
| 2491 | weston_log("Failed to initialize backlight\n"); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2492 | } |
| 2493 | |
Jonas Ådahl | e5a1225 | 2013-04-05 23:07:11 +0200 | [diff] [blame] | 2494 | output->base.start_repaint_loop = drm_output_start_repaint_loop; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 2495 | output->base.repaint = drm_output_repaint; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2496 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2497 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 2498 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 2499 | |
Richard Hughes | e729996 | 2013-05-01 21:52:12 +0100 | [diff] [blame] | 2500 | output->base.gamma_size = output->original_crtc->gamma_size; |
| 2501 | output->base.set_gamma = drm_output_set_gamma; |
| 2502 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2503 | output->base.subpixel = drm_subpixel_to_wayland(output->connector->subpixel); |
| 2504 | |
| 2505 | find_and_parse_output_edid(b, output, output->connector); |
Armin Krezović | 9bcf4c5 | 2017-02-08 13:55:26 +0100 | [diff] [blame] | 2506 | if (output->connector->connector_type == DRM_MODE_CONNECTOR_LVDS || |
| 2507 | output->connector->connector_type == DRM_MODE_CONNECTOR_eDP) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2508 | output->base.connection_internal = 1; |
| 2509 | |
Derek Foreman | b042756 | 2016-02-05 15:55:20 -0600 | [diff] [blame] | 2510 | weston_plane_init(&output->cursor_plane, b->compositor, |
| 2511 | INT32_MIN, INT32_MIN); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2512 | weston_plane_init(&output->fb_plane, b->compositor, 0, 0); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2513 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2514 | weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL); |
| 2515 | weston_compositor_stack_plane(b->compositor, &output->fb_plane, |
| 2516 | &b->compositor->primary_plane); |
Ander Conselvan de Oliveira | 8ad1982 | 2013-03-05 17:30:27 +0200 | [diff] [blame] | 2517 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 2518 | weston_log("Output %s, (connector %d, crtc %d)\n", |
Richard Hughes | afe690c | 2013-05-02 10:10:04 +0100 | [diff] [blame] | 2519 | output->base.name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2520 | wl_list_for_each(m, &output->base.mode_list, link) |
U. Artie Eoff | d3ed6cb | 2014-01-10 10:15:17 -0800 | [diff] [blame] | 2521 | weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n", |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2522 | m->width, m->height, m->refresh / 1000.0, |
| 2523 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 2524 | ", preferred" : "", |
| 2525 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 2526 | ", current" : "", |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2527 | output->connector->count_modes == 0 ? |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2528 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 2529 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2530 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2531 | |
| 2532 | err_free: |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2533 | drmModeFreeProperty(output->dpms_prop); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2534 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 2535 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2536 | } |
| 2537 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2538 | static void |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2539 | drm_output_deinit(struct weston_output *base) |
| 2540 | { |
| 2541 | struct drm_output *output = to_drm_output(base); |
| 2542 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2543 | |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2544 | if (b->use_pixman) |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2545 | drm_output_fini_pixman(output); |
Daniel Stone | 3e661f7 | 2016-11-04 17:24:06 +0000 | [diff] [blame] | 2546 | else |
| 2547 | drm_output_fini_egl(output); |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2548 | |
| 2549 | weston_plane_release(&output->fb_plane); |
| 2550 | weston_plane_release(&output->cursor_plane); |
| 2551 | |
| 2552 | drmModeFreeProperty(output->dpms_prop); |
| 2553 | |
| 2554 | /* Turn off hardware cursor */ |
| 2555 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
| 2556 | } |
| 2557 | |
| 2558 | static void |
| 2559 | drm_output_destroy(struct weston_output *base) |
| 2560 | { |
| 2561 | struct drm_output *output = to_drm_output(base); |
| 2562 | struct drm_backend *b = to_drm_backend(base->compositor); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2563 | struct drm_mode *drm_mode, *next; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2564 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 2565 | |
| 2566 | if (output->page_flip_pending) { |
| 2567 | output->destroy_pending = 1; |
| 2568 | weston_log("destroy output while page flip pending\n"); |
| 2569 | return; |
| 2570 | } |
| 2571 | |
| 2572 | if (output->base.enabled) |
| 2573 | drm_output_deinit(&output->base); |
| 2574 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2575 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 2576 | base.link) { |
| 2577 | wl_list_remove(&drm_mode->base.link); |
| 2578 | free(drm_mode); |
| 2579 | } |
| 2580 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2581 | if (origcrtc) { |
| 2582 | /* Restore original CRTC state */ |
| 2583 | drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
| 2584 | origcrtc->x, origcrtc->y, |
| 2585 | &output->connector_id, 1, &origcrtc->mode); |
| 2586 | drmModeFreeCrtc(origcrtc); |
| 2587 | } |
| 2588 | |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 2589 | if (output->pageflip_timer) |
| 2590 | wl_event_source_remove(output->pageflip_timer); |
| 2591 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2592 | weston_output_destroy(&output->base); |
| 2593 | |
| 2594 | drmModeFreeConnector(output->connector); |
| 2595 | |
| 2596 | if (output->backlight) |
| 2597 | backlight_destroy(output->backlight); |
| 2598 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2599 | free(output); |
| 2600 | } |
| 2601 | |
| 2602 | static int |
| 2603 | drm_output_disable(struct weston_output *base) |
| 2604 | { |
| 2605 | struct drm_output *output = to_drm_output(base); |
| 2606 | struct drm_backend *b = to_drm_backend(base->compositor); |
| 2607 | |
| 2608 | if (output->page_flip_pending) { |
| 2609 | output->disable_pending = 1; |
| 2610 | return -1; |
| 2611 | } |
| 2612 | |
| 2613 | if (output->base.enabled) |
| 2614 | drm_output_deinit(&output->base); |
| 2615 | |
| 2616 | output->disable_pending = 0; |
| 2617 | |
| 2618 | weston_log("Disabling output %s\n", output->base.name); |
| 2619 | drmModeSetCrtc(b->drm.fd, output->crtc_id, |
| 2620 | 0, 0, 0, 0, 0, NULL); |
| 2621 | |
| 2622 | return 0; |
| 2623 | } |
| 2624 | |
| 2625 | /** |
| 2626 | * Create a Weston output structure |
| 2627 | * |
| 2628 | * Given a DRM connector, create a matching drm_output structure and add it |
| 2629 | * to Weston's output list. It also takes ownership of the connector, which |
| 2630 | * is released when output is destroyed. |
| 2631 | * |
| 2632 | * @param b Weston backend structure |
| 2633 | * @param resources DRM resources for this device |
| 2634 | * @param connector DRM connector to use for this new output |
| 2635 | * @param drm_device udev device pointer |
| 2636 | * @returns 0 on success, or -1 on failure |
| 2637 | */ |
| 2638 | static int |
| 2639 | create_output_for_connector(struct drm_backend *b, |
| 2640 | drmModeRes *resources, |
| 2641 | drmModeConnector *connector, |
| 2642 | struct udev_device *drm_device) |
| 2643 | { |
| 2644 | struct drm_output *output; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2645 | struct drm_mode *drm_mode; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2646 | int i; |
| 2647 | |
| 2648 | i = find_crtc_for_connector(b, resources, connector); |
| 2649 | if (i < 0) { |
| 2650 | weston_log("No usable crtc/encoder pair for connector.\n"); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2651 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | output = zalloc(sizeof *output); |
| 2655 | if (output == NULL) |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2656 | goto err; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2657 | |
| 2658 | output->connector = connector; |
| 2659 | output->crtc_id = resources->crtcs[i]; |
| 2660 | output->pipe = i; |
| 2661 | output->connector_id = connector->connector_id; |
| 2662 | |
| 2663 | output->backlight = backlight_init(drm_device, |
| 2664 | connector->connector_type); |
| 2665 | |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2666 | output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id); |
| 2667 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2668 | output->base.enable = drm_output_enable; |
| 2669 | output->base.destroy = drm_output_destroy; |
| 2670 | output->base.disable = drm_output_disable; |
| 2671 | output->base.name = make_connector_name(connector); |
| 2672 | |
| 2673 | output->destroy_pending = 0; |
| 2674 | output->disable_pending = 0; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2675 | |
Armin Krezović | 4008740 | 2016-09-30 14:11:12 +0200 | [diff] [blame] | 2676 | weston_output_init(&output->base, b->compositor); |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2677 | |
| 2678 | wl_list_init(&output->base.mode_list); |
| 2679 | |
| 2680 | for (i = 0; i < output->connector->count_modes; i++) { |
| 2681 | drm_mode = drm_output_add_mode(output, &output->connector->modes[i]); |
| 2682 | if (!drm_mode) { |
| 2683 | drm_output_destroy(&output->base); |
| 2684 | return -1; |
| 2685 | } |
| 2686 | } |
| 2687 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2688 | weston_compositor_add_pending_output(&output->base, b->compositor); |
| 2689 | |
| 2690 | return 0; |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2691 | |
| 2692 | err: |
| 2693 | drmModeFreeConnector(connector); |
| 2694 | |
| 2695 | return -1; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2699 | create_sprites(struct drm_backend *b) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2700 | { |
| 2701 | struct drm_sprite *sprite; |
| 2702 | drmModePlaneRes *plane_res; |
| 2703 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 2704 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2705 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2706 | plane_res = drmModeGetPlaneResources(b->drm.fd); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2707 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2708 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2709 | strerror(errno)); |
| 2710 | return; |
| 2711 | } |
| 2712 | |
| 2713 | for (i = 0; i < plane_res->count_planes; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2714 | plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2715 | if (!plane) |
| 2716 | continue; |
| 2717 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 2718 | sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2719 | plane->count_formats)); |
| 2720 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2721 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2722 | __func__); |
Chris Michael | 8b37687 | 2014-01-02 11:39:40 +0000 | [diff] [blame] | 2723 | drmModeFreePlane(plane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2724 | continue; |
| 2725 | } |
| 2726 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2727 | sprite->possible_crtcs = plane->possible_crtcs; |
| 2728 | sprite->plane_id = plane->plane_id; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 2729 | sprite->current = NULL; |
| 2730 | sprite->next = NULL; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2731 | sprite->backend = b; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2732 | sprite->count_formats = plane->count_formats; |
| 2733 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 2734 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2735 | drmModeFreePlane(plane); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2736 | weston_plane_init(&sprite->plane, b->compositor, 0, 0); |
| 2737 | weston_compositor_stack_plane(b->compositor, &sprite->plane, |
| 2738 | &b->compositor->primary_plane); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2739 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2740 | wl_list_insert(&b->sprite_list, &sprite->link); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2741 | } |
| 2742 | |
Samuel Iglesias Gonsalvez | de46673 | 2013-06-13 10:03:33 +0200 | [diff] [blame] | 2743 | drmModeFreePlaneResources(plane_res); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2744 | } |
| 2745 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2746 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2747 | destroy_sprites(struct drm_backend *backend) |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2748 | { |
| 2749 | struct drm_sprite *sprite, *next; |
| 2750 | struct drm_output *output; |
| 2751 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2752 | output = container_of(backend->compositor->output_list.next, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2753 | struct drm_output, base.link); |
| 2754 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2755 | wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) { |
| 2756 | drmModeSetPlane(backend->drm.fd, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2757 | sprite->plane_id, |
| 2758 | output->crtc_id, 0, 0, |
| 2759 | 0, 0, 0, 0, 0, 0, 0, 0); |
Ander Conselvan de Oliveira | 526d461 | 2013-01-25 15:13:03 +0200 | [diff] [blame] | 2760 | drm_output_release_fb(output, sprite->current); |
| 2761 | drm_output_release_fb(output, sprite->next); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 2762 | weston_plane_release(&sprite->plane); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2763 | free(sprite); |
| 2764 | } |
| 2765 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2766 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2767 | static int |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 2768 | create_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2769 | { |
| 2770 | drmModeConnector *connector; |
| 2771 | drmModeRes *resources; |
| 2772 | int i; |
| 2773 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2774 | resources = drmModeGetResources(b->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2775 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2776 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2777 | return -1; |
| 2778 | } |
| 2779 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2780 | b->min_width = resources->min_width; |
| 2781 | b->max_width = resources->max_width; |
| 2782 | b->min_height = resources->min_height; |
| 2783 | b->max_height = resources->max_height; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 2784 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2785 | for (i = 0; i < resources->count_connectors; i++) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2786 | connector = drmModeGetConnector(b->drm.fd, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2787 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2788 | if (connector == NULL) |
| 2789 | continue; |
| 2790 | |
| 2791 | if (connector->connection == DRM_MODE_CONNECTED && |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 2792 | (b->connector == 0 || |
| 2793 | connector->connector_id == b->connector)) { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2794 | if (create_output_for_connector(b, resources, |
Armin Krezović | 445b41b | 2016-10-09 23:48:16 +0200 | [diff] [blame] | 2795 | connector, drm_device) < 0) |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 2796 | continue; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2797 | } else { |
| 2798 | drmModeFreeConnector(connector); |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 2799 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2800 | } |
| 2801 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2802 | if (wl_list_empty(&b->compositor->output_list) && |
| 2803 | wl_list_empty(&b->compositor->pending_output_list)) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2804 | weston_log("No currently active connector found.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2805 | |
| 2806 | drmModeFreeResources(resources); |
| 2807 | |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2811 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2812 | update_outputs(struct drm_backend *b, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2813 | { |
| 2814 | drmModeConnector *connector; |
| 2815 | drmModeRes *resources; |
| 2816 | struct drm_output *output, *next; |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2817 | uint32_t *connected; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2818 | int i; |
| 2819 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2820 | resources = drmModeGetResources(b->drm.fd); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2821 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2822 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2823 | return; |
| 2824 | } |
| 2825 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2826 | connected = calloc(resources->count_connectors, sizeof(uint32_t)); |
| 2827 | if (!connected) { |
| 2828 | drmModeFreeResources(resources); |
| 2829 | return; |
| 2830 | } |
| 2831 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2832 | /* collect new connects */ |
| 2833 | for (i = 0; i < resources->count_connectors; i++) { |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 2834 | uint32_t connector_id = resources->connectors[i]; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2835 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2836 | connector = drmModeGetConnector(b->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 2837 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2838 | continue; |
| 2839 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 2840 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 2841 | drmModeFreeConnector(connector); |
| 2842 | continue; |
| 2843 | } |
| 2844 | |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 2845 | if (b->connector && (b->connector != connector_id)) { |
| 2846 | drmModeFreeConnector(connector); |
| 2847 | continue; |
| 2848 | } |
| 2849 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2850 | connected[i] = connector_id; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2851 | |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 2852 | if (drm_output_find_by_connector(b, connector_id)) { |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2853 | drmModeFreeConnector(connector); |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 2854 | continue; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2855 | } |
Daniel Stone | c0ec759 | 2017-02-09 13:58:35 +0000 | [diff] [blame] | 2856 | |
| 2857 | create_output_for_connector(b, resources, |
| 2858 | connector, drm_device); |
| 2859 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2860 | } |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2861 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2862 | wl_list_for_each_safe(output, next, &b->compositor->output_list, |
| 2863 | base.link) { |
| 2864 | bool disconnected = true; |
| 2865 | |
| 2866 | for (i = 0; i < resources->count_connectors; i++) { |
| 2867 | if (connected[i] == output->connector_id) { |
| 2868 | disconnected = false; |
| 2869 | break; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2870 | } |
| 2871 | } |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 2872 | |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2873 | if (!disconnected) |
| 2874 | continue; |
| 2875 | |
| 2876 | weston_log("connector %d disconnected\n", output->connector_id); |
| 2877 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2878 | } |
Daniel Stone | efc2b1d | 2017-02-09 14:06:31 +0000 | [diff] [blame] | 2879 | |
| 2880 | wl_list_for_each_safe(output, next, &b->compositor->pending_output_list, |
| 2881 | base.link) { |
| 2882 | bool disconnected = true; |
| 2883 | |
| 2884 | for (i = 0; i < resources->count_connectors; i++) { |
| 2885 | if (connected[i] == output->connector_id) { |
| 2886 | disconnected = false; |
| 2887 | break; |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | if (!disconnected) |
| 2892 | continue; |
| 2893 | |
| 2894 | weston_log("connector %d disconnected\n", output->connector_id); |
| 2895 | drm_output_destroy(&output->base); |
| 2896 | } |
| 2897 | |
| 2898 | free(connected); |
| 2899 | drmModeFreeResources(resources); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | static int |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2903 | udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2904 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2905 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2906 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2907 | |
| 2908 | sysnum = udev_device_get_sysnum(device); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2909 | if (!sysnum || atoi(sysnum) != b->drm.id) |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 2910 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2911 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2912 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 2913 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2914 | return 0; |
| 2915 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 2916 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2917 | } |
| 2918 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2919 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2920 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 2921 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2922 | struct drm_backend *b = data; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2923 | struct udev_device *event; |
| 2924 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2925 | event = udev_monitor_receive_device(b->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2926 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2927 | if (udev_event_is_hotplug(b, event)) |
| 2928 | update_outputs(b, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2929 | |
| 2930 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 2931 | |
| 2932 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2933 | } |
| 2934 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2935 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2936 | drm_restore(struct weston_compositor *ec) |
| 2937 | { |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 2938 | weston_launcher_restore(ec->launcher); |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2942 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2943 | { |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2944 | struct drm_backend *b = to_drm_backend(ec); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2945 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2946 | udev_input_destroy(&b->input); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2947 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2948 | wl_event_source_remove(b->udev_drm_source); |
| 2949 | wl_event_source_remove(b->drm_source); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 2950 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2951 | destroy_sprites(b); |
Kristian Høgsberg | 3d64a3e | 2013-05-10 12:36:04 -0400 | [diff] [blame] | 2952 | |
Ander Conselvan de Oliveira | 6b16214 | 2013-10-25 16:26:32 +0300 | [diff] [blame] | 2953 | weston_compositor_shutdown(ec); |
| 2954 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2955 | if (b->gbm) |
| 2956 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 2957 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2958 | weston_launcher_destroy(ec->launcher); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2959 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2960 | close(b->drm.fd); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2961 | free(b); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 2962 | } |
| 2963 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2964 | static void |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2965 | session_notify(struct wl_listener *listener, void *data) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2966 | { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2967 | struct weston_compositor *compositor = data; |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 2968 | struct drm_backend *b = to_drm_backend(compositor); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2969 | struct drm_sprite *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2970 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2971 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2972 | if (compositor->session_active) { |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2973 | weston_log("activating session\n"); |
Daniel Stone | f33e104 | 2016-11-05 08:10:13 +0000 | [diff] [blame] | 2974 | weston_compositor_wake(compositor); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2975 | weston_compositor_damage_all(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2976 | udev_input_enable(&b->input); |
Kristian Høgsberg | 61741a2 | 2013-09-17 16:02:57 -0700 | [diff] [blame] | 2977 | } else { |
| 2978 | weston_log("deactivating session\n"); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2979 | udev_input_disable(&b->input); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 2980 | |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2981 | weston_compositor_offscreen(compositor); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 2982 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2983 | /* If we have a repaint scheduled (either from a |
| 2984 | * pending pageflip or the idle handler), make sure we |
| 2985 | * cancel that so we don't try to pageflip when we're |
Philipp Brüschweiler | 57edf7f | 2013-03-29 13:01:56 +0100 | [diff] [blame] | 2986 | * vt switched away. The OFFSCREEN state will prevent |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 2987 | * further attempts at repainting. When we switch |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2988 | * back, we schedule a repaint, which will process |
| 2989 | * pending frame callbacks. */ |
| 2990 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2991 | wl_list_for_each(output, &compositor->output_list, base.link) { |
Daniel Stone | 09a97e2 | 2017-03-01 11:34:06 +0000 | [diff] [blame] | 2992 | output->base.repaint_needed = false; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2993 | drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2994 | } |
| 2995 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2996 | output = container_of(compositor->output_list.next, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2997 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2998 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 2999 | wl_list_for_each(sprite, &b->sprite_list, link) |
| 3000 | drmModeSetPlane(b->drm.fd, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 3001 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 3002 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 3003 | 0, 0, 0, 0, 0, 0, 0, 0); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 3004 | }; |
| 3005 | } |
| 3006 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3007 | /** |
| 3008 | * Determines whether or not a device is capable of modesetting. If successful, |
| 3009 | * sets b->drm.fd and b->drm.filename to the opened device. |
| 3010 | */ |
| 3011 | static bool |
| 3012 | drm_device_is_kms(struct drm_backend *b, struct udev_device *device) |
| 3013 | { |
| 3014 | const char *filename = udev_device_get_devnode(device); |
| 3015 | const char *sysnum = udev_device_get_sysnum(device); |
| 3016 | drmModeRes *res; |
| 3017 | int id, fd; |
| 3018 | |
| 3019 | if (!filename) |
| 3020 | return false; |
| 3021 | |
| 3022 | fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR); |
| 3023 | if (fd < 0) |
| 3024 | return false; |
| 3025 | |
| 3026 | res = drmModeGetResources(fd); |
| 3027 | if (!res) |
| 3028 | goto out_fd; |
| 3029 | |
| 3030 | if (res->count_crtcs <= 0 || res->count_connectors <= 0 || |
| 3031 | res->count_encoders <= 0) |
| 3032 | goto out_res; |
| 3033 | |
| 3034 | if (sysnum) |
| 3035 | id = atoi(sysnum); |
| 3036 | if (!sysnum || id < 0) { |
| 3037 | weston_log("couldn't get sysnum for device %s\n", filename); |
| 3038 | goto out_res; |
| 3039 | } |
| 3040 | |
| 3041 | /* We can be called successfully on multiple devices; if we have, |
| 3042 | * clean up old entries. */ |
| 3043 | if (b->drm.fd >= 0) |
| 3044 | weston_launcher_close(b->compositor->launcher, b->drm.fd); |
| 3045 | free(b->drm.filename); |
| 3046 | |
| 3047 | b->drm.fd = fd; |
| 3048 | b->drm.id = id; |
| 3049 | b->drm.filename = strdup(filename); |
| 3050 | |
Sergi Granell | ceb5981 | 2017-03-28 12:44:04 +0200 | [diff] [blame] | 3051 | drmModeFreeResources(res); |
| 3052 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3053 | return true; |
| 3054 | |
| 3055 | out_res: |
| 3056 | drmModeFreeResources(res); |
| 3057 | out_fd: |
| 3058 | weston_launcher_close(b->compositor->launcher, fd); |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3062 | /* |
| 3063 | * Find primary GPU |
| 3064 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 3065 | * function loops over all devices and tries to find a PCI device with the |
| 3066 | * boot_vga sysfs attribute set to 1. |
| 3067 | * If no such device is found, the first DRM device reported by udev is used. |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3068 | * Devices are also vetted to make sure they are are capable of modesetting, |
| 3069 | * rather than pure render nodes (GPU with no display), or pure |
| 3070 | * memory-allocation devices (VGEM). |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3071 | */ |
| 3072 | static struct udev_device* |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3073 | find_primary_gpu(struct drm_backend *b, const char *seat) |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3074 | { |
| 3075 | struct udev_enumerate *e; |
| 3076 | struct udev_list_entry *entry; |
| 3077 | const char *path, *device_seat, *id; |
| 3078 | struct udev_device *device, *drm_device, *pci; |
| 3079 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3080 | e = udev_enumerate_new(b->udev); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3081 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 3082 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 3083 | |
| 3084 | udev_enumerate_scan_devices(e); |
| 3085 | drm_device = NULL; |
| 3086 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3087 | bool is_boot_vga = false; |
| 3088 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3089 | path = udev_list_entry_get_name(entry); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3090 | device = udev_device_new_from_syspath(b->udev, path); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3091 | if (!device) |
| 3092 | continue; |
| 3093 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 3094 | if (!device_seat) |
| 3095 | device_seat = default_seat; |
| 3096 | if (strcmp(device_seat, seat)) { |
| 3097 | udev_device_unref(device); |
| 3098 | continue; |
| 3099 | } |
| 3100 | |
| 3101 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 3102 | "pci", NULL); |
| 3103 | if (pci) { |
| 3104 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3105 | if (id && !strcmp(id, "1")) |
| 3106 | is_boot_vga = true; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3107 | } |
| 3108 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3109 | /* If we already have a modesetting-capable device, and this |
| 3110 | * device isn't our boot-VGA device, we aren't going to use |
| 3111 | * it. */ |
| 3112 | if (!is_boot_vga && drm_device) { |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3113 | udev_device_unref(device); |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3114 | continue; |
| 3115 | } |
| 3116 | |
| 3117 | /* Make sure this device is actually capable of modesetting; |
| 3118 | * if this call succeeds, b->drm.{fd,filename} will be set, |
| 3119 | * and any old values freed. */ |
| 3120 | if (!drm_device_is_kms(b, device)) { |
| 3121 | udev_device_unref(device); |
| 3122 | continue; |
| 3123 | } |
| 3124 | |
| 3125 | /* There can only be one boot_vga device, and we try to use it |
| 3126 | * at all costs. */ |
| 3127 | if (is_boot_vga) { |
| 3128 | if (drm_device) |
| 3129 | udev_device_unref(drm_device); |
| 3130 | drm_device = device; |
| 3131 | break; |
| 3132 | } |
| 3133 | |
| 3134 | /* Per the (!is_boot_vga && drm_device) test above, we only |
| 3135 | * trump existing saved devices with boot-VGA devices, so if |
| 3136 | * we end up here, this must be the first device we've seen. */ |
| 3137 | assert(!drm_device); |
| 3138 | drm_device = device; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3139 | } |
| 3140 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3141 | /* If we're returning a device to use, we must have an open FD for |
| 3142 | * it. */ |
| 3143 | assert(!!drm_device == (b->drm.fd >= 0)); |
| 3144 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3145 | udev_enumerate_unref(e); |
| 3146 | return drm_device; |
| 3147 | } |
| 3148 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3149 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3150 | planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
| 3151 | void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3152 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3153 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3154 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3155 | switch (key) { |
| 3156 | case KEY_C: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3157 | b->cursors_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3158 | break; |
| 3159 | case KEY_V: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3160 | b->sprites_are_broken ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3161 | break; |
| 3162 | case KEY_O: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3163 | b->sprites_hidden ^= 1; |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 3164 | break; |
| 3165 | default: |
| 3166 | break; |
| 3167 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
Kristian Høgsberg | 0eac34a | 2013-08-30 14:28:22 -0700 | [diff] [blame] | 3170 | #ifdef BUILD_VAAPI_RECORDER |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3171 | static void |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3172 | recorder_destroy(struct drm_output *output) |
| 3173 | { |
| 3174 | vaapi_recorder_destroy(output->recorder); |
| 3175 | output->recorder = NULL; |
| 3176 | |
| 3177 | output->base.disable_planes--; |
| 3178 | |
| 3179 | wl_list_remove(&output->recorder_frame_listener.link); |
| 3180 | weston_log("[libva recorder] done\n"); |
| 3181 | } |
| 3182 | |
| 3183 | static void |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3184 | recorder_frame_notify(struct wl_listener *listener, void *data) |
| 3185 | { |
| 3186 | struct drm_output *output; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3187 | struct drm_backend *b; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3188 | int fd, ret; |
| 3189 | |
| 3190 | output = container_of(listener, struct drm_output, |
| 3191 | recorder_frame_listener); |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3192 | b = to_drm_backend(output->base.compositor); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3193 | |
| 3194 | if (!output->recorder) |
| 3195 | return; |
| 3196 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3197 | ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3198 | DRM_CLOEXEC, &fd); |
| 3199 | if (ret) { |
| 3200 | weston_log("[libva recorder] " |
| 3201 | "failed to create prime fd for front buffer\n"); |
| 3202 | return; |
| 3203 | } |
| 3204 | |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3205 | ret = vaapi_recorder_frame(output->recorder, fd, |
| 3206 | output->current->stride); |
| 3207 | if (ret < 0) { |
| 3208 | weston_log("[libva recorder] aborted: %m\n"); |
| 3209 | recorder_destroy(output); |
| 3210 | } |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | static void * |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3214 | create_recorder(struct drm_backend *b, int width, int height, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3215 | const char *filename) |
| 3216 | { |
| 3217 | int fd; |
| 3218 | drm_magic_t magic; |
| 3219 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3220 | fd = open(b->drm.filename, O_RDWR | O_CLOEXEC); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3221 | if (fd < 0) |
| 3222 | return NULL; |
| 3223 | |
| 3224 | drmGetMagic(fd, &magic); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3225 | drmAuthMagic(b->drm.fd, magic); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3226 | |
| 3227 | return vaapi_recorder_create(fd, width, height, filename); |
| 3228 | } |
| 3229 | |
| 3230 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3231 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3232 | void *data) |
| 3233 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3234 | struct drm_backend *b = data; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3235 | struct drm_output *output; |
| 3236 | int width, height; |
| 3237 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3238 | output = container_of(b->compositor->output_list.next, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3239 | struct drm_output, base.link); |
| 3240 | |
| 3241 | if (!output->recorder) { |
Miguel A. Vico | fcf4b6c | 2016-03-21 17:41:03 +0100 | [diff] [blame] | 3242 | if (output->gbm_format != GBM_FORMAT_XRGB8888) { |
Ander Conselvan de Oliveira | 2ef1cd1 | 2014-05-06 16:49:06 +0300 | [diff] [blame] | 3243 | weston_log("failed to start vaapi recorder: " |
| 3244 | "output format not supported\n"); |
| 3245 | return; |
| 3246 | } |
| 3247 | |
Hardening | ff39efa | 2013-09-18 23:56:35 +0200 | [diff] [blame] | 3248 | width = output->base.current_mode->width; |
| 3249 | height = output->base.current_mode->height; |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3250 | |
| 3251 | output->recorder = |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3252 | create_recorder(b, width, height, "capture.h264"); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3253 | if (!output->recorder) { |
| 3254 | weston_log("failed to create vaapi recorder\n"); |
| 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | output->base.disable_planes++; |
| 3259 | |
| 3260 | output->recorder_frame_listener.notify = recorder_frame_notify; |
| 3261 | wl_signal_add(&output->base.frame_signal, |
| 3262 | &output->recorder_frame_listener); |
| 3263 | |
| 3264 | weston_output_schedule_repaint(&output->base); |
| 3265 | |
| 3266 | weston_log("[libva recorder] initialized\n"); |
| 3267 | } else { |
Ander Conselvan de Oliveira | 2d13fde | 2014-05-09 15:57:38 +0300 | [diff] [blame] | 3268 | recorder_destroy(output); |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | #else |
| 3272 | static void |
Bryce Harrington | 4a8a3a1 | 2015-07-16 19:12:26 -0700 | [diff] [blame] | 3273 | recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key, |
Ander Conselvan de Oliveira | 6aae4d3 | 2013-08-23 17:15:48 +0300 | [diff] [blame] | 3274 | void *data) |
| 3275 | { |
| 3276 | weston_log("Compiled without libva support\n"); |
| 3277 | } |
| 3278 | #endif |
| 3279 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3280 | static void |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3281 | switch_to_gl_renderer(struct drm_backend *b) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3282 | { |
| 3283 | struct drm_output *output; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3284 | bool dmabuf_support_inited; |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3285 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3286 | if (!b->use_pixman) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3287 | return; |
| 3288 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3289 | dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf; |
| 3290 | |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3291 | weston_log("Switching to GL renderer\n"); |
| 3292 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3293 | b->gbm = create_gbm_device(b->drm.fd); |
| 3294 | if (!b->gbm) { |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3295 | weston_log("Failed to create gbm device. " |
| 3296 | "Aborting renderer switch\n"); |
| 3297 | return; |
| 3298 | } |
| 3299 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3300 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3301 | pixman_renderer_output_destroy(&output->base); |
| 3302 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3303 | b->compositor->renderer->destroy(b->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3304 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3305 | if (drm_backend_create_gl_renderer(b) < 0) { |
| 3306 | gbm_device_destroy(b->gbm); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3307 | weston_log("Failed to create GL renderer. Quitting.\n"); |
| 3308 | /* FIXME: we need a function to shutdown cleanly */ |
| 3309 | assert(0); |
| 3310 | } |
| 3311 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3312 | wl_list_for_each(output, &b->compositor->output_list, base.link) |
| 3313 | drm_output_init_egl(output, b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3314 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3315 | b->use_pixman = 0; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3316 | |
| 3317 | if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) { |
| 3318 | if (linux_dmabuf_setup(b->compositor) < 0) |
| 3319 | weston_log("Error: initializing dmabuf " |
| 3320 | "support failed.\n"); |
| 3321 | } |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | static void |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3325 | renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time, |
| 3326 | uint32_t key, void *data) |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3327 | { |
Derek Foreman | 8ae2db5 | 2015-07-15 13:00:36 -0500 | [diff] [blame] | 3328 | struct drm_backend *b = |
Armin Krezović | 545dba6 | 2016-08-05 15:54:18 +0200 | [diff] [blame] | 3329 | to_drm_backend(keyboard->seat->compositor); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3330 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3331 | switch_to_gl_renderer(b); |
Ander Conselvan de Oliveira | 6579681 | 2013-11-19 15:22:04 +0200 | [diff] [blame] | 3332 | } |
| 3333 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3334 | static const struct weston_drm_output_api api = { |
| 3335 | drm_output_set_mode, |
| 3336 | drm_output_set_gbm_format, |
| 3337 | drm_output_set_seat, |
| 3338 | }; |
| 3339 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3340 | static struct drm_backend * |
| 3341 | drm_backend_create(struct weston_compositor *compositor, |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3342 | struct weston_drm_backend_config *config) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3343 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3344 | struct drm_backend *b; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 3345 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3346 | struct wl_event_loop *loop; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3347 | const char *seat_id = default_seat; |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3348 | int ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3349 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 3350 | weston_log("initializing drm backend\n"); |
| 3351 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3352 | b = zalloc(sizeof *b); |
| 3353 | if (b == NULL) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3354 | return NULL; |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 3355 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3356 | b->drm.fd = -1; |
| 3357 | |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3358 | /* |
| 3359 | * KMS support for hardware planes cannot properly synchronize |
| 3360 | * without nuclear page flip. Without nuclear/atomic, hw plane |
| 3361 | * and cursor plane updates would either tear or cause extra |
| 3362 | * waits for vblanks which means dropping the compositor framerate |
Pekka Paalanen | 3f32a13 | 2015-09-07 15:38:43 +0300 | [diff] [blame] | 3363 | * to a fraction. For cursors, it's not so bad, so they are |
| 3364 | * enabled. |
Pekka Paalanen | 6858383 | 2015-05-19 09:53:16 +0300 | [diff] [blame] | 3365 | * |
| 3366 | * These can be enabled again when nuclear/atomic support lands. |
| 3367 | */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3368 | b->sprites_are_broken = 1; |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3369 | b->compositor = compositor; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3370 | b->use_pixman = config->use_pixman; |
Emmanuel Gil Peyrot | 11ae2a3 | 2017-03-07 13:27:54 +0000 | [diff] [blame] | 3371 | b->pageflip_timeout = config->pageflip_timeout; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3372 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3373 | if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0) |
| 3374 | goto err_compositor; |
Kristian Høgsberg | 8e6f376 | 2013-10-16 16:31:42 -0700 | [diff] [blame] | 3375 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3376 | if (config->seat_id) |
| 3377 | seat_id = config->seat_id; |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3378 | |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3379 | /* Check if we run drm-backend using weston-launch */ |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3380 | compositor->launcher = weston_launcher_connect(compositor, config->tty, |
| 3381 | seat_id, true); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3382 | if (compositor->launcher == NULL) { |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3383 | weston_log("fatal: drm backend should be run " |
Kristian Høgsberg | b76237e | 2013-04-04 21:36:20 -0400 | [diff] [blame] | 3384 | "using weston-launch binary or as root\n"); |
Rafal Mielniczuk | 32069c0 | 2013-03-27 18:39:28 +0100 | [diff] [blame] | 3385 | goto err_compositor; |
| 3386 | } |
| 3387 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3388 | b->udev = udev_new(); |
| 3389 | if (b->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3390 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3391 | goto err_launcher; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3392 | } |
| 3393 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3394 | b->session_listener.notify = session_notify; |
| 3395 | wl_signal_add(&compositor->session_signal, &b->session_listener); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 3396 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3397 | drm_device = find_primary_gpu(b, seat_id); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 3398 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3399 | weston_log("no drm device found\n"); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3400 | goto err_udev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3401 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3402 | |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3403 | if (init_kms_caps(b) < 0) { |
Ander Conselvan de Oliveira | 2292917 | 2013-01-25 15:13:02 +0200 | [diff] [blame] | 3404 | weston_log("failed to initialize kms\n"); |
| 3405 | goto err_udev_dev; |
| 3406 | } |
| 3407 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3408 | if (b->use_pixman) { |
| 3409 | if (init_pixman(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3410 | weston_log("failed to initialize pixman renderer\n"); |
| 3411 | goto err_udev_dev; |
| 3412 | } |
| 3413 | } else { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3414 | if (init_egl(b) < 0) { |
Ander Conselvan de Oliveira | 5fb4414 | 2013-01-25 15:13:05 +0200 | [diff] [blame] | 3415 | weston_log("failed to initialize egl\n"); |
| 3416 | goto err_udev_dev; |
| 3417 | } |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3418 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 3419 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3420 | b->base.destroy = drm_destroy; |
| 3421 | b->base.restore = drm_restore; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 3422 | |
Bob Ham | 91880f1 | 2016-01-12 10:21:47 +0000 | [diff] [blame] | 3423 | weston_setup_vt_switch_bindings(compositor); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 3424 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3425 | wl_list_init(&b->sprite_list); |
| 3426 | create_sprites(b); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 3427 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3428 | if (udev_input_init(&b->input, |
Giulio Camuffo | 8aedf7b | 2016-06-02 21:48:12 +0300 | [diff] [blame] | 3429 | compositor, b->udev, seat_id, |
| 3430 | config->configure_device) < 0) { |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3431 | weston_log("failed to create input devices\n"); |
| 3432 | goto err_sprite; |
| 3433 | } |
| 3434 | |
Ucan, Emre (ADITG/SW1) | 21e4944 | 2017-02-02 14:06:55 +0000 | [diff] [blame] | 3435 | b->connector = config->connector; |
| 3436 | |
Ucan, Emre (ADITG/SW1) | 9a200d7 | 2017-02-02 14:06:56 +0000 | [diff] [blame] | 3437 | if (create_outputs(b, drm_device) < 0) { |
Daniel Stone | efa504f | 2016-12-19 16:48:20 +0000 | [diff] [blame] | 3438 | weston_log("failed to create output for %s\n", b->drm.filename); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3439 | goto err_udev_input; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3440 | } |
| 3441 | |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3442 | /* A this point we have some idea of whether or not we have a working |
| 3443 | * cursor plane. */ |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3444 | if (!b->cursors_are_broken) |
| 3445 | compositor->capabilities |= WESTON_CAP_CURSOR_PLANE; |
Jason Ekstrand | 9fc7151 | 2014-04-02 19:53:46 -0500 | [diff] [blame] | 3446 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3447 | loop = wl_display_get_event_loop(compositor->wl_display); |
| 3448 | b->drm_source = |
| 3449 | wl_event_loop_add_fd(loop, b->drm.fd, |
| 3450 | WL_EVENT_READABLE, on_drm_input, b); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 3451 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3452 | b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev"); |
| 3453 | if (b->udev_monitor == NULL) { |
Abdur Rehman | 4dca0e1 | 2017-01-01 19:46:35 +0500 | [diff] [blame] | 3454 | weston_log("failed to initialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3455 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3456 | } |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3457 | udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3458 | "drm", NULL); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3459 | b->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3460 | wl_event_loop_add_fd(loop, |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3461 | udev_monitor_get_fd(b->udev_monitor), |
| 3462 | WL_EVENT_READABLE, udev_drm_event, b); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3463 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3464 | if (udev_monitor_enable_receiving(b->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 3465 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3466 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 3467 | } |
| 3468 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3469 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3470 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3471 | weston_compositor_add_debug_binding(compositor, KEY_O, |
| 3472 | planes_binding, b); |
| 3473 | weston_compositor_add_debug_binding(compositor, KEY_C, |
| 3474 | planes_binding, b); |
| 3475 | weston_compositor_add_debug_binding(compositor, KEY_V, |
| 3476 | planes_binding, b); |
| 3477 | weston_compositor_add_debug_binding(compositor, KEY_Q, |
| 3478 | recorder_binding, b); |
| 3479 | weston_compositor_add_debug_binding(compositor, KEY_W, |
| 3480 | renderer_switch_binding, b); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 3481 | |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3482 | if (compositor->renderer->import_dmabuf) { |
| 3483 | if (linux_dmabuf_setup(compositor) < 0) |
| 3484 | weston_log("Error: initializing dmabuf " |
| 3485 | "support failed.\n"); |
| 3486 | } |
| 3487 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3488 | compositor->backend = &b->base; |
Pekka Paalanen | e4d231e | 2014-06-12 15:12:48 +0300 | [diff] [blame] | 3489 | |
Armin Krezović | 0836813 | 2016-09-30 14:11:05 +0200 | [diff] [blame] | 3490 | ret = weston_plugin_api_register(compositor, WESTON_DRM_OUTPUT_API_NAME, |
| 3491 | &api, sizeof(api)); |
| 3492 | |
| 3493 | if (ret < 0) { |
| 3494 | weston_log("Failed to register output API.\n"); |
| 3495 | goto err_udev_monitor; |
| 3496 | } |
| 3497 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3498 | return b; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3499 | |
| 3500 | err_udev_monitor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3501 | wl_event_source_remove(b->udev_drm_source); |
| 3502 | udev_monitor_unref(b->udev_monitor); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3503 | err_drm_source: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3504 | wl_event_source_remove(b->drm_source); |
Ander Conselvan de Oliveira | 4ade0e4 | 2014-04-17 13:08:45 +0300 | [diff] [blame] | 3505 | err_udev_input: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3506 | udev_input_destroy(&b->input); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 3507 | err_sprite: |
Emmanuel Gil Peyrot | b8347e3 | 2016-05-02 22:40:13 +0100 | [diff] [blame] | 3508 | if (b->gbm) |
| 3509 | gbm_device_destroy(b->gbm); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3510 | destroy_sprites(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3511 | err_udev_dev: |
| 3512 | udev_device_unref(drm_device); |
Kristian Høgsberg | 3f49587 | 2013-09-18 23:00:17 -0700 | [diff] [blame] | 3513 | err_launcher: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3514 | weston_launcher_destroy(compositor->launcher); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3515 | err_udev: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3516 | udev_unref(b->udev); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3517 | err_compositor: |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3518 | weston_compositor_shutdown(compositor); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3519 | free(b); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 3520 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 3521 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3522 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3523 | static void |
| 3524 | config_init_to_defaults(struct weston_drm_backend_config *config) |
| 3525 | { |
| 3526 | } |
| 3527 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3528 | WL_EXPORT int |
Quentin Glidic | 23e1d6f | 2016-12-02 14:08:44 +0100 | [diff] [blame] | 3529 | weston_backend_init(struct weston_compositor *compositor, |
| 3530 | struct weston_backend_config *config_base) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3531 | { |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3532 | struct drm_backend *b; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3533 | struct weston_drm_backend_config config = {{ 0, }}; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3534 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3535 | if (config_base == NULL || |
| 3536 | config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION || |
| 3537 | config_base->struct_size > sizeof(struct weston_drm_backend_config)) { |
| 3538 | weston_log("drm backend config structure is invalid\n"); |
| 3539 | return -1; |
| 3540 | } |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 3541 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3542 | config_init_to_defaults(&config); |
| 3543 | memcpy(&config, config_base, config_base->struct_size); |
Kristian Høgsberg | d8e9833 | 2013-10-16 16:15:11 -0700 | [diff] [blame] | 3544 | |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3545 | b = drm_backend_create(compositor, &config); |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3546 | if (b == NULL) |
| 3547 | return -1; |
Giulio Camuffo | 1c0e40d | 2016-04-29 15:40:34 -0700 | [diff] [blame] | 3548 | |
Giulio Camuffo | 954f183 | 2014-10-11 18:27:30 +0300 | [diff] [blame] | 3549 | return 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 3550 | } |