blob: 40dee5813cbec24ce62b2adf282b2944fb1b8c21 [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,
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,
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500319 int32_t x, int32_t y,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100320 int32_t width, int32_t height)
321{
322 struct wayland_compositor *c = data;
323
Kristian Høgsbergf8fc08f2010-12-01 20:10:10 -0500324 c->parent.screen_allocation.x = x;
325 c->parent.screen_allocation.y = y;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100326 c->parent.screen_allocation.width = width;
327 c->parent.screen_allocation.height = height;
328}
329
330static const struct wl_output_listener output_listener = {
331 display_handle_geometry,
332};
333
334/* parent shell interface */
335static void
336handle_configure(void *data, struct wl_shell *shell,
337 uint32_t time, uint32_t edges,
338 struct wl_surface *surface,
339 int32_t x, int32_t y, int32_t width, int32_t height)
340{
341#if 0
342 struct output *output = wl_surface_get_user_data(surface);
343
344 /* FIXME: add resize? */
345#endif
346}
347
348static const struct wl_shell_listener shell_listener = {
349 handle_configure,
350};
351
352/* parent drm interface */
353static void
354drm_handle_device(void *data, struct wl_drm *drm, const char *device)
355{
356 struct wayland_compositor *c = data;
357
358 c->parent.device_name = strdup(device);
359}
360
361static void drm_handle_authenticated(void *data, struct wl_drm *drm)
362{
363 struct wayland_compositor *c = data;
364
365 c->parent.authenticated = 1;
366}
367
368static const struct wl_drm_listener drm_listener = {
369 drm_handle_device,
370 drm_handle_authenticated
371};
372
373/* parent input interface */
374static void
375input_handle_motion(void *data, struct wl_input_device *input_device,
376 uint32_t time,
377 int32_t x, int32_t y, int32_t sx, int32_t sy)
378{
379 struct wayland_input *input = data;
380 struct wayland_compositor *c = input->compositor;
381
382 notify_motion(c->base.input_device, time, sx, sy);
383}
384
385static void
386input_handle_button(void *data,
387 struct wl_input_device *input_device,
388 uint32_t time, uint32_t button, uint32_t state)
389{
390 struct wayland_input *input = data;
391 struct wayland_compositor *c = input->compositor;
392
393 notify_button(c->base.input_device, time, button, state);
394}
395
396static void
397input_handle_key(void *data, struct wl_input_device *input_device,
398 uint32_t time, uint32_t key, uint32_t state)
399{
400 struct wayland_input *input = data;
401 struct wayland_compositor *c = input->compositor;
402
403 notify_key(c->base.input_device, time, key, state);
404}
405
406static void
407input_handle_pointer_focus(void *data,
408 struct wl_input_device *input_device,
409 uint32_t time, struct wl_surface *surface,
410 int32_t x, int32_t y, int32_t sx, int32_t sy)
411{
412 struct wayland_input *input = data;
413 struct wayland_compositor *c = input->compositor;
414 static struct wl_buffer *pntr_buffer = NULL;
415
416 if (surface) {
417 c->base.focus = 1;
418 /* FIXME: extend protocol to allow hiding the cursor? */
419 if (pntr_buffer == NULL)
420 pntr_buffer = create_invisible_pointer(c);
421 wl_input_device_attach(input_device, time, pntr_buffer, x, y);
422 } else {
423 /* FIXME. hide our own pointer */
424 c->base.focus = 0;
425 }
426}
427
428static void
429input_handle_keyboard_focus(void *data,
430 struct wl_input_device *input_device,
431 uint32_t time,
432 struct wl_surface *surface,
433 struct wl_array *keys)
434{
435 /* FIXME: sth to be done here? */
436}
437
438static const struct wl_input_device_listener input_device_listener = {
439 input_handle_motion,
440 input_handle_button,
441 input_handle_key,
442 input_handle_pointer_focus,
443 input_handle_keyboard_focus,
444};
445
446static void
447display_add_input(struct wayland_compositor *c, uint32_t id)
448{
449 struct wayland_input *input;
450
451 input = malloc(sizeof *input);
452 if (input == NULL)
453 return;
454
455 memset(input, 0, sizeof *input);
456
457 input->compositor = c;
458 input->input_device = wl_input_device_create(c->parent.display, id);
459 wl_list_insert(c->input_list.prev, &input->link);
460
461 wl_input_device_add_listener(input->input_device,
462 &input_device_listener, input);
463 wl_input_device_set_user_data(input->input_device, input);
464}
465
466static void
467display_handle_global(struct wl_display *display, uint32_t id,
468 const char *interface, uint32_t version, void *data)
469{
470 struct wayland_compositor *c = data;
471
472 if (strcmp(interface, "compositor") == 0) {
473 c->parent.compositor = wl_compositor_create(display, id);
474 } else if (strcmp(interface, "output") == 0) {
475 c->parent.output = wl_output_create(display, id);
476 wl_output_add_listener(c->parent.output, &output_listener, c);
477 } else if (strcmp(interface, "input_device") == 0) {
478 display_add_input(c, id);
479 } else if (strcmp(interface, "shell") == 0) {
480 c->parent.shell = wl_shell_create(display, id);
481 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
482 } else if (strcmp(interface, "drm") == 0) {
483 c->parent.drm = wl_drm_create(display, id);
484 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
485 }
486}
487
488static int
489update_event_mask(uint32_t mask, void *data)
490{
491 struct wayland_compositor *c = data;
492
493 c->parent.event_mask = mask;
494 if (c->parent.wl_source)
495 wl_event_source_fd_update(c->parent.wl_source, mask);
496
497 return 0;
498}
499
500static void
501wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
502{
503 struct wayland_compositor *c = data;
504
505 if (mask & WL_EVENT_READABLE)
506 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
507 if (mask & WL_EVENT_WRITEABLE)
508 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
509}
510
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500511static void
512wayland_destroy(struct wlsc_compositor *ec)
513{
514 free(ec);
515}
516
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100517struct wlsc_compositor *
518wayland_compositor_create(struct wl_display *display, int width, int height)
519{
520 struct wayland_compositor *c;
521 struct wl_event_loop *loop;
522 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100523
524 c = malloc(sizeof *c);
525 if (c == NULL)
526 return NULL;
527
528 memset(c, 0, sizeof *c);
529
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -0500530 c->parent.display = wl_display_connect(NULL);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100531
532 if (c->parent.display == NULL) {
533 fprintf(stderr, "failed to create display: %m\n");
534 return NULL;
535 }
536
537 wl_list_init(&c->input_list);
538 wl_display_add_global_listener(c->parent.display,
539 display_handle_global, c);
540
541 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
542
543 c->base.wl_display = display;
544 if (wayland_compositor_init_egl(c) < 0)
545 return NULL;
546
547 /* Can't init base class until we have a current egl context */
548 if (wlsc_compositor_init(&c->base, display) < 0)
549 return NULL;
550
551 if (wayland_compositor_create_output(c, width, height) < 0)
552 return NULL;
553
554 if (wayland_input_create(c) < 0)
555 return NULL;
556
557 loop = wl_display_get_event_loop(c->base.wl_display);
558
559 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
560 c->parent.wl_source =
561 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
562 wayland_compositor_handle_event, c);
563 if (c->parent.wl_source == NULL)
564 return NULL;
565
Kristian Høgsbergcaa64422010-12-01 16:52:15 -0500566 c->base.destroy = wayland_destroy;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100567 c->base.authenticate = wayland_authenticate;
568 c->base.present = wayland_compositor_present;
569
570 return &c->base;
571}