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