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