2 * Copyright © 2008-2010 Kristian Høgsberg
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.
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.
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.
26 #include <xf86drmMode.h>
28 #define GL_GLEXT_PROTOTYPES
29 #define EGL_EGLEXT_PROTOTYPES
30 #include <GLES2/gl2.h>
31 #include <GLES2/gl2ext.h>
33 #include <EGL/eglext.h>
35 #include "compositor.h"
37 struct drm_compositor {
38 struct wlsc_compositor base;
41 struct wl_event_source *drm_source;
46 uint32_t crtc_allocator;
51 struct wlsc_output base;
55 uint32_t connector_id;
63 drm_output_prepare_render(struct wlsc_output *output_base)
65 struct drm_output *output = (struct drm_output *) output_base;
67 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
70 output->rbo[output->current]);
72 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
79 drm_compositor_present(struct wlsc_compositor *ec)
81 struct drm_compositor *c = (struct drm_compositor *) ec;
82 struct drm_output *output;
84 wl_list_for_each(output, &ec->output_list, base.link) {
85 if (drm_output_prepare_render(&output->base))
91 drmModePageFlip(c->drm.fd, output->crtc_id,
92 output->fb_id[output->current ^ 1],
93 DRM_MODE_PAGE_FLIP_EVENT, output);
98 page_flip_handler(int fd, unsigned int frame,
99 unsigned int sec, unsigned int usec, void *data)
101 struct wlsc_output *output = data;
102 struct wlsc_compositor *compositor = output->compositor;
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);
115 on_drm_input(int fd, uint32_t mask, void *data)
117 drmEventContext evctx;
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);
126 init_egl(struct drm_compositor *ec, struct udev_device *device)
129 const char *extensions, *filename;
131 static const EGLint context_attribs[] = {
132 EGL_CONTEXT_CLIENT_VERSION, 2,
136 filename = udev_device_get_devnode(device);
137 fd = open(filename, O_RDWR);
139 /* Probably permissions error */
140 fprintf(stderr, "couldn't open %s, skipping\n",
141 udev_device_get_devnode(device));
146 ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
147 if (ec->base.display == NULL) {
148 fprintf(stderr, "failed to create display\n");
152 if (!eglInitialize(ec->base.display, &major, &minor)) {
153 fprintf(stderr, "failed to initialize display\n");
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");
163 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
164 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
168 ec->base.context = eglCreateContext(ec->base.display, NULL,
169 EGL_NO_CONTEXT, context_attribs);
170 if (ec->base.context == NULL) {
171 fprintf(stderr, "failed to create context\n");
175 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
176 EGL_NO_SURFACE, ec->base.context)) {
177 fprintf(stderr, "failed to make context current\n");
184 static drmModeModeInfo builtin_1024x768 = {
186 1024, 1072, 1176, 1328, 0,
187 768, 771, 775, 798, 0,
189 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
195 create_output_for_connector(struct drm_compositor *ec,
196 drmModeRes *resources,
197 drmModeConnector *connector,
200 struct drm_output *output;
201 drmModeEncoder *encoder;
202 drmModeModeInfo *mode;
204 EGLint handle, stride, attribs[] = {
207 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
208 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
212 output = malloc(sizeof *output);
216 if (connector->count_modes > 0)
217 mode = &connector->modes[0];
219 mode = &builtin_1024x768;
221 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
222 if (encoder == NULL) {
223 fprintf(stderr, "No encoder for connector.\n");
227 for (i = 0; i < resources->count_crtcs; i++) {
228 if (encoder->possible_crtcs & (1 << i) &&
229 !(ec->crtc_allocator & (1 << i)))
232 if (i == resources->count_crtcs) {
233 fprintf(stderr, "No usable crtc for encoder.\n");
237 memset(output, 0, sizeof *output);
238 wlsc_output_init(&output->base, &ec->base, x, y,
239 mode->hdisplay, mode->vdisplay, 0);
241 ec->crtc_allocator |= (1 << i);
242 output->crtc_id = resources->crtcs[i];
243 output->connector_id = connector->connector_id;
244 output->mode = *mode;
246 drmModeFreeEncoder(encoder);
248 glGenRenderbuffers(2, output->rbo);
249 for (i = 0; i < 2; i++) {
250 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
252 attribs[1] = output->base.width;
253 attribs[3] = output->base.height;
255 eglCreateDRMImageMESA(ec->base.display, attribs);
256 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
258 eglExportDRMImageMESA(ec->base.display, output->image[i],
259 NULL, &handle, &stride);
261 ret = drmModeAddFB(ec->drm.fd,
262 output->base.width, output->base.height,
263 32, 32, stride, handle, &output->fb_id[i]);
265 fprintf(stderr, "failed to add fb %d: %m\n", i);
271 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
272 GL_COLOR_ATTACHMENT0,
274 output->rbo[output->current]);
275 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
276 output->fb_id[output->current ^ 1], 0, 0,
277 &output->connector_id, 1, &output->mode);
279 fprintf(stderr, "failed to set mode: %m\n");
283 output->base.prepare_render = drm_output_prepare_render;
285 wl_list_insert(ec->base.output_list.prev, &output->base.link);
291 create_outputs(struct drm_compositor *ec, int option_connector)
293 drmModeConnector *connector;
294 drmModeRes *resources;
298 resources = drmModeGetResources(ec->drm.fd);
300 fprintf(stderr, "drmModeGetResources failed\n");
304 for (i = 0; i < resources->count_connectors; i++) {
305 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
306 if (connector == NULL)
309 if (connector->connection == DRM_MODE_CONNECTED &&
310 (option_connector == 0 ||
311 connector->connector_id == option_connector))
312 if (create_output_for_connector(ec, resources,
313 connector, x, y) < 0)
316 x += container_of(ec->base.output_list.prev, struct wlsc_output,
319 drmModeFreeConnector(connector);
322 if (wl_list_empty(&ec->base.output_list)) {
323 fprintf(stderr, "No currently active connector found.\n");
327 drmModeFreeResources(resources);
333 drm_destroy(struct wlsc_compositor *ec)
335 struct drm_compositor *d = (struct drm_compositor *) ec;
342 struct wlsc_compositor *
343 drm_compositor_create(struct wl_display *display, int connector)
345 struct drm_compositor *ec;
346 struct udev_enumerate *e;
347 struct udev_list_entry *entry;
348 struct udev_device *device;
350 struct wl_event_loop *loop;
352 ec = malloc(sizeof *ec);
356 memset(ec, 0, sizeof *ec);
357 ec->udev = udev_new();
358 if (ec->udev == NULL) {
359 fprintf(stderr, "failed to initialize udev context\n");
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);
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);
373 udev_enumerate_unref(e);
375 if (device == NULL) {
376 fprintf(stderr, "no drm device found\n");
380 ec->base.wl_display = display;
381 if (init_egl(ec, device) < 0) {
382 fprintf(stderr, "failed to initialize egl\n");
386 ec->base.destroy = drm_destroy;
387 ec->base.present = drm_compositor_present;
388 ec->base.create_buffer = wlsc_shm_buffer_create;
391 glGenFramebuffers(1, &ec->base.fbo);
392 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
394 /* Can't init base class until we have a current egl context */
395 if (wlsc_compositor_init(&ec->base, display) < 0)
398 if (create_outputs(ec, connector) < 0) {
399 fprintf(stderr, "failed to create output for %s\n", path);
403 evdev_input_add_devices(&ec->base, ec->udev);
405 loop = wl_display_get_event_loop(ec->base.wl_display);
407 wl_event_loop_add_fd(loop, ec->drm.fd,
408 WL_EVENT_READABLE, on_drm_input, ec);
409 ec->tty = tty_create(&ec->base);