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>
68 #include "glxclient.h"
72 #include <sys/types.h>
76 #include "dri_common.h"
77 #include "dri3_priv.h"
81 static const struct glx_context_vtable dri3_context_vtable
;
84 dri3_fence_reset(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
86 xshmfence_reset(buffer
->shm_fence
);
90 dri3_fence_set(struct dri3_buffer
*buffer
)
92 xshmfence_trigger(buffer
->shm_fence
);
96 dri3_fence_trigger(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
98 xcb_sync_trigger_fence(c
, buffer
->sync_fence
);
102 dri3_fence_await(xcb_connection_t
*c
, struct dri3_buffer
*buffer
)
105 xshmfence_await(buffer
->shm_fence
);
109 dri3_fence_triggered(struct dri3_buffer
*buffer
)
111 return xshmfence_query(buffer
->shm_fence
);
115 dri3_destroy_context(struct glx_context
*context
)
117 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
118 struct dri3_screen
*psc
= (struct dri3_screen
*) context
->psc
;
120 driReleaseDrawables(&pcp
->base
);
122 free((char *) context
->extensions
);
124 (*psc
->core
->destroyContext
) (pcp
->driContext
);
130 dri3_bind_context(struct glx_context
*context
, struct glx_context
*old
,
131 GLXDrawable draw
, GLXDrawable read
)
133 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
134 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
135 struct dri3_drawable
*pdraw
, *pread
;
137 pdraw
= (struct dri3_drawable
*) driFetchDrawable(context
, draw
);
138 pread
= (struct dri3_drawable
*) driFetchDrawable(context
, read
);
140 driReleaseDrawables(&pcp
->base
);
142 if (pdraw
== NULL
|| pread
== NULL
)
143 return GLXBadDrawable
;
145 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
146 pdraw
->driDrawable
, pread
->driDrawable
))
147 return GLXBadContext
;
153 dri3_unbind_context(struct glx_context
*context
, struct glx_context
*new)
155 struct dri3_context
*pcp
= (struct dri3_context
*) context
;
156 struct dri3_screen
*psc
= (struct dri3_screen
*) pcp
->base
.psc
;
158 (*psc
->core
->unbindContext
) (pcp
->driContext
);
161 static struct glx_context
*
162 dri3_create_context_attribs(struct glx_screen
*base
,
163 struct glx_config
*config_base
,
164 struct glx_context
*shareList
,
165 unsigned num_attribs
,
166 const uint32_t *attribs
,
169 struct dri3_context
*pcp
= NULL
;
170 struct dri3_context
*pcp_shared
= NULL
;
171 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
172 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
173 __DRIcontext
*shared
= NULL
;
175 uint32_t minor_ver
= 1;
176 uint32_t major_ver
= 2;
179 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
180 uint32_t ctx_attribs
[2 * 5];
181 unsigned num_ctx_attribs
= 0;
182 uint32_t render_type
;
184 /* Remap the GLX tokens to DRI2 tokens.
186 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
187 &major_ver
, &minor_ver
,
188 &render_type
, &flags
, &api
,
192 /* Check the renderType value */
193 if (!validate_renderType_against_config(config_base
, render_type
))
197 pcp_shared
= (struct dri3_context
*) shareList
;
198 shared
= pcp_shared
->driContext
;
201 pcp
= calloc(1, sizeof *pcp
);
203 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
207 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
210 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
211 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
212 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
213 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
215 /* Only send a value when the non-default value is requested. By doing
216 * this we don't have to check the driver's DRI3 version before sending the
219 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
220 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
221 ctx_attribs
[num_ctx_attribs
++] = reset
;
225 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
227 /* The current __DRI_CTX_FLAG_* values are identical to the
228 * GLX_CONTEXT_*_BIT values.
230 ctx_attribs
[num_ctx_attribs
++] = flags
;
234 (*psc
->image_driver
->createContextAttribs
) (psc
->driScreen
,
243 if (pcp
->driContext
== NULL
)
246 pcp
->base
.vtable
= &dri3_context_vtable
;
256 static struct glx_context
*
257 dri3_create_context(struct glx_screen
*base
,
258 struct glx_config
*config_base
,
259 struct glx_context
*shareList
, int renderType
)
263 return dri3_create_context_attribs(base
, config_base
, shareList
,
268 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
);
271 dri3_update_num_back(struct dri3_drawable
*priv
)
276 if (priv
->swap_interval
== 0)
281 dri3_destroy_drawable(__GLXDRIdrawable
*base
)
283 struct dri3_screen
*psc
= (struct dri3_screen
*) base
->psc
;
284 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
285 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
288 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
290 for (i
= 0; i
< DRI3_NUM_BUFFERS
; i
++) {
291 if (pdraw
->buffers
[i
])
292 dri3_free_render_buffer(pdraw
, pdraw
->buffers
[i
]);
295 if (pdraw
->special_event
)
296 xcb_unregister_for_special_event(c
, pdraw
->special_event
);
300 static __GLXDRIdrawable
*
301 dri3_create_drawable(struct glx_screen
*base
, XID xDrawable
,
302 GLXDrawable drawable
, struct glx_config
*config_base
)
304 struct dri3_drawable
*pdraw
;
305 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
306 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
307 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
309 pdraw
= calloc(1, sizeof(*pdraw
));
313 pdraw
->base
.destroyDrawable
= dri3_destroy_drawable
;
314 pdraw
->base
.xDrawable
= xDrawable
;
315 pdraw
->base
.drawable
= drawable
;
316 pdraw
->base
.psc
= &psc
->base
;
317 pdraw
->swap_interval
= 1; /* default may be overridden below */
318 pdraw
->have_back
= 0;
319 pdraw
->have_fake_front
= 0;
322 psc
->config
->configQueryi(psc
->driScreen
,
323 "vblank_mode", &vblank_mode
);
325 switch (vblank_mode
) {
326 case DRI_CONF_VBLANK_NEVER
:
327 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
328 pdraw
->swap_interval
= 0;
330 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
331 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
333 pdraw
->swap_interval
= 1;
337 dri3_update_num_back(pdraw
);
339 (void) __glXInitialize(psc
->base
.dpy
);
341 /* Create a new drawable */
343 (*psc
->image_driver
->createNewDrawable
) (psc
->driScreen
,
344 config
->driConfig
, pdraw
);
346 if (!pdraw
->driDrawable
) {
352 * Make sure server has the same swap interval we do for the new
355 if (psc
->vtable
.setSwapInterval
)
356 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
362 * Process one Present event
365 dri3_handle_present_event(struct dri3_drawable
*priv
, xcb_present_generic_event_t
*ge
)
367 switch (ge
->evtype
) {
368 case XCB_PRESENT_CONFIGURE_NOTIFY
: {
369 xcb_present_configure_notify_event_t
*ce
= (void *) ge
;
371 priv
->width
= ce
->width
;
372 priv
->height
= ce
->height
;
375 case XCB_PRESENT_COMPLETE_NOTIFY
: {
376 xcb_present_complete_notify_event_t
*ce
= (void *) ge
;
378 /* Compute the processed SBC number from the received 32-bit serial number merged
379 * with the upper 32-bits of the sent 64-bit serial number while checking for
382 if (ce
->kind
== XCB_PRESENT_COMPLETE_KIND_PIXMAP
) {
383 priv
->recv_sbc
= (priv
->send_sbc
& 0xffffffff00000000LL
) | ce
->serial
;
384 if (priv
->recv_sbc
> priv
->send_sbc
)
385 priv
->recv_sbc
-= 0x100000000;
387 case XCB_PRESENT_COMPLETE_MODE_FLIP
:
388 priv
->flipping
= true;
390 case XCB_PRESENT_COMPLETE_MODE_COPY
:
391 priv
->flipping
= false;
394 dri3_update_num_back(priv
);
396 priv
->recv_msc_serial
= ce
->serial
;
402 case XCB_PRESENT_EVENT_IDLE_NOTIFY
: {
403 xcb_present_idle_notify_event_t
*ie
= (void *) ge
;
406 for (b
= 0; b
< sizeof (priv
->buffers
) / sizeof (priv
->buffers
[0]); b
++) {
407 struct dri3_buffer
*buf
= priv
->buffers
[b
];
409 if (buf
&& buf
->pixmap
== ie
->pixmap
) {
411 if (priv
->num_back
<= b
&& b
< DRI3_MAX_BACK
) {
412 dri3_free_render_buffer(priv
, buf
);
413 priv
->buffers
[b
] = NULL
;
425 dri3_wait_for_event(__GLXDRIdrawable
*pdraw
)
427 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
428 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
429 xcb_generic_event_t
*ev
;
430 xcb_present_generic_event_t
*ge
;
433 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
437 dri3_handle_present_event(priv
, ge
);
441 /** dri3_wait_for_msc
443 * Get the X server to send an event when the target msc/divisor/remainder is
447 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
448 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
450 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
451 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
454 /* Ask for the an event for the target MSC */
455 msc_serial
= ++priv
->send_msc_serial
;
456 xcb_present_notify_msc(c
,
457 priv
->base
.xDrawable
,
465 /* Wait for the event */
466 if (priv
->special_event
) {
467 while ((int32_t) (msc_serial
- priv
->recv_msc_serial
) > 0) {
468 if (!dri3_wait_for_event(pdraw
))
475 *sbc
= priv
->recv_sbc
;
480 /** dri3_drawable_get_msc
482 * Return the current UST/MSC/SBC triplet by asking the server
486 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
487 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
489 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
492 /** dri3_wait_for_sbc
494 * Wait for the completed swap buffer count to reach the specified
495 * target. Presumably the application knows that this will be reached with
496 * outstanding complete events, or we're going to be here awhile.
499 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
500 int64_t *msc
, int64_t *sbc
)
502 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
504 while (priv
->recv_sbc
< target_sbc
) {
505 if (!dri3_wait_for_event(pdraw
))
511 *sbc
= priv
->recv_sbc
;
516 * Asks the driver to flush any queued work necessary for serializing with the
517 * X command stream, and optionally the slightly more strict requirement of
518 * glFlush() equivalence (which would require flushing even if nothing had
519 * been drawn to a window system framebuffer, for example).
522 dri3_flush(struct dri3_screen
*psc
,
523 struct dri3_drawable
*draw
,
525 enum __DRI2throttleReason throttle_reason
)
527 struct glx_context
*gc
= __glXGetCurrentContext();
530 struct dri3_context
*dri3Ctx
= (struct dri3_context
*)gc
;
532 (*psc
->f
->flush_with_flags
)(dri3Ctx
->driContext
, draw
->driDrawable
, flags
, throttle_reason
);
536 static xcb_gcontext_t
537 dri3_drawable_gc(struct dri3_drawable
*priv
)
541 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
545 (priv
->gc
= xcb_generate_id(c
)),
546 priv
->base
.xDrawable
,
547 XCB_GC_GRAPHICS_EXPOSURES
,
553 static struct dri3_buffer
*
554 dri3_back_buffer(struct dri3_drawable
*priv
)
556 return priv
->buffers
[DRI3_BACK_ID(priv
->cur_back
)];
559 static struct dri3_buffer
*
560 dri3_fake_front_buffer(struct dri3_drawable
*priv
)
562 return priv
->buffers
[DRI3_FRONT_ID
];
566 dri3_copy_area (xcb_connection_t
*c
/**< */,
567 xcb_drawable_t src_drawable
/**< */,
568 xcb_drawable_t dst_drawable
/**< */,
569 xcb_gcontext_t gc
/**< */,
570 int16_t src_x
/**< */,
571 int16_t src_y
/**< */,
572 int16_t dst_x
/**< */,
573 int16_t dst_y
/**< */,
574 uint16_t width
/**< */,
575 uint16_t height
/**< */)
577 xcb_void_cookie_t cookie
;
579 cookie
= xcb_copy_area_checked(c
,
589 xcb_discard_reply(c
, cookie
.sequence
);
593 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
594 int width
, int height
,
597 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
598 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->psc
;
599 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
600 struct dri3_buffer
*back
= dri3_back_buffer(priv
);
604 /* Check we have the right attachments */
605 if (!priv
->have_back
|| priv
->is_pixmap
)
608 flags
= __DRI2_FLUSH_DRAWABLE
;
610 flags
|= __DRI2_FLUSH_CONTEXT
;
611 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
613 y
= priv
->height
- y
- height
;
615 dri3_fence_reset(c
, back
);
617 dri3_back_buffer(priv
)->pixmap
,
618 priv
->base
.xDrawable
,
619 dri3_drawable_gc(priv
),
620 x
, y
, x
, y
, width
, height
);
621 dri3_fence_trigger(c
, back
);
622 /* Refresh the fake front (if present) after we just damaged the real
625 if (priv
->have_fake_front
) {
626 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
628 dri3_back_buffer(priv
)->pixmap
,
629 dri3_fake_front_buffer(priv
)->pixmap
,
630 dri3_drawable_gc(priv
),
631 x
, y
, x
, y
, width
, height
);
632 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
633 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
635 dri3_fence_await(c
, back
);
639 dri3_copy_drawable(struct dri3_drawable
*priv
, Drawable dest
, Drawable src
)
641 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
642 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
644 dri3_flush(psc
, priv
, __DRI2_FLUSH_DRAWABLE
, 0);
646 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
649 dri3_drawable_gc(priv
),
650 0, 0, 0, 0, priv
->width
, priv
->height
);
651 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
652 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
656 dri3_wait_x(struct glx_context
*gc
)
658 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
659 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
661 if (priv
== NULL
|| !priv
->have_fake_front
)
664 dri3_copy_drawable(priv
, dri3_fake_front_buffer(priv
)->pixmap
, priv
->base
.xDrawable
);
668 dri3_wait_gl(struct glx_context
*gc
)
670 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
671 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
673 if (priv
== NULL
|| !priv
->have_fake_front
)
676 dri3_copy_drawable(priv
, priv
->base
.xDrawable
, dri3_fake_front_buffer(priv
)->pixmap
);
680 * Called by the driver when it needs to update the real front buffer with the
681 * contents of its fake front buffer.
684 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
686 struct glx_context
*gc
;
687 struct dri3_drawable
*pdraw
= loaderPrivate
;
688 struct dri3_screen
*psc
;
693 if (!pdraw
->base
.psc
)
696 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
698 (void) __glXInitialize(psc
->base
.dpy
);
700 gc
= __glXGetCurrentContext();
702 dri3_flush(psc
, pdraw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
708 dri3_cpp_for_format(uint32_t format
) {
710 case __DRI_IMAGE_FORMAT_R8
:
712 case __DRI_IMAGE_FORMAT_RGB565
:
713 case __DRI_IMAGE_FORMAT_GR88
:
715 case __DRI_IMAGE_FORMAT_XRGB8888
:
716 case __DRI_IMAGE_FORMAT_ARGB8888
:
717 case __DRI_IMAGE_FORMAT_ABGR8888
:
718 case __DRI_IMAGE_FORMAT_XBGR8888
:
719 case __DRI_IMAGE_FORMAT_XRGB2101010
:
720 case __DRI_IMAGE_FORMAT_ARGB2101010
:
721 case __DRI_IMAGE_FORMAT_SARGB8
:
723 case __DRI_IMAGE_FORMAT_NONE
:
730 /** dri3_alloc_render_buffer
732 * Use the driver createImage function to construct a __DRIimage, then
733 * get a file descriptor for that and create an X pixmap from that
735 * Allocate an xshmfence for synchronization
737 static struct dri3_buffer
*
738 dri3_alloc_render_buffer(struct glx_screen
*glx_screen
, Drawable draw
,
739 unsigned int format
, int width
, int height
, int depth
)
741 struct dri3_screen
*psc
= (struct dri3_screen
*) glx_screen
;
742 Display
*dpy
= glx_screen
->dpy
;
743 struct dri3_buffer
*buffer
;
744 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
746 xcb_sync_fence_t sync_fence
;
747 struct xshmfence
*shm_fence
;
748 int buffer_fd
, fence_fd
;
751 /* Create an xshmfence object and
752 * prepare to send that to the X server
755 fence_fd
= xshmfence_alloc_shm();
758 shm_fence
= xshmfence_map_shm(fence_fd
);
759 if (shm_fence
== NULL
)
762 /* Allocate the image from the driver
764 buffer
= calloc(1, sizeof (struct dri3_buffer
));
768 buffer
->cpp
= dri3_cpp_for_format(format
);
772 buffer
->image
= (*psc
->image
->createImage
) (psc
->driScreen
,
775 __DRI_IMAGE_USE_SHARE
|__DRI_IMAGE_USE_SCANOUT
,
782 /* X wants the stride, so ask the image for it
784 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
))
785 goto no_buffer_attrib
;
787 buffer
->pitch
= stride
;
789 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_FD
, &buffer_fd
))
790 goto no_buffer_attrib
;
792 xcb_dri3_pixmap_from_buffer(c
,
793 (pixmap
= xcb_generate_id(c
)),
796 width
, height
, buffer
->pitch
,
797 depth
, buffer
->cpp
* 8,
800 xcb_dri3_fence_from_fd(c
,
802 (sync_fence
= xcb_generate_id(c
)),
806 buffer
->pixmap
= pixmap
;
807 buffer
->own_pixmap
= true;
808 buffer
->sync_fence
= sync_fence
;
809 buffer
->shm_fence
= shm_fence
;
810 buffer
->width
= width
;
811 buffer
->height
= height
;
813 /* Mark the buffer as idle
815 dri3_fence_set(buffer
);
820 (*psc
->image
->destroyImage
)(buffer
->image
);
824 xshmfence_unmap_shm(shm_fence
);
830 /** dri3_free_render_buffer
832 * Free everything associated with one render buffer including pixmap, fence
833 * stuff and the driver image
836 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
)
838 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
839 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
841 if (buffer
->own_pixmap
)
842 xcb_free_pixmap(c
, buffer
->pixmap
);
843 xcb_sync_destroy_fence(c
, buffer
->sync_fence
);
844 xshmfence_unmap_shm(buffer
->shm_fence
);
845 (*psc
->image
->destroyImage
)(buffer
->image
);
850 /** dri3_flush_present_events
852 * Process any present events that have been received from the X server
855 dri3_flush_present_events(struct dri3_drawable
*priv
)
857 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
859 /* Check to see if any configuration changes have occurred
860 * since we were last invoked
862 if (priv
->special_event
) {
863 xcb_generic_event_t
*ev
;
865 while ((ev
= xcb_poll_for_special_event(c
, priv
->special_event
)) != NULL
) {
866 xcb_present_generic_event_t
*ge
= (void *) ev
;
867 dri3_handle_present_event(priv
, ge
);
872 /** dri3_update_drawable
874 * Called the first time we use the drawable and then
875 * after we receive present configure notify events to
876 * track the geometry of the drawable
879 dri3_update_drawable(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
881 struct dri3_drawable
*priv
= loaderPrivate
;
882 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
884 /* First time through, go get the current drawable geometry
886 if (priv
->width
== 0 || priv
->height
== 0 || priv
->depth
== 0) {
887 xcb_get_geometry_cookie_t geom_cookie
;
888 xcb_get_geometry_reply_t
*geom_reply
;
889 xcb_void_cookie_t cookie
;
890 xcb_generic_error_t
*error
;
892 /* Try to select for input on the window.
894 * If the drawable is a window, this will get our events
897 * Otherwise, we'll get a BadWindow error back from this request which
898 * will let us know that the drawable is a pixmap instead.
902 cookie
= xcb_present_select_input_checked(c
,
903 (priv
->eid
= xcb_generate_id(c
)),
904 priv
->base
.xDrawable
,
905 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
906 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
907 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
909 /* Create an XCB event queue to hold present events outside of the usual
910 * application event queue
912 priv
->special_event
= xcb_register_for_special_xge(c
,
917 geom_cookie
= xcb_get_geometry(c
, priv
->base
.xDrawable
);
919 geom_reply
= xcb_get_geometry_reply(c
, geom_cookie
, NULL
);
924 priv
->width
= geom_reply
->width
;
925 priv
->height
= geom_reply
->height
;
926 priv
->depth
= geom_reply
->depth
;
927 priv
->is_pixmap
= false;
931 /* Check to see if our select input call failed. If it failed with a
932 * BadWindow error, then assume the drawable is a pixmap. Destroy the
933 * special event queue created above and mark the drawable as a pixmap
936 error
= xcb_request_check(c
, cookie
);
939 if (error
->error_code
!= BadWindow
) {
943 priv
->is_pixmap
= true;
944 xcb_unregister_for_special_event(c
, priv
->special_event
);
945 priv
->special_event
= NULL
;
948 dri3_flush_present_events(priv
);
952 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
953 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
954 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
955 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
958 image_format_to_fourcc(int format
)
961 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
963 case __DRI_IMAGE_FORMAT_SARGB8
: return __DRI_IMAGE_FOURCC_SARGB8888
;
964 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
965 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
966 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
967 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
968 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
973 /** dri3_get_pixmap_buffer
975 * Get the DRM object for a pixmap from the X server and
976 * wrap that with a __DRIimage structure using createImageFromFds
978 static struct dri3_buffer
*
979 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
,
981 enum dri3_buffer_type buffer_type
,
984 struct dri3_drawable
*pdraw
= loaderPrivate
;
985 int buf_id
= dri3_pixmap_buf_id(buffer_type
);
986 struct dri3_buffer
*buffer
= pdraw
->buffers
[buf_id
];
988 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
989 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
992 struct dri3_screen
*psc
;
994 xcb_sync_fence_t sync_fence
;
995 struct xshmfence
*shm_fence
;
997 __DRIimage
*image_planar
;
1003 pixmap
= pdraw
->base
.xDrawable
;
1004 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
1005 dpy
= psc
->base
.dpy
;
1006 c
= XGetXCBConnection(dpy
);
1008 buffer
= calloc(1, sizeof (struct dri3_buffer
));
1012 fence_fd
= xshmfence_alloc_shm();
1015 shm_fence
= xshmfence_map_shm(fence_fd
);
1016 if (shm_fence
== NULL
) {
1021 xcb_dri3_fence_from_fd(c
,
1023 (sync_fence
= xcb_generate_id(c
)),
1027 /* Get an FD for the pixmap object
1029 bp_cookie
= xcb_dri3_buffer_from_pixmap(c
, pixmap
);
1030 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(c
, bp_cookie
, NULL
);
1033 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
1035 stride
= bp_reply
->stride
;
1038 /* createImageFromFds creates a wrapper __DRIimage structure which
1039 * can deal with multiple planes for things like Yuv images. So, once
1040 * we've gotten the planar wrapper, pull the single plane out of it and
1041 * discard the wrapper.
1043 image_planar
= (*psc
->image
->createImageFromFds
) (psc
->driScreen
,
1046 image_format_to_fourcc(format
),
1048 &stride
, &offset
, buffer
);
1053 buffer
->image
= (*psc
->image
->fromPlanar
)(image_planar
, 0, buffer
);
1055 (*psc
->image
->destroyImage
)(image_planar
);
1060 buffer
->pixmap
= pixmap
;
1061 buffer
->own_pixmap
= false;
1062 buffer
->width
= bp_reply
->width
;
1063 buffer
->height
= bp_reply
->height
;
1064 buffer
->buffer_type
= buffer_type
;
1065 buffer
->shm_fence
= shm_fence
;
1066 buffer
->sync_fence
= sync_fence
;
1068 pdraw
->buffers
[buf_id
] = buffer
;
1072 xcb_sync_destroy_fence(c
, sync_fence
);
1073 xshmfence_unmap_shm(shm_fence
);
1082 * Find an idle back buffer. If there isn't one, then
1083 * wait for a present idle notify event from the X server
1086 dri3_find_back(xcb_connection_t
*c
, struct dri3_drawable
*priv
)
1089 xcb_generic_event_t
*ev
;
1090 xcb_present_generic_event_t
*ge
;
1093 for (b
= 0; b
< priv
->num_back
; b
++) {
1094 int id
= DRI3_BACK_ID((b
+ priv
->cur_back
) % priv
->num_back
);
1095 struct dri3_buffer
*buffer
= priv
->buffers
[id
];
1097 if (!buffer
|| !buffer
->busy
) {
1098 priv
->cur_back
= id
;
1103 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
1107 dri3_handle_present_event(priv
, ge
);
1113 * Find a front or back buffer, allocating new ones as necessary
1115 static struct dri3_buffer
*
1116 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1117 unsigned int format
,
1118 enum dri3_buffer_type buffer_type
,
1119 void *loaderPrivate
)
1121 struct dri3_drawable
*priv
= loaderPrivate
;
1122 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
1123 struct dri3_buffer
*buffer
;
1126 if (buffer_type
== dri3_buffer_back
) {
1127 buf_id
= dri3_find_back(c
, priv
);
1132 buf_id
= DRI3_FRONT_ID
;
1135 buffer
= priv
->buffers
[buf_id
];
1137 /* Allocate a new buffer if there isn't an old one, or if that
1138 * old one is the wrong size
1140 if (!buffer
|| buffer
->width
!= priv
->width
|| buffer
->height
!= priv
->height
) {
1141 struct dri3_buffer
*new_buffer
;
1143 /* Allocate the new buffers
1145 new_buffer
= dri3_alloc_render_buffer(priv
->base
.psc
,
1146 priv
->base
.xDrawable
,
1147 format
, priv
->width
, priv
->height
, priv
->depth
);
1151 /* When resizing, copy the contents of the old buffer, waiting for that
1152 * copy to complete using our fences before proceeding
1154 switch (buffer_type
) {
1155 case dri3_buffer_back
:
1157 dri3_fence_reset(c
, new_buffer
);
1158 dri3_fence_await(c
, buffer
);
1162 dri3_drawable_gc(priv
),
1163 0, 0, 0, 0, priv
->width
, priv
->height
);
1164 dri3_fence_trigger(c
, new_buffer
);
1165 dri3_free_render_buffer(priv
, buffer
);
1168 case dri3_buffer_front
:
1169 dri3_fence_reset(c
, new_buffer
);
1171 priv
->base
.xDrawable
,
1173 dri3_drawable_gc(priv
),
1174 0, 0, 0, 0, priv
->width
, priv
->height
);
1175 dri3_fence_trigger(c
, new_buffer
);
1178 buffer
= new_buffer
;
1179 buffer
->buffer_type
= buffer_type
;
1180 priv
->buffers
[buf_id
] = buffer
;
1182 dri3_fence_await(c
, buffer
);
1184 /* Return the requested buffer */
1188 /** dri3_free_buffers
1190 * Free the front bufffer or all of the back buffers. Used
1191 * when the application changes which buffers it needs
1194 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1195 enum dri3_buffer_type buffer_type
,
1196 void *loaderPrivate
)
1198 struct dri3_drawable
*priv
= loaderPrivate
;
1199 struct dri3_buffer
*buffer
;
1204 switch (buffer_type
) {
1205 case dri3_buffer_back
:
1206 first_id
= DRI3_BACK_ID(0);
1207 n_id
= DRI3_MAX_BACK
;
1209 case dri3_buffer_front
:
1210 first_id
= DRI3_FRONT_ID
;
1214 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1215 buffer
= priv
->buffers
[buf_id
];
1217 dri3_free_render_buffer(priv
, buffer
);
1218 priv
->buffers
[buf_id
] = NULL
;
1223 /** dri3_get_buffers
1225 * The published buffer allocation API.
1226 * Returns all of the necessary buffers, allocating
1230 dri3_get_buffers(__DRIdrawable
*driDrawable
,
1231 unsigned int format
,
1233 void *loaderPrivate
,
1234 uint32_t buffer_mask
,
1235 struct __DRIimageList
*buffers
)
1237 struct dri3_drawable
*priv
= loaderPrivate
;
1238 struct dri3_buffer
*front
, *back
;
1240 buffers
->image_mask
= 0;
1241 buffers
->front
= NULL
;
1242 buffers
->back
= NULL
;
1247 if (!dri3_update_drawable(driDrawable
, loaderPrivate
))
1250 /* pixmaps always have front buffers */
1251 if (priv
->is_pixmap
)
1252 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1254 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1255 if (priv
->is_pixmap
)
1256 front
= dri3_get_pixmap_buffer(driDrawable
,
1261 front
= dri3_get_buffer(driDrawable
,
1269 dri3_free_buffers(driDrawable
, dri3_buffer_front
, loaderPrivate
);
1270 priv
->have_fake_front
= 0;
1273 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1274 back
= dri3_get_buffer(driDrawable
,
1280 priv
->have_back
= 1;
1282 dri3_free_buffers(driDrawable
, dri3_buffer_back
, loaderPrivate
);
1283 priv
->have_back
= 0;
1287 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1288 buffers
->front
= front
->image
;
1289 priv
->have_fake_front
= !priv
->is_pixmap
;
1293 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1294 buffers
->back
= back
->image
;
1297 priv
->stamp
= stamp
;
1302 /* The image loader extension record for DRI3
1304 static const __DRIimageLoaderExtension imageLoaderExtension
= {
1305 .base
= { __DRI_IMAGE_LOADER
, 1 },
1307 .getBuffers
= dri3_get_buffers
,
1308 .flushFrontBuffer
= dri3_flush_front_buffer
,
1311 static const __DRIextension
*loader_extensions
[] = {
1312 &imageLoaderExtension
.base
,
1313 &systemTimeExtension
.base
,
1317 /** dri3_swap_buffers
1319 * Make the current back buffer visible using the present extension
1322 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
1323 int64_t remainder
, Bool flush
)
1325 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1326 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1327 Display
*dpy
= priv
->base
.psc
->dpy
;
1328 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1329 int buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1332 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
1334 flags
|= __DRI2_FLUSH_CONTEXT
;
1335 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
1337 dri3_flush_present_events(priv
);
1339 if (priv
->buffers
[buf_id
] && !priv
->is_pixmap
) {
1340 dri3_fence_reset(c
, priv
->buffers
[buf_id
]);
1342 /* Compute when we want the frame shown by taking the last known successful
1343 * MSC and adding in a swap interval for each outstanding swap request
1346 if (target_msc
== 0)
1347 target_msc
= priv
->msc
+ priv
->swap_interval
* (priv
->send_sbc
- priv
->recv_sbc
);
1349 priv
->buffers
[buf_id
]->busy
= 1;
1350 priv
->buffers
[buf_id
]->last_swap
= priv
->send_sbc
;
1351 xcb_present_pixmap(c
,
1352 priv
->base
.xDrawable
,
1353 priv
->buffers
[buf_id
]->pixmap
,
1354 (uint32_t) priv
->send_sbc
,
1359 None
, /* target_crtc */
1361 priv
->buffers
[buf_id
]->sync_fence
,
1362 XCB_PRESENT_OPTION_NONE
,
1365 remainder
, 0, NULL
);
1366 ret
= (int64_t) priv
->send_sbc
;
1368 /* If there's a fake front, then copy the source back buffer
1369 * to the fake front to keep it up to date. This needs
1370 * to reset the fence and make future users block until
1371 * the X server is done copying the bits
1373 if (priv
->have_fake_front
) {
1374 dri3_fence_reset(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1376 priv
->buffers
[buf_id
]->pixmap
,
1377 priv
->buffers
[DRI3_FRONT_ID
]->pixmap
,
1378 dri3_drawable_gc(priv
),
1379 0, 0, 0, 0, priv
->width
, priv
->height
);
1380 dri3_fence_trigger(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1391 dri3_get_buffer_age(__GLXDRIdrawable
*pdraw
)
1393 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
1394 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1395 int back_id
= DRI3_BACK_ID(dri3_find_back(c
, priv
));
1397 if (back_id
< 0 || !priv
->buffers
[back_id
])
1400 if (priv
->buffers
[back_id
]->last_swap
!= 0)
1401 return priv
->send_sbc
- priv
->buffers
[back_id
]->last_swap
+ 1;
1408 * Wrapper around xcb_dri3_open
1411 dri3_open(Display
*dpy
,
1415 xcb_dri3_open_cookie_t cookie
;
1416 xcb_dri3_open_reply_t
*reply
;
1417 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1420 cookie
= xcb_dri3_open(c
,
1424 reply
= xcb_dri3_open_reply(c
, cookie
, NULL
);
1428 if (reply
->nfd
!= 1) {
1433 fd
= xcb_dri3_open_reply_fds(c
, reply
)[0];
1434 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1440 /** dri3_destroy_screen
1443 dri3_destroy_screen(struct glx_screen
*base
)
1445 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
1447 /* Free the direct rendering per screen data */
1448 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
1449 driDestroyConfigs(psc
->driver_configs
);
1454 /** dri3_set_swap_interval
1456 * Record the application swap interval specification,
1459 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
1461 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1462 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1463 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1466 psc
->config
->configQueryi(psc
->driScreen
,
1467 "vblank_mode", &vblank_mode
);
1469 switch (vblank_mode
) {
1470 case DRI_CONF_VBLANK_NEVER
:
1472 return GLX_BAD_VALUE
;
1474 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1476 return GLX_BAD_VALUE
;
1482 priv
->swap_interval
= interval
;
1483 dri3_update_num_back(priv
);
1488 /** dri3_get_swap_interval
1490 * Return the stored swap interval
1493 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
1495 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1497 return priv
->swap_interval
;
1501 dri3_bind_tex_image(Display
* dpy
,
1502 GLXDrawable drawable
,
1503 int buffer
, const int *attrib_list
)
1505 struct glx_context
*gc
= __glXGetCurrentContext();
1506 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1507 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1508 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1509 struct dri3_screen
*psc
;
1511 if (pdraw
!= NULL
) {
1512 psc
= (struct dri3_screen
*) base
->psc
;
1514 (*psc
->f
->invalidate
)(pdraw
->driDrawable
);
1518 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1519 pdraw
->base
.textureTarget
,
1520 pdraw
->base
.textureFormat
,
1521 pdraw
->driDrawable
);
1526 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1528 struct glx_context
*gc
= __glXGetCurrentContext();
1529 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1530 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1531 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1532 struct dri3_screen
*psc
;
1534 if (pdraw
!= NULL
) {
1535 psc
= (struct dri3_screen
*) base
->psc
;
1537 if (psc
->texBuffer
->base
.version
>= 3 &&
1538 psc
->texBuffer
->releaseTexBuffer
!= NULL
)
1539 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1540 pdraw
->base
.textureTarget
,
1541 pdraw
->driDrawable
);
1545 static const struct glx_context_vtable dri3_context_vtable
= {
1546 .destroy
= dri3_destroy_context
,
1547 .bind
= dri3_bind_context
,
1548 .unbind
= dri3_unbind_context
,
1549 .wait_gl
= dri3_wait_gl
,
1550 .wait_x
= dri3_wait_x
,
1551 .use_x_font
= DRI_glXUseXFont
,
1552 .bind_tex_image
= dri3_bind_tex_image
,
1553 .release_tex_image
= dri3_release_tex_image
,
1554 .get_proc_address
= NULL
,
1557 /** dri3_bind_extensions
1559 * Enable all of the extensions supported on DRI3
1562 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
1563 const char *driverName
)
1565 const __DRIextension
**extensions
;
1569 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1571 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1572 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1573 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1574 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1575 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
1577 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
1579 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1580 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1582 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1583 __glXEnableDirectExtension(&psc
->base
,
1584 "GLX_EXT_create_context_es2_profile");
1586 for (i
= 0; extensions
[i
]; i
++) {
1587 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1588 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1589 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1592 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1593 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1594 /* internal driver extension, no GL extension exposed */
1597 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1598 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1600 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1601 __glXEnableDirectExtension(&psc
->base
,
1602 "GLX_ARB_create_context_robustness");
1604 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
1605 psc
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
1606 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_query_renderer");
1611 static const struct glx_screen_vtable dri3_screen_vtable
= {
1612 .create_context
= dri3_create_context
,
1613 .create_context_attribs
= dri3_create_context_attribs
,
1614 .query_renderer_integer
= dri3_query_renderer_integer
,
1615 .query_renderer_string
= dri3_query_renderer_string
,
1618 /** dri3_create_screen
1620 * Initialize DRI3 on the specified screen.
1622 * Opens the DRI device, locates the appropriate DRI driver
1625 * Checks to see if the driver supports the necessary extensions
1627 * Initializes the driver for the screen and sets up our structures
1630 static struct glx_screen
*
1631 dri3_create_screen(int screen
, struct glx_display
* priv
)
1633 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
1634 const __DRIconfig
**driver_configs
;
1635 const __DRIextension
**extensions
;
1636 const struct dri3_display
*const pdp
= (struct dri3_display
*)
1638 struct dri3_screen
*psc
;
1639 __GLXDRIscreen
*psp
;
1640 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1641 char *driverName
, *deviceName
;
1644 psc
= calloc(1, sizeof *psc
);
1650 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1655 psc
->fd
= dri3_open(priv
->dpy
, RootWindow(priv
->dpy
, screen
), None
);
1657 int conn_error
= xcb_connection_has_error(c
);
1659 glx_screen_cleanup(&psc
->base
);
1661 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
1664 ErrorMessageF("Connection closed during DRI3 initialization failure");
1670 driverName
= loader_get_driver_for_fd(psc
->fd
, 0);
1672 ErrorMessageF("No driver found\n");
1676 psc
->driver
= driOpenDriver(driverName
);
1677 if (psc
->driver
== NULL
) {
1678 ErrorMessageF("driver pointer missing\n");
1682 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1683 if (extensions
== NULL
)
1686 for (i
= 0; extensions
[i
]; i
++) {
1687 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1688 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1689 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
1690 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
1694 if (psc
->core
== NULL
) {
1695 ErrorMessageF("core dri driver extension not found\n");
1699 if (psc
->image_driver
== NULL
) {
1700 ErrorMessageF("image driver extension not found\n");
1705 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
1706 pdp
->loader_extensions
,
1708 &driver_configs
, psc
);
1710 if (psc
->driScreen
== NULL
) {
1711 ErrorMessageF("failed to create dri screen\n");
1715 extensions
= (*psc
->core
->getExtensions
)(psc
->driScreen
);
1717 for (i
= 0; extensions
[i
]; i
++) {
1718 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
1719 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
1722 if (psc
->image
== NULL
) {
1723 ErrorMessageF("image extension not found\n");
1727 dri3_bind_extensions(psc
, priv
, driverName
);
1729 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
1730 ErrorMessageF("Version 4 or later of flush extension not found\n");
1734 if (!psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
1735 !psc
->texBuffer
->setTexBuffer2
)
1737 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1741 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1742 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1744 if (!configs
|| !visuals
)
1747 glx_config_destroy_list(psc
->base
.configs
);
1748 psc
->base
.configs
= configs
;
1749 glx_config_destroy_list(psc
->base
.visuals
);
1750 psc
->base
.visuals
= visuals
;
1752 psc
->driver_configs
= driver_configs
;
1754 psc
->base
.vtable
= &dri3_screen_vtable
;
1756 psc
->base
.driScreen
= psp
;
1757 psp
->destroyScreen
= dri3_destroy_screen
;
1758 psp
->createDrawable
= dri3_create_drawable
;
1759 psp
->swapBuffers
= dri3_swap_buffers
;
1761 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
1762 psp
->waitForMSC
= dri3_wait_for_msc
;
1763 psp
->waitForSBC
= dri3_wait_for_sbc
;
1764 psp
->setSwapInterval
= dri3_set_swap_interval
;
1765 psp
->getSwapInterval
= dri3_get_swap_interval
;
1766 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1768 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
1769 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1771 psp
->getBufferAge
= dri3_get_buffer_age
;
1772 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_buffer_age");
1780 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1783 glx_config_destroy_list(configs
);
1785 glx_config_destroy_list(visuals
);
1787 psc
->core
->destroyScreen(psc
->driScreen
);
1788 psc
->driScreen
= NULL
;
1792 dlclose(psc
->driver
);
1796 glx_screen_cleanup(&psc
->base
);
1802 /** dri_destroy_display
1804 * Called from __glXFreeDisplayPrivate.
1807 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
1812 /** dri3_create_display
1814 * Allocate, initialize and return a __DRIdisplayPrivate object.
1815 * This is called from __glXInitialize() when we are given a new
1816 * display pointer. This is public to that function, but hidden from
1819 _X_HIDDEN __GLXDRIdisplay
*
1820 dri3_create_display(Display
* dpy
)
1822 struct dri3_display
*pdp
;
1823 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1824 xcb_dri3_query_version_cookie_t dri3_cookie
;
1825 xcb_dri3_query_version_reply_t
*dri3_reply
;
1826 xcb_present_query_version_cookie_t present_cookie
;
1827 xcb_present_query_version_reply_t
*present_reply
;
1828 xcb_generic_error_t
*error
;
1829 const xcb_query_extension_reply_t
*extension
;
1831 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1832 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1834 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1835 if (!(extension
&& extension
->present
))
1838 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1839 if (!(extension
&& extension
->present
))
1842 dri3_cookie
= xcb_dri3_query_version(c
,
1843 XCB_DRI3_MAJOR_VERSION
,
1844 XCB_DRI3_MINOR_VERSION
);
1847 present_cookie
= xcb_present_query_version(c
,
1848 XCB_PRESENT_MAJOR_VERSION
,
1849 XCB_PRESENT_MINOR_VERSION
);
1851 pdp
= malloc(sizeof *pdp
);
1855 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1861 pdp
->dri3Major
= dri3_reply
->major_version
;
1862 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1865 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1866 if (!present_reply
) {
1870 pdp
->presentMajor
= present_reply
->major_version
;
1871 pdp
->presentMinor
= present_reply
->minor_version
;
1872 free(present_reply
);
1874 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1875 pdp
->base
.createScreen
= dri3_create_screen
;
1877 loader_set_logger(dri_message
);
1879 pdp
->loader_extensions
= loader_extensions
;
1887 #endif /* GLX_DIRECT_RENDERING */