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 | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 38 | struct wl_egl_display *native; |
| 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, |
| 99 | EGL_DEPTH_SIZE, 1, |
| 100 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 101 | EGL_NONE |
| 102 | }; |
| 103 | |
| 104 | EGLint major, minor, n; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 105 | EGLBoolean ret; |
| 106 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 107 | display->egl.dpy = |
| 108 | eglGetDisplay((EGLNativeDisplayType) display->native); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 109 | assert(display->egl.dpy); |
| 110 | |
| 111 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 112 | assert(ret == EGL_TRUE); |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 113 | ret = eglBindAPI(EGL_OPENGL_ES_API); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 114 | assert(ret == EGL_TRUE); |
| 115 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 116 | assert(eglChooseConfig(display->egl.dpy, config_attribs, |
| 117 | &display->egl.conf, 1, &n) && n == 1); |
| 118 | |
| 119 | display->egl.ctx = eglCreateContext(display->egl.dpy, |
| 120 | display->egl.conf, |
Kristian Høgsberg | 1a11fac | 2011-01-14 20:39:21 -0500 | [diff] [blame] | 121 | EGL_NO_CONTEXT, context_attribs); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 122 | assert(display->egl.ctx); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 123 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static GLuint |
| 127 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 128 | { |
| 129 | GLuint shader; |
| 130 | GLint status; |
| 131 | |
| 132 | shader = glCreateShader(shader_type); |
| 133 | assert(shader != 0); |
| 134 | |
| 135 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 136 | glCompileShader(shader); |
| 137 | |
| 138 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 139 | if (!status) { |
| 140 | char log[1000]; |
| 141 | GLsizei len; |
| 142 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 143 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 144 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 145 | len, log); |
| 146 | exit(1); |
| 147 | } |
| 148 | |
| 149 | return shader; |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | init_gl(struct window *window) |
| 154 | { |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 155 | GLuint frag, vert; |
| 156 | GLint status; |
| 157 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 158 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 159 | |
| 160 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 161 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 162 | |
| 163 | window->gl.program = glCreateProgram(); |
| 164 | glAttachShader(window->gl.program, frag); |
| 165 | glAttachShader(window->gl.program, vert); |
| 166 | glLinkProgram(window->gl.program); |
| 167 | |
| 168 | glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); |
| 169 | if (!status) { |
| 170 | char log[1000]; |
| 171 | GLsizei len; |
| 172 | glGetProgramInfoLog(window->gl.program, 1000, &len, log); |
| 173 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | glUseProgram(window->gl.program); |
| 178 | |
| 179 | window->gl.pos = 0; |
| 180 | window->gl.pos = 1; |
| 181 | |
| 182 | glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); |
| 183 | glBindAttribLocation(window->gl.program, window->gl.col, "color"); |
| 184 | glLinkProgram(window->gl.program); |
| 185 | |
| 186 | window->gl.rotation_uniform = |
| 187 | glGetUniformLocation(window->gl.program, "rotation"); |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | create_surface(struct window *window) |
| 192 | { |
| 193 | struct display *display = window->display; |
| 194 | struct wl_visual *visual; |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 195 | EGLBoolean ret; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 196 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 197 | window->surface = wl_compositor_create_surface(display->compositor); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 198 | visual = wl_display_get_premultiplied_argb_visual(display->display); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 199 | window->native = |
| 200 | wl_egl_native_window_create(window->surface, |
| 201 | window->geometry.width, |
| 202 | window->geometry.height, |
| 203 | visual); |
| 204 | window->egl_surface = |
| 205 | eglCreateWindowSurface(display->egl.dpy, |
| 206 | display->egl.conf, |
| 207 | (EGLNativeWindowType) window->native, |
| 208 | NULL); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 209 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 210 | wl_surface_map_toplevel(window->surface); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 211 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 212 | ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, |
| 213 | window->egl_surface, window->display->egl.ctx); |
| 214 | assert(ret == EGL_TRUE); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void |
| 218 | redraw(void *data, uint32_t time) |
| 219 | { |
| 220 | struct window *window = data; |
| 221 | static const GLfloat verts[3][2] = { |
| 222 | { -0.5, -0.5 }, |
| 223 | { 0.5, -0.5 }, |
| 224 | { 0, 0.5 } |
| 225 | }; |
| 226 | static const GLfloat colors[3][3] = { |
| 227 | { 1, 0, 0 }, |
| 228 | { 0, 1, 0 }, |
| 229 | { 0, 0, 1 } |
| 230 | }; |
| 231 | GLfloat angle; |
| 232 | GLfloat rotation[4][4] = { |
| 233 | { 1, 0, 0, 0 }, |
| 234 | { 0, 1, 0, 0 }, |
| 235 | { 0, 0, 1, 0 }, |
| 236 | { 0, 0, 0, 1 } |
| 237 | }; |
| 238 | static const int32_t speed_div = 5; |
| 239 | static uint32_t start_time = 0; |
| 240 | |
| 241 | if (start_time == 0) |
| 242 | start_time = time; |
| 243 | |
| 244 | angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
| 245 | rotation[0][0] = cos(angle); |
| 246 | rotation[0][2] = sin(angle); |
| 247 | rotation[2][0] = -sin(angle); |
| 248 | rotation[2][2] = cos(angle); |
| 249 | |
| 250 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 251 | (GLfloat *) rotation); |
| 252 | |
| 253 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 254 | glClear(GL_COLOR_BUFFER_BIT); |
| 255 | |
| 256 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 257 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 258 | glEnableVertexAttribArray(window->gl.pos); |
| 259 | glEnableVertexAttribArray(window->gl.col); |
| 260 | |
| 261 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 262 | |
| 263 | glDisableVertexAttribArray(window->gl.pos); |
| 264 | glDisableVertexAttribArray(window->gl.col); |
| 265 | |
| 266 | glFlush(); |
| 267 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 268 | eglSwapBuffers(window->display->egl.dpy, window->egl_surface); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 269 | wl_display_frame_callback(window->display->display, redraw, window); |
| 270 | } |
| 271 | |
| 272 | static void |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 273 | display_handle_global(struct wl_display *display, uint32_t id, |
| 274 | const char *interface, uint32_t version, void *data) |
| 275 | { |
| 276 | struct display *d = data; |
| 277 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 278 | if (strcmp(interface, "compositor") == 0) |
| 279 | d->compositor = wl_compositor_create(display, id); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static int |
| 283 | event_mask_update(uint32_t mask, void *data) |
| 284 | { |
| 285 | struct display *d = data; |
| 286 | |
| 287 | d->mask = mask; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int |
| 293 | main(int argc, char **argv) |
| 294 | { |
| 295 | struct display display = { 0 }; |
| 296 | struct window window = { 0 }; |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 297 | |
| 298 | memset(&display, 0, sizeof display); |
| 299 | memset(&window, 0, sizeof window); |
| 300 | |
| 301 | window.display = &display; |
| 302 | window.geometry.width = 250; |
| 303 | window.geometry.height = 250; |
| 304 | |
| 305 | display.display = wl_display_connect(NULL); |
| 306 | assert(display.display); |
| 307 | |
| 308 | wl_display_add_global_listener(display.display, |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 309 | display_handle_global, &display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 310 | |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 311 | display.native = wl_egl_native_display_create(display.display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 312 | |
| 313 | init_egl(&display); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 314 | create_surface(&window); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 315 | init_gl(&window); |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 316 | |
| 317 | wl_display_frame_callback(display.display, redraw, &window); |
| 318 | |
| 319 | wl_display_get_fd(display.display, event_mask_update, &display); |
| 320 | while (true) |
| 321 | wl_display_iterate(display.display, display.mask); |
Kristian Høgsberg | a495a5e | 2011-02-04 15:31:33 -0500 | [diff] [blame^] | 322 | |
Benjamin Franzke | aabdce0 | 2011-01-15 00:40:17 +0100 | [diff] [blame] | 323 | return 0; |
| 324 | } |