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>
39 #include <sys/types.h>
43 #include "egl_dri2_fallbacks.h"
46 dri2_x11_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
50 swrastCreateDrawable(struct dri2_egl_display
* dri2_dpy
,
51 struct dri2_egl_surface
* dri2_surf
,
55 const uint32_t function
= GXcopy
;
59 dri2_surf
->gc
= xcb_generate_id(dri2_dpy
->conn
);
60 mask
= XCB_GC_FUNCTION
;
61 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->gc
, dri2_surf
->drawable
, mask
, &function
);
63 dri2_surf
->swapgc
= xcb_generate_id(dri2_dpy
->conn
);
64 mask
= XCB_GC_FUNCTION
| XCB_GC_GRAPHICS_EXPOSURES
;
67 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
, dri2_surf
->drawable
, mask
, valgc
);
68 dri2_surf
->depth
= depth
;
72 dri2_surf
->bytes_per_pixel
= 4;
75 dri2_surf
->bytes_per_pixel
= 2;
78 dri2_surf
->bytes_per_pixel
= 1;
81 dri2_surf
->bytes_per_pixel
= 0;
84 _eglLog(_EGL_WARNING
, "unsupported depth %d", depth
);
89 swrastDestroyDrawable(struct dri2_egl_display
* dri2_dpy
,
90 struct dri2_egl_surface
* dri2_surf
)
92 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->gc
);
93 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
);
97 swrastGetDrawableInfo(__DRIdrawable
* draw
,
98 int *x
, int *y
, int *w
, int *h
,
101 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
102 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
104 xcb_get_geometry_cookie_t cookie
;
105 xcb_get_geometry_reply_t
*reply
;
106 xcb_generic_error_t
*error
;
109 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
110 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
115 _eglLog(_EGL_WARNING
, "error in xcb_get_geometry");
125 swrastPutImage(__DRIdrawable
* draw
, int op
,
126 int x
, int y
, int w
, int h
,
127 char *data
, void *loaderPrivate
)
129 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
130 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
135 case __DRI_SWRAST_IMAGE_OP_DRAW
:
138 case __DRI_SWRAST_IMAGE_OP_SWAP
:
139 gc
= dri2_surf
->swapgc
;
145 xcb_put_image(dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, dri2_surf
->drawable
,
146 gc
, w
, h
, x
, y
, 0, dri2_surf
->depth
,
147 w
*h
*dri2_surf
->bytes_per_pixel
, (const uint8_t *)data
);
151 swrastGetImage(__DRIdrawable
* read
,
152 int x
, int y
, int w
, int h
,
153 char *data
, void *loaderPrivate
)
155 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
156 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
158 xcb_get_image_cookie_t cookie
;
159 xcb_get_image_reply_t
*reply
;
160 xcb_generic_error_t
*error
;
162 cookie
= xcb_get_image (dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
,
163 dri2_surf
->drawable
, x
, y
, w
, h
, ~0);
164 reply
= xcb_get_image_reply (dri2_dpy
->conn
, cookie
, &error
);
169 _eglLog(_EGL_WARNING
, "error in xcb_get_image");
172 uint32_t bytes
= xcb_get_image_data_length(reply
);
173 uint8_t *idata
= xcb_get_image_data(reply
);
174 memcpy(data
, idata
, bytes
);
181 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
184 dri2_x11_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
185 _EGLConfig
*conf
, EGLNativeWindowType native_window
,
186 const EGLint
*attrib_list
)
188 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
189 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
190 struct dri2_egl_surface
*dri2_surf
;
191 xcb_get_geometry_cookie_t cookie
;
192 xcb_get_geometry_reply_t
*reply
;
193 xcb_screen_iterator_t s
;
194 xcb_generic_error_t
*error
;
195 xcb_drawable_t window
= (uintptr_t )native_window
;
199 dri2_surf
= malloc(sizeof *dri2_surf
);
201 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
205 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
208 dri2_surf
->region
= XCB_NONE
;
209 if (type
== EGL_PBUFFER_BIT
) {
210 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
211 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
212 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
213 dri2_surf
->drawable
, s
.data
->root
,
214 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
216 dri2_surf
->drawable
= window
;
219 if (dri2_dpy
->dri2
) {
220 dri2_surf
->dri_drawable
=
221 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
222 type
== EGL_WINDOW_BIT
?
223 dri2_conf
->dri_double_config
:
224 dri2_conf
->dri_single_config
,
227 assert(dri2_dpy
->swrast
);
228 dri2_surf
->dri_drawable
=
229 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
230 dri2_conf
->dri_double_config
,
234 if (dri2_surf
->dri_drawable
== NULL
) {
235 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
239 if (dri2_dpy
->dri2
) {
240 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
242 swrastCreateDrawable(dri2_dpy
, dri2_surf
, _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
));
245 if (type
!= EGL_PBUFFER_BIT
) {
246 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
247 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
248 if (reply
== NULL
|| error
!= NULL
) {
249 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
251 goto cleanup_dri_drawable
;
254 dri2_surf
->base
.Width
= reply
->width
;
255 dri2_surf
->base
.Height
= reply
->height
;
259 /* we always copy the back buffer to front */
260 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
262 return &dri2_surf
->base
;
264 cleanup_dri_drawable
:
265 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
267 if (type
== EGL_PBUFFER_BIT
)
268 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
276 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
279 dri2_x11_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
280 _EGLConfig
*conf
, EGLNativeWindowType window
,
281 const EGLint
*attrib_list
)
283 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
286 surf
= dri2_x11_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
287 window
, attrib_list
);
289 /* When we first create the DRI2 drawable, its swap interval on the
292 surf
->SwapInterval
= 1;
294 /* Override that with a driconf-set value. */
295 dri2_x11_swap_interval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
302 dri2_x11_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
303 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
304 const EGLint
*attrib_list
)
306 return dri2_x11_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
307 pixmap
, attrib_list
);
311 dri2_x11_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
312 _EGLConfig
*conf
, const EGLint
*attrib_list
)
314 return dri2_x11_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
315 XCB_WINDOW_NONE
, attrib_list
);
319 dri2_x11_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
321 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
322 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
326 if (!_eglPutSurface(surf
))
329 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
331 if (dri2_dpy
->dri2
) {
332 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
334 assert(dri2_dpy
->swrast
);
335 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
338 if (surf
->Type
== EGL_PBUFFER_BIT
)
339 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
347 * Process list of buffer received from the server
349 * Processes the list of buffers received in a reply from the server to either
350 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
353 dri2_x11_process_buffers(struct dri2_egl_surface
*dri2_surf
,
354 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
356 struct dri2_egl_display
*dri2_dpy
=
357 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
358 xcb_rectangle_t rectangle
;
361 dri2_surf
->buffer_count
= count
;
362 dri2_surf
->have_fake_front
= 0;
364 /* This assumes the DRI2 buffer attachment tokens matches the
365 * __DRIbuffer tokens. */
366 for (i
= 0; i
< count
; i
++) {
367 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
368 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
369 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
370 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
371 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
373 /* We only use the DRI drivers single buffer configs. This
374 * means that if we try to render to a window, DRI2 will give us
375 * the fake front buffer, which we'll use as a back buffer.
376 * Note that EGL doesn't require that several clients rendering
377 * to the same window must see the same aux buffers. */
378 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
379 dri2_surf
->have_fake_front
= 1;
382 if (dri2_surf
->region
!= XCB_NONE
)
383 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
387 rectangle
.width
= dri2_surf
->base
.Width
;
388 rectangle
.height
= dri2_surf
->base
.Height
;
389 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
390 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
394 dri2_x11_get_buffers(__DRIdrawable
* driDrawable
,
395 int *width
, int *height
,
396 unsigned int *attachments
, int count
,
397 int *out_count
, void *loaderPrivate
)
399 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
400 struct dri2_egl_display
*dri2_dpy
=
401 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
402 xcb_dri2_dri2_buffer_t
*buffers
;
403 xcb_dri2_get_buffers_reply_t
*reply
;
404 xcb_dri2_get_buffers_cookie_t cookie
;
408 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
410 count
, count
, attachments
);
411 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
412 buffers
= xcb_dri2_get_buffers_buffers (reply
);
416 *out_count
= reply
->count
;
417 dri2_surf
->base
.Width
= *width
= reply
->width
;
418 dri2_surf
->base
.Height
= *height
= reply
->height
;
419 dri2_x11_process_buffers(dri2_surf
, buffers
, *out_count
);
423 return dri2_surf
->buffers
;
427 dri2_x11_get_buffers_with_format(__DRIdrawable
* driDrawable
,
428 int *width
, int *height
,
429 unsigned int *attachments
, int count
,
430 int *out_count
, void *loaderPrivate
)
432 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
433 struct dri2_egl_display
*dri2_dpy
=
434 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
435 xcb_dri2_dri2_buffer_t
*buffers
;
436 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
437 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
438 xcb_dri2_attach_format_t
*format_attachments
;
442 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
443 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
448 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
453 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
454 dri2_surf
->base
.Width
= *width
= reply
->width
;
455 dri2_surf
->base
.Height
= *height
= reply
->height
;
456 *out_count
= reply
->count
;
457 dri2_x11_process_buffers(dri2_surf
, buffers
, *out_count
);
461 return dri2_surf
->buffers
;
465 dri2_x11_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
469 /* FIXME: Does EGL support front buffer rendering at all? */
472 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
474 dri2WaitGL(dri2_surf
);
476 (void) loaderPrivate
;
481 dri2_x11_strndup(const char *s
, int length
)
485 d
= malloc(length
+ 1);
489 memcpy(d
, s
, length
);
496 dri2_x11_connect(struct dri2_egl_display
*dri2_dpy
)
498 xcb_xfixes_query_version_reply_t
*xfixes_query
;
499 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
500 xcb_dri2_query_version_reply_t
*dri2_query
;
501 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
502 xcb_dri2_connect_reply_t
*connect
;
503 xcb_dri2_connect_cookie_t connect_cookie
;
504 xcb_generic_error_t
*error
;
505 xcb_screen_iterator_t s
;
506 char *driver_name
, *device_name
;
507 const xcb_query_extension_reply_t
*extension
;
509 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
510 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
512 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
513 if (!(extension
&& extension
->present
))
516 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
517 if (!(extension
&& extension
->present
))
520 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
521 XCB_XFIXES_MAJOR_VERSION
,
522 XCB_XFIXES_MINOR_VERSION
);
524 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
525 XCB_DRI2_MAJOR_VERSION
,
526 XCB_DRI2_MINOR_VERSION
);
528 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
529 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
531 XCB_DRI2_DRIVER_TYPE_DRI
);
534 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
535 xfixes_query_cookie
, &error
);
536 if (xfixes_query
== NULL
||
537 error
!= NULL
|| xfixes_query
->major_version
< 2) {
538 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
545 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
546 if (dri2_query
== NULL
|| error
!= NULL
) {
547 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
551 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
552 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
555 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
556 if (connect
== NULL
||
557 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
558 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
562 driver_name
= xcb_dri2_connect_driver_name (connect
);
563 dri2_dpy
->driver_name
=
564 dri2_x11_strndup(driver_name
,
565 xcb_dri2_connect_driver_name_length(connect
));
567 device_name
= xcb_dri2_connect_device_name (connect
);
569 dri2_dpy
->device_name
=
570 dri2_x11_strndup(device_name
,
571 xcb_dri2_connect_device_name_length(connect
));
573 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
574 free(dri2_dpy
->device_name
);
575 free(dri2_dpy
->driver_name
);
585 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
587 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
588 xcb_dri2_authenticate_reply_t
*authenticate
;
589 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
590 xcb_screen_iterator_t s
;
593 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
594 authenticate_cookie
=
595 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, id
);
597 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
599 if (authenticate
== NULL
|| !authenticate
->authenticated
)
608 dri2_x11_local_authenticate(_EGLDisplay
*disp
)
611 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
614 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
615 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
619 if (dri2_x11_authenticate(disp
, magic
) < 0) {
620 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
628 dri2_x11_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
631 xcb_screen_iterator_t s
;
632 xcb_depth_iterator_t d
;
633 xcb_visualtype_t
*visuals
;
635 unsigned int rgba_masks
[4];
637 EGLint config_attrs
[] = {
638 EGL_NATIVE_VISUAL_ID
, 0,
639 EGL_NATIVE_VISUAL_TYPE
, 0,
643 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
644 d
= xcb_screen_allowed_depths_iterator(s
.data
);
651 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
654 EGLBoolean class_added
[6] = { 0, };
656 visuals
= xcb_depth_visuals(d
.data
);
657 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
658 if (class_added
[visuals
[i
]._class
])
661 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
662 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
663 config_attrs
[1] = visuals
[i
].visual_id
;
664 config_attrs
[3] = visuals
[i
]._class
;
666 rgba_masks
[0] = visuals
[i
].red_mask
;
667 rgba_masks
[1] = visuals
[i
].green_mask
;
668 rgba_masks
[2] = visuals
[i
].blue_mask
;
670 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
671 surface_type
, config_attrs
, rgba_masks
);
673 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
674 * Otherwise it will only match a 32-bit RGBA visual. On a
675 * composited window manager on X11, this will make all of the
676 * EGLConfigs with destination alpha get blended by the
677 * compositor. This is probably not what the application
678 * wants... especially on drivers that only have 32-bit RGBA
680 if (d
.data
->depth
== 24) {
682 ~(rgba_masks
[0] | rgba_masks
[1] | rgba_masks
[2]);
683 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
684 surface_type
, config_attrs
, rgba_masks
);
692 if (!_eglGetArraySize(disp
->Configs
)) {
693 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
701 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
702 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
704 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
705 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
706 enum xcb_dri2_attachment_t render_attachment
;
707 xcb_dri2_copy_region_cookie_t cookie
;
709 /* No-op for a pixmap or pbuffer surface */
710 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
714 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
716 if (dri2_surf
->have_fake_front
)
717 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
719 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
721 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
724 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
726 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
732 dri2_x11_swap_buffers_msc(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
733 int64_t msc
, int64_t divisor
, int64_t remainder
)
735 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
736 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
737 uint32_t msc_hi
= msc
>> 32;
738 uint32_t msc_lo
= msc
& 0xffffffff;
739 uint32_t divisor_hi
= divisor
>> 32;
740 uint32_t divisor_lo
= divisor
& 0xffffffff;
741 uint32_t remainder_hi
= remainder
>> 32;
742 uint32_t remainder_lo
= remainder
& 0xffffffff;
743 xcb_dri2_swap_buffers_cookie_t cookie
;
744 xcb_dri2_swap_buffers_reply_t
*reply
;
745 int64_t swap_count
= -1;
747 /* No-op for a pixmap or pbuffer surface */
748 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
751 if (draw
->SwapBehavior
== EGL_BUFFER_PRESERVED
|| !dri2_dpy
->swap_available
)
752 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
) ? 0 : -1;
755 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
757 cookie
= xcb_dri2_swap_buffers_unchecked(dri2_dpy
->conn
, dri2_surf
->drawable
,
758 msc_hi
, msc_lo
, divisor_hi
, divisor_lo
, remainder_hi
, remainder_lo
);
760 reply
= xcb_dri2_swap_buffers_reply(dri2_dpy
->conn
, cookie
, NULL
);
763 swap_count
= (((int64_t)reply
->swap_hi
) << 32) | reply
->swap_lo
;
767 /* Since we aren't watching for the server's invalidate events like we're
768 * supposed to (due to XCB providing no mechanism for filtering the events
769 * the way xlib does), and SwapBuffers is a common cause of invalidate
770 * events, just shove one down to the driver, even though we haven't told
771 * the driver that we're the kind of loader that provides reliable
772 * invalidate events. This causes the driver to request buffers again at
773 * its next draw, so that we get the correct buffers if a pageflip
774 * happened. The driver should still be using the viewport hack to catch
777 if (dri2_dpy
->flush
&&
778 dri2_dpy
->flush
->base
.version
>= 3 && dri2_dpy
->flush
->invalidate
)
779 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
785 dri2_x11_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
787 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
788 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
790 if (dri2_dpy
->dri2
) {
791 return dri2_x11_swap_buffers_msc(drv
, disp
, draw
, 0, 0, 0) != -1;
793 assert(dri2_dpy
->swrast
);
795 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
801 dri2_x11_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
803 EGLint numRects
, const EGLint
*rects
)
805 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
806 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
808 xcb_xfixes_region_t region
;
809 xcb_rectangle_t rectangles
[16];
812 if (numRects
> (int)ARRAY_SIZE(rectangles
))
813 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
815 for (i
= 0; i
< numRects
; i
++) {
816 rectangles
[i
].x
= rects
[i
* 4];
817 rectangles
[i
].y
= dri2_surf
->base
.Height
- rects
[i
* 4 + 1] - rects
[i
* 4 + 3];
818 rectangles
[i
].width
= rects
[i
* 4 + 2];
819 rectangles
[i
].height
= rects
[i
* 4 + 3];
822 region
= xcb_generate_id(dri2_dpy
->conn
);
823 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
824 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
825 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
831 dri2_x11_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
832 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
834 const EGLint rect
[4] = { x
, y
, width
, height
};
836 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
837 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
839 return dri2_x11_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
843 dri2_x11_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
846 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
847 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
849 if (interval
> surf
->Config
->MaxSwapInterval
)
850 interval
= surf
->Config
->MaxSwapInterval
;
851 else if (interval
< surf
->Config
->MinSwapInterval
)
852 interval
= surf
->Config
->MinSwapInterval
;
854 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
855 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
857 surf
->SwapInterval
= interval
;
863 dri2_x11_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
864 EGLNativePixmapType native_target
)
866 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
867 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
869 xcb_pixmap_t target
= (uintptr_t )native_target
;
873 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
875 gc
= xcb_generate_id(dri2_dpy
->conn
);
876 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
877 xcb_copy_area(dri2_dpy
->conn
,
883 dri2_surf
->base
.Width
,
884 dri2_surf
->base
.Height
);
885 xcb_free_gc(dri2_dpy
->conn
, gc
);
891 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
892 EGLClientBuffer buffer
, const EGLint
*attr_list
)
894 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
895 struct dri2_egl_image
*dri2_img
;
896 unsigned int attachments
[1];
897 xcb_drawable_t drawable
;
898 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
899 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
900 xcb_dri2_dri2_buffer_t
*buffers
;
901 xcb_get_geometry_cookie_t geometry_cookie
;
902 xcb_get_geometry_reply_t
*geometry_reply
;
903 xcb_generic_error_t
*error
;
908 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
909 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
910 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
912 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
913 drawable
, 1, 1, attachments
);
914 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
915 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
916 buffers_cookie
, NULL
);
917 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
918 if (buffers
== NULL
) {
922 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
923 geometry_cookie
, &error
);
924 if (geometry_reply
== NULL
|| error
!= NULL
) {
925 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
931 switch (geometry_reply
->depth
) {
933 format
= __DRI_IMAGE_FORMAT_RGB565
;
936 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
939 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
942 _eglError(EGL_BAD_PARAMETER
,
943 "dri2_create_image_khr: unsupported pixmap depth");
945 free(geometry_reply
);
949 dri2_img
= malloc(sizeof *dri2_img
);
952 free(geometry_reply
);
953 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
954 return EGL_NO_IMAGE_KHR
;
957 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
959 free(geometry_reply
);
961 return EGL_NO_IMAGE_KHR
;
964 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
965 dri2_img
->dri_image
=
966 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
967 buffers_reply
->width
,
968 buffers_reply
->height
,
975 free(geometry_reply
);
977 return &dri2_img
->base
;
981 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
982 _EGLContext
*ctx
, EGLenum target
,
983 EGLClientBuffer buffer
, const EGLint
*attr_list
)
988 case EGL_NATIVE_PIXMAP_KHR
:
989 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
991 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
995 static struct dri2_egl_display_vtbl dri2_x11_swrast_display_vtbl
= {
996 .authenticate
= NULL
,
997 .create_window_surface
= dri2_x11_create_window_surface
,
998 .create_pixmap_surface
= dri2_x11_create_pixmap_surface
,
999 .create_pbuffer_surface
= dri2_x11_create_pbuffer_surface
,
1000 .destroy_surface
= dri2_x11_destroy_surface
,
1001 .swap_interval
= dri2_fallback_swap_interval
,
1002 .swap_buffers
= dri2_x11_swap_buffers
,
1003 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1004 .copy_buffers
= dri2_x11_copy_buffers
,
1005 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1008 static struct dri2_egl_display_vtbl dri2_x11_display_vtbl
= {
1009 .authenticate
= dri2_x11_authenticate
,
1010 .create_window_surface
= dri2_x11_create_window_surface
,
1011 .create_pixmap_surface
= dri2_x11_create_pixmap_surface
,
1012 .create_pbuffer_surface
= dri2_x11_create_pbuffer_surface
,
1013 .destroy_surface
= dri2_x11_destroy_surface
,
1014 .swap_interval
= dri2_x11_swap_interval
,
1015 .swap_buffers
= dri2_x11_swap_buffers
,
1016 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
1017 .swap_buffers_region
= dri2_x11_swap_buffers_region
,
1018 .copy_buffers
= dri2_x11_copy_buffers
,
1019 .query_buffer_age
= dri2_fallback_query_buffer_age
,
1023 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1025 struct dri2_egl_display
*dri2_dpy
;
1027 drv
->API
.CreateImageKHR
= NULL
;
1028 drv
->API
.DestroyImageKHR
= NULL
;
1029 drv
->API
.CreateDRMImageMESA
= NULL
;
1030 drv
->API
.ExportDRMImageMESA
= NULL
;
1032 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1034 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1036 disp
->DriverData
= (void *) dri2_dpy
;
1037 if (disp
->PlatformDisplay
== NULL
) {
1038 dri2_dpy
->conn
= xcb_connect(0, 0);
1040 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1043 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1044 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1048 if (!dri2_load_driver_swrast(disp
))
1051 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1052 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
1053 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
1054 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
1055 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
1057 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1058 dri2_dpy
->extensions
[1] = NULL
;
1059 dri2_dpy
->extensions
[2] = NULL
;
1061 if (!dri2_create_screen(disp
))
1062 goto cleanup_driver
;
1064 if (dri2_dpy
->conn
) {
1065 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
))
1066 goto cleanup_configs
;
1069 /* we're supporting EGL 1.4 */
1070 disp
->VersionMajor
= 1;
1071 disp
->VersionMinor
= 4;
1073 /* Fill vtbl last to prevent accidentally calling virtual function during
1076 dri2_dpy
->vtbl
= &dri2_x11_swrast_display_vtbl
;
1081 _eglCleanupDisplay(disp
);
1082 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1084 dlclose(dri2_dpy
->driver
);
1086 if (disp
->PlatformDisplay
== NULL
)
1087 xcb_disconnect(dri2_dpy
->conn
);
1095 dri2_x11_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1097 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1098 int arbitrary_max_interval
= 1000;
1100 /* default behavior for no SwapBuffers support: no vblank syncing
1103 dri2_dpy
->min_swap_interval
= 0;
1104 dri2_dpy
->max_swap_interval
= 0;
1106 if (!dri2_dpy
->swap_available
)
1109 /* If we do have swapbuffers, then we can support pretty much any swap
1110 * interval, but we allow driconf to override applications.
1112 if (dri2_dpy
->config
)
1113 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1114 "vblank_mode", &vblank_mode
);
1115 switch (vblank_mode
) {
1116 case DRI_CONF_VBLANK_NEVER
:
1117 dri2_dpy
->min_swap_interval
= 0;
1118 dri2_dpy
->max_swap_interval
= 0;
1119 dri2_dpy
->default_swap_interval
= 0;
1121 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1122 dri2_dpy
->min_swap_interval
= 1;
1123 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1124 dri2_dpy
->default_swap_interval
= 1;
1126 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1127 dri2_dpy
->min_swap_interval
= 0;
1128 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1129 dri2_dpy
->default_swap_interval
= 0;
1132 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1133 dri2_dpy
->min_swap_interval
= 0;
1134 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1135 dri2_dpy
->default_swap_interval
= 1;
1141 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1143 struct dri2_egl_display
*dri2_dpy
;
1145 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
1146 drv
->API
.PostSubBufferNV
= dri2_x11_post_sub_buffer
;
1148 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1150 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1152 disp
->DriverData
= (void *) dri2_dpy
;
1153 if (disp
->PlatformDisplay
== NULL
) {
1154 dri2_dpy
->conn
= xcb_connect(0, 0);
1156 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1159 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1160 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1164 if (dri2_dpy
->conn
) {
1165 if (!dri2_x11_connect(dri2_dpy
))
1169 if (!dri2_load_driver(disp
))
1173 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
1174 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
1177 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
1178 if (dri2_dpy
->fd
!= -1)
1179 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
1182 if (dri2_dpy
->fd
== -1) {
1183 _eglLog(_EGL_WARNING
,
1184 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
1186 goto cleanup_driver
;
1189 if (dri2_dpy
->conn
) {
1190 if (!dri2_x11_local_authenticate(disp
))
1194 if (dri2_dpy
->dri2_minor
>= 1) {
1195 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1196 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1197 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_x11_get_buffers
;
1198 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_x11_flush_front_buffer
;
1199 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1200 dri2_x11_get_buffers_with_format
;
1202 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1203 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
1204 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_x11_get_buffers
;
1205 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_x11_flush_front_buffer
;
1206 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
1209 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1210 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1211 dri2_dpy
->extensions
[2] = NULL
;
1213 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1214 dri2_dpy
->invalidate_available
= (dri2_dpy
->dri2_minor
>= 3);
1216 if (!dri2_create_screen(disp
))
1219 dri2_x11_setup_swap_interval(dri2_dpy
);
1221 if (dri2_dpy
->conn
) {
1222 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
))
1223 goto cleanup_configs
;
1226 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1227 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1228 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1229 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1231 #ifdef HAVE_WAYLAND_PLATFORM
1232 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1235 if (dri2_dpy
->conn
) {
1236 if (!dri2_x11_add_configs_for_visuals(dri2_dpy
, disp
))
1237 goto cleanup_configs
;
1240 /* we're supporting EGL 1.4 */
1241 disp
->VersionMajor
= 1;
1242 disp
->VersionMinor
= 4;
1244 /* Fill vtbl last to prevent accidentally calling virtual function during
1247 dri2_dpy
->vtbl
= &dri2_x11_display_vtbl
;
1252 _eglCleanupDisplay(disp
);
1253 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1255 close(dri2_dpy
->fd
);
1257 dlclose(dri2_dpy
->driver
);
1259 if (disp
->PlatformDisplay
== NULL
)
1260 xcb_disconnect(dri2_dpy
->conn
);
1268 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1270 EGLBoolean initialized
= EGL_TRUE
;
1272 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1274 if (x11_dri2_accel
) {
1275 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1276 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1279 initialized
= dri2_initialize_x11_swrast(drv
, disp
);