Debian Codegraph @

src/egl/drivers/dri2/platform_x11.c

raw
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 <[email protected]>
26 */
27
28 #include <dlfcn.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 /* clang-format off */
39 #include <xcb/xcb.h>
40 #include <xcb/shm.h>
41 #include <vulkan/vulkan_core.h>
42 #include <vulkan/vulkan_xcb.h>
43 /* clang-format on */
44 #ifdef HAVE_LIBDRM
45 #include "util/libdrm.h"
46 #include "platform_x11_dri3.h"
47 #endif
48 #include "util/bitscan.h"
49 #include "util/macros.h"
50 #include "util/u_debug.h"
51 #include "util/log.h"
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <sys/ipc.h>
55 #include "x11_dri3.h"
56 #include "x11_display.h"
57 #include "kopper_interface.h"
58 #include "loader.h"
59 #include "platform_x11.h"
60 #include "drm-uapi/drm_fourcc.h"
61 #include "dri_util.h"
62
63 static void
64 swrastCreateDrawable(struct dri2_egl_display *dri2_dpy,
65 struct dri2_egl_surface *dri2_surf)
66 {
67 uint32_t mask;
68 const uint32_t function = GXcopy;
69 uint32_t valgc[2];
70
71 /* create GC's */
72 dri2_surf->gc = xcb_generate_id(dri2_dpy->conn);
73 mask = XCB_GC_FUNCTION;
74 xcb_create_gc(dri2_dpy->conn, dri2_surf->gc, dri2_surf->drawable, mask,
75 &function);
76
77 dri2_surf->swapgc = xcb_generate_id(dri2_dpy->conn);
78 mask = XCB_GC_FUNCTION | XCB_GC_GRAPHICS_EXPOSURES;
79 valgc[0] = function;
80 valgc[1] = False;
81 xcb_create_gc(dri2_dpy->conn, dri2_surf->swapgc, dri2_surf->drawable, mask,
82 valgc);
83 switch (dri2_surf->depth) {
84 case 32:
85 case 30:
86 case 24:
87 dri2_surf->bytes_per_pixel = 4;
88 break;
89 case 16:
90 dri2_surf->bytes_per_pixel = 2;
91 break;
92 case 8:
93 dri2_surf->bytes_per_pixel = 1;
94 break;
95 case 0:
96 dri2_surf->bytes_per_pixel = 0;
97 break;
98 default:
99 _eglLog(_EGL_WARNING, "unsupported depth %d", dri2_surf->depth);
100 }
101 }
102
103 static void
104 swrastDestroyDrawable(struct dri2_egl_display *dri2_dpy,
105 struct dri2_egl_surface *dri2_surf)
106 {
107 xcb_free_gc(dri2_dpy->conn, dri2_surf->gc);
108 xcb_free_gc(dri2_dpy->conn, dri2_surf->swapgc);
109 }
110
111 static bool
112 x11_get_drawable_info(struct dri_drawable *draw, int *x, int *y, int *w, int *h,
113 void *loaderPrivate)
114 {
115 struct dri2_egl_surface *dri2_surf = loaderPrivate;
116 struct dri2_egl_display *dri2_dpy =
117 dri2_egl_display(dri2_surf->base.Resource.Display);
118
119 xcb_get_geometry_cookie_t cookie;
120 xcb_get_geometry_reply_t *reply;
121 xcb_generic_error_t *error;
122 bool ret;
123
124 cookie = xcb_get_geometry(dri2_dpy->conn, dri2_surf->drawable);
125 reply = xcb_get_geometry_reply(dri2_dpy->conn, cookie, &error);
126 if (reply == NULL)
127 return false;
128
129 if (error != NULL) {
130 ret = false;
131 _eglLog(_EGL_WARNING, "error in xcb_get_geometry");
132 free(error);
133 } else {
134 *x = reply->x;
135 *y = reply->y;
136 *w = reply->width;
137 *h = reply->height;
138 ret = true;
139 }
140 free(reply);
141 return ret;
142 }
143
144 static void
145 swrastGetDrawableInfo(struct dri_drawable *draw, int *x, int *y, int *w, int *h,
146 void *loaderPrivate)
147 {
148 *x = *y = *w = *h = 0;
149 x11_get_drawable_info(draw, x, y, w, h, loaderPrivate);
150 }
151
152 static void
153 swrastPutImage2(struct dri_drawable *draw, int op, int x, int y, int w, int h,
154 int stride, char *data, void *loaderPrivate)
155 {
156 struct dri2_egl_surface *dri2_surf = loaderPrivate;
157 struct dri2_egl_display *dri2_dpy =
158 dri2_egl_display(dri2_surf->base.Resource.Display);
159 int stride_b = dri2_surf->bytes_per_pixel * w;
160 size_t hdr_len = sizeof(xcb_put_image_request_t);
161 size_t size = (hdr_len + stride_b * h) >> 2;
162 uint64_t max_req_len = xcb_get_maximum_request_length(dri2_dpy->conn);
163
164 xcb_gcontext_t gc;
165 xcb_void_cookie_t cookie;
166 switch (op) {
167 case __DRI_SWRAST_IMAGE_OP_DRAW:
168 gc = dri2_surf->gc;
169 break;
170 case __DRI_SWRAST_IMAGE_OP_SWAP:
171 gc = dri2_surf->swapgc;
172 break;
173 default:
174 return;
175 }
176
177 /* clamp to drawable size */
178 if (y + h > dri2_surf->base.Height)
179 h = dri2_surf->base.Height - y;
180
181 /* If stride of pixels to copy is different from the surface stride
182 * then we need to copy lines one by one.
183 */
184 if (stride_b != stride) {
185 for (unsigned i = 0; i < h; i++) {
186 cookie = xcb_put_image(
187 dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable, gc, w,
188 1, x, y+i, 0, dri2_surf->depth, stride_b, (uint8_t*)data);
189 xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
190
191 data += stride;
192 }
193 } else if (size < max_req_len) {
194 cookie = xcb_put_image(
195 dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP, dri2_surf->drawable, gc, w,
196 h, x, y, 0, dri2_surf->depth, h * stride_b, (const uint8_t *)data);
197 xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
198 } else {
199 int num_lines = ((max_req_len << 2) - hdr_len) / stride_b;
200 int y_start = 0;
201 int y_todo = h;
202 while (y_todo) {
203 int this_lines = MIN2(num_lines, y_todo);
204 cookie =
205 xcb_put_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
206 dri2_surf->drawable, gc, w, this_lines, x, y_start, 0,
207 dri2_surf->depth, this_lines * stride_b,
208 ((const uint8_t *)data + y_start * stride_b));
209 xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
210 y_start += this_lines;
211 y_todo -= this_lines;
212 }
213 }
214 xcb_flush(dri2_dpy->conn);
215 }
216
217 static void
218 swrastPutImage(struct dri_drawable *draw, int op, int x, int y, int w, int h,
219 char *data, void *loaderPrivate)
220 {
221 struct dri2_egl_surface *dri2_surf = loaderPrivate;
222 int stride_b = dri2_surf->bytes_per_pixel * w;
223 swrastPutImage2(draw, op, x, y, w, h, stride_b, data, loaderPrivate);
224 }
225
226 static void
227 swrastGetImage2(struct dri_drawable * read,
228 int x, int y, int w, int h, int stride,
229 char *data, void *loaderPrivate)
230 {
231 struct dri2_egl_surface *dri2_surf = loaderPrivate;
232 struct dri2_egl_display *dri2_dpy =
233 dri2_egl_display(dri2_surf->base.Resource.Display);
234
235 xcb_get_image_cookie_t cookie;
236 xcb_get_image_reply_t *reply;
237 xcb_generic_error_t *error;
238
239 cookie = xcb_get_image(dri2_dpy->conn, XCB_IMAGE_FORMAT_Z_PIXMAP,
240 dri2_surf->drawable, x, y, w, h, ~0);
241 reply = xcb_get_image_reply(dri2_dpy->conn, cookie, &error);
242 if (reply == NULL)
243 return;
244
245 if (error != NULL) {
246 _eglLog(_EGL_WARNING, "error in xcb_get_image");
247 free(error);
248 } else {
249 uint32_t bytes = xcb_get_image_data_length(reply);
250 uint8_t *idata = xcb_get_image_data(reply);
251 int stride_b = w * dri2_surf->bytes_per_pixel;
252 /* Only copy line by line if we have a different stride */
253 if (stride != stride_b) {
254 for (int i = 0; i < h; i++) {
255 memcpy(data, idata, stride_b);
256 data += stride;
257 idata += stride_b;
258 }
259 } else {
260 memcpy(data, idata, bytes);
261 }
262 }
263 free(reply);
264 }
265
266 static void
267 swrastGetImage(struct dri_drawable *read, int x, int y, int w, int h, char *data,
268 void *loaderPrivate)
269 {
270 struct dri2_egl_surface *dri2_surf = loaderPrivate;
271 int stride_b = w * dri2_surf->bytes_per_pixel;
272 swrastGetImage2(read, x, y, w, h, stride_b, data, loaderPrivate);
273 }
274
275 static void
276 swrastPutImageShm(struct dri_drawable * draw, int op,
277 int x, int y, int w, int h, int stride,
278 int shmid, char *shmaddr, unsigned offset,
279 void *loaderPrivate)
280 {
281 struct dri2_egl_surface *dri2_surf = loaderPrivate;
282 struct dri2_egl_display *dri2_dpy =
283 dri2_egl_display(dri2_surf->base.Resource.Display);
284 xcb_generic_error_t *error = NULL;
285
286 xcb_shm_seg_t shm_seg = xcb_generate_id(dri2_dpy->conn);
287 error = xcb_request_check(dri2_dpy->conn,
288 xcb_shm_attach_checked(dri2_dpy->conn,
289 shm_seg, shmid, 0));
290 if (error) {
291 mesa_loge("Failed to attach to x11 shm");
292 _eglError(EGL_BAD_SURFACE, "xcb_shm_attach_checked");
293 free(error);
294 return;
295 }
296
297 xcb_gcontext_t gc;
298 xcb_void_cookie_t cookie;
299 switch (op) {
300 case __DRI_SWRAST_IMAGE_OP_DRAW:
301 gc = dri2_surf->gc;
302 break;
303 case __DRI_SWRAST_IMAGE_OP_SWAP:
304 gc = dri2_surf->swapgc;
305 break;
306 default:
307 return;
308 }
309
310 cookie = xcb_shm_put_image(dri2_dpy->conn,
311 dri2_surf->drawable,
312 gc,
313 stride / dri2_surf->bytes_per_pixel, h,
314 x, 0,
315 w, h,
316 x, y,
317 dri2_surf->depth,
318 XCB_IMAGE_FORMAT_Z_PIXMAP,
319 0, shm_seg, stride * y);
320 xcb_discard_reply(dri2_dpy->conn, cookie.sequence);
321
322 xcb_flush(dri2_dpy->conn);
323 xcb_shm_detach(dri2_dpy->conn, shm_seg);
324 }
325
326 static void
327 swrastGetImageShm(struct dri_drawable * read,
328 int x, int y, int w, int h,
329 int shmid, void *loaderPrivate)
330 {
331 struct dri2_egl_surface *dri2_surf = loaderPrivate;
332 struct dri2_egl_display *dri2_dpy =
333 dri2_egl_display(dri2_surf->base.Resource.Display);
334 xcb_generic_error_t *error = NULL;
335
336 xcb_shm_seg_t shm_seg = xcb_generate_id(dri2_dpy->conn);
337 error = xcb_request_check(dri2_dpy->conn,
338 xcb_shm_attach_checked(dri2_dpy->conn,
339 shm_seg, shmid, 0));
340 if (error) {
341 mesa_loge("Failed to attach to x11 shm");
342 _eglError(EGL_BAD_SURFACE, "xcb_shm_attach_checked");
343 free(error);
344 return;
345 }
346
347 xcb_shm_get_image_cookie_t cookie;
348 xcb_shm_get_image_reply_t *reply;
349
350 cookie = xcb_shm_get_image(dri2_dpy->conn,
351 dri2_surf->drawable,
352 x, y,
353 w, h,
354 ~0, XCB_IMAGE_FORMAT_Z_PIXMAP,
355 shm_seg, 0);
356 reply = xcb_shm_get_image_reply(dri2_dpy->conn, cookie, NULL);
357 if (reply == NULL)
358 _eglLog(_EGL_WARNING, "error in xcb_shm_get_image");
359 else
360 free(reply);
361
362 xcb_shm_detach(dri2_dpy->conn, shm_seg);
363 }
364
365 static xcb_screen_t *
366 get_xcb_screen(xcb_screen_iterator_t iter, int screen)
367 {
368 for (; iter.rem; --screen, xcb_screen_next(&iter))
369 if (screen == 0)
370 return iter.data;
371
372 return NULL;
373 }
374
375 static xcb_visualtype_t *
376 get_xcb_visualtype_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
377 {
378 xcb_visualtype_iterator_t visual_iter;
379 xcb_screen_t *screen = dri2_dpy->screen;
380 xcb_depth_iterator_t depth_iter = xcb_screen_allowed_depths_iterator(screen);
381
382 for (; depth_iter.rem; xcb_depth_next(&depth_iter)) {
383 if (depth_iter.data->depth != depth)
384 continue;
385
386 visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
387 if (visual_iter.rem)
388 return visual_iter.data;
389 }
390
391 return NULL;
392 }
393
394 /* Get red channel mask for given depth. */
395 unsigned int
396 dri2_x11_get_red_mask_for_depth(struct dri2_egl_display *dri2_dpy, int depth)
397 {
398 xcb_visualtype_t *visual = get_xcb_visualtype_for_depth(dri2_dpy, depth);
399
400 if (visual)
401 return visual->red_mask;
402
403 return 0;
404 }
405
406 /**
407 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
408 */
409 static _EGLSurface *
410 dri2_x11_create_surface(_EGLDisplay *disp, EGLint type, _EGLConfig *conf,
411 void *native_surface, const EGLint *attrib_list)
412 {
413 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
414 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
415 struct dri2_egl_surface *dri2_surf;
416 xcb_get_geometry_cookie_t cookie;
417 xcb_get_geometry_reply_t *reply;
418 xcb_generic_error_t *error;
419 const struct dri_config *config;
420
421 dri2_surf = calloc(1, sizeof *dri2_surf);
422 if (!dri2_surf) {
423 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
424 return NULL;
425 }
426
427 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
428 false, native_surface))
429 goto cleanup_surf;
430
431 dri2_surf->region = XCB_NONE;
432 if (type == EGL_PBUFFER_BIT) {
433 dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
434 xcb_create_pixmap(dri2_dpy->conn, conf->BufferSize, dri2_surf->drawable,
435 dri2_dpy->screen->root, dri2_surf->base.Width,
436 dri2_surf->base.Height);
437 } else {
438 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface));
439 dri2_surf->drawable = (uintptr_t)native_surface;
440 }
441
442 config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
443
444 if (!config) {
445 _eglError(EGL_BAD_MATCH,
446 "Unsupported surfacetype/colorspace configuration");
447 goto cleanup_pixmap;
448 }
449
450 if (type != EGL_PBUFFER_BIT) {
451 cookie = xcb_get_geometry(dri2_dpy->conn, dri2_surf->drawable);
452 reply = xcb_get_geometry_reply(dri2_dpy->conn, cookie, &error);
453 if (error != NULL) {
454 if (error->error_code == BadAlloc)
455 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
456 else if (type == EGL_WINDOW_BIT)
457 _eglError(EGL_BAD_NATIVE_WINDOW, "xcb_get_geometry");
458 else
459 _eglError(EGL_BAD_NATIVE_PIXMAP, "xcb_get_geometry");
460 free(error);
461 free(reply);
462 goto cleanup_dri_drawable;
463 } else if (reply == NULL) {
464 _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
465 goto cleanup_dri_drawable;
466 }
467
468 dri2_surf->base.Width = reply->width;
469 dri2_surf->base.Height = reply->height;
470 dri2_surf->depth = reply->depth;
471 free(reply);
472 }
473
474 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
475 goto cleanup_pixmap;
476
477 if (type == EGL_PBUFFER_BIT) {
478 dri2_surf->depth = conf->BufferSize;
479 }
480 swrastCreateDrawable(dri2_dpy, dri2_surf);
481
482 return &dri2_surf->base;
483
484 cleanup_dri_drawable:
485 driDestroyDrawable(dri2_surf->dri_drawable);
486 cleanup_pixmap:
487 if (type == EGL_PBUFFER_BIT)
488 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
489 cleanup_surf:
490 free(dri2_surf);
491
492 return NULL;
493 }
494
495 /**
496 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
497 */
498 static _EGLSurface *
499 dri2_x11_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
500 void *native_window, const EGLint *attrib_list)
501 {
502 _EGLSurface *surf;
503
504 surf = dri2_x11_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
505 attrib_list);
506 if (surf != NULL) {
507 /* When we first create the DRI2 drawable, its swap interval on the
508 * server side is 1.
509 */
510 surf->SwapInterval = 1;
511 }
512
513 return surf;
514 }
515
516 static _EGLSurface *
517 dri2_x11_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
518 void *native_pixmap, const EGLint *attrib_list)
519 {
520 return dri2_x11_create_surface(disp, EGL_PIXMAP_BIT, conf, native_pixmap,
521 attrib_list);
522 }
523
524 static _EGLSurface *
525 dri2_x11_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
526 const EGLint *attrib_list)
527 {
528 return dri2_x11_create_surface(disp, EGL_PBUFFER_BIT, conf, NULL,
529 attrib_list);
530 }
531
532 static EGLBoolean
533 dri2_x11_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
534 {
535 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
536 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
537
538 driDestroyDrawable(dri2_surf->dri_drawable);
539
540 if (dri2_dpy->swrast)
541 swrastDestroyDrawable(dri2_dpy, dri2_surf);
542
543 if (surf->Type == EGL_PBUFFER_BIT)
544 xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
545
546 dri2_fini_surface(surf);
547 free(surf);
548
549 return EGL_TRUE;
550 }
551
552 /**
553 * Function utilizes swrastGetDrawableInfo to get surface
554 * geometry from x server and calls default query surface
555 * implementation that returns the updated values.
556 *
557 * In case of errors we still return values that we currently
558 * have.
559 */
560 static EGLBoolean
561 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute,
562 EGLint *value)
563 {
564 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
565 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
566 int x, y, w, h;
567
568 struct dri_drawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
569
570 switch (attribute) {
571 case EGL_WIDTH:
572 case EGL_HEIGHT:
573 if (x11_get_drawable_info(drawable, &x, &y, &w, &h, dri2_surf)) {
574 bool changed = surf->Width != w || surf->Height != h;
575 surf->Width = w;
576 surf->Height = h;
577 if (changed && !dri2_dpy->swrast_not_kms)
578 dri_invalidate_drawable(drawable);
579 }
580 break;
581 default:
582 break;
583 }
584 return _eglQuerySurface(disp, surf, attribute, value);
585 }
586
587 static void
588 dri2_x11_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
589 _EGLDisplay *disp, bool supports_preserved)
590 {
591 xcb_depth_iterator_t d;
592 xcb_visualtype_t *visuals;
593 EGLint surface_type;
594
595 d = xcb_screen_allowed_depths_iterator(dri2_dpy->screen);
596
597 surface_type = EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT;
598
599 if (supports_preserved)
600 surface_type |= EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
601
602 while (d.rem > 0) {
603 EGLBoolean class_added[6] = {
604 0,
605 };
606
607 visuals = xcb_depth_visuals(d.data);
608
609 for (int i = 0; i < xcb_depth_visuals_length(d.data); i++) {
610 if (class_added[visuals[i]._class])
611 continue;
612
613 class_added[visuals[i]._class] = EGL_TRUE;
614
615 const int rgb_shifts[3] = {
616 ffs(visuals[i].red_mask) - 1,
617 ffs(visuals[i].green_mask) - 1,
618 ffs(visuals[i].blue_mask) - 1,
619 };
620
621 const unsigned int rgb_sizes[3] = {
622 util_bitcount(visuals[i].red_mask),
623 util_bitcount(visuals[i].green_mask),
624 util_bitcount(visuals[i].blue_mask),
625 };
626
627 const EGLint config_attrs[] = {
628 EGL_NATIVE_VISUAL_ID,
629 visuals[i].visual_id,
630 EGL_NATIVE_VISUAL_TYPE,
631 visuals[i]._class,
632 EGL_NONE,
633 };
634
635 const EGLint config_attrs_2nd_group[] = {
636 EGL_NATIVE_VISUAL_ID,
637 visuals[i].visual_id,
638 EGL_NATIVE_VISUAL_TYPE,
639 visuals[i]._class,
640 EGL_CONFIG_SELECT_GROUP_EXT,
641 1,
642 EGL_NONE,
643 };
644
645 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
646 const struct dri_config *config = dri2_dpy->driver_configs[j];
647 int shifts[4];
648 unsigned int sizes[4];
649
650 dri2_get_shifts_and_sizes(config, shifts, sizes);
651
652 if (memcmp(shifts, rgb_shifts, sizeof(rgb_shifts)) != 0 ||
653 memcmp(sizes, rgb_sizes, sizeof(rgb_sizes)) != 0) {
654 continue;
655 }
656
657 /* Allows RGB visuals to match a 32-bit RGBA EGLConfig.
658 * Otherwise it will only match a 32-bit RGBA visual. On a
659 * composited window manager on X11, this will make all of the
660 * EGLConfigs with destination alpha get blended by the
661 * compositor. This is probably not what the application
662 * wants... especially on drivers that only have 32-bit RGBA
663 * EGLConfigs! */
664 if (sizes[3] != 0) {
665 unsigned int rgba_mask =
666 ~(visuals[i].red_mask | visuals[i].green_mask |
667 visuals[i].blue_mask);
668
669 if (shifts[3] != ffs(rgba_mask) - 1 ||
670 sizes[3] != util_bitcount(rgba_mask))
671 continue;
672 }
673
674 unsigned int bit_per_pixel = sizes[0] + sizes[1] + sizes[2] + sizes[3];
675 if (sizes[3] != 0 && d.data->depth == bit_per_pixel) {
676 dri2_add_config(disp, config, surface_type, config_attrs_2nd_group);
677 } else {
678 dri2_add_config(disp, config, surface_type, config_attrs);
679 }
680 }
681 }
682
683 xcb_depth_next(&d);
684 }
685 }
686
687 static EGLBoolean
688 dri2_x11_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
689 {
690 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
691 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
692
693 if (dri2_dpy->swrast) {
694 /* aka the swrast path, which does the swap in the gallium driver. */
695 driSwapBuffers(dri2_surf->dri_drawable);
696 return EGL_TRUE;
697 }
698
699 return EGL_TRUE;
700 }
701
702 static EGLBoolean
703 dri2_x11_kopper_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
704 const EGLint *rects, EGLint numRects)
705 {
706 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
707
708 /* swrast path unsupported for now */
709 if (numRects)
710 kopperSwapBuffersWithDamage(dri2_surf->dri_drawable, __DRI2_FLUSH_CONTEXT | __DRI2_FLUSH_INVALIDATE_ANCILLARY, numRects, rects);
711 else
712 kopperSwapBuffers(dri2_surf->dri_drawable, __DRI2_FLUSH_CONTEXT | __DRI2_FLUSH_INVALIDATE_ANCILLARY);
713
714 /* If the X11 window has been resized, vkQueuePresentKHR() or
715 * vkAcquireNextImageKHR() may return VK_ERROR_SURFACE_LOST or
716 * VK_SUBOPTIMAL_KHR, causing kopper to re-create the swapchain with
717 * a different size. We need to resize the EGLSurface in that case.
718 */
719 kopperQuerySurfaceSize(dri2_surf->dri_drawable,
720 &dri2_surf->base.Width,
721 &dri2_surf->base.Height);
722 return EGL_TRUE;
723 }
724
725 static EGLBoolean
726 dri2_x11_kopper_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
727 {
728 return dri2_x11_kopper_swap_buffers_with_damage(disp, draw, NULL, 0);
729 }
730
731 static EGLBoolean
732 dri2_x11_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
733 const EGLint *rects, EGLint numRects)
734 {
735 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
736 if (numRects)
737 driSwapBuffersWithDamage(dri2_surf->dri_drawable, numRects, rects);
738 else
739 driSwapBuffers(dri2_surf->dri_drawable);
740 return EGL_TRUE;
741 }
742
743 static EGLBoolean
744 dri2_x11_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf,
745 void *native_pixmap_target)
746 {
747 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
748 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
749 xcb_gcontext_t gc;
750 xcb_pixmap_t target;
751
752 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target));
753 target = (uintptr_t)native_pixmap_target;
754
755 if (!dri2_dpy->swrast_not_kms)
756 dri_flush_drawable(dri2_surf->dri_drawable);
757 else {
758 /* This should not be a swapBuffers, because it could present an
759 * incomplete frame, and it could invalidate the back buffer if it's not
760 * preserved. We really do want to flush. But it ends up working out
761 * okay-ish on swrast because those aren't invalidating the back buffer on
762 * swap.
763 */
764 driSwapBuffers(dri2_surf->dri_drawable);
765 }
766
767 gc = xcb_generate_id(dri2_dpy->conn);
768 xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
769 xcb_copy_area(dri2_dpy->conn, dri2_surf->drawable, target, gc, 0, 0, 0, 0,
770 dri2_surf->base.Width, dri2_surf->base.Height);
771 xcb_free_gc(dri2_dpy->conn, gc);
772
773 return EGL_TRUE;
774 }
775
776 uint32_t
777 dri2_fourcc_for_depth(struct dri2_egl_display *dri2_dpy, uint32_t depth)
778 {
779 switch (depth) {
780 case 16:
781 return DRM_FORMAT_RGB565;
782 case 24:
783 return DRM_FORMAT_XRGB8888;
784 case 30:
785 /* Different preferred formats for different hw */
786 if (dri2_x11_get_red_mask_for_depth(dri2_dpy, 30) == 0x3ff)
787 return DRM_FORMAT_XBGR2101010;
788 else
789 return DRM_FORMAT_XRGB2101010;
790 case 32:
791 return DRM_FORMAT_ARGB8888;
792 default:
793 return DRM_FORMAT_INVALID;
794 }
795 }
796
797 static int
798 box_intersection_area(int16_t a_x, int16_t a_y, int16_t a_width,
799 int16_t a_height, int16_t b_x, int16_t b_y,
800 int16_t b_width, int16_t b_height)
801 {
802 int w = MIN2(a_x + a_width, b_x + b_width) - MAX2(a_x, b_x);
803 int h = MIN2(a_y + a_height, b_y + b_height) - MAX2(a_y, b_y);
804
805 return (w < 0 || h < 0) ? 0 : w * h;
806 }
807
808 EGLBoolean
809 dri2_x11_get_msc_rate(_EGLDisplay *display, _EGLSurface *surface,
810 EGLint *numerator, EGLint *denominator)
811 {
812 struct dri2_egl_display *dri2_dpy = dri2_egl_display(display);
813
814 #ifdef HAVE_LIBDRM
815 loader_update_screen_resources(&dri2_dpy->screen_resources);
816
817 if (dri2_dpy->screen_resources.num_crtcs == 0) {
818 /* If there's no CRTC active, use the present fake vblank of 1Hz */
819 *numerator = 1;
820 *denominator = 1;
821 return EGL_TRUE;
822 }
823
824 /* Default to the first CRTC in the list */
825 *numerator = dri2_dpy->screen_resources.crtcs[0].refresh_numerator;
826 *denominator = dri2_dpy->screen_resources.crtcs[0].refresh_denominator;
827
828 /* If there's only one active CRTC, we're done */
829 if (dri2_dpy->screen_resources.num_crtcs == 1)
830 return EGL_TRUE;
831 #else
832 *numerator = 0;
833 *denominator = 1;
834 #endif
835
836
837 /* In a multi-monitor setup, look at each CRTC and perform a box
838 * intersection between the CRTC and surface. Use the CRTC whose
839 * box intersection has the largest area.
840 */
841 if (surface->Type != EGL_WINDOW_BIT)
842 return EGL_TRUE;
843
844 xcb_window_t window = (uintptr_t)surface->NativeSurface;
845
846 xcb_translate_coordinates_cookie_t cookie =
847 xcb_translate_coordinates_unchecked(dri2_dpy->conn, window,
848 dri2_dpy->screen->root, 0, 0);
849 xcb_translate_coordinates_reply_t *reply =
850 xcb_translate_coordinates_reply(dri2_dpy->conn, cookie, NULL);
851
852 if (!reply) {
853 _eglError(EGL_BAD_SURFACE,
854 "eglGetMscRateANGLE failed to translate coordinates");
855 return EGL_FALSE;
856 }
857
858 #ifdef HAVE_LIBDRM
859 int area = 0;
860
861 for (unsigned c = 0; c < dri2_dpy->screen_resources.num_crtcs; c++) {
862 struct loader_crtc_info *crtc = &dri2_dpy->screen_resources.crtcs[c];
863
864 int c_area = box_intersection_area(
865 reply->dst_x, reply->dst_y, surface->Width, surface->Height, crtc->x,
866 crtc->y, crtc->width, crtc->height);
867 if (c_area > area) {
868 *numerator = crtc->refresh_numerator;
869 *denominator = crtc->refresh_denominator;
870 area = c_area;
871 }
872 }
873 #endif
874 /* If the window is entirely off-screen, then area will still be 0.
875 * We defaulted to the first CRTC in the list's refresh rate, earlier.
876 */
877
878 free(reply);
879
880 return EGL_TRUE;
881 }
882
883 static EGLBoolean
884 dri2_kopper_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
885 {
886 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
887
888 kopperSetSwapInterval(dri2_surf->dri_drawable, interval);
889
890 return EGL_TRUE;
891 }
892
893 static _EGLSurface *
894 dri2_kopper_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
895 void *native_window,
896 const EGLint *attrib_list)
897 {
898 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
899 _EGLSurface *surf;
900
901 surf = dri2_x11_create_surface(disp, EGL_WINDOW_BIT, conf, native_window,
902 attrib_list);
903 if (surf != NULL) {
904 /* When we first create the DRI2 drawable, its swap interval on the
905 * server side is 1.
906 */
907 surf->SwapInterval = 1;
908
909 /* Override that with a driconf-set value. */
910 dri2_kopper_swap_interval(disp, surf, dri2_dpy->default_swap_interval);
911 }
912
913 return surf;
914 }
915
916 static EGLint
917 dri2_kopper_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
918 {
919 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
920
921 return kopperQueryBufferAge(dri2_surf->dri_drawable);
922 }
923
924 static EGLint
925 dri2_swrast_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
926 {
927 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
928
929 return driSWRastQueryBufferAge(dri2_surf->dri_drawable);
930 }
931
932 static const struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl = {
933 .authenticate = NULL,
934 .create_window_surface = dri2_x11_create_window_surface,
935 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
936 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
937 .destroy_surface = dri2_x11_destroy_surface,
938 .create_image = dri2_create_image_khr,
939 .swap_buffers = dri2_x11_swap_buffers,
940 .swap_buffers_with_damage = dri2_x11_swap_buffers_with_damage,
941 .copy_buffers = dri2_x11_copy_buffers,
942 .query_buffer_age = dri2_swrast_query_buffer_age,
943 /* XXX: should really implement this since X11 has pixmaps */
944 .query_surface = dri2_query_surface,
945 .get_msc_rate = dri2_x11_get_msc_rate,
946 .get_dri_drawable = dri2_surface_get_dri_drawable,
947 };
948
949 static const struct dri2_egl_display_vtbl dri2_x11_kopper_display_vtbl = {
950 .authenticate = NULL,
951 .create_window_surface = dri2_kopper_create_window_surface,
952 .create_pixmap_surface = dri2_x11_create_pixmap_surface,
953 .create_pbuffer_surface = dri2_x11_create_pbuffer_surface,
954 .destroy_surface = dri2_x11_destroy_surface,
955 .create_image = dri2_create_image_khr,
956 .swap_interval = dri2_kopper_swap_interval,
957 .swap_buffers = dri2_x11_kopper_swap_buffers,
958 .swap_buffers_with_damage = dri2_x11_kopper_swap_buffers_with_damage,
959 .copy_buffers = dri2_x11_copy_buffers,
960 .query_buffer_age = dri2_kopper_query_buffer_age,
961 /* XXX: should really implement this since X11 has pixmaps */
962 .query_surface = dri2_query_surface,
963 .get_msc_rate = dri2_x11_get_msc_rate,
964 .get_dri_drawable = dri2_surface_get_dri_drawable,
965 };
966
967 static const __DRIswrastLoaderExtension swrast_loader_extension = {
968 .base = {__DRI_SWRAST_LOADER, 1},
969
970 .getDrawableInfo = swrastGetDrawableInfo,
971 .putImage = swrastPutImage,
972 .putImage2 = swrastPutImage2,
973 .getImage = swrastGetImage,
974 };
975
976 static const __DRIswrastLoaderExtension swrast_loader_shm_extension = {
977 .base = {__DRI_SWRAST_LOADER, 4},
978
979 .getDrawableInfo = swrastGetDrawableInfo,
980 .putImage = swrastPutImage,
981 .putImage2 = swrastPutImage2,
982 .putImageShm = swrastPutImageShm,
983 .getImage = swrastGetImage,
984 .getImage2 = swrastGetImage2,
985 .getImageShm = swrastGetImageShm,
986 };
987
988 static_assert(sizeof(struct kopper_vk_surface_create_storage) >=
989 sizeof(VkXcbSurfaceCreateInfoKHR),
990 "");
991
992 static void
993 kopperSetSurfaceCreateInfo(void *_draw, struct kopper_loader_info *ci)
994 {
995 struct dri2_egl_surface *dri2_surf = _draw;
996 struct dri2_egl_display *dri2_dpy =
997 dri2_egl_display(dri2_surf->base.Resource.Display);
998 VkXcbSurfaceCreateInfoKHR *xcb = (VkXcbSurfaceCreateInfoKHR *)&ci->bos;
999
1000 xcb->sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
1001 xcb->pNext = NULL;
1002 xcb->flags = 0;
1003 xcb->connection = dri2_dpy->conn;
1004 xcb->window = dri2_surf->drawable;
1005 ci->has_alpha = dri2_surf->depth == 32;
1006 ci->present_opaque = dri2_surf->base.PresentOpaque;
1007 }
1008
1009 static void
1010 kopperGetDrawableInfo(struct dri_drawable *draw, int *w, int *h,
1011 void *loaderPrivate)
1012 {
1013 int x = 0, y = 0;
1014 *w = *h = 0;
1015 x11_get_drawable_info(draw, &x, &y, w, h, loaderPrivate);
1016 }
1017
1018 static const __DRIkopperLoaderExtension kopper_loader_extension = {
1019 .base = {__DRI_KOPPER_LOADER, 1},
1020
1021 .SetSurfaceCreateInfo = kopperSetSurfaceCreateInfo,
1022 .GetDrawableInfo = kopperGetDrawableInfo,
1023 };
1024
1025 static const __DRIextension *kopper_loader_extensions[] = {
1026 &kopper_loader_extension.base,
1027 &image_lookup_extension.base,
1028 NULL,
1029 };
1030
1031 static const __DRIextension *swrast_loader_extensions[] = {
1032 &swrast_loader_extension.base,
1033 &image_lookup_extension.base,
1034 NULL,
1035 };
1036
1037 static const __DRIextension *swrast_loader_shm_extensions[] = {
1038 &swrast_loader_shm_extension.base,
1039 &image_lookup_extension.base,
1040 NULL,
1041 };
1042
1043 static int
1044 dri2_find_screen_for_display(const _EGLDisplay *disp, int fallback_screen)
1045 {
1046 const EGLAttrib *attr;
1047
1048 if (!disp->Options.Attribs)
1049 return fallback_screen;
1050
1051 for (attr = disp->Options.Attribs; attr[0] != EGL_NONE; attr += 2) {
1052 if (attr[0] == EGL_PLATFORM_X11_SCREEN_EXT ||
1053 attr[0] == EGL_PLATFORM_XCB_SCREEN_EXT)
1054 return attr[1];
1055 }
1056
1057 return fallback_screen;
1058 }
1059
1060 static EGLBoolean
1061 dri2_get_xcb_connection(_EGLDisplay *disp, struct dri2_egl_display *dri2_dpy)
1062 {
1063 xcb_screen_iterator_t s;
1064 int screen;
1065 const char *msg;
1066
1067 if (disp->PlatformDisplay == NULL) {
1068 dri2_dpy->conn = xcb_connect(NULL, &screen);
1069 dri2_dpy->own_device = true;
1070 screen = dri2_find_screen_for_display(disp, screen);
1071 } else if (disp->Platform == _EGL_PLATFORM_X11) {
1072 Display *dpy = disp->PlatformDisplay;
1073 dri2_dpy->conn = XGetXCBConnection(dpy);
1074 screen = DefaultScreen(dpy);
1075 } else {
1076 /* _EGL_PLATFORM_XCB */
1077 dri2_dpy->conn = disp->PlatformDisplay;
1078 screen = dri2_find_screen_for_display(disp, 0);
1079 }
1080
1081 if (!dri2_dpy->conn || xcb_connection_has_error(dri2_dpy->conn)) {
1082 msg = "xcb_connect failed";
1083 goto disconnect;
1084 }
1085
1086 s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
1087 dri2_dpy->screen = get_xcb_screen(s, screen);
1088 if (!dri2_dpy->screen) {
1089 msg = "failed to get xcb screen";
1090 goto disconnect;
1091 }
1092
1093 return EGL_TRUE;
1094 disconnect:
1095 if (disp->PlatformDisplay == NULL)
1096 xcb_disconnect(dri2_dpy->conn);
1097
1098 return _eglError(EGL_BAD_ALLOC, msg);
1099 }
1100
1101 static void
1102 dri2_x11_setup_swap_interval(_EGLDisplay *disp)
1103 {
1104 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1105 int arbitrary_max_interval = 1000;
1106
1107 /* default behavior for no SwapBuffers support: no vblank syncing
1108 * either.
1109 */
1110 dri2_dpy->min_swap_interval = 0;
1111 dri2_dpy->max_swap_interval = 0;
1112 dri2_dpy->default_swap_interval = 0;
1113
1114 if (!dri2_dpy->swap_available)
1115 return;
1116
1117 /* If we do have swapbuffers, then we can support pretty much any swap
1118 * interval. Unless we're kopper, for now.
1119 */
1120 if (dri2_dpy->kopper)
1121 arbitrary_max_interval = 1;
1122
1123 dri2_setup_swap_interval(disp, arbitrary_max_interval);
1124 }
1125
1126 static bool
1127 platform_x11_finalize(_EGLDisplay *disp, bool force_zink)
1128 {
1129 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1130
1131 if (!dri2_create_screen(disp))
1132 return false;
1133
1134 if (!dri2_setup_device(disp, disp->Options.ForceSoftware || force_zink)) {
1135 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
1136 return false;
1137 }
1138
1139 dri2_setup_screen(disp);
1140
1141 if (!dri2_dpy->swrast) {
1142 #ifdef HAVE_WAYLAND_PLATFORM
1143 if (dri2_dpy->kopper) {
1144 free(dri2_dpy->device_name);
1145 dri2_dpy->device_name = strdup("zink");
1146 }
1147 #endif
1148
1149 dri2_dpy->swap_available = true;
1150 dri2_x11_setup_swap_interval(disp);
1151 if (dri2_dpy->fd_render_gpu == dri2_dpy->fd_display_gpu)
1152 disp->Extensions.KHR_image_pixmap = EGL_TRUE;
1153 disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
1154 disp->Extensions.CHROMIUM_sync_control = EGL_TRUE;
1155 #ifdef HAVE_LIBDRM
1156 if (dri2_dpy->multibuffers_available)
1157 dri2_set_WL_bind_wayland_display(disp);
1158 #endif
1159 }
1160 disp->Extensions.ANGLE_sync_control_rate = EGL_TRUE;
1161 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1162 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1163
1164 dri2_x11_add_configs_for_visuals(dri2_dpy, disp, !dri2_dpy->kopper);
1165
1166 return true;
1167 }
1168
1169 static EGLBoolean
1170 dri2_initialize_x11_kopper(_EGLDisplay *disp, bool force_zink)
1171 {
1172 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1173
1174 dri2_dpy->loader_extensions = kopper_loader_extensions;
1175
1176 if (!platform_x11_finalize(disp, force_zink))
1177 return EGL_FALSE;
1178
1179 /* Fill vtbl last to prevent accidentally calling virtual function during
1180 * initialization.
1181 */
1182 dri2_dpy->vtbl = &dri2_x11_kopper_display_vtbl;
1183
1184 return EGL_TRUE;
1185 }
1186
1187 static EGLBoolean
1188 dri2_initialize_x11_swrast(_EGLDisplay *disp)
1189 {
1190 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1191
1192 if (x11_xcb_display_supports_xshm(dri2_dpy->conn)) {
1193 dri2_dpy->loader_extensions = swrast_loader_shm_extensions;
1194 } else {
1195 dri2_dpy->loader_extensions = swrast_loader_extensions;
1196 }
1197
1198 if (!platform_x11_finalize(disp, false))
1199 return EGL_FALSE;
1200
1201 /* Fill vtbl last to prevent accidentally calling virtual function during
1202 * initialization.
1203 */
1204 dri2_dpy->vtbl = &dri2_x11_swrast_display_vtbl;
1205
1206 return EGL_TRUE;
1207 }
1208
1209 #ifdef HAVE_LIBDRM
1210 static const __DRIextension *dri3_image_loader_extensions[] = {
1211 &dri3_image_loader_extension.base,
1212 &image_lookup_extension.base,
1213 NULL,
1214 };
1215
1216 static EGLBoolean
1217 dri2_initialize_x11_dri3(_EGLDisplay *disp)
1218 {
1219 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1220
1221 dri2_dpy->loader_extensions = dri3_image_loader_extensions;
1222
1223 if (!platform_x11_finalize(disp, false))
1224 return EGL_FALSE;
1225
1226 loader_init_screen_resources(&dri2_dpy->screen_resources, dri2_dpy->conn,
1227 dri2_dpy->screen);
1228
1229 /* Fill vtbl last to prevent accidentally calling virtual function during
1230 * initialization.
1231 */
1232 dri2_dpy->vtbl = &dri3_x11_display_vtbl;
1233
1234 _eglLog(_EGL_INFO, "Using DRI3");
1235
1236 return EGL_TRUE;
1237 }
1238 #endif
1239
1240 EGLBoolean
1241 dri2_initialize_x11(_EGLDisplay *disp)
1242 {
1243 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1244
1245 /* True if we're going to force-enable a HW Zink driver, even if the X
1246 * server is missing a bunch of features.
1247 */
1248 const bool force_zink =
1249 disp->Options.Zink && debug_get_bool_option("LIBGL_KOPPER_DRI2", false);
1250
1251 /*
1252 * Every hardware driver_name is set using strdup. Doing the same in
1253 * here will allow is to simply free the memory at dri2_terminate().
1254 */
1255 if (disp->Options.Zink)
1256 dri2_dpy->driver_name = strdup("zink");
1257 else if (disp->Options.ForceSoftware)
1258 dri2_dpy->driver_name = strdup("swrast");
1259
1260 if (!dri2_get_xcb_connection(disp, dri2_dpy))
1261 return EGL_FALSE;
1262
1263 #ifdef HAVE_X11_DRM
1264 dri2_dpy->multibuffers_available = x11_dri3_has_multibuffer(dri2_dpy->conn);
1265
1266 /* If we've selected Zink and we're not taking the swrast path then we need
1267 * multibuffers or else import won't work. We shouldn't enable Zink in
1268 * this case. The user is allowed to override this with LIBGL_KOPPER_DRI2.
1269 */
1270 if (disp->Options.Zink && !disp->Options.ForceSoftware &&
1271 !force_zink && !dri2_dpy->multibuffers_available)
1272 return EGL_FALSE;
1273 #endif
1274
1275 #ifdef HAVE_LIBDRM
1276 /* If LIBGL_KOPPER_DRI2 is enabled, skip the X11 render device checks.
1277 * We're going to enable Zink anyway.
1278 */
1279 if (!force_zink) {
1280 bool status = dri3_x11_connect(dri2_dpy, disp->Options.ForceSoftware);
1281 /* the status here is ignored for zink-with-kopper and swrast,
1282 * otherwise return whatever error/fallback status as failure
1283 */
1284 if (!status && !dri2_dpy->kopper && !disp->Options.ForceSoftware)
1285 return EGL_FALSE;
1286 }
1287 #endif
1288
1289 dri2_detect_swrast_kopper(disp);
1290
1291 if (dri2_dpy->kopper)
1292 return dri2_initialize_x11_kopper(disp, force_zink);
1293
1294 if (disp->Options.ForceSoftware)
1295 return dri2_initialize_x11_swrast(disp);
1296
1297 #ifdef HAVE_LIBDRM
1298 if (dri2_initialize_x11_dri3(disp))
1299 return EGL_TRUE;
1300 #endif
1301
1302 return EGL_FALSE;
1303 }
1304
1305 void
1306 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy)
1307 {
1308 #ifdef HAVE_LIBDRM
1309 loader_destroy_screen_resources(&dri2_dpy->screen_resources);
1310 #endif
1311
1312 if (dri2_dpy->own_device)
1313 xcb_disconnect(dri2_dpy->conn);
1314 }