2 * Copyright © 2011 Benjamin Franzke
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
33 #include <linux/input.h>
35 #include <wayland-client.h>
36 #include <wayland-egl.h>
37 #include <wayland-cursor.h>
39 #include <GLES2/gl2.h>
41 #include <EGL/eglext.h>
43 #include "xdg-shell-client-protocol.h"
45 #ifndef EGL_EXT_swap_buffers_with_damage
46 #define EGL_EXT_swap_buffers_with_damage 1
47 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
50 #ifndef EGL_EXT_buffer_age
51 #define EGL_EXT_buffer_age 1
52 #define EGL_BUFFER_AGE_EXT 0x313D
59 struct wl_display *display;
60 struct wl_registry *registry;
61 struct wl_compositor *compositor;
62 struct xdg_shell *shell;
64 struct wl_pointer *pointer;
65 struct wl_touch *touch;
66 struct wl_keyboard *keyboard;
68 struct wl_cursor_theme *cursor_theme;
69 struct wl_cursor *default_cursor;
70 struct wl_surface *cursor_surface;
76 struct window *window;
78 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
86 struct display *display;
87 struct geometry geometry, window_size;
89 GLuint rotation_uniform;
94 uint32_t benchmark_time, frames;
95 struct wl_egl_window *native;
96 struct wl_surface *surface;
97 struct xdg_surface *xdg_surface;
98 EGLSurface egl_surface;
99 struct wl_callback *callback;
100 int fullscreen, opaque, buffer_size, frame_sync;
103 static const char *vert_shader_text =
104 "uniform mat4 rotation;\n"
105 "attribute vec4 pos;\n"
106 "attribute vec4 color;\n"
107 "varying vec4 v_color;\n"
109 " gl_Position = rotation * pos;\n"
110 " v_color = color;\n"
113 static const char *frag_shader_text =
114 "precision mediump float;\n"
115 "varying vec4 v_color;\n"
117 " gl_FragColor = v_color;\n"
120 static int running = 1;
123 init_egl(struct display *display, struct window *window)
125 static const EGLint context_attribs[] = {
126 EGL_CONTEXT_CLIENT_VERSION, 2,
129 const char *extensions;
131 EGLint config_attribs[] = {
132 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
136 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
140 EGLint major, minor, n, count, i, size;
144 if (window->opaque || window->buffer_size == 16)
145 config_attribs[9] = 0;
147 display->egl.dpy = eglGetDisplay(display->display);
148 assert(display->egl.dpy);
150 ret = eglInitialize(display->egl.dpy, &major, &minor);
151 assert(ret == EGL_TRUE);
152 ret = eglBindAPI(EGL_OPENGL_ES_API);
153 assert(ret == EGL_TRUE);
155 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
158 configs = calloc(count, sizeof *configs);
161 ret = eglChooseConfig(display->egl.dpy, config_attribs,
163 assert(ret && n >= 1);
165 for (i = 0; i < n; i++) {
166 eglGetConfigAttrib(display->egl.dpy,
167 configs[i], EGL_BUFFER_SIZE, &size);
168 if (window->buffer_size == size) {
169 display->egl.conf = configs[i];
174 if (display->egl.conf == NULL) {
175 fprintf(stderr, "did not find config with buffer size %d\n",
176 window->buffer_size);
180 display->egl.ctx = eglCreateContext(display->egl.dpy,
182 EGL_NO_CONTEXT, context_attribs);
183 assert(display->egl.ctx);
185 display->swap_buffers_with_damage = NULL;
186 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
188 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
189 strstr(extensions, "EGL_EXT_buffer_age"))
190 display->swap_buffers_with_damage =
191 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
192 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
194 if (display->swap_buffers_with_damage)
195 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
200 fini_egl(struct display *display)
202 eglTerminate(display->egl.dpy);
207 create_shader(struct window *window, const char *source, GLenum shader_type)
212 shader = glCreateShader(shader_type);
215 glShaderSource(shader, 1, (const char **) &source, NULL);
216 glCompileShader(shader);
218 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
222 glGetShaderInfoLog(shader, 1000, &len, log);
223 fprintf(stderr, "Error: compiling %s: %*s\n",
224 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
233 init_gl(struct window *window)
239 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
240 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
242 program = glCreateProgram();
243 glAttachShader(program, frag);
244 glAttachShader(program, vert);
245 glLinkProgram(program);
247 glGetProgramiv(program, GL_LINK_STATUS, &status);
251 glGetProgramInfoLog(program, 1000, &len, log);
252 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
256 glUseProgram(program);
261 glBindAttribLocation(program, window->gl.pos, "pos");
262 glBindAttribLocation(program, window->gl.col, "color");
263 glLinkProgram(program);
265 window->gl.rotation_uniform =
266 glGetUniformLocation(program, "rotation");
270 handle_surface_configure(void *data, struct xdg_surface *surface,
271 int32_t width, int32_t height,
272 struct wl_array *states, uint32_t serial)
274 struct window *window = data;
277 window->fullscreen = 0;
278 wl_array_for_each(p, states) {
281 case XDG_SURFACE_STATE_FULLSCREEN:
282 window->fullscreen = 1;
287 if (width > 0 && height > 0) {
288 if (!window->fullscreen) {
289 window->window_size.width = width;
290 window->window_size.height = height;
292 window->geometry.width = width;
293 window->geometry.height = height;
294 } else if (!window->fullscreen) {
295 window->geometry = window->window_size;
299 wl_egl_window_resize(window->native,
300 window->geometry.width,
301 window->geometry.height, 0, 0);
303 xdg_surface_ack_configure(surface, serial);
307 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
312 static const struct xdg_surface_listener xdg_surface_listener = {
313 handle_surface_configure,
314 handle_surface_delete,
318 create_surface(struct window *window)
320 struct display *display = window->display;
323 window->surface = wl_compositor_create_surface(display->compositor);
324 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
327 xdg_surface_add_listener(window->xdg_surface,
328 &xdg_surface_listener, window);
331 wl_egl_window_create(window->surface,
332 window->geometry.width,
333 window->geometry.height);
334 window->egl_surface =
335 eglCreateWindowSurface(display->egl.dpy,
337 window->native, NULL);
339 xdg_surface_set_title(window->xdg_surface, "simple-egl");
341 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
342 window->egl_surface, window->display->egl.ctx);
343 assert(ret == EGL_TRUE);
345 if (!window->frame_sync)
346 eglSwapInterval(display->egl.dpy, 0);
348 if (window->fullscreen)
349 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
353 destroy_surface(struct window *window)
355 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
356 * on eglReleaseThread(). */
357 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
360 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
361 wl_egl_window_destroy(window->native);
363 xdg_surface_destroy(window->xdg_surface);
364 wl_surface_destroy(window->surface);
366 if (window->callback)
367 wl_callback_destroy(window->callback);
371 redraw(void *data, struct wl_callback *callback, uint32_t time)
373 struct window *window = data;
374 struct display *display = window->display;
375 static const GLfloat verts[3][2] = {
380 static const GLfloat colors[3][3] = {
386 GLfloat rotation[4][4] = {
392 static const uint32_t speed_div = 5, benchmark_interval = 5;
393 struct wl_region *region;
395 EGLint buffer_age = 0;
398 assert(window->callback == callback);
399 window->callback = NULL;
402 wl_callback_destroy(callback);
404 gettimeofday(&tv, NULL);
405 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
406 if (window->frames == 0)
407 window->benchmark_time = time;
408 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
409 printf("%d frames in %d seconds: %f fps\n",
412 (float) window->frames / benchmark_interval);
413 window->benchmark_time = time;
417 angle = (time / speed_div) % 360 * M_PI / 180.0;
418 rotation[0][0] = cos(angle);
419 rotation[0][2] = sin(angle);
420 rotation[2][0] = -sin(angle);
421 rotation[2][2] = cos(angle);
423 if (display->swap_buffers_with_damage)
424 eglQuerySurface(display->egl.dpy, window->egl_surface,
425 EGL_BUFFER_AGE_EXT, &buffer_age);
427 glViewport(0, 0, window->geometry.width, window->geometry.height);
429 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
430 (GLfloat *) rotation);
432 glClearColor(0.0, 0.0, 0.0, 0.5);
433 glClear(GL_COLOR_BUFFER_BIT);
435 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
436 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
437 glEnableVertexAttribArray(window->gl.pos);
438 glEnableVertexAttribArray(window->gl.col);
440 glDrawArrays(GL_TRIANGLES, 0, 3);
442 glDisableVertexAttribArray(window->gl.pos);
443 glDisableVertexAttribArray(window->gl.col);
445 if (window->opaque || window->fullscreen) {
446 region = wl_compositor_create_region(window->display->compositor);
447 wl_region_add(region, 0, 0,
448 window->geometry.width,
449 window->geometry.height);
450 wl_surface_set_opaque_region(window->surface, region);
451 wl_region_destroy(region);
453 wl_surface_set_opaque_region(window->surface, NULL);
456 if (display->swap_buffers_with_damage && buffer_age > 0) {
457 rect[0] = window->geometry.width / 4 - 1;
458 rect[1] = window->geometry.height / 4 - 1;
459 rect[2] = window->geometry.width / 2 + 2;
460 rect[3] = window->geometry.height / 2 + 2;
461 display->swap_buffers_with_damage(display->egl.dpy,
465 eglSwapBuffers(display->egl.dpy, window->egl_surface);
471 pointer_handle_enter(void *data, struct wl_pointer *pointer,
472 uint32_t serial, struct wl_surface *surface,
473 wl_fixed_t sx, wl_fixed_t sy)
475 struct display *display = data;
476 struct wl_buffer *buffer;
477 struct wl_cursor *cursor = display->default_cursor;
478 struct wl_cursor_image *image;
480 if (display->window->fullscreen)
481 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
483 image = display->default_cursor->images[0];
484 buffer = wl_cursor_image_get_buffer(image);
487 wl_pointer_set_cursor(pointer, serial,
488 display->cursor_surface,
491 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
492 wl_surface_damage(display->cursor_surface, 0, 0,
493 image->width, image->height);
494 wl_surface_commit(display->cursor_surface);
499 pointer_handle_leave(void *data, struct wl_pointer *pointer,
500 uint32_t serial, struct wl_surface *surface)
505 pointer_handle_motion(void *data, struct wl_pointer *pointer,
506 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
511 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
512 uint32_t serial, uint32_t time, uint32_t button,
515 struct display *display = data;
517 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
518 xdg_surface_move(display->window->xdg_surface,
519 display->seat, serial);
523 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
524 uint32_t time, uint32_t axis, wl_fixed_t value)
528 static const struct wl_pointer_listener pointer_listener = {
529 pointer_handle_enter,
530 pointer_handle_leave,
531 pointer_handle_motion,
532 pointer_handle_button,
537 touch_handle_down(void *data, struct wl_touch *wl_touch,
538 uint32_t serial, uint32_t time, struct wl_surface *surface,
539 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
541 struct display *d = (struct display *)data;
543 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
547 touch_handle_up(void *data, struct wl_touch *wl_touch,
548 uint32_t serial, uint32_t time, int32_t id)
553 touch_handle_motion(void *data, struct wl_touch *wl_touch,
554 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
559 touch_handle_frame(void *data, struct wl_touch *wl_touch)
564 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
568 static const struct wl_touch_listener touch_listener = {
577 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
578 uint32_t format, int fd, uint32_t size)
583 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
584 uint32_t serial, struct wl_surface *surface,
585 struct wl_array *keys)
590 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
591 uint32_t serial, struct wl_surface *surface)
596 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
597 uint32_t serial, uint32_t time, uint32_t key,
600 struct display *d = data;
602 if (key == KEY_F11 && state) {
603 if (d->window->fullscreen)
604 xdg_surface_unset_fullscreen(d->window->xdg_surface);
606 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
607 } else if (key == KEY_ESC && state)
612 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
613 uint32_t serial, uint32_t mods_depressed,
614 uint32_t mods_latched, uint32_t mods_locked,
619 static const struct wl_keyboard_listener keyboard_listener = {
620 keyboard_handle_keymap,
621 keyboard_handle_enter,
622 keyboard_handle_leave,
624 keyboard_handle_modifiers,
628 seat_handle_capabilities(void *data, struct wl_seat *seat,
629 enum wl_seat_capability caps)
631 struct display *d = data;
633 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
634 d->pointer = wl_seat_get_pointer(seat);
635 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
636 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
637 wl_pointer_destroy(d->pointer);
641 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
642 d->keyboard = wl_seat_get_keyboard(seat);
643 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
644 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
645 wl_keyboard_destroy(d->keyboard);
649 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
650 d->touch = wl_seat_get_touch(seat);
651 wl_touch_set_user_data(d->touch, d);
652 wl_touch_add_listener(d->touch, &touch_listener, d);
653 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
654 wl_touch_destroy(d->touch);
659 static const struct wl_seat_listener seat_listener = {
660 seat_handle_capabilities,
664 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
666 xdg_shell_pong(shell, serial);
669 static const struct xdg_shell_listener xdg_shell_listener = {
673 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
675 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
676 "Interface version doesn't match implementation version");
680 registry_handle_global(void *data, struct wl_registry *registry,
681 uint32_t name, const char *interface, uint32_t version)
683 struct display *d = data;
685 if (strcmp(interface, "wl_compositor") == 0) {
687 wl_registry_bind(registry, name,
688 &wl_compositor_interface, 1);
689 } else if (strcmp(interface, "xdg_shell") == 0) {
690 d->shell = wl_registry_bind(registry, name,
691 &xdg_shell_interface, 1);
692 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
693 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
694 } else if (strcmp(interface, "wl_seat") == 0) {
695 d->seat = wl_registry_bind(registry, name,
696 &wl_seat_interface, 1);
697 wl_seat_add_listener(d->seat, &seat_listener, d);
698 } else if (strcmp(interface, "wl_shm") == 0) {
699 d->shm = wl_registry_bind(registry, name,
700 &wl_shm_interface, 1);
701 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
702 if (!d->cursor_theme) {
703 fprintf(stderr, "unable to load default theme\n");
707 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
708 if (!d->default_cursor) {
709 fprintf(stderr, "unable to load default left pointer\n");
716 registry_handle_global_remove(void *data, struct wl_registry *registry,
721 static const struct wl_registry_listener registry_listener = {
722 registry_handle_global,
723 registry_handle_global_remove
727 signal_int(int signum)
733 usage(int error_code)
735 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
736 " -f\tRun in fullscreen mode\n"
737 " -o\tCreate an opaque surface\n"
738 " -s\tUse a 16 bpp EGL config\n"
739 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
740 " -h\tThis help text\n\n");
746 main(int argc, char **argv)
748 struct sigaction sigint;
749 struct display display = { 0 };
750 struct window window = { 0 };
753 window.display = &display;
754 display.window = &window;
755 window.geometry.width = 250;
756 window.geometry.height = 250;
757 window.window_size = window.geometry;
758 window.buffer_size = 32;
759 window.frame_sync = 1;
761 for (i = 1; i < argc; i++) {
762 if (strcmp("-f", argv[i]) == 0)
763 window.fullscreen = 1;
764 else if (strcmp("-o", argv[i]) == 0)
766 else if (strcmp("-s", argv[i]) == 0)
767 window.buffer_size = 16;
768 else if (strcmp("-b", argv[i]) == 0)
769 window.frame_sync = 0;
770 else if (strcmp("-h", argv[i]) == 0)
776 display.display = wl_display_connect(NULL);
777 assert(display.display);
779 display.registry = wl_display_get_registry(display.display);
780 wl_registry_add_listener(display.registry,
781 ®istry_listener, &display);
783 wl_display_dispatch(display.display);
785 init_egl(&display, &window);
786 create_surface(&window);
789 display.cursor_surface =
790 wl_compositor_create_surface(display.compositor);
792 sigint.sa_handler = signal_int;
793 sigemptyset(&sigint.sa_mask);
794 sigint.sa_flags = SA_RESETHAND;
795 sigaction(SIGINT, &sigint, NULL);
797 /* The mainloop here is a little subtle. Redrawing will cause
798 * EGL to read events so we can just call
799 * wl_display_dispatch_pending() to handle any events that got
800 * queued up as a side effect. */
801 while (running && ret != -1) {
802 wl_display_dispatch_pending(display.display);
803 redraw(&window, NULL, 0);
806 fprintf(stderr, "simple-egl exiting\n");
808 destroy_surface(&window);
811 wl_surface_destroy(display.cursor_surface);
812 if (display.cursor_theme)
813 wl_cursor_theme_destroy(display.cursor_theme);
816 xdg_shell_destroy(display.shell);
818 if (display.compositor)
819 wl_compositor_destroy(display.compositor);
821 wl_registry_destroy(display.registry);
822 wl_display_flush(display.display);
823 wl_display_disconnect(display.display);