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>
43 swrastCreateDrawable(struct dri2_egl_display
* dri2_dpy
,
44 struct dri2_egl_surface
* dri2_surf
,
48 const uint32_t function
= GXcopy
;
52 dri2_surf
->gc
= xcb_generate_id(dri2_dpy
->conn
);
53 mask
= XCB_GC_FUNCTION
;
54 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->gc
, dri2_surf
->drawable
, mask
, &function
);
56 dri2_surf
->swapgc
= xcb_generate_id(dri2_dpy
->conn
);
57 mask
= XCB_GC_FUNCTION
| XCB_GC_GRAPHICS_EXPOSURES
;
60 xcb_create_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
, dri2_surf
->drawable
, mask
, valgc
);
61 dri2_surf
->depth
= depth
;
65 dri2_surf
->bytes_per_pixel
= 4;
68 dri2_surf
->bytes_per_pixel
= 2;
71 dri2_surf
->bytes_per_pixel
= 1;
74 dri2_surf
->bytes_per_pixel
= 0;
77 _eglLog(_EGL_WARNING
, "unsupported depth %d", depth
);
82 swrastDestroyDrawable(struct dri2_egl_display
* dri2_dpy
,
83 struct dri2_egl_surface
* dri2_surf
)
85 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->gc
);
86 xcb_free_gc(dri2_dpy
->conn
, dri2_surf
->swapgc
);
90 swrastGetDrawableInfo(__DRIdrawable
* draw
,
91 int *x
, int *y
, int *w
, int *h
,
94 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
95 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
97 xcb_get_geometry_cookie_t cookie
;
98 xcb_get_geometry_reply_t
*reply
;
99 xcb_generic_error_t
*error
;
102 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
103 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
108 _eglLog(_EGL_WARNING
, "error in xcb_get_geometry");
118 swrastPutImage(__DRIdrawable
* draw
, int op
,
119 int x
, int y
, int w
, int h
,
120 char *data
, void *loaderPrivate
)
122 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
123 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
128 case __DRI_SWRAST_IMAGE_OP_DRAW
:
131 case __DRI_SWRAST_IMAGE_OP_SWAP
:
132 gc
= dri2_surf
->swapgc
;
138 xcb_put_image(dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
, dri2_surf
->drawable
,
139 gc
, w
, h
, x
, y
, 0, dri2_surf
->depth
,
140 w
*h
*dri2_surf
->bytes_per_pixel
, (const uint8_t *)data
);
144 swrastGetImage(__DRIdrawable
* read
,
145 int x
, int y
, int w
, int h
,
146 char *data
, void *loaderPrivate
)
148 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
149 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
151 xcb_get_image_cookie_t cookie
;
152 xcb_get_image_reply_t
*reply
;
153 xcb_generic_error_t
*error
;
155 cookie
= xcb_get_image (dri2_dpy
->conn
, XCB_IMAGE_FORMAT_Z_PIXMAP
,
156 dri2_surf
->drawable
, x
, y
, w
, h
, ~0);
157 reply
= xcb_get_image_reply (dri2_dpy
->conn
, cookie
, &error
);
162 _eglLog(_EGL_WARNING
, "error in xcb_get_image");
165 uint32_t bytes
= xcb_get_image_data_length(reply
);
166 uint8_t *idata
= xcb_get_image_data(reply
);
167 memcpy(data
, idata
, bytes
);
174 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
177 dri2_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
178 _EGLConfig
*conf
, EGLNativeWindowType native_window
,
179 const EGLint
*attrib_list
)
181 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
182 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
183 struct dri2_egl_surface
*dri2_surf
;
184 xcb_get_geometry_cookie_t cookie
;
185 xcb_get_geometry_reply_t
*reply
;
186 xcb_screen_iterator_t s
;
187 xcb_generic_error_t
*error
;
188 xcb_drawable_t window
= (uintptr_t )native_window
;
192 dri2_surf
= malloc(sizeof *dri2_surf
);
194 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
198 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
201 dri2_surf
->region
= XCB_NONE
;
202 if (type
== EGL_PBUFFER_BIT
) {
203 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
204 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
205 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
206 dri2_surf
->drawable
, s
.data
->root
,
207 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
209 dri2_surf
->drawable
= window
;
212 if (dri2_dpy
->dri2
) {
213 dri2_surf
->dri_drawable
=
214 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
215 type
== EGL_WINDOW_BIT
?
216 dri2_conf
->dri_double_config
:
217 dri2_conf
->dri_single_config
,
220 assert(dri2_dpy
->swrast
);
221 dri2_surf
->dri_drawable
=
222 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
223 dri2_conf
->dri_double_config
,
227 if (dri2_surf
->dri_drawable
== NULL
) {
228 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
232 if (dri2_dpy
->dri2
) {
233 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
235 swrastCreateDrawable(dri2_dpy
, dri2_surf
, _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
));
238 if (type
!= EGL_PBUFFER_BIT
) {
239 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
240 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
241 if (reply
== NULL
|| error
!= NULL
) {
242 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
244 goto cleanup_dri_drawable
;
247 dri2_surf
->base
.Width
= reply
->width
;
248 dri2_surf
->base
.Height
= reply
->height
;
252 /* we always copy the back buffer to front */
253 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
255 return &dri2_surf
->base
;
257 cleanup_dri_drawable
:
258 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
260 if (type
== EGL_PBUFFER_BIT
)
261 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
269 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
272 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
273 _EGLConfig
*conf
, EGLNativeWindowType window
,
274 const EGLint
*attrib_list
)
276 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
277 window
, attrib_list
);
281 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
282 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
283 const EGLint
*attrib_list
)
285 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
286 pixmap
, attrib_list
);
290 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
291 _EGLConfig
*conf
, const EGLint
*attrib_list
)
293 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
294 XCB_WINDOW_NONE
, attrib_list
);
298 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
300 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
301 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
305 if (!_eglPutSurface(surf
))
308 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
310 if (dri2_dpy
->dri2
) {
311 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
313 assert(dri2_dpy
->swrast
);
314 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
317 if (surf
->Type
== EGL_PBUFFER_BIT
)
318 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
326 * Process list of buffer received from the server
328 * Processes the list of buffers received in a reply from the server to either
329 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
332 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
333 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
335 struct dri2_egl_display
*dri2_dpy
=
336 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
337 xcb_rectangle_t rectangle
;
340 dri2_surf
->buffer_count
= count
;
341 dri2_surf
->have_fake_front
= 0;
343 /* This assumes the DRI2 buffer attachment tokens matches the
344 * __DRIbuffer tokens. */
345 for (i
= 0; i
< count
; i
++) {
346 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
347 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
348 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
349 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
350 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
352 /* We only use the DRI drivers single buffer configs. This
353 * means that if we try to render to a window, DRI2 will give us
354 * the fake front buffer, which we'll use as a back buffer.
355 * Note that EGL doesn't require that several clients rendering
356 * to the same window must see the same aux buffers. */
357 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
358 dri2_surf
->have_fake_front
= 1;
361 if (dri2_surf
->region
!= XCB_NONE
)
362 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
366 rectangle
.width
= dri2_surf
->base
.Width
;
367 rectangle
.height
= dri2_surf
->base
.Height
;
368 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
369 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
373 dri2_get_buffers(__DRIdrawable
* driDrawable
,
374 int *width
, int *height
,
375 unsigned int *attachments
, int count
,
376 int *out_count
, void *loaderPrivate
)
378 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
379 struct dri2_egl_display
*dri2_dpy
=
380 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
381 xcb_dri2_dri2_buffer_t
*buffers
;
382 xcb_dri2_get_buffers_reply_t
*reply
;
383 xcb_dri2_get_buffers_cookie_t cookie
;
387 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
389 count
, count
, attachments
);
390 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
391 buffers
= xcb_dri2_get_buffers_buffers (reply
);
395 *out_count
= reply
->count
;
396 dri2_surf
->base
.Width
= *width
= reply
->width
;
397 dri2_surf
->base
.Height
= *height
= reply
->height
;
398 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
402 return dri2_surf
->buffers
;
406 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
407 int *width
, int *height
,
408 unsigned int *attachments
, int count
,
409 int *out_count
, void *loaderPrivate
)
411 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
412 struct dri2_egl_display
*dri2_dpy
=
413 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
414 xcb_dri2_dri2_buffer_t
*buffers
;
415 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
416 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
417 xcb_dri2_attach_format_t
*format_attachments
;
421 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
422 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
427 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
432 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
433 dri2_surf
->base
.Width
= *width
= reply
->width
;
434 dri2_surf
->base
.Height
= *height
= reply
->height
;
435 *out_count
= reply
->count
;
436 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
440 return dri2_surf
->buffers
;
444 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
448 /* FIXME: Does EGL support front buffer rendering at all? */
451 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
453 dri2WaitGL(dri2_surf
);
455 (void) loaderPrivate
;
460 dri2_strndup(const char *s
, int length
)
464 d
= malloc(length
+ 1);
468 memcpy(d
, s
, length
);
475 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
477 xcb_xfixes_query_version_reply_t
*xfixes_query
;
478 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
479 xcb_dri2_query_version_reply_t
*dri2_query
;
480 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
481 xcb_dri2_connect_reply_t
*connect
;
482 xcb_dri2_connect_cookie_t connect_cookie
;
483 xcb_generic_error_t
*error
;
484 xcb_screen_iterator_t s
;
485 char *driver_name
, *device_name
;
486 const xcb_query_extension_reply_t
*extension
;
488 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
489 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
491 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
492 if (!(extension
&& extension
->present
))
495 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
496 if (!(extension
&& extension
->present
))
499 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
500 XCB_XFIXES_MAJOR_VERSION
,
501 XCB_XFIXES_MINOR_VERSION
);
503 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
504 XCB_DRI2_MAJOR_VERSION
,
505 XCB_DRI2_MINOR_VERSION
);
507 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
508 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
510 XCB_DRI2_DRIVER_TYPE_DRI
);
513 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
514 xfixes_query_cookie
, &error
);
515 if (xfixes_query
== NULL
||
516 error
!= NULL
|| xfixes_query
->major_version
< 2) {
517 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
524 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
525 if (dri2_query
== NULL
|| error
!= NULL
) {
526 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
530 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
531 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
534 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
535 if (connect
== NULL
||
536 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
537 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
541 driver_name
= xcb_dri2_connect_driver_name (connect
);
542 dri2_dpy
->driver_name
=
543 dri2_strndup(driver_name
,
544 xcb_dri2_connect_driver_name_length (connect
));
546 device_name
= xcb_dri2_connect_device_name (connect
);
548 dri2_dpy
->device_name
=
549 dri2_strndup(device_name
,
550 xcb_dri2_connect_device_name_length (connect
));
552 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
553 free(dri2_dpy
->device_name
);
554 free(dri2_dpy
->driver_name
);
564 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
566 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
567 xcb_dri2_authenticate_reply_t
*authenticate
;
568 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
569 xcb_screen_iterator_t s
;
572 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
573 authenticate_cookie
=
574 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, id
);
576 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
578 if (authenticate
== NULL
|| !authenticate
->authenticated
)
587 dri2_authenticate(_EGLDisplay
*disp
)
589 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
592 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
593 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
597 if (dri2_x11_authenticate(disp
, magic
) < 0) {
598 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
606 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
609 xcb_screen_iterator_t s
;
610 xcb_depth_iterator_t d
;
611 xcb_visualtype_t
*visuals
;
614 EGLint config_attrs
[] = {
615 EGL_NATIVE_VISUAL_ID
, 0,
616 EGL_NATIVE_VISUAL_TYPE
, 0,
620 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
621 d
= xcb_screen_allowed_depths_iterator(s
.data
);
628 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
631 EGLBoolean class_added
[6] = { 0, };
633 visuals
= xcb_depth_visuals(d
.data
);
634 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
635 if (class_added
[visuals
[i
]._class
])
638 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
639 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
640 config_attrs
[1] = visuals
[i
].visual_id
;
641 config_attrs
[3] = visuals
[i
]._class
;
643 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
644 d
.data
->depth
, surface_type
, config_attrs
, NULL
);
651 if (!_eglGetArraySize(disp
->Configs
)) {
652 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
660 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
661 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
663 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
664 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
665 enum xcb_dri2_attachment_t render_attachment
;
666 xcb_dri2_copy_region_cookie_t cookie
;
668 /* No-op for a pixmap or pbuffer surface */
669 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
673 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
675 if (dri2_surf
->have_fake_front
)
676 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
678 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
680 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
683 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
685 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
691 dri2_swap_buffers_msc(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
692 int64_t msc
, int64_t divisor
, int64_t remainder
)
694 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
695 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
696 uint32_t msc_hi
= msc
>> 32;
697 uint32_t msc_lo
= msc
& 0xffffffff;
698 uint32_t divisor_hi
= divisor
>> 32;
699 uint32_t divisor_lo
= divisor
& 0xffffffff;
700 uint32_t remainder_hi
= remainder
>> 32;
701 uint32_t remainder_lo
= remainder
& 0xffffffff;
702 xcb_dri2_swap_buffers_cookie_t cookie
;
703 xcb_dri2_swap_buffers_reply_t
*reply
;
704 int64_t swap_count
= -1;
706 /* No-op for a pixmap or pbuffer surface */
707 if (draw
->Type
== EGL_PIXMAP_BIT
|| draw
->Type
== EGL_PBUFFER_BIT
)
710 if (draw
->SwapBehavior
== EGL_BUFFER_PRESERVED
|| !dri2_dpy
->swap_available
)
711 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
) ? 0 : -1;
714 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
716 cookie
= xcb_dri2_swap_buffers_unchecked(dri2_dpy
->conn
, dri2_surf
->drawable
,
717 msc_hi
, msc_lo
, divisor_hi
, divisor_lo
, remainder_hi
, remainder_lo
);
719 reply
= xcb_dri2_swap_buffers_reply(dri2_dpy
->conn
, cookie
, NULL
);
722 swap_count
= (((int64_t)reply
->swap_hi
) << 32) | reply
->swap_lo
;
726 /* If the server doesn't send invalidate events */
727 if (dri2_dpy
->invalidate_available
&& dri2_dpy
->flush
&&
728 dri2_dpy
->flush
->base
.version
>= 3 && dri2_dpy
->flush
->invalidate
)
729 (*dri2_dpy
->flush
->invalidate
)(dri2_surf
->dri_drawable
);
735 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
737 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
738 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
740 if (dri2_dpy
->dri2
) {
741 return dri2_swap_buffers_msc(drv
, disp
, draw
, 0, 0, 0) != -1;
743 assert(dri2_dpy
->swrast
);
745 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
751 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
752 EGLint numRects
, const EGLint
*rects
)
754 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
755 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
757 xcb_xfixes_region_t region
;
758 xcb_rectangle_t rectangles
[16];
761 if (numRects
> (int)ARRAY_SIZE(rectangles
))
762 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
764 for (i
= 0; i
< numRects
; i
++) {
765 rectangles
[i
].x
= rects
[i
* 4];
766 rectangles
[i
].y
= dri2_surf
->base
.Height
- rects
[i
* 4 + 1] - rects
[i
* 4 + 3];
767 rectangles
[i
].width
= rects
[i
* 4 + 2];
768 rectangles
[i
].height
= rects
[i
* 4 + 3];
771 region
= xcb_generate_id(dri2_dpy
->conn
);
772 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
773 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
774 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
780 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
781 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
783 const EGLint rect
[4] = { x
, draw
->Height
- y
- height
, width
, height
};
785 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
786 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
788 return dri2_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
792 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint interval
)
794 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
795 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
797 /* XXX Check vblank_mode here? */
799 if (interval
> surf
->Config
->MaxSwapInterval
)
800 interval
= surf
->Config
->MaxSwapInterval
;
801 else if (interval
< surf
->Config
->MinSwapInterval
)
802 interval
= surf
->Config
->MinSwapInterval
;
804 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
805 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
807 surf
->SwapInterval
= interval
;
813 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
814 EGLNativePixmapType native_target
)
816 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
817 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
819 xcb_pixmap_t target
= (uintptr_t )native_target
;
823 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
825 gc
= xcb_generate_id(dri2_dpy
->conn
);
826 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
827 xcb_copy_area(dri2_dpy
->conn
,
833 dri2_surf
->base
.Width
,
834 dri2_surf
->base
.Height
);
835 xcb_free_gc(dri2_dpy
->conn
, gc
);
841 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
842 EGLClientBuffer buffer
, const EGLint
*attr_list
)
844 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
845 struct dri2_egl_image
*dri2_img
;
846 unsigned int attachments
[1];
847 xcb_drawable_t drawable
;
848 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
849 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
850 xcb_dri2_dri2_buffer_t
*buffers
;
851 xcb_get_geometry_cookie_t geometry_cookie
;
852 xcb_get_geometry_reply_t
*geometry_reply
;
853 xcb_generic_error_t
*error
;
858 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
859 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
860 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
862 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
863 drawable
, 1, 1, attachments
);
864 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
865 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
866 buffers_cookie
, NULL
);
867 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
868 if (buffers
== NULL
) {
872 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
873 geometry_cookie
, &error
);
874 if (geometry_reply
== NULL
|| error
!= NULL
) {
875 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
881 switch (geometry_reply
->depth
) {
883 format
= __DRI_IMAGE_FORMAT_RGB565
;
886 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
889 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
892 _eglError(EGL_BAD_PARAMETER
,
893 "dri2_create_image_khr: unsupported pixmap depth");
895 free(geometry_reply
);
899 dri2_img
= malloc(sizeof *dri2_img
);
902 free(geometry_reply
);
903 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
904 return EGL_NO_IMAGE_KHR
;
907 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
909 free(geometry_reply
);
911 return EGL_NO_IMAGE_KHR
;
914 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
915 dri2_img
->dri_image
=
916 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
917 buffers_reply
->width
,
918 buffers_reply
->height
,
925 free(geometry_reply
);
927 return &dri2_img
->base
;
931 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
932 _EGLContext
*ctx
, EGLenum target
,
933 EGLClientBuffer buffer
, const EGLint
*attr_list
)
938 case EGL_NATIVE_PIXMAP_KHR
:
939 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
941 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
946 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
948 struct dri2_egl_display
*dri2_dpy
;
950 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
951 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
952 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
953 drv
->API
.DestroySurface
= dri2_destroy_surface
;
954 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
955 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
957 drv
->API
.SwapBuffersRegionNOK
= NULL
;
958 drv
->API
.CreateImageKHR
= NULL
;
959 drv
->API
.DestroyImageKHR
= NULL
;
960 drv
->API
.CreateDRMImageMESA
= NULL
;
961 drv
->API
.ExportDRMImageMESA
= NULL
;
963 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
965 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
967 disp
->DriverData
= (void *) dri2_dpy
;
968 if (disp
->PlatformDisplay
== NULL
) {
969 dri2_dpy
->conn
= xcb_connect(0, 0);
971 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
974 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
975 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
979 if (!dri2_load_driver_swrast(disp
))
982 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
983 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
984 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
985 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
986 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
988 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
989 dri2_dpy
->extensions
[1] = NULL
;
990 dri2_dpy
->extensions
[2] = NULL
;
992 if (!dri2_create_screen(disp
))
995 if (dri2_dpy
->conn
) {
996 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
997 goto cleanup_configs
;
1000 /* we're supporting EGL 1.4 */
1001 disp
->VersionMajor
= 1;
1002 disp
->VersionMinor
= 4;
1007 _eglCleanupDisplay(disp
);
1008 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1010 dlclose(dri2_dpy
->driver
);
1012 if (disp
->PlatformDisplay
== NULL
)
1013 xcb_disconnect(dri2_dpy
->conn
);
1022 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1024 struct dri2_egl_display
*dri2_dpy
;
1026 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
1027 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1028 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1029 drv
->API
.DestroySurface
= dri2_destroy_surface
;
1030 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
1031 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1032 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
1033 drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1034 drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
1035 drv
->API
.SwapInterval
= dri2_swap_interval
;
1037 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1039 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1041 disp
->DriverData
= (void *) dri2_dpy
;
1042 if (disp
->PlatformDisplay
== NULL
) {
1043 dri2_dpy
->conn
= xcb_connect(0, 0);
1045 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1048 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1049 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1053 if (dri2_dpy
->conn
) {
1054 if (!dri2_connect(dri2_dpy
))
1058 if (!dri2_load_driver(disp
))
1062 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
1063 if (dri2_dpy
->fd
== -1 && errno
== EINVAL
)
1066 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
1067 if (dri2_dpy
->fd
!= -1)
1068 fcntl(dri2_dpy
->fd
, F_SETFD
, fcntl(dri2_dpy
->fd
, F_GETFD
) |
1071 if (dri2_dpy
->fd
== -1) {
1072 _eglLog(_EGL_WARNING
,
1073 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
1075 goto cleanup_driver
;
1078 if (dri2_dpy
->conn
) {
1079 if (!dri2_authenticate(disp
))
1083 if (dri2_dpy
->dri2_minor
>= 1) {
1084 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1085 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1086 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1087 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1088 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1089 dri2_get_buffers_with_format
;
1091 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1092 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
1093 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1094 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1095 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
1098 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1099 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1100 dri2_dpy
->extensions
[2] = NULL
;
1102 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1103 dri2_dpy
->invalidate_available
= (dri2_dpy
->dri2_minor
>= 3);
1105 if (!dri2_create_screen(disp
))
1108 if (dri2_dpy
->conn
) {
1109 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1110 goto cleanup_configs
;
1113 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1114 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1115 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1116 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1118 #ifdef HAVE_WAYLAND_PLATFORM
1119 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1121 dri2_dpy
->authenticate
= dri2_x11_authenticate
;
1123 /* we're supporting EGL 1.4 */
1124 disp
->VersionMajor
= 1;
1125 disp
->VersionMinor
= 4;
1130 _eglCleanupDisplay(disp
);
1131 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1133 close(dri2_dpy
->fd
);
1135 dlclose(dri2_dpy
->driver
);
1137 if (disp
->PlatformDisplay
== NULL
)
1138 xcb_disconnect(dri2_dpy
->conn
);
1146 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1148 EGLBoolean initialized
= EGL_TRUE
;
1150 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1152 if (x11_dri2_accel
) {
1153 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1154 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1157 initialized
= dri2_initialize_x11_swrast(drv
, disp
);