blob: a7a7874f463fbb5208fbb383f9c0bfcfdd717815 [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
2 * Copyright © 2010 Benjamin Franzke
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#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <stddef.h>
24#define _GNU_SOURCE
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030
31#include "wayland-client.h"
32
33#define GL_GLEXT_PROTOTYPES
34#define EGL_EGLEXT_PROTOTYPES
35#include <GLES2/gl2.h>
36#include <GLES2/gl2ext.h>
37#include <EGL/egl.h>
38#include <EGL/eglext.h>
39
40#include "compositor.h"
41
42struct wayland_compositor {
43 struct wlsc_compositor base;
44
45 struct {
46 struct wl_display *display;
47 struct wl_compositor *compositor;
48 struct wl_shell *shell;
49 struct wl_drm *drm;
50 struct wl_output *output;
51
52 struct {
53 int32_t x, y, width, height;
54 } screen_allocation;
55
56 char *device_name;
57 int authenticated;
58
59 struct wl_event_source *wl_source;
60 uint32_t event_mask;
61 } parent;
62
63 struct wl_list window_list;
64 struct wl_list input_list;
65};
66
67struct wayland_output {
68 struct wlsc_output base;
69
70 struct {
71 struct wl_surface *surface;
72 struct wl_buffer *buffer[2];
73 } parent;
74 EGLImageKHR image[2];
75 GLuint rbo[2];
76 uint32_t fb_id[2];
77 uint32_t current;
78};
79
80struct wayland_input {
81 struct wayland_compositor *compositor;
82 struct wl_input_device *input_device;
83 struct wl_list link;
84};
85
86static int
87wayland_input_create(struct wayland_compositor *c)
88{
89 struct wlsc_input_device *input;
90
91 input = malloc(sizeof *input);
92 if (input == NULL)
93 return -1;
94
95 memset(input, 0, sizeof *input);
96 wlsc_input_device_init(input, &c->base);
97
Kristian Høgsberg9c3e8d72010-12-08 09:48:52 -050098 c->base.input_device = &input->input_device;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010099
100 return 0;
101}
102
103static int
104wayland_compositor_init_egl(struct wayland_compositor *c)
105{
106 EGLint major, minor;
107 const char *extensions;
108 drm_magic_t magic;
109 int fd;
110 static const EGLint context_attribs[] = {
111 EGL_CONTEXT_CLIENT_VERSION, 2,
112 EGL_NONE
113 };
114
115 fd = open(c->parent.device_name, O_RDWR);
116 if (fd < 0) {
117 fprintf(stderr, "drm open failed: %m\n");
118 return -1;
119 }
120
121 wlsc_drm_init(&c->base, fd, c->parent.device_name);
122
123 if (drmGetMagic(fd, &magic)) {
124 fprintf(stderr, "DRI2: failed to get drm magic");
125 return -1;
126 }
127
128 wl_drm_authenticate(c->parent.drm, magic);
129 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
130 while (!c->parent.authenticated)
131 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
132
133 c->base.display = eglGetDRMDisplayMESA(fd);
134 if (c->base.display == NULL) {
135 fprintf(stderr, "failed to create display\n");
136 return -1;
137 }
138
139 if (!eglInitialize(c->base.display, &major, &minor)) {
140 fprintf(stderr, "failed to initialize display\n");
141 return -1;
142 }
143
144 extensions = eglQueryString(c->base.display, EGL_EXTENSIONS);
145 if (!strstr(extensions, "EGL_KHR_surfaceless_opengl")) {
146 fprintf(stderr, "EGL_KHR_surfaceless_opengl not available\n");
147 return -1;
148 }
149
150 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
151 fprintf(stderr, "failed to bind EGL_OPENGL_ES_API\n");
152 return -1;
153 }
154
155 c->base.context = eglCreateContext(c->base.display, NULL,
156 EGL_NO_CONTEXT, context_attribs);
157 if (c->base.context == NULL) {
158 fprintf(stderr, "failed to create context\n");
159 return -1;
160 }
161
162 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
163 EGL_NO_SURFACE, c->base.context)) {
164 fprintf(stderr, "failed to make context current\n");
165 return -1;
166 }
167
168 return 0;
169}
170
171static void
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500172frame_callback(void *data, uint32_t time)
173{
174 struct wayland_compositor *c = (struct wayland_compositor *) data;
175
176 wlsc_compositor_finish_frame(&c->base, time);
177}
178
179static void
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100180wayland_compositor_present(struct wlsc_compositor *base)
181{
182 struct wayland_compositor *c = (struct wayland_compositor *) base;
183 struct wayland_output *output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100184
Kristian Høgsberg4203df12010-12-01 09:40:58 -0500185 glFlush();
186
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100187 wl_list_for_each(output, &base->output_list, base.link) {
188 output->current ^= 1;
189
190 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
191 GL_COLOR_ATTACHMENT0,
192 GL_RENDERBUFFER,
193 output->rbo[output->current]);
194
195 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500196 output->parent.buffer[output->current ^ 1],
197 0, 0);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100198 wl_surface_damage(output->parent.surface, 0, 0,
199 output->base.width, output->base.height);
200 }
201
Kristian Høgsberg3ada7ec2010-12-01 09:42:10 -0500202 wl_display_frame_callback(c->parent.display, frame_callback, c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100203}
204
205static int
206wayland_authenticate(struct wlsc_compositor *ec, uint32_t id)
207{
208 struct wayland_compositor *c = (struct wayland_compositor *) ec;
209
210 wl_drm_authenticate(c->parent.drm, id);
211 /* FIXME: recv drm_authenticated event from parent? */
212 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
213
214 return 0;
215}
216
217static int
218wayland_compositor_create_output(struct wayland_compositor *c,
219 int width, int height)
220{
221 struct wayland_output *output;
222 struct wl_visual *visual;
223 int i;
224 EGLint name, stride, attribs[] = {
225 EGL_WIDTH, 0,
226 EGL_HEIGHT, 0,
227 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
228 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
229 EGL_NONE
230 };
231
232 output = malloc(sizeof *output);
233 if (output == NULL)
234 return -1;
235 memset(output, 0, sizeof *output);
236
237 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
238 output->parent.surface =
239 wl_compositor_create_surface(c->parent.compositor);
240 wl_surface_set_user_data(output->parent.surface, output);
241
242 glGenRenderbuffers(2, output->rbo);
243 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
244 for (i = 0; i < 2; i++) {
245 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
246
247 attribs[1] = width;
248 attribs[3] = height;
249 output->image[i] =
250 eglCreateDRMImageMESA(c->base.display, attribs);
251 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
252 output->image[i]);
253 eglExportDRMImageMESA(c->base.display, output->image[i],
254 &name, NULL, &stride);
255 output->parent.buffer[i] =
256 wl_drm_create_buffer(c->parent.drm, name,
257 width, height,
258 stride, visual);
259 }
260
261 output->current = 0;
262 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
263 GL_COLOR_ATTACHMENT0,
264 GL_RENDERBUFFER,
265 output->rbo[output->current]);
266
267 wl_surface_attach(output->parent.surface,
Kristian Høgsberg82da52b2010-12-17 09:53:12 -0500268 output->parent.buffer[output->current], 0, 0);
269 wl_surface_map_toplevel(output->parent.surface);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100270
271 glClearColor(0, 0, 0, 0.5);
272
273 wl_list_insert(c->base.output_list.prev, &output->base.link);
274
275 return 0;
276}
277
278static struct wl_buffer *
279create_invisible_pointer(struct wayland_compositor *c)
280{
281 struct wlsc_drm_buffer *wlsc_buffer;
282 struct wl_buffer *buffer;
283 int name, stride;
284 struct wl_visual *visual;
285 GLuint texture;
286 const int width = 1, height = 1;
287 const GLubyte data[] = { 0, 0, 0, 0 };
288
289 glGenTextures(1, &texture);
290 glBindTexture(GL_TEXTURE_2D, texture);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
295
296 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
297 wlsc_buffer = wlsc_drm_buffer_create(&c->base, width, height, visual);
298
299 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, wlsc_buffer->image);
300 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
301 GL_RGBA, GL_UNSIGNED_BYTE, data);
302
303 eglExportDRMImageMESA(c->base.display, wlsc_buffer->image,
304 &name, NULL, &stride);
305
306 buffer = wl_drm_create_buffer(c->parent.drm, name,
307 width, height,
308 stride, visual);
309 return buffer;
310}
311
312/* Events received from the wayland-server this compositor is client of: */
313
314/* parent output interface */
315static void
316display_handle_geometry(void *data,
317 struct wl_output *output,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500318 int32_t x, int32_t y,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100319 int32_t width, int32_t height)
320{
321 struct wayland_compositor *c = data;
322
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500323 c->parent.screen_allocation.x = x;
324 c->parent.screen_allocation.y = y;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100325 c->parent.screen_allocation.width = width;
326 c->parent.screen_allocation.height = height;
327}
328
329static const struct wl_output_listener output_listener = {
330 display_handle_geometry,
331};
332
333/* parent shell interface */
334static void
335handle_configure(void *data, struct wl_shell *shell,
336 uint32_t time, uint32_t edges,
Kristian Høgsbergcbe6f042010-12-17 09:54:45 -0500337 struct wl_surface *surface, int32_t width, int32_t height)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100338{
339#if 0
340 struct output *output = wl_surface_get_user_data(surface);
341
342 /* FIXME: add resize? */
343#endif
344}
345
346static const struct wl_shell_listener shell_listener = {
347 handle_configure,
348};
349
350/* parent drm interface */
351static void
352drm_handle_device(void *data, struct wl_drm *drm, const char *device)
353{
354 struct wayland_compositor *c = data;
355
356 c->parent.device_name = strdup(device);
357}
358
359static void drm_handle_authenticated(void *data, struct wl_drm *drm)
360{
361 struct wayland_compositor *c = data;
362
363 c->parent.authenticated = 1;
364}
365
366static const struct wl_drm_listener drm_listener = {
367 drm_handle_device,
368 drm_handle_authenticated
369};
370
371/* parent input interface */
372static void
373input_handle_motion(void *data, struct wl_input_device *input_device,
374 uint32_t time,
375 int32_t x, int32_t y, int32_t sx, int32_t sy)
376{
377 struct wayland_input *input = data;
378 struct wayland_compositor *c = input->compositor;
379
380 notify_motion(c->base.input_device, time, sx, sy);
381}
382
383static void
384input_handle_button(void *data,
385 struct wl_input_device *input_device,
386 uint32_t time, uint32_t button, uint32_t state)
387{
388 struct wayland_input *input = data;
389 struct wayland_compositor *c = input->compositor;
390
391 notify_button(c->base.input_device, time, button, state);
392}
393
394static void
395input_handle_key(void *data, struct wl_input_device *input_device,
396 uint32_t time, uint32_t key, uint32_t state)
397{
398 struct wayland_input *input = data;
399 struct wayland_compositor *c = input->compositor;
400
401 notify_key(c->base.input_device, time, key, state);
402}
403
404static void
405input_handle_pointer_focus(void *data,
406 struct wl_input_device *input_device,
407 uint32_t time, struct wl_surface *surface,
408 int32_t x, int32_t y, int32_t sx, int32_t sy)
409{
410 struct wayland_input *input = data;
411 struct wayland_compositor *c = input->compositor;
412 static struct wl_buffer *pntr_buffer = NULL;
413
414 if (surface) {
415 c->base.focus = 1;
416 /* FIXME: extend protocol to allow hiding the cursor? */
417 if (pntr_buffer == NULL)
418 pntr_buffer = create_invisible_pointer(c);
419 wl_input_device_attach(input_device, time, pntr_buffer, x, y);
420 } else {
421 /* FIXME. hide our own pointer */
422 c->base.focus = 0;
423 }
424}
425
426static void
427input_handle_keyboard_focus(void *data,
428 struct wl_input_device *input_device,
429 uint32_t time,
430 struct wl_surface *surface,
431 struct wl_array *keys)
432{
433 /* FIXME: sth to be done here? */
434}
435
436static const struct wl_input_device_listener input_device_listener = {
437 input_handle_motion,
438 input_handle_button,
439 input_handle_key,
440 input_handle_pointer_focus,
441 input_handle_keyboard_focus,
442};
443
444static void
445display_add_input(struct wayland_compositor *c, uint32_t id)
446{
447 struct wayland_input *input;
448
449 input = malloc(sizeof *input);
450 if (input == NULL)
451 return;
452
453 memset(input, 0, sizeof *input);
454
455 input->compositor = c;
456 input->input_device = wl_input_device_create(c->parent.display, id);
457 wl_list_insert(c->input_list.prev, &input->link);
458
459 wl_input_device_add_listener(input->input_device,
460 &input_device_listener, input);
461 wl_input_device_set_user_data(input->input_device, input);
462}
463
464static void
465display_handle_global(struct wl_display *display, uint32_t id,
466 const char *interface, uint32_t version, void *data)
467{
468 struct wayland_compositor *c = data;
469
470 if (strcmp(interface, "compositor") == 0) {
471 c->parent.compositor = wl_compositor_create(display, id);
472 } else if (strcmp(interface, "output") == 0) {
473 c->parent.output = wl_output_create(display, id);
474 wl_output_add_listener(c->parent.output, &output_listener, c);
475 } else if (strcmp(interface, "input_device") == 0) {
476 display_add_input(c, id);
477 } else if (strcmp(interface, "shell") == 0) {
478 c->parent.shell = wl_shell_create(display, id);
479 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
480 } else if (strcmp(interface, "drm") == 0) {
481 c->parent.drm = wl_drm_create(display, id);
482 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
483 }
484}
485
486static int
487update_event_mask(uint32_t mask, void *data)
488{
489 struct wayland_compositor *c = data;
490
491 c->parent.event_mask = mask;
492 if (c->parent.wl_source)
493 wl_event_source_fd_update(c->parent.wl_source, mask);
494
495 return 0;
496}
497
498static void
499wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
500{
501 struct wayland_compositor *c = data;
502
503 if (mask & WL_EVENT_READABLE)
504 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
505 if (mask & WL_EVENT_WRITEABLE)
506 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
507}
508
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500509static void
510wayland_destroy(struct wlsc_compositor *ec)
511{
512 free(ec);
513}
514
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100515struct wlsc_compositor *
516wayland_compositor_create(struct wl_display *display, int width, int height)
517{
518 struct wayland_compositor *c;
519 struct wl_event_loop *loop;
520 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100521
522 c = malloc(sizeof *c);
523 if (c == NULL)
524 return NULL;
525
526 memset(c, 0, sizeof *c);
527
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500528 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100529
530 if (c->parent.display == NULL) {
531 fprintf(stderr, "failed to create display: %m\n");
532 return NULL;
533 }
534
535 wl_list_init(&c->input_list);
536 wl_display_add_global_listener(c->parent.display,
537 display_handle_global, c);
538
539 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
540
541 c->base.wl_display = display;
542 if (wayland_compositor_init_egl(c) < 0)
543 return NULL;
544
545 /* Can't init base class until we have a current egl context */
546 if (wlsc_compositor_init(&c->base, display) < 0)
547 return NULL;
548
549 if (wayland_compositor_create_output(c, width, height) < 0)
550 return NULL;
551
552 if (wayland_input_create(c) < 0)
553 return NULL;
554
555 loop = wl_display_get_event_loop(c->base.wl_display);
556
557 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
558 c->parent.wl_source =
559 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
560 wayland_compositor_handle_event, c);
561 if (c->parent.wl_source == NULL)
562 return NULL;
563
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500564 c->base.destroy = wayland_destroy;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100565 c->base.authenticate = wayland_authenticate;
566 c->base.present = wayland_compositor_present;
567
568 return &c->base;
569}