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> |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdbool.h> |
| 29 | #include <math.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | #include <fcntl.h> |
| 33 | |
| 34 | #include <wayland-client.h> |
| 35 | #include <xf86drm.h> |
| 36 | |
| 37 | #define GL_GLEXT_PROTOTYPES |
| 38 | #define EGL_EGLEXT_PROTOTYPES |
| 39 | #include <GLES2/gl2.h> |
| 40 | #include <GLES2/gl2ext.h> |
| 41 | #include <EGL/egl.h> |
| 42 | #include <EGL/eglext.h> |
| 43 | |
| 44 | struct display { |
| 45 | struct wl_display *display; |
| 46 | struct { |
| 47 | struct wl_compositor *compositor; |
| 48 | struct wl_drm *drm; |
| 49 | } interface; |
| 50 | struct { |
| 51 | EGLDisplay dpy; |
| 52 | EGLContext ctx; |
| 53 | } egl; |
| 54 | struct { |
| 55 | int fd; |
| 56 | const char *device_name; |
| 57 | bool authenticated; |
| 58 | } drm; |
| 59 | uint32_t mask; |
| 60 | }; |
| 61 | |
| 62 | struct window { |
| 63 | struct display *display; |
| 64 | struct { |
| 65 | int width, height; |
| 66 | } geometry; |
| 67 | struct { |
| 68 | GLuint fbo; |
| 69 | GLuint color_rbo; |
| 70 | |
| 71 | GLuint program; |
| 72 | GLuint rotation_uniform; |
| 73 | |
| 74 | GLuint pos; |
| 75 | GLuint col; |
| 76 | } gl; |
| 77 | struct { |
| 78 | struct wl_buffer *buffer; |
| 79 | struct wl_surface *surface; |
| 80 | EGLImageKHR image; |
| 81 | } drm_surface; |
| 82 | }; |
| 83 | |
| 84 | static const char *vert_shader_text = |
| 85 | "uniform mat4 rotation;\n" |
| 86 | "attribute vec4 pos;\n" |
| 87 | "attribute vec4 color;\n" |
| 88 | "varying vec4 v_color;\n" |
| 89 | "void main() {\n" |
| 90 | " gl_Position = rotation * pos;\n" |
| 91 | " v_color = color;\n" |
| 92 | "}\n"; |
| 93 | |
| 94 | static const char *frag_shader_text = |
| 95 | "varying vec4 v_color;\n" |
| 96 | "void main() {\n" |
| 97 | " gl_FragColor = v_color;\n" |
| 98 | "}\n"; |
| 99 | |
| 100 | static void |
| 101 | init_egl(struct display *display) |
| 102 | { |
| 103 | EGLint major, minor; |
| 104 | EGLBoolean ret; |
| 105 | |
| 106 | display->egl.dpy = eglGetDRMDisplayMESA(display->drm.fd); |
| 107 | assert(display->egl.dpy); |
| 108 | |
| 109 | ret = eglInitialize(display->egl.dpy, &major, &minor); |
| 110 | assert(ret == EGL_TRUE); |
| 111 | ret = eglBindAPI(EGL_OPENGL_API); |
| 112 | assert(ret == EGL_TRUE); |
| 113 | |
| 114 | display->egl.ctx = eglCreateContext(display->egl.dpy, NULL, |
| 115 | EGL_NO_CONTEXT, NULL); |
| 116 | assert(display->egl.ctx); |
| 117 | ret = eglMakeCurrent(display->egl.dpy, NULL, NULL, display->egl.ctx); |
| 118 | assert(ret == EGL_TRUE); |
| 119 | } |
| 120 | |
| 121 | static GLuint |
| 122 | create_shader(struct window *window, const char *source, GLenum shader_type) |
| 123 | { |
| 124 | GLuint shader; |
| 125 | GLint status; |
| 126 | |
| 127 | shader = glCreateShader(shader_type); |
| 128 | assert(shader != 0); |
| 129 | |
| 130 | glShaderSource(shader, 1, (const char **) &source, NULL); |
| 131 | glCompileShader(shader); |
| 132 | |
| 133 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 134 | if (!status) { |
| 135 | char log[1000]; |
| 136 | GLsizei len; |
| 137 | glGetShaderInfoLog(shader, 1000, &len, log); |
| 138 | fprintf(stderr, "Error: compiling %s: %*s\n", |
| 139 | shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", |
| 140 | len, log); |
| 141 | exit(1); |
| 142 | } |
| 143 | |
| 144 | return shader; |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | init_gl(struct window *window) |
| 149 | { |
| 150 | GLfloat ar; |
| 151 | GLuint frag, vert; |
| 152 | GLint status; |
| 153 | |
| 154 | glGenFramebuffers(1, &window->gl.fbo); |
| 155 | glBindFramebuffer(GL_FRAMEBUFFER, window->gl.fbo); |
| 156 | |
| 157 | glGenRenderbuffers(1, &window->gl.color_rbo); |
| 158 | |
| 159 | glViewport(0, 0, window->geometry.width, window->geometry.height); |
| 160 | ar = (GLfloat)window->geometry.width / (GLfloat)window->geometry.height; |
| 161 | |
| 162 | frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); |
| 163 | vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); |
| 164 | |
| 165 | window->gl.program = glCreateProgram(); |
| 166 | glAttachShader(window->gl.program, frag); |
| 167 | glAttachShader(window->gl.program, vert); |
| 168 | glLinkProgram(window->gl.program); |
| 169 | |
| 170 | glGetProgramiv(window->gl.program, GL_LINK_STATUS, &status); |
| 171 | if (!status) { |
| 172 | char log[1000]; |
| 173 | GLsizei len; |
| 174 | glGetProgramInfoLog(window->gl.program, 1000, &len, log); |
| 175 | fprintf(stderr, "Error: linking:\n%*s\n", len, log); |
| 176 | exit(1); |
| 177 | } |
| 178 | |
| 179 | glUseProgram(window->gl.program); |
| 180 | |
| 181 | window->gl.pos = 0; |
| 182 | window->gl.pos = 1; |
| 183 | |
| 184 | glBindAttribLocation(window->gl.program, window->gl.pos, "pos"); |
| 185 | glBindAttribLocation(window->gl.program, window->gl.col, "color"); |
| 186 | glLinkProgram(window->gl.program); |
| 187 | |
| 188 | window->gl.rotation_uniform = |
| 189 | glGetUniformLocation(window->gl.program, "rotation"); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | create_surface(struct window *window) |
| 194 | { |
| 195 | struct display *display = window->display; |
| 196 | struct wl_visual *visual; |
| 197 | EGLint name, stride; |
| 198 | EGLint image_attribs[] = { |
| 199 | EGL_WIDTH, 0, |
| 200 | EGL_HEIGHT, 0, |
| 201 | EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA, |
| 202 | EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA, |
| 203 | EGL_NONE |
| 204 | }; |
| 205 | |
| 206 | window->drm_surface.surface = |
| 207 | wl_compositor_create_surface(display->interface.compositor); |
| 208 | |
| 209 | image_attribs[1] = window->geometry.width; |
| 210 | image_attribs[3] = window->geometry.height; |
| 211 | |
| 212 | window->drm_surface.image = eglCreateDRMImageMESA(display->egl.dpy, |
| 213 | image_attribs); |
| 214 | eglExportDRMImageMESA(display->egl.dpy, window->drm_surface.image, |
| 215 | &name, NULL, &stride); |
| 216 | visual = wl_display_get_premultiplied_argb_visual(display->display); |
| 217 | |
| 218 | window->drm_surface.buffer = |
| 219 | wl_drm_create_buffer(display->interface.drm, name, |
| 220 | window->geometry.width, |
| 221 | window->geometry.height, |
| 222 | stride, visual); |
| 223 | /* Process wl_drm_create_buffer. */ |
| 224 | wl_display_iterate(display->display, WL_DISPLAY_WRITABLE); |
| 225 | |
| 226 | wl_surface_attach(window->drm_surface.surface, |
| 227 | window->drm_surface.buffer, 0, 0); |
| 228 | wl_surface_map_toplevel(window->drm_surface.surface); |
| 229 | |
| 230 | glBindRenderbuffer(GL_RENDERBUFFER, window->gl.color_rbo); |
| 231 | glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, |
| 232 | window->drm_surface.image); |
| 233 | |
| 234 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, |
| 235 | GL_COLOR_ATTACHMENT0, |
| 236 | GL_RENDERBUFFER, |
| 237 | window->gl.color_rbo); |
| 238 | |
| 239 | assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == |
| 240 | GL_FRAMEBUFFER_COMPLETE); |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | redraw(void *data, uint32_t time) |
| 245 | { |
| 246 | struct window *window = data; |
| 247 | static const GLfloat verts[3][2] = { |
| 248 | { -0.5, -0.5 }, |
| 249 | { 0.5, -0.5 }, |
| 250 | { 0, 0.5 } |
| 251 | }; |
| 252 | static const GLfloat colors[3][3] = { |
| 253 | { 1, 0, 0 }, |
| 254 | { 0, 1, 0 }, |
| 255 | { 0, 0, 1 } |
| 256 | }; |
| 257 | GLfloat angle; |
| 258 | GLfloat rotation[4][4] = { |
| 259 | { 1, 0, 0, 0 }, |
| 260 | { 0, 1, 0, 0 }, |
| 261 | { 0, 0, 1, 0 }, |
| 262 | { 0, 0, 0, 1 } |
| 263 | }; |
| 264 | static const int32_t speed_div = 5; |
| 265 | static uint32_t start_time = 0; |
| 266 | |
| 267 | if (start_time == 0) |
| 268 | start_time = time; |
| 269 | |
| 270 | angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; |
| 271 | rotation[0][0] = cos(angle); |
| 272 | rotation[0][2] = sin(angle); |
| 273 | rotation[2][0] = -sin(angle); |
| 274 | rotation[2][2] = cos(angle); |
| 275 | |
| 276 | glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, |
| 277 | (GLfloat *) rotation); |
| 278 | |
| 279 | glClearColor(0.0, 0.0, 0.0, 0.5); |
| 280 | glClear(GL_COLOR_BUFFER_BIT); |
| 281 | |
| 282 | glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); |
| 283 | glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); |
| 284 | glEnableVertexAttribArray(window->gl.pos); |
| 285 | glEnableVertexAttribArray(window->gl.col); |
| 286 | |
| 287 | glDrawArrays(GL_TRIANGLES, 0, 3); |
| 288 | |
| 289 | glDisableVertexAttribArray(window->gl.pos); |
| 290 | glDisableVertexAttribArray(window->gl.col); |
| 291 | |
| 292 | glFlush(); |
| 293 | |
| 294 | wl_surface_damage(window->drm_surface.surface, 0, 0, |
| 295 | window->geometry.width, window->geometry.height); |
| 296 | |
| 297 | wl_display_frame_callback(window->display->display, redraw, window); |
| 298 | } |
| 299 | |
| 300 | static void |
| 301 | drm_handle_device(void *data, struct wl_drm *drm, const char *device) |
| 302 | { |
| 303 | struct display *d = data; |
| 304 | |
| 305 | d->drm.device_name = strdup(device); |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | drm_handle_authenticated(void *data, struct wl_drm *drm) |
| 310 | { |
| 311 | struct display *d = data; |
| 312 | |
| 313 | d->drm.authenticated = true; |
| 314 | } |
| 315 | |
| 316 | static const struct wl_drm_listener drm_listener = { |
| 317 | drm_handle_device, |
| 318 | drm_handle_authenticated |
| 319 | }; |
| 320 | |
| 321 | static void |
| 322 | display_handle_global(struct wl_display *display, uint32_t id, |
| 323 | const char *interface, uint32_t version, void *data) |
| 324 | { |
| 325 | struct display *d = data; |
| 326 | |
| 327 | if (strcmp(interface, "compositor") == 0) { |
| 328 | d->interface.compositor = wl_compositor_create(display, id); |
| 329 | } else if (strcmp(interface, "drm") == 0) { |
| 330 | d->interface.drm = wl_drm_create(display, id); |
| 331 | wl_drm_add_listener(d->interface.drm, &drm_listener, d); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static int |
| 336 | event_mask_update(uint32_t mask, void *data) |
| 337 | { |
| 338 | struct display *d = data; |
| 339 | |
| 340 | d->mask = mask; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | int |
| 346 | main(int argc, char **argv) |
| 347 | { |
| 348 | struct display display = { 0 }; |
| 349 | struct window window = { 0 }; |
| 350 | drm_magic_t magic; |
| 351 | int ret; |
| 352 | |
| 353 | memset(&display, 0, sizeof display); |
| 354 | memset(&window, 0, sizeof window); |
| 355 | |
| 356 | window.display = &display; |
| 357 | window.geometry.width = 250; |
| 358 | window.geometry.height = 250; |
| 359 | |
| 360 | display.display = wl_display_connect(NULL); |
| 361 | assert(display.display); |
| 362 | |
| 363 | wl_display_add_global_listener(display.display, |
| 364 | display_handle_global, &display); |
| 365 | /* process connection events */ |
| 366 | wl_display_iterate(display.display, WL_DISPLAY_READABLE); |
| 367 | |
| 368 | display.drm.fd = open(display.drm.device_name, O_RDWR); |
| 369 | assert(display.drm.fd >= 0); |
| 370 | |
| 371 | ret = drmGetMagic(display.drm.fd, &magic); |
| 372 | assert(ret == 0); |
| 373 | wl_drm_authenticate(display.interface.drm, magic); |
| 374 | wl_display_iterate(display.display, WL_DISPLAY_WRITABLE); |
| 375 | while (!display.drm.authenticated) |
| 376 | wl_display_iterate(display.display, WL_DISPLAY_READABLE); |
| 377 | |
| 378 | init_egl(&display); |
| 379 | init_gl(&window); |
| 380 | create_surface(&window); |
| 381 | |
| 382 | wl_display_frame_callback(display.display, redraw, &window); |
| 383 | |
| 384 | wl_display_get_fd(display.display, event_mask_update, &display); |
| 385 | while (true) |
| 386 | wl_display_iterate(display.display, display.mask); |
| 387 | |
| 388 | return 0; |
| 389 | } |