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 | |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <unistd.h> |
| 24 | |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 25 | #include <xf86drm.h> |
| 26 | #include <xf86drmMode.h> |
| 27 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 28 | #define GL_GLEXT_PROTOTYPES |
| 29 | #define EGL_EGLEXT_PROTOTYPES |
| 30 | #include <GLES2/gl2.h> |
| 31 | #include <GLES2/gl2ext.h> |
| 32 | #include <EGL/egl.h> |
| 33 | #include <EGL/eglext.h> |
| 34 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 35 | #include "compositor.h" |
| 36 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 37 | struct drm_compositor { |
| 38 | struct wlsc_compositor base; |
| 39 | |
| 40 | struct udev *udev; |
| 41 | struct wl_event_source *drm_source; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 42 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 43 | struct udev_monitor *udev_monitor; |
| 44 | struct wl_event_source *udev_drm_source; |
| 45 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 46 | struct { |
| 47 | int fd; |
| 48 | } drm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 49 | uint32_t crtc_allocator; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 50 | uint32_t connector_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 51 | struct tty *tty; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | struct drm_output { |
| 55 | struct wlsc_output base; |
| 56 | |
| 57 | drmModeModeInfo mode; |
| 58 | uint32_t crtc_id; |
| 59 | uint32_t connector_id; |
| 60 | GLuint rbo[2]; |
| 61 | uint32_t fb_id[2]; |
| 62 | EGLImageKHR image[2]; |
| 63 | uint32_t current; |
| 64 | }; |
| 65 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 66 | static int |
| 67 | drm_output_prepare_render(struct wlsc_output *output_base) |
| 68 | { |
| 69 | struct drm_output *output = (struct drm_output *) output_base; |
| 70 | |
| 71 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 72 | GL_COLOR_ATTACHMENT0, |
| 73 | GL_RENDERBUFFER, |
| 74 | output->rbo[output->current]); |
| 75 | |
| 76 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 77 | return -1; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 82 | static int |
| 83 | drm_output_present(struct wlsc_output *output_base) |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 84 | { |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 85 | struct drm_output *output = (struct drm_output *) output_base; |
| 86 | struct drm_compositor *c = |
| 87 | (struct drm_compositor *) output->base.compositor; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 88 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 89 | if (drm_output_prepare_render(&output->base)) |
| 90 | return -1; |
| 91 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 92 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 93 | output->current ^= 1; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 94 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 95 | drmModePageFlip(c->drm.fd, output->crtc_id, |
| 96 | output->fb_id[output->current ^ 1], |
| 97 | DRM_MODE_PAGE_FLIP_EVENT, output); |
| 98 | |
| 99 | return 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void |
| 103 | page_flip_handler(int fd, unsigned int frame, |
| 104 | unsigned int sec, unsigned int usec, void *data) |
| 105 | { |
| 106 | struct wlsc_output *output = data; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 107 | uint32_t msecs; |
| 108 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 109 | msecs = sec * 1000 + usec / 1000; |
| 110 | wlsc_output_finish_frame(output, msecs); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
| 114 | on_drm_input(int fd, uint32_t mask, void *data) |
| 115 | { |
| 116 | drmEventContext evctx; |
| 117 | |
| 118 | memset(&evctx, 0, sizeof evctx); |
| 119 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 120 | evctx.page_flip_handler = page_flip_handler; |
| 121 | drmHandleEvent(fd, &evctx); |
| 122 | } |
| 123 | |
| 124 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 125 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 126 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 127 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 128 | const char *extensions, *filename; |
| 129 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 130 | static const EGLint context_attribs[] = { |
| 131 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 132 | EGL_NONE |
| 133 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 134 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 135 | filename = udev_device_get_devnode(device); |
Kristian Høgsberg | 9cd7aca | 2011-04-11 13:19:16 -0400 | [diff] [blame] | 136 | fd = open(filename, O_RDWR, O_CLOEXEC); |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 137 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 138 | /* Probably permissions error */ |
| 139 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 140 | udev_device_get_devnode(device)); |
| 141 | return -1; |
| 142 | } |
| 143 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 144 | ec->drm.fd = fd; |
| 145 | ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 146 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 147 | fprintf(stderr, "failed to create display\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 151 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 152 | fprintf(stderr, "failed to initialize display\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 156 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
| 157 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 158 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 162 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 163 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 164 | return -1; |
| 165 | } |
| 166 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 167 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 168 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 169 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 170 | fprintf(stderr, "failed to create context\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 174 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 175 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 176 | fprintf(stderr, "failed to make context current\n"); |
| 177 | return -1; |
| 178 | } |
| 179 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static drmModeModeInfo builtin_1024x768 = { |
| 184 | 63500, /* clock */ |
| 185 | 1024, 1072, 1176, 1328, 0, |
| 186 | 768, 771, 775, 798, 0, |
| 187 | 59920, |
| 188 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 189 | 0, |
| 190 | "1024x768" |
| 191 | }; |
| 192 | |
| 193 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 194 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 195 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 196 | drmModeConnector *connector, |
| 197 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 198 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 199 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 200 | drmModeEncoder *encoder; |
| 201 | drmModeModeInfo *mode; |
| 202 | int i, ret; |
| 203 | EGLint handle, stride, attribs[] = { |
| 204 | EGL_WIDTH, 0, |
| 205 | EGL_HEIGHT, 0, |
Kristian Høgsberg | b12fcce | 2010-08-24 17:34:23 -0400 | [diff] [blame] | 206 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 207 | EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 208 | EGL_NONE |
| 209 | }; |
| 210 | |
| 211 | output = malloc(sizeof *output); |
| 212 | if (output == NULL) |
| 213 | return -1; |
| 214 | |
| 215 | if (connector->count_modes > 0) |
| 216 | mode = &connector->modes[0]; |
| 217 | else |
| 218 | mode = &builtin_1024x768; |
| 219 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 220 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 221 | if (encoder == NULL) { |
| 222 | fprintf(stderr, "No encoder for connector.\n"); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 227 | if (encoder->possible_crtcs & (1 << i) && |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 228 | !(ec->crtc_allocator & (1 << resources->crtcs[i]))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | if (i == resources->count_crtcs) { |
| 232 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 236 | memset(output, 0, sizeof *output); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 237 | wlsc_output_init(&output->base, &ec->base, x, y, |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 238 | mode->hdisplay, mode->vdisplay, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 239 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 240 | output->crtc_id = resources->crtcs[i]; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 241 | ec->crtc_allocator |= (1 << output->crtc_id); |
| 242 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 243 | output->connector_id = connector->connector_id; |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 244 | ec->connector_allocator |= (1 << output->connector_id); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 245 | output->mode = *mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 246 | |
| 247 | drmModeFreeEncoder(encoder); |
| 248 | |
| 249 | glGenRenderbuffers(2, output->rbo); |
| 250 | for (i = 0; i < 2; i++) { |
| 251 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 252 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 253 | attribs[1] = output->base.width; |
| 254 | attribs[3] = output->base.height; |
| 255 | output->image[i] = |
| 256 | eglCreateDRMImageMESA(ec->base.display, attribs); |
| 257 | glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
| 258 | output->image[i]); |
| 259 | eglExportDRMImageMESA(ec->base.display, output->image[i], |
| 260 | NULL, &handle, &stride); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 261 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 262 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 263 | output->base.width, output->base.height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 264 | 32, 32, stride, handle, &output->fb_id[i]); |
| 265 | if (ret) { |
| 266 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 267 | return -1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | output->current = 0; |
| 272 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 273 | GL_COLOR_ATTACHMENT0, |
| 274 | GL_RENDERBUFFER, |
| 275 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 276 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 277 | output->fb_id[output->current ^ 1], 0, 0, |
| 278 | &output->connector_id, 1, &output->mode); |
| 279 | if (ret) { |
| 280 | fprintf(stderr, "failed to set mode: %m\n"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 284 | output->base.prepare_render = drm_output_prepare_render; |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 285 | output->base.present = drm_output_present; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 286 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 287 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 293 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 294 | { |
| 295 | drmModeConnector *connector; |
| 296 | drmModeRes *resources; |
| 297 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 298 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 299 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 300 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 301 | if (!resources) { |
| 302 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 307 | connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 308 | if (connector == NULL) |
| 309 | continue; |
| 310 | |
| 311 | if (connector->connection == DRM_MODE_CONNECTED && |
| 312 | (option_connector == 0 || |
| 313 | connector->connector_id == option_connector)) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 314 | if (create_output_for_connector(ec, resources, |
| 315 | connector, x, y) < 0) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 316 | return -1; |
| 317 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame] | 318 | x += container_of(ec->base.output_list.prev, struct wlsc_output, |
| 319 | link)->width; |
| 320 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 321 | drmModeFreeConnector(connector); |
| 322 | } |
| 323 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 324 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 325 | fprintf(stderr, "No currently active connector found.\n"); |
| 326 | return -1; |
| 327 | } |
| 328 | |
| 329 | drmModeFreeResources(resources); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 334 | static int |
| 335 | destroy_output(struct drm_output *output) |
| 336 | { |
| 337 | struct drm_compositor *ec = |
| 338 | (struct drm_compositor *) output->base.compositor; |
| 339 | int i; |
| 340 | |
| 341 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 342 | GL_COLOR_ATTACHMENT0, |
| 343 | GL_RENDERBUFFER, |
| 344 | 0); |
| 345 | |
| 346 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 347 | glDeleteRenderbuffers(2, output->rbo); |
| 348 | |
| 349 | for (i = 0; i < 2; i++) { |
| 350 | eglDestroyImageKHR(ec->base.display, output->image[i]); |
| 351 | drmModeRmFB(ec->drm.fd, output->fb_id[i]); |
| 352 | } |
| 353 | |
| 354 | ec->crtc_allocator &= ~(1 << output->crtc_id); |
| 355 | ec->connector_allocator &= ~(1 << output->connector_id); |
| 356 | |
| 357 | wlsc_output_destroy(&output->base); |
| 358 | wl_list_remove(&output->base.link); |
| 359 | |
| 360 | free(output); |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | update_outputs(struct drm_compositor *ec) |
| 367 | { |
| 368 | drmModeConnector *connector; |
| 369 | drmModeRes *resources; |
| 370 | struct drm_output *output, *next; |
| 371 | int x = 0, y = 0; |
| 372 | int x_offset = 0, y_offset = 0; |
| 373 | uint32_t connected = 0, disconnects = 0; |
| 374 | int i; |
| 375 | |
| 376 | resources = drmModeGetResources(ec->drm.fd); |
| 377 | if (!resources) { |
| 378 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | /* collect new connects */ |
| 383 | for (i = 0; i < resources->count_connectors; i++) { |
| 384 | connector = |
| 385 | drmModeGetConnector(ec->drm.fd, |
| 386 | resources->connectors[i]); |
| 387 | if (connector == NULL || |
| 388 | connector->connection != DRM_MODE_CONNECTED) |
| 389 | continue; |
| 390 | |
| 391 | connected |= (1 << connector->connector_id); |
| 392 | |
| 393 | if (!(ec->connector_allocator & (1 << connector->connector_id))) { |
| 394 | struct wlsc_output *last_output = |
| 395 | container_of(ec->base.output_list.prev, |
| 396 | struct wlsc_output, link); |
| 397 | |
| 398 | /* XXX: not yet needed, we die with 0 outputs */ |
| 399 | if (!wl_list_empty(&ec->base.output_list)) |
| 400 | x = last_output->x + last_output->width; |
| 401 | else |
| 402 | x = 0; |
| 403 | y = 0; |
| 404 | create_output_for_connector(ec, resources, |
| 405 | connector, x, y); |
| 406 | printf("connector %d connected\n", |
| 407 | connector->connector_id); |
| 408 | |
| 409 | } |
| 410 | drmModeFreeConnector(connector); |
| 411 | } |
| 412 | drmModeFreeResources(resources); |
| 413 | |
| 414 | disconnects = ec->connector_allocator & ~connected; |
| 415 | if (disconnects) { |
| 416 | wl_list_for_each_safe(output, next, &ec->base.output_list, |
| 417 | base.link) { |
| 418 | if (x_offset != 0 || y_offset != 0) { |
| 419 | wlsc_output_move(&output->base, |
| 420 | output->base.x - x_offset, |
| 421 | output->base.y - y_offset); |
| 422 | } |
| 423 | |
| 424 | if (disconnects & (1 << output->connector_id)) { |
| 425 | disconnects &= ~(1 << output->connector_id); |
| 426 | printf("connector %d disconnected\n", |
| 427 | output->connector_id); |
| 428 | x_offset += output->base.width; |
| 429 | destroy_output(output); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* FIXME: handle zero outputs, without terminating */ |
| 435 | if (ec->connector_allocator == 0) |
| 436 | wl_display_terminate(ec->base.wl_display); |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | udev_event_is_hotplug(struct udev_device *device) |
| 441 | { |
| 442 | struct udev_list_entry *list, *hotplug_entry; |
| 443 | |
| 444 | list = udev_device_get_properties_list_entry(device); |
| 445 | |
| 446 | hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG"); |
| 447 | if (hotplug_entry == NULL) |
| 448 | return 0; |
| 449 | |
| 450 | return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0; |
| 451 | } |
| 452 | |
| 453 | static void |
| 454 | udev_drm_event(int fd, uint32_t mask, void *data) |
| 455 | { |
| 456 | struct drm_compositor *ec = data; |
| 457 | struct udev_device *event; |
| 458 | |
| 459 | event = udev_monitor_receive_device(ec->udev_monitor); |
| 460 | |
| 461 | if (udev_event_is_hotplug(event)) |
| 462 | update_outputs(ec); |
| 463 | |
| 464 | udev_device_unref(event); |
| 465 | } |
| 466 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 467 | static void |
| 468 | drm_destroy(struct wlsc_compositor *ec) |
| 469 | { |
| 470 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 471 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 472 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 473 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 474 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 477 | struct wlsc_compositor * |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 478 | drm_compositor_create(struct wl_display *display, int connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 479 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 480 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 481 | struct udev_enumerate *e; |
| 482 | struct udev_list_entry *entry; |
| 483 | struct udev_device *device; |
| 484 | const char *path; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 485 | struct wl_event_loop *loop; |
| 486 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 487 | ec = malloc(sizeof *ec); |
| 488 | if (ec == NULL) |
| 489 | return NULL; |
| 490 | |
| 491 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 492 | ec->udev = udev_new(); |
| 493 | if (ec->udev == NULL) { |
| 494 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 495 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 496 | } |
| 497 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 498 | e = udev_enumerate_new(ec->udev); |
| 499 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 500 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 501 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 502 | device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 503 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 504 | path = udev_list_entry_get_name(entry); |
| 505 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 506 | break; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 507 | } |
| 508 | udev_enumerate_unref(e); |
| 509 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 510 | if (device == NULL) { |
| 511 | fprintf(stderr, "no drm device found\n"); |
| 512 | return NULL; |
| 513 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 514 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 515 | ec->base.wl_display = display; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 516 | if (init_egl(ec, device) < 0) { |
| 517 | fprintf(stderr, "failed to initialize egl\n"); |
| 518 | return NULL; |
| 519 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 520 | |
| 521 | ec->base.destroy = drm_destroy; |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 522 | ec->base.create_buffer = wlsc_shm_buffer_create; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 523 | ec->base.focus = 1; |
| 524 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 525 | glGenFramebuffers(1, &ec->base.fbo); |
| 526 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 527 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 528 | /* Can't init base class until we have a current egl context */ |
Kristian Høgsberg | a946821 | 2010-06-14 21:03:11 -0400 | [diff] [blame] | 529 | if (wlsc_compositor_init(&ec->base, display) < 0) |
| 530 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 531 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 532 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 533 | fprintf(stderr, "failed to create output for %s\n", path); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 537 | evdev_input_add_devices(&ec->base, ec->udev); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 538 | |
| 539 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 540 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 541 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 542 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 543 | ec->tty = tty_create(&ec->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 544 | |
Benjamin Franzke | 9c26ff3 | 2011-03-15 15:08:41 +0100 | [diff] [blame] | 545 | ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev"); |
| 546 | if (ec->udev_monitor == NULL) { |
| 547 | fprintf(stderr, "failed to intialize udev monitor\n"); |
| 548 | return NULL; |
| 549 | } |
| 550 | udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor, |
| 551 | "drm", NULL); |
| 552 | ec->udev_drm_source = |
| 553 | wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor), |
| 554 | WL_EVENT_READABLE, udev_drm_event, ec); |
| 555 | |
| 556 | if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) { |
| 557 | fprintf(stderr, "failed to enable udev-monitor receiving\n"); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 561 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 562 | } |