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