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 | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 43 | struct { |
| 44 | int fd; |
| 45 | } drm; |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 46 | uint32_t crtc_allocator; |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 47 | struct tty *tty; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct drm_output { |
| 51 | struct wlsc_output base; |
| 52 | |
| 53 | drmModeModeInfo mode; |
| 54 | uint32_t crtc_id; |
| 55 | uint32_t connector_id; |
| 56 | GLuint rbo[2]; |
| 57 | uint32_t fb_id[2]; |
| 58 | EGLImageKHR image[2]; |
| 59 | uint32_t current; |
| 60 | }; |
| 61 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 62 | static int |
| 63 | drm_output_prepare_render(struct wlsc_output *output_base) |
| 64 | { |
| 65 | struct drm_output *output = (struct drm_output *) output_base; |
| 66 | |
| 67 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 68 | GL_COLOR_ATTACHMENT0, |
| 69 | GL_RENDERBUFFER, |
| 70 | output->rbo[output->current]); |
| 71 | |
| 72 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) |
| 73 | return -1; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 78 | static void |
| 79 | drm_compositor_present(struct wlsc_compositor *ec) |
| 80 | { |
| 81 | struct drm_compositor *c = (struct drm_compositor *) ec; |
| 82 | struct drm_output *output; |
| 83 | |
| 84 | wl_list_for_each(output, &ec->output_list, base.link) { |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 85 | if (drm_output_prepare_render(&output->base)) |
| 86 | continue; |
Benjamin Franzke | feb370e | 2011-02-14 13:20:09 +0100 | [diff] [blame] | 87 | glFlush(); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 88 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 89 | output->current ^= 1; |
| 90 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 91 | drmModePageFlip(c->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 92 | output->fb_id[output->current ^ 1], |
| 93 | DRM_MODE_PAGE_FLIP_EVENT, output); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | page_flip_handler(int fd, unsigned int frame, |
| 99 | unsigned int sec, unsigned int usec, void *data) |
| 100 | { |
| 101 | struct wlsc_output *output = data; |
| 102 | struct wlsc_compositor *compositor = output->compositor; |
| 103 | uint32_t msecs; |
| 104 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 105 | /* run synchronized to first output, ignore other pflip events. |
| 106 | * FIXME: support per output/surface frame callbacks */ |
| 107 | if (output == container_of(compositor->output_list.prev, |
| 108 | struct wlsc_output, link)) { |
| 109 | msecs = sec * 1000 + usec / 1000; |
| 110 | wlsc_compositor_finish_frame(compositor, msecs); |
| 111 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void |
| 115 | on_drm_input(int fd, uint32_t mask, void *data) |
| 116 | { |
| 117 | drmEventContext evctx; |
| 118 | |
| 119 | memset(&evctx, 0, sizeof evctx); |
| 120 | evctx.version = DRM_EVENT_CONTEXT_VERSION; |
| 121 | evctx.page_flip_handler = page_flip_handler; |
| 122 | drmHandleEvent(fd, &evctx); |
| 123 | } |
| 124 | |
| 125 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 126 | init_egl(struct drm_compositor *ec, struct udev_device *device) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 127 | { |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 128 | EGLint major, minor; |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 129 | const char *extensions, *filename; |
| 130 | int fd; |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 131 | static const EGLint context_attribs[] = { |
| 132 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 133 | EGL_NONE |
| 134 | }; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 135 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 136 | filename = udev_device_get_devnode(device); |
| 137 | fd = open(filename, O_RDWR); |
| 138 | if (fd < 0) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 139 | /* Probably permissions error */ |
| 140 | fprintf(stderr, "couldn't open %s, skipping\n", |
| 141 | udev_device_get_devnode(device)); |
| 142 | return -1; |
| 143 | } |
| 144 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 145 | ec->drm.fd = fd; |
| 146 | ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 147 | if (ec->base.display == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 148 | fprintf(stderr, "failed to create display\n"); |
| 149 | return -1; |
| 150 | } |
| 151 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 152 | if (!eglInitialize(ec->base.display, &major, &minor)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 153 | fprintf(stderr, "failed to initialize display\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | |
Kristian Høgsberg | 379b678 | 2010-07-28 22:52:28 -0400 | [diff] [blame] | 157 | extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS); |
| 158 | if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) { |
| 159 | fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n"); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 160 | return -1; |
| 161 | } |
| 162 | |
Darxus | 55973f2 | 2010-11-22 21:24:39 -0500 | [diff] [blame] | 163 | if (!eglBindAPI(EGL_OPENGL_ES_API)) { |
| 164 | fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n"); |
| 165 | return -1; |
| 166 | } |
| 167 | |
Kristian Høgsberg | 2c28aa5 | 2010-07-28 23:47:54 -0400 | [diff] [blame] | 168 | ec->base.context = eglCreateContext(ec->base.display, NULL, |
| 169 | EGL_NO_CONTEXT, context_attribs); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 170 | if (ec->base.context == NULL) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 171 | fprintf(stderr, "failed to create context\n"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 175 | if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE, |
| 176 | EGL_NO_SURFACE, ec->base.context)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 177 | fprintf(stderr, "failed to make context current\n"); |
| 178 | return -1; |
| 179 | } |
| 180 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static drmModeModeInfo builtin_1024x768 = { |
| 185 | 63500, /* clock */ |
| 186 | 1024, 1072, 1176, 1328, 0, |
| 187 | 768, 771, 775, 798, 0, |
| 188 | 59920, |
| 189 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC, |
| 190 | 0, |
| 191 | "1024x768" |
| 192 | }; |
| 193 | |
| 194 | static int |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 195 | create_output_for_connector(struct drm_compositor *ec, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 196 | drmModeRes *resources, |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 197 | drmModeConnector *connector, |
| 198 | int x, int y) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 199 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 200 | struct drm_output *output; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 201 | drmModeEncoder *encoder; |
| 202 | drmModeModeInfo *mode; |
| 203 | int i, ret; |
| 204 | EGLint handle, stride, attribs[] = { |
| 205 | EGL_WIDTH, 0, |
| 206 | EGL_HEIGHT, 0, |
Kristian Høgsberg | b12fcce | 2010-08-24 17:34:23 -0400 | [diff] [blame] | 207 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 208 | EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 209 | EGL_NONE |
| 210 | }; |
| 211 | |
| 212 | output = malloc(sizeof *output); |
| 213 | if (output == NULL) |
| 214 | return -1; |
| 215 | |
| 216 | if (connector->count_modes > 0) |
| 217 | mode = &connector->modes[0]; |
| 218 | else |
| 219 | mode = &builtin_1024x768; |
| 220 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 221 | encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 222 | if (encoder == NULL) { |
| 223 | fprintf(stderr, "No encoder for connector.\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | for (i = 0; i < resources->count_crtcs; i++) { |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 228 | if (encoder->possible_crtcs & (1 << i) && |
| 229 | !(ec->crtc_allocator & (1 << i))) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | if (i == resources->count_crtcs) { |
| 233 | fprintf(stderr, "No usable crtc for encoder.\n"); |
| 234 | return -1; |
| 235 | } |
| 236 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 237 | memset(output, 0, sizeof *output); |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 238 | wlsc_output_init(&output->base, &ec->base, x, y, |
Benjamin Franzke | 1b765ff | 2011-02-18 16:51:37 +0100 | [diff] [blame] | 239 | mode->hdisplay, mode->vdisplay, 0); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 240 | |
Marty Jack | 13d9db2 | 2011-02-09 19:01:42 -0500 | [diff] [blame] | 241 | ec->crtc_allocator |= (1 << i); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 242 | output->crtc_id = resources->crtcs[i]; |
| 243 | output->connector_id = connector->connector_id; |
| 244 | output->mode = *mode; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 245 | |
| 246 | drmModeFreeEncoder(encoder); |
| 247 | |
| 248 | glGenRenderbuffers(2, output->rbo); |
| 249 | for (i = 0; i < 2; i++) { |
| 250 | glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]); |
| 251 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 252 | attribs[1] = output->base.width; |
| 253 | attribs[3] = output->base.height; |
| 254 | output->image[i] = |
| 255 | eglCreateDRMImageMESA(ec->base.display, attribs); |
| 256 | glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
| 257 | output->image[i]); |
| 258 | eglExportDRMImageMESA(ec->base.display, output->image[i], |
| 259 | NULL, &handle, &stride); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 260 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 261 | ret = drmModeAddFB(ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 262 | output->base.width, output->base.height, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 263 | 32, 32, stride, handle, &output->fb_id[i]); |
| 264 | if (ret) { |
| 265 | fprintf(stderr, "failed to add fb %d: %m\n", i); |
| 266 | return -1; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | output->current = 0; |
| 271 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 272 | GL_COLOR_ATTACHMENT0, |
| 273 | GL_RENDERBUFFER, |
| 274 | output->rbo[output->current]); |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 275 | ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id, |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 276 | output->fb_id[output->current ^ 1], 0, 0, |
| 277 | &output->connector_id, 1, &output->mode); |
| 278 | if (ret) { |
| 279 | fprintf(stderr, "failed to set mode: %m\n"); |
| 280 | return -1; |
| 281 | } |
| 282 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 283 | output->base.prepare_render = drm_output_prepare_render; |
| 284 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 285 | wl_list_insert(ec->base.output_list.prev, &output->base.link); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 291 | create_outputs(struct drm_compositor *ec, int option_connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 292 | { |
| 293 | drmModeConnector *connector; |
| 294 | drmModeRes *resources; |
| 295 | int i; |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 296 | int x = 0, y = 0; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 297 | |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 298 | resources = drmModeGetResources(ec->drm.fd); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 299 | if (!resources) { |
| 300 | fprintf(stderr, "drmModeGetResources failed\n"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | for (i = 0; i < resources->count_connectors; i++) { |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 305 | connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 306 | if (connector == NULL) |
| 307 | continue; |
| 308 | |
| 309 | if (connector->connection == DRM_MODE_CONNECTED && |
| 310 | (option_connector == 0 || |
| 311 | connector->connector_id == option_connector)) |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 312 | if (create_output_for_connector(ec, resources, |
| 313 | connector, x, y) < 0) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 314 | return -1; |
| 315 | |
Benjamin Franzke | eefc36c | 2011-03-11 16:39:20 +0100 | [diff] [blame^] | 316 | x += container_of(ec->base.output_list.prev, struct wlsc_output, |
| 317 | link)->width; |
| 318 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 319 | drmModeFreeConnector(connector); |
| 320 | } |
| 321 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 322 | if (wl_list_empty(&ec->base.output_list)) { |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 323 | fprintf(stderr, "No currently active connector found.\n"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | drmModeFreeResources(resources); |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 332 | static void |
| 333 | drm_destroy(struct wlsc_compositor *ec) |
| 334 | { |
| 335 | struct drm_compositor *d = (struct drm_compositor *) ec; |
| 336 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 337 | tty_destroy(d->tty); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 338 | |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 339 | free(d); |
Kristian Høgsberg | caa6442 | 2010-12-01 16:52:15 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 342 | struct wlsc_compositor * |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 343 | drm_compositor_create(struct wl_display *display, int connector) |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 344 | { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 345 | struct drm_compositor *ec; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 346 | struct udev_enumerate *e; |
| 347 | struct udev_list_entry *entry; |
| 348 | struct udev_device *device; |
| 349 | const char *path; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 350 | struct wl_event_loop *loop; |
| 351 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 352 | ec = malloc(sizeof *ec); |
| 353 | if (ec == NULL) |
| 354 | return NULL; |
| 355 | |
| 356 | memset(ec, 0, sizeof *ec); |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 357 | ec->udev = udev_new(); |
| 358 | if (ec->udev == NULL) { |
| 359 | fprintf(stderr, "failed to initialize udev context\n"); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 360 | return NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 363 | e = udev_enumerate_new(ec->udev); |
| 364 | udev_enumerate_add_match_subsystem(e, "drm"); |
| 365 | udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1"); |
| 366 | udev_enumerate_scan_devices(e); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 367 | device = NULL; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 368 | udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) { |
| 369 | path = udev_list_entry_get_name(entry); |
| 370 | device = udev_device_new_from_syspath(ec->udev, path); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 371 | break; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 372 | } |
| 373 | udev_enumerate_unref(e); |
| 374 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 375 | if (device == NULL) { |
| 376 | fprintf(stderr, "no drm device found\n"); |
| 377 | return NULL; |
| 378 | } |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 379 | |
Kristian Høgsberg | 5fcd0aa | 2010-08-09 14:43:33 -0400 | [diff] [blame] | 380 | ec->base.wl_display = display; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 381 | if (init_egl(ec, device) < 0) { |
| 382 | fprintf(stderr, "failed to initialize egl\n"); |
| 383 | return NULL; |
| 384 | } |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 385 | |
| 386 | ec->base.destroy = drm_destroy; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 387 | ec->base.present = drm_compositor_present; |
Benjamin Franzke | c649a92 | 2011-03-02 11:56:04 +0100 | [diff] [blame] | 388 | ec->base.create_buffer = wlsc_shm_buffer_create; |
Kristian Høgsberg | 8525a50 | 2011-01-14 16:20:21 -0500 | [diff] [blame] | 389 | ec->base.focus = 1; |
| 390 | |
Benjamin Franzke | 5b2cb6f | 2011-02-18 16:54:55 +0100 | [diff] [blame] | 391 | glGenFramebuffers(1, &ec->base.fbo); |
| 392 | glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo); |
| 393 | |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 394 | /* 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] | 395 | if (wlsc_compositor_init(&ec->base, display) < 0) |
| 396 | return NULL; |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 397 | |
Kristian Høgsberg | 61a8251 | 2010-10-26 11:26:44 -0400 | [diff] [blame] | 398 | if (create_outputs(ec, connector) < 0) { |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 399 | fprintf(stderr, "failed to create output for %s\n", path); |
| 400 | return NULL; |
| 401 | } |
| 402 | |
Kristian Høgsberg | 43db401 | 2011-01-14 14:45:42 -0500 | [diff] [blame] | 403 | evdev_input_add_devices(&ec->base, ec->udev); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 404 | |
| 405 | loop = wl_display_get_event_loop(ec->base.wl_display); |
| 406 | ec->drm_source = |
Benjamin Franzke | 2af7f10 | 2011-03-02 11:14:59 +0100 | [diff] [blame] | 407 | wl_event_loop_add_fd(loop, ec->drm.fd, |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 408 | WL_EVENT_READABLE, on_drm_input, ec); |
Kristian Høgsberg | e4762a6 | 2011-01-14 14:59:13 -0500 | [diff] [blame] | 409 | ec->tty = tty_create(&ec->base); |
Kristian Høgsberg | ce5325d | 2010-06-14 11:54:00 -0400 | [diff] [blame] | 410 | |
| 411 | return &ec->base; |
Kristian Høgsberg | fc783d4 | 2010-06-11 12:56:24 -0400 | [diff] [blame] | 412 | } |