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 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 14 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 22 | */ |
| 23 | |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 24 | #define _GNU_SOURCE |
| 25 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 26 | #include <errno.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 31 | #include <linux/input.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 32 | #include <assert.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 34 | #include <xf86drm.h> |
| 35 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 36 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 37 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 38 | #include <gbm.h> |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 39 | #include <libbacklight.h> |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 40 | #include <libudev.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 41 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 42 | #include "compositor.h" |
John Kåre Alsaker | 30d2b1f | 2012-11-13 19:10:28 +0100 | [diff] [blame] | 43 | #include "gl-renderer.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 44 | #include "evdev.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 45 | #include "launcher-util.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 46 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 47 | static int option_current_mode = 0; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 48 | static char *output_name; |
| 49 | static char *output_mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 50 | static char *output_transform; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 51 | static struct wl_list configured_output_list; |
| 52 | |
| 53 | enum output_config { |
| 54 | OUTPUT_CONFIG_INVALID = 0, |
| 55 | OUTPUT_CONFIG_OFF, |
| 56 | OUTPUT_CONFIG_PREFERRED, |
| 57 | OUTPUT_CONFIG_CURRENT, |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 58 | OUTPUT_CONFIG_MODE, |
| 59 | OUTPUT_CONFIG_MODELINE |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | struct drm_configured_output { |
| 63 | char *name; |
| 64 | char *mode; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 65 | uint32_t transform; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 66 | int32_t width, height; |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 67 | drmModeModeInfo crtc_mode; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 68 | enum output_config config; |
| 69 | struct wl_list link; |
| 70 | }; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 71 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 72 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 73 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 74 | |
| 75 | struct udev *udev; |
| 76 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 77 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 78 | struct udev_monitor *udev_monitor; |
| 79 | struct wl_event_source *udev_drm_source; |
| 80 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 81 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 82 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 83 | int fd; |
| 84 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 85 | struct gbm_device *gbm; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 86 | uint32_t *crtcs; |
| 87 | int num_crtcs; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 88 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 89 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 90 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 91 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 92 | /* we need these parameters in order to not fail drmModeAddFB2() |
| 93 | * due to out of bounds dimensions, and then mistakenly set |
| 94 | * sprites_are_broken: |
| 95 | */ |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 96 | uint32_t min_width, max_width; |
| 97 | uint32_t min_height, max_height; |
| 98 | int no_addfb2; |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 99 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 100 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 101 | int sprites_are_broken; |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 102 | int sprites_hidden; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 103 | |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 104 | int cursors_are_broken; |
| 105 | |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 106 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 109 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 110 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 111 | drmModeModeInfo mode_info; |
| 112 | }; |
| 113 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 114 | struct drm_output; |
| 115 | |
| 116 | struct drm_fb { |
| 117 | struct gbm_bo *bo; |
| 118 | struct drm_output *output; |
| 119 | uint32_t fb_id; |
| 120 | int is_client_buffer; |
| 121 | struct wl_buffer *buffer; |
| 122 | struct wl_listener buffer_destroy_listener; |
| 123 | }; |
| 124 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 125 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 126 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 127 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 128 | char *name; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 129 | uint32_t crtc_id; |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 130 | int pipe; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 131 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 132 | drmModeCrtcPtr original_crtc; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 133 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 134 | int vblank_pending; |
| 135 | int page_flip_pending; |
| 136 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 137 | struct gbm_surface *surface; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 138 | struct gbm_bo *cursor_bo[2]; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 139 | struct weston_plane cursor_plane; |
| 140 | struct weston_plane fb_plane; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 141 | struct weston_surface *cursor_surface; |
| 142 | int current_cursor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 143 | struct drm_fb *current, *next; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 144 | struct backlight *backlight; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 145 | }; |
| 146 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 147 | /* |
| 148 | * An output has a primary display plane plus zero or more sprites for |
| 149 | * blending display contents. |
| 150 | */ |
| 151 | struct drm_sprite { |
| 152 | struct wl_list link; |
| 153 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 154 | struct weston_plane plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 155 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 156 | struct drm_fb *current, *next; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 157 | struct drm_output *output; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 158 | struct drm_compositor *compositor; |
| 159 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 160 | uint32_t possible_crtcs; |
| 161 | uint32_t plane_id; |
| 162 | uint32_t count_formats; |
| 163 | |
| 164 | int32_t src_x, src_y; |
| 165 | uint32_t src_w, src_h; |
| 166 | uint32_t dest_x, dest_y; |
| 167 | uint32_t dest_w, dest_h; |
| 168 | |
| 169 | uint32_t formats[]; |
| 170 | }; |
| 171 | |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 172 | struct drm_seat { |
| 173 | struct weston_seat base; |
| 174 | struct wl_list devices_list; |
| 175 | struct udev_monitor *udev_monitor; |
| 176 | struct wl_event_source *udev_monitor_source; |
| 177 | char *seat_id; |
| 178 | }; |
| 179 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 180 | static void |
| 181 | drm_output_set_cursor(struct drm_output *output); |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 182 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 183 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 184 | drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported) |
| 185 | { |
| 186 | struct weston_compositor *ec = output_base->compositor; |
| 187 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 188 | struct drm_output *output = (struct drm_output *) output_base; |
| 189 | int crtc; |
| 190 | |
| 191 | for (crtc = 0; crtc < c->num_crtcs; crtc++) { |
| 192 | if (c->crtcs[crtc] != output->crtc_id) |
| 193 | continue; |
| 194 | |
| 195 | if (supported & (1 << crtc)) |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 202 | static void |
| 203 | drm_fb_destroy_callback(struct gbm_bo *bo, void *data) |
| 204 | { |
| 205 | struct drm_fb *fb = data; |
| 206 | struct gbm_device *gbm = gbm_bo_get_device(bo); |
| 207 | |
| 208 | if (fb->fb_id) |
| 209 | drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id); |
| 210 | |
| 211 | if (fb->buffer) { |
| 212 | weston_buffer_post_release(fb->buffer); |
| 213 | wl_list_remove(&fb->buffer_destroy_listener.link); |
| 214 | } |
| 215 | |
| 216 | free(data); |
| 217 | } |
| 218 | |
| 219 | static struct drm_fb * |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 220 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_compositor *compositor) |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 221 | { |
| 222 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 223 | uint32_t width, height, stride, handle, format; |
| 224 | uint32_t handles[4], pitches[4], offsets[4]; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 225 | int ret; |
| 226 | |
| 227 | if (fb) |
| 228 | return fb; |
| 229 | |
| 230 | fb = malloc(sizeof *fb); |
| 231 | |
| 232 | fb->bo = bo; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 233 | fb->is_client_buffer = 0; |
| 234 | fb->buffer = NULL; |
| 235 | |
| 236 | width = gbm_bo_get_width(bo); |
| 237 | height = gbm_bo_get_height(bo); |
Kristian Høgsberg | 270a7cb | 2012-07-16 16:44:16 -0400 | [diff] [blame] | 238 | stride = gbm_bo_get_stride(bo); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 239 | handle = gbm_bo_get_handle(bo).u32; |
| 240 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 241 | if (compositor->min_width > width || width > compositor->max_width || |
| 242 | compositor->min_height > height || |
| 243 | height > compositor->max_height) { |
| 244 | weston_log("bo geometry out of bounds\n"); |
| 245 | goto err_free; |
| 246 | } |
| 247 | |
| 248 | ret = -1; |
| 249 | |
| 250 | format = gbm_bo_get_format(bo); |
| 251 | |
| 252 | if (format && !compositor->no_addfb2) { |
| 253 | handles[0] = handle; |
| 254 | pitches[0] = stride; |
| 255 | offsets[0] = 0; |
| 256 | |
| 257 | ret = drmModeAddFB2(compositor->drm.fd, width, height, |
| 258 | format, handles, pitches, offsets, |
| 259 | &fb->fb_id, 0); |
| 260 | if (ret) { |
| 261 | weston_log("addfb2 failed: %m\n"); |
| 262 | compositor->no_addfb2 = 1; |
| 263 | compositor->sprites_are_broken = 1; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (ret) |
| 268 | ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32, |
| 269 | stride, handle, &fb->fb_id); |
| 270 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 271 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 272 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 273 | goto err_free; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); |
| 277 | |
| 278 | return fb; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 279 | |
| 280 | err_free: |
| 281 | free(fb); |
| 282 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void |
| 286 | fb_handle_buffer_destroy(struct wl_listener *listener, void *data) |
| 287 | { |
| 288 | struct drm_fb *fb = container_of(listener, struct drm_fb, |
| 289 | buffer_destroy_listener); |
| 290 | |
| 291 | fb->buffer = NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 292 | } |
| 293 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 294 | static void |
| 295 | drm_fb_set_buffer(struct drm_fb *fb, struct wl_buffer *buffer) |
| 296 | { |
| 297 | assert(fb->buffer == NULL); |
| 298 | |
| 299 | fb->is_client_buffer = 1; |
| 300 | fb->buffer = buffer; |
| 301 | fb->buffer->busy_count++; |
| 302 | fb->buffer_destroy_listener.notify = fb_handle_buffer_destroy; |
| 303 | |
| 304 | wl_signal_add(&fb->buffer->resource.destroy_signal, |
| 305 | &fb->buffer_destroy_listener); |
| 306 | } |
| 307 | |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame^] | 308 | static int |
| 309 | drm_output_check_scanout_format(struct drm_output *output, |
| 310 | struct weston_surface *es, struct gbm_bo *bo) |
| 311 | { |
| 312 | int ret = 0; |
| 313 | uint32_t format; |
| 314 | pixman_region32_t r; |
| 315 | |
| 316 | format = gbm_bo_get_format(bo); |
| 317 | |
| 318 | if (format == GBM_FORMAT_XRGB8888) |
| 319 | ret = 1; |
| 320 | else if (format == GBM_FORMAT_ARGB8888) { |
| 321 | /* We can only scanout an ARGB buffer if the surface's |
| 322 | * opaque region covers the whole output */ |
| 323 | pixman_region32_init(&r); |
| 324 | pixman_region32_subtract(&r, &output->base.region, |
| 325 | &es->opaque); |
| 326 | |
| 327 | if (!pixman_region32_not_empty(&r)) |
| 328 | ret = 1; |
| 329 | |
| 330 | pixman_region32_fini(&r); |
| 331 | } |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 336 | static struct weston_plane * |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 337 | drm_output_prepare_scanout_surface(struct weston_output *_output, |
| 338 | struct weston_surface *es) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 339 | { |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 340 | struct drm_output *output = (struct drm_output *) _output; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 341 | struct drm_compositor *c = |
| 342 | (struct drm_compositor *) output->base.compositor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 343 | struct gbm_bo *bo; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 344 | |
Kristian Høgsberg | 101cb65 | 2012-02-17 10:45:16 -0500 | [diff] [blame] | 345 | if (es->geometry.x != output->base.x || |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 346 | es->geometry.y != output->base.y || |
Ander Conselvan de Oliveira | 2908a3d | 2012-11-27 17:03:43 +0200 | [diff] [blame] | 347 | es->buffer == NULL || |
| 348 | es->buffer->width != output->base.current->width || |
| 349 | es->buffer->height != output->base.current->height || |
| 350 | output->base.transform != es->buffer_transform || |
| 351 | es->transform.enabled) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 352 | return NULL; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 353 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 354 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 355 | es->buffer, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 356 | |
Rob Bradford | 9b10187 | 2012-09-14 23:25:41 +0100 | [diff] [blame] | 357 | /* Unable to use the buffer for scanout */ |
| 358 | if (!bo) |
| 359 | return NULL; |
| 360 | |
Ander Conselvan de Oliveira | e920941 | 2012-11-30 17:34:22 +0200 | [diff] [blame^] | 361 | if (!drm_output_check_scanout_format(output, es, bo)) { |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 362 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 363 | return NULL; |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 364 | } |
| 365 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 366 | output->next = drm_fb_get_from_bo(bo, c); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 367 | if (!output->next) { |
| 368 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 369 | return NULL; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 370 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 371 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 372 | drm_fb_set_buffer(output->next, es->buffer); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 373 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 374 | return &output->fb_plane; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 377 | static void |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 378 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 379 | { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 380 | struct drm_compositor *c = |
| 381 | (struct drm_compositor *) output->base.compositor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 382 | struct gbm_bo *bo; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 383 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 384 | c->base.renderer->repaint_output(&output->base, damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 385 | |
Ander Conselvan de Oliveira | 0a88772 | 2012-11-22 15:57:00 +0200 | [diff] [blame] | 386 | pixman_region32_subtract(&c->base.primary_plane.damage, |
| 387 | &c->base.primary_plane.damage, damage); |
| 388 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 389 | bo = gbm_surface_lock_front_buffer(output->surface); |
| 390 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 391 | weston_log("failed to lock front buffer: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 392 | return; |
| 393 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 394 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 395 | output->next = drm_fb_get_from_bo(bo, c); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 396 | if (!output->next) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 397 | weston_log("failed to get drm_fb for bo\n"); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 398 | gbm_surface_release_buffer(output->surface, bo); |
| 399 | return; |
| 400 | } |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 404 | drm_output_repaint(struct weston_output *output_base, |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 405 | pixman_region32_t *damage) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 406 | { |
| 407 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 408 | struct drm_compositor *compositor = |
| 409 | (struct drm_compositor *) output->base.compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 410 | struct drm_sprite *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 411 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 412 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 413 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 414 | if (!output->next) |
Kristian Høgsberg | d7c1726 | 2012-09-05 21:54:15 -0400 | [diff] [blame] | 415 | drm_output_render(output, damage); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 416 | if (!output->next) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 417 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 418 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 419 | mode = container_of(output->base.current, struct drm_mode, base); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 420 | if (!output->current) { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 421 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 422 | output->next->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 423 | &output->connector_id, 1, |
| 424 | &mode->mode_info); |
| 425 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 426 | weston_log("set mode failed: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 427 | return; |
| 428 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 431 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 432 | output->next->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 433 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 434 | weston_log("queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 435 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 436 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 437 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 438 | output->page_flip_pending = 1; |
| 439 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 440 | drm_output_set_cursor(output); |
| 441 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 442 | /* |
| 443 | * Now, update all the sprite surfaces |
| 444 | */ |
| 445 | wl_list_for_each(s, &compositor->sprite_list, link) { |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 446 | uint32_t flags = 0, fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 447 | drmVBlank vbl = { |
| 448 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 449 | .request.sequence = 1, |
| 450 | }; |
| 451 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 452 | if ((!s->current && !s->next) || |
Ander Conselvan de Oliveira | 2f7a30b | 2012-11-09 14:19:03 +0200 | [diff] [blame] | 453 | !drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 454 | continue; |
| 455 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 456 | if (s->next && !compositor->sprites_hidden) |
| 457 | fb_id = s->next->fb_id; |
| 458 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 459 | ret = drmModeSetPlane(compositor->drm.fd, s->plane_id, |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 460 | output->crtc_id, fb_id, flags, |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 461 | s->dest_x, s->dest_y, |
| 462 | s->dest_w, s->dest_h, |
| 463 | s->src_x, s->src_y, |
| 464 | s->src_w, s->src_h); |
| 465 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 466 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 467 | ret, strerror(errno)); |
| 468 | |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 469 | if (output->pipe > 0) |
| 470 | vbl.request.type |= DRM_VBLANK_SECONDARY; |
| 471 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 472 | /* |
| 473 | * Queue a vblank signal so we know when the surface |
| 474 | * becomes active on the display or has been replaced. |
| 475 | */ |
| 476 | vbl.request.signal = (unsigned long)s; |
| 477 | ret = drmWaitVBlank(compositor->drm.fd, &vbl); |
| 478 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 479 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 480 | ret, strerror(errno)); |
| 481 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 482 | |
| 483 | s->output = output; |
| 484 | output->vblank_pending = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 487 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 491 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 492 | void *data) |
| 493 | { |
| 494 | struct drm_sprite *s = (struct drm_sprite *)data; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 495 | struct drm_output *output = s->output; |
| 496 | uint32_t msecs; |
| 497 | |
| 498 | output->vblank_pending = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 499 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 500 | if (s->current) |
| 501 | gbm_bo_destroy(s->current->bo); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 502 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 503 | s->current = s->next; |
| 504 | s->next = NULL; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 505 | |
| 506 | if (!output->page_flip_pending) { |
| 507 | msecs = sec * 1000 + usec / 1000; |
| 508 | weston_output_finish_frame(&output->base, msecs); |
| 509 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 513 | page_flip_handler(int fd, unsigned int frame, |
| 514 | unsigned int sec, unsigned int usec, void *data) |
| 515 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 516 | struct drm_output *output = (struct drm_output *) data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 517 | uint32_t msecs; |
| 518 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 519 | output->page_flip_pending = 0; |
| 520 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 521 | if (output->current) { |
| 522 | if (output->current->is_client_buffer) |
| 523 | gbm_bo_destroy(output->current->bo); |
| 524 | else |
| 525 | gbm_surface_release_buffer(output->surface, |
| 526 | output->current->bo); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 529 | output->current = output->next; |
| 530 | output->next = NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 531 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 532 | if (!output->vblank_pending) { |
| 533 | msecs = sec * 1000 + usec / 1000; |
| 534 | weston_output_finish_frame(&output->base, msecs); |
| 535 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 539 | drm_surface_format_supported(struct drm_sprite *s, uint32_t format) |
| 540 | { |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 541 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 542 | |
| 543 | for (i = 0; i < s->count_formats; i++) |
| 544 | if (s->formats[i] == format) |
| 545 | return 1; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | drm_surface_transform_supported(struct weston_surface *es) |
| 552 | { |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 553 | struct weston_matrix *matrix = &es->transform.matrix; |
| 554 | int i; |
| 555 | |
| 556 | if (!es->transform.enabled) |
| 557 | return 1; |
| 558 | |
| 559 | for (i = 0; i < 16; i++) { |
| 560 | switch (i) { |
| 561 | case 10: |
| 562 | case 15: |
| 563 | if (matrix->d[i] != 1.0) |
| 564 | return 0; |
| 565 | break; |
| 566 | case 0: |
| 567 | case 5: |
| 568 | case 12: |
| 569 | case 13: |
| 570 | break; |
| 571 | default: |
| 572 | if (matrix->d[i] != 0.0) |
| 573 | return 0; |
| 574 | break; |
| 575 | } |
| 576 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 577 | |
| 578 | return 1; |
| 579 | } |
| 580 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 581 | static struct weston_plane * |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 582 | drm_output_prepare_overlay_surface(struct weston_output *output_base, |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 583 | struct weston_surface *es) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 584 | { |
| 585 | struct weston_compositor *ec = output_base->compositor; |
| 586 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 587 | struct drm_sprite *s; |
| 588 | int found = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 589 | struct gbm_bo *bo; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 590 | pixman_region32_t dest_rect, src_rect; |
| 591 | pixman_box32_t *box; |
| 592 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 593 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 594 | |
Ander Conselvan de Oliveira | 2908a3d | 2012-11-27 17:03:43 +0200 | [diff] [blame] | 595 | if (output_base->transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 596 | return NULL; |
| 597 | |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 598 | if (c->sprites_are_broken) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 599 | return NULL; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 600 | |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 601 | if (es->output_mask != (1u << output_base->id)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 602 | return NULL; |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 603 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 604 | if (es->buffer == NULL) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 605 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 606 | |
Ville Syrjälä | 5a84f31 | 2012-11-16 11:48:46 +0200 | [diff] [blame] | 607 | if (es->alpha != 1.0f) |
| 608 | return NULL; |
| 609 | |
Rob Clark | 702ffae | 2012-08-09 14:18:27 -0500 | [diff] [blame] | 610 | if (wl_buffer_is_shm(es->buffer)) |
| 611 | return NULL; |
| 612 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 613 | if (!drm_surface_transform_supported(es)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 614 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 615 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 616 | wl_list_for_each(s, &c->sprite_list, link) { |
| 617 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 618 | continue; |
| 619 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 620 | if (!s->next) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 621 | found = 1; |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* No sprites available */ |
| 627 | if (!found) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 628 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 629 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 630 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 631 | es->buffer, GBM_BO_USE_SCANOUT); |
| 632 | if (!bo) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 633 | return NULL; |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 634 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 635 | format = gbm_bo_get_format(bo); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 636 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 637 | if (!drm_surface_format_supported(s, format)) { |
| 638 | gbm_bo_destroy(bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 639 | return NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 640 | } |
| 641 | |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 642 | s->next = drm_fb_get_from_bo(bo, c); |
| 643 | if (!s->next) { |
| 644 | gbm_bo_destroy(bo); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
| 648 | drm_fb_set_buffer(s->next, es->buffer); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 649 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 650 | box = pixman_region32_extents(&es->transform.boundingbox); |
| 651 | s->plane.x = box->x1; |
| 652 | s->plane.y = box->y1; |
| 653 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 654 | /* |
| 655 | * Calculate the source & dest rects properly based on actual |
Martin Olsson | 3b132e3 | 2012-09-29 15:13:56 +0200 | [diff] [blame] | 656 | * position (note the caller has called weston_surface_update_transform() |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 657 | * for us already). |
| 658 | */ |
| 659 | pixman_region32_init(&dest_rect); |
| 660 | pixman_region32_intersect(&dest_rect, &es->transform.boundingbox, |
| 661 | &output_base->region); |
| 662 | pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y); |
| 663 | box = pixman_region32_extents(&dest_rect); |
| 664 | s->dest_x = box->x1; |
| 665 | s->dest_y = box->y1; |
| 666 | s->dest_w = box->x2 - box->x1; |
| 667 | s->dest_h = box->y2 - box->y1; |
| 668 | pixman_region32_fini(&dest_rect); |
| 669 | |
| 670 | pixman_region32_init(&src_rect); |
| 671 | pixman_region32_intersect(&src_rect, &es->transform.boundingbox, |
| 672 | &output_base->region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 673 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 674 | |
| 675 | weston_surface_from_global_fixed(es, |
| 676 | wl_fixed_from_int(box->x1), |
| 677 | wl_fixed_from_int(box->y1), |
| 678 | &sx1, &sy1); |
| 679 | weston_surface_from_global_fixed(es, |
| 680 | wl_fixed_from_int(box->x2), |
| 681 | wl_fixed_from_int(box->y2), |
| 682 | &sx2, &sy2); |
| 683 | |
| 684 | if (sx1 < 0) |
| 685 | sx1 = 0; |
| 686 | if (sy1 < 0) |
| 687 | sy1 = 0; |
| 688 | if (sx2 > wl_fixed_from_int(es->geometry.width)) |
| 689 | sx2 = wl_fixed_from_int(es->geometry.width); |
| 690 | if (sy2 > wl_fixed_from_int(es->geometry.height)) |
| 691 | sy2 = wl_fixed_from_int(es->geometry.height); |
| 692 | |
| 693 | s->src_x = sx1 << 8; |
| 694 | s->src_y = sy1 << 8; |
| 695 | s->src_w = (sx2 - sx1) << 8; |
| 696 | s->src_h = (sy2 - sy1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 697 | pixman_region32_fini(&src_rect); |
| 698 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 699 | return &s->plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 700 | } |
| 701 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 702 | static struct weston_plane * |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 703 | drm_output_prepare_cursor_surface(struct weston_output *output_base, |
| 704 | struct weston_surface *es) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 705 | { |
Kristian Høgsberg | 8a01580 | 2012-08-09 17:19:23 -0400 | [diff] [blame] | 706 | struct drm_compositor *c = |
| 707 | (struct drm_compositor *) output_base->compositor; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 708 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 709 | |
Ander Conselvan de Oliveira | 2908a3d | 2012-11-27 17:03:43 +0200 | [diff] [blame] | 710 | if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL) |
| 711 | return NULL; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 712 | if (output->cursor_surface) |
| 713 | return NULL; |
| 714 | if (es->output_mask != (1u << output_base->id)) |
| 715 | return NULL; |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 716 | if (c->cursors_are_broken) |
Kristian Høgsberg | 8a01580 | 2012-08-09 17:19:23 -0400 | [diff] [blame] | 717 | return NULL; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 718 | if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) || |
| 719 | es->geometry.width > 64 || es->geometry.height > 64) |
| 720 | return NULL; |
| 721 | |
| 722 | output->cursor_surface = es; |
| 723 | |
| 724 | return &output->cursor_plane; |
| 725 | } |
| 726 | |
| 727 | static void |
| 728 | drm_output_set_cursor(struct drm_output *output) |
| 729 | { |
| 730 | struct weston_surface *es = output->cursor_surface; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 731 | struct drm_compositor *c = |
| 732 | (struct drm_compositor *) output->base.compositor; |
| 733 | EGLint handle, stride; |
| 734 | struct gbm_bo *bo; |
| 735 | uint32_t buf[64 * 64]; |
| 736 | unsigned char *s; |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 737 | int i, x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 738 | |
Kristian Høgsberg | 79af73e | 2012-08-03 15:45:23 -0400 | [diff] [blame] | 739 | output->cursor_surface = NULL; |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 740 | if (es == NULL) { |
| 741 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 742 | return; |
| 743 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 744 | |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 745 | if (es->buffer && pixman_region32_not_empty(&output->cursor_plane.damage)) { |
| 746 | pixman_region32_fini(&output->cursor_plane.damage); |
| 747 | pixman_region32_init(&output->cursor_plane.damage); |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 748 | output->current_cursor ^= 1; |
| 749 | bo = output->cursor_bo[output->current_cursor]; |
| 750 | memset(buf, 0, sizeof buf); |
| 751 | stride = wl_shm_buffer_get_stride(es->buffer); |
| 752 | s = wl_shm_buffer_get_data(es->buffer); |
| 753 | for (i = 0; i < es->geometry.height; i++) |
| 754 | memcpy(buf + i * 64, s + i * stride, |
| 755 | es->geometry.width * 4); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 756 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 757 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 758 | weston_log("failed update cursor: %m\n"); |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 759 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 760 | handle = gbm_bo_get_handle(bo).s32; |
| 761 | if (drmModeSetCursor(c->drm.fd, |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 762 | output->crtc_id, handle, 64, 64)) { |
Pekka Paalanen | ae29da2 | 2012-08-06 14:57:05 +0300 | [diff] [blame] | 763 | weston_log("failed to set cursor: %m\n"); |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 764 | c->cursors_are_broken = 1; |
| 765 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 768 | x = es->geometry.x - output->base.x; |
| 769 | y = es->geometry.y - output->base.y; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 770 | if (output->cursor_plane.x != x || output->cursor_plane.y != y) { |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 771 | if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) { |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 772 | weston_log("failed to move cursor: %m\n"); |
Rob Clark | ab5b1e3 | 2012-08-09 13:24:45 -0500 | [diff] [blame] | 773 | c->cursors_are_broken = 1; |
| 774 | } |
| 775 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 776 | output->cursor_plane.x = x; |
| 777 | output->cursor_plane.y = y; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 778 | } |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 781 | static void |
| 782 | drm_assign_planes(struct weston_output *output) |
| 783 | { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 784 | struct drm_compositor *c = |
| 785 | (struct drm_compositor *) output->compositor; |
Ander Conselvan de Oliveira | 4bcf3a5 | 2012-10-31 17:55:45 +0200 | [diff] [blame] | 786 | struct drm_output *drm_output = (struct drm_output *) output; |
| 787 | struct drm_sprite *s; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 788 | struct weston_surface *es, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 789 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 790 | struct weston_plane *primary, *next_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 791 | |
Ander Conselvan de Oliveira | 4bcf3a5 | 2012-10-31 17:55:45 +0200 | [diff] [blame] | 792 | /* Reset the opaque region of the planes */ |
| 793 | pixman_region32_fini(&drm_output->cursor_plane.opaque); |
| 794 | pixman_region32_init(&drm_output->cursor_plane.opaque); |
| 795 | pixman_region32_fini(&drm_output->fb_plane.opaque); |
| 796 | pixman_region32_init(&drm_output->fb_plane.opaque); |
| 797 | |
| 798 | wl_list_for_each (s, &c->sprite_list, link) { |
| 799 | if (!drm_sprite_crtc_supported(output, s->possible_crtcs)) |
| 800 | continue; |
| 801 | |
| 802 | pixman_region32_fini(&s->plane.opaque); |
| 803 | pixman_region32_init(&s->plane.opaque); |
| 804 | } |
| 805 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 806 | /* |
| 807 | * Find a surface for each sprite in the output using some heuristics: |
| 808 | * 1) size |
| 809 | * 2) frequency of update |
| 810 | * 3) opacity (though some hw might support alpha blending) |
| 811 | * 4) clipping (this can be fixed with color keys) |
| 812 | * |
| 813 | * The idea is to save on blitting since this should save power. |
| 814 | * If we can get a large video surface on the sprite for example, |
| 815 | * the main display surface may not need to update at all, and |
| 816 | * the client buffer can be used directly for the sprite surface |
| 817 | * as we do for flipping full screen surfaces. |
| 818 | */ |
| 819 | pixman_region32_init(&overlap); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 820 | primary = &c->base.primary_plane; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 821 | wl_list_for_each_safe(es, next, &c->base.surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 822 | pixman_region32_init(&surface_overlap); |
| 823 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 824 | &es->transform.boundingbox); |
| 825 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 826 | next_plane = NULL; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 827 | if (pixman_region32_not_empty(&surface_overlap)) |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 828 | next_plane = primary; |
| 829 | if (next_plane == NULL) |
Kristian Høgsberg | 5626d34 | 2012-08-03 11:50:05 -0400 | [diff] [blame] | 830 | next_plane = drm_output_prepare_cursor_surface(output, es); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 831 | if (next_plane == NULL) |
| 832 | next_plane = drm_output_prepare_scanout_surface(output, es); |
| 833 | if (next_plane == NULL) |
| 834 | next_plane = drm_output_prepare_overlay_surface(output, es); |
| 835 | if (next_plane == NULL) |
| 836 | next_plane = primary; |
| 837 | weston_surface_move_to_plane(es, next_plane); |
| 838 | if (next_plane == primary) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 839 | pixman_region32_union(&overlap, &overlap, |
| 840 | &es->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 841 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 842 | pixman_region32_fini(&surface_overlap); |
| 843 | } |
| 844 | pixman_region32_fini(&overlap); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 845 | } |
| 846 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 847 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 848 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 849 | { |
| 850 | struct drm_output *output = (struct drm_output *) output_base; |
| 851 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 852 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 853 | drmModeCrtcPtr origcrtc = output->original_crtc; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 854 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 855 | if (output->backlight) |
| 856 | backlight_destroy(output->backlight); |
| 857 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 858 | /* Turn off hardware cursor */ |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 859 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 860 | |
| 861 | /* Restore original CRTC state */ |
| 862 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 863 | origcrtc->x, origcrtc->y, |
| 864 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 865 | drmModeFreeCrtc(origcrtc); |
| 866 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 867 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 868 | c->connector_allocator &= ~(1 << output->connector_id); |
| 869 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 870 | gl_renderer_output_destroy(output_base); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 871 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 872 | gbm_surface_destroy(output->surface); |
| 873 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 874 | weston_plane_release(&output->fb_plane); |
| 875 | weston_plane_release(&output->cursor_plane); |
| 876 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 877 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 878 | wl_list_remove(&output->base.link); |
| 879 | |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 880 | free(output->name); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 881 | free(output); |
| 882 | } |
| 883 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 884 | static struct drm_mode * |
| 885 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 886 | { |
| 887 | struct drm_mode *tmp_mode = NULL, *mode; |
| 888 | |
| 889 | if (output->base.current->width == target_mode->width && |
| 890 | output->base.current->height == target_mode->height && |
| 891 | (output->base.current->refresh == target_mode->refresh || |
| 892 | target_mode->refresh == 0)) |
| 893 | return (struct drm_mode *)output->base.current; |
| 894 | |
| 895 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 896 | if (mode->mode_info.hdisplay == target_mode->width && |
| 897 | mode->mode_info.vdisplay == target_mode->height) { |
| 898 | if (mode->mode_info.vrefresh == target_mode->refresh || |
| 899 | target_mode->refresh == 0) { |
| 900 | return mode; |
| 901 | } else if (!tmp_mode) |
| 902 | tmp_mode = mode; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | return tmp_mode; |
| 907 | } |
| 908 | |
| 909 | static int |
| 910 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 911 | { |
| 912 | struct drm_output *output; |
| 913 | struct drm_mode *drm_mode; |
| 914 | int ret; |
| 915 | struct drm_compositor *ec; |
| 916 | struct gbm_surface *surface; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 917 | |
| 918 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 919 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 920 | return -1; |
| 921 | } |
| 922 | |
| 923 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 924 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 925 | return -1; |
| 926 | } |
| 927 | |
| 928 | ec = (struct drm_compositor *)output_base->compositor; |
| 929 | output = (struct drm_output *)output_base; |
| 930 | drm_mode = choose_mode (output, mode); |
| 931 | |
| 932 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 933 | 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] | 934 | return -1; |
| 935 | } else if (&drm_mode->base == output->base.current) { |
| 936 | return 0; |
| 937 | } else if (drm_mode->base.width == output->base.current->width && |
| 938 | drm_mode->base.height == output->base.current->height) { |
| 939 | /* only change refresh value */ |
| 940 | ret = drmModeSetCrtc(ec->drm.fd, |
| 941 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 942 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 943 | &output->connector_id, 1, &drm_mode->mode_info); |
| 944 | |
| 945 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 946 | weston_log("failed to set mode (%dx%d) %u Hz\n", |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 947 | drm_mode->base.width, |
| 948 | drm_mode->base.height, |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 949 | drm_mode->base.refresh / 1000); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 950 | ret = -1; |
| 951 | } else { |
| 952 | output->base.current->flags = 0; |
| 953 | output->base.current = &drm_mode->base; |
| 954 | drm_mode->base.flags = |
| 955 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 956 | ret = 0; |
| 957 | } |
| 958 | |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | drm_mode->base.flags = |
| 963 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 964 | |
| 965 | surface = gbm_surface_create(ec->gbm, |
| 966 | drm_mode->base.width, |
| 967 | drm_mode->base.height, |
| 968 | GBM_FORMAT_XRGB8888, |
| 969 | GBM_BO_USE_SCANOUT | |
| 970 | GBM_BO_USE_RENDERING); |
| 971 | if (!surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 972 | weston_log("failed to create gbm surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 973 | return -1; |
| 974 | } |
| 975 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 976 | gl_renderer_output_destroy(&output->base); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 977 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 978 | if (!gl_renderer_output_create(&output->base, surface)) { |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 979 | weston_log("failed to create renderer output\n"); |
| 980 | goto err_gbm; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | ret = drmModeSetCrtc(ec->drm.fd, |
| 984 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 985 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 986 | &output->connector_id, 1, &drm_mode->mode_info); |
| 987 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 988 | weston_log("failed to set mode\n"); |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 989 | goto err_gl; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 993 | if (output->current) { |
| 994 | if (output->current->is_client_buffer) |
| 995 | gbm_bo_destroy(output->current->bo); |
| 996 | else |
| 997 | gbm_surface_release_buffer(output->surface, |
| 998 | output->current->bo); |
| 999 | } |
| 1000 | output->current = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1001 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1002 | if (output->next) { |
| 1003 | if (output->next->is_client_buffer) |
| 1004 | gbm_bo_destroy(output->next->bo); |
| 1005 | else |
| 1006 | gbm_surface_release_buffer(output->surface, |
| 1007 | output->next->bo); |
| 1008 | } |
| 1009 | output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1010 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1011 | gbm_surface_destroy(output->surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1012 | output->surface = surface; |
| 1013 | |
| 1014 | /*update output*/ |
| 1015 | output->base.current = &drm_mode->base; |
| 1016 | output->base.dirty = 1; |
| 1017 | weston_output_move(&output->base, output->base.x, output->base.y); |
| 1018 | return 0; |
| 1019 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1020 | err_gl: |
| 1021 | gl_renderer_output_destroy(&output->base); |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 1022 | err_gbm: |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1023 | gbm_surface_destroy(surface); |
| 1024 | return -1; |
| 1025 | } |
| 1026 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1027 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1028 | on_drm_input(int fd, uint32_t mask, void *data) |
| 1029 | { |
| 1030 | drmEventContext evctx; |
| 1031 | |
| 1032 | memset(&evctx, 0, sizeof evctx); |
| 1033 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 1034 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1035 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1036 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1037 | |
| 1038 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1042 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1043 | { |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1044 | const char *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1045 | int fd; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1046 | |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1047 | sysnum = udev_device_get_sysnum(device); |
| 1048 | if (sysnum) |
| 1049 | ec->drm.id = atoi(sysnum); |
| 1050 | if (!sysnum || ec->drm.id < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1051 | weston_log("cannot get device sysnum\n"); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
| 1054 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1055 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 1056 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1057 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1058 | /* Probably permissions error */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1059 | weston_log("couldn't open %s, skipping\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1060 | udev_device_get_devnode(device)); |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1064 | weston_log("using %s\n", filename); |
| 1065 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1066 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1067 | ec->gbm = gbm_create_device(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1068 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1069 | if (gl_renderer_create(&ec->base, ec->gbm, gl_renderer_opaque_attribs, |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 1070 | NULL) < 0) { |
| 1071 | gbm_device_destroy(ec->gbm); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1072 | return -1; |
| 1073 | } |
| 1074 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1078 | static struct drm_mode * |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1079 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 1080 | { |
| 1081 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1082 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1083 | |
| 1084 | mode = malloc(sizeof *mode); |
| 1085 | if (mode == NULL) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1086 | return NULL; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1087 | |
| 1088 | mode->base.flags = 0; |
| 1089 | mode->base.width = info->hdisplay; |
| 1090 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1091 | |
| 1092 | /* Calculate higher precision (mHz) refresh rate */ |
| 1093 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1094 | info->vtotal / 2) / info->vtotal; |
| 1095 | |
| 1096 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1097 | refresh *= 2; |
| 1098 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1099 | refresh /= 2; |
| 1100 | if (info->vscan > 1) |
| 1101 | refresh /= info->vscan; |
| 1102 | |
| 1103 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1104 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1105 | |
| 1106 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1107 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1108 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1109 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1110 | |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1111 | return mode; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static int |
| 1115 | drm_subpixel_to_wayland(int drm_value) |
| 1116 | { |
| 1117 | switch (drm_value) { |
| 1118 | default: |
| 1119 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1120 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1121 | case DRM_MODE_SUBPIXEL_NONE: |
| 1122 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1123 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1124 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1125 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1126 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1127 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1128 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1129 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1130 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1131 | } |
| 1132 | } |
| 1133 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1134 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1135 | static uint32_t |
| 1136 | drm_get_backlight(struct drm_output *output) |
| 1137 | { |
| 1138 | long brightness, max_brightness, norm; |
| 1139 | |
| 1140 | brightness = backlight_get_brightness(output->backlight); |
| 1141 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1142 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1143 | /* convert it on a scale of 0 to 255 */ |
| 1144 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1145 | |
| 1146 | return (uint32_t) norm; |
| 1147 | } |
| 1148 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1149 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1150 | static void |
| 1151 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1152 | { |
| 1153 | struct drm_output *output = (struct drm_output *) output_base; |
| 1154 | long max_brightness, new_brightness; |
| 1155 | |
| 1156 | if (!output->backlight) |
| 1157 | return; |
| 1158 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1159 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1160 | return; |
| 1161 | |
| 1162 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1163 | |
| 1164 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1165 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1166 | |
| 1167 | backlight_set_brightness(output->backlight, new_brightness); |
| 1168 | } |
| 1169 | |
| 1170 | static drmModePropertyPtr |
| 1171 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1172 | { |
| 1173 | drmModePropertyPtr props; |
| 1174 | int i; |
| 1175 | |
| 1176 | for (i = 0; i < connector->count_props; i++) { |
| 1177 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1178 | if (!props) |
| 1179 | continue; |
| 1180 | |
| 1181 | if (!strcmp(props->name, name)) |
| 1182 | return props; |
| 1183 | |
| 1184 | drmModeFreeProperty(props); |
| 1185 | } |
| 1186 | |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | static void |
| 1191 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1192 | { |
| 1193 | struct drm_output *output = (struct drm_output *) output_base; |
| 1194 | struct weston_compositor *ec = output_base->compositor; |
| 1195 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 1196 | drmModeConnectorPtr connector; |
| 1197 | drmModePropertyPtr prop; |
| 1198 | |
| 1199 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 1200 | if (!connector) |
| 1201 | return; |
| 1202 | |
| 1203 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 1204 | if (!prop) { |
| 1205 | drmModeFreeConnector(connector); |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1210 | prop->prop_id, level); |
| 1211 | drmModeFreeProperty(prop); |
| 1212 | drmModeFreeConnector(connector); |
| 1213 | } |
| 1214 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1215 | static const char *connector_type_names[] = { |
| 1216 | "None", |
| 1217 | "VGA", |
| 1218 | "DVI", |
| 1219 | "DVI", |
| 1220 | "DVI", |
| 1221 | "Composite", |
| 1222 | "TV", |
| 1223 | "LVDS", |
| 1224 | "CTV", |
| 1225 | "DIN", |
| 1226 | "DP", |
| 1227 | "HDMI", |
| 1228 | "HDMI", |
| 1229 | "TV", |
| 1230 | "eDP", |
| 1231 | }; |
| 1232 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1233 | static int |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1234 | find_crtc_for_connector(struct drm_compositor *ec, |
| 1235 | drmModeRes *resources, drmModeConnector *connector) |
| 1236 | { |
| 1237 | drmModeEncoder *encoder; |
| 1238 | uint32_t possible_crtcs; |
| 1239 | int i, j; |
| 1240 | |
| 1241 | for (j = 0; j < connector->count_encoders; j++) { |
| 1242 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]); |
| 1243 | if (encoder == NULL) { |
| 1244 | weston_log("Failed to get encoder.\n"); |
| 1245 | return -1; |
| 1246 | } |
| 1247 | possible_crtcs = encoder->possible_crtcs; |
| 1248 | drmModeFreeEncoder(encoder); |
| 1249 | |
| 1250 | for (i = 0; i < resources->count_crtcs; i++) { |
| 1251 | if (possible_crtcs & (1 << i) && |
| 1252 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
| 1253 | return i; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return -1; |
| 1258 | } |
| 1259 | |
| 1260 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1261 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1262 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1263 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1264 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1265 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1266 | struct drm_output *output; |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1267 | struct drm_mode *drm_mode, *next, *preferred, *current, *configured; |
| 1268 | struct weston_mode *m; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1269 | struct drm_configured_output *o = NULL, *temp; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1270 | drmModeEncoder *encoder; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1271 | drmModeModeInfo crtc_mode; |
| 1272 | drmModeCrtc *crtc; |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1273 | int i; |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1274 | char name[32]; |
| 1275 | const char *type_name; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1276 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1277 | i = find_crtc_for_connector(ec, resources, connector); |
| 1278 | if (i < 0) { |
| 1279 | weston_log("No usable crtc/encoder pair for connector.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1280 | return -1; |
| 1281 | } |
| 1282 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1283 | output = malloc(sizeof *output); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1284 | if (output == NULL) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1285 | return -1; |
| 1286 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1287 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1288 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1289 | output->base.make = "unknown"; |
| 1290 | output->base.model = "unknown"; |
| 1291 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1292 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1293 | if (connector->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1294 | type_name = connector_type_names[connector->connector_type]; |
| 1295 | else |
| 1296 | type_name = "UNKNOWN"; |
| 1297 | snprintf(name, 32, "%s%d", type_name, connector->connector_type_id); |
| 1298 | output->name = strdup(name); |
| 1299 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1300 | output->crtc_id = resources->crtcs[i]; |
Rob Clark | 5ca1a47 | 2012-08-08 20:27:37 -0500 | [diff] [blame] | 1301 | output->pipe = i; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1302 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1303 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1304 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1305 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1306 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
| 1307 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1308 | /* Get the current mode on the crtc that's currently driving |
| 1309 | * this connector. */ |
| 1310 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id); |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1311 | memset(&crtc_mode, 0, sizeof crtc_mode); |
| 1312 | if (encoder != NULL) { |
| 1313 | crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id); |
| 1314 | drmModeFreeEncoder(encoder); |
| 1315 | if (crtc == NULL) |
| 1316 | goto err_free; |
| 1317 | if (crtc->mode_valid) |
| 1318 | crtc_mode = crtc->mode; |
| 1319 | drmModeFreeCrtc(crtc); |
| 1320 | } |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1321 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1322 | for (i = 0; i < connector->count_modes; i++) { |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1323 | drm_mode = drm_output_add_mode(output, &connector->modes[i]); |
| 1324 | if (!drm_mode) |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1325 | goto err_free; |
| 1326 | } |
| 1327 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1328 | preferred = NULL; |
| 1329 | current = NULL; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1330 | configured = NULL; |
| 1331 | |
| 1332 | wl_list_for_each(temp, &configured_output_list, link) { |
| 1333 | if (strcmp(temp->name, output->name) == 0) { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1334 | if (temp->mode) |
| 1335 | weston_log("%s mode \"%s\" in config\n", |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1336 | temp->name, temp->mode); |
| 1337 | o = temp; |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Ander Conselvan de Oliveira | dc79e6d | 2012-08-09 16:45:01 +0300 | [diff] [blame] | 1342 | if (o && o->config == OUTPUT_CONFIG_OFF) { |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1343 | weston_log("Disabling output %s\n", o->name); |
| 1344 | |
| 1345 | drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
| 1346 | 0, 0, 0, 0, 0, NULL); |
| 1347 | goto err_free; |
| 1348 | } |
| 1349 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1350 | wl_list_for_each(drm_mode, &output->base.mode_list, base.link) { |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1351 | if (o && o->config == OUTPUT_CONFIG_MODE && |
| 1352 | o->width == drm_mode->base.width && |
| 1353 | o->height == drm_mode->base.height) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1354 | configured = drm_mode; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1355 | 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] | 1356 | current = drm_mode; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1357 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1358 | preferred = drm_mode; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1359 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1360 | |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 1361 | if (o && o->config == OUTPUT_CONFIG_MODELINE) { |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1362 | configured = drm_output_add_mode(output, &o->crtc_mode); |
| 1363 | if (!configured) |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 1364 | goto err_free; |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 1365 | current = configured; |
| 1366 | } |
| 1367 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1368 | if (current == NULL && crtc_mode.clock != 0) { |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1369 | current = drm_output_add_mode(output, &crtc_mode); |
| 1370 | if (!current) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1371 | goto err_free; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1372 | } |
| 1373 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1374 | if (o && o->config == OUTPUT_CONFIG_CURRENT) |
| 1375 | configured = current; |
| 1376 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1377 | if (option_current_mode && current) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1378 | output->base.current = ¤t->base; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1379 | else if (configured) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1380 | output->base.current = &configured->base; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1381 | else if (preferred) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1382 | output->base.current = &preferred->base; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1383 | else if (current) |
Ander Conselvan de Oliveira | 42c4646 | 2012-08-09 16:45:00 +0300 | [diff] [blame] | 1384 | output->base.current = ¤t->base; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1385 | |
| 1386 | if (output->base.current == NULL) { |
| 1387 | weston_log("no available modes for %s\n", output->name); |
| 1388 | goto err_free; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1389 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1390 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame] | 1391 | output->base.current->flags |= WL_OUTPUT_MODE_CURRENT; |
| 1392 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1393 | output->surface = gbm_surface_create(ec->gbm, |
| 1394 | output->base.current->width, |
| 1395 | output->base.current->height, |
| 1396 | GBM_FORMAT_XRGB8888, |
| 1397 | GBM_BO_USE_SCANOUT | |
| 1398 | GBM_BO_USE_RENDERING); |
| 1399 | if (!output->surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1400 | weston_log("failed to create gbm surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1401 | goto err_free; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1402 | } |
| 1403 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 1404 | weston_output_init(&output->base, &ec->base, x, y, |
| 1405 | connector->mmWidth, connector->mmHeight, |
| 1406 | o ? o->transform : WL_OUTPUT_TRANSFORM_NORMAL); |
| 1407 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1408 | if (gl_renderer_output_create(&output->base, output->surface) < 0) |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 1409 | goto err_output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1410 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1411 | output->cursor_bo[0] = |
| 1412 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1413 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1414 | output->cursor_bo[1] = |
| 1415 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1416 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
Kristian Høgsberg | 1d1e0a5 | 2012-10-21 13:29:26 -0400 | [diff] [blame] | 1417 | if (output->cursor_bo[0] == NULL || output->cursor_bo[1] == NULL) { |
| 1418 | weston_log("cursor buffers unavailable, using gl cursors\n"); |
| 1419 | ec->cursors_are_broken = 1; |
| 1420 | } |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1421 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1422 | output->backlight = backlight_init(drm_device, |
| 1423 | connector->connector_type); |
| 1424 | if (output->backlight) { |
| 1425 | output->base.set_backlight = drm_set_backlight; |
| 1426 | output->base.backlight_current = drm_get_backlight(output); |
| 1427 | } |
| 1428 | |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1429 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1430 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 1431 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1432 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1433 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1434 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1435 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1436 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1437 | |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1438 | weston_plane_init(&output->cursor_plane, 0, 0); |
| 1439 | weston_plane_init(&output->fb_plane, 0, 0); |
| 1440 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1441 | weston_log("Output %s, (connector %d, crtc %d)\n", |
| 1442 | output->name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1443 | wl_list_for_each(m, &output->base.mode_list, link) |
| 1444 | weston_log_continue(" mode %dx%d@%.1f%s%s%s\n", |
| 1445 | m->width, m->height, m->refresh / 1000.0, |
| 1446 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 1447 | ", preferred" : "", |
| 1448 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 1449 | ", current" : "", |
| 1450 | connector->count_modes == 0 ? |
| 1451 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1452 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1453 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1454 | |
John Kåre Alsaker | 9465927 | 2012-11-13 19:10:18 +0100 | [diff] [blame] | 1455 | err_output: |
| 1456 | weston_output_destroy(&output->base); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1457 | gbm_surface_destroy(output->surface); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1458 | err_free: |
| 1459 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1460 | base.link) { |
| 1461 | wl_list_remove(&drm_mode->base.link); |
| 1462 | free(drm_mode); |
| 1463 | } |
| 1464 | |
| 1465 | drmModeFreeCrtc(output->original_crtc); |
| 1466 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1467 | ec->connector_allocator &= ~(1 << output->connector_id); |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 1468 | free(output->name); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1469 | free(output); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1470 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1471 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1472 | } |
| 1473 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1474 | static void |
| 1475 | create_sprites(struct drm_compositor *ec) |
| 1476 | { |
| 1477 | struct drm_sprite *sprite; |
| 1478 | drmModePlaneRes *plane_res; |
| 1479 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1480 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1481 | |
| 1482 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1483 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1484 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1485 | strerror(errno)); |
| 1486 | return; |
| 1487 | } |
| 1488 | |
| 1489 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1490 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1491 | if (!plane) |
| 1492 | continue; |
| 1493 | |
| 1494 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1495 | plane->count_formats)); |
| 1496 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1497 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1498 | __func__); |
| 1499 | free(plane); |
| 1500 | continue; |
| 1501 | } |
| 1502 | |
| 1503 | memset(sprite, 0, sizeof *sprite); |
| 1504 | |
| 1505 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1506 | sprite->plane_id = plane->plane_id; |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1507 | sprite->current = NULL; |
| 1508 | sprite->next = NULL; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1509 | sprite->compositor = ec; |
| 1510 | sprite->count_formats = plane->count_formats; |
| 1511 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 1512 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1513 | drmModeFreePlane(plane); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1514 | weston_plane_init(&sprite->plane, 0, 0); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1515 | |
| 1516 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1517 | } |
| 1518 | |
| 1519 | free(plane_res->planes); |
| 1520 | free(plane_res); |
| 1521 | } |
| 1522 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1523 | static void |
| 1524 | destroy_sprites(struct drm_compositor *compositor) |
| 1525 | { |
| 1526 | struct drm_sprite *sprite, *next; |
| 1527 | struct drm_output *output; |
| 1528 | |
| 1529 | output = container_of(compositor->base.output_list.next, |
| 1530 | struct drm_output, base.link); |
| 1531 | |
| 1532 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1533 | drmModeSetPlane(compositor->drm.fd, |
| 1534 | sprite->plane_id, |
| 1535 | output->crtc_id, 0, 0, |
| 1536 | 0, 0, 0, 0, 0, 0, 0, 0); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1537 | if (sprite->current) |
| 1538 | gbm_bo_destroy(sprite->current->bo); |
| 1539 | if (sprite->next) |
| 1540 | gbm_bo_destroy(sprite->next->bo); |
Kristian Høgsberg | 65a11e1 | 2012-08-03 11:30:18 -0400 | [diff] [blame] | 1541 | weston_plane_release(&sprite->plane); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1542 | free(sprite); |
| 1543 | } |
| 1544 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1545 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1546 | static int |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1547 | create_outputs(struct drm_compositor *ec, uint32_t option_connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1548 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1549 | { |
| 1550 | drmModeConnector *connector; |
| 1551 | drmModeRes *resources; |
| 1552 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1553 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1554 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1555 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1556 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1557 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1558 | return -1; |
| 1559 | } |
| 1560 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1561 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1562 | if (!ec->crtcs) { |
| 1563 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1564 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1565 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1566 | |
Rob Clark | 4339add | 2012-08-09 14:18:28 -0500 | [diff] [blame] | 1567 | ec->min_width = resources->min_width; |
| 1568 | ec->max_width = resources->max_width; |
| 1569 | ec->min_height = resources->min_height; |
| 1570 | ec->max_height = resources->max_height; |
| 1571 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1572 | ec->num_crtcs = resources->count_crtcs; |
| 1573 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1574 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1575 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1576 | connector = drmModeGetConnector(ec->drm.fd, |
| 1577 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1578 | if (connector == NULL) |
| 1579 | continue; |
| 1580 | |
| 1581 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1582 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1583 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1584 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1585 | connector, x, y, |
| 1586 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1587 | drmModeFreeConnector(connector); |
| 1588 | continue; |
| 1589 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1590 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1591 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1592 | struct weston_output, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1593 | link)->width; |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1594 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1595 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1596 | drmModeFreeConnector(connector); |
| 1597 | } |
| 1598 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1599 | if (wl_list_empty(&ec->base.output_list)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1600 | weston_log("No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1601 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1602 | return -1; |
| 1603 | } |
| 1604 | |
| 1605 | drmModeFreeResources(resources); |
| 1606 | |
| 1607 | return 0; |
| 1608 | } |
| 1609 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1610 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1611 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1612 | { |
| 1613 | drmModeConnector *connector; |
| 1614 | drmModeRes *resources; |
| 1615 | struct drm_output *output, *next; |
| 1616 | int x = 0, y = 0; |
| 1617 | int x_offset = 0, y_offset = 0; |
| 1618 | uint32_t connected = 0, disconnects = 0; |
| 1619 | int i; |
| 1620 | |
| 1621 | resources = drmModeGetResources(ec->drm.fd); |
| 1622 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1623 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1624 | return; |
| 1625 | } |
| 1626 | |
| 1627 | /* collect new connects */ |
| 1628 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1629 | int connector_id = resources->connectors[i]; |
| 1630 | |
| 1631 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1632 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1633 | continue; |
| 1634 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1635 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1636 | drmModeFreeConnector(connector); |
| 1637 | continue; |
| 1638 | } |
| 1639 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1640 | connected |= (1 << connector_id); |
| 1641 | |
| 1642 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1643 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1644 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1645 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1646 | |
| 1647 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1648 | if (!wl_list_empty(&ec->base.output_list)) |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1649 | x = last->x + last->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1650 | else |
| 1651 | x = 0; |
| 1652 | y = 0; |
| 1653 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1654 | connector, x, y, |
| 1655 | drm_device); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1656 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1657 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1658 | } |
| 1659 | drmModeFreeConnector(connector); |
| 1660 | } |
| 1661 | drmModeFreeResources(resources); |
| 1662 | |
| 1663 | disconnects = ec->connector_allocator & ~connected; |
| 1664 | if (disconnects) { |
| 1665 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1666 | base.link) { |
| 1667 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1668 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1669 | output->base.x - x_offset, |
| 1670 | output->base.y - y_offset); |
| 1671 | } |
| 1672 | |
| 1673 | if (disconnects & (1 << output->connector_id)) { |
| 1674 | disconnects &= ~(1 << output->connector_id); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1675 | weston_log("connector %d disconnected\n", |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1676 | output->connector_id); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 1677 | x_offset += output->base.width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1678 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* FIXME: handle zero outputs, without terminating */ |
| 1684 | if (ec->connector_allocator == 0) |
| 1685 | wl_display_terminate(ec->base.wl_display); |
| 1686 | } |
| 1687 | |
| 1688 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1689 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1690 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1691 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1692 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1693 | |
| 1694 | sysnum = udev_device_get_sysnum(device); |
| 1695 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1696 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1697 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1698 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 1699 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1700 | return 0; |
| 1701 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1702 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1705 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1706 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1707 | { |
| 1708 | struct drm_compositor *ec = data; |
| 1709 | struct udev_device *event; |
| 1710 | |
| 1711 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1712 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1713 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1714 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1715 | |
| 1716 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1717 | |
| 1718 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1719 | } |
| 1720 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1721 | static void |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1722 | drm_restore(struct weston_compositor *ec) |
| 1723 | { |
| 1724 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 1725 | |
| 1726 | if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0) |
| 1727 | weston_log("failed to drop master: %m\n"); |
| 1728 | tty_reset(d->tty); |
| 1729 | } |
| 1730 | |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1731 | static const char default_seat[] = "seat0"; |
| 1732 | |
| 1733 | static void |
| 1734 | device_added(struct udev_device *udev_device, struct drm_seat *master) |
| 1735 | { |
| 1736 | struct weston_compositor *c; |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1737 | struct evdev_device *device; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1738 | const char *devnode; |
| 1739 | const char *device_seat; |
| 1740 | int fd; |
| 1741 | |
| 1742 | device_seat = udev_device_get_property_value(udev_device, "ID_SEAT"); |
| 1743 | if (!device_seat) |
| 1744 | device_seat = default_seat; |
| 1745 | |
| 1746 | if (strcmp(device_seat, master->seat_id)) |
| 1747 | return; |
| 1748 | |
| 1749 | c = master->base.compositor; |
| 1750 | devnode = udev_device_get_devnode(udev_device); |
| 1751 | |
| 1752 | /* Use non-blocking mode so that we can loop on read on |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1753 | * evdev_device_data() until all events on the fd are |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1754 | * read. mtdev_get() also expects this. */ |
| 1755 | fd = weston_launcher_open(c, devnode, O_RDWR | O_NONBLOCK); |
| 1756 | if (fd < 0) { |
| 1757 | weston_log("opening input device '%s' failed.\n", devnode); |
| 1758 | return; |
| 1759 | } |
| 1760 | |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1761 | device = evdev_device_create(&master->base, devnode, fd); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1762 | if (!device) { |
| 1763 | close(fd); |
| 1764 | weston_log("not using input device '%s'.\n", devnode); |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | wl_list_insert(master->devices_list.prev, &device->link); |
| 1769 | } |
| 1770 | |
| 1771 | static void |
| 1772 | evdev_add_devices(struct udev *udev, struct weston_seat *seat_base) |
| 1773 | { |
| 1774 | struct drm_seat *seat = (struct drm_seat *) seat_base; |
| 1775 | struct udev_enumerate *e; |
| 1776 | struct udev_list_entry *entry; |
| 1777 | struct udev_device *device; |
| 1778 | const char *path, *sysname; |
| 1779 | |
| 1780 | e = udev_enumerate_new(udev); |
| 1781 | udev_enumerate_add_match_subsystem(e, "input"); |
| 1782 | udev_enumerate_scan_devices(e); |
| 1783 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 1784 | path = udev_list_entry_get_name(entry); |
| 1785 | device = udev_device_new_from_syspath(udev, path); |
| 1786 | |
| 1787 | sysname = udev_device_get_sysname(device); |
| 1788 | if (strncmp("event", sysname, 5) != 0) { |
| 1789 | udev_device_unref(device); |
| 1790 | continue; |
| 1791 | } |
| 1792 | |
| 1793 | device_added(device, seat); |
| 1794 | |
| 1795 | udev_device_unref(device); |
| 1796 | } |
| 1797 | udev_enumerate_unref(e); |
| 1798 | |
| 1799 | evdev_notify_keyboard_focus(&seat->base, &seat->devices_list); |
| 1800 | |
| 1801 | if (wl_list_empty(&seat->devices_list)) { |
| 1802 | weston_log( |
| 1803 | "warning: no input devices on entering Weston. " |
| 1804 | "Possible causes:\n" |
| 1805 | "\t- no permissions to read /dev/input/event*\n" |
| 1806 | "\t- seats misconfigured " |
| 1807 | "(Weston backend option 'seat', " |
| 1808 | "udev device property ID_SEAT)\n"); |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | static int |
| 1813 | evdev_udev_handler(int fd, uint32_t mask, void *data) |
| 1814 | { |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1815 | struct drm_seat *seat = data; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1816 | struct udev_device *udev_device; |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1817 | struct evdev_device *device, *next; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1818 | const char *action; |
| 1819 | const char *devnode; |
| 1820 | |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1821 | udev_device = udev_monitor_receive_device(seat->udev_monitor); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1822 | if (!udev_device) |
| 1823 | return 1; |
| 1824 | |
| 1825 | action = udev_device_get_action(udev_device); |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1826 | if (!action) |
| 1827 | goto out; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1828 | |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1829 | if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0) |
| 1830 | goto out; |
| 1831 | |
| 1832 | if (!strcmp(action, "add")) { |
| 1833 | device_added(udev_device, seat); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1834 | } |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1835 | else if (!strcmp(action, "remove")) { |
| 1836 | devnode = udev_device_get_devnode(udev_device); |
| 1837 | wl_list_for_each_safe(device, next, &seat->devices_list, link) |
| 1838 | if (!strcmp(device->devnode, devnode)) { |
Pekka Paalanen | 42b3f6a | 2012-08-03 14:39:09 +0300 | [diff] [blame] | 1839 | weston_log("input device %s, %s removed\n", |
| 1840 | device->devname, device->devnode); |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1841 | evdev_device_destroy(device); |
Pekka Paalanen | d2e69c2 | 2012-08-03 14:39:08 +0300 | [diff] [blame] | 1842 | break; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | out: |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1847 | udev_device_unref(udev_device); |
| 1848 | |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | static int |
| 1853 | evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base) |
| 1854 | { |
| 1855 | struct drm_seat *master = (struct drm_seat *) seat_base; |
| 1856 | struct wl_event_loop *loop; |
| 1857 | struct weston_compositor *c = master->base.compositor; |
| 1858 | int fd; |
| 1859 | |
| 1860 | master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev"); |
| 1861 | if (!master->udev_monitor) { |
| 1862 | weston_log("udev: failed to create the udev monitor\n"); |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor, |
| 1867 | "input", NULL); |
| 1868 | |
| 1869 | if (udev_monitor_enable_receiving(master->udev_monitor)) { |
| 1870 | weston_log("udev: failed to bind the udev monitor\n"); |
| 1871 | udev_monitor_unref(master->udev_monitor); |
| 1872 | return 0; |
| 1873 | } |
| 1874 | |
| 1875 | loop = wl_display_get_event_loop(c->wl_display); |
| 1876 | fd = udev_monitor_get_fd(master->udev_monitor); |
| 1877 | master->udev_monitor_source = |
| 1878 | wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE, |
| 1879 | evdev_udev_handler, master); |
| 1880 | if (!master->udev_monitor_source) { |
| 1881 | udev_monitor_unref(master->udev_monitor); |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | return 1; |
| 1886 | } |
| 1887 | |
| 1888 | static void |
| 1889 | evdev_disable_udev_monitor(struct weston_seat *seat_base) |
| 1890 | { |
| 1891 | struct drm_seat *seat = (struct drm_seat *) seat_base; |
| 1892 | |
| 1893 | if (!seat->udev_monitor) |
| 1894 | return; |
| 1895 | |
| 1896 | udev_monitor_unref(seat->udev_monitor); |
| 1897 | seat->udev_monitor = NULL; |
| 1898 | wl_event_source_remove(seat->udev_monitor_source); |
| 1899 | seat->udev_monitor_source = NULL; |
| 1900 | } |
| 1901 | |
| 1902 | static void |
| 1903 | drm_led_update(struct weston_seat *seat_base, enum weston_led leds) |
| 1904 | { |
| 1905 | struct drm_seat *seat = (struct drm_seat *) seat_base; |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1906 | struct evdev_device *device; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1907 | |
Pekka Paalanen | b9d38f4 | 2012-08-06 14:57:07 +0300 | [diff] [blame] | 1908 | wl_list_for_each(device, &seat->devices_list, link) |
| 1909 | evdev_led_update(device, leds); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static void |
| 1913 | evdev_input_create(struct weston_compositor *c, struct udev *udev, |
| 1914 | const char *seat_id) |
| 1915 | { |
| 1916 | struct drm_seat *seat; |
| 1917 | |
| 1918 | seat = malloc(sizeof *seat); |
| 1919 | if (seat == NULL) |
| 1920 | return; |
| 1921 | |
| 1922 | memset(seat, 0, sizeof *seat); |
| 1923 | weston_seat_init(&seat->base, c); |
| 1924 | seat->base.led_update = drm_led_update; |
| 1925 | |
| 1926 | wl_list_init(&seat->devices_list); |
| 1927 | seat->seat_id = strdup(seat_id); |
| 1928 | if (!evdev_enable_udev_monitor(udev, &seat->base)) { |
| 1929 | free(seat->seat_id); |
| 1930 | free(seat); |
| 1931 | return; |
| 1932 | } |
| 1933 | |
| 1934 | evdev_add_devices(udev, &seat->base); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | static void |
| 1938 | evdev_remove_devices(struct weston_seat *seat_base) |
| 1939 | { |
| 1940 | struct drm_seat *seat = (struct drm_seat *) seat_base; |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1941 | struct evdev_device *device, *next; |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1942 | |
| 1943 | wl_list_for_each_safe(device, next, &seat->devices_list, link) |
Pekka Paalanen | 3eb4761 | 2012-08-06 14:57:08 +0300 | [diff] [blame] | 1944 | evdev_device_destroy(device); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1945 | |
Pekka Paalanen | d858351 | 2012-08-03 14:39:11 +0300 | [diff] [blame] | 1946 | if (seat->base.seat.keyboard) |
Kristian Høgsberg | cb3eaae | 2012-08-10 09:50:11 -0400 | [diff] [blame] | 1947 | notify_keyboard_focus_out(&seat->base); |
Pekka Paalanen | 3315697 | 2012-08-03 13:30:30 -0400 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | static void |
| 1951 | evdev_input_destroy(struct weston_seat *seat_base) |
| 1952 | { |
| 1953 | struct drm_seat *seat = (struct drm_seat *) seat_base; |
| 1954 | |
| 1955 | evdev_remove_devices(seat_base); |
| 1956 | evdev_disable_udev_monitor(&seat->base); |
| 1957 | |
| 1958 | weston_seat_release(seat_base); |
| 1959 | free(seat->seat_id); |
| 1960 | free(seat); |
| 1961 | } |
| 1962 | |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 1963 | static void |
Scott Moreau | c50645c | 2012-07-31 22:29:56 -0600 | [diff] [blame] | 1964 | drm_free_configured_output(struct drm_configured_output *output) |
| 1965 | { |
| 1966 | free(output->name); |
| 1967 | free(output->mode); |
| 1968 | free(output); |
| 1969 | } |
| 1970 | |
| 1971 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1972 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1973 | { |
| 1974 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1975 | struct weston_seat *seat, *next; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1976 | struct drm_configured_output *o, *n; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1977 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1978 | wl_list_for_each_safe(seat, next, &ec->seat_list, link) |
| 1979 | evdev_input_destroy(seat); |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 1980 | wl_list_for_each_safe(o, n, &configured_output_list, link) |
Scott Moreau | c50645c | 2012-07-31 22:29:56 -0600 | [diff] [blame] | 1981 | drm_free_configured_output(o); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1982 | |
| 1983 | wl_event_source_remove(d->udev_drm_source); |
| 1984 | wl_event_source_remove(d->drm_source); |
| 1985 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1986 | weston_compositor_shutdown(ec); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1987 | |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 1988 | gl_renderer_destroy(ec); |
Kristian Høgsberg | 3a0de88 | 2012-09-06 21:44:24 -0400 | [diff] [blame] | 1989 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1990 | destroy_sprites(d); |
Ander Conselvan de Oliveira | 8d360b4 | 2012-11-09 14:19:05 +0200 | [diff] [blame] | 1991 | gbm_device_destroy(d->gbm); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1992 | if (weston_launcher_drm_set_master(&d->base, d->drm.fd, 0) < 0) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1993 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1994 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1995 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1996 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1997 | } |
| 1998 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1999 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 2000 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 2001 | { |
| 2002 | struct drm_output *output; |
| 2003 | struct drm_mode *drm_mode; |
| 2004 | int ret; |
| 2005 | |
| 2006 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 2007 | drm_mode = (struct drm_mode *) output->base.current; |
| 2008 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 2009 | output->current->fb_id, 0, 0, |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 2010 | &output->connector_id, 1, |
| 2011 | &drm_mode->mode_info); |
| 2012 | if (ret < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2013 | weston_log( |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 2014 | "failed to set mode %dx%d for output at %d,%d: %m\n", |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 2015 | drm_mode->base.width, drm_mode->base.height, |
| 2016 | output->base.x, output->base.y); |
| 2017 | } |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2022 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2023 | { |
| 2024 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2025 | struct weston_seat *seat; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2026 | struct drm_sprite *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2027 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2028 | |
| 2029 | switch (event) { |
| 2030 | case TTY_ENTER_VT: |
Pekka Paalanen | 81a13a3 | 2012-08-03 14:39:10 +0300 | [diff] [blame] | 2031 | weston_log("entering VT\n"); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2032 | compositor->focus = 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 2033 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2034 | weston_log("failed to set master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 2035 | wl_display_terminate(compositor->wl_display); |
| 2036 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 2037 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 2038 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2039 | weston_compositor_damage_all(compositor); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2040 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 2041 | evdev_add_devices(ec->udev, seat); |
| 2042 | evdev_enable_udev_monitor(ec->udev, seat); |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 2043 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2044 | break; |
| 2045 | case TTY_LEAVE_VT: |
Pekka Paalanen | 81a13a3 | 2012-08-03 14:39:10 +0300 | [diff] [blame] | 2046 | weston_log("leaving VT\n"); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 2047 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 2048 | evdev_disable_udev_monitor(seat); |
| 2049 | evdev_remove_devices(seat); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 2050 | } |
| 2051 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2052 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 2053 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2054 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 2055 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2056 | /* If we have a repaint scheduled (either from a |
| 2057 | * pending pageflip or the idle handler), make sure we |
| 2058 | * cancel that so we don't try to pageflip when we're |
| 2059 | * vt switched away. The SLEEPING state will prevent |
| 2060 | * further attemps at repainting. When we switch |
| 2061 | * back, we schedule a repaint, which will process |
| 2062 | * pending frame callbacks. */ |
| 2063 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2064 | wl_list_for_each(output, &ec->base.output_list, base.link) { |
| 2065 | output->base.repaint_needed = 0; |
| 2066 | drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 2067 | } |
| 2068 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2069 | output = container_of(ec->base.output_list.next, |
| 2070 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2071 | |
| 2072 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 2073 | drmModeSetPlane(ec->drm.fd, |
| 2074 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 2075 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 2076 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 2077 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 2078 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 0) < 0) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2079 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 2080 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 2081 | break; |
| 2082 | }; |
| 2083 | } |
| 2084 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2085 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 2086 | switch_vt_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data) |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2087 | { |
| 2088 | struct drm_compositor *ec = data; |
| 2089 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 2090 | tty_activate_vt(ec->tty, key - KEY_F1 + 1); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2091 | } |
| 2092 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2093 | /* |
| 2094 | * Find primary GPU |
| 2095 | * Some systems may have multiple DRM devices attached to a single seat. This |
| 2096 | * function loops over all devices and tries to find a PCI device with the |
| 2097 | * boot_vga sysfs attribute set to 1. |
| 2098 | * If no such device is found, the first DRM device reported by udev is used. |
| 2099 | */ |
| 2100 | static struct udev_device* |
| 2101 | find_primary_gpu(struct drm_compositor *ec, const char *seat) |
| 2102 | { |
| 2103 | struct udev_enumerate *e; |
| 2104 | struct udev_list_entry *entry; |
| 2105 | const char *path, *device_seat, *id; |
| 2106 | struct udev_device *device, *drm_device, *pci; |
| 2107 | |
| 2108 | e = udev_enumerate_new(ec->udev); |
| 2109 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 2110 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
| 2111 | |
| 2112 | udev_enumerate_scan_devices(e); |
| 2113 | drm_device = NULL; |
| 2114 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 2115 | path = udev_list_entry_get_name(entry); |
| 2116 | device = udev_device_new_from_syspath(ec->udev, path); |
| 2117 | if (!device) |
| 2118 | continue; |
| 2119 | device_seat = udev_device_get_property_value(device, "ID_SEAT"); |
| 2120 | if (!device_seat) |
| 2121 | device_seat = default_seat; |
| 2122 | if (strcmp(device_seat, seat)) { |
| 2123 | udev_device_unref(device); |
| 2124 | continue; |
| 2125 | } |
| 2126 | |
| 2127 | pci = udev_device_get_parent_with_subsystem_devtype(device, |
| 2128 | "pci", NULL); |
| 2129 | if (pci) { |
| 2130 | id = udev_device_get_sysattr_value(pci, "boot_vga"); |
| 2131 | if (id && !strcmp(id, "1")) { |
| 2132 | if (drm_device) |
| 2133 | udev_device_unref(drm_device); |
| 2134 | drm_device = device; |
| 2135 | break; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | if (!drm_device) |
| 2140 | drm_device = device; |
| 2141 | else |
| 2142 | udev_device_unref(device); |
| 2143 | } |
| 2144 | |
| 2145 | udev_enumerate_unref(e); |
| 2146 | return drm_device; |
| 2147 | } |
| 2148 | |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2149 | static void |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2150 | planes_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data) |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2151 | { |
| 2152 | struct drm_compositor *c = data; |
| 2153 | |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2154 | switch (key) { |
| 2155 | case KEY_C: |
| 2156 | c->cursors_are_broken ^= 1; |
| 2157 | break; |
| 2158 | case KEY_V: |
| 2159 | c->sprites_are_broken ^= 1; |
| 2160 | break; |
| 2161 | case KEY_O: |
| 2162 | c->sprites_hidden ^= 1; |
| 2163 | break; |
| 2164 | default: |
| 2165 | break; |
| 2166 | } |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 2167 | } |
| 2168 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2169 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 2170 | drm_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 2171 | int connector, const char *seat, int tty, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2172 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2173 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2174 | struct drm_compositor *ec; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2175 | struct udev_device *drm_device; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2176 | struct wl_event_loop *loop; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2177 | struct weston_seat *weston_seat, *next; |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2178 | const char *path; |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2179 | uint32_t key; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2180 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 2181 | weston_log("initializing drm backend\n"); |
| 2182 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2183 | ec = malloc(sizeof *ec); |
| 2184 | if (ec == NULL) |
| 2185 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2186 | memset(ec, 0, sizeof *ec); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 2187 | |
Kristian Høgsberg | de37d67 | 2012-11-02 10:14:40 -0400 | [diff] [blame] | 2188 | /* KMS support for sprites is not complete yet, so disable the |
| 2189 | * functionality for now. */ |
| 2190 | ec->sprites_are_broken = 1; |
| 2191 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 2192 | if (weston_compositor_init(&ec->base, display, argc, argv, |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2193 | config_file) < 0) { |
| 2194 | weston_log("weston_compositor_init failed\n"); |
| 2195 | goto err_base; |
| 2196 | } |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 2197 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2198 | ec->udev = udev_new(); |
| 2199 | if (ec->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2200 | weston_log("failed to initialize udev context\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2201 | goto err_compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2202 | } |
| 2203 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 2204 | ec->base.wl_display = display; |
| 2205 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 2206 | if (!ec->tty) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2207 | weston_log("failed to initialize tty\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2208 | goto err_udev; |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 2209 | } |
| 2210 | |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2211 | drm_device = find_primary_gpu(ec, seat); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 2212 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2213 | weston_log("no drm device found\n"); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2214 | goto err_tty; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2215 | } |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2216 | path = udev_device_get_syspath(drm_device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2217 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 2218 | if (init_egl(ec, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2219 | weston_log("failed to initialize egl\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2220 | goto err_udev_dev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2221 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 2222 | |
| 2223 | ec->base.destroy = drm_destroy; |
Kristian Høgsberg | 7b884bc | 2012-07-31 14:32:01 -0400 | [diff] [blame] | 2224 | ec->base.restore = drm_restore; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 2225 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 2226 | ec->base.focus = 1; |
| 2227 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2228 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 2229 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2230 | for (key = KEY_F1; key < KEY_F9; key++) |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 2231 | weston_compositor_add_key_binding(&ec->base, key, |
| 2232 | MODIFIER_CTRL | MODIFIER_ALT, |
| 2233 | switch_vt_binding, ec); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 2234 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 2235 | wl_list_init(&ec->sprite_list); |
| 2236 | create_sprites(ec); |
| 2237 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 2238 | if (create_outputs(ec, connector, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2239 | weston_log("failed to create output for %s\n", path); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2240 | goto err_sprite; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2241 | } |
| 2242 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 2243 | path = NULL; |
| 2244 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 2245 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2246 | |
| 2247 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 2248 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 2249 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2250 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2251 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2252 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 2253 | if (ec->udev_monitor == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2254 | weston_log("failed to intialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2255 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2256 | } |
| 2257 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 2258 | "drm", NULL); |
| 2259 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2260 | wl_event_loop_add_fd(loop, |
| 2261 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2262 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 2263 | |
| 2264 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 2265 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2266 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2269 | udev_device_unref(drm_device); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2270 | |
Ander Conselvan de Oliveira | c509d2b | 2012-11-08 17:20:45 +0200 | [diff] [blame] | 2271 | weston_compositor_add_debug_binding(&ec->base, KEY_O, |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2272 | planes_binding, ec); |
Ander Conselvan de Oliveira | 180f42a | 2012-11-21 15:11:37 +0200 | [diff] [blame] | 2273 | weston_compositor_add_debug_binding(&ec->base, KEY_C, |
Ander Conselvan de Oliveira | 7e918da | 2012-11-22 15:56:59 +0200 | [diff] [blame] | 2274 | planes_binding, ec); |
| 2275 | weston_compositor_add_debug_binding(&ec->base, KEY_V, |
| 2276 | planes_binding, ec); |
Ander Conselvan de Oliveira | da1c908 | 2012-10-31 17:55:46 +0200 | [diff] [blame] | 2277 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 2278 | return &ec->base; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2279 | |
| 2280 | err_udev_monitor: |
| 2281 | wl_event_source_remove(ec->udev_drm_source); |
| 2282 | udev_monitor_unref(ec->udev_monitor); |
| 2283 | err_drm_source: |
| 2284 | wl_event_source_remove(ec->drm_source); |
| 2285 | wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link) |
| 2286 | evdev_input_destroy(weston_seat); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 2287 | err_sprite: |
John Kåre Alsaker | 779b52a | 2012-11-13 19:10:29 +0100 | [diff] [blame] | 2288 | gl_renderer_destroy(&ec->base); |
John Kåre Alsaker | 1b7ad16 | 2012-11-13 19:10:19 +0100 | [diff] [blame] | 2289 | gbm_device_destroy(ec->gbm); |
Kristian Høgsberg | 2bc5e8e | 2012-09-06 20:51:00 -0400 | [diff] [blame] | 2290 | destroy_sprites(ec); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2291 | err_udev_dev: |
| 2292 | udev_device_unref(drm_device); |
David Herrmann | 0af066f | 2012-10-29 19:21:16 +0100 | [diff] [blame] | 2293 | err_tty: |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 2294 | tty_destroy(ec->tty); |
| 2295 | err_udev: |
| 2296 | udev_unref(ec->udev); |
| 2297 | err_compositor: |
| 2298 | weston_compositor_shutdown(&ec->base); |
| 2299 | err_base: |
| 2300 | free(ec); |
| 2301 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 2302 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2303 | |
Scott Moreau | 8f37e0b | 2012-07-31 15:30:41 -0600 | [diff] [blame] | 2304 | static int |
| 2305 | set_sync_flags(drmModeModeInfo *mode, char *hsync, char *vsync) |
| 2306 | { |
| 2307 | mode->flags = 0; |
| 2308 | |
| 2309 | if (strcmp(hsync, "+hsync") == 0) |
| 2310 | mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 2311 | else if (strcmp(hsync, "-hsync") == 0) |
| 2312 | mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 2313 | else |
| 2314 | return -1; |
| 2315 | |
| 2316 | if (strcmp(vsync, "+vsync") == 0) |
| 2317 | mode->flags |= DRM_MODE_FLAG_PVSYNC; |
| 2318 | else if (strcmp(vsync, "-vsync") == 0) |
| 2319 | mode->flags |= DRM_MODE_FLAG_NVSYNC; |
| 2320 | else |
| 2321 | return -1; |
| 2322 | |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | static int |
| 2327 | check_for_modeline(struct drm_configured_output *output) |
| 2328 | { |
| 2329 | drmModeModeInfo mode; |
| 2330 | char hsync[16]; |
| 2331 | char vsync[16]; |
| 2332 | char mode_name[16]; |
| 2333 | float fclock; |
| 2334 | |
| 2335 | mode.type = DRM_MODE_TYPE_USERDEF; |
| 2336 | mode.hskew = 0; |
| 2337 | mode.vscan = 0; |
| 2338 | mode.vrefresh = 0; |
| 2339 | |
| 2340 | if (sscanf(output_mode, "%f %hd %hd %hd %hd %hd %hd %hd %hd %s %s", |
| 2341 | &fclock, &mode.hdisplay, |
| 2342 | &mode.hsync_start, |
| 2343 | &mode.hsync_end, &mode.htotal, |
| 2344 | &mode.vdisplay, |
| 2345 | &mode.vsync_start, |
| 2346 | &mode.vsync_end, &mode.vtotal, |
| 2347 | hsync, vsync) == 11) { |
| 2348 | if (set_sync_flags(&mode, hsync, vsync)) |
| 2349 | return -1; |
| 2350 | |
| 2351 | sprintf(mode_name, "%dx%d", mode.hdisplay, mode.vdisplay); |
| 2352 | strcpy(mode.name, mode_name); |
| 2353 | |
| 2354 | mode.clock = fclock * 1000; |
| 2355 | } else |
| 2356 | return -1; |
| 2357 | |
| 2358 | output->crtc_mode = mode; |
| 2359 | |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2363 | static void |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2364 | drm_output_set_transform(struct drm_configured_output *output) |
| 2365 | { |
| 2366 | if (!output_transform) { |
| 2367 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 2368 | return; |
| 2369 | } |
| 2370 | |
| 2371 | if (!strcmp(output_transform, "normal")) |
| 2372 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 2373 | else if (!strcmp(output_transform, "90")) |
| 2374 | output->transform = WL_OUTPUT_TRANSFORM_90; |
| 2375 | else if (!strcmp(output_transform, "180")) |
| 2376 | output->transform = WL_OUTPUT_TRANSFORM_180; |
| 2377 | else if (!strcmp(output_transform, "270")) |
| 2378 | output->transform = WL_OUTPUT_TRANSFORM_270; |
| 2379 | else if (!strcmp(output_transform, "flipped")) |
| 2380 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED; |
| 2381 | else if (!strcmp(output_transform, "flipped-90")) |
| 2382 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90; |
| 2383 | else if (!strcmp(output_transform, "flipped-180")) |
| 2384 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180; |
| 2385 | else if (!strcmp(output_transform, "flipped-270")) |
| 2386 | output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270; |
| 2387 | else { |
| 2388 | weston_log("Invalid transform \"%s\" for output %s\n", |
| 2389 | output_transform, output_name); |
| 2390 | output->transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 2391 | } |
| 2392 | |
| 2393 | free(output_transform); |
| 2394 | output_transform = NULL; |
| 2395 | } |
| 2396 | |
| 2397 | static void |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2398 | output_section_done(void *data) |
| 2399 | { |
| 2400 | struct drm_configured_output *output; |
| 2401 | |
| 2402 | output = malloc(sizeof *output); |
| 2403 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2404 | if (!output || !output_name || (output_name[0] == 'X') || |
| 2405 | (!output_mode && !output_transform)) { |
Kristian Høgsberg | 8ff6915 | 2012-07-31 22:18:28 -0400 | [diff] [blame] | 2406 | free(output_name); |
Kristian Høgsberg | 8ff6915 | 2012-07-31 22:18:28 -0400 | [diff] [blame] | 2407 | free(output_mode); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2408 | free(output_transform); |
Rob Bradford | 6b6795f | 2012-10-09 18:44:35 +0100 | [diff] [blame] | 2409 | free(output); |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2410 | output_name = NULL; |
Kristian Høgsberg | 8ff6915 | 2012-07-31 22:18:28 -0400 | [diff] [blame] | 2411 | output_mode = NULL; |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2412 | output_transform = NULL; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2413 | return; |
Kristian Høgsberg | 8ff6915 | 2012-07-31 22:18:28 -0400 | [diff] [blame] | 2414 | } |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2415 | |
| 2416 | output->config = OUTPUT_CONFIG_INVALID; |
| 2417 | output->name = output_name; |
| 2418 | output->mode = output_mode; |
| 2419 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2420 | if (output_mode) { |
| 2421 | if (strcmp(output_mode, "off") == 0) |
| 2422 | output->config = OUTPUT_CONFIG_OFF; |
| 2423 | else if (strcmp(output_mode, "preferred") == 0) |
| 2424 | output->config = OUTPUT_CONFIG_PREFERRED; |
| 2425 | else if (strcmp(output_mode, "current") == 0) |
| 2426 | output->config = OUTPUT_CONFIG_CURRENT; |
| 2427 | else if (sscanf(output_mode, "%dx%d", |
| 2428 | &output->width, &output->height) == 2) |
| 2429 | output->config = OUTPUT_CONFIG_MODE; |
| 2430 | else if (check_for_modeline(output) == 0) |
| 2431 | output->config = OUTPUT_CONFIG_MODELINE; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2432 | |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2433 | if (output->config == OUTPUT_CONFIG_INVALID) |
| 2434 | weston_log("Invalid mode \"%s\" for output %s\n", |
| 2435 | output_mode, output_name); |
| 2436 | output_mode = NULL; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2437 | } |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2438 | |
| 2439 | drm_output_set_transform(output); |
| 2440 | |
| 2441 | wl_list_insert(&configured_output_list, &output->link); |
| 2442 | |
| 2443 | if (output_transform) |
| 2444 | free(output_transform); |
| 2445 | output_transform = NULL; |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2446 | } |
| 2447 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 2448 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2449 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 2450 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2451 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2452 | int connector = 0, tty = 0; |
| 2453 | const char *seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2454 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2455 | const struct weston_option drm_options[] = { |
| 2456 | { WESTON_OPTION_INTEGER, "connector", 0, &connector }, |
| 2457 | { WESTON_OPTION_STRING, "seat", 0, &seat }, |
| 2458 | { WESTON_OPTION_INTEGER, "tty", 0, &tty }, |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 2459 | { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode }, |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2460 | }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 2461 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 2462 | parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2463 | |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2464 | wl_list_init(&configured_output_list); |
| 2465 | |
| 2466 | const struct config_key drm_config_keys[] = { |
| 2467 | { "name", CONFIG_KEY_STRING, &output_name }, |
| 2468 | { "mode", CONFIG_KEY_STRING, &output_mode }, |
Scott Moreau | 1bad5db | 2012-08-18 01:04:05 -0600 | [diff] [blame] | 2469 | { "transform", CONFIG_KEY_STRING, &output_transform }, |
Scott Moreau | 8ab5d45 | 2012-07-30 19:51:08 -0600 | [diff] [blame] | 2470 | }; |
| 2471 | |
| 2472 | const struct config_section config_section[] = { |
| 2473 | { "output", drm_config_keys, |
| 2474 | ARRAY_LENGTH(drm_config_keys), output_section_done }, |
| 2475 | }; |
| 2476 | |
| 2477 | parse_config_file(config_file, config_section, |
| 2478 | ARRAY_LENGTH(config_section), NULL); |
| 2479 | |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2480 | return drm_compositor_create(display, connector, seat, tty, argc, argv, |
| 2481 | config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2482 | } |