blob: 85010b6ae7c813ea69608580ed9bc3a533a01001 [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
180 glFinish();
181 wl_list_for_each(output, &base->output_list, base.link) {
182 output->current ^= 1;
183
184 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
185 GL_COLOR_ATTACHMENT0,
186 GL_RENDERBUFFER,
187 output->rbo[output->current]);
188
189 wl_surface_attach(output->parent.surface,
190 output->parent.buffer[output->current ^ 1]);
191 wl_surface_damage(output->parent.surface, 0, 0,
192 output->base.width, output->base.height);
193 }
194
195
196 gettimeofday(&tv, NULL);
197 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
198 wlsc_compositor_finish_frame(&c->base, msec);
199}
200
201static int
202wayland_authenticate(struct wlsc_compositor *ec, uint32_t id)
203{
204 struct wayland_compositor *c = (struct wayland_compositor *) ec;
205
206 wl_drm_authenticate(c->parent.drm, id);
207 /* FIXME: recv drm_authenticated event from parent? */
208 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
209
210 return 0;
211}
212
213static int
214wayland_compositor_create_output(struct wayland_compositor *c,
215 int width, int height)
216{
217 struct wayland_output *output;
218 struct wl_visual *visual;
219 int i;
220 EGLint name, stride, attribs[] = {
221 EGL_WIDTH, 0,
222 EGL_HEIGHT, 0,
223 EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
224 EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
225 EGL_NONE
226 };
227
228 output = malloc(sizeof *output);
229 if (output == NULL)
230 return -1;
231 memset(output, 0, sizeof *output);
232
233 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
234 output->parent.surface =
235 wl_compositor_create_surface(c->parent.compositor);
236 wl_surface_set_user_data(output->parent.surface, output);
237
238 glGenRenderbuffers(2, output->rbo);
239 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
240 for (i = 0; i < 2; i++) {
241 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo[i]);
242
243 attribs[1] = width;
244 attribs[3] = height;
245 output->image[i] =
246 eglCreateDRMImageMESA(c->base.display, attribs);
247 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
248 output->image[i]);
249 eglExportDRMImageMESA(c->base.display, output->image[i],
250 &name, NULL, &stride);
251 output->parent.buffer[i] =
252 wl_drm_create_buffer(c->parent.drm, name,
253 width, height,
254 stride, visual);
255 }
256
257 output->current = 0;
258 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
259 GL_COLOR_ATTACHMENT0,
260 GL_RENDERBUFFER,
261 output->rbo[output->current]);
262
263 wl_surface_attach(output->parent.surface,
264 output->parent.buffer[output->current]);
265 wl_surface_map(output->parent.surface,
266 output->base.x, output->base.y,
267 output->base.width, output->base.height);
268
269 glClearColor(0, 0, 0, 0.5);
270
271 wl_list_insert(c->base.output_list.prev, &output->base.link);
272
273 return 0;
274}
275
276static struct wl_buffer *
277create_invisible_pointer(struct wayland_compositor *c)
278{
279 struct wlsc_drm_buffer *wlsc_buffer;
280 struct wl_buffer *buffer;
281 int name, stride;
282 struct wl_visual *visual;
283 GLuint texture;
284 const int width = 1, height = 1;
285 const GLubyte data[] = { 0, 0, 0, 0 };
286
287 glGenTextures(1, &texture);
288 glBindTexture(GL_TEXTURE_2D, texture);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
292 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
293
294 visual = wl_display_get_premultiplied_argb_visual(c->parent.display);
295 wlsc_buffer = wlsc_drm_buffer_create(&c->base, width, height, visual);
296
297 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, wlsc_buffer->image);
298 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height,
299 GL_RGBA, GL_UNSIGNED_BYTE, data);
300
301 eglExportDRMImageMESA(c->base.display, wlsc_buffer->image,
302 &name, NULL, &stride);
303
304 buffer = wl_drm_create_buffer(c->parent.drm, name,
305 width, height,
306 stride, visual);
307 return buffer;
308}
309
310/* Events received from the wayland-server this compositor is client of: */
311
312/* parent output interface */
313static void
314display_handle_geometry(void *data,
315 struct wl_output *output,
316 int32_t width, int32_t height)
317{
318 struct wayland_compositor *c = data;
319
320 c->parent.screen_allocation.x = 0;
321 c->parent.screen_allocation.y = 0;
322 c->parent.screen_allocation.width = width;
323 c->parent.screen_allocation.height = height;
324}
325
326static const struct wl_output_listener output_listener = {
327 display_handle_geometry,
328};
329
330/* parent shell interface */
331static void
332handle_configure(void *data, struct wl_shell *shell,
333 uint32_t time, uint32_t edges,
334 struct wl_surface *surface,
335 int32_t x, int32_t y, int32_t width, int32_t height)
336{
337#if 0
338 struct output *output = wl_surface_get_user_data(surface);
339
340 /* FIXME: add resize? */
341#endif
342}
343
344static const struct wl_shell_listener shell_listener = {
345 handle_configure,
346};
347
348/* parent drm interface */
349static void
350drm_handle_device(void *data, struct wl_drm *drm, const char *device)
351{
352 struct wayland_compositor *c = data;
353
354 c->parent.device_name = strdup(device);
355}
356
357static void drm_handle_authenticated(void *data, struct wl_drm *drm)
358{
359 struct wayland_compositor *c = data;
360
361 c->parent.authenticated = 1;
362}
363
364static const struct wl_drm_listener drm_listener = {
365 drm_handle_device,
366 drm_handle_authenticated
367};
368
369/* parent input interface */
370static void
371input_handle_motion(void *data, struct wl_input_device *input_device,
372 uint32_t time,
373 int32_t x, int32_t y, int32_t sx, int32_t sy)
374{
375 struct wayland_input *input = data;
376 struct wayland_compositor *c = input->compositor;
377
378 notify_motion(c->base.input_device, time, sx, sy);
379}
380
381static void
382input_handle_button(void *data,
383 struct wl_input_device *input_device,
384 uint32_t time, uint32_t button, uint32_t state)
385{
386 struct wayland_input *input = data;
387 struct wayland_compositor *c = input->compositor;
388
389 notify_button(c->base.input_device, time, button, state);
390}
391
392static void
393input_handle_key(void *data, struct wl_input_device *input_device,
394 uint32_t time, uint32_t key, uint32_t state)
395{
396 struct wayland_input *input = data;
397 struct wayland_compositor *c = input->compositor;
398
399 notify_key(c->base.input_device, time, key, state);
400}
401
402static void
403input_handle_pointer_focus(void *data,
404 struct wl_input_device *input_device,
405 uint32_t time, struct wl_surface *surface,
406 int32_t x, int32_t y, int32_t sx, int32_t sy)
407{
408 struct wayland_input *input = data;
409 struct wayland_compositor *c = input->compositor;
410 static struct wl_buffer *pntr_buffer = NULL;
411
412 if (surface) {
413 c->base.focus = 1;
414 /* FIXME: extend protocol to allow hiding the cursor? */
415 if (pntr_buffer == NULL)
416 pntr_buffer = create_invisible_pointer(c);
417 wl_input_device_attach(input_device, time, pntr_buffer, x, y);
418 } else {
419 /* FIXME. hide our own pointer */
420 c->base.focus = 0;
421 }
422}
423
424static void
425input_handle_keyboard_focus(void *data,
426 struct wl_input_device *input_device,
427 uint32_t time,
428 struct wl_surface *surface,
429 struct wl_array *keys)
430{
431 /* FIXME: sth to be done here? */
432}
433
434static const struct wl_input_device_listener input_device_listener = {
435 input_handle_motion,
436 input_handle_button,
437 input_handle_key,
438 input_handle_pointer_focus,
439 input_handle_keyboard_focus,
440};
441
442static void
443display_add_input(struct wayland_compositor *c, uint32_t id)
444{
445 struct wayland_input *input;
446
447 input = malloc(sizeof *input);
448 if (input == NULL)
449 return;
450
451 memset(input, 0, sizeof *input);
452
453 input->compositor = c;
454 input->input_device = wl_input_device_create(c->parent.display, id);
455 wl_list_insert(c->input_list.prev, &input->link);
456
457 wl_input_device_add_listener(input->input_device,
458 &input_device_listener, input);
459 wl_input_device_set_user_data(input->input_device, input);
460}
461
462static void
463display_handle_global(struct wl_display *display, uint32_t id,
464 const char *interface, uint32_t version, void *data)
465{
466 struct wayland_compositor *c = data;
467
468 if (strcmp(interface, "compositor") == 0) {
469 c->parent.compositor = wl_compositor_create(display, id);
470 } else if (strcmp(interface, "output") == 0) {
471 c->parent.output = wl_output_create(display, id);
472 wl_output_add_listener(c->parent.output, &output_listener, c);
473 } else if (strcmp(interface, "input_device") == 0) {
474 display_add_input(c, id);
475 } else if (strcmp(interface, "shell") == 0) {
476 c->parent.shell = wl_shell_create(display, id);
477 wl_shell_add_listener(c->parent.shell, &shell_listener, c);
478 } else if (strcmp(interface, "drm") == 0) {
479 c->parent.drm = wl_drm_create(display, id);
480 wl_drm_add_listener(c->parent.drm, &drm_listener, c);
481 }
482}
483
484static int
485update_event_mask(uint32_t mask, void *data)
486{
487 struct wayland_compositor *c = data;
488
489 c->parent.event_mask = mask;
490 if (c->parent.wl_source)
491 wl_event_source_fd_update(c->parent.wl_source, mask);
492
493 return 0;
494}
495
496static void
497wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
498{
499 struct wayland_compositor *c = data;
500
501 if (mask & WL_EVENT_READABLE)
502 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
503 if (mask & WL_EVENT_WRITEABLE)
504 wl_display_iterate(c->parent.display, WL_DISPLAY_WRITABLE);
505}
506
507struct wlsc_compositor *
508wayland_compositor_create(struct wl_display *display, int width, int height)
509{
510 struct wayland_compositor *c;
511 struct wl_event_loop *loop;
512 int fd;
513 char *socket_name;
514 int socket_name_size;
515
516 c = malloc(sizeof *c);
517 if (c == NULL)
518 return NULL;
519
520 memset(c, 0, sizeof *c);
521
522 socket_name_size = 1 + asprintf(&socket_name, "%c%s", '\0',
523 getenv("WAYLAND_DISPLAY"));
524
525 c->parent.display = wl_display_connect(socket_name, socket_name_size);
526 free(socket_name);
527
528 if (c->parent.display == NULL) {
529 fprintf(stderr, "failed to create display: %m\n");
530 return NULL;
531 }
532
533 wl_list_init(&c->input_list);
534 wl_display_add_global_listener(c->parent.display,
535 display_handle_global, c);
536
537 wl_display_iterate(c->parent.display, WL_DISPLAY_READABLE);
538
539 c->base.wl_display = display;
540 if (wayland_compositor_init_egl(c) < 0)
541 return NULL;
542
543 /* Can't init base class until we have a current egl context */
544 if (wlsc_compositor_init(&c->base, display) < 0)
545 return NULL;
546
547 if (wayland_compositor_create_output(c, width, height) < 0)
548 return NULL;
549
550 if (wayland_input_create(c) < 0)
551 return NULL;
552
553 loop = wl_display_get_event_loop(c->base.wl_display);
554
555 fd = wl_display_get_fd(c->parent.display, update_event_mask, c);
556 c->parent.wl_source =
557 wl_event_loop_add_fd(loop, fd, c->parent.event_mask,
558 wayland_compositor_handle_event, c);
559 if (c->parent.wl_source == NULL)
560 return NULL;
561
562 c->base.authenticate = wayland_authenticate;
563 c->base.present = wayland_compositor_present;
564
565 return &c->base;
566}