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