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