7a58e71849285c32928d5c85f6f854fbb9eb9eb7
[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 <stdbool.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #ifdef HAVE_LIBDRM
39 #include <xf86drm.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include "util/debug.h"
44 #include "util/macros.h"
45
46 #include "egl_dri2.h"
47 #include "egl_dri2_fallbacks.h"
48 #include "loader.h"
49
50 #ifdef HAVE_DRI3
51 #include "platform_x11_dri3.h"
52 #endif
53
54 static EGLBoolean
55 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
56 EGLint interval);
57
58 uint32_t
59 dri2_format_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth);
60
61 static void
62 swrastCreateDrawable(struct dri2_egl_display * dri2_dpy,
63 struct dri2_egl_surface * dri2_surf)
64 {
65 uint32_t mask;
66 const uint32_t function = GXcopy;
67 uint32_t valgc[2];
68
69 /* create GC's */
70 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
71 mask = XCB_GC_FUNCTION;
72 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask, &function);
73
74 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
75 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
76 valgc[0] = function;
77 valgc[1] = False;
78 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask, valgc);
79 switch (dri2_surf->depth) {
80 case 32:
81 case 30:
82 case 24:
83 dri2_surf->bytes_per_pixel = 4;
84 break;
85 case 16:
86 dri2_surf->bytes_per_pixel = 2;
87 break;
88 case 8:
89 dri2_surf->bytes_per_pixel = 1;
90 break;
91 case 0:
92 dri2_surf->bytes_per_pixel = 0;
93 break;
94 default:
95 _eglLog(_EGL_WARNING, "unsupported depth %d", dri2_surf->depth);
96 }
97 }
98
99 static void
100 swrastDestroyDrawable(struct dri2_egl_display * dri2_dpy,
101 struct dri2_egl_surface * dri2_surf)
102 {
103 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
104 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
105 }
106
107 static bool
108 x11_get_drawable_info(__DRIdrawable * draw,
109 int *x, int *y, int *w, int *h,
110 void *loaderPrivate)
111 {
112 struct dri2_egl_surface *dri2_surf = loaderPrivate;
113 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
114
115 xcb_get_geometry_cookie_t cookie;
116 xcb_get_geometry_reply_t *reply;
117 xcb_generic_error_t *error;
118 bool ret;
119
120 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
121 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
122 if (reply == NULL)
123 return false;
124
125 if (error != NULL) {
126 ret = false;
127 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
128 free(error);
129 } else {
130 *x = reply->x;
131 *y = reply->y;
132 *w = reply->width;
133 *h = reply->height;
134 ret = true;
135 }
136 free(reply);
137 return ret;
138 }
139
140 static void
141 swrastGetDrawableInfo(__DRIdrawable * draw,
142 int *x, int *y, int *w, int *h,
143 void *loaderPrivate)
144 {
145 *x = *y = *w = *h = 0;
146 x11_get_drawable_info(draw, x, y, w, h, loaderPrivate);
147 }
148
149 static void
150 swrastPutImage(__DRIdrawable * draw, int op,
151 int x, int y, int w, int h,
152 char *data, void *loaderPrivate)
153 {
154 struct dri2_egl_surface *dri2_surf = loaderPrivate;
155 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
156
157 xcb_gcontext_t gc;
158
159 switch (op) {
160 case __DRI_SWRAST_IMAGE_OP_DRAW:
161 gc = dri2_surf->gc;
162 break;
163 case __DRI_SWRAST_IMAGE_OP_SWAP:
164 gc = dri2_surf->swapgc;
165 break;
166 default:
167 return;
168 }
169
170 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable,
171 gc, w, h, x, y, 0, dri2_surf->depth,
172 w*h*dri2_surf->bytes_per_pixel, (const uint8_t *)data);
173 }
174
175 static void
176 swrastGetImage(__DRIdrawable * read,
177 int x, int y, int w, int h,
178 char *data, void *loaderPrivate)
179 {
180 struct dri2_egl_surface *dri2_surf = loaderPrivate;
181 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
182
183 xcb_get_image_cookie_t cookie;
184 xcb_get_image_reply_t *reply;
185 xcb_generic_error_t *error;
186
187 cookie = xcb_get_image (dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
188 dri2_surf->drawable, x, y, w, h, ~0);
189 reply = xcb_get_image_reply (dri2_dpy->conn, cookie, &error);
190 if (reply == NULL)
191 return;
192
193 if (error != NULL) {
194 _eglLog(_EGL_WARNING, "error in xcb_get_image");
195 free(error);
196 } else {
197 uint32_t bytes = xcb_get_image_data_length(reply);
198 uint8_t *idata = xcb_get_image_data(reply);
199 memcpy(data, idata, bytes);
200 }
201 free(reply);
202 }
203
204
205 static xcb_screen_t *
206 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
207 {
208 for (; iter.rem; --screen, xcb_screen_next(&iter))
209 if (screen == 0)
210 return iter.data;
211
212 return NULL;
213 }
214
215 static xcb_visualtype_t *
216 get_xcb_visualtype_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
217 {
218 xcb_visualtype_iterator_t visual_iter;
219 xcb_screen_t *screen = dri2_dpy->screen;
220 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(screen);
221
222 for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
223 if (depth_iter.data->depth != depth)
224 continue;
225
226 visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
227 if (visual_iter.rem)
228 return visual_iter.data;
229 }
230
231 return NULL;
232 }
233
234 /* Get red channel mask for given depth. */
235 unsigned int
236 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
237 {
238 xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(dri2_dpy, depth);
239
240 if (visual)
241 return visual->red_mask;
242
243 return 0;
244 }
245
246 /**
247 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
248 */
249 static _EGLSurface *
250 dri2_x11_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
251 _EGLConfig *conf, void *native_surface,
252 const EGLint *attrib_list)
253 {
254 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
255 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
256 struct dri2_egl_surface *dri2_surf;
257 xcb_get_geometry_cookie_t cookie;
258 xcb_get_geometry_reply_t *reply;
259 xcb_generic_error_t *error;
260 const __DRIconfig *config;
261
262 (void) drv;
263
264 dri2_surf = calloc(1, sizeof *dri2_surf);
265 if (!dri2_surf) {
266 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
267 return NULL;
268 }
269
270 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
271 false, native_surface))
272 goto cleanup_surf;
273
274 dri2_surf->region = XCB_NONE;
275 if (type == EGL_PBUFFER_BIT) {
276 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
277 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize,
278 dri2_surf->drawable, dri2_dpy->screen->root,
279 dri2_surf->base.Width, dri2_surf->base.Height);
280 } else {
281 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
282 dri2_surf->drawable = (uintptr_t) native_surface;
283 }
284
285 config = dri2_get_dri_config(dri2_conf, type,
286 dri2_surf->base.GLColorspace);
287
288 if (!config) {
289 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
290 goto cleanup_pixmap;
291 }
292
293 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
294 goto cleanup_pixmap;
295
296 if (type != EGL_PBUFFER_BIT) {
297 cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
298 reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
299 if (error != NULL) {
300 if (error->error_code == BadAlloc)
301 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
302 else if (type == EGL_WINDOW_BIT)
303 _eglError(EGL_BAD_NATIVE_WINDOW, "xcb_get_geometry");
304 else
305 _eglError(EGL_BAD_NATIVE_PIXMAP, "xcb_get_geometry");
306 free(error);
307 goto cleanup_dri_drawable;
308 } else if (reply == NULL) {
309 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
310 goto cleanup_dri_drawable;
311 }
312
313 dri2_surf->base.Width = reply->width;
314 dri2_surf->base.Height = reply->height;
315 dri2_surf->depth = reply->depth;
316 free(reply);
317 }
318
319 if (dri2_dpy->dri2) {
320 xcb_void_cookie_t cookie;
321 int conn_error;
322
323 cookie = xcb_dri2_create_drawable_checked(dri2_dpy->conn,
324 dri2_surf->drawable);
325 error = xcb_request_check(dri2_dpy->conn, cookie);
326 conn_error = xcb_connection_has_error(dri2_dpy->conn);
327 if (conn_error || error != NULL) {
328 if (type == EGL_PBUFFER_BIT || conn_error || error->error_code == BadAlloc)
329 _eglError(EGL_BAD_ALLOC, "xcb_dri2_create_drawable_checked");
330 else if (type == EGL_WINDOW_BIT)
331 _eglError(EGL_BAD_NATIVE_WINDOW,
332 "xcb_dri2_create_drawable_checked");
333 else
334 _eglError(EGL_BAD_NATIVE_PIXMAP,
335 "xcb_dri2_create_drawable_checked");
336 free(error);
337 goto cleanup_dri_drawable;
338 }
339 } else {
340 if (type == EGL_PBUFFER_BIT) {
341 dri2_surf->depth = _eglGetConfigKey(conf, EGL_BUFFER_SIZE);
342 }
343 swrastCreateDrawable(dri2_dpy, dri2_surf);
344 }
345
346 /* we always copy the back buffer to front */
347 dri2_surf->base.PostSubBufferSupportedNV = EGL_TRUE;
348
349 return &dri2_surf->base;
350
351 cleanup_dri_drawable:
352 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
353 cleanup_pixmap:
354 if (type == EGL_PBUFFER_BIT)
355 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
356 cleanup_surf:
357 free(dri2_surf);
358
359 return NULL;
360 }
361
362 /**
363 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
364 */
365 static _EGLSurface *
366 dri2_x11_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
367 _EGLConfig *conf, void *native_window,
368 const EGLint *attrib_list)
369 {
370 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
371 _EGLSurface *surf;
372
373 surf = dri2_x11_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
374 native_window, attrib_list);
375 if (surf != NULL) {
376 /* When we first create the DRI2 drawable, its swap interval on the
377 * server side is 1.
378 */
379 surf->SwapInterval = 1;
380
381 /* Override that with a driconf-set value. */
382 dri2_x11_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
383 }
384
385 return surf;
386 }
387
388 static _EGLSurface *
389 dri2_x11_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
390 _EGLConfig *conf, void *native_pixmap,
391 const EGLint *attrib_list)
392 {
393 return dri2_x11_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
394 native_pixmap, attrib_list);
395 }
396
397 static _EGLSurface *
398 dri2_x11_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
399 _EGLConfig *conf, const EGLint *attrib_list)
400 {
401 return dri2_x11_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
402 NULL, attrib_list);
403 }
404
405 static EGLBoolean
406 dri2_x11_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
407 {
408 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
409 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
410
411 (void) drv;
412
413 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
414
415 if (dri2_dpy->dri2) {
416 xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
417 } else {
418 assert(dri2_dpy->swrast);
419 swrastDestroyDrawable(dri2_dpy, dri2_surf);
420 }
421
422 if (surf->Type == EGL_PBUFFER_BIT)
423 xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
424
425 dri2_fini_surface(surf);
426 free(surf);
427
428 return EGL_TRUE;
429 }
430
431 /**
432 * Function utilizes swrastGetDrawableInfo to get surface
433 * geometry from x server and calls default query surface
434 * implementation that returns the updated values.
435 *
436 * In case of errors we still return values that we currently
437 * have.
438 */
439 static EGLBoolean
440 dri2_query_surface(_EGLDriver *drv, _EGLDisplay *disp,
441 _EGLSurface *surf, EGLint attribute,
442 EGLint *value)
443 {
444 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
445 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
446 int x, y, w, h;
447
448 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
449
450 switch (attribute) {
451 case EGL_WIDTH:
452 case EGL_HEIGHT:
453 if (x11_get_drawable_info(drawable, &x, &y, &w, &h, dri2_surf)) {
454 surf->Width = w;
455 surf->Height = h;
456 }
457 break;
458 default:
459 break;
460 }
461 return _eglQuerySurface(drv, disp, surf, attribute, value);
462 }
463
464 /**
465 * Process list of buffer received from the server
466 *
467 * Processes the list of buffers received in a reply from the server to either
468 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
469 */
470 static void
471 dri2_x11_process_buffers(struct dri2_egl_surface *dri2_surf,
472 xcb_dri2_dri2_buffer_t *buffers, unsigned count)
473 {
474 struct dri2_egl_display *dri2_dpy =
475 dri2_egl_display(dri2_surf->base.Resource.Display);
476 xcb_rectangle_t rectangle;
477
478 dri2_surf->have_fake_front = false;
479
480 /* This assumes the DRI2 buffer attachment tokens matches the
481 * __DRIbuffer tokens. */
482 for (unsigned i = 0; i < count; i++) {
483 dri2_surf->buffers[i].attachment = buffers[i].attachment;
484 dri2_surf->buffers[i].name = buffers[i].name;
485 dri2_surf->buffers[i].pitch = buffers[i].pitch;
486 dri2_surf->buffers[i].cpp = buffers[i].cpp;
487 dri2_surf->buffers[i].flags = buffers[i].flags;
488
489 /* We only use the DRI drivers single buffer configs. This
490 * means that if we try to render to a window, DRI2 will give us
491 * the fake front buffer, which we'll use as a back buffer.
492 * Note that EGL doesn't require that several clients rendering
493 * to the same window must see the same aux buffers. */
494 if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
495 dri2_surf->have_fake_front = true;
496 }
497
498 if (dri2_surf->region != XCB_NONE)
499 xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
500
501 rectangle.x = 0;
502 rectangle.y = 0;
503 rectangle.width = dri2_surf->base.Width;
504 rectangle.height = dri2_surf->base.Height;
505 dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
506 xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
507 }
508
509 static __DRIbuffer *
510 dri2_x11_get_buffers(__DRIdrawable * driDrawable,
511 int *width, int *height,
512 unsigned int *attachments, int count,
513 int *out_count, void *loaderPrivate)
514 {
515 struct dri2_egl_surface *dri2_surf = loaderPrivate;
516 struct dri2_egl_display *dri2_dpy =
517 dri2_egl_display(dri2_surf->base.Resource.Display);
518 xcb_dri2_dri2_buffer_t *buffers;
519 xcb_dri2_get_buffers_reply_t *reply;
520 xcb_dri2_get_buffers_cookie_t cookie;
521
522 (void) driDrawable;
523
524 cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
525 dri2_surf->drawable,
526 count, count, attachments);
527 reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
528 if (reply == NULL)
529 return NULL;
530 buffers = xcb_dri2_get_buffers_buffers (reply);
531 if (buffers == NULL)
532 return NULL;
533
534 *out_count = reply->count;
535 dri2_surf->base.Width = *width = reply->width;
536 dri2_surf->base.Height = *height = reply->height;
537 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
538
539 free(reply);
540
541 return dri2_surf->buffers;
542 }
543
544 static __DRIbuffer *
545 dri2_x11_get_buffers_with_format(__DRIdrawable * driDrawable,
546 int *width, int *height,
547 unsigned int *attachments, int count,
548 int *out_count, void *loaderPrivate)
549 {
550 struct dri2_egl_surface *dri2_surf = loaderPrivate;
551 struct dri2_egl_display *dri2_dpy =
552 dri2_egl_display(dri2_surf->base.Resource.Display);
553 xcb_dri2_dri2_buffer_t *buffers;
554 xcb_dri2_get_buffers_with_format_reply_t *reply;
555 xcb_dri2_get_buffers_with_format_cookie_t cookie;
556 xcb_dri2_attach_format_t *format_attachments;
557
558 (void) driDrawable;
559
560 format_attachments = (xcb_dri2_attach_format_t *) attachments;
561 cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
562 dri2_surf->drawable,
563 count, count,
564 format_attachments);
565
566 reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
567 cookie, NULL);
568 if (reply == NULL)
569 return NULL;
570
571 buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
572 dri2_surf->base.Width = *width = reply->width;
573 dri2_surf->base.Height = *height = reply->height;
574 *out_count = reply->count;
575 dri2_x11_process_buffers(dri2_surf, buffers, *out_count);
576
577 free(reply);
578
579 return dri2_surf->buffers;
580 }
581
582 static void
583 dri2_x11_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
584 {
585 (void) driDrawable;
586
587 /* FIXME: Does EGL support front buffer rendering at all? */
588
589 #if 0
590 struct dri2_egl_surface *dri2_surf = loaderPrivate;
591
592 dri2WaitGL(dri2_surf);
593 #else
594 (void) loaderPrivate;
595 #endif
596 }
597
598 static int
599 dri2_x11_do_authenticate(struct dri2_egl_display *dri2_dpy, uint32_t id)
600 {
601 xcb_dri2_authenticate_reply_t *authenticate;
602 xcb_dri2_authenticate_cookie_t authenticate_cookie;
603 int ret = 0;
604
605 authenticate_cookie =
606 xcb_dri2_authenticate_unchecked(dri2_dpy->conn, dri2_dpy->screen->root, id);
607 authenticate =
608 xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
609
610 if (authenticate == NULL || !authenticate->authenticated)
611 ret = -1;
612
613 free(authenticate);
614
615 return ret;
616 }
617
618 static EGLBoolean
619 dri2_x11_local_authenticate(struct dri2_egl_display *dri2_dpy)
620 {
621 #ifdef HAVE_LIBDRM
622 drm_magic_t magic;
623
624 if (drmGetMagic(dri2_dpy->fd, &magic)) {
625 _eglLog(_EGL_WARNING, "DRI2: failed to get drm magic");
626 return EGL_FALSE;
627 }
628
629 if (dri2_x11_do_authenticate(dri2_dpy, magic) < 0) {
630 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
631 return EGL_FALSE;
632 }
633 #endif
634 return EGL_TRUE;
635 }
636
637 static EGLBoolean
638 dri2_x11_connect(struct dri2_egl_display *dri2_dpy)
639 {
640 xcb_xfixes_query_version_reply_t *xfixes_query;
641 xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
642 xcb_dri2_query_version_reply_t *dri2_query;
643 xcb_dri2_query_version_cookie_t dri2_query_cookie;
644 xcb_dri2_connect_reply_t *connect;
645 xcb_dri2_connect_cookie_t connect_cookie;
646 xcb_generic_error_t *error;
647 char *driver_name, *loader_driver_name, *device_name;
648 const xcb_query_extension_reply_t *extension;
649
650 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
651 xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
652
653 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_xfixes_id);
654 if (!(extension && extension->present))
655 return EGL_FALSE;
656
657 extension = xcb_get_extension_data(dri2_dpy->conn, &xcb_dri2_id);
658 if (!(extension && extension->present))
659 return EGL_FALSE;
660
661 xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
662 XCB_XFIXES_MAJOR_VERSION,
663 XCB_XFIXES_MINOR_VERSION);
664
665 dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
666 XCB_DRI2_MAJOR_VERSION,
667 XCB_DRI2_MINOR_VERSION);
668
669 connect_cookie = xcb_dri2_connect_unchecked(dri2_dpy->conn, dri2_dpy->screen->root,
670 XCB_DRI2_DRIVER_TYPE_DRI);
671
672 xfixes_query =
673 xcb_xfixes_query_version_reply (dri2_dpy->conn,
674 xfixes_query_cookie, &error);
675 if (xfixes_query == NULL ||
676 error != NULL || xfixes_query->major_version < 2) {
677 _eglLog(_EGL_WARNING, "DRI2: failed to query xfixes version");
678 free(error);
679 free(xfixes_query);
680 return EGL_FALSE;
681 }
682 free(xfixes_query);
683
684 dri2_query =
685 xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
686 if (dri2_query == NULL || error != NULL) {
687 _eglLog(_EGL_WARNING, "DRI2: failed to query version");
688 free(error);
689 return EGL_FALSE;
690 }
691 dri2_dpy->dri2_major = dri2_query->major_version;
692 dri2_dpy->dri2_minor = dri2_query->minor_version;
693 free(dri2_query);
694
695 connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
696 if (connect == NULL ||
697 connect->driver_name_length + connect->device_name_length == 0) {
698 _eglLog(_EGL_WARNING, "DRI2: failed to authenticate");
699 return EGL_FALSE;
700 }
701
702 device_name = xcb_dri2_connect_device_name (connect);
703
704 dri2_dpy->fd = loader_open_device(device_name);
705 if (dri2_dpy->fd == -1) {
706 _eglLog(_EGL_WARNING,
707 "DRI2: could not open %s (%s)", device_name, strerror(errno));
708 free(connect);
709 return EGL_FALSE;
710 }
711
712 if (!dri2_x11_local_authenticate(dri2_dpy)) {
713 close(dri2_dpy->fd);
714 free(connect);
715 return EGL_FALSE;
716 }
717
718 driver_name = xcb_dri2_connect_driver_name (connect);
719
720 /* If Mesa knows about the appropriate driver for this fd, then trust it.
721 * Otherwise, default to the server's value.
722 */
723 loader_driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
724 if (loader_driver_name) {
725 dri2_dpy->driver_name = loader_driver_name;
726 } else {
727 dri2_dpy->driver_name =
728 strndup(driver_name,
729 xcb_dri2_connect_driver_name_length(connect));
730 }
731
732 if (dri2_dpy->driver_name == NULL) {
733 close(dri2_dpy->fd);
734 free(connect);
735 return EGL_FALSE;
736 }
737
738 #ifdef HAVE_WAYLAND_PLATFORM
739 dri2_dpy->device_name =
740 strndup(device_name,
741 xcb_dri2_connect_device_name_length(connect));
742 #endif
743
744 free(connect);
745
746 return EGL_TRUE;
747 }
748
749 static int
750 dri2_x11_authenticate(_EGLDisplay *disp, uint32_t id)
751 {
752 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
753
754 return dri2_x11_do_authenticate(dri2_dpy, id);
755 }
756
757 static bool
758 dri2_x11_config_match_attrib(struct dri2_egl_display *dri2_dpy,
759 const __DRIconfig *config,
760 unsigned int attrib,
761 unsigned int value)
762 {
763 uint32_t config_val;
764 if (!dri2_dpy->core->getConfigAttrib(config, attrib, &config_val))
765 return false;
766 return config_val == value;
767 }
768
769 /**
770 * See if the X server can export a pixmap with the given color depth.
771 *
772 * Glamor in xorg-server 1.20 can't export pixmaps which have a different
773 * color depth than the root window as a DRI image. This makes it impossible
774 * to expose pbuffer-only visuals with, say, 16bpp on a 24bpp X display.
775 */
776 static bool
777 x11_can_export_pixmap_with_bpp(struct dri2_egl_display *dri2_dpy, int bpp)
778 {
779 bool supported = false;
780
781 #ifdef HAVE_DRI3
782 xcb_dri3_buffer_from_pixmap_cookie_t cookie;
783 xcb_dri3_buffer_from_pixmap_reply_t *reply;
784
785 xcb_pixmap_t pixmap = xcb_generate_id(dri2_dpy->conn);
786 xcb_create_pixmap(dri2_dpy->conn, bpp, pixmap, dri2_dpy->screen->root, 1, 1);
787 cookie = xcb_dri3_buffer_from_pixmap(dri2_dpy->conn, pixmap);
788 reply = xcb_dri3_buffer_from_pixmap_reply(dri2_dpy->conn, cookie, NULL);
789
790 if (reply) {
791 int *fds = xcb_dri3_buffer_from_pixmap_reply_fds(dri2_dpy->conn, reply);
792
793 for (int i = 0; i < reply->nfd; i++) {
794 close(fds[i]);
795 }
796
797 supported = true;
798
799 free(reply);
800 }
801
802 xcb_free_pixmap(dri2_dpy->conn, pixmap);
803 #endif
804
805 return supported;
806 }
807
808 static EGLBoolean
809 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
810 _EGLDisplay *disp, bool supports_preserved,
811 bool add_pbuffer_configs)
812 {
813 xcb_depth_iterator_t d;
814 xcb_visualtype_t *visuals;
815 int config_count = 0;
816 EGLint surface_type;
817
818 d = xcb_screen_allowed_depths_iterator(dri2_dpy->screen);
819
820 surface_type =
821 EGL_WINDOW_BIT |
822 EGL_PIXMAP_BIT |
823 EGL_PBUFFER_BIT;
824
825 if (supports_preserved)
826 surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
827
828 while (d.rem > 0) {
829 EGLBoolean class_added[6] = { 0, };
830
831 visuals = xcb_depth_visuals(d.data);
832
833 for (int i = 0; i < xcb_depth_visuals_length(d.data); i++) {
834 if (class_added[visuals[i]._class])
835 continue;
836
837 class_added[visuals[i]._class] = EGL_TRUE;
838
839 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
840 struct dri2_egl_config *dri2_conf;
841 const __DRIconfig *config = dri2_dpy->driver_configs[j];
842
843 const EGLint config_attrs[] = {
844 EGL_NATIVE_VISUAL_ID, visuals[i].visual_id,
845 EGL_NATIVE_VISUAL_TYPE, visuals[i]._class,
846 EGL_NONE
847 };
848
849 unsigned int rgba_masks[4] = {
850 visuals[i].red_mask,
851 visuals[i].green_mask,
852 visuals[i].blue_mask,
853 0,
854 };
855
856 dri2_conf = dri2_add_config(disp, config, config_count + 1,
857 surface_type, config_attrs,
858 rgba_masks);
859 if (dri2_conf)
860 if (dri2_conf->base.ConfigID == config_count + 1)
861 config_count++;
862
863 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
864 * Ditto for 30-bit RGB visuals to match a 32-bit RGBA EGLConfig.
865 * Otherwise it will only match a 32-bit RGBA visual. On a
866 * composited window manager on X11, this will make all of the
867 * EGLConfigs with destination alpha get blended by the
868 * compositor. This is probably not what the application
869 * wants... especially on drivers that only have 32-bit RGBA
870 * EGLConfigs! */
871 if (d.data->depth == 24 || d.data->depth == 30) {
872 rgba_masks[3] =
873 ~(rgba_masks[0] | rgba_masks[1] | rgba_masks[2]);
874 dri2_conf = dri2_add_config(disp, config, config_count + 1,
875 surface_type, config_attrs,
876 rgba_masks);
877 if (dri2_conf)
878 if (dri2_conf->base.ConfigID == config_count + 1)
879 config_count++;
880 }
881 }
882 }
883
884 xcb_depth_next(&d);
885 }
886
887 /* Add a 565-no-depth-no-stencil pbuffer-only config. If X11 is depth 24,
888 * we wouldn't have 565 available, which the CTS demands.
889 */
890 if (add_pbuffer_configs && x11_can_export_pixmap_with_bpp(dri2_dpy, 16)) {
891 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
892 const __DRIconfig *config = dri2_dpy->driver_configs[j];
893 const EGLint config_attrs[] = {
894 EGL_NATIVE_VISUAL_ID, 0,
895 EGL_NATIVE_VISUAL_TYPE, EGL_NONE,
896 EGL_NONE
897 };
898 EGLint surface_type = EGL_PBUFFER_BIT;
899 unsigned int rgba_masks[4] = {
900 0x1f << 11,
901 0x3f << 5,
902 0x1f << 0,
903 0,
904 };
905
906 /* Check that we've found single-sample, no depth, no stencil,
907 * and single-buffered.
908 */
909 if (!dri2_x11_config_match_attrib(dri2_dpy, config,
910 __DRI_ATTRIB_DEPTH_SIZE, 0) ||
911 !dri2_x11_config_match_attrib(dri2_dpy, config,
912 __DRI_ATTRIB_STENCIL_SIZE, 0) ||
913 !dri2_x11_config_match_attrib(dri2_dpy, config,
914 __DRI_ATTRIB_SAMPLES, 0) ||
915 !dri2_x11_config_match_attrib(dri2_dpy, config,
916 __DRI_ATTRIB_DOUBLE_BUFFER, 0)) {
917 continue;
918 }
919
920 if (dri2_add_config(disp, config, config_count + 1, surface_type,
921 config_attrs, rgba_masks)) {
922 config_count++;
923 break;
924 }
925 }
926 }
927
928 if (!config_count) {
929 _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
930 return EGL_FALSE;
931 }
932
933 return EGL_TRUE;
934 }
935
936 static EGLBoolean
937 dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
938 _EGLSurface *draw, xcb_xfixes_region_t region)
939 {
940 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
941 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
942 enum xcb_dri2_attachment_t render_attachment;
943 xcb_dri2_copy_region_cookie_t cookie;
944
945 /* No-op for a pixmap or pbuffer surface */
946 if (draw->Type == EGL_PIXMAP_BIT || draw->Type == EGL_PBUFFER_BIT)
947 return EGL_TRUE;
948
949 dri2_dpy->flush->flush(dri2_surf->dri_drawable);
950
951 if (dri2_surf->have_fake_front)
952 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT;
953 else
954 render_attachment = XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT;
955
956 cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
957 dri2_surf->drawable,
958 region,
959 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
960 render_attachment);
961 free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
962
963 return EGL_TRUE;
964 }
965
966 static int64_t
967 dri2_x11_swap_buffers_msc(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
968 int64_t msc, int64_t divisor, int64_t remainder)
969 {
970 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
971 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
972 uint32_t msc_hi = msc >> 32;
973 uint32_t msc_lo = msc & 0xffffffff;
974 uint32_t divisor_hi = divisor >> 32;
975 uint32_t divisor_lo = divisor & 0xffffffff;
976 uint32_t remainder_hi = remainder >> 32;
977 uint32_t remainder_lo = remainder & 0xffffffff;
978 xcb_dri2_swap_buffers_cookie_t cookie;
979 xcb_dri2_swap_buffers_reply_t *reply;
980 int64_t swap_count = -1;
981
982 if (draw->SwapBehavior == EGL_BUFFER_PRESERVED || !dri2_dpy->swap_available) {
983 swap_count = dri2_copy_region(drv, disp, draw, dri2_surf->region) ? 0 : -1;
984 } else {
985 dri2_flush_drawable_for_swapbuffers(disp, draw);
986
987 cookie = xcb_dri2_swap_buffers_unchecked(dri2_dpy->conn,
988 dri2_surf->drawable, msc_hi,
989 msc_lo, divisor_hi, divisor_lo,
990 remainder_hi, remainder_lo);
991
992 reply = xcb_dri2_swap_buffers_reply(dri2_dpy->conn, cookie, NULL);
993
994 if (reply) {
995 swap_count = combine_u32_into_u64(reply->swap_hi, reply->swap_lo);
996 free(reply);
997 }
998 }
999
1000 /* Since we aren't watching for the server's invalidate events like we're
1001 * supposed to (due to XCB providing no mechanism for filtering the events
1002 * the way xlib does), and SwapBuffers is a common cause of invalidate
1003 * events, just shove one down to the driver, even though we haven't told
1004 * the driver that we're the kind of loader that provides reliable
1005 * invalidate events. This causes the driver to request buffers again at
1006 * its next draw, so that we get the correct buffers if a pageflip
1007 * happened. The driver should still be using the viewport hack to catch
1008 * window resizes.
1009 */
1010 if (dri2_dpy->flush->base.version >= 3 && dri2_dpy->flush->invalidate)
1011 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1012
1013 return swap_count;
1014 }
1015
1016 static EGLBoolean
1017 dri2_x11_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1018 {
1019 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1020 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1021
1022 if (!dri2_dpy->flush) {
1023 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1024 return EGL_TRUE;
1025 }
1026
1027 if (dri2_x11_swap_buffers_msc(drv, disp, draw, 0, 0, 0) == -1) {
1028 /* Swap failed with a window drawable. */
1029 return _eglError(EGL_BAD_NATIVE_WINDOW, __func__);
1030 }
1031 return EGL_TRUE;
1032 }
1033
1034 static EGLBoolean
1035 dri2_x11_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp,
1036 _EGLSurface *draw,
1037 EGLint numRects, const EGLint *rects)
1038 {
1039 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1040 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1041 EGLBoolean ret;
1042 xcb_xfixes_region_t region;
1043 xcb_rectangle_t rectangles[16];
1044
1045 if (numRects > (int)ARRAY_SIZE(rectangles))
1046 return dri2_copy_region(drv, disp, draw, dri2_surf->region);
1047
1048 for (int i = 0; i < numRects; i++) {
1049 rectangles[i].x = rects[i * 4];
1050 rectangles[i].y = dri2_surf->base.Height - rects[i * 4 + 1] - rects[i * 4 + 3];
1051 rectangles[i].width = rects[i * 4 + 2];
1052 rectangles[i].height = rects[i * 4 + 3];
1053 }
1054
1055 region = xcb_generate_id(dri2_dpy->conn);
1056 xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
1057 ret = dri2_copy_region(drv, disp, draw, region);
1058 xcb_xfixes_destroy_region(dri2_dpy->conn, region);
1059
1060 return ret;
1061 }
1062
1063 static EGLBoolean
1064 dri2_x11_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
1065 EGLint x, EGLint y, EGLint width, EGLint height)
1066 {
1067 const EGLint rect[4] = { x, y, width, height };
1068
1069 if (x < 0 || y < 0 || width < 0 || height < 0)
1070 _eglError(EGL_BAD_PARAMETER, "eglPostSubBufferNV");
1071
1072 return dri2_x11_swap_buffers_region(drv, disp, draw, 1, rect);
1073 }
1074
1075 static EGLBoolean
1076 dri2_x11_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1077 EGLint interval)
1078 {
1079 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1080 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1081
1082 if (dri2_dpy->swap_available)
1083 xcb_dri2_swap_interval(dri2_dpy->conn, dri2_surf->drawable, interval);
1084
1085 return EGL_TRUE;
1086 }
1087
1088 static EGLBoolean
1089 dri2_x11_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1090 void *native_pixmap_target)
1091 {
1092 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1093 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1094 xcb_gcontext_t gc;
1095 xcb_pixmap_t target;
1096
1097 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
1098 target = (uintptr_t) native_pixmap_target;
1099
1100 (void) drv;
1101
1102 dri2_dpy->flush->flush(dri2_surf->dri_drawable);
1103
1104 gc = xcb_generate_id(dri2_dpy->conn);
1105 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
1106 xcb_copy_area(dri2_dpy->conn,
1107 dri2_surf->drawable,
1108 target,
1109 gc,
1110 0, 0,
1111 0, 0,
1112 dri2_surf->base.Width,
1113 dri2_surf->base.Height);
1114 xcb_free_gc(dri2_dpy->conn, gc);
1115
1116 return EGL_TRUE;
1117 }
1118
1119 uint32_t
1120 dri2_format_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth)
1121 {
1122 switch (depth) {
1123 case 16:
1124 return __DRI_IMAGE_FORMAT_RGB565;
1125 case 24:
1126 return __DRI_IMAGE_FORMAT_XRGB8888;
1127 case 30:
1128 /* Different preferred formats for different hw */
1129 if (dri2_x11_get_red_mask_for_depth(dri2_dpy, 30) == 0x3ff)
1130 return __DRI_IMAGE_FORMAT_XBGR2101010;
1131 else
1132 return __DRI_IMAGE_FORMAT_XRGB2101010;
1133 case 32:
1134 return __DRI_IMAGE_FORMAT_ARGB8888;
1135 default:
1136 return __DRI_IMAGE_FORMAT_NONE;
1137 }
1138 }
1139
1140 static _EGLImage *
1141 dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
1142 EGLClientBuffer buffer, const EGLint *attr_list)
1143 {
1144 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1145 struct dri2_egl_image *dri2_img;
1146 unsigned int attachments[1];
1147 xcb_drawable_t drawable;
1148 xcb_dri2_get_buffers_cookie_t buffers_cookie;
1149 xcb_dri2_get_buffers_reply_t *buffers_reply;
1150 xcb_dri2_dri2_buffer_t *buffers;
1151 xcb_get_geometry_cookie_t geometry_cookie;
1152 xcb_get_geometry_reply_t *geometry_reply;
1153 xcb_generic_error_t *error;
1154 int stride, format;
1155
1156 (void) ctx;
1157
1158 drawable = (xcb_drawable_t) (uintptr_t) buffer;
1159 xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
1160 attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
1161 buffers_cookie =
1162 xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
1163 drawable, 1, 1, attachments);
1164 geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
1165 buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
1166 buffers_cookie, NULL);
1167 if (buffers_reply == NULL)
1168 return NULL;
1169
1170 buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
1171 if (buffers == NULL) {
1172 return NULL;
1173 }
1174
1175 geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
1176 geometry_cookie, &error);
1177 if (geometry_reply == NULL || error != NULL) {
1178 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
1179 free(error);
1180 free(buffers_reply);
1181 return NULL;
1182 }
1183
1184 format = dri2_format_for_depth(dri2_dpy, geometry_reply->depth);
1185 if (format == __DRI_IMAGE_FORMAT_NONE) {
1186 _eglError(EGL_BAD_PARAMETER,
1187 "dri2_create_image_khr: unsupported pixmap depth");
1188 free(buffers_reply);
1189 free(geometry_reply);
1190 return NULL;
1191 }
1192
1193 dri2_img = malloc(sizeof *dri2_img);
1194 if (!dri2_img) {
1195 free(buffers_reply);
1196 free(geometry_reply);
1197 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
1198 return EGL_NO_IMAGE_KHR;
1199 }
1200
1201 _eglInitImage(&dri2_img->base, disp);
1202
1203 stride = buffers[0].pitch / buffers[0].cpp;
1204 dri2_img->dri_image =
1205 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
1206 buffers_reply->width,
1207 buffers_reply->height,
1208 format,
1209 buffers[0].name,
1210 stride,
1211 dri2_img);
1212
1213 free(buffers_reply);
1214 free(geometry_reply);
1215
1216 return &dri2_img->base;
1217 }
1218
1219 static _EGLImage *
1220 dri2_x11_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
1221 _EGLContext *ctx, EGLenum target,
1222 EGLClientBuffer buffer, const EGLint *attr_list)
1223 {
1224 (void) drv;
1225
1226 switch (target) {
1227 case EGL_NATIVE_PIXMAP_KHR:
1228 return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
1229 default:
1230 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
1231 }
1232 }
1233
1234 static EGLBoolean
1235 dri2_x11_get_sync_values(_EGLDisplay *display, _EGLSurface *surface,
1236 EGLuint64KHR *ust, EGLuint64KHR *msc,
1237 EGLuint64KHR *sbc)
1238 {
1239 struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
1240 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1241 xcb_dri2_get_msc_cookie_t cookie;
1242 xcb_dri2_get_msc_reply_t *reply;
1243
1244 cookie = xcb_dri2_get_msc(dri2_dpy->conn, dri2_surf->drawable);
1245 reply = xcb_dri2_get_msc_reply(dri2_dpy->conn, cookie, NULL);
1246
1247 if (!reply)
1248 return _eglError(EGL_BAD_ACCESS, __func__);
1249
1250 *ust = ((EGLuint64KHR) reply->ust_hi << 32) | reply->ust_lo;
1251 *msc = ((EGLuint64KHR) reply->msc_hi << 32) | reply->msc_lo;
1252 *sbc = ((EGLuint64KHR) reply->sbc_hi << 32) | reply->sbc_lo;
1253 free(reply);
1254
1255 return EGL_TRUE;
1256 }
1257
1258 static const struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
1259 .authenticate = NULL,
1260 .create_window_surface = dri2_x11_create_window_surface,
1261 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1262 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1263 .destroy_surface = dri2_x11_destroy_surface,
1264 .create_image = dri2_create_image_khr,
1265 .swap_buffers = dri2_x11_swap_buffers,
1266 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1267 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1268 /* XXX: should really implement this since X11 has pixmaps */
1269 .copy_buffers = dri2_fallback_copy_buffers,
1270 .query_buffer_age = dri2_fallback_query_buffer_age,
1271 .query_surface = dri2_query_surface,
1272 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1273 .get_sync_values = dri2_fallback_get_sync_values,
1274 .get_dri_drawable = dri2_surface_get_dri_drawable,
1275 };
1276
1277 static const struct dri2_egl_display_vtbl dri2_x11_display_vtbl = {
1278 .authenticate = dri2_x11_authenticate,
1279 .create_window_surface = dri2_x11_create_window_surface,
1280 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
1281 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
1282 .destroy_surface = dri2_x11_destroy_surface,
1283 .create_image = dri2_x11_create_image_khr,
1284 .swap_interval = dri2_x11_swap_interval,
1285 .swap_buffers = dri2_x11_swap_buffers,
1286 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1287 .swap_buffers_region = dri2_x11_swap_buffers_region,
1288 .post_sub_buffer = dri2_x11_post_sub_buffer,
1289 .copy_buffers = dri2_x11_copy_buffers,
1290 .query_buffer_age = dri2_fallback_query_buffer_age,
1291 .query_surface = dri2_query_surface,
1292 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1293 .get_sync_values = dri2_x11_get_sync_values,
1294 .get_dri_drawable = dri2_surface_get_dri_drawable,
1295 };
1296
1297 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1298 .base = { __DRI_SWRAST_LOADER, 1 },
1299
1300 .getDrawableInfo = swrastGetDrawableInfo,
1301 .putImage = swrastPutImage,
1302 .getImage = swrastGetImage,
1303 };
1304
1305 static const __DRIextension *swrast_loader_extensions[] = {
1306 &swrast_loader_extension.base,
1307 &image_lookup_extension.base,
1308 NULL,
1309 };
1310
1311 static int
1312 dri2_find_screen_for_display(const _EGLDisplay *disp, int fallback_screen)
1313 {
1314 const EGLAttrib *attr;
1315
1316 for (attr = disp->Options.Attribs; attr; attr += 2) {
1317 if (attr[0] == EGL_PLATFORM_X11_SCREEN_EXT)
1318 return attr[1];
1319 }
1320
1321 return fallback_screen;
1322 }
1323
1324 static EGLBoolean
1325 dri2_get_xcb_connection(_EGLDriver *drv, _EGLDisplay *disp,
1326 struct dri2_egl_display *dri2_dpy)
1327 {
1328 xcb_screen_iterator_t s;
1329 int screen;
1330 const char *msg;
1331
1332 disp->DriverData = (void *) dri2_dpy;
1333 if (disp->PlatformDisplay == NULL) {
1334 dri2_dpy->conn = xcb_connect(NULL, &screen);
1335 dri2_dpy->own_device = true;
1336 screen = dri2_find_screen_for_display(disp, screen);
1337 } else {
1338 Display *dpy = disp->PlatformDisplay;
1339 dri2_dpy->conn = XGetXCBConnection(dpy);
1340 screen = DefaultScreen(dpy);
1341 }
1342
1343 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1344 msg = "xcb_connect failed";
1345 goto disconnect;
1346 }
1347
1348 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
1349 dri2_dpy->screen = get_xcb_screen(s, screen);
1350 if (!dri2_dpy->screen) {
1351 msg = "failed to get xcb screen";
1352 goto disconnect;
1353 }
1354
1355 return EGL_TRUE;
1356 disconnect:
1357 if (disp->PlatformDisplay == NULL)
1358 xcb_disconnect(dri2_dpy->conn);
1359
1360 return _eglError(EGL_BAD_ALLOC, msg);
1361 }
1362
1363 static EGLBoolean
1364 dri2_initialize_x11_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1365 {
1366 _EGLDevice *dev;
1367 struct dri2_egl_display *dri2_dpy;
1368
1369 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1370 if (!dri2_dpy)
1371 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1372
1373 dri2_dpy->fd = -1;
1374 if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
1375 goto cleanup;
1376
1377 dev = _eglAddDevice(dri2_dpy->fd, true);
1378 if (!dev) {
1379 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1380 goto cleanup;
1381 }
1382
1383 disp->Device = dev;
1384
1385 /*
1386 * Every hardware driver_name is set using strdup. Doing the same in
1387 * here will allow is to simply free the memory at dri2_terminate().
1388 */
1389 dri2_dpy->driver_name = strdup("swrast");
1390 if (!dri2_load_driver_swrast(disp))
1391 goto cleanup;
1392
1393 dri2_dpy->loader_extensions = swrast_loader_extensions;
1394
1395 if (!dri2_create_screen(disp))
1396 goto cleanup;
1397
1398 if (!dri2_setup_extensions(disp))
1399 goto cleanup;
1400
1401 dri2_setup_screen(disp);
1402
1403 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true, false))
1404 goto cleanup;
1405
1406 /* Fill vtbl last to prevent accidentally calling virtual function during
1407 * initialization.
1408 */
1409 dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1410
1411 return EGL_TRUE;
1412
1413 cleanup:
1414 dri2_display_destroy(disp);
1415 return EGL_FALSE;
1416 }
1417
1418 static void
1419 dri2_x11_setup_swap_interval(_EGLDisplay *disp)
1420 {
1421 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1422 int arbitrary_max_interval = 1000;
1423
1424 /* default behavior for no SwapBuffers support: no vblank syncing
1425 * either.
1426 */
1427 dri2_dpy->min_swap_interval = 0;
1428 dri2_dpy->max_swap_interval = 0;
1429 dri2_dpy->default_swap_interval = 0;
1430
1431 if (!dri2_dpy->swap_available)
1432 return;
1433
1434 /* If we do have swapbuffers, then we can support pretty much any swap
1435 * interval.
1436 */
1437 dri2_setup_swap_interval(disp, arbitrary_max_interval);
1438 }
1439
1440 #ifdef HAVE_DRI3
1441
1442 static const __DRIextension *dri3_image_loader_extensions[] = {
1443 &dri3_image_loader_extension.base,
1444 &image_lookup_extension.base,
1445 &use_invalidate.base,
1446 &background_callable_extension.base,
1447 NULL,
1448 };
1449
1450 static EGLBoolean
1451 dri2_initialize_x11_dri3(_EGLDriver *drv, _EGLDisplay *disp)
1452 {
1453 _EGLDevice *dev;
1454 struct dri2_egl_display *dri2_dpy;
1455
1456 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1457 if (!dri2_dpy)
1458 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1459
1460 dri2_dpy->fd = -1;
1461 if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
1462 goto cleanup;
1463
1464 if (!dri3_x11_connect(dri2_dpy))
1465 goto cleanup;
1466
1467 dev = _eglAddDevice(dri2_dpy->fd, false);
1468 if (!dev) {
1469 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1470 goto cleanup;
1471 }
1472
1473 disp->Device = dev;
1474
1475 if (!dri2_load_driver_dri3(disp))
1476 goto cleanup;
1477
1478 dri2_dpy->loader_extensions = dri3_image_loader_extensions;
1479
1480 dri2_dpy->swap_available = true;
1481 dri2_dpy->invalidate_available = true;
1482
1483 if (!dri2_create_screen(disp))
1484 goto cleanup;
1485
1486 if (!dri2_setup_extensions(disp))
1487 goto cleanup;
1488
1489 dri2_setup_screen(disp);
1490
1491 dri2_x11_setup_swap_interval(disp);
1492
1493 if (!dri2_dpy->is_different_gpu)
1494 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1495 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1496 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1497 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1498
1499 dri2_set_WL_bind_wayland_display(drv, disp);
1500
1501 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, false, true))
1502 goto cleanup;
1503
1504 dri2_dpy->loader_dri3_ext.core = dri2_dpy->core;
1505 dri2_dpy->loader_dri3_ext.image_driver = dri2_dpy->image_driver;
1506 dri2_dpy->loader_dri3_ext.flush = dri2_dpy->flush;
1507 dri2_dpy->loader_dri3_ext.tex_buffer = dri2_dpy->tex_buffer;
1508 dri2_dpy->loader_dri3_ext.image = dri2_dpy->image;
1509 dri2_dpy->loader_dri3_ext.config = dri2_dpy->config;
1510
1511 /* Fill vtbl last to prevent accidentally calling virtual function during
1512 * initialization.
1513 */
1514 dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1515
1516 _eglLog(_EGL_INFO, "Using DRI3");
1517
1518 return EGL_TRUE;
1519
1520 cleanup:
1521 dri2_display_destroy(disp);
1522 return EGL_FALSE;
1523 }
1524 #endif
1525
1526 static const __DRIdri2LoaderExtension dri2_loader_extension_old = {
1527 .base = { __DRI_DRI2_LOADER, 2 },
1528
1529 .getBuffers = dri2_x11_get_buffers,
1530 .flushFrontBuffer = dri2_x11_flush_front_buffer,
1531 .getBuffersWithFormat = NULL,
1532 };
1533
1534 static const __DRIdri2LoaderExtension dri2_loader_extension = {
1535 .base = { __DRI_DRI2_LOADER, 3 },
1536
1537 .getBuffers = dri2_x11_get_buffers,
1538 .flushFrontBuffer = dri2_x11_flush_front_buffer,
1539 .getBuffersWithFormat = dri2_x11_get_buffers_with_format,
1540 };
1541
1542 static const __DRIextension *dri2_loader_extensions_old[] = {
1543 &dri2_loader_extension_old.base,
1544 &image_lookup_extension.base,
1545 &background_callable_extension.base,
1546 NULL,
1547 };
1548
1549 static const __DRIextension *dri2_loader_extensions[] = {
1550 &dri2_loader_extension.base,
1551 &image_lookup_extension.base,
1552 &use_invalidate.base,
1553 &background_callable_extension.base,
1554 NULL,
1555 };
1556
1557 static EGLBoolean
1558 dri2_initialize_x11_dri2(_EGLDriver *drv, _EGLDisplay *disp)
1559 {
1560 _EGLDevice *dev;
1561 struct dri2_egl_display *dri2_dpy;
1562
1563 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1564 if (!dri2_dpy)
1565 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1566
1567 dri2_dpy->fd = -1;
1568 if (!dri2_get_xcb_connection(drv, disp, dri2_dpy))
1569 goto cleanup;
1570
1571 if (!dri2_x11_connect(dri2_dpy))
1572 goto cleanup;
1573
1574 dev = _eglAddDevice(dri2_dpy->fd, false);
1575 if (!dev) {
1576 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1577 goto cleanup;
1578 }
1579
1580 disp->Device = dev;
1581
1582 if (!dri2_load_driver(disp))
1583 goto cleanup;
1584
1585 if (dri2_dpy->dri2_minor >= 1)
1586 dri2_dpy->loader_extensions = dri2_loader_extensions;
1587 else
1588 dri2_dpy->loader_extensions = dri2_loader_extensions_old;
1589
1590 dri2_dpy->swap_available = (dri2_dpy->dri2_minor >= 2);
1591 dri2_dpy->invalidate_available = (dri2_dpy->dri2_minor >= 3);
1592
1593 if (!dri2_create_screen(disp))
1594 goto cleanup;
1595
1596 if (!dri2_setup_extensions(disp))
1597 goto cleanup;
1598
1599 dri2_setup_screen(disp);
1600
1601 dri2_x11_setup_swap_interval(disp);
1602
1603 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1604 disp->Extensions.NOK_swap_region = EGL_TRUE;
1605 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1606 disp->Extensions.NV_post_sub_buffer = EGL_TRUE;
1607 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1608
1609 dri2_set_WL_bind_wayland_display(drv, disp);
1610
1611 if (!dri2_x11_add_configs_for_visuals(dri2_dpy, disp, true, false))
1612 goto cleanup;
1613
1614 /* Fill vtbl last to prevent accidentally calling virtual function during
1615 * initialization.
1616 */
1617 dri2_dpy->vtbl = &dri2_x11_display_vtbl;
1618
1619 _eglLog(_EGL_INFO, "Using DRI2");
1620
1621 return EGL_TRUE;
1622
1623 cleanup:
1624 dri2_display_destroy(disp);
1625 return EGL_FALSE;
1626 }
1627
1628 EGLBoolean
1629 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
1630 {
1631 EGLBoolean initialized = EGL_FALSE;
1632
1633 if (!disp->Options.ForceSoftware) {
1634 #ifdef HAVE_DRI3
1635 if (!env_var_as_boolean("LIBGL_DRI3_DISABLE", false))
1636 initialized = dri2_initialize_x11_dri3(drv, disp);
1637 #endif
1638
1639 if (!initialized)
1640 initialized = dri2_initialize_x11_dri2(drv, disp);
1641 }
1642
1643 if (!initialized)
1644 initialized = dri2_initialize_x11_swrast(drv, disp);
1645
1646 return initialized;
1647 }
1648
1649 void
1650 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy)
1651 {
1652 if (dri2_dpy->own_device)
1653 xcb_disconnect(dri2_dpy->conn);
1654 }