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