egl: Quit checking for a bug in old xcb when we require new xcb.
[mesa.git] / src / egl / drivers / dri2 / platform_x11.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <xf86drm.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #include "egl_dri2.h"
41
42 static void
43 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
44 struct dri2_egl_surface * dri2_surf,
45 int depth)
46 {
47 uint32_t mask;
48 const uint32_t function = GXcopy;
49 uint32_t valgc[2];
50
51 /* create GC's */
52 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
53 mask = XCB_GC_FUNCTION;
54 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
55
56 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
57 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
58 valgc[0] = function;
59 valgc[1] = False;
60 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
61 dri2_surf->depth = depth;
62 switch (depth) {
63 case 32:
64 case 24:
65 dri2_surf->bytes_per_pixel = 4;
66 break;
67 case 16:
68 dri2_surf->bytes_per_pixel = 2;
69 break;
70 case 8:
71 dri2_surf->bytes_per_pixel = 1;
72 break;
73 case 0:
74 dri2_surf->bytes_per_pixel = 0;
75 break;
76 default:
77 _eglLog(_EGL_WARNING, "unsupported depth %d", depth);
78 }
79 }
80
81 static void
82 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
83 struct dri2_egl_surface * dri2_surf)
84 {
85 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
86 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
87 }
88
89 static void
90 swrastGetDrawableInfo(__DRIdrawable * draw,
91 int *x, int *y, int *w, int *h,
92 void *loaderPrivate)
93 {
94 struct dri2_egl_surface *dri2_surf = loaderPrivate;
95 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
96
97 xcb_get_geometry_cookie_t cookie;
98 xcb_get_geometry_reply_t *reply;
99 xcb_generic_error_t *error;
100
101 *w = *h = 0;
102 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
103 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
104 if (reply == NULL)
105 return;
106
107 if (error != NULL) {
108 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
109 free(error);
110 } else {
111 *w = reply->width;
112 *h = reply->height;
113 }
114 free(reply);
115 }
116
117 static void
118 swrastPutImage(__DRIdrawable * draw, int op,
119 int x, int y, int w, int h,
120 char *data, void *loaderPrivate)
121 {
122 struct dri2_egl_surface *dri2_surf = loaderPrivate;
123 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
124
125 xcb_gcontext_t gc;
126
127 switch (op) {
128 case __DRI_SWRAST_IMAGE_OP_DRAW:
129 gc = dri2_surf->gc;
130 break;
131 case __DRI_SWRAST_IMAGE_OP_SWAP:
132 gc = dri2_surf->swapgc;
133 break;
134 default:
135 return;
136 }
137
138 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
139 gc, w, h, x, y, 0, dri2_surf->depth,
140 w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
141 }
142
143 static void
144 swrastGetImage(__DRIdrawable * read,
145 int x, int y, int w, int h,
146 char *data, void *loaderPrivate)
147 {
148 struct dri2_egl_surface *dri2_surf = loaderPrivate;
149 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
150
151 xcb_get_image_cookie_t cookie;
152 xcb_get_image_reply_t *reply;
153 xcb_generic_error_t *error;
154
155 cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
156 dri2_surf->drawable, x, y, w, h, ~0);
157 reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
158 if (reply == NULL)
159 return;
160
161 if (error != NULL) {
162 _eglLog(_EGL_WARNING, "error in xcb_get_image");
163 free(error);
164 } else {
165 uint32_t bytes = xcb_get_image_data_length(reply);
166 uint8_t *idata = xcb_get_image_data(reply);
167 memcpy(data, idata, bytes);
168 }
169 free(reply);
170 }
171
172
173 /**
174 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
175 */
176 static _EGLSurface *
177 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
178 _EGLConfig *conf, EGLNativeWindowType native_window,
179 const EGLint *attrib_list)
180 {
181 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
182 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
183 struct dri2_egl_surface *dri2_surf;
184 xcb_get_geometry_cookie_t cookie;
185 xcb_get_geometry_reply_t *reply;
186 xcb_screen_iterator_t s;
187 xcb_generic_error_t *error;
188 xcb_drawable_t window = (uintptr_t )native_window;
189
190 (void) drv;
191
192 dri2_surf = malloc(sizeof *dri2_surf);
193 if (!dri2_surf) {
194 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
195 return NULL;
196 }
197
198 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
199 goto cleanup_surf;
200
201 dri2_surf->region = XCB_NONE;
202 if (type == EGL_PBUFFER_BIT) {
203 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
204 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
205 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
206 dri2_surf->drawable, s.data->root,
207 dri2_surf->base.Width, dri2_surf->base.Height);
208 } else {
209 dri2_surf->drawable = window;
210 }
211
212 if (dri2_dpy->dri2) {
213 dri2_surf->dri_drawable =
214 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
215 type == EGL_WINDOW_BIT ?
216 dri2_conf->dri_double_config :
217 dri2_conf->dri_single_config,
218 dri2_surf);
219 } else {
220 assert(dri2_dpy->swrast);
221 dri2_surf->dri_drawable =
222 (*dri2_dpy->swrast->createNewDrawable) (dri2_dpy->dri_screen,
223 dri2_conf->dri_double_config,
224 dri2_surf);
225 }
226
227 if (dri2_surf->dri_drawable == NULL) {
228 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
229 goto cleanup_pixmap;
230 }
231
232 if (dri2_dpy->dri2) {
233 xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
234 } else {
235 swrastCreateDrawable(dri2_dpy, dri2_surf, _eglGetConfigKey(conf, EGL_BUFFER_SIZE));
236 }
237
238 if (type != EGL_PBUFFER_BIT) {
239 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
240 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
241 if (reply == NULL || error != NULL) {
242 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
243 free(error);
244 goto cleanup_dri_drawable;
245 }
246
247 dri2_surf->base.Width = reply->width;
248 dri2_surf->base.Height = reply->height;
249 free(reply);
250 }
251
252 /* we always copy the back buffer to front */
253 dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
254
255 return &dri2_surf->base;
256
257 cleanup_dri_drawable:
258 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
259 cleanup_pixmap:
260 if (type == EGL_PBUFFER_BIT)
261 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
262 cleanup_surf:
263 free(dri2_surf);
264
265 return NULL;
266 }
267
268 /**
269 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
270 */
271 static _EGLSurface *
272 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
273 _EGLConfig *conf, EGLNativeWindowType window,
274 const EGLint *attrib_list)
275 {
276 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
277 window, attrib_list);
278 }
279
280 static _EGLSurface *
281 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
282 _EGLConfig *conf, EGLNativePixmapType pixmap,
283 const EGLint *attrib_list)
284 {
285 return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
286 pixmap, attrib_list);
287 }
288
289 static _EGLSurface *
290 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
291 _EGLConfig *conf, const EGLint *attrib_list)
292 {
293 return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
294 XCB_WINDOW_NONE, attrib_list);
295 }
296
297 static EGLBoolean
298 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
299 {
300 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
301 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
302
303 (void) drv;
304
305 if (!_eglPutSurface(surf))
306 return EGL_TRUE;
307
308 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
309
310 if (dri2_dpy->dri2) {
311 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
312 } else {
313 assert(dri2_dpy->swrast);
314 swrastDestroyDrawable(dri2_dpy, dri2_surf);
315 }
316
317 if (surf->Type == EGL_PBUFFER_BIT)
318 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
319
320 free(surf);
321
322 return EGL_TRUE;
323 }
324
325 /**
326 * Process list of buffer received from the server
327 *
328 * Processes the list of buffers received in a reply from the server to either
329 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
330 */
331 static void
332 dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
333 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
334 {
335 struct dri2_egl_display *dri2_dpy =
336 dri2_egl_display(dri2_surf->base.Resource.Display);
337 xcb_rectangle_t rectangle;
338 unsigned i;
339
340 dri2_surf->buffer_count = count;
341 dri2_surf->have_fake_front = 0;
342
343 /* This assumes the DRI2 buffer attachment tokens matches the
344 * __DRIbuffer tokens. */
345 for (i = 0; i < count; i++) {
346 dri2_surf->buffers[i].attachment = buffers[i].attachment;
347 dri2_surf->buffers[i].name = buffers[i].name;
348 dri2_surf->buffers[i].pitch = buffers[i].pitch;
349 dri2_surf->buffers[i].cpp = buffers[i].cpp;
350 dri2_surf->buffers[i].flags = buffers[i].flags;
351
352 /* We only use the DRI drivers single buffer configs. This
353 * means that if we try to render to a window, DRI2 will give us
354 * the fake front buffer, which we'll use as a back buffer.
355 * Note that EGL doesn't require that several clients rendering
356 * to the same window must see the same aux buffers. */
357 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
358 dri2_surf->have_fake_front = 1;
359 }
360
361 if (dri2_surf->region != XCB_NONE)
362 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
363
364 rectangle.x = 0;
365 rectangle.y = 0;
366 rectangle.width = dri2_surf->base.Width;
367 rectangle.height = dri2_surf->base.Height;
368 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
369 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
370 }
371
372 static __DRIbuffer *
373 dri2_get_buffers(__DRIdrawable * driDrawable,
374 int *width, int *height,
375 unsigned int *attachments, int count,
376 int *out_count, void *loaderPrivate)
377 {
378 struct dri2_egl_surface *dri2_surf = loaderPrivate;
379 struct dri2_egl_display *dri2_dpy =
380 dri2_egl_display(dri2_surf->base.Resource.Display);
381 xcb_dri2_dri2_buffer_t *buffers;
382 xcb_dri2_get_buffers_reply_t *reply;
383 xcb_dri2_get_buffers_cookie_t cookie;
384
385 (void) driDrawable;
386
387 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
388 dri2_surf->drawable,
389 count, count, attachments);
390 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
391 buffers = xcb_dri2_get_buffers_buffers (reply);
392 if (buffers == NULL)
393 return NULL;
394
395 *out_count = reply->count;
396 dri2_surf->base.Width = *width = reply->width;
397 dri2_surf->base.Height = *height = reply->height;
398 dri2_process_buffers(dri2_surf, buffers, *out_count);
399
400 free(reply);
401
402 return dri2_surf->buffers;
403 }
404
405 static __DRIbuffer *
406 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
407 int *width, int *height,
408 unsigned int *attachments, int count,
409 int *out_count, void *loaderPrivate)
410 {
411 struct dri2_egl_surface *dri2_surf = loaderPrivate;
412 struct dri2_egl_display *dri2_dpy =
413 dri2_egl_display(dri2_surf->base.Resource.Display);
414 xcb_dri2_dri2_buffer_t *buffers;
415 xcb_dri2_get_buffers_with_format_reply_t *reply;
416 xcb_dri2_get_buffers_with_format_cookie_t cookie;
417 xcb_dri2_attach_format_t *format_attachments;
418
419 (void) driDrawable;
420
421 format_attachments = (xcb_dri2_attach_format_t *) attachments;
422 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
423 dri2_surf->drawable,
424 count, count,
425 format_attachments);
426
427 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
428 cookie, NULL);
429 if (reply == NULL)
430 return NULL;
431
432 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
433 dri2_surf->base.Width = *width = reply->width;
434 dri2_surf->base.Height = *height = reply->height;
435 *out_count = reply->count;
436 dri2_process_buffers(dri2_surf, buffers, *out_count);
437
438 free(reply);
439
440 return dri2_surf->buffers;
441 }
442
443 static void
444 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
445 {
446 (void) driDrawable;
447
448 /* FIXME: Does EGL support front buffer rendering at all? */
449
450 #if 0
451 struct dri2_egl_surface *dri2_surf = loaderPrivate;
452
453 dri2WaitGL(dri2_surf);
454 #else
455 (void) loaderPrivate;
456 #endif
457 }
458
459 static char *
460 dri2_strndup(const char *s, int length)
461 {
462 char *d;
463
464 d = malloc(length + 1);
465 if (d == NULL)
466 return NULL;
467
468 memcpy(d, s, length);
469 d[length] = '\0';
470
471 return d;
472 }
473
474 static EGLBoolean
475 dri2_connect(struct dri2_egl_display *dri2_dpy)
476 {
477 xcb_xfixes_query_version_reply_t *xfixes_query;
478 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
479 xcb_dri2_query_version_reply_t *dri2_query;
480 xcb_dri2_query_version_cookie_t dri2_query_cookie;
481 xcb_dri2_connect_reply_t *connect;
482 xcb_dri2_connect_cookie_t connect_cookie;
483 xcb_generic_error_t *error;
484 xcb_screen_iterator_t s;
485 char *driver_name, *device_name;
486 const xcb_query_extension_reply_t *extension;
487
488 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
489 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
490
491 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
492 if (!(extension && extension->present))
493 return EGL_FALSE;
494
495 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
496 if (!(extension && extension->present))
497 return EGL_FALSE;
498
499 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
500 XCB_XFIXES_MAJOR_VERSION,
501 XCB_XFIXES_MINOR_VERSION);
502
503 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
504 XCB_DRI2_MAJOR_VERSION,
505 XCB_DRI2_MINOR_VERSION);
506
507 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
508 connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
509 s.data->root,
510 XCB_DRI2_DRIVER_TYPE_DRI);
511
512 xfixes_query =
513 xcb_xfixes_query_version_reply (dri2_dpy->conn,
514 xfixes_query_cookie, &error);
515 if (xfixes_query == NULL ||
516 error != NULL || xfixes_query->major_version < 2) {
517 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
518 free(error);
519 return EGL_FALSE;
520 }
521 free(xfixes_query);
522
523 dri2_query =
524 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
525 if (dri2_query == NULL || error != NULL) {
526 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
527 free(error);
528 return EGL_FALSE;
529 }
530 dri2_dpy->dri2_major = dri2_query->major_version;
531 dri2_dpy->dri2_minor = dri2_query->minor_version;
532 free(dri2_query);
533
534 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
535 if (connect == NULL ||
536 connect->driver_name_length + connect->device_name_length == 0) {
537 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
538 return EGL_FALSE;
539 }
540
541 driver_name = xcb_dri2_connect_driver_name (connect);
542 dri2_dpy->driver_name =
543 dri2_strndup(driver_name,
544 xcb_dri2_connect_driver_name_length (connect));
545
546 device_name = xcb_dri2_connect_device_name (connect);
547
548 dri2_dpy->device_name =
549 dri2_strndup(device_name,
550 xcb_dri2_connect_device_name_length (connect));
551
552 if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
553 free(dri2_dpy->device_name);
554 free(dri2_dpy->driver_name);
555 free(connect);
556 return EGL_FALSE;
557 }
558 free(connect);
559
560 return EGL_TRUE;
561 }
562
563 static int
564 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
565 {
566 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
567 xcb_dri2_authenticate_reply_t *authenticate;
568 xcb_dri2_authenticate_cookie_t authenticate_cookie;
569 xcb_screen_iterator_t s;
570 int ret = 0;
571
572 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
573 authenticate_cookie =
574 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, id);
575 authenticate =
576 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
577
578 if (authenticate == NULL || !authenticate->authenticated)
579 ret = -1;
580
581 free(authenticate);
582
583 return ret;
584 }
585
586 static EGLBoolean
587 dri2_authenticate(_EGLDisplay *disp)
588 {
589 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
590 drm_magic_t magic;
591
592 if (drmGetMagic(dri2_dpy->fd, &magic)) {
593 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
594 return EGL_FALSE;
595 }
596
597 if (dri2_x11_authenticate(disp, magic) < 0) {
598 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
599 return EGL_FALSE;
600 }
601
602 return EGL_TRUE;
603 }
604
605 static EGLBoolean
606 dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
607 _EGLDisplay *disp)
608 {
609 xcb_screen_iterator_t s;
610 xcb_depth_iterator_t d;
611 xcb_visualtype_t *visuals;
612 int i, j, id;
613 EGLint surface_type;
614 EGLint config_attrs[] = {
615 EGL_NATIVE_VISUAL_ID, 0,
616 EGL_NATIVE_VISUAL_TYPE, 0,
617 EGL_NONE
618 };
619
620 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
621 d = xcb_screen_allowed_depths_iterator(s.data);
622 id = 1;
623
624 surface_type =
625 EGL_WINDOW_BIT |
626 EGL_PIXMAP_BIT |
627 EGL_PBUFFER_BIT |
628 EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
629
630 while (d.rem > 0) {
631 EGLBoolean class_added[6] = { 0, };
632
633 visuals = xcb_depth_visuals(d.data);
634 for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
635 if (class_added[visuals[i]._class])
636 continue;
637
638 class_added[visuals[i]._class] = EGL_TRUE;
639 for (j = 0; dri2_dpy->driver_configs[j]; j++) {
640 config_attrs[1] = visuals[i].visual_id;
641 config_attrs[3] = visuals[i]._class;
642
643 dri2_add_config(disp, dri2_dpy->driver_configs[j], id++,
644 d.data->depth, surface_type, config_attrs, NULL);
645 }
646 }
647
648 xcb_depth_next(&d);
649 }
650
651 if (!_eglGetArraySize(disp->Configs)) {
652 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
653 return EGL_FALSE;
654 }
655
656 return EGL_TRUE;
657 }
658
659 static EGLBoolean
660 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
661 _EGLSurface *draw, xcb_xfixes_region_t region)
662 {
663 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
664 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
665 enum xcb_dri2_attachment_t render_attachment;
666 xcb_dri2_copy_region_cookie_t cookie;
667
668 /* No-op for a pixmap or pbuffer surface */
669 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
670 return EGL_TRUE;
671
672 if (dri2_dpy->flush)
673 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
674
675 if (dri2_surf->have_fake_front)
676 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
677 else
678 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
679
680 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
681 dri2_surf->drawable,
682 region,
683 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
684 render_attachment);
685 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
686
687 return EGL_TRUE;
688 }
689
690 static int64_t
691 dri2_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
692 int64_t msc, int64_t divisor, int64_t remainder)
693 {
694 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
695 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
696 uint32_t msc_hi = msc >> 32;
697 uint32_t msc_lo = msc & 0xffffffff;
698 uint32_t divisor_hi = divisor >> 32;
699 uint32_t divisor_lo = divisor & 0xffffffff;
700 uint32_t remainder_hi = remainder >> 32;
701 uint32_t remainder_lo = remainder & 0xffffffff;
702 xcb_dri2_swap_buffers_cookie_t cookie;
703 xcb_dri2_swap_buffers_reply_t *reply;
704 int64_t swap_count = -1;
705
706 /* No-op for a pixmap or pbuffer surface */
707 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
708 return 0;
709
710 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available)
711 return dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
712
713 if (dri2_dpy->flush)
714 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
715
716 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn, dri2_surf->drawable,
717 msc_hi, msc_lo, divisor_hi, divisor_lo, remainder_hi, remainder_lo);
718
719 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
720
721 if (reply) {
722 swap_count = (((int64_t)reply->swap_hi) << 32) | reply->swap_lo;
723 free(reply);
724 }
725
726 /* If the server doesn't send invalidate events */
727 if (dri2_dpy->invalidate_available && dri2_dpy->flush &&
728 dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
729 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
730
731 return swap_count;
732 }
733
734 static EGLBoolean
735 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
736 {
737 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
738 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
739
740 if (dri2_dpy->dri2) {
741 return dri2_swap_buffers_msc(drv, disp, draw, 0, 0, 0) != -1;
742 } else {
743 assert(dri2_dpy->swrast);
744
745 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
746 return EGL_TRUE;
747 }
748 }
749
750 static EGLBoolean
751 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
752 EGLint numRects, const EGLint *rects)
753 {
754 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
755 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
756 EGLBoolean ret;
757 xcb_xfixes_region_t region;
758 xcb_rectangle_t rectangles[16];
759 int i;
760
761 if (numRects > (int)ARRAY_SIZE(rectangles))
762 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
763
764 for (i = 0; i < numRects; i++) {
765 rectangles[i].x = rects[i * 4];
766 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
767 rectangles[i].width = rects[i * 4 + 2];
768 rectangles[i].height = rects[i * 4 + 3];
769 }
770
771 region = xcb_generate_id(dri2_dpy->conn);
772 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
773 ret = dri2_copy_region(drv, disp, draw, region);
774 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
775
776 return ret;
777 }
778
779 static EGLBoolean
780 dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
781 EGLint x, EGLint y, EGLint width, EGLint height)
782 {
783 const EGLint rect[4] = { x, draw->Height - y - height, width, height };
784
785 if (x < 0 || y < 0 || width < 0 || height < 0)
786 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
787
788 return dri2_swap_buffers_region(drv, disp, draw, 1, rect);
789 }
790
791 static EGLBoolean
792 dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
793 {
794 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
795 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
796
797 /* XXX Check vblank_mode here? */
798
799 if (interval > surf->Config->MaxSwapInterval)
800 interval = surf->Config->MaxSwapInterval;
801 else if (interval < surf->Config->MinSwapInterval)
802 interval = surf->Config->MinSwapInterval;
803
804 if (interval != surf->SwapInterval && dri2_dpy->swap_available)
805 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
806
807 surf->SwapInterval = interval;
808
809 return EGL_TRUE;
810 }
811
812 static EGLBoolean
813 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
814 EGLNativePixmapType native_target)
815 {
816 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
817 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
818 xcb_gcontext_t gc;
819 xcb_pixmap_t target = (uintptr_t )native_target;
820
821 (void) drv;
822
823 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
824
825 gc = xcb_generate_id(dri2_dpy->conn);
826 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
827 xcb_copy_area(dri2_dpy->conn,
828 dri2_surf->drawable,
829 target,
830 gc,
831 0, 0,
832 0, 0,
833 dri2_surf->base.Width,
834 dri2_surf->base.Height);
835 xcb_free_gc(dri2_dpy->conn, gc);
836
837 return EGL_TRUE;
838 }
839
840 static _EGLImage *
841 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
842 EGLClientBuffer buffer, const EGLint *attr_list)
843 {
844 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
845 struct dri2_egl_image *dri2_img;
846 unsigned int attachments[1];
847 xcb_drawable_t drawable;
848 xcb_dri2_get_buffers_cookie_t buffers_cookie;
849 xcb_dri2_get_buffers_reply_t *buffers_reply;
850 xcb_dri2_dri2_buffer_t *buffers;
851 xcb_get_geometry_cookie_t geometry_cookie;
852 xcb_get_geometry_reply_t *geometry_reply;
853 xcb_generic_error_t *error;
854 int stride, format;
855
856 (void) ctx;
857
858 drawable = (xcb_drawable_t) (uintptr_t) buffer;
859 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
860 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
861 buffers_cookie =
862 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
863 drawable, 1, 1, attachments);
864 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
865 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
866 buffers_cookie, NULL);
867 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
868 if (buffers == NULL) {
869 return NULL;
870 }
871
872 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
873 geometry_cookie, &error);
874 if (geometry_reply == NULL || error != NULL) {
875 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
876 free(error);
877 free(buffers_reply);
878 return NULL;
879 }
880
881 switch (geometry_reply->depth) {
882 case 16:
883 format = __DRI_IMAGE_FORMAT_RGB565;
884 break;
885 case 24:
886 format = __DRI_IMAGE_FORMAT_XRGB8888;
887 break;
888 case 32:
889 format = __DRI_IMAGE_FORMAT_ARGB8888;
890 break;
891 default:
892 _eglError(EGL_BAD_PARAMETER,
893 "dri2_create_image_khr: unsupported pixmap depth");
894 free(buffers_reply);
895 free(geometry_reply);
896 return NULL;
897 }
898
899 dri2_img = malloc(sizeof *dri2_img);
900 if (!dri2_img) {
901 free(buffers_reply);
902 free(geometry_reply);
903 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
904 return EGL_NO_IMAGE_KHR;
905 }
906
907 if (!_eglInitImage(&dri2_img->base, disp)) {
908 free(buffers_reply);
909 free(geometry_reply);
910 free(dri2_img);
911 return EGL_NO_IMAGE_KHR;
912 }
913
914 stride = buffers[0].pitch / buffers[0].cpp;
915 dri2_img->dri_image =
916 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
917 buffers_reply->width,
918 buffers_reply->height,
919 format,
920 buffers[0].name,
921 stride,
922 dri2_img);
923
924 free(buffers_reply);
925 free(geometry_reply);
926
927 return &dri2_img->base;
928 }
929
930 static _EGLImage *
931 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
932 _EGLContext *ctx, EGLenum target,
933 EGLClientBuffer buffer, const EGLint *attr_list)
934 {
935 (void) drv;
936
937 switch (target) {
938 case EGL_NATIVE_PIXMAP_KHR:
939 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
940 default:
941 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
942 }
943 }
944
945 static EGLBoolean
946 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
947 {
948 struct dri2_egl_display *dri2_dpy;
949
950 drv->API.CreateWindowSurface = dri2_create_window_surface;
951 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
952 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
953 drv->API.DestroySurface = dri2_destroy_surface;
954 drv->API.SwapBuffers = dri2_swap_buffers;
955 drv->API.CopyBuffers = dri2_copy_buffers;
956
957 drv->API.SwapBuffersRegionNOK = NULL;
958 drv->API.CreateImageKHR = NULL;
959 drv->API.DestroyImageKHR = NULL;
960 drv->API.CreateDRMImageMESA = NULL;
961 drv->API.ExportDRMImageMESA = NULL;
962
963 dri2_dpy = calloc(1, sizeof *dri2_dpy);
964 if (!dri2_dpy)
965 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
966
967 disp->DriverData = (void *) dri2_dpy;
968 if (disp->PlatformDisplay == NULL) {
969 dri2_dpy->conn = xcb_connect(0, 0);
970 } else {
971 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
972 }
973
974 if (xcb_connection_has_error(dri2_dpy->conn)) {
975 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
976 goto cleanup_dpy;
977 }
978
979 if (!dri2_load_driver_swrast(disp))
980 goto cleanup_conn;
981
982 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
983 dri2_dpy->swrast_loader_extension.base.version = __DRI_SWRAST_LOADER_VERSION;
984 dri2_dpy->swrast_loader_extension.getDrawableInfo = swrastGetDrawableInfo;
985 dri2_dpy->swrast_loader_extension.putImage = swrastPutImage;
986 dri2_dpy->swrast_loader_extension.getImage = swrastGetImage;
987
988 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
989 dri2_dpy->extensions[1] = NULL;
990 dri2_dpy->extensions[2] = NULL;
991
992 if (!dri2_create_screen(disp))
993 goto cleanup_driver;
994
995 if (dri2_dpy->conn) {
996 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
997 goto cleanup_configs;
998 }
999
1000 /* we're supporting EGL 1.4 */
1001 disp->VersionMajor = 1;
1002 disp->VersionMinor = 4;
1003
1004 return EGL_TRUE;
1005
1006 cleanup_configs:
1007 _eglCleanupDisplay(disp);
1008 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1009 cleanup_driver:
1010 dlclose(dri2_dpy->driver);
1011 cleanup_conn:
1012 if (disp->PlatformDisplay == NULL)
1013 xcb_disconnect(dri2_dpy->conn);
1014 cleanup_dpy:
1015 free(dri2_dpy);
1016
1017 return EGL_FALSE;
1018 }
1019
1020
1021 static EGLBoolean
1022 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1023 {
1024 struct dri2_egl_display *dri2_dpy;
1025
1026 drv->API.CreateWindowSurface = dri2_create_window_surface;
1027 drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
1028 drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
1029 drv->API.DestroySurface = dri2_destroy_surface;
1030 drv->API.SwapBuffers = dri2_swap_buffers;
1031 drv->API.CopyBuffers = dri2_copy_buffers;
1032 drv->API.CreateImageKHR = dri2_x11_create_image_khr;
1033 drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
1034 drv->API.PostSubBufferNV = dri2_post_sub_buffer;
1035 drv->API.SwapInterval = dri2_swap_interval;
1036
1037 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1038 if (!dri2_dpy)
1039 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1040
1041 disp->DriverData = (void *) dri2_dpy;
1042 if (disp->PlatformDisplay == NULL) {
1043 dri2_dpy->conn = xcb_connect(0, 0);
1044 } else {
1045 dri2_dpy->conn = XGetXCBConnection((Display *) disp->PlatformDisplay);
1046 }
1047
1048 if (xcb_connection_has_error(dri2_dpy->conn)) {
1049 _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
1050 goto cleanup_dpy;
1051 }
1052
1053 if (dri2_dpy->conn) {
1054 if (!dri2_connect(dri2_dpy))
1055 goto cleanup_conn;
1056 }
1057
1058 if (!dri2_load_driver(disp))
1059 goto cleanup_conn;
1060
1061 #ifdef O_CLOEXEC
1062 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
1063 if (dri2_dpy->fd == -1 && errno == EINVAL)
1064 #endif
1065 {
1066 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
1067 if (dri2_dpy->fd != -1)
1068 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
1069 FD_CLOEXEC);
1070 }
1071 if (dri2_dpy->fd == -1) {
1072 _eglLog(_EGL_WARNING,
1073 "DRI2: could not open %s (%s)", dri2_dpy->device_name,
1074 strerror(errno));
1075 goto cleanup_driver;
1076 }
1077
1078 if (dri2_dpy->conn) {
1079 if (!dri2_authenticate(disp))
1080 goto cleanup_fd;
1081 }
1082
1083 if (dri2_dpy->dri2_minor >= 1) {
1084 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1085 dri2_dpy->dri2_loader_extension.base.version = 3;
1086 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1087 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1088 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1089 dri2_get_buffers_with_format;
1090 } else {
1091 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1092 dri2_dpy->dri2_loader_extension.base.version = 2;
1093 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
1094 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
1095 dri2_dpy->dri2_loader_extension.getBuffersWithFormat = NULL;
1096 }
1097
1098 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1099 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1100 dri2_dpy->extensions[2] = NULL;
1101
1102 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1103 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1104
1105 if (!dri2_create_screen(disp))
1106 goto cleanup_fd;
1107
1108 if (dri2_dpy->conn) {
1109 if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
1110 goto cleanup_configs;
1111 }
1112
1113 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1114 disp->Extensions.NOK_swap_region = EGL_TRUE;
1115 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1116 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1117
1118 #ifdef HAVE_WAYLAND_PLATFORM
1119 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1120 #endif
1121 dri2_dpy->authenticate = dri2_x11_authenticate;
1122
1123 /* we're supporting EGL 1.4 */
1124 disp->VersionMajor = 1;
1125 disp->VersionMinor = 4;
1126
1127 return EGL_TRUE;
1128
1129 cleanup_configs:
1130 _eglCleanupDisplay(disp);
1131 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1132 cleanup_fd:
1133 close(dri2_dpy->fd);
1134 cleanup_driver:
1135 dlclose(dri2_dpy->driver);
1136 cleanup_conn:
1137 if (disp->PlatformDisplay == NULL)
1138 xcb_disconnect(dri2_dpy->conn);
1139 cleanup_dpy:
1140 free(dri2_dpy);
1141
1142 return EGL_FALSE;
1143 }
1144
1145 EGLBoolean
1146 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1147 {
1148 EGLBoolean initialized = EGL_TRUE;
1149
1150 int x11_dri2_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1151
1152 if (x11_dri2_accel) {
1153 if (!dri2_initialize_x11_dri2(drv, disp)) {
1154 initialized = dri2_initialize_x11_swrast(drv, disp);
1155 }
1156 } else {
1157 initialized = dri2_initialize_x11_swrast(drv, disp);
1158 }
1159
1160 return initialized;
1161 }
1162