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 #ifndef EGL_EXT_swap_buffers_with_damage
42 #define EGL_EXT_swap_buffers_with_damage 1
43 typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
46 #ifndef EGL_EXT_buffer_age
47 #define EGL_EXT_buffer_age 1
48 #define EGL_BUFFER_AGE_EXT 0x313D
55 struct wl_display *display;
56 struct wl_registry *registry;
57 struct wl_compositor *compositor;
58 struct wl_shell *shell;
60 struct wl_pointer *pointer;
61 struct wl_touch *touch;
62 struct wl_keyboard *keyboard;
64 struct wl_cursor_theme *cursor_theme;
65 struct wl_cursor *default_cursor;
66 struct wl_surface *cursor_surface;
72 struct window *window;
74 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
82 struct display *display;
83 struct geometry geometry, window_size;
85 GLuint rotation_uniform;
90 uint32_t benchmark_time, frames;
91 struct wl_egl_window *native;
92 struct wl_surface *surface;
93 struct wl_shell_surface *shell_surface;
94 EGLSurface egl_surface;
95 struct wl_callback *callback;
96 int fullscreen, configured, opaque, buffer_size;
99 static const char *vert_shader_text =
100 "uniform mat4 rotation;\n"
101 "attribute vec4 pos;\n"
102 "attribute vec4 color;\n"
103 "varying vec4 v_color;\n"
105 " gl_Position = rotation * pos;\n"
106 " v_color = color;\n"
109 static const char *frag_shader_text =
110 "precision mediump float;\n"
111 "varying vec4 v_color;\n"
113 " gl_FragColor = v_color;\n"
116 static int running = 1;
119 init_egl(struct display *display, struct window *window)
121 static const EGLint context_attribs[] = {
122 EGL_CONTEXT_CLIENT_VERSION, 2,
125 const char *extensions;
127 EGLint config_attribs[] = {
128 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
132 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
136 EGLint major, minor, n, count, i, size;
140 if (window->opaque || window->buffer_size == 16)
141 config_attribs[9] = 0;
143 display->egl.dpy = eglGetDisplay(display->display);
144 assert(display->egl.dpy);
146 ret = eglInitialize(display->egl.dpy, &major, &minor);
147 assert(ret == EGL_TRUE);
148 ret = eglBindAPI(EGL_OPENGL_ES_API);
149 assert(ret == EGL_TRUE);
151 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
154 configs = calloc(count, sizeof *configs);
157 ret = eglChooseConfig(display->egl.dpy, config_attribs,
159 assert(ret && n >= 1);
161 for (i = 0; i < n; i++) {
162 eglGetConfigAttrib(display->egl.dpy,
163 configs[i], EGL_BUFFER_SIZE, &size);
164 if (window->buffer_size == size) {
165 display->egl.conf = configs[i];
170 if (display->egl.conf == NULL) {
171 fprintf(stderr, "did not find config with buffer size %d\n",
172 window->buffer_size);
176 display->egl.ctx = eglCreateContext(display->egl.dpy,
178 EGL_NO_CONTEXT, context_attribs);
179 assert(display->egl.ctx);
181 display->swap_buffers_with_damage = NULL;
182 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
184 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
185 strstr(extensions, "EGL_EXT_buffer_age"))
186 display->swap_buffers_with_damage =
187 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
188 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
190 if (display->swap_buffers_with_damage)
191 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
196 fini_egl(struct display *display)
198 eglTerminate(display->egl.dpy);
203 create_shader(struct window *window, const char *source, GLenum shader_type)
208 shader = glCreateShader(shader_type);
211 glShaderSource(shader, 1, (const char **) &source, NULL);
212 glCompileShader(shader);
214 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
218 glGetShaderInfoLog(shader, 1000, &len, log);
219 fprintf(stderr, "Error: compiling %s: %*s\n",
220 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
229 init_gl(struct window *window)
235 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
236 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
238 program = glCreateProgram();
239 glAttachShader(program, frag);
240 glAttachShader(program, vert);
241 glLinkProgram(program);
243 glGetProgramiv(program, GL_LINK_STATUS, &status);
247 glGetProgramInfoLog(program, 1000, &len, log);
248 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
252 glUseProgram(program);
257 glBindAttribLocation(program, window->gl.pos, "pos");
258 glBindAttribLocation(program, window->gl.col, "color");
259 glLinkProgram(program);
261 window->gl.rotation_uniform =
262 glGetUniformLocation(program, "rotation");
266 handle_ping(void *data, struct wl_shell_surface *shell_surface,
269 wl_shell_surface_pong(shell_surface, serial);
273 handle_configure(void *data, struct wl_shell_surface *shell_surface,
274 uint32_t edges, int32_t width, int32_t height)
276 struct window *window = data;
279 wl_egl_window_resize(window->native, width, height, 0, 0);
281 window->geometry.width = width;
282 window->geometry.height = height;
284 if (!window->fullscreen)
285 window->window_size = window->geometry;
289 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
293 static const struct wl_shell_surface_listener shell_surface_listener = {
300 redraw(void *data, struct wl_callback *callback, uint32_t time);
303 configure_callback(void *data, struct wl_callback *callback, uint32_t time)
305 struct window *window = data;
307 wl_callback_destroy(callback);
309 window->configured = 1;
311 if (window->callback == NULL)
312 redraw(data, NULL, time);
315 static struct wl_callback_listener configure_callback_listener = {
320 toggle_fullscreen(struct window *window, int fullscreen)
322 struct wl_callback *callback;
324 window->fullscreen = fullscreen;
325 window->configured = 0;
328 wl_shell_surface_set_fullscreen(window->shell_surface,
329 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
332 wl_shell_surface_set_toplevel(window->shell_surface);
333 handle_configure(window, window->shell_surface, 0,
334 window->window_size.width,
335 window->window_size.height);
338 callback = wl_display_sync(window->display->display);
339 wl_callback_add_listener(callback, &configure_callback_listener,
344 create_surface(struct window *window)
346 struct display *display = window->display;
349 window->surface = wl_compositor_create_surface(display->compositor);
350 window->shell_surface = wl_shell_get_shell_surface(display->shell,
353 wl_shell_surface_add_listener(window->shell_surface,
354 &shell_surface_listener, window);
357 wl_egl_window_create(window->surface,
358 window->window_size.width,
359 window->window_size.height);
360 window->egl_surface =
361 eglCreateWindowSurface(display->egl.dpy,
363 window->native, NULL);
365 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
367 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
368 window->egl_surface, window->display->egl.ctx);
369 assert(ret == EGL_TRUE);
371 toggle_fullscreen(window, window->fullscreen);
375 destroy_surface(struct window *window)
377 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
378 * on eglReleaseThread(). */
379 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
382 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
383 wl_egl_window_destroy(window->native);
385 wl_shell_surface_destroy(window->shell_surface);
386 wl_surface_destroy(window->surface);
388 if (window->callback)
389 wl_callback_destroy(window->callback);
392 static const struct wl_callback_listener frame_listener;
395 redraw(void *data, struct wl_callback *callback, uint32_t time)
397 struct window *window = data;
398 struct display *display = window->display;
399 static const GLfloat verts[3][2] = {
404 static const GLfloat colors[3][3] = {
410 GLfloat rotation[4][4] = {
416 static const int32_t speed_div = 5, benchmark_interval = 5;
417 struct wl_region *region;
419 EGLint buffer_age = 0;
422 assert(window->callback == callback);
423 window->callback = NULL;
426 wl_callback_destroy(callback);
428 if (!window->configured)
431 gettimeofday(&tv, NULL);
432 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
433 if (window->frames == 0)
434 window->benchmark_time = time;
435 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
436 printf("%d frames in %d seconds: %f fps\n",
439 (float) window->frames / benchmark_interval);
440 window->benchmark_time = time;
444 angle = (time / speed_div) % 360 * M_PI / 180.0;
445 rotation[0][0] = cos(angle);
446 rotation[0][2] = sin(angle);
447 rotation[2][0] = -sin(angle);
448 rotation[2][2] = cos(angle);
450 if (display->swap_buffers_with_damage)
451 eglQuerySurface(display->egl.dpy, window->egl_surface,
452 EGL_BUFFER_AGE_EXT, &buffer_age);
454 glViewport(0, 0, window->geometry.width, window->geometry.height);
456 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
457 (GLfloat *) rotation);
459 glClearColor(0.0, 0.0, 0.0, 0.5);
460 glClear(GL_COLOR_BUFFER_BIT);
462 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
463 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
464 glEnableVertexAttribArray(window->gl.pos);
465 glEnableVertexAttribArray(window->gl.col);
467 glDrawArrays(GL_TRIANGLES, 0, 3);
469 glDisableVertexAttribArray(window->gl.pos);
470 glDisableVertexAttribArray(window->gl.col);
472 if (window->opaque || window->fullscreen) {
473 region = wl_compositor_create_region(window->display->compositor);
474 wl_region_add(region, 0, 0,
475 window->geometry.width,
476 window->geometry.height);
477 wl_surface_set_opaque_region(window->surface, region);
478 wl_region_destroy(region);
480 wl_surface_set_opaque_region(window->surface, NULL);
483 window->callback = wl_surface_frame(window->surface);
484 wl_callback_add_listener(window->callback, &frame_listener, window);
486 if (display->swap_buffers_with_damage && buffer_age > 0) {
487 rect[0] = window->geometry.width / 4 - 1;
488 rect[1] = window->geometry.height / 4 - 1;
489 rect[2] = window->geometry.width / 2 + 2;
490 rect[3] = window->geometry.height / 2 + 2;
491 display->swap_buffers_with_damage(display->egl.dpy,
495 eglSwapBuffers(display->egl.dpy, window->egl_surface);
500 static const struct wl_callback_listener frame_listener = {
505 pointer_handle_enter(void *data, struct wl_pointer *pointer,
506 uint32_t serial, struct wl_surface *surface,
507 wl_fixed_t sx, wl_fixed_t sy)
509 struct display *display = data;
510 struct wl_buffer *buffer;
511 struct wl_cursor *cursor = display->default_cursor;
512 struct wl_cursor_image *image;
514 if (display->window->fullscreen)
515 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
517 image = display->default_cursor->images[0];
518 buffer = wl_cursor_image_get_buffer(image);
519 wl_pointer_set_cursor(pointer, serial,
520 display->cursor_surface,
523 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
524 wl_surface_damage(display->cursor_surface, 0, 0,
525 image->width, image->height);
526 wl_surface_commit(display->cursor_surface);
531 pointer_handle_leave(void *data, struct wl_pointer *pointer,
532 uint32_t serial, struct wl_surface *surface)
537 pointer_handle_motion(void *data, struct wl_pointer *pointer,
538 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
543 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
544 uint32_t serial, uint32_t time, uint32_t button,
547 struct display *display = data;
549 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
550 wl_shell_surface_move(display->window->shell_surface,
551 display->seat, serial);
555 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
556 uint32_t time, uint32_t axis, wl_fixed_t value)
560 static const struct wl_pointer_listener pointer_listener = {
561 pointer_handle_enter,
562 pointer_handle_leave,
563 pointer_handle_motion,
564 pointer_handle_button,
569 touch_handle_down(void *data, struct wl_touch *wl_touch,
570 uint32_t serial, uint32_t time, struct wl_surface *surface,
571 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
573 struct display *d = (struct display *)data;
575 wl_shell_surface_move(d->window->shell_surface, d->seat, serial);
579 touch_handle_up(void *data, struct wl_touch *wl_touch,
580 uint32_t serial, uint32_t time, int32_t id)
585 touch_handle_motion(void *data, struct wl_touch *wl_touch,
586 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
591 touch_handle_frame(void *data, struct wl_touch *wl_touch)
596 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
600 static const struct wl_touch_listener touch_listener = {
609 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
610 uint32_t format, int fd, uint32_t size)
615 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
616 uint32_t serial, struct wl_surface *surface,
617 struct wl_array *keys)
622 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
623 uint32_t serial, struct wl_surface *surface)
628 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
629 uint32_t serial, uint32_t time, uint32_t key,
632 struct display *d = data;
634 if (key == KEY_F11 && state)
635 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
636 else if (key == KEY_ESC && state)
641 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
642 uint32_t serial, uint32_t mods_depressed,
643 uint32_t mods_latched, uint32_t mods_locked,
648 static const struct wl_keyboard_listener keyboard_listener = {
649 keyboard_handle_keymap,
650 keyboard_handle_enter,
651 keyboard_handle_leave,
653 keyboard_handle_modifiers,
657 seat_handle_capabilities(void *data, struct wl_seat *seat,
658 enum wl_seat_capability caps)
660 struct display *d = data;
662 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
663 d->pointer = wl_seat_get_pointer(seat);
664 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
665 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
666 wl_pointer_destroy(d->pointer);
670 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
671 d->keyboard = wl_seat_get_keyboard(seat);
672 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
673 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
674 wl_keyboard_destroy(d->keyboard);
678 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
679 d->touch = wl_seat_get_touch(seat);
680 wl_touch_set_user_data(d->touch, d);
681 wl_touch_add_listener(d->touch, &touch_listener, d);
682 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
683 wl_touch_destroy(d->touch);
688 static const struct wl_seat_listener seat_listener = {
689 seat_handle_capabilities,
693 registry_handle_global(void *data, struct wl_registry *registry,
694 uint32_t name, const char *interface, uint32_t version)
696 struct display *d = data;
698 if (strcmp(interface, "wl_compositor") == 0) {
700 wl_registry_bind(registry, name,
701 &wl_compositor_interface, 1);
702 } else if (strcmp(interface, "wl_shell") == 0) {
703 d->shell = wl_registry_bind(registry, name,
704 &wl_shell_interface, 1);
705 } else if (strcmp(interface, "wl_seat") == 0) {
706 d->seat = wl_registry_bind(registry, name,
707 &wl_seat_interface, 1);
708 wl_seat_add_listener(d->seat, &seat_listener, d);
709 } else if (strcmp(interface, "wl_shm") == 0) {
710 d->shm = wl_registry_bind(registry, name,
711 &wl_shm_interface, 1);
712 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
714 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
719 registry_handle_global_remove(void *data, struct wl_registry *registry,
724 static const struct wl_registry_listener registry_listener = {
725 registry_handle_global,
726 registry_handle_global_remove
730 signal_int(int signum)
736 usage(int error_code)
738 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
739 " -f\tRun in fullscreen mode\n"
740 " -o\tCreate an opaque surface\n"
741 " -s\tUse a 16 bpp EGL config\n"
742 " -h\tThis help text\n\n");
748 main(int argc, char **argv)
750 struct sigaction sigint;
751 struct display display = { 0 };
752 struct window window = { 0 };
755 window.display = &display;
756 display.window = &window;
757 window.window_size.width = 250;
758 window.window_size.height = 250;
759 window.buffer_size = 32;
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("-h", argv[i]) == 0)
774 display.display = wl_display_connect(NULL);
775 assert(display.display);
777 display.registry = wl_display_get_registry(display.display);
778 wl_registry_add_listener(display.registry,
779 ®istry_listener, &display);
781 wl_display_dispatch(display.display);
783 init_egl(&display, &window);
784 create_surface(&window);
787 display.cursor_surface =
788 wl_compositor_create_surface(display.compositor);
790 sigint.sa_handler = signal_int;
791 sigemptyset(&sigint.sa_mask);
792 sigint.sa_flags = SA_RESETHAND;
793 sigaction(SIGINT, &sigint, NULL);
795 while (running && ret != -1)
796 ret = wl_display_dispatch(display.display);
798 fprintf(stderr, "simple-egl exiting\n");
800 destroy_surface(&window);
803 wl_surface_destroy(display.cursor_surface);
804 if (display.cursor_theme)
805 wl_cursor_theme_destroy(display.cursor_theme);
808 wl_shell_destroy(display.shell);
810 if (display.compositor)
811 wl_compositor_destroy(display.compositor);
813 wl_registry_destroy(display.registry);
814 wl_display_flush(display.display);
815 wl_display_disconnect(display.display);