Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 5 | #include <stdarg.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 6 | #include <i915_drm.h> |
| 7 | #include <sys/ioctl.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <unistd.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 11 | #include <signal.h> |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 12 | #include <cairo.h> |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 13 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 14 | #include <glib.h> |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 15 | #include <png.h> |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 16 | |
| 17 | #include "wayland.h" |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 18 | #include "cairo-util.h" |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 19 | |
| 20 | #include <GL/gl.h> |
| 21 | #include <eagle.h> |
| 22 | |
| 23 | #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) |
| 24 | |
| 25 | struct egl_compositor { |
| 26 | struct wl_compositor base; |
| 27 | EGLDisplay display; |
| 28 | EGLSurface surface; |
| 29 | EGLContext context; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 30 | EGLConfig config; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 31 | struct wl_display *wl_display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 32 | int gem_fd; |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 33 | int width, height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 34 | struct egl_surface *pointer; |
| 35 | struct egl_surface *background; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 36 | }; |
| 37 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 38 | struct egl_surface { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 39 | GLuint texture; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 40 | struct wl_map map; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 41 | EGLSurface surface; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 42 | }; |
| 43 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 44 | static int do_screenshot; |
| 45 | |
| 46 | static void |
| 47 | handle_sigusr1(int s) |
| 48 | { |
| 49 | do_screenshot = 1; |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | die(const char *msg, ...) |
| 54 | { |
| 55 | va_list ap; |
| 56 | |
| 57 | va_start (ap, msg); |
| 58 | vfprintf(stderr, msg, ap); |
| 59 | va_end (ap); |
| 60 | |
| 61 | exit(EXIT_FAILURE); |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | stdio_write_func (png_structp png, png_bytep data, png_size_t size) |
| 66 | { |
| 67 | FILE *fp; |
| 68 | size_t ret; |
| 69 | |
| 70 | fp = png_get_io_ptr (png); |
| 71 | while (size) { |
| 72 | ret = fwrite (data, 1, size, fp); |
| 73 | size -= ret; |
| 74 | data += ret; |
| 75 | if (size && ferror (fp)) |
| 76 | die("write: %m\n"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | png_simple_output_flush_fn (png_structp png_ptr) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | png_simple_error_callback (png_structp png, |
| 87 | png_const_charp error_msg) |
| 88 | { |
| 89 | die("png error: %s\n", error_msg); |
| 90 | } |
| 91 | |
| 92 | static void |
| 93 | png_simple_warning_callback (png_structp png, |
| 94 | png_const_charp error_msg) |
| 95 | { |
| 96 | fprintf(stderr, "png warning: %s\n", error_msg); |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | convert_pixels(png_structp png, png_row_infop row_info, png_bytep data) |
| 101 | { |
| 102 | unsigned int i; |
| 103 | |
| 104 | for (i = 0; i < row_info->rowbytes; i += 4) { |
| 105 | uint8_t *b = &data[i]; |
| 106 | uint32_t pixel; |
| 107 | |
| 108 | memcpy (&pixel, b, sizeof (uint32_t)); |
| 109 | b[0] = (pixel & 0xff0000) >> 16; |
| 110 | b[1] = (pixel & 0x00ff00) >> 8; |
| 111 | b[2] = (pixel & 0x0000ff) >> 0; |
| 112 | b[3] = 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void |
| 117 | screenshot(struct egl_compositor *ec) |
| 118 | { |
| 119 | png_struct *png; |
| 120 | png_info *info; |
| 121 | png_byte **volatile rows = NULL; |
| 122 | png_color_16 white; |
| 123 | int depth, i; |
| 124 | FILE *fp; |
| 125 | uint8_t *data; |
| 126 | GLuint stride; |
| 127 | static const char filename[] = "wayland-screenshot.png"; |
| 128 | |
| 129 | data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride); |
| 130 | if (data == NULL) |
| 131 | die("eglReadBuffer failed\n"); |
| 132 | rows = malloc(ec->height * sizeof rows[0]); |
| 133 | if (rows == NULL) |
| 134 | die("malloc failed\n"); |
| 135 | |
| 136 | for (i = 0; i < ec->height; i++) |
| 137 | rows[i] = (png_byte *) data + i * stride; |
| 138 | |
| 139 | png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 140 | png_simple_error_callback, |
| 141 | png_simple_warning_callback); |
| 142 | if (png == NULL) |
| 143 | die("png_create_write_struct failed\n"); |
| 144 | |
| 145 | info = png_create_info_struct(png); |
| 146 | if (info == NULL) |
| 147 | die("png_create_info_struct failed\n"); |
| 148 | |
| 149 | fp = fopen(filename, "w"); |
| 150 | if (fp == NULL) |
| 151 | die("fopen failed: %m\n"); |
| 152 | |
| 153 | png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn); |
| 154 | |
| 155 | depth = 8; |
| 156 | png_set_IHDR(png, info, |
| 157 | ec->width, |
| 158 | ec->height, depth, |
| 159 | PNG_COLOR_TYPE_RGB, |
| 160 | PNG_INTERLACE_NONE, |
| 161 | PNG_COMPRESSION_TYPE_DEFAULT, |
| 162 | PNG_FILTER_TYPE_DEFAULT); |
| 163 | |
| 164 | white.gray = (1 << depth) - 1; |
| 165 | white.red = white.blue = white.green = white.gray; |
| 166 | png_set_bKGD(png, info, &white); |
| 167 | png_write_info (png, info); |
| 168 | png_set_write_user_transform_fn(png, convert_pixels); |
| 169 | |
| 170 | png_set_filler(png, 0, PNG_FILLER_AFTER); |
| 171 | png_write_image(png, rows); |
| 172 | png_write_end(png, info); |
| 173 | |
| 174 | png_destroy_write_struct(&png, &info); |
| 175 | fclose(fp); |
| 176 | free(rows); |
| 177 | free(data); |
| 178 | } |
| 179 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 180 | static void |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 181 | pointer_path(cairo_t *cr, int x, int y) |
| 182 | { |
| 183 | const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10; |
| 184 | const int width = 16, height = 16; |
| 185 | |
| 186 | cairo_move_to(cr, x, y); |
| 187 | cairo_line_to(cr, x + tx, y + ty); |
| 188 | cairo_line_to(cr, x + dx, y + dy); |
| 189 | cairo_line_to(cr, x + width - end, y + height); |
| 190 | cairo_line_to(cr, x + width, y + height - end); |
| 191 | cairo_line_to(cr, x + dy, y + dx); |
| 192 | cairo_line_to(cr, x + ty, y + tx); |
| 193 | cairo_close_path(cr); |
| 194 | } |
| 195 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 196 | static struct egl_surface * |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 197 | pointer_create(int x, int y, int width, int height) |
| 198 | { |
| 199 | const int hotspot_x = 16, hotspot_y = 16; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 200 | struct egl_surface *pointer; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 201 | cairo_surface_t *surface; |
| 202 | cairo_t *cr; |
| 203 | int stride; |
| 204 | void *data; |
| 205 | |
| 206 | pointer = malloc(sizeof *pointer); |
| 207 | if (pointer == NULL) |
| 208 | return NULL; |
| 209 | |
| 210 | surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 211 | width, height); |
| 212 | |
| 213 | cr = cairo_create(surface); |
| 214 | pointer_path(cr, hotspot_x + 5, hotspot_y + 4); |
| 215 | cairo_set_line_width (cr, 2); |
| 216 | cairo_set_source_rgb(cr, 0, 0, 0); |
| 217 | cairo_stroke_preserve(cr); |
| 218 | cairo_fill(cr); |
| 219 | blur_surface(surface, width); |
| 220 | |
| 221 | pointer_path(cr, hotspot_x, hotspot_y); |
| 222 | cairo_stroke_preserve(cr); |
| 223 | cairo_set_source_rgb(cr, 1, 1, 1); |
| 224 | cairo_fill(cr); |
| 225 | cairo_destroy(cr); |
| 226 | |
| 227 | stride = cairo_image_surface_get_stride(surface); |
| 228 | data = cairo_image_surface_get_data(surface); |
| 229 | |
| 230 | glGenTextures(1, &pointer->texture); |
| 231 | glBindTexture(GL_TEXTURE_2D, pointer->texture); |
| 232 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 233 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 234 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 235 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 236 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, |
| 237 | GL_BGRA, GL_UNSIGNED_BYTE, data); |
| 238 | |
| 239 | cairo_surface_destroy(surface); |
| 240 | |
| 241 | pointer->map.x = x; |
| 242 | pointer->map.y = y; |
| 243 | pointer->map.width = width; |
| 244 | pointer->map.height = height; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 245 | pointer->surface = EGL_NO_SURFACE; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 246 | |
| 247 | return pointer; |
| 248 | } |
| 249 | |
| 250 | static void |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 251 | egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 252 | { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 253 | glDeleteTextures(1, &es->texture); |
| 254 | if (es->surface != EGL_NO_SURFACE) |
| 255 | eglDestroySurface(ec->display, es->surface); |
| 256 | free(es); |
| 257 | } |
| 258 | |
| 259 | static struct egl_surface * |
| 260 | background_create(const char *filename, int width, int height) |
| 261 | { |
| 262 | struct egl_surface *background; |
| 263 | GdkPixbuf *pixbuf; |
| 264 | GError *error = NULL; |
| 265 | int pixbuf_width, pixbuf_height; |
| 266 | void *data; |
| 267 | |
| 268 | background = malloc(sizeof *background); |
| 269 | if (background == NULL) |
| 270 | return NULL; |
| 271 | |
| 272 | g_type_init(); |
| 273 | |
| 274 | pixbuf = gdk_pixbuf_new_from_file(filename, &error); |
| 275 | if (error != NULL) { |
| 276 | free(background); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | pixbuf_width = gdk_pixbuf_get_width(pixbuf); |
| 281 | pixbuf_height = gdk_pixbuf_get_height(pixbuf); |
| 282 | data = gdk_pixbuf_get_pixels(pixbuf); |
| 283 | |
| 284 | glGenTextures(1, &background->texture); |
| 285 | glBindTexture(GL_TEXTURE_2D, background->texture); |
| 286 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 287 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 288 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 289 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 290 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0, |
| 291 | GL_BGR, GL_UNSIGNED_BYTE, data); |
| 292 | |
| 293 | background->map.x = 0; |
| 294 | background->map.y = 0; |
| 295 | background->map.width = width; |
| 296 | background->map.height = height; |
| 297 | background->surface = EGL_NO_SURFACE; |
| 298 | |
| 299 | return background; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 303 | draw_surface(struct egl_surface *es) |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 304 | { |
| 305 | GLint vertices[12]; |
| 306 | GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 }; |
| 307 | GLuint indices[4] = { 0, 1, 2, 3 }; |
| 308 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 309 | vertices[0] = es->map.x; |
| 310 | vertices[1] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 311 | vertices[2] = 0; |
| 312 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 313 | vertices[3] = es->map.x; |
| 314 | vertices[4] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 315 | vertices[5] = 0; |
| 316 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 317 | vertices[6] = es->map.x + es->map.width; |
| 318 | vertices[7] = es->map.y; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 319 | vertices[8] = 0; |
| 320 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 321 | vertices[9] = es->map.x + es->map.width; |
| 322 | vertices[10] = es->map.y + es->map.height; |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 323 | vertices[11] = 0; |
| 324 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 325 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 326 | glEnable(GL_TEXTURE_2D); |
| 327 | glEnable(GL_BLEND); |
| 328 | /* Assume pre-multiplied alpha for now, this probably |
| 329 | * needs to be a wayland visual type of thing. */ |
| 330 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 331 | |
| 332 | glEnableClientState(GL_VERTEX_ARRAY); |
| 333 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 334 | glVertexPointer(3, GL_INT, 0, vertices); |
| 335 | glTexCoordPointer(2, GL_INT, 0, tex_coords); |
| 336 | glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices); |
| 337 | } |
| 338 | |
| 339 | static void |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 340 | repaint(void *data) |
| 341 | { |
| 342 | struct egl_compositor *ec = data; |
| 343 | struct wl_surface_iterator *iterator; |
| 344 | struct wl_surface *surface; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 345 | struct egl_surface *es; |
| 346 | |
| 347 | draw_surface(ec->background); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 348 | |
| 349 | iterator = wl_surface_iterator_create(ec->wl_display, 0); |
| 350 | while (wl_surface_iterator_next(iterator, &surface)) { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 351 | es = wl_surface_get_data(surface); |
| 352 | if (es == NULL) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 353 | continue; |
| 354 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 355 | draw_surface(es); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 356 | } |
| 357 | wl_surface_iterator_destroy(iterator); |
| 358 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 359 | draw_surface(ec->pointer); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 360 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 361 | eglSwapBuffers(ec->display, ec->surface); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 362 | |
| 363 | if (do_screenshot) { |
| 364 | glFinish(); |
| 365 | /* FIXME: There's a bug somewhere so that glFinish() |
| 366 | * doesn't actually wait for all rendering to finish. |
| 367 | * I *think* it's fixed in upstream drm, but for my |
| 368 | * kernel I need this sleep now... */ |
| 369 | sleep(1); |
| 370 | screenshot(ec); |
| 371 | do_screenshot = 0; |
| 372 | } |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 375 | static void |
| 376 | schedule_repaint(struct egl_compositor *ec) |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 377 | { |
| 378 | struct wl_event_loop *loop; |
| 379 | |
| 380 | loop = wl_display_get_event_loop(ec->wl_display); |
| 381 | wl_event_loop_add_idle(loop, repaint, ec); |
| 382 | } |
| 383 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 384 | static void |
| 385 | notify_surface_create(struct wl_compositor *compositor, |
| 386 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 387 | { |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 388 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 389 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 390 | es = malloc(sizeof *es); |
| 391 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 392 | return; |
| 393 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 394 | es->surface = EGL_NO_SURFACE; |
| 395 | wl_surface_set_data(surface, es); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 396 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 397 | glGenTextures(1, &es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 400 | static void |
| 401 | notify_surface_destroy(struct wl_compositor *compositor, |
| 402 | struct wl_surface *surface) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 403 | { |
| 404 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 405 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 406 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 407 | es = wl_surface_get_data(surface); |
| 408 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 409 | return; |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 410 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 411 | egl_surface_destroy(es, ec); |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 412 | |
| 413 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 414 | } |
| 415 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 416 | static void |
| 417 | notify_surface_attach(struct wl_compositor *compositor, |
| 418 | struct wl_surface *surface, uint32_t name, |
| 419 | uint32_t width, uint32_t height, uint32_t stride) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 420 | { |
| 421 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 422 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 423 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 424 | es = wl_surface_get_data(surface); |
| 425 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 426 | return; |
| 427 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 428 | if (es->surface != EGL_NO_SURFACE) |
| 429 | eglDestroySurface(ec->display, es->surface); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 430 | |
Kristian Høgsberg | 56f3c71 | 2008-11-05 07:55:45 -0500 | [diff] [blame] | 431 | /* FIXME: We need to use a single buffer config without depth |
| 432 | * or stencil buffers here to keep egl from creating auxillary |
| 433 | * buffers for the pixmap here. */ |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 434 | es->surface = eglCreateSurfaceForName(ec->display, ec->config, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 435 | name, width, height, stride, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 436 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 437 | glBindTexture(GL_TEXTURE_2D, es->texture); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 438 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 439 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT); |
| 440 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 441 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 442 | eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 443 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 444 | schedule_repaint(ec); |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 447 | static void |
| 448 | notify_surface_map(struct wl_compositor *compositor, |
| 449 | struct wl_surface *surface, struct wl_map *map) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 450 | { |
| 451 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 452 | struct egl_surface *es; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 453 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 454 | es = wl_surface_get_data(surface); |
| 455 | if (es == NULL) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 456 | return; |
| 457 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 458 | es->map = *map; |
Kristian Høgsberg | 5ebb317 | 2008-10-11 19:21:35 -0400 | [diff] [blame] | 459 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 460 | schedule_repaint(ec); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 463 | static void |
| 464 | notify_surface_copy(struct wl_compositor *compositor, |
| 465 | struct wl_surface *surface, |
| 466 | int32_t dst_x, int32_t dst_y, |
| 467 | uint32_t name, uint32_t stride, |
| 468 | int32_t x, int32_t y, int32_t width, int32_t height) |
| 469 | { |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 470 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 471 | EGLSurface src; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 472 | struct egl_surface *es; |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 473 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 474 | es = wl_surface_get_data(surface); |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 475 | |
| 476 | /* FIXME: glCopyPixels should work, but then we'll have to |
| 477 | * call eglMakeCurrent to set up the src and dest surfaces |
| 478 | * first. This seems cheaper, but maybe there's a better way |
| 479 | * to accomplish this. */ |
| 480 | |
| 481 | src = eglCreateSurfaceForName(ec->display, ec->config, |
| 482 | name, x + width, y + height, stride, NULL); |
| 483 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 484 | eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 485 | src, GL_FRONT_LEFT, x, y, width, height); |
| 486 | schedule_repaint(ec); |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void |
| 490 | notify_surface_damage(struct wl_compositor *compositor, |
Kristian Høgsberg | 78231c8 | 2008-11-08 15:06:01 -0500 | [diff] [blame] | 491 | struct wl_surface *surface, |
| 492 | int32_t x, int32_t y, int32_t width, int32_t height) |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 493 | { |
| 494 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 495 | |
| 496 | /* FIXME: This need to take a damage region, of course. */ |
| 497 | schedule_repaint(ec); |
| 498 | } |
| 499 | |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 500 | static void |
| 501 | notify_pointer_motion(struct wl_compositor *compositor, |
| 502 | struct wl_object *source, int x, int y) |
| 503 | { |
| 504 | struct egl_compositor *ec = (struct egl_compositor *) compositor; |
| 505 | |
| 506 | ec->pointer->map.x = x; |
| 507 | ec->pointer->map.y = y; |
| 508 | schedule_repaint(ec); |
| 509 | } |
| 510 | |
Kristian Høgsberg | 5503bf8 | 2008-11-06 10:38:17 -0500 | [diff] [blame] | 511 | static const struct wl_compositor_interface interface = { |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 512 | notify_surface_create, |
| 513 | notify_surface_destroy, |
| 514 | notify_surface_attach, |
Kristian Høgsberg | 7f77bd8 | 2008-11-07 08:39:37 -0500 | [diff] [blame] | 515 | notify_surface_map, |
| 516 | notify_surface_copy, |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 517 | notify_surface_damage, |
| 518 | notify_pointer_motion |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 519 | }; |
| 520 | |
| 521 | static const char gem_device[] = "/dev/dri/card0"; |
| 522 | |
Kristian Høgsberg | b7a0192 | 2008-11-08 15:39:41 -0500 | [diff] [blame] | 523 | WL_EXPORT struct wl_compositor * |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 524 | wl_compositor_create(struct wl_display *display) |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 525 | { |
| 526 | EGLConfig configs[64]; |
| 527 | EGLint major, minor, count; |
| 528 | struct egl_compositor *ec; |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 529 | const char *filename; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 530 | |
| 531 | ec = malloc(sizeof *ec); |
| 532 | if (ec == NULL) |
| 533 | return NULL; |
| 534 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 535 | ec->width = 1280; |
| 536 | ec->height = 800; |
| 537 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 538 | ec->base.interface = &interface; |
Kristian Høgsberg | f921289 | 2008-10-11 18:40:23 -0400 | [diff] [blame] | 539 | ec->wl_display = display; |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 540 | |
Kristian Høgsberg | c508d93 | 2008-10-13 22:52:42 -0400 | [diff] [blame] | 541 | ec->display = eglCreateDisplayNative(gem_device, "i965"); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 542 | if (ec->display == NULL) { |
| 543 | fprintf(stderr, "failed to create display\n"); |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | if (!eglInitialize(ec->display, &major, &minor)) { |
| 548 | fprintf(stderr, "failed to initialize display\n"); |
| 549 | return NULL; |
| 550 | } |
| 551 | |
| 552 | if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) { |
| 553 | fprintf(stderr, "failed to get configs\n"); |
| 554 | return NULL; |
| 555 | } |
| 556 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 557 | ec->config = configs[24]; |
| 558 | ec->surface = eglCreateSurfaceNative(ec->display, ec->config, |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 559 | 0, 0, ec->width, ec->height); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 560 | if (ec->surface == NULL) { |
| 561 | fprintf(stderr, "failed to create surface\n"); |
| 562 | return NULL; |
| 563 | } |
| 564 | |
Kristian Høgsberg | 2d9cd1e | 2008-11-03 08:09:34 -0500 | [diff] [blame] | 565 | ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 566 | if (ec->context == NULL) { |
| 567 | fprintf(stderr, "failed to create context\n"); |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) { |
| 572 | fprintf(stderr, "failed to make context current\n"); |
| 573 | return NULL; |
| 574 | } |
| 575 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 576 | glViewport(0, 0, ec->width, ec->height); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 577 | glMatrixMode(GL_PROJECTION); |
| 578 | glLoadIdentity(); |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 579 | glOrtho(0, ec->width, ec->height, 0, 0, 1000.0); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 580 | glMatrixMode(GL_MODELVIEW); |
Kristian Høgsberg | a234e70 | 2008-10-11 22:13:51 -0400 | [diff] [blame] | 581 | glClearColor(0.0, 0.05, 0.2, 0.0); |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 582 | |
Kristian Høgsberg | aa5b5be | 2008-11-21 21:31:54 -0500 | [diff] [blame^] | 583 | filename = getenv("WAYLAND_BACKGROUND"); |
| 584 | if (filename == NULL) |
| 585 | filename = "background.jpg"; |
| 586 | ec->background = background_create(filename, 1280, 800); |
Kristian Høgsberg | 4c9f2c9 | 2008-11-21 19:25:44 -0500 | [diff] [blame] | 587 | ec->pointer = pointer_create(100, 100, 64, 64); |
| 588 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 589 | ec->gem_fd = open(gem_device, O_RDWR); |
| 590 | if (ec->gem_fd < 0) { |
| 591 | fprintf(stderr, "failed to open drm device\n"); |
| 592 | return NULL; |
| 593 | } |
| 594 | |
Kristian Høgsberg | 8d7ca6b | 2008-11-09 00:22:51 -0500 | [diff] [blame] | 595 | signal(SIGUSR1, handle_sigusr1); |
| 596 | |
Kristian Høgsberg | ef7a9ca | 2008-10-11 21:21:39 -0400 | [diff] [blame] | 597 | schedule_repaint(ec); |
| 598 | |
Kristian Høgsberg | 16eb675 | 2008-10-08 22:51:32 -0400 | [diff] [blame] | 599 | return &ec->base; |
| 600 | } |