2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * Kristian Høgsberg <krh@bitplanet.net>
41 #include <sys/types.h>
45 #include "egl_dri2_fallbacks.h"
49 #include "platform_x11_dri3.h"
53 dri2_x11_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
57 swrastCreateDrawable(struct dri2_egl_display
* dri2_dpy
,
58 struct dri2_egl_surface
* dri2_surf
)
61 const uint32_t function
= GXcopy
;
65 dri2_surf
->gc
= xcb_generate_id(dri2_dpy
->conn
);
66 mask
= XCB_GC_FUNCTION
;
67 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->gc
, dri2_surf
->drawable
, mask
, &function
);
69 dri2_surf
->swapgc
= xcb_generate_id(dri2_dpy
->conn
);
70 mask
= XCB_GC_FUNCTION
| XCB_GC_GRAPHICS_EXPOSURES
;
73 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
, dri2_surf
->drawable
, mask
, valgc
);
74 switch (dri2_surf
->depth
) {
77 dri2_surf
->bytes_per_pixel
= 4;
80 dri2_surf
->bytes_per_pixel
= 2;
83 dri2_surf
->bytes_per_pixel
= 1;
86 dri2_surf
->bytes_per_pixel
= 0;
89 _eglLog(_EGL_WARNING
, "unsupported depth %d", dri2_surf
->depth
);
94 swrastDestroyDrawable(struct dri2_egl_display
* dri2_dpy
,
95 struct dri2_egl_surface
* dri2_surf
)
97 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->gc
);
98 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
);
102 swrastGetDrawableInfo(__DRIdrawable
* draw
,
103 int *x
, int *y
, int *w
, int *h
,
106 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
107 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
109 xcb_get_geometry_cookie_t cookie
;
110 xcb_get_geometry_reply_t
*reply
;
111 xcb_generic_error_t
*error
;
114 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
115 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
120 _eglLog(_EGL_WARNING
, "error in xcb_get_geometry");
130 swrastPutImage(__DRIdrawable
* draw
, int op
,
131 int x
, int y
, int w
, int h
,
132 char *data
, void *loaderPrivate
)
134 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
135 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
140 case __DRI_SWRAST_IMAGE_OP_DRAW
:
143 case __DRI_SWRAST_IMAGE_OP_SWAP
:
144 gc
= dri2_surf
->swapgc
;
150 xcb_put_image(dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, dri2_surf
->drawable
,
151 gc
, w
, h
, x
, y
, 0, dri2_surf
->depth
,
152 w
*h
*dri2_surf
->bytes_per_pixel
, (const uint8_t *)data
);
156 swrastGetImage(__DRIdrawable
* read
,
157 int x
, int y
, int w
, int h
,
158 char *data
, void *loaderPrivate
)
160 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
161 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
163 xcb_get_image_cookie_t cookie
;
164 xcb_get_image_reply_t
*reply
;
165 xcb_generic_error_t
*error
;
167 cookie
= xcb_get_image (dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
,
168 dri2_surf
->drawable
, x
, y
, w
, h
, ~0);
169 reply
= xcb_get_image_reply (dri2_dpy
->conn
, cookie
, &error
);
174 _eglLog(_EGL_WARNING
, "error in xcb_get_image");
177 uint32_t bytes
= xcb_get_image_data_length(reply
);
178 uint8_t *idata
= xcb_get_image_data(reply
);
179 memcpy(data
, idata
, bytes
);
185 static xcb_screen_t
*
186 get_xcb_screen(xcb_screen_iterator_t iter
, int screen
)
188 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
197 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
200 dri2_x11_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
201 _EGLConfig
*conf
, void *native_surface
,
202 const EGLint
*attrib_list
)
204 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
205 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
206 struct dri2_egl_surface
*dri2_surf
;
207 xcb_get_geometry_cookie_t cookie
;
208 xcb_get_geometry_reply_t
*reply
;
209 xcb_screen_iterator_t s
;
210 xcb_generic_error_t
*error
;
211 xcb_drawable_t drawable
;
212 xcb_screen_t
*screen
;
213 const __DRIconfig
*config
;
215 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_surface
));
216 drawable
= (uintptr_t) native_surface
;
220 dri2_surf
= malloc(sizeof *dri2_surf
);
222 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
226 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
229 dri2_surf
->region
= XCB_NONE
;
230 if (type
== EGL_PBUFFER_BIT
) {
231 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
232 screen
= get_xcb_screen(s
, dri2_dpy
->screen
);
234 _eglError(EGL_BAD_ALLOC
, "failed to get xcb screen");
238 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
239 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
240 dri2_surf
->drawable
, screen
->root
,
241 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
244 if (type
== EGL_WINDOW_BIT
)
245 _eglError(EGL_BAD_NATIVE_WINDOW
, "dri2_create_surface");
247 _eglError(EGL_BAD_NATIVE_PIXMAP
, "dri2_create_surface");
250 dri2_surf
->drawable
= drawable
;
253 config
= dri2_get_dri_config(dri2_conf
, type
,
254 dri2_surf
->base
.GLColorspace
);
256 if (dri2_dpy
->dri2
) {
257 dri2_surf
->dri_drawable
=
258 (*dri2_dpy
->dri2
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
261 assert(dri2_dpy
->swrast
);
262 dri2_surf
->dri_drawable
=
263 (*dri2_dpy
->swrast
->createNewDrawable
)(dri2_dpy
->dri_screen
, config
,
267 if (dri2_surf
->dri_drawable
== NULL
) {
268 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
272 if (type
!= EGL_PBUFFER_BIT
) {
273 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
274 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
276 if (error
->error_code
== BadAlloc
)
277 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
278 else if (type
== EGL_WINDOW_BIT
)
279 _eglError(EGL_BAD_NATIVE_WINDOW
, "xcb_get_geometry");
281 _eglError(EGL_BAD_NATIVE_PIXMAP
, "xcb_get_geometry");
283 goto cleanup_dri_drawable
;
284 } else if (reply
== NULL
) {
285 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
286 goto cleanup_dri_drawable
;
289 dri2_surf
->base
.Width
= reply
->width
;
290 dri2_surf
->base
.Height
= reply
->height
;
291 dri2_surf
->depth
= reply
->depth
;
295 if (dri2_dpy
->dri2
) {
296 xcb_void_cookie_t cookie
;
299 cookie
= xcb_dri2_create_drawable_checked(dri2_dpy
->conn
,
300 dri2_surf
->drawable
);
301 error
= xcb_request_check(dri2_dpy
->conn
, cookie
);
302 conn_error
= xcb_connection_has_error(dri2_dpy
->conn
);
303 if (conn_error
|| error
!= NULL
) {
304 if (type
== EGL_PBUFFER_BIT
|| conn_error
|| error
->error_code
== BadAlloc
)
305 _eglError(EGL_BAD_ALLOC
, "xcb_dri2_create_drawable_checked");
306 else if (type
== EGL_WINDOW_BIT
)
307 _eglError(EGL_BAD_NATIVE_WINDOW
,
308 "xcb_dri2_create_drawable_checked");
310 _eglError(EGL_BAD_NATIVE_PIXMAP
,
311 "xcb_dri2_create_drawable_checked");
313 goto cleanup_dri_drawable
;
316 if (type
== EGL_PBUFFER_BIT
) {
317 dri2_surf
->depth
= _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
);
319 swrastCreateDrawable(dri2_dpy
, dri2_surf
);
322 /* we always copy the back buffer to front */
323 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
325 return &dri2_surf
->base
;
327 cleanup_dri_drawable
:
328 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
330 if (type
== EGL_PBUFFER_BIT
)
331 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
339 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
342 dri2_x11_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
343 _EGLConfig
*conf
, void *native_window
,
344 const EGLint
*attrib_list
)
346 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
349 surf
= dri2_x11_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
350 native_window
, attrib_list
);
352 /* When we first create the DRI2 drawable, its swap interval on the
355 surf
->SwapInterval
= 1;
357 /* Override that with a driconf-set value. */
358 dri2_x11_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
365 dri2_x11_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
366 _EGLConfig
*conf
, void *native_pixmap
,
367 const EGLint
*attrib_list
)
369 return dri2_x11_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
370 native_pixmap
, attrib_list
);
374 dri2_x11_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
375 _EGLConfig
*conf
, const EGLint
*attrib_list
)
377 return dri2_x11_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
378 XCB_WINDOW_NONE
, attrib_list
);
382 dri2_x11_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
384 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
385 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
389 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
391 if (dri2_dpy
->dri2
) {
392 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
394 assert(dri2_dpy
->swrast
);
395 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
398 if (surf
->Type
== EGL_PBUFFER_BIT
)
399 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
407 * Process list of buffer received from the server
409 * Processes the list of buffers received in a reply from the server to either
410 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
413 dri2_x11_process_buffers(struct dri2_egl_surface
*dri2_surf
,
414 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
416 struct dri2_egl_display
*dri2_dpy
=
417 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
418 xcb_rectangle_t rectangle
;
421 dri2_surf
->buffer_count
= count
;
422 dri2_surf
->have_fake_front
= 0;
424 /* This assumes the DRI2 buffer attachment tokens matches the
425 * __DRIbuffer tokens. */
426 for (i
= 0; i
< count
; i
++) {
427 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
428 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
429 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
430 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
431 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
433 /* We only use the DRI drivers single buffer configs. This
434 * means that if we try to render to a window, DRI2 will give us
435 * the fake front buffer, which we'll use as a back buffer.
436 * Note that EGL doesn't require that several clients rendering
437 * to the same window must see the same aux buffers. */
438 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
439 dri2_surf
->have_fake_front
= 1;
442 if (dri2_surf
->region
!= XCB_NONE
)
443 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
447 rectangle
.width
= dri2_surf
->base
.Width
;
448 rectangle
.height
= dri2_surf
->base
.Height
;
449 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
450 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
454 dri2_x11_get_buffers(__DRIdrawable
* driDrawable
,
455 int *width
, int *height
,
456 unsigned int *attachments
, int count
,
457 int *out_count
, void *loaderPrivate
)
459 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
460 struct dri2_egl_display
*dri2_dpy
=
461 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
462 xcb_dri2_dri2_buffer_t
*buffers
;
463 xcb_dri2_get_buffers_reply_t
*reply
;
464 xcb_dri2_get_buffers_cookie_t cookie
;
468 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
470 count
, count
, attachments
);
471 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
474 buffers
= xcb_dri2_get_buffers_buffers (reply
);
478 *out_count
= reply
->count
;
479 dri2_surf
->base
.Width
= *width
= reply
->width
;
480 dri2_surf
->base
.Height
= *height
= reply
->height
;
481 dri2_x11_process_buffers(dri2_surf
, buffers
, *out_count
);
485 return dri2_surf
->buffers
;
489 dri2_x11_get_buffers_with_format(__DRIdrawable
* driDrawable
,
490 int *width
, int *height
,
491 unsigned int *attachments
, int count
,
492 int *out_count
, void *loaderPrivate
)
494 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
495 struct dri2_egl_display
*dri2_dpy
=
496 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
497 xcb_dri2_dri2_buffer_t
*buffers
;
498 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
499 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
500 xcb_dri2_attach_format_t
*format_attachments
;
504 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
505 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
510 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
515 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
516 dri2_surf
->base
.Width
= *width
= reply
->width
;
517 dri2_surf
->base
.Height
= *height
= reply
->height
;
518 *out_count
= reply
->count
;
519 dri2_x11_process_buffers(dri2_surf
, buffers
, *out_count
);
523 return dri2_surf
->buffers
;
527 dri2_x11_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
531 /* FIXME: Does EGL support front buffer rendering at all? */
534 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
536 dri2WaitGL(dri2_surf
);
538 (void) loaderPrivate
;
543 dri2_x11_do_authenticate(struct dri2_egl_display
*dri2_dpy
, uint32_t id
)
545 xcb_dri2_authenticate_reply_t
*authenticate
;
546 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
547 xcb_screen_iterator_t s
;
548 xcb_screen_t
*screen
;
551 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
553 screen
= get_xcb_screen(s
, dri2_dpy
->screen
);
555 _eglLog(_EGL_WARNING
, "DRI2: failed to get xcb screen");
559 authenticate_cookie
=
560 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, screen
->root
, id
);
562 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
564 if (authenticate
== NULL
|| !authenticate
->authenticated
)
573 dri2_x11_local_authenticate(struct dri2_egl_display
*dri2_dpy
)
578 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
579 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
583 if (dri2_x11_do_authenticate(dri2_dpy
, magic
) < 0) {
584 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
592 dri2_x11_connect(struct dri2_egl_display
*dri2_dpy
)
594 xcb_xfixes_query_version_reply_t
*xfixes_query
;
595 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
596 xcb_dri2_query_version_reply_t
*dri2_query
;
597 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
598 xcb_dri2_connect_reply_t
*connect
;
599 xcb_dri2_connect_cookie_t connect_cookie
;
600 xcb_generic_error_t
*error
;
601 xcb_screen_iterator_t s
;
602 xcb_screen_t
*screen
;
603 char *driver_name
, *loader_driver_name
, *device_name
;
604 const xcb_query_extension_reply_t
*extension
;
606 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
607 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
609 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
610 if (!(extension
&& extension
->present
))
613 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
614 if (!(extension
&& extension
->present
))
617 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
618 XCB_XFIXES_MAJOR_VERSION
,
619 XCB_XFIXES_MINOR_VERSION
);
621 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
622 XCB_DRI2_MAJOR_VERSION
,
623 XCB_DRI2_MINOR_VERSION
);
625 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
626 screen
= get_xcb_screen(s
, dri2_dpy
->screen
);
628 _eglLog(_EGL_WARNING
, "DRI2: failed to get xcb screen");
631 connect_cookie
= xcb_dri2_connect_unchecked(dri2_dpy
->conn
, screen
->root
,
632 XCB_DRI2_DRIVER_TYPE_DRI
);
635 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
636 xfixes_query_cookie
, &error
);
637 if (xfixes_query
== NULL
||
638 error
!= NULL
|| xfixes_query
->major_version
< 2) {
639 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
646 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
647 if (dri2_query
== NULL
|| error
!= NULL
) {
648 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
652 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
653 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
656 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
657 if (connect
== NULL
||
658 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
659 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
663 device_name
= xcb_dri2_connect_device_name (connect
);
665 dri2_dpy
->fd
= loader_open_device(device_name
);
666 if (dri2_dpy
->fd
== -1) {
667 _eglLog(_EGL_WARNING
,
668 "DRI2: could not open %s (%s)", device_name
, strerror(errno
));
673 if (!dri2_x11_local_authenticate(dri2_dpy
)) {
679 driver_name
= xcb_dri2_connect_driver_name (connect
);
681 /* If Mesa knows about the appropriate driver for this fd, then trust it.
682 * Otherwise, default to the server's value.
684 loader_driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
, 0);
685 if (loader_driver_name
) {
686 dri2_dpy
->driver_name
= loader_driver_name
;
688 dri2_dpy
->driver_name
=
690 xcb_dri2_connect_driver_name_length(connect
));
693 if (dri2_dpy
->driver_name
== NULL
) {
695 free(dri2_dpy
->driver_name
);
700 #ifdef HAVE_WAYLAND_PLATFORM
701 dri2_dpy
->device_name
=
703 xcb_dri2_connect_device_name_length(connect
));
712 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
714 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
716 return dri2_x11_do_authenticate(dri2_dpy
, id
);
720 dri2_x11_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
721 _EGLDisplay
*disp
, bool supports_preserved
)
723 xcb_screen_iterator_t s
;
724 xcb_depth_iterator_t d
;
725 xcb_visualtype_t
*visuals
;
727 unsigned int rgba_masks
[4];
729 EGLint config_attrs
[] = {
730 EGL_NATIVE_VISUAL_ID
, 0,
731 EGL_NATIVE_VISUAL_TYPE
, 0,
735 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
736 d
= xcb_screen_allowed_depths_iterator(get_xcb_screen(s
, dri2_dpy
->screen
));
744 if (supports_preserved
)
745 surface_type
|= EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
748 EGLBoolean class_added
[6] = { 0, };
750 visuals
= xcb_depth_visuals(d
.data
);
751 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
752 if (class_added
[visuals
[i
]._class
])
755 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
756 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
757 struct dri2_egl_config
*dri2_conf
;
758 const __DRIconfig
*config
= dri2_dpy
->driver_configs
[j
];
760 config_attrs
[1] = visuals
[i
].visual_id
;
761 config_attrs
[3] = visuals
[i
]._class
;
763 rgba_masks
[0] = visuals
[i
].red_mask
;
764 rgba_masks
[1] = visuals
[i
].green_mask
;
765 rgba_masks
[2] = visuals
[i
].blue_mask
;
767 dri2_conf
= dri2_add_config(disp
, config
, count
+ 1, surface_type
,
768 config_attrs
, rgba_masks
);
772 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
773 * Otherwise it will only match a 32-bit RGBA visual. On a
774 * composited window manager on X11, this will make all of the
775 * EGLConfigs with destination alpha get blended by the
776 * compositor. This is probably not what the application
777 * wants... especially on drivers that only have 32-bit RGBA
779 if (d
.data
->depth
== 24) {
781 ~(rgba_masks
[0] | rgba_masks
[1] | rgba_masks
[2]);
782 dri2_conf
= dri2_add_config(disp
, config
, count
+ 1, surface_type
,
783 config_attrs
, rgba_masks
);
794 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
802 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
803 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
805 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
806 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
807 enum xcb_dri2_attachment_t render_attachment
;
808 xcb_dri2_copy_region_cookie_t cookie
;
810 /* No-op for a pixmap or pbuffer surface */
811 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
815 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
817 if (dri2_surf
->have_fake_front
)
818 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
820 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
822 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
825 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
827 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
833 dri2_x11_swap_buffers_msc(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
834 int64_t msc
, int64_t divisor
, int64_t remainder
)
836 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
837 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
838 uint32_t msc_hi
= msc
>> 32;
839 uint32_t msc_lo
= msc
& 0xffffffff;
840 uint32_t divisor_hi
= divisor
>> 32;
841 uint32_t divisor_lo
= divisor
& 0xffffffff;
842 uint32_t remainder_hi
= remainder
>> 32;
843 uint32_t remainder_lo
= remainder
& 0xffffffff;
844 xcb_dri2_swap_buffers_cookie_t cookie
;
845 xcb_dri2_swap_buffers_reply_t
*reply
;
846 int64_t swap_count
= -1;
848 /* No-op for a pixmap or pbuffer surface */
849 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
852 if (draw
->SwapBehavior
== EGL_BUFFER_PRESERVED
|| !dri2_dpy
->swap_available
)
853 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
) ? 0 : -1;
855 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
857 cookie
= xcb_dri2_swap_buffers_unchecked(dri2_dpy
->conn
, dri2_surf
->drawable
,
858 msc_hi
, msc_lo
, divisor_hi
, divisor_lo
, remainder_hi
, remainder_lo
);
860 reply
= xcb_dri2_swap_buffers_reply(dri2_dpy
->conn
, cookie
, NULL
);
863 swap_count
= (((int64_t)reply
->swap_hi
) << 32) | reply
->swap_lo
;
867 /* Since we aren't watching for the server's invalidate events like we're
868 * supposed to (due to XCB providing no mechanism for filtering the events
869 * the way xlib does), and SwapBuffers is a common cause of invalidate
870 * events, just shove one down to the driver, even though we haven't told
871 * the driver that we're the kind of loader that provides reliable
872 * invalidate events. This causes the driver to request buffers again at
873 * its next draw, so that we get the correct buffers if a pageflip
874 * happened. The driver should still be using the viewport hack to catch
877 if (dri2_dpy
->flush
&&
878 dri2_dpy
->flush
->base
.version
>= 3 && dri2_dpy
->flush
->invalidate
)
879 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
885 dri2_x11_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
887 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
888 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
890 if (dri2_dpy
->dri2
) {
891 if (dri2_x11_swap_buffers_msc(drv
, disp
, draw
, 0, 0, 0) != -1) {
894 /* Swap failed with a window drawable. */
895 _eglError(EGL_BAD_NATIVE_WINDOW
, __FUNCTION__
);
898 assert(dri2_dpy
->swrast
);
900 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
906 dri2_x11_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
908 EGLint numRects
, const EGLint
*rects
)
910 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
911 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
913 xcb_xfixes_region_t region
;
914 xcb_rectangle_t rectangles
[16];
917 if (numRects
> (int)ARRAY_SIZE(rectangles
))
918 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
920 for (i
= 0; i
< numRects
; i
++) {
921 rectangles
[i
].x
= rects
[i
* 4];
922 rectangles
[i
].y
= dri2_surf
->base
.Height
- rects
[i
* 4 + 1] - rects
[i
* 4 + 3];
923 rectangles
[i
].width
= rects
[i
* 4 + 2];
924 rectangles
[i
].height
= rects
[i
* 4 + 3];
927 region
= xcb_generate_id(dri2_dpy
->conn
);
928 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
929 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
930 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
936 dri2_x11_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
937 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
939 const EGLint rect
[4] = { x
, y
, width
, height
};
941 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
942 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
944 return dri2_x11_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
948 dri2_x11_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
951 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
952 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
954 if (interval
> surf
->Config
->MaxSwapInterval
)
955 interval
= surf
->Config
->MaxSwapInterval
;
956 else if (interval
< surf
->Config
->MinSwapInterval
)
957 interval
= surf
->Config
->MinSwapInterval
;
959 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
960 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
962 surf
->SwapInterval
= interval
;
968 dri2_x11_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
969 void *native_pixmap_target
)
971 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
972 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
976 STATIC_ASSERT(sizeof(uintptr_t) == sizeof(native_pixmap_target
));
977 target
= (uintptr_t) native_pixmap_target
;
981 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
983 gc
= xcb_generate_id(dri2_dpy
->conn
);
984 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
985 xcb_copy_area(dri2_dpy
->conn
,
991 dri2_surf
->base
.Width
,
992 dri2_surf
->base
.Height
);
993 xcb_free_gc(dri2_dpy
->conn
, gc
);
999 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1000 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1002 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1003 struct dri2_egl_image
*dri2_img
;
1004 unsigned int attachments
[1];
1005 xcb_drawable_t drawable
;
1006 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
1007 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
1008 xcb_dri2_dri2_buffer_t
*buffers
;
1009 xcb_get_geometry_cookie_t geometry_cookie
;
1010 xcb_get_geometry_reply_t
*geometry_reply
;
1011 xcb_generic_error_t
*error
;
1016 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
1017 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
1018 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
1020 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
1021 drawable
, 1, 1, attachments
);
1022 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
1023 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
1024 buffers_cookie
, NULL
);
1025 if (buffers_reply
== NULL
)
1028 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
1029 if (buffers
== NULL
) {
1033 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
1034 geometry_cookie
, &error
);
1035 if (geometry_reply
== NULL
|| error
!= NULL
) {
1036 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
1038 free(buffers_reply
);
1042 switch (geometry_reply
->depth
) {
1044 format
= __DRI_IMAGE_FORMAT_RGB565
;
1047 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
1050 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1053 _eglError(EGL_BAD_PARAMETER
,
1054 "dri2_create_image_khr: unsupported pixmap depth");
1055 free(buffers_reply
);
1056 free(geometry_reply
);
1060 dri2_img
= malloc(sizeof *dri2_img
);
1062 free(buffers_reply
);
1063 free(geometry_reply
);
1064 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1065 return EGL_NO_IMAGE_KHR
;
1068 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1069 free(buffers_reply
);
1070 free(geometry_reply
);
1072 return EGL_NO_IMAGE_KHR
;
1075 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
1076 dri2_img
->dri_image
=
1077 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1078 buffers_reply
->width
,
1079 buffers_reply
->height
,
1085 free(buffers_reply
);
1086 free(geometry_reply
);
1088 return &dri2_img
->base
;
1092 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1093 _EGLContext
*ctx
, EGLenum target
,
1094 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1099 case EGL_NATIVE_PIXMAP_KHR
:
1100 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
1102 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
1107 dri2_x11_get_sync_values(_EGLDisplay
*display
, _EGLSurface
*surface
,
1108 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1111 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(display
);
1112 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1113 xcb_dri2_get_msc_cookie_t cookie
;
1114 xcb_dri2_get_msc_reply_t
*reply
;
1116 cookie
= xcb_dri2_get_msc(dri2_dpy
->conn
, dri2_surf
->drawable
);
1117 reply
= xcb_dri2_get_msc_reply(dri2_dpy
->conn
, cookie
, NULL
);
1120 _eglError(EGL_BAD_ACCESS
, __func__
);
1124 *ust
= ((EGLuint64KHR
) reply
->ust_hi
<< 32) | reply
->ust_lo
;
1125 *msc
= ((EGLuint64KHR
) reply
->msc_hi
<< 32) | reply
->msc_lo
;
1126 *sbc
= ((EGLuint64KHR
) reply
->sbc_hi
<< 32) | reply
->sbc_lo
;
1132 static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl
= {
1133 .authenticate
= NULL
,
1134 .create_window_surface
= dri2_x11_create_window_surface
,
1135 .create_pixmap_surface
= dri2_x11_create_pixmap_surface
,
1136 .create_pbuffer_surface
= dri2_x11_create_pbuffer_surface
,
1137 .destroy_surface
= dri2_x11_destroy_surface
,
1138 .create_image
= dri2_fallback_create_image_khr
,
1139 .swap_interval
= dri2_fallback_swap_interval
,
1140 .swap_buffers
= dri2_x11_swap_buffers
,
1141 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1142 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1143 .copy_buffers
= dri2_x11_copy_buffers
,
1144 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1145 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1146 .get_sync_values
= dri2_fallback_get_sync_values
,
1147 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1150 static struct dri2_egl_display_vtbl dri2_x11_display_vtbl
= {
1151 .authenticate
= dri2_x11_authenticate
,
1152 .create_window_surface
= dri2_x11_create_window_surface
,
1153 .create_pixmap_surface
= dri2_x11_create_pixmap_surface
,
1154 .create_pbuffer_surface
= dri2_x11_create_pbuffer_surface
,
1155 .destroy_surface
= dri2_x11_destroy_surface
,
1156 .create_image
= dri2_x11_create_image_khr
,
1157 .swap_interval
= dri2_x11_swap_interval
,
1158 .swap_buffers
= dri2_x11_swap_buffers
,
1159 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1160 .swap_buffers_region
= dri2_x11_swap_buffers_region
,
1161 .post_sub_buffer
= dri2_x11_post_sub_buffer
,
1162 .copy_buffers
= dri2_x11_copy_buffers
,
1163 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1164 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
1165 .get_sync_values
= dri2_x11_get_sync_values
,
1166 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1169 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
1170 .base
= { __DRI_SWRAST_LOADER
, 1 },
1172 .getDrawableInfo
= swrastGetDrawableInfo
,
1173 .putImage
= swrastPutImage
,
1174 .getImage
= swrastGetImage
,
1177 static const __DRIextension
*swrast_loader_extensions
[] = {
1178 &swrast_loader_extension
.base
,
1183 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1185 struct dri2_egl_display
*dri2_dpy
;
1187 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1189 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1191 disp
->DriverData
= (void *) dri2_dpy
;
1192 if (disp
->PlatformDisplay
== NULL
) {
1193 dri2_dpy
->conn
= xcb_connect(0, &dri2_dpy
->screen
);
1194 dri2_dpy
->own_device
= true;
1196 Display
*dpy
= disp
->PlatformDisplay
;
1198 dri2_dpy
->conn
= XGetXCBConnection(dpy
);
1199 dri2_dpy
->screen
= DefaultScreen(dpy
);
1202 if (!dri2_dpy
->conn
|| xcb_connection_has_error(dri2_dpy
->conn
)) {
1203 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1208 * Every hardware driver_name is set using strdup. Doing the same in
1209 * here will allow is to simply free the memory at dri2_terminate().
1212 dri2_dpy
->driver_name
= strdup("swrast");
1213 if (!dri2_load_driver_swrast(disp
))
1216 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
1218 if (!dri2_create_screen(disp
))
1219 goto cleanup_driver
;
1221 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
, true))
1222 goto cleanup_configs
;
1224 /* Fill vtbl last to prevent accidentally calling virtual function during
1227 dri2_dpy
->vtbl
= &dri2_x11_swrast_display_vtbl
;
1232 _eglCleanupDisplay(disp
);
1233 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1235 dlclose(dri2_dpy
->driver
);
1237 free(dri2_dpy
->driver_name
);
1238 if (disp
->PlatformDisplay
== NULL
)
1239 xcb_disconnect(dri2_dpy
->conn
);
1242 disp
->DriverData
= NULL
;
1248 dri2_x11_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1250 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1251 int arbitrary_max_interval
= 1000;
1253 /* default behavior for no SwapBuffers support: no vblank syncing
1256 dri2_dpy
->min_swap_interval
= 0;
1257 dri2_dpy
->max_swap_interval
= 0;
1259 if (!dri2_dpy
->swap_available
)
1262 /* If we do have swapbuffers, then we can support pretty much any swap
1263 * interval, but we allow driconf to override applications.
1265 if (dri2_dpy
->config
)
1266 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1267 "vblank_mode", &vblank_mode
);
1268 switch (vblank_mode
) {
1269 case DRI_CONF_VBLANK_NEVER
:
1270 dri2_dpy
->min_swap_interval
= 0;
1271 dri2_dpy
->max_swap_interval
= 0;
1272 dri2_dpy
->default_swap_interval
= 0;
1274 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1275 dri2_dpy
->min_swap_interval
= 1;
1276 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1277 dri2_dpy
->default_swap_interval
= 1;
1279 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1280 dri2_dpy
->min_swap_interval
= 0;
1281 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1282 dri2_dpy
->default_swap_interval
= 0;
1285 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1286 dri2_dpy
->min_swap_interval
= 0;
1287 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1288 dri2_dpy
->default_swap_interval
= 1;
1295 static const __DRIextension
*dri3_image_loader_extensions
[] = {
1296 &dri3_image_loader_extension
.base
,
1297 &image_lookup_extension
.base
,
1298 &use_invalidate
.base
,
1303 dri2_initialize_x11_dri3(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1305 struct dri2_egl_display
*dri2_dpy
;
1307 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1309 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1311 disp
->DriverData
= (void *) dri2_dpy
;
1312 if (disp
->PlatformDisplay
== NULL
) {
1313 dri2_dpy
->conn
= xcb_connect(0, &dri2_dpy
->screen
);
1314 dri2_dpy
->own_device
= true;
1316 Display
*dpy
= disp
->PlatformDisplay
;
1318 dri2_dpy
->conn
= XGetXCBConnection(dpy
);
1319 dri2_dpy
->screen
= DefaultScreen(dpy
);
1322 if (!dri2_dpy
->conn
|| xcb_connection_has_error(dri2_dpy
->conn
)) {
1323 _eglLog(_EGL_WARNING
, "DRI3: xcb_connect failed");
1327 if (!dri3_x11_connect(dri2_dpy
))
1330 if (!dri2_load_driver_dri3(disp
))
1333 dri2_dpy
->loader_extensions
= dri3_image_loader_extensions
;
1335 dri2_dpy
->swap_available
= true;
1336 dri2_dpy
->invalidate_available
= true;
1338 if (!dri2_create_screen(disp
))
1341 dri2_x11_setup_swap_interval(dri2_dpy
);
1343 if (!dri2_dpy
->is_different_gpu
)
1344 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1345 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1346 disp
->Extensions
.CHROMIUM_sync_control
= EGL_TRUE
;
1347 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1349 dri2_set_WL_bind_wayland_display(drv
, disp
);
1351 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
, false))
1352 goto cleanup_configs
;
1354 dri2_dpy
->loader_dri3_ext
.core
= dri2_dpy
->core
;
1355 dri2_dpy
->loader_dri3_ext
.image_driver
= dri2_dpy
->image_driver
;
1356 dri2_dpy
->loader_dri3_ext
.flush
= dri2_dpy
->flush
;
1357 dri2_dpy
->loader_dri3_ext
.tex_buffer
= dri2_dpy
->tex_buffer
;
1358 dri2_dpy
->loader_dri3_ext
.image
= dri2_dpy
->image
;
1359 dri2_dpy
->loader_dri3_ext
.config
= dri2_dpy
->config
;
1361 /* Fill vtbl last to prevent accidentally calling virtual function during
1364 dri2_dpy
->vtbl
= &dri3_x11_display_vtbl
;
1366 _eglLog(_EGL_INFO
, "Using DRI3");
1371 _eglCleanupDisplay(disp
);
1372 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1373 dlclose(dri2_dpy
->driver
);
1375 close(dri2_dpy
->fd
);
1377 if (disp
->PlatformDisplay
== NULL
)
1378 xcb_disconnect(dri2_dpy
->conn
);
1381 disp
->DriverData
= NULL
;
1387 static const __DRIdri2LoaderExtension dri2_loader_extension_old
= {
1388 .base
= { __DRI_DRI2_LOADER
, 2 },
1390 .getBuffers
= dri2_x11_get_buffers
,
1391 .flushFrontBuffer
= dri2_x11_flush_front_buffer
,
1392 .getBuffersWithFormat
= NULL
,
1395 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
1396 .base
= { __DRI_DRI2_LOADER
, 3 },
1398 .getBuffers
= dri2_x11_get_buffers
,
1399 .flushFrontBuffer
= dri2_x11_flush_front_buffer
,
1400 .getBuffersWithFormat
= dri2_x11_get_buffers_with_format
,
1403 static const __DRIextension
*dri2_loader_extensions_old
[] = {
1404 &dri2_loader_extension_old
.base
,
1405 &image_lookup_extension
.base
,
1409 static const __DRIextension
*dri2_loader_extensions
[] = {
1410 &dri2_loader_extension
.base
,
1411 &image_lookup_extension
.base
,
1416 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1418 struct dri2_egl_display
*dri2_dpy
;
1420 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1422 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1424 disp
->DriverData
= (void *) dri2_dpy
;
1425 if (disp
->PlatformDisplay
== NULL
) {
1426 dri2_dpy
->conn
= xcb_connect(0, &dri2_dpy
->screen
);
1427 dri2_dpy
->own_device
= true;
1429 Display
*dpy
= disp
->PlatformDisplay
;
1431 dri2_dpy
->conn
= XGetXCBConnection(dpy
);
1432 dri2_dpy
->screen
= DefaultScreen(dpy
);
1435 if (!dri2_dpy
->conn
|| xcb_connection_has_error(dri2_dpy
->conn
)) {
1436 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1440 if (!dri2_x11_connect(dri2_dpy
))
1443 if (!dri2_load_driver(disp
))
1446 if (dri2_dpy
->dri2_minor
>= 1)
1447 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1449 dri2_dpy
->loader_extensions
= dri2_loader_extensions_old
;
1451 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1452 dri2_dpy
->invalidate_available
= (dri2_dpy
->dri2_minor
>= 3);
1454 if (!dri2_create_screen(disp
))
1455 goto cleanup_driver
;
1457 dri2_x11_setup_swap_interval(dri2_dpy
);
1459 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1460 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1461 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1462 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1463 disp
->Extensions
.CHROMIUM_sync_control
= EGL_TRUE
;
1465 dri2_set_WL_bind_wayland_display(drv
, disp
);
1467 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
, true))
1468 goto cleanup_configs
;
1470 /* Fill vtbl last to prevent accidentally calling virtual function during
1473 dri2_dpy
->vtbl
= &dri2_x11_display_vtbl
;
1475 _eglLog(_EGL_INFO
, "Using DRI2");
1480 _eglCleanupDisplay(disp
);
1481 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1483 dlclose(dri2_dpy
->driver
);
1485 close(dri2_dpy
->fd
);
1487 if (disp
->PlatformDisplay
== NULL
)
1488 xcb_disconnect(dri2_dpy
->conn
);
1491 disp
->DriverData
= NULL
;
1497 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1499 EGLBoolean initialized
= EGL_TRUE
;
1501 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1503 if (x11_dri2_accel
) {
1505 if (getenv("LIBGL_DRI3_DISABLE") != NULL
||
1506 !dri2_initialize_x11_dri3(drv
, disp
)) {
1508 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1509 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1515 initialized
= dri2_initialize_x11_swrast(drv
, disp
);