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