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