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