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