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_event(__GLXDRIdrawable
*pdraw
)
404 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
405 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
406 xcb_generic_event_t
*ev
;
407 xcb_present_generic_event_t
*ge
;
409 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
413 dri3_handle_present_event(priv
, ge
);
417 /** dri3_wait_for_msc
419 * Get the X server to send an event when the target msc/divisor/remainder is
423 dri3_wait_for_msc(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
424 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
426 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
427 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
430 /* Ask for the an event for the target MSC */
431 msc_serial
= ++priv
->send_msc_serial
;
432 xcb_present_notify_msc(c
,
433 priv
->base
.xDrawable
,
441 /* Wait for the event */
442 if (priv
->special_event
) {
443 while ((int32_t) (msc_serial
- priv
->recv_msc_serial
) > 0) {
444 if (!dri3_wait_for_event(pdraw
))
451 *sbc
= priv
->recv_sbc
;
456 /** dri3_drawable_get_msc
458 * Return the current UST/MSC/SBC triplet by asking the server
462 dri3_drawable_get_msc(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
463 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
465 return dri3_wait_for_msc(pdraw
, 0, 0, 0, ust
, msc
,sbc
);
468 /** dri3_wait_for_sbc
470 * Wait for the completed swap buffer count to reach the specified
471 * target. Presumably the application knows that this will be reached with
472 * outstanding complete events, or we're going to be here awhile.
475 dri3_wait_for_sbc(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
476 int64_t *msc
, int64_t *sbc
)
478 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
480 while (priv
->recv_sbc
< target_sbc
) {
481 if (!dri3_wait_for_event(pdraw
))
487 *sbc
= priv
->recv_sbc
;
492 * Asks the driver to flush any queued work necessary for serializing with the
493 * X command stream, and optionally the slightly more strict requirement of
494 * glFlush() equivalence (which would require flushing even if nothing had
495 * been drawn to a window system framebuffer, for example).
498 dri3_flush(struct dri3_screen
*psc
,
499 struct dri3_drawable
*draw
,
501 enum __DRI2throttleReason throttle_reason
)
503 struct glx_context
*gc
= __glXGetCurrentContext();
506 struct dri3_context
*dri3Ctx
= (struct dri3_context
*)gc
;
508 (*psc
->f
->flush_with_flags
)(dri3Ctx
->driContext
, draw
->driDrawable
, flags
, throttle_reason
);
512 static xcb_gcontext_t
513 dri3_drawable_gc(struct dri3_drawable
*priv
)
517 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
521 (priv
->gc
= xcb_generate_id(c
)),
522 priv
->base
.xDrawable
,
523 XCB_GC_GRAPHICS_EXPOSURES
,
529 static struct dri3_buffer
*
530 dri3_back_buffer(struct dri3_drawable
*priv
)
532 return priv
->buffers
[DRI3_BACK_ID(priv
->cur_back
)];
535 static struct dri3_buffer
*
536 dri3_fake_front_buffer(struct dri3_drawable
*priv
)
538 return priv
->buffers
[DRI3_FRONT_ID
];
542 dri3_copy_area (xcb_connection_t
*c
/**< */,
543 xcb_drawable_t src_drawable
/**< */,
544 xcb_drawable_t dst_drawable
/**< */,
545 xcb_gcontext_t gc
/**< */,
546 int16_t src_x
/**< */,
547 int16_t src_y
/**< */,
548 int16_t dst_x
/**< */,
549 int16_t dst_y
/**< */,
550 uint16_t width
/**< */,
551 uint16_t height
/**< */)
553 xcb_void_cookie_t cookie
;
555 cookie
= xcb_copy_area_checked(c
,
565 xcb_discard_reply(c
, cookie
.sequence
);
569 dri3_copy_sub_buffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
570 int width
, int height
,
573 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
574 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->psc
;
575 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
576 struct dri3_buffer
*back
= dri3_back_buffer(priv
);
580 /* Check we have the right attachments */
581 if (!priv
->have_back
|| priv
->is_pixmap
)
584 flags
= __DRI2_FLUSH_DRAWABLE
;
586 flags
|= __DRI2_FLUSH_CONTEXT
;
587 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
589 y
= priv
->height
- y
- height
;
591 dri3_fence_reset(c
, back
);
593 dri3_back_buffer(priv
)->pixmap
,
594 priv
->base
.xDrawable
,
595 dri3_drawable_gc(priv
),
596 x
, y
, x
, y
, width
, height
);
597 dri3_fence_trigger(c
, back
);
598 /* Refresh the fake front (if present) after we just damaged the real
601 if (priv
->have_fake_front
) {
602 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
604 dri3_back_buffer(priv
)->pixmap
,
605 dri3_fake_front_buffer(priv
)->pixmap
,
606 dri3_drawable_gc(priv
),
607 x
, y
, x
, y
, width
, height
);
608 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
609 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
611 dri3_fence_await(c
, back
);
615 dri3_copy_drawable(struct dri3_drawable
*priv
, Drawable dest
, Drawable src
)
617 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
618 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
620 dri3_flush(psc
, priv
, __DRI2_FLUSH_DRAWABLE
, 0);
622 dri3_fence_reset(c
, dri3_fake_front_buffer(priv
));
625 dri3_drawable_gc(priv
),
626 0, 0, 0, 0, priv
->width
, priv
->height
);
627 dri3_fence_trigger(c
, dri3_fake_front_buffer(priv
));
628 dri3_fence_await(c
, dri3_fake_front_buffer(priv
));
632 dri3_wait_x(struct glx_context
*gc
)
634 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
635 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
637 if (priv
== NULL
|| !priv
->have_fake_front
)
640 dri3_copy_drawable(priv
, dri3_fake_front_buffer(priv
)->pixmap
, priv
->base
.xDrawable
);
644 dri3_wait_gl(struct glx_context
*gc
)
646 struct dri3_drawable
*priv
= (struct dri3_drawable
*)
647 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
649 if (priv
== NULL
|| !priv
->have_fake_front
)
652 dri3_copy_drawable(priv
, priv
->base
.xDrawable
, dri3_fake_front_buffer(priv
)->pixmap
);
656 * Called by the driver when it needs to update the real front buffer with the
657 * contents of its fake front buffer.
660 dri3_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
662 struct glx_context
*gc
;
663 struct dri3_drawable
*pdraw
= loaderPrivate
;
664 struct dri3_screen
*psc
;
669 if (!pdraw
->base
.psc
)
672 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
674 (void) __glXInitialize(psc
->base
.dpy
);
676 gc
= __glXGetCurrentContext();
678 dri3_flush(psc
, pdraw
, __DRI2_FLUSH_DRAWABLE
, __DRI2_THROTTLE_FLUSHFRONT
);
684 dri3_cpp_for_format(uint32_t format
) {
686 case __DRI_IMAGE_FORMAT_R8
:
688 case __DRI_IMAGE_FORMAT_RGB565
:
689 case __DRI_IMAGE_FORMAT_GR88
:
691 case __DRI_IMAGE_FORMAT_XRGB8888
:
692 case __DRI_IMAGE_FORMAT_ARGB8888
:
693 case __DRI_IMAGE_FORMAT_ABGR8888
:
694 case __DRI_IMAGE_FORMAT_XBGR8888
:
695 case __DRI_IMAGE_FORMAT_XRGB2101010
:
696 case __DRI_IMAGE_FORMAT_ARGB2101010
:
697 case __DRI_IMAGE_FORMAT_SARGB8
:
699 case __DRI_IMAGE_FORMAT_NONE
:
706 /** dri3_alloc_render_buffer
708 * Use the driver createImage function to construct a __DRIimage, then
709 * get a file descriptor for that and create an X pixmap from that
711 * Allocate an xshmfence for synchronization
713 static struct dri3_buffer
*
714 dri3_alloc_render_buffer(struct glx_screen
*glx_screen
, Drawable draw
,
715 unsigned int format
, int width
, int height
, int depth
)
717 struct dri3_screen
*psc
= (struct dri3_screen
*) glx_screen
;
718 Display
*dpy
= glx_screen
->dpy
;
719 struct dri3_buffer
*buffer
;
720 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
722 xcb_sync_fence_t sync_fence
;
723 struct xshmfence
*shm_fence
;
724 int buffer_fd
, fence_fd
;
727 /* Create an xshmfence object and
728 * prepare to send that to the X server
731 fence_fd
= xshmfence_alloc_shm();
734 shm_fence
= xshmfence_map_shm(fence_fd
);
735 if (shm_fence
== NULL
)
738 /* Allocate the image from the driver
740 buffer
= calloc(1, sizeof (struct dri3_buffer
));
744 buffer
->cpp
= dri3_cpp_for_format(format
);
748 buffer
->image
= (*psc
->image
->createImage
) (psc
->driScreen
,
751 __DRI_IMAGE_USE_SHARE
|__DRI_IMAGE_USE_SCANOUT
,
758 /* X wants the stride, so ask the image for it
760 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
))
761 goto no_buffer_attrib
;
763 buffer
->pitch
= stride
;
765 if (!(*psc
->image
->queryImage
)(buffer
->image
, __DRI_IMAGE_ATTRIB_FD
, &buffer_fd
))
766 goto no_buffer_attrib
;
768 xcb_dri3_pixmap_from_buffer(c
,
769 (pixmap
= xcb_generate_id(c
)),
772 width
, height
, buffer
->pitch
,
773 depth
, buffer
->cpp
* 8,
776 xcb_dri3_fence_from_fd(c
,
778 (sync_fence
= xcb_generate_id(c
)),
782 buffer
->pixmap
= pixmap
;
783 buffer
->own_pixmap
= true;
784 buffer
->sync_fence
= sync_fence
;
785 buffer
->shm_fence
= shm_fence
;
786 buffer
->width
= width
;
787 buffer
->height
= height
;
789 /* Mark the buffer as idle
791 dri3_fence_set(buffer
);
796 (*psc
->image
->destroyImage
)(buffer
->image
);
800 xshmfence_unmap_shm(shm_fence
);
806 /** dri3_free_render_buffer
808 * Free everything associated with one render buffer including pixmap, fence
809 * stuff and the driver image
812 dri3_free_render_buffer(struct dri3_drawable
*pdraw
, struct dri3_buffer
*buffer
)
814 struct dri3_screen
*psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
815 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->base
.psc
->dpy
);
817 if (buffer
->own_pixmap
)
818 xcb_free_pixmap(c
, buffer
->pixmap
);
819 xcb_sync_destroy_fence(c
, buffer
->sync_fence
);
820 xshmfence_unmap_shm(buffer
->shm_fence
);
821 (*psc
->image
->destroyImage
)(buffer
->image
);
826 /** dri3_flush_present_events
828 * Process any present events that have been received from the X server
831 dri3_flush_present_events(struct dri3_drawable
*priv
)
833 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
835 /* Check to see if any configuration changes have occurred
836 * since we were last invoked
838 if (priv
->special_event
) {
839 xcb_generic_event_t
*ev
;
841 while ((ev
= xcb_poll_for_special_event(c
, priv
->special_event
)) != NULL
) {
842 xcb_present_generic_event_t
*ge
= (void *) ev
;
843 dri3_handle_present_event(priv
, ge
);
848 /** dri3_update_drawable
850 * Called the first time we use the drawable and then
851 * after we receive present configure notify events to
852 * track the geometry of the drawable
855 dri3_update_drawable(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
857 struct dri3_drawable
*priv
= loaderPrivate
;
858 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
860 /* First time through, go get the current drawable geometry
862 if (priv
->width
== 0 || priv
->height
== 0 || priv
->depth
== 0) {
863 xcb_get_geometry_cookie_t geom_cookie
;
864 xcb_get_geometry_reply_t
*geom_reply
;
865 xcb_void_cookie_t cookie
;
866 xcb_generic_error_t
*error
;
868 /* Try to select for input on the window.
870 * If the drawable is a window, this will get our events
873 * Otherwise, we'll get a BadWindow error back from this request which
874 * will let us know that the drawable is a pixmap instead.
878 cookie
= xcb_present_select_input_checked(c
,
879 (priv
->eid
= xcb_generate_id(c
)),
880 priv
->base
.xDrawable
,
881 XCB_PRESENT_EVENT_MASK_CONFIGURE_NOTIFY
|
882 XCB_PRESENT_EVENT_MASK_COMPLETE_NOTIFY
|
883 XCB_PRESENT_EVENT_MASK_IDLE_NOTIFY
);
885 /* Create an XCB event queue to hold present events outside of the usual
886 * application event queue
888 priv
->special_event
= xcb_register_for_special_xge(c
,
893 geom_cookie
= xcb_get_geometry(c
, priv
->base
.xDrawable
);
895 geom_reply
= xcb_get_geometry_reply(c
, geom_cookie
, NULL
);
900 priv
->width
= geom_reply
->width
;
901 priv
->height
= geom_reply
->height
;
902 priv
->depth
= geom_reply
->depth
;
903 priv
->is_pixmap
= false;
907 /* Check to see if our select input call failed. If it failed with a
908 * BadWindow error, then assume the drawable is a pixmap. Destroy the
909 * special event queue created above and mark the drawable as a pixmap
912 error
= xcb_request_check(c
, cookie
);
915 if (error
->error_code
!= BadWindow
) {
919 priv
->is_pixmap
= true;
920 xcb_unregister_for_special_event(c
, priv
->special_event
);
921 priv
->special_event
= NULL
;
924 dri3_flush_present_events(priv
);
928 /* the DRIimage createImage function takes __DRI_IMAGE_FORMAT codes, while
929 * the createImageFromFds call takes __DRI_IMAGE_FOURCC codes. To avoid
930 * complete confusion, just deal in __DRI_IMAGE_FORMAT codes for now and
931 * translate to __DRI_IMAGE_FOURCC codes in the call to createImageFromFds
934 image_format_to_fourcc(int format
)
937 /* Convert from __DRI_IMAGE_FORMAT to __DRI_IMAGE_FOURCC (sigh) */
939 case __DRI_IMAGE_FORMAT_RGB565
: return __DRI_IMAGE_FOURCC_RGB565
;
940 case __DRI_IMAGE_FORMAT_XRGB8888
: return __DRI_IMAGE_FOURCC_XRGB8888
;
941 case __DRI_IMAGE_FORMAT_ARGB8888
: return __DRI_IMAGE_FOURCC_ARGB8888
;
942 case __DRI_IMAGE_FORMAT_ABGR8888
: return __DRI_IMAGE_FOURCC_ABGR8888
;
943 case __DRI_IMAGE_FORMAT_XBGR8888
: return __DRI_IMAGE_FOURCC_XBGR8888
;
948 /** dri3_get_pixmap_buffer
950 * Get the DRM object for a pixmap from the X server and
951 * wrap that with a __DRIimage structure using createImageFromFds
953 static struct dri3_buffer
*
954 dri3_get_pixmap_buffer(__DRIdrawable
*driDrawable
,
956 enum dri3_buffer_type buffer_type
,
959 struct dri3_drawable
*pdraw
= loaderPrivate
;
960 int buf_id
= dri3_pixmap_buf_id(buffer_type
);
961 struct dri3_buffer
*buffer
= pdraw
->buffers
[buf_id
];
963 xcb_dri3_buffer_from_pixmap_cookie_t bp_cookie
;
964 xcb_dri3_buffer_from_pixmap_reply_t
*bp_reply
;
967 struct dri3_screen
*psc
;
969 xcb_sync_fence_t sync_fence
;
970 struct xshmfence
*shm_fence
;
972 __DRIimage
*image_planar
;
978 pixmap
= pdraw
->base
.xDrawable
;
979 psc
= (struct dri3_screen
*) pdraw
->base
.psc
;
981 c
= XGetXCBConnection(dpy
);
983 buffer
= calloc(1, sizeof (struct dri3_buffer
));
987 fence_fd
= xshmfence_alloc_shm();
990 shm_fence
= xshmfence_map_shm(fence_fd
);
991 if (shm_fence
== NULL
) {
996 xcb_dri3_fence_from_fd(c
,
998 (sync_fence
= xcb_generate_id(c
)),
1002 /* Get an FD for the pixmap object
1004 bp_cookie
= xcb_dri3_buffer_from_pixmap(c
, pixmap
);
1005 bp_reply
= xcb_dri3_buffer_from_pixmap_reply(c
, bp_cookie
, NULL
);
1008 fds
= xcb_dri3_buffer_from_pixmap_reply_fds(c
, bp_reply
);
1010 stride
= bp_reply
->stride
;
1013 /* createImageFromFds creates a wrapper __DRIimage structure which
1014 * can deal with multiple planes for things like Yuv images. So, once
1015 * we've gotten the planar wrapper, pull the single plane out of it and
1016 * discard the wrapper.
1018 image_planar
= (*psc
->image
->createImageFromFds
) (psc
->driScreen
,
1021 image_format_to_fourcc(format
),
1023 &stride
, &offset
, buffer
);
1028 buffer
->image
= (*psc
->image
->fromPlanar
)(image_planar
, 0, buffer
);
1030 (*psc
->image
->destroyImage
)(image_planar
);
1035 buffer
->pixmap
= pixmap
;
1036 buffer
->own_pixmap
= false;
1037 buffer
->width
= bp_reply
->width
;
1038 buffer
->height
= bp_reply
->height
;
1039 buffer
->buffer_type
= buffer_type
;
1040 buffer
->shm_fence
= shm_fence
;
1041 buffer
->sync_fence
= sync_fence
;
1043 pdraw
->buffers
[buf_id
] = buffer
;
1047 xcb_sync_destroy_fence(c
, sync_fence
);
1048 xshmfence_unmap_shm(shm_fence
);
1057 * Find an idle back buffer. If there isn't one, then
1058 * wait for a present idle notify event from the X server
1061 dri3_find_back(xcb_connection_t
*c
, struct dri3_drawable
*priv
)
1064 xcb_generic_event_t
*ev
;
1065 xcb_present_generic_event_t
*ge
;
1069 for (b
= 0; b
< DRI3_NUM_BACK
; b
++) {
1070 int id
= DRI3_BACK_ID(b
);
1071 struct dri3_buffer
*buffer
= priv
->buffers
[id
];
1078 ev
= xcb_wait_for_special_event(c
, priv
->special_event
);
1082 dri3_handle_present_event(priv
, ge
);
1088 * Find a front or back buffer, allocating new ones as necessary
1090 static struct dri3_buffer
*
1091 dri3_get_buffer(__DRIdrawable
*driDrawable
,
1092 unsigned int format
,
1093 enum dri3_buffer_type buffer_type
,
1094 void *loaderPrivate
)
1096 struct dri3_drawable
*priv
= loaderPrivate
;
1097 xcb_connection_t
*c
= XGetXCBConnection(priv
->base
.psc
->dpy
);
1098 struct dri3_buffer
*buffer
;
1101 if (buffer_type
== dri3_buffer_back
) {
1102 int back
= dri3_find_back(c
, priv
);
1107 priv
->cur_back
= back
;
1108 buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1110 buf_id
= DRI3_FRONT_ID
;
1113 buffer
= priv
->buffers
[buf_id
];
1115 /* Allocate a new buffer if there isn't an old one, or if that
1116 * old one is the wrong size
1118 if (!buffer
|| buffer
->width
!= priv
->width
|| buffer
->height
!= priv
->height
) {
1119 struct dri3_buffer
*new_buffer
;
1121 /* Allocate the new buffers
1123 new_buffer
= dri3_alloc_render_buffer(priv
->base
.psc
,
1124 priv
->base
.xDrawable
,
1125 format
, priv
->width
, priv
->height
, priv
->depth
);
1129 /* When resizing, copy the contents of the old buffer, waiting for that
1130 * copy to complete using our fences before proceeding
1132 switch (buffer_type
) {
1133 case dri3_buffer_back
:
1135 dri3_fence_reset(c
, new_buffer
);
1136 dri3_fence_await(c
, buffer
);
1140 dri3_drawable_gc(priv
),
1141 0, 0, 0, 0, priv
->width
, priv
->height
);
1142 dri3_fence_trigger(c
, new_buffer
);
1143 dri3_free_render_buffer(priv
, buffer
);
1146 case dri3_buffer_front
:
1147 dri3_fence_reset(c
, new_buffer
);
1149 priv
->base
.xDrawable
,
1151 dri3_drawable_gc(priv
),
1152 0, 0, 0, 0, priv
->width
, priv
->height
);
1153 dri3_fence_trigger(c
, new_buffer
);
1156 buffer
= new_buffer
;
1157 buffer
->buffer_type
= buffer_type
;
1158 priv
->buffers
[buf_id
] = buffer
;
1160 dri3_fence_await(c
, buffer
);
1162 /* Return the requested buffer */
1166 /** dri3_free_buffers
1168 * Free the front bufffer or all of the back buffers. Used
1169 * when the application changes which buffers it needs
1172 dri3_free_buffers(__DRIdrawable
*driDrawable
,
1173 enum dri3_buffer_type buffer_type
,
1174 void *loaderPrivate
)
1176 struct dri3_drawable
*priv
= loaderPrivate
;
1177 struct dri3_buffer
*buffer
;
1182 switch (buffer_type
) {
1183 case dri3_buffer_back
:
1184 first_id
= DRI3_BACK_ID(0);
1185 n_id
= DRI3_NUM_BACK
;
1187 case dri3_buffer_front
:
1188 first_id
= DRI3_FRONT_ID
;
1192 for (buf_id
= first_id
; buf_id
< first_id
+ n_id
; buf_id
++) {
1193 buffer
= priv
->buffers
[buf_id
];
1195 dri3_free_render_buffer(priv
, buffer
);
1196 priv
->buffers
[buf_id
] = NULL
;
1201 /** dri3_get_buffers
1203 * The published buffer allocation API.
1204 * Returns all of the necessary buffers, allocating
1208 dri3_get_buffers(__DRIdrawable
*driDrawable
,
1209 unsigned int format
,
1211 void *loaderPrivate
,
1212 uint32_t buffer_mask
,
1213 struct __DRIimageList
*buffers
)
1215 struct dri3_drawable
*priv
= loaderPrivate
;
1216 struct dri3_buffer
*front
, *back
;
1218 buffers
->image_mask
= 0;
1219 buffers
->front
= NULL
;
1220 buffers
->back
= NULL
;
1225 if (!dri3_update_drawable(driDrawable
, loaderPrivate
))
1228 /* pixmaps always have front buffers */
1229 if (priv
->is_pixmap
)
1230 buffer_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1232 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
1233 if (priv
->is_pixmap
)
1234 front
= dri3_get_pixmap_buffer(driDrawable
,
1239 front
= dri3_get_buffer(driDrawable
,
1247 dri3_free_buffers(driDrawable
, dri3_buffer_front
, loaderPrivate
);
1248 priv
->have_fake_front
= 0;
1251 if (buffer_mask
& __DRI_IMAGE_BUFFER_BACK
) {
1252 back
= dri3_get_buffer(driDrawable
,
1258 priv
->have_back
= 1;
1260 dri3_free_buffers(driDrawable
, dri3_buffer_back
, loaderPrivate
);
1261 priv
->have_back
= 0;
1265 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
1266 buffers
->front
= front
->image
;
1267 priv
->have_fake_front
= !priv
->is_pixmap
;
1271 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_BACK
;
1272 buffers
->back
= back
->image
;
1275 priv
->stamp
= stamp
;
1280 /* The image loader extension record for DRI3
1282 static const __DRIimageLoaderExtension imageLoaderExtension
= {
1283 {__DRI_IMAGE_LOADER
, __DRI_IMAGE_LOADER_VERSION
},
1284 .getBuffers
= dri3_get_buffers
,
1285 .flushFrontBuffer
= dri3_flush_front_buffer
,
1288 /** dri3_swap_buffers
1290 * Make the current back buffer visible using the present extension
1293 dri3_swap_buffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
1294 int64_t remainder
, Bool flush
)
1296 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1297 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1298 Display
*dpy
= priv
->base
.psc
->dpy
;
1299 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1300 int buf_id
= DRI3_BACK_ID(priv
->cur_back
);
1303 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
1305 flags
|= __DRI2_FLUSH_CONTEXT
;
1306 dri3_flush(psc
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
1308 dri3_flush_present_events(priv
);
1310 if (priv
->buffers
[buf_id
] && !priv
->is_pixmap
) {
1311 dri3_fence_reset(c
, priv
->buffers
[buf_id
]);
1313 /* Compute when we want the frame shown by taking the last known successful
1314 * MSC and adding in a swap interval for each outstanding swap request
1317 if (target_msc
== 0)
1318 target_msc
= priv
->msc
+ priv
->swap_interval
* (priv
->send_sbc
- priv
->recv_sbc
);
1320 priv
->buffers
[buf_id
]->busy
= 1;
1321 xcb_present_pixmap(c
,
1322 priv
->base
.xDrawable
,
1323 priv
->buffers
[buf_id
]->pixmap
,
1324 (uint32_t) priv
->send_sbc
,
1329 None
, /* target_crtc */
1331 priv
->buffers
[buf_id
]->sync_fence
,
1332 XCB_PRESENT_OPTION_NONE
,
1335 remainder
, 0, NULL
);
1336 ret
= (int64_t) priv
->send_sbc
;
1338 /* If there's a fake front, then copy the source back buffer
1339 * to the fake front to keep it up to date. This needs
1340 * to reset the fence and make future users block until
1341 * the X server is done copying the bits
1343 if (priv
->have_fake_front
) {
1344 dri3_fence_reset(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1346 priv
->buffers
[buf_id
]->pixmap
,
1347 priv
->buffers
[DRI3_FRONT_ID
]->pixmap
,
1348 dri3_drawable_gc(priv
),
1349 0, 0, 0, 0, priv
->width
, priv
->height
);
1350 dri3_fence_trigger(c
, priv
->buffers
[DRI3_FRONT_ID
]);
1362 * Wrapper around xcb_dri3_open
1365 dri3_open(Display
*dpy
,
1369 xcb_dri3_open_cookie_t cookie
;
1370 xcb_dri3_open_reply_t
*reply
;
1371 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1374 cookie
= xcb_dri3_open(c
,
1378 reply
= xcb_dri3_open_reply(c
, cookie
, NULL
);
1382 if (reply
->nfd
!= 1) {
1387 fd
= xcb_dri3_open_reply_fds(c
, reply
)[0];
1388 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1394 /** dri3_destroy_screen
1397 dri3_destroy_screen(struct glx_screen
*base
)
1399 struct dri3_screen
*psc
= (struct dri3_screen
*) base
;
1401 /* Free the direct rendering per screen data */
1402 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
1403 driDestroyConfigs(psc
->driver_configs
);
1408 /** dri3_set_swap_interval
1410 * Record the application swap interval specification,
1413 dri3_set_swap_interval(__GLXDRIdrawable
*pdraw
, int interval
)
1415 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1416 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1417 struct dri3_screen
*psc
= (struct dri3_screen
*) priv
->base
.psc
;
1420 psc
->config
->configQueryi(psc
->driScreen
,
1421 "vblank_mode", &vblank_mode
);
1423 switch (vblank_mode
) {
1424 case DRI_CONF_VBLANK_NEVER
:
1426 return GLX_BAD_VALUE
;
1428 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1430 return GLX_BAD_VALUE
;
1436 priv
->swap_interval
= interval
;
1441 /** dri3_get_swap_interval
1443 * Return the stored swap interval
1446 dri3_get_swap_interval(__GLXDRIdrawable
*pdraw
)
1448 struct dri3_drawable
*priv
= (struct dri3_drawable
*) pdraw
;
1450 return priv
->swap_interval
;
1454 dri3_bind_tex_image(Display
* dpy
,
1455 GLXDrawable drawable
,
1456 int buffer
, const int *attrib_list
)
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 (*psc
->f
->invalidate
)(pdraw
->driDrawable
);
1471 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1472 pdraw
->base
.textureTarget
,
1473 pdraw
->base
.textureFormat
,
1474 pdraw
->driDrawable
);
1479 dri3_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1481 struct glx_context
*gc
= __glXGetCurrentContext();
1482 struct dri3_context
*pcp
= (struct dri3_context
*) gc
;
1483 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1484 struct dri3_drawable
*pdraw
= (struct dri3_drawable
*) base
;
1485 struct dri3_screen
*psc
;
1487 if (pdraw
!= NULL
) {
1488 psc
= (struct dri3_screen
*) base
->psc
;
1490 if (psc
->texBuffer
->releaseTexBuffer
)
1491 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1492 pdraw
->base
.textureTarget
,
1493 pdraw
->driDrawable
);
1497 static const struct glx_context_vtable dri3_context_vtable
= {
1498 dri3_destroy_context
,
1500 dri3_unbind_context
,
1504 dri3_bind_tex_image
,
1505 dri3_release_tex_image
,
1506 NULL
, /* get_proc_address */
1509 /** dri3_bind_extensions
1511 * Enable all of the extensions supported on DRI3
1514 dri3_bind_extensions(struct dri3_screen
*psc
, struct glx_display
* priv
,
1515 const char *driverName
)
1517 const __DRIextension
**extensions
;
1521 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1523 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1524 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1525 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1526 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1529 * GLX_INTEL_swap_event is broken on the server side, where it's
1530 * currently unconditionally enabled. This completely breaks
1531 * systems running on drivers which don't support that extension.
1532 * There's no way to test for its presence on this side, so instead
1533 * of disabling it unconditionally, just disable it for drivers
1534 * which are known to not support it, or for DDX drivers supporting
1535 * only an older (pre-ScheduleSwap) version of DRI2.
1537 * This is a hack which is required until:
1538 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1539 * is merged and updated xserver makes it's way into distros:
1541 // if (pdp->swapAvailable && strcmp(driverName, "vmwgfx") != 0) {
1542 // __glXEnableDirectExtension(&psc->base, "GLX_INTEL_swap_event");
1545 mask
= psc
->image_driver
->getAPIMask(psc
->driScreen
);
1547 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1548 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1550 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1551 __glXEnableDirectExtension(&psc
->base
,
1552 "GLX_EXT_create_context_es2_profile");
1554 for (i
= 0; extensions
[i
]; i
++) {
1555 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1556 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1557 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1560 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1561 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1562 /* internal driver extension, no GL extension exposed */
1565 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1566 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1568 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1569 __glXEnableDirectExtension(&psc
->base
,
1570 "GLX_ARB_create_context_robustness");
1574 static const struct glx_screen_vtable dri3_screen_vtable
= {
1575 dri3_create_context
,
1576 dri3_create_context_attribs
1579 /** dri3_create_screen
1581 * Initialize DRI3 on the specified screen.
1583 * Opens the DRI device, locates the appropriate DRI driver
1586 * Checks to see if the driver supports the necessary extensions
1588 * Initializes the driver for the screen and sets up our structures
1591 static struct glx_screen
*
1592 dri3_create_screen(int screen
, struct glx_display
* priv
)
1594 xcb_connection_t
*c
= XGetXCBConnection(priv
->dpy
);
1595 const __DRIconfig
**driver_configs
;
1596 const __DRIextension
**extensions
;
1597 const struct dri3_display
*const pdp
= (struct dri3_display
*)
1599 struct dri3_screen
*psc
;
1600 __GLXDRIscreen
*psp
;
1601 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1602 char *driverName
, *deviceName
;
1605 psc
= calloc(1, sizeof *psc
);
1611 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1616 psc
->fd
= dri3_open(priv
->dpy
, RootWindow(priv
->dpy
, screen
), None
);
1618 int conn_error
= xcb_connection_has_error(c
);
1620 glx_screen_cleanup(&psc
->base
);
1622 InfoMessageF("screen %d does not appear to be DRI3 capable\n", screen
);
1625 ErrorMessageF("Connection closed during DRI3 initialization failure");
1631 driverName
= loader_get_driver_for_fd(psc
->fd
, 0);
1633 ErrorMessageF("No driver found\n");
1637 psc
->driver
= driOpenDriver(driverName
);
1638 if (psc
->driver
== NULL
) {
1639 ErrorMessageF("driver pointer missing\n");
1643 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1644 if (extensions
== NULL
)
1647 for (i
= 0; extensions
[i
]; i
++) {
1648 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1649 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1650 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE_DRIVER
) == 0)
1651 psc
->image_driver
= (__DRIimageDriverExtension
*) extensions
[i
];
1655 if (psc
->core
== NULL
) {
1656 ErrorMessageF("core dri driver extension not found\n");
1660 if (psc
->image_driver
== NULL
) {
1661 ErrorMessageF("image driver extension not found\n");
1666 psc
->image_driver
->createNewScreen2(screen
, psc
->fd
,
1667 (const __DRIextension
**)
1668 &pdp
->loader_extensions
[0],
1670 &driver_configs
, psc
);
1672 if (psc
->driScreen
== NULL
) {
1673 ErrorMessageF("failed to create dri screen\n");
1677 extensions
= (*psc
->core
->getExtensions
)(psc
->driScreen
);
1679 for (i
= 0; extensions
[i
]; i
++) {
1680 if (strcmp(extensions
[i
]->name
, __DRI_IMAGE
) == 0)
1681 psc
->image
= (__DRIimageExtension
*) extensions
[i
];
1684 if (psc
->image
== NULL
) {
1685 ErrorMessageF("image extension not found\n");
1689 dri3_bind_extensions(psc
, priv
, driverName
);
1691 if (!psc
->f
|| psc
->f
->base
.version
< 4) {
1692 ErrorMessageF("Version 4 or later of flush extension not found\n");
1696 if (!psc
->texBuffer
|| psc
->texBuffer
->base
.version
< 2 ||
1697 !psc
->texBuffer
->setTexBuffer2
)
1699 ErrorMessageF("Version 2 or later of texBuffer extension not found\n");
1703 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1704 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1706 if (!configs
|| !visuals
)
1709 glx_config_destroy_list(psc
->base
.configs
);
1710 psc
->base
.configs
= configs
;
1711 glx_config_destroy_list(psc
->base
.visuals
);
1712 psc
->base
.visuals
= visuals
;
1714 psc
->driver_configs
= driver_configs
;
1716 psc
->base
.vtable
= &dri3_screen_vtable
;
1718 psc
->base
.driScreen
= psp
;
1719 psp
->destroyScreen
= dri3_destroy_screen
;
1720 psp
->createDrawable
= dri3_create_drawable
;
1721 psp
->swapBuffers
= dri3_swap_buffers
;
1723 psp
->getDrawableMSC
= dri3_drawable_get_msc
;
1724 psp
->waitForMSC
= dri3_wait_for_msc
;
1725 psp
->waitForSBC
= dri3_wait_for_sbc
;
1726 psp
->setSwapInterval
= dri3_set_swap_interval
;
1727 psp
->getSwapInterval
= dri3_get_swap_interval
;
1728 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1730 psp
->copySubBuffer
= dri3_copy_sub_buffer
;
1731 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1739 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1742 glx_config_destroy_list(configs
);
1744 glx_config_destroy_list(visuals
);
1746 psc
->core
->destroyScreen(psc
->driScreen
);
1747 psc
->driScreen
= NULL
;
1751 dlclose(psc
->driver
);
1755 glx_screen_cleanup(&psc
->base
);
1761 /** dri_destroy_display
1763 * Called from __glXFreeDisplayPrivate.
1766 dri3_destroy_display(__GLXDRIdisplay
* dpy
)
1771 /** dri3_create_display
1773 * Allocate, initialize and return a __DRIdisplayPrivate object.
1774 * This is called from __glXInitialize() when we are given a new
1775 * display pointer. This is public to that function, but hidden from
1778 _X_HIDDEN __GLXDRIdisplay
*
1779 dri3_create_display(Display
* dpy
)
1781 struct dri3_display
*pdp
;
1783 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1784 xcb_dri3_query_version_cookie_t dri3_cookie
;
1785 xcb_dri3_query_version_reply_t
*dri3_reply
;
1786 xcb_present_query_version_cookie_t present_cookie
;
1787 xcb_present_query_version_reply_t
*present_reply
;
1788 xcb_generic_error_t
*error
;
1789 const xcb_query_extension_reply_t
*extension
;
1791 xcb_prefetch_extension_data(c
, &xcb_dri3_id
);
1792 xcb_prefetch_extension_data(c
, &xcb_present_id
);
1794 extension
= xcb_get_extension_data(c
, &xcb_dri3_id
);
1795 if (!(extension
&& extension
->present
))
1798 extension
= xcb_get_extension_data(c
, &xcb_present_id
);
1799 if (!(extension
&& extension
->present
))
1802 dri3_cookie
= xcb_dri3_query_version(c
,
1803 XCB_DRI3_MAJOR_VERSION
,
1804 XCB_DRI3_MINOR_VERSION
);
1807 present_cookie
= xcb_present_query_version(c
,
1808 XCB_PRESENT_MAJOR_VERSION
,
1809 XCB_PRESENT_MINOR_VERSION
);
1811 pdp
= malloc(sizeof *pdp
);
1815 dri3_reply
= xcb_dri3_query_version_reply(c
, dri3_cookie
, &error
);
1821 pdp
->dri3Major
= dri3_reply
->major_version
;
1822 pdp
->dri3Minor
= dri3_reply
->minor_version
;
1825 present_reply
= xcb_present_query_version_reply(c
, present_cookie
, &error
);
1826 if (!present_reply
) {
1830 pdp
->presentMajor
= present_reply
->major_version
;
1831 pdp
->presentMinor
= present_reply
->minor_version
;
1832 free(present_reply
);
1834 pdp
->base
.destroyDisplay
= dri3_destroy_display
;
1835 pdp
->base
.createScreen
= dri3_create_screen
;
1837 loader_set_logger(dri_message
);
1840 pdp
->loader_extensions
[i
++] = &imageLoaderExtension
.base
;
1842 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1844 pdp
->loader_extensions
[i
++] = NULL
;
1852 #endif /* GLX_DIRECT_RENDERING */