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