blob: e289a292cb69823499d3506436071fd315b4c2ad [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
98 c->base.input_device = input;
99
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,
196 output->parent.buffer[output->current ^ 1]);
197 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,
267 output->parent.buffer[output->current]);
268 wl_surface_map(output->parent.surface,
269 output->base.x, output->base.y,
270 output->base.width, output->base.height);
271
272 glClearColor(0, 0, 0, 0.5);
273
274 wl_list_insert(c->base.output_list.prev, &output->base.link);
275
276 return 0;
277}
278
279static struct wl_buffer *
280create_invisible_pointer(struct wayland_compositor *c)
281{
282 struct wlsc_drm_buffer *wlsc_buffer;
283 struct wl_buffer *buffer;
284 int name, stride;
285 struct wl_visual *visual;
286 GLuint texture;
287 const int width = 1, height = 1;
288 const GLubyte data[] = { 0, 0, 0, 0 };
289
290 glGenTextures(1, &texture);
291 glBindTexture(GL_TEXTURE_2D, texture);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
293 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
294 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
295 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
296
297 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
298 wlsc_buffer = wlsc_drm_buffer_create(&c->base, width, height, visual);
299
300 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, wlsc_buffer->image);
301 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
302 GL_RGBA, GL_UNSIGNED_BYTE, data);
303
304 eglExportDRMImageMESA(c->base.display, wlsc_buffer->image,
305 &name, NULL, &stride);
306
307 buffer = wl_drm_create_buffer(c->parent.drm, name,
308 width, height,
309 stride, visual);
310 return buffer;
311}
312
313/* Events received from the wayland-server this compositor is client of: */
314
315/* parent output interface */
316static void
317display_handle_geometry(void *data,
318 struct wl_output *output,
319 int32_t width, int32_t height)
320{
321 struct wayland_compositor *c = data;
322
323 c->parent.screen_allocation.x = 0;
324 c->parent.screen_allocation.y = 0;
325 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,
337 struct wl_surface *surface,
338 int32_t x, int32_t y, int32_t width, int32_t height)
339{
340#if 0
341 struct output *output = wl_surface_get_user_data(surface);
342
343 /* FIXME: add resize? */
344#endif
345}
346
347static const struct wl_shell_listener shell_listener = {
348 handle_configure,
349};
350
351/* parent drm interface */
352static void
353drm_handle_device(void *data, struct wl_drm *drm, const char *device)
354{
355 struct wayland_compositor *c = data;
356
357 c->parent.device_name = strdup(device);
358}
359
360static void drm_handle_authenticated(void *data, struct wl_drm *drm)
361{
362 struct wayland_compositor *c = data;
363
364 c->parent.authenticated = 1;
365}
366
367static const struct wl_drm_listener drm_listener = {
368 drm_handle_device,
369 drm_handle_authenticated
370};
371
372/* parent input interface */
373static void
374input_handle_motion(void *data, struct wl_input_device *input_device,
375 uint32_t time,
376 int32_t x, int32_t y, int32_t sx, int32_t sy)
377{
378 struct wayland_input *input = data;
379 struct wayland_compositor *c = input->compositor;
380
381 notify_motion(c->base.input_device, time, sx, sy);
382}
383
384static void
385input_handle_button(void *data,
386 struct wl_input_device *input_device,
387 uint32_t time, uint32_t button, uint32_t state)
388{
389 struct wayland_input *input = data;
390 struct wayland_compositor *c = input->compositor;
391
392 notify_button(c->base.input_device, time, button, state);
393}
394
395static void
396input_handle_key(void *data, struct wl_input_device *input_device,
397 uint32_t time, uint32_t key, uint32_t state)
398{
399 struct wayland_input *input = data;
400 struct wayland_compositor *c = input->compositor;
401
402 notify_key(c->base.input_device, time, key, state);
403}
404
405static void
406input_handle_pointer_focus(void *data,
407 struct wl_input_device *input_device,
408 uint32_t time, struct wl_surface *surface,
409 int32_t x, int32_t y, int32_t sx, int32_t sy)
410{
411 struct wayland_input *input = data;
412 struct wayland_compositor *c = input->compositor;
413 static struct wl_buffer *pntr_buffer = NULL;
414
415 if (surface) {
416 c->base.focus = 1;
417 /* FIXME: extend protocol to allow hiding the cursor? */
418 if (pntr_buffer == NULL)
419 pntr_buffer = create_invisible_pointer(c);
420 wl_input_device_attach(input_device, time, pntr_buffer, x, y);
421 } else {
422 /* FIXME. hide our own pointer */
423 c->base.focus = 0;
424 }
425}
426
427static void
428input_handle_keyboard_focus(void *data,
429 struct wl_input_device *input_device,
430 uint32_t time,
431 struct wl_surface *surface,
432 struct wl_array *keys)
433{
434 /* FIXME: sth to be done here? */
435}
436
437static const struct wl_input_device_listener input_device_listener = {
438 input_handle_motion,
439 input_handle_button,
440 input_handle_key,
441 input_handle_pointer_focus,
442 input_handle_keyboard_focus,
443};
444
445static void
446display_add_input(struct wayland_compositor *c, uint32_t id)
447{
448 struct wayland_input *input;
449
450 input = malloc(sizeof *input);
451 if (input == NULL)
452 return;
453
454 memset(input, 0, sizeof *input);
455
456 input->compositor = c;
457 input->input_device = wl_input_device_create(c->parent.display, id);
458 wl_list_insert(c->input_list.prev, &input->link);
459
460 wl_input_device_add_listener(input->input_device,
461 &input_device_listener, input);
462 wl_input_device_set_user_data(input->input_device, input);
463}
464
465static void
466display_handle_global(struct wl_display *display, uint32_t id,
467 const char *interface, uint32_t version, void *data)
468{
469 struct wayland_compositor *c = data;
470
471 if (strcmp(interface, "compositor") == 0) {
472 c->parent.compositor = wl_compositor_create(display, id);
473 } else if (strcmp(interface, "output") == 0) {
474 c->parent.output = wl_output_create(display, id);
475 wl_output_add_listener(c->parent.output, &output_listener, c);
476 } else if (strcmp(interface, "input_device") == 0) {
477 display_add_input(c, id);
478 } else if (strcmp(interface, "shell") == 0) {
479 c->parent.shell = wl_shell_create(display, id);
480 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
481 } else if (strcmp(interface, "drm") == 0) {
482 c->parent.drm = wl_drm_create(display, id);
483 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
484 }
485}
486
487static int
488update_event_mask(uint32_t mask, void *data)
489{
490 struct wayland_compositor *c = data;
491
492 c->parent.event_mask = mask;
493 if (c->parent.wl_source)
494 wl_event_source_fd_update(c->parent.wl_source, mask);
495
496 return 0;
497}
498
499static void
500wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
501{
502 struct wayland_compositor *c = data;
503
504 if (mask & WL_EVENT_READABLE)
505 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
506 if (mask & WL_EVENT_WRITEABLE)
507 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
508}
509
510struct wlsc_compositor *
511wayland_compositor_create(struct wl_display *display, int width, int height)
512{
513 struct wayland_compositor *c;
514 struct wl_event_loop *loop;
515 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100516
517 c = malloc(sizeof *c);
518 if (c == NULL)
519 return NULL;
520
521 memset(c, 0, sizeof *c);
522
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500523 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100524
525 if (c->parent.display == NULL) {
526 fprintf(stderr, "failed to create display: %m\n");
527 return NULL;
528 }
529
530 wl_list_init(&c->input_list);
531 wl_display_add_global_listener(c->parent.display,
532 display_handle_global, c);
533
534 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
535
536 c->base.wl_display = display;
537 if (wayland_compositor_init_egl(c) < 0)
538 return NULL;
539
540 /* Can't init base class until we have a current egl context */
541 if (wlsc_compositor_init(&c->base, display) < 0)
542 return NULL;
543
544 if (wayland_compositor_create_output(c, width, height) < 0)
545 return NULL;
546
547 if (wayland_input_create(c) < 0)
548 return NULL;
549
550 loop = wl_display_get_event_loop(c->base.wl_display);
551
552 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
553 c->parent.wl_source =
554 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
555 wayland_compositor_handle_event, c);
556 if (c->parent.wl_source == NULL)
557 return NULL;
558
559 c->base.authenticate = wayland_authenticate;
560 c->base.present = wayland_compositor_present;
561
562 return &c->base;
563}