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_DEPTH_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 return &dri2_surf
->base
;
253 cleanup_dri_drawable
:
254 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
256 if (type
== EGL_PBUFFER_BIT
)
257 xcb_free_pixmap(dri2_dpy
->conn
, dri2_surf
->drawable
);
265 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
268 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
269 _EGLConfig
*conf
, EGLNativeWindowType window
,
270 const EGLint
*attrib_list
)
272 return dri2_create_surface(drv
, disp
, EGL_WINDOW_BIT
, conf
,
273 window
, attrib_list
);
277 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
278 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
279 const EGLint
*attrib_list
)
281 return dri2_create_surface(drv
, disp
, EGL_PIXMAP_BIT
, conf
,
282 pixmap
, attrib_list
);
286 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
287 _EGLConfig
*conf
, const EGLint
*attrib_list
)
289 return dri2_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
290 XCB_WINDOW_NONE
, attrib_list
);
294 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
296 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
297 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
301 if (!_eglPutSurface(surf
))
304 (*dri2_dpy
->core
->destroyDrawable
)(dri2_surf
->dri_drawable
);
306 if (dri2_dpy
->dri2
) {
307 xcb_dri2_destroy_drawable (dri2_dpy
->conn
, dri2_surf
->drawable
);
309 assert(dri2_dpy
->swrast
);
310 swrastDestroyDrawable(dri2_dpy
, dri2_surf
);
313 if (surf
->Type
== EGL_PBUFFER_BIT
)
314 xcb_free_pixmap (dri2_dpy
->conn
, dri2_surf
->drawable
);
322 * Process list of buffer received from the server
324 * Processes the list of buffers received in a reply from the server to either
325 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
328 dri2_process_buffers(struct dri2_egl_surface
*dri2_surf
,
329 xcb_dri2_dri2_buffer_t
*buffers
, unsigned count
)
331 struct dri2_egl_display
*dri2_dpy
=
332 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
333 xcb_rectangle_t rectangle
;
336 dri2_surf
->buffer_count
= count
;
337 dri2_surf
->have_fake_front
= 0;
339 /* This assumes the DRI2 buffer attachment tokens matches the
340 * __DRIbuffer tokens. */
341 for (i
= 0; i
< count
; i
++) {
342 dri2_surf
->buffers
[i
].attachment
= buffers
[i
].attachment
;
343 dri2_surf
->buffers
[i
].name
= buffers
[i
].name
;
344 dri2_surf
->buffers
[i
].pitch
= buffers
[i
].pitch
;
345 dri2_surf
->buffers
[i
].cpp
= buffers
[i
].cpp
;
346 dri2_surf
->buffers
[i
].flags
= buffers
[i
].flags
;
348 /* We only use the DRI drivers single buffer configs. This
349 * means that if we try to render to a window, DRI2 will give us
350 * the fake front buffer, which we'll use as a back buffer.
351 * Note that EGL doesn't require that several clients rendering
352 * to the same window must see the same aux buffers. */
353 if (dri2_surf
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
354 dri2_surf
->have_fake_front
= 1;
357 if (dri2_surf
->region
!= XCB_NONE
)
358 xcb_xfixes_destroy_region(dri2_dpy
->conn
, dri2_surf
->region
);
362 rectangle
.width
= dri2_surf
->base
.Width
;
363 rectangle
.height
= dri2_surf
->base
.Height
;
364 dri2_surf
->region
= xcb_generate_id(dri2_dpy
->conn
);
365 xcb_xfixes_create_region(dri2_dpy
->conn
, dri2_surf
->region
, 1, &rectangle
);
369 dri2_get_buffers(__DRIdrawable
* driDrawable
,
370 int *width
, int *height
,
371 unsigned int *attachments
, int count
,
372 int *out_count
, void *loaderPrivate
)
374 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
375 struct dri2_egl_display
*dri2_dpy
=
376 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
377 xcb_dri2_dri2_buffer_t
*buffers
;
378 xcb_dri2_get_buffers_reply_t
*reply
;
379 xcb_dri2_get_buffers_cookie_t cookie
;
383 cookie
= xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
385 count
, count
, attachments
);
386 reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
, cookie
, NULL
);
387 buffers
= xcb_dri2_get_buffers_buffers (reply
);
391 *out_count
= reply
->count
;
392 dri2_surf
->base
.Width
= *width
= reply
->width
;
393 dri2_surf
->base
.Height
= *height
= reply
->height
;
394 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
398 return dri2_surf
->buffers
;
402 dri2_get_buffers_with_format(__DRIdrawable
* driDrawable
,
403 int *width
, int *height
,
404 unsigned int *attachments
, int count
,
405 int *out_count
, void *loaderPrivate
)
407 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
408 struct dri2_egl_display
*dri2_dpy
=
409 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
410 xcb_dri2_dri2_buffer_t
*buffers
;
411 xcb_dri2_get_buffers_with_format_reply_t
*reply
;
412 xcb_dri2_get_buffers_with_format_cookie_t cookie
;
413 xcb_dri2_attach_format_t
*format_attachments
;
417 format_attachments
= (xcb_dri2_attach_format_t
*) attachments
;
418 cookie
= xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy
->conn
,
423 reply
= xcb_dri2_get_buffers_with_format_reply (dri2_dpy
->conn
,
428 buffers
= xcb_dri2_get_buffers_with_format_buffers (reply
);
429 dri2_surf
->base
.Width
= *width
= reply
->width
;
430 dri2_surf
->base
.Height
= *height
= reply
->height
;
431 *out_count
= reply
->count
;
432 dri2_process_buffers(dri2_surf
, buffers
, *out_count
);
436 return dri2_surf
->buffers
;
440 dri2_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
444 /* FIXME: Does EGL support front buffer rendering at all? */
447 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
449 dri2WaitGL(dri2_surf
);
451 (void) loaderPrivate
;
456 dri2_strndup(const char *s
, int length
)
460 d
= malloc(length
+ 1);
464 memcpy(d
, s
, length
);
471 dri2_connect(struct dri2_egl_display
*dri2_dpy
)
473 xcb_xfixes_query_version_reply_t
*xfixes_query
;
474 xcb_xfixes_query_version_cookie_t xfixes_query_cookie
;
475 xcb_dri2_query_version_reply_t
*dri2_query
;
476 xcb_dri2_query_version_cookie_t dri2_query_cookie
;
477 xcb_dri2_connect_reply_t
*connect
;
478 xcb_dri2_connect_cookie_t connect_cookie
;
479 xcb_generic_error_t
*error
;
480 xcb_screen_iterator_t s
;
482 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_xfixes_id
);
483 xcb_prefetch_extension_data (dri2_dpy
->conn
, &xcb_dri2_id
);
485 xfixes_query_cookie
= xcb_xfixes_query_version(dri2_dpy
->conn
,
486 XCB_XFIXES_MAJOR_VERSION
,
487 XCB_XFIXES_MINOR_VERSION
);
489 dri2_query_cookie
= xcb_dri2_query_version (dri2_dpy
->conn
,
490 XCB_DRI2_MAJOR_VERSION
,
491 XCB_DRI2_MINOR_VERSION
);
493 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
494 connect_cookie
= xcb_dri2_connect_unchecked (dri2_dpy
->conn
,
496 XCB_DRI2_DRIVER_TYPE_DRI
);
499 xcb_xfixes_query_version_reply (dri2_dpy
->conn
,
500 xfixes_query_cookie
, &error
);
501 if (xfixes_query
== NULL
||
502 error
!= NULL
|| xfixes_query
->major_version
< 2) {
503 _eglLog(_EGL_WARNING
, "DRI2: failed to query xfixes version");
510 xcb_dri2_query_version_reply (dri2_dpy
->conn
, dri2_query_cookie
, &error
);
511 if (dri2_query
== NULL
|| error
!= NULL
) {
512 _eglLog(_EGL_WARNING
, "DRI2: failed to query version");
516 dri2_dpy
->dri2_major
= dri2_query
->major_version
;
517 dri2_dpy
->dri2_minor
= dri2_query
->minor_version
;
520 connect
= xcb_dri2_connect_reply (dri2_dpy
->conn
, connect_cookie
, NULL
);
521 if (connect
== NULL
||
522 connect
->driver_name_length
+ connect
->device_name_length
== 0) {
523 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
527 dri2_dpy
->device_name
=
528 dri2_strndup(xcb_dri2_connect_device_name (connect
),
529 xcb_dri2_connect_device_name_length (connect
));
531 dri2_dpy
->driver_name
=
532 dri2_strndup(xcb_dri2_connect_driver_name (connect
),
533 xcb_dri2_connect_driver_name_length (connect
));
535 if (dri2_dpy
->device_name
== NULL
|| dri2_dpy
->driver_name
== NULL
) {
536 free(dri2_dpy
->device_name
);
537 free(dri2_dpy
->driver_name
);
547 dri2_authenticate(struct dri2_egl_display
*dri2_dpy
)
549 xcb_dri2_authenticate_reply_t
*authenticate
;
550 xcb_dri2_authenticate_cookie_t authenticate_cookie
;
551 xcb_screen_iterator_t s
;
554 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
555 _eglLog(_EGL_WARNING
, "DRI2: failed to get drm magic");
559 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
560 authenticate_cookie
=
561 xcb_dri2_authenticate_unchecked(dri2_dpy
->conn
, s
.data
->root
, magic
);
563 xcb_dri2_authenticate_reply(dri2_dpy
->conn
, authenticate_cookie
, NULL
);
564 if (authenticate
== NULL
|| !authenticate
->authenticated
) {
565 _eglLog(_EGL_WARNING
, "DRI2: failed to authenticate");
576 dri2_add_configs_for_visuals(struct dri2_egl_display
*dri2_dpy
,
579 xcb_screen_iterator_t s
;
580 xcb_depth_iterator_t d
;
581 xcb_visualtype_t
*visuals
;
584 EGLint config_attrs
[] = {
585 EGL_NATIVE_VISUAL_ID
, 0,
586 EGL_NATIVE_VISUAL_TYPE
, 0,
590 s
= xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy
->conn
));
591 d
= xcb_screen_allowed_depths_iterator(s
.data
);
598 EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
601 EGLBoolean class_added
[6] = { 0, };
603 visuals
= xcb_depth_visuals(d
.data
);
604 for (i
= 0; i
< xcb_depth_visuals_length(d
.data
); i
++) {
605 if (class_added
[visuals
[i
]._class
])
608 class_added
[visuals
[i
]._class
] = EGL_TRUE
;
609 for (j
= 0; dri2_dpy
->driver_configs
[j
]; j
++) {
610 config_attrs
[1] = visuals
[i
].visual_id
;
611 config_attrs
[3] = visuals
[i
]._class
;
613 dri2_add_config(disp
, dri2_dpy
->driver_configs
[j
], id
++,
614 d
.data
->depth
, surface_type
, config_attrs
);
621 if (!_eglGetArraySize(disp
->Configs
)) {
622 _eglLog(_EGL_WARNING
, "DRI2: failed to create any config");
630 dri2_copy_region(_EGLDriver
*drv
, _EGLDisplay
*disp
,
631 _EGLSurface
*draw
, xcb_xfixes_region_t region
)
633 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
634 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
635 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
637 enum xcb_dri2_attachment_t render_attachment
;
638 xcb_dri2_copy_region_cookie_t cookie
;
640 if (dri2_drv
->glFlush
) {
641 ctx
= _eglGetCurrentContext();
642 if (ctx
&& ctx
->DrawSurface
== &dri2_surf
->base
)
646 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
649 /* FIXME: Add support for dri swapbuffers, that'll give us swap
650 * interval and page flipping (at least for fullscreen windows) as
651 * well as the page flip event. Unless surface->SwapBehavior is
652 * EGL_BUFFER_PRESERVED. */
653 #if __DRI2_FLUSH_VERSION >= 2
655 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
659 if (dri2_surf
->have_fake_front
)
660 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT
;
662 render_attachment
= XCB_DRI2_ATTACHMENT_BUFFER_BACK_LEFT
;
664 cookie
= xcb_dri2_copy_region_unchecked(dri2_dpy
->conn
,
667 XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
,
669 free(xcb_dri2_copy_region_reply(dri2_dpy
->conn
, cookie
, NULL
));
675 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
677 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
678 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
680 if (dri2_dpy
->dri2
) {
681 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
683 assert(dri2_dpy
->swrast
);
685 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
691 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
,
692 EGLint numRects
, const EGLint
*rects
)
694 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
695 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
697 xcb_xfixes_region_t region
;
698 xcb_rectangle_t rectangles
[16];
701 if (numRects
> (int)ARRAY_SIZE(rectangles
))
702 return dri2_copy_region(drv
, disp
, draw
, dri2_surf
->region
);
704 /* FIXME: Invert y here? */
705 for (i
= 0; i
< numRects
; i
++) {
706 rectangles
[i
].x
= rects
[i
* 4];
707 rectangles
[i
].y
= rects
[i
* 4 + 1];
708 rectangles
[i
].width
= rects
[i
* 4 + 2];
709 rectangles
[i
].height
= rects
[i
* 4 + 3];
712 region
= xcb_generate_id(dri2_dpy
->conn
);
713 xcb_xfixes_create_region(dri2_dpy
->conn
, region
, numRects
, rectangles
);
714 ret
= dri2_copy_region(drv
, disp
, draw
, region
);
715 xcb_xfixes_destroy_region(dri2_dpy
->conn
, region
);
721 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
722 EGLNativePixmapType target
)
724 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
725 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
730 (*dri2_dpy
->flush
->flush
)(dri2_surf
->dri_drawable
);
732 gc
= xcb_generate_id(dri2_dpy
->conn
);
733 xcb_create_gc(dri2_dpy
->conn
, gc
, target
, 0, NULL
);
734 xcb_copy_area(dri2_dpy
->conn
,
740 dri2_surf
->base
.Width
,
741 dri2_surf
->base
.Height
);
742 xcb_free_gc(dri2_dpy
->conn
, gc
);
748 dri2_create_image_khr_pixmap(_EGLDisplay
*disp
, _EGLContext
*ctx
,
749 EGLClientBuffer buffer
, const EGLint
*attr_list
)
751 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
752 struct dri2_egl_image
*dri2_img
;
753 unsigned int attachments
[1];
754 xcb_drawable_t drawable
;
755 xcb_dri2_get_buffers_cookie_t buffers_cookie
;
756 xcb_dri2_get_buffers_reply_t
*buffers_reply
;
757 xcb_dri2_dri2_buffer_t
*buffers
;
758 xcb_get_geometry_cookie_t geometry_cookie
;
759 xcb_get_geometry_reply_t
*geometry_reply
;
760 xcb_generic_error_t
*error
;
765 drawable
= (xcb_drawable_t
) buffer
;
766 xcb_dri2_create_drawable (dri2_dpy
->conn
, drawable
);
767 attachments
[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT
;
769 xcb_dri2_get_buffers_unchecked (dri2_dpy
->conn
,
770 drawable
, 1, 1, attachments
);
771 geometry_cookie
= xcb_get_geometry (dri2_dpy
->conn
, drawable
);
772 buffers_reply
= xcb_dri2_get_buffers_reply (dri2_dpy
->conn
,
773 buffers_cookie
, NULL
);
774 buffers
= xcb_dri2_get_buffers_buffers (buffers_reply
);
775 if (buffers
== NULL
) {
779 geometry_reply
= xcb_get_geometry_reply (dri2_dpy
->conn
,
780 geometry_cookie
, &error
);
781 if (geometry_reply
== NULL
|| error
!= NULL
) {
782 _eglError(EGL_BAD_ALLOC
, "xcb_get_geometry");
788 switch (geometry_reply
->depth
) {
790 format
= __DRI_IMAGE_FORMAT_RGB565
;
793 format
= __DRI_IMAGE_FORMAT_XRGB8888
;
796 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
799 _eglError(EGL_BAD_PARAMETER
,
800 "dri2_create_image_khr: unsupported pixmap depth");
802 free(geometry_reply
);
806 dri2_img
= malloc(sizeof *dri2_img
);
809 free(geometry_reply
);
810 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
811 return EGL_NO_IMAGE_KHR
;
814 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
816 free(geometry_reply
);
817 return EGL_NO_IMAGE_KHR
;
820 stride
= buffers
[0].pitch
/ buffers
[0].cpp
;
821 dri2_img
->dri_image
=
822 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
823 buffers_reply
->width
,
824 buffers_reply
->height
,
831 free(geometry_reply
);
833 return &dri2_img
->base
;
837 dri2_x11_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
838 _EGLContext
*ctx
, EGLenum target
,
839 EGLClientBuffer buffer
, const EGLint
*attr_list
)
844 case EGL_NATIVE_PIXMAP_KHR
:
845 return dri2_create_image_khr_pixmap(disp
, ctx
, buffer
, attr_list
);
847 return dri2_create_image_khr(drv
, disp
, ctx
, target
, buffer
, attr_list
);
852 dri2_initialize_x11_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
854 struct dri2_egl_display
*dri2_dpy
;
856 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
857 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
858 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
859 drv
->API
.DestroySurface
= dri2_destroy_surface
;
860 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
861 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
863 drv
->API
.SwapBuffersRegionNOK
= NULL
;
864 drv
->API
.CreateImageKHR
= NULL
;
865 drv
->API
.DestroyImageKHR
= NULL
;
866 drv
->API
.CreateDRMImageMESA
= NULL
;
867 drv
->API
.ExportDRMImageMESA
= NULL
;
869 dri2_dpy
= malloc(sizeof *dri2_dpy
);
871 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
873 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
875 disp
->DriverData
= (void *) dri2_dpy
;
876 if (disp
->PlatformDisplay
== NULL
) {
877 dri2_dpy
->conn
= xcb_connect(0, 0);
879 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
882 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
883 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
887 dri2_dpy
->driver_name
= dri2_strndup("swrast", strlen("swrast"));
889 if (!dri2_load_driver(disp
))
892 dri2_dpy
->swrast_loader_extension
.base
.name
= __DRI_SWRAST_LOADER
;
893 dri2_dpy
->swrast_loader_extension
.base
.version
= __DRI_SWRAST_LOADER_VERSION
;
894 dri2_dpy
->swrast_loader_extension
.getDrawableInfo
= swrastGetDrawableInfo
;
895 dri2_dpy
->swrast_loader_extension
.putImage
= swrastPutImage
;
896 dri2_dpy
->swrast_loader_extension
.getImage
= swrastGetImage
;
898 dri2_dpy
->extensions
[0] = &dri2_dpy
->swrast_loader_extension
.base
;
899 dri2_dpy
->extensions
[1] = NULL
;
900 dri2_dpy
->extensions
[2] = NULL
;
902 if (!dri2_create_screen(disp
))
905 if (dri2_dpy
->conn
) {
906 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
907 goto cleanup_configs
;
910 /* we're supporting EGL 1.4 */
911 disp
->VersionMajor
= 1;
912 disp
->VersionMinor
= 4;
917 _eglCleanupDisplay(disp
);
918 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
920 dlclose(dri2_dpy
->driver
);
922 if (disp
->PlatformDisplay
== NULL
)
923 xcb_disconnect(dri2_dpy
->conn
);
932 dri2_initialize_x11_dri2(_EGLDriver
*drv
, _EGLDisplay
*disp
)
934 struct dri2_egl_display
*dri2_dpy
;
936 drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
937 drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
938 drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
939 drv
->API
.DestroySurface
= dri2_destroy_surface
;
940 drv
->API
.SwapBuffers
= dri2_swap_buffers
;
941 drv
->API
.CopyBuffers
= dri2_copy_buffers
;
942 drv
->API
.CreateImageKHR
= dri2_x11_create_image_khr
;
943 drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
945 dri2_dpy
= malloc(sizeof *dri2_dpy
);
947 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
949 memset(dri2_dpy
, 0, sizeof *dri2_dpy
);
951 disp
->DriverData
= (void *) dri2_dpy
;
952 if (disp
->PlatformDisplay
== NULL
) {
953 dri2_dpy
->conn
= xcb_connect(0, 0);
955 dri2_dpy
->conn
= XGetXCBConnection((Display
*) disp
->PlatformDisplay
);
958 if (xcb_connection_has_error(dri2_dpy
->conn
)) {
959 _eglLog(_EGL_WARNING
, "DRI2: xcb_connect failed");
963 if (dri2_dpy
->conn
) {
964 if (!dri2_connect(dri2_dpy
))
968 if (!dri2_load_driver(disp
))
971 dri2_dpy
->fd
= open(dri2_dpy
->device_name
, O_RDWR
);
972 if (dri2_dpy
->fd
== -1) {
973 _eglLog(_EGL_WARNING
,
974 "DRI2: could not open %s (%s)", dri2_dpy
->device_name
,
979 if (dri2_dpy
->conn
) {
980 if (!dri2_authenticate(dri2_dpy
))
984 if (dri2_dpy
->dri2_minor
>= 1) {
985 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
986 dri2_dpy
->dri2_loader_extension
.base
.version
= 3;
987 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
988 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
989 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
=
990 dri2_get_buffers_with_format
;
992 dri2_dpy
->dri2_loader_extension
.base
.name
= __DRI_DRI2_LOADER
;
993 dri2_dpy
->dri2_loader_extension
.base
.version
= 2;
994 dri2_dpy
->dri2_loader_extension
.getBuffers
= dri2_get_buffers
;
995 dri2_dpy
->dri2_loader_extension
.flushFrontBuffer
= dri2_flush_front_buffer
;
996 dri2_dpy
->dri2_loader_extension
.getBuffersWithFormat
= NULL
;
999 dri2_dpy
->extensions
[0] = &dri2_dpy
->dri2_loader_extension
.base
;
1000 dri2_dpy
->extensions
[1] = &image_lookup_extension
.base
;
1001 dri2_dpy
->extensions
[2] = NULL
;
1003 if (!dri2_create_screen(disp
))
1006 if (dri2_dpy
->conn
) {
1007 if (!dri2_add_configs_for_visuals(dri2_dpy
, disp
))
1008 goto cleanup_configs
;
1011 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
1012 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
1013 disp
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
1014 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
1015 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
1016 disp
->Extensions
.NOK_swap_region
= EGL_TRUE
;
1017 disp
->Extensions
.NOK_texture_from_pixmap
= EGL_TRUE
;
1019 /* we're supporting EGL 1.4 */
1020 disp
->VersionMajor
= 1;
1021 disp
->VersionMinor
= 4;
1026 _eglCleanupDisplay(disp
);
1027 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1029 close(dri2_dpy
->fd
);
1031 dlclose(dri2_dpy
->driver
);
1033 if (disp
->PlatformDisplay
== NULL
)
1034 xcb_disconnect(dri2_dpy
->conn
);
1042 dri2_initialize_x11(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1044 EGLBoolean initialized
= EGL_TRUE
;
1046 int x11_dri2_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
1048 if (x11_dri2_accel
) {
1049 if (!dri2_initialize_x11_dri2(drv
, disp
)) {
1050 initialized
= dri2_initialize_x11_swrast(drv
, disp
);
1053 initialized
= dri2_initialize_x11_swrast(drv
, disp
);