Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 2 | * Copyright © 2008-2011 Kristian Høgsberg |
| 3 | * Copyright © 2011 Intel Corporation |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 4 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 14 | * |
Kristian Høgsberg | 96aa7da | 2011-09-15 15:43:14 -0400 | [diff] [blame] | 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 22 | */ |
| 23 | |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 24 | #define _GNU_SOURCE |
| 25 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 26 | #include <errno.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 31 | #include <linux/input.h> |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 32 | #include <assert.h> |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 33 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 34 | #include <xf86drm.h> |
| 35 | #include <xf86drmMode.h> |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 36 | #include <drm_fourcc.h> |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 37 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 38 | #include <gbm.h> |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 39 | #include <libbacklight.h> |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 40 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 41 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 42 | #include "evdev.h" |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 43 | #include "launcher-util.h" |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 44 | #include "log.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 45 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 46 | static int option_current_mode = 0; |
| 47 | |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 48 | enum { |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 49 | WESTON_PLANE_DRM_CURSOR = 0x100, |
| 50 | WESTON_PLANE_DRM_PLANE = 0x101, |
| 51 | WESTON_PLANE_DRM_FB = 0x102 |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 54 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 55 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 56 | |
| 57 | struct udev *udev; |
| 58 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 59 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 60 | struct udev_monitor *udev_monitor; |
| 61 | struct wl_event_source *udev_drm_source; |
| 62 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 63 | struct { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 64 | int id; |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 65 | int fd; |
| 66 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 67 | struct gbm_device *gbm; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 68 | uint32_t *crtcs; |
| 69 | int num_crtcs; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 70 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 71 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 72 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 73 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 74 | struct gbm_surface *dummy_surface; |
| 75 | EGLSurface dummy_egl_surface; |
| 76 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 77 | struct wl_list sprite_list; |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 78 | int sprites_are_broken; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 79 | |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 80 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 81 | }; |
| 82 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 83 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 84 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 85 | drmModeModeInfo mode_info; |
| 86 | }; |
| 87 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 88 | struct drm_output; |
| 89 | |
| 90 | struct drm_fb { |
| 91 | struct gbm_bo *bo; |
| 92 | struct drm_output *output; |
| 93 | uint32_t fb_id; |
| 94 | int is_client_buffer; |
| 95 | struct wl_buffer *buffer; |
| 96 | struct wl_listener buffer_destroy_listener; |
| 97 | }; |
| 98 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 99 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 100 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 101 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 102 | char *name; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 103 | uint32_t crtc_id; |
| 104 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 105 | drmModeCrtcPtr original_crtc; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 106 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 107 | int vblank_pending; |
| 108 | int page_flip_pending; |
| 109 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 110 | struct gbm_surface *surface; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 111 | struct gbm_bo *cursor_bo[2]; |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 112 | int current_cursor, cursor_free, cursor_x, cursor_y; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 113 | EGLSurface egl_surface; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 114 | struct drm_fb *current, *next; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 115 | struct backlight *backlight; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 116 | }; |
| 117 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 118 | /* |
| 119 | * An output has a primary display plane plus zero or more sprites for |
| 120 | * blending display contents. |
| 121 | */ |
| 122 | struct drm_sprite { |
| 123 | struct wl_list link; |
| 124 | |
| 125 | uint32_t fb_id; |
| 126 | uint32_t pending_fb_id; |
| 127 | struct weston_surface *surface; |
| 128 | struct weston_surface *pending_surface; |
| 129 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 130 | struct drm_output *output; |
| 131 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 132 | struct drm_compositor *compositor; |
| 133 | |
| 134 | struct wl_listener destroy_listener; |
| 135 | struct wl_listener pending_destroy_listener; |
| 136 | |
| 137 | uint32_t possible_crtcs; |
| 138 | uint32_t plane_id; |
| 139 | uint32_t count_formats; |
| 140 | |
| 141 | int32_t src_x, src_y; |
| 142 | uint32_t src_w, src_h; |
| 143 | uint32_t dest_x, dest_y; |
| 144 | uint32_t dest_w, dest_h; |
| 145 | |
| 146 | uint32_t formats[]; |
| 147 | }; |
| 148 | |
| 149 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 150 | drm_sprite_crtc_supported(struct weston_output *output_base, uint32_t supported) |
| 151 | { |
| 152 | struct weston_compositor *ec = output_base->compositor; |
| 153 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 154 | struct drm_output *output = (struct drm_output *) output_base; |
| 155 | int crtc; |
| 156 | |
| 157 | for (crtc = 0; crtc < c->num_crtcs; crtc++) { |
| 158 | if (c->crtcs[crtc] != output->crtc_id) |
| 159 | continue; |
| 160 | |
| 161 | if (supported & (1 << crtc)) |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 168 | static void |
| 169 | drm_fb_destroy_callback(struct gbm_bo *bo, void *data) |
| 170 | { |
| 171 | struct drm_fb *fb = data; |
| 172 | struct gbm_device *gbm = gbm_bo_get_device(bo); |
| 173 | |
| 174 | if (fb->fb_id) |
| 175 | drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id); |
| 176 | |
| 177 | if (fb->buffer) { |
| 178 | weston_buffer_post_release(fb->buffer); |
| 179 | wl_list_remove(&fb->buffer_destroy_listener.link); |
| 180 | } |
| 181 | |
| 182 | free(data); |
| 183 | } |
| 184 | |
| 185 | static struct drm_fb * |
| 186 | drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_output *output) |
| 187 | { |
| 188 | struct drm_fb *fb = gbm_bo_get_user_data(bo); |
| 189 | struct drm_compositor *compositor = |
| 190 | (struct drm_compositor *) output->base.compositor; |
| 191 | uint32_t width, height, stride, handle; |
| 192 | int ret; |
| 193 | |
| 194 | if (fb) |
| 195 | return fb; |
| 196 | |
| 197 | fb = malloc(sizeof *fb); |
| 198 | |
| 199 | fb->bo = bo; |
| 200 | fb->output = output; |
| 201 | fb->is_client_buffer = 0; |
| 202 | fb->buffer = NULL; |
| 203 | |
| 204 | width = gbm_bo_get_width(bo); |
| 205 | height = gbm_bo_get_height(bo); |
Kristian Høgsberg | 270a7cb | 2012-07-16 16:44:16 -0400 | [diff] [blame] | 206 | stride = gbm_bo_get_stride(bo); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 207 | handle = gbm_bo_get_handle(bo).u32; |
| 208 | |
| 209 | ret = drmModeAddFB(compositor->drm.fd, width, height, 24, 32, |
| 210 | stride, handle, &fb->fb_id); |
| 211 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 212 | weston_log("failed to create kms fb: %m\n"); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 213 | free(fb); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback); |
| 218 | |
| 219 | return fb; |
| 220 | } |
| 221 | |
| 222 | static void |
| 223 | fb_handle_buffer_destroy(struct wl_listener *listener, void *data) |
| 224 | { |
| 225 | struct drm_fb *fb = container_of(listener, struct drm_fb, |
| 226 | buffer_destroy_listener); |
| 227 | |
| 228 | fb->buffer = NULL; |
| 229 | |
| 230 | if (fb == fb->output->next || |
| 231 | (fb == fb->output->current && !fb->output->next)) |
Kristian Høgsberg | 49952d1 | 2012-06-20 00:35:59 -0400 | [diff] [blame] | 232 | weston_output_schedule_repaint(&fb->output->base); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 233 | } |
| 234 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 235 | static int |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 236 | drm_output_prepare_scanout_surface(struct weston_output *_output, |
| 237 | struct weston_surface *es) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 238 | { |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 239 | struct drm_output *output = (struct drm_output *) _output; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 240 | struct drm_compositor *c = |
| 241 | (struct drm_compositor *) output->base.compositor; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 242 | struct gbm_bo *bo; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 243 | |
Kristian Høgsberg | 101cb65 | 2012-02-17 10:45:16 -0500 | [diff] [blame] | 244 | if (es->geometry.x != output->base.x || |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 245 | es->geometry.y != output->base.y || |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 246 | es->geometry.width != output->base.current->width || |
| 247 | es->geometry.height != output->base.current->height || |
Pekka Paalanen | f1f5b36 | 2012-01-25 14:33:33 +0200 | [diff] [blame] | 248 | es->transform.enabled || |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 249 | es->buffer == NULL) |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 250 | return -1; |
| 251 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 252 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 253 | es->buffer, GBM_BO_USE_SCANOUT); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 254 | |
Ander Conselvan de Oliveira | a64b15d | 2012-05-02 16:42:22 +0300 | [diff] [blame] | 255 | /* Need to verify output->region contained in surface opaque |
| 256 | * region. Or maybe just that format doesn't have alpha. |
| 257 | * For now, scanout only if format is XRGB8888. */ |
| 258 | if (gbm_bo_get_format(bo) != GBM_FORMAT_XRGB8888) { |
| 259 | gbm_bo_destroy(bo); |
| 260 | return -1; |
| 261 | } |
| 262 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 263 | output->next = drm_fb_get_from_bo(bo, output); |
| 264 | if (!output->next) { |
| 265 | gbm_bo_destroy(bo); |
| 266 | return -1; |
| 267 | } |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 268 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 269 | output->next->is_client_buffer = 1; |
| 270 | output->next->buffer = es->buffer; |
| 271 | output->next->buffer->busy_count++; |
| 272 | output->next->buffer_destroy_listener.notify = fb_handle_buffer_destroy; |
| 273 | |
| 274 | wl_signal_add(&output->next->buffer->resource.destroy_signal, |
| 275 | &output->next->buffer_destroy_listener); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 276 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 277 | es->plane = WESTON_PLANE_DRM_FB; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 282 | static void |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 283 | drm_output_render(struct drm_output *output, pixman_region32_t *damage) |
| 284 | { |
| 285 | struct drm_compositor *compositor = |
| 286 | (struct drm_compositor *) output->base.compositor; |
| 287 | struct weston_surface *surface; |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 288 | struct gbm_bo *bo; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 289 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 290 | if (!eglMakeCurrent(compositor->base.egl_display, output->egl_surface, |
| 291 | output->egl_surface, |
| 292 | compositor->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 293 | weston_log("failed to make current\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
| 297 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 298 | if (surface->plane == WESTON_PLANE_PRIMARY) |
| 299 | weston_surface_draw(surface, &output->base, damage); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 300 | |
Kristian Høgsberg | e0f832b | 2012-06-20 00:13:18 -0400 | [diff] [blame] | 301 | wl_signal_emit(&output->base.frame_signal, output); |
Scott Moreau | 062be7e | 2012-04-20 13:37:33 -0600 | [diff] [blame] | 302 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 303 | eglSwapBuffers(compositor->base.egl_display, output->egl_surface); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 304 | bo = gbm_surface_lock_front_buffer(output->surface); |
| 305 | if (!bo) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 306 | weston_log("failed to lock front buffer: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 307 | return; |
| 308 | } |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 309 | |
| 310 | output->next = drm_fb_get_from_bo(bo, output); |
| 311 | if (!output->next) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 312 | weston_log("failed to get drm_fb for bo\n"); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 313 | gbm_surface_release_buffer(output->surface, bo); |
| 314 | return; |
| 315 | } |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void |
Kristian Høgsberg | 6ddcdae | 2012-02-28 22:31:58 -0500 | [diff] [blame] | 319 | drm_output_repaint(struct weston_output *output_base, |
| 320 | pixman_region32_t *damage) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 321 | { |
| 322 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 323 | struct drm_compositor *compositor = |
| 324 | (struct drm_compositor *) output->base.compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 325 | struct drm_sprite *s; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 326 | struct drm_mode *mode; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 327 | int ret = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 328 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 329 | if (!output->next) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 330 | drm_output_render(output, damage); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 331 | if (!output->next) |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 332 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 333 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 334 | mode = container_of(output->base.current, struct drm_mode, base); |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 335 | if (!output->current) { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 336 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 337 | output->next->fb_id, 0, 0, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 338 | &output->connector_id, 1, |
| 339 | &mode->mode_info); |
| 340 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 341 | weston_log("set mode failed: %m\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 342 | return; |
| 343 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 346 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 347 | output->next->fb_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 348 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 349 | weston_log("queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 350 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 351 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 352 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 353 | output->page_flip_pending = 1; |
| 354 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 355 | /* |
| 356 | * Now, update all the sprite surfaces |
| 357 | */ |
| 358 | wl_list_for_each(s, &compositor->sprite_list, link) { |
| 359 | uint32_t flags = 0; |
| 360 | drmVBlank vbl = { |
| 361 | .request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT, |
| 362 | .request.sequence = 1, |
| 363 | }; |
| 364 | |
| 365 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 366 | continue; |
| 367 | |
| 368 | ret = drmModeSetPlane(compositor->drm.fd, s->plane_id, |
| 369 | output->crtc_id, s->pending_fb_id, flags, |
| 370 | s->dest_x, s->dest_y, |
| 371 | s->dest_w, s->dest_h, |
| 372 | s->src_x, s->src_y, |
| 373 | s->src_w, s->src_h); |
| 374 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 375 | weston_log("setplane failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 376 | ret, strerror(errno)); |
| 377 | |
| 378 | /* |
| 379 | * Queue a vblank signal so we know when the surface |
| 380 | * becomes active on the display or has been replaced. |
| 381 | */ |
| 382 | vbl.request.signal = (unsigned long)s; |
| 383 | ret = drmWaitVBlank(compositor->drm.fd, &vbl); |
| 384 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 385 | weston_log("vblank event request failed: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 386 | ret, strerror(errno)); |
| 387 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 388 | |
| 389 | s->output = output; |
| 390 | output->vblank_pending = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 393 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static void |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 397 | vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, |
| 398 | void *data) |
| 399 | { |
| 400 | struct drm_sprite *s = (struct drm_sprite *)data; |
| 401 | struct drm_compositor *c = s->compositor; |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 402 | struct drm_output *output = s->output; |
| 403 | uint32_t msecs; |
| 404 | |
| 405 | output->vblank_pending = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 406 | |
| 407 | if (s->surface) { |
| 408 | weston_buffer_post_release(s->surface->buffer); |
| 409 | wl_list_remove(&s->destroy_listener.link); |
| 410 | s->surface = NULL; |
| 411 | drmModeRmFB(c->drm.fd, s->fb_id); |
| 412 | s->fb_id = 0; |
| 413 | } |
| 414 | |
| 415 | if (s->pending_surface) { |
| 416 | wl_list_remove(&s->pending_destroy_listener.link); |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 417 | wl_signal_add(&s->pending_surface->buffer->resource.destroy_signal, |
| 418 | &s->destroy_listener); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 419 | s->surface = s->pending_surface; |
| 420 | s->pending_surface = NULL; |
| 421 | s->fb_id = s->pending_fb_id; |
| 422 | s->pending_fb_id = 0; |
| 423 | } |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 424 | |
| 425 | if (!output->page_flip_pending) { |
| 426 | msecs = sec * 1000 + usec / 1000; |
| 427 | weston_output_finish_frame(&output->base, msecs); |
| 428 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | static void |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 432 | page_flip_handler(int fd, unsigned int frame, |
| 433 | unsigned int sec, unsigned int usec, void *data) |
| 434 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 435 | struct drm_output *output = (struct drm_output *) data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 436 | uint32_t msecs; |
| 437 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 438 | output->page_flip_pending = 0; |
| 439 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 440 | if (output->current) { |
| 441 | if (output->current->is_client_buffer) |
| 442 | gbm_bo_destroy(output->current->bo); |
| 443 | else |
| 444 | gbm_surface_release_buffer(output->surface, |
| 445 | output->current->bo); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 448 | output->current = output->next; |
| 449 | output->next = NULL; |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 450 | |
Ander Conselvan de Oliveira | a732696 | 2012-06-26 17:09:13 +0300 | [diff] [blame] | 451 | if (!output->vblank_pending) { |
| 452 | msecs = sec * 1000 + usec / 1000; |
| 453 | weston_output_finish_frame(&output->base, msecs); |
| 454 | } |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 458 | drm_surface_format_supported(struct drm_sprite *s, uint32_t format) |
| 459 | { |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 460 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 461 | |
| 462 | for (i = 0; i < s->count_formats; i++) |
| 463 | if (s->formats[i] == format) |
| 464 | return 1; |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int |
| 470 | drm_surface_transform_supported(struct weston_surface *es) |
| 471 | { |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 472 | struct weston_matrix *matrix = &es->transform.matrix; |
| 473 | int i; |
| 474 | |
| 475 | if (!es->transform.enabled) |
| 476 | return 1; |
| 477 | |
| 478 | for (i = 0; i < 16; i++) { |
| 479 | switch (i) { |
| 480 | case 10: |
| 481 | case 15: |
| 482 | if (matrix->d[i] != 1.0) |
| 483 | return 0; |
| 484 | break; |
| 485 | case 0: |
| 486 | case 5: |
| 487 | case 12: |
| 488 | case 13: |
| 489 | break; |
| 490 | default: |
| 491 | if (matrix->d[i] != 0.0) |
| 492 | return 0; |
| 493 | break; |
| 494 | } |
| 495 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 496 | |
| 497 | return 1; |
| 498 | } |
| 499 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 500 | static void |
| 501 | drm_disable_unused_sprites(struct weston_output *output_base) |
| 502 | { |
| 503 | struct weston_compositor *ec = output_base->compositor; |
| 504 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 505 | struct drm_output *output = (struct drm_output *) output_base; |
| 506 | struct drm_sprite *s; |
| 507 | int ret; |
| 508 | |
| 509 | wl_list_for_each(s, &c->sprite_list, link) { |
| 510 | if (s->pending_fb_id) |
| 511 | continue; |
| 512 | |
| 513 | ret = drmModeSetPlane(c->drm.fd, s->plane_id, |
| 514 | output->crtc_id, 0, 0, |
| 515 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 516 | if (ret) |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 517 | weston_log("failed to disable plane: %d: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 518 | ret, strerror(errno)); |
| 519 | drmModeRmFB(c->drm.fd, s->fb_id); |
Ander Conselvan de Oliveira | fd1f4c6 | 2012-06-26 17:09:14 +0300 | [diff] [blame] | 520 | |
| 521 | if (s->surface) { |
| 522 | s->surface = NULL; |
| 523 | wl_list_remove(&s->destroy_listener.link); |
| 524 | } |
| 525 | |
| 526 | assert(!s->pending_surface); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 527 | s->fb_id = 0; |
| 528 | s->pending_fb_id = 0; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * This function must take care to damage any previously assigned surface |
| 534 | * if the sprite ends up binding to a different surface than in the |
| 535 | * previous frame. |
| 536 | */ |
| 537 | static int |
| 538 | drm_output_prepare_overlay_surface(struct weston_output *output_base, |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 539 | struct weston_surface *es) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 540 | { |
| 541 | struct weston_compositor *ec = output_base->compositor; |
| 542 | struct drm_compositor *c =(struct drm_compositor *) ec; |
| 543 | struct drm_sprite *s; |
| 544 | int found = 0; |
| 545 | EGLint handle, stride; |
| 546 | struct gbm_bo *bo; |
| 547 | uint32_t fb_id = 0; |
| 548 | uint32_t handles[4], pitches[4], offsets[4]; |
| 549 | int ret = 0; |
| 550 | pixman_region32_t dest_rect, src_rect; |
| 551 | pixman_box32_t *box; |
| 552 | uint32_t format; |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 553 | wl_fixed_t sx1, sy1, sx2, sy2; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 554 | |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 555 | if (c->sprites_are_broken) |
| 556 | return -1; |
| 557 | |
Ander Conselvan de Oliveira | d450b19 | 2012-06-26 17:09:12 +0300 | [diff] [blame] | 558 | if (es->output_mask != (1u << output_base->id)) |
| 559 | return -1; |
| 560 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 561 | if (es->buffer == NULL) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 562 | return -1; |
| 563 | |
| 564 | if (!drm_surface_transform_supported(es)) |
| 565 | return -1; |
| 566 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 567 | wl_list_for_each(s, &c->sprite_list, link) { |
| 568 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 569 | continue; |
| 570 | |
| 571 | if (!s->pending_fb_id) { |
| 572 | found = 1; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* No sprites available */ |
| 578 | if (!found) |
| 579 | return -1; |
| 580 | |
Kristian Høgsberg | 2763a2e | 2012-07-13 22:54:43 -0400 | [diff] [blame] | 581 | bo = gbm_bo_import(c->gbm, GBM_BO_IMPORT_WL_BUFFER, |
| 582 | es->buffer, GBM_BO_USE_SCANOUT); |
| 583 | if (!bo) |
| 584 | return -1; |
| 585 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 586 | format = gbm_bo_get_format(bo); |
| 587 | handle = gbm_bo_get_handle(bo).s32; |
Kristian Høgsberg | 270a7cb | 2012-07-16 16:44:16 -0400 | [diff] [blame] | 588 | stride = gbm_bo_get_stride(bo); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 589 | |
| 590 | gbm_bo_destroy(bo); |
| 591 | |
| 592 | if (!drm_surface_format_supported(s, format)) |
| 593 | return -1; |
| 594 | |
| 595 | if (!handle) |
| 596 | return -1; |
| 597 | |
| 598 | handles[0] = handle; |
| 599 | pitches[0] = stride; |
| 600 | offsets[0] = 0; |
| 601 | |
| 602 | ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height, |
| 603 | format, handles, pitches, offsets, |
| 604 | &fb_id, 0); |
| 605 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 606 | weston_log("addfb2 failed: %d\n", ret); |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 607 | c->sprites_are_broken = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 608 | return -1; |
| 609 | } |
| 610 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 611 | s->pending_fb_id = fb_id; |
| 612 | s->pending_surface = es; |
| 613 | es->buffer->busy_count++; |
| 614 | |
| 615 | /* |
| 616 | * Calculate the source & dest rects properly based on actual |
| 617 | * postion (note the caller has called weston_surface_update_transform() |
| 618 | * for us already). |
| 619 | */ |
| 620 | pixman_region32_init(&dest_rect); |
| 621 | pixman_region32_intersect(&dest_rect, &es->transform.boundingbox, |
| 622 | &output_base->region); |
| 623 | pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y); |
| 624 | box = pixman_region32_extents(&dest_rect); |
| 625 | s->dest_x = box->x1; |
| 626 | s->dest_y = box->y1; |
| 627 | s->dest_w = box->x2 - box->x1; |
| 628 | s->dest_h = box->y2 - box->y1; |
| 629 | pixman_region32_fini(&dest_rect); |
| 630 | |
| 631 | pixman_region32_init(&src_rect); |
| 632 | pixman_region32_intersect(&src_rect, &es->transform.boundingbox, |
| 633 | &output_base->region); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 634 | box = pixman_region32_extents(&src_rect); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 635 | |
| 636 | weston_surface_from_global_fixed(es, |
| 637 | wl_fixed_from_int(box->x1), |
| 638 | wl_fixed_from_int(box->y1), |
| 639 | &sx1, &sy1); |
| 640 | weston_surface_from_global_fixed(es, |
| 641 | wl_fixed_from_int(box->x2), |
| 642 | wl_fixed_from_int(box->y2), |
| 643 | &sx2, &sy2); |
| 644 | |
| 645 | if (sx1 < 0) |
| 646 | sx1 = 0; |
| 647 | if (sy1 < 0) |
| 648 | sy1 = 0; |
| 649 | if (sx2 > wl_fixed_from_int(es->geometry.width)) |
| 650 | sx2 = wl_fixed_from_int(es->geometry.width); |
| 651 | if (sy2 > wl_fixed_from_int(es->geometry.height)) |
| 652 | sy2 = wl_fixed_from_int(es->geometry.height); |
| 653 | |
| 654 | s->src_x = sx1 << 8; |
| 655 | s->src_y = sy1 << 8; |
| 656 | s->src_w = (sx2 - sx1) << 8; |
| 657 | s->src_h = (sy2 - sy1) << 8; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 658 | pixman_region32_fini(&src_rect); |
| 659 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 660 | es->plane = WESTON_PLANE_DRM_PLANE; |
| 661 | |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 662 | wl_signal_add(&es->buffer->resource.destroy_signal, |
| 663 | &s->pending_destroy_listener); |
Kristian Høgsberg | 3b00bae | 2012-07-13 15:25:07 -0400 | [diff] [blame] | 664 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 665 | return 0; |
| 666 | } |
| 667 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 668 | static void |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 669 | drm_output_set_cursor(struct weston_output *output_base, |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 670 | struct weston_surface *es) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 671 | { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 672 | struct drm_output *output = (struct drm_output *) output_base; |
| 673 | struct drm_compositor *c = |
| 674 | (struct drm_compositor *) output->base.compositor; |
| 675 | EGLint handle, stride; |
| 676 | struct gbm_bo *bo; |
| 677 | uint32_t buf[64 * 64]; |
| 678 | unsigned char *s; |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 679 | int i, x, y; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 680 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 681 | if (!output->cursor_free) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 682 | return; |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 683 | if (es->output_mask != (1u << output_base->id)) |
| 684 | return; |
| 685 | if (es->buffer == NULL || !wl_buffer_is_shm(es->buffer) || |
| 686 | es->geometry.width > 64 || es->geometry.height > 64) |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 687 | return; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 688 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 689 | if (es->buffer && pixman_region32_not_empty(&es->damage)) { |
| 690 | output->current_cursor ^= 1; |
| 691 | bo = output->cursor_bo[output->current_cursor]; |
| 692 | memset(buf, 0, sizeof buf); |
| 693 | stride = wl_shm_buffer_get_stride(es->buffer); |
| 694 | s = wl_shm_buffer_get_data(es->buffer); |
| 695 | for (i = 0; i < es->geometry.height; i++) |
| 696 | memcpy(buf + i * 64, s + i * stride, |
| 697 | es->geometry.width * 4); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 698 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 699 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
| 700 | return; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 701 | |
Kristian Høgsberg | 1f5de35 | 2012-07-18 12:09:58 -0400 | [diff] [blame] | 702 | handle = gbm_bo_get_handle(bo).s32; |
| 703 | if (drmModeSetCursor(c->drm.fd, |
| 704 | output->crtc_id, handle, 64, 64)) { |
| 705 | weston_log("failed to set cursor: %n\n"); |
| 706 | return; |
| 707 | } |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Kristian Høgsberg | 24e4275 | 2012-07-18 12:08:37 -0400 | [diff] [blame] | 710 | x = es->geometry.x - output->base.x; |
| 711 | y = es->geometry.y - output->base.y; |
| 712 | if (output->cursor_x != x || output->cursor_y != y) { |
| 713 | if (drmModeMoveCursor(c->drm.fd, output->crtc_id, x, y)) { |
| 714 | weston_log("failed to move cursor: %m\n"); |
| 715 | return; |
| 716 | } |
| 717 | output->cursor_x = x; |
| 718 | output->cursor_y = y; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 719 | } |
| 720 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 721 | es->plane = WESTON_PLANE_DRM_CURSOR; |
| 722 | output->cursor_free = 0; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 723 | } |
| 724 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 725 | static void |
| 726 | drm_assign_planes(struct weston_output *output) |
| 727 | { |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 728 | struct drm_compositor *c = |
| 729 | (struct drm_compositor *) output->compositor; |
| 730 | struct drm_output *drm_output = (struct drm_output *) output; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 731 | struct weston_surface *es, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 732 | pixman_region32_t overlap, surface_overlap; |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 733 | int prev_plane; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Find a surface for each sprite in the output using some heuristics: |
| 737 | * 1) size |
| 738 | * 2) frequency of update |
| 739 | * 3) opacity (though some hw might support alpha blending) |
| 740 | * 4) clipping (this can be fixed with color keys) |
| 741 | * |
| 742 | * The idea is to save on blitting since this should save power. |
| 743 | * If we can get a large video surface on the sprite for example, |
| 744 | * the main display surface may not need to update at all, and |
| 745 | * the client buffer can be used directly for the sprite surface |
| 746 | * as we do for flipping full screen surfaces. |
| 747 | */ |
| 748 | pixman_region32_init(&overlap); |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 749 | drm_output->cursor_free = 1; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 750 | wl_list_for_each_safe(es, next, &c->base.surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 751 | pixman_region32_init(&surface_overlap); |
| 752 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 753 | &es->transform.boundingbox); |
| 754 | |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 755 | prev_plane = es->plane; |
| 756 | es->plane = WESTON_PLANE_PRIMARY; |
| 757 | if (pixman_region32_not_empty(&surface_overlap)) |
| 758 | goto bail; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 759 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 760 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 761 | drm_output_set_cursor(output, es); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 762 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 763 | drm_output_prepare_scanout_surface(output, es); |
| 764 | if (es->plane == WESTON_PLANE_PRIMARY) |
| 765 | drm_output_prepare_overlay_surface(output, es); |
| 766 | |
| 767 | bail: |
| 768 | if (es->plane == WESTON_PLANE_PRIMARY) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 769 | pixman_region32_union(&overlap, &overlap, |
| 770 | &es->transform.boundingbox); |
Kristian Høgsberg | 6143f7d | 2012-07-14 00:31:32 -0400 | [diff] [blame] | 771 | |
| 772 | if (prev_plane != WESTON_PLANE_PRIMARY && |
| 773 | es->plane == WESTON_PLANE_PRIMARY) |
| 774 | weston_surface_damage(es); |
| 775 | else if (prev_plane == WESTON_PLANE_PRIMARY && |
| 776 | es->plane != WESTON_PLANE_PRIMARY) |
| 777 | weston_surface_damage_below(es); |
| 778 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 779 | pixman_region32_fini(&surface_overlap); |
| 780 | } |
| 781 | pixman_region32_fini(&overlap); |
| 782 | |
Kristian Høgsberg | 4901f6c | 2012-07-14 01:24:58 -0400 | [diff] [blame] | 783 | if (drm_output->cursor_free) |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 784 | drmModeSetCursor(c->drm.fd, drm_output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | f2735ea | 2012-06-20 23:03:53 -0400 | [diff] [blame] | 785 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 786 | drm_disable_unused_sprites(output); |
| 787 | } |
| 788 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 789 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 790 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 791 | { |
| 792 | struct drm_output *output = (struct drm_output *) output_base; |
| 793 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 794 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 795 | drmModeCrtcPtr origcrtc = output->original_crtc; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 796 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 797 | if (output->backlight) |
| 798 | backlight_destroy(output->backlight); |
| 799 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 800 | /* Turn off hardware cursor */ |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 801 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 802 | |
| 803 | /* Restore original CRTC state */ |
| 804 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 805 | origcrtc->x, origcrtc->y, |
| 806 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 807 | drmModeFreeCrtc(origcrtc); |
| 808 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 809 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 810 | c->connector_allocator &= ~(1 << output->connector_id); |
| 811 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 812 | eglDestroySurface(c->base.egl_display, output->egl_surface); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 813 | gbm_surface_destroy(output->surface); |
| 814 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 815 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 816 | wl_list_remove(&output->base.link); |
| 817 | |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 818 | free(output->name); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 819 | free(output); |
| 820 | } |
| 821 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 822 | static struct drm_mode * |
| 823 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 824 | { |
| 825 | struct drm_mode *tmp_mode = NULL, *mode; |
| 826 | |
| 827 | if (output->base.current->width == target_mode->width && |
| 828 | output->base.current->height == target_mode->height && |
| 829 | (output->base.current->refresh == target_mode->refresh || |
| 830 | target_mode->refresh == 0)) |
| 831 | return (struct drm_mode *)output->base.current; |
| 832 | |
| 833 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 834 | if (mode->mode_info.hdisplay == target_mode->width && |
| 835 | mode->mode_info.vdisplay == target_mode->height) { |
| 836 | if (mode->mode_info.vrefresh == target_mode->refresh || |
| 837 | target_mode->refresh == 0) { |
| 838 | return mode; |
| 839 | } else if (!tmp_mode) |
| 840 | tmp_mode = mode; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return tmp_mode; |
| 845 | } |
| 846 | |
| 847 | static int |
| 848 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 849 | { |
| 850 | struct drm_output *output; |
| 851 | struct drm_mode *drm_mode; |
| 852 | int ret; |
| 853 | struct drm_compositor *ec; |
| 854 | struct gbm_surface *surface; |
| 855 | EGLSurface egl_surface; |
| 856 | |
| 857 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 858 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 859 | return -1; |
| 860 | } |
| 861 | |
| 862 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 863 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | ec = (struct drm_compositor *)output_base->compositor; |
| 868 | output = (struct drm_output *)output_base; |
| 869 | drm_mode = choose_mode (output, mode); |
| 870 | |
| 871 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 872 | 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] | 873 | return -1; |
| 874 | } else if (&drm_mode->base == output->base.current) { |
| 875 | return 0; |
| 876 | } else if (drm_mode->base.width == output->base.current->width && |
| 877 | drm_mode->base.height == output->base.current->height) { |
| 878 | /* only change refresh value */ |
| 879 | ret = drmModeSetCrtc(ec->drm.fd, |
| 880 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 881 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 882 | &output->connector_id, 1, &drm_mode->mode_info); |
| 883 | |
| 884 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 885 | weston_log("failed to set mode (%dx%d) %u Hz\n", |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 886 | drm_mode->base.width, |
| 887 | drm_mode->base.height, |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 888 | drm_mode->base.refresh / 1000); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 889 | ret = -1; |
| 890 | } else { |
| 891 | output->base.current->flags = 0; |
| 892 | output->base.current = &drm_mode->base; |
| 893 | drm_mode->base.flags = |
| 894 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 895 | ret = 0; |
| 896 | } |
| 897 | |
| 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | drm_mode->base.flags = |
| 902 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 903 | |
| 904 | surface = gbm_surface_create(ec->gbm, |
| 905 | drm_mode->base.width, |
| 906 | drm_mode->base.height, |
| 907 | GBM_FORMAT_XRGB8888, |
| 908 | GBM_BO_USE_SCANOUT | |
| 909 | GBM_BO_USE_RENDERING); |
| 910 | if (!surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 911 | weston_log("failed to create gbm surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 912 | return -1; |
| 913 | } |
| 914 | |
| 915 | egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 916 | eglCreateWindowSurface(ec->base.egl_display, |
| 917 | ec->base.egl_config, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 918 | surface, NULL); |
| 919 | |
| 920 | if (egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 921 | weston_log("failed to create egl surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 922 | goto err; |
| 923 | } |
| 924 | |
| 925 | ret = drmModeSetCrtc(ec->drm.fd, |
| 926 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 927 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 928 | &output->connector_id, 1, &drm_mode->mode_info); |
| 929 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 930 | weston_log("failed to set mode\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 931 | goto err; |
| 932 | } |
| 933 | |
| 934 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 935 | if (output->current) { |
| 936 | if (output->current->is_client_buffer) |
| 937 | gbm_bo_destroy(output->current->bo); |
| 938 | else |
| 939 | gbm_surface_release_buffer(output->surface, |
| 940 | output->current->bo); |
| 941 | } |
| 942 | output->current = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 943 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 944 | if (output->next) { |
| 945 | if (output->next->is_client_buffer) |
| 946 | gbm_bo_destroy(output->next->bo); |
| 947 | else |
| 948 | gbm_surface_release_buffer(output->surface, |
| 949 | output->next->bo); |
| 950 | } |
| 951 | output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 952 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 953 | eglDestroySurface(ec->base.egl_display, output->egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 954 | gbm_surface_destroy(output->surface); |
| 955 | output->egl_surface = egl_surface; |
| 956 | output->surface = surface; |
| 957 | |
| 958 | /*update output*/ |
| 959 | output->base.current = &drm_mode->base; |
| 960 | output->base.dirty = 1; |
| 961 | weston_output_move(&output->base, output->base.x, output->base.y); |
| 962 | return 0; |
| 963 | |
| 964 | err: |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 965 | eglDestroySurface(ec->base.egl_display, egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 966 | gbm_surface_destroy(surface); |
| 967 | return -1; |
| 968 | } |
| 969 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 970 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 971 | on_drm_input(int fd, uint32_t mask, void *data) |
| 972 | { |
| 973 | drmEventContext evctx; |
| 974 | |
| 975 | memset(&evctx, 0, sizeof evctx); |
| 976 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 977 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 978 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 979 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 980 | |
| 981 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 985 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 986 | { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 987 | EGLint major, minor, n; |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 988 | const char *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 989 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 990 | static const EGLint context_attribs[] = { |
| 991 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 992 | EGL_NONE |
| 993 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 994 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 995 | static const EGLint config_attribs[] = { |
| 996 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 997 | EGL_RED_SIZE, 1, |
| 998 | EGL_GREEN_SIZE, 1, |
| 999 | EGL_BLUE_SIZE, 1, |
| 1000 | EGL_ALPHA_SIZE, 0, |
| 1001 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 1002 | EGL_NONE |
| 1003 | }; |
| 1004 | |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1005 | sysnum = udev_device_get_sysnum(device); |
| 1006 | if (sysnum) |
| 1007 | ec->drm.id = atoi(sysnum); |
| 1008 | if (!sysnum || ec->drm.id < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1009 | weston_log("cannot get device sysnum\n"); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1010 | return -1; |
| 1011 | } |
| 1012 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1013 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 1014 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1015 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1016 | /* Probably permissions error */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1017 | weston_log("couldn't open %s, skipping\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1018 | udev_device_get_devnode(device)); |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1022 | weston_log("using %s\n", filename); |
| 1023 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1024 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1025 | ec->gbm = gbm_create_device(ec->drm.fd); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1026 | ec->base.egl_display = eglGetDisplay(ec->gbm); |
| 1027 | if (ec->base.egl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1028 | weston_log("failed to create display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1029 | return -1; |
| 1030 | } |
| 1031 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1032 | if (!eglInitialize(ec->base.egl_display, &major, &minor)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1033 | weston_log("failed to initialize display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1034 | return -1; |
| 1035 | } |
| 1036 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1037 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1038 | weston_log("failed to bind api EGL_OPENGL_ES_API\n"); |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1039 | return -1; |
| 1040 | } |
| 1041 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1042 | if (!eglChooseConfig(ec->base.egl_display, config_attribs, |
| 1043 | &ec->base.egl_config, 1, &n) || n != 1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1044 | weston_log("failed to choose config: %d\n", n); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1045 | return -1; |
| 1046 | } |
| 1047 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1048 | ec->base.egl_context = |
| 1049 | eglCreateContext(ec->base.egl_display, ec->base.egl_config, |
| 1050 | EGL_NO_CONTEXT, context_attribs); |
| 1051 | if (ec->base.egl_context == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1052 | weston_log("failed to create context\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1053 | return -1; |
| 1054 | } |
| 1055 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1056 | ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10, |
| 1057 | GBM_FORMAT_XRGB8888, |
| 1058 | GBM_BO_USE_RENDERING); |
| 1059 | if (!ec->dummy_surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1060 | weston_log("failed to create dummy gbm surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | ec->dummy_egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1065 | eglCreateWindowSurface(ec->base.egl_display, |
| 1066 | ec->base.egl_config, |
| 1067 | ec->dummy_surface, |
| 1068 | NULL); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1069 | if (ec->dummy_egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1070 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1071 | return -1; |
| 1072 | } |
| 1073 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1074 | if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface, |
| 1075 | ec->dummy_egl_surface, ec->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1076 | weston_log("failed to make context current\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1077 | return -1; |
| 1078 | } |
| 1079 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1083 | static int |
| 1084 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 1085 | { |
| 1086 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1087 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1088 | |
| 1089 | mode = malloc(sizeof *mode); |
| 1090 | if (mode == NULL) |
| 1091 | return -1; |
| 1092 | |
| 1093 | mode->base.flags = 0; |
| 1094 | mode->base.width = info->hdisplay; |
| 1095 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1096 | |
| 1097 | /* Calculate higher precision (mHz) refresh rate */ |
| 1098 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1099 | info->vtotal / 2) / info->vtotal; |
| 1100 | |
| 1101 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1102 | refresh *= 2; |
| 1103 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1104 | refresh /= 2; |
| 1105 | if (info->vscan > 1) |
| 1106 | refresh /= info->vscan; |
| 1107 | |
| 1108 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1109 | mode->mode_info = *info; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1110 | |
| 1111 | if (info->type & DRM_MODE_TYPE_PREFERRED) |
| 1112 | mode->base.flags |= WL_OUTPUT_MODE_PREFERRED; |
| 1113 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1114 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1115 | |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | static int |
| 1120 | drm_subpixel_to_wayland(int drm_value) |
| 1121 | { |
| 1122 | switch (drm_value) { |
| 1123 | default: |
| 1124 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1125 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1126 | case DRM_MODE_SUBPIXEL_NONE: |
| 1127 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1128 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1129 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1130 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1131 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1132 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1133 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1134 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1135 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1136 | } |
| 1137 | } |
| 1138 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 1139 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1140 | sprite_handle_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1141 | { |
| 1142 | struct drm_sprite *sprite = |
| 1143 | container_of(listener, struct drm_sprite, |
| 1144 | destroy_listener); |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1145 | struct drm_compositor *compositor = sprite->compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1146 | |
| 1147 | sprite->surface = NULL; |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1148 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1149 | sprite->fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1153 | sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1154 | { |
| 1155 | struct drm_sprite *sprite = |
| 1156 | container_of(listener, struct drm_sprite, |
| 1157 | pending_destroy_listener); |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1158 | struct drm_compositor *compositor = sprite->compositor; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1159 | |
| 1160 | sprite->pending_surface = NULL; |
Ander Conselvan de Oliveira | 01a57ed | 2012-06-26 17:09:15 +0300 | [diff] [blame] | 1161 | drmModeRmFB(compositor->drm.fd, sprite->pending_fb_id); |
| 1162 | sprite->pending_fb_id = 0; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1163 | } |
| 1164 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1165 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1166 | static uint32_t |
| 1167 | drm_get_backlight(struct drm_output *output) |
| 1168 | { |
| 1169 | long brightness, max_brightness, norm; |
| 1170 | |
| 1171 | brightness = backlight_get_brightness(output->backlight); |
| 1172 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1173 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1174 | /* convert it on a scale of 0 to 255 */ |
| 1175 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1176 | |
| 1177 | return (uint32_t) norm; |
| 1178 | } |
| 1179 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1180 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1181 | static void |
| 1182 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1183 | { |
| 1184 | struct drm_output *output = (struct drm_output *) output_base; |
| 1185 | long max_brightness, new_brightness; |
| 1186 | |
| 1187 | if (!output->backlight) |
| 1188 | return; |
| 1189 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1190 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1191 | return; |
| 1192 | |
| 1193 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1194 | |
| 1195 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1196 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1197 | |
| 1198 | backlight_set_brightness(output->backlight, new_brightness); |
| 1199 | } |
| 1200 | |
| 1201 | static drmModePropertyPtr |
| 1202 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1203 | { |
| 1204 | drmModePropertyPtr props; |
| 1205 | int i; |
| 1206 | |
| 1207 | for (i = 0; i < connector->count_props; i++) { |
| 1208 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1209 | if (!props) |
| 1210 | continue; |
| 1211 | |
| 1212 | if (!strcmp(props->name, name)) |
| 1213 | return props; |
| 1214 | |
| 1215 | drmModeFreeProperty(props); |
| 1216 | } |
| 1217 | |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | static void |
| 1222 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1223 | { |
| 1224 | struct drm_output *output = (struct drm_output *) output_base; |
| 1225 | struct weston_compositor *ec = output_base->compositor; |
| 1226 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 1227 | drmModeConnectorPtr connector; |
| 1228 | drmModePropertyPtr prop; |
| 1229 | |
| 1230 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 1231 | if (!connector) |
| 1232 | return; |
| 1233 | |
| 1234 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 1235 | if (!prop) { |
| 1236 | drmModeFreeConnector(connector); |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1241 | prop->prop_id, level); |
| 1242 | drmModeFreeProperty(prop); |
| 1243 | drmModeFreeConnector(connector); |
| 1244 | } |
| 1245 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1246 | static const char *connector_type_names[] = { |
| 1247 | "None", |
| 1248 | "VGA", |
| 1249 | "DVI", |
| 1250 | "DVI", |
| 1251 | "DVI", |
| 1252 | "Composite", |
| 1253 | "TV", |
| 1254 | "LVDS", |
| 1255 | "CTV", |
| 1256 | "DIN", |
| 1257 | "DP", |
| 1258 | "HDMI", |
| 1259 | "HDMI", |
| 1260 | "TV", |
| 1261 | "eDP", |
| 1262 | }; |
| 1263 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1264 | static int |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1265 | find_crtc_for_connector(struct drm_compositor *ec, |
| 1266 | drmModeRes *resources, drmModeConnector *connector) |
| 1267 | { |
| 1268 | drmModeEncoder *encoder; |
| 1269 | uint32_t possible_crtcs; |
| 1270 | int i, j; |
| 1271 | |
| 1272 | for (j = 0; j < connector->count_encoders; j++) { |
| 1273 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[j]); |
| 1274 | if (encoder == NULL) { |
| 1275 | weston_log("Failed to get encoder.\n"); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | possible_crtcs = encoder->possible_crtcs; |
| 1279 | drmModeFreeEncoder(encoder); |
| 1280 | |
| 1281 | for (i = 0; i < resources->count_crtcs; i++) { |
| 1282 | if (possible_crtcs & (1 << i) && |
| 1283 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
| 1284 | return i; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | return -1; |
| 1289 | } |
| 1290 | |
| 1291 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1292 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1293 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1294 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1295 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1296 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1297 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1298 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1299 | struct weston_mode *m, *preferred, *current; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1300 | drmModeEncoder *encoder; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1301 | drmModeModeInfo crtc_mode; |
| 1302 | drmModeCrtc *crtc; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1303 | int i, ret; |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1304 | char name[32]; |
| 1305 | const char *type_name; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1306 | |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1307 | i = find_crtc_for_connector(ec, resources, connector); |
| 1308 | if (i < 0) { |
| 1309 | weston_log("No usable crtc/encoder pair for connector.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1310 | return -1; |
| 1311 | } |
| 1312 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1313 | output = malloc(sizeof *output); |
Kristian Høgsberg | 9ca3846 | 2012-07-26 22:44:55 -0400 | [diff] [blame] | 1314 | if (output == NULL) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1315 | return -1; |
| 1316 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1317 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1318 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1319 | output->base.make = "unknown"; |
| 1320 | output->base.model = "unknown"; |
| 1321 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1322 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1323 | if (connector->connector_type < ARRAY_LENGTH(connector_type_names)) |
| 1324 | type_name = connector_type_names[connector->connector_type]; |
| 1325 | else |
| 1326 | type_name = "UNKNOWN"; |
| 1327 | snprintf(name, 32, "%s%d", type_name, connector->connector_type_id); |
| 1328 | output->name = strdup(name); |
| 1329 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1330 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1331 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1332 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1333 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1334 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1335 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
| 1336 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1337 | /* Get the current mode on the crtc that's currently driving |
| 1338 | * this connector. */ |
| 1339 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoder_id); |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1340 | memset(&crtc_mode, 0, sizeof crtc_mode); |
| 1341 | if (encoder != NULL) { |
| 1342 | crtc = drmModeGetCrtc(ec->drm.fd, encoder->crtc_id); |
| 1343 | drmModeFreeEncoder(encoder); |
| 1344 | if (crtc == NULL) |
| 1345 | goto err_free; |
| 1346 | if (crtc->mode_valid) |
| 1347 | crtc_mode = crtc->mode; |
| 1348 | drmModeFreeCrtc(crtc); |
| 1349 | } |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1350 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1351 | for (i = 0; i < connector->count_modes; i++) { |
| 1352 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 1353 | if (ret) |
| 1354 | goto err_free; |
| 1355 | } |
| 1356 | |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1357 | preferred = NULL; |
| 1358 | current = NULL; |
| 1359 | wl_list_for_each(drm_mode, &output->base.mode_list, base.link) { |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1360 | if (!memcmp(&crtc_mode, &drm_mode->mode_info, sizeof crtc_mode)) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1361 | current = &drm_mode->base; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1362 | if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED) |
| 1363 | preferred = &drm_mode->base; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1364 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1365 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1366 | if (current == NULL && crtc_mode.clock != 0) { |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1367 | ret = drm_output_add_mode(output, &crtc_mode); |
| 1368 | if (ret) |
| 1369 | goto err_free; |
| 1370 | current = container_of(output->base.mode_list.prev, |
| 1371 | struct weston_mode, link); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1372 | } |
| 1373 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1374 | if (option_current_mode && current) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1375 | output->base.current = current; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1376 | else if (preferred) |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1377 | output->base.current = preferred; |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1378 | else if (current) |
| 1379 | output->base.current = current; |
| 1380 | |
| 1381 | if (output->base.current == NULL) { |
| 1382 | weston_log("no available modes for %s\n", output->name); |
| 1383 | goto err_free; |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1384 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1385 | |
Wang Quanxian | acb805a | 2012-07-30 18:09:46 -0400 | [diff] [blame^] | 1386 | output->base.current->flags |= WL_OUTPUT_MODE_CURRENT; |
| 1387 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1388 | output->surface = gbm_surface_create(ec->gbm, |
| 1389 | output->base.current->width, |
| 1390 | output->base.current->height, |
| 1391 | GBM_FORMAT_XRGB8888, |
| 1392 | GBM_BO_USE_SCANOUT | |
| 1393 | GBM_BO_USE_RENDERING); |
| 1394 | if (!output->surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1395 | weston_log("failed to create gbm surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1396 | goto err_free; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1399 | output->egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1400 | eglCreateWindowSurface(ec->base.egl_display, |
| 1401 | ec->base.egl_config, |
| 1402 | output->surface, |
| 1403 | NULL); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1404 | if (output->egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1405 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1406 | goto err_surface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1407 | } |
| 1408 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1409 | output->cursor_bo[0] = |
| 1410 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1411 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1412 | output->cursor_bo[1] = |
| 1413 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1414 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1415 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1416 | output->backlight = backlight_init(drm_device, |
| 1417 | connector->connector_type); |
| 1418 | if (output->backlight) { |
| 1419 | output->base.set_backlight = drm_set_backlight; |
| 1420 | output->base.backlight_current = drm_get_backlight(output); |
| 1421 | } |
| 1422 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1423 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1424 | connector->mmWidth, connector->mmHeight, |
| 1425 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1426 | |
| 1427 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1428 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 1429 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1430 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1431 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1432 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1433 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1434 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1435 | |
Kristian Høgsberg | 2f9ed71 | 2012-07-26 17:57:15 -0400 | [diff] [blame] | 1436 | weston_log("Output %s, (connector %d, crtc %d)\n", |
| 1437 | output->name, output->connector_id, output->crtc_id); |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1438 | wl_list_for_each(m, &output->base.mode_list, link) |
| 1439 | weston_log_continue(" mode %dx%d@%.1f%s%s%s\n", |
| 1440 | m->width, m->height, m->refresh / 1000.0, |
| 1441 | m->flags & WL_OUTPUT_MODE_PREFERRED ? |
| 1442 | ", preferred" : "", |
| 1443 | m->flags & WL_OUTPUT_MODE_CURRENT ? |
| 1444 | ", current" : "", |
| 1445 | connector->count_modes == 0 ? |
| 1446 | ", built-in" : ""); |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1447 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1448 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1449 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1450 | err_surface: |
| 1451 | gbm_surface_destroy(output->surface); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1452 | err_free: |
| 1453 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1454 | base.link) { |
| 1455 | wl_list_remove(&drm_mode->base.link); |
| 1456 | free(drm_mode); |
| 1457 | } |
| 1458 | |
| 1459 | drmModeFreeCrtc(output->original_crtc); |
| 1460 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1461 | ec->connector_allocator &= ~(1 << output->connector_id); |
Kristian Høgsberg | 148ef01 | 2012-07-26 23:03:57 -0400 | [diff] [blame] | 1462 | free(output->name); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1463 | free(output); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1464 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1465 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1468 | static void |
| 1469 | create_sprites(struct drm_compositor *ec) |
| 1470 | { |
| 1471 | struct drm_sprite *sprite; |
| 1472 | drmModePlaneRes *plane_res; |
| 1473 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1474 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1475 | |
| 1476 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1477 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1478 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1479 | strerror(errno)); |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1484 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1485 | if (!plane) |
| 1486 | continue; |
| 1487 | |
| 1488 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1489 | plane->count_formats)); |
| 1490 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1491 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1492 | __func__); |
| 1493 | free(plane); |
| 1494 | continue; |
| 1495 | } |
| 1496 | |
| 1497 | memset(sprite, 0, sizeof *sprite); |
| 1498 | |
| 1499 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1500 | sprite->plane_id = plane->plane_id; |
| 1501 | sprite->surface = NULL; |
| 1502 | sprite->pending_surface = NULL; |
| 1503 | sprite->fb_id = 0; |
| 1504 | sprite->pending_fb_id = 0; |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1505 | sprite->destroy_listener.notify = sprite_handle_buffer_destroy; |
| 1506 | sprite->pending_destroy_listener.notify = |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1507 | sprite_handle_pending_buffer_destroy; |
| 1508 | sprite->compositor = ec; |
| 1509 | sprite->count_formats = plane->count_formats; |
| 1510 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 1511 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1512 | drmModeFreePlane(plane); |
| 1513 | |
| 1514 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1515 | } |
| 1516 | |
| 1517 | free(plane_res->planes); |
| 1518 | free(plane_res); |
| 1519 | } |
| 1520 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1521 | static void |
| 1522 | destroy_sprites(struct drm_compositor *compositor) |
| 1523 | { |
| 1524 | struct drm_sprite *sprite, *next; |
| 1525 | struct drm_output *output; |
| 1526 | |
| 1527 | output = container_of(compositor->base.output_list.next, |
| 1528 | struct drm_output, base.link); |
| 1529 | |
| 1530 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1531 | drmModeSetPlane(compositor->drm.fd, |
| 1532 | sprite->plane_id, |
| 1533 | output->crtc_id, 0, 0, |
| 1534 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1535 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1536 | free(sprite); |
| 1537 | } |
| 1538 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1539 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1540 | static int |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1541 | create_outputs(struct drm_compositor *ec, uint32_t option_connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1542 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1543 | { |
| 1544 | drmModeConnector *connector; |
| 1545 | drmModeRes *resources; |
| 1546 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1547 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1548 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1549 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1550 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1551 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1552 | return -1; |
| 1553 | } |
| 1554 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1555 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1556 | if (!ec->crtcs) { |
| 1557 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1558 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1559 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1560 | |
| 1561 | ec->num_crtcs = resources->count_crtcs; |
| 1562 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1563 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1564 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1565 | connector = drmModeGetConnector(ec->drm.fd, |
| 1566 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1567 | if (connector == NULL) |
| 1568 | continue; |
| 1569 | |
| 1570 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1571 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1572 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1573 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1574 | connector, x, y, |
| 1575 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1576 | drmModeFreeConnector(connector); |
| 1577 | continue; |
| 1578 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1579 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1580 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1581 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1582 | link)->current->width; |
| 1583 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1584 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1585 | drmModeFreeConnector(connector); |
| 1586 | } |
| 1587 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1588 | if (wl_list_empty(&ec->base.output_list)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1589 | weston_log("No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1590 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1591 | return -1; |
| 1592 | } |
| 1593 | |
| 1594 | drmModeFreeResources(resources); |
| 1595 | |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1599 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1600 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1601 | { |
| 1602 | drmModeConnector *connector; |
| 1603 | drmModeRes *resources; |
| 1604 | struct drm_output *output, *next; |
| 1605 | int x = 0, y = 0; |
| 1606 | int x_offset = 0, y_offset = 0; |
| 1607 | uint32_t connected = 0, disconnects = 0; |
| 1608 | int i; |
| 1609 | |
| 1610 | resources = drmModeGetResources(ec->drm.fd); |
| 1611 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1612 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1613 | return; |
| 1614 | } |
| 1615 | |
| 1616 | /* collect new connects */ |
| 1617 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1618 | int connector_id = resources->connectors[i]; |
| 1619 | |
| 1620 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1621 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1622 | continue; |
| 1623 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1624 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1625 | drmModeFreeConnector(connector); |
| 1626 | continue; |
| 1627 | } |
| 1628 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1629 | connected |= (1 << connector_id); |
| 1630 | |
| 1631 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1632 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1633 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1634 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1635 | |
| 1636 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1637 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1638 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1639 | else |
| 1640 | x = 0; |
| 1641 | y = 0; |
| 1642 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1643 | connector, x, y, |
| 1644 | drm_device); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1645 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1646 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1647 | } |
| 1648 | drmModeFreeConnector(connector); |
| 1649 | } |
| 1650 | drmModeFreeResources(resources); |
| 1651 | |
| 1652 | disconnects = ec->connector_allocator & ~connected; |
| 1653 | if (disconnects) { |
| 1654 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1655 | base.link) { |
| 1656 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1657 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1658 | output->base.x - x_offset, |
| 1659 | output->base.y - y_offset); |
| 1660 | } |
| 1661 | |
| 1662 | if (disconnects & (1 << output->connector_id)) { |
| 1663 | disconnects &= ~(1 << output->connector_id); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1664 | weston_log("connector %d disconnected\n", |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1665 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1666 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1667 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | /* FIXME: handle zero outputs, without terminating */ |
| 1673 | if (ec->connector_allocator == 0) |
| 1674 | wl_display_terminate(ec->base.wl_display); |
| 1675 | } |
| 1676 | |
| 1677 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1678 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1679 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1680 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1681 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1682 | |
| 1683 | sysnum = udev_device_get_sysnum(device); |
| 1684 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1685 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1686 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1687 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 1688 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1689 | return 0; |
| 1690 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1691 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1692 | } |
| 1693 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1694 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1695 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1696 | { |
| 1697 | struct drm_compositor *ec = data; |
| 1698 | struct udev_device *event; |
| 1699 | |
| 1700 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1701 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1702 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1703 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1704 | |
| 1705 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1706 | |
| 1707 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1708 | } |
| 1709 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1710 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1711 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1712 | { |
| 1713 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1714 | struct weston_seat *seat, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1715 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1716 | wl_list_for_each_safe(seat, next, &ec->seat_list, link) |
| 1717 | evdev_input_destroy(seat); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1718 | |
| 1719 | wl_event_source_remove(d->udev_drm_source); |
| 1720 | wl_event_source_remove(d->drm_source); |
| 1721 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1722 | weston_compositor_shutdown(ec); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1723 | |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1724 | /* Work around crash in egl_dri2.c's dri2_make_current() */ |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1725 | eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1726 | EGL_NO_CONTEXT); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1727 | eglTerminate(ec->egl_display); |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1728 | eglReleaseThread(); |
| 1729 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1730 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1731 | destroy_sprites(d); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1732 | 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] | 1733 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1734 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1735 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1736 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1737 | } |
| 1738 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1739 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1740 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 1741 | { |
| 1742 | struct drm_output *output; |
| 1743 | struct drm_mode *drm_mode; |
| 1744 | int ret; |
| 1745 | |
| 1746 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 1747 | drm_mode = (struct drm_mode *) output->base.current; |
| 1748 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1749 | output->current->fb_id, 0, 0, |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1750 | &output->connector_id, 1, |
| 1751 | &drm_mode->mode_info); |
| 1752 | if (ret < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1753 | weston_log( |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1754 | "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] | 1755 | drm_mode->base.width, drm_mode->base.height, |
| 1756 | output->base.x, output->base.y); |
| 1757 | } |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1762 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1763 | { |
| 1764 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1765 | struct weston_seat *seat; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1766 | struct drm_sprite *sprite; |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1767 | struct drm_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1768 | |
| 1769 | switch (event) { |
| 1770 | case TTY_ENTER_VT: |
| 1771 | compositor->focus = 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1772 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1773 | weston_log("failed to set master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1774 | wl_display_terminate(compositor->wl_display); |
| 1775 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1776 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1777 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1778 | weston_compositor_damage_all(compositor); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1779 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1780 | evdev_add_devices(ec->udev, seat); |
| 1781 | evdev_enable_udev_monitor(ec->udev, seat); |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 1782 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1783 | break; |
| 1784 | case TTY_LEAVE_VT: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1785 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1786 | evdev_disable_udev_monitor(seat); |
| 1787 | evdev_remove_devices(seat); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 1788 | } |
| 1789 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1790 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1791 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1792 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1793 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1794 | /* If we have a repaint scheduled (either from a |
| 1795 | * pending pageflip or the idle handler), make sure we |
| 1796 | * cancel that so we don't try to pageflip when we're |
| 1797 | * vt switched away. The SLEEPING state will prevent |
| 1798 | * further attemps at repainting. When we switch |
| 1799 | * back, we schedule a repaint, which will process |
| 1800 | * pending frame callbacks. */ |
| 1801 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1802 | wl_list_for_each(output, &ec->base.output_list, base.link) { |
| 1803 | output->base.repaint_needed = 0; |
| 1804 | drmModeSetCursor(ec->drm.fd, output->crtc_id, 0, 0, 0); |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1805 | } |
| 1806 | |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1807 | output = container_of(ec->base.output_list.next, |
| 1808 | struct drm_output, base.link); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1809 | |
| 1810 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 1811 | drmModeSetPlane(ec->drm.fd, |
| 1812 | sprite->plane_id, |
Kristian Høgsberg | a6edab3 | 2012-07-14 01:06:28 -0400 | [diff] [blame] | 1813 | output->crtc_id, 0, 0, |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1814 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1815 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1816 | 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] | 1817 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1818 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1819 | break; |
| 1820 | }; |
| 1821 | } |
| 1822 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1823 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | struct drm_compositor *ec = data; |
| 1827 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1828 | tty_activate_vt(ec->tty, key - KEY_F1 + 1); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1829 | } |
| 1830 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1831 | static const char default_seat[] = "seat0"; |
| 1832 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1833 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1834 | drm_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 1835 | int connector, const char *seat, int tty, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1836 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1837 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1838 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1839 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1840 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1841 | struct udev_device *device, *drm_device; |
| 1842 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1843 | struct wl_event_loop *loop; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1844 | struct weston_seat *weston_seat, *next; |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1845 | uint32_t key; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1846 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1847 | weston_log("initializing drm backend\n"); |
| 1848 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1849 | ec = malloc(sizeof *ec); |
| 1850 | if (ec == NULL) |
| 1851 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1852 | memset(ec, 0, sizeof *ec); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1853 | |
| 1854 | if (weston_compositor_init(&ec->base, display, argc, argv, |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1855 | config_file) < 0) { |
| 1856 | weston_log("weston_compositor_init failed\n"); |
| 1857 | goto err_base; |
| 1858 | } |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1859 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1860 | ec->udev = udev_new(); |
| 1861 | if (ec->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1862 | weston_log("failed to initialize udev context\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1863 | goto err_compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1864 | } |
| 1865 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1866 | ec->base.wl_display = display; |
| 1867 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 1868 | if (!ec->tty) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1869 | weston_log("failed to initialize tty\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1870 | goto err_udev; |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1871 | } |
| 1872 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1873 | e = udev_enumerate_new(ec->udev); |
| 1874 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 1875 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1876 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1877 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1878 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1879 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1880 | path = udev_list_entry_get_name(entry); |
| 1881 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1882 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1883 | udev_device_get_property_value(device, "ID_SEAT"); |
| 1884 | if (!device_seat) |
| 1885 | device_seat = default_seat; |
| 1886 | if (strcmp(device_seat, seat) == 0) { |
| 1887 | drm_device = device; |
| 1888 | break; |
| 1889 | } |
| 1890 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1891 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1892 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1893 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1894 | weston_log("no drm device found\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1895 | goto err_udev_enum; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1896 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1897 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1898 | if (init_egl(ec, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1899 | weston_log("failed to initialize egl\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1900 | goto err_udev_dev; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1901 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1902 | |
| 1903 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1904 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1905 | ec->base.focus = 1; |
| 1906 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1907 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1908 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame] | 1909 | if (weston_compositor_init_gl(&ec->base) < 0) |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1910 | goto err_egl; |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1911 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1912 | for (key = KEY_F1; key < KEY_F9; key++) |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1913 | weston_compositor_add_key_binding(&ec->base, key, |
| 1914 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1915 | switch_vt_binding, ec); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1916 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1917 | wl_list_init(&ec->sprite_list); |
| 1918 | create_sprites(ec); |
| 1919 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1920 | if (create_outputs(ec, connector, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1921 | weston_log("failed to create output for %s\n", path); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1922 | goto err_sprite; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1923 | } |
| 1924 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1925 | path = NULL; |
| 1926 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1927 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1928 | |
| 1929 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1930 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1931 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1932 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1933 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1934 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1935 | if (ec->udev_monitor == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1936 | weston_log("failed to intialize udev monitor\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1937 | goto err_drm_source; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1938 | } |
| 1939 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1940 | "drm", NULL); |
| 1941 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1942 | wl_event_loop_add_fd(loop, |
| 1943 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1944 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1945 | |
| 1946 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1947 | weston_log("failed to enable udev-monitor receiving\n"); |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1948 | goto err_udev_monitor; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1951 | udev_device_unref(drm_device); |
| 1952 | udev_enumerate_unref(e); |
| 1953 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1954 | return &ec->base; |
Daniel Stone | a96b93c | 2012-06-22 14:04:37 +0100 | [diff] [blame] | 1955 | |
| 1956 | err_udev_monitor: |
| 1957 | wl_event_source_remove(ec->udev_drm_source); |
| 1958 | udev_monitor_unref(ec->udev_monitor); |
| 1959 | err_drm_source: |
| 1960 | wl_event_source_remove(ec->drm_source); |
| 1961 | wl_list_for_each_safe(weston_seat, next, &ec->base.seat_list, link) |
| 1962 | evdev_input_destroy(weston_seat); |
| 1963 | err_sprite: |
| 1964 | destroy_sprites(ec); |
| 1965 | err_egl: |
| 1966 | eglMakeCurrent(ec->base.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 1967 | EGL_NO_CONTEXT); |
| 1968 | eglTerminate(ec->base.egl_display); |
| 1969 | eglReleaseThread(); |
| 1970 | gbm_device_destroy(ec->gbm); |
| 1971 | err_udev_dev: |
| 1972 | udev_device_unref(drm_device); |
| 1973 | err_udev_enum: |
| 1974 | udev_enumerate_unref(e); |
| 1975 | tty_destroy(ec->tty); |
| 1976 | err_udev: |
| 1977 | udev_unref(ec->udev); |
| 1978 | err_compositor: |
| 1979 | weston_compositor_shutdown(&ec->base); |
| 1980 | err_base: |
| 1981 | free(ec); |
| 1982 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1983 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1984 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1985 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1986 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 1987 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1988 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1989 | int connector = 0, tty = 0; |
| 1990 | const char *seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1991 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1992 | const struct weston_option drm_options[] = { |
| 1993 | { WESTON_OPTION_INTEGER, "connector", 0, &connector }, |
| 1994 | { WESTON_OPTION_STRING, "seat", 0, &seat }, |
| 1995 | { WESTON_OPTION_INTEGER, "tty", 0, &tty }, |
Kristian Høgsberg | 061c425 | 2012-06-28 11:28:15 -0400 | [diff] [blame] | 1996 | { WESTON_OPTION_BOOLEAN, "current-mode", 0, &option_current_mode }, |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1997 | }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1998 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1999 | parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2000 | |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 2001 | return drm_compositor_create(display, connector, seat, tty, argc, argv, |
| 2002 | config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 2003 | } |