blob: b2eead6eff21da4fa3d6f7574199fdfdc33b4e27 [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
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 Franzkec649a922011-03-02 11:56:04 +010025#include <xf86drm.h>
26#include <xf86drmMode.h>
27
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040028#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øgsbergfc783d42010-06-11 12:56:24 -040035#include "compositor.h"
36
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037struct drm_compositor {
38 struct wlsc_compositor base;
39
40 struct udev *udev;
41 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040042
Benjamin Franzke2af7f102011-03-02 11:14:59 +010043 struct {
44 int fd;
45 } drm;
Marty Jack13d9db22011-02-09 19:01:42 -050046 uint32_t crtc_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050047 struct tty *tty;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040048};
49
50struct 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 Franzkeeefc36c2011-03-11 16:39:20 +010062static int
63drm_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øgsbergce5325d2010-06-14 11:54:00 -040078static void
79drm_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 Franzkeeefc36c2011-03-11 16:39:20 +010085 if (drm_output_prepare_render(&output->base))
86 continue;
Benjamin Franzkefeb370e2011-02-14 13:20:09 +010087 glFlush();
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040088
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010089 output->current ^= 1;
90
Benjamin Franzke2af7f102011-03-02 11:14:59 +010091 drmModePageFlip(c->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040092 output->fb_id[output->current ^ 1],
93 DRM_MODE_PAGE_FLIP_EVENT, output);
94 }
95}
96
97static void
98page_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 Franzkeeefc36c2011-03-11 16:39:20 +0100105 /* 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øgsbergfc783d42010-06-11 12:56:24 -0400112}
113
114static void
115on_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
125static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400126init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400127{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400128 EGLint major, minor;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400129 const char *extensions, *filename;
130 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400131 static const EGLint context_attribs[] = {
132 EGL_CONTEXT_CLIENT_VERSION, 2,
133 EGL_NONE
134 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400135
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400136 filename = udev_device_get_devnode(device);
137 fd = open(filename, O_RDWR);
138 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400139 /* Probably permissions error */
140 fprintf(stderr, "couldn't open %s, skipping\n",
141 udev_device_get_devnode(device));
142 return -1;
143 }
144
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100145 ec->drm.fd = fd;
146 ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400147 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400148 fprintf(stderr, "failed to create display\n");
149 return -1;
150 }
151
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400152 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400153 fprintf(stderr, "failed to initialize display\n");
154 return -1;
155 }
156
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400157 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øgsbergfc783d42010-06-11 12:56:24 -0400160 return -1;
161 }
162
Darxus55973f22010-11-22 21:24:39 -0500163 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øgsberg2c28aa52010-07-28 23:47:54 -0400168 ec->base.context = eglCreateContext(ec->base.display, NULL,
169 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400170 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400171 fprintf(stderr, "failed to create context\n");
172 return -1;
173 }
174
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400175 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
176 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400177 fprintf(stderr, "failed to make context current\n");
178 return -1;
179 }
180
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400181 return 0;
182}
183
184static 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
194static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400195create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400196 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100197 drmModeConnector *connector,
198 int x, int y)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400199{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400200 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400201 drmModeEncoder *encoder;
202 drmModeModeInfo *mode;
203 int i, ret;
204 EGLint handle, stride, attribs[] = {
205 EGL_WIDTH, 0,
206 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400207 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øgsbergfc783d42010-06-11 12:56:24 -0400209 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 Franzke2af7f102011-03-02 11:14:59 +0100221 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400222 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 Jack13d9db22011-02-09 19:01:42 -0500228 if (encoder->possible_crtcs & (1 << i) &&
229 !(ec->crtc_allocator & (1 << i)))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400230 break;
231 }
232 if (i == resources->count_crtcs) {
233 fprintf(stderr, "No usable crtc for encoder.\n");
234 return -1;
235 }
236
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400237 memset(output, 0, sizeof *output);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100238 wlsc_output_init(&output->base, &ec->base, x, y,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +0100239 mode->hdisplay, mode->vdisplay, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400240
Marty Jack13d9db22011-02-09 19:01:42 -0500241 ec->crtc_allocator |= (1 << i);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400242 output->crtc_id = resources->crtcs[i];
243 output->connector_id = connector->connector_id;
244 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400245
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øgsbergce5325d2010-06-14 11:54:00 -0400252 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øgsbergfc783d42010-06-11 12:56:24 -0400260
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100261 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400262 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400263 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 Franzke2af7f102011-03-02 11:14:59 +0100275 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400276 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 Franzkeeefc36c2011-03-11 16:39:20 +0100283 output->base.prepare_render = drm_output_prepare_render;
284
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400285 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400286
287 return 0;
288}
289
290static int
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400291create_outputs(struct drm_compositor *ec, int option_connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400292{
293 drmModeConnector *connector;
294 drmModeRes *resources;
295 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100296 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400297
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100298 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400299 if (!resources) {
300 fprintf(stderr, "drmModeGetResources failed\n");
301 return -1;
302 }
303
304 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100305 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400306 if (connector == NULL)
307 continue;
308
309 if (connector->connection == DRM_MODE_CONNECTED &&
310 (option_connector == 0 ||
311 connector->connector_id == option_connector))
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100312 if (create_output_for_connector(ec, resources,
313 connector, x, y) < 0)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400314 return -1;
315
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100316 x += container_of(ec->base.output_list.prev, struct wlsc_output,
317 link)->width;
318
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400319 drmModeFreeConnector(connector);
320 }
321
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400322 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400323 fprintf(stderr, "No currently active connector found.\n");
324 return -1;
325 }
326
327 drmModeFreeResources(resources);
328
329 return 0;
330}
331
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500332static void
333drm_destroy(struct wlsc_compositor *ec)
334{
335 struct drm_compositor *d = (struct drm_compositor *) ec;
336
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500337 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500338
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500339 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500340}
341
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400342struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400343drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400344{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400345 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400346 struct udev_enumerate *e;
347 struct udev_list_entry *entry;
348 struct udev_device *device;
349 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400350 struct wl_event_loop *loop;
351
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400352 ec = malloc(sizeof *ec);
353 if (ec == NULL)
354 return NULL;
355
356 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400357 ec->udev = udev_new();
358 if (ec->udev == NULL) {
359 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400360 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400361 }
362
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400363 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øgsbergce5325d2010-06-14 11:54:00 -0400367 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400368 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øgsbergce5325d2010-06-14 11:54:00 -0400371 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400372 }
373 udev_enumerate_unref(e);
374
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375 if (device == NULL) {
376 fprintf(stderr, "no drm device found\n");
377 return NULL;
378 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400379
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400380 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400381 if (init_egl(ec, device) < 0) {
382 fprintf(stderr, "failed to initialize egl\n");
383 return NULL;
384 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500385
386 ec->base.destroy = drm_destroy;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500387 ec->base.present = drm_compositor_present;
Benjamin Franzkec649a922011-03-02 11:56:04 +0100388 ec->base.create_buffer = wlsc_shm_buffer_create;
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500389 ec->base.focus = 1;
390
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100391 glGenFramebuffers(1, &ec->base.fbo);
392 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
393
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400394 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400395 if (wlsc_compositor_init(&ec->base, display) < 0)
396 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400397
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400398 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400399 fprintf(stderr, "failed to create output for %s\n", path);
400 return NULL;
401 }
402
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500403 evdev_input_add_devices(&ec->base, ec->udev);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400404
405 loop = wl_display_get_event_loop(ec->base.wl_display);
406 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100407 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400408 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500409 ec->tty = tty_create(&ec->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400410
411 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400412}