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
31 #include <linux/input.h>
33 #include <wayland-client.h>
34 #include <wayland-egl.h>
35 #include <wayland-cursor.h>
37 #include <GLES2/gl2.h>
39 #include <EGL/eglext.h>
41 #include "xdg-shell-client-protocol.h"
43 #ifndef EGL_EXT_swap_buffers_with_damage
44 #define EGL_EXT_swap_buffers_with_damage 1
45 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
48 #ifndef EGL_EXT_buffer_age
49 #define EGL_EXT_buffer_age 1
50 #define EGL_BUFFER_AGE_EXT 0x313D
57 struct wl_display *display;
58 struct wl_registry *registry;
59 struct wl_compositor *compositor;
60 struct xdg_shell *shell;
62 struct wl_pointer *pointer;
63 struct wl_touch *touch;
64 struct wl_keyboard *keyboard;
66 struct wl_cursor_theme *cursor_theme;
67 struct wl_cursor *default_cursor;
68 struct wl_surface *cursor_surface;
74 struct window *window;
76 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
84 struct display *display;
85 struct geometry geometry, window_size;
87 GLuint rotation_uniform;
92 uint32_t benchmark_time, frames;
93 struct wl_egl_window *native;
94 struct wl_surface *surface;
95 struct xdg_surface *xdg_surface;
96 EGLSurface egl_surface;
97 struct wl_callback *callback;
98 int fullscreen, configured, opaque, buffer_size, frame_sync;
101 static const char *vert_shader_text =
102 "uniform mat4 rotation;\n"
103 "attribute vec4 pos;\n"
104 "attribute vec4 color;\n"
105 "varying vec4 v_color;\n"
107 " gl_Position = rotation * pos;\n"
108 " v_color = color;\n"
111 static const char *frag_shader_text =
112 "precision mediump float;\n"
113 "varying vec4 v_color;\n"
115 " gl_FragColor = v_color;\n"
118 static int running = 1;
121 init_egl(struct display *display, struct window *window)
123 static const EGLint context_attribs[] = {
124 EGL_CONTEXT_CLIENT_VERSION, 2,
127 const char *extensions;
129 EGLint config_attribs[] = {
130 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
134 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
138 EGLint major, minor, n, count, i, size;
142 if (window->opaque || window->buffer_size == 16)
143 config_attribs[9] = 0;
145 display->egl.dpy = eglGetDisplay(display->display);
146 assert(display->egl.dpy);
148 ret = eglInitialize(display->egl.dpy, &major, &minor);
149 assert(ret == EGL_TRUE);
150 ret = eglBindAPI(EGL_OPENGL_ES_API);
151 assert(ret == EGL_TRUE);
153 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
156 configs = calloc(count, sizeof *configs);
159 ret = eglChooseConfig(display->egl.dpy, config_attribs,
161 assert(ret && n >= 1);
163 for (i = 0; i < n; i++) {
164 eglGetConfigAttrib(display->egl.dpy,
165 configs[i], EGL_BUFFER_SIZE, &size);
166 if (window->buffer_size == size) {
167 display->egl.conf = configs[i];
172 if (display->egl.conf == NULL) {
173 fprintf(stderr, "did not find config with buffer size %d\n",
174 window->buffer_size);
178 display->egl.ctx = eglCreateContext(display->egl.dpy,
180 EGL_NO_CONTEXT, context_attribs);
181 assert(display->egl.ctx);
183 display->swap_buffers_with_damage = NULL;
184 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
186 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
187 strstr(extensions, "EGL_EXT_buffer_age"))
188 display->swap_buffers_with_damage =
189 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
190 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
192 if (display->swap_buffers_with_damage)
193 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
198 fini_egl(struct display *display)
200 eglTerminate(display->egl.dpy);
205 create_shader(struct window *window, const char *source, GLenum shader_type)
210 shader = glCreateShader(shader_type);
213 glShaderSource(shader, 1, (const char **) &source, NULL);
214 glCompileShader(shader);
216 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
220 glGetShaderInfoLog(shader, 1000, &len, log);
221 fprintf(stderr, "Error: compiling %s: %*s\n",
222 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
231 init_gl(struct window *window)
237 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
238 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
240 program = glCreateProgram();
241 glAttachShader(program, frag);
242 glAttachShader(program, vert);
243 glLinkProgram(program);
245 glGetProgramiv(program, GL_LINK_STATUS, &status);
249 glGetProgramInfoLog(program, 1000, &len, log);
250 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
254 glUseProgram(program);
259 glBindAttribLocation(program, window->gl.pos, "pos");
260 glBindAttribLocation(program, window->gl.col, "color");
261 glLinkProgram(program);
263 window->gl.rotation_uniform =
264 glGetUniformLocation(program, "rotation");
268 handle_surface_configure(void *data, struct xdg_surface *surface,
269 int32_t width, int32_t height)
271 struct window *window = data;
274 wl_egl_window_resize(window->native, width, height, 0, 0);
276 window->geometry.width = width;
277 window->geometry.height = height;
279 if (!window->fullscreen)
280 window->window_size = window->geometry;
284 handle_surface_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
289 handle_surface_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
294 handle_surface_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
299 handle_surface_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
304 handle_surface_focused_set(void *data, struct xdg_surface *xdg_surface)
309 handle_surface_focused_unset(void *data, struct xdg_surface *xdg_surface)
313 static const struct xdg_surface_listener xdg_surface_listener = {
314 handle_surface_configure,
315 handle_surface_request_set_maximized,
316 handle_surface_request_unset_maximized,
317 handle_surface_request_set_fullscreen,
318 handle_surface_request_unset_fullscreen,
319 handle_surface_focused_set,
320 handle_surface_focused_unset,
324 configure_callback(void *data, struct wl_callback *callback, uint32_t time)
326 struct window *window = data;
328 wl_callback_destroy(callback);
330 window->configured = 1;
333 static struct wl_callback_listener configure_callback_listener = {
338 set_fullscreen(struct window *window, int fullscreen)
340 struct wl_callback *callback;
342 window->fullscreen = fullscreen;
343 window->configured = 0;
346 xdg_surface_set_fullscreen(window->xdg_surface);
347 callback = wl_display_sync(window->display->display);
348 wl_callback_add_listener(callback,
349 &configure_callback_listener,
353 xdg_surface_unset_fullscreen(window->xdg_surface);
354 handle_surface_configure(window, window->xdg_surface,
355 window->window_size.width,
356 window->window_size.height);
357 window->configured = 1;
362 create_surface(struct window *window)
364 struct display *display = window->display;
367 window->surface = wl_compositor_create_surface(display->compositor);
368 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
371 xdg_surface_add_listener(window->xdg_surface,
372 &xdg_surface_listener, window);
375 wl_egl_window_create(window->surface,
376 window->window_size.width,
377 window->window_size.height);
378 window->egl_surface =
379 eglCreateWindowSurface(display->egl.dpy,
381 window->native, NULL);
383 xdg_surface_set_title(window->xdg_surface, "simple-egl");
385 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
386 window->egl_surface, window->display->egl.ctx);
387 assert(ret == EGL_TRUE);
389 if (!window->frame_sync)
390 eglSwapInterval(display->egl.dpy, 0);
392 set_fullscreen(window, window->fullscreen);
396 destroy_surface(struct window *window)
398 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
399 * on eglReleaseThread(). */
400 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
403 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
404 wl_egl_window_destroy(window->native);
406 xdg_surface_destroy(window->xdg_surface);
407 wl_surface_destroy(window->surface);
409 if (window->callback)
410 wl_callback_destroy(window->callback);
413 static const struct wl_callback_listener frame_listener;
416 redraw(void *data, struct wl_callback *callback, uint32_t time)
418 struct window *window = data;
419 struct display *display = window->display;
420 static const GLfloat verts[3][2] = {
425 static const GLfloat colors[3][3] = {
431 GLfloat rotation[4][4] = {
437 static const uint32_t speed_div = 5, benchmark_interval = 5;
438 struct wl_region *region;
440 EGLint buffer_age = 0;
443 assert(window->callback == callback);
444 window->callback = NULL;
447 wl_callback_destroy(callback);
449 if (!window->configured)
452 gettimeofday(&tv, NULL);
453 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
454 if (window->frames == 0)
455 window->benchmark_time = time;
456 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
457 printf("%d frames in %d seconds: %f fps\n",
460 (float) window->frames / benchmark_interval);
461 window->benchmark_time = time;
465 angle = (time / speed_div) % 360 * M_PI / 180.0;
466 rotation[0][0] = cos(angle);
467 rotation[0][2] = sin(angle);
468 rotation[2][0] = -sin(angle);
469 rotation[2][2] = cos(angle);
471 if (display->swap_buffers_with_damage)
472 eglQuerySurface(display->egl.dpy, window->egl_surface,
473 EGL_BUFFER_AGE_EXT, &buffer_age);
475 glViewport(0, 0, window->geometry.width, window->geometry.height);
477 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
478 (GLfloat *) rotation);
480 glClearColor(0.0, 0.0, 0.0, 0.5);
481 glClear(GL_COLOR_BUFFER_BIT);
483 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
484 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
485 glEnableVertexAttribArray(window->gl.pos);
486 glEnableVertexAttribArray(window->gl.col);
488 glDrawArrays(GL_TRIANGLES, 0, 3);
490 glDisableVertexAttribArray(window->gl.pos);
491 glDisableVertexAttribArray(window->gl.col);
493 if (window->opaque || window->fullscreen) {
494 region = wl_compositor_create_region(window->display->compositor);
495 wl_region_add(region, 0, 0,
496 window->geometry.width,
497 window->geometry.height);
498 wl_surface_set_opaque_region(window->surface, region);
499 wl_region_destroy(region);
501 wl_surface_set_opaque_region(window->surface, NULL);
504 if (display->swap_buffers_with_damage && buffer_age > 0) {
505 rect[0] = window->geometry.width / 4 - 1;
506 rect[1] = window->geometry.height / 4 - 1;
507 rect[2] = window->geometry.width / 2 + 2;
508 rect[3] = window->geometry.height / 2 + 2;
509 display->swap_buffers_with_damage(display->egl.dpy,
513 eglSwapBuffers(display->egl.dpy, window->egl_surface);
518 static const struct wl_callback_listener frame_listener = {
523 pointer_handle_enter(void *data, struct wl_pointer *pointer,
524 uint32_t serial, struct wl_surface *surface,
525 wl_fixed_t sx, wl_fixed_t sy)
527 struct display *display = data;
528 struct wl_buffer *buffer;
529 struct wl_cursor *cursor = display->default_cursor;
530 struct wl_cursor_image *image;
532 if (display->window->fullscreen)
533 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
535 image = display->default_cursor->images[0];
536 buffer = wl_cursor_image_get_buffer(image);
537 wl_pointer_set_cursor(pointer, serial,
538 display->cursor_surface,
541 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
542 wl_surface_damage(display->cursor_surface, 0, 0,
543 image->width, image->height);
544 wl_surface_commit(display->cursor_surface);
549 pointer_handle_leave(void *data, struct wl_pointer *pointer,
550 uint32_t serial, struct wl_surface *surface)
555 pointer_handle_motion(void *data, struct wl_pointer *pointer,
556 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
561 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
562 uint32_t serial, uint32_t time, uint32_t button,
565 struct display *display = data;
567 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
568 xdg_surface_move(display->window->xdg_surface,
569 display->seat, serial);
573 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
574 uint32_t time, uint32_t axis, wl_fixed_t value)
578 static const struct wl_pointer_listener pointer_listener = {
579 pointer_handle_enter,
580 pointer_handle_leave,
581 pointer_handle_motion,
582 pointer_handle_button,
587 touch_handle_down(void *data, struct wl_touch *wl_touch,
588 uint32_t serial, uint32_t time, struct wl_surface *surface,
589 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
591 struct display *d = (struct display *)data;
593 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
597 touch_handle_up(void *data, struct wl_touch *wl_touch,
598 uint32_t serial, uint32_t time, int32_t id)
603 touch_handle_motion(void *data, struct wl_touch *wl_touch,
604 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
609 touch_handle_frame(void *data, struct wl_touch *wl_touch)
614 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
618 static const struct wl_touch_listener touch_listener = {
627 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
628 uint32_t format, int fd, uint32_t size)
633 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
634 uint32_t serial, struct wl_surface *surface,
635 struct wl_array *keys)
640 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
641 uint32_t serial, struct wl_surface *surface)
646 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
647 uint32_t serial, uint32_t time, uint32_t key,
650 struct display *d = data;
652 if (key == KEY_F11 && state)
653 set_fullscreen(d->window, d->window->fullscreen ^ 1);
654 else if (key == KEY_ESC && state)
659 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
660 uint32_t serial, uint32_t mods_depressed,
661 uint32_t mods_latched, uint32_t mods_locked,
666 static const struct wl_keyboard_listener keyboard_listener = {
667 keyboard_handle_keymap,
668 keyboard_handle_enter,
669 keyboard_handle_leave,
671 keyboard_handle_modifiers,
675 seat_handle_capabilities(void *data, struct wl_seat *seat,
676 enum wl_seat_capability caps)
678 struct display *d = data;
680 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
681 d->pointer = wl_seat_get_pointer(seat);
682 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
683 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
684 wl_pointer_destroy(d->pointer);
688 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
689 d->keyboard = wl_seat_get_keyboard(seat);
690 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
691 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
692 wl_keyboard_destroy(d->keyboard);
696 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
697 d->touch = wl_seat_get_touch(seat);
698 wl_touch_set_user_data(d->touch, d);
699 wl_touch_add_listener(d->touch, &touch_listener, d);
700 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
701 wl_touch_destroy(d->touch);
706 static const struct wl_seat_listener seat_listener = {
707 seat_handle_capabilities,
711 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
713 xdg_shell_pong(shell, serial);
716 static const struct xdg_shell_listener xdg_shell_listener = {
720 #define XDG_VERSION 2 /* The version of xdg-shell that we implement */
722 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
723 "Interface version doesn't match implementation version");
727 registry_handle_global(void *data, struct wl_registry *registry,
728 uint32_t name, const char *interface, uint32_t version)
730 struct display *d = data;
732 if (strcmp(interface, "wl_compositor") == 0) {
734 wl_registry_bind(registry, name,
735 &wl_compositor_interface, 1);
736 } else if (strcmp(interface, "xdg_shell") == 0) {
737 d->shell = wl_registry_bind(registry, name,
738 &xdg_shell_interface, 1);
739 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
740 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
741 } else if (strcmp(interface, "wl_seat") == 0) {
742 d->seat = wl_registry_bind(registry, name,
743 &wl_seat_interface, 1);
744 wl_seat_add_listener(d->seat, &seat_listener, d);
745 } else if (strcmp(interface, "wl_shm") == 0) {
746 d->shm = wl_registry_bind(registry, name,
747 &wl_shm_interface, 1);
748 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
750 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
755 registry_handle_global_remove(void *data, struct wl_registry *registry,
760 static const struct wl_registry_listener registry_listener = {
761 registry_handle_global,
762 registry_handle_global_remove
766 signal_int(int signum)
772 usage(int error_code)
774 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
775 " -f\tRun in fullscreen mode\n"
776 " -o\tCreate an opaque surface\n"
777 " -s\tUse a 16 bpp EGL config\n"
778 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
779 " -h\tThis help text\n\n");
785 main(int argc, char **argv)
787 struct sigaction sigint;
788 struct display display = { 0 };
789 struct window window = { 0 };
792 window.display = &display;
793 display.window = &window;
794 window.window_size.width = 250;
795 window.window_size.height = 250;
796 window.buffer_size = 32;
797 window.frame_sync = 1;
799 for (i = 1; i < argc; i++) {
800 if (strcmp("-f", argv[i]) == 0)
801 window.fullscreen = 1;
802 else if (strcmp("-o", argv[i]) == 0)
804 else if (strcmp("-s", argv[i]) == 0)
805 window.buffer_size = 16;
806 else if (strcmp("-b", argv[i]) == 0)
807 window.frame_sync = 0;
808 else if (strcmp("-h", argv[i]) == 0)
814 display.display = wl_display_connect(NULL);
815 assert(display.display);
817 display.registry = wl_display_get_registry(display.display);
818 wl_registry_add_listener(display.registry,
819 ®istry_listener, &display);
821 wl_display_dispatch(display.display);
823 init_egl(&display, &window);
824 create_surface(&window);
827 display.cursor_surface =
828 wl_compositor_create_surface(display.compositor);
830 sigint.sa_handler = signal_int;
831 sigemptyset(&sigint.sa_mask);
832 sigint.sa_flags = SA_RESETHAND;
833 sigaction(SIGINT, &sigint, NULL);
835 /* The mainloop here is a little subtle. Redrawing will cause
836 * EGL to read events so we can just call
837 * wl_display_dispatch_pending() to handle any events that got
838 * queued up as a side effect. */
839 while (running && ret != -1) {
840 wl_display_dispatch_pending(display.display);
841 while (!window.configured)
842 wl_display_dispatch(display.display);
843 redraw(&window, NULL, 0);
846 fprintf(stderr, "simple-egl exiting\n");
848 destroy_surface(&window);
851 wl_surface_destroy(display.cursor_surface);
852 if (display.cursor_theme)
853 wl_cursor_theme_destroy(display.cursor_theme);
856 xdg_shell_destroy(display.shell);
858 if (display.compositor)
859 wl_compositor_destroy(display.compositor);
861 wl_registry_destroy(display.registry);
862 wl_display_flush(display.display);
863 wl_display_disconnect(display.display);