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