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"
53 #include "dri2_priv.h"
55 /* From xmlpool/options.h, user exposed so should be stable */
56 #define DRI_CONF_VBLANK_NEVER 0
57 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
58 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
59 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
69 ** XFree86-DRI version information
75 int invalidateAvailable
;
77 __glxHashTable
*dri2Hash
;
79 const __DRIextension
*loader_extensions
[4];
84 struct glx_context base
;
85 __DRIcontext
*driContext
;
90 __GLXDRIdrawable base
;
91 __DRIdrawable
*driDrawable
;
92 __DRIbuffer buffers
[5];
99 uint64_t previous_time
;
103 static const struct glx_context_vtable dri2_context_vtable
;
105 /* For XCB's handling of ust/msc/sbc counters, we have to hand it the high and
106 * low halves separately. This helps you split them.
109 split_counter(uint64_t counter
, uint32_t *hi
, uint32_t *lo
)
111 *hi
= (counter
>> 32);
112 *lo
= counter
& 0xffffffff;
116 merge_counter(uint32_t hi
, uint32_t lo
)
118 return ((uint64_t)hi
<< 32) | lo
;
122 dri2_destroy_context(struct glx_context
*context
)
124 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
125 struct dri2_screen
*psc
= (struct dri2_screen
*) context
->psc
;
127 driReleaseDrawables(&pcp
->base
);
129 free((char *) context
->extensions
);
131 (*psc
->core
->destroyContext
) (pcp
->driContext
);
137 dri2_bind_context(struct glx_context
*context
, struct glx_context
*old
,
138 GLXDrawable draw
, GLXDrawable read
)
140 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
141 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
142 struct dri2_drawable
*pdraw
, *pread
;
143 struct dri2_display
*pdp
;
145 pdraw
= (struct dri2_drawable
*) driFetchDrawable(context
, draw
);
146 pread
= (struct dri2_drawable
*) driFetchDrawable(context
, read
);
148 driReleaseDrawables(&pcp
->base
);
150 if (pdraw
== NULL
|| pread
== NULL
)
151 return GLXBadDrawable
;
153 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
154 pdraw
->driDrawable
, pread
->driDrawable
))
155 return GLXBadContext
;
157 /* If the server doesn't send invalidate events, we may miss a
158 * resize before the rendering starts. Invalidate the buffers now
159 * so the driver will recheck before rendering starts. */
160 pdp
= (struct dri2_display
*) psc
->base
.display
;
161 if (!pdp
->invalidateAvailable
) {
162 dri2InvalidateBuffers(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
164 dri2InvalidateBuffers(psc
->base
.dpy
, pread
->base
.xDrawable
);
171 dri2_unbind_context(struct glx_context
*context
, struct glx_context
*new)
173 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
174 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
176 (*psc
->core
->unbindContext
) (pcp
->driContext
);
179 static struct glx_context
*
180 dri2_create_context(struct glx_screen
*base
,
181 struct glx_config
*config_base
,
182 struct glx_context
*shareList
, int renderType
)
184 struct dri2_context
*pcp
, *pcp_shared
;
185 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
186 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
187 __DRIcontext
*shared
= NULL
;
189 /* Check the renderType value */
190 if (!validate_renderType_against_config(config_base
, renderType
))
194 /* If the shareList context is not a DRI2 context, we cannot possibly
195 * create a DRI2 context that shares it.
197 if (shareList
->vtable
->destroy
!= dri2_destroy_context
) {
201 pcp_shared
= (struct dri2_context
*) shareList
;
202 shared
= pcp_shared
->driContext
;
205 pcp
= calloc(1, sizeof *pcp
);
209 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
214 pcp
->base
.renderType
= renderType
;
217 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
218 config
->driConfig
, shared
, pcp
);
220 if (pcp
->driContext
== NULL
) {
225 pcp
->base
.vtable
= &dri2_context_vtable
;
230 static struct glx_context
*
231 dri2_create_context_attribs(struct glx_screen
*base
,
232 struct glx_config
*config_base
,
233 struct glx_context
*shareList
,
234 unsigned num_attribs
,
235 const uint32_t *attribs
,
238 struct dri2_context
*pcp
= NULL
;
239 struct dri2_context
*pcp_shared
= NULL
;
240 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
241 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
242 __DRIcontext
*shared
= NULL
;
250 uint32_t ctx_attribs
[2 * 5];
251 unsigned num_ctx_attribs
= 0;
253 if (psc
->dri2
->base
.version
< 3) {
254 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
258 /* Remap the GLX tokens to DRI2 tokens.
260 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
261 &major_ver
, &minor_ver
, &renderType
, &flags
,
262 &api
, &reset
, error
))
265 /* Check the renderType value */
266 if (!validate_renderType_against_config(config_base
, renderType
))
270 pcp_shared
= (struct dri2_context
*) shareList
;
271 shared
= pcp_shared
->driContext
;
274 pcp
= calloc(1, sizeof *pcp
);
276 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
280 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
283 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
284 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
285 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
286 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
288 /* Only send a value when the non-default value is requested. By doing
289 * this we don't have to check the driver's DRI2 version before sending the
292 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
293 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
294 ctx_attribs
[num_ctx_attribs
++] = reset
;
298 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
300 /* The current __DRI_CTX_FLAG_* values are identical to the
301 * GLX_CONTEXT_*_BIT values.
303 ctx_attribs
[num_ctx_attribs
++] = flags
;
306 /* The renderType is retrieved from attribs, or set to default
309 pcp
->base
.renderType
= renderType
;
312 (*psc
->dri2
->createContextAttribs
) (psc
->driScreen
,
321 if (pcp
->driContext
== NULL
)
324 pcp
->base
.vtable
= &dri2_context_vtable
;
335 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
337 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
338 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
339 struct glx_display
*dpyPriv
= psc
->base
.display
;
340 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
342 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
343 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
345 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
346 * now, as the application explicitly asked to destroy the GLX
347 * drawable. Otherwise, for legacy drawables, we let the DRI2
348 * drawable linger on the server, since there's no good way of
349 * knowing when the application is done with it. The server will
350 * destroy the DRI2 drawable when it destroys the X drawable or the
351 * client exits anyway. */
352 if (pdraw
->base
.xDrawable
!= pdraw
->base
.drawable
)
353 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
358 static __GLXDRIdrawable
*
359 dri2CreateDrawable(struct glx_screen
*base
, XID xDrawable
,
360 GLXDrawable drawable
, struct glx_config
*config_base
)
362 struct dri2_drawable
*pdraw
;
363 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
364 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
365 struct glx_display
*dpyPriv
;
366 struct dri2_display
*pdp
;
367 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
369 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
373 pdraw
= calloc(1, sizeof(*pdraw
));
377 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
378 pdraw
->base
.xDrawable
= xDrawable
;
379 pdraw
->base
.drawable
= drawable
;
380 pdraw
->base
.psc
= &psc
->base
;
381 pdraw
->bufferCount
= 0;
382 pdraw
->swap_interval
= 1; /* default may be overridden below */
383 pdraw
->have_back
= 0;
386 psc
->config
->configQueryi(psc
->driScreen
,
387 "vblank_mode", &vblank_mode
);
389 switch (vblank_mode
) {
390 case DRI_CONF_VBLANK_NEVER
:
391 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
392 pdraw
->swap_interval
= 0;
394 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
395 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
397 pdraw
->swap_interval
= 1;
401 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
402 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
403 /* Create a new drawable */
405 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
406 config
->driConfig
, pdraw
);
408 if (!pdraw
->driDrawable
) {
409 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
414 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
415 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
416 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
422 * Make sure server has the same swap interval we do for the new
425 if (psc
->vtable
.setSwapInterval
)
426 psc
->vtable
.setSwapInterval(&pdraw
->base
, pdraw
->swap_interval
);
432 dri2DrawableGetMSC(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
433 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
435 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
436 xcb_dri2_get_msc_cookie_t get_msc_cookie
;
437 xcb_dri2_get_msc_reply_t
*get_msc_reply
;
439 get_msc_cookie
= xcb_dri2_get_msc_unchecked(c
, pdraw
->xDrawable
);
440 get_msc_reply
= xcb_dri2_get_msc_reply(c
, get_msc_cookie
, NULL
);
445 *ust
= merge_counter(get_msc_reply
->ust_hi
, get_msc_reply
->ust_lo
);
446 *msc
= merge_counter(get_msc_reply
->msc_hi
, get_msc_reply
->msc_lo
);
447 *sbc
= merge_counter(get_msc_reply
->sbc_hi
, get_msc_reply
->sbc_lo
);
454 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
455 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
457 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
458 xcb_dri2_wait_msc_cookie_t wait_msc_cookie
;
459 xcb_dri2_wait_msc_reply_t
*wait_msc_reply
;
460 uint32_t target_msc_hi
, target_msc_lo
;
461 uint32_t divisor_hi
, divisor_lo
;
462 uint32_t remainder_hi
, remainder_lo
;
464 split_counter(target_msc
, &target_msc_hi
, &target_msc_lo
);
465 split_counter(divisor
, &divisor_hi
, &divisor_lo
);
466 split_counter(remainder
, &remainder_hi
, &remainder_lo
);
468 wait_msc_cookie
= xcb_dri2_wait_msc_unchecked(c
, pdraw
->xDrawable
,
469 target_msc_hi
, target_msc_lo
,
470 divisor_hi
, divisor_lo
,
471 remainder_hi
, remainder_lo
);
472 wait_msc_reply
= xcb_dri2_wait_msc_reply(c
, wait_msc_cookie
, NULL
);
477 *ust
= merge_counter(wait_msc_reply
->ust_hi
, wait_msc_reply
->ust_lo
);
478 *msc
= merge_counter(wait_msc_reply
->msc_hi
, wait_msc_reply
->msc_lo
);
479 *sbc
= merge_counter(wait_msc_reply
->sbc_hi
, wait_msc_reply
->sbc_lo
);
480 free(wait_msc_reply
);
486 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
487 int64_t *msc
, int64_t *sbc
)
489 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
490 xcb_dri2_wait_sbc_cookie_t wait_sbc_cookie
;
491 xcb_dri2_wait_sbc_reply_t
*wait_sbc_reply
;
492 uint32_t target_sbc_hi
, target_sbc_lo
;
494 split_counter(target_sbc
, &target_sbc_hi
, &target_sbc_lo
);
496 wait_sbc_cookie
= xcb_dri2_wait_sbc_unchecked(c
, pdraw
->xDrawable
,
497 target_sbc_hi
, target_sbc_lo
);
498 wait_sbc_reply
= xcb_dri2_wait_sbc_reply(c
, wait_sbc_cookie
, NULL
);
503 *ust
= merge_counter(wait_sbc_reply
->ust_hi
, wait_sbc_reply
->ust_lo
);
504 *msc
= merge_counter(wait_sbc_reply
->msc_hi
, wait_sbc_reply
->msc_lo
);
505 *sbc
= merge_counter(wait_sbc_reply
->sbc_hi
, wait_sbc_reply
->sbc_lo
);
506 free(wait_sbc_reply
);
511 static __DRIcontext
*
512 dri2GetCurrentContext()
514 struct glx_context
*gc
= __glXGetCurrentContext();
515 struct dri2_context
*dri2Ctx
= (struct dri2_context
*)gc
;
517 return dri2Ctx
? dri2Ctx
->driContext
: NULL
;
521 * dri2Throttle - Request driver throttling
523 * This function uses the DRI2 throttle extension to give the
524 * driver the opportunity to throttle on flush front, copysubbuffer
528 dri2Throttle(struct dri2_screen
*psc
,
529 struct dri2_drawable
*draw
,
530 enum __DRI2throttleReason reason
)
533 __DRIcontext
*ctx
= dri2GetCurrentContext();
535 psc
->throttle
->throttle(ctx
, draw
->driDrawable
, reason
);
540 * Asks the driver to flush any queued work necessary for serializing with the
541 * X command stream, and optionally the slightly more strict requirement of
542 * glFlush() equivalence (which would require flushing even if nothing had
543 * been drawn to a window system framebuffer, for example).
546 dri2Flush(struct dri2_screen
*psc
,
548 struct dri2_drawable
*draw
,
550 enum __DRI2throttleReason throttle_reason
)
552 if (ctx
&& psc
->f
&& psc
->f
->base
.version
>= 4) {
553 psc
->f
->flush_with_flags(ctx
, draw
->driDrawable
, flags
, throttle_reason
);
555 if (flags
& __DRI2_FLUSH_CONTEXT
)
559 psc
->f
->flush(draw
->driDrawable
);
561 dri2Throttle(psc
, draw
, throttle_reason
);
566 __dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
567 int width
, int height
,
568 enum __DRI2throttleReason reason
, Bool flush
)
570 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
571 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
573 XserverRegion region
;
574 __DRIcontext
*ctx
= dri2GetCurrentContext();
577 /* Check we have the right attachments */
578 if (!priv
->have_back
)
582 xrect
.y
= priv
->height
- y
- height
;
584 xrect
.height
= height
;
586 flags
= __DRI2_FLUSH_DRAWABLE
;
588 flags
|= __DRI2_FLUSH_CONTEXT
;
589 dri2Flush(psc
, ctx
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
591 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
592 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
593 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
595 /* Refresh the fake front (if present) after we just damaged the real
598 if (priv
->have_fake_front
)
599 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
600 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
602 XFixesDestroyRegion(psc
->base
.dpy
, region
);
606 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
607 int width
, int height
, Bool flush
)
609 __dri2CopySubBuffer(pdraw
, x
, y
, width
, height
,
610 __DRI2_THROTTLE_COPYSUBBUFFER
, flush
);
615 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
618 XserverRegion region
;
619 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
623 xrect
.width
= priv
->width
;
624 xrect
.height
= priv
->height
;
627 (*psc
->f
->flush
) (priv
->driDrawable
);
629 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
630 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
631 XFixesDestroyRegion(psc
->base
.dpy
, region
);
636 dri2_wait_x(struct glx_context
*gc
)
638 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
639 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
641 if (priv
== NULL
|| !priv
->have_fake_front
)
644 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
648 dri2_wait_gl(struct glx_context
*gc
)
650 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
651 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
653 if (priv
== NULL
|| !priv
->have_fake_front
)
656 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
660 * Called by the driver when it needs to update the real front buffer with the
661 * contents of its fake front buffer.
664 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
666 struct glx_display
*priv
;
667 struct dri2_display
*pdp
;
668 struct glx_context
*gc
;
669 struct dri2_drawable
*pdraw
= loaderPrivate
;
670 struct dri2_screen
*psc
;
675 if (!pdraw
->base
.psc
)
678 psc
= (struct dri2_screen
*) pdraw
->base
.psc
;
680 priv
= __glXInitialize(psc
->base
.dpy
);
685 pdp
= (struct dri2_display
*) priv
->dri2Display
;
686 gc
= __glXGetCurrentContext();
688 dri2Throttle(psc
, pdraw
, __DRI2_THROTTLE_FLUSHFRONT
);
690 /* Old servers don't send invalidate events */
691 if (!pdp
->invalidateAvailable
)
692 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
699 dri2DestroyScreen(struct glx_screen
*base
)
701 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
703 /* Free the direct rendering per screen data */
704 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
705 driDestroyConfigs(psc
->driver_configs
);
711 * Process list of buffer received from the server
713 * Processes the list of buffers received in a reply from the server to either
714 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
717 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
722 pdraw
->bufferCount
= count
;
723 pdraw
->have_fake_front
= 0;
724 pdraw
->have_back
= 0;
726 /* This assumes the DRI2 buffer attachment tokens matches the
727 * __DRIbuffer tokens. */
728 for (i
= 0; i
< count
; i
++) {
729 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
730 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
731 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
732 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
733 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
734 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
735 pdraw
->have_fake_front
= 1;
736 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
737 pdraw
->have_back
= 1;
742 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
744 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
745 __GLXDRIdrawable
*pdraw
;
746 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
747 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
749 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
752 static void show_fps(struct dri2_drawable
*draw
)
755 ((struct dri2_screen
*) draw
->base
.psc
)->show_fps_interval
;
757 uint64_t current_time
;
759 gettimeofday(&tv
, 0);
760 current_time
= (uint64_t)tv
.tv_sec
*1000000 + (uint64_t)tv
.tv_usec
;
764 if (draw
->previous_time
+ interval
* 1000000 <= current_time
) {
765 if (draw
->previous_time
) {
766 fprintf(stderr
, "libGL: FPS = %.1f\n",
767 ((uint64_t)draw
->frames
* 1000000) /
768 (double)(current_time
- draw
->previous_time
));
771 draw
->previous_time
= current_time
;
776 dri2XcbSwapBuffers(Display
*dpy
,
777 __GLXDRIdrawable
*pdraw
,
782 xcb_dri2_swap_buffers_cookie_t swap_buffers_cookie
;
783 xcb_dri2_swap_buffers_reply_t
*swap_buffers_reply
;
784 uint32_t target_msc_hi
, target_msc_lo
;
785 uint32_t divisor_hi
, divisor_lo
;
786 uint32_t remainder_hi
, remainder_lo
;
788 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
790 split_counter(target_msc
, &target_msc_hi
, &target_msc_lo
);
791 split_counter(divisor
, &divisor_hi
, &divisor_lo
);
792 split_counter(remainder
, &remainder_hi
, &remainder_lo
);
794 swap_buffers_cookie
=
795 xcb_dri2_swap_buffers_unchecked(c
, pdraw
->xDrawable
,
796 target_msc_hi
, target_msc_lo
,
797 divisor_hi
, divisor_lo
,
798 remainder_hi
, remainder_lo
);
800 /* Immediately wait on the swapbuffers reply. If we didn't, we'd have
801 * to do so some time before reusing a (non-pageflipped) backbuffer.
802 * Otherwise, the new rendering could get ahead of the X Server's
803 * dispatch of the swapbuffer and you'd display garbage.
805 * We use XSync() first to reap the invalidate events through the event
806 * filter, to ensure that the next drawing doesn't use an invalidated
812 xcb_dri2_swap_buffers_reply(c
, swap_buffers_cookie
, NULL
);
813 if (swap_buffers_reply
) {
814 ret
= merge_counter(swap_buffers_reply
->swap_hi
,
815 swap_buffers_reply
->swap_lo
);
816 free(swap_buffers_reply
);
822 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
823 int64_t remainder
, Bool flush
)
825 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
826 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
827 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
828 struct dri2_display
*pdp
=
829 (struct dri2_display
*)dpyPriv
->dri2Display
;
832 /* Check we have the right attachments */
833 if (!priv
->have_back
)
836 /* Old servers can't handle swapbuffers */
837 if (!pdp
->swapAvailable
) {
838 __dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
,
839 __DRI2_THROTTLE_SWAPBUFFER
, flush
);
841 __DRIcontext
*ctx
= dri2GetCurrentContext();
842 unsigned flags
= __DRI2_FLUSH_DRAWABLE
;
844 flags
|= __DRI2_FLUSH_CONTEXT
;
845 dri2Flush(psc
, ctx
, priv
, flags
, __DRI2_THROTTLE_SWAPBUFFER
);
847 ret
= dri2XcbSwapBuffers(pdraw
->psc
->dpy
, pdraw
,
848 target_msc
, divisor
, remainder
);
851 if (psc
->show_fps_interval
) {
855 /* Old servers don't send invalidate events */
856 if (!pdp
->invalidateAvailable
)
857 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
863 dri2GetBuffers(__DRIdrawable
* driDrawable
,
864 int *width
, int *height
,
865 unsigned int *attachments
, int count
,
866 int *out_count
, void *loaderPrivate
)
868 struct dri2_drawable
*pdraw
= loaderPrivate
;
871 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
872 width
, height
, attachments
, count
, out_count
);
876 pdraw
->width
= *width
;
877 pdraw
->height
= *height
;
878 process_buffers(pdraw
, buffers
, *out_count
);
882 return pdraw
->buffers
;
886 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
887 int *width
, int *height
,
888 unsigned int *attachments
, int count
,
889 int *out_count
, void *loaderPrivate
)
891 struct dri2_drawable
*pdraw
= loaderPrivate
;
894 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
895 pdraw
->base
.xDrawable
,
896 width
, height
, attachments
,
901 pdraw
->width
= *width
;
902 pdraw
->height
= *height
;
903 process_buffers(pdraw
, buffers
, *out_count
);
907 return pdraw
->buffers
;
911 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
913 xcb_connection_t
*c
= XGetXCBConnection(pdraw
->psc
->dpy
);
914 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
915 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
916 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
919 psc
->config
->configQueryi(psc
->driScreen
,
920 "vblank_mode", &vblank_mode
);
922 switch (vblank_mode
) {
923 case DRI_CONF_VBLANK_NEVER
:
925 return GLX_BAD_VALUE
;
927 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
929 return GLX_BAD_VALUE
;
935 xcb_dri2_swap_interval(c
, priv
->base
.xDrawable
, interval
);
936 priv
->swap_interval
= interval
;
942 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
944 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
946 return priv
->swap_interval
;
949 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
950 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
952 dri2FlushFrontBuffer
,
953 dri2GetBuffersWithFormat
,
956 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
957 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
959 dri2FlushFrontBuffer
,
963 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
964 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
968 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
970 __GLXDRIdrawable
*pdraw
=
971 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
972 struct dri2_screen
*psc
;
973 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
978 psc
= (struct dri2_screen
*) pdraw
->psc
;
980 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
981 psc
->f
->invalidate(pdp
->driDrawable
);
985 dri2_bind_tex_image(Display
* dpy
,
986 GLXDrawable drawable
,
987 int buffer
, const int *attrib_list
)
989 struct glx_context
*gc
= __glXGetCurrentContext();
990 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
991 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
992 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
993 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
994 struct dri2_display
*pdp
;
995 struct dri2_screen
*psc
;
1000 pdp
= (struct dri2_display
*) dpyPriv
->dri2Display
;
1002 if (pdraw
!= NULL
) {
1003 psc
= (struct dri2_screen
*) base
->psc
;
1005 if (!pdp
->invalidateAvailable
&& psc
->f
&&
1006 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
1007 psc
->f
->invalidate(pdraw
->driDrawable
);
1009 if (psc
->texBuffer
->base
.version
>= 2 &&
1010 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
1011 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
1012 pdraw
->base
.textureTarget
,
1013 pdraw
->base
.textureFormat
,
1014 pdraw
->driDrawable
);
1017 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
1018 pdraw
->base
.textureTarget
,
1019 pdraw
->driDrawable
);
1025 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
1027 #if __DRI_TEX_BUFFER_VERSION >= 3
1028 struct glx_context
*gc
= __glXGetCurrentContext();
1029 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
1030 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
1031 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
1032 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
1033 struct dri2_display
*pdp
=
1034 (struct dri2_display
*) dpyPriv
->dri2Display
;
1035 struct dri2_screen
*psc
;
1037 if (pdraw
!= NULL
) {
1038 psc
= (struct dri2_screen
*) base
->psc
;
1040 if (psc
->texBuffer
->base
.version
>= 3 &&
1041 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
1042 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
1043 pdraw
->base
.textureTarget
,
1044 pdraw
->driDrawable
);
1050 static const struct glx_context_vtable dri2_context_vtable
= {
1051 dri2_destroy_context
,
1053 dri2_unbind_context
,
1057 dri2_bind_tex_image
,
1058 dri2_release_tex_image
,
1059 NULL
, /* get_proc_address */
1063 dri2BindExtensions(struct dri2_screen
*psc
, struct glx_display
* priv
,
1064 const char *driverName
)
1066 const struct dri2_display
*const pdp
= (struct dri2_display
*)
1068 const __DRIextension
**extensions
;
1071 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1073 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
1074 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
1075 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
1076 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
1079 * GLX_INTEL_swap_event is broken on the server side, where it's
1080 * currently unconditionally enabled. This completely breaks
1081 * systems running on drivers which don't support that extension.
1082 * There's no way to test for its presence on this side, so instead
1083 * of disabling it unconditionally, just disable it for drivers
1084 * which are known to not support it, or for DDX drivers supporting
1085 * only an older (pre-ScheduleSwap) version of DRI2.
1087 * This is a hack which is required until:
1088 * http://lists.x.org/archives/xorg-devel/2013-February/035449.html
1089 * is merged and updated xserver makes it's way into distros:
1091 if (pdp
->swapAvailable
&& strcmp(driverName
, "vmwgfx") != 0) {
1092 __glXEnableDirectExtension(&psc
->base
, "GLX_INTEL_swap_event");
1095 if (psc
->dri2
->base
.version
>= 3) {
1096 const unsigned mask
= psc
->dri2
->getAPIMask(psc
->driScreen
);
1098 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
1099 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
1101 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
1102 __glXEnableDirectExtension(&psc
->base
,
1103 "GLX_EXT_create_context_es2_profile");
1106 for (i
= 0; extensions
[i
]; i
++) {
1107 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
1108 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
1109 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
1112 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
1113 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
1114 /* internal driver extension, no GL extension exposed */
1117 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
1118 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
1120 if (((strcmp(extensions
[i
]->name
, __DRI2_THROTTLE
) == 0)))
1121 psc
->throttle
= (__DRI2throttleExtension
*) extensions
[i
];
1123 /* DRI2 version 3 is also required because
1124 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
1126 if (psc
->dri2
->base
.version
>= 3
1127 && strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
1128 __glXEnableDirectExtension(&psc
->base
,
1129 "GLX_ARB_create_context_robustness");
1131 /* DRI2 version 3 is also required because GLX_MESA_query_renderer
1132 * requires GLX_ARB_create_context_profile.
1134 if (psc
->dri2
->base
.version
>= 3
1135 && strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
1136 psc
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
1137 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_query_renderer");
1142 static const struct glx_screen_vtable dri2_screen_vtable
= {
1143 dri2_create_context
,
1144 dri2_create_context_attribs
,
1145 dri2_query_renderer_integer
,
1146 dri2_query_renderer_string
,
1149 static struct glx_screen
*
1150 dri2CreateScreen(int screen
, struct glx_display
* priv
)
1152 const __DRIconfig
**driver_configs
;
1153 const __DRIextension
**extensions
;
1154 const struct dri2_display
*const pdp
= (struct dri2_display
*)
1156 struct dri2_screen
*psc
;
1157 __GLXDRIscreen
*psp
;
1158 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1159 char *driverName
, *deviceName
, *tmp
;
1163 psc
= calloc(1, sizeof *psc
);
1169 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1174 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
1175 &driverName
, &deviceName
)) {
1176 glx_screen_cleanup(&psc
->base
);
1178 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen
);
1182 psc
->driver
= driOpenDriver(driverName
);
1183 if (psc
->driver
== NULL
) {
1184 ErrorMessageF("driver pointer missing\n");
1188 extensions
= driGetDriverExtensions(psc
->driver
, driverName
);
1189 if (extensions
== NULL
)
1192 for (i
= 0; extensions
[i
]; i
++) {
1193 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1194 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1195 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
1196 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
1199 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
1200 ErrorMessageF("core dri or dri2 extension not found\n");
1205 psc
->fd
= open(deviceName
, O_RDWR
| O_CLOEXEC
);
1206 if (psc
->fd
== -1 && errno
== EINVAL
)
1209 psc
->fd
= open(deviceName
, O_RDWR
);
1211 fcntl(psc
->fd
, F_SETFD
, fcntl(psc
->fd
, F_GETFD
) | FD_CLOEXEC
);
1214 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
1218 if (drmGetMagic(psc
->fd
, &magic
)) {
1219 ErrorMessageF("failed to get magic\n");
1223 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
1224 ErrorMessageF("failed to authenticate magic %d\n", magic
);
1228 if (psc
->dri2
->base
.version
>= 4) {
1230 psc
->dri2
->createNewScreen2(screen
, psc
->fd
,
1231 (const __DRIextension
**)
1232 &pdp
->loader_extensions
[0],
1234 &driver_configs
, psc
);
1237 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
1238 (const __DRIextension
**)
1239 &pdp
->loader_extensions
[0],
1240 &driver_configs
, psc
);
1243 if (psc
->driScreen
== NULL
) {
1244 ErrorMessageF("failed to create dri screen\n");
1248 dri2BindExtensions(psc
, priv
, driverName
);
1250 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1251 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1253 if (!configs
|| !visuals
)
1256 glx_config_destroy_list(psc
->base
.configs
);
1257 psc
->base
.configs
= configs
;
1258 glx_config_destroy_list(psc
->base
.visuals
);
1259 psc
->base
.visuals
= visuals
;
1261 psc
->driver_configs
= driver_configs
;
1263 psc
->base
.vtable
= &dri2_screen_vtable
;
1265 psc
->base
.driScreen
= psp
;
1266 psp
->destroyScreen
= dri2DestroyScreen
;
1267 psp
->createDrawable
= dri2CreateDrawable
;
1268 psp
->swapBuffers
= dri2SwapBuffers
;
1269 psp
->getDrawableMSC
= NULL
;
1270 psp
->waitForMSC
= NULL
;
1271 psp
->waitForSBC
= NULL
;
1272 psp
->setSwapInterval
= NULL
;
1273 psp
->getSwapInterval
= NULL
;
1275 if (pdp
->driMinor
>= 2) {
1276 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
1277 psp
->waitForMSC
= dri2WaitForMSC
;
1278 psp
->waitForSBC
= dri2WaitForSBC
;
1279 psp
->setSwapInterval
= dri2SetSwapInterval
;
1280 psp
->getSwapInterval
= dri2GetSwapInterval
;
1281 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1284 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1286 psp
->copySubBuffer
= dri2CopySubBuffer
;
1287 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1292 tmp
= getenv("LIBGL_SHOW_FPS");
1293 psc
->show_fps_interval
= (tmp
) ? atoi(tmp
) : 0;
1294 if (psc
->show_fps_interval
< 0)
1295 psc
->show_fps_interval
= 0;
1300 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1303 glx_config_destroy_list(configs
);
1305 glx_config_destroy_list(visuals
);
1307 psc
->core
->destroyScreen(psc
->driScreen
);
1308 psc
->driScreen
= NULL
;
1312 dlclose(psc
->driver
);
1316 glx_screen_cleanup(&psc
->base
);
1322 /* Called from __glXFreeDisplayPrivate.
1325 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
1327 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
1329 __glxHashDestroy(pdp
->dri2Hash
);
1333 _X_HIDDEN __GLXDRIdrawable
*
1334 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
1336 struct glx_display
*d
= __glXInitialize(dpy
);
1337 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
1338 __GLXDRIdrawable
*pdraw
;
1340 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
1347 * Allocate, initialize and return a __DRIdisplayPrivate object.
1348 * This is called from __glXInitialize() when we are given a new
1351 _X_HIDDEN __GLXDRIdisplay
*
1352 dri2CreateDisplay(Display
* dpy
)
1354 struct dri2_display
*pdp
;
1355 int eventBase
, errorBase
, i
;
1357 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
1360 pdp
= malloc(sizeof *pdp
);
1364 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1370 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1371 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1373 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1374 pdp
->base
.createScreen
= dri2CreateScreen
;
1377 if (pdp
->driMinor
< 1)
1378 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1380 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1382 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1384 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1386 pdp
->loader_extensions
[i
++] = NULL
;
1388 pdp
->dri2Hash
= __glxHashCreate();
1389 if (pdp
->dri2Hash
== NULL
) {
1397 #endif /* GLX_DIRECT_RENDERING */