blob: 33e711c548d866c5eaee36032b83be1bd1a91ed0 [file] [log] [blame]
Benjamin Franzkeaabdce02011-01-15 00:40:17 +01001/*
2 * Copyright © 2011 Benjamin Franzke
3 *
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.
13 *
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
20 * OF THIS SOFTWARE.
21 */
22
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010023#include "config.h"
24
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010028#include <stdbool.h>
29#include <math.h>
30#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020031#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010032
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030033#include <linux/input.h>
34
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010035#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050036#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040037#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010039#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010040#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040041#include <EGL/eglext.h>
42
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080043#include "xdg-shell-client-protocol.h"
44
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040045#ifndef EGL_EXT_swap_buffers_with_damage
46#define EGL_EXT_swap_buffers_with_damage 1
47typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
48#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010049
Kristian Høgsberg77787432013-08-22 12:16:51 -070050#ifndef EGL_EXT_buffer_age
51#define EGL_EXT_buffer_age 1
52#define EGL_BUFFER_AGE_EXT 0x313D
53#endif
54
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030055struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010056struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030057
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010058struct display {
59 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040060 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050061 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080062 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040063 struct wl_seat *seat;
64 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070065 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030066 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040067 struct wl_shm *shm;
68 struct wl_cursor_theme *cursor_theme;
69 struct wl_cursor *default_cursor;
70 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010071 struct {
72 EGLDisplay dpy;
73 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050074 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010075 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030076 struct window *window;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040077
78 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010079};
80
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030081struct geometry {
82 int width, height;
83};
84
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010085struct window {
86 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060087 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010088 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010089 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010090 GLuint pos;
91 GLuint col;
92 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050093
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080094 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050095 struct wl_egl_window *native;
96 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080097 struct xdg_surface *xdg_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050098 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020099 struct wl_callback *callback;
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500100 int fullscreen, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100101};
102
103static 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"
108 "void main() {\n"
109 " gl_Position = rotation * pos;\n"
110 " v_color = color;\n"
111 "}\n";
112
113static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500114 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100115 "varying vec4 v_color;\n"
116 "void main() {\n"
117 " gl_FragColor = v_color;\n"
118 "}\n";
119
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400120static int running = 1;
121
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100122static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700123init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100124{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500125 static const EGLint context_attribs[] = {
126 EGL_CONTEXT_CLIENT_VERSION, 2,
127 EGL_NONE
128 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400129 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500130
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300131 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500132 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500133 EGL_RED_SIZE, 1,
134 EGL_GREEN_SIZE, 1,
135 EGL_BLUE_SIZE, 1,
Arnaud Vrac488b7cd2014-08-25 20:56:48 +0200136 EGL_ALPHA_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500137 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
138 EGL_NONE
139 };
140
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700141 EGLint major, minor, n, count, i, size;
142 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100143 EGLBoolean ret;
144
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700145 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400146 config_attribs[9] = 0;
147
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400148 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100149 assert(display->egl.dpy);
150
151 ret = eglInitialize(display->egl.dpy, &major, &minor);
152 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500153 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100154 assert(ret == EGL_TRUE);
155
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700156 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
157 assert(0);
158
159 configs = calloc(count, sizeof *configs);
160 assert(configs);
161
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300162 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700163 configs, count, &n);
164 assert(ret && n >= 1);
165
166 for (i = 0; i < n; i++) {
167 eglGetConfigAttrib(display->egl.dpy,
168 configs[i], EGL_BUFFER_SIZE, &size);
169 if (window->buffer_size == size) {
170 display->egl.conf = configs[i];
171 break;
172 }
173 }
174 free(configs);
175 if (display->egl.conf == NULL) {
176 fprintf(stderr, "did not find config with buffer size %d\n",
177 window->buffer_size);
178 exit(EXIT_FAILURE);
179 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500180
181 display->egl.ctx = eglCreateContext(display->egl.dpy,
182 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500183 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100184 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500185
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400186 display->swap_buffers_with_damage = NULL;
187 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
188 if (extensions &&
189 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
190 strstr(extensions, "EGL_EXT_buffer_age"))
191 display->swap_buffers_with_damage =
192 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
193 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
194
195 if (display->swap_buffers_with_damage)
196 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
197
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100198}
199
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200200static void
201fini_egl(struct display *display)
202{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200203 eglTerminate(display->egl.dpy);
204 eglReleaseThread();
205}
206
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100207static GLuint
208create_shader(struct window *window, const char *source, GLenum shader_type)
209{
210 GLuint shader;
211 GLint status;
212
213 shader = glCreateShader(shader_type);
214 assert(shader != 0);
215
216 glShaderSource(shader, 1, (const char **) &source, NULL);
217 glCompileShader(shader);
218
219 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
220 if (!status) {
221 char log[1000];
222 GLsizei len;
223 glGetShaderInfoLog(shader, 1000, &len, log);
224 fprintf(stderr, "Error: compiling %s: %*s\n",
225 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
226 len, log);
227 exit(1);
228 }
229
230 return shader;
231}
232
233static void
234init_gl(struct window *window)
235{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100236 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600237 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100238 GLint status;
239
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100240 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
241 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
242
Scott Moreau3ea23d02012-06-13 17:42:21 -0600243 program = glCreateProgram();
244 glAttachShader(program, frag);
245 glAttachShader(program, vert);
246 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100247
Scott Moreau3ea23d02012-06-13 17:42:21 -0600248 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100249 if (!status) {
250 char log[1000];
251 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600252 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100253 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
254 exit(1);
255 }
256
Scott Moreau3ea23d02012-06-13 17:42:21 -0600257 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100258
259 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600260 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100261
Scott Moreau3ea23d02012-06-13 17:42:21 -0600262 glBindAttribLocation(program, window->gl.pos, "pos");
263 glBindAttribLocation(program, window->gl.col, "color");
264 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100265
266 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600267 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100268}
269
270static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800271handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700272 int32_t width, int32_t height,
273 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300274{
275 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700276 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300277
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700278 window->fullscreen = 0;
279 wl_array_for_each(p, states) {
280 uint32_t state = *p;
281 switch (state) {
282 case XDG_SURFACE_STATE_FULLSCREEN:
283 window->fullscreen = 1;
284 break;
285 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500286 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800287
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700288 if (width > 0 && height > 0) {
289 if (!window->fullscreen) {
290 window->window_size.width = width;
291 window->window_size.height = height;
292 }
293 window->geometry.width = width;
294 window->geometry.height = height;
295 } else if (!window->fullscreen) {
296 window->geometry = window->window_size;
297 }
298
299 if (window->native)
300 wl_egl_window_resize(window->native,
301 window->geometry.width,
302 window->geometry.height, 0, 0);
303
304 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800305}
306
307static void
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500308handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
309{
310 running = 0;
311}
312
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800313static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800314 handle_surface_configure,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500315 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300316};
317
318static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100319create_surface(struct window *window)
320{
321 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500322 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200323
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500324 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800325 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
326 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300327
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800328 xdg_surface_add_listener(window->xdg_surface,
329 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300330
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500331 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400332 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700333 window->geometry.width,
334 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500335 window->egl_surface =
336 eglCreateWindowSurface(display->egl.dpy,
337 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500338 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100339
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800340 xdg_surface_set_title(window->xdg_surface, "simple-egl");
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600341
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500342 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
343 window->egl_surface, window->display->egl.ctx);
344 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300345
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800346 if (!window->frame_sync)
347 eglSwapInterval(display->egl.dpy, 0);
348
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700349 if (window->fullscreen)
350 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100351}
352
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200353static void
354destroy_surface(struct window *window)
355{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000356 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
357 * on eglReleaseThread(). */
358 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
359 EGL_NO_CONTEXT);
360
361 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200362 wl_egl_window_destroy(window->native);
363
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800364 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200365 wl_surface_destroy(window->surface);
366
367 if (window->callback)
368 wl_callback_destroy(window->callback);
369}
370
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100371static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400372redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100373{
374 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400375 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100376 static const GLfloat verts[3][2] = {
377 { -0.5, -0.5 },
378 { 0.5, -0.5 },
379 { 0, 0.5 }
380 };
381 static const GLfloat colors[3][3] = {
382 { 1, 0, 0 },
383 { 0, 1, 0 },
384 { 0, 0, 1 }
385 };
386 GLfloat angle;
387 GLfloat rotation[4][4] = {
388 { 1, 0, 0, 0 },
389 { 0, 1, 0, 0 },
390 { 0, 0, 1, 0 },
391 { 0, 0, 0, 1 }
392 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100393 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400394 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400395 EGLint rect[4];
396 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800397 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100398
Scott Moreau7e300db2012-08-31 03:18:15 -0600399 assert(window->callback == callback);
400 window->callback = NULL;
401
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300402 if (callback)
403 wl_callback_destroy(callback);
404
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800405 gettimeofday(&tv, NULL);
406 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
407 if (window->frames == 0)
408 window->benchmark_time = time;
409 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
410 printf("%d frames in %d seconds: %f fps\n",
411 window->frames,
412 benchmark_interval,
413 (float) window->frames / benchmark_interval);
414 window->benchmark_time = time;
415 window->frames = 0;
416 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100417
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800418 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100419 rotation[0][0] = cos(angle);
420 rotation[0][2] = sin(angle);
421 rotation[2][0] = -sin(angle);
422 rotation[2][2] = cos(angle);
423
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400424 if (display->swap_buffers_with_damage)
425 eglQuerySurface(display->egl.dpy, window->egl_surface,
426 EGL_BUFFER_AGE_EXT, &buffer_age);
427
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300428 glViewport(0, 0, window->geometry.width, window->geometry.height);
429
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100430 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
431 (GLfloat *) rotation);
432
433 glClearColor(0.0, 0.0, 0.0, 0.5);
434 glClear(GL_COLOR_BUFFER_BIT);
435
436 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
437 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
438 glEnableVertexAttribArray(window->gl.pos);
439 glEnableVertexAttribArray(window->gl.col);
440
441 glDrawArrays(GL_TRIANGLES, 0, 3);
442
443 glDisableVertexAttribArray(window->gl.pos);
444 glDisableVertexAttribArray(window->gl.col);
445
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300446 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400447 region = wl_compositor_create_region(window->display->compositor);
448 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300449 window->geometry.width,
450 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400451 wl_surface_set_opaque_region(window->surface, region);
452 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700453 } else {
454 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400455 }
456
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400457 if (display->swap_buffers_with_damage && buffer_age > 0) {
458 rect[0] = window->geometry.width / 4 - 1;
459 rect[1] = window->geometry.height / 4 - 1;
460 rect[2] = window->geometry.width / 2 + 2;
461 rect[3] = window->geometry.height / 2 + 2;
462 display->swap_buffers_with_damage(display->egl.dpy,
463 window->egl_surface,
464 rect, 1);
465 } else {
466 eglSwapBuffers(display->egl.dpy, window->egl_surface);
467 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800468 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100469}
470
471static void
Daniel Stone37816df2012-05-16 18:45:18 +0100472pointer_handle_enter(void *data, struct wl_pointer *pointer,
473 uint32_t serial, struct wl_surface *surface,
474 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300475{
476 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400477 struct wl_buffer *buffer;
478 struct wl_cursor *cursor = display->default_cursor;
479 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300480
481 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300482 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400483 else if (cursor) {
484 image = display->default_cursor->images[0];
485 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100486 if (!buffer)
487 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400488 wl_pointer_set_cursor(pointer, serial,
489 display->cursor_surface,
490 image->hotspot_x,
491 image->hotspot_y);
492 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
493 wl_surface_damage(display->cursor_surface, 0, 0,
494 image->width, image->height);
495 wl_surface_commit(display->cursor_surface);
496 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300497}
498
499static void
Daniel Stone37816df2012-05-16 18:45:18 +0100500pointer_handle_leave(void *data, struct wl_pointer *pointer,
501 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300502{
503}
504
505static void
Daniel Stone37816df2012-05-16 18:45:18 +0100506pointer_handle_motion(void *data, struct wl_pointer *pointer,
507 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300508{
509}
510
511static void
Daniel Stone37816df2012-05-16 18:45:18 +0100512pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
513 uint32_t serial, uint32_t time, uint32_t button,
514 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300515{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300516 struct display *display = data;
517
518 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800519 xdg_surface_move(display->window->xdg_surface,
520 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300521}
522
523static void
Daniel Stone37816df2012-05-16 18:45:18 +0100524pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100525 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300526{
527}
528
Daniel Stone37816df2012-05-16 18:45:18 +0100529static const struct wl_pointer_listener pointer_listener = {
530 pointer_handle_enter,
531 pointer_handle_leave,
532 pointer_handle_motion,
533 pointer_handle_button,
534 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300535};
536
537static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700538touch_handle_down(void *data, struct wl_touch *wl_touch,
539 uint32_t serial, uint32_t time, struct wl_surface *surface,
540 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
541{
542 struct display *d = (struct display *)data;
543
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800544 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700545}
546
547static void
548touch_handle_up(void *data, struct wl_touch *wl_touch,
549 uint32_t serial, uint32_t time, int32_t id)
550{
551}
552
553static void
554touch_handle_motion(void *data, struct wl_touch *wl_touch,
555 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
556{
557}
558
559static void
560touch_handle_frame(void *data, struct wl_touch *wl_touch)
561{
562}
563
564static void
565touch_handle_cancel(void *data, struct wl_touch *wl_touch)
566{
567}
568
569static const struct wl_touch_listener touch_listener = {
570 touch_handle_down,
571 touch_handle_up,
572 touch_handle_motion,
573 touch_handle_frame,
574 touch_handle_cancel,
575};
576
577static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300578keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
579 uint32_t format, int fd, uint32_t size)
580{
581}
582
583static void
584keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
585 uint32_t serial, struct wl_surface *surface,
586 struct wl_array *keys)
587{
588}
589
590static void
591keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
592 uint32_t serial, struct wl_surface *surface)
593{
594}
595
596static void
597keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
598 uint32_t serial, uint32_t time, uint32_t key,
599 uint32_t state)
600{
601 struct display *d = data;
602
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700603 if (key == KEY_F11 && state) {
604 if (d->window->fullscreen)
605 xdg_surface_unset_fullscreen(d->window->xdg_surface);
606 else
607 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
608 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400609 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300610}
611
612static void
613keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
614 uint32_t serial, uint32_t mods_depressed,
615 uint32_t mods_latched, uint32_t mods_locked,
616 uint32_t group)
617{
618}
619
620static const struct wl_keyboard_listener keyboard_listener = {
621 keyboard_handle_keymap,
622 keyboard_handle_enter,
623 keyboard_handle_leave,
624 keyboard_handle_key,
625 keyboard_handle_modifiers,
626};
627
628static void
Daniel Stone37816df2012-05-16 18:45:18 +0100629seat_handle_capabilities(void *data, struct wl_seat *seat,
630 enum wl_seat_capability caps)
631{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400632 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100633
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400634 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
635 d->pointer = wl_seat_get_pointer(seat);
636 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
637 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
638 wl_pointer_destroy(d->pointer);
639 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100640 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300641
642 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
643 d->keyboard = wl_seat_get_keyboard(seat);
644 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
645 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
646 wl_keyboard_destroy(d->keyboard);
647 d->keyboard = NULL;
648 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700649
650 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
651 d->touch = wl_seat_get_touch(seat);
652 wl_touch_set_user_data(d->touch, d);
653 wl_touch_add_listener(d->touch, &touch_listener, d);
654 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
655 wl_touch_destroy(d->touch);
656 d->touch = NULL;
657 }
Daniel Stone37816df2012-05-16 18:45:18 +0100658}
659
660static const struct wl_seat_listener seat_listener = {
661 seat_handle_capabilities,
662};
663
664static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800665xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
666{
667 xdg_shell_pong(shell, serial);
668}
669
670static const struct xdg_shell_listener xdg_shell_listener = {
671 xdg_shell_ping,
672};
673
Pekka Paalanen71182ae2014-08-21 17:47:20 +0300674#define XDG_VERSION 4 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800675#ifdef static_assert
676static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
677 "Interface version doesn't match implementation version");
678#endif
679
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800680static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400681registry_handle_global(void *data, struct wl_registry *registry,
682 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100683{
684 struct display *d = data;
685
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400686 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400687 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400688 wl_registry_bind(registry, name,
689 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800690 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400691 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800692 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800693 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800694 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100695 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400696 d->seat = wl_registry_bind(registry, name,
697 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400698 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400699 } else if (strcmp(interface, "wl_shm") == 0) {
700 d->shm = wl_registry_bind(registry, name,
701 &wl_shm_interface, 1);
702 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100703 if (!d->cursor_theme) {
704 fprintf(stderr, "unable to load default theme\n");
705 return;
706 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400707 d->default_cursor =
708 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100709 if (!d->default_cursor) {
710 fprintf(stderr, "unable to load default left pointer\n");
711 // TODO: abort ?
712 }
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400713 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100714}
715
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200716static void
717registry_handle_global_remove(void *data, struct wl_registry *registry,
718 uint32_t name)
719{
720}
721
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400722static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200723 registry_handle_global,
724 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400725};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100726
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200727static void
728signal_int(int signum)
729{
730 running = 0;
731}
732
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400733static void
734usage(int error_code)
735{
736 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
737 " -f\tRun in fullscreen mode\n"
738 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700739 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800740 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400741 " -h\tThis help text\n\n");
742
743 exit(error_code);
744}
745
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100746int
747main(int argc, char **argv)
748{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200749 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100750 struct display display = { 0 };
751 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400752 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100753
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100754 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300755 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700756 window.geometry.width = 250;
757 window.geometry.height = 250;
758 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700759 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800760 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100761
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400762 for (i = 1; i < argc; i++) {
763 if (strcmp("-f", argv[i]) == 0)
764 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400765 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300766 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700767 else if (strcmp("-s", argv[i]) == 0)
768 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800769 else if (strcmp("-b", argv[i]) == 0)
770 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400771 else if (strcmp("-h", argv[i]) == 0)
772 usage(EXIT_SUCCESS);
773 else
774 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400775 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300776
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100777 display.display = wl_display_connect(NULL);
778 assert(display.display);
779
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400780 display.registry = wl_display_get_registry(display.display);
781 wl_registry_add_listener(display.registry,
782 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100783
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400784 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200785
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700786 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100787 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500788 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100789
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400790 display.cursor_surface =
791 wl_compositor_create_surface(display.compositor);
792
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200793 sigint.sa_handler = signal_int;
794 sigemptyset(&sigint.sa_mask);
795 sigint.sa_flags = SA_RESETHAND;
796 sigaction(SIGINT, &sigint, NULL);
797
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800798 /* The mainloop here is a little subtle. Redrawing will cause
799 * EGL to read events so we can just call
800 * wl_display_dispatch_pending() to handle any events that got
801 * queued up as a side effect. */
802 while (running && ret != -1) {
803 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800804 redraw(&window, NULL, 0);
805 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500806
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200807 fprintf(stderr, "simple-egl exiting\n");
808
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200809 destroy_surface(&window);
810 fini_egl(&display);
811
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400812 wl_surface_destroy(display.cursor_surface);
813 if (display.cursor_theme)
814 wl_cursor_theme_destroy(display.cursor_theme);
815
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200816 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800817 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200818
819 if (display.compositor)
820 wl_compositor_destroy(display.compositor);
821
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200822 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200823 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500824 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200825
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100826 return 0;
827}