blob: 8c2cfdde424417d15eb70c439ebdaabc441ba673 [file] [log] [blame]
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdint.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05005#include <stdarg.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04006#include <i915_drm.h>
7#include <sys/ioctl.h>
8#include <sys/mman.h>
9#include <fcntl.h>
10#include <unistd.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050011#include <signal.h>
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050012#include <cairo.h>
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050013#include <gdk-pixbuf/gdk-pixbuf.h>
14#include <glib.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050015#include <png.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050016#include <math.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040017
18#include "wayland.h"
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050019#include "cairo-util.h"
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040020
21#include <GL/gl.h>
22#include <eagle.h>
23
24#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
25
26struct egl_compositor {
27 struct wl_compositor base;
28 EGLDisplay display;
29 EGLSurface surface;
30 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050031 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040032 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040033 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050034 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050035 struct egl_surface *pointer;
36 struct egl_surface *background;
Kristian Høgsberg54879822008-11-23 17:07:32 -050037 struct egl_surface *overlay;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040038};
39
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050040struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040042 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050043 EGLSurface surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040044};
45
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050046static int do_screenshot;
47
48static void
49handle_sigusr1(int s)
50{
51 do_screenshot = 1;
52}
53
54static void
55die(const char *msg, ...)
56{
57 va_list ap;
58
59 va_start (ap, msg);
60 vfprintf(stderr, msg, ap);
61 va_end (ap);
62
63 exit(EXIT_FAILURE);
64}
65
66static void
67stdio_write_func (png_structp png, png_bytep data, png_size_t size)
68{
69 FILE *fp;
70 size_t ret;
71
72 fp = png_get_io_ptr (png);
73 while (size) {
74 ret = fwrite (data, 1, size, fp);
75 size -= ret;
76 data += ret;
77 if (size && ferror (fp))
78 die("write: %m\n");
79 }
80}
81
82static void
83png_simple_output_flush_fn (png_structp png_ptr)
84{
85}
86
87static void
88png_simple_error_callback (png_structp png,
89 png_const_charp error_msg)
90{
91 die("png error: %s\n", error_msg);
92}
93
94static void
95png_simple_warning_callback (png_structp png,
96 png_const_charp error_msg)
97{
98 fprintf(stderr, "png warning: %s\n", error_msg);
99}
100
101static void
102convert_pixels(png_structp png, png_row_infop row_info, png_bytep data)
103{
104 unsigned int i;
105
106 for (i = 0; i < row_info->rowbytes; i += 4) {
107 uint8_t *b = &data[i];
108 uint32_t pixel;
109
110 memcpy (&pixel, b, sizeof (uint32_t));
111 b[0] = (pixel & 0xff0000) >> 16;
112 b[1] = (pixel & 0x00ff00) >> 8;
113 b[2] = (pixel & 0x0000ff) >> 0;
114 b[3] = 0;
115 }
116}
117
118static void
119screenshot(struct egl_compositor *ec)
120{
121 png_struct *png;
122 png_info *info;
123 png_byte **volatile rows = NULL;
124 png_color_16 white;
125 int depth, i;
126 FILE *fp;
127 uint8_t *data;
128 GLuint stride;
129 static const char filename[] = "wayland-screenshot.png";
130
131 data = eglReadBuffer(ec->display, ec->surface, GL_FRONT_LEFT, &stride);
132 if (data == NULL)
133 die("eglReadBuffer failed\n");
134 rows = malloc(ec->height * sizeof rows[0]);
135 if (rows == NULL)
136 die("malloc failed\n");
137
138 for (i = 0; i < ec->height; i++)
139 rows[i] = (png_byte *) data + i * stride;
140
141 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
142 png_simple_error_callback,
143 png_simple_warning_callback);
144 if (png == NULL)
145 die("png_create_write_struct failed\n");
146
147 info = png_create_info_struct(png);
148 if (info == NULL)
149 die("png_create_info_struct failed\n");
150
151 fp = fopen(filename, "w");
152 if (fp == NULL)
153 die("fopen failed: %m\n");
154
155 png_set_write_fn(png, fp, stdio_write_func, png_simple_output_flush_fn);
156
157 depth = 8;
158 png_set_IHDR(png, info,
159 ec->width,
160 ec->height, depth,
161 PNG_COLOR_TYPE_RGB,
162 PNG_INTERLACE_NONE,
163 PNG_COMPRESSION_TYPE_DEFAULT,
164 PNG_FILTER_TYPE_DEFAULT);
165
166 white.gray = (1 << depth) - 1;
167 white.red = white.blue = white.green = white.gray;
168 png_set_bKGD(png, info, &white);
169 png_write_info (png, info);
170 png_set_write_user_transform_fn(png, convert_pixels);
171
172 png_set_filler(png, 0, PNG_FILLER_AFTER);
173 png_write_image(png, rows);
174 png_write_end(png, info);
175
176 png_destroy_write_struct(&png, &info);
177 fclose(fp);
178 free(rows);
179 free(data);
180}
181
Kristian Høgsberg54879822008-11-23 17:07:32 -0500182static struct egl_surface *
183egl_surface_create_from_cairo_surface(cairo_surface_t *surface,
184 int x, int y, int width, int height)
185{
186 struct egl_surface *es;
187 int stride;
188 void *data;
189
190 stride = cairo_image_surface_get_stride(surface);
191 data = cairo_image_surface_get_data(surface);
192
193 es = malloc(sizeof *es);
194 if (es == NULL)
195 return NULL;
196
197 glGenTextures(1, &es->texture);
198 glBindTexture(GL_TEXTURE_2D, es->texture);
199 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
200 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
201 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
202 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
203 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
204 GL_BGRA, GL_UNSIGNED_BYTE, data);
205
206 es->map.x = x;
207 es->map.y = y;
208 es->map.width = width;
209 es->map.height = height;
210 es->surface = EGL_NO_SURFACE;
211
212 return es;
213}
214
215static void
216egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
217{
218 glDeleteTextures(1, &es->texture);
219 if (es->surface != EGL_NO_SURFACE)
220 eglDestroySurface(ec->display, es->surface);
221 free(es);
222}
223
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400224static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500225pointer_path(cairo_t *cr, int x, int y)
226{
227 const int end = 3, tx = 4, ty = 12, dx = 5, dy = 10;
228 const int width = 16, height = 16;
229
230 cairo_move_to(cr, x, y);
231 cairo_line_to(cr, x + tx, y + ty);
232 cairo_line_to(cr, x + dx, y + dy);
233 cairo_line_to(cr, x + width - end, y + height);
234 cairo_line_to(cr, x + width, y + height - end);
235 cairo_line_to(cr, x + dy, y + dx);
236 cairo_line_to(cr, x + ty, y + tx);
237 cairo_close_path(cr);
238}
239
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500240static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500241pointer_create(int x, int y, int width, int height)
242{
Kristian Høgsberg54879822008-11-23 17:07:32 -0500243 struct egl_surface *es;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500244 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500245 cairo_surface_t *surface;
246 cairo_t *cr;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500247
248 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
249 width, height);
250
251 cr = cairo_create(surface);
252 pointer_path(cr, hotspot_x + 5, hotspot_y + 4);
253 cairo_set_line_width (cr, 2);
254 cairo_set_source_rgb(cr, 0, 0, 0);
255 cairo_stroke_preserve(cr);
256 cairo_fill(cr);
257 blur_surface(surface, width);
258
259 pointer_path(cr, hotspot_x, hotspot_y);
260 cairo_stroke_preserve(cr);
261 cairo_set_source_rgb(cr, 1, 1, 1);
262 cairo_fill(cr);
263 cairo_destroy(cr);
264
Kristian Høgsberg54879822008-11-23 17:07:32 -0500265 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
266
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500267 cairo_surface_destroy(surface);
268
Kristian Høgsberg54879822008-11-23 17:07:32 -0500269 return es;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500270}
271
272static struct egl_surface *
273background_create(const char *filename, int width, int height)
274{
275 struct egl_surface *background;
276 GdkPixbuf *pixbuf;
277 GError *error = NULL;
278 int pixbuf_width, pixbuf_height;
279 void *data;
280
281 background = malloc(sizeof *background);
282 if (background == NULL)
283 return NULL;
284
285 g_type_init();
286
287 pixbuf = gdk_pixbuf_new_from_file(filename, &error);
288 if (error != NULL) {
289 free(background);
290 return NULL;
291 }
292
293 pixbuf_width = gdk_pixbuf_get_width(pixbuf);
294 pixbuf_height = gdk_pixbuf_get_height(pixbuf);
295 data = gdk_pixbuf_get_pixels(pixbuf);
296
297 glGenTextures(1, &background->texture);
298 glBindTexture(GL_TEXTURE_2D, background->texture);
299 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
300 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
301 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
302 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
303 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pixbuf_width, pixbuf_height, 0,
304 GL_BGR, GL_UNSIGNED_BYTE, data);
305
306 background->map.x = 0;
307 background->map.y = 0;
308 background->map.width = width;
309 background->map.height = height;
310 background->surface = EGL_NO_SURFACE;
311
312 return background;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500313}
314
315static void
Kristian Høgsberg54879822008-11-23 17:07:32 -0500316rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
317{
318 cairo_move_to(cr, x0, y0 + radius);
319 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
320 cairo_line_to(cr, x1 - radius, y0);
321 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
322 cairo_line_to(cr, x1, y1 - radius);
323 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
324 cairo_line_to(cr, x0 + radius, y1);
325 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
326 cairo_close_path(cr);
327}
328
329static void
330draw_button(cairo_t *cr, int x, int y, int width, int height, const char *text)
331{
332 cairo_pattern_t *gradient;
333 cairo_text_extents_t extents;
334 double bright = 0.15, dim = 0.02;
335 int radius = 10;
336
337 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
338 cairo_set_line_width (cr, 2);
339 rounded_rect(cr, x, y, x + width, y + height, radius);
340 cairo_set_source_rgb(cr, dim, dim, dim);
341 cairo_stroke(cr);
342 rounded_rect(cr, x + 2, y + 2, x + width, y + height, radius);
343 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
344 cairo_stroke(cr);
345
346 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
347 cairo_set_source_rgb(cr, bright, bright, bright);
348 cairo_stroke(cr);
349 rounded_rect(cr, x + 3, y + 3, x + width - 1, y + height - 1, radius - 1);
350 cairo_set_source_rgb(cr, dim, dim, dim);
351 cairo_stroke(cr);
352
353 rounded_rect(cr, x + 1, y + 1, x + width - 1, y + height - 1, radius - 1);
354 gradient = cairo_pattern_create_linear (0, y, 0, y + height);
355 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.15, 0.15, 0.15);
356 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.08, 0.08, 0.08);
357 cairo_pattern_add_color_stop_rgb(gradient, 0.5, 0.07, 0.07, 0.07);
358 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.1, 0.1, 0.1);
359 cairo_set_source(cr, gradient);
360 cairo_fill(cr);
361
362 cairo_set_font_size(cr, 16);
363 cairo_text_extents(cr, text, &extents);
364 cairo_move_to(cr, x + (width - extents.width) / 2, y + (height - extents.height) / 2 - extents.y_bearing);
365 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
366 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
367 cairo_set_line_width (cr, 4);
368 cairo_text_path(cr, text);
369 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
370 cairo_stroke_preserve(cr);
371 cairo_set_source_rgb(cr, 1, 1, 1);
372 cairo_fill(cr);
373}
374
375static struct egl_surface *
376overlay_create(int x, int y, int width, int height)
377{
378 struct egl_surface *es;
379 cairo_surface_t *surface;
380 cairo_t *cr;
381 int total_width, button_x, button_y;
382 const int button_width = 150;
383 const int button_height = 40;
384 const int spacing = 50;
385
386 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
387 width, height);
388
389 cr = cairo_create(surface);
390 cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 0.7);
391 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
392 cairo_paint(cr);
393
394 total_width = button_width * 2 + spacing;
395 button_x = (width - total_width) / 2;
396 button_y = height - button_height - 20;
397 draw_button(cr, button_x, button_y, button_width, button_height, "Previous");
398 button_x += button_width + spacing;
399 draw_button(cr, button_x, button_y, button_width, button_height, "Next");
400
401 cairo_destroy(cr);
402
403 es = egl_surface_create_from_cairo_surface(surface, x, y, width, height);
404
405 cairo_surface_destroy(surface);
406
407 return es;
408}
409
410static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500411draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500412{
413 GLint vertices[12];
414 GLint tex_coords[12] = { 0, 0, 0, 1, 1, 0, 1, 1 };
415 GLuint indices[4] = { 0, 1, 2, 3 };
416
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500417 vertices[0] = es->map.x;
418 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500419 vertices[2] = 0;
420
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500421 vertices[3] = es->map.x;
422 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500423 vertices[5] = 0;
424
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500425 vertices[6] = es->map.x + es->map.width;
426 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500427 vertices[8] = 0;
428
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500429 vertices[9] = es->map.x + es->map.width;
430 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500431 vertices[11] = 0;
432
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500433 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500434 glEnable(GL_TEXTURE_2D);
435 glEnable(GL_BLEND);
436 /* Assume pre-multiplied alpha for now, this probably
437 * needs to be a wayland visual type of thing. */
438 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
439
440 glEnableClientState(GL_VERTEX_ARRAY);
441 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
442 glVertexPointer(3, GL_INT, 0, vertices);
443 glTexCoordPointer(2, GL_INT, 0, tex_coords);
444 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_INT, indices);
445}
446
447static void
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400448repaint(void *data)
449{
450 struct egl_compositor *ec = data;
451 struct wl_surface_iterator *iterator;
452 struct wl_surface *surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500453 struct egl_surface *es;
454
455 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400456
457 iterator = wl_surface_iterator_create(ec->wl_display, 0);
458 while (wl_surface_iterator_next(iterator, &surface)) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500459 es = wl_surface_get_data(surface);
460 if (es == NULL)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400461 continue;
462
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500463 draw_surface(es);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400464 }
465 wl_surface_iterator_destroy(iterator);
466
Kristian Høgsberg54879822008-11-23 17:07:32 -0500467 draw_surface(ec->overlay);
468
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500469 draw_surface(ec->pointer);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500470
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400471 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500472
473 if (do_screenshot) {
474 glFinish();
475 /* FIXME: There's a bug somewhere so that glFinish()
476 * doesn't actually wait for all rendering to finish.
477 * I *think* it's fixed in upstream drm, but for my
478 * kernel I need this sleep now... */
479 sleep(1);
480 screenshot(ec);
481 do_screenshot = 0;
482 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400483}
484
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500485static void
486schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400487{
488 struct wl_event_loop *loop;
489
490 loop = wl_display_get_event_loop(ec->wl_display);
491 wl_event_loop_add_idle(loop, repaint, ec);
492}
493
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500494static void
495notify_surface_create(struct wl_compositor *compositor,
496 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400497{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500498 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400499
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500500 es = malloc(sizeof *es);
501 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400502 return;
503
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500504 es->surface = EGL_NO_SURFACE;
505 wl_surface_set_data(surface, es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400506
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500507 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400508}
509
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500510static void
511notify_surface_destroy(struct wl_compositor *compositor,
512 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400513{
514 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500515 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400516
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500517 es = wl_surface_get_data(surface);
518 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400519 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500520
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500521 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400522
523 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400524}
525
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500526static void
527notify_surface_attach(struct wl_compositor *compositor,
528 struct wl_surface *surface, uint32_t name,
529 uint32_t width, uint32_t height, uint32_t stride)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400530{
531 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500532 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400533
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500534 es = wl_surface_get_data(surface);
535 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400536 return;
537
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500538 if (es->surface != EGL_NO_SURFACE)
539 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400540
Kristian Høgsberg56f3c712008-11-05 07:55:45 -0500541 /* FIXME: We need to use a single buffer config without depth
542 * or stencil buffers here to keep egl from creating auxillary
543 * buffers for the pixmap here. */
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500544 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500545 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400546
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500547 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400548 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
549 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
550 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
551 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500552 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400553
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400554 schedule_repaint(ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400555}
556
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500557static void
558notify_surface_map(struct wl_compositor *compositor,
559 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400560{
561 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500562 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400563
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500564 es = wl_surface_get_data(surface);
565 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400566 return;
567
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500568 es->map = *map;
Kristian Høgsberg5ebb3172008-10-11 19:21:35 -0400569
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400570 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400571}
572
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500573static void
574notify_surface_copy(struct wl_compositor *compositor,
575 struct wl_surface *surface,
576 int32_t dst_x, int32_t dst_y,
577 uint32_t name, uint32_t stride,
578 int32_t x, int32_t y, int32_t width, int32_t height)
579{
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500580 struct egl_compositor *ec = (struct egl_compositor *) compositor;
581 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500582 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500583
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500584 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500585
586 /* FIXME: glCopyPixels should work, but then we'll have to
587 * call eglMakeCurrent to set up the src and dest surfaces
588 * first. This seems cheaper, but maybe there's a better way
589 * to accomplish this. */
590
591 src = eglCreateSurfaceForName(ec->display, ec->config,
592 name, x + width, y + height, stride, NULL);
593
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500594 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500595 src, GL_FRONT_LEFT, x, y, width, height);
596 schedule_repaint(ec);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500597}
598
599static void
600notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500601 struct wl_surface *surface,
602 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500603{
604 struct egl_compositor *ec = (struct egl_compositor *) compositor;
605
606 /* FIXME: This need to take a damage region, of course. */
607 schedule_repaint(ec);
608}
609
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500610static void
611notify_pointer_motion(struct wl_compositor *compositor,
612 struct wl_object *source, int x, int y)
613{
614 struct egl_compositor *ec = (struct egl_compositor *) compositor;
615
616 ec->pointer->map.x = x;
617 ec->pointer->map.y = y;
618 schedule_repaint(ec);
619}
620
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500621static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400622 notify_surface_create,
623 notify_surface_destroy,
624 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500625 notify_surface_map,
626 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500627 notify_surface_damage,
628 notify_pointer_motion
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400629};
630
631static const char gem_device[] = "/dev/dri/card0";
632
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500633WL_EXPORT struct wl_compositor *
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400634wl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400635{
636 EGLConfig configs[64];
637 EGLint major, minor, count;
638 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500639 const char *filename;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400640
641 ec = malloc(sizeof *ec);
642 if (ec == NULL)
643 return NULL;
644
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500645 ec->width = 1280;
646 ec->height = 800;
647
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400648 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400649 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400650
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400651 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400652 if (ec->display == NULL) {
653 fprintf(stderr, "failed to create display\n");
654 return NULL;
655 }
656
657 if (!eglInitialize(ec->display, &major, &minor)) {
658 fprintf(stderr, "failed to initialize display\n");
659 return NULL;
660 }
661
662 if (!eglGetConfigs(ec->display, configs, ARRAY_LENGTH(configs), &count)) {
663 fprintf(stderr, "failed to get configs\n");
664 return NULL;
665 }
666
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500667 ec->config = configs[24];
668 ec->surface = eglCreateSurfaceNative(ec->display, ec->config,
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500669 0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400670 if (ec->surface == NULL) {
671 fprintf(stderr, "failed to create surface\n");
672 return NULL;
673 }
674
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500675 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400676 if (ec->context == NULL) {
677 fprintf(stderr, "failed to create context\n");
678 return NULL;
679 }
680
681 if (!eglMakeCurrent(ec->display, ec->surface, ec->surface, ec->context)) {
682 fprintf(stderr, "failed to make context current\n");
683 return NULL;
684 }
685
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500686 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400687 glMatrixMode(GL_PROJECTION);
688 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500689 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400690 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400691 glClearColor(0.0, 0.05, 0.2, 0.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400692
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500693 filename = getenv("WAYLAND_BACKGROUND");
694 if (filename == NULL)
695 filename = "background.jpg";
696 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500697 ec->pointer = pointer_create(100, 100, 64, 64);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500698 ec->overlay = overlay_create(0, ec->height - 200, ec->width, 200);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500699
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400700 ec->gem_fd = open(gem_device, O_RDWR);
701 if (ec->gem_fd < 0) {
702 fprintf(stderr, "failed to open drm device\n");
703 return NULL;
704 }
705
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500706 signal(SIGUSR1, handle_sigusr1);
707
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400708 schedule_repaint(ec);
709
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400710 return &ec->base;
711}