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
30 #include <wayland-client.h>
31 #include <wayland-egl.h>
33 #include <GLES2/gl2.h>
37 struct wl_display *display;
38 struct wl_visual *premultiplied_argb_visual;
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 EGLSurface egl_surface;
70 static const char *vert_shader_text =
71 "uniform mat4 rotation;\n"
72 "attribute vec4 pos;\n"
73 "attribute vec4 color;\n"
74 "varying vec4 v_color;\n"
76 " gl_Position = rotation * pos;\n"
80 static const char *frag_shader_text =
81 "precision mediump float;\n"
82 "varying vec4 v_color;\n"
84 " gl_FragColor = v_color;\n"
88 init_egl(struct display *display)
90 static const EGLint context_attribs[] = {
91 EGL_CONTEXT_CLIENT_VERSION, 2,
95 static const EGLint config_attribs[] = {
96 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_VG_ALPHA_FORMAT_PRE_BIT,
102 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
106 EGLint major, minor, n;
109 display->egl.dpy = eglGetDisplay(display->display);
110 assert(display->egl.dpy);
112 ret = eglInitialize(display->egl.dpy, &major, &minor);
113 assert(ret == EGL_TRUE);
114 ret = eglBindAPI(EGL_OPENGL_ES_API);
115 assert(ret == EGL_TRUE);
117 assert(eglChooseConfig(display->egl.dpy, config_attribs,
118 &display->egl.conf, 1, &n) && n == 1);
120 display->egl.ctx = eglCreateContext(display->egl.dpy,
122 EGL_NO_CONTEXT, context_attribs);
123 assert(display->egl.ctx);
128 create_shader(struct window *window, const char *source, GLenum shader_type)
133 shader = glCreateShader(shader_type);
136 glShaderSource(shader, 1, (const char **) &source, NULL);
137 glCompileShader(shader);
139 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
143 glGetShaderInfoLog(shader, 1000, &len, log);
144 fprintf(stderr, "Error: compiling %s: %*s\n",
145 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
154 init_gl(struct window *window)
159 glViewport(0, 0, window->geometry.width, window->geometry.height);
161 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
162 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
164 window->gl.program = glCreateProgram();
165 glAttachShader(window->gl.program, frag);
166 glAttachShader(window->gl.program, vert);
167 glLinkProgram(window->gl.program);
169 glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status);
173 glGetProgramInfoLog(window->gl.program, 1000, &len, log);
174 fprintf(stderr, "Error: linking:\n%*s\n", len, log);
178 glUseProgram(window->gl.program);
183 glBindAttribLocation(window->gl.program, window->gl.pos, "pos");
184 glBindAttribLocation(window->gl.program, window->gl.col, "color");
185 glLinkProgram(window->gl.program);
187 window->gl.rotation_uniform =
188 glGetUniformLocation(window->gl.program, "rotation");
192 create_surface(struct window *window)
194 struct display *display = window->display;
195 struct wl_visual *visual;
197 static const EGLint surface_attribs[] = {
198 EGL_ALPHA_FORMAT, EGL_ALPHA_FORMAT_PRE,
202 window->surface = wl_compositor_create_surface(display->compositor);
203 visual = display->premultiplied_argb_visual;
205 wl_egl_window_create(window->surface,
206 window->geometry.width,
207 window->geometry.height);
208 window->egl_surface =
209 eglCreateWindowSurface(display->egl.dpy,
214 wl_shell_set_toplevel(display->shell, window->surface);
216 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
217 window->egl_surface, window->display->egl.ctx);
218 assert(ret == EGL_TRUE);
221 static const struct wl_callback_listener frame_listener;
224 redraw(void *data, struct wl_callback *callback, uint32_t time)
226 struct window *window = data;
227 static const GLfloat verts[3][2] = {
232 static const GLfloat colors[3][3] = {
238 GLfloat rotation[4][4] = {
244 static const int32_t speed_div = 5;
245 static uint32_t start_time = 0;
250 angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0;
251 rotation[0][0] = cos(angle);
252 rotation[0][2] = sin(angle);
253 rotation[2][0] = -sin(angle);
254 rotation[2][2] = cos(angle);
256 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
257 (GLfloat *) rotation);
259 glClearColor(0.0, 0.0, 0.0, 0.5);
260 glClear(GL_COLOR_BUFFER_BIT);
262 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
263 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
264 glEnableVertexAttribArray(window->gl.pos);
265 glEnableVertexAttribArray(window->gl.col);
267 glDrawArrays(GL_TRIANGLES, 0, 3);
269 glDisableVertexAttribArray(window->gl.pos);
270 glDisableVertexAttribArray(window->gl.col);
274 eglSwapBuffers(window->display->egl.dpy, window->egl_surface);
276 wl_callback_destroy(callback);
278 callback = wl_surface_frame(window->surface);
279 wl_callback_add_listener(callback, &frame_listener, window);
282 static const struct wl_callback_listener frame_listener = {
287 display_handle_global(struct wl_display *display, uint32_t id,
288 const char *interface, uint32_t version, void *data)
290 struct display *d = data;
292 if (strcmp(interface, "wl_compositor") == 0) {
294 wl_display_bind(display, id, &wl_compositor_interface);
295 } else if (strcmp(interface, "wl_shell") == 0) {
296 d->shell = wl_display_bind(display, id, &wl_shell_interface);
301 event_mask_update(uint32_t mask, void *data)
303 struct display *d = data;
311 main(int argc, char **argv)
313 struct display display = { 0 };
314 struct window window = { 0 };
316 memset(&display, 0, sizeof display);
317 memset(&window, 0, sizeof window);
319 window.display = &display;
320 window.geometry.width = 250;
321 window.geometry.height = 250;
323 display.display = wl_display_connect(NULL);
324 assert(display.display);
326 wl_display_add_global_listener(display.display,
327 display_handle_global, &display);
329 wl_display_get_fd(display.display, event_mask_update, &display);
330 wl_display_iterate(display.display, WL_DISPLAY_READABLE);
333 create_surface(&window);
336 redraw(&window, NULL, 0);
339 wl_display_iterate(display.display, display.mask);