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