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 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
;
191 dri2_surf
= malloc(sizeof *dri2_surf
);
193 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
197 if (!_eglInitSurface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
))
200 dri2_surf
->region
= XCB_NONE
;
201 if (type
== EGL_PBUFFER_BIT
) {
202 dri2_surf
->drawable
= xcb_generate_id(dri2_dpy
->conn
);
203 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
204 xcb_create_pixmap(dri2_dpy
->conn
, conf
->BufferSize
,
205 dri2_surf
->drawable
, s
.data
->root
,
206 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
208 dri2_surf
->drawable
= window
;
211 if (dri2_dpy
->dri2
) {
212 dri2_surf
->dri_drawable
=
213 (*dri2_dpy
->dri2
->createNewDrawable
) (dri2_dpy
->dri_screen
,
214 type
== EGL_WINDOW_BIT
?
215 dri2_conf
->dri_double_config
:
216 dri2_conf
->dri_single_config
,
219 assert(dri2_dpy
->swrast
);
220 dri2_surf
->dri_drawable
=
221 (*dri2_dpy
->swrast
->createNewDrawable
) (dri2_dpy
->dri_screen
,
222 dri2_conf
->dri_double_config
,
226 if (dri2_surf
->dri_drawable
== NULL
) {
227 _eglError(EGL_BAD_ALLOC
, "dri2->createNewDrawable");
231 if (dri2_dpy
->dri2
) {
232 xcb_dri2_create_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
234 swrastCreateDrawable(dri2_dpy
, dri2_surf
, _eglGetConfigKey(conf
, EGL_BUFFER_SIZE
));
237 if (type
!= EGL_PBUFFER_BIT
) {
238 cookie
= xcb_get_geometry (dri2_dpy
->conn
, dri2_surf
->drawable
);
239 reply
= xcb_get_geometry_reply (dri2_dpy
->conn
, cookie
, &error
);
240 if (reply
== NULL
|| error
!= NULL
) {
241 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
243 goto cleanup_dri_drawable
;
246 dri2_surf
->base
.Width
= reply
->width
;
247 dri2_surf
->base
.Height
= reply
->height
;
251 /* we always copy the back buffer to front */
252 dri2_surf
->base
.PostSubBufferSupportedNV
= EGL_TRUE
;
254 return &dri2_surf
->base
;
256 cleanup_dri_drawable
:
257 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
259 if (type
== EGL_PBUFFER_BIT
)
260 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
268 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
271 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
272 _EGLConfig
*conf
, EGLNativeWindowType window
,
273 const EGLint
*attrib_list
)
275 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
276 window
, attrib_list
);
280 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
281 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
282 const EGLint
*attrib_list
)
284 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
285 pixmap
, attrib_list
);
289 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
290 _EGLConfig
*conf
, const EGLint
*attrib_list
)
292 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
293 XCB_WINDOW_NONE
, attrib_list
);
297 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
299 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
300 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
304 if (!_eglPutSurface(surf
))
307 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
309 if (dri2_dpy
->dri2
) {
310 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
312 assert(dri2_dpy
->swrast
);
313 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
316 if (surf
->Type
== EGL_PBUFFER_BIT
)
317 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
325 * Process list of buffer received from the server
327 * Processes the list of buffers received in a reply from the server to either
328 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
331 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
332 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
334 struct dri2_egl_display
*dri2_dpy
=
335 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
336 xcb_rectangle_t rectangle
;
339 dri2_surf
->buffer_count
= count
;
340 dri2_surf
->have_fake_front
= 0;
342 /* This assumes the DRI2 buffer attachment tokens matches the
343 * __DRIbuffer tokens. */
344 for (i
= 0; i
< count
; i
++) {
345 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
346 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
347 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
348 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
349 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
351 /* We only use the DRI drivers single buffer configs. This
352 * means that if we try to render to a window, DRI2 will give us
353 * the fake front buffer, which we'll use as a back buffer.
354 * Note that EGL doesn't require that several clients rendering
355 * to the same window must see the same aux buffers. */
356 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
357 dri2_surf
->have_fake_front
= 1;
360 if (dri2_surf
->region
!= XCB_NONE
)
361 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
365 rectangle
.width
= dri2_surf
->base
.Width
;
366 rectangle
.height
= dri2_surf
->base
.Height
;
367 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
368 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
372 dri2_get_buffers(__DRIdrawable
* driDrawable
,
373 int *width
, int *height
,
374 unsigned int *attachments
, int count
,
375 int *out_count
, void *loaderPrivate
)
377 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
378 struct dri2_egl_display
*dri2_dpy
=
379 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
380 xcb_dri2_dri2_buffer_t
*buffers
;
381 xcb_dri2_get_buffers_reply_t
*reply
;
382 xcb_dri2_get_buffers_cookie_t cookie
;
386 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
388 count
, count
, attachments
);
389 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
390 buffers
= xcb_dri2_get_buffers_buffers (reply
);
394 *out_count
= reply
->count
;
395 dri2_surf
->base
.Width
= *width
= reply
->width
;
396 dri2_surf
->base
.Height
= *height
= reply
->height
;
397 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
401 return dri2_surf
->buffers
;
405 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
406 int *width
, int *height
,
407 unsigned int *attachments
, int count
,
408 int *out_count
, void *loaderPrivate
)
410 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
411 struct dri2_egl_display
*dri2_dpy
=
412 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
413 xcb_dri2_dri2_buffer_t
*buffers
;
414 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
415 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
416 xcb_dri2_attach_format_t
*format_attachments
;
420 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
421 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
426 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
431 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
432 dri2_surf
->base
.Width
= *width
= reply
->width
;
433 dri2_surf
->base
.Height
= *height
= reply
->height
;
434 *out_count
= reply
->count
;
435 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
439 return dri2_surf
->buffers
;
443 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
447 /* FIXME: Does EGL support front buffer rendering at all? */
450 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
452 dri2WaitGL(dri2_surf
);
454 (void) loaderPrivate
;
459 dri2_strndup(const char *s
, int length
)
463 d
= malloc(length
+ 1);
467 memcpy(d
, s
, length
);
474 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
476 xcb_xfixes_query_version_reply_t
*xfixes_query
;
477 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
478 xcb_dri2_query_version_reply_t
*dri2_query
;
479 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
480 xcb_dri2_connect_reply_t
*connect
;
481 xcb_dri2_connect_cookie_t connect_cookie
;
482 xcb_generic_error_t
*error
;
483 xcb_screen_iterator_t s
;
484 char *driver_name
, *device_name
;
485 const xcb_query_extension_reply_t
*extension
;
487 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
488 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
490 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_xfixes_id
);
491 if (!(extension
&& extension
->present
))
494 extension
= xcb_get_extension_data(dri2_dpy
->conn
, &xcb_dri2_id
);
495 if (!(extension
&& extension
->present
))
498 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
499 XCB_XFIXES_MAJOR_VERSION
,
500 XCB_XFIXES_MINOR_VERSION
);
502 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
503 XCB_DRI2_MAJOR_VERSION
,
504 XCB_DRI2_MINOR_VERSION
);
506 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
507 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
509 XCB_DRI2_DRIVER_TYPE_DRI
);
512 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
513 xfixes_query_cookie
, &error
);
514 if (xfixes_query
== NULL
||
515 error
!= NULL
|| xfixes_query
->major_version
< 2) {
516 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
523 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
524 if (dri2_query
== NULL
|| error
!= NULL
) {
525 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
529 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
530 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
533 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
534 if (connect
== NULL
||
535 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
536 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
540 driver_name
= xcb_dri2_connect_driver_name (connect
);
541 dri2_dpy
->driver_name
=
542 dri2_strndup(driver_name
,
543 xcb_dri2_connect_driver_name_length (connect
));
545 #if XCB_DRI2_CONNECT_DEVICE_NAME_BROKEN
546 device_name
= driver_name
+ ((connect
->driver_name_length
+ 3) & ~3);
548 device_name
= xcb_dri2_connect_device_name (connect
);
551 dri2_dpy
->device_name
=
552 dri2_strndup(device_name
,
553 xcb_dri2_connect_device_name_length (connect
));
555 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
556 free(dri2_dpy
->device_name
);
557 free(dri2_dpy
->driver_name
);
567 dri2_x11_authenticate(_EGLDisplay
*disp
, uint32_t id
)
569 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
570 xcb_dri2_authenticate_reply_t
*authenticate
;
571 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
572 xcb_screen_iterator_t s
;
575 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
576 authenticate_cookie
=
577 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, id
);
579 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
581 if (authenticate
== NULL
|| !authenticate
->authenticated
)
591 dri2_authenticate(_EGLDisplay
*disp
)
593 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
596 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
597 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
601 if (dri2_x11_authenticate(disp
, magic
) < 0) {
602 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
610 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
613 xcb_screen_iterator_t s
;
614 xcb_depth_iterator_t d
;
615 xcb_visualtype_t
*visuals
;
618 EGLint config_attrs
[] = {
619 EGL_NATIVE_VISUAL_ID
, 0,
620 EGL_NATIVE_VISUAL_TYPE
, 0,
624 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
625 d
= xcb_screen_allowed_depths_iterator(s
.data
);
632 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
635 EGLBoolean class_added
[6] = { 0, };
637 visuals
= xcb_depth_visuals(d
.data
);
638 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
639 if (class_added
[visuals
[i
]._class
])
642 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
643 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
644 config_attrs
[1] = visuals
[i
].visual_id
;
645 config_attrs
[3] = visuals
[i
]._class
;
647 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
648 d
.data
->depth
, surface_type
, config_attrs
, NULL
);
655 if (!_eglGetArraySize(disp
->Configs
)) {
656 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
664 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
665 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
667 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
668 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
669 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
671 enum xcb_dri2_attachment_t render_attachment
;
672 xcb_dri2_copy_region_cookie_t cookie
;
674 if (dri2_drv
->glFlush
) {
675 ctx
= _eglGetCurrentContext();
676 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
680 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
683 /* FIXME: Add support for dri swapbuffers, that'll give us swap
684 * interval and page flipping (at least for fullscreen windows) as
685 * well as the page flip event. Unless surface->SwapBehavior is
686 * EGL_BUFFER_PRESERVED. */
687 #if __DRI2_FLUSH_VERSION >= 2
689 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
693 if (dri2_surf
->have_fake_front
)
694 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
696 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
698 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
701 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
703 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
709 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
711 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
712 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
714 if (dri2_dpy
->dri2
) {
715 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
717 assert(dri2_dpy
->swrast
);
719 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
725 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
726 EGLint numRects
, const EGLint
*rects
)
728 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
729 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
731 xcb_xfixes_region_t region
;
732 xcb_rectangle_t rectangles
[16];
735 if (numRects
> (int)ARRAY_SIZE(rectangles
))
736 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
738 /* FIXME: Invert y here? */
739 for (i
= 0; i
< numRects
; i
++) {
740 rectangles
[i
].x
= rects
[i
* 4];
741 rectangles
[i
].y
= rects
[i
* 4 + 1];
742 rectangles
[i
].width
= rects
[i
* 4 + 2];
743 rectangles
[i
].height
= rects
[i
* 4 + 3];
746 region
= xcb_generate_id(dri2_dpy
->conn
);
747 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
748 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
749 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
755 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
756 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
758 const EGLint rect
[4] = { x
, draw
->Height
- y
- height
, width
, height
};
760 if (x
< 0 || y
< 0 || width
< 0 || height
< 0)
761 _eglError(EGL_BAD_PARAMETER
, "eglPostSubBufferNV");
763 return dri2_swap_buffers_region(drv
, disp
, draw
, 1, rect
);
767 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint interval
)
769 #if XCB_DRI2_MINOR_VERSION >= 3
770 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
771 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
773 /* XXX Check vblank_mode here? */
775 if (interval
> surf
->Config
->MaxSwapInterval
)
776 interval
= surf
->Config
->MaxSwapInterval
;
777 else if (interval
< surf
->Config
->MinSwapInterval
)
778 interval
= surf
->Config
->MinSwapInterval
;
780 if (interval
!= surf
->SwapInterval
&& dri2_dpy
->swap_available
)
781 xcb_dri2_swap_interval(dri2_dpy
->conn
, dri2_surf
->drawable
, interval
);
783 surf
->SwapInterval
= interval
;
792 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
793 EGLNativePixmapType target
)
795 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
796 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
801 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
803 gc
= xcb_generate_id(dri2_dpy
->conn
);
804 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
805 xcb_copy_area(dri2_dpy
->conn
,
811 dri2_surf
->base
.Width
,
812 dri2_surf
->base
.Height
);
813 xcb_free_gc(dri2_dpy
->conn
, gc
);
819 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
820 EGLClientBuffer buffer
, const EGLint
*attr_list
)
822 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
823 struct dri2_egl_image
*dri2_img
;
824 unsigned int attachments
[1];
825 xcb_drawable_t drawable
;
826 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
827 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
828 xcb_dri2_dri2_buffer_t
*buffers
;
829 xcb_get_geometry_cookie_t geometry_cookie
;
830 xcb_get_geometry_reply_t
*geometry_reply
;
831 xcb_generic_error_t
*error
;
836 drawable
= (xcb_drawable_t
) (uintptr_t) buffer
;
837 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
838 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
840 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
841 drawable
, 1, 1, attachments
);
842 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
843 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
844 buffers_cookie
, NULL
);
845 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
846 if (buffers
== NULL
) {
850 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
851 geometry_cookie
, &error
);
852 if (geometry_reply
== NULL
|| error
!= NULL
) {
853 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
859 switch (geometry_reply
->depth
) {
861 format
= __DRI_IMAGE_FORMAT_RGB565
;
864 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
867 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
870 _eglError(EGL_BAD_PARAMETER
,
871 "dri2_create_image_khr: unsupported pixmap depth");
873 free(geometry_reply
);
877 dri2_img
= malloc(sizeof *dri2_img
);
880 free(geometry_reply
);
881 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
882 return EGL_NO_IMAGE_KHR
;
885 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
887 free(geometry_reply
);
889 return EGL_NO_IMAGE_KHR
;
892 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
893 dri2_img
->dri_image
=
894 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
895 buffers_reply
->width
,
896 buffers_reply
->height
,
903 free(geometry_reply
);
905 return &dri2_img
->base
;
909 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
910 _EGLContext
*ctx
, EGLenum target
,
911 EGLClientBuffer buffer
, const EGLint
*attr_list
)
916 case EGL_NATIVE_PIXMAP_KHR
:
917 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
919 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
924 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
926 struct dri2_egl_display
*dri2_dpy
;
928 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
929 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
930 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
931 drv
->API
.DestroySurface
= dri2_destroy_surface
;
932 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
933 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
935 drv
->API
.SwapBuffersRegionNOK
= NULL
;
936 drv
->API
.CreateImageKHR
= NULL
;
937 drv
->API
.DestroyImageKHR
= NULL
;
938 drv
->API
.CreateDRMImageMESA
= NULL
;
939 drv
->API
.ExportDRMImageMESA
= NULL
;
941 dri2_dpy
= malloc(sizeof *dri2_dpy
);
943 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
945 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
947 disp
->DriverData
= (void *) dri2_dpy
;
948 if (disp
->PlatformDisplay
== NULL
) {
949 dri2_dpy
->conn
= xcb_connect(0, 0);
951 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
954 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
955 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
959 if (!dri2_load_driver_swrast(disp
))
962 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
963 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
964 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
965 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
966 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
968 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
969 dri2_dpy
->extensions
[1] = NULL
;
970 dri2_dpy
->extensions
[2] = NULL
;
972 if (!dri2_create_screen(disp
))
975 if (dri2_dpy
->conn
) {
976 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
977 goto cleanup_configs
;
980 /* we're supporting EGL 1.4 */
981 disp
->VersionMajor
= 1;
982 disp
->VersionMinor
= 4;
987 _eglCleanupDisplay(disp
);
988 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
990 dlclose(dri2_dpy
->driver
);
992 if (disp
->PlatformDisplay
== NULL
)
993 xcb_disconnect(dri2_dpy
->conn
);
1002 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1004 struct dri2_egl_display
*dri2_dpy
;
1006 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
1007 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
1008 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
1009 drv
->API
.DestroySurface
= dri2_destroy_surface
;
1010 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
1011 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
1012 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
1013 drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
1014 drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
1015 drv
->API
.SwapInterval
= dri2_swap_interval
;
1017 dri2_dpy
= malloc(sizeof *dri2_dpy
);
1019 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1021 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
1023 disp
->DriverData
= (void *) dri2_dpy
;
1024 if (disp
->PlatformDisplay
== NULL
) {
1025 dri2_dpy
->conn
= xcb_connect(0, 0);
1027 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
1030 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
1031 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
1035 if (dri2_dpy
->conn
) {
1036 if (!dri2_connect(dri2_dpy
))
1040 if (!dri2_load_driver(disp
))
1043 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
| O_CLOEXEC
);
1044 if (dri2_dpy
->fd
== -1) {
1045 _eglLog(_EGL_WARNING
,
1046 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
1048 goto cleanup_driver
;
1051 if (dri2_dpy
->conn
) {
1052 if (!dri2_authenticate(disp
))
1056 if (dri2_dpy
->dri2_minor
>= 1) {
1057 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1058 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
1059 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1060 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1061 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
1062 dri2_get_buffers_with_format
;
1064 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
1065 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
1066 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
1067 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
1068 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
1071 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1072 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1073 dri2_dpy
->extensions
[2] = NULL
;
1075 dri2_dpy
->swap_available
= (dri2_dpy
->dri2_minor
>= 2);
1077 if (!dri2_create_screen(disp
))
1080 if (dri2_dpy
->conn
) {
1081 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1082 goto cleanup_configs
;
1085 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1086 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1087 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1088 disp
->Extensions
.NV_post_sub_buffer
= EGL_TRUE
;
1090 #ifdef HAVE_WAYLAND_PLATFORM
1091 disp
->Extensions
.WL_bind_wayland_display
= EGL_TRUE
;
1093 dri2_dpy
->authenticate
= dri2_x11_authenticate
;
1095 /* we're supporting EGL 1.4 */
1096 disp
->VersionMajor
= 1;
1097 disp
->VersionMinor
= 4;
1102 _eglCleanupDisplay(disp
);
1103 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1105 close(dri2_dpy
->fd
);
1107 dlclose(dri2_dpy
->driver
);
1109 if (disp
->PlatformDisplay
== NULL
)
1110 xcb_disconnect(dri2_dpy
->conn
);
1118 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1120 EGLBoolean initialized
= EGL_TRUE
;
1122 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1124 if (x11_dri2_accel
) {
1125 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1126 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1129 initialized
= dri2_initialize_x11_swrast(drv
, disp
);