Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008-2010 Kristian Høgsberg |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software Foundation, |
| 16 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
Kristian Høgsberg | 0b9334a | 2011-04-12 11:34:32 -0400 | [diff] [blame] | 19 | #define _GNU_SOURCE |
| 20 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <unistd.h> |
| 26 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 27 | #include <xf86drm.h> |
| 28 | #include <xf86drmMode.h> |
| 29 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 30 | #include "compositor.h" |
| 31 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 32 | struct drm_compositor { |
| 33 | struct wlsc_compositor base; |
| 34 | |
| 35 | struct udev *udev; |
| 36 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 37 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 38 | struct udev_monitor *udev_monitor; |
| 39 | struct wl_event_source *udev_drm_source; |
| 40 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 41 | struct { |
| 42 | int fd; |
| 43 | } drm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 44 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 45 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 46 | struct tty *tty; |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 47 | |
| 48 | PFNEGLCREATEDRMIMAGEMESA create_drm_image; |
| 49 | PFNEGLEXPORTDRMIMAGEMESA export_drm_image; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 52 | struct drm_mode { |
| 53 | struct wlsc_mode base; |
| 54 | drmModeModeInfo mode_info; |
| 55 | }; |
| 56 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 57 | struct drm_output { |
| 58 | struct wlsc_output base; |
| 59 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 60 | uint32_t crtc_id; |
| 61 | uint32_t connector_id; |
| 62 | GLuint rbo[2]; |
| 63 | uint32_t fb_id[2]; |
| 64 | EGLImageKHR image[2]; |
| 65 | uint32_t current; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 66 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 67 | struct wlsc_surface *scanout_surface; |
| 68 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 69 | uint32_t fs_surf_fb_id; |
| 70 | uint32_t pending_fs_surf_fb_id; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 73 | static int |
| 74 | drm_output_prepare_render(struct wlsc_output *output_base) |
| 75 | { |
| 76 | struct drm_output *output = (struct drm_output *) output_base; |
| 77 | |
| 78 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 79 | GL_COLOR_ATTACHMENT0, |
| 80 | GL_RENDERBUFFER, |
| 81 | output->rbo[output->current]); |
| 82 | |
| 83 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 84 | return -1; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 89 | static int |
| 90 | drm_output_present(struct wlsc_output *output_base) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 91 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 92 | struct drm_output *output = (struct drm_output *) output_base; |
| 93 | struct drm_compositor *c = |
| 94 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 95 | uint32_t fb_id = 0; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 96 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 97 | if (drm_output_prepare_render(&output->base)) |
| 98 | return -1; |
| 99 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 100 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 101 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 102 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 103 | if (output->scanout_surface != NULL) { |
| 104 | output->scanout_surface = NULL; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 105 | fb_id = output->fs_surf_fb_id; |
| 106 | } else { |
| 107 | fb_id = output->fb_id[output->current ^ 1]; |
| 108 | } |
| 109 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 110 | drmModePageFlip(c->drm.fd, output->crtc_id, |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 111 | fb_id, |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 112 | DRM_MODE_PAGE_FLIP_EVENT, output); |
| 113 | |
| 114 | return 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void |
| 118 | page_flip_handler(int fd, unsigned int frame, |
| 119 | unsigned int sec, unsigned int usec, void *data) |
| 120 | { |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 121 | struct drm_output *output = (struct drm_output *) data; |
| 122 | struct drm_compositor *c = |
| 123 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 124 | uint32_t msecs; |
| 125 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 126 | if (output->pending_fs_surf_fb_id) { |
| 127 | drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id); |
| 128 | output->pending_fs_surf_fb_id = 0; |
| 129 | } |
| 130 | |
| 131 | if (output->fs_surf_fb_id) { |
| 132 | output->pending_fs_surf_fb_id = output->fs_surf_fb_id; |
| 133 | output->fs_surf_fb_id = 0; |
| 134 | } |
| 135 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 136 | msecs = sec * 1000 + usec / 1000; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 137 | wlsc_output_finish_frame(&output->base, msecs); |
| 138 | } |
| 139 | |
| 140 | static int |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 141 | drm_output_prepare_scanout_surface(struct wlsc_output *output_base, |
| 142 | struct wlsc_surface *es) |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 143 | { |
| 144 | struct drm_output *output = (struct drm_output *) output_base; |
| 145 | struct drm_compositor *c = |
| 146 | (struct drm_compositor *) output->base.compositor; |
| 147 | EGLint handle, stride; |
| 148 | int ret; |
| 149 | uint32_t fb_id = 0; |
| 150 | |
Kristian Høgsberg | 4d07a1c | 2011-05-06 14:03:12 -0400 | [diff] [blame] | 151 | if (es->x != output->base.x || |
| 152 | es->y != output->base.y || |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 153 | es->width != output->base.current->width || |
| 154 | es->height != output->base.current->height || |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 155 | es->image == EGL_NO_IMAGE_KHR) |
| 156 | return -1; |
| 157 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 158 | c->export_drm_image(c->base.display, |
| 159 | es->image, NULL, &handle, &stride); |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 160 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 161 | if (handle == 0) |
| 162 | return -1; |
| 163 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 164 | ret = drmModeAddFB(c->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 165 | output->base.current->width, |
| 166 | output->base.current->height, |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 167 | 32, 32, stride, handle, &fb_id); |
| 168 | |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 169 | if (ret) |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 170 | return -1; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 171 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 172 | output->fs_surf_fb_id = fb_id; |
| 173 | output->scanout_surface = es; |
Benjamin Franzke | 1178a3c | 2011-04-10 16:49:52 +0200 | [diff] [blame] | 174 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 175 | return 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 178 | static int |
| 179 | drm_output_set_cursor(struct wlsc_output *output_base, |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 180 | struct wlsc_input_device *eid) |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 181 | { |
| 182 | struct drm_output *output = (struct drm_output *) output_base; |
| 183 | struct drm_compositor *c = |
| 184 | (struct drm_compositor *) output->base.compositor; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 185 | EGLint handle, stride; |
| 186 | int ret = -1; |
| 187 | pixman_region32_t cursor_region; |
| 188 | |
Kristian Høgsberg | e4c40a4 | 2011-05-06 14:04:21 -0400 | [diff] [blame] | 189 | if (eid == NULL) { |
| 190 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 194 | pixman_region32_init_rect(&cursor_region, |
| 195 | eid->sprite->x, eid->sprite->y, |
| 196 | eid->sprite->width, eid->sprite->height); |
| 197 | |
| 198 | pixman_region32_intersect_rect(&cursor_region, &cursor_region, |
| 199 | output->base.x, output->base.y, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 200 | output->base.current->width, |
| 201 | output->base.current->height); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 202 | |
| 203 | if (!pixman_region32_not_empty(&cursor_region)) { |
| 204 | ret = 0; |
| 205 | goto out; |
| 206 | } |
| 207 | |
| 208 | if (eid->sprite->image == EGL_NO_IMAGE_KHR) |
| 209 | goto out; |
| 210 | |
| 211 | if (eid->sprite->width > 64 || eid->sprite->height > 64) |
| 212 | goto out; |
| 213 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 214 | c->export_drm_image(c->base.display, eid->sprite->image, |
| 215 | NULL, &handle, &stride); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 216 | |
| 217 | if (stride != 64 * 4) { |
| 218 | fprintf(stderr, "info: cursor stride is != 64\n"); |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64); |
| 223 | if (ret) { |
| 224 | fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret)); |
| 225 | goto out; |
| 226 | } |
| 227 | |
| 228 | ret = drmModeMoveCursor(c->drm.fd, output->crtc_id, |
| 229 | eid->sprite->x - output->base.x, |
| 230 | eid->sprite->y - output->base.y); |
| 231 | if (ret) { |
| 232 | fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret)); |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | printf("info: set hardware cursor\n"); |
| 237 | |
| 238 | out: |
| 239 | pixman_region32_fini(&cursor_region); |
| 240 | if (ret) |
| 241 | drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0); |
| 242 | return ret; |
| 243 | } |
| 244 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 245 | static int |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 246 | on_drm_input(int fd, uint32_t mask, void *data) |
| 247 | { |
| 248 | drmEventContext evctx; |
| 249 | |
| 250 | memset(&evctx, 0, sizeof evctx); |
| 251 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 252 | evctx.page_flip_handler = page_flip_handler; |
| 253 | drmHandleEvent(fd, &evctx); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 254 | |
| 255 | return 1; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 259 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 260 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 261 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 262 | const char *extensions, *filename; |
| 263 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 264 | static const EGLint context_attribs[] = { |
| 265 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 266 | EGL_NONE |
| 267 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 268 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 269 | filename = udev_device_get_devnode(device); |
Kristian Høgsberg | 9cd7aca | 2011-04-11 13:19:16 -0400 | [diff] [blame] | 270 | fd = open(filename, O_RDWR, O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 271 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 272 | /* Probably permissions error */ |
| 273 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 274 | udev_device_get_devnode(device)); |
| 275 | return -1; |
| 276 | } |
| 277 | |
Egbert Eich | e7b8d90 | 2011-05-10 20:00:19 +0000 | [diff] [blame] | 278 | setenv("EGL_PLATFORM", "drm", 1); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 279 | ec->drm.fd = fd; |
Benjamin Franzke | e5b3b26 | 2011-04-26 09:21:13 +0200 | [diff] [blame] | 280 | ec->base.display = eglGetDisplay(FD_TO_EGL_NATIVE_DPY(ec->drm.fd)); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 281 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 282 | fprintf(stderr, "failed to create display\n"); |
| 283 | return -1; |
| 284 | } |
| 285 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 286 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 287 | fprintf(stderr, "failed to initialize display\n"); |
| 288 | return -1; |
| 289 | } |
| 290 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 291 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
| 292 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 293 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 294 | return -1; |
| 295 | } |
| 296 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 297 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 298 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 299 | return -1; |
| 300 | } |
| 301 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 302 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 303 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 304 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 305 | fprintf(stderr, "failed to create context\n"); |
| 306 | return -1; |
| 307 | } |
| 308 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 309 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 310 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 311 | fprintf(stderr, "failed to make context current\n"); |
| 312 | return -1; |
| 313 | } |
| 314 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static drmModeModeInfo builtin_1024x768 = { |
| 319 | 63500, /* clock */ |
| 320 | 1024, 1072, 1176, 1328, 0, |
| 321 | 768, 771, 775, 798, 0, |
| 322 | 59920, |
| 323 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 324 | 0, |
| 325 | "1024x768" |
| 326 | }; |
| 327 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 328 | |
| 329 | static int |
| 330 | drm_output_add_mode(struct drm_output *output, drmModeModeInfo *info) |
| 331 | { |
| 332 | struct drm_mode *mode; |
| 333 | |
| 334 | mode = malloc(sizeof *mode); |
| 335 | if (mode == NULL) |
| 336 | return -1; |
| 337 | |
| 338 | mode->base.flags = 0; |
| 339 | mode->base.width = info->hdisplay; |
| 340 | mode->base.height = info->vdisplay; |
| 341 | mode->base.refresh = info->vrefresh; |
| 342 | mode->mode_info = *info; |
| 343 | wl_list_insert(output->base.mode_list.prev, &mode->base.link); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int |
| 349 | drm_subpixel_to_wayland(int drm_value) |
| 350 | { |
| 351 | switch (drm_value) { |
| 352 | default: |
| 353 | case DRM_MODE_SUBPIXEL_UNKNOWN: |
| 354 | return WL_OUTPUT_SUBPIXEL_UNKNOWN; |
| 355 | case DRM_MODE_SUBPIXEL_NONE: |
| 356 | return WL_OUTPUT_SUBPIXEL_NONE; |
| 357 | case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: |
| 358 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB; |
| 359 | case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: |
| 360 | return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR; |
| 361 | case DRM_MODE_SUBPIXEL_VERTICAL_RGB: |
| 362 | return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB; |
| 363 | case DRM_MODE_SUBPIXEL_VERTICAL_BGR: |
| 364 | return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR; |
| 365 | } |
| 366 | } |
| 367 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 368 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 369 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 370 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 371 | drmModeConnector *connector, |
| 372 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 373 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 374 | struct drm_output *output; |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 375 | struct drm_mode *drm_mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 376 | drmModeEncoder *encoder; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 377 | int i, ret; |
| 378 | EGLint handle, stride, attribs[] = { |
| 379 | EGL_WIDTH, 0, |
| 380 | EGL_HEIGHT, 0, |
Kristian Høgsberg | b12fcce | 2010-08-24 17:34:23 -0400 | [diff] [blame] | 381 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 382 | EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 383 | EGL_NONE |
| 384 | }; |
| 385 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 386 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 387 | if (encoder == NULL) { |
| 388 | fprintf(stderr, "No encoder for connector.\n"); |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 393 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 394 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | if (i == resources->count_crtcs) { |
| 398 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 399 | return -1; |
| 400 | } |
| 401 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 402 | output = malloc(sizeof *output); |
| 403 | if (output == NULL) |
| 404 | return -1; |
| 405 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 406 | memset(output, 0, sizeof *output); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 407 | output->base.x = x; |
| 408 | output->base.y = y; |
| 409 | output->base.mm_width = connector->mmWidth; |
| 410 | output->base.mm_height = connector->mmHeight; |
| 411 | output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel); |
| 412 | output->base.make = "unknown"; |
| 413 | output->base.model = "unknown"; |
| 414 | wl_list_init(&output->base.mode_list); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 415 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 416 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 417 | ec->crtc_allocator |= (1 << output->crtc_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 418 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 419 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 420 | |
| 421 | for (i = 0; i < connector->count_modes; i++) |
| 422 | drm_output_add_mode(output, &connector->modes[i]); |
| 423 | if (connector->count_modes == 0) |
| 424 | drm_output_add_mode(output, &builtin_1024x768); |
| 425 | |
| 426 | drm_mode = container_of(output->base.mode_list.next, |
| 427 | struct drm_mode, base.link); |
| 428 | output->base.current = &drm_mode->base; |
| 429 | drm_mode->base.flags = |
| 430 | WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED; |
| 431 | |
| 432 | wlsc_output_init(&output->base, &ec->base, x, y, 200, 100, 0); |
| 433 | |
| 434 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 435 | |
| 436 | drmModeFreeEncoder(encoder); |
| 437 | |
| 438 | glGenRenderbuffers(2, output->rbo); |
| 439 | for (i = 0; i < 2; i++) { |
| 440 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 441 | |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 442 | attribs[1] = output->base.current->width; |
| 443 | attribs[3] = output->base.current->height; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 444 | output->image[i] = |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 445 | ec->create_drm_image(ec->base.display, attribs); |
| 446 | ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER, |
| 447 | output->image[i]); |
| 448 | ec->export_drm_image(ec->base.display, output->image[i], |
| 449 | NULL, &handle, &stride); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 450 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 451 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 452 | output->base.current->width, |
| 453 | output->base.current->height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 454 | 32, 32, stride, handle, &output->fb_id[i]); |
| 455 | if (ret) { |
| 456 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 457 | return -1; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | output->current = 0; |
| 462 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 463 | GL_COLOR_ATTACHMENT0, |
| 464 | GL_RENDERBUFFER, |
| 465 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 466 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 467 | output->fb_id[output->current ^ 1], 0, 0, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 468 | &output->connector_id, 1, |
| 469 | &drm_mode->mode_info); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 470 | if (ret) { |
| 471 | fprintf(stderr, "failed to set mode: %m\n"); |
| 472 | return -1; |
| 473 | } |
| 474 | |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 475 | output->scanout_surface = NULL; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 476 | output->base.prepare_render = drm_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 477 | output->base.present = drm_output_present; |
Benjamin Franzke | 66aa235 | 2011-04-20 17:06:13 +0200 | [diff] [blame] | 478 | output->base.prepare_scanout_surface = |
| 479 | drm_output_prepare_scanout_surface; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 480 | output->base.set_hardware_cursor = drm_output_set_cursor; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 481 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 486 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 487 | { |
| 488 | drmModeConnector *connector; |
| 489 | drmModeRes *resources; |
| 490 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 491 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 492 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 493 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 494 | if (!resources) { |
| 495 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 496 | return -1; |
| 497 | } |
| 498 | |
| 499 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 500 | connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 501 | if (connector == NULL) |
| 502 | continue; |
| 503 | |
| 504 | if (connector->connection == DRM_MODE_CONNECTED && |
| 505 | (option_connector == 0 || |
| 506 | connector->connector_id == option_connector)) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 507 | if (create_output_for_connector(ec, resources, |
| 508 | connector, x, y) < 0) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 509 | return -1; |
| 510 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 511 | x += container_of(ec->base.output_list.prev, struct wlsc_output, |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 512 | link)->current->width; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 513 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 514 | drmModeFreeConnector(connector); |
| 515 | } |
| 516 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 517 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 518 | fprintf(stderr, "No currently active connector found.\n"); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | drmModeFreeResources(resources); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 527 | static int |
| 528 | destroy_output(struct drm_output *output) |
| 529 | { |
| 530 | struct drm_compositor *ec = |
| 531 | (struct drm_compositor *) output->base.compositor; |
| 532 | int i; |
| 533 | |
| 534 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 535 | GL_COLOR_ATTACHMENT0, |
| 536 | GL_RENDERBUFFER, |
| 537 | 0); |
| 538 | |
| 539 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 540 | glDeleteRenderbuffers(2, output->rbo); |
| 541 | |
| 542 | for (i = 0; i < 2; i++) { |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 543 | ec->base.destroy_image(ec->base.display, output->image[i]); |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 544 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 545 | } |
| 546 | |
| 547 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 548 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 549 | |
| 550 | wlsc_output_destroy(&output->base); |
| 551 | wl_list_remove(&output->base.link); |
| 552 | |
| 553 | free(output); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static void |
| 559 | update_outputs(struct drm_compositor *ec) |
| 560 | { |
| 561 | drmModeConnector *connector; |
| 562 | drmModeRes *resources; |
| 563 | struct drm_output *output, *next; |
| 564 | int x = 0, y = 0; |
| 565 | int x_offset = 0, y_offset = 0; |
| 566 | uint32_t connected = 0, disconnects = 0; |
| 567 | int i; |
| 568 | |
| 569 | resources = drmModeGetResources(ec->drm.fd); |
| 570 | if (!resources) { |
| 571 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | /* collect new connects */ |
| 576 | for (i = 0; i < resources->count_connectors; i++) { |
| 577 | connector = |
| 578 | drmModeGetConnector(ec->drm.fd, |
| 579 | resources->connectors[i]); |
| 580 | if (connector == NULL || |
| 581 | connector->connection != DRM_MODE_CONNECTED) |
| 582 | continue; |
| 583 | |
| 584 | connected |= (1 << connector->connector_id); |
| 585 | |
| 586 | if (!(ec->connector_allocator & (1 << connector->connector_id))) { |
| 587 | struct wlsc_output *last_output = |
| 588 | container_of(ec->base.output_list.prev, |
| 589 | struct wlsc_output, link); |
| 590 | |
| 591 | /* XXX: not yet needed, we die with 0 outputs */ |
| 592 | if (!wl_list_empty(&ec->base.output_list)) |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 593 | x = last_output->x + last_output->current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 594 | else |
| 595 | x = 0; |
| 596 | y = 0; |
| 597 | create_output_for_connector(ec, resources, |
| 598 | connector, x, y); |
| 599 | printf("connector %d connected\n", |
| 600 | connector->connector_id); |
| 601 | |
| 602 | } |
| 603 | drmModeFreeConnector(connector); |
| 604 | } |
| 605 | drmModeFreeResources(resources); |
| 606 | |
| 607 | disconnects = ec->connector_allocator & ~connected; |
| 608 | if (disconnects) { |
| 609 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 610 | base.link) { |
| 611 | if (x_offset != 0 || y_offset != 0) { |
| 612 | wlsc_output_move(&output->base, |
| 613 | output->base.x - x_offset, |
| 614 | output->base.y - y_offset); |
| 615 | } |
| 616 | |
| 617 | if (disconnects & (1 << output->connector_id)) { |
| 618 | disconnects &= ~(1 << output->connector_id); |
| 619 | printf("connector %d disconnected\n", |
| 620 | output->connector_id); |
Kristian Høgsberg | 8f0ce05 | 2011-06-21 11:16:58 -0400 | [diff] [blame^] | 621 | x_offset += output->base.current->width; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 622 | destroy_output(output); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* FIXME: handle zero outputs, without terminating */ |
| 628 | if (ec->connector_allocator == 0) |
| 629 | wl_display_terminate(ec->base.wl_display); |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | udev_event_is_hotplug(struct udev_device *device) |
| 634 | { |
| 635 | struct udev_list_entry *list, *hotplug_entry; |
| 636 | |
| 637 | list = udev_device_get_properties_list_entry(device); |
| 638 | |
| 639 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 640 | if (hotplug_entry == NULL) |
| 641 | return 0; |
| 642 | |
| 643 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 644 | } |
| 645 | |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 646 | static int |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 647 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 648 | { |
| 649 | struct drm_compositor *ec = data; |
| 650 | struct udev_device *event; |
| 651 | |
| 652 | event = udev_monitor_receive_device(ec->udev_monitor); |
| 653 | |
| 654 | if (udev_event_is_hotplug(event)) |
| 655 | update_outputs(ec); |
| 656 | |
| 657 | udev_device_unref(event); |
Kristian Høgsberg | b186847 | 2011-04-22 12:27:57 -0400 | [diff] [blame] | 658 | |
| 659 | return 1; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 662 | static EGLImageKHR |
| 663 | drm_compositor_create_cursor_image(struct wlsc_compositor *ec, |
| 664 | int32_t width, int32_t height) |
| 665 | { |
Kristian Høgsberg | 7981d00 | 2011-05-06 13:23:49 -0400 | [diff] [blame] | 666 | static const EGLint image_attribs[] = { |
| 667 | EGL_WIDTH, 64, |
| 668 | EGL_HEIGHT, 64, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 669 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
Kristian Høgsberg | 7981d00 | 2011-05-06 13:23:49 -0400 | [diff] [blame] | 670 | EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_CURSOR_MESA, |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 671 | EGL_NONE |
| 672 | }; |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 673 | struct drm_compositor *c = (struct drm_compositor *) ec; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 674 | |
| 675 | if (width > 64 || height > 64) |
| 676 | return EGL_NO_IMAGE_KHR; |
| 677 | |
Kristian Høgsberg | 7981d00 | 2011-05-06 13:23:49 -0400 | [diff] [blame] | 678 | return c->create_drm_image(ec->display, image_attribs); |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 681 | static void |
| 682 | drm_destroy(struct wlsc_compositor *ec) |
| 683 | { |
| 684 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 685 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 686 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 687 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 688 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 689 | } |
| 690 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 691 | static void |
| 692 | vt_func(struct wlsc_compositor *compositor, int event) |
| 693 | { |
| 694 | struct drm_compositor *ec = (struct drm_compositor *) compositor; |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 695 | struct wlsc_output *output; |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 696 | |
| 697 | switch (event) { |
| 698 | case TTY_ENTER_VT: |
| 699 | compositor->focus = 1; |
| 700 | drmSetMaster(ec->drm.fd); |
| 701 | compositor->state = WLSC_COMPOSITOR_ACTIVE; |
| 702 | wlsc_compositor_damage_all(compositor); |
| 703 | break; |
| 704 | case TTY_LEAVE_VT: |
| 705 | compositor->focus = 0; |
| 706 | compositor->state = WLSC_COMPOSITOR_SLEEPING; |
| 707 | drmDropMaster(ec->drm.fd); |
Kristian Høgsberg | d8e181b | 2011-05-06 15:38:28 -0400 | [diff] [blame] | 708 | |
| 709 | wl_list_for_each(output, &ec->base.output_list, link) |
| 710 | drm_output_set_cursor(output, NULL); |
| 711 | |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 712 | break; |
| 713 | }; |
| 714 | } |
| 715 | |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 716 | static struct wlsc_compositor * |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 717 | drm_compositor_create(struct wl_display *display, int connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 718 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 719 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 720 | struct udev_enumerate *e; |
| 721 | struct udev_list_entry *entry; |
| 722 | struct udev_device *device; |
| 723 | const char *path; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 724 | struct wl_event_loop *loop; |
| 725 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 726 | ec = malloc(sizeof *ec); |
| 727 | if (ec == NULL) |
| 728 | return NULL; |
| 729 | |
| 730 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 731 | ec->udev = udev_new(); |
| 732 | if (ec->udev == NULL) { |
| 733 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 734 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 735 | } |
| 736 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 737 | e = udev_enumerate_new(ec->udev); |
| 738 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 739 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 740 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 741 | device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 742 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 743 | path = udev_list_entry_get_name(entry); |
| 744 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 745 | break; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 746 | } |
| 747 | udev_enumerate_unref(e); |
| 748 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 749 | if (device == NULL) { |
| 750 | fprintf(stderr, "no drm device found\n"); |
| 751 | return NULL; |
| 752 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 753 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 754 | ec->base.wl_display = display; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 755 | if (init_egl(ec, device) < 0) { |
| 756 | fprintf(stderr, "failed to initialize egl\n"); |
| 757 | return NULL; |
| 758 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 759 | |
| 760 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | 431da9a | 2011-04-20 11:02:58 +0200 | [diff] [blame] | 761 | ec->base.create_cursor_image = drm_compositor_create_cursor_image; |
| 762 | |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 763 | ec->base.focus = 1; |
| 764 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 765 | glGenFramebuffers(1, &ec->base.fbo); |
| 766 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 767 | |
Kristian Høgsberg | b5819dc | 2011-04-25 15:08:20 -0400 | [diff] [blame] | 768 | ec->create_drm_image = |
| 769 | (void *) eglGetProcAddress("eglCreateDRMImageMESA"); |
| 770 | ec->export_drm_image = |
| 771 | (void *) eglGetProcAddress("eglExportDRMImageMESA"); |
| 772 | |
Benjamin Franzke | d59eb1c | 2011-04-29 22:14:54 +0200 | [diff] [blame] | 773 | /* Can't init base class until we have a current egl context */ |
| 774 | if (wlsc_compositor_init(&ec->base, display) < 0) |
| 775 | return NULL; |
| 776 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 777 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 778 | fprintf(stderr, "failed to create output for %s\n", path); |
| 779 | return NULL; |
| 780 | } |
| 781 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 782 | evdev_input_add_devices(&ec->base, ec->udev); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 783 | |
| 784 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 785 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 786 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 787 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | 9396fc5 | 2011-05-06 15:15:37 -0400 | [diff] [blame] | 788 | ec->tty = tty_create(&ec->base, vt_func); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 789 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 790 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 791 | if (ec->udev_monitor == NULL) { |
| 792 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 793 | return NULL; |
| 794 | } |
| 795 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 796 | "drm", NULL); |
| 797 | ec->udev_drm_source = |
| 798 | wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor), |
| 799 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 800 | |
| 801 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 802 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 803 | return NULL; |
| 804 | } |
| 805 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 806 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 807 | } |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 808 | |
| 809 | struct wlsc_compositor * |
Kristian Høgsberg | 6c709a3 | 2011-05-06 14:52:41 -0400 | [diff] [blame] | 810 | backend_init(struct wl_display *display, char *options); |
| 811 | |
| 812 | WL_EXPORT struct wlsc_compositor * |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 813 | backend_init(struct wl_display *display, char *options) |
| 814 | { |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 815 | int connector = 0, i; |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 816 | char *p, *value; |
| 817 | |
| 818 | static char * const tokens[] = { "connector", NULL }; |
| 819 | |
| 820 | p = options; |
| 821 | while (i = getsubopt(&p, tokens, &value), i != -1) { |
| 822 | switch (i) { |
| 823 | case 0: |
Kristian Høgsberg | 8050bd2 | 2011-05-04 15:08:04 -0400 | [diff] [blame] | 824 | connector = strtol(value, NULL, 0); |
Kristian Høgsberg | 1c56218 | 2011-05-02 22:09:20 -0400 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return drm_compositor_create(display, connector); |
| 830 | } |