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