blob: eb452abdc41c72c87feedd002f429562e8a6a048 [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
Kristian Høgsberg4d07a1c2011-05-06 14:03:12 -0400147 if (es->x != output->base.x ||
148 es->y != output->base.y ||
149 es->width != output->base.width ||
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200150 es->height != output->base.height ||
151 es->image == EGL_NO_IMAGE_KHR)
152 return -1;
153
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400154 c->export_drm_image(c->base.display,
155 es->image, NULL, &handle, &stride);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200156
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200157 if (handle == 0)
158 return -1;
159
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200160 ret = drmModeAddFB(c->drm.fd,
161 output->base.width, output->base.height,
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200162 32, 32, stride, handle, &fb_id);
163
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200164 if (ret)
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200165 return -1;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200166
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200167 output->fs_surf_fb_id = fb_id;
168 output->scanout_surface = es;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200169
Benjamin Franzke66aa2352011-04-20 17:06:13 +0200170 return 0;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400171}
172
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200173static int
174drm_output_set_cursor(struct wlsc_output *output_base,
175 struct wl_input_device *input)
176{
177 struct drm_output *output = (struct drm_output *) output_base;
178 struct drm_compositor *c =
179 (struct drm_compositor *) output->base.compositor;
180 struct wlsc_input_device *eid = (struct wlsc_input_device *) input;
181 EGLint handle, stride;
182 int ret = -1;
183 pixman_region32_t cursor_region;
184
185 pixman_region32_init_rect(&cursor_region,
186 eid->sprite->x, eid->sprite->y,
187 eid->sprite->width, eid->sprite->height);
188
189 pixman_region32_intersect_rect(&cursor_region, &cursor_region,
190 output->base.x, output->base.y,
191 output->base.width, output->base.height);
192
193 if (!pixman_region32_not_empty(&cursor_region)) {
194 ret = 0;
195 goto out;
196 }
197
198 if (eid->sprite->image == EGL_NO_IMAGE_KHR)
199 goto out;
200
201 if (eid->sprite->width > 64 || eid->sprite->height > 64)
202 goto out;
203
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400204 c->export_drm_image(c->base.display, eid->sprite->image,
205 NULL, &handle, &stride);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200206
207 if (stride != 64 * 4) {
208 fprintf(stderr, "info: cursor stride is != 64\n");
209 goto out;
210 }
211
212 ret = drmModeSetCursor(c->drm.fd, output->crtc_id, handle, 64, 64);
213 if (ret) {
214 fprintf(stderr, "failed to set cursor: %s\n", strerror(-ret));
215 goto out;
216 }
217
218 ret = drmModeMoveCursor(c->drm.fd, output->crtc_id,
219 eid->sprite->x - output->base.x,
220 eid->sprite->y - output->base.y);
221 if (ret) {
222 fprintf(stderr, "failed to move cursor: %s\n", strerror(-ret));
223 goto out;
224 }
225
226 printf("info: set hardware cursor\n");
227
228out:
229 pixman_region32_fini(&cursor_region);
230 if (ret)
231 drmModeSetCursor(c->drm.fd, output->crtc_id, 0, 0, 0);
232 return ret;
233}
234
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400235static int
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400236on_drm_input(int fd, uint32_t mask, void *data)
237{
238 drmEventContext evctx;
239
240 memset(&evctx, 0, sizeof evctx);
241 evctx.version = DRM_EVENT_CONTEXT_VERSION;
242 evctx.page_flip_handler = page_flip_handler;
243 drmHandleEvent(fd, &evctx);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400244
245 return 1;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400246}
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;
Benjamin Franzkee5b3b262011-04-26 09:21:13 +0200269 ec->base.display = eglGetDisplay(FD_TO_EGL_NATIVE_DPY(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] =
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400380 ec->create_drm_image(ec->base.display, attribs);
381 ec->base.image_target_renderbuffer_storage(GL_RENDERBUFFER,
382 output->image[i]);
383 ec->export_drm_image(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++) {
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400478 ec->base.destroy_image(ec->base.display, output->image[i]);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100479 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
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400581static int
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100582udev_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);
Kristian Høgsbergb1868472011-04-22 12:27:57 -0400593
594 return 1;
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100595}
596
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200597static EGLImageKHR
598drm_compositor_create_cursor_image(struct wlsc_compositor *ec,
599 int32_t width, int32_t height)
600{
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400601 static const EGLint image_attribs[] = {
602 EGL_WIDTH, 64,
603 EGL_HEIGHT, 64,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200604 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400605 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_CURSOR_MESA,
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200606 EGL_NONE
607 };
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400608 struct drm_compositor *c = (struct drm_compositor *) ec;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200609
610 if (width > 64 || height > 64)
611 return EGL_NO_IMAGE_KHR;
612
Kristian Høgsberg7981d002011-05-06 13:23:49 -0400613 return c->create_drm_image(ec->display, image_attribs);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200614}
615
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500616static void
617drm_destroy(struct wlsc_compositor *ec)
618{
619 struct drm_compositor *d = (struct drm_compositor *) ec;
620
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500621 tty_destroy(d->tty);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500622
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500623 free(d);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500624}
625
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400626struct wlsc_compositor *
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400627drm_compositor_create(struct wl_display *display, int connector)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400628{
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400629 struct drm_compositor *ec;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400630 struct udev_enumerate *e;
631 struct udev_list_entry *entry;
632 struct udev_device *device;
633 const char *path;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400634 struct wl_event_loop *loop;
635
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400636 ec = malloc(sizeof *ec);
637 if (ec == NULL)
638 return NULL;
639
640 memset(ec, 0, sizeof *ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400641 ec->udev = udev_new();
642 if (ec->udev == NULL) {
643 fprintf(stderr, "failed to initialize udev context\n");
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400644 return NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400645 }
646
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400647 e = udev_enumerate_new(ec->udev);
648 udev_enumerate_add_match_subsystem(e, "drm");
649 udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
650 udev_enumerate_scan_devices(e);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400651 device = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400652 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
653 path = udev_list_entry_get_name(entry);
654 device = udev_device_new_from_syspath(ec->udev, path);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400655 break;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400656 }
657 udev_enumerate_unref(e);
658
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400659 if (device == NULL) {
660 fprintf(stderr, "no drm device found\n");
661 return NULL;
662 }
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400663
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -0400664 ec->base.wl_display = display;
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400665 if (init_egl(ec, device) < 0) {
666 fprintf(stderr, "failed to initialize egl\n");
667 return NULL;
668 }
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500669
670 ec->base.destroy = drm_destroy;
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200671 ec->base.create_cursor_image = drm_compositor_create_cursor_image;
672
Kristian Høgsberg8525a502011-01-14 16:20:21 -0500673 ec->base.focus = 1;
674
Benjamin Franzke5b2cb6f2011-02-18 16:54:55 +0100675 glGenFramebuffers(1, &ec->base.fbo);
676 glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
677
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400678 ec->create_drm_image =
679 (void *) eglGetProcAddress("eglCreateDRMImageMESA");
680 ec->export_drm_image =
681 (void *) eglGetProcAddress("eglExportDRMImageMESA");
682
Benjamin Franzked59eb1c2011-04-29 22:14:54 +0200683 /* Can't init base class until we have a current egl context */
684 if (wlsc_compositor_init(&ec->base, display) < 0)
685 return NULL;
686
Kristian Høgsberg61a82512010-10-26 11:26:44 -0400687 if (create_outputs(ec, connector) < 0) {
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400688 fprintf(stderr, "failed to create output for %s\n", path);
689 return NULL;
690 }
691
Kristian Høgsberg43db4012011-01-14 14:45:42 -0500692 evdev_input_add_devices(&ec->base, ec->udev);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400693
694 loop = wl_display_get_event_loop(ec->base.wl_display);
695 ec->drm_source =
Benjamin Franzke2af7f102011-03-02 11:14:59 +0100696 wl_event_loop_add_fd(loop, ec->drm.fd,
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400697 WL_EVENT_READABLE, on_drm_input, ec);
Kristian Høgsberge4762a62011-01-14 14:59:13 -0500698 ec->tty = tty_create(&ec->base);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400699
Benjamin Franzke9c26ff32011-03-15 15:08:41 +0100700 ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
701 if (ec->udev_monitor == NULL) {
702 fprintf(stderr, "failed to intialize udev monitor\n");
703 return NULL;
704 }
705 udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
706 "drm", NULL);
707 ec->udev_drm_source =
708 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
709 WL_EVENT_READABLE, udev_drm_event, ec);
710
711 if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
712 fprintf(stderr, "failed to enable udev-monitor receiving\n");
713 return NULL;
714 }
715
Kristian Høgsbergce5325d2010-06-14 11:54:00 -0400716 return &ec->base;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400717}
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400718
719struct wlsc_compositor *
720backend_init(struct wl_display *display, char *options)
721{
Kristian Høgsberg8050bd22011-05-04 15:08:04 -0400722 int connector = 0, i;
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400723 char *p, *value;
724
725 static char * const tokens[] = { "connector", NULL };
726
727 p = options;
728 while (i = getsubopt(&p, tokens, &value), i != -1) {
729 switch (i) {
730 case 0:
Kristian Høgsberg8050bd22011-05-04 15:08:04 -0400731 connector = strtol(value, NULL, 0);
Kristian Høgsberg1c562182011-05-02 22:09:20 -0400732 break;
733 }
734 }
735
736 return drm_compositor_create(display, connector);
737}