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>
45 swrastCreateDrawable(struct dri2_egl_display
* dri2_dpy
,
46 struct dri2_egl_surface
* dri2_surf
,
50 const uint32_t function
= GXcopy
;
54 dri2_surf
->gc
= xcb_generate_id(dri2_dpy
->conn
);
55 mask
= XCB_GC_FUNCTION
;
56 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->gc
, dri2_surf
->drawable
, mask
, &function
);
58 dri2_surf
->swapgc
= xcb_generate_id(dri2_dpy
->conn
);
59 mask
= XCB_GC_FUNCTION
| XCB_GC_GRAPHICS_EXPOSURES
;
62 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
, dri2_surf
->drawable
, mask
, valgc
);
63 dri2_surf
->depth
= depth
;
67 dri2_surf
->bytes_per_pixel
= 4;
70 dri2_surf
->bytes_per_pixel
= 2;
73 dri2_surf
->bytes_per_pixel
= 1;
76 dri2_surf
->bytes_per_pixel
= 0;
79 _eglLog(_EGL_WARNING
, "unsupported depth %d", depth
);
84 swrastDestroyDrawable(struct dri2_egl_display
* dri2_dpy
,
85 struct dri2_egl_surface
* dri2_surf
)
87 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->gc
);
88 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
);
92 swrastGetDrawableInfo(__DRIdrawable
* draw
,
93 int *x
, int *y
, int *w
, int *h
,
96 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
97 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
99 xcb_get_geometry_cookie_t cookie
;
100 xcb_get_geometry_reply_t
*reply
;
101 xcb_generic_error_t
*error
;
104 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
105 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
110 _eglLog(_EGL_WARNING
, "error in xcb_get_geometry");
120 swrastPutImage(__DRIdrawable
* draw
, int op
,
121 int x
, int y
, int w
, int h
,
122 char *data
, void *loaderPrivate
)
124 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
125 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
130 case __DRI_SWRAST_IMAGE_OP_DRAW
:
133 case __DRI_SWRAST_IMAGE_OP_SWAP
:
134 gc
= dri2_surf
->swapgc
;
140 xcb_put_image(dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, dri2_surf
->drawable
,
141 gc
, w
, h
, x
, y
, 0, dri2_surf
->depth
,
142 w
*h
*dri2_surf
->bytes_per_pixel
, (const uint8_t *)data
);
146 swrastGetImage(__DRIdrawable
* read
,
147 int x
, int y
, int w
, int h
,
148 char *data
, void *loaderPrivate
)
150 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
151 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
153 xcb_get_image_cookie_t cookie
;
154 xcb_get_image_reply_t
*reply
;
155 xcb_generic_error_t
*error
;
157 cookie
= xcb_get_image (dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
,
158 dri2_surf
->drawable
, x
, y
, w
, h
, ~0);
159 reply
= xcb_get_image_reply (dri2_dpy
->conn
, cookie
, &error
);
164 _eglLog(_EGL_WARNING
, "error in xcb_get_image");
167 uint32_t bytes
= xcb_get_image_data_length(reply
);
168 uint8_t *idata
= xcb_get_image_data(reply
);
169 memcpy(data
, idata
, bytes
);
176 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
179 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
180 _EGLConfig
*conf
, EGLNativeWindowType native_window
,
181 const EGLint
*attrib_list
)
183 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
184 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
185 struct dri2_egl_surface
*dri2_surf
;
186 xcb_get_geometry_cookie_t cookie
;
187 xcb_get_geometry_reply_t
*reply
;
188 xcb_screen_iterator_t s
;
189 xcb_generic_error_t
*error
;
190 xcb_drawable_t window
= (uintptr_t )native_window
;
194 dri2_surf
= malloc(sizeof *dri2_surf
);
196 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
200 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
203 dri2_surf
->region
= XCB_NONE
;
204 if (type
== EGL_PBUFFER_BIT
) {
205 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
206 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
207 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
208 dri2_surf
->drawable
, s
.data
->root
,
209 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
211 dri2_surf
->drawable
= window
;
214 if (dri2_dpy
->dri2
) {
215 dri2_surf
->dri_drawable
=
216 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
217 type
== EGL_WINDOW_BIT
?
218 dri2_conf
->dri_double_config
:
219 dri2_conf
->dri_single_config
,
222 assert(dri2_dpy
->swrast
);
223 dri2_surf
->dri_drawable
=
224 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
225 dri2_conf
->dri_double_config
,
229 if (dri2_surf
->dri_drawable
== NULL
) {
230 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
234 if (dri2_dpy
->dri2
) {
235 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
237 swrastCreateDrawable(dri2_dpy
, dri2_surf
, _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
));
240 if (type
!= EGL_PBUFFER_BIT
) {
241 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
242 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
243 if (reply
== NULL
|| error
!= NULL
) {
244 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
246 goto cleanup_dri_drawable
;
249 dri2_surf
->base
.Width
= reply
->width
;
250 dri2_surf
->base
.Height
= reply
->height
;
254 /* we always copy the back buffer to front */
255 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
257 return &dri2_surf
->base
;
259 cleanup_dri_drawable
:
260 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
262 if (type
== EGL_PBUFFER_BIT
)
263 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
271 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
274 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
275 _EGLConfig
*conf
, EGLNativeWindowType window
,
276 const EGLint
*attrib_list
)
278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
281 surf
= dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
282 window
, attrib_list
);
284 /* When we first create the DRI2 drawable, its swap interval on the
287 surf
->SwapInterval
= 1;
289 /* Override that with a driconf-set value. */
290 drv
->API
.SwapInterval(drv
, disp
, surf
, dri2_dpy
->default_swap_interval
);
297 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
298 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
299 const EGLint
*attrib_list
)
301 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
302 pixmap
, attrib_list
);
306 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
307 _EGLConfig
*conf
, const EGLint
*attrib_list
)
309 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
310 XCB_WINDOW_NONE
, attrib_list
);
314 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
316 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
317 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
321 if (!_eglPutSurface(surf
))
324 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
326 if (dri2_dpy
->dri2
) {
327 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
329 assert(dri2_dpy
->swrast
);
330 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
333 if (surf
->Type
== EGL_PBUFFER_BIT
)
334 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
342 * Process list of buffer received from the server
344 * Processes the list of buffers received in a reply from the server to either
345 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
348 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
349 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
351 struct dri2_egl_display
*dri2_dpy
=
352 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
353 xcb_rectangle_t rectangle
;
356 dri2_surf
->buffer_count
= count
;
357 dri2_surf
->have_fake_front
= 0;
359 /* This assumes the DRI2 buffer attachment tokens matches the
360 * __DRIbuffer tokens. */
361 for (i
= 0; i
< count
; i
++) {
362 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
363 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
364 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
365 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
366 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
368 /* We only use the DRI drivers single buffer configs. This
369 * means that if we try to render to a window, DRI2 will give us
370 * the fake front buffer, which we'll use as a back buffer.
371 * Note that EGL doesn't require that several clients rendering
372 * to the same window must see the same aux buffers. */
373 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
374 dri2_surf
->have_fake_front
= 1;
377 if (dri2_surf
->region
!= XCB_NONE
)
378 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
382 rectangle
.width
= dri2_surf
->base
.Width
;
383 rectangle
.height
= dri2_surf
->base
.Height
;
384 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
385 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
389 dri2_get_buffers(__DRIdrawable
* driDrawable
,
390 int *width
, int *height
,
391 unsigned int *attachments
, int count
,
392 int *out_count
, void *loaderPrivate
)
394 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
395 struct dri2_egl_display
*dri2_dpy
=
396 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
397 xcb_dri2_dri2_buffer_t
*buffers
;
398 xcb_dri2_get_buffers_reply_t
*reply
;
399 xcb_dri2_get_buffers_cookie_t cookie
;
403 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
405 count
, count
, attachments
);
406 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
407 buffers
= xcb_dri2_get_buffers_buffers (reply
);
411 *out_count
= reply
->count
;
412 dri2_surf
->base
.Width
= *width
= reply
->width
;
413 dri2_surf
->base
.Height
= *height
= reply
->height
;
414 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
418 return dri2_surf
->buffers
;
422 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
423 int *width
, int *height
,
424 unsigned int *attachments
, int count
,
425 int *out_count
, void *loaderPrivate
)
427 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
428 struct dri2_egl_display
*dri2_dpy
=
429 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
430 xcb_dri2_dri2_buffer_t
*buffers
;
431 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
432 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
433 xcb_dri2_attach_format_t
*format_attachments
;
437 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
438 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
443 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
448 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
449 dri2_surf
->base
.Width
= *width
= reply
->width
;
450 dri2_surf
->base
.Height
= *height
= reply
->height
;
451 *out_count
= reply
->count
;
452 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
456 return dri2_surf
->buffers
;
460 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
464 /* FIXME: Does EGL support front buffer rendering at all? */
467 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
469 dri2WaitGL(dri2_surf
);
471 (void) loaderPrivate
;
476 dri2_strndup(const char *s
, int length
)
480 d
= malloc(length
+ 1);
484 memcpy(d
, s
, length
);
491 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
493 xcb_xfixes_query_version_reply_t
*xfixes_query
;
494 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
495 xcb_dri2_query_version_reply_t
*dri2_query
;
496 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
497 xcb_dri2_connect_reply_t
*connect
;
498 xcb_dri2_connect_cookie_t connect_cookie
;
499 xcb_generic_error_t
*error
;
500 xcb_screen_iterator_t s
;
501 char *driver_name
, *device_name
;
502 const xcb_query_extension_reply_t
*extension
;
504 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
505 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
507 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
508 if (!(extension
&& extension
->present
))
511 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
512 if (!(extension
&& extension
->present
))
515 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
516 XCB_XFIXES_MAJOR_VERSION
,
517 XCB_XFIXES_MINOR_VERSION
);
519 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
520 XCB_DRI2_MAJOR_VERSION
,
521 XCB_DRI2_MINOR_VERSION
);
523 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
524 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
526 XCB_DRI2_DRIVER_TYPE_DRI
);
529 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
530 xfixes_query_cookie
, &error
);
531 if (xfixes_query
== NULL
||
532 error
!= NULL
|| xfixes_query
->major_version
< 2) {
533 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
540 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
541 if (dri2_query
== NULL
|| error
!= NULL
) {
542 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
546 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
547 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
550 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
551 if (connect
== NULL
||
552 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
553 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
557 driver_name
= xcb_dri2_connect_driver_name (connect
);
558 dri2_dpy
->driver_name
=
559 dri2_strndup(driver_name
,
560 xcb_dri2_connect_driver_name_length (connect
));
562 device_name
= xcb_dri2_connect_device_name (connect
);
564 dri2_dpy
->device_name
=
565 dri2_strndup(device_name
,
566 xcb_dri2_connect_device_name_length (connect
));
568 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
569 free(dri2_dpy
->device_name
);
570 free(dri2_dpy
->driver_name
);
580 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
582 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
583 xcb_dri2_authenticate_reply_t
*authenticate
;
584 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
585 xcb_screen_iterator_t s
;
588 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
589 authenticate_cookie
=
590 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, id
);
592 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
594 if (authenticate
== NULL
|| !authenticate
->authenticated
)
603 dri2_authenticate(_EGLDisplay
*disp
)
606 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
609 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
610 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
614 if (dri2_x11_authenticate(disp
, magic
) < 0) {
615 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
623 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
626 xcb_screen_iterator_t s
;
627 xcb_depth_iterator_t d
;
628 xcb_visualtype_t
*visuals
;
630 unsigned int rgba_masks
[4];
632 EGLint config_attrs
[] = {
633 EGL_NATIVE_VISUAL_ID
, 0,
634 EGL_NATIVE_VISUAL_TYPE
, 0,
638 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
639 d
= xcb_screen_allowed_depths_iterator(s
.data
);
646 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
649 EGLBoolean class_added
[6] = { 0, };
651 visuals
= xcb_depth_visuals(d
.data
);
652 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
653 if (class_added
[visuals
[i
]._class
])
656 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
657 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
658 config_attrs
[1] = visuals
[i
].visual_id
;
659 config_attrs
[3] = visuals
[i
]._class
;
661 rgba_masks
[0] = visuals
[i
].red_mask
;
662 rgba_masks
[1] = visuals
[i
].green_mask
;
663 rgba_masks
[2] = visuals
[i
].blue_mask
;
665 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
666 surface_type
, config_attrs
, rgba_masks
);
668 /* Allow a 24-bit RGB visual to match a 32-bit RGBA EGLConfig.
669 * Otherwise it will only match a 32-bit RGBA visual. On a
670 * composited window manager on X11, this will make all of the
671 * EGLConfigs with destination alpha get blended by the
672 * compositor. This is probably not what the application
673 * wants... especially on drivers that only have 32-bit RGBA
675 if (d
.data
->depth
== 24) {
677 ~(rgba_masks
[0] | rgba_masks
[1] | rgba_masks
[2]);
678 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
679 surface_type
, config_attrs
, rgba_masks
);
687 if (!_eglGetArraySize(disp
->Configs
)) {
688 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
696 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
697 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
699 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
700 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
701 enum xcb_dri2_attachment_t render_attachment
;
702 xcb_dri2_copy_region_cookie_t cookie
;
704 /* No-op for a pixmap or pbuffer surface */
705 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
709 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
711 if (dri2_surf
->have_fake_front
)
712 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
714 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
716 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
719 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
721 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
727 dri2_swap_buffers_msc(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
728 int64_t msc
, int64_t divisor
, int64_t remainder
)
730 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
731 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
732 uint32_t msc_hi
= msc
>> 32;
733 uint32_t msc_lo
= msc
& 0xffffffff;
734 uint32_t divisor_hi
= divisor
>> 32;
735 uint32_t divisor_lo
= divisor
& 0xffffffff;
736 uint32_t remainder_hi
= remainder
>> 32;
737 uint32_t remainder_lo
= remainder
& 0xffffffff;
738 xcb_dri2_swap_buffers_cookie_t cookie
;
739 xcb_dri2_swap_buffers_reply_t
*reply
;
740 int64_t swap_count
= -1;
742 /* No-op for a pixmap or pbuffer surface */
743 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
746 if (draw
->SwapBehavior
== EGL_BUFFER_PRESERVED
|| !dri2_dpy
->swap_available
)
747 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
) ? 0 : -1;
750 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
752 cookie
= xcb_dri2_swap_buffers_unchecked(dri2_dpy
->conn
, dri2_surf
->drawable
,
753 msc_hi
, msc_lo
, divisor_hi
, divisor_lo
, remainder_hi
, remainder_lo
);
755 reply
= xcb_dri2_swap_buffers_reply(dri2_dpy
->conn
, cookie
, NULL
);
758 swap_count
= (((int64_t)reply
->swap_hi
) << 32) | reply
->swap_lo
;
762 /* Since we aren't watching for the server's invalidate events like we're
763 * supposed to (due to XCB providing no mechanism for filtering the events
764 * the way xlib does), and SwapBuffers is a common cause of invalidate
765 * events, just shove one down to the driver, even though we haven't told
766 * the driver that we're the kind of loader that provides reliable
767 * invalidate events. This causes the driver to request buffers again at
768 * its next draw, so that we get the correct buffers if a pageflip
769 * happened. The driver should still be using the viewport hack to catch
772 if (dri2_dpy
->flush
&&
773 dri2_dpy
->flush
->base
.version
>= 3 && dri2_dpy
->flush
->invalidate
)
774 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
780 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
782 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
783 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
785 if (dri2_dpy
->dri2
) {
786 return dri2_swap_buffers_msc(drv
, disp
, draw
, 0, 0, 0) != -1;
788 assert(dri2_dpy
->swrast
);
790 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
796 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
797 EGLint numRects
, const EGLint
*rects
)
799 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
800 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
802 xcb_xfixes_region_t region
;
803 xcb_rectangle_t rectangles
[16];
806 if (numRects
> (int)ARRAY_SIZE(rectangles
))
807 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
809 for (i
= 0; i
< numRects
; i
++) {
810 rectangles
[i
].x
= rects
[i
* 4];
811 rectangles
[i
].y
= dri2_surf
->base
.Height
- rects
[i
* 4 + 1] - rects
[i
* 4 + 3];
812 rectangles
[i
].width
= rects
[i
* 4 + 2];
813 rectangles
[i
].height
= rects
[i
* 4 + 3];
816 region
= xcb_generate_id(dri2_dpy
->conn
);
817 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
818 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
819 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
825 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
826 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
828 const EGLint rect
[4] = { x
, y
, width
, height
};
830 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
831 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
833 return dri2_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
837 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint interval
)
839 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
840 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
842 if (interval
> surf
->Config
->MaxSwapInterval
)
843 interval
= surf
->Config
->MaxSwapInterval
;
844 else if (interval
< surf
->Config
->MinSwapInterval
)
845 interval
= surf
->Config
->MinSwapInterval
;
847 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
848 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
850 surf
->SwapInterval
= interval
;
856 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
857 EGLNativePixmapType native_target
)
859 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
860 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
862 xcb_pixmap_t target
= (uintptr_t )native_target
;
866 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
868 gc
= xcb_generate_id(dri2_dpy
->conn
);
869 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
870 xcb_copy_area(dri2_dpy
->conn
,
876 dri2_surf
->base
.Width
,
877 dri2_surf
->base
.Height
);
878 xcb_free_gc(dri2_dpy
->conn
, gc
);
884 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
885 EGLClientBuffer buffer
, const EGLint
*attr_list
)
887 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
888 struct dri2_egl_image
*dri2_img
;
889 unsigned int attachments
[1];
890 xcb_drawable_t drawable
;
891 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
892 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
893 xcb_dri2_dri2_buffer_t
*buffers
;
894 xcb_get_geometry_cookie_t geometry_cookie
;
895 xcb_get_geometry_reply_t
*geometry_reply
;
896 xcb_generic_error_t
*error
;
901 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
902 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
903 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
905 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
906 drawable
, 1, 1, attachments
);
907 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
908 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
909 buffers_cookie
, NULL
);
910 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
911 if (buffers
== NULL
) {
915 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
916 geometry_cookie
, &error
);
917 if (geometry_reply
== NULL
|| error
!= NULL
) {
918 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
924 switch (geometry_reply
->depth
) {
926 format
= __DRI_IMAGE_FORMAT_RGB565
;
929 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
932 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
935 _eglError(EGL_BAD_PARAMETER
,
936 "dri2_create_image_khr: unsupported pixmap depth");
938 free(geometry_reply
);
942 dri2_img
= malloc(sizeof *dri2_img
);
945 free(geometry_reply
);
946 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
947 return EGL_NO_IMAGE_KHR
;
950 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
952 free(geometry_reply
);
954 return EGL_NO_IMAGE_KHR
;
957 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
958 dri2_img
->dri_image
=
959 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
960 buffers_reply
->width
,
961 buffers_reply
->height
,
968 free(geometry_reply
);
970 return &dri2_img
->base
;
974 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
975 _EGLContext
*ctx
, EGLenum target
,
976 EGLClientBuffer buffer
, const EGLint
*attr_list
)
981 case EGL_NATIVE_PIXMAP_KHR
:
982 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
984 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
989 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
991 struct dri2_egl_display
*dri2_dpy
;
993 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
994 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
995 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
996 drv
->API
.DestroySurface
= dri2_destroy_surface
;
997 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
998 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1000 drv
->API
.SwapBuffersRegionNOK
= NULL
;
1001 drv
->API
.CreateImageKHR
= NULL
;
1002 drv
->API
.DestroyImageKHR
= NULL
;
1003 drv
->API
.CreateDRMImageMESA
= NULL
;
1004 drv
->API
.ExportDRMImageMESA
= NULL
;
1006 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1008 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1010 disp
->DriverData
= (void *) dri2_dpy
;
1011 if (disp
->PlatformDisplay
== NULL
) {
1012 dri2_dpy
->conn
= xcb_connect(0, 0);
1014 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1017 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1018 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1022 if (!dri2_load_driver_swrast(disp
))
1025 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
1026 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
1027 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
1028 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
1029 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
1031 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
1032 dri2_dpy
->extensions
[1] = NULL
;
1033 dri2_dpy
->extensions
[2] = NULL
;
1035 if (!dri2_create_screen(disp
))
1036 goto cleanup_driver
;
1038 if (dri2_dpy
->conn
) {
1039 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1040 goto cleanup_configs
;
1043 /* we're supporting EGL 1.4 */
1044 disp
->VersionMajor
= 1;
1045 disp
->VersionMinor
= 4;
1050 _eglCleanupDisplay(disp
);
1051 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1053 dlclose(dri2_dpy
->driver
);
1055 if (disp
->PlatformDisplay
== NULL
)
1056 xcb_disconnect(dri2_dpy
->conn
);
1064 dri2_setup_swap_interval(struct dri2_egl_display
*dri2_dpy
)
1066 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1067 int arbitrary_max_interval
= 1000;
1069 /* default behavior for no SwapBuffers support: no vblank syncing
1072 dri2_dpy
->min_swap_interval
= 0;
1073 dri2_dpy
->max_swap_interval
= 0;
1075 if (!dri2_dpy
->swap_available
)
1078 /* If we do have swapbuffers, then we can support pretty much any swap
1079 * interval, but we allow driconf to override applications.
1081 if (dri2_dpy
->config
)
1082 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1083 "vblank_mode", &vblank_mode
);
1084 switch (vblank_mode
) {
1085 case DRI_CONF_VBLANK_NEVER
:
1086 dri2_dpy
->min_swap_interval
= 0;
1087 dri2_dpy
->max_swap_interval
= 0;
1088 dri2_dpy
->default_swap_interval
= 0;
1090 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1091 dri2_dpy
->min_swap_interval
= 1;
1092 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1093 dri2_dpy
->default_swap_interval
= 1;
1095 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1096 dri2_dpy
->min_swap_interval
= 0;
1097 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1098 dri2_dpy
->default_swap_interval
= 0;
1101 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1102 dri2_dpy
->min_swap_interval
= 0;
1103 dri2_dpy
->max_swap_interval
= arbitrary_max_interval
;
1104 dri2_dpy
->default_swap_interval
= 1;
1110 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1112 struct dri2_egl_display
*dri2_dpy
;
1114 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
1115 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1116 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1117 drv
->API
.DestroySurface
= dri2_destroy_surface
;
1118 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
1119 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1120 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
1121 drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1122 drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
1123 drv
->API
.SwapInterval
= dri2_swap_interval
;
1125 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1127 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1129 disp
->DriverData
= (void *) dri2_dpy
;
1130 if (disp
->PlatformDisplay
== NULL
) {
1131 dri2_dpy
->conn
= xcb_connect(0, 0);
1133 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1136 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1137 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1141 if (dri2_dpy
->conn
) {
1142 if (!dri2_connect(dri2_dpy
))
1146 if (!dri2_load_driver(disp
))
1150 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
1151 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
1154 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
1155 if (dri2_dpy
->fd
!= -1)
1156 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
1159 if (dri2_dpy
->fd
== -1) {
1160 _eglLog(_EGL_WARNING
,
1161 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
1163 goto cleanup_driver
;
1166 if (dri2_dpy
->conn
) {
1167 if (!dri2_authenticate(disp
))
1171 if (dri2_dpy
->dri2_minor
>= 1) {
1172 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1173 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1174 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1175 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1176 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1177 dri2_get_buffers_with_format
;
1179 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1180 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
1181 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1182 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1183 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
1186 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1187 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1188 dri2_dpy
->extensions
[2] = NULL
;
1190 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1191 dri2_dpy
->invalidate_available
= (dri2_dpy
->dri2_minor
>= 3);
1193 if (!dri2_create_screen(disp
))
1196 dri2_setup_swap_interval(dri2_dpy
);
1198 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1199 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1200 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1201 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1203 #ifdef HAVE_WAYLAND_PLATFORM
1204 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1207 if (dri2_dpy
->conn
) {
1208 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1209 goto cleanup_configs
;
1212 dri2_dpy
->authenticate
= dri2_x11_authenticate
;
1214 /* we're supporting EGL 1.4 */
1215 disp
->VersionMajor
= 1;
1216 disp
->VersionMinor
= 4;
1221 _eglCleanupDisplay(disp
);
1222 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1224 close(dri2_dpy
->fd
);
1226 dlclose(dri2_dpy
->driver
);
1228 if (disp
->PlatformDisplay
== NULL
)
1229 xcb_disconnect(dri2_dpy
->conn
);
1237 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1239 EGLBoolean initialized
= EGL_TRUE
;
1241 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1243 if (x11_dri2_accel
) {
1244 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1245 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1248 initialized
= dri2_initialize_x11_swrast(drv
, disp
);