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 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 533 | if (surface_is_primary(ec, es)) |
| 534 | return -1; |
| 535 | |
Kristian Høgsberg | 7a011ea | 2012-03-26 11:48:55 -0400 | [diff] [blame] | 536 | if (es->image == EGL_NO_IMAGE_KHR) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 537 | return -1; |
| 538 | |
| 539 | if (!drm_surface_transform_supported(es)) |
| 540 | return -1; |
| 541 | |
| 542 | if (!drm_surface_overlap_supported(output_base, overlap)) |
| 543 | return -1; |
| 544 | |
| 545 | wl_list_for_each(s, &c->sprite_list, link) { |
| 546 | if (!drm_sprite_crtc_supported(output_base, s->possible_crtcs)) |
| 547 | continue; |
| 548 | |
| 549 | if (!s->pending_fb_id) { |
| 550 | found = 1; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* No sprites available */ |
| 556 | if (!found) |
| 557 | return -1; |
| 558 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 559 | bo = gbm_bo_create_from_egl_image(c->gbm, c->base.egl_display, |
| 560 | es->image, es->geometry.width, |
| 561 | es->geometry.height, |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 562 | GBM_BO_USE_SCANOUT); |
| 563 | format = gbm_bo_get_format(bo); |
| 564 | handle = gbm_bo_get_handle(bo).s32; |
| 565 | stride = gbm_bo_get_pitch(bo); |
| 566 | |
| 567 | gbm_bo_destroy(bo); |
| 568 | |
| 569 | if (!drm_surface_format_supported(s, format)) |
| 570 | return -1; |
| 571 | |
| 572 | if (!handle) |
| 573 | return -1; |
| 574 | |
| 575 | handles[0] = handle; |
| 576 | pitches[0] = stride; |
| 577 | offsets[0] = 0; |
| 578 | |
| 579 | ret = drmModeAddFB2(c->drm.fd, es->geometry.width, es->geometry.height, |
| 580 | format, handles, pitches, offsets, |
| 581 | &fb_id, 0); |
| 582 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 583 | weston_log("addfb2 failed: %d\n", ret); |
Kristian Høgsberg | 65bec24 | 2012-03-05 19:57:35 -0500 | [diff] [blame] | 584 | c->sprites_are_broken = 1; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | if (s->surface && s->surface != es) { |
| 589 | struct weston_surface *old_surf = s->surface; |
| 590 | pixman_region32_fini(&old_surf->damage); |
| 591 | pixman_region32_init_rect(&old_surf->damage, |
| 592 | old_surf->geometry.x, old_surf->geometry.y, |
| 593 | old_surf->geometry.width, old_surf->geometry.height); |
| 594 | } |
| 595 | |
| 596 | s->pending_fb_id = fb_id; |
| 597 | s->pending_surface = es; |
| 598 | es->buffer->busy_count++; |
| 599 | |
| 600 | /* |
| 601 | * Calculate the source & dest rects properly based on actual |
| 602 | * postion (note the caller has called weston_surface_update_transform() |
| 603 | * for us already). |
| 604 | */ |
| 605 | pixman_region32_init(&dest_rect); |
| 606 | pixman_region32_intersect(&dest_rect, &es->transform.boundingbox, |
| 607 | &output_base->region); |
| 608 | pixman_region32_translate(&dest_rect, -output_base->x, -output_base->y); |
| 609 | box = pixman_region32_extents(&dest_rect); |
| 610 | s->dest_x = box->x1; |
| 611 | s->dest_y = box->y1; |
| 612 | s->dest_w = box->x2 - box->x1; |
| 613 | s->dest_h = box->y2 - box->y1; |
| 614 | pixman_region32_fini(&dest_rect); |
| 615 | |
| 616 | pixman_region32_init(&src_rect); |
| 617 | pixman_region32_intersect(&src_rect, &es->transform.boundingbox, |
| 618 | &output_base->region); |
| 619 | pixman_region32_translate(&src_rect, -es->geometry.x, -es->geometry.y); |
| 620 | box = pixman_region32_extents(&src_rect); |
Rob Clark | 3c97b90 | 2012-03-11 19:48:43 -0500 | [diff] [blame] | 621 | s->src_x = box->x1 << 16; |
| 622 | s->src_y = box->y1 << 16; |
| 623 | s->src_w = (box->x2 - box->x1) << 16; |
| 624 | s->src_h = (box->y2 - box->y1) << 16; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 625 | pixman_region32_fini(&src_rect); |
| 626 | |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 627 | wl_signal_add(&es->buffer->resource.destroy_signal, |
| 628 | &s->pending_destroy_listener); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 632 | static int |
| 633 | drm_output_set_cursor(struct weston_output *output_base, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 634 | struct weston_seat *es); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 635 | |
| 636 | static void |
| 637 | weston_output_set_cursor(struct weston_output *output, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 638 | struct weston_seat *seat, |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 639 | pixman_region32_t *overlap) |
| 640 | { |
| 641 | pixman_region32_t cursor_region; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 642 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 643 | if (seat->sprite == NULL) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 644 | return; |
| 645 | |
| 646 | pixman_region32_init(&cursor_region); |
| 647 | pixman_region32_intersect(&cursor_region, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 648 | &seat->sprite->transform.boundingbox, |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 649 | &output->region); |
| 650 | |
| 651 | if (!pixman_region32_not_empty(&cursor_region)) { |
| 652 | drm_output_set_cursor(output, NULL); |
| 653 | goto out; |
| 654 | } |
| 655 | |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 656 | if (pixman_region32_not_empty(overlap) || |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 657 | drm_output_set_cursor(output, seat) < 0) { |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 658 | if (seat->sprite->plane == WESTON_PLANE_DRM_CURSOR) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 659 | weston_surface_damage(seat->sprite); |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 660 | drm_output_set_cursor(output, NULL); |
| 661 | } |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 662 | seat->sprite->plane = WESTON_PLANE_PRIMARY; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 663 | } else { |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 664 | if (seat->sprite->plane == WESTON_PLANE_PRIMARY) |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 665 | weston_surface_damage_below(seat->sprite); |
| 666 | wl_list_remove(&seat->sprite->link); |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 667 | seat->sprite->plane = WESTON_PLANE_DRM_CURSOR; |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | out: |
| 671 | pixman_region32_fini(&cursor_region); |
| 672 | } |
| 673 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 674 | static void |
| 675 | drm_assign_planes(struct weston_output *output) |
| 676 | { |
| 677 | struct weston_compositor *ec = output->compositor; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 678 | struct weston_surface *es, *next; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 679 | pixman_region32_t overlap, surface_overlap; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 680 | struct weston_seat *seat; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 681 | |
| 682 | /* |
| 683 | * Find a surface for each sprite in the output using some heuristics: |
| 684 | * 1) size |
| 685 | * 2) frequency of update |
| 686 | * 3) opacity (though some hw might support alpha blending) |
| 687 | * 4) clipping (this can be fixed with color keys) |
| 688 | * |
| 689 | * The idea is to save on blitting since this should save power. |
| 690 | * If we can get a large video surface on the sprite for example, |
| 691 | * the main display surface may not need to update at all, and |
| 692 | * the client buffer can be used directly for the sprite surface |
| 693 | * as we do for flipping full screen surfaces. |
| 694 | */ |
Kristian Høgsberg | f2735ea | 2012-06-20 23:03:53 -0400 | [diff] [blame] | 695 | seat = (struct weston_seat *) ec->seat; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 696 | pixman_region32_init(&overlap); |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 697 | wl_list_for_each_safe(es, next, &ec->surface_list, link) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 698 | /* |
| 699 | * FIXME: try to assign hw cursors here too, they're just |
| 700 | * special overlays |
| 701 | */ |
| 702 | pixman_region32_init(&surface_overlap); |
| 703 | pixman_region32_intersect(&surface_overlap, &overlap, |
| 704 | &es->transform.boundingbox); |
| 705 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 706 | if (es == seat->sprite) { |
| 707 | weston_output_set_cursor(output, seat, |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 708 | &surface_overlap); |
| 709 | |
Kristian Høgsberg | d553bfc | 2012-06-18 22:37:35 -0400 | [diff] [blame] | 710 | if (seat->sprite->plane == WESTON_PLANE_PRIMARY) |
Kristian Høgsberg | d8bf90c | 2012-02-23 23:03:14 -0500 | [diff] [blame] | 711 | pixman_region32_union(&overlap, &overlap, |
| 712 | &es->transform.boundingbox); |
| 713 | } else if (!drm_output_prepare_overlay_surface(output, es, |
| 714 | &surface_overlap)) { |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 715 | pixman_region32_fini(&es->damage); |
| 716 | pixman_region32_init(&es->damage); |
| 717 | } else { |
| 718 | pixman_region32_union(&overlap, &overlap, |
| 719 | &es->transform.boundingbox); |
| 720 | } |
| 721 | pixman_region32_fini(&surface_overlap); |
| 722 | } |
| 723 | pixman_region32_fini(&overlap); |
| 724 | |
Kristian Høgsberg | f2735ea | 2012-06-20 23:03:53 -0400 | [diff] [blame] | 725 | if (!seat->sprite || !weston_surface_is_mapped(seat->sprite)) |
| 726 | drm_output_set_cursor(output, NULL); |
| 727 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 728 | drm_disable_unused_sprites(output); |
| 729 | } |
| 730 | |
| 731 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 732 | drm_output_set_cursor(struct weston_output *output_base, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 733 | struct weston_seat *es) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 734 | { |
| 735 | struct drm_output *output = (struct drm_output *) output_base; |
| 736 | struct drm_compositor *c = |
| 737 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 738 | EGLint handle, stride; |
| 739 | int ret = -1; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 740 | struct gbm_bo *bo; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 741 | uint32_t buf[64 * 64]; |
| 742 | unsigned char *d, *s, *end; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 743 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 744 | if (es == NULL) { |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 745 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 746 | return 0; |
| 747 | } |
| 748 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 749 | if (es->sprite->buffer == NULL || |
| 750 | !wl_buffer_is_shm(es->sprite->buffer)) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 751 | goto out; |
| 752 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 753 | if (es->sprite->geometry.width > 64 || |
| 754 | es->sprite->geometry.height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 755 | goto out; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 756 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 757 | output->current_cursor ^= 1; |
| 758 | bo = output->cursor_bo[output->current_cursor]; |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 759 | if (bo == NULL) |
| 760 | goto out; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 761 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 762 | memset(buf, 0, sizeof buf); |
| 763 | d = (unsigned char *) buf; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 764 | stride = wl_shm_buffer_get_stride(es->sprite->buffer); |
| 765 | s = wl_shm_buffer_get_data(es->sprite->buffer); |
| 766 | end = s + stride * es->sprite->geometry.height; |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 767 | while (s < end) { |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 768 | memcpy(d, s, es->sprite->geometry.width * 4); |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 769 | s += stride; |
| 770 | d += 64 * 4; |
| 771 | } |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 772 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 773 | if (gbm_bo_write(bo, buf, sizeof buf) < 0) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 774 | goto out; |
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 | handle = gbm_bo_get_handle(bo).s32; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 777 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 778 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 779 | weston_log("failed to set cursor: %s\n", strerror(-ret)); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 780 | goto out; |
| 781 | } |
| 782 | |
| 783 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 784 | es->sprite->geometry.x - output->base.x, |
| 785 | es->sprite->geometry.y - output->base.y); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 786 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 787 | weston_log("failed to move cursor: %s\n", strerror(-ret)); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 788 | goto out; |
| 789 | } |
| 790 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 791 | out: |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 792 | if (ret) |
| 793 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 794 | return ret; |
| 795 | } |
| 796 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 797 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 798 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 799 | { |
| 800 | struct drm_output *output = (struct drm_output *) output_base; |
| 801 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 802 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 803 | drmModeCrtcPtr origcrtc = output->original_crtc; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 804 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 805 | if (output->backlight) |
| 806 | backlight_destroy(output->backlight); |
| 807 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 808 | /* Turn off hardware cursor */ |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 809 | drm_output_set_cursor(&output->base, NULL); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 810 | |
| 811 | /* Restore original CRTC state */ |
| 812 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 813 | origcrtc->x, origcrtc->y, |
| 814 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 815 | drmModeFreeCrtc(origcrtc); |
| 816 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 817 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 818 | c->connector_allocator &= ~(1 << output->connector_id); |
| 819 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 820 | eglDestroySurface(c->base.egl_display, output->egl_surface); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 821 | gbm_surface_destroy(output->surface); |
| 822 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 823 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 824 | wl_list_remove(&output->base.link); |
| 825 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 826 | free(output); |
| 827 | } |
| 828 | |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 829 | static struct drm_mode * |
| 830 | choose_mode (struct drm_output *output, struct weston_mode *target_mode) |
| 831 | { |
| 832 | struct drm_mode *tmp_mode = NULL, *mode; |
| 833 | |
| 834 | if (output->base.current->width == target_mode->width && |
| 835 | output->base.current->height == target_mode->height && |
| 836 | (output->base.current->refresh == target_mode->refresh || |
| 837 | target_mode->refresh == 0)) |
| 838 | return (struct drm_mode *)output->base.current; |
| 839 | |
| 840 | wl_list_for_each(mode, &output->base.mode_list, base.link) { |
| 841 | if (mode->mode_info.hdisplay == target_mode->width && |
| 842 | mode->mode_info.vdisplay == target_mode->height) { |
| 843 | if (mode->mode_info.vrefresh == target_mode->refresh || |
| 844 | target_mode->refresh == 0) { |
| 845 | return mode; |
| 846 | } else if (!tmp_mode) |
| 847 | tmp_mode = mode; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | return tmp_mode; |
| 852 | } |
| 853 | |
| 854 | static int |
| 855 | drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode) |
| 856 | { |
| 857 | struct drm_output *output; |
| 858 | struct drm_mode *drm_mode; |
| 859 | int ret; |
| 860 | struct drm_compositor *ec; |
| 861 | struct gbm_surface *surface; |
| 862 | EGLSurface egl_surface; |
| 863 | |
| 864 | if (output_base == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 865 | weston_log("output is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | if (mode == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 870 | weston_log("mode is NULL.\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 871 | return -1; |
| 872 | } |
| 873 | |
| 874 | ec = (struct drm_compositor *)output_base->compositor; |
| 875 | output = (struct drm_output *)output_base; |
| 876 | drm_mode = choose_mode (output, mode); |
| 877 | |
| 878 | if (!drm_mode) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 879 | 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] | 880 | return -1; |
| 881 | } else if (&drm_mode->base == output->base.current) { |
| 882 | return 0; |
| 883 | } else if (drm_mode->base.width == output->base.current->width && |
| 884 | drm_mode->base.height == output->base.current->height) { |
| 885 | /* only change refresh value */ |
| 886 | ret = drmModeSetCrtc(ec->drm.fd, |
| 887 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 888 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 889 | &output->connector_id, 1, &drm_mode->mode_info); |
| 890 | |
| 891 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 892 | weston_log("failed to set mode (%dx%d) %u Hz\n", |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 893 | drm_mode->base.width, |
| 894 | drm_mode->base.height, |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 895 | drm_mode->base.refresh / 1000); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 896 | ret = -1; |
| 897 | } else { |
| 898 | output->base.current->flags = 0; |
| 899 | output->base.current = &drm_mode->base; |
| 900 | drm_mode->base.flags = |
| 901 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 902 | ret = 0; |
| 903 | } |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | drm_mode->base.flags = |
| 909 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 910 | |
| 911 | surface = gbm_surface_create(ec->gbm, |
| 912 | drm_mode->base.width, |
| 913 | drm_mode->base.height, |
| 914 | GBM_FORMAT_XRGB8888, |
| 915 | GBM_BO_USE_SCANOUT | |
| 916 | GBM_BO_USE_RENDERING); |
| 917 | if (!surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 918 | weston_log("failed to create gbm surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 923 | eglCreateWindowSurface(ec->base.egl_display, |
| 924 | ec->base.egl_config, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 925 | surface, NULL); |
| 926 | |
| 927 | if (egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 928 | weston_log("failed to create egl surface\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 929 | goto err; |
| 930 | } |
| 931 | |
| 932 | ret = drmModeSetCrtc(ec->drm.fd, |
| 933 | output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 934 | output->current->fb_id, 0, 0, |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 935 | &output->connector_id, 1, &drm_mode->mode_info); |
| 936 | if (ret) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 937 | weston_log("failed to set mode\n"); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 938 | goto err; |
| 939 | } |
| 940 | |
| 941 | /* reset rendering stuff. */ |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 942 | if (output->current) { |
| 943 | if (output->current->is_client_buffer) |
| 944 | gbm_bo_destroy(output->current->bo); |
| 945 | else |
| 946 | gbm_surface_release_buffer(output->surface, |
| 947 | output->current->bo); |
| 948 | } |
| 949 | output->current = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 950 | |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 951 | if (output->next) { |
| 952 | if (output->next->is_client_buffer) |
| 953 | gbm_bo_destroy(output->next->bo); |
| 954 | else |
| 955 | gbm_surface_release_buffer(output->surface, |
| 956 | output->next->bo); |
| 957 | } |
| 958 | output->next = NULL; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 959 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 960 | eglDestroySurface(ec->base.egl_display, output->egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 961 | gbm_surface_destroy(output->surface); |
| 962 | output->egl_surface = egl_surface; |
| 963 | output->surface = surface; |
| 964 | |
| 965 | /*update output*/ |
| 966 | output->base.current = &drm_mode->base; |
| 967 | output->base.dirty = 1; |
| 968 | weston_output_move(&output->base, output->base.x, output->base.y); |
| 969 | return 0; |
| 970 | |
| 971 | err: |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 972 | eglDestroySurface(ec->base.egl_display, egl_surface); |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 973 | gbm_surface_destroy(surface); |
| 974 | return -1; |
| 975 | } |
| 976 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 977 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 978 | on_drm_input(int fd, uint32_t mask, void *data) |
| 979 | { |
| 980 | drmEventContext evctx; |
| 981 | |
| 982 | memset(&evctx, 0, sizeof evctx); |
| 983 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 984 | evctx.page_flip_handler = page_flip_handler; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 985 | evctx.vblank_handler = vblank_handler; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 986 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 987 | |
| 988 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 992 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 993 | { |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 994 | EGLint major, minor, n; |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 995 | const char *filename, *sysnum; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 996 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 997 | static const EGLint context_attribs[] = { |
| 998 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 999 | EGL_NONE |
| 1000 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1001 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1002 | static const EGLint config_attribs[] = { |
| 1003 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 1004 | EGL_RED_SIZE, 1, |
| 1005 | EGL_GREEN_SIZE, 1, |
| 1006 | EGL_BLUE_SIZE, 1, |
| 1007 | EGL_ALPHA_SIZE, 0, |
| 1008 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 1009 | EGL_NONE |
| 1010 | }; |
| 1011 | |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1012 | sysnum = udev_device_get_sysnum(device); |
| 1013 | if (sysnum) |
| 1014 | ec->drm.id = atoi(sysnum); |
| 1015 | if (!sysnum || ec->drm.id < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1016 | weston_log("cannot get device sysnum\n"); |
Kristian Høgsberg | b71302e | 2012-05-10 12:28:35 -0400 | [diff] [blame] | 1017 | return -1; |
| 1018 | } |
| 1019 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1020 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 1021 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 1022 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1023 | /* Probably permissions error */ |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1024 | weston_log("couldn't open %s, skipping\n", |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1025 | udev_device_get_devnode(device)); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1029 | weston_log("using %s\n", filename); |
| 1030 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1031 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 1032 | ec->gbm = gbm_create_device(ec->drm.fd); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1033 | ec->base.egl_display = eglGetDisplay(ec->gbm); |
| 1034 | if (ec->base.egl_display == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1035 | weston_log("failed to create display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1036 | return -1; |
| 1037 | } |
| 1038 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1039 | if (!eglInitialize(ec->base.egl_display, &major, &minor)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1040 | weston_log("failed to initialize display\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1041 | return -1; |
| 1042 | } |
| 1043 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1044 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1045 | weston_log("failed to bind api EGL_OPENGL_ES_API\n"); |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 1046 | return -1; |
| 1047 | } |
| 1048 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1049 | if (!eglChooseConfig(ec->base.egl_display, config_attribs, |
| 1050 | &ec->base.egl_config, 1, &n) || n != 1) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1051 | weston_log("failed to choose config: %d\n", n); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1052 | return -1; |
| 1053 | } |
| 1054 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1055 | ec->base.egl_context = |
| 1056 | eglCreateContext(ec->base.egl_display, ec->base.egl_config, |
| 1057 | EGL_NO_CONTEXT, context_attribs); |
| 1058 | if (ec->base.egl_context == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1059 | weston_log("failed to create context\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1060 | return -1; |
| 1061 | } |
| 1062 | |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1063 | ec->dummy_surface = gbm_surface_create(ec->gbm, 10, 10, |
| 1064 | GBM_FORMAT_XRGB8888, |
| 1065 | GBM_BO_USE_RENDERING); |
| 1066 | if (!ec->dummy_surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1067 | weston_log("failed to create dummy gbm surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1068 | return -1; |
| 1069 | } |
| 1070 | |
| 1071 | ec->dummy_egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1072 | eglCreateWindowSurface(ec->base.egl_display, |
| 1073 | ec->base.egl_config, |
| 1074 | ec->dummy_surface, |
| 1075 | NULL); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1076 | if (ec->dummy_egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1077 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | b5ef591 | 2012-03-28 22:53:49 -0400 | [diff] [blame] | 1078 | return -1; |
| 1079 | } |
| 1080 | |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1081 | if (!eglMakeCurrent(ec->base.egl_display, ec->dummy_egl_surface, |
| 1082 | ec->dummy_egl_surface, ec->base.egl_context)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1083 | weston_log("failed to make context current\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1084 | return -1; |
| 1085 | } |
| 1086 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | static drmModeModeInfo builtin_1024x768 = { |
| 1091 | 63500, /* clock */ |
| 1092 | 1024, 1072, 1176, 1328, 0, |
| 1093 | 768, 771, 775, 798, 0, |
| 1094 | 59920, |
| 1095 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 1096 | 0, |
| 1097 | "1024x768" |
| 1098 | }; |
| 1099 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1100 | |
| 1101 | static int |
| 1102 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 1103 | { |
| 1104 | struct drm_mode *mode; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1105 | uint64_t refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1106 | |
| 1107 | mode = malloc(sizeof *mode); |
| 1108 | if (mode == NULL) |
| 1109 | return -1; |
| 1110 | |
| 1111 | mode->base.flags = 0; |
| 1112 | mode->base.width = info->hdisplay; |
| 1113 | mode->base.height = info->vdisplay; |
Kristian Høgsberg | c4621b0 | 2012-05-10 12:23:53 -0400 | [diff] [blame] | 1114 | |
| 1115 | /* Calculate higher precision (mHz) refresh rate */ |
| 1116 | refresh = (info->clock * 1000000LL / info->htotal + |
| 1117 | info->vtotal / 2) / info->vtotal; |
| 1118 | |
| 1119 | if (info->flags & DRM_MODE_FLAG_INTERLACE) |
| 1120 | refresh *= 2; |
| 1121 | if (info->flags & DRM_MODE_FLAG_DBLSCAN) |
| 1122 | refresh /= 2; |
| 1123 | if (info->vscan > 1) |
| 1124 | refresh /= info->vscan; |
| 1125 | |
| 1126 | mode->base.refresh = refresh; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1127 | mode->mode_info = *info; |
| 1128 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | static int |
| 1134 | drm_subpixel_to_wayland(int drm_value) |
| 1135 | { |
| 1136 | switch (drm_value) { |
| 1137 | default: |
| 1138 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 1139 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 1140 | case DRM_MODE_SUBPIXEL_NONE: |
| 1141 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 1142 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 1143 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 1144 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 1145 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 1146 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 1147 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 1148 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 1149 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 1150 | } |
| 1151 | } |
| 1152 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 1153 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1154 | sprite_handle_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1155 | { |
| 1156 | struct drm_sprite *sprite = |
| 1157 | container_of(listener, struct drm_sprite, |
| 1158 | destroy_listener); |
| 1159 | |
| 1160 | sprite->surface = NULL; |
| 1161 | } |
| 1162 | |
| 1163 | static void |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1164 | sprite_handle_pending_buffer_destroy(struct wl_listener *listener, void *data) |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1165 | { |
| 1166 | struct drm_sprite *sprite = |
| 1167 | container_of(listener, struct drm_sprite, |
| 1168 | pending_destroy_listener); |
| 1169 | |
| 1170 | sprite->pending_surface = NULL; |
| 1171 | } |
| 1172 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1173 | /* returns a value between 0-255 range, where higher is brighter */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1174 | static uint32_t |
| 1175 | drm_get_backlight(struct drm_output *output) |
| 1176 | { |
| 1177 | long brightness, max_brightness, norm; |
| 1178 | |
| 1179 | brightness = backlight_get_brightness(output->backlight); |
| 1180 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1181 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1182 | /* convert it on a scale of 0 to 255 */ |
| 1183 | norm = (brightness * 255)/(max_brightness); |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1184 | |
| 1185 | return (uint32_t) norm; |
| 1186 | } |
| 1187 | |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1188 | /* values accepted are between 0-255 range */ |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1189 | static void |
| 1190 | drm_set_backlight(struct weston_output *output_base, uint32_t value) |
| 1191 | { |
| 1192 | struct drm_output *output = (struct drm_output *) output_base; |
| 1193 | long max_brightness, new_brightness; |
| 1194 | |
| 1195 | if (!output->backlight) |
| 1196 | return; |
| 1197 | |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1198 | if (value > 255) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1199 | return; |
| 1200 | |
| 1201 | max_brightness = backlight_get_max_brightness(output->backlight); |
| 1202 | |
| 1203 | /* get denormalized value */ |
Tiago Vignatti | 5ab91ad | 2012-03-12 19:40:09 -0300 | [diff] [blame] | 1204 | new_brightness = (value * max_brightness) / 255; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1205 | |
| 1206 | backlight_set_brightness(output->backlight, new_brightness); |
| 1207 | } |
| 1208 | |
| 1209 | static drmModePropertyPtr |
| 1210 | drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name) |
| 1211 | { |
| 1212 | drmModePropertyPtr props; |
| 1213 | int i; |
| 1214 | |
| 1215 | for (i = 0; i < connector->count_props; i++) { |
| 1216 | props = drmModeGetProperty(fd, connector->props[i]); |
| 1217 | if (!props) |
| 1218 | continue; |
| 1219 | |
| 1220 | if (!strcmp(props->name, name)) |
| 1221 | return props; |
| 1222 | |
| 1223 | drmModeFreeProperty(props); |
| 1224 | } |
| 1225 | |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
| 1229 | static void |
| 1230 | drm_set_dpms(struct weston_output *output_base, enum dpms_enum level) |
| 1231 | { |
| 1232 | struct drm_output *output = (struct drm_output *) output_base; |
| 1233 | struct weston_compositor *ec = output_base->compositor; |
| 1234 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 1235 | drmModeConnectorPtr connector; |
| 1236 | drmModePropertyPtr prop; |
| 1237 | |
| 1238 | connector = drmModeGetConnector(c->drm.fd, output->connector_id); |
| 1239 | if (!connector) |
| 1240 | return; |
| 1241 | |
| 1242 | prop = drm_get_prop(c->drm.fd, connector, "DPMS"); |
| 1243 | if (!prop) { |
| 1244 | drmModeFreeConnector(connector); |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | drmModeConnectorSetProperty(c->drm.fd, connector->connector_id, |
| 1249 | prop->prop_id, level); |
| 1250 | drmModeFreeProperty(prop); |
| 1251 | drmModeFreeConnector(connector); |
| 1252 | } |
| 1253 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1254 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1255 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1256 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1257 | drmModeConnector *connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1258 | int x, int y, struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1259 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1260 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1261 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1262 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1263 | int i, ret; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1264 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1265 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1266 | if (encoder == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1267 | weston_log("No encoder for connector.\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1268 | return -1; |
| 1269 | } |
| 1270 | |
| 1271 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 1272 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1273 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1274 | break; |
| 1275 | } |
| 1276 | if (i == resources->count_crtcs) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1277 | weston_log("No usable crtc for encoder.\n"); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1278 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1279 | return -1; |
| 1280 | } |
| 1281 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1282 | output = malloc(sizeof *output); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1283 | if (output == NULL) { |
| 1284 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1285 | return -1; |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 1286 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1287 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1288 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1289 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 1290 | output->base.make = "unknown"; |
| 1291 | output->base.model = "unknown"; |
| 1292 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1293 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1294 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1295 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1296 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1297 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1298 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1299 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1300 | drmModeFreeEncoder(encoder); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1301 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1302 | for (i = 0; i < connector->count_modes; i++) { |
| 1303 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 1304 | if (ret) |
| 1305 | goto err_free; |
| 1306 | } |
| 1307 | |
| 1308 | if (connector->count_modes == 0) { |
| 1309 | ret = drm_output_add_mode(output, &builtin_1024x768); |
| 1310 | if (ret) |
| 1311 | goto err_free; |
| 1312 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1313 | |
| 1314 | drm_mode = container_of(output->base.mode_list.next, |
| 1315 | struct drm_mode, base.link); |
| 1316 | output->base.current = &drm_mode->base; |
| 1317 | drm_mode->base.flags = |
| 1318 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 1319 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1320 | output->surface = gbm_surface_create(ec->gbm, |
| 1321 | output->base.current->width, |
| 1322 | output->base.current->height, |
| 1323 | GBM_FORMAT_XRGB8888, |
| 1324 | GBM_BO_USE_SCANOUT | |
| 1325 | GBM_BO_USE_RENDERING); |
| 1326 | if (!output->surface) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1327 | weston_log("failed to create gbm surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1328 | goto err_free; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1329 | } |
| 1330 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1331 | output->egl_surface = |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1332 | eglCreateWindowSurface(ec->base.egl_display, |
| 1333 | ec->base.egl_config, |
| 1334 | output->surface, |
| 1335 | NULL); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1336 | if (output->egl_surface == EGL_NO_SURFACE) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1337 | weston_log("failed to create egl surface\n"); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1338 | goto err_surface; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
Kristian Høgsberg | 8e1f77f | 2012-05-03 11:39:35 -0400 | [diff] [blame] | 1341 | output->cursor_bo[0] = |
| 1342 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1343 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1344 | output->cursor_bo[1] = |
| 1345 | gbm_bo_create(ec->gbm, 64, 64, GBM_FORMAT_ARGB8888, |
| 1346 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_WRITE); |
| 1347 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1348 | output->backlight = backlight_init(drm_device, |
| 1349 | connector->connector_type); |
| 1350 | if (output->backlight) { |
| 1351 | output->base.set_backlight = drm_set_backlight; |
| 1352 | output->base.backlight_current = drm_get_backlight(output); |
| 1353 | } |
| 1354 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1355 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1356 | connector->mmWidth, connector->mmHeight, |
| 1357 | WL_OUTPUT_FLIPPED); |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 1358 | |
| 1359 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 1360 | |
Alex Wu | bd3354b | 2012-04-17 17:20:49 +0800 | [diff] [blame] | 1361 | output->base.origin = output->base.current; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 1362 | output->base.repaint = drm_output_repaint; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1363 | output->base.destroy = drm_output_destroy; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1364 | output->base.assign_planes = drm_assign_planes; |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1365 | output->base.set_dpms = drm_set_dpms; |
Alex Wu | b7b8bda | 2012-04-17 17:20:48 +0800 | [diff] [blame] | 1366 | output->base.switch_mode = drm_output_switch_mode; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1367 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1368 | weston_log("kms connector %d, crtc %d at mode %dx%d@%.1f\n", |
| 1369 | output->connector_id, output->crtc_id, |
| 1370 | output->base.current->width, |
| 1371 | output->base.current->height, |
| 1372 | output->base.current->refresh / 1000.0); |
| 1373 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1374 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1375 | |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1376 | err_surface: |
| 1377 | gbm_surface_destroy(output->surface); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1378 | err_free: |
| 1379 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 1380 | base.link) { |
| 1381 | wl_list_remove(&drm_mode->base.link); |
| 1382 | free(drm_mode); |
| 1383 | } |
| 1384 | |
| 1385 | drmModeFreeCrtc(output->original_crtc); |
| 1386 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 1387 | ec->connector_allocator &= ~(1 << output->connector_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1388 | free(output); |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1389 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 1390 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1393 | static void |
| 1394 | create_sprites(struct drm_compositor *ec) |
| 1395 | { |
| 1396 | struct drm_sprite *sprite; |
| 1397 | drmModePlaneRes *plane_res; |
| 1398 | drmModePlane *plane; |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1399 | uint32_t i; |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1400 | |
| 1401 | plane_res = drmModeGetPlaneResources(ec->drm.fd); |
| 1402 | if (!plane_res) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1403 | weston_log("failed to get plane resources: %s\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1404 | strerror(errno)); |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | for (i = 0; i < plane_res->count_planes; i++) { |
| 1409 | plane = drmModeGetPlane(ec->drm.fd, plane_res->planes[i]); |
| 1410 | if (!plane) |
| 1411 | continue; |
| 1412 | |
| 1413 | sprite = malloc(sizeof(*sprite) + ((sizeof(uint32_t)) * |
| 1414 | plane->count_formats)); |
| 1415 | if (!sprite) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1416 | weston_log("%s: out of memory\n", |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1417 | __func__); |
| 1418 | free(plane); |
| 1419 | continue; |
| 1420 | } |
| 1421 | |
| 1422 | memset(sprite, 0, sizeof *sprite); |
| 1423 | |
| 1424 | sprite->possible_crtcs = plane->possible_crtcs; |
| 1425 | sprite->plane_id = plane->plane_id; |
| 1426 | sprite->surface = NULL; |
| 1427 | sprite->pending_surface = NULL; |
| 1428 | sprite->fb_id = 0; |
| 1429 | sprite->pending_fb_id = 0; |
Kristian Høgsberg | 27e3052 | 2012-04-11 23:18:23 -0400 | [diff] [blame] | 1430 | sprite->destroy_listener.notify = sprite_handle_buffer_destroy; |
| 1431 | sprite->pending_destroy_listener.notify = |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1432 | sprite_handle_pending_buffer_destroy; |
| 1433 | sprite->compositor = ec; |
| 1434 | sprite->count_formats = plane->count_formats; |
| 1435 | memcpy(sprite->formats, plane->formats, |
Rob Clark | 8efbc8e | 2012-03-11 19:48:44 -0500 | [diff] [blame] | 1436 | plane->count_formats * sizeof(plane->formats[0])); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1437 | drmModeFreePlane(plane); |
| 1438 | |
| 1439 | wl_list_insert(&ec->sprite_list, &sprite->link); |
| 1440 | } |
| 1441 | |
| 1442 | free(plane_res->planes); |
| 1443 | free(plane_res); |
| 1444 | } |
| 1445 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1446 | static void |
| 1447 | destroy_sprites(struct drm_compositor *compositor) |
| 1448 | { |
| 1449 | struct drm_sprite *sprite, *next; |
| 1450 | struct drm_output *output; |
| 1451 | |
| 1452 | output = container_of(compositor->base.output_list.next, |
| 1453 | struct drm_output, base.link); |
| 1454 | |
| 1455 | wl_list_for_each_safe(sprite, next, &compositor->sprite_list, link) { |
| 1456 | drmModeSetPlane(compositor->drm.fd, |
| 1457 | sprite->plane_id, |
| 1458 | output->crtc_id, 0, 0, |
| 1459 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1460 | drmModeRmFB(compositor->drm.fd, sprite->fb_id); |
| 1461 | free(sprite); |
| 1462 | } |
| 1463 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1464 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1465 | static int |
Kristian Høgsberg | 875ab9e | 2012-03-30 11:52:39 -0400 | [diff] [blame] | 1466 | create_outputs(struct drm_compositor *ec, uint32_t option_connector, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1467 | struct udev_device *drm_device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1468 | { |
| 1469 | drmModeConnector *connector; |
| 1470 | drmModeRes *resources; |
| 1471 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1472 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1473 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1474 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1475 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1476 | weston_log("drmModeGetResources failed\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1477 | return -1; |
| 1478 | } |
| 1479 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1480 | ec->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t)); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1481 | if (!ec->crtcs) { |
| 1482 | drmModeFreeResources(resources); |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1483 | return -1; |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1484 | } |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1485 | |
| 1486 | ec->num_crtcs = resources->count_crtcs; |
| 1487 | memcpy(ec->crtcs, resources->crtcs, sizeof(uint32_t) * ec->num_crtcs); |
| 1488 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1489 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1490 | connector = drmModeGetConnector(ec->drm.fd, |
| 1491 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1492 | if (connector == NULL) |
| 1493 | continue; |
| 1494 | |
| 1495 | if (connector->connection == DRM_MODE_CONNECTED && |
| 1496 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1497 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1498 | if (create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1499 | connector, x, y, |
| 1500 | drm_device) < 0) { |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 1501 | drmModeFreeConnector(connector); |
| 1502 | continue; |
| 1503 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1504 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1505 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1506 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 1507 | link)->current->width; |
| 1508 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 1509 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1510 | drmModeFreeConnector(connector); |
| 1511 | } |
| 1512 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1513 | if (wl_list_empty(&ec->base.output_list)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1514 | weston_log("No currently active connector found.\n"); |
Christopher Michael | eb866cd | 2012-03-07 14:55:21 -0500 | [diff] [blame] | 1515 | drmModeFreeResources(resources); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1516 | return -1; |
| 1517 | } |
| 1518 | |
| 1519 | drmModeFreeResources(resources); |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1524 | static void |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1525 | update_outputs(struct drm_compositor *ec, struct udev_device *drm_device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1526 | { |
| 1527 | drmModeConnector *connector; |
| 1528 | drmModeRes *resources; |
| 1529 | struct drm_output *output, *next; |
| 1530 | int x = 0, y = 0; |
| 1531 | int x_offset = 0, y_offset = 0; |
| 1532 | uint32_t connected = 0, disconnects = 0; |
| 1533 | int i; |
| 1534 | |
| 1535 | resources = drmModeGetResources(ec->drm.fd); |
| 1536 | if (!resources) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1537 | weston_log("drmModeGetResources failed\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | |
| 1541 | /* collect new connects */ |
| 1542 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1543 | int connector_id = resources->connectors[i]; |
| 1544 | |
| 1545 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1546 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1547 | continue; |
| 1548 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 1549 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 1550 | drmModeFreeConnector(connector); |
| 1551 | continue; |
| 1552 | } |
| 1553 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1554 | connected |= (1 << connector_id); |
| 1555 | |
| 1556 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1557 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1558 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1559 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1560 | |
| 1561 | /* XXX: not yet needed, we die with 0 outputs */ |
| 1562 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1563 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1564 | else |
| 1565 | x = 0; |
| 1566 | y = 0; |
| 1567 | create_output_for_connector(ec, resources, |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1568 | connector, x, y, |
| 1569 | drm_device); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1570 | weston_log("connector %d connected\n", connector_id); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1571 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1572 | } |
| 1573 | drmModeFreeConnector(connector); |
| 1574 | } |
| 1575 | drmModeFreeResources(resources); |
| 1576 | |
| 1577 | disconnects = ec->connector_allocator & ~connected; |
| 1578 | if (disconnects) { |
| 1579 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 1580 | base.link) { |
| 1581 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1582 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1583 | output->base.x - x_offset, |
| 1584 | output->base.y - y_offset); |
| 1585 | } |
| 1586 | |
| 1587 | if (disconnects & (1 << output->connector_id)) { |
| 1588 | disconnects &= ~(1 << output->connector_id); |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1589 | weston_log("connector %d disconnected\n", |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1590 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 1591 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 1592 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | /* FIXME: handle zero outputs, without terminating */ |
| 1598 | if (ec->connector_allocator == 0) |
| 1599 | wl_display_terminate(ec->base.wl_display); |
| 1600 | } |
| 1601 | |
| 1602 | static int |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1603 | udev_event_is_hotplug(struct drm_compositor *ec, struct udev_device *device) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1604 | { |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1605 | const char *sysnum; |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1606 | const char *val; |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1607 | |
| 1608 | sysnum = udev_device_get_sysnum(device); |
| 1609 | if (!sysnum || atoi(sysnum) != ec->drm.id) |
| 1610 | return 0; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1611 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1612 | val = udev_device_get_property_value(device, "HOTPLUG"); |
| 1613 | if (!val) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1614 | return 0; |
| 1615 | |
David Herrmann | 6ac52db | 2012-03-11 20:05:22 +0100 | [diff] [blame] | 1616 | return strcmp(val, "1") == 0; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1617 | } |
| 1618 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1619 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1620 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 1621 | { |
| 1622 | struct drm_compositor *ec = data; |
| 1623 | struct udev_device *event; |
| 1624 | |
| 1625 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1626 | |
David Herrmann | d7488c2 | 2012-03-11 20:05:21 +0100 | [diff] [blame] | 1627 | if (udev_event_is_hotplug(ec, event)) |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1628 | update_outputs(ec, event); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1629 | |
| 1630 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 1631 | |
| 1632 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1633 | } |
| 1634 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1635 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1636 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1637 | { |
| 1638 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1639 | struct weston_seat *seat, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1640 | |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1641 | wl_list_for_each_safe(seat, next, &ec->seat_list, link) |
| 1642 | evdev_input_destroy(seat); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1643 | |
| 1644 | wl_event_source_remove(d->udev_drm_source); |
| 1645 | wl_event_source_remove(d->drm_source); |
| 1646 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1647 | weston_compositor_shutdown(ec); |
Jonas Ådahl | c97af92 | 2012-03-28 22:36:09 +0200 | [diff] [blame] | 1648 | |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1649 | /* Work around crash in egl_dri2.c's dri2_make_current() */ |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1650 | eglMakeCurrent(ec->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1651 | EGL_NO_CONTEXT); |
Kristian Høgsberg | 362b672 | 2012-06-18 15:13:51 -0400 | [diff] [blame] | 1652 | eglTerminate(ec->egl_display); |
Ander Conselvan de Oliveira | 5f5f319 | 2012-04-30 13:31:28 +0300 | [diff] [blame] | 1653 | eglReleaseThread(); |
| 1654 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 1655 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1656 | destroy_sprites(d); |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1657 | 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] | 1658 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1659 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1660 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 1661 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 1662 | } |
| 1663 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1664 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1665 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 1666 | { |
| 1667 | struct drm_output *output; |
| 1668 | struct drm_mode *drm_mode; |
| 1669 | int ret; |
| 1670 | |
| 1671 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 1672 | drm_mode = (struct drm_mode *) output->base.current; |
| 1673 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
Ander Conselvan de Oliveira | 555c17d | 2012-05-02 16:42:21 +0300 | [diff] [blame] | 1674 | output->current->fb_id, 0, 0, |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1675 | &output->connector_id, 1, |
| 1676 | &drm_mode->mode_info); |
| 1677 | if (ret < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1678 | weston_log( |
Kristian Høgsberg | cbcd047 | 2012-03-11 18:27:41 -0400 | [diff] [blame] | 1679 | "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] | 1680 | drm_mode->base.width, drm_mode->base.height, |
| 1681 | output->base.x, output->base.y); |
| 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1687 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1688 | { |
| 1689 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1690 | struct weston_output *output; |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1691 | struct weston_seat *seat; |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1692 | struct drm_sprite *sprite; |
| 1693 | struct drm_output *drm_output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1694 | |
| 1695 | switch (event) { |
| 1696 | case TTY_ENTER_VT: |
| 1697 | compositor->focus = 1; |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1698 | if (weston_launcher_drm_set_master(&ec->base, ec->drm.fd, 1)) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1699 | weston_log("failed to set master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1700 | wl_display_terminate(compositor->wl_display); |
| 1701 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1702 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 1703 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1704 | weston_compositor_damage_all(compositor); |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1705 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1706 | evdev_add_devices(ec->udev, seat); |
| 1707 | evdev_enable_udev_monitor(ec->udev, seat); |
Benjamin Franzke | 78d3afe | 2012-04-09 18:14:58 +0200 | [diff] [blame] | 1708 | } |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1709 | break; |
| 1710 | case TTY_LEAVE_VT: |
Daniel Stone | 37816df | 2012-05-16 18:45:18 +0100 | [diff] [blame] | 1711 | wl_list_for_each(seat, &compositor->seat_list, link) { |
| 1712 | evdev_disable_udev_monitor(seat); |
| 1713 | evdev_remove_devices(seat); |
Kristian Høgsberg | 4014a6b | 2012-04-10 00:08:45 -0400 | [diff] [blame] | 1714 | } |
| 1715 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1716 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1717 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1718 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 1719 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 1720 | /* If we have a repaint scheduled (either from a |
| 1721 | * pending pageflip or the idle handler), make sure we |
| 1722 | * cancel that so we don't try to pageflip when we're |
| 1723 | * vt switched away. The SLEEPING state will prevent |
| 1724 | * further attemps at repainting. When we switch |
| 1725 | * back, we schedule a repaint, which will process |
| 1726 | * pending frame callbacks. */ |
| 1727 | |
| 1728 | wl_list_for_each(output, &ec->base.output_list, link) { |
| 1729 | output->repaint_needed = 0; |
| 1730 | drm_output_set_cursor(output, NULL); |
| 1731 | } |
| 1732 | |
Kristian Høgsberg | 85fd327 | 2012-02-23 21:45:32 -0500 | [diff] [blame] | 1733 | drm_output = container_of(ec->base.output_list.next, |
| 1734 | struct drm_output, base.link); |
| 1735 | |
| 1736 | wl_list_for_each(sprite, &ec->sprite_list, link) |
| 1737 | drmModeSetPlane(ec->drm.fd, |
| 1738 | sprite->plane_id, |
| 1739 | drm_output->crtc_id, 0, 0, |
| 1740 | 0, 0, 0, 0, 0, 0, 0, 0); |
| 1741 | |
Benjamin Franzke | bfeda13 | 2012-01-30 14:04:04 +0100 | [diff] [blame] | 1742 | 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] | 1743 | weston_log("failed to drop master: %m\n"); |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 1744 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 1745 | break; |
| 1746 | }; |
| 1747 | } |
| 1748 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1749 | static void |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1750 | 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] | 1751 | { |
| 1752 | struct drm_compositor *ec = data; |
| 1753 | |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1754 | tty_activate_vt(ec->tty, key - KEY_F1 + 1); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1755 | } |
| 1756 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1757 | static const char default_seat[] = "seat0"; |
| 1758 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1759 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1760 | drm_compositor_create(struct wl_display *display, |
Daniel Stone | baf4359 | 2012-06-01 11:11:10 -0400 | [diff] [blame] | 1761 | int connector, const char *seat, int tty, |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1762 | int argc, char *argv[], const char *config_file) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1763 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1764 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1765 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1766 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1767 | struct udev_device *device, *drm_device; |
| 1768 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1769 | struct wl_event_loop *loop; |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1770 | uint32_t key; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1771 | |
Kristian Høgsberg | fc9c5e0 | 2012-06-08 16:45:33 -0400 | [diff] [blame] | 1772 | weston_log("initializing drm backend\n"); |
| 1773 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1774 | ec = malloc(sizeof *ec); |
| 1775 | if (ec == NULL) |
| 1776 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1777 | memset(ec, 0, sizeof *ec); |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame^] | 1778 | |
| 1779 | if (weston_compositor_init(&ec->base, display, argc, argv, |
| 1780 | config_file) < 0) |
| 1781 | return NULL; |
| 1782 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1783 | ec->udev = udev_new(); |
| 1784 | if (ec->udev == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1785 | weston_log("failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1786 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1789 | ec->base.wl_display = display; |
| 1790 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 1791 | if (!ec->tty) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1792 | weston_log("failed to initialize tty\n"); |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 1793 | free(ec); |
| 1794 | return NULL; |
| 1795 | } |
| 1796 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1797 | e = udev_enumerate_new(ec->udev); |
| 1798 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 1799 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1800 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1801 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1802 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1803 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1804 | path = udev_list_entry_get_name(entry); |
| 1805 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1806 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1807 | udev_device_get_property_value(device, "ID_SEAT"); |
| 1808 | if (!device_seat) |
| 1809 | device_seat = default_seat; |
| 1810 | if (strcmp(device_seat, seat) == 0) { |
| 1811 | drm_device = device; |
| 1812 | break; |
| 1813 | } |
| 1814 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1815 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1816 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1817 | if (drm_device == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1818 | weston_log("no drm device found\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1819 | return NULL; |
| 1820 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1821 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1822 | if (init_egl(ec, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1823 | weston_log("failed to initialize egl\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1824 | return NULL; |
| 1825 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1826 | |
| 1827 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 1828 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 1829 | ec->base.focus = 1; |
| 1830 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1831 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 1832 | |
Daniel Stone | 725c2c3 | 2012-06-22 14:04:36 +0100 | [diff] [blame^] | 1833 | if (weston_compositor_init_gl(&ec->base) < 0) |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1834 | return NULL; |
| 1835 | |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1836 | for (key = KEY_F1; key < KEY_F9; key++) |
Daniel Stone | 325fc2d | 2012-05-30 16:31:58 +0100 | [diff] [blame] | 1837 | weston_compositor_add_key_binding(&ec->base, key, |
| 1838 | MODIFIER_CTRL | MODIFIER_ALT, |
| 1839 | switch_vt_binding, ec); |
Kristian Høgsberg | 5d1c0c5 | 2012-04-10 00:11:50 -0400 | [diff] [blame] | 1840 | |
Jesse Barnes | 58ef379 | 2012-02-23 09:45:49 -0500 | [diff] [blame] | 1841 | wl_list_init(&ec->sprite_list); |
| 1842 | create_sprites(ec); |
| 1843 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1844 | if (create_outputs(ec, connector, drm_device) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1845 | weston_log("failed to create output for %s\n", path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1846 | return NULL; |
| 1847 | } |
| 1848 | |
Tiago Vignatti | 8e53c7f | 2012-02-29 19:53:50 +0200 | [diff] [blame] | 1849 | udev_device_unref(drm_device); |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1850 | udev_enumerate_unref(e); |
| 1851 | path = NULL; |
| 1852 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1853 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1854 | |
| 1855 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1856 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1857 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1858 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1859 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1860 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1861 | if (ec->udev_monitor == NULL) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1862 | weston_log("failed to intialize udev monitor\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1863 | return NULL; |
| 1864 | } |
| 1865 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1866 | "drm", NULL); |
| 1867 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1868 | wl_event_loop_add_fd(loop, |
| 1869 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1870 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1871 | |
| 1872 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
Martin Minarik | 6d11836 | 2012-06-07 18:01:59 +0200 | [diff] [blame] | 1873 | weston_log("failed to enable udev-monitor receiving\n"); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1874 | return NULL; |
| 1875 | } |
| 1876 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1877 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1878 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1879 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1880 | WL_EXPORT struct weston_compositor * |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1881 | backend_init(struct wl_display *display, int argc, char *argv[], |
| 1882 | const char *config_file) |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1883 | { |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1884 | int connector = 0, tty = 0; |
| 1885 | const char *seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1886 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1887 | const struct weston_option drm_options[] = { |
| 1888 | { WESTON_OPTION_INTEGER, "connector", 0, &connector }, |
| 1889 | { WESTON_OPTION_STRING, "seat", 0, &seat }, |
| 1890 | { WESTON_OPTION_INTEGER, "tty", 0, &tty }, |
| 1891 | }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1892 | |
Kristian Høgsberg | bcacef1 | 2012-03-11 21:05:57 -0400 | [diff] [blame] | 1893 | parse_options(drm_options, ARRAY_LENGTH(drm_options), argc, argv); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1894 | |
Daniel Stone | c1be8e5 | 2012-06-01 11:14:02 -0400 | [diff] [blame] | 1895 | return drm_compositor_create(display, connector, seat, tty, argc, argv, |
| 1896 | config_file); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1897 | } |