blob: 2902168ef0f877934e446cc47a0b6a591035bbab [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#include "compositor.h"
31
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040032struct drm_compositor {
33 struct wlsc_compositor base;
34
35 struct udev *udev;
36 struct wl_event_source *drm_source;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040037
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010038 struct udev_monitor *udev_monitor;
39 struct wl_event_source *udev_drm_source;
40
Benjamin Franzke2af7f102011-03-02 11:14:59 +010041 struct {
42 int fd;
43 } drm;
Marty Jack13d9db22011-02-09 19:01:42 -050044 uint32_t crtc_allocator;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +010045 uint32_t connector_allocator;
Kristian Høgsberge4762a62011-01-14 14:59:13 -050046 struct tty *tty;
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -040047
48 PFNEGLCREATEDRMIMAGEMESA create_drm_image;
49 PFNEGLEXPORTDRMIMAGEMESA export_drm_image;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040050};
51
52struct drm_output {
53 struct wlsc_output base;
54
55 drmModeModeInfo mode;
56 uint32_t crtc_id;
57 uint32_t connector_id;
58 GLuint rbo[2];
59 uint32_t fb_id[2];
60 EGLImageKHR image[2];
61 uint32_t current;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020062
Benjamin Franzke66aa2352011-04-20 17:06:13 +020063 struct wlsc_surface *scanout_surface;
64
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020065 uint32_t fs_surf_fb_id;
66 uint32_t pending_fs_surf_fb_id;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040067};
68
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010069static int
70drm_output_prepare_render(struct wlsc_output *output_base)
71{
72 struct drm_output *output = (struct drm_output *) output_base;
73
74 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
75 GL_COLOR_ATTACHMENT0,
76 GL_RENDERBUFFER,
77 output->rbo[output->current]);
78
79 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
80 return -1;
81
82 return 0;
83}
84
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010085static int
86drm_output_present(struct wlsc_output *output_base)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040087{
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010088 struct drm_output *output = (struct drm_output *) output_base;
89 struct drm_compositor *c =
90 (struct drm_compositor *) output->base.compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +020091 uint32_t fb_id = 0;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040092
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010093 if (drm_output_prepare_render(&output->base))
94 return -1;
95 glFlush();
Kristian Høgsbergce5325d2010-06-14 11:54:00 -040096
Benjamin Franzkeec4d3422011-03-14 12:07:26 +010097 output->current ^= 1;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010098
Benjamin Franzke66aa2352011-04-20 17:06:13 +020099 if (output->scanout_surface != NULL) {
100 output->scanout_surface = NULL;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200101 fb_id = output->fs_surf_fb_id;
102 } else {
103 fb_id = output->fb_id[output->current ^ 1];
104 }
105
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100106 drmModePageFlip(c->drm.fd, output->crtc_id,
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200107 fb_id,
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100108 DRM_MODE_PAGE_FLIP_EVENT, output);
109
110 return 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400111}
112
113static void
114page_flip_handler(int fd, unsigned int frame,
115 unsigned int sec, unsigned int usec, void *data)
116{
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200117 struct drm_output *output = (struct drm_output *) data;
118 struct drm_compositor *c =
119 (struct drm_compositor *) output->base.compositor;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400120 uint32_t msecs;
121
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200122 if (output->pending_fs_surf_fb_id) {
123 drmModeRmFB(c->drm.fd, output->pending_fs_surf_fb_id);
124 output->pending_fs_surf_fb_id = 0;
125 }
126
127 if (output->fs_surf_fb_id) {
128 output->pending_fs_surf_fb_id = output->fs_surf_fb_id;
129 output->fs_surf_fb_id = 0;
130 }
131
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100132 msecs = sec * 1000 + usec / 1000;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200133 wlsc_output_finish_frame(&output->base, msecs);
134}
135
136static int
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200137drm_output_prepare_scanout_surface(struct wlsc_output *output_base,
138 struct wlsc_surface *es)
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200139{
140 struct drm_output *output = (struct drm_output *) output_base;
141 struct drm_compositor *c =
142 (struct drm_compositor *) output->base.compositor;
143 EGLint handle, stride;
144 int ret;
145 uint32_t fb_id = 0;
146
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200147 if (es->width != output->base.width ||
148 es->height != output->base.height ||
149 es->image == EGL_NO_IMAGE_KHR)
150 return -1;
151
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400152 c->export_drm_image(c->base.display,
153 es->image, NULL, &handle, &stride);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200154
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200155 if (handle == 0)
156 return -1;
157
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200158 ret = drmModeAddFB(c->drm.fd,
159 output->base.width, output->base.height,
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200160 32, 32, stride, handle, &fb_id);
161
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200162 if (ret)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200163 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200164
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200165 output->fs_surf_fb_id = fb_id;
166 output->scanout_surface = es;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200167
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200168 return 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400169}
170
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200171static int
172drm_output_set_cursor(struct wlsc_output *output_base,
173 struct wl_input_device *input)
174{
175 struct drm_output *output = (struct drm_output *) output_base;
176 struct drm_compositor *c =
177 (struct drm_compositor *) output->base.compositor;
178 struct wlsc_input_device *eid = (struct wlsc_input_device *) input;
179 EGLint handle, stride;
180 int ret = -1;
181 pixman_region32_t cursor_region;
182
183 pixman_region32_init_rect(&cursor_region,
184 eid->sprite->x, eid->sprite->y,
185 eid->sprite->width, eid->sprite->height);
186
187 pixman_region32_intersect_rect(&cursor_region, &cursor_region,
188 output->base.x, output->base.y,
189 output->base.width, output->base.height);
190
191 if (!pixman_region32_not_empty(&cursor_region)) {
192 ret = 0;
193 goto out;
194 }
195
196 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
197 goto out;
198
199 if (eid->sprite->width > 64 || eid->sprite->height > 64)
200 goto out;
201
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400202 c->export_drm_image(c->base.display, eid->sprite->image,
203 NULL, &handle, &stride);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200204
205 if (stride != 64 * 4) {
206 fprintf(stderr, "info: cursor stride is != 64\n");
207 goto out;
208 }
209
210 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
211 if (ret) {
212 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
213 goto out;
214 }
215
216 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
217 eid->sprite->x - output->base.x,
218 eid->sprite->y - output->base.y);
219 if (ret) {
220 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
221 goto out;
222 }
223
224 printf("info: set hardware cursor\n");
225
226out:
227 pixman_region32_fini(&cursor_region);
228 if (ret)
229 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
230 return ret;
231}
232
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400233static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400234on_drm_input(int fd, uint32_t mask, void *data)
235{
236 drmEventContext evctx;
237
238 memset(&evctx, 0, sizeof evctx);
239 evctx.version = DRM_EVENT_CONTEXT_VERSION;
240 evctx.page_flip_handler = page_flip_handler;
241 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400242
243 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400244}
245
246static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400247init_egl(struct drm_compositor *ec, struct udev_device *device)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400248{
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400249 EGLint major, minor;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400250 const char *extensions, *filename;
251 int fd;
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400252 static const EGLint context_attribs[] = {
253 EGL_CONTEXT_CLIENT_VERSION, 2,
254 EGL_NONE
255 };
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400256
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400257 filename = udev_device_get_devnode(device);
Kristian Høgsberg9cd7aca2011-04-11 13:19:16 -0400258 fd = open(filename, O_RDWR, O_CLOEXEC);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400259 if (fd < 0) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400260 /* Probably permissions error */
261 fprintf(stderr, "couldn't open %s, skipping\n",
262 udev_device_get_devnode(device));
263 return -1;
264 }
265
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100266 ec->drm.fd = fd;
Benjamin Franzkee5b3b262011-04-26 09:21:13 +0200267 ec->base.display = eglGetDisplay(FD_TO_EGL_NATIVE_DPY(ec->drm.fd));
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400268 if (ec->base.display == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400269 fprintf(stderr, "failed to create display\n");
270 return -1;
271 }
272
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400273 if (!eglInitialize(ec->base.display, &major, &minor)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400274 fprintf(stderr, "failed to initialize display\n");
275 return -1;
276 }
277
Kristian Høgsberg379b6782010-07-28 22:52:28 -0400278 extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
279 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
280 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400281 return -1;
282 }
283
Darxus55973f22010-11-22 21:24:39 -0500284 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
285 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
286 return -1;
287 }
288
Kristian Høgsberg2c28aa52010-07-28 23:47:54 -0400289 ec->base.context = eglCreateContext(ec->base.display, NULL,
290 EGL_NO_CONTEXT, context_attribs);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400291 if (ec->base.context == NULL) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400292 fprintf(stderr, "failed to create context\n");
293 return -1;
294 }
295
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400296 if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
297 EGL_NO_SURFACE, ec->base.context)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400298 fprintf(stderr, "failed to make context current\n");
299 return -1;
300 }
301
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400302 return 0;
303}
304
305static drmModeModeInfo builtin_1024x768 = {
306 63500, /* clock */
307 1024, 1072, 1176, 1328, 0,
308 768, 771, 775, 798, 0,
309 59920,
310 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
311 0,
312 "1024x768"
313};
314
315static int
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400316create_output_for_connector(struct drm_compositor *ec,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400317 drmModeRes *resources,
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100318 drmModeConnector *connector,
319 int x, int y)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400320{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400321 struct drm_output *output;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400322 drmModeEncoder *encoder;
323 drmModeModeInfo *mode;
324 int i, ret;
325 EGLint handle, stride, attribs[] = {
326 EGL_WIDTH, 0,
327 EGL_HEIGHT, 0,
Kristian Høgsbergb12fcce2010-08-24 17:34:23 -0400328 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
329 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400330 EGL_NONE
331 };
332
333 output = malloc(sizeof *output);
334 if (output == NULL)
335 return -1;
336
337 if (connector->count_modes > 0)
338 mode = &connector->modes[0];
339 else
340 mode = &builtin_1024x768;
341
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100342 encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400343 if (encoder == NULL) {
344 fprintf(stderr, "No encoder for connector.\n");
345 return -1;
346 }
347
348 for (i = 0; i < resources->count_crtcs; i++) {
Marty Jack13d9db22011-02-09 19:01:42 -0500349 if (encoder->possible_crtcs & (1 << i) &&
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100350 !(ec->crtc_allocator & (1 << resources->crtcs[i])))
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400351 break;
352 }
353 if (i == resources->count_crtcs) {
354 fprintf(stderr, "No usable crtc for encoder.\n");
355 return -1;
356 }
357
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400358 memset(output, 0, sizeof *output);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100359 wlsc_output_init(&output->base, &ec->base, x, y,
Benjamin Franzke1b765ff2011-02-18 16:51:37 +0100360 mode->hdisplay, mode->vdisplay, 0);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400361
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400362 output->crtc_id = resources->crtcs[i];
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100363 ec->crtc_allocator |= (1 << output->crtc_id);
364
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400365 output->connector_id = connector->connector_id;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100366 ec->connector_allocator |= (1 << output->connector_id);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400367 output->mode = *mode;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400368
369 drmModeFreeEncoder(encoder);
370
371 glGenRenderbuffers(2, output->rbo);
372 for (i = 0; i < 2; i++) {
373 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
374
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400375 attribs[1] = output->base.width;
376 attribs[3] = output->base.height;
377 output->image[i] =
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400378 ec->create_drm_image(ec->base.display, attribs);
379 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
380 output->image[i]);
381 ec->export_drm_image(ec->base.display, output->image[i],
382 NULL, &handle, &stride);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400383
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100384 ret = drmModeAddFB(ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400385 output->base.width, output->base.height,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400386 32, 32, stride, handle, &output->fb_id[i]);
387 if (ret) {
388 fprintf(stderr, "failed to add fb %d: %m\n", i);
389 return -1;
390 }
391 }
392
393 output->current = 0;
394 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
395 GL_COLOR_ATTACHMENT0,
396 GL_RENDERBUFFER,
397 output->rbo[output->current]);
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100398 ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400399 output->fb_id[output->current ^ 1], 0, 0,
400 &output->connector_id, 1, &output->mode);
401 if (ret) {
402 fprintf(stderr, "failed to set mode: %m\n");
403 return -1;
404 }
405
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200406 output->scanout_surface = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100407 output->base.prepare_render = drm_output_prepare_render;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +0100408 output->base.present = drm_output_present;
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200409 output->base.prepare_scanout_surface =
410 drm_output_prepare_scanout_surface;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200411 output->base.set_hardware_cursor = drm_output_set_cursor;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100412
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400413 wl_list_insert(ec->base.output_list.prev, &output->base.link);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400414
415 return 0;
416}
417
418static int
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400419create_outputs(struct drm_compositor *ec, int option_connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400420{
421 drmModeConnector *connector;
422 drmModeRes *resources;
423 int i;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100424 int x = 0, y = 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400425
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100426 resources = drmModeGetResources(ec->drm.fd);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400427 if (!resources) {
428 fprintf(stderr, "drmModeGetResources failed\n");
429 return -1;
430 }
431
432 for (i = 0; i < resources->count_connectors; i++) {
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100433 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400434 if (connector == NULL)
435 continue;
436
437 if (connector->connection == DRM_MODE_CONNECTED &&
438 (option_connector == 0 ||
439 connector->connector_id == option_connector))
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100440 if (create_output_for_connector(ec, resources,
441 connector, x, y) < 0)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400442 return -1;
443
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100444 x += container_of(ec->base.output_list.prev, struct wlsc_output,
445 link)->width;
446
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400447 drmModeFreeConnector(connector);
448 }
449
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400450 if (wl_list_empty(&ec->base.output_list)) {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400451 fprintf(stderr, "No currently active connector found.\n");
452 return -1;
453 }
454
455 drmModeFreeResources(resources);
456
457 return 0;
458}
459
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100460static int
461destroy_output(struct drm_output *output)
462{
463 struct drm_compositor *ec =
464 (struct drm_compositor *) output->base.compositor;
465 int i;
466
467 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
468 GL_COLOR_ATTACHMENT0,
469 GL_RENDERBUFFER,
470 0);
471
472 glBindRenderbuffer(GL_RENDERBUFFER, 0);
473 glDeleteRenderbuffers(2, output->rbo);
474
475 for (i = 0; i < 2; i++) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400476 ec->base.destroy_image(ec->base.display, output->image[i]);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100477 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
478 }
479
480 ec->crtc_allocator &= ~(1 << output->crtc_id);
481 ec->connector_allocator &= ~(1 << output->connector_id);
482
483 wlsc_output_destroy(&output->base);
484 wl_list_remove(&output->base.link);
485
486 free(output);
487
488 return 0;
489}
490
491static void
492update_outputs(struct drm_compositor *ec)
493{
494 drmModeConnector *connector;
495 drmModeRes *resources;
496 struct drm_output *output, *next;
497 int x = 0, y = 0;
498 int x_offset = 0, y_offset = 0;
499 uint32_t connected = 0, disconnects = 0;
500 int i;
501
502 resources = drmModeGetResources(ec->drm.fd);
503 if (!resources) {
504 fprintf(stderr, "drmModeGetResources failed\n");
505 return;
506 }
507
508 /* collect new connects */
509 for (i = 0; i < resources->count_connectors; i++) {
510 connector =
511 drmModeGetConnector(ec->drm.fd,
512 resources->connectors[i]);
513 if (connector == NULL ||
514 connector->connection != DRM_MODE_CONNECTED)
515 continue;
516
517 connected |= (1 << connector->connector_id);
518
519 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
520 struct wlsc_output *last_output =
521 container_of(ec->base.output_list.prev,
522 struct wlsc_output, link);
523
524 /* XXX: not yet needed, we die with 0 outputs */
525 if (!wl_list_empty(&ec->base.output_list))
526 x = last_output->x + last_output->width;
527 else
528 x = 0;
529 y = 0;
530 create_output_for_connector(ec, resources,
531 connector, x, y);
532 printf("connector %d connected\n",
533 connector->connector_id);
534
535 }
536 drmModeFreeConnector(connector);
537 }
538 drmModeFreeResources(resources);
539
540 disconnects = ec->connector_allocator & ~connected;
541 if (disconnects) {
542 wl_list_for_each_safe(output, next, &ec->base.output_list,
543 base.link) {
544 if (x_offset != 0 || y_offset != 0) {
545 wlsc_output_move(&output->base,
546 output->base.x - x_offset,
547 output->base.y - y_offset);
548 }
549
550 if (disconnects & (1 << output->connector_id)) {
551 disconnects &= ~(1 << output->connector_id);
552 printf("connector %d disconnected\n",
553 output->connector_id);
554 x_offset += output->base.width;
555 destroy_output(output);
556 }
557 }
558 }
559
560 /* FIXME: handle zero outputs, without terminating */
561 if (ec->connector_allocator == 0)
562 wl_display_terminate(ec->base.wl_display);
563}
564
565static int
566udev_event_is_hotplug(struct udev_device *device)
567{
568 struct udev_list_entry *list, *hotplug_entry;
569
570 list = udev_device_get_properties_list_entry(device);
571
572 hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
573 if (hotplug_entry == NULL)
574 return 0;
575
576 return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
577}
578
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400579static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100580udev_drm_event(int fd, uint32_t mask, void *data)
581{
582 struct drm_compositor *ec = data;
583 struct udev_device *event;
584
585 event = udev_monitor_receive_device(ec->udev_monitor);
586
587 if (udev_event_is_hotplug(event))
588 update_outputs(ec);
589
590 udev_device_unref(event);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400591
592 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100593}
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{
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400599 static const EGLint image_attribs[] = {
600 EGL_WIDTH, 64,
601 EGL_HEIGHT, 64,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200602 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400603 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_CURSOR_MESA,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200604 EGL_NONE
605 };
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400606 struct drm_compositor *c = (struct drm_compositor *) ec;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200607
608 if (width > 64 || height > 64)
609 return EGL_NO_IMAGE_KHR;
610
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400611 return c->create_drm_image(ec->display, image_attribs);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200612}
613
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500614static void
615drm_destroy(struct wlsc_compositor *ec)
616{
617 struct drm_compositor *d = (struct drm_compositor *) ec;
618
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500619 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500620
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500621 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500622}
623
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400624struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400625drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400626{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400627 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400628 struct udev_enumerate *e;
629 struct udev_list_entry *entry;
630 struct udev_device *device;
631 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400632 struct wl_event_loop *loop;
633
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400634 ec = malloc(sizeof *ec);
635 if (ec == NULL)
636 return NULL;
637
638 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400639 ec->udev = udev_new();
640 if (ec->udev == NULL) {
641 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400642 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400643 }
644
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400645 e = udev_enumerate_new(ec->udev);
646 udev_enumerate_add_match_subsystem(e, "drm");
647 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
648 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400649 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400650 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
651 path = udev_list_entry_get_name(entry);
652 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400653 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400654 }
655 udev_enumerate_unref(e);
656
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400657 if (device == NULL) {
658 fprintf(stderr, "no drm device found\n");
659 return NULL;
660 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400661
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400662 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400663 if (init_egl(ec, device) < 0) {
664 fprintf(stderr, "failed to initialize egl\n");
665 return NULL;
666 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500667
668 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200669 ec->base.create_cursor_image = drm_compositor_create_cursor_image;
670
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500671 ec->base.focus = 1;
672
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100673 glGenFramebuffers(1, &ec->base.fbo);
674 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
675
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400676 ec->create_drm_image =
677 (void *) eglGetProcAddress("eglCreateDRMImageMESA");
678 ec->export_drm_image =
679 (void *) eglGetProcAddress("eglExportDRMImageMESA");
680
Benjamin Franzked59eb1c2011-04-29 22:14:54 +0200681 /* Can't init base class until we have a current egl context */
682 if (wlsc_compositor_init(&ec->base, display) < 0)
683 return NULL;
684
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400685 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400686 fprintf(stderr, "failed to create output for %s\n", path);
687 return NULL;
688 }
689
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500690 evdev_input_add_devices(&ec->base, ec->udev);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400691
692 loop = wl_display_get_event_loop(ec->base.wl_display);
693 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100694 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400695 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500696 ec->tty = tty_create(&ec->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400697
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100698 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
699 if (ec->udev_monitor == NULL) {
700 fprintf(stderr, "failed to intialize udev monitor\n");
701 return NULL;
702 }
703 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
704 "drm", NULL);
705 ec->udev_drm_source =
706 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
707 WL_EVENT_READABLE, udev_drm_event, ec);
708
709 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
710 fprintf(stderr, "failed to enable udev-monitor receiving\n");
711 return NULL;
712 }
713
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400714 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400715}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400716
717struct wlsc_compositor *
718backend_init(struct wl_display *display, char *options)
719{
Kristian Høgsberg8050bd22011-05-04 15:08:04 -0400720 int connector = 0, i;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400721 char *p, *value;
722
723 static char * const tokens[] = { "connector", NULL };
724
725 p = options;
726 while (i = getsubopt(&p, tokens, &value), i != -1) {
727 switch (i) {
728 case 0:
Kristian Høgsberg8050bd22011-05-04 15:08:04 -0400729 connector = strtol(value, NULL, 0);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400730 break;
731 }
732 }
733
734 return drm_compositor_create(display, connector);
735}