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