blob: b94f31d2eaeb54494bdf50e128e6ab66e8ac7e6b [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
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080041#include "xdg-shell-client-protocol.h"
42
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040043#ifndef EGL_EXT_swap_buffers_with_damage
44#define EGL_EXT_swap_buffers_with_damage 1
45typedef EGLBoolean (EGLAPIENTRYP PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)(EGLDisplay dpy, EGLSurface surface, EGLint *rects, EGLint n_rects);
46#endif
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010047
Kristian Høgsberg77787432013-08-22 12:16:51 -070048#ifndef EGL_EXT_buffer_age
49#define EGL_EXT_buffer_age 1
50#define EGL_BUFFER_AGE_EXT 0x313D
51#endif
52
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030053struct window;
Daniel Stone37816df2012-05-16 18:45:18 +010054struct seat;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030055
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010056struct display {
57 struct wl_display *display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040058 struct wl_registry *registry;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050059 struct wl_compositor *compositor;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080060 struct xdg_shell *shell;
Kristian Høgsbergb84108d2012-05-16 16:16:19 -040061 struct wl_seat *seat;
62 struct wl_pointer *pointer;
Rusty Lynch1084da52013-08-15 09:10:08 -070063 struct wl_touch *touch;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030064 struct wl_keyboard *keyboard;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -040065 struct wl_shm *shm;
66 struct wl_cursor_theme *cursor_theme;
67 struct wl_cursor *default_cursor;
68 struct wl_surface *cursor_surface;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010069 struct {
70 EGLDisplay dpy;
71 EGLContext ctx;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050072 EGLConfig conf;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010073 } egl;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +030074 struct window *window;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -040075
76 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010077};
78
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +030079struct geometry {
80 int width, height;
81};
82
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010083struct window {
84 struct display *display;
Scott Moreau1ee53e72012-08-30 14:44:15 -060085 struct geometry geometry, window_size;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010086 struct {
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010087 GLuint rotation_uniform;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010088 GLuint pos;
89 GLuint col;
90 } gl;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050091
Kristian Høgsbergdeb32222013-12-06 22:02:45 -080092 uint32_t benchmark_time, frames;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050093 struct wl_egl_window *native;
94 struct wl_surface *surface;
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -080095 struct xdg_surface *xdg_surface;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -050096 EGLSurface egl_surface;
Pekka Paalanen2c2c1062011-12-13 14:50:25 +020097 struct wl_callback *callback;
Kristian Høgsberg1e658402013-12-07 22:25:56 -080098 int fullscreen, configured, opaque, buffer_size, frame_sync;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +010099};
100
101static const char *vert_shader_text =
102 "uniform mat4 rotation;\n"
103 "attribute vec4 pos;\n"
104 "attribute vec4 color;\n"
105 "varying vec4 v_color;\n"
106 "void main() {\n"
107 " gl_Position = rotation * pos;\n"
108 " v_color = color;\n"
109 "}\n";
110
111static const char *frag_shader_text =
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500112 "precision mediump float;\n"
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100113 "varying vec4 v_color;\n"
114 "void main() {\n"
115 " gl_FragColor = v_color;\n"
116 "}\n";
117
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400118static int running = 1;
119
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100120static void
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700121init_egl(struct display *display, struct window *window)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100122{
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500123 static const EGLint context_attribs[] = {
124 EGL_CONTEXT_CLIENT_VERSION, 2,
125 EGL_NONE
126 };
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400127 const char *extensions;
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500128
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300129 EGLint config_attribs[] = {
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500130 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500131 EGL_RED_SIZE, 1,
132 EGL_GREEN_SIZE, 1,
133 EGL_BLUE_SIZE, 1,
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500134 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
135 EGL_NONE
136 };
137
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700138 EGLint major, minor, n, count, i, size;
139 EGLConfig *configs;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100140 EGLBoolean ret;
141
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700142 if (window->opaque || window->buffer_size == 16)
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400143 config_attribs[9] = 0;
144
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400145 display->egl.dpy = eglGetDisplay(display->display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100146 assert(display->egl.dpy);
147
148 ret = eglInitialize(display->egl.dpy, &major, &minor);
149 assert(ret == EGL_TRUE);
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500150 ret = eglBindAPI(EGL_OPENGL_ES_API);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100151 assert(ret == EGL_TRUE);
152
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700153 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
154 assert(0);
155
156 configs = calloc(count, sizeof *configs);
157 assert(configs);
158
Pekka Paalanenb79b6352012-06-12 17:42:24 +0300159 ret = eglChooseConfig(display->egl.dpy, config_attribs,
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700160 configs, count, &n);
161 assert(ret && n >= 1);
162
163 for (i = 0; i < n; i++) {
164 eglGetConfigAttrib(display->egl.dpy,
165 configs[i], EGL_BUFFER_SIZE, &size);
166 if (window->buffer_size == size) {
167 display->egl.conf = configs[i];
168 break;
169 }
170 }
171 free(configs);
172 if (display->egl.conf == NULL) {
173 fprintf(stderr, "did not find config with buffer size %d\n",
174 window->buffer_size);
175 exit(EXIT_FAILURE);
176 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500177
178 display->egl.ctx = eglCreateContext(display->egl.dpy,
179 display->egl.conf,
Kristian Høgsberg1a11fac2011-01-14 20:39:21 -0500180 EGL_NO_CONTEXT, context_attribs);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100181 assert(display->egl.ctx);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500182
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400183 display->swap_buffers_with_damage = NULL;
184 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
185 if (extensions &&
186 strstr(extensions, "EGL_EXT_swap_buffers_with_damage") &&
187 strstr(extensions, "EGL_EXT_buffer_age"))
188 display->swap_buffers_with_damage =
189 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
190 eglGetProcAddress("eglSwapBuffersWithDamageEXT");
191
192 if (display->swap_buffers_with_damage)
193 printf("has EGL_EXT_buffer_age and EGL_EXT_swap_buffers_with_damage\n");
194
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100195}
196
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200197static void
198fini_egl(struct display *display)
199{
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200200 eglTerminate(display->egl.dpy);
201 eglReleaseThread();
202}
203
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100204static GLuint
205create_shader(struct window *window, const char *source, GLenum shader_type)
206{
207 GLuint shader;
208 GLint status;
209
210 shader = glCreateShader(shader_type);
211 assert(shader != 0);
212
213 glShaderSource(shader, 1, (const char **) &source, NULL);
214 glCompileShader(shader);
215
216 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
217 if (!status) {
218 char log[1000];
219 GLsizei len;
220 glGetShaderInfoLog(shader, 1000, &len, log);
221 fprintf(stderr, "Error: compiling %s: %*s\n",
222 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
223 len, log);
224 exit(1);
225 }
226
227 return shader;
228}
229
230static void
231init_gl(struct window *window)
232{
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100233 GLuint frag, vert;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600234 GLuint program;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100235 GLint status;
236
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100237 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
238 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
239
Scott Moreau3ea23d02012-06-13 17:42:21 -0600240 program = glCreateProgram();
241 glAttachShader(program, frag);
242 glAttachShader(program, vert);
243 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100244
Scott Moreau3ea23d02012-06-13 17:42:21 -0600245 glGetProgramiv(program, GL_LINK_STATUS, &status);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100246 if (!status) {
247 char log[1000];
248 GLsizei len;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600249 glGetProgramInfoLog(program, 1000, &len, log);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100250 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
251 exit(1);
252 }
253
Scott Moreau3ea23d02012-06-13 17:42:21 -0600254 glUseProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100255
256 window->gl.pos = 0;
Scott Moreau3ea23d02012-06-13 17:42:21 -0600257 window->gl.col = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100258
Scott Moreau3ea23d02012-06-13 17:42:21 -0600259 glBindAttribLocation(program, window->gl.pos, "pos");
260 glBindAttribLocation(program, window->gl.col, "color");
261 glLinkProgram(program);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100262
263 window->gl.rotation_uniform =
Scott Moreau3ea23d02012-06-13 17:42:21 -0600264 glGetUniformLocation(program, "rotation");
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100265}
266
267static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800268handle_surface_ping(void *data,
269 struct xdg_surface *xdg_surface, uint32_t serial)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300270{
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800271 xdg_surface_pong(xdg_surface, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300272}
273
274static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800275handle_surface_configure(void *data, struct xdg_surface *surface,
276 int32_t width, int32_t height)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300277{
278 struct window *window = data;
279
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300280 if (window->native)
281 wl_egl_window_resize(window->native, width, height, 0, 0);
282
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300283 window->geometry.width = width;
284 window->geometry.height = height;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300285
Scott Moreau1ee53e72012-08-30 14:44:15 -0600286 if (!window->fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300287 window->window_size = window->geometry;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300288}
289
290static void
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800291handle_surface_request_set_maximized(void *data, struct xdg_surface *xdg_surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300292{
293}
294
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800295static void
296handle_surface_request_unset_maximized(void *data, struct xdg_surface *xdg_surface)
297{
298}
299
300static void
301handle_surface_request_set_fullscreen(void *data, struct xdg_surface *xdg_surface)
302{
303}
304
305static void
306handle_surface_request_unset_fullscreen(void *data, struct xdg_surface *xdg_surface)
307{
308}
309
310static void
311handle_surface_focused_set(void *data, struct xdg_surface *xdg_surface)
312{
313}
314
315static void
316handle_surface_focused_unset(void *data, struct xdg_surface *xdg_surface)
317{
318}
319
320static const struct xdg_surface_listener xdg_surface_listener = {
321 handle_surface_ping,
322 handle_surface_configure,
323 handle_surface_request_set_maximized,
324 handle_surface_request_unset_maximized,
325 handle_surface_request_set_fullscreen,
326 handle_surface_request_unset_fullscreen,
327 handle_surface_focused_set,
328 handle_surface_focused_unset,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300329};
330
331static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300332configure_callback(void *data, struct wl_callback *callback, uint32_t time)
333{
334 struct window *window = data;
335
336 wl_callback_destroy(callback);
337
338 window->configured = 1;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300339}
340
341static struct wl_callback_listener configure_callback_listener = {
342 configure_callback,
343};
344
345static void
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800346set_fullscreen(struct window *window, int fullscreen)
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300347{
348 struct wl_callback *callback;
349
350 window->fullscreen = fullscreen;
351 window->configured = 0;
352
353 if (fullscreen) {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800354 xdg_surface_set_fullscreen(window->xdg_surface);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800355 callback = wl_display_sync(window->display->display);
356 wl_callback_add_listener(callback,
357 &configure_callback_listener,
358 window);
359
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300360 } else {
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800361 xdg_surface_unset_fullscreen(window->xdg_surface);
362 handle_surface_configure(window, window->xdg_surface,
363 window->window_size.width,
364 window->window_size.height);
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800365 window->configured = 1;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300366 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300367}
368
369static void
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100370create_surface(struct window *window)
371{
372 struct display *display = window->display;
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500373 EGLBoolean ret;
Benjamin Franzke65e50512011-05-31 11:36:31 +0200374
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500375 window->surface = wl_compositor_create_surface(display->compositor);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800376 window->xdg_surface = xdg_shell_get_xdg_surface(display->shell,
377 window->surface);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300378
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800379 xdg_surface_add_listener(window->xdg_surface,
380 &xdg_surface_listener, window);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300381
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500382 window->native =
Kristian Høgsberg91342c62011-04-14 14:44:58 -0400383 wl_egl_window_create(window->surface,
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300384 window->window_size.width,
385 window->window_size.height);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500386 window->egl_surface =
387 eglCreateWindowSurface(display->egl.dpy,
388 display->egl.conf,
Kristian Høgsberg8e81df42012-01-11 14:24:46 -0500389 window->native, NULL);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100390
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800391 xdg_surface_set_title(window->xdg_surface, "simple-egl");
Scott Moreau01a9f1b2012-10-07 08:56:30 -0600392
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500393 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
394 window->egl_surface, window->display->egl.ctx);
395 assert(ret == EGL_TRUE);
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300396
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800397 if (!window->frame_sync)
398 eglSwapInterval(display->egl.dpy, 0);
399
400 set_fullscreen(window, window->fullscreen);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100401}
402
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200403static void
404destroy_surface(struct window *window)
405{
Yeh, Sinclair952e6df2013-04-19 17:49:12 +0000406 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
407 * on eglReleaseThread(). */
408 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
409 EGL_NO_CONTEXT);
410
411 eglDestroySurface(window->display->egl.dpy, window->egl_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200412 wl_egl_window_destroy(window->native);
413
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800414 xdg_surface_destroy(window->xdg_surface);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200415 wl_surface_destroy(window->surface);
416
417 if (window->callback)
418 wl_callback_destroy(window->callback);
419}
420
Kristian Høgsberg33418202011-08-16 23:01:28 -0400421static const struct wl_callback_listener frame_listener;
422
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100423static void
Kristian Høgsberg33418202011-08-16 23:01:28 -0400424redraw(void *data, struct wl_callback *callback, uint32_t time)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100425{
426 struct window *window = data;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400427 struct display *display = window->display;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100428 static const GLfloat verts[3][2] = {
429 { -0.5, -0.5 },
430 { 0.5, -0.5 },
431 { 0, 0.5 }
432 };
433 static const GLfloat colors[3][3] = {
434 { 1, 0, 0 },
435 { 0, 1, 0 },
436 { 0, 0, 1 }
437 };
438 GLfloat angle;
439 GLfloat rotation[4][4] = {
440 { 1, 0, 0, 0 },
441 { 0, 1, 0, 0 },
442 { 0, 0, 1, 0 },
443 { 0, 0, 0, 1 }
444 };
Jonas Ådahl82fced42014-01-03 19:46:50 +0100445 static const uint32_t speed_div = 5, benchmark_interval = 5;
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400446 struct wl_region *region;
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400447 EGLint rect[4];
448 EGLint buffer_age = 0;
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800449 struct timeval tv;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100450
Scott Moreau7e300db2012-08-31 03:18:15 -0600451 assert(window->callback == callback);
452 window->callback = NULL;
453
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300454 if (callback)
455 wl_callback_destroy(callback);
456
457 if (!window->configured)
458 return;
459
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800460 gettimeofday(&tv, NULL);
461 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
462 if (window->frames == 0)
463 window->benchmark_time = time;
464 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
465 printf("%d frames in %d seconds: %f fps\n",
466 window->frames,
467 benchmark_interval,
468 (float) window->frames / benchmark_interval);
469 window->benchmark_time = time;
470 window->frames = 0;
471 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100472
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800473 angle = (time / speed_div) % 360 * M_PI / 180.0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100474 rotation[0][0] = cos(angle);
475 rotation[0][2] = sin(angle);
476 rotation[2][0] = -sin(angle);
477 rotation[2][2] = cos(angle);
478
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400479 if (display->swap_buffers_with_damage)
480 eglQuerySurface(display->egl.dpy, window->egl_surface,
481 EGL_BUFFER_AGE_EXT, &buffer_age);
482
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300483 glViewport(0, 0, window->geometry.width, window->geometry.height);
484
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100485 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
486 (GLfloat *) rotation);
487
488 glClearColor(0.0, 0.0, 0.0, 0.5);
489 glClear(GL_COLOR_BUFFER_BIT);
490
491 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
492 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
493 glEnableVertexAttribArray(window->gl.pos);
494 glEnableVertexAttribArray(window->gl.col);
495
496 glDrawArrays(GL_TRIANGLES, 0, 3);
497
498 glDisableVertexAttribArray(window->gl.pos);
499 glDisableVertexAttribArray(window->gl.col);
500
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300501 if (window->opaque || window->fullscreen) {
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400502 region = wl_compositor_create_region(window->display->compositor);
503 wl_region_add(region, 0, 0,
Ander Conselvan de Oliveiraedce9c22012-09-07 17:32:16 +0300504 window->geometry.width,
505 window->geometry.height);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400506 wl_surface_set_opaque_region(window->surface, region);
507 wl_region_destroy(region);
Scott Moreau6655e002012-11-19 14:17:52 -0700508 } else {
509 wl_surface_set_opaque_region(window->surface, NULL);
Kristian Høgsberg45ce9882012-08-03 15:27:14 -0400510 }
511
Kristian Høgsberg9e885d42013-05-08 11:37:28 -0400512 if (display->swap_buffers_with_damage && buffer_age > 0) {
513 rect[0] = window->geometry.width / 4 - 1;
514 rect[1] = window->geometry.height / 4 - 1;
515 rect[2] = window->geometry.width / 2 + 2;
516 rect[3] = window->geometry.height / 2 + 2;
517 display->swap_buffers_with_damage(display->egl.dpy,
518 window->egl_surface,
519 rect, 1);
520 } else {
521 eglSwapBuffers(display->egl.dpy, window->egl_surface);
522 }
Kristian Høgsbergdeb32222013-12-06 22:02:45 -0800523 window->frames++;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100524}
525
Kristian Høgsberg33418202011-08-16 23:01:28 -0400526static const struct wl_callback_listener frame_listener = {
527 redraw
528};
529
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100530static void
Daniel Stone37816df2012-05-16 18:45:18 +0100531pointer_handle_enter(void *data, struct wl_pointer *pointer,
532 uint32_t serial, struct wl_surface *surface,
533 wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300534{
535 struct display *display = data;
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400536 struct wl_buffer *buffer;
537 struct wl_cursor *cursor = display->default_cursor;
538 struct wl_cursor_image *image;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300539
540 if (display->window->fullscreen)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +0300541 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400542 else if (cursor) {
543 image = display->default_cursor->images[0];
544 buffer = wl_cursor_image_get_buffer(image);
545 wl_pointer_set_cursor(pointer, serial,
546 display->cursor_surface,
547 image->hotspot_x,
548 image->hotspot_y);
549 wl_surface_attach(display->cursor_surface, buffer, 0, 0);
550 wl_surface_damage(display->cursor_surface, 0, 0,
551 image->width, image->height);
552 wl_surface_commit(display->cursor_surface);
553 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300554}
555
556static void
Daniel Stone37816df2012-05-16 18:45:18 +0100557pointer_handle_leave(void *data, struct wl_pointer *pointer,
558 uint32_t serial, struct wl_surface *surface)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300559{
560}
561
562static void
Daniel Stone37816df2012-05-16 18:45:18 +0100563pointer_handle_motion(void *data, struct wl_pointer *pointer,
564 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300565{
566}
567
568static void
Daniel Stone37816df2012-05-16 18:45:18 +0100569pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
570 uint32_t serial, uint32_t time, uint32_t button,
571 uint32_t state)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300572{
Ander Conselvan de Oliveira57e0ce12012-06-26 17:09:11 +0300573 struct display *display = data;
574
575 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800576 xdg_surface_move(display->window->xdg_surface,
577 display->seat, serial);
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300578}
579
580static void
Daniel Stone37816df2012-05-16 18:45:18 +0100581pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +0100582 uint32_t time, uint32_t axis, wl_fixed_t value)
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300583{
584}
585
Daniel Stone37816df2012-05-16 18:45:18 +0100586static const struct wl_pointer_listener pointer_listener = {
587 pointer_handle_enter,
588 pointer_handle_leave,
589 pointer_handle_motion,
590 pointer_handle_button,
591 pointer_handle_axis,
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300592};
593
594static void
Rusty Lynch1084da52013-08-15 09:10:08 -0700595touch_handle_down(void *data, struct wl_touch *wl_touch,
596 uint32_t serial, uint32_t time, struct wl_surface *surface,
597 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
598{
599 struct display *d = (struct display *)data;
600
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800601 xdg_surface_move(d->window->xdg_surface, d->seat, serial);
Rusty Lynch1084da52013-08-15 09:10:08 -0700602}
603
604static void
605touch_handle_up(void *data, struct wl_touch *wl_touch,
606 uint32_t serial, uint32_t time, int32_t id)
607{
608}
609
610static void
611touch_handle_motion(void *data, struct wl_touch *wl_touch,
612 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
613{
614}
615
616static void
617touch_handle_frame(void *data, struct wl_touch *wl_touch)
618{
619}
620
621static void
622touch_handle_cancel(void *data, struct wl_touch *wl_touch)
623{
624}
625
626static const struct wl_touch_listener touch_listener = {
627 touch_handle_down,
628 touch_handle_up,
629 touch_handle_motion,
630 touch_handle_frame,
631 touch_handle_cancel,
632};
633
634static void
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300635keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
636 uint32_t format, int fd, uint32_t size)
637{
638}
639
640static void
641keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
642 uint32_t serial, struct wl_surface *surface,
643 struct wl_array *keys)
644{
645}
646
647static void
648keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
649 uint32_t serial, struct wl_surface *surface)
650{
651}
652
653static void
654keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
655 uint32_t serial, uint32_t time, uint32_t key,
656 uint32_t state)
657{
658 struct display *d = data;
659
660 if (key == KEY_F11 && state)
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800661 set_fullscreen(d->window, d->window->fullscreen ^ 1);
Kristian Høgsberg321e8b72012-07-30 15:40:57 -0400662 else if (key == KEY_ESC && state)
663 running = 0;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300664}
665
666static void
667keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
668 uint32_t serial, uint32_t mods_depressed,
669 uint32_t mods_latched, uint32_t mods_locked,
670 uint32_t group)
671{
672}
673
674static const struct wl_keyboard_listener keyboard_listener = {
675 keyboard_handle_keymap,
676 keyboard_handle_enter,
677 keyboard_handle_leave,
678 keyboard_handle_key,
679 keyboard_handle_modifiers,
680};
681
682static void
Daniel Stone37816df2012-05-16 18:45:18 +0100683seat_handle_capabilities(void *data, struct wl_seat *seat,
684 enum wl_seat_capability caps)
685{
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400686 struct display *d = data;
Daniel Stone37816df2012-05-16 18:45:18 +0100687
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400688 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
689 d->pointer = wl_seat_get_pointer(seat);
690 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
691 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
692 wl_pointer_destroy(d->pointer);
693 d->pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +0100694 }
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300695
696 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
697 d->keyboard = wl_seat_get_keyboard(seat);
698 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
699 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
700 wl_keyboard_destroy(d->keyboard);
701 d->keyboard = NULL;
702 }
Rusty Lynch1084da52013-08-15 09:10:08 -0700703
704 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
705 d->touch = wl_seat_get_touch(seat);
706 wl_touch_set_user_data(d->touch, d);
707 wl_touch_add_listener(d->touch, &touch_listener, d);
708 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
709 wl_touch_destroy(d->touch);
710 d->touch = NULL;
711 }
Daniel Stone37816df2012-05-16 18:45:18 +0100712}
713
714static const struct wl_seat_listener seat_listener = {
715 seat_handle_capabilities,
716};
717
718static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400719registry_handle_global(void *data, struct wl_registry *registry,
720 uint32_t name, const char *interface, uint32_t version)
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100721{
722 struct display *d = data;
723
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400724 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -0400725 d->compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400726 wl_registry_bind(registry, name,
727 &wl_compositor_interface, 1);
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800728 } else if (strcmp(interface, "xdg_shell") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400729 d->shell = wl_registry_bind(registry, name,
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800730 &xdg_shell_interface, 1);
731 xdg_shell_use_unstable_version(d->shell, 1);
Daniel Stone37816df2012-05-16 18:45:18 +0100732 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400733 d->seat = wl_registry_bind(registry, name,
734 &wl_seat_interface, 1);
Kristian Høgsbergb84108d2012-05-16 16:16:19 -0400735 wl_seat_add_listener(d->seat, &seat_listener, d);
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400736 } else if (strcmp(interface, "wl_shm") == 0) {
737 d->shm = wl_registry_bind(registry, name,
738 &wl_shm_interface, 1);
739 d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
740 d->default_cursor =
741 wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Kristian Høgsberg8357cd62011-05-13 13:24:56 -0400742 }
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100743}
744
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200745static void
746registry_handle_global_remove(void *data, struct wl_registry *registry,
747 uint32_t name)
748{
749}
750
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400751static const struct wl_registry_listener registry_listener = {
Pekka Paalanen0eab05d2013-01-22 14:53:55 +0200752 registry_handle_global,
753 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400754};
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100755
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200756static void
757signal_int(int signum)
758{
759 running = 0;
760}
761
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400762static void
763usage(int error_code)
764{
765 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
766 " -f\tRun in fullscreen mode\n"
767 " -o\tCreate an opaque surface\n"
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700768 " -s\tUse a 16 bpp EGL config\n"
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800769 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400770 " -h\tThis help text\n\n");
771
772 exit(error_code);
773}
774
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100775int
776main(int argc, char **argv)
777{
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200778 struct sigaction sigint;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100779 struct display display = { 0 };
780 struct window window = { 0 };
Kristian Høgsberga17f7a12012-10-16 13:16:10 -0400781 int i, ret = 0;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100782
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100783 window.display = &display;
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300784 display.window = &window;
Ander Conselvan de Oliveira69f98402012-07-27 17:18:13 +0300785 window.window_size.width = 250;
786 window.window_size.height = 250;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700787 window.buffer_size = 32;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800788 window.frame_sync = 1;
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100789
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400790 for (i = 1; i < argc; i++) {
791 if (strcmp("-f", argv[i]) == 0)
792 window.fullscreen = 1;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400793 else if (strcmp("-o", argv[i]) == 0)
Ander Conselvan de Oliveirad7f282b2012-09-10 15:55:53 +0300794 window.opaque = 1;
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700795 else if (strcmp("-s", argv[i]) == 0)
796 window.buffer_size = 16;
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800797 else if (strcmp("-b", argv[i]) == 0)
798 window.frame_sync = 0;
Kristian Høgsbergbcf48642012-08-03 15:29:08 -0400799 else if (strcmp("-h", argv[i]) == 0)
800 usage(EXIT_SUCCESS);
801 else
802 usage(EXIT_FAILURE);
Kristian Høgsberg3593f812012-05-10 20:40:51 -0400803 }
Ander Conselvan de Oliveirad51624f2012-05-02 16:42:23 +0300804
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100805 display.display = wl_display_connect(NULL);
806 assert(display.display);
807
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400808 display.registry = wl_display_get_registry(display.display);
809 wl_registry_add_listener(display.registry,
810 &registry_listener, &display);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100811
Kristian Høgsbergfa80e112012-10-10 21:34:26 -0400812 wl_display_dispatch(display.display);
Benjamin Franzke65e50512011-05-31 11:36:31 +0200813
Kristian Høgsberg78fe7532013-10-15 22:03:31 -0700814 init_egl(&display, &window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100815 create_surface(&window);
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500816 init_gl(&window);
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100817
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400818 display.cursor_surface =
819 wl_compositor_create_surface(display.compositor);
820
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200821 sigint.sa_handler = signal_int;
822 sigemptyset(&sigint.sa_mask);
823 sigint.sa_flags = SA_RESETHAND;
824 sigaction(SIGINT, &sigint, NULL);
825
Kristian Høgsberg1e658402013-12-07 22:25:56 -0800826 /* The mainloop here is a little subtle. Redrawing will cause
827 * EGL to read events so we can just call
828 * wl_display_dispatch_pending() to handle any events that got
829 * queued up as a side effect. */
830 while (running && ret != -1) {
831 wl_display_dispatch_pending(display.display);
832 while (!window.configured)
833 wl_display_dispatch(display.display);
834 redraw(&window, NULL, 0);
835 }
Kristian Høgsberga495a5e2011-02-04 15:31:33 -0500836
Pekka Paalanen88e60fc2011-12-13 12:09:09 +0200837 fprintf(stderr, "simple-egl exiting\n");
838
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200839 destroy_surface(&window);
840 fini_egl(&display);
841
Kristian Høgsberg191e0ee2012-10-29 17:41:46 -0400842 wl_surface_destroy(display.cursor_surface);
843 if (display.cursor_theme)
844 wl_cursor_theme_destroy(display.cursor_theme);
845
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200846 if (display.shell)
Kristian Høgsbergdfaf65b2014-02-07 17:01:57 -0800847 xdg_shell_destroy(display.shell);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200848
849 if (display.compositor)
850 wl_compositor_destroy(display.compositor);
851
Pekka Paalanenaac1c132012-12-04 16:01:15 +0200852 wl_registry_destroy(display.registry);
Pekka Paalanenfb850c42011-12-15 10:07:52 +0200853 wl_display_flush(display.display);
Kristian Høgsbergfcfc83f2012-02-28 14:29:19 -0500854 wl_display_disconnect(display.display);
Pekka Paalanen2c2c1062011-12-13 14:50:25 +0200855
Benjamin Franzkeaabdce02011-01-15 00:40:17 +0100856 return 0;
857}