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)
273 struct window *window = data;
276 wl_egl_window_resize(window->native, width, height, 0, 0);
278 window->geometry.width = width;
279 window->geometry.height = height;
281 if (!window->fullscreen)
282 window->window_size = window->geometry;
286 handle_surface_change_state(void *data, struct xdg_surface *xdg_surface,
291 struct window *window = data;
294 case XDG_SURFACE_STATE_FULLSCREEN:
295 window->fullscreen = value;
298 handle_surface_configure(window, window->xdg_surface,
299 window->window_size.width,
300 window->window_size.height);
304 xdg_surface_ack_change_state(xdg_surface, state, value, serial);
308 handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
313 handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
318 handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
323 static const struct xdg_surface_listener xdg_surface_listener = {
324 handle_surface_configure,
325 handle_surface_change_state,
326 handle_surface_activated,
327 handle_surface_deactivated,
328 handle_surface_delete,
332 create_surface(struct window *window)
334 struct display *display = window->display;
337 window->surface = wl_compositor_create_surface(display->compositor);
338 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
341 xdg_surface_add_listener(window->xdg_surface,
342 &xdg_surface_listener, window);
345 wl_egl_window_create(window->surface,
346 window->window_size.width,
347 window->window_size.height);
348 window->egl_surface =
349 eglCreateWindowSurface(display->egl.dpy,
351 window->native, NULL);
353 xdg_surface_set_title(window->xdg_surface, "simple-egl");
355 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
356 window->egl_surface, window->display->egl.ctx);
357 assert(ret == EGL_TRUE);
359 if (!window->frame_sync)
360 eglSwapInterval(display->egl.dpy, 0);
362 xdg_surface_request_change_state(window->xdg_surface,
363 XDG_SURFACE_STATE_FULLSCREEN,
364 window->fullscreen, 0);
368 destroy_surface(struct window *window)
370 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
371 * on eglReleaseThread(). */
372 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
375 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
376 wl_egl_window_destroy(window->native);
378 xdg_surface_destroy(window->xdg_surface);
379 wl_surface_destroy(window->surface);
381 if (window->callback)
382 wl_callback_destroy(window->callback);
385 static const struct wl_callback_listener frame_listener;
388 redraw(void *data, struct wl_callback *callback, uint32_t time)
390 struct window *window = data;
391 struct display *display = window->display;
392 static const GLfloat verts[3][2] = {
397 static const GLfloat colors[3][3] = {
403 GLfloat rotation[4][4] = {
409 static const uint32_t speed_div = 5, benchmark_interval = 5;
410 struct wl_region *region;
412 EGLint buffer_age = 0;
415 assert(window->callback == callback);
416 window->callback = NULL;
419 wl_callback_destroy(callback);
421 gettimeofday(&tv, NULL);
422 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
423 if (window->frames == 0)
424 window->benchmark_time = time;
425 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
426 printf("%d frames in %d seconds: %f fps\n",
429 (float) window->frames / benchmark_interval);
430 window->benchmark_time = time;
434 angle = (time / speed_div) % 360 * M_PI / 180.0;
435 rotation[0][0] = cos(angle);
436 rotation[0][2] = sin(angle);
437 rotation[2][0] = -sin(angle);
438 rotation[2][2] = cos(angle);
440 if (display->swap_buffers_with_damage)
441 eglQuerySurface(display->egl.dpy, window->egl_surface,
442 EGL_BUFFER_AGE_EXT, &buffer_age);
444 glViewport(0, 0, window->geometry.width, window->geometry.height);
446 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
447 (GLfloat *) rotation);
449 glClearColor(0.0, 0.0, 0.0, 0.5);
450 glClear(GL_COLOR_BUFFER_BIT);
452 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
453 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
454 glEnableVertexAttribArray(window->gl.pos);
455 glEnableVertexAttribArray(window->gl.col);
457 glDrawArrays(GL_TRIANGLES, 0, 3);
459 glDisableVertexAttribArray(window->gl.pos);
460 glDisableVertexAttribArray(window->gl.col);
462 if (window->opaque || window->fullscreen) {
463 region = wl_compositor_create_region(window->display->compositor);
464 wl_region_add(region, 0, 0,
465 window->geometry.width,
466 window->geometry.height);
467 wl_surface_set_opaque_region(window->surface, region);
468 wl_region_destroy(region);
470 wl_surface_set_opaque_region(window->surface, NULL);
473 if (display->swap_buffers_with_damage && buffer_age > 0) {
474 rect[0] = window->geometry.width / 4 - 1;
475 rect[1] = window->geometry.height / 4 - 1;
476 rect[2] = window->geometry.width / 2 + 2;
477 rect[3] = window->geometry.height / 2 + 2;
478 display->swap_buffers_with_damage(display->egl.dpy,
482 eglSwapBuffers(display->egl.dpy, window->egl_surface);
487 static const struct wl_callback_listener frame_listener = {
492 pointer_handle_enter(void *data, struct wl_pointer *pointer,
493 uint32_t serial, struct wl_surface *surface,
494 wl_fixed_t sx, wl_fixed_t sy)
496 struct display *display = data;
497 struct wl_buffer *buffer;
498 struct wl_cursor *cursor = display->default_cursor;
499 struct wl_cursor_image *image;
501 if (display->window->fullscreen)
502 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
504 image = display->default_cursor->images[0];
505 buffer = wl_cursor_image_get_buffer(image);
508 wl_pointer_set_cursor(pointer, serial,
509 display->cursor_surface,
512 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
513 wl_surface_damage(display->cursor_surface, 0, 0,
514 image->width, image->height);
515 wl_surface_commit(display->cursor_surface);
520 pointer_handle_leave(void *data, struct wl_pointer *pointer,
521 uint32_t serial, struct wl_surface *surface)
526 pointer_handle_motion(void *data, struct wl_pointer *pointer,
527 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
532 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
533 uint32_t serial, uint32_t time, uint32_t button,
536 struct display *display = data;
538 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
539 xdg_surface_move(display->window->xdg_surface,
540 display->seat, serial);
544 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
545 uint32_t time, uint32_t axis, wl_fixed_t value)
549 static const struct wl_pointer_listener pointer_listener = {
550 pointer_handle_enter,
551 pointer_handle_leave,
552 pointer_handle_motion,
553 pointer_handle_button,
558 touch_handle_down(void *data, struct wl_touch *wl_touch,
559 uint32_t serial, uint32_t time, struct wl_surface *surface,
560 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
562 struct display *d = (struct display *)data;
564 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
568 touch_handle_up(void *data, struct wl_touch *wl_touch,
569 uint32_t serial, uint32_t time, int32_t id)
574 touch_handle_motion(void *data, struct wl_touch *wl_touch,
575 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
580 touch_handle_frame(void *data, struct wl_touch *wl_touch)
585 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
589 static const struct wl_touch_listener touch_listener = {
598 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
599 uint32_t format, int fd, uint32_t size)
604 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
605 uint32_t serial, struct wl_surface *surface,
606 struct wl_array *keys)
611 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
612 uint32_t serial, struct wl_surface *surface)
617 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
618 uint32_t serial, uint32_t time, uint32_t key,
621 struct display *d = data;
623 if (key == KEY_F11 && state)
624 xdg_surface_request_change_state(d->window->xdg_surface,
625 XDG_SURFACE_STATE_FULLSCREEN,
626 !d->window->fullscreen, 0);
627 else if (key == KEY_ESC && state)
632 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
633 uint32_t serial, uint32_t mods_depressed,
634 uint32_t mods_latched, uint32_t mods_locked,
639 static const struct wl_keyboard_listener keyboard_listener = {
640 keyboard_handle_keymap,
641 keyboard_handle_enter,
642 keyboard_handle_leave,
644 keyboard_handle_modifiers,
648 seat_handle_capabilities(void *data, struct wl_seat *seat,
649 enum wl_seat_capability caps)
651 struct display *d = data;
653 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
654 d->pointer = wl_seat_get_pointer(seat);
655 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
656 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
657 wl_pointer_destroy(d->pointer);
661 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
662 d->keyboard = wl_seat_get_keyboard(seat);
663 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
664 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
665 wl_keyboard_destroy(d->keyboard);
669 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
670 d->touch = wl_seat_get_touch(seat);
671 wl_touch_set_user_data(d->touch, d);
672 wl_touch_add_listener(d->touch, &touch_listener, d);
673 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
674 wl_touch_destroy(d->touch);
679 static const struct wl_seat_listener seat_listener = {
680 seat_handle_capabilities,
684 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
686 xdg_shell_pong(shell, serial);
689 static const struct xdg_shell_listener xdg_shell_listener = {
693 #define XDG_VERSION 3 /* The version of xdg-shell that we implement */
695 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
696 "Interface version doesn't match implementation version");
700 registry_handle_global(void *data, struct wl_registry *registry,
701 uint32_t name, const char *interface, uint32_t version)
703 struct display *d = data;
705 if (strcmp(interface, "wl_compositor") == 0) {
707 wl_registry_bind(registry, name,
708 &wl_compositor_interface, 1);
709 } else if (strcmp(interface, "xdg_shell") == 0) {
710 d->shell = wl_registry_bind(registry, name,
711 &xdg_shell_interface, 1);
712 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
713 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
714 } else if (strcmp(interface, "wl_seat") == 0) {
715 d->seat = wl_registry_bind(registry, name,
716 &wl_seat_interface, 1);
717 wl_seat_add_listener(d->seat, &seat_listener, d);
718 } else if (strcmp(interface, "wl_shm") == 0) {
719 d->shm = wl_registry_bind(registry, name,
720 &wl_shm_interface, 1);
721 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
722 if (!d->cursor_theme) {
723 fprintf(stderr, "unable to load default theme\n");
727 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
728 if (!d->default_cursor) {
729 fprintf(stderr, "unable to load default left pointer\n");
736 registry_handle_global_remove(void *data, struct wl_registry *registry,
741 static const struct wl_registry_listener registry_listener = {
742 registry_handle_global,
743 registry_handle_global_remove
747 signal_int(int signum)
753 usage(int error_code)
755 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
756 " -f\tRun in fullscreen mode\n"
757 " -o\tCreate an opaque surface\n"
758 " -s\tUse a 16 bpp EGL config\n"
759 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
760 " -h\tThis help text\n\n");
766 main(int argc, char **argv)
768 struct sigaction sigint;
769 struct display display = { 0 };
770 struct window window = { 0 };
773 window.display = &display;
774 display.window = &window;
775 window.window_size.width = 250;
776 window.window_size.height = 250;
777 window.buffer_size = 32;
778 window.frame_sync = 1;
780 for (i = 1; i < argc; i++) {
781 if (strcmp("-f", argv[i]) == 0)
782 window.fullscreen = 1;
783 else if (strcmp("-o", argv[i]) == 0)
785 else if (strcmp("-s", argv[i]) == 0)
786 window.buffer_size = 16;
787 else if (strcmp("-b", argv[i]) == 0)
788 window.frame_sync = 0;
789 else if (strcmp("-h", argv[i]) == 0)
795 display.display = wl_display_connect(NULL);
796 assert(display.display);
798 display.registry = wl_display_get_registry(display.display);
799 wl_registry_add_listener(display.registry,
800 ®istry_listener, &display);
802 wl_display_dispatch(display.display);
804 init_egl(&display, &window);
805 create_surface(&window);
808 display.cursor_surface =
809 wl_compositor_create_surface(display.compositor);
811 sigint.sa_handler = signal_int;
812 sigemptyset(&sigint.sa_mask);
813 sigint.sa_flags = SA_RESETHAND;
814 sigaction(SIGINT, &sigint, NULL);
816 /* The mainloop here is a little subtle. Redrawing will cause
817 * EGL to read events so we can just call
818 * wl_display_dispatch_pending() to handle any events that got
819 * queued up as a side effect. */
820 while (running && ret != -1) {
821 wl_display_dispatch_pending(display.display);
822 redraw(&window, NULL, 0);
825 fprintf(stderr, "simple-egl exiting\n");
827 destroy_surface(&window);
830 wl_surface_destroy(display.cursor_surface);
831 if (display.cursor_theme)
832 wl_cursor_theme_destroy(display.cursor_theme);
835 xdg_shell_destroy(display.shell);
837 if (display.compositor)
838 wl_compositor_destroy(display.compositor);
840 wl_registry_destroy(display.registry);
841 wl_display_flush(display.display);
842 wl_display_disconnect(display.display);