blob: 4c3b1f686895d519d43748ddd13344fef97509c9 [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
Kristian Høgsberg0b9334a2011-04-12 11:34:32 -040019#define _GNU_SOURCE
20
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <fcntl.h>
25#include <unistd.h>
26
Benjamin Franzkec649a922011-03-02 11:56:04 +010027#include <xf86drm.h>
28#include <xf86drmMode.h>
29
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030#define GL_GLEXT_PROTOTYPES
31#define EGL_EGLEXT_PROTOTYPES
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34#include <EGL/egl.h>
35#include <EGL/eglext.h>
36
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037#include "compositor.h"
38
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040039struct drm_compositor {
40 struct wlsc_compositor base;
41
42 struct udev *udev;
43 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040044
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010045 struct udev_monitor *udev_monitor;
46 struct wl_event_source *udev_drm_source;
47
Benjamin Franzke2af7f102011-03-02 11:14:59 +010048 struct {
49 int fd;
50 } drm;
Marty Jack13d9db22011-02-09 19:01:42 -050051 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010052 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050053 struct tty *tty;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040054};
55
56struct drm_output {
57 struct wlsc_output base;
58
59 drmModeModeInfo mode;
60 uint32_t crtc_id;
61 uint32_t connector_id;
62 GLuint rbo[2];
63 uint32_t fb_id[2];
64 EGLImageKHR image[2];
65 uint32_t current;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020066
Benjamin Franzke66aa2352011-04-20 17:06:13 +020067 struct wlsc_surface *scanout_surface;
68
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020069 uint32_t fs_surf_fb_id;
70 uint32_t pending_fs_surf_fb_id;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040071};
72
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010073static int
74drm_output_prepare_render(struct wlsc_output *output_base)
75{
76 struct drm_output *output = (struct drm_output *) output_base;
77
78 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
79 GL_COLOR_ATTACHMENT0,
80 GL_RENDERBUFFER,
81 output->rbo[output->current]);
82
83 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
84 return -1;
85
86 return 0;
87}
88
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010089static int
90drm_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040091{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010092 struct drm_output *output = (struct drm_output *) output_base;
93 struct drm_compositor *c =
94 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020095 uint32_t fb_id = 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010097 if (drm_output_prepare_render(&output->base))
98 return -1;
99 glFlush();
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400100
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100101 output->current ^= 1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100102
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200103 if (output->scanout_surface != NULL) {
104 output->scanout_surface = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200105 fb_id = output->fs_surf_fb_id;
106 } else {
107 fb_id = output->fb_id[output->current ^ 1];
108 }
109
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100110 drmModePageFlip(c->drm.fd, output->crtc_id,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200111 fb_id,
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100112 DRM_MODE_PAGE_FLIP_EVENT, output);
113
114 return 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400115}
116
117static void
118page_flip_handler(int fd, unsigned int frame,
119 unsigned int sec, unsigned int usec, void *data)
120{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200121 struct drm_output *output = (struct drm_output *) data;
122 struct drm_compositor *c =
123 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400124 uint32_t msecs;
125
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200126 if (output->pending_fs_surf_fb_id) {
127 drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id);
128 output->pending_fs_surf_fb_id = 0;
129 }
130
131 if (output->fs_surf_fb_id) {
132 output->pending_fs_surf_fb_id = output->fs_surf_fb_id;
133 output->fs_surf_fb_id = 0;
134 }
135
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100136 msecs = sec * 1000 + usec / 1000;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200137 wlsc_output_finish_frame(&output->base, msecs);
138}
139
140static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200141drm_output_prepare_scanout_surface(struct wlsc_output *output_base,
142 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200143{
144 struct drm_output *output = (struct drm_output *) output_base;
145 struct drm_compositor *c =
146 (struct drm_compositor *) output->base.compositor;
147 EGLint handle, stride;
148 int ret;
149 uint32_t fb_id = 0;
150
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200151 if (es->width != output->base.width ||
152 es->height != output->base.height ||
153 es->image == EGL_NO_IMAGE_KHR)
154 return -1;
155
156 eglExportDRMImageMESA(c->base.display, es->image,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200157 NULL, &handle, &stride);
158
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200159 if (handle == 0)
160 return -1;
161
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200162 ret = drmModeAddFB(c->drm.fd,
163 output->base.width, output->base.height,
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200164 32, 32, stride, handle, &fb_id);
165
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200166 if (ret)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200167 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200168
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200169 output->fs_surf_fb_id = fb_id;
170 output->scanout_surface = es;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200171
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200172 return 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400173}
174
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200175static int
176drm_output_set_cursor(struct wlsc_output *output_base,
177 struct wl_input_device *input)
178{
179 struct drm_output *output = (struct drm_output *) output_base;
180 struct drm_compositor *c =
181 (struct drm_compositor *) output->base.compositor;
182 struct wlsc_input_device *eid = (struct wlsc_input_device *) input;
183 EGLint handle, stride;
184 int ret = -1;
185 pixman_region32_t cursor_region;
186
187 pixman_region32_init_rect(&cursor_region,
188 eid->sprite->x, eid->sprite->y,
189 eid->sprite->width, eid->sprite->height);
190
191 pixman_region32_intersect_rect(&cursor_region, &cursor_region,
192 output->base.x, output->base.y,
193 output->base.width, output->base.height);
194
195 if (!pixman_region32_not_empty(&cursor_region)) {
196 ret = 0;
197 goto out;
198 }
199
200 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
201 goto out;
202
203 if (eid->sprite->width > 64 || eid->sprite->height > 64)
204 goto out;
205
206 eglExportDRMImageMESA(c->base.display, eid->sprite->image,
207 NULL, &handle, &stride);
208
209 if (stride != 64 * 4) {
210 fprintf(stderr, "info: cursor stride is != 64\n");
211 goto out;
212 }
213
214 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
215 if (ret) {
216 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
217 goto out;
218 }
219
220 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
221 eid->sprite->x - output->base.x,
222 eid->sprite->y - output->base.y);
223 if (ret) {
224 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
225 goto out;
226 }
227
228 printf("info: set hardware cursor\n");
229
230out:
231 pixman_region32_fini(&cursor_region);
232 if (ret)
233 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
234 return ret;
235}
236
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400237static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400238on_drm_input(int fd, uint32_t mask, void *data)
239{
240 drmEventContext evctx;
241
242 memset(&evctx, 0, sizeof evctx);
243 evctx.version = DRM_EVENT_CONTEXT_VERSION;
244 evctx.page_flip_handler = page_flip_handler;
245 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400246
247 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400248}
249
250static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400251init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400252{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400253 EGLint major, minor;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400254 const char *extensions, *filename;
255 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400256 static const EGLint context_attribs[] = {
257 EGL_CONTEXT_CLIENT_VERSION, 2,
258 EGL_NONE
259 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400260
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400261 filename = udev_device_get_devnode(device);
Kristian Høgsberg9cd7aca2011-04-11 13:19:16 -0400262 fd = open(filename, O_RDWR, O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400263 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400264 /* Probably permissions error */
265 fprintf(stderr, "couldn't open %s, skipping\n",
266 udev_device_get_devnode(device));
267 return -1;
268 }
269
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100270 ec->drm.fd = fd;
271 ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400272 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400273 fprintf(stderr, "failed to create display\n");
274 return -1;
275 }
276
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400277 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400278 fprintf(stderr, "failed to initialize display\n");
279 return -1;
280 }
281
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400282 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
283 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
284 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400285 return -1;
286 }
287
Darxus55973f22010-11-22 21:24:39 -0500288 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
289 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
290 return -1;
291 }
292
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400293 ec->base.context = eglCreateContext(ec->base.display, NULL,
294 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400295 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400296 fprintf(stderr, "failed to create context\n");
297 return -1;
298 }
299
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400300 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
301 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400302 fprintf(stderr, "failed to make context current\n");
303 return -1;
304 }
305
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400306 return 0;
307}
308
309static drmModeModeInfo builtin_1024x768 = {
310 63500, /* clock */
311 1024, 1072, 1176, 1328, 0,
312 768, 771, 775, 798, 0,
313 59920,
314 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
315 0,
316 "1024x768"
317};
318
319static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400320create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400321 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100322 drmModeConnector *connector,
323 int x, int y)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400324{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400325 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400326 drmModeEncoder *encoder;
327 drmModeModeInfo *mode;
328 int i, ret;
329 EGLint handle, stride, attribs[] = {
330 EGL_WIDTH, 0,
331 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400332 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
333 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400334 EGL_NONE
335 };
336
337 output = malloc(sizeof *output);
338 if (output == NULL)
339 return -1;
340
341 if (connector->count_modes > 0)
342 mode = &connector->modes[0];
343 else
344 mode = &builtin_1024x768;
345
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100346 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400347 if (encoder == NULL) {
348 fprintf(stderr, "No encoder for connector.\n");
349 return -1;
350 }
351
352 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -0500353 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100354 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400355 break;
356 }
357 if (i == resources->count_crtcs) {
358 fprintf(stderr, "No usable crtc for encoder.\n");
359 return -1;
360 }
361
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400362 memset(output, 0, sizeof *output);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100363 wlsc_output_init(&output->base, &ec->base, x, y,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +0100364 mode->hdisplay, mode->vdisplay, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400365
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400366 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100367 ec->crtc_allocator |= (1 << output->crtc_id);
368
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400369 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100370 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400371 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400372
373 drmModeFreeEncoder(encoder);
374
375 glGenRenderbuffers(2, output->rbo);
376 for (i = 0; i < 2; i++) {
377 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
378
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400379 attribs[1] = output->base.width;
380 attribs[3] = output->base.height;
381 output->image[i] =
382 eglCreateDRMImageMESA(ec->base.display, attribs);
383 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
384 output->image[i]);
385 eglExportDRMImageMESA(ec->base.display, output->image[i],
386 NULL, &handle, &stride);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400387
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100388 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400389 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400390 32, 32, stride, handle, &output->fb_id[i]);
391 if (ret) {
392 fprintf(stderr, "failed to add fb %d: %m\n", i);
393 return -1;
394 }
395 }
396
397 output->current = 0;
398 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
399 GL_COLOR_ATTACHMENT0,
400 GL_RENDERBUFFER,
401 output->rbo[output->current]);
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100402 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400403 output->fb_id[output->current ^ 1], 0, 0,
404 &output->connector_id, 1, &output->mode);
405 if (ret) {
406 fprintf(stderr, "failed to set mode: %m\n");
407 return -1;
408 }
409
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200410 output->scanout_surface = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100411 output->base.prepare_render = drm_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100412 output->base.present = drm_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200413 output->base.prepare_scanout_surface =
414 drm_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200415 output->base.set_hardware_cursor = drm_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100416
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400417 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400418
419 return 0;
420}
421
422static int
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400423create_outputs(struct drm_compositor *ec, int option_connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400424{
425 drmModeConnector *connector;
426 drmModeRes *resources;
427 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100428 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400429
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100430 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400431 if (!resources) {
432 fprintf(stderr, "drmModeGetResources failed\n");
433 return -1;
434 }
435
436 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100437 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400438 if (connector == NULL)
439 continue;
440
441 if (connector->connection == DRM_MODE_CONNECTED &&
442 (option_connector == 0 ||
443 connector->connector_id == option_connector))
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100444 if (create_output_for_connector(ec, resources,
445 connector, x, y) < 0)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400446 return -1;
447
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100448 x += container_of(ec->base.output_list.prev, struct wlsc_output,
449 link)->width;
450
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400451 drmModeFreeConnector(connector);
452 }
453
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400454 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400455 fprintf(stderr, "No currently active connector found.\n");
456 return -1;
457 }
458
459 drmModeFreeResources(resources);
460
461 return 0;
462}
463
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100464static int
465destroy_output(struct drm_output *output)
466{
467 struct drm_compositor *ec =
468 (struct drm_compositor *) output->base.compositor;
469 int i;
470
471 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
472 GL_COLOR_ATTACHMENT0,
473 GL_RENDERBUFFER,
474 0);
475
476 glBindRenderbuffer(GL_RENDERBUFFER, 0);
477 glDeleteRenderbuffers(2, output->rbo);
478
479 for (i = 0; i < 2; i++) {
480 eglDestroyImageKHR(ec->base.display, output->image[i]);
481 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
482 }
483
484 ec->crtc_allocator &= ~(1 << output->crtc_id);
485 ec->connector_allocator &= ~(1 << output->connector_id);
486
487 wlsc_output_destroy(&output->base);
488 wl_list_remove(&output->base.link);
489
490 free(output);
491
492 return 0;
493}
494
495static void
496update_outputs(struct drm_compositor *ec)
497{
498 drmModeConnector *connector;
499 drmModeRes *resources;
500 struct drm_output *output, *next;
501 int x = 0, y = 0;
502 int x_offset = 0, y_offset = 0;
503 uint32_t connected = 0, disconnects = 0;
504 int i;
505
506 resources = drmModeGetResources(ec->drm.fd);
507 if (!resources) {
508 fprintf(stderr, "drmModeGetResources failed\n");
509 return;
510 }
511
512 /* collect new connects */
513 for (i = 0; i < resources->count_connectors; i++) {
514 connector =
515 drmModeGetConnector(ec->drm.fd,
516 resources->connectors[i]);
517 if (connector == NULL ||
518 connector->connection != DRM_MODE_CONNECTED)
519 continue;
520
521 connected |= (1 << connector->connector_id);
522
523 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
524 struct wlsc_output *last_output =
525 container_of(ec->base.output_list.prev,
526 struct wlsc_output, link);
527
528 /* XXX: not yet needed, we die with 0 outputs */
529 if (!wl_list_empty(&ec->base.output_list))
530 x = last_output->x + last_output->width;
531 else
532 x = 0;
533 y = 0;
534 create_output_for_connector(ec, resources,
535 connector, x, y);
536 printf("connector %d connected\n",
537 connector->connector_id);
538
539 }
540 drmModeFreeConnector(connector);
541 }
542 drmModeFreeResources(resources);
543
544 disconnects = ec->connector_allocator & ~connected;
545 if (disconnects) {
546 wl_list_for_each_safe(output, next, &ec->base.output_list,
547 base.link) {
548 if (x_offset != 0 || y_offset != 0) {
549 wlsc_output_move(&output->base,
550 output->base.x - x_offset,
551 output->base.y - y_offset);
552 }
553
554 if (disconnects & (1 << output->connector_id)) {
555 disconnects &= ~(1 << output->connector_id);
556 printf("connector %d disconnected\n",
557 output->connector_id);
558 x_offset += output->base.width;
559 destroy_output(output);
560 }
561 }
562 }
563
564 /* FIXME: handle zero outputs, without terminating */
565 if (ec->connector_allocator == 0)
566 wl_display_terminate(ec->base.wl_display);
567}
568
569static int
570udev_event_is_hotplug(struct udev_device *device)
571{
572 struct udev_list_entry *list, *hotplug_entry;
573
574 list = udev_device_get_properties_list_entry(device);
575
576 hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
577 if (hotplug_entry == NULL)
578 return 0;
579
580 return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
581}
582
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400583static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100584udev_drm_event(int fd, uint32_t mask, void *data)
585{
586 struct drm_compositor *ec = data;
587 struct udev_device *event;
588
589 event = udev_monitor_receive_device(ec->udev_monitor);
590
591 if (udev_event_is_hotplug(event))
592 update_outputs(ec);
593
594 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400595
596 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100597}
598
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200599static EGLImageKHR
600drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
601 int32_t width, int32_t height)
602{
603 EGLint image_attribs[] = {
604 EGL_WIDTH, 0,
605 EGL_HEIGHT, 0,
606 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
607 0, 0,
608 EGL_NONE
609 };
610 EGLint stride, name;
611 EGLImageKHR tmp_image, image;
612
613 if (width > 64 || height > 64)
614 return EGL_NO_IMAGE_KHR;
615
616 image_attribs[1] = 64;
617 image_attribs[3] = 64;
618 image_attribs[6] = EGL_DRM_BUFFER_USE_MESA;
619 image_attribs[7] = EGL_DRM_BUFFER_USE_SCANOUT_MESA;
620
621 tmp_image = eglCreateDRMImageMESA(ec->display, image_attribs);
622
623 eglExportDRMImageMESA(ec->display, tmp_image, &name, NULL, &stride);
624
625 if (stride == 64)
626 return tmp_image;
627
628 /* recreate image width stide 64 forced */
629 image_attribs[1] = width;
630 image_attribs[3] = height;
631 image_attribs[6] = EGL_DRM_BUFFER_STRIDE_MESA;
632 image_attribs[7] = 64;
633
634 image = eglCreateImageKHR(ec->display, EGL_NO_CONTEXT, EGL_DRM_BUFFER_MESA,
635 (EGLClientBuffer)(intptr_t) name, image_attribs);
636 eglExportDRMImageMESA(ec->display, image, &name, NULL, &stride);
637
638 eglDestroyImageKHR(ec->display, tmp_image);
639
640 return image;
641}
642
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500643static void
644drm_destroy(struct wlsc_compositor *ec)
645{
646 struct drm_compositor *d = (struct drm_compositor *) ec;
647
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500648 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500649
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500650 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500651}
652
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400653struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400654drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400655{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400656 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400657 struct udev_enumerate *e;
658 struct udev_list_entry *entry;
659 struct udev_device *device;
660 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400661 struct wl_event_loop *loop;
662
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400663 ec = malloc(sizeof *ec);
664 if (ec == NULL)
665 return NULL;
666
667 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400668 ec->udev = udev_new();
669 if (ec->udev == NULL) {
670 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400671 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400672 }
673
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400674 e = udev_enumerate_new(ec->udev);
675 udev_enumerate_add_match_subsystem(e, "drm");
676 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
677 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400678 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400679 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
680 path = udev_list_entry_get_name(entry);
681 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400682 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400683 }
684 udev_enumerate_unref(e);
685
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400686 if (device == NULL) {
687 fprintf(stderr, "no drm device found\n");
688 return NULL;
689 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400690
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400691 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400692 if (init_egl(ec, device) < 0) {
693 fprintf(stderr, "failed to initialize egl\n");
694 return NULL;
695 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500696
697 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200698 ec->base.create_cursor_image = drm_compositor_create_cursor_image;
699
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500700 ec->base.focus = 1;
701
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100702 glGenFramebuffers(1, &ec->base.fbo);
703 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
704
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400705 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400706 if (wlsc_compositor_init(&ec->base, display) < 0)
707 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400708
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400709 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400710 fprintf(stderr, "failed to create output for %s\n", path);
711 return NULL;
712 }
713
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500714 evdev_input_add_devices(&ec->base, ec->udev);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400715
716 loop = wl_display_get_event_loop(ec->base.wl_display);
717 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100718 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400719 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500720 ec->tty = tty_create(&ec->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400721
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100722 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
723 if (ec->udev_monitor == NULL) {
724 fprintf(stderr, "failed to intialize udev monitor\n");
725 return NULL;
726 }
727 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
728 "drm", NULL);
729 ec->udev_drm_source =
730 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
731 WL_EVENT_READABLE, udev_drm_event, ec);
732
733 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
734 fprintf(stderr, "failed to enable udev-monitor receiving\n");
735 return NULL;
736 }
737
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400738 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400739}