blob: 165ce10a32e02241adca97a4538f644803cc830b [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,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500136 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
137 EGL_NONE
138 };
139
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700140 EGLint major, minor, n, count, i, size;
141 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100142 EGLBoolean ret;
143
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700144 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400145 config_attribs[9] = 0;
146
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400147 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100148 assert(display->egl.dpy);
149
150 ret = eglInitialize(display->egl.dpy, &major, &minor);
151 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500152 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100153 assert(ret == EGL_TRUE);
154
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700155 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
156 assert(0);
157
158 configs = calloc(count, sizeof *configs);
159 assert(configs);
160
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300161 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700162 configs, count, &n);
163 assert(ret && n >= 1);
164
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];
170 break;
171 }
172 }
173 free(configs);
174 if (display->egl.conf == NULL) {
175 fprintf(stderr, "did not find config with buffer size %d\n",
176 window->buffer_size);
177 exit(EXIT_FAILURE);
178 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500179
180 display->egl.ctx = eglCreateContext(display->egl.dpy,
181 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500182 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100183 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500184
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400185 display->swap_buffers_with_damage = NULL;
186 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
187 if (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");
193
194 if (display->swap_buffers_with_damage)
195 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
196
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100197}
198
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200199static void
200fini_egl(struct display *display)
201{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200202 eglTerminate(display->egl.dpy);
203 eglReleaseThread();
204}
205
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100206static GLuint
207create_shader(struct window *window, const char *source, GLenum shader_type)
208{
209 GLuint shader;
210 GLint status;
211
212 shader = glCreateShader(shader_type);
213 assert(shader != 0);
214
215 glShaderSource(shader, 1, (const char **) &source, NULL);
216 glCompileShader(shader);
217
218 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
219 if (!status) {
220 char log[1000];
221 GLsizei len;
222 glGetShaderInfoLog(shader, 1000, &len, log);
223 fprintf(stderr, "Error: compiling %s: %*s\n",
224 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
225 len, log);
226 exit(1);
227 }
228
229 return shader;
230}
231
232static void
233init_gl(struct window *window)
234{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100235 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600236 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100237 GLint status;
238
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100239 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
240 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
241
Scott Moreau3ea23d02012-06-13 17:42:21 -0600242 program = glCreateProgram();
243 glAttachShader(program, frag);
244 glAttachShader(program, vert);
245 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100246
Scott Moreau3ea23d02012-06-13 17:42:21 -0600247 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100248 if (!status) {
249 char log[1000];
250 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600251 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100252 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
253 exit(1);
254 }
255
Scott Moreau3ea23d02012-06-13 17:42:21 -0600256 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100257
258 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600259 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100260
Scott Moreau3ea23d02012-06-13 17:42:21 -0600261 glBindAttribLocation(program, window->gl.pos, "pos");
262 glBindAttribLocation(program, window->gl.col, "color");
263 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100264
265 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600266 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100267}
268
269static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800270handle_surface_configure(void *data, struct xdg_surface *surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700271 int32_t width, int32_t height,
272 struct wl_array *states, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300273{
274 struct window *window = data;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700275 uint32_t *p;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300276
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700277 window->fullscreen = 0;
278 wl_array_for_each(p, states) {
279 uint32_t state = *p;
280 switch (state) {
281 case XDG_SURFACE_STATE_FULLSCREEN:
282 window->fullscreen = 1;
283 break;
284 }
Jasper St. Pierre8c6aa452014-02-08 18:29:49 -0500285 }
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800286
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700287 if (width > 0 && height > 0) {
288 if (!window->fullscreen) {
289 window->window_size.width = width;
290 window->window_size.height = height;
291 }
292 window->geometry.width = width;
293 window->geometry.height = height;
294 } else if (!window->fullscreen) {
295 window->geometry = window->window_size;
296 }
297
298 if (window->native)
299 wl_egl_window_resize(window->native,
300 window->geometry.width,
301 window->geometry.height, 0, 0);
302
303 xdg_surface_ack_configure(surface, serial);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800304}
305
306static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -0500307handle_surface_activated(void *data, struct xdg_surface *xdg_surface)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800308{
309}
310
311static void
Jasper St. Pierreb223a722014-02-08 18:11:53 -0500312handle_surface_deactivated(void *data, struct xdg_surface *xdg_surface)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800313{
314}
315
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500316static void
317handle_surface_delete(void *data, struct xdg_surface *xdg_surface)
318{
319 running = 0;
320}
321
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800322static const struct xdg_surface_listener xdg_surface_listener = {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800323 handle_surface_configure,
Jasper St. Pierreb223a722014-02-08 18:11:53 -0500324 handle_surface_activated,
325 handle_surface_deactivated,
Jasper St. Pierrea0d8a302014-02-08 18:31:10 -0500326 handle_surface_delete,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300327};
328
329static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100330create_surface(struct window *window)
331{
332 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500333 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200334
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500335 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800336 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
337 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300338
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800339 xdg_surface_add_listener(window->xdg_surface,
340 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300341
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500342 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400343 wl_egl_window_create(window->surface,
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700344 window->geometry.width,
345 window->geometry.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500346 window->egl_surface =
347 eglCreateWindowSurface(display->egl.dpy,
348 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500349 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100350
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800351 xdg_surface_set_title(window->xdg_surface, "simple-egl");
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600352
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500353 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
354 window->egl_surface, window->display->egl.ctx);
355 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300356
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800357 if (!window->frame_sync)
358 eglSwapInterval(display->egl.dpy, 0);
359
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700360 if (window->fullscreen)
361 xdg_surface_set_fullscreen(window->xdg_surface, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100362}
363
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200364static void
365destroy_surface(struct window *window)
366{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000367 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
368 * on eglReleaseThread(). */
369 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
370 EGL_NO_CONTEXT);
371
372 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200373 wl_egl_window_destroy(window->native);
374
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800375 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200376 wl_surface_destroy(window->surface);
377
378 if (window->callback)
379 wl_callback_destroy(window->callback);
380}
381
Kristian Høgsberg33418202011-08-16 23:01:28 -0400382static const struct wl_callback_listener frame_listener;
383
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100384static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400385redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100386{
387 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400388 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100389 static const GLfloat verts[3][2] = {
390 { -0.5, -0.5 },
391 { 0.5, -0.5 },
392 { 0, 0.5 }
393 };
394 static const GLfloat colors[3][3] = {
395 { 1, 0, 0 },
396 { 0, 1, 0 },
397 { 0, 0, 1 }
398 };
399 GLfloat angle;
400 GLfloat rotation[4][4] = {
401 { 1, 0, 0, 0 },
402 { 0, 1, 0, 0 },
403 { 0, 0, 1, 0 },
404 { 0, 0, 0, 1 }
405 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100406 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400407 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400408 EGLint rect[4];
409 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800410 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100411
Scott Moreau7e300db2012-08-31 03:18:15 -0600412 assert(window->callback == callback);
413 window->callback = NULL;
414
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300415 if (callback)
416 wl_callback_destroy(callback);
417
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800418 gettimeofday(&tv, NULL);
419 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
420 if (window->frames == 0)
421 window->benchmark_time = time;
422 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
423 printf("%d frames in %d seconds: %f fps\n",
424 window->frames,
425 benchmark_interval,
426 (float) window->frames / benchmark_interval);
427 window->benchmark_time = time;
428 window->frames = 0;
429 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100430
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800431 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100432 rotation[0][0] = cos(angle);
433 rotation[0][2] = sin(angle);
434 rotation[2][0] = -sin(angle);
435 rotation[2][2] = cos(angle);
436
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400437 if (display->swap_buffers_with_damage)
438 eglQuerySurface(display->egl.dpy, window->egl_surface,
439 EGL_BUFFER_AGE_EXT, &buffer_age);
440
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300441 glViewport(0, 0, window->geometry.width, window->geometry.height);
442
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100443 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
444 (GLfloat *) rotation);
445
446 glClearColor(0.0, 0.0, 0.0, 0.5);
447 glClear(GL_COLOR_BUFFER_BIT);
448
449 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
450 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
451 glEnableVertexAttribArray(window->gl.pos);
452 glEnableVertexAttribArray(window->gl.col);
453
454 glDrawArrays(GL_TRIANGLES, 0, 3);
455
456 glDisableVertexAttribArray(window->gl.pos);
457 glDisableVertexAttribArray(window->gl.col);
458
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300459 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400460 region = wl_compositor_create_region(window->display->compositor);
461 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300462 window->geometry.width,
463 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400464 wl_surface_set_opaque_region(window->surface, region);
465 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700466 } else {
467 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400468 }
469
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400470 if (display->swap_buffers_with_damage && buffer_age > 0) {
471 rect[0] = window->geometry.width / 4 - 1;
472 rect[1] = window->geometry.height / 4 - 1;
473 rect[2] = window->geometry.width / 2 + 2;
474 rect[3] = window->geometry.height / 2 + 2;
475 display->swap_buffers_with_damage(display->egl.dpy,
476 window->egl_surface,
477 rect, 1);
478 } else {
479 eglSwapBuffers(display->egl.dpy, window->egl_surface);
480 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800481 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100482}
483
Kristian Høgsberg33418202011-08-16 23:01:28 -0400484static const struct wl_callback_listener frame_listener = {
485 redraw
486};
487
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100488static void
Daniel Stone37816df2012-05-16 18:45:18 +0100489pointer_handle_enter(void *data, struct wl_pointer *pointer,
490 uint32_t serial, struct wl_surface *surface,
491 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300492{
493 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400494 struct wl_buffer *buffer;
495 struct wl_cursor *cursor = display->default_cursor;
496 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300497
498 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300499 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400500 else if (cursor) {
501 image = display->default_cursor->images[0];
502 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100503 if (!buffer)
504 return;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400505 wl_pointer_set_cursor(pointer, serial,
506 display->cursor_surface,
507 image->hotspot_x,
508 image->hotspot_y);
509 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
510 wl_surface_damage(display->cursor_surface, 0, 0,
511 image->width, image->height);
512 wl_surface_commit(display->cursor_surface);
513 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300514}
515
516static void
Daniel Stone37816df2012-05-16 18:45:18 +0100517pointer_handle_leave(void *data, struct wl_pointer *pointer,
518 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300519{
520}
521
522static void
Daniel Stone37816df2012-05-16 18:45:18 +0100523pointer_handle_motion(void *data, struct wl_pointer *pointer,
524 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300525{
526}
527
528static void
Daniel Stone37816df2012-05-16 18:45:18 +0100529pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
530 uint32_t serial, uint32_t time, uint32_t button,
531 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300532{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300533 struct display *display = data;
534
535 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800536 xdg_surface_move(display->window->xdg_surface,
537 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300538}
539
540static void
Daniel Stone37816df2012-05-16 18:45:18 +0100541pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100542 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300543{
544}
545
Daniel Stone37816df2012-05-16 18:45:18 +0100546static const struct wl_pointer_listener pointer_listener = {
547 pointer_handle_enter,
548 pointer_handle_leave,
549 pointer_handle_motion,
550 pointer_handle_button,
551 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300552};
553
554static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700555touch_handle_down(void *data, struct wl_touch *wl_touch,
556 uint32_t serial, uint32_t time, struct wl_surface *surface,
557 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
558{
559 struct display *d = (struct display *)data;
560
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800561 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700562}
563
564static void
565touch_handle_up(void *data, struct wl_touch *wl_touch,
566 uint32_t serial, uint32_t time, int32_t id)
567{
568}
569
570static void
571touch_handle_motion(void *data, struct wl_touch *wl_touch,
572 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
573{
574}
575
576static void
577touch_handle_frame(void *data, struct wl_touch *wl_touch)
578{
579}
580
581static void
582touch_handle_cancel(void *data, struct wl_touch *wl_touch)
583{
584}
585
586static const struct wl_touch_listener touch_listener = {
587 touch_handle_down,
588 touch_handle_up,
589 touch_handle_motion,
590 touch_handle_frame,
591 touch_handle_cancel,
592};
593
594static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300595keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
596 uint32_t format, int fd, uint32_t size)
597{
598}
599
600static void
601keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
602 uint32_t serial, struct wl_surface *surface,
603 struct wl_array *keys)
604{
605}
606
607static void
608keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
609 uint32_t serial, struct wl_surface *surface)
610{
611}
612
613static void
614keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
615 uint32_t serial, uint32_t time, uint32_t key,
616 uint32_t state)
617{
618 struct display *d = data;
619
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700620 if (key == KEY_F11 && state) {
621 if (d->window->fullscreen)
622 xdg_surface_unset_fullscreen(d->window->xdg_surface);
623 else
624 xdg_surface_set_fullscreen(d->window->xdg_surface, NULL);
625 } else if (key == KEY_ESC && state)
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400626 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300627}
628
629static void
630keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
631 uint32_t serial, uint32_t mods_depressed,
632 uint32_t mods_latched, uint32_t mods_locked,
633 uint32_t group)
634{
635}
636
637static const struct wl_keyboard_listener keyboard_listener = {
638 keyboard_handle_keymap,
639 keyboard_handle_enter,
640 keyboard_handle_leave,
641 keyboard_handle_key,
642 keyboard_handle_modifiers,
643};
644
645static void
Daniel Stone37816df2012-05-16 18:45:18 +0100646seat_handle_capabilities(void *data, struct wl_seat *seat,
647 enum wl_seat_capability caps)
648{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400649 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100650
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400651 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
652 d->pointer = wl_seat_get_pointer(seat);
653 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
654 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
655 wl_pointer_destroy(d->pointer);
656 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100657 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300658
659 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
660 d->keyboard = wl_seat_get_keyboard(seat);
661 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
662 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
663 wl_keyboard_destroy(d->keyboard);
664 d->keyboard = NULL;
665 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700666
667 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
668 d->touch = wl_seat_get_touch(seat);
669 wl_touch_set_user_data(d->touch, d);
670 wl_touch_add_listener(d->touch, &touch_listener, d);
671 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
672 wl_touch_destroy(d->touch);
673 d->touch = NULL;
674 }
Daniel Stone37816df2012-05-16 18:45:18 +0100675}
676
677static const struct wl_seat_listener seat_listener = {
678 seat_handle_capabilities,
679};
680
681static void
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800682xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
683{
684 xdg_shell_pong(shell, serial);
685}
686
687static const struct xdg_shell_listener xdg_shell_listener = {
688 xdg_shell_ping,
689};
690
Kristian Høgsbergc7680b02014-02-19 10:14:46 -0800691#define XDG_VERSION 3 /* The version of xdg-shell that we implement */
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800692#ifdef static_assert
693static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
694 "Interface version doesn't match implementation version");
695#endif
696
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800697static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400698registry_handle_global(void *data, struct wl_registry *registry,
699 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100700{
701 struct display *d = data;
702
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400703 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400704 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400705 wl_registry_bind(registry, name,
706 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800707 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400708 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800709 &xdg_shell_interface, 1);
Kristian Høgsberg2bff94e2014-02-11 12:22:51 -0800710 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Kristian Høgsberg239902b2014-02-11 13:50:08 -0800711 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
Daniel Stone37816df2012-05-16 18:45:18 +0100712 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400713 d->seat = wl_registry_bind(registry, name,
714 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400715 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400716 } else if (strcmp(interface, "wl_shm") == 0) {
717 d->shm = wl_registry_bind(registry, name,
718 &wl_shm_interface, 1);
719 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
Hardening842a36a2014-03-18 14:12:50 +0100720 if (!d->cursor_theme) {
721 fprintf(stderr, "unable to load default theme\n");
722 return;
723 }
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400724 d->default_cursor =
725 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Hardening842a36a2014-03-18 14:12:50 +0100726 if (!d->default_cursor) {
727 fprintf(stderr, "unable to load default left pointer\n");
728 // TODO: abort ?
729 }
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400730 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100731}
732
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200733static void
734registry_handle_global_remove(void *data, struct wl_registry *registry,
735 uint32_t name)
736{
737}
738
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400739static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200740 registry_handle_global,
741 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400742};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100743
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200744static void
745signal_int(int signum)
746{
747 running = 0;
748}
749
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400750static void
751usage(int error_code)
752{
753 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
754 " -f\tRun in fullscreen mode\n"
755 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700756 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800757 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400758 " -h\tThis help text\n\n");
759
760 exit(error_code);
761}
762
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100763int
764main(int argc, char **argv)
765{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200766 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100767 struct display display = { 0 };
768 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400769 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100770
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100771 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300772 display.window = &window;
Jasper St. Pierreab2c1082014-04-10 10:41:46 -0700773 window.geometry.width = 250;
774 window.geometry.height = 250;
775 window.window_size = window.geometry;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700776 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800777 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100778
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400779 for (i = 1; i < argc; i++) {
780 if (strcmp("-f", argv[i]) == 0)
781 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400782 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300783 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700784 else if (strcmp("-s", argv[i]) == 0)
785 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800786 else if (strcmp("-b", argv[i]) == 0)
787 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400788 else if (strcmp("-h", argv[i]) == 0)
789 usage(EXIT_SUCCESS);
790 else
791 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400792 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300793
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100794 display.display = wl_display_connect(NULL);
795 assert(display.display);
796
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400797 display.registry = wl_display_get_registry(display.display);
798 wl_registry_add_listener(display.registry,
799 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100800
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400801 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200802
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700803 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100804 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500805 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100806
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400807 display.cursor_surface =
808 wl_compositor_create_surface(display.compositor);
809
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200810 sigint.sa_handler = signal_int;
811 sigemptyset(&sigint.sa_mask);
812 sigint.sa_flags = SA_RESETHAND;
813 sigaction(SIGINT, &sigint, NULL);
814
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800815 /* The mainloop here is a little subtle. Redrawing will cause
816 * EGL to read events so we can just call
817 * wl_display_dispatch_pending() to handle any events that got
818 * queued up as a side effect. */
819 while (running && ret != -1) {
820 wl_display_dispatch_pending(display.display);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800821 redraw(&window, NULL, 0);
822 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500823
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200824 fprintf(stderr, "simple-egl exiting\n");
825
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200826 destroy_surface(&window);
827 fini_egl(&display);
828
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400829 wl_surface_destroy(display.cursor_surface);
830 if (display.cursor_theme)
831 wl_cursor_theme_destroy(display.cursor_theme);
832
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200833 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800834 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200835
836 if (display.compositor)
837 wl_compositor_destroy(display.compositor);
838
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200839 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200840 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500841 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200842
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100843 return 0;
844}