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>
38 #include "glxclient.h"
39 #include <X11/extensions/dri2proto.h>
44 #include <sys/types.h>
49 #include "dri_common.h"
51 /* From xmlpool/options.h, user exposed so should be stable */
52 #define DRI_CONF_VBLANK_NEVER 0
53 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
54 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
55 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
65 ** XFree86-DRI version information
71 int invalidateAvailable
;
73 __glxHashTable
*dri2Hash
;
75 const __DRIextension
*loader_extensions
[4];
79 struct glx_screen base
;
81 __DRIscreen
*driScreen
;
82 __GLXDRIscreen vtable
;
83 const __DRIdri2Extension
*dri2
;
84 const __DRIcoreExtension
*core
;
86 const __DRI2flushExtension
*f
;
87 const __DRI2configQueryExtension
*config
;
88 const __DRItexBufferExtension
*texBuffer
;
89 const __DRI2throttleExtension
*throttle
;
90 const __DRIconfig
**driver_configs
;
100 struct glx_context base
;
101 __DRIcontext
*driContext
;
106 __GLXDRIdrawable base
;
107 __DRIdrawable
*driDrawable
;
108 __DRIbuffer buffers
[5];
115 uint64_t previous_time
;
119 static const struct glx_context_vtable dri2_context_vtable
;
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
;
190 /* If the shareList context is not a DRI2 context, we cannot possibly
191 * create a DRI2 context that shares it.
193 if (shareList
->vtable
->destroy
!= dri2_destroy_context
) {
197 pcp_shared
= (struct dri2_context
*) shareList
;
198 shared
= pcp_shared
->driContext
;
201 pcp
= calloc(1, sizeof *pcp
);
205 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
211 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
212 config
->driConfig
, shared
, pcp
);
214 if (pcp
->driContext
== NULL
) {
219 pcp
->base
.vtable
= &dri2_context_vtable
;
224 static struct glx_context
*
225 dri2_create_context_attribs(struct glx_screen
*base
,
226 struct glx_config
*config_base
,
227 struct glx_context
*shareList
,
228 unsigned num_attribs
,
229 const uint32_t *attribs
,
232 struct dri2_context
*pcp
= NULL
;
233 struct dri2_context
*pcp_shared
= NULL
;
234 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
235 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
236 __DRIcontext
*shared
= NULL
;
238 uint32_t minor_ver
= 1;
239 uint32_t major_ver
= 2;
242 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
243 uint32_t ctx_attribs
[2 * 5];
244 unsigned num_ctx_attribs
= 0;
246 if (psc
->dri2
->base
.version
< 3) {
247 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
251 /* Remap the GLX tokens to DRI2 tokens.
253 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
254 &major_ver
, &minor_ver
, &flags
, &api
, &reset
,
259 pcp_shared
= (struct dri2_context
*) shareList
;
260 shared
= pcp_shared
->driContext
;
263 pcp
= calloc(1, sizeof *pcp
);
265 *error
= __DRI_CTX_ERROR_NO_MEMORY
;
269 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
))
272 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
273 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
274 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
275 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
277 /* Only send a value when the non-default value is requested. By doing
278 * this we don't have to check the driver's DRI2 version before sending the
281 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
) {
282 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
283 ctx_attribs
[num_ctx_attribs
++] = reset
;
287 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
289 /* The current __DRI_CTX_FLAG_* values are identical to the
290 * GLX_CONTEXT_*_BIT values.
292 ctx_attribs
[num_ctx_attribs
++] = flags
;
296 (*psc
->dri2
->createContextAttribs
) (psc
->driScreen
,
305 if (pcp
->driContext
== NULL
)
308 pcp
->base
.vtable
= &dri2_context_vtable
;
319 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
321 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
322 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
323 struct glx_display
*dpyPriv
= psc
->base
.display
;
324 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
326 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
327 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
329 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
330 * now, as the application explicitly asked to destroy the GLX
331 * drawable. Otherwise, for legacy drawables, we let the DRI2
332 * drawable linger on the server, since there's no good way of
333 * knowing when the application is done with it. The server will
334 * destroy the DRI2 drawable when it destroys the X drawable or the
335 * client exits anyway. */
336 if (pdraw
->base
.xDrawable
!= pdraw
->base
.drawable
)
337 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
342 static __GLXDRIdrawable
*
343 dri2CreateDrawable(struct glx_screen
*base
, XID xDrawable
,
344 GLXDrawable drawable
, struct glx_config
*config_base
)
346 struct dri2_drawable
*pdraw
;
347 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
348 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
349 struct glx_display
*dpyPriv
;
350 struct dri2_display
*pdp
;
351 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
353 pdraw
= calloc(1, sizeof(*pdraw
));
357 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
358 pdraw
->base
.xDrawable
= xDrawable
;
359 pdraw
->base
.drawable
= drawable
;
360 pdraw
->base
.psc
= &psc
->base
;
361 pdraw
->bufferCount
= 0;
362 pdraw
->swap_interval
= 1; /* default may be overridden below */
363 pdraw
->have_back
= 0;
366 psc
->config
->configQueryi(psc
->driScreen
,
367 "vblank_mode", &vblank_mode
);
369 switch (vblank_mode
) {
370 case DRI_CONF_VBLANK_NEVER
:
371 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
372 pdraw
->swap_interval
= 0;
374 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
375 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
377 pdraw
->swap_interval
= 1;
381 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
383 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
384 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
385 /* Create a new drawable */
387 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
388 config
->driConfig
, pdraw
);
390 if (!pdraw
->driDrawable
) {
391 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
396 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
397 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
398 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
404 #ifdef X_DRI2SwapInterval
406 * Make sure server has the same swap interval we do for the new
409 if (pdp
->swapAvailable
)
410 DRI2SwapInterval(psc
->base
.dpy
, xDrawable
, pdraw
->swap_interval
);
419 dri2DrawableGetMSC(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
420 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
422 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
425 ret
= DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
,
426 &dri2_ust
, &dri2_msc
, &dri2_sbc
);
440 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
441 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
443 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
446 ret
= DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
447 remainder
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
456 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
457 int64_t *msc
, int64_t *sbc
)
459 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
462 ret
= DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
,
463 target_sbc
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
471 #endif /* X_DRI2WaitMSC */
474 * dri2Throttle - Request driver throttling
476 * This function uses the DRI2 throttle extension to give the
477 * driver the opportunity to throttle on flush front, copysubbuffer
481 dri2Throttle(struct dri2_screen
*psc
,
482 struct dri2_drawable
*draw
,
483 enum __DRI2throttleReason reason
)
486 struct glx_context
*gc
= __glXGetCurrentContext();
487 struct dri2_context
*dri2Ctx
= (struct dri2_context
*)gc
;
489 (dri2Ctx
) ? dri2Ctx
->driContext
: NULL
;
491 psc
->throttle
->throttle(ctx
, draw
->driDrawable
, reason
);
496 __dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
497 int width
, int height
,
498 enum __DRI2throttleReason reason
)
500 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
501 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
503 XserverRegion region
;
505 /* Check we have the right attachments */
506 if (!priv
->have_back
)
510 xrect
.y
= priv
->height
- y
- height
;
512 xrect
.height
= height
;
515 (*psc
->f
->flush
) (priv
->driDrawable
);
517 dri2Throttle(psc
, priv
, reason
);
519 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
520 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
521 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
523 /* Refresh the fake front (if present) after we just damaged the real
526 if (priv
->have_fake_front
)
527 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
528 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
530 XFixesDestroyRegion(psc
->base
.dpy
, region
);
534 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
535 int width
, int height
)
537 __dri2CopySubBuffer(pdraw
, x
, y
, width
, height
,
538 __DRI2_THROTTLE_COPYSUBBUFFER
);
543 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
546 XserverRegion region
;
547 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
551 xrect
.width
= priv
->width
;
552 xrect
.height
= priv
->height
;
555 (*psc
->f
->flush
) (priv
->driDrawable
);
557 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
558 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
559 XFixesDestroyRegion(psc
->base
.dpy
, region
);
564 dri2_wait_x(struct glx_context
*gc
)
566 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
567 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
569 if (priv
== NULL
|| !priv
->have_fake_front
)
572 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
576 dri2_wait_gl(struct glx_context
*gc
)
578 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
579 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
581 if (priv
== NULL
|| !priv
->have_fake_front
)
584 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
588 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
590 struct glx_display
*priv
;
591 struct dri2_display
*pdp
;
592 struct glx_context
*gc
;
593 struct dri2_drawable
*pdraw
= loaderPrivate
;
594 struct dri2_screen
*psc
;
599 if (!pdraw
->base
.psc
)
602 psc
= (struct dri2_screen
*) pdraw
->base
.psc
;
604 priv
= __glXInitialize(psc
->base
.dpy
);
605 pdp
= (struct dri2_display
*) priv
->dri2Display
;
606 gc
= __glXGetCurrentContext();
608 dri2Throttle(psc
, pdraw
, __DRI2_THROTTLE_FLUSHFRONT
);
610 /* Old servers don't send invalidate events */
611 if (!pdp
->invalidateAvailable
)
612 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
619 dri2DestroyScreen(struct glx_screen
*base
)
621 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
623 /* Free the direct rendering per screen data */
624 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
625 driDestroyConfigs(psc
->driver_configs
);
631 * Process list of buffer received from the server
633 * Processes the list of buffers received in a reply from the server to either
634 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
637 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
642 pdraw
->bufferCount
= count
;
643 pdraw
->have_fake_front
= 0;
644 pdraw
->have_back
= 0;
646 /* This assumes the DRI2 buffer attachment tokens matches the
647 * __DRIbuffer tokens. */
648 for (i
= 0; i
< count
; i
++) {
649 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
650 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
651 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
652 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
653 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
654 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
655 pdraw
->have_fake_front
= 1;
656 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
657 pdraw
->have_back
= 1;
662 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
664 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
665 __GLXDRIdrawable
*pdraw
;
666 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
667 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
669 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
672 static void show_fps(struct dri2_drawable
*draw
)
675 uint64_t current_time
;
677 gettimeofday(&tv
, 0);
678 current_time
= (uint64_t)tv
.tv_sec
*1000000 + (uint64_t)tv
.tv_usec
;
682 if (draw
->previous_time
+ 1000000 <= current_time
) {
683 if (draw
->previous_time
) {
684 fprintf(stderr
, "libGL: FPS = %.1f\n",
685 ((uint64_t)draw
->frames
* 1000000) /
686 (double)(current_time
- draw
->previous_time
));
689 draw
->previous_time
= current_time
;
694 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
697 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
698 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
699 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
700 struct dri2_display
*pdp
=
701 (struct dri2_display
*)dpyPriv
->dri2Display
;
704 /* Check we have the right attachments */
705 if (!priv
->have_back
)
708 /* Old servers can't handle swapbuffers */
709 if (!pdp
->swapAvailable
) {
710 __dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
,
711 __DRI2_THROTTLE_SWAPBUFFER
);
713 #ifdef X_DRI2SwapBuffers
715 struct glx_context
*gc
= __glXGetCurrentContext();
718 (*psc
->f
->flush
)(priv
->driDrawable
);
722 dri2Throttle(psc
, priv
, __DRI2_THROTTLE_SWAPBUFFER
);
724 DRI2SwapBuffers(psc
->base
.dpy
, pdraw
->xDrawable
,
725 target_msc
, divisor
, remainder
, &ret
);
733 /* Old servers don't send invalidate events */
734 if (!pdp
->invalidateAvailable
)
735 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
741 dri2GetBuffers(__DRIdrawable
* driDrawable
,
742 int *width
, int *height
,
743 unsigned int *attachments
, int count
,
744 int *out_count
, void *loaderPrivate
)
746 struct dri2_drawable
*pdraw
= loaderPrivate
;
749 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
750 width
, height
, attachments
, count
, out_count
);
754 pdraw
->width
= *width
;
755 pdraw
->height
= *height
;
756 process_buffers(pdraw
, buffers
, *out_count
);
760 return pdraw
->buffers
;
764 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
765 int *width
, int *height
,
766 unsigned int *attachments
, int count
,
767 int *out_count
, void *loaderPrivate
)
769 struct dri2_drawable
*pdraw
= loaderPrivate
;
772 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
773 pdraw
->base
.xDrawable
,
774 width
, height
, attachments
,
779 pdraw
->width
= *width
;
780 pdraw
->height
= *height
;
781 process_buffers(pdraw
, buffers
, *out_count
);
785 return pdraw
->buffers
;
788 #ifdef X_DRI2SwapInterval
791 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
793 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
794 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
795 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
798 psc
->config
->configQueryi(psc
->driScreen
,
799 "vblank_mode", &vblank_mode
);
801 switch (vblank_mode
) {
802 case DRI_CONF_VBLANK_NEVER
:
803 return GLX_BAD_VALUE
;
804 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
806 return GLX_BAD_VALUE
;
812 DRI2SwapInterval(priv
->base
.psc
->dpy
, priv
->base
.xDrawable
, interval
);
813 priv
->swap_interval
= interval
;
819 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
821 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
823 return priv
->swap_interval
;
826 #endif /* X_DRI2SwapInterval */
828 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
829 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
831 dri2FlushFrontBuffer
,
832 dri2GetBuffersWithFormat
,
835 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
836 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
838 dri2FlushFrontBuffer
,
842 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
843 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
847 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
849 __GLXDRIdrawable
*pdraw
=
850 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
851 struct dri2_screen
*psc
;
852 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
857 psc
= (struct dri2_screen
*) pdraw
->psc
;
859 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
860 psc
->f
->invalidate(pdp
->driDrawable
);
864 dri2_bind_tex_image(Display
* dpy
,
865 GLXDrawable drawable
,
866 int buffer
, const int *attrib_list
)
868 struct glx_context
*gc
= __glXGetCurrentContext();
869 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
870 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
871 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
872 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
873 struct dri2_display
*pdp
=
874 (struct dri2_display
*) dpyPriv
->dri2Display
;
875 struct dri2_screen
*psc
;
878 psc
= (struct dri2_screen
*) base
->psc
;
880 if (!pdp
->invalidateAvailable
&& psc
->f
&&
881 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
882 psc
->f
->invalidate(pdraw
->driDrawable
);
884 if (psc
->texBuffer
->base
.version
>= 2 &&
885 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
886 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
887 pdraw
->base
.textureTarget
,
888 pdraw
->base
.textureFormat
,
892 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
893 pdraw
->base
.textureTarget
,
900 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
902 #if __DRI_TEX_BUFFER_VERSION >= 3
903 struct glx_context
*gc
= __glXGetCurrentContext();
904 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
905 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
906 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
907 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
908 struct dri2_display
*pdp
=
909 (struct dri2_display
*) dpyPriv
->dri2Display
;
910 struct dri2_screen
*psc
;
913 psc
= (struct dri2_screen
*) base
->psc
;
915 if (psc
->texBuffer
->base
.version
>= 3 &&
916 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
917 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
918 pdraw
->base
.textureTarget
,
925 static const struct glx_context_vtable dri2_context_vtable
= {
926 dri2_destroy_context
,
933 dri2_release_tex_image
,
934 NULL
, /* get_proc_address */
938 dri2BindExtensions(struct dri2_screen
*psc
, const __DRIextension
**extensions
)
942 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
943 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
944 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
945 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
947 /* FIXME: if DRI2 version supports it... */
948 __glXEnableDirectExtension(&psc
->base
, "INTEL_swap_event");
950 if (psc
->dri2
->base
.version
>= 3) {
951 const unsigned mask
= psc
->dri2
->getAPIMask(psc
->driScreen
);
953 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
954 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
956 if ((mask
& (1 << __DRI_API_GLES2
)) != 0)
957 __glXEnableDirectExtension(&psc
->base
,
958 "GLX_EXT_create_context_es2_profile");
961 for (i
= 0; extensions
[i
]; i
++) {
962 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
963 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
964 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
967 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
968 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
969 /* internal driver extension, no GL extension exposed */
972 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
973 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
975 if (((strcmp(extensions
[i
]->name
, __DRI2_THROTTLE
) == 0)))
976 psc
->throttle
= (__DRI2throttleExtension
*) extensions
[i
];
978 /* DRI2 version 3 is also required because
979 * GLX_ARB_create_context_robustness requires GLX_ARB_create_context.
981 if (psc
->dri2
->base
.version
>= 3
982 && strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0)
983 __glXEnableDirectExtension(&psc
->base
,
984 "GLX_ARB_create_context_robustness");
988 static const struct glx_screen_vtable dri2_screen_vtable
= {
990 dri2_create_context_attribs
993 static struct glx_screen
*
994 dri2CreateScreen(int screen
, struct glx_display
* priv
)
996 const __DRIconfig
**driver_configs
;
997 const __DRIextension
**extensions
;
998 const struct dri2_display
*const pdp
= (struct dri2_display
*)
1000 struct dri2_screen
*psc
;
1001 __GLXDRIscreen
*psp
;
1002 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
1003 char *driverName
, *deviceName
, *tmp
;
1007 psc
= calloc(1, sizeof *psc
);
1013 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
1018 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
1019 &driverName
, &deviceName
)) {
1020 glx_screen_cleanup(&psc
->base
);
1022 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen
);
1026 psc
->driver
= driOpenDriver(driverName
);
1027 if (psc
->driver
== NULL
) {
1028 ErrorMessageF("driver pointer missing\n");
1032 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
1033 if (extensions
== NULL
) {
1034 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
1038 for (i
= 0; extensions
[i
]; i
++) {
1039 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
1040 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
1041 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
1042 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
1045 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
1046 ErrorMessageF("core dri or dri2 extension not found\n");
1051 psc
->fd
= open(deviceName
, O_RDWR
| O_CLOEXEC
);
1052 if (psc
->fd
== -1 && errno
== EINVAL
)
1055 psc
->fd
= open(deviceName
, O_RDWR
);
1057 fcntl(psc
->fd
, F_SETFD
, fcntl(psc
->fd
, F_GETFD
) | FD_CLOEXEC
);
1060 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
1064 if (drmGetMagic(psc
->fd
, &magic
)) {
1065 ErrorMessageF("failed to get magic\n");
1069 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
1070 ErrorMessageF("failed to authenticate magic %d\n", magic
);
1075 /* If the server does not support the protocol for
1076 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
1079 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
1080 (const __DRIextension
**)
1081 &pdp
->loader_extensions
[0],
1082 &driver_configs
, psc
);
1084 if (psc
->driScreen
== NULL
) {
1085 ErrorMessageF("failed to create dri screen\n");
1089 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
1090 dri2BindExtensions(psc
, extensions
);
1092 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
1093 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
1095 if (!configs
|| !visuals
)
1098 glx_config_destroy_list(psc
->base
.configs
);
1099 psc
->base
.configs
= configs
;
1100 glx_config_destroy_list(psc
->base
.visuals
);
1101 psc
->base
.visuals
= visuals
;
1103 psc
->driver_configs
= driver_configs
;
1105 psc
->base
.vtable
= &dri2_screen_vtable
;
1107 psc
->base
.driScreen
= psp
;
1108 psp
->destroyScreen
= dri2DestroyScreen
;
1109 psp
->createDrawable
= dri2CreateDrawable
;
1110 psp
->swapBuffers
= dri2SwapBuffers
;
1111 psp
->getDrawableMSC
= NULL
;
1112 psp
->waitForMSC
= NULL
;
1113 psp
->waitForSBC
= NULL
;
1114 psp
->setSwapInterval
= NULL
;
1115 psp
->getSwapInterval
= NULL
;
1117 if (pdp
->driMinor
>= 2) {
1119 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
1121 #ifdef X_DRI2WaitMSC
1122 psp
->waitForMSC
= dri2WaitForMSC
;
1123 psp
->waitForSBC
= dri2WaitForSBC
;
1125 #ifdef X_DRI2SwapInterval
1126 psp
->setSwapInterval
= dri2SetSwapInterval
;
1127 psp
->getSwapInterval
= dri2GetSwapInterval
;
1129 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
1130 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
1134 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
1136 psp
->copySubBuffer
= dri2CopySubBuffer
;
1137 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1142 tmp
= getenv("LIBGL_SHOW_FPS");
1143 psc
->show_fps
= tmp
&& strcmp(tmp
, "1") == 0;
1148 CriticalErrorMessageF("failed to load driver: %s\n", driverName
);
1151 glx_config_destroy_list(configs
);
1153 glx_config_destroy_list(visuals
);
1155 psc
->core
->destroyScreen(psc
->driScreen
);
1156 psc
->driScreen
= NULL
;
1160 dlclose(psc
->driver
);
1164 glx_screen_cleanup(&psc
->base
);
1170 /* Called from __glXFreeDisplayPrivate.
1173 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
1175 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
1177 __glxHashDestroy(pdp
->dri2Hash
);
1181 _X_HIDDEN __GLXDRIdrawable
*
1182 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
1184 struct glx_display
*d
= __glXInitialize(dpy
);
1185 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
1186 __GLXDRIdrawable
*pdraw
;
1188 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
1195 * Allocate, initialize and return a __DRIdisplayPrivate object.
1196 * This is called from __glXInitialize() when we are given a new
1199 _X_HIDDEN __GLXDRIdisplay
*
1200 dri2CreateDisplay(Display
* dpy
)
1202 struct dri2_display
*pdp
;
1203 int eventBase
, errorBase
, i
;
1205 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
1208 pdp
= malloc(sizeof *pdp
);
1212 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1218 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1219 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1221 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1222 pdp
->base
.createScreen
= dri2CreateScreen
;
1225 if (pdp
->driMinor
< 1)
1226 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1228 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1230 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1232 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1234 pdp
->loader_extensions
[i
++] = NULL
;
1236 pdp
->dri2Hash
= __glxHashCreate();
1237 if (pdp
->dri2Hash
== NULL
) {
1245 #endif /* GLX_DIRECT_RENDERING */