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