Merge remote-tracking branch 'bnf/surface-frame-event'
[profile/ivi/weston.git] / compositor / compositor-drm.c
1 /*
2  * Copyright © 2008-2010 Kristian Høgsberg
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include <xf86drm.h>
26 #include <xf86drmMode.h>
27
28 #define GL_GLEXT_PROTOTYPES
29 #define EGL_EGLEXT_PROTOTYPES
30 #include <GLES2/gl2.h>
31 #include <GLES2/gl2ext.h>
32 #include <EGL/egl.h>
33 #include <EGL/eglext.h>
34
35 #include "compositor.h"
36
37 struct drm_compositor {
38         struct wlsc_compositor base;
39
40         struct udev *udev;
41         struct wl_event_source *drm_source;
42
43         struct udev_monitor *udev_monitor;
44         struct wl_event_source *udev_drm_source;
45
46         struct {
47                 int fd;
48         } drm;
49         uint32_t crtc_allocator;
50         uint32_t connector_allocator;
51         struct tty *tty;
52 };
53
54 struct drm_output {
55         struct wlsc_output   base;
56
57         drmModeModeInfo mode;
58         uint32_t crtc_id;
59         uint32_t connector_id;
60         GLuint rbo[2];
61         uint32_t fb_id[2];
62         EGLImageKHR image[2];
63         uint32_t current;       
64 };
65
66 static int
67 drm_output_prepare_render(struct wlsc_output *output_base)
68 {
69         struct drm_output *output = (struct drm_output *) output_base;
70
71         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
72                                   GL_COLOR_ATTACHMENT0,
73                                   GL_RENDERBUFFER,
74                                   output->rbo[output->current]);
75
76         if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
77                 return -1;
78
79         return 0;
80 }
81
82 static int
83 drm_output_present(struct wlsc_output *output_base)
84 {
85         struct drm_output *output = (struct drm_output *) output_base;
86         struct drm_compositor *c =
87                 (struct drm_compositor *) output->base.compositor;
88
89         if (drm_output_prepare_render(&output->base))
90                 return -1;
91         glFlush();
92
93         output->current ^= 1;
94
95         drmModePageFlip(c->drm.fd, output->crtc_id,
96                         output->fb_id[output->current ^ 1],
97                         DRM_MODE_PAGE_FLIP_EVENT, output);
98
99         return 0;
100 }
101
102 static void
103 page_flip_handler(int fd, unsigned int frame,
104                   unsigned int sec, unsigned int usec, void *data)
105 {
106         struct wlsc_output *output = data;
107         uint32_t msecs;
108
109         msecs = sec * 1000 + usec / 1000;
110         wlsc_output_finish_frame(output, msecs);
111 }
112
113 static void
114 on_drm_input(int fd, uint32_t mask, void *data)
115 {
116         drmEventContext evctx;
117
118         memset(&evctx, 0, sizeof evctx);
119         evctx.version = DRM_EVENT_CONTEXT_VERSION;
120         evctx.page_flip_handler = page_flip_handler;
121         drmHandleEvent(fd, &evctx);
122 }
123
124 static int
125 init_egl(struct drm_compositor *ec, struct udev_device *device)
126 {
127         EGLint major, minor;
128         const char *extensions, *filename;
129         int fd;
130         static const EGLint context_attribs[] = {
131                 EGL_CONTEXT_CLIENT_VERSION, 2,
132                 EGL_NONE
133         };
134
135         filename = udev_device_get_devnode(device);
136         fd = open(filename, O_RDWR, O_CLOEXEC);
137         if (fd < 0) {
138                 /* Probably permissions error */
139                 fprintf(stderr, "couldn't open %s, skipping\n",
140                         udev_device_get_devnode(device));
141                 return -1;
142         }
143
144         ec->drm.fd = fd;
145         ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
146         if (ec->base.display == NULL) {
147                 fprintf(stderr, "failed to create display\n");
148                 return -1;
149         }
150
151         if (!eglInitialize(ec->base.display, &major, &minor)) {
152                 fprintf(stderr, "failed to initialize display\n");
153                 return -1;
154         }
155
156         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
157         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
158                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
159                 return -1;
160         }
161
162         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
163                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
164                 return -1;
165         }
166
167         ec->base.context = eglCreateContext(ec->base.display, NULL,
168                                             EGL_NO_CONTEXT, context_attribs);
169         if (ec->base.context == NULL) {
170                 fprintf(stderr, "failed to create context\n");
171                 return -1;
172         }
173
174         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
175                             EGL_NO_SURFACE, ec->base.context)) {
176                 fprintf(stderr, "failed to make context current\n");
177                 return -1;
178         }
179
180         return 0;
181 }
182
183 static drmModeModeInfo builtin_1024x768 = {
184         63500,                  /* clock */
185         1024, 1072, 1176, 1328, 0,
186         768, 771, 775, 798, 0,
187         59920,
188         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
189         0,
190         "1024x768"
191 };
192
193 static int
194 create_output_for_connector(struct drm_compositor *ec,
195                             drmModeRes *resources,
196                             drmModeConnector *connector,
197                             int x, int y)
198 {
199         struct drm_output *output;
200         drmModeEncoder *encoder;
201         drmModeModeInfo *mode;
202         int i, ret;
203         EGLint handle, stride, attribs[] = {
204                 EGL_WIDTH,              0,
205                 EGL_HEIGHT,             0,
206                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
207                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
208                 EGL_NONE
209         };
210
211         output = malloc(sizeof *output);
212         if (output == NULL)
213                 return -1;
214
215         if (connector->count_modes > 0) 
216                 mode = &connector->modes[0];
217         else
218                 mode = &builtin_1024x768;
219
220         encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
221         if (encoder == NULL) {
222                 fprintf(stderr, "No encoder for connector.\n");
223                 return -1;
224         }
225
226         for (i = 0; i < resources->count_crtcs; i++) {
227                 if (encoder->possible_crtcs & (1 << i) &&
228                     !(ec->crtc_allocator & (1 << resources->crtcs[i])))
229                         break;
230         }
231         if (i == resources->count_crtcs) {
232                 fprintf(stderr, "No usable crtc for encoder.\n");
233                 return -1;
234         }
235
236         memset(output, 0, sizeof *output);
237         wlsc_output_init(&output->base, &ec->base, x, y,
238                          mode->hdisplay, mode->vdisplay, 0);
239
240         output->crtc_id = resources->crtcs[i];
241         ec->crtc_allocator |= (1 << output->crtc_id);
242
243         output->connector_id = connector->connector_id;
244         ec->connector_allocator |= (1 << output->connector_id);
245         output->mode = *mode;
246
247         drmModeFreeEncoder(encoder);
248
249         glGenRenderbuffers(2, output->rbo);
250         for (i = 0; i < 2; i++) {
251                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
252
253                 attribs[1] = output->base.width;
254                 attribs[3] = output->base.height;
255                 output->image[i] =
256                         eglCreateDRMImageMESA(ec->base.display, attribs);
257                 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
258                                                        output->image[i]);
259                 eglExportDRMImageMESA(ec->base.display, output->image[i],
260                                       NULL, &handle, &stride);
261
262                 ret = drmModeAddFB(ec->drm.fd,
263                                    output->base.width, output->base.height,
264                                    32, 32, stride, handle, &output->fb_id[i]);
265                 if (ret) {
266                         fprintf(stderr, "failed to add fb %d: %m\n", i);
267                         return -1;
268                 }
269         }
270
271         output->current = 0;
272         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
273                                   GL_COLOR_ATTACHMENT0,
274                                   GL_RENDERBUFFER,
275                                   output->rbo[output->current]);
276         ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
277                              output->fb_id[output->current ^ 1], 0, 0,
278                              &output->connector_id, 1, &output->mode);
279         if (ret) {
280                 fprintf(stderr, "failed to set mode: %m\n");
281                 return -1;
282         }
283
284         output->base.prepare_render = drm_output_prepare_render;
285         output->base.present = drm_output_present;
286
287         wl_list_insert(ec->base.output_list.prev, &output->base.link);
288
289         return 0;
290 }
291
292 static int
293 create_outputs(struct drm_compositor *ec, int option_connector)
294 {
295         drmModeConnector *connector;
296         drmModeRes *resources;
297         int i;
298         int x = 0, y = 0;
299
300         resources = drmModeGetResources(ec->drm.fd);
301         if (!resources) {
302                 fprintf(stderr, "drmModeGetResources failed\n");
303                 return -1;
304         }
305
306         for (i = 0; i < resources->count_connectors; i++) {
307                 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
308                 if (connector == NULL)
309                         continue;
310
311                 if (connector->connection == DRM_MODE_CONNECTED &&
312                     (option_connector == 0 ||
313                      connector->connector_id == option_connector))
314                         if (create_output_for_connector(ec, resources,
315                                                         connector, x, y) < 0)
316                                 return -1;
317
318                 x += container_of(ec->base.output_list.prev, struct wlsc_output,
319                                   link)->width;
320
321                 drmModeFreeConnector(connector);
322         }
323
324         if (wl_list_empty(&ec->base.output_list)) {
325                 fprintf(stderr, "No currently active connector found.\n");
326                 return -1;
327         }
328
329         drmModeFreeResources(resources);
330
331         return 0;
332 }
333
334 static int
335 destroy_output(struct drm_output *output)
336 {
337         struct drm_compositor *ec =
338                 (struct drm_compositor *) output->base.compositor;
339         int i;
340
341         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
342                                   GL_COLOR_ATTACHMENT0,
343                                   GL_RENDERBUFFER,
344                                   0);
345
346         glBindRenderbuffer(GL_RENDERBUFFER, 0);
347         glDeleteRenderbuffers(2, output->rbo);
348
349         for (i = 0; i < 2; i++) {
350                 eglDestroyImageKHR(ec->base.display, output->image[i]);
351                 drmModeRmFB(ec->drm.fd, output->fb_id[i]);
352         }
353         
354         ec->crtc_allocator &= ~(1 << output->crtc_id);
355         ec->connector_allocator &= ~(1 << output->connector_id);
356
357         wlsc_output_destroy(&output->base);
358         wl_list_remove(&output->base.link);
359
360         free(output);
361
362         return 0;
363 }
364
365 static void
366 update_outputs(struct drm_compositor *ec)
367 {
368         drmModeConnector *connector;
369         drmModeRes *resources;
370         struct drm_output *output, *next;
371         int x = 0, y = 0;
372         int x_offset = 0, y_offset = 0;
373         uint32_t connected = 0, disconnects = 0;
374         int i;
375
376         resources = drmModeGetResources(ec->drm.fd);
377         if (!resources) {
378                 fprintf(stderr, "drmModeGetResources failed\n");
379                 return;
380         }
381
382         /* collect new connects */
383         for (i = 0; i < resources->count_connectors; i++) {
384                 connector =
385                         drmModeGetConnector(ec->drm.fd,
386                                             resources->connectors[i]);
387                 if (connector == NULL ||
388                     connector->connection != DRM_MODE_CONNECTED)
389                         continue;
390
391                 connected |= (1 << connector->connector_id);
392                 
393                 if (!(ec->connector_allocator & (1 << connector->connector_id))) {
394                         struct wlsc_output *last_output =
395                                 container_of(ec->base.output_list.prev,
396                                              struct wlsc_output, link);
397
398                         /* XXX: not yet needed, we die with 0 outputs */
399                         if (!wl_list_empty(&ec->base.output_list))
400                                 x = last_output->x + last_output->width;
401                         else
402                                 x = 0;
403                         y = 0;
404                         create_output_for_connector(ec, resources,
405                                                     connector, x, y);
406                         printf("connector %d connected\n",
407                                connector->connector_id);
408                                 
409                 }
410                 drmModeFreeConnector(connector);
411         }
412         drmModeFreeResources(resources);
413
414         disconnects = ec->connector_allocator & ~connected;
415         if (disconnects) {
416                 wl_list_for_each_safe(output, next, &ec->base.output_list,
417                                       base.link) {
418                         if (x_offset != 0 || y_offset != 0) {
419                                 wlsc_output_move(&output->base,
420                                                  output->base.x - x_offset,
421                                                  output->base.y - y_offset);
422                         }
423
424                         if (disconnects & (1 << output->connector_id)) {
425                                 disconnects &= ~(1 << output->connector_id);
426                                 printf("connector %d disconnected\n",
427                                        output->connector_id);
428                                 x_offset += output->base.width;
429                                 destroy_output(output);
430                         }
431                 }
432         }
433
434         /* FIXME: handle zero outputs, without terminating */   
435         if (ec->connector_allocator == 0)
436                 wl_display_terminate(ec->base.wl_display);
437 }
438
439 static int
440 udev_event_is_hotplug(struct udev_device *device)
441 {
442         struct udev_list_entry *list, *hotplug_entry;
443         
444         list = udev_device_get_properties_list_entry(device);
445
446         hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
447         if (hotplug_entry == NULL)
448                 return 0;
449
450         return strcmp(udev_list_entry_get_value(hotplug_entry), "1") == 0;
451 }
452
453 static void
454 udev_drm_event(int fd, uint32_t mask, void *data)
455 {
456         struct drm_compositor *ec = data;
457         struct udev_device *event;
458
459         event = udev_monitor_receive_device(ec->udev_monitor);
460         
461         if (udev_event_is_hotplug(event))
462                 update_outputs(ec);
463
464         udev_device_unref(event);
465 }
466
467 static void
468 drm_destroy(struct wlsc_compositor *ec)
469 {
470         struct drm_compositor *d = (struct drm_compositor *) ec;
471
472         tty_destroy(d->tty);
473
474         free(d);
475 }
476
477 struct wlsc_compositor *
478 drm_compositor_create(struct wl_display *display, int connector)
479 {
480         struct drm_compositor *ec;
481         struct udev_enumerate *e;
482         struct udev_list_entry *entry;
483         struct udev_device *device;
484         const char *path;
485         struct wl_event_loop *loop;
486
487         ec = malloc(sizeof *ec);
488         if (ec == NULL)
489                 return NULL;
490
491         memset(ec, 0, sizeof *ec);
492         ec->udev = udev_new();
493         if (ec->udev == NULL) {
494                 fprintf(stderr, "failed to initialize udev context\n");
495                 return NULL;
496         }
497
498         e = udev_enumerate_new(ec->udev);
499         udev_enumerate_add_match_subsystem(e, "drm");
500         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
501         udev_enumerate_scan_devices(e);
502         device = NULL;
503         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
504                 path = udev_list_entry_get_name(entry);
505                 device = udev_device_new_from_syspath(ec->udev, path);
506                 break;
507         }
508         udev_enumerate_unref(e);
509
510         if (device == NULL) {
511                 fprintf(stderr, "no drm device found\n");
512                 return NULL;
513         }
514
515         ec->base.wl_display = display;
516         if (init_egl(ec, device) < 0) {
517                 fprintf(stderr, "failed to initialize egl\n");
518                 return NULL;
519         }
520
521         ec->base.destroy = drm_destroy;
522         ec->base.create_buffer = wlsc_shm_buffer_create;
523         ec->base.focus = 1;
524
525         glGenFramebuffers(1, &ec->base.fbo);
526         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
527
528         /* Can't init base class until we have a current egl context */
529         if (wlsc_compositor_init(&ec->base, display) < 0)
530                 return NULL;
531
532         if (create_outputs(ec, connector) < 0) {
533                 fprintf(stderr, "failed to create output for %s\n", path);
534                 return NULL;
535         }
536
537         evdev_input_add_devices(&ec->base, ec->udev);
538
539         loop = wl_display_get_event_loop(ec->base.wl_display);
540         ec->drm_source =
541                 wl_event_loop_add_fd(loop, ec->drm.fd,
542                                      WL_EVENT_READABLE, on_drm_input, ec);
543         ec->tty = tty_create(&ec->base);
544
545         ec->udev_monitor = udev_monitor_new_from_netlink(ec->udev, "udev");
546         if (ec->udev_monitor == NULL) {
547                 fprintf(stderr, "failed to intialize udev monitor\n");
548                 return NULL;
549         }
550         udev_monitor_filter_add_match_subsystem_devtype(ec->udev_monitor,
551                                                         "drm", NULL);
552         ec->udev_drm_source =
553                 wl_event_loop_add_fd(loop, udev_monitor_get_fd(ec->udev_monitor),
554                                      WL_EVENT_READABLE, udev_drm_event, ec);
555
556         if (udev_monitor_enable_receiving(ec->udev_monitor) < 0) {
557                 fprintf(stderr, "failed to enable udev-monitor receiving\n");
558                 return NULL;
559         }
560
561         return &ec->base;
562 }