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 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <unistd.h> |
| 31 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 32 | #include <xf86drm.h> |
| 33 | #include <xf86drmMode.h> |
| 34 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 35 | #include <gbm.h> |
| 36 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 37 | #include "compositor.h" |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 38 | #include "evdev.h" |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 39 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 40 | struct drm_compositor { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 41 | struct weston_compositor base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 42 | |
| 43 | struct udev *udev; |
| 44 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 45 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 46 | struct udev_monitor *udev_monitor; |
| 47 | struct wl_event_source *udev_drm_source; |
| 48 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 49 | struct { |
| 50 | int fd; |
| 51 | } drm; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 52 | struct gbm_device *gbm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 53 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 54 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 55 | struct tty *tty; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 56 | |
| 57 | uint32_t prev_state; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 58 | }; |
| 59 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 60 | struct drm_mode { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 61 | struct weston_mode base; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 62 | drmModeModeInfo mode_info; |
| 63 | }; |
| 64 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 65 | struct drm_output { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 66 | struct weston_output base; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 67 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 68 | uint32_t crtc_id; |
| 69 | uint32_t connector_id; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 70 | drmModeCrtcPtr original_crtc; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 71 | GLuint rbo[2]; |
| 72 | uint32_t fb_id[2]; |
| 73 | EGLImageKHR image[2]; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 74 | struct gbm_bo *bo[2]; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 75 | uint32_t current; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 76 | |
| 77 | uint32_t fs_surf_fb_id; |
| 78 | uint32_t pending_fs_surf_fb_id; |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 79 | struct wl_buffer *scanout_buffer; |
| 80 | struct wl_listener scanout_buffer_destroy_listener; |
| 81 | struct wl_buffer *pending_scanout_buffer; |
| 82 | struct wl_listener pending_scanout_buffer_destroy_listener; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 85 | static int |
| 86 | drm_output_prepare_scanout_surface(struct drm_output *output) |
| 87 | { |
| 88 | struct drm_compositor *c = |
| 89 | (struct drm_compositor *) output->base.compositor; |
| 90 | struct weston_surface *es; |
| 91 | EGLint handle, stride; |
| 92 | int ret; |
| 93 | uint32_t fb_id = 0; |
| 94 | struct gbm_bo *bo; |
| 95 | |
| 96 | es = container_of(c->base.surface_list.next, |
| 97 | struct weston_surface, link); |
| 98 | |
Kristian Høgsberg | 101cb65 | 2012-02-17 10:45:16 -0500 | [diff] [blame^] | 99 | /* Need to verify output->region contained in surface opaque |
| 100 | * region. Or maybe just that format doesn't have alpha. */ |
| 101 | return -1; |
| 102 | |
| 103 | if (es->geometry.x != output->base.x || |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 104 | es->geometry.y != output->base.y || |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 105 | es->geometry.width != output->base.current->width || |
| 106 | es->geometry.height != output->base.current->height || |
Pekka Paalanen | f1f5b36 | 2012-01-25 14:33:33 +0200 | [diff] [blame] | 107 | es->transform.enabled || |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 108 | es->image == EGL_NO_IMAGE_KHR) |
| 109 | return -1; |
| 110 | |
| 111 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 112 | c->base.display, es->image, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 113 | es->geometry.width, |
| 114 | es->geometry.height, |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 115 | GBM_BO_USE_SCANOUT); |
| 116 | |
| 117 | handle = gbm_bo_get_handle(bo).s32; |
| 118 | stride = gbm_bo_get_pitch(bo); |
| 119 | |
| 120 | gbm_bo_destroy(bo); |
| 121 | |
| 122 | if (handle == 0) |
| 123 | return -1; |
| 124 | |
| 125 | ret = drmModeAddFB(c->drm.fd, |
| 126 | output->base.current->width, |
| 127 | output->base.current->height, |
| 128 | 24, 32, stride, handle, &fb_id); |
| 129 | |
| 130 | if (ret) |
| 131 | return -1; |
| 132 | |
| 133 | output->pending_fs_surf_fb_id = fb_id; |
| 134 | |
| 135 | /* assert output->pending_scanout_buffer == NULL */ |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 136 | output->pending_scanout_buffer = es->buffer; |
| 137 | output->pending_scanout_buffer->busy_count++; |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 138 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 139 | wl_list_insert(output->pending_scanout_buffer->resource.destroy_listener_list.prev, |
| 140 | &output->pending_scanout_buffer_destroy_listener.link); |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 141 | |
| 142 | pixman_region32_fini(&es->damage); |
| 143 | pixman_region32_init(&es->damage); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 148 | static void |
| 149 | drm_output_repaint(struct weston_output *output_base) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 150 | { |
| 151 | struct drm_output *output = (struct drm_output *) output_base; |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 152 | struct drm_compositor *compositor = |
| 153 | (struct drm_compositor *) output->base.compositor; |
| 154 | struct weston_surface *surface; |
| 155 | uint32_t fb_id = 0; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 156 | |
| 157 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 158 | GL_COLOR_ATTACHMENT0, |
| 159 | GL_RENDERBUFFER, |
| 160 | output->rbo[output->current]); |
| 161 | |
| 162 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 163 | return; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 164 | |
Kristian Høgsberg | 5f5e42e | 2012-01-25 23:59:42 -0500 | [diff] [blame] | 165 | drm_output_prepare_scanout_surface(output); |
| 166 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 167 | wl_list_for_each_reverse(surface, &compositor->base.surface_list, link) |
| 168 | weston_surface_draw(surface, &output->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 169 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 170 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 171 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 172 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 173 | |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 174 | if (output->pending_fs_surf_fb_id != 0) { |
| 175 | fb_id = output->pending_fs_surf_fb_id; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 176 | } else { |
| 177 | fb_id = output->fb_id[output->current ^ 1]; |
| 178 | } |
| 179 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 180 | if (drmModePageFlip(compositor->drm.fd, output->crtc_id, |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 181 | fb_id, |
| 182 | DRM_MODE_PAGE_FLIP_EVENT, output) < 0) { |
| 183 | fprintf(stderr, "queueing pageflip failed: %m\n"); |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 184 | return; |
Kristian Høgsberg | 54f14c3 | 2012-01-18 11:47:41 -0500 | [diff] [blame] | 185 | } |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 186 | |
Kristian Høgsberg | 06cf6b0 | 2012-01-25 23:47:45 -0500 | [diff] [blame] | 187 | return; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void |
| 191 | page_flip_handler(int fd, unsigned int frame, |
| 192 | unsigned int sec, unsigned int usec, void *data) |
| 193 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 194 | struct drm_output *output = (struct drm_output *) data; |
| 195 | struct drm_compositor *c = |
| 196 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 197 | uint32_t msecs; |
| 198 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 199 | if (output->scanout_buffer) { |
| 200 | weston_buffer_post_release(output->scanout_buffer); |
| 201 | wl_list_remove(&output->scanout_buffer_destroy_listener.link); |
| 202 | output->scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 203 | drmModeRmFB(c->drm.fd, output->fs_surf_fb_id); |
| 204 | output->fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 207 | if (output->pending_scanout_buffer) { |
| 208 | output->scanout_buffer = output->pending_scanout_buffer; |
| 209 | wl_list_remove(&output->pending_scanout_buffer_destroy_listener.link); |
| 210 | wl_list_insert(output->scanout_buffer->resource.destroy_listener_list.prev, |
| 211 | &output->scanout_buffer_destroy_listener.link); |
| 212 | output->pending_scanout_buffer = NULL; |
Kristian Høgsberg | 191454e | 2011-10-20 17:51:45 -0400 | [diff] [blame] | 213 | output->fs_surf_fb_id = output->pending_fs_surf_fb_id; |
| 214 | output->pending_fs_surf_fb_id = 0; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 217 | msecs = sec * 1000 + usec / 1000; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 218 | weston_output_finish_frame(&output->base, msecs); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 222 | drm_output_set_cursor(struct weston_output *output_base, |
| 223 | struct weston_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 224 | { |
| 225 | struct drm_output *output = (struct drm_output *) output_base; |
| 226 | struct drm_compositor *c = |
| 227 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 228 | EGLint handle, stride; |
| 229 | int ret = -1; |
| 230 | pixman_region32_t cursor_region; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 231 | struct gbm_bo *bo; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 232 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 233 | if (eid == NULL) { |
| 234 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 238 | pixman_region32_init_rect(&cursor_region, |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 239 | eid->sprite->geometry.x, |
| 240 | eid->sprite->geometry.y, |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 241 | eid->sprite->geometry.width, |
| 242 | eid->sprite->geometry.height); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 243 | |
| 244 | pixman_region32_intersect_rect(&cursor_region, &cursor_region, |
| 245 | output->base.x, output->base.y, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 246 | output->base.current->width, |
| 247 | output->base.current->height); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 248 | |
Benjamin Franzke | a8bdeae | 2011-06-28 22:56:43 +0200 | [diff] [blame] | 249 | if (!pixman_region32_not_empty(&cursor_region)) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 250 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 251 | |
| 252 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 253 | goto out; |
| 254 | |
Pekka Paalanen | 60921e5 | 2012-01-25 15:55:43 +0200 | [diff] [blame] | 255 | if (eid->sprite->geometry.width > 64 || |
| 256 | eid->sprite->geometry.height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 257 | goto out; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 258 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 259 | bo = gbm_bo_create_from_egl_image(c->gbm, |
| 260 | c->base.display, |
| 261 | eid->sprite->image, 64, 64, |
| 262 | GBM_BO_USE_CURSOR_64X64); |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 263 | /* Not suitable for hw cursor, fall back */ |
| 264 | if (bo == NULL) |
| 265 | goto out; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 266 | |
| 267 | handle = gbm_bo_get_handle(bo).s32; |
| 268 | stride = gbm_bo_get_pitch(bo); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 269 | gbm_bo_destroy(bo); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 270 | |
Kristian Høgsberg | 6916d9c | 2012-01-18 15:11:07 -0500 | [diff] [blame] | 271 | /* gbm_bo_create_from_egl_image() didn't always validate the usage |
| 272 | * flags, and in that case we might end up with a bad stride. */ |
| 273 | if (stride != 64 * 4) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 274 | goto out; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 275 | |
| 276 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 277 | if (ret) { |
| 278 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
Pekka Paalanen | ba3cf95 | 2012-01-25 16:22:05 +0200 | [diff] [blame] | 283 | eid->sprite->geometry.x - output->base.x, |
| 284 | eid->sprite->geometry.y - output->base.y); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 285 | if (ret) { |
| 286 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 287 | goto out; |
| 288 | } |
| 289 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 290 | out: |
| 291 | pixman_region32_fini(&cursor_region); |
| 292 | if (ret) |
| 293 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 294 | return ret; |
| 295 | } |
| 296 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 297 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 298 | drm_output_destroy(struct weston_output *output_base) |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 299 | { |
| 300 | struct drm_output *output = (struct drm_output *) output_base; |
| 301 | struct drm_compositor *c = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 302 | (struct drm_compositor *) output->base.compositor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 303 | drmModeCrtcPtr origcrtc = output->original_crtc; |
| 304 | int i; |
| 305 | |
| 306 | /* Turn off hardware cursor */ |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 307 | drm_output_set_cursor(&output->base, NULL); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 308 | |
| 309 | /* Restore original CRTC state */ |
| 310 | drmModeSetCrtc(c->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 311 | origcrtc->x, origcrtc->y, |
| 312 | &output->connector_id, 1, &origcrtc->mode); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 313 | drmModeFreeCrtc(origcrtc); |
| 314 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 315 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 316 | GL_COLOR_ATTACHMENT0, |
| 317 | GL_RENDERBUFFER, |
| 318 | 0); |
| 319 | |
| 320 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 321 | glDeleteRenderbuffers(2, output->rbo); |
| 322 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 323 | /* Destroy output buffers */ |
| 324 | for (i = 0; i < 2; i++) { |
| 325 | drmModeRmFB(c->drm.fd, output->fb_id[i]); |
| 326 | c->base.destroy_image(c->base.display, output->image[i]); |
| 327 | gbm_bo_destroy(output->bo[i]); |
| 328 | } |
| 329 | |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 330 | c->crtc_allocator &= ~(1 << output->crtc_id); |
| 331 | c->connector_allocator &= ~(1 << output->connector_id); |
| 332 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 333 | weston_output_destroy(&output->base); |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 334 | wl_list_remove(&output->base.link); |
| 335 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 336 | free(output); |
| 337 | } |
| 338 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 339 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 340 | on_drm_input(int fd, uint32_t mask, void *data) |
| 341 | { |
| 342 | drmEventContext evctx; |
| 343 | |
| 344 | memset(&evctx, 0, sizeof evctx); |
| 345 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 346 | evctx.page_flip_handler = page_flip_handler; |
| 347 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 348 | |
| 349 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 353 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 354 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 355 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 356 | const char *extensions, *filename; |
| 357 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 358 | static const EGLint context_attribs[] = { |
| 359 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 360 | EGL_NONE |
| 361 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 362 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 363 | filename = udev_device_get_devnode(device); |
David Herrmann | 63ff706 | 2011-11-05 18:46:01 +0100 | [diff] [blame] | 364 | fd = open(filename, O_RDWR | O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 365 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 366 | /* Probably permissions error */ |
| 367 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 368 | udev_device_get_devnode(device)); |
| 369 | return -1; |
| 370 | } |
| 371 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 372 | ec->drm.fd = fd; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 373 | ec->gbm = gbm_create_device(ec->drm.fd); |
| 374 | ec->base.display = eglGetDisplay(ec->gbm); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 375 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 376 | fprintf(stderr, "failed to create display\n"); |
| 377 | return -1; |
| 378 | } |
| 379 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 380 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 381 | fprintf(stderr, "failed to initialize display\n"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 385 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
Ander Conselvan de Oliveira | ef7c8d9 | 2011-11-01 16:37:41 +0200 | [diff] [blame] | 386 | if (!strstr(extensions, "EGL_KHR_surfaceless_gles2")) { |
| 387 | fprintf(stderr, "EGL_KHR_surfaceless_gles2 not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 388 | return -1; |
| 389 | } |
| 390 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 391 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 392 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 393 | return -1; |
| 394 | } |
| 395 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 396 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 397 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 398 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 399 | fprintf(stderr, "failed to create context\n"); |
| 400 | return -1; |
| 401 | } |
| 402 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 403 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 404 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 405 | fprintf(stderr, "failed to make context current\n"); |
| 406 | return -1; |
| 407 | } |
| 408 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static drmModeModeInfo builtin_1024x768 = { |
| 413 | 63500, /* clock */ |
| 414 | 1024, 1072, 1176, 1328, 0, |
| 415 | 768, 771, 775, 798, 0, |
| 416 | 59920, |
| 417 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 418 | 0, |
| 419 | "1024x768" |
| 420 | }; |
| 421 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 422 | |
| 423 | static int |
| 424 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 425 | { |
| 426 | struct drm_mode *mode; |
| 427 | |
| 428 | mode = malloc(sizeof *mode); |
| 429 | if (mode == NULL) |
| 430 | return -1; |
| 431 | |
| 432 | mode->base.flags = 0; |
| 433 | mode->base.width = info->hdisplay; |
| 434 | mode->base.height = info->vdisplay; |
| 435 | mode->base.refresh = info->vrefresh; |
| 436 | mode->mode_info = *info; |
| 437 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int |
| 443 | drm_subpixel_to_wayland(int drm_value) |
| 444 | { |
| 445 | switch (drm_value) { |
| 446 | default: |
| 447 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 448 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 449 | case DRM_MODE_SUBPIXEL_NONE: |
| 450 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 451 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 452 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 453 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 454 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 455 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 456 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 457 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 458 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 459 | } |
| 460 | } |
| 461 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 462 | static void |
| 463 | output_handle_scanout_buffer_destroy(struct wl_listener *listener, |
| 464 | struct wl_resource *resource, |
| 465 | uint32_t time) |
| 466 | { |
| 467 | struct drm_output *output = |
| 468 | container_of(listener, struct drm_output, |
| 469 | scanout_buffer_destroy_listener); |
| 470 | |
| 471 | output->scanout_buffer = NULL; |
| 472 | |
| 473 | if (!output->pending_scanout_buffer) |
| 474 | weston_compositor_schedule_repaint(output->base.compositor); |
| 475 | } |
| 476 | |
| 477 | static void |
| 478 | output_handle_pending_scanout_buffer_destroy(struct wl_listener *listener, |
| 479 | struct wl_resource *resource, |
| 480 | uint32_t time) |
| 481 | { |
| 482 | struct drm_output *output = |
| 483 | container_of(listener, struct drm_output, |
| 484 | pending_scanout_buffer_destroy_listener); |
| 485 | |
| 486 | output->pending_scanout_buffer = NULL; |
| 487 | |
| 488 | weston_compositor_schedule_repaint(output->base.compositor); |
| 489 | } |
| 490 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 491 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 492 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 493 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 494 | drmModeConnector *connector, |
| 495 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 496 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 497 | struct drm_output *output; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 498 | struct drm_mode *drm_mode, *next; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 499 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 500 | int i, ret; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 501 | unsigned handle, stride; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 502 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 503 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 504 | if (encoder == NULL) { |
| 505 | fprintf(stderr, "No encoder for connector.\n"); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 510 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 511 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 512 | break; |
| 513 | } |
| 514 | if (i == resources->count_crtcs) { |
| 515 | fprintf(stderr, "No usable crtc for encoder.\n"); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 516 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 517 | return -1; |
| 518 | } |
| 519 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 520 | output = malloc(sizeof *output); |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 521 | if (output == NULL) { |
| 522 | drmModeFreeEncoder(encoder); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 523 | return -1; |
David Herrmann | eb8bed5 | 2011-12-08 17:05:44 +0100 | [diff] [blame] | 524 | } |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 525 | output->fb_id[0] = -1; |
| 526 | output->fb_id[1] = -1; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 527 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 528 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 529 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 530 | output->base.make = "unknown"; |
| 531 | output->base.model = "unknown"; |
| 532 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 533 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 534 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 535 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 536 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 537 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 538 | |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 539 | output->original_crtc = drmModeGetCrtc(ec->drm.fd, output->crtc_id); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 540 | drmModeFreeEncoder(encoder); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 541 | |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 542 | for (i = 0; i < connector->count_modes; i++) { |
| 543 | ret = drm_output_add_mode(output, &connector->modes[i]); |
| 544 | if (ret) |
| 545 | goto err_free; |
| 546 | } |
| 547 | |
| 548 | if (connector->count_modes == 0) { |
| 549 | ret = drm_output_add_mode(output, &builtin_1024x768); |
| 550 | if (ret) |
| 551 | goto err_free; |
| 552 | } |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 553 | |
| 554 | drm_mode = container_of(output->base.mode_list.next, |
| 555 | struct drm_mode, base.link); |
| 556 | output->base.current = &drm_mode->base; |
| 557 | drm_mode->base.flags = |
| 558 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 559 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 560 | glGenRenderbuffers(2, output->rbo); |
| 561 | for (i = 0; i < 2; i++) { |
| 562 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 563 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 564 | output->bo[i] = |
| 565 | gbm_bo_create(ec->gbm, |
| 566 | output->base.current->width, |
| 567 | output->base.current->height, |
| 568 | GBM_BO_FORMAT_XRGB8888, |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 569 | GBM_BO_USE_SCANOUT | |
| 570 | GBM_BO_USE_RENDERING); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 571 | if (!output->bo[i]) |
| 572 | goto err_bufs; |
| 573 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 574 | output->image[i] = ec->base.create_image(ec->base.display, |
| 575 | NULL, |
| 576 | EGL_NATIVE_PIXMAP_KHR, |
| 577 | output->bo[i], NULL); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 578 | if (!output->image[i]) |
| 579 | goto err_bufs; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 580 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 581 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 582 | output->image[i]); |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 583 | stride = gbm_bo_get_pitch(output->bo[i]); |
| 584 | handle = gbm_bo_get_handle(output->bo[i]).u32; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 585 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 586 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 587 | output->base.current->width, |
| 588 | output->base.current->height, |
Kristian Høgsberg | 9c46ff1 | 2011-10-24 13:13:22 -0400 | [diff] [blame] | 589 | 24, 32, stride, handle, &output->fb_id[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 590 | if (ret) { |
| 591 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 592 | goto err_bufs; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | output->current = 0; |
| 597 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 598 | GL_COLOR_ATTACHMENT0, |
| 599 | GL_RENDERBUFFER, |
| 600 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 601 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 602 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 603 | &output->connector_id, 1, |
| 604 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 605 | if (ret) { |
| 606 | fprintf(stderr, "failed to set mode: %m\n"); |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 607 | goto err_fb; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 608 | } |
| 609 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 610 | weston_output_init(&output->base, &ec->base, x, y, |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 611 | connector->mmWidth, connector->mmHeight, 0); |
| 612 | |
| 613 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
| 614 | |
Kristian Høgsberg | 9f404b7 | 2012-01-26 00:11:01 -0500 | [diff] [blame] | 615 | output->scanout_buffer_destroy_listener.func = |
| 616 | output_handle_scanout_buffer_destroy; |
| 617 | output->pending_scanout_buffer_destroy_listener.func = |
| 618 | output_handle_pending_scanout_buffer_destroy; |
| 619 | |
Kristian Høgsberg | a4b7e20 | 2011-10-24 13:26:32 -0400 | [diff] [blame] | 620 | output->pending_fs_surf_fb_id = 0; |
Kristian Høgsberg | 68c479a | 2012-01-25 23:32:28 -0500 | [diff] [blame] | 621 | output->base.repaint = drm_output_repaint; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 622 | output->base.set_hardware_cursor = drm_output_set_cursor; |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 623 | output->base.destroy = drm_output_destroy; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 624 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 625 | return 0; |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 626 | |
David Herrmann | d7c4551 | 2011-12-08 17:05:46 +0100 | [diff] [blame] | 627 | err_fb: |
| 628 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 629 | GL_COLOR_ATTACHMENT0, |
| 630 | GL_RENDERBUFFER, |
| 631 | 0); |
| 632 | err_bufs: |
| 633 | for (i = 0; i < 2; i++) { |
| 634 | if (output->fb_id[i] != -1) |
| 635 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 636 | if (output->image[i]) |
| 637 | ec->base.destroy_image(ec->base.display, |
| 638 | output->image[i]); |
| 639 | if (output->bo[i]) |
| 640 | gbm_bo_destroy(output->bo[i]); |
| 641 | } |
| 642 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 643 | glDeleteRenderbuffers(2, output->rbo); |
David Herrmann | 0f0d54e | 2011-12-08 17:05:45 +0100 | [diff] [blame] | 644 | err_free: |
| 645 | wl_list_for_each_safe(drm_mode, next, &output->base.mode_list, |
| 646 | base.link) { |
| 647 | wl_list_remove(&drm_mode->base.link); |
| 648 | free(drm_mode); |
| 649 | } |
| 650 | |
| 651 | drmModeFreeCrtc(output->original_crtc); |
| 652 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 653 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 654 | |
| 655 | free(output); |
| 656 | return -1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 660 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 661 | { |
| 662 | drmModeConnector *connector; |
| 663 | drmModeRes *resources; |
| 664 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 665 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 666 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 667 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 668 | if (!resources) { |
| 669 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 670 | return -1; |
| 671 | } |
| 672 | |
| 673 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 674 | connector = drmModeGetConnector(ec->drm.fd, |
| 675 | resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 676 | if (connector == NULL) |
| 677 | continue; |
| 678 | |
| 679 | if (connector->connection == DRM_MODE_CONNECTED && |
| 680 | (option_connector == 0 || |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 681 | connector->connector_id == option_connector)) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 682 | if (create_output_for_connector(ec, resources, |
Benjamin Franzke | 439d986 | 2011-10-07 08:20:53 +0200 | [diff] [blame] | 683 | connector, x, y) < 0) { |
| 684 | drmModeFreeConnector(connector); |
| 685 | continue; |
| 686 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 687 | |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 688 | x += container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 689 | struct weston_output, |
Benjamin Franzke | 9eaee35 | 2011-08-02 13:03:54 +0200 | [diff] [blame] | 690 | link)->current->width; |
| 691 | } |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 692 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 693 | drmModeFreeConnector(connector); |
| 694 | } |
| 695 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 696 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 697 | fprintf(stderr, "No currently active connector found.\n"); |
| 698 | return -1; |
| 699 | } |
| 700 | |
| 701 | drmModeFreeResources(resources); |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 706 | static void |
| 707 | update_outputs(struct drm_compositor *ec) |
| 708 | { |
| 709 | drmModeConnector *connector; |
| 710 | drmModeRes *resources; |
| 711 | struct drm_output *output, *next; |
| 712 | int x = 0, y = 0; |
| 713 | int x_offset = 0, y_offset = 0; |
| 714 | uint32_t connected = 0, disconnects = 0; |
| 715 | int i; |
| 716 | |
| 717 | resources = drmModeGetResources(ec->drm.fd); |
| 718 | if (!resources) { |
| 719 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | /* collect new connects */ |
| 724 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 725 | int connector_id = resources->connectors[i]; |
| 726 | |
| 727 | connector = drmModeGetConnector(ec->drm.fd, connector_id); |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 728 | if (connector == NULL) |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 729 | continue; |
| 730 | |
David Herrmann | 7551cff | 2011-12-08 17:05:43 +0100 | [diff] [blame] | 731 | if (connector->connection != DRM_MODE_CONNECTED) { |
| 732 | drmModeFreeConnector(connector); |
| 733 | continue; |
| 734 | } |
| 735 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 736 | connected |= (1 << connector_id); |
| 737 | |
| 738 | if (!(ec->connector_allocator & (1 << connector_id))) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 739 | struct weston_output *last = |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 740 | container_of(ec->base.output_list.prev, |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 741 | struct weston_output, link); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 742 | |
| 743 | /* XXX: not yet needed, we die with 0 outputs */ |
| 744 | if (!wl_list_empty(&ec->base.output_list)) |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 745 | x = last->x + last->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 746 | else |
| 747 | x = 0; |
| 748 | y = 0; |
| 749 | create_output_for_connector(ec, resources, |
| 750 | connector, x, y); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 751 | printf("connector %d connected\n", connector_id); |
| 752 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 753 | } |
| 754 | drmModeFreeConnector(connector); |
| 755 | } |
| 756 | drmModeFreeResources(resources); |
| 757 | |
| 758 | disconnects = ec->connector_allocator & ~connected; |
| 759 | if (disconnects) { |
| 760 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 761 | base.link) { |
| 762 | if (x_offset != 0 || y_offset != 0) { |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 763 | weston_output_move(&output->base, |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 764 | output->base.x - x_offset, |
| 765 | output->base.y - y_offset); |
| 766 | } |
| 767 | |
| 768 | if (disconnects & (1 << output->connector_id)) { |
| 769 | disconnects &= ~(1 << output->connector_id); |
| 770 | printf("connector %d disconnected\n", |
| 771 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame] | 772 | x_offset += output->base.current->width; |
Benjamin Franzke | 48c4ea2 | 2011-08-30 11:44:56 +0200 | [diff] [blame] | 773 | drm_output_destroy(&output->base); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | /* FIXME: handle zero outputs, without terminating */ |
| 779 | if (ec->connector_allocator == 0) |
| 780 | wl_display_terminate(ec->base.wl_display); |
| 781 | } |
| 782 | |
| 783 | static int |
| 784 | udev_event_is_hotplug(struct udev_device *device) |
| 785 | { |
| 786 | struct udev_list_entry *list, *hotplug_entry; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 787 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 788 | list = udev_device_get_properties_list_entry(device); |
| 789 | |
| 790 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 791 | if (hotplug_entry == NULL) |
| 792 | return 0; |
| 793 | |
| 794 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 795 | } |
| 796 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 797 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 798 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 799 | { |
| 800 | struct drm_compositor *ec = data; |
| 801 | struct udev_device *event; |
| 802 | |
| 803 | event = udev_monitor_receive_device(ec->udev_monitor); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 804 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 805 | if (udev_event_is_hotplug(event)) |
| 806 | update_outputs(ec); |
| 807 | |
| 808 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 809 | |
| 810 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 811 | } |
| 812 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 813 | static EGLImageKHR |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 814 | drm_compositor_create_cursor_image(struct weston_compositor *ec, |
Ander Conselvan de Oliveira | 0de0aaf | 2011-10-27 17:09:17 +0300 | [diff] [blame] | 815 | int32_t *width, int32_t *height) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 816 | { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 817 | struct drm_compositor *c = (struct drm_compositor *) ec; |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 818 | struct gbm_bo *bo; |
| 819 | EGLImageKHR image; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 820 | |
Ander Conselvan de Oliveira | 0de0aaf | 2011-10-27 17:09:17 +0300 | [diff] [blame] | 821 | if (*width > 64 || *height > 64) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 822 | return EGL_NO_IMAGE_KHR; |
| 823 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 824 | bo = gbm_bo_create(c->gbm, |
| 825 | /* width, height, */ 64, 64, |
| 826 | GBM_BO_FORMAT_ARGB8888, |
| 827 | GBM_BO_USE_CURSOR_64X64 | GBM_BO_USE_RENDERING); |
| 828 | |
| 829 | image = ec->create_image(c->base.display, NULL, |
| 830 | EGL_NATIVE_PIXMAP_KHR, bo, NULL); |
| 831 | gbm_bo_destroy(bo); |
| 832 | |
Kristian Høgsberg | ff298d9 | 2011-10-28 20:41:28 -0400 | [diff] [blame] | 833 | *width = 64; |
| 834 | *height = 64; |
Ander Conselvan de Oliveira | c483917 | 2011-08-16 14:25:15 +0300 | [diff] [blame] | 835 | |
Benjamin Franzke | 060cf80 | 2011-04-30 09:32:11 +0200 | [diff] [blame] | 836 | return image; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 839 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 840 | drm_destroy(struct weston_compositor *ec) |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 841 | { |
| 842 | struct drm_compositor *d = (struct drm_compositor *) ec; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 843 | struct weston_input_device *input, *next; |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 844 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 845 | weston_compositor_shutdown(ec); |
Matt Roper | 361d2ad | 2011-08-29 13:52:23 -0700 | [diff] [blame] | 846 | gbm_device_destroy(d->gbm); |
Kristian Høgsberg | 62d2774 | 2012-01-18 12:38:33 -0500 | [diff] [blame] | 847 | drmDropMaster(d->drm.fd); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 848 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 849 | |
Tiago Vignatti | c349e1d | 2011-12-18 23:52:18 +0200 | [diff] [blame] | 850 | wl_list_for_each_safe(input, next, &ec->input_device_list, link) |
| 851 | evdev_input_destroy(input); |
| 852 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 853 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 854 | } |
| 855 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 856 | static void |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 857 | drm_compositor_set_modes(struct drm_compositor *compositor) |
| 858 | { |
| 859 | struct drm_output *output; |
| 860 | struct drm_mode *drm_mode; |
| 861 | int ret; |
| 862 | |
| 863 | wl_list_for_each(output, &compositor->base.output_list, base.link) { |
| 864 | drm_mode = (struct drm_mode *) output->base.current; |
| 865 | ret = drmModeSetCrtc(compositor->drm.fd, output->crtc_id, |
| 866 | output->fb_id[output->current ^ 1], 0, 0, |
| 867 | &output->connector_id, 1, |
| 868 | &drm_mode->mode_info); |
| 869 | if (ret < 0) { |
| 870 | fprintf(stderr, |
| 871 | "failed to set mode %dx%d for output at %d,%d: %m", |
| 872 | drm_mode->base.width, drm_mode->base.height, |
| 873 | output->base.x, output->base.y); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static void |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 879 | vt_func(struct weston_compositor *compositor, int event) |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 880 | { |
| 881 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 882 | struct weston_output *output; |
| 883 | struct weston_input_device *input; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 884 | |
| 885 | switch (event) { |
| 886 | case TTY_ENTER_VT: |
| 887 | compositor->focus = 1; |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 888 | if (drmSetMaster(ec->drm.fd)) { |
| 889 | fprintf(stderr, "failed to set master: %m\n"); |
| 890 | wl_display_terminate(compositor->wl_display); |
| 891 | } |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 892 | compositor->state = ec->prev_state; |
Kristian Høgsberg | 835cd49 | 2012-01-18 11:48:46 -0500 | [diff] [blame] | 893 | drm_compositor_set_modes(ec); |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 894 | weston_compositor_damage_all(compositor); |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 895 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 896 | evdev_add_devices(ec->udev, input); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 897 | break; |
| 898 | case TTY_LEAVE_VT: |
| 899 | compositor->focus = 0; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 900 | ec->prev_state = compositor->state; |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 901 | compositor->state = WESTON_COMPOSITOR_SLEEPING; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 902 | |
Kristian Høgsberg | 34f80ff | 2012-01-18 11:50:31 -0500 | [diff] [blame] | 903 | /* If we have a repaint scheduled (either from a |
| 904 | * pending pageflip or the idle handler), make sure we |
| 905 | * cancel that so we don't try to pageflip when we're |
| 906 | * vt switched away. The SLEEPING state will prevent |
| 907 | * further attemps at repainting. When we switch |
| 908 | * back, we schedule a repaint, which will process |
| 909 | * pending frame callbacks. */ |
| 910 | |
| 911 | wl_list_for_each(output, &ec->base.output_list, link) { |
| 912 | output->repaint_needed = 0; |
| 913 | drm_output_set_cursor(output, NULL); |
| 914 | } |
| 915 | |
Tiago Vignatti | 6e2d5f1 | 2011-12-19 00:32:48 +0200 | [diff] [blame] | 916 | wl_list_for_each(input, &compositor->input_device_list, link) |
| 917 | evdev_remove_devices(input); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 918 | |
Kristian Høgsberg | a018fb0 | 2012-01-16 10:52:52 -0500 | [diff] [blame] | 919 | if (drmDropMaster(ec->drm.fd) < 0) |
| 920 | fprintf(stderr, "failed to drop master: %m\n"); |
| 921 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 922 | break; |
| 923 | }; |
| 924 | } |
| 925 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 926 | static const char default_seat[] = "seat0"; |
| 927 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 928 | static struct weston_compositor * |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 929 | drm_compositor_create(struct wl_display *display, |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 930 | int connector, const char *seat, int tty) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 931 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 932 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 933 | struct udev_enumerate *e; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 934 | struct udev_list_entry *entry; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 935 | struct udev_device *device, *drm_device; |
| 936 | const char *path, *device_seat; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 937 | struct wl_event_loop *loop; |
| 938 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 939 | ec = malloc(sizeof *ec); |
| 940 | if (ec == NULL) |
| 941 | return NULL; |
| 942 | |
| 943 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 944 | ec->udev = udev_new(); |
| 945 | if (ec->udev == NULL) { |
| 946 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 947 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 948 | } |
| 949 | |
Kristian Høgsberg | c5b9ddb | 2012-01-15 14:29:09 -0500 | [diff] [blame] | 950 | ec->base.wl_display = display; |
| 951 | ec->tty = tty_create(&ec->base, vt_func, tty); |
| 952 | if (!ec->tty) { |
| 953 | fprintf(stderr, "failed to initialize tty\n"); |
| 954 | free(ec); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 958 | e = udev_enumerate_new(ec->udev); |
| 959 | udev_enumerate_add_match_subsystem(e, "drm"); |
Benjamin Franzke | a764ee5 | 2011-10-07 08:23:22 +0200 | [diff] [blame] | 960 | udev_enumerate_add_match_sysname(e, "card[0-9]*"); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 961 | |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 962 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 963 | drm_device = NULL; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 964 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 965 | path = udev_list_entry_get_name(entry); |
| 966 | device = udev_device_new_from_syspath(ec->udev, path); |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 967 | device_seat = |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 968 | udev_device_get_property_value(device, "ID_SEAT"); |
| 969 | if (!device_seat) |
| 970 | device_seat = default_seat; |
| 971 | if (strcmp(device_seat, seat) == 0) { |
| 972 | drm_device = device; |
| 973 | break; |
| 974 | } |
| 975 | udev_device_unref(device); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 976 | } |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 977 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 978 | if (drm_device == NULL) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 979 | fprintf(stderr, "no drm device found\n"); |
| 980 | return NULL; |
| 981 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 982 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 983 | if (init_egl(ec, drm_device) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 984 | fprintf(stderr, "failed to initialize egl\n"); |
| 985 | return NULL; |
| 986 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 987 | |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 988 | udev_device_unref(drm_device); |
| 989 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 990 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 991 | ec->base.create_cursor_image = drm_compositor_create_cursor_image; |
| 992 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 993 | ec->base.focus = 1; |
| 994 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 995 | ec->prev_state = WESTON_COMPOSITOR_ACTIVE; |
Pekka Paalanen | bce2d3f | 2011-12-02 13:07:27 +0200 | [diff] [blame] | 996 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 997 | glGenFramebuffers(1, &ec->base.fbo); |
| 998 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 999 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1000 | /* Can't init base class until we have a current egl context */ |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1001 | if (weston_compositor_init(&ec->base, display) < 0) |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 1002 | return NULL; |
| 1003 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 1004 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1005 | fprintf(stderr, "failed to create output for %s\n", path); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | |
Benjamin Franzke | 02dee2c | 2011-10-07 08:27:26 +0200 | [diff] [blame] | 1009 | udev_enumerate_unref(e); |
| 1010 | path = NULL; |
| 1011 | |
Tiago Vignatti | ce03ec3 | 2011-12-19 01:14:03 +0200 | [diff] [blame] | 1012 | evdev_input_create(&ec->base, ec->udev, seat); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1013 | |
| 1014 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 1015 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 1016 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1017 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1018 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1019 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 1020 | if (ec->udev_monitor == NULL) { |
| 1021 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 1022 | return NULL; |
| 1023 | } |
| 1024 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 1025 | "drm", NULL); |
| 1026 | ec->udev_drm_source = |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1027 | wl_event_loop_add_fd(loop, |
| 1028 | udev_monitor_get_fd(ec->udev_monitor), |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 1029 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 1030 | |
| 1031 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 1032 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 1033 | return NULL; |
| 1034 | } |
| 1035 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 1036 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1037 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1038 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1039 | struct weston_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 1040 | backend_init(struct wl_display *display, char *options); |
| 1041 | |
Kristian Høgsberg | 8334bc1 | 2012-01-03 10:29:47 -0500 | [diff] [blame] | 1042 | WL_EXPORT struct weston_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1043 | backend_init(struct wl_display *display, char *options) |
| 1044 | { |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1045 | int connector = 0, i; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1046 | const char *seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1047 | char *p, *value; |
Kristian Høgsberg | 1201b75 | 2012-01-15 14:27:10 -0500 | [diff] [blame] | 1048 | int tty = 0; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1049 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1050 | static char * const tokens[] = { "connector", "seat", "tty", NULL }; |
Benjamin Franzke | 117483d | 2011-08-30 11:38:26 +0200 | [diff] [blame] | 1051 | |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1052 | p = options; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1053 | seat = default_seat; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1054 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 1055 | switch (i) { |
| 1056 | case 0: |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 1057 | connector = strtol(value, NULL, 0); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1058 | break; |
Kristian Høgsberg | 8d51f14 | 2011-07-15 21:28:38 -0400 | [diff] [blame] | 1059 | case 1: |
| 1060 | seat = value; |
| 1061 | break; |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1062 | case 2: |
| 1063 | tty = strtol(value, NULL, 0); |
| 1064 | break; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Tiago Vignatti | faee801 | 2011-09-01 15:58:17 -0400 | [diff] [blame] | 1068 | return drm_compositor_create(display, connector, seat, tty); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 1069 | } |