2 * Copyright © 2011 Benjamin Franzke
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
31 #include <wayland-client.h>
32 #include <wayland-egl.h>
34 #include <GLES2/gl2.h>
38 struct wl_display *display;
39 struct wl_compositor *compositor;
40 struct wl_shell *shell;
50 struct display *display;
59 GLuint rotation_uniform;
65 struct wl_egl_window *native;
66 struct wl_surface *surface;
67 struct wl_shell_surface *shell_surface;
68 EGLSurface egl_surface;
69 struct wl_callback *callback;
72 static const char *vert_shader_text =
73 "uniform mat4 rotation;\n"
74 "attribute vec4 pos;\n"
75 "attribute vec4 color;\n"
76 "varying vec4 v_color;\n"
78 " gl_Position = rotation * pos;\n"
82 static const char *frag_shader_text =
83 "precision mediump float;\n"
84 "varying vec4 v_color;\n"
86 " gl_FragColor = v_color;\n"
90 init_egl(struct display *display)
92 static const EGLint context_attribs[] = {
93 EGL_CONTEXT_CLIENT_VERSION, 2,
97 static const EGLint config_attribs[] = {
98 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_VG_ALPHA_FORMAT_PRE_BIT,
104 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
108 EGLint major, minor, n;
111 display->egl.dpy = eglGetDisplay(display->display);
112 assert(display->egl.dpy);
114 ret = eglInitialize(display->egl.dpy, &major, &minor);
115 assert(ret == EGL_TRUE);
116 ret = eglBindAPI(EGL_OPENGL_ES_API);
117 assert(ret == EGL_TRUE);
119 assert(eglChooseConfig(display->egl.dpy, config_attribs,
120 &display->egl.conf, 1, &n) && n == 1);
122 display->egl.ctx = eglCreateContext(display->egl.dpy,
124 EGL_NO_CONTEXT, context_attribs);
125 assert(display->egl.ctx);
130 fini_egl(struct display *display)
132 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
133 * on eglReleaseThread(). */
134 eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
137 eglTerminate(display->egl.dpy);
142 create_shader(struct window *window, const char *source, GLenum shader_type)
147 shader = glCreateShader(shader_type);
150 glShaderSource(shader, 1, (const char **) &source, NULL);
151 glCompileShader(shader);
153 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
157 glGetShaderInfoLog(shader, 1000, &len, log);
158 fprintf(stderr, "Error: compiling %s: %*s\n",
159 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
168 init_gl(struct window *window)
173 glViewport(0, 0, window->geometry.width, window->geometry.height);
175 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
176 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
178 window->gl.program = glCreateProgram();
179 glAttachShader(window->gl.program, frag);
180 glAttachShader(window->gl.program, vert);
181 glLinkProgram(window->gl.program);
183 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
187 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
188 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
192 glUseProgram(window->gl.program);
197 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
198 glBindAttribLocation(window->gl.program, window->gl.col, "color");
199 glLinkProgram(window->gl.program);
201 window->gl.rotation_uniform =
202 glGetUniformLocation(window->gl.program, "rotation");
206 create_surface(struct window *window)
208 struct display *display = window->display;
210 static const EGLint surface_attribs[] = {
211 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
215 window->surface = wl_compositor_create_surface(display->compositor);
216 window->shell_surface = wl_shell_get_shell_surface(display->shell,
219 wl_egl_window_create(window->surface,
220 window->geometry.width,
221 window->geometry.height);
222 window->egl_surface =
223 eglCreateWindowSurface(display->egl.dpy,
228 wl_shell_surface_set_toplevel(window->shell_surface);
230 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
231 window->egl_surface, window->display->egl.ctx);
232 assert(ret == EGL_TRUE);
236 destroy_surface(struct window *window)
238 wl_egl_window_destroy(window->native);
240 wl_shell_surface_destroy(window->shell_surface);
241 wl_surface_destroy(window->surface);
243 if (window->callback)
244 wl_callback_destroy(window->callback);
247 static const struct wl_callback_listener frame_listener;
250 redraw(void *data, struct wl_callback *callback, uint32_t time)
252 struct window *window = data;
253 static const GLfloat verts[3][2] = {
258 static const GLfloat colors[3][3] = {
264 GLfloat rotation[4][4] = {
270 static const int32_t speed_div = 5;
271 static uint32_t start_time = 0;
276 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
277 rotation[0][0] = cos(angle);
278 rotation[0][2] = sin(angle);
279 rotation[2][0] = -sin(angle);
280 rotation[2][2] = cos(angle);
282 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
283 (GLfloat *) rotation);
285 glClearColor(0.0, 0.0, 0.0, 0.5);
286 glClear(GL_COLOR_BUFFER_BIT);
288 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
289 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
290 glEnableVertexAttribArray(window->gl.pos);
291 glEnableVertexAttribArray(window->gl.col);
293 glDrawArrays(GL_TRIANGLES, 0, 3);
295 glDisableVertexAttribArray(window->gl.pos);
296 glDisableVertexAttribArray(window->gl.col);
300 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
302 wl_callback_destroy(callback);
304 window->callback = wl_surface_frame(window->surface);
305 wl_callback_add_listener(window->callback, &frame_listener, window);
308 static const struct wl_callback_listener frame_listener = {
313 display_handle_global(struct wl_display *display, uint32_t id,
314 const char *interface, uint32_t version, void *data)
316 struct display *d = data;
318 if (strcmp(interface, "wl_compositor") == 0) {
320 wl_display_bind(display, id, &wl_compositor_interface);
321 } else if (strcmp(interface, "wl_shell") == 0) {
322 d->shell = wl_display_bind(display, id, &wl_shell_interface);
327 event_mask_update(uint32_t mask, void *data)
329 struct display *d = data;
336 static int running = 1;
339 signal_int(int signum)
345 main(int argc, char **argv)
347 struct sigaction sigint;
348 struct display display = { 0 };
349 struct window window = { 0 };
351 window.display = &display;
352 window.geometry.width = 250;
353 window.geometry.height = 250;
355 display.display = wl_display_connect(NULL);
356 assert(display.display);
358 wl_display_add_global_listener(display.display,
359 display_handle_global, &display);
361 wl_display_get_fd(display.display, event_mask_update, &display);
362 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
365 create_surface(&window);
368 sigint.sa_handler = signal_int;
369 sigemptyset(&sigint.sa_mask);
370 sigint.sa_flags = SA_RESETHAND;
371 sigaction(SIGINT, &sigint, NULL);
373 redraw(&window, NULL, 0);
376 wl_display_iterate(display.display, display.mask);
378 fprintf(stderr, "simple-egl exiting\n");
380 destroy_surface(&window);
384 wl_shell_destroy(display.shell);
386 if (display.compositor)
387 wl_compositor_destroy(display.compositor);
389 wl_display_destroy(display.display);