blob: 3db0e64021f4f4b46591c861fad8e474a54263d4 [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
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010026#include <stdbool.h>
27#include <math.h>
28#include <assert.h>
Pekka Paalanen88e60fc2011-12-13 12:09:09 +020029#include <signal.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010030
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +030031#include <linux/input.h>
32
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010033#include <wayland-client.h>
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050034#include <wayland-egl.h>
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040035#include <wayland-cursor.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010036
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010037#include <GLES2/gl2.h>
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010038#include <EGL/egl.h>
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040039#include <EGL/eglext.h>
40
41#ifndef EGL_EXT_swap_buffers_with_damage
42#define EGL_EXT_swap_buffers_with_damage 1
43typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
44#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010045
Kristian Høgsberg77787432013-08-22 12:16:51 -070046#ifndef EGL_EXT_buffer_age
47#define EGL_EXT_buffer_age 1
48#define EGL_BUFFER_AGE_EXT 0x313D
49#endif
50
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030051struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010052struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030053
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010054struct display {
55 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040056 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050057 struct wl_compositor *compositor;
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -040058 struct wl_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040059 struct wl_seat *seat;
60 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070061 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030062 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040063 struct wl_shm *shm;
64 struct wl_cursor_theme *cursor_theme;
65 struct wl_cursor *default_cursor;
66 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010067 struct {
68 EGLDisplay dpy;
69 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050070 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010071 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030072 struct window *window;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040073
74 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010075};
76
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030077struct geometry {
78 int width, height;
79};
80
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010081struct window {
82 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060083 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010084 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010085 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086 GLuint pos;
87 GLuint col;
88 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050089
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080090 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050091 struct wl_egl_window *native;
92 struct wl_surface *surface;
Pekka Paalanen9d1613e2011-11-25 12:09:16 +020093 struct wl_shell_surface *shell_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050094 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020095 struct wl_callback *callback;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -070096 int fullscreen, configured, opaque, buffer_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010097};
98
99static 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"
104 "void main() {\n"
105 " gl_Position = rotation * pos;\n"
106 " v_color = color;\n"
107 "}\n";
108
109static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500110 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100111 "varying vec4 v_color;\n"
112 "void main() {\n"
113 " gl_FragColor = v_color;\n"
114 "}\n";
115
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400116static int running = 1;
117
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100118static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700119init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100120{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500121 static const EGLint context_attribs[] = {
122 EGL_CONTEXT_CLIENT_VERSION, 2,
123 EGL_NONE
124 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400125 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500126
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300127 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500128 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500129 EGL_RED_SIZE, 1,
130 EGL_GREEN_SIZE, 1,
131 EGL_BLUE_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500132 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
133 EGL_NONE
134 };
135
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700136 EGLint major, minor, n, count, i, size;
137 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100138 EGLBoolean ret;
139
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700140 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400141 config_attribs[9] = 0;
142
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400143 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100144 assert(display->egl.dpy);
145
146 ret = eglInitialize(display->egl.dpy, &major, &minor);
147 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500148 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100149 assert(ret == EGL_TRUE);
150
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700151 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
152 assert(0);
153
154 configs = calloc(count, sizeof *configs);
155 assert(configs);
156
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300157 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700158 configs, count, &n);
159 assert(ret && n >= 1);
160
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];
166 break;
167 }
168 }
169 free(configs);
170 if (display->egl.conf == NULL) {
171 fprintf(stderr, "did not find config with buffer size %d\n",
172 window->buffer_size);
173 exit(EXIT_FAILURE);
174 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500175
176 display->egl.ctx = eglCreateContext(display->egl.dpy,
177 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500178 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100179 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500180
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400181 display->swap_buffers_with_damage = NULL;
182 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
183 if (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");
189
190 if (display->swap_buffers_with_damage)
191 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
192
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100193}
194
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200195static void
196fini_egl(struct display *display)
197{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200198 eglTerminate(display->egl.dpy);
199 eglReleaseThread();
200}
201
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100202static GLuint
203create_shader(struct window *window, const char *source, GLenum shader_type)
204{
205 GLuint shader;
206 GLint status;
207
208 shader = glCreateShader(shader_type);
209 assert(shader != 0);
210
211 glShaderSource(shader, 1, (const char **) &source, NULL);
212 glCompileShader(shader);
213
214 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
215 if (!status) {
216 char log[1000];
217 GLsizei len;
218 glGetShaderInfoLog(shader, 1000, &len, log);
219 fprintf(stderr, "Error: compiling %s: %*s\n",
220 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
221 len, log);
222 exit(1);
223 }
224
225 return shader;
226}
227
228static void
229init_gl(struct window *window)
230{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100231 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600232 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100233 GLint status;
234
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100235 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
236 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
237
Scott Moreau3ea23d02012-06-13 17:42:21 -0600238 program = glCreateProgram();
239 glAttachShader(program, frag);
240 glAttachShader(program, vert);
241 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100242
Scott Moreau3ea23d02012-06-13 17:42:21 -0600243 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100244 if (!status) {
245 char log[1000];
246 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600247 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100248 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
249 exit(1);
250 }
251
Scott Moreau3ea23d02012-06-13 17:42:21 -0600252 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100253
254 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600255 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100256
Scott Moreau3ea23d02012-06-13 17:42:21 -0600257 glBindAttribLocation(program, window->gl.pos, "pos");
258 glBindAttribLocation(program, window->gl.col, "color");
259 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100260
261 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600262 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100263}
264
265static void
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300266handle_ping(void *data, struct wl_shell_surface *shell_surface,
267 uint32_t serial)
268{
269 wl_shell_surface_pong(shell_surface, serial);
270}
271
272static void
273handle_configure(void *data, struct wl_shell_surface *shell_surface,
274 uint32_t edges, int32_t width, int32_t height)
275{
276 struct window *window = data;
277
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300278 if (window->native)
279 wl_egl_window_resize(window->native, width, height, 0, 0);
280
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300281 window->geometry.width = width;
282 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300283
Scott Moreau1ee53e72012-08-30 14:44:15 -0600284 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300285 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300286}
287
288static void
289handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
290{
291}
292
293static const struct wl_shell_surface_listener shell_surface_listener = {
294 handle_ping,
295 handle_configure,
296 handle_popup_done
297};
298
299static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300300redraw(void *data, struct wl_callback *callback, uint32_t time);
301
302static void
303configure_callback(void *data, struct wl_callback *callback, uint32_t time)
304{
305 struct window *window = data;
306
307 wl_callback_destroy(callback);
308
309 window->configured = 1;
Scott Moreau7e300db2012-08-31 03:18:15 -0600310
311 if (window->callback == NULL)
312 redraw(data, NULL, time);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300313}
314
315static struct wl_callback_listener configure_callback_listener = {
316 configure_callback,
317};
318
319static void
320toggle_fullscreen(struct window *window, int fullscreen)
321{
322 struct wl_callback *callback;
323
324 window->fullscreen = fullscreen;
325 window->configured = 0;
326
327 if (fullscreen) {
328 wl_shell_surface_set_fullscreen(window->shell_surface,
329 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
330 0, NULL);
331 } else {
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);
336 }
337
338 callback = wl_display_sync(window->display->display);
339 wl_callback_add_listener(callback, &configure_callback_listener,
340 window);
341}
342
343static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100344create_surface(struct window *window)
345{
346 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500347 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200348
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500349 window->surface = wl_compositor_create_surface(display->compositor);
Pekka Paalanen9d1613e2011-11-25 12:09:16 +0200350 window->shell_surface = wl_shell_get_shell_surface(display->shell,
351 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300352
353 wl_shell_surface_add_listener(window->shell_surface,
354 &shell_surface_listener, window);
355
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500356 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400357 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300358 window->window_size.width,
359 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500360 window->egl_surface =
361 eglCreateWindowSurface(display->egl.dpy,
362 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500363 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100364
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600365 wl_shell_surface_set_title(window->shell_surface, "simple-egl");
366
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500367 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
368 window->egl_surface, window->display->egl.ctx);
369 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300370
371 toggle_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100372}
373
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200374static void
375destroy_surface(struct window *window)
376{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000377 /* 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,
380 EGL_NO_CONTEXT);
381
382 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200383 wl_egl_window_destroy(window->native);
384
385 wl_shell_surface_destroy(window->shell_surface);
386 wl_surface_destroy(window->surface);
387
388 if (window->callback)
389 wl_callback_destroy(window->callback);
390}
391
Kristian Høgsberg33418202011-08-16 23:01:28 -0400392static const struct wl_callback_listener frame_listener;
393
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100394static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400395redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100396{
397 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400398 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100399 static const GLfloat verts[3][2] = {
400 { -0.5, -0.5 },
401 { 0.5, -0.5 },
402 { 0, 0.5 }
403 };
404 static const GLfloat colors[3][3] = {
405 { 1, 0, 0 },
406 { 0, 1, 0 },
407 { 0, 0, 1 }
408 };
409 GLfloat angle;
410 GLfloat rotation[4][4] = {
411 { 1, 0, 0, 0 },
412 { 0, 1, 0, 0 },
413 { 0, 0, 1, 0 },
414 { 0, 0, 0, 1 }
415 };
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800416 static const int32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400417 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400418 EGLint rect[4];
419 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800420 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100421
Scott Moreau7e300db2012-08-31 03:18:15 -0600422 assert(window->callback == callback);
423 window->callback = NULL;
424
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300425 if (callback)
426 wl_callback_destroy(callback);
427
428 if (!window->configured)
429 return;
430
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800431 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",
437 window->frames,
438 benchmark_interval,
439 (float) window->frames / benchmark_interval);
440 window->benchmark_time = time;
441 window->frames = 0;
442 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100443
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800444 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100445 rotation[0][0] = cos(angle);
446 rotation[0][2] = sin(angle);
447 rotation[2][0] = -sin(angle);
448 rotation[2][2] = cos(angle);
449
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400450 if (display->swap_buffers_with_damage)
451 eglQuerySurface(display->egl.dpy, window->egl_surface,
452 EGL_BUFFER_AGE_EXT, &buffer_age);
453
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300454 glViewport(0, 0, window->geometry.width, window->geometry.height);
455
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100456 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
457 (GLfloat *) rotation);
458
459 glClearColor(0.0, 0.0, 0.0, 0.5);
460 glClear(GL_COLOR_BUFFER_BIT);
461
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);
466
467 glDrawArrays(GL_TRIANGLES, 0, 3);
468
469 glDisableVertexAttribArray(window->gl.pos);
470 glDisableVertexAttribArray(window->gl.col);
471
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300472 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400473 region = wl_compositor_create_region(window->display->compositor);
474 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300475 window->geometry.width,
476 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400477 wl_surface_set_opaque_region(window->surface, region);
478 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700479 } else {
480 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400481 }
482
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200483 window->callback = wl_surface_frame(window->surface);
484 wl_callback_add_listener(window->callback, &frame_listener, window);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300485
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400486 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,
492 window->egl_surface,
493 rect, 1);
494 } else {
495 eglSwapBuffers(display->egl.dpy, window->egl_surface);
496 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800497 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100498}
499
Kristian Høgsberg33418202011-08-16 23:01:28 -0400500static const struct wl_callback_listener frame_listener = {
501 redraw
502};
503
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100504static void
Daniel Stone37816df2012-05-16 18:45:18 +0100505pointer_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)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300508{
509 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400510 struct wl_buffer *buffer;
511 struct wl_cursor *cursor = display->default_cursor;
512 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300513
514 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300515 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400516 else if (cursor) {
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,
521 image->hotspot_x,
522 image->hotspot_y);
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);
527 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300528}
529
530static void
Daniel Stone37816df2012-05-16 18:45:18 +0100531pointer_handle_leave(void *data, struct wl_pointer *pointer,
532 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300533{
534}
535
536static void
Daniel Stone37816df2012-05-16 18:45:18 +0100537pointer_handle_motion(void *data, struct wl_pointer *pointer,
538 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300539{
540}
541
542static void
Daniel Stone37816df2012-05-16 18:45:18 +0100543pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
544 uint32_t serial, uint32_t time, uint32_t button,
545 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300546{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300547 struct display *display = data;
548
549 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
550 wl_shell_surface_move(display->window->shell_surface,
551 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300552}
553
554static void
Daniel Stone37816df2012-05-16 18:45:18 +0100555pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100556 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300557{
558}
559
Daniel Stone37816df2012-05-16 18:45:18 +0100560static const struct wl_pointer_listener pointer_listener = {
561 pointer_handle_enter,
562 pointer_handle_leave,
563 pointer_handle_motion,
564 pointer_handle_button,
565 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300566};
567
568static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700569touch_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)
572{
573 struct display *d = (struct display *)data;
574
575 wl_shell_surface_move(d->window->shell_surface, d->seat, serial);
576}
577
578static void
579touch_handle_up(void *data, struct wl_touch *wl_touch,
580 uint32_t serial, uint32_t time, int32_t id)
581{
582}
583
584static void
585touch_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)
587{
588}
589
590static void
591touch_handle_frame(void *data, struct wl_touch *wl_touch)
592{
593}
594
595static void
596touch_handle_cancel(void *data, struct wl_touch *wl_touch)
597{
598}
599
600static const struct wl_touch_listener touch_listener = {
601 touch_handle_down,
602 touch_handle_up,
603 touch_handle_motion,
604 touch_handle_frame,
605 touch_handle_cancel,
606};
607
608static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300609keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
610 uint32_t format, int fd, uint32_t size)
611{
612}
613
614static void
615keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
616 uint32_t serial, struct wl_surface *surface,
617 struct wl_array *keys)
618{
619}
620
621static void
622keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
623 uint32_t serial, struct wl_surface *surface)
624{
625}
626
627static void
628keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
629 uint32_t serial, uint32_t time, uint32_t key,
630 uint32_t state)
631{
632 struct display *d = data;
633
634 if (key == KEY_F11 && state)
635 toggle_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400636 else if (key == KEY_ESC && state)
637 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300638}
639
640static void
641keyboard_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,
644 uint32_t group)
645{
646}
647
648static const struct wl_keyboard_listener keyboard_listener = {
649 keyboard_handle_keymap,
650 keyboard_handle_enter,
651 keyboard_handle_leave,
652 keyboard_handle_key,
653 keyboard_handle_modifiers,
654};
655
656static void
Daniel Stone37816df2012-05-16 18:45:18 +0100657seat_handle_capabilities(void *data, struct wl_seat *seat,
658 enum wl_seat_capability caps)
659{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400660 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100661
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400662 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);
667 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100668 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300669
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);
675 d->keyboard = NULL;
676 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700677
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);
684 d->touch = NULL;
685 }
Daniel Stone37816df2012-05-16 18:45:18 +0100686}
687
688static const struct wl_seat_listener seat_listener = {
689 seat_handle_capabilities,
690};
691
692static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400693registry_handle_global(void *data, struct wl_registry *registry,
694 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100695{
696 struct display *d = data;
697
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400698 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400699 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400700 wl_registry_bind(registry, name,
701 &wl_compositor_interface, 1);
Kristian Høgsberg7a5c9792011-06-18 06:12:54 -0400702 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400703 d->shell = wl_registry_bind(registry, name,
704 &wl_shell_interface, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100705 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400706 d->seat = wl_registry_bind(registry, name,
707 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400708 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400709 } 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);
713 d->default_cursor =
714 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400715 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100716}
717
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200718static void
719registry_handle_global_remove(void *data, struct wl_registry *registry,
720 uint32_t name)
721{
722}
723
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400724static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200725 registry_handle_global,
726 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400727};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100728
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200729static void
730signal_int(int signum)
731{
732 running = 0;
733}
734
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400735static void
736usage(int error_code)
737{
738 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
739 " -f\tRun in fullscreen mode\n"
740 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700741 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400742 " -h\tThis help text\n\n");
743
744 exit(error_code);
745}
746
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100747int
748main(int argc, char **argv)
749{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200750 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100751 struct display display = { 0 };
752 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400753 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100754
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100755 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300756 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300757 window.window_size.width = 250;
758 window.window_size.height = 250;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700759 window.buffer_size = 32;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100760
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400761 for (i = 1; i < argc; i++) {
762 if (strcmp("-f", argv[i]) == 0)
763 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400764 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300765 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700766 else if (strcmp("-s", argv[i]) == 0)
767 window.buffer_size = 16;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400768 else if (strcmp("-h", argv[i]) == 0)
769 usage(EXIT_SUCCESS);
770 else
771 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400772 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300773
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100774 display.display = wl_display_connect(NULL);
775 assert(display.display);
776
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400777 display.registry = wl_display_get_registry(display.display);
778 wl_registry_add_listener(display.registry,
779 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100780
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400781 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200782
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700783 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100784 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500785 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100786
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400787 display.cursor_surface =
788 wl_compositor_create_surface(display.compositor);
789
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200790 sigint.sa_handler = signal_int;
791 sigemptyset(&sigint.sa_mask);
792 sigint.sa_flags = SA_RESETHAND;
793 sigaction(SIGINT, &sigint, NULL);
794
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400795 while (running && ret != -1)
796 ret = wl_display_dispatch(display.display);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500797
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200798 fprintf(stderr, "simple-egl exiting\n");
799
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200800 destroy_surface(&window);
801 fini_egl(&display);
802
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400803 wl_surface_destroy(display.cursor_surface);
804 if (display.cursor_theme)
805 wl_cursor_theme_destroy(display.cursor_theme);
806
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200807 if (display.shell)
808 wl_shell_destroy(display.shell);
809
810 if (display.compositor)
811 wl_compositor_destroy(display.compositor);
812
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200813 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200814 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500815 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200816
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100817 return 0;
818}