2 * Copyright © 2013 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * Portions of this code were adapted from dri2_glx.c which carries the
25 * following copyright:
27 * Copyright © 2008 Red Hat, Inc.
29 * Permission is hereby granted, free of charge, to any person obtaining a
30 * copy of this software and associated documentation files (the "Soft-
31 * ware"), to deal in the Software without restriction, including without
32 * limitation the rights to use, copy, modify, merge, publish, distribute,
33 * and/or sell copies of the Software, and to permit persons to whom the
34 * Software is furnished to do so, provided that the above copyright
35 * notice(s) and this permission notice appear in all copies of the Soft-
36 * ware and that both the above copyright notice(s) and this permission
37 * notice appear in supporting documentation.
39 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
40 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
41 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
42 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
43 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
44 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
45 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
46 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
47 * MANCE OF THIS SOFTWARE.
49 * Except as contained in this notice, the name of a copyright holder shall
50 * not be used in advertising or otherwise to promote the sale, use or
51 * other dealings in this Software without prior written authorization of
52 * the copyright holder.
55 * Kristian Høgsberg (krh@redhat.com)
58 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
61 #include <X11/extensions/Xfixes.h>
62 #include <X11/Xlib-xcb.h>
63 #include <X11/xshmfence.h>
66 #include <xcb/present.h>
69 #include "glxclient.h"
74 #include <sys/types.h>
79 #include "dri_common.h"
80 #include "dri3_priv.h"
83 static const struct glx_context_vtable dri3_context_vtable
;
86 dri3_fence_reset(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
88 xshmfence_reset(buffer
->shm_fence
);
92 dri3_fence_set(struct dri3_buffer
*buffer
)
94 xshmfence_trigger(buffer
->shm_fence
);
98 dri3_fence_trigger(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
100 xcb_sync_trigger_fence(c
, buffer
->sync_fence
);
104 dri3_fence_await(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
107 xshmfence_await(buffer
->shm_fence
);
111 dri3_fence_triggered(struct dri3_buffer
*buffer
)
113 return xshmfence_query(buffer
->shm_fence
);
117 dri3_destroy_context(struct glx_context
*context
)
119 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
120 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
122 driReleaseDrawables(&pcp
->base
);
124 free((char *) context
->extensions
);
126 (*psc
->core
->destroyContext
) (pcp
->driContext
);
132 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
133 GLXDrawable draw
, GLXDrawable read
)
135 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
136 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
137 struct dri3_drawable
*pdraw
, *pread
;
139 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
140 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
142 driReleaseDrawables(&pcp
->base
);
144 if (pdraw
== NULL
|| pread
== NULL
)
145 return GLXBadDrawable
;
147 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
148 pdraw
->driDrawable
, pread
->driDrawable
))
149 return GLXBadContext
;
155 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
157 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
158 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
160 (*psc
->core
->unbindContext
) (pcp
->driContext
);
163 static struct glx_context
*
164 dri3_create_context_attribs(struct glx_screen
*base
,
165 struct glx_config
*config_base
,
166 struct glx_context
*shareList
,
167 unsigned num_attribs
,
168 const uint32_t *attribs
,
171 struct dri3_context
*pcp
= NULL
;
172 struct dri3_context
*pcp_shared
= NULL
;
173 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
174 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
175 __DRIcontext
*shared
= NULL
;
177 uint32_t minor_ver
= 1;
178 uint32_t major_ver
= 2;
181 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
182 uint32_t ctx_attribs
[2 * 5];
183 unsigned num_ctx_attribs
= 0;
184 uint32_t render_type
;
186 /* Remap the GLX tokens to DRI2 tokens.
188 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
189 &major_ver
, &minor_ver
,
190 &render_type
, &flags
, &api
,
194 /* Check the renderType value */
195 if (!validate_renderType_against_config(config_base
, render_type
))
199 pcp_shared
= (struct dri3_context
*) shareList
;
200 shared
= pcp_shared
->driContext
;
203 pcp
= calloc(1, sizeof *pcp
);
205 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
209 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
212 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
213 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
214 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
215 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
217 /* Only send a value when the non-default value is requested. By doing
218 * this we don't have to check the driver's DRI3 version before sending the
221 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
222 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
223 ctx_attribs
[num_ctx_attribs
++] = reset
;
227 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
229 /* The current __DRI_CTX_FLAG_* values are identical to the
230 * GLX_CONTEXT_*_BIT values.
232 ctx_attribs
[num_ctx_attribs
++] = flags
;
236 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
245 if (pcp
->driContext
== NULL
)
248 pcp
->base
.vtable
= &dri3_context_vtable
;
258 static struct glx_context
*
259 dri3_create_context(struct glx_screen
*base
,
260 struct glx_config
*config_base
,
261 struct glx_context
*shareList
, int renderType
)
265 return dri3_create_context_attribs(base
, config_base
, shareList
,
270 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
);
273 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
275 struct dri3_screen
*psc
= (struct dri3_screen
*) base
->psc
;
276 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
277 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
280 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
282 for (i
= 0; i
< DRI3_NUM_BUFFERS
; i
++) {
283 if (pdraw
->buffers
[i
])
284 dri3_free_render_buffer(pdraw
, pdraw
->buffers
[i
]);
287 if (pdraw
->special_event
)
288 xcb_unregister_for_special_event(c
, pdraw
->special_event
);
292 static __GLXDRIdrawable
*
293 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
294 GLXDrawable drawable
, struct glx_config
*config_base
)
296 struct dri3_drawable
*pdraw
;
297 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
298 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
299 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
301 pdraw
= calloc(1, sizeof(*pdraw
));
305 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
306 pdraw
->base
.xDrawable
= xDrawable
;
307 pdraw
->base
.drawable
= drawable
;
308 pdraw
->base
.psc
= &psc
->base
;
309 pdraw
->swap_interval
= 1; /* default may be overridden below */
310 pdraw
->have_back
= 0;
311 pdraw
->have_fake_front
= 0;
314 psc
->config
->configQueryi(psc
->driScreen
,
315 "vblank_mode", &vblank_mode
);
317 switch (vblank_mode
) {
318 case DRI_CONF_VBLANK_NEVER
:
319 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
320 pdraw
->swap_interval
= 0;
322 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
323 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
325 pdraw
->swap_interval
= 1;
329 (void) __glXInitialize(psc
->base
.dpy
);
331 /* Create a new drawable */
333 (*psc
->image_driver
->createNewDrawable
) (psc
->driScreen
,
334 config
->driConfig
, pdraw
);
336 if (!pdraw
->driDrawable
) {
342 * Make sure server has the same swap interval we do for the new
345 if (psc
->vtable
.setSwapInterval
)
346 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
352 * Process one Present event
355 dri3_handle_present_event(struct dri3_drawable
*priv
, xcb_present_generic_event_t
*ge
)
357 switch (ge
->evtype
) {
358 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
359 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
361 priv
->width
= ce
->width
;
362 priv
->height
= ce
->height
;
365 case XCB_PRESENT_COMPLETE_NOTIFY
: {
366 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
368 /* Compute the processed SBC number from the received 32-bit serial number merged
369 * with the upper 32-bits of the sent 64-bit serial number while checking for
372 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
373 priv
->recv_sbc
= (priv
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
374 if (priv
->recv_sbc
> priv
->send_sbc
)
375 priv
->recv_sbc
-= 0x100000000;
377 priv
->recv_msc_serial
= ce
->serial
;
383 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
384 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
387 for (b
= 0; b
< sizeof (priv
->buffers
) / sizeof (priv
->buffers
[0]); b
++) {
388 struct dri3_buffer
*buf
= priv
->buffers
[b
];
390 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
402 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
403 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
405 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
406 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
407 xcb_generic_event_t
*ev
;
408 xcb_present_generic_event_t
*ge
;
411 /* Ask for the an event for the target MSC */
412 msc_serial
= ++priv
->send_msc_serial
;
413 xcb_present_notify_msc(c
,
414 priv
->base
.xDrawable
,
422 /* Wait for the event */
423 if (priv
->special_event
) {
424 while ((int32_t) (msc_serial
- priv
->recv_msc_serial
) > 0) {
425 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
429 dri3_handle_present_event(priv
, ge
);
435 *sbc
= priv
->recv_sbc
;
441 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
442 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
444 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
447 /** dri3_wait_for_sbc
449 * Wait for the swap buffer count to increase. The only way this
450 * can happen is if some other thread is doing swap buffers as
451 * we no longer share swap buffer counts with other processes.
453 * I'm not sure this is actually useful as such, and so this
454 * implementation is a kludge that just polls once a second
457 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
458 int64_t *msc
, int64_t *sbc
)
460 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
462 while (priv
->send_sbc
< target_sbc
) {
465 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
, sbc
);
469 * Asks the driver to flush any queued work necessary for serializing with the
470 * X command stream, and optionally the slightly more strict requirement of
471 * glFlush() equivalence (which would require flushing even if nothing had
472 * been drawn to a window system framebuffer, for example).
475 dri3_flush(struct dri3_screen
*psc
,
476 struct dri3_drawable
*draw
,
478 enum __DRI2throttleReason throttle_reason
)
480 struct glx_context
*gc
= __glXGetCurrentContext();
483 struct dri3_context
*dri3Ctx
= (struct dri3_context
*)gc
;
485 (*psc
->f
->flush_with_flags
)(dri3Ctx
->driContext
, draw
->driDrawable
, flags
, throttle_reason
);
489 static xcb_gcontext_t
490 dri3_drawable_gc(struct dri3_drawable
*priv
)
494 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
498 (priv
->gc
= xcb_generate_id(c
)),
499 priv
->base
.xDrawable
,
500 XCB_GC_GRAPHICS_EXPOSURES
,
506 static struct dri3_buffer
*
507 dri3_back_buffer(struct dri3_drawable
*priv
)
509 return priv
->buffers
[DRI3_BACK_ID(priv
->cur_back
)];
512 static struct dri3_buffer
*
513 dri3_fake_front_buffer(struct dri3_drawable
*priv
)
515 return priv
->buffers
[DRI3_FRONT_ID
];
519 dri3_copy_area (xcb_connection_t
*c
/**< */,
520 xcb_drawable_t src_drawable
/**< */,
521 xcb_drawable_t dst_drawable
/**< */,
522 xcb_gcontext_t gc
/**< */,
523 int16_t src_x
/**< */,
524 int16_t src_y
/**< */,
525 int16_t dst_x
/**< */,
526 int16_t dst_y
/**< */,
527 uint16_t width
/**< */,
528 uint16_t height
/**< */)
530 xcb_void_cookie_t cookie
;
532 cookie
= xcb_copy_area_checked(c
,
542 xcb_discard_reply(c
, cookie
.sequence
);
546 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
547 int width
, int height
,
550 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
551 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->psc
;
552 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
553 struct dri3_buffer
*back
= dri3_back_buffer(priv
);
557 /* Check we have the right attachments */
558 if (!priv
->have_back
|| priv
->is_pixmap
)
561 flags
= __DRI2_FLUSH_DRAWABLE
;
563 flags
|= __DRI2_FLUSH_CONTEXT
;
564 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
566 y
= priv
->height
- y
- height
;
568 dri3_fence_reset(c
, back
);
570 dri3_back_buffer(priv
)->pixmap
,
571 priv
->base
.xDrawable
,
572 dri3_drawable_gc(priv
),
573 x
, y
, x
, y
, width
, height
);
574 dri3_fence_trigger(c
, back
);
575 /* Refresh the fake front (if present) after we just damaged the real
578 if (priv
->have_fake_front
) {
579 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
581 dri3_back_buffer(priv
)->pixmap
,
582 dri3_fake_front_buffer(priv
)->pixmap
,
583 dri3_drawable_gc(priv
),
584 x
, y
, x
, y
, width
, height
);
585 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
586 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
588 dri3_fence_await(c
, back
);
592 dri3_copy_drawable(struct dri3_drawable
*priv
, Drawable dest
, Drawable src
)
594 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
595 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
597 dri3_flush(psc
, priv
, __DRI2_FLUSH_DRAWABLE
, 0);
599 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
602 dri3_drawable_gc(priv
),
603 0, 0, 0, 0, priv
->width
, priv
->height
);
604 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
605 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
609 dri3_wait_x(struct glx_context
*gc
)
611 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
612 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
614 if (priv
== NULL
|| !priv
->have_fake_front
)
617 dri3_copy_drawable(priv
, dri3_fake_front_buffer(priv
)->pixmap
, priv
->base
.xDrawable
);
621 dri3_wait_gl(struct glx_context
*gc
)
623 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
624 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
626 if (priv
== NULL
|| !priv
->have_fake_front
)
629 dri3_copy_drawable(priv
, priv
->base
.xDrawable
, dri3_fake_front_buffer(priv
)->pixmap
);
633 * Called by the driver when it needs to update the real front buffer with the
634 * contents of its fake front buffer.
637 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
639 struct glx_context
*gc
;
640 struct dri3_drawable
*pdraw
= loaderPrivate
;
641 struct dri3_screen
*psc
;
646 if (!pdraw
->base
.psc
)
649 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
651 (void) __glXInitialize(psc
->base
.dpy
);
653 gc
= __glXGetCurrentContext();
655 dri3_flush(psc
, pdraw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
661 dri3_cpp_for_format(uint32_t format
) {
663 case __DRI_IMAGE_FORMAT_R8
:
665 case __DRI_IMAGE_FORMAT_RGB565
:
666 case __DRI_IMAGE_FORMAT_GR88
:
668 case __DRI_IMAGE_FORMAT_XRGB8888
:
669 case __DRI_IMAGE_FORMAT_ARGB8888
:
670 case __DRI_IMAGE_FORMAT_ABGR8888
:
671 case __DRI_IMAGE_FORMAT_XBGR8888
:
672 case __DRI_IMAGE_FORMAT_XRGB2101010
:
673 case __DRI_IMAGE_FORMAT_ARGB2101010
:
674 case __DRI_IMAGE_FORMAT_SARGB8
:
676 case __DRI_IMAGE_FORMAT_NONE
:
683 /** dri3_alloc_render_buffer
685 * Use the driver createImage function to construct a __DRIimage, then
686 * get a file descriptor for that and create an X pixmap from that
688 * Allocate an xshmfence for synchronization
690 static struct dri3_buffer
*
691 dri3_alloc_render_buffer(struct glx_screen
*glx_screen
, Drawable draw
,
692 unsigned int format
, int width
, int height
, int depth
)
694 struct dri3_screen
*psc
= (struct dri3_screen
*) glx_screen
;
695 Display
*dpy
= glx_screen
->dpy
;
696 struct dri3_buffer
*buffer
;
697 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
699 xcb_sync_fence_t sync_fence
;
700 struct xshmfence
*shm_fence
;
701 int buffer_fd
, fence_fd
;
704 /* Create an xshmfence object and
705 * prepare to send that to the X server
708 fence_fd
= xshmfence_alloc_shm();
711 shm_fence
= xshmfence_map_shm(fence_fd
);
712 if (shm_fence
== NULL
)
715 /* Allocate the image from the driver
717 buffer
= calloc(1, sizeof (struct dri3_buffer
));
721 buffer
->cpp
= dri3_cpp_for_format(format
);
725 buffer
->image
= (*psc
->image
->createImage
) (psc
->driScreen
,
728 __DRI_IMAGE_USE_SHARE
|__DRI_IMAGE_USE_SCANOUT
,
735 /* X wants the stride, so ask the image for it
737 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
))
738 goto no_buffer_attrib
;
740 buffer
->pitch
= stride
;
742 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_FD
, &buffer_fd
))
743 goto no_buffer_attrib
;
745 xcb_dri3_pixmap_from_buffer(c
,
746 (pixmap
= xcb_generate_id(c
)),
749 width
, height
, buffer
->pitch
,
750 depth
, buffer
->cpp
* 8,
753 xcb_dri3_fence_from_fd(c
,
755 (sync_fence
= xcb_generate_id(c
)),
759 buffer
->pixmap
= pixmap
;
760 buffer
->own_pixmap
= true;
761 buffer
->sync_fence
= sync_fence
;
762 buffer
->shm_fence
= shm_fence
;
763 buffer
->width
= width
;
764 buffer
->height
= height
;
766 /* Mark the buffer as idle
768 dri3_fence_set(buffer
);
773 (*psc
->image
->destroyImage
)(buffer
->image
);
777 xshmfence_unmap_shm(shm_fence
);
783 /** dri3_free_render_buffer
785 * Free everything associated with one render buffer including pixmap, fence
786 * stuff and the driver image
789 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
)
791 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
792 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
794 if (buffer
->own_pixmap
)
795 xcb_free_pixmap(c
, buffer
->pixmap
);
796 xcb_sync_destroy_fence(c
, buffer
->sync_fence
);
797 xshmfence_unmap_shm(buffer
->shm_fence
);
798 (*psc
->image
->destroyImage
)(buffer
->image
);
803 /** dri3_flush_present_events
805 * Process any present events that have been received from the X server
808 dri3_flush_present_events(struct dri3_drawable
*priv
)
810 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
812 /* Check to see if any configuration changes have occurred
813 * since we were last invoked
815 if (priv
->special_event
) {
816 xcb_generic_event_t
*ev
;
818 while ((ev
= xcb_poll_for_special_event(c
, priv
->special_event
)) != NULL
) {
819 xcb_present_generic_event_t
*ge
= (void *) ev
;
820 dri3_handle_present_event(priv
, ge
);
825 /** dri3_update_drawable
827 * Called the first time we use the drawable and then
828 * after we receive present configure notify events to
829 * track the geometry of the drawable
832 dri3_update_drawable(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
834 struct dri3_drawable
*priv
= loaderPrivate
;
835 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
837 /* First time through, go get the current drawable geometry
839 if (priv
->width
== 0 || priv
->height
== 0 || priv
->depth
== 0) {
840 xcb_get_geometry_cookie_t geom_cookie
;
841 xcb_get_geometry_reply_t
*geom_reply
;
842 xcb_void_cookie_t cookie
;
843 xcb_generic_error_t
*error
;
845 /* Try to select for input on the window.
847 * If the drawable is a window, this will get our events
850 * Otherwise, we'll get a BadWindow error back from this request which
851 * will let us know that the drawable is a pixmap instead.
855 cookie
= xcb_present_select_input_checked(c
,
856 (priv
->eid
= xcb_generate_id(c
)),
857 priv
->base
.xDrawable
,
858 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
859 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
860 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
862 /* Create an XCB event queue to hold present events outside of the usual
863 * application event queue
865 priv
->special_event
= xcb_register_for_special_xge(c
,
870 geom_cookie
= xcb_get_geometry(c
, priv
->base
.xDrawable
);
872 geom_reply
= xcb_get_geometry_reply(c
, geom_cookie
, NULL
);
877 priv
->width
= geom_reply
->width
;
878 priv
->height
= geom_reply
->height
;
879 priv
->depth
= geom_reply
->depth
;
880 priv
->is_pixmap
= false;
884 /* Check to see if our select input call failed. If it failed with a
885 * BadWindow error, then assume the drawable is a pixmap. Destroy the
886 * special event queue created above and mark the drawable as a pixmap
889 error
= xcb_request_check(c
, cookie
);
892 if (error
->error_code
!= BadWindow
) {
896 priv
->is_pixmap
= true;
897 xcb_unregister_for_special_event(c
, priv
->special_event
);
898 priv
->special_event
= NULL
;
901 dri3_flush_present_events(priv
);
905 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
906 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
907 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
908 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
911 image_format_to_fourcc(int format
)
914 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
916 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
917 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
918 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
919 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
920 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
925 /** dri3_get_pixmap_buffer
927 * Get the DRM object for a pixmap from the X server and
928 * wrap that with a __DRIimage structure using createImageFromFds
930 static struct dri3_buffer
*
931 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
,
933 enum dri3_buffer_type buffer_type
,
936 struct dri3_drawable
*pdraw
= loaderPrivate
;
937 int buf_id
= dri3_pixmap_buf_id(buffer_type
);
938 struct dri3_buffer
*buffer
= pdraw
->buffers
[buf_id
];
940 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
941 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
944 struct dri3_screen
*psc
;
946 xcb_sync_fence_t sync_fence
;
947 struct xshmfence
*shm_fence
;
949 __DRIimage
*image_planar
;
955 pixmap
= pdraw
->base
.xDrawable
;
956 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
958 c
= XGetXCBConnection(dpy
);
960 buffer
= calloc(1, sizeof (struct dri3_buffer
));
964 fence_fd
= xshmfence_alloc_shm();
967 shm_fence
= xshmfence_map_shm(fence_fd
);
968 if (shm_fence
== NULL
) {
973 xcb_dri3_fence_from_fd(c
,
975 (sync_fence
= xcb_generate_id(c
)),
979 /* Get an FD for the pixmap object
981 bp_cookie
= xcb_dri3_buffer_from_pixmap(c
, pixmap
);
982 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(c
, bp_cookie
, NULL
);
985 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
987 stride
= bp_reply
->stride
;
990 /* createImageFromFds creates a wrapper __DRIimage structure which
991 * can deal with multiple planes for things like Yuv images. So, once
992 * we've gotten the planar wrapper, pull the single plane out of it and
993 * discard the wrapper.
995 image_planar
= (*psc
->image
->createImageFromFds
) (psc
->driScreen
,
998 image_format_to_fourcc(format
),
1000 &stride
, &offset
, buffer
);
1005 buffer
->image
= (*psc
->image
->fromPlanar
)(image_planar
, 0, buffer
);
1007 (*psc
->image
->destroyImage
)(image_planar
);
1012 buffer
->pixmap
= pixmap
;
1013 buffer
->own_pixmap
= false;
1014 buffer
->width
= bp_reply
->width
;
1015 buffer
->height
= bp_reply
->height
;
1016 buffer
->buffer_type
= buffer_type
;
1017 buffer
->shm_fence
= shm_fence
;
1018 buffer
->sync_fence
= sync_fence
;
1020 pdraw
->buffers
[buf_id
] = buffer
;
1024 xcb_sync_destroy_fence(c
, sync_fence
);
1025 xshmfence_unmap_shm(shm_fence
);
1034 * Find an idle back buffer. If there isn't one, then
1035 * wait for a present idle notify event from the X server
1038 dri3_find_back(xcb_connection_t
*c
, struct dri3_drawable
*priv
)
1041 xcb_generic_event_t
*ev
;
1042 xcb_present_generic_event_t
*ge
;
1046 for (b
= 0; b
< DRI3_NUM_BACK
; b
++) {
1047 int id
= DRI3_BACK_ID(b
);
1048 struct dri3_buffer
*buffer
= priv
->buffers
[id
];
1055 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
1059 dri3_handle_present_event(priv
, ge
);
1065 * Find a front or back buffer, allocating new ones as necessary
1067 static struct dri3_buffer
*
1068 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1069 unsigned int format
,
1070 enum dri3_buffer_type buffer_type
,
1071 void *loaderPrivate
)
1073 struct dri3_drawable
*priv
= loaderPrivate
;
1074 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
1075 struct dri3_buffer
*buffer
;
1078 if (buffer_type
== dri3_buffer_back
) {
1079 int back
= dri3_find_back(c
, priv
);
1084 priv
->cur_back
= back
;
1085 buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1087 buf_id
= DRI3_FRONT_ID
;
1090 buffer
= priv
->buffers
[buf_id
];
1092 /* Allocate a new buffer if there isn't an old one, or if that
1093 * old one is the wrong size
1095 if (!buffer
|| buffer
->width
!= priv
->width
|| buffer
->height
!= priv
->height
) {
1096 struct dri3_buffer
*new_buffer
;
1098 /* Allocate the new buffers
1100 new_buffer
= dri3_alloc_render_buffer(priv
->base
.psc
,
1101 priv
->base
.xDrawable
,
1102 format
, priv
->width
, priv
->height
, priv
->depth
);
1106 /* When resizing, copy the contents of the old buffer, waiting for that
1107 * copy to complete using our fences before proceeding
1109 switch (buffer_type
) {
1110 case dri3_buffer_back
:
1112 dri3_fence_reset(c
, new_buffer
);
1113 dri3_fence_await(c
, buffer
);
1117 dri3_drawable_gc(priv
),
1118 0, 0, 0, 0, priv
->width
, priv
->height
);
1119 dri3_fence_trigger(c
, new_buffer
);
1120 dri3_free_render_buffer(priv
, buffer
);
1123 case dri3_buffer_front
:
1124 dri3_fence_reset(c
, new_buffer
);
1126 priv
->base
.xDrawable
,
1128 dri3_drawable_gc(priv
),
1129 0, 0, 0, 0, priv
->width
, priv
->height
);
1130 dri3_fence_trigger(c
, new_buffer
);
1133 buffer
= new_buffer
;
1134 buffer
->buffer_type
= buffer_type
;
1135 priv
->buffers
[buf_id
] = buffer
;
1137 dri3_fence_await(c
, buffer
);
1139 /* Return the requested buffer */
1143 /** dri3_free_buffers
1145 * Free the front bufffer or all of the back buffers. Used
1146 * when the application changes which buffers it needs
1149 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1150 enum dri3_buffer_type buffer_type
,
1151 void *loaderPrivate
)
1153 struct dri3_drawable
*priv
= loaderPrivate
;
1154 struct dri3_buffer
*buffer
;
1159 switch (buffer_type
) {
1160 case dri3_buffer_back
:
1161 first_id
= DRI3_BACK_ID(0);
1162 n_id
= DRI3_NUM_BACK
;
1164 case dri3_buffer_front
:
1165 first_id
= DRI3_FRONT_ID
;
1169 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1170 buffer
= priv
->buffers
[buf_id
];
1172 dri3_free_render_buffer(priv
, buffer
);
1173 priv
->buffers
[buf_id
] = NULL
;
1178 /** dri3_get_buffers
1180 * The published buffer allocation API.
1181 * Returns all of the necessary buffers, allocating
1185 dri3_get_buffers(__DRIdrawable
*driDrawable
,
1186 unsigned int format
,
1188 void *loaderPrivate
,
1189 uint32_t buffer_mask
,
1190 struct __DRIimageList
*buffers
)
1192 struct dri3_drawable
*priv
= loaderPrivate
;
1193 struct dri3_buffer
*front
, *back
;
1195 buffers
->image_mask
= 0;
1196 buffers
->front
= NULL
;
1197 buffers
->back
= NULL
;
1202 if (!dri3_update_drawable(driDrawable
, loaderPrivate
))
1205 /* pixmaps always have front buffers */
1206 if (priv
->is_pixmap
)
1207 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1209 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1210 if (priv
->is_pixmap
)
1211 front
= dri3_get_pixmap_buffer(driDrawable
,
1216 front
= dri3_get_buffer(driDrawable
,
1224 dri3_free_buffers(driDrawable
, dri3_buffer_front
, loaderPrivate
);
1225 priv
->have_fake_front
= 0;
1228 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1229 back
= dri3_get_buffer(driDrawable
,
1235 priv
->have_back
= 1;
1237 dri3_free_buffers(driDrawable
, dri3_buffer_back
, loaderPrivate
);
1238 priv
->have_back
= 0;
1242 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1243 buffers
->front
= front
->image
;
1244 priv
->have_fake_front
= !priv
->is_pixmap
;
1248 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1249 buffers
->back
= back
->image
;
1252 priv
->stamp
= stamp
;
1257 /* The image loader extension record for DRI3
1259 static const __DRIimageLoaderExtension imageLoaderExtension
= {
1260 {__DRI_IMAGE_LOADER
, __DRI_IMAGE_LOADER_VERSION
},
1261 .getBuffers
= dri3_get_buffers
,
1262 .flushFrontBuffer
= dri3_flush_front_buffer
,
1265 /** dri3_swap_buffers
1267 * Make the current back buffer visible using the present extension
1270 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
1271 int64_t remainder
, Bool flush
)
1273 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1274 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1275 Display
*dpy
= priv
->base
.psc
->dpy
;
1276 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1277 int buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1280 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
1282 flags
|= __DRI2_FLUSH_CONTEXT
;
1283 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
1285 dri3_flush_present_events(priv
);
1287 if (priv
->buffers
[buf_id
] && !priv
->is_pixmap
) {
1288 dri3_fence_reset(c
, priv
->buffers
[buf_id
]);
1290 /* Compute when we want the frame shown by taking the last known successful
1291 * MSC and adding in a swap interval for each outstanding swap request
1294 if (target_msc
== 0)
1295 target_msc
= priv
->msc
+ priv
->swap_interval
* (priv
->send_sbc
- priv
->recv_sbc
);
1297 priv
->buffers
[buf_id
]->busy
= 1;
1298 xcb_present_pixmap(c
,
1299 priv
->base
.xDrawable
,
1300 priv
->buffers
[buf_id
]->pixmap
,
1301 (uint32_t) priv
->send_sbc
,
1306 None
, /* target_crtc */
1308 priv
->buffers
[buf_id
]->sync_fence
,
1309 XCB_PRESENT_OPTION_NONE
,
1312 remainder
, 0, NULL
);
1313 ret
= (int64_t) priv
->send_sbc
;
1315 /* If there's a fake front, then copy the source back buffer
1316 * to the fake front to keep it up to date. This needs
1317 * to reset the fence and make future users block until
1318 * the X server is done copying the bits
1320 if (priv
->have_fake_front
) {
1321 dri3_fence_reset(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1323 priv
->buffers
[buf_id
]->pixmap
,
1324 priv
->buffers
[DRI3_FRONT_ID
]->pixmap
,
1325 dri3_drawable_gc(priv
),
1326 0, 0, 0, 0, priv
->width
, priv
->height
);
1327 dri3_fence_trigger(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1339 * Wrapper around xcb_dri3_open
1342 dri3_open(Display
*dpy
,
1346 xcb_dri3_open_cookie_t cookie
;
1347 xcb_dri3_open_reply_t
*reply
;
1348 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1351 cookie
= xcb_dri3_open(c
,
1355 reply
= xcb_dri3_open_reply(c
, cookie
, NULL
);
1359 if (reply
->nfd
!= 1) {
1364 fd
= xcb_dri3_open_reply_fds(c
, reply
)[0];
1365 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1371 /** dri3_destroy_screen
1374 dri3_destroy_screen(struct glx_screen
*base
)
1376 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
1378 /* Free the direct rendering per screen data */
1379 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
1380 driDestroyConfigs(psc
->driver_configs
);
1385 /** dri3_set_swap_interval
1387 * Record the application swap interval specification,
1390 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
1392 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1393 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1394 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1397 psc
->config
->configQueryi(psc
->driScreen
,
1398 "vblank_mode", &vblank_mode
);
1400 switch (vblank_mode
) {
1401 case DRI_CONF_VBLANK_NEVER
:
1403 return GLX_BAD_VALUE
;
1405 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1407 return GLX_BAD_VALUE
;
1413 priv
->swap_interval
= interval
;
1418 /** dri3_get_swap_interval
1420 * Return the stored swap interval
1423 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
1425 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1427 return priv
->swap_interval
;
1431 dri3_bind_tex_image(Display
* dpy
,
1432 GLXDrawable drawable
,
1433 int buffer
, const int *attrib_list
)
1435 struct glx_context
*gc
= __glXGetCurrentContext();
1436 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1437 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1438 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1439 struct dri3_screen
*psc
;
1441 if (pdraw
!= NULL
) {
1442 psc
= (struct dri3_screen
*) base
->psc
;
1444 (*psc
->f
->invalidate
)(pdraw
->driDrawable
);
1448 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1449 pdraw
->base
.textureTarget
,
1450 pdraw
->base
.textureFormat
,
1451 pdraw
->driDrawable
);
1456 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1458 struct glx_context
*gc
= __glXGetCurrentContext();
1459 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1460 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1461 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1462 struct dri3_screen
*psc
;
1464 if (pdraw
!= NULL
) {
1465 psc
= (struct dri3_screen
*) base
->psc
;
1467 if (psc
->texBuffer
->releaseTexBuffer
)
1468 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1469 pdraw
->base
.textureTarget
,
1470 pdraw
->driDrawable
);
1474 static const struct glx_context_vtable dri3_context_vtable
= {
1475 dri3_destroy_context
,
1477 dri3_unbind_context
,
1481 dri3_bind_tex_image
,
1482 dri3_release_tex_image
,
1483 NULL
, /* get_proc_address */
1486 /** dri3_bind_extensions
1488 * Enable all of the extensions supported on DRI3
1491 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
1492 const char *driverName
)
1494 const __DRIextension
**extensions
;
1498 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1500 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1501 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1502 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1503 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1506 * GLX_INTEL_swap_event is broken on the server side, where it's
1507 * currently unconditionally enabled. This completely breaks
1508 * systems running on drivers which don't support that extension.
1509 * There's no way to test for its presence on this side, so instead
1510 * of disabling it unconditionally, just disable it for drivers
1511 * which are known to not support it, or for DDX drivers supporting
1512 * only an older (pre-ScheduleSwap) version of DRI2.
1514 * This is a hack which is required until:
1515 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1516 * is merged and updated xserver makes it's way into distros:
1518 // if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1519 // __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1522 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
1524 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1525 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1527 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1528 __glXEnableDirectExtension(&psc
->base
,
1529 "GLX_EXT_create_context_es2_profile");
1531 for (i
= 0; extensions
[i
]; i
++) {
1532 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1533 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1534 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1537 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1538 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1539 /* internal driver extension, no GL extension exposed */
1542 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1543 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1545 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1546 __glXEnableDirectExtension(&psc
->base
,
1547 "GLX_ARB_create_context_robustness");
1551 static const struct glx_screen_vtable dri3_screen_vtable
= {
1552 dri3_create_context
,
1553 dri3_create_context_attribs
1556 /** dri3_create_screen
1558 * Initialize DRI3 on the specified screen.
1560 * Opens the DRI device, locates the appropriate DRI driver
1563 * Checks to see if the driver supports the necessary extensions
1565 * Initializes the driver for the screen and sets up our structures
1568 static struct glx_screen
*
1569 dri3_create_screen(int screen
, struct glx_display
* priv
)
1571 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
1572 const __DRIconfig
**driver_configs
;
1573 const __DRIextension
**extensions
;
1574 const struct dri3_display
*const pdp
= (struct dri3_display
*)
1576 struct dri3_screen
*psc
;
1577 __GLXDRIscreen
*psp
;
1578 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1579 char *driverName
, *deviceName
;
1582 psc
= calloc(1, sizeof *psc
);
1588 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1593 psc
->fd
= dri3_open(priv
->dpy
, RootWindow(priv
->dpy
, screen
), None
);
1595 int conn_error
= xcb_connection_has_error(c
);
1597 glx_screen_cleanup(&psc
->base
);
1599 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
1602 ErrorMessageF("Connection closed during DRI3 initialization failure");
1608 driverName
= loader_get_driver_for_fd(psc
->fd
, 0);
1610 ErrorMessageF("No driver found\n");
1614 psc
->driver
= driOpenDriver(driverName
);
1615 if (psc
->driver
== NULL
) {
1616 ErrorMessageF("driver pointer missing\n");
1620 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1621 if (extensions
== NULL
)
1624 for (i
= 0; extensions
[i
]; i
++) {
1625 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1626 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1627 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
1628 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
1632 if (psc
->core
== NULL
) {
1633 ErrorMessageF("core dri driver extension not found\n");
1637 if (psc
->image_driver
== NULL
) {
1638 ErrorMessageF("image driver extension not found\n");
1643 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
1644 (const __DRIextension
**)
1645 &pdp
->loader_extensions
[0],
1647 &driver_configs
, psc
);
1649 if (psc
->driScreen
== NULL
) {
1650 ErrorMessageF("failed to create dri screen\n");
1654 extensions
= (*psc
->core
->getExtensions
)(psc
->driScreen
);
1656 for (i
= 0; extensions
[i
]; i
++) {
1657 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
1658 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
1661 if (psc
->image
== NULL
) {
1662 ErrorMessageF("image extension not found\n");
1666 dri3_bind_extensions(psc
, priv
, driverName
);
1668 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
1669 ErrorMessageF("Version 4 or later of flush extension not found\n");
1673 if (!psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
1674 !psc
->texBuffer
->setTexBuffer2
)
1676 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1680 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1681 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1683 if (!configs
|| !visuals
)
1686 glx_config_destroy_list(psc
->base
.configs
);
1687 psc
->base
.configs
= configs
;
1688 glx_config_destroy_list(psc
->base
.visuals
);
1689 psc
->base
.visuals
= visuals
;
1691 psc
->driver_configs
= driver_configs
;
1693 psc
->base
.vtable
= &dri3_screen_vtable
;
1695 psc
->base
.driScreen
= psp
;
1696 psp
->destroyScreen
= dri3_destroy_screen
;
1697 psp
->createDrawable
= dri3_create_drawable
;
1698 psp
->swapBuffers
= dri3_swap_buffers
;
1700 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
1701 psp
->waitForMSC
= dri3_wait_for_msc
;
1702 psp
->waitForSBC
= dri3_wait_for_sbc
;
1703 psp
->setSwapInterval
= dri3_set_swap_interval
;
1704 psp
->getSwapInterval
= dri3_get_swap_interval
;
1705 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1707 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
1708 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1716 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1719 glx_config_destroy_list(configs
);
1721 glx_config_destroy_list(visuals
);
1723 psc
->core
->destroyScreen(psc
->driScreen
);
1724 psc
->driScreen
= NULL
;
1728 dlclose(psc
->driver
);
1732 glx_screen_cleanup(&psc
->base
);
1738 /** dri_destroy_display
1740 * Called from __glXFreeDisplayPrivate.
1743 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
1748 /** dri3_create_display
1750 * Allocate, initialize and return a __DRIdisplayPrivate object.
1751 * This is called from __glXInitialize() when we are given a new
1752 * display pointer. This is public to that function, but hidden from
1755 _X_HIDDEN __GLXDRIdisplay
*
1756 dri3_create_display(Display
* dpy
)
1758 struct dri3_display
*pdp
;
1760 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1761 xcb_dri3_query_version_cookie_t dri3_cookie
;
1762 xcb_dri3_query_version_reply_t
*dri3_reply
;
1763 xcb_present_query_version_cookie_t present_cookie
;
1764 xcb_present_query_version_reply_t
*present_reply
;
1765 xcb_generic_error_t
*error
;
1766 const xcb_query_extension_reply_t
*extension
;
1768 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1769 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1771 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1772 if (!(extension
&& extension
->present
))
1775 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1776 if (!(extension
&& extension
->present
))
1779 dri3_cookie
= xcb_dri3_query_version(c
,
1780 XCB_DRI3_MAJOR_VERSION
,
1781 XCB_DRI3_MINOR_VERSION
);
1784 present_cookie
= xcb_present_query_version(c
,
1785 XCB_PRESENT_MAJOR_VERSION
,
1786 XCB_PRESENT_MINOR_VERSION
);
1788 pdp
= malloc(sizeof *pdp
);
1792 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1798 pdp
->dri3Major
= dri3_reply
->major_version
;
1799 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1802 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1803 if (!present_reply
) {
1807 pdp
->presentMajor
= present_reply
->major_version
;
1808 pdp
->presentMinor
= present_reply
->minor_version
;
1809 free(present_reply
);
1811 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1812 pdp
->base
.createScreen
= dri3_create_screen
;
1814 loader_set_logger(dri_message
);
1817 pdp
->loader_extensions
[i
++] = &imageLoaderExtension
.base
;
1819 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1821 pdp
->loader_extensions
[i
++] = NULL
;
1829 #endif /* GLX_DIRECT_RENDERING */