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