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>
37 #include <sys/types.h>
42 /* From xmlpool/options.h, user exposed so should be stable */
43 #define DRI_CONF_VBLANK_NEVER 0
44 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
45 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
46 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
49 swrastCreateDrawable(struct dri2_egl_display
* dri2_dpy
,
50 struct dri2_egl_surface
* dri2_surf
,
54 const uint32_t function
= GXcopy
;
58 dri2_surf
->gc
= xcb_generate_id(dri2_dpy
->conn
);
59 mask
= XCB_GC_FUNCTION
;
60 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->gc
, dri2_surf
->drawable
, mask
, &function
);
62 dri2_surf
->swapgc
= xcb_generate_id(dri2_dpy
->conn
);
63 mask
= XCB_GC_FUNCTION
| XCB_GC_GRAPHICS_EXPOSURES
;
66 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
, dri2_surf
->drawable
, mask
, valgc
);
67 dri2_surf
->depth
= depth
;
71 dri2_surf
->bytes_per_pixel
= 4;
74 dri2_surf
->bytes_per_pixel
= 2;
77 dri2_surf
->bytes_per_pixel
= 1;
80 dri2_surf
->bytes_per_pixel
= 0;
83 _eglLog(_EGL_WARNING
, "unsupported depth %d", depth
);
88 swrastDestroyDrawable(struct dri2_egl_display
* dri2_dpy
,
89 struct dri2_egl_surface
* dri2_surf
)
91 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->gc
);
92 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
);
96 swrastGetDrawableInfo(__DRIdrawable
* draw
,
97 int *x
, int *y
, int *w
, int *h
,
100 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
101 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
103 xcb_get_geometry_cookie_t cookie
;
104 xcb_get_geometry_reply_t
*reply
;
105 xcb_generic_error_t
*error
;
108 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
109 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
114 _eglLog(_EGL_WARNING
, "error in xcb_get_geometry");
124 swrastPutImage(__DRIdrawable
* draw
, int op
,
125 int x
, int y
, int w
, int h
,
126 char *data
, void *loaderPrivate
)
128 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
129 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
134 case __DRI_SWRAST_IMAGE_OP_DRAW
:
137 case __DRI_SWRAST_IMAGE_OP_SWAP
:
138 gc
= dri2_surf
->swapgc
;
144 xcb_put_image(dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, dri2_surf
->drawable
,
145 gc
, w
, h
, x
, y
, 0, dri2_surf
->depth
,
146 w
*h
*dri2_surf
->bytes_per_pixel
, (const uint8_t *)data
);
150 swrastGetImage(__DRIdrawable
* read
,
151 int x
, int y
, int w
, int h
,
152 char *data
, void *loaderPrivate
)
154 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
155 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
157 xcb_get_image_cookie_t cookie
;
158 xcb_get_image_reply_t
*reply
;
159 xcb_generic_error_t
*error
;
161 cookie
= xcb_get_image (dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
,
162 dri2_surf
->drawable
, x
, y
, w
, h
, ~0);
163 reply
= xcb_get_image_reply (dri2_dpy
->conn
, cookie
, &error
);
168 _eglLog(_EGL_WARNING
, "error in xcb_get_image");
171 uint32_t bytes
= xcb_get_image_data_length(reply
);
172 uint8_t *idata
= xcb_get_image_data(reply
);
173 memcpy(data
, idata
, bytes
);
180 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
183 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
184 _EGLConfig
*conf
, EGLNativeWindowType native_window
,
185 const EGLint
*attrib_list
)
187 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
188 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
189 struct dri2_egl_surface
*dri2_surf
;
190 xcb_get_geometry_cookie_t cookie
;
191 xcb_get_geometry_reply_t
*reply
;
192 xcb_screen_iterator_t s
;
193 xcb_generic_error_t
*error
;
194 xcb_drawable_t window
= (uintptr_t )native_window
;
198 dri2_surf
= malloc(sizeof *dri2_surf
);
200 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
204 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
207 dri2_surf
->region
= XCB_NONE
;
208 if (type
== EGL_PBUFFER_BIT
) {
209 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
210 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
211 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
212 dri2_surf
->drawable
, s
.data
->root
,
213 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
215 dri2_surf
->drawable
= window
;
218 if (dri2_dpy
->dri2
) {
219 dri2_surf
->dri_drawable
=
220 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
221 type
== EGL_WINDOW_BIT
?
222 dri2_conf
->dri_double_config
:
223 dri2_conf
->dri_single_config
,
226 assert(dri2_dpy
->swrast
);
227 dri2_surf
->dri_drawable
=
228 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
229 dri2_conf
->dri_double_config
,
233 if (dri2_surf
->dri_drawable
== NULL
) {
234 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
238 if (dri2_dpy
->dri2
) {
239 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
241 swrastCreateDrawable(dri2_dpy
, dri2_surf
, _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
));
244 if (type
!= EGL_PBUFFER_BIT
) {
245 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
246 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
247 if (reply
== NULL
|| error
!= NULL
) {
248 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
250 goto cleanup_dri_drawable
;
253 dri2_surf
->base
.Width
= reply
->width
;
254 dri2_surf
->base
.Height
= reply
->height
;
258 /* we always copy the back buffer to front */
259 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
261 return &dri2_surf
->base
;
263 cleanup_dri_drawable
:
264 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
266 if (type
== EGL_PBUFFER_BIT
)
267 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
275 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
278 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
279 _EGLConfig
*conf
, EGLNativeWindowType window
,
280 const EGLint
*attrib_list
)
282 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
285 surf
= dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
286 window
, attrib_list
);
288 /* When we first create the DRI2 drawable, its swap interval on the
291 surf
->SwapInterval
= 1;
293 /* Override that with a driconf-set value. */
294 drv
->API
.SwapInterval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
301 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
302 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
303 const EGLint
*attrib_list
)
305 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
306 pixmap
, attrib_list
);
310 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
311 _EGLConfig
*conf
, const EGLint
*attrib_list
)
313 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
314 XCB_WINDOW_NONE
, attrib_list
);
318 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
320 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
321 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
325 if (!_eglPutSurface(surf
))
328 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
330 if (dri2_dpy
->dri2
) {
331 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
333 assert(dri2_dpy
->swrast
);
334 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
337 if (surf
->Type
== EGL_PBUFFER_BIT
)
338 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
346 * Process list of buffer received from the server
348 * Processes the list of buffers received in a reply from the server to either
349 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
352 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
353 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
355 struct dri2_egl_display
*dri2_dpy
=
356 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
357 xcb_rectangle_t rectangle
;
360 dri2_surf
->buffer_count
= count
;
361 dri2_surf
->have_fake_front
= 0;
363 /* This assumes the DRI2 buffer attachment tokens matches the
364 * __DRIbuffer tokens. */
365 for (i
= 0; i
< count
; i
++) {
366 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
367 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
368 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
369 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
370 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
372 /* We only use the DRI drivers single buffer configs. This
373 * means that if we try to render to a window, DRI2 will give us
374 * the fake front buffer, which we'll use as a back buffer.
375 * Note that EGL doesn't require that several clients rendering
376 * to the same window must see the same aux buffers. */
377 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
378 dri2_surf
->have_fake_front
= 1;
381 if (dri2_surf
->region
!= XCB_NONE
)
382 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
386 rectangle
.width
= dri2_surf
->base
.Width
;
387 rectangle
.height
= dri2_surf
->base
.Height
;
388 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
389 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
393 dri2_get_buffers(__DRIdrawable
* driDrawable
,
394 int *width
, int *height
,
395 unsigned int *attachments
, int count
,
396 int *out_count
, void *loaderPrivate
)
398 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
399 struct dri2_egl_display
*dri2_dpy
=
400 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
401 xcb_dri2_dri2_buffer_t
*buffers
;
402 xcb_dri2_get_buffers_reply_t
*reply
;
403 xcb_dri2_get_buffers_cookie_t cookie
;
407 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
409 count
, count
, attachments
);
410 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
411 buffers
= xcb_dri2_get_buffers_buffers (reply
);
415 *out_count
= reply
->count
;
416 dri2_surf
->base
.Width
= *width
= reply
->width
;
417 dri2_surf
->base
.Height
= *height
= reply
->height
;
418 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
422 return dri2_surf
->buffers
;
426 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
427 int *width
, int *height
,
428 unsigned int *attachments
, int count
,
429 int *out_count
, void *loaderPrivate
)
431 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
432 struct dri2_egl_display
*dri2_dpy
=
433 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
434 xcb_dri2_dri2_buffer_t
*buffers
;
435 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
436 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
437 xcb_dri2_attach_format_t
*format_attachments
;
441 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
442 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
447 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
452 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
453 dri2_surf
->base
.Width
= *width
= reply
->width
;
454 dri2_surf
->base
.Height
= *height
= reply
->height
;
455 *out_count
= reply
->count
;
456 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
460 return dri2_surf
->buffers
;
464 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
468 /* FIXME: Does EGL support front buffer rendering at all? */
471 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
473 dri2WaitGL(dri2_surf
);
475 (void) loaderPrivate
;
480 dri2_strndup(const char *s
, int length
)
484 d
= malloc(length
+ 1);
488 memcpy(d
, s
, length
);
495 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
497 xcb_xfixes_query_version_reply_t
*xfixes_query
;
498 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
499 xcb_dri2_query_version_reply_t
*dri2_query
;
500 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
501 xcb_dri2_connect_reply_t
*connect
;
502 xcb_dri2_connect_cookie_t connect_cookie
;
503 xcb_generic_error_t
*error
;
504 xcb_screen_iterator_t s
;
505 char *driver_name
, *device_name
;
506 const xcb_query_extension_reply_t
*extension
;
508 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
509 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
511 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
512 if (!(extension
&& extension
->present
))
515 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
516 if (!(extension
&& extension
->present
))
519 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
520 XCB_XFIXES_MAJOR_VERSION
,
521 XCB_XFIXES_MINOR_VERSION
);
523 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
524 XCB_DRI2_MAJOR_VERSION
,
525 XCB_DRI2_MINOR_VERSION
);
527 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
528 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
530 XCB_DRI2_DRIVER_TYPE_DRI
);
533 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
534 xfixes_query_cookie
, &error
);
535 if (xfixes_query
== NULL
||
536 error
!= NULL
|| xfixes_query
->major_version
< 2) {
537 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
544 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
545 if (dri2_query
== NULL
|| error
!= NULL
) {
546 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
550 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
551 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
554 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
555 if (connect
== NULL
||
556 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
557 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
561 driver_name
= xcb_dri2_connect_driver_name (connect
);
562 dri2_dpy
->driver_name
=
563 dri2_strndup(driver_name
,
564 xcb_dri2_connect_driver_name_length (connect
));
566 device_name
= xcb_dri2_connect_device_name (connect
);
568 dri2_dpy
->device_name
=
569 dri2_strndup(device_name
,
570 xcb_dri2_connect_device_name_length (connect
));
572 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
573 free(dri2_dpy
->device_name
);
574 free(dri2_dpy
->driver_name
);
584 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
586 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
587 xcb_dri2_authenticate_reply_t
*authenticate
;
588 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
589 xcb_screen_iterator_t s
;
592 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
593 authenticate_cookie
=
594 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, id
);
596 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
598 if (authenticate
== NULL
|| !authenticate
->authenticated
)
607 dri2_authenticate(_EGLDisplay
*disp
)
609 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
612 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
613 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
617 if (dri2_x11_authenticate(disp
, magic
) < 0) {
618 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
626 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
629 xcb_screen_iterator_t s
;
630 xcb_depth_iterator_t d
;
631 xcb_visualtype_t
*visuals
;
633 unsigned int rgba_masks
[4];
635 EGLint config_attrs
[] = {
636 EGL_NATIVE_VISUAL_ID
, 0,
637 EGL_NATIVE_VISUAL_TYPE
, 0,
641 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
642 d
= xcb_screen_allowed_depths_iterator(s
.data
);
649 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
652 EGLBoolean class_added
[6] = { 0, };
654 visuals
= xcb_depth_visuals(d
.data
);
655 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
656 if (class_added
[visuals
[i
]._class
])
659 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
660 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
661 config_attrs
[1] = visuals
[i
].visual_id
;
662 config_attrs
[3] = visuals
[i
]._class
;
664 rgba_masks
[0] = visuals
[i
].red_mask
;
665 rgba_masks
[1] = visuals
[i
].green_mask
;
666 rgba_masks
[2] = visuals
[i
].blue_mask
;
668 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
669 surface_type
, config_attrs
, rgba_masks
);
671 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
672 * Otherwise it will only match a 32-bit RGBA visual. On a
673 * composited window manager on X11, this will make all of the
674 * EGLConfigs with destination alpha get blended by the
675 * compositor. This is probably not what the application
676 * wants... especially on drivers that only have 32-bit RGBA
678 if (d
.data
->depth
== 24) {
680 ~(rgba_masks
[0] | rgba_masks
[1] | rgba_masks
[2]);
681 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
682 surface_type
, config_attrs
, rgba_masks
);
690 if (!_eglGetArraySize(disp
->Configs
)) {
691 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
699 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
700 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
702 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
703 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
704 enum xcb_dri2_attachment_t render_attachment
;
705 xcb_dri2_copy_region_cookie_t cookie
;
707 /* No-op for a pixmap or pbuffer surface */
708 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
712 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
714 if (dri2_surf
->have_fake_front
)
715 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
717 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
719 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
722 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
724 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
730 dri2_swap_buffers_msc(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
731 int64_t msc
, int64_t divisor
, int64_t remainder
)
733 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
734 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
735 uint32_t msc_hi
= msc
>> 32;
736 uint32_t msc_lo
= msc
& 0xffffffff;
737 uint32_t divisor_hi
= divisor
>> 32;
738 uint32_t divisor_lo
= divisor
& 0xffffffff;
739 uint32_t remainder_hi
= remainder
>> 32;
740 uint32_t remainder_lo
= remainder
& 0xffffffff;
741 xcb_dri2_swap_buffers_cookie_t cookie
;
742 xcb_dri2_swap_buffers_reply_t
*reply
;
743 int64_t swap_count
= -1;
745 /* No-op for a pixmap or pbuffer surface */
746 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
749 if (draw
->SwapBehavior
== EGL_BUFFER_PRESERVED
|| !dri2_dpy
->swap_available
)
750 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
) ? 0 : -1;
753 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
755 cookie
= xcb_dri2_swap_buffers_unchecked(dri2_dpy
->conn
, dri2_surf
->drawable
,
756 msc_hi
, msc_lo
, divisor_hi
, divisor_lo
, remainder_hi
, remainder_lo
);
758 reply
= xcb_dri2_swap_buffers_reply(dri2_dpy
->conn
, cookie
, NULL
);
761 swap_count
= (((int64_t)reply
->swap_hi
) << 32) | reply
->swap_lo
;
765 /* Since we aren't watching for the server's invalidate events like we're
766 * supposed to (due to XCB providing no mechanism for filtering the events
767 * the way xlib does), and SwapBuffers is a common cause of invalidate
768 * events, just shove one down to the driver, even though we haven't told
769 * the driver that we're the kind of loader that provides reliable
770 * invalidate events. This causes the driver to request buffers again at
771 * its next draw, so that we get the correct buffers if a pageflip
772 * happened. The driver should still be using the viewport hack to catch
775 if (dri2_dpy
->flush
&&
776 dri2_dpy
->flush
->base
.version
>= 3 && dri2_dpy
->flush
->invalidate
)
777 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
783 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
785 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
786 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
788 if (dri2_dpy
->dri2
) {
789 return dri2_swap_buffers_msc(drv
, disp
, draw
, 0, 0, 0) != -1;
791 assert(dri2_dpy
->swrast
);
793 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
799 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
800 EGLint numRects
, const EGLint
*rects
)
802 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
803 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
805 xcb_xfixes_region_t region
;
806 xcb_rectangle_t rectangles
[16];
809 if (numRects
> (int)ARRAY_SIZE(rectangles
))
810 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
812 for (i
= 0; i
< numRects
; i
++) {
813 rectangles
[i
].x
= rects
[i
* 4];
814 rectangles
[i
].y
= dri2_surf
->base
.Height
- rects
[i
* 4 + 1] - rects
[i
* 4 + 3];
815 rectangles
[i
].width
= rects
[i
* 4 + 2];
816 rectangles
[i
].height
= rects
[i
* 4 + 3];
819 region
= xcb_generate_id(dri2_dpy
->conn
);
820 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
821 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
822 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
828 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
829 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
831 const EGLint rect
[4] = { x
, y
, width
, height
};
833 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
834 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
836 return dri2_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
840 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint interval
)
842 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
843 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
845 if (interval
> surf
->Config
->MaxSwapInterval
)
846 interval
= surf
->Config
->MaxSwapInterval
;
847 else if (interval
< surf
->Config
->MinSwapInterval
)
848 interval
= surf
->Config
->MinSwapInterval
;
850 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
851 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
853 surf
->SwapInterval
= interval
;
859 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
860 EGLNativePixmapType native_target
)
862 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
863 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
865 xcb_pixmap_t target
= (uintptr_t )native_target
;
869 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
871 gc
= xcb_generate_id(dri2_dpy
->conn
);
872 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
873 xcb_copy_area(dri2_dpy
->conn
,
879 dri2_surf
->base
.Width
,
880 dri2_surf
->base
.Height
);
881 xcb_free_gc(dri2_dpy
->conn
, gc
);
887 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
888 EGLClientBuffer buffer
, const EGLint
*attr_list
)
890 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
891 struct dri2_egl_image
*dri2_img
;
892 unsigned int attachments
[1];
893 xcb_drawable_t drawable
;
894 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
895 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
896 xcb_dri2_dri2_buffer_t
*buffers
;
897 xcb_get_geometry_cookie_t geometry_cookie
;
898 xcb_get_geometry_reply_t
*geometry_reply
;
899 xcb_generic_error_t
*error
;
904 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
905 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
906 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
908 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
909 drawable
, 1, 1, attachments
);
910 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
911 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
912 buffers_cookie
, NULL
);
913 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
914 if (buffers
== NULL
) {
918 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
919 geometry_cookie
, &error
);
920 if (geometry_reply
== NULL
|| error
!= NULL
) {
921 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
927 switch (geometry_reply
->depth
) {
929 format
= __DRI_IMAGE_FORMAT_RGB565
;
932 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
935 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
938 _eglError(EGL_BAD_PARAMETER
,
939 "dri2_create_image_khr: unsupported pixmap depth");
941 free(geometry_reply
);
945 dri2_img
= malloc(sizeof *dri2_img
);
948 free(geometry_reply
);
949 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
950 return EGL_NO_IMAGE_KHR
;
953 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
955 free(geometry_reply
);
957 return EGL_NO_IMAGE_KHR
;
960 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
961 dri2_img
->dri_image
=
962 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
963 buffers_reply
->width
,
964 buffers_reply
->height
,
971 free(geometry_reply
);
973 return &dri2_img
->base
;
977 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
978 _EGLContext
*ctx
, EGLenum target
,
979 EGLClientBuffer buffer
, const EGLint
*attr_list
)
984 case EGL_NATIVE_PIXMAP_KHR
:
985 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
987 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
992 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
994 struct dri2_egl_display
*dri2_dpy
;
996 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
997 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
998 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
999 drv
->API
.DestroySurface
= dri2_destroy_surface
;
1000 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
1001 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1003 drv
->API
.SwapBuffersRegionNOK
= NULL
;
1004 drv
->API
.CreateImageKHR
= NULL
;
1005 drv
->API
.DestroyImageKHR
= NULL
;
1006 drv
->API
.CreateDRMImageMESA
= NULL
;
1007 drv
->API
.ExportDRMImageMESA
= NULL
;
1009 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1011 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1013 disp
->DriverData
= (void *) dri2_dpy
;
1014 if (disp
->PlatformDisplay
== NULL
) {
1015 dri2_dpy
->conn
= xcb_connect(0, 0);
1017 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1020 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1021 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1025 if (!dri2_load_driver_swrast(disp
))
1028 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1029 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
1030 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
1031 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
1032 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
1034 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1035 dri2_dpy
->extensions
[1] = NULL
;
1036 dri2_dpy
->extensions
[2] = NULL
;
1038 if (!dri2_create_screen(disp
))
1039 goto cleanup_driver
;
1041 if (dri2_dpy
->conn
) {
1042 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1043 goto cleanup_configs
;
1046 /* we're supporting EGL 1.4 */
1047 disp
->VersionMajor
= 1;
1048 disp
->VersionMinor
= 4;
1053 _eglCleanupDisplay(disp
);
1054 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1056 dlclose(dri2_dpy
->driver
);
1058 if (disp
->PlatformDisplay
== NULL
)
1059 xcb_disconnect(dri2_dpy
->conn
);
1067 dri2_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1069 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1070 int arbitrary_max_interval
= 1000;
1072 /* default behavior for no SwapBuffers support: no vblank syncing
1075 dri2_dpy
->min_swap_interval
= 0;
1076 dri2_dpy
->max_swap_interval
= 0;
1078 if (!dri2_dpy
->swap_available
)
1081 /* If we do have swapbuffers, then we can support pretty much any swap
1082 * interval, but we allow driconf to override applications.
1084 if (dri2_dpy
->config
)
1085 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1086 "vblank_mode", &vblank_mode
);
1087 switch (vblank_mode
) {
1088 case DRI_CONF_VBLANK_NEVER
:
1089 dri2_dpy
->min_swap_interval
= 0;
1090 dri2_dpy
->max_swap_interval
= 0;
1091 dri2_dpy
->default_swap_interval
= 0;
1093 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1094 dri2_dpy
->min_swap_interval
= 1;
1095 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1096 dri2_dpy
->default_swap_interval
= 1;
1098 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1099 dri2_dpy
->min_swap_interval
= 0;
1100 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1101 dri2_dpy
->default_swap_interval
= 0;
1104 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1105 dri2_dpy
->min_swap_interval
= 0;
1106 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1107 dri2_dpy
->default_swap_interval
= 1;
1113 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1115 struct dri2_egl_display
*dri2_dpy
;
1117 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
1118 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1119 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1120 drv
->API
.DestroySurface
= dri2_destroy_surface
;
1121 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
1122 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1123 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
1124 drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1125 drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
1126 drv
->API
.SwapInterval
= dri2_swap_interval
;
1128 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1130 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1132 disp
->DriverData
= (void *) dri2_dpy
;
1133 if (disp
->PlatformDisplay
== NULL
) {
1134 dri2_dpy
->conn
= xcb_connect(0, 0);
1136 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1139 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1140 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1144 if (dri2_dpy
->conn
) {
1145 if (!dri2_connect(dri2_dpy
))
1149 if (!dri2_load_driver(disp
))
1153 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
1154 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
1157 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
1158 if (dri2_dpy
->fd
!= -1)
1159 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
1162 if (dri2_dpy
->fd
== -1) {
1163 _eglLog(_EGL_WARNING
,
1164 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
1166 goto cleanup_driver
;
1169 if (dri2_dpy
->conn
) {
1170 if (!dri2_authenticate(disp
))
1174 if (dri2_dpy
->dri2_minor
>= 1) {
1175 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1176 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1177 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1178 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1179 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1180 dri2_get_buffers_with_format
;
1182 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1183 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
1184 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1185 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1186 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
1189 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1190 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1191 dri2_dpy
->extensions
[2] = NULL
;
1193 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1194 dri2_dpy
->invalidate_available
= (dri2_dpy
->dri2_minor
>= 3);
1196 if (!dri2_create_screen(disp
))
1199 dri2_setup_swap_interval(dri2_dpy
);
1201 if (dri2_dpy
->conn
) {
1202 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1203 goto cleanup_configs
;
1206 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1207 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1208 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1209 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1211 #ifdef HAVE_WAYLAND_PLATFORM
1212 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1214 dri2_dpy
->authenticate
= dri2_x11_authenticate
;
1216 /* we're supporting EGL 1.4 */
1217 disp
->VersionMajor
= 1;
1218 disp
->VersionMinor
= 4;
1223 _eglCleanupDisplay(disp
);
1224 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1226 close(dri2_dpy
->fd
);
1228 dlclose(dri2_dpy
->driver
);
1230 if (disp
->PlatformDisplay
== NULL
)
1231 xcb_disconnect(dri2_dpy
->conn
);
1239 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1241 EGLBoolean initialized
= EGL_TRUE
;
1243 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1245 if (x11_dri2_accel
) {
1246 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1247 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1250 initialized
= dri2_initialize_x11_swrast(drv
, disp
);