blob: ecc074f4925d1a3a9da192928fcf0a57d69ee369 [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øgsbergfc783d42010-06-11 12:56:24 -0400237static void
238on_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);
246}
247
248static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400249init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400250{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400251 EGLint major, minor;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400252 const char *extensions, *filename;
253 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400254 static const EGLint context_attribs[] = {
255 EGL_CONTEXT_CLIENT_VERSION, 2,
256 EGL_NONE
257 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400258
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400259 filename = udev_device_get_devnode(device);
Kristian Høgsberg9cd7aca2011-04-11 13:19:16 -0400260 fd = open(filename, O_RDWR, O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400261 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400262 /* Probably permissions error */
263 fprintf(stderr, "couldn't open %s, skipping\n",
264 udev_device_get_devnode(device));
265 return -1;
266 }
267
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100268 ec->drm.fd = fd;
269 ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400270 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400271 fprintf(stderr, "failed to create display\n");
272 return -1;
273 }
274
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400275 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400276 fprintf(stderr, "failed to initialize display\n");
277 return -1;
278 }
279
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400280 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
281 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
282 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400283 return -1;
284 }
285
Darxus55973f22010-11-22 21:24:39 -0500286 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
287 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
288 return -1;
289 }
290
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400291 ec->base.context = eglCreateContext(ec->base.display, NULL,
292 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400293 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400294 fprintf(stderr, "failed to create context\n");
295 return -1;
296 }
297
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400298 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
299 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400300 fprintf(stderr, "failed to make context current\n");
301 return -1;
302 }
303
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400304 return 0;
305}
306
307static drmModeModeInfo builtin_1024x768 = {
308 63500, /* clock */
309 1024, 1072, 1176, 1328, 0,
310 768, 771, 775, 798, 0,
311 59920,
312 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
313 0,
314 "1024x768"
315};
316
317static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400318create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400319 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100320 drmModeConnector *connector,
321 int x, int y)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400322{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400323 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400324 drmModeEncoder *encoder;
325 drmModeModeInfo *mode;
326 int i, ret;
327 EGLint handle, stride, attribs[] = {
328 EGL_WIDTH, 0,
329 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400330 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
331 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400332 EGL_NONE
333 };
334
335 output = malloc(sizeof *output);
336 if (output == NULL)
337 return -1;
338
339 if (connector->count_modes > 0)
340 mode = &connector->modes[0];
341 else
342 mode = &builtin_1024x768;
343
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100344 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400345 if (encoder == NULL) {
346 fprintf(stderr, "No encoder for connector.\n");
347 return -1;
348 }
349
350 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -0500351 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100352 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400353 break;
354 }
355 if (i == resources->count_crtcs) {
356 fprintf(stderr, "No usable crtc for encoder.\n");
357 return -1;
358 }
359
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400360 memset(output, 0, sizeof *output);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100361 wlsc_output_init(&output->base, &ec->base, x, y,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +0100362 mode->hdisplay, mode->vdisplay, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400363
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400364 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100365 ec->crtc_allocator |= (1 << output->crtc_id);
366
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400367 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100368 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400369 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400370
371 drmModeFreeEncoder(encoder);
372
373 glGenRenderbuffers(2, output->rbo);
374 for (i = 0; i < 2; i++) {
375 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
376
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400377 attribs[1] = output->base.width;
378 attribs[3] = output->base.height;
379 output->image[i] =
380 eglCreateDRMImageMESA(ec->base.display, attribs);
381 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
382 output->image[i]);
383 eglExportDRMImageMESA(ec->base.display, output->image[i],
384 NULL, &handle, &stride);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400385
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100386 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400387 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400388 32, 32, stride, handle, &output->fb_id[i]);
389 if (ret) {
390 fprintf(stderr, "failed to add fb %d: %m\n", i);
391 return -1;
392 }
393 }
394
395 output->current = 0;
396 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
397 GL_COLOR_ATTACHMENT0,
398 GL_RENDERBUFFER,
399 output->rbo[output->current]);
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100400 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400401 output->fb_id[output->current ^ 1], 0, 0,
402 &output->connector_id, 1, &output->mode);
403 if (ret) {
404 fprintf(stderr, "failed to set mode: %m\n");
405 return -1;
406 }
407
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200408 output->scanout_surface = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100409 output->base.prepare_render = drm_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100410 output->base.present = drm_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200411 output->base.prepare_scanout_surface =
412 drm_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200413 output->base.set_hardware_cursor = drm_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100414
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400415 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400416
417 return 0;
418}
419
420static int
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400421create_outputs(struct drm_compositor *ec, int option_connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400422{
423 drmModeConnector *connector;
424 drmModeRes *resources;
425 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100426 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400427
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100428 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400429 if (!resources) {
430 fprintf(stderr, "drmModeGetResources failed\n");
431 return -1;
432 }
433
434 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100435 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400436 if (connector == NULL)
437 continue;
438
439 if (connector->connection == DRM_MODE_CONNECTED &&
440 (option_connector == 0 ||
441 connector->connector_id == option_connector))
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100442 if (create_output_for_connector(ec, resources,
443 connector, x, y) < 0)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400444 return -1;
445
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100446 x += container_of(ec->base.output_list.prev, struct wlsc_output,
447 link)->width;
448
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400449 drmModeFreeConnector(connector);
450 }
451
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400452 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400453 fprintf(stderr, "No currently active connector found.\n");
454 return -1;
455 }
456
457 drmModeFreeResources(resources);
458
459 return 0;
460}
461
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100462static int
463destroy_output(struct drm_output *output)
464{
465 struct drm_compositor *ec =
466 (struct drm_compositor *) output->base.compositor;
467 int i;
468
469 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
470 GL_COLOR_ATTACHMENT0,
471 GL_RENDERBUFFER,
472 0);
473
474 glBindRenderbuffer(GL_RENDERBUFFER, 0);
475 glDeleteRenderbuffers(2, output->rbo);
476
477 for (i = 0; i < 2; i++) {
478 eglDestroyImageKHR(ec->base.display, output->image[i]);
479 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
480 }
481
482 ec->crtc_allocator &= ~(1 << output->crtc_id);
483 ec->connector_allocator &= ~(1 << output->connector_id);
484
485 wlsc_output_destroy(&output->base);
486 wl_list_remove(&output->base.link);
487
488 free(output);
489
490 return 0;
491}
492
493static void
494update_outputs(struct drm_compositor *ec)
495{
496 drmModeConnector *connector;
497 drmModeRes *resources;
498 struct drm_output *output, *next;
499 int x = 0, y = 0;
500 int x_offset = 0, y_offset = 0;
501 uint32_t connected = 0, disconnects = 0;
502 int i;
503
504 resources = drmModeGetResources(ec->drm.fd);
505 if (!resources) {
506 fprintf(stderr, "drmModeGetResources failed\n");
507 return;
508 }
509
510 /* collect new connects */
511 for (i = 0; i < resources->count_connectors; i++) {
512 connector =
513 drmModeGetConnector(ec->drm.fd,
514 resources->connectors[i]);
515 if (connector == NULL ||
516 connector->connection != DRM_MODE_CONNECTED)
517 continue;
518
519 connected |= (1 << connector->connector_id);
520
521 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
522 struct wlsc_output *last_output =
523 container_of(ec->base.output_list.prev,
524 struct wlsc_output, link);
525
526 /* XXX: not yet needed, we die with 0 outputs */
527 if (!wl_list_empty(&ec->base.output_list))
528 x = last_output->x + last_output->width;
529 else
530 x = 0;
531 y = 0;
532 create_output_for_connector(ec, resources,
533 connector, x, y);
534 printf("connector %d connected\n",
535 connector->connector_id);
536
537 }
538 drmModeFreeConnector(connector);
539 }
540 drmModeFreeResources(resources);
541
542 disconnects = ec->connector_allocator & ~connected;
543 if (disconnects) {
544 wl_list_for_each_safe(output, next, &ec->base.output_list,
545 base.link) {
546 if (x_offset != 0 || y_offset != 0) {
547 wlsc_output_move(&output->base,
548 output->base.x - x_offset,
549 output->base.y - y_offset);
550 }
551
552 if (disconnects & (1 << output->connector_id)) {
553 disconnects &= ~(1 << output->connector_id);
554 printf("connector %d disconnected\n",
555 output->connector_id);
556 x_offset += output->base.width;
557 destroy_output(output);
558 }
559 }
560 }
561
562 /* FIXME: handle zero outputs, without terminating */
563 if (ec->connector_allocator == 0)
564 wl_display_terminate(ec->base.wl_display);
565}
566
567static int
568udev_event_is_hotplug(struct udev_device *device)
569{
570 struct udev_list_entry *list, *hotplug_entry;
571
572 list = udev_device_get_properties_list_entry(device);
573
574 hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
575 if (hotplug_entry == NULL)
576 return 0;
577
578 return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
579}
580
581static void
582udev_drm_event(int fd, uint32_t mask, void *data)
583{
584 struct drm_compositor *ec = data;
585 struct udev_device *event;
586
587 event = udev_monitor_receive_device(ec->udev_monitor);
588
589 if (udev_event_is_hotplug(event))
590 update_outputs(ec);
591
592 udev_device_unref(event);
593}
594
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200595static EGLImageKHR
596drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
597 int32_t width, int32_t height)
598{
599 EGLint image_attribs[] = {
600 EGL_WIDTH, 0,
601 EGL_HEIGHT, 0,
602 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
603 0, 0,
604 EGL_NONE
605 };
606 EGLint stride, name;
607 EGLImageKHR tmp_image, image;
608
609 if (width > 64 || height > 64)
610 return EGL_NO_IMAGE_KHR;
611
612 image_attribs[1] = 64;
613 image_attribs[3] = 64;
614 image_attribs[6] = EGL_DRM_BUFFER_USE_MESA;
615 image_attribs[7] = EGL_DRM_BUFFER_USE_SCANOUT_MESA;
616
617 tmp_image = eglCreateDRMImageMESA(ec->display, image_attribs);
618
619 eglExportDRMImageMESA(ec->display, tmp_image, &name, NULL, &stride);
620
621 if (stride == 64)
622 return tmp_image;
623
624 /* recreate image width stide 64 forced */
625 image_attribs[1] = width;
626 image_attribs[3] = height;
627 image_attribs[6] = EGL_DRM_BUFFER_STRIDE_MESA;
628 image_attribs[7] = 64;
629
630 image = eglCreateImageKHR(ec->display, EGL_NO_CONTEXT, EGL_DRM_BUFFER_MESA,
631 (EGLClientBuffer)(intptr_t) name, image_attribs);
632 eglExportDRMImageMESA(ec->display, image, &name, NULL, &stride);
633
634 eglDestroyImageKHR(ec->display, tmp_image);
635
636 return image;
637}
638
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500639static void
640drm_destroy(struct wlsc_compositor *ec)
641{
642 struct drm_compositor *d = (struct drm_compositor *) ec;
643
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500644 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500645
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500646 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500647}
648
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400649struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400650drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400651{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400652 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400653 struct udev_enumerate *e;
654 struct udev_list_entry *entry;
655 struct udev_device *device;
656 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400657 struct wl_event_loop *loop;
658
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400659 ec = malloc(sizeof *ec);
660 if (ec == NULL)
661 return NULL;
662
663 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400664 ec->udev = udev_new();
665 if (ec->udev == NULL) {
666 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400667 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400668 }
669
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400670 e = udev_enumerate_new(ec->udev);
671 udev_enumerate_add_match_subsystem(e, "drm");
672 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
673 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400674 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400675 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
676 path = udev_list_entry_get_name(entry);
677 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400678 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400679 }
680 udev_enumerate_unref(e);
681
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400682 if (device == NULL) {
683 fprintf(stderr, "no drm device found\n");
684 return NULL;
685 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400686
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400687 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400688 if (init_egl(ec, device) < 0) {
689 fprintf(stderr, "failed to initialize egl\n");
690 return NULL;
691 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500692
693 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200694 ec->base.create_cursor_image = drm_compositor_create_cursor_image;
695
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500696 ec->base.focus = 1;
697
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100698 glGenFramebuffers(1, &ec->base.fbo);
699 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
700
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400701 /* Can't init base class until we have a current egl context */
Kristian Høgsberga9468212010-06-14 21:03:11 -0400702 if (wlsc_compositor_init(&ec->base, display) < 0)
703 return NULL;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400704
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400705 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400706 fprintf(stderr, "failed to create output for %s\n", path);
707 return NULL;
708 }
709
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500710 evdev_input_add_devices(&ec->base, ec->udev);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400711
712 loop = wl_display_get_event_loop(ec->base.wl_display);
713 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100714 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400715 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500716 ec->tty = tty_create(&ec->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400717
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100718 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
719 if (ec->udev_monitor == NULL) {
720 fprintf(stderr, "failed to intialize udev monitor\n");
721 return NULL;
722 }
723 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
724 "drm", NULL);
725 ec->udev_drm_source =
726 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
727 WL_EVENT_READABLE, udev_drm_event, ec);
728
729 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
730 fprintf(stderr, "failed to enable udev-monitor receiving\n");
731 return NULL;
732 }
733
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400734 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400735}