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