compositor-drm: Fix multi head rendering
[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 {
44                 int fd;
45         } drm;
46         uint32_t crtc_allocator;
47         struct tty *tty;
48 };
49
50 struct drm_output {
51         struct wlsc_output   base;
52
53         drmModeModeInfo mode;
54         uint32_t crtc_id;
55         uint32_t connector_id;
56         GLuint rbo[2];
57         uint32_t fb_id[2];
58         EGLImageKHR image[2];
59         uint32_t current;       
60 };
61
62 static int
63 drm_output_prepare_render(struct wlsc_output *output_base)
64 {
65         struct drm_output *output = (struct drm_output *) output_base;
66
67         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
68                                   GL_COLOR_ATTACHMENT0,
69                                   GL_RENDERBUFFER,
70                                   output->rbo[output->current]);
71
72         if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
73                 return -1;
74
75         return 0;
76 }
77
78 static void
79 drm_compositor_present(struct wlsc_compositor *ec)
80 {
81         struct drm_compositor *c = (struct drm_compositor *) ec;
82         struct drm_output *output;
83
84         wl_list_for_each(output, &ec->output_list, base.link) {
85                 if (drm_output_prepare_render(&output->base))
86                         continue;
87                 glFlush();
88
89                 output->current ^= 1;
90
91                 drmModePageFlip(c->drm.fd, output->crtc_id,
92                                 output->fb_id[output->current ^ 1],
93                                 DRM_MODE_PAGE_FLIP_EVENT, output);
94         }       
95 }
96
97 static void
98 page_flip_handler(int fd, unsigned int frame,
99                   unsigned int sec, unsigned int usec, void *data)
100 {
101         struct wlsc_output *output = data;
102         struct wlsc_compositor *compositor = output->compositor;
103         uint32_t msecs;
104
105         /* run synchronized to first output, ignore other pflip events.
106          * FIXME: support per output/surface frame callbacks */
107         if (output == container_of(compositor->output_list.prev,
108                                    struct wlsc_output, link)) {
109                 msecs = sec * 1000 + usec / 1000;
110                 wlsc_compositor_finish_frame(compositor, msecs);
111         }
112 }
113
114 static void
115 on_drm_input(int fd, uint32_t mask, void *data)
116 {
117         drmEventContext evctx;
118
119         memset(&evctx, 0, sizeof evctx);
120         evctx.version = DRM_EVENT_CONTEXT_VERSION;
121         evctx.page_flip_handler = page_flip_handler;
122         drmHandleEvent(fd, &evctx);
123 }
124
125 static int
126 init_egl(struct drm_compositor *ec, struct udev_device *device)
127 {
128         EGLint major, minor;
129         const char *extensions, *filename;
130         int fd;
131         static const EGLint context_attribs[] = {
132                 EGL_CONTEXT_CLIENT_VERSION, 2,
133                 EGL_NONE
134         };
135
136         filename = udev_device_get_devnode(device);
137         fd = open(filename, O_RDWR);
138         if (fd < 0) {
139                 /* Probably permissions error */
140                 fprintf(stderr, "couldn't open %s, skipping\n",
141                         udev_device_get_devnode(device));
142                 return -1;
143         }
144
145         ec->drm.fd = fd;
146         ec->base.display = eglGetDRMDisplayMESA(ec->drm.fd);
147         if (ec->base.display == NULL) {
148                 fprintf(stderr, "failed to create display\n");
149                 return -1;
150         }
151
152         if (!eglInitialize(ec->base.display, &major, &minor)) {
153                 fprintf(stderr, "failed to initialize display\n");
154                 return -1;
155         }
156
157         extensions = eglQueryString(ec->base.display, EGL_EXTENSIONS);
158         if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
159                 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
160                 return -1;
161         }
162
163         if (!eglBindAPI(EGL_OPENGL_ES_API)) {
164                 fprintf(stderr, "failed to bind api EGL_OPENGL_ES_API\n");
165                 return -1;
166         }
167
168         ec->base.context = eglCreateContext(ec->base.display, NULL,
169                                             EGL_NO_CONTEXT, context_attribs);
170         if (ec->base.context == NULL) {
171                 fprintf(stderr, "failed to create context\n");
172                 return -1;
173         }
174
175         if (!eglMakeCurrent(ec->base.display, EGL_NO_SURFACE,
176                             EGL_NO_SURFACE, ec->base.context)) {
177                 fprintf(stderr, "failed to make context current\n");
178                 return -1;
179         }
180
181         return 0;
182 }
183
184 static drmModeModeInfo builtin_1024x768 = {
185         63500,                  /* clock */
186         1024, 1072, 1176, 1328, 0,
187         768, 771, 775, 798, 0,
188         59920,
189         DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC,
190         0,
191         "1024x768"
192 };
193
194 static int
195 create_output_for_connector(struct drm_compositor *ec,
196                             drmModeRes *resources,
197                             drmModeConnector *connector,
198                             int x, int y)
199 {
200         struct drm_output *output;
201         drmModeEncoder *encoder;
202         drmModeModeInfo *mode;
203         int i, ret;
204         EGLint handle, stride, attribs[] = {
205                 EGL_WIDTH,              0,
206                 EGL_HEIGHT,             0,
207                 EGL_DRM_BUFFER_FORMAT_MESA,     EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
208                 EGL_DRM_BUFFER_USE_MESA,        EGL_DRM_BUFFER_USE_SCANOUT_MESA,
209                 EGL_NONE
210         };
211
212         output = malloc(sizeof *output);
213         if (output == NULL)
214                 return -1;
215
216         if (connector->count_modes > 0) 
217                 mode = &connector->modes[0];
218         else
219                 mode = &builtin_1024x768;
220
221         encoder = drmModeGetEncoder(ec->drm.fd, connector->encoders[0]);
222         if (encoder == NULL) {
223                 fprintf(stderr, "No encoder for connector.\n");
224                 return -1;
225         }
226
227         for (i = 0; i < resources->count_crtcs; i++) {
228                 if (encoder->possible_crtcs & (1 << i) &&
229                     !(ec->crtc_allocator & (1 << i)))
230                         break;
231         }
232         if (i == resources->count_crtcs) {
233                 fprintf(stderr, "No usable crtc for encoder.\n");
234                 return -1;
235         }
236
237         memset(output, 0, sizeof *output);
238         wlsc_output_init(&output->base, &ec->base, x, y,
239                          mode->hdisplay, mode->vdisplay, 0);
240
241         ec->crtc_allocator |= (1 << i);
242         output->crtc_id = resources->crtcs[i];
243         output->connector_id = connector->connector_id;
244         output->mode = *mode;
245
246         drmModeFreeEncoder(encoder);
247
248         glGenRenderbuffers(2, output->rbo);
249         for (i = 0; i < 2; i++) {
250                 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
251
252                 attribs[1] = output->base.width;
253                 attribs[3] = output->base.height;
254                 output->image[i] =
255                         eglCreateDRMImageMESA(ec->base.display, attribs);
256                 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
257                                                        output->image[i]);
258                 eglExportDRMImageMESA(ec->base.display, output->image[i],
259                                       NULL, &handle, &stride);
260
261                 ret = drmModeAddFB(ec->drm.fd,
262                                    output->base.width, output->base.height,
263                                    32, 32, stride, handle, &output->fb_id[i]);
264                 if (ret) {
265                         fprintf(stderr, "failed to add fb %d: %m\n", i);
266                         return -1;
267                 }
268         }
269
270         output->current = 0;
271         glFramebufferRenderbuffer(GL_FRAMEBUFFER,
272                                   GL_COLOR_ATTACHMENT0,
273                                   GL_RENDERBUFFER,
274                                   output->rbo[output->current]);
275         ret = drmModeSetCrtc(ec->drm.fd, output->crtc_id,
276                              output->fb_id[output->current ^ 1], 0, 0,
277                              &output->connector_id, 1, &output->mode);
278         if (ret) {
279                 fprintf(stderr, "failed to set mode: %m\n");
280                 return -1;
281         }
282
283         output->base.prepare_render = drm_output_prepare_render;
284
285         wl_list_insert(ec->base.output_list.prev, &output->base.link);
286
287         return 0;
288 }
289
290 static int
291 create_outputs(struct drm_compositor *ec, int option_connector)
292 {
293         drmModeConnector *connector;
294         drmModeRes *resources;
295         int i;
296         int x = 0, y = 0;
297
298         resources = drmModeGetResources(ec->drm.fd);
299         if (!resources) {
300                 fprintf(stderr, "drmModeGetResources failed\n");
301                 return -1;
302         }
303
304         for (i = 0; i < resources->count_connectors; i++) {
305                 connector = drmModeGetConnector(ec->drm.fd, resources->connectors[i]);
306                 if (connector == NULL)
307                         continue;
308
309                 if (connector->connection == DRM_MODE_CONNECTED &&
310                     (option_connector == 0 ||
311                      connector->connector_id == option_connector))
312                         if (create_output_for_connector(ec, resources,
313                                                         connector, x, y) < 0)
314                                 return -1;
315
316                 x += container_of(ec->base.output_list.prev, struct wlsc_output,
317                                   link)->width;
318
319                 drmModeFreeConnector(connector);
320         }
321
322         if (wl_list_empty(&ec->base.output_list)) {
323                 fprintf(stderr, "No currently active connector found.\n");
324                 return -1;
325         }
326
327         drmModeFreeResources(resources);
328
329         return 0;
330 }
331
332 static void
333 drm_destroy(struct wlsc_compositor *ec)
334 {
335         struct drm_compositor *d = (struct drm_compositor *) ec;
336
337         tty_destroy(d->tty);
338
339         free(d);
340 }
341
342 struct wlsc_compositor *
343 drm_compositor_create(struct wl_display *display, int connector)
344 {
345         struct drm_compositor *ec;
346         struct udev_enumerate *e;
347         struct udev_list_entry *entry;
348         struct udev_device *device;
349         const char *path;
350         struct wl_event_loop *loop;
351
352         ec = malloc(sizeof *ec);
353         if (ec == NULL)
354                 return NULL;
355
356         memset(ec, 0, sizeof *ec);
357         ec->udev = udev_new();
358         if (ec->udev == NULL) {
359                 fprintf(stderr, "failed to initialize udev context\n");
360                 return NULL;
361         }
362
363         e = udev_enumerate_new(ec->udev);
364         udev_enumerate_add_match_subsystem(e, "drm");
365         udev_enumerate_add_match_property(e, "WAYLAND_SEAT", "1");
366         udev_enumerate_scan_devices(e);
367         device = NULL;
368         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
369                 path = udev_list_entry_get_name(entry);
370                 device = udev_device_new_from_syspath(ec->udev, path);
371                 break;
372         }
373         udev_enumerate_unref(e);
374
375         if (device == NULL) {
376                 fprintf(stderr, "no drm device found\n");
377                 return NULL;
378         }
379
380         ec->base.wl_display = display;
381         if (init_egl(ec, device) < 0) {
382                 fprintf(stderr, "failed to initialize egl\n");
383                 return NULL;
384         }
385
386         ec->base.destroy = drm_destroy;
387         ec->base.present = drm_compositor_present;
388         ec->base.create_buffer = wlsc_shm_buffer_create;
389         ec->base.focus = 1;
390
391         glGenFramebuffers(1, &ec->base.fbo);
392         glBindFramebuffer(GL_FRAMEBUFFER, ec->base.fbo);
393
394         /* Can't init base class until we have a current egl context */
395         if (wlsc_compositor_init(&ec->base, display) < 0)
396                 return NULL;
397
398         if (create_outputs(ec, connector) < 0) {
399                 fprintf(stderr, "failed to create output for %s\n", path);
400                 return NULL;
401         }
402
403         evdev_input_add_devices(&ec->base, ec->udev);
404
405         loop = wl_display_get_event_loop(ec->base.wl_display);
406         ec->drm_source =
407                 wl_event_loop_add_fd(loop, ec->drm.fd,
408                                      WL_EVENT_READABLE, on_drm_input, ec);
409         ec->tty = tty_create(&ec->base);
410
411         return &ec->base;
412 }