blob: 24f47337894d3c3720ed704f6f623ee58d7415e1 [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øgsberg16eb6752008-10-08 22:51:32 -040016
17#include "wayland.h"
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -050018#include "cairo-util.h"
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040019
20#include <GL/gl.h>
21#include <eagle.h>
22
23#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
24
25struct egl_compositor {
26 struct wl_compositor base;
27 EGLDisplay display;
28 EGLSurface surface;
29 EGLContext context;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050030 EGLConfig config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040031 struct wl_display *wl_display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040032 int gem_fd;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033 int width, height;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050034 struct egl_surface *pointer;
35 struct egl_surface *background;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040036};
37
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -050038struct egl_surface {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040039 GLuint texture;
Kristian Høgsbergf9212892008-10-11 18:40:23 -040040 struct wl_map map;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -050041 EGLSurface surface;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040042};
43
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050044static int do_screenshot;
45
46static void
47handle_sigusr1(int s)
48{
49 do_screenshot = 1;
50}
51
52static void
53die(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
64static void
65stdio_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
80static void
81png_simple_output_flush_fn (png_structp png_ptr)
82{
83}
84
85static void
86png_simple_error_callback (png_structp png,
87 png_const_charp error_msg)
88{
89 die("png error: %s\n", error_msg);
90}
91
92static void
93png_simple_warning_callback (png_structp png,
94 png_const_charp error_msg)
95{
96 fprintf(stderr, "png warning: %s\n", error_msg);
97}
98
99static void
100convert_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
116static void
117screenshot(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øgsbergef7a9ca2008-10-11 21:21:39 -0400180static void
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500181pointer_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øgsbergaa5b5be2008-11-21 21:31:54 -0500196static struct egl_surface *
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500197pointer_create(int x, int y, int width, int height)
198{
199 const int hotspot_x = 16, hotspot_y = 16;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500200 struct egl_surface *pointer;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500201 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øgsbergaa5b5be2008-11-21 21:31:54 -0500245 pointer->surface = EGL_NO_SURFACE;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500246
247 return pointer;
248}
249
250static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500251egl_surface_destroy(struct egl_surface *es, struct egl_compositor *ec)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500252{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500253 glDeleteTextures(1, &es->texture);
254 if (es->surface != EGL_NO_SURFACE)
255 eglDestroySurface(ec->display, es->surface);
256 free(es);
257}
258
259static struct egl_surface *
260background_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øgsberg4c9f2c92008-11-21 19:25:44 -0500300}
301
302static void
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500303draw_surface(struct egl_surface *es)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500304{
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øgsbergaa5b5be2008-11-21 21:31:54 -0500309 vertices[0] = es->map.x;
310 vertices[1] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500311 vertices[2] = 0;
312
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500313 vertices[3] = es->map.x;
314 vertices[4] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500315 vertices[5] = 0;
316
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500317 vertices[6] = es->map.x + es->map.width;
318 vertices[7] = es->map.y;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500319 vertices[8] = 0;
320
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500321 vertices[9] = es->map.x + es->map.width;
322 vertices[10] = es->map.y + es->map.height;
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500323 vertices[11] = 0;
324
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500325 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500326 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
339static void
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400340repaint(void *data)
341{
342 struct egl_compositor *ec = data;
343 struct wl_surface_iterator *iterator;
344 struct wl_surface *surface;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500345 struct egl_surface *es;
346
347 draw_surface(ec->background);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400348
349 iterator = wl_surface_iterator_create(ec->wl_display, 0);
350 while (wl_surface_iterator_next(iterator, &surface)) {
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500351 es = wl_surface_get_data(surface);
352 if (es == NULL)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400353 continue;
354
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500355 draw_surface(es);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400356 }
357 wl_surface_iterator_destroy(iterator);
358
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500359 draw_surface(ec->pointer);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500360
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400361 eglSwapBuffers(ec->display, ec->surface);
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500362
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øgsbergef7a9ca2008-10-11 21:21:39 -0400373}
374
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500375static void
376schedule_repaint(struct egl_compositor *ec)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400377{
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øgsberg5503bf82008-11-06 10:38:17 -0500384static void
385notify_surface_create(struct wl_compositor *compositor,
386 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400387{
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500388 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400389
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500390 es = malloc(sizeof *es);
391 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400392 return;
393
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500394 es->surface = EGL_NO_SURFACE;
395 wl_surface_set_data(surface, es);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400396
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500397 glGenTextures(1, &es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400398}
399
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500400static void
401notify_surface_destroy(struct wl_compositor *compositor,
402 struct wl_surface *surface)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400403{
404 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500405 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400406
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500407 es = wl_surface_get_data(surface);
408 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400409 return;
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500410
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500411 egl_surface_destroy(es, ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400412
413 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400414}
415
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500416static void
417notify_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øgsberg16eb6752008-10-08 22:51:32 -0400420{
421 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500422 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400423
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500424 es = wl_surface_get_data(surface);
425 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400426 return;
427
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500428 if (es->surface != EGL_NO_SURFACE)
429 eglDestroySurface(ec->display, es->surface);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400430
Kristian Høgsberg56f3c712008-11-05 07:55:45 -0500431 /* 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øgsbergaa5b5be2008-11-21 21:31:54 -0500434 es->surface = eglCreateSurfaceForName(ec->display, ec->config,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500435 name, width, height, stride, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400436
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500437 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400438 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øgsbergaa5b5be2008-11-21 21:31:54 -0500442 eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400443
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400444 schedule_repaint(ec);
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400445}
446
Kristian Høgsberg5503bf82008-11-06 10:38:17 -0500447static void
448notify_surface_map(struct wl_compositor *compositor,
449 struct wl_surface *surface, struct wl_map *map)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400450{
451 struct egl_compositor *ec = (struct egl_compositor *) compositor;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500452 struct egl_surface *es;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400453
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500454 es = wl_surface_get_data(surface);
455 if (es == NULL)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400456 return;
457
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500458 es->map = *map;
Kristian Høgsberg5ebb3172008-10-11 19:21:35 -0400459
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400460 schedule_repaint(ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400461}
462
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500463static void
464notify_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øgsberg78231c82008-11-08 15:06:01 -0500470 struct egl_compositor *ec = (struct egl_compositor *) compositor;
471 EGLSurface src;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500472 struct egl_surface *es;
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500473
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500474 es = wl_surface_get_data(surface);
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500475
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øgsbergaa5b5be2008-11-21 21:31:54 -0500484 eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500485 src, GL_FRONT_LEFT, x, y, width, height);
486 schedule_repaint(ec);
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500487}
488
489static void
490notify_surface_damage(struct wl_compositor *compositor,
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500491 struct wl_surface *surface,
492 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500493{
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øgsberg4c9f2c92008-11-21 19:25:44 -0500500static void
501notify_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øgsberg5503bf82008-11-06 10:38:17 -0500511static const struct wl_compositor_interface interface = {
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400512 notify_surface_create,
513 notify_surface_destroy,
514 notify_surface_attach,
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -0500515 notify_surface_map,
516 notify_surface_copy,
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500517 notify_surface_damage,
518 notify_pointer_motion
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400519};
520
521static const char gem_device[] = "/dev/dri/card0";
522
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500523WL_EXPORT struct wl_compositor *
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400524wl_compositor_create(struct wl_display *display)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400525{
526 EGLConfig configs[64];
527 EGLint major, minor, count;
528 struct egl_compositor *ec;
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500529 const char *filename;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400530
531 ec = malloc(sizeof *ec);
532 if (ec == NULL)
533 return NULL;
534
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500535 ec->width = 1280;
536 ec->height = 800;
537
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400538 ec->base.interface = &interface;
Kristian Høgsbergf9212892008-10-11 18:40:23 -0400539 ec->wl_display = display;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400540
Kristian Høgsbergc508d932008-10-13 22:52:42 -0400541 ec->display = eglCreateDisplayNative(gem_device, "i965");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400542 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øgsberg2d9cd1e2008-11-03 08:09:34 -0500557 ec->config = configs[24];
558 ec->surface = eglCreateSurfaceNative(ec->display, ec->config,
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500559 0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400560 if (ec->surface == NULL) {
561 fprintf(stderr, "failed to create surface\n");
562 return NULL;
563 }
564
Kristian Høgsberg2d9cd1e2008-11-03 08:09:34 -0500565 ec->context = eglCreateContext(ec->display, ec->config, NULL, NULL);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400566 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øgsberg8d7ca6b2008-11-09 00:22:51 -0500576 glViewport(0, 0, ec->width, ec->height);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400577 glMatrixMode(GL_PROJECTION);
578 glLoadIdentity();
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -0500579 glOrtho(0, ec->width, ec->height, 0, 0, 1000.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400580 glMatrixMode(GL_MODELVIEW);
Kristian Høgsberga234e702008-10-11 22:13:51 -0400581 glClearColor(0.0, 0.05, 0.2, 0.0);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400582
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500583 filename = getenv("WAYLAND_BACKGROUND");
584 if (filename == NULL)
585 filename = "background.jpg";
586 ec->background = background_create(filename, 1280, 800);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500587 ec->pointer = pointer_create(100, 100, 64, 64);
588
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400589 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øgsberg8d7ca6b2008-11-09 00:22:51 -0500595 signal(SIGUSR1, handle_sigusr1);
596
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400597 schedule_repaint(ec);
598
Kristian Høgsberg16eb6752008-10-08 22:51:32 -0400599 return &ec->base;
600}