2 * Copyright © 2008 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Soft-
6 * ware"), to deal in the Software without restriction, including without
7 * limitation the rights to use, copy, modify, merge, publish, distribute,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, provided that the above copyright
10 * notice(s) and this permission notice appear in all copies of the Soft-
11 * ware and that both the above copyright notice(s) and this permission
12 * notice appear in supporting documentation.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16 * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
17 * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
18 * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
19 * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
22 * MANCE OF THIS SOFTWARE.
24 * Except as contained in this notice, the name of a copyright holder shall
25 * not be used in advertising or otherwise to promote the sale, use or
26 * other dealings in this Software without prior written authorization of
27 * the copyright holder.
30 * Kristian Høgsberg (krh@redhat.com)
33 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
36 #include <X11/extensions/Xfixes.h>
37 #include <X11/Xlib-xcb.h>
41 #include "glxclient.h"
42 #include <X11/extensions/dri2proto.h>
47 #include <sys/types.h>
52 #include "dri_common.h"
54 /* From xmlpool/options.h, user exposed so should be stable */
55 #define DRI_CONF_VBLANK_NEVER 0
56 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
57 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
58 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
68 ** XFree86-DRI version information
74 int invalidateAvailable
;
76 __glxHashTable
*dri2Hash
;
78 const __DRIextension
*loader_extensions
[4];
82 struct glx_screen base
;
84 __DRIscreen
*driScreen
;
85 __GLXDRIscreen vtable
;
86 const __DRIdri2Extension
*dri2
;
87 const __DRIcoreExtension
*core
;
89 const __DRI2flushExtension
*f
;
90 const __DRI2configQueryExtension
*config
;
91 const __DRItexBufferExtension
*texBuffer
;
92 const __DRI2throttleExtension
*throttle
;
93 const __DRIconfig
**driver_configs
;
103 struct glx_context base
;
104 __DRIcontext
*driContext
;
109 __GLXDRIdrawable base
;
110 __DRIdrawable
*driDrawable
;
111 __DRIbuffer buffers
[5];
118 uint64_t previous_time
;
122 static const struct glx_context_vtable dri2_context_vtable
;
124 /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
125 * low halves separately. This helps you split them.
128 split_counter(uint64_t counter
, uint32_t *hi
, uint32_t *lo
)
130 *hi
= (counter
>> 32);
131 *lo
= counter
& 0xffffffff;
135 merge_counter(uint32_t hi
, uint32_t lo
)
137 return ((uint64_t)hi
<< 32) | lo
;
141 dri2_destroy_context(struct glx_context
*context
)
143 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
144 struct dri2_screen
*psc
= (struct dri2_screen
*) context
->psc
;
146 driReleaseDrawables(&pcp
->base
);
148 free((char *) context
->extensions
);
150 (*psc
->core
->destroyContext
) (pcp
->driContext
);
156 dri2_bind_context(struct glx_context
*context
, struct glx_context
*old
,
157 GLXDrawable draw
, GLXDrawable read
)
159 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
160 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
161 struct dri2_drawable
*pdraw
, *pread
;
162 struct dri2_display
*pdp
;
164 pdraw
= (struct dri2_drawable
*) driFetchDrawable(context
, draw
);
165 pread
= (struct dri2_drawable
*) driFetchDrawable(context
, read
);
167 driReleaseDrawables(&pcp
->base
);
169 if (pdraw
== NULL
|| pread
== NULL
)
170 return GLXBadDrawable
;
172 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
173 pdraw
->driDrawable
, pread
->driDrawable
))
174 return GLXBadContext
;
176 /* If the server doesn't send invalidate events, we may miss a
177 * resize before the rendering starts. Invalidate the buffers now
178 * so the driver will recheck before rendering starts. */
179 pdp
= (struct dri2_display
*) psc
->base
.display
;
180 if (!pdp
->invalidateAvailable
) {
181 dri2InvalidateBuffers(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
183 dri2InvalidateBuffers(psc
->base
.dpy
, pread
->base
.xDrawable
);
190 dri2_unbind_context(struct glx_context
*context
, struct glx_context
*new)
192 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
193 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
195 (*psc
->core
->unbindContext
) (pcp
->driContext
);
198 static struct glx_context
*
199 dri2_create_context(struct glx_screen
*base
,
200 struct glx_config
*config_base
,
201 struct glx_context
*shareList
, int renderType
)
203 struct dri2_context
*pcp
, *pcp_shared
;
204 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
205 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
206 __DRIcontext
*shared
= NULL
;
209 /* If the shareList context is not a DRI2 context, we cannot possibly
210 * create a DRI2 context that shares it.
212 if (shareList
->vtable
->destroy
!= dri2_destroy_context
) {
216 pcp_shared
= (struct dri2_context
*) shareList
;
217 shared
= pcp_shared
->driContext
;
220 pcp
= calloc(1, sizeof *pcp
);
224 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
230 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
231 config
->driConfig
, shared
, pcp
);
233 if (pcp
->driContext
== NULL
) {
238 pcp
->base
.vtable
= &dri2_context_vtable
;
243 static struct glx_context
*
244 dri2_create_context_attribs(struct glx_screen
*base
,
245 struct glx_config
*config_base
,
246 struct glx_context
*shareList
,
247 unsigned num_attribs
,
248 const uint32_t *attribs
,
251 struct dri2_context
*pcp
= NULL
;
252 struct dri2_context
*pcp_shared
= NULL
;
253 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
254 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
255 __DRIcontext
*shared
= NULL
;
257 uint32_t minor_ver
= 1;
258 uint32_t major_ver
= 2;
261 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
262 uint32_t ctx_attribs
[2 * 5];
263 unsigned num_ctx_attribs
= 0;
265 if (psc
->dri2
->base
.version
< 3) {
266 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
270 /* Remap the GLX tokens to DRI2 tokens.
272 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
273 &major_ver
, &minor_ver
, &flags
, &api
, &reset
,
278 pcp_shared
= (struct dri2_context
*) shareList
;
279 shared
= pcp_shared
->driContext
;
282 pcp
= calloc(1, sizeof *pcp
);
284 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
288 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
291 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
292 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
293 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
294 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
296 /* Only send a value when the non-default value is requested. By doing
297 * this we don't have to check the driver's DRI2 version before sending the
300 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
301 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
302 ctx_attribs
[num_ctx_attribs
++] = reset
;
306 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
308 /* The current __DRI_CTX_FLAG_* values are identical to the
309 * GLX_CONTEXT_*_BIT values.
311 ctx_attribs
[num_ctx_attribs
++] = flags
;
315 (*psc
->dri2
->createContextAttribs
) (psc
->driScreen
,
324 if (pcp
->driContext
== NULL
)
327 pcp
->base
.vtable
= &dri2_context_vtable
;
338 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
340 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
341 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
342 struct glx_display
*dpyPriv
= psc
->base
.display
;
343 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
345 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
346 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
348 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
349 * now, as the application explicitly asked to destroy the GLX
350 * drawable. Otherwise, for legacy drawables, we let the DRI2
351 * drawable linger on the server, since there's no good way of
352 * knowing when the application is done with it. The server will
353 * destroy the DRI2 drawable when it destroys the X drawable or the
354 * client exits anyway. */
355 if (pdraw
->base
.xDrawable
!= pdraw
->base
.drawable
)
356 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
361 static __GLXDRIdrawable
*
362 dri2CreateDrawable(struct glx_screen
*base
, XID xDrawable
,
363 GLXDrawable drawable
, struct glx_config
*config_base
)
365 struct dri2_drawable
*pdraw
;
366 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
367 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
368 struct glx_display
*dpyPriv
;
369 struct dri2_display
*pdp
;
370 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
372 pdraw
= calloc(1, sizeof(*pdraw
));
376 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
377 pdraw
->base
.xDrawable
= xDrawable
;
378 pdraw
->base
.drawable
= drawable
;
379 pdraw
->base
.psc
= &psc
->base
;
380 pdraw
->bufferCount
= 0;
381 pdraw
->swap_interval
= 1; /* default may be overridden below */
382 pdraw
->have_back
= 0;
385 psc
->config
->configQueryi(psc
->driScreen
,
386 "vblank_mode", &vblank_mode
);
388 switch (vblank_mode
) {
389 case DRI_CONF_VBLANK_NEVER
:
390 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
391 pdraw
->swap_interval
= 0;
393 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
394 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
396 pdraw
->swap_interval
= 1;
400 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
402 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
403 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
404 /* Create a new drawable */
406 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
407 config
->driConfig
, pdraw
);
409 if (!pdraw
->driDrawable
) {
410 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
415 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
416 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
417 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
423 * Make sure server has the same swap interval we do for the new
426 if (psc
->vtable
.setSwapInterval
)
427 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
433 dri2DrawableGetMSC(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
434 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
436 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
437 xcb_dri2_get_msc_cookie_t get_msc_cookie
;
438 xcb_dri2_get_msc_reply_t
*get_msc_reply
;
440 get_msc_cookie
= xcb_dri2_get_msc_unchecked(c
, pdraw
->xDrawable
);
441 get_msc_reply
= xcb_dri2_get_msc_reply(c
, get_msc_cookie
, NULL
);
446 *ust
= merge_counter(get_msc_reply
->ust_hi
, get_msc_reply
->ust_lo
);
447 *msc
= merge_counter(get_msc_reply
->msc_hi
, get_msc_reply
->msc_lo
);
448 *sbc
= merge_counter(get_msc_reply
->sbc_hi
, get_msc_reply
->sbc_lo
);
455 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
456 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
458 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
459 xcb_dri2_wait_msc_cookie_t wait_msc_cookie
;
460 xcb_dri2_wait_msc_reply_t
*wait_msc_reply
;
461 uint32_t target_msc_hi
, target_msc_lo
;
462 uint32_t divisor_hi
, divisor_lo
;
463 uint32_t remainder_hi
, remainder_lo
;
465 split_counter(target_msc
, &target_msc_hi
, &target_msc_lo
);
466 split_counter(divisor
, &divisor_hi
, &divisor_lo
);
467 split_counter(remainder
, &remainder_hi
, &remainder_lo
);
469 wait_msc_cookie
= xcb_dri2_wait_msc_unchecked(c
, pdraw
->xDrawable
,
470 target_msc_hi
, target_msc_lo
,
471 divisor_hi
, divisor_lo
,
472 remainder_hi
, remainder_lo
);
473 wait_msc_reply
= xcb_dri2_wait_msc_reply(c
, wait_msc_cookie
, NULL
);
478 *ust
= merge_counter(wait_msc_reply
->ust_hi
, wait_msc_reply
->ust_lo
);
479 *msc
= merge_counter(wait_msc_reply
->msc_hi
, wait_msc_reply
->msc_lo
);
480 *sbc
= merge_counter(wait_msc_reply
->sbc_hi
, wait_msc_reply
->sbc_lo
);
481 free(wait_msc_reply
);
487 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
488 int64_t *msc
, int64_t *sbc
)
490 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
491 xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie
;
492 xcb_dri2_wait_sbc_reply_t
*wait_sbc_reply
;
493 uint32_t target_sbc_hi
, target_sbc_lo
;
495 split_counter(target_sbc
, &target_sbc_hi
, &target_sbc_lo
);
497 wait_sbc_cookie
= xcb_dri2_wait_sbc_unchecked(c
, pdraw
->xDrawable
,
498 target_sbc_hi
, target_sbc_lo
);
499 wait_sbc_reply
= xcb_dri2_wait_sbc_reply(c
, wait_sbc_cookie
, NULL
);
504 *ust
= merge_counter(wait_sbc_reply
->ust_hi
, wait_sbc_reply
->ust_lo
);
505 *msc
= merge_counter(wait_sbc_reply
->msc_hi
, wait_sbc_reply
->msc_lo
);
506 *sbc
= merge_counter(wait_sbc_reply
->sbc_hi
, wait_sbc_reply
->sbc_lo
);
507 free(wait_sbc_reply
);
512 static __DRIcontext
*
513 dri2GetCurrentContext()
515 struct glx_context
*gc
= __glXGetCurrentContext();
516 struct dri2_context
*dri2Ctx
= (struct dri2_context
*)gc
;
518 return dri2Ctx
? dri2Ctx
->driContext
: NULL
;
522 * dri2Throttle - Request driver throttling
524 * This function uses the DRI2 throttle extension to give the
525 * driver the opportunity to throttle on flush front, copysubbuffer
529 dri2Throttle(struct dri2_screen
*psc
,
530 struct dri2_drawable
*draw
,
531 enum __DRI2throttleReason reason
)
534 __DRIcontext
*ctx
= dri2GetCurrentContext();
536 psc
->throttle
->throttle(ctx
, draw
->driDrawable
, reason
);
541 * Asks the driver to flush any queued work necessary for serializing with the
542 * X command stream, and optionally the slightly more strict requirement of
543 * glFlush() equivalence (which would require flushing even if nothing had
544 * been drawn to a window system framebuffer, for example).
547 dri2Flush(struct dri2_screen
*psc
,
549 struct dri2_drawable
*draw
,
551 enum __DRI2throttleReason throttle_reason
)
553 if (ctx
&& psc
->f
&& psc
->f
->base
.version
>= 4) {
554 psc
->f
->flush_with_flags(ctx
, draw
->driDrawable
, flags
, throttle_reason
);
556 if (flags
& __DRI2_FLUSH_CONTEXT
)
560 psc
->f
->flush(draw
->driDrawable
);
562 dri2Throttle(psc
, draw
, throttle_reason
);
567 __dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
568 int width
, int height
,
569 enum __DRI2throttleReason reason
, Bool flush
)
571 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
572 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
574 XserverRegion region
;
575 __DRIcontext
*ctx
= dri2GetCurrentContext();
578 /* Check we have the right attachments */
579 if (!priv
->have_back
)
583 xrect
.y
= priv
->height
- y
- height
;
585 xrect
.height
= height
;
587 flags
= __DRI2_FLUSH_DRAWABLE
;
589 flags
|= __DRI2_FLUSH_CONTEXT
;
590 dri2Flush(psc
, ctx
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
592 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
593 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
594 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
596 /* Refresh the fake front (if present) after we just damaged the real
599 if (priv
->have_fake_front
)
600 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
601 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
603 XFixesDestroyRegion(psc
->base
.dpy
, region
);
607 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
608 int width
, int height
, Bool flush
)
610 __dri2CopySubBuffer(pdraw
, x
, y
, width
, height
,
611 __DRI2_THROTTLE_COPYSUBBUFFER
, flush
);
616 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
619 XserverRegion region
;
620 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
624 xrect
.width
= priv
->width
;
625 xrect
.height
= priv
->height
;
628 (*psc
->f
->flush
) (priv
->driDrawable
);
630 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
631 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
632 XFixesDestroyRegion(psc
->base
.dpy
, region
);
637 dri2_wait_x(struct glx_context
*gc
)
639 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
640 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
642 if (priv
== NULL
|| !priv
->have_fake_front
)
645 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
649 dri2_wait_gl(struct glx_context
*gc
)
651 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
652 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
654 if (priv
== NULL
|| !priv
->have_fake_front
)
657 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
661 * Called by the driver when it needs to update the real front buffer with the
662 * contents of its fake front buffer.
665 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
667 struct glx_display
*priv
;
668 struct dri2_display
*pdp
;
669 struct glx_context
*gc
;
670 struct dri2_drawable
*pdraw
= loaderPrivate
;
671 struct dri2_screen
*psc
;
676 if (!pdraw
->base
.psc
)
679 psc
= (struct dri2_screen
*) pdraw
->base
.psc
;
681 priv
= __glXInitialize(psc
->base
.dpy
);
682 pdp
= (struct dri2_display
*) priv
->dri2Display
;
683 gc
= __glXGetCurrentContext();
685 dri2Throttle(psc
, pdraw
, __DRI2_THROTTLE_FLUSHFRONT
);
687 /* Old servers don't send invalidate events */
688 if (!pdp
->invalidateAvailable
)
689 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
696 dri2DestroyScreen(struct glx_screen
*base
)
698 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
700 /* Free the direct rendering per screen data */
701 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
702 driDestroyConfigs(psc
->driver_configs
);
708 * Process list of buffer received from the server
710 * Processes the list of buffers received in a reply from the server to either
711 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
714 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
719 pdraw
->bufferCount
= count
;
720 pdraw
->have_fake_front
= 0;
721 pdraw
->have_back
= 0;
723 /* This assumes the DRI2 buffer attachment tokens matches the
724 * __DRIbuffer tokens. */
725 for (i
= 0; i
< count
; i
++) {
726 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
727 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
728 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
729 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
730 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
731 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
732 pdraw
->have_fake_front
= 1;
733 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
734 pdraw
->have_back
= 1;
739 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
741 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
742 __GLXDRIdrawable
*pdraw
;
743 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
744 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
746 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
749 static void show_fps(struct dri2_drawable
*draw
)
752 uint64_t current_time
;
754 gettimeofday(&tv
, 0);
755 current_time
= (uint64_t)tv
.tv_sec
*1000000 + (uint64_t)tv
.tv_usec
;
759 if (draw
->previous_time
+ 1000000 <= current_time
) {
760 if (draw
->previous_time
) {
761 fprintf(stderr
, "libGL: FPS = %.1f\n",
762 ((uint64_t)draw
->frames
* 1000000) /
763 (double)(current_time
- draw
->previous_time
));
766 draw
->previous_time
= current_time
;
771 dri2XcbSwapBuffers(Display
*dpy
,
772 __GLXDRIdrawable
*pdraw
,
777 xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie
;
778 xcb_dri2_swap_buffers_reply_t
*swap_buffers_reply
;
779 uint32_t target_msc_hi
, target_msc_lo
;
780 uint32_t divisor_hi
, divisor_lo
;
781 uint32_t remainder_hi
, remainder_lo
;
783 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
785 split_counter(target_msc
, &target_msc_hi
, &target_msc_lo
);
786 split_counter(divisor
, &divisor_hi
, &divisor_lo
);
787 split_counter(remainder
, &remainder_hi
, &remainder_lo
);
789 swap_buffers_cookie
=
790 xcb_dri2_swap_buffers_unchecked(c
, pdraw
->xDrawable
,
791 target_msc_hi
, target_msc_lo
,
792 divisor_hi
, divisor_lo
,
793 remainder_hi
, remainder_lo
);
795 /* Immediately wait on the swapbuffers reply. If we didn't, we'd have
796 * to do so some time before reusing a (non-pageflipped) backbuffer.
797 * Otherwise, the new rendering could get ahead of the X Server's
798 * dispatch of the swapbuffer and you'd display garbage.
800 * We use XSync() first to reap the invalidate events through the event
801 * filter, to ensure that the next drawing doesn't use an invalidated
807 xcb_dri2_swap_buffers_reply(c
, swap_buffers_cookie
, NULL
);
808 if (swap_buffers_reply
) {
809 ret
= merge_counter(swap_buffers_reply
->swap_hi
,
810 swap_buffers_reply
->swap_lo
);
811 free(swap_buffers_reply
);
817 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
818 int64_t remainder
, Bool flush
)
820 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
821 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
822 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
823 struct dri2_display
*pdp
=
824 (struct dri2_display
*)dpyPriv
->dri2Display
;
827 /* Check we have the right attachments */
828 if (!priv
->have_back
)
831 /* Old servers can't handle swapbuffers */
832 if (!pdp
->swapAvailable
) {
833 __dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
,
834 __DRI2_THROTTLE_SWAPBUFFER
, flush
);
836 __DRIcontext
*ctx
= dri2GetCurrentContext();
837 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
839 flags
|= __DRI2_FLUSH_CONTEXT
;
840 dri2Flush(psc
, ctx
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
842 ret
= dri2XcbSwapBuffers(pdraw
->psc
->dpy
, pdraw
,
843 target_msc
, divisor
, remainder
);
850 /* Old servers don't send invalidate events */
851 if (!pdp
->invalidateAvailable
)
852 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
858 dri2GetBuffers(__DRIdrawable
* driDrawable
,
859 int *width
, int *height
,
860 unsigned int *attachments
, int count
,
861 int *out_count
, void *loaderPrivate
)
863 struct dri2_drawable
*pdraw
= loaderPrivate
;
866 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
867 width
, height
, attachments
, count
, out_count
);
871 pdraw
->width
= *width
;
872 pdraw
->height
= *height
;
873 process_buffers(pdraw
, buffers
, *out_count
);
877 return pdraw
->buffers
;
881 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
882 int *width
, int *height
,
883 unsigned int *attachments
, int count
,
884 int *out_count
, void *loaderPrivate
)
886 struct dri2_drawable
*pdraw
= loaderPrivate
;
889 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
890 pdraw
->base
.xDrawable
,
891 width
, height
, attachments
,
896 pdraw
->width
= *width
;
897 pdraw
->height
= *height
;
898 process_buffers(pdraw
, buffers
, *out_count
);
902 return pdraw
->buffers
;
906 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
908 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
909 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
910 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
911 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
914 psc
->config
->configQueryi(psc
->driScreen
,
915 "vblank_mode", &vblank_mode
);
917 switch (vblank_mode
) {
918 case DRI_CONF_VBLANK_NEVER
:
920 return GLX_BAD_VALUE
;
922 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
924 return GLX_BAD_VALUE
;
930 xcb_dri2_swap_interval(c
, priv
->base
.xDrawable
, interval
);
931 priv
->swap_interval
= interval
;
937 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
939 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
941 return priv
->swap_interval
;
944 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
945 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
947 dri2FlushFrontBuffer
,
948 dri2GetBuffersWithFormat
,
951 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
952 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
954 dri2FlushFrontBuffer
,
958 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
959 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
963 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
965 __GLXDRIdrawable
*pdraw
=
966 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
967 struct dri2_screen
*psc
;
968 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
973 psc
= (struct dri2_screen
*) pdraw
->psc
;
975 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
976 psc
->f
->invalidate(pdp
->driDrawable
);
980 dri2_bind_tex_image(Display
* dpy
,
981 GLXDrawable drawable
,
982 int buffer
, const int *attrib_list
)
984 struct glx_context
*gc
= __glXGetCurrentContext();
985 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
986 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
987 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
988 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
989 struct dri2_display
*pdp
=
990 (struct dri2_display
*) dpyPriv
->dri2Display
;
991 struct dri2_screen
*psc
;
994 psc
= (struct dri2_screen
*) base
->psc
;
996 if (!pdp
->invalidateAvailable
&& psc
->f
&&
997 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
998 psc
->f
->invalidate(pdraw
->driDrawable
);
1000 if (psc
->texBuffer
->base
.version
>= 2 &&
1001 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
1002 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1003 pdraw
->base
.textureTarget
,
1004 pdraw
->base
.textureFormat
,
1005 pdraw
->driDrawable
);
1008 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
1009 pdraw
->base
.textureTarget
,
1010 pdraw
->driDrawable
);
1016 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1018 #if __DRI_TEX_BUFFER_VERSION >= 3
1019 struct glx_context
*gc
= __glXGetCurrentContext();
1020 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
1021 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1022 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
1023 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
1024 struct dri2_display
*pdp
=
1025 (struct dri2_display
*) dpyPriv
->dri2Display
;
1026 struct dri2_screen
*psc
;
1028 if (pdraw
!= NULL
) {
1029 psc
= (struct dri2_screen
*) base
->psc
;
1031 if (psc
->texBuffer
->base
.version
>= 3 &&
1032 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
1033 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1034 pdraw
->base
.textureTarget
,
1035 pdraw
->driDrawable
);
1041 static const struct glx_context_vtable dri2_context_vtable
= {
1042 dri2_destroy_context
,
1044 dri2_unbind_context
,
1048 dri2_bind_tex_image
,
1049 dri2_release_tex_image
,
1050 NULL
, /* get_proc_address */
1054 dri2BindExtensions(struct dri2_screen
*psc
, struct glx_display
* priv
,
1055 const char *driverName
)
1057 const struct dri2_display
*const pdp
= (struct dri2_display
*)
1059 const __DRIextension
**extensions
;
1062 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1064 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1065 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1066 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1067 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1070 * GLX_INTEL_swap_event is broken on the server side, where it's
1071 * currently unconditionally enabled. This completely breaks
1072 * systems running on drivers which don't support that extension.
1073 * There's no way to test for its presence on this side, so instead
1074 * of disabling it unconditionally, just disable it for drivers
1075 * which are known to not support it, or for DDX drivers supporting
1076 * only an older (pre-ScheduleSwap) version of DRI2.
1078 * This is a hack which is required until:
1079 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1080 * is merged and updated xserver makes it's way into distros:
1082 if (pdp
->swapAvailable
&& strcmp(driverName
, "vmwgfx") != 0) {
1083 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
1086 if (psc
->dri2
->base
.version
>= 3) {
1087 const unsigned mask
= psc
->dri2
->getAPIMask(psc
->driScreen
);
1089 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1090 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1092 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1093 __glXEnableDirectExtension(&psc
->base
,
1094 "GLX_EXT_create_context_es2_profile");
1097 for (i
= 0; extensions
[i
]; i
++) {
1098 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1099 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1100 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1103 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1104 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1105 /* internal driver extension, no GL extension exposed */
1108 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1109 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1111 if (((strcmp(extensions
[i
]->name
, __DRI2_THROTTLE
) == 0)))
1112 psc
->throttle
= (__DRI2throttleExtension
*) extensions
[i
];
1114 /* DRI2 version 3 is also required because
1115 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1117 if (psc
->dri2
->base
.version
>= 3
1118 && strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1119 __glXEnableDirectExtension(&psc
->base
,
1120 "GLX_ARB_create_context_robustness");
1124 static const struct glx_screen_vtable dri2_screen_vtable
= {
1125 dri2_create_context
,
1126 dri2_create_context_attribs
1129 static struct glx_screen
*
1130 dri2CreateScreen(int screen
, struct glx_display
* priv
)
1132 const __DRIconfig
**driver_configs
;
1133 const __DRIextension
**extensions
;
1134 const struct dri2_display
*const pdp
= (struct dri2_display
*)
1136 struct dri2_screen
*psc
;
1137 __GLXDRIscreen
*psp
;
1138 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1139 char *driverName
, *deviceName
, *tmp
;
1143 psc
= calloc(1, sizeof *psc
);
1149 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1154 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
1155 &driverName
, &deviceName
)) {
1156 glx_screen_cleanup(&psc
->base
);
1158 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen
);
1162 psc
->driver
= driOpenDriver(driverName
);
1163 if (psc
->driver
== NULL
) {
1164 ErrorMessageF("driver pointer missing\n");
1168 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
1169 if (extensions
== NULL
) {
1170 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1174 for (i
= 0; extensions
[i
]; i
++) {
1175 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1176 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1177 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
1178 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
1181 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
1182 ErrorMessageF("core dri or dri2 extension not found\n");
1187 psc
->fd
= open(deviceName
, O_RDWR
| O_CLOEXEC
);
1188 if (psc
->fd
== -1 && errno
== EINVAL
)
1191 psc
->fd
= open(deviceName
, O_RDWR
);
1193 fcntl(psc
->fd
, F_SETFD
, fcntl(psc
->fd
, F_GETFD
) | FD_CLOEXEC
);
1196 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
1200 if (drmGetMagic(psc
->fd
, &magic
)) {
1201 ErrorMessageF("failed to get magic\n");
1205 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
1206 ErrorMessageF("failed to authenticate magic %d\n", magic
);
1211 /* If the server does not support the protocol for
1212 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1215 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
1216 (const __DRIextension
**)
1217 &pdp
->loader_extensions
[0],
1218 &driver_configs
, psc
);
1220 if (psc
->driScreen
== NULL
) {
1221 ErrorMessageF("failed to create dri screen\n");
1225 dri2BindExtensions(psc
, priv
, driverName
);
1227 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1228 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1230 if (!configs
|| !visuals
)
1233 glx_config_destroy_list(psc
->base
.configs
);
1234 psc
->base
.configs
= configs
;
1235 glx_config_destroy_list(psc
->base
.visuals
);
1236 psc
->base
.visuals
= visuals
;
1238 psc
->driver_configs
= driver_configs
;
1240 psc
->base
.vtable
= &dri2_screen_vtable
;
1242 psc
->base
.driScreen
= psp
;
1243 psp
->destroyScreen
= dri2DestroyScreen
;
1244 psp
->createDrawable
= dri2CreateDrawable
;
1245 psp
->swapBuffers
= dri2SwapBuffers
;
1246 psp
->getDrawableMSC
= NULL
;
1247 psp
->waitForMSC
= NULL
;
1248 psp
->waitForSBC
= NULL
;
1249 psp
->setSwapInterval
= NULL
;
1250 psp
->getSwapInterval
= NULL
;
1252 if (pdp
->driMinor
>= 2) {
1253 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
1254 psp
->waitForMSC
= dri2WaitForMSC
;
1255 psp
->waitForSBC
= dri2WaitForSBC
;
1256 psp
->setSwapInterval
= dri2SetSwapInterval
;
1257 psp
->getSwapInterval
= dri2GetSwapInterval
;
1258 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1261 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1263 psp
->copySubBuffer
= dri2CopySubBuffer
;
1264 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1269 tmp
= getenv("LIBGL_SHOW_FPS");
1270 psc
->show_fps
= tmp
&& strcmp(tmp
, "1") == 0;
1275 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1278 glx_config_destroy_list(configs
);
1280 glx_config_destroy_list(visuals
);
1282 psc
->core
->destroyScreen(psc
->driScreen
);
1283 psc
->driScreen
= NULL
;
1287 dlclose(psc
->driver
);
1291 glx_screen_cleanup(&psc
->base
);
1297 /* Called from __glXFreeDisplayPrivate.
1300 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
1302 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
1304 __glxHashDestroy(pdp
->dri2Hash
);
1308 _X_HIDDEN __GLXDRIdrawable
*
1309 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
1311 struct glx_display
*d
= __glXInitialize(dpy
);
1312 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
1313 __GLXDRIdrawable
*pdraw
;
1315 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
1322 * Allocate, initialize and return a __DRIdisplayPrivate object.
1323 * This is called from __glXInitialize() when we are given a new
1326 _X_HIDDEN __GLXDRIdisplay
*
1327 dri2CreateDisplay(Display
* dpy
)
1329 struct dri2_display
*pdp
;
1330 int eventBase
, errorBase
, i
;
1332 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
1335 pdp
= malloc(sizeof *pdp
);
1339 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1345 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1346 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1348 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1349 pdp
->base
.createScreen
= dri2CreateScreen
;
1352 if (pdp
->driMinor
< 1)
1353 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1355 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1357 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1359 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1361 pdp
->loader_extensions
[i
++] = NULL
;
1363 pdp
->dri2Hash
= __glxHashCreate();
1364 if (pdp
->dri2Hash
== NULL
) {
1372 #endif /* GLX_DIRECT_RENDERING */