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