blob: 82fecc8d7b276129e6c45ef8315611c22f657f02 [file] [log] [blame]
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04001/*
2 * Copyright © 2008-2010 Kristian Høgsberg
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#include <stddef.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <errno.h>
26#include <sys/time.h>
27
28#include <xcb/xcb.h>
29#include <xcb/dri2.h>
30#include <xcb/xfixes.h>
31
32#define GL_GLEXT_PROTOTYPES
33#define EGL_EGLEXT_PROTOTYPES
34#include <GLES2/gl2.h>
35#include <GLES2/gl2ext.h>
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38
39#include "wayland.h"
40#include "wayland-protocol.h"
41#include "compositor.h"
42
43#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
44
45struct x11_compositor {
46 struct wlsc_compositor base;
47
48 xcb_connection_t *conn;
49 xcb_screen_t *screen;
50 xcb_cursor_t null_cursor;
51 int dri2_major;
52 int dri2_minor;
53 int drm_fd;
54 struct wl_event_source *xcb_source;
55 struct {
56 xcb_atom_t wm_protocols;
57 xcb_atom_t wm_normal_hints;
58 xcb_atom_t wm_size_hints;
59 xcb_atom_t wm_delete_window;
60 xcb_atom_t net_wm_name;
61 xcb_atom_t utf8_string;
62 } atom;
63};
64
65struct x11_output {
66 struct wlsc_output base;
67
68 xcb_xfixes_region_t region;
69 xcb_window_t window;
70 GLuint rbo;
71 EGLImageKHR image;
72 xcb_rectangle_t damage[16];
73 int damage_count;
74};
75
76struct x11_input {
77 struct wlsc_input_device base;
78};
79
80
81static void
82x11_input_create(struct x11_compositor *c)
83{
84 struct x11_input *input;
85
86 input = malloc(sizeof *input);
87 if (input == NULL)
88 return;
89
90 memset(input, 0, sizeof *input);
91 wlsc_input_device_init(&input->base, &c->base);
92
93 c->base.input_device = &input->base;
94}
95
96
97static int
98dri2_connect(struct x11_compositor *c)
99{
100 xcb_xfixes_query_version_reply_t *xfixes_query;
101 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
102 xcb_dri2_query_version_reply_t *dri2_query;
103 xcb_dri2_query_version_cookie_t dri2_query_cookie;
104 xcb_dri2_connect_reply_t *connect;
105 xcb_dri2_connect_cookie_t connect_cookie;
106 xcb_generic_error_t *error;
107
108 xcb_prefetch_extension_data (c->conn, &xcb_xfixes_id);
109 xcb_prefetch_extension_data (c->conn, &xcb_dri2_id);
110
111 xfixes_query_cookie =
112 xcb_xfixes_query_version(c->conn,
113 XCB_XFIXES_MAJOR_VERSION,
114 XCB_XFIXES_MINOR_VERSION);
115
116 dri2_query_cookie =
117 xcb_dri2_query_version (c->conn,
118 XCB_DRI2_MAJOR_VERSION,
119 XCB_DRI2_MINOR_VERSION);
120
121 connect_cookie = xcb_dri2_connect_unchecked (c->conn,
122 c->screen->root,
123 XCB_DRI2_DRIVER_TYPE_DRI);
124
125 xfixes_query =
126 xcb_xfixes_query_version_reply (c->conn,
127 xfixes_query_cookie, &error);
128 if (xfixes_query == NULL ||
129 error != NULL || xfixes_query->major_version < 2) {
130 free(error);
131 return -1;
132 }
133 free(xfixes_query);
134
135 dri2_query =
136 xcb_dri2_query_version_reply (c->conn,
137 dri2_query_cookie, &error);
138 if (dri2_query == NULL || error != NULL) {
139 fprintf(stderr, "DRI2: failed to query version");
140 free(error);
141 return EGL_FALSE;
142 }
143 c->dri2_major = dri2_query->major_version;
144 c->dri2_minor = dri2_query->minor_version;
145 free(dri2_query);
146
147 connect = xcb_dri2_connect_reply (c->conn,
148 connect_cookie, NULL);
149 if (connect == NULL ||
150 connect->driver_name_length + connect->device_name_length == 0) {
151 fprintf(stderr, "DRI2: failed to authenticate");
152 return -1;
153 }
154
155 c->base.base.device =
156 strndup(xcb_dri2_connect_device_name (connect),
157 xcb_dri2_connect_device_name_length (connect));
158
159 if (c->base.base.device == NULL) {
160 free(connect);
161 return -1;
162 }
163 free(connect);
164
165 return 0;
166}
167
168static int
169dri2_authenticate(struct x11_compositor *c)
170{
171 xcb_dri2_authenticate_reply_t *authenticate;
172 xcb_dri2_authenticate_cookie_t authenticate_cookie;
173 drm_magic_t magic;
174
175 if (drmGetMagic(c->drm_fd, &magic)) {
176 fprintf(stderr, "DRI2: failed to get drm magic");
177 return -1;
178 }
179
180 authenticate_cookie =
181 xcb_dri2_authenticate_unchecked(c->conn,
182 c->screen->root, magic);
183 authenticate =
184 xcb_dri2_authenticate_reply(c->conn,
185 authenticate_cookie, NULL);
186 if (authenticate == NULL || !authenticate->authenticated) {
187 fprintf(stderr, "DRI2: failed to authenticate");
188 free(authenticate);
189 return -1;
190 }
191
192 free(authenticate);
193
194 return 0;
195}
196
197static int
198x11_compositor_init_egl(struct x11_compositor *c)
199{
200 PFNEGLGETTYPEDDISPLAYMESA get_typed_display_mesa;
201 EGLint major, minor, count;
202 EGLConfig config;
203
204 static const EGLint config_attribs[] = {
205 EGL_SURFACE_TYPE, 0,
206 EGL_NO_SURFACE_CAPABLE_MESA, EGL_OPENGL_BIT,
207 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
208 EGL_NONE
209 };
210
211 if (dri2_connect(c) < 0)
212 return -1;
213
214 c->drm_fd = open(c->base.base.device, O_RDWR);
215 if (c->drm_fd == -1) {
216 fprintf(stderr,
217 "DRI2: could not open %s (%s)", c->base.base.device,
218 strerror(errno));
219 return -1;
220 }
221
222 if (dri2_authenticate(c) < 0)
223 return -1;
224
225 get_typed_display_mesa =
226 (PFNEGLGETTYPEDDISPLAYMESA)
227 eglGetProcAddress("eglGetTypedDisplayMESA");
228 if (get_typed_display_mesa == NULL) {
229 fprintf(stderr, "eglGetTypedDisplayMESA() not found\n");
230 return -1;
231 }
232
233 c->base.display = get_typed_display_mesa(EGL_DRM_DISPLAY_TYPE_MESA,
234 (void *) c->drm_fd);
235 if (c->base.display == NULL) {
236 fprintf(stderr, "failed to create display\n");
237 return -1;
238 }
239
240 if (!eglInitialize(c->base.display, &major, &minor)) {
241 fprintf(stderr, "failed to initialize display\n");
242 return -1;
243 }
244
245 if (!eglChooseConfig(c->base.display,
246 config_attribs, &config, 1, &count) ||
247 count == 0) {
248 fprintf(stderr, "eglChooseConfig() failed\n");
249 return -1;
250 }
251
252 eglBindAPI(EGL_OPENGL_API);
253 c->base.context = eglCreateContext(c->base.display,
254 config, EGL_NO_CONTEXT, NULL);
255 if (c->base.context == NULL) {
256 fprintf(stderr, "failed to create context\n");
257 return -1;
258 }
259
260 if (!eglMakeCurrent(c->base.display, EGL_NO_SURFACE,
261 EGL_NO_SURFACE, c->base.context)) {
262 fprintf(stderr, "failed to make context current\n");
263 return -1;
264 }
265
266 return 0;
267}
268
269static void
270x11_compositor_present(struct wlsc_compositor *base)
271{
272 struct x11_compositor *c = (struct x11_compositor *) base;
273 struct x11_output *output;
274 xcb_dri2_copy_region_cookie_t cookie;
275 struct timeval tv;
276 uint32_t msec;
277
278 glFlush();
279
280 wl_list_for_each(output, &c->base.output_list, base.link) {
281 cookie = xcb_dri2_copy_region_unchecked(c->conn,
282 output->window,
283 output->region,
284 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
285 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
286 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
287 }
288
289 gettimeofday(&tv, NULL);
290 msec = tv.tv_sec * 1000 + tv.tv_usec / 1000;
291 wlsc_compositor_finish_frame(&c->base, msec);
292}
293
294static void
295x11_output_set_wm_protocols(struct x11_output *output)
296{
297 xcb_atom_t list[1];
298 struct x11_compositor *c =
299 (struct x11_compositor *) output->base.compositor;
300
301 list[0] = c->atom.wm_delete_window;
302 xcb_change_property (c->conn,
303 XCB_PROP_MODE_REPLACE,
304 output->window,
305 c->atom.wm_protocols,
306 XCB_ATOM_ATOM,
307 32,
308 ARRAY_SIZE(list),
309 list);
310}
311
312struct wm_normal_hints {
313 uint32_t flags;
314 uint32_t pad[4];
315 int32_t min_width, min_height;
316 int32_t max_width, max_height;
317 int32_t width_inc, height_inc;
318 int32_t min_aspect_x, min_aspect_y;
319 int32_t max_aspect_x, max_aspect_y;
320 int32_t base_width, base_height;
321 int32_t win_gravity;
322};
323
324#define WM_NORMAL_HINTS_MIN_SIZE 16
325#define WM_NORMAL_HINTS_MAX_SIZE 32
326
327static int
328x11_compositor_create_output(struct x11_compositor *c, int width, int height)
329{
330 static const char name[] = "Wayland Compositor";
331 struct x11_output *output;
332 xcb_dri2_dri2_buffer_t *buffers;
333 xcb_dri2_get_buffers_reply_t *reply;
334 xcb_dri2_get_buffers_cookie_t cookie;
335 xcb_screen_iterator_t iter;
336 xcb_rectangle_t rectangle;
337 struct wm_normal_hints normal_hints;
338 unsigned int attachments[] =
339 { XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT};
340 uint32_t mask = XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
341 uint32_t values[2] = {
342 XCB_EVENT_MASK_KEY_PRESS |
343 XCB_EVENT_MASK_KEY_RELEASE |
344 XCB_EVENT_MASK_BUTTON_PRESS |
345 XCB_EVENT_MASK_BUTTON_RELEASE |
346 XCB_EVENT_MASK_POINTER_MOTION |
347 XCB_EVENT_MASK_EXPOSURE |
348 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
349 0
350 };
351
352 EGLint attribs[] = {
353 EGL_WIDTH, 0,
354 EGL_HEIGHT, 0,
355 EGL_IMAGE_STRIDE_MESA, 0,
356 EGL_IMAGE_FORMAT_MESA, EGL_IMAGE_FORMAT_ARGB8888_MESA,
357 EGL_NONE
358 };
359
360 output = malloc(sizeof *output);
361 if (output == NULL)
362 return -1;
363
364 memset(output, 0, sizeof *output);
365 wlsc_output_init(&output->base, &c->base, 0, 0, width, height);
366
367 values[1] = c->null_cursor;
368 output->window = xcb_generate_id(c->conn);
369 iter = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
370 xcb_create_window(c->conn,
371 XCB_COPY_FROM_PARENT,
372 output->window,
373 iter.data->root,
374 0, 0,
375 width, height,
376 0,
377 XCB_WINDOW_CLASS_INPUT_OUTPUT,
378 iter.data->root_visual,
379 mask, values);
380
381 /* Don't resize me. */
382 memset(&normal_hints, 0, sizeof normal_hints);
383 normal_hints.flags =
384 WM_NORMAL_HINTS_MAX_SIZE | WM_NORMAL_HINTS_MIN_SIZE;
385 normal_hints.min_width = width;
386 normal_hints.min_height = height;
387 normal_hints.max_width = width;
388 normal_hints.max_height = height;
389 xcb_change_property (c->conn, XCB_PROP_MODE_REPLACE, output->window,
390 c->atom.wm_normal_hints,
391 c->atom.wm_size_hints, 32,
392 sizeof normal_hints / 4,
393 (uint8_t *) &normal_hints);
394
395 xcb_map_window(c->conn, output->window);
396
397 /* Set window name. Don't bother with non-EWMH WMs. */
398 xcb_change_property(c->conn, XCB_PROP_MODE_REPLACE, output->window,
399 c->atom.net_wm_name, c->atom.utf8_string, 8,
400 strlen(name), name);
401
402 rectangle.x = 0;
403 rectangle.y = 0;
404 rectangle.width = width;
405 rectangle.height = height;
406 output->region = xcb_generate_id(c->conn);
407 xcb_xfixes_create_region(c->conn, output->region, 1, &rectangle);
408
409 xcb_dri2_create_drawable (c->conn, output->window);
410
411 x11_output_set_wm_protocols(output);
412
413 cookie = xcb_dri2_get_buffers_unchecked (c->conn,
414 output->window,
415 1, 1, attachments);
416 reply = xcb_dri2_get_buffers_reply (c->conn, cookie, NULL);
417 if (reply == NULL)
418 return -1;
419 buffers = xcb_dri2_get_buffers_buffers (reply);
420 if (buffers == NULL)
421 return -1;
422
423 if (reply->count != 1) {
424 fprintf(stderr,
425 "got wrong number of buffers (%d)\n", reply->count);
426 return -1;
427 }
428
429 attribs[1] = reply->width;
430 attribs[3] = reply->height;
431 attribs[5] = buffers[0].pitch / 4;
432 free(reply);
433
434 output->image =
435 eglCreateImageKHR(c->base.display, c->base.context,
436 EGL_DRM_IMAGE_MESA,
437 (EGLClientBuffer) buffers[0].name,
438 attribs);
439
440 glGenRenderbuffers(1, &output->rbo);
441 glBindRenderbuffer(GL_RENDERBUFFER, output->rbo);
442
443 glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
444 output->image);
445
446 glFramebufferRenderbuffer(GL_FRAMEBUFFER,
447 GL_COLOR_ATTACHMENT0,
448 GL_RENDERBUFFER,
449 output->rbo);
450
451 wl_list_insert(c->base.output_list.prev, &output->base.link);
452
453 return 0;
454}
455
456static void
457idle_repaint(void *data)
458{
459 struct x11_output *output = data;
460 struct x11_compositor *c =
461 (struct x11_compositor *) output->base.compositor;
462 xcb_xfixes_region_t region;
463 xcb_dri2_copy_region_cookie_t cookie;
464
465 if (output->damage_count <= ARRAY_SIZE(output->damage)) {
466 region = xcb_generate_id(c->conn);
467 xcb_xfixes_create_region(c->conn, region,
468 output->damage_count, output->damage);
469 } else {
470 region = output->region;
471 }
472
473 cookie = xcb_dri2_copy_region_unchecked(c->conn,
474 output->window,
475 region,
476 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
477 XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT);
478
479 if (region != output->region)
480 xcb_xfixes_destroy_region(c->conn, region);
481
482 free(xcb_dri2_copy_region_reply(c->conn, cookie, NULL));
483 output->damage_count = 0;
484}
485
486static struct x11_output *
487x11_compositor_find_output(struct x11_compositor *c, xcb_window_t window)
488{
489 struct x11_output *output;
490
491 wl_list_for_each(output, &c->base.output_list, base.link) {
492 if (output->window == window)
493 return output;
494 }
495
496 return NULL;
497}
498
499static void
500x11_compositor_handle_event(int fd, uint32_t mask, void *data)
501{
502 struct x11_compositor *c = data;
503 struct x11_output *output;
504 xcb_generic_event_t *event;
505 struct wl_event_loop *loop;
506 xcb_client_message_event_t *client_message;
507 xcb_motion_notify_event_t *motion_notify;
508 xcb_key_press_event_t *key_press;
509 xcb_button_press_event_t *button_press;
510 xcb_expose_event_t *expose;
511 xcb_rectangle_t *r;
512 xcb_atom_t atom;
513
514 loop = wl_display_get_event_loop(c->base.wl_display);
515 while (event = xcb_poll_for_event (c->conn), event != NULL) {
516 switch (event->response_type & ~0x80) {
517
518 case XCB_KEY_PRESS:
519 key_press = (xcb_key_press_event_t *) event;
520 notify_key(c->base.input_device,
521 key_press->detail, 1);
522
523 fprintf(stderr, "code %d, sequence %d\n",
524 key_press->detail, key_press->sequence);
525
526 break;
527 case XCB_KEY_RELEASE:
528 key_press = (xcb_key_press_event_t *) event;
529 notify_key(c->base.input_device,
530 key_press->detail, 0);
531 break;
532 case XCB_BUTTON_PRESS:
533 button_press = (xcb_button_press_event_t *) event;
534 notify_button(c->base.input_device,
535 button_press->detail, 1);
536 break;
537 case XCB_BUTTON_RELEASE:
538 button_press = (xcb_button_press_event_t *) event;
539 notify_button(c->base.input_device,
540 button_press->detail, 0);
541 break;
542
543 case XCB_MOTION_NOTIFY:
544 motion_notify = (xcb_motion_notify_event_t *) event;
545 notify_motion(c->base.input_device,
546 motion_notify->event_x,
547 motion_notify->event_y);
548 break;
549
550 case XCB_EXPOSE:
551 expose = (xcb_expose_event_t *) event;
552 output = x11_compositor_find_output(c, expose->window);
553 if (output->damage_count == 0)
554 wl_event_loop_add_idle(loop,
555 idle_repaint, output);
556
557 r = &output->damage[output->damage_count++];
558 if (output->damage_count > 16)
559 break;
560 r->x = expose->x;
561 r->y = expose->y;
562 r->width = expose->width;
563 r->height = expose->height;
564 break;
565 case XCB_CLIENT_MESSAGE:
566 client_message = (xcb_client_message_event_t *) event;
567 atom = client_message->data.data32[0];
568 if (atom == c->atom.wm_delete_window)
569 exit(1);
570 break;
571 default:
572
573 break;
574 }
575
576 free (event);
577 }
578}
579
580#define F(field) offsetof(struct x11_compositor, field)
581
582static void
583x11_compositor_get_resources(struct x11_compositor *c)
584{
585 static const struct { const char *name; int offset; } atoms[] = {
586 { "WM_PROTOCOLS", F(atom.wm_protocols) },
587 { "WM_NORMAL_HINTS", F(atom.wm_normal_hints) },
588 { "WM_SIZE_HINTS", F(atom.wm_size_hints) },
589 { "WM_DELETE_WINDOW", F(atom.wm_delete_window) },
590 { "_NET_WM_NAME", F(atom.net_wm_name) },
591 { "UTF8_STRING", F(atom.utf8_string) },
592 };
593
594 xcb_intern_atom_cookie_t cookies[ARRAY_SIZE(atoms)];
595 xcb_intern_atom_reply_t *reply;
596 xcb_pixmap_t pixmap;
597 xcb_gc_t gc;
598 int i;
599 uint8_t data[] = { 0, 0, 0, 0 };
600
601 for (i = 0; i < ARRAY_SIZE(atoms); i++)
602 cookies[i] = xcb_intern_atom (c->conn, 0,
603 strlen(atoms[i].name),
604 atoms[i].name);
605
606 for (i = 0; i < ARRAY_SIZE(atoms); i++) {
607 reply = xcb_intern_atom_reply (c->conn, cookies[i], NULL);
608 *(xcb_atom_t *) ((char *) c + atoms[i].offset) = reply->atom;
609 free(reply);
610 }
611
612 pixmap = xcb_generate_id(c->conn);
613 gc = xcb_generate_id(c->conn);
614 xcb_create_pixmap(c->conn, 1, pixmap, c->screen->root, 1, 1);
615 xcb_create_gc(c->conn, gc, pixmap, 0, NULL);
616 xcb_put_image(c->conn, XCB_IMAGE_FORMAT_XY_PIXMAP,
617 pixmap, gc, 1, 1, 0, 0, 0, 32, sizeof data, data);
618 c->null_cursor = xcb_generate_id(c->conn);
619 xcb_create_cursor (c->conn, c->null_cursor,
620 pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
621 xcb_free_gc(c->conn, gc);
622 xcb_free_pixmap(c->conn, pixmap);
623}
624
625struct wlsc_compositor *
626x11_compositor_create(struct wl_display *display)
627{
628 struct x11_compositor *c;
629 struct wl_event_loop *loop;
630 xcb_screen_iterator_t s;
631
632 c = malloc(sizeof *c);
633 if (c == NULL)
634 return NULL;
635
636 memset(c, 0, sizeof *c);
637 c->conn = xcb_connect(0, 0);
638
639 if (xcb_connection_has_error(c->conn))
640 return NULL;
641
642 s = xcb_setup_roots_iterator(xcb_get_setup(c->conn));
643 c->screen = s.data;
644
645 x11_compositor_get_resources(c);
646
647 x11_compositor_init_egl(c);
648
649 /* Can't init base class until we have a current egl context */
650 wlsc_compositor_init(&c->base, display);
651
652 x11_compositor_create_output(c, 1024, 640);
653
654 x11_input_create(c);
655
656 loop = wl_display_get_event_loop(c->base.wl_display);
657
658 c->xcb_source =
659 wl_event_loop_add_fd(loop, xcb_get_file_descriptor(c->conn),
660 WL_EVENT_READABLE,
661 x11_compositor_handle_event, c);
662
663 c->base.present = x11_compositor_present;
664
665 return &c->base;
666}