Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 1 | /* |
| 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 Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | #include <math.h> |
| 28 | #include <assert.h> |
| 29 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 30 | #include <wayland-client.h> |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 31 | #include <wayland-egl.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 32 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 33 | #include <GLES2/gl2.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 34 | #include <EGL/egl.h> |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 35 | |
| 36 | struct display { |
| 37 | struct wl_display *display; |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 38 | struct wl_visual *premultiplied_argb_visual; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 39 | struct wl_compositor *compositor; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 40 | struct { |
| 41 | EGLDisplay dpy; |
| 42 | EGLContext ctx; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 43 | EGLConfig conf; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 44 | } egl; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 45 | uint32_t mask; |
| 46 | }; |
| 47 | |
| 48 | struct window { |
| 49 | struct display *display; |
| 50 | struct { |
| 51 | int width, height; |
| 52 | } geometry; |
| 53 | struct { |
| 54 | GLuint fbo; |
| 55 | GLuint color_rbo; |
| 56 | |
| 57 | GLuint program; |
| 58 | GLuint rotation_uniform; |
| 59 | |
| 60 | GLuint pos; |
| 61 | GLuint col; |
| 62 | } gl; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 63 | |
| 64 | struct wl_egl_window *native; |
| 65 | struct wl_surface *surface; |
| 66 | EGLSurface egl_surface; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static const char *vert_shader_text = |
| 70 | "uniform mat4 rotation;\n" |
| 71 | "attribute vec4 pos;\n" |
| 72 | "attribute vec4 color;\n" |
| 73 | "varying vec4 v_color;\n" |
| 74 | "void main() {\n" |
| 75 | " gl_Position = rotation * pos;\n" |
| 76 | " v_color = color;\n" |
| 77 | "}\n"; |
| 78 | |
| 79 | static const char *frag_shader_text = |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 80 | "precision mediump float;\n" |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 81 | "varying vec4 v_color;\n" |
| 82 | "void main() {\n" |
| 83 | " gl_FragColor = v_color;\n" |
| 84 | "}\n"; |
| 85 | |
| 86 | static void |
| 87 | init_egl(struct display *display) |
| 88 | { |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 89 | static const EGLint context_attribs[] = { |
| 90 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 91 | EGL_NONE |
| 92 | }; |
| 93 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 94 | static const EGLint config_attribs[] = { |
| 95 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 96 | EGL_RED_SIZE, 1, |
| 97 | EGL_GREEN_SIZE, 1, |
| 98 | EGL_BLUE_SIZE, 1, |
Benjamin Franzke | 2eae9e3 | 2011-03-17 15:43:21 +0100 | [diff] [blame] | 99 | EGL_ALPHA_SIZE, 1, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 100 | EGL_DEPTH_SIZE, 1, |
| 101 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 102 | EGL_NONE |
| 103 | }; |
| 104 | |
| 105 | EGLint major, minor, n; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 106 | EGLBoolean ret; |
| 107 | |
Kristian Høgsberg | ded9ff3 | 2011-05-13 17:07:23 -0400 | [diff] [blame] | 108 | setenv("EGL_PLATFORM", "wayland", 1); |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 109 | display->egl.dpy = eglGetDisplay(display->display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 110 | assert(display->egl.dpy); |
| 111 | |
| 112 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 113 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 114 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 115 | assert(ret == EGL_TRUE); |
| 116 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 117 | assert(eglChooseConfig(display->egl.dpy, config_attribs, |
| 118 | &display->egl.conf, 1, &n) && n == 1); |
| 119 | |
| 120 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 121 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 122 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 123 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 124 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static GLuint |
| 128 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 129 | { |
| 130 | GLuint shader; |
| 131 | GLint status; |
| 132 | |
| 133 | shader = glCreateShader(shader_type); |
| 134 | assert(shader != 0); |
| 135 | |
| 136 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 137 | glCompileShader(shader); |
| 138 | |
| 139 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 140 | if (!status) { |
| 141 | char log[1000]; |
| 142 | GLsizei len; |
| 143 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 144 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 145 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 146 | len, log); |
| 147 | exit(1); |
| 148 | } |
| 149 | |
| 150 | return shader; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | init_gl(struct window *window) |
| 155 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 156 | GLuint frag, vert; |
| 157 | GLint status; |
| 158 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 159 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 160 | |
| 161 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 162 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 163 | |
| 164 | window->gl.program = glCreateProgram(); |
| 165 | glAttachShader(window->gl.program, frag); |
| 166 | glAttachShader(window->gl.program, vert); |
| 167 | glLinkProgram(window->gl.program); |
| 168 | |
| 169 | glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); |
| 170 | if (!status) { |
| 171 | char log[1000]; |
| 172 | GLsizei len; |
| 173 | glGetProgramInfoLog(window->gl.program, 1000, &len, log); |
| 174 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 175 | exit(1); |
| 176 | } |
| 177 | |
| 178 | glUseProgram(window->gl.program); |
| 179 | |
| 180 | window->gl.pos = 0; |
| 181 | window->gl.pos = 1; |
| 182 | |
| 183 | glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); |
| 184 | glBindAttribLocation(window->gl.program, window->gl.col, "color"); |
| 185 | glLinkProgram(window->gl.program); |
| 186 | |
| 187 | window->gl.rotation_uniform = |
| 188 | glGetUniformLocation(window->gl.program, "rotation"); |
| 189 | } |
| 190 | |
| 191 | static void |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame^] | 192 | sync_callback(void *data) |
| 193 | { |
| 194 | int *done = data; |
| 195 | |
| 196 | *done = 1; |
| 197 | } |
| 198 | |
| 199 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 200 | create_surface(struct window *window) |
| 201 | { |
| 202 | struct display *display = window->display; |
| 203 | struct wl_visual *visual; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 204 | EGLBoolean ret; |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame^] | 205 | int done = 0; |
| 206 | |
| 207 | if (!display->premultiplied_argb_visual) { |
| 208 | wl_display_sync_callback(display->display, sync_callback, &done); |
| 209 | while (!done) |
| 210 | wl_display_iterate(display->display, display->mask); |
| 211 | if (!display->premultiplied_argb_visual) { |
| 212 | fprintf(stderr, "premultiplied argb visual missing\n"); |
| 213 | exit(1); |
| 214 | } |
| 215 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 216 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 217 | window->surface = wl_compositor_create_surface(display->compositor); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 218 | visual = display->premultiplied_argb_visual; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 219 | window->native = |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 220 | wl_egl_window_create(window->surface, |
Kristian Høgsberg | bfb8e61 | 2011-02-07 10:30:38 -0500 | [diff] [blame] | 221 | window->geometry.width, |
| 222 | window->geometry.height, |
| 223 | visual); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 224 | window->egl_surface = |
| 225 | eglCreateWindowSurface(display->egl.dpy, |
| 226 | display->egl.conf, |
Kristian Høgsberg | 297d6dd | 2011-02-09 10:51:15 -0500 | [diff] [blame] | 227 | window->native, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 228 | NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 229 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 230 | wl_surface_map_toplevel(window->surface); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 231 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 232 | ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, |
| 233 | window->egl_surface, window->display->egl.ctx); |
| 234 | assert(ret == EGL_TRUE); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 238 | redraw(struct wl_surface *surface, void *data, uint32_t time) |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 239 | { |
| 240 | struct window *window = data; |
| 241 | static const GLfloat verts[3][2] = { |
| 242 | { -0.5, -0.5 }, |
| 243 | { 0.5, -0.5 }, |
| 244 | { 0, 0.5 } |
| 245 | }; |
| 246 | static const GLfloat colors[3][3] = { |
| 247 | { 1, 0, 0 }, |
| 248 | { 0, 1, 0 }, |
| 249 | { 0, 0, 1 } |
| 250 | }; |
| 251 | GLfloat angle; |
| 252 | GLfloat rotation[4][4] = { |
| 253 | { 1, 0, 0, 0 }, |
| 254 | { 0, 1, 0, 0 }, |
| 255 | { 0, 0, 1, 0 }, |
| 256 | { 0, 0, 0, 1 } |
| 257 | }; |
| 258 | static const int32_t speed_div = 5; |
| 259 | static uint32_t start_time = 0; |
| 260 | |
| 261 | if (start_time == 0) |
| 262 | start_time = time; |
| 263 | |
| 264 | angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
| 265 | rotation[0][0] = cos(angle); |
| 266 | rotation[0][2] = sin(angle); |
| 267 | rotation[2][0] = -sin(angle); |
| 268 | rotation[2][2] = cos(angle); |
| 269 | |
| 270 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 271 | (GLfloat *) rotation); |
| 272 | |
| 273 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 274 | glClear(GL_COLOR_BUFFER_BIT); |
| 275 | |
| 276 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 277 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 278 | glEnableVertexAttribArray(window->gl.pos); |
| 279 | glEnableVertexAttribArray(window->gl.col); |
| 280 | |
| 281 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 282 | |
| 283 | glDisableVertexAttribArray(window->gl.pos); |
| 284 | glDisableVertexAttribArray(window->gl.col); |
| 285 | |
| 286 | glFlush(); |
| 287 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 288 | eglSwapBuffers(window->display->egl.dpy, window->egl_surface); |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 289 | wl_display_frame_callback(window->display->display, |
| 290 | window->surface, |
| 291 | redraw, window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 295 | compositor_handle_visual(void *data, |
| 296 | struct wl_compositor *compositor, |
| 297 | uint32_t id, uint32_t token) |
| 298 | { |
| 299 | struct display *d = data; |
| 300 | |
| 301 | switch (token) { |
| 302 | case WL_COMPOSITOR_VISUAL_PREMULTIPLIED_ARGB32: |
| 303 | d->premultiplied_argb_visual = |
| 304 | wl_visual_create(d->display, id, 1); |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static const struct wl_compositor_listener compositor_listener = { |
| 310 | compositor_handle_visual, |
| 311 | }; |
| 312 | |
| 313 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 314 | display_handle_global(struct wl_display *display, uint32_t id, |
| 315 | const char *interface, uint32_t version, void *data) |
| 316 | { |
| 317 | struct display *d = data; |
| 318 | |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 319 | if (strcmp(interface, "wl_compositor") == 0) { |
Kristian Høgsberg | 91342c6 | 2011-04-14 14:44:58 -0400 | [diff] [blame] | 320 | d->compositor = wl_compositor_create(display, id, 1); |
Kristian Høgsberg | 8357cd6 | 2011-05-13 13:24:56 -0400 | [diff] [blame] | 321 | wl_compositor_add_listener(d->compositor, |
| 322 | &compositor_listener, d); |
| 323 | } |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static int |
| 327 | event_mask_update(uint32_t mask, void *data) |
| 328 | { |
| 329 | struct display *d = data; |
| 330 | |
| 331 | d->mask = mask; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int |
| 337 | main(int argc, char **argv) |
| 338 | { |
| 339 | struct display display = { 0 }; |
| 340 | struct window window = { 0 }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 341 | |
| 342 | memset(&display, 0, sizeof display); |
| 343 | memset(&window, 0, sizeof window); |
| 344 | |
| 345 | window.display = &display; |
| 346 | window.geometry.width = 250; |
| 347 | window.geometry.height = 250; |
| 348 | |
| 349 | display.display = wl_display_connect(NULL); |
| 350 | assert(display.display); |
| 351 | |
| 352 | wl_display_add_global_listener(display.display, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 353 | display_handle_global, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 354 | |
Benjamin Franzke | 65e5051 | 2011-05-31 11:36:31 +0200 | [diff] [blame^] | 355 | wl_display_get_fd(display.display, event_mask_update, &display); |
| 356 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 357 | init_egl(&display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 358 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 359 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 360 | |
Benjamin Franzke | ec4d342 | 2011-03-14 12:07:26 +0100 | [diff] [blame] | 361 | wl_display_frame_callback(display.display, window.surface, |
| 362 | redraw, &window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 363 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 364 | while (true) |
| 365 | wl_display_iterate(display.display, display.mask); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame] | 366 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 367 | return 0; |
| 368 | } |