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>
48 #include "dri_common.h"
50 /* From xmlpool/options.h, user exposed so should be stable */
51 #define DRI_CONF_VBLANK_NEVER 0
52 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
53 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
54 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
64 ** XFree86-DRI version information
70 int invalidateAvailable
;
72 __glxHashTable
*dri2Hash
;
74 const __DRIextension
*loader_extensions
[4];
78 struct glx_screen base
;
80 __DRIscreen
*driScreen
;
81 __GLXDRIscreen vtable
;
82 const __DRIdri2Extension
*dri2
;
83 const __DRIcoreExtension
*core
;
85 const __DRI2flushExtension
*f
;
86 const __DRI2configQueryExtension
*config
;
87 const __DRItexBufferExtension
*texBuffer
;
88 const __DRI2throttleExtension
*throttle
;
89 const __DRIconfig
**driver_configs
;
97 struct glx_context base
;
98 __DRIcontext
*driContext
;
103 __GLXDRIdrawable base
;
104 __DRIdrawable
*driDrawable
;
105 __DRIbuffer buffers
[5];
113 static const struct glx_context_vtable dri2_context_vtable
;
116 dri2_destroy_context(struct glx_context
*context
)
118 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
119 struct dri2_screen
*psc
= (struct dri2_screen
*) context
->psc
;
121 driReleaseDrawables(&pcp
->base
);
124 glx_send_destroy_context(psc
->base
.dpy
, context
->xid
);
126 if (context
->extensions
)
127 XFree((char *) context
->extensions
);
129 (*psc
->core
->destroyContext
) (pcp
->driContext
);
135 dri2_bind_context(struct glx_context
*context
, struct glx_context
*old
,
136 GLXDrawable draw
, GLXDrawable read
)
138 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
139 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
140 struct dri2_drawable
*pdraw
, *pread
;
141 struct dri2_display
*pdp
;
143 pdraw
= (struct dri2_drawable
*) driFetchDrawable(context
, draw
);
144 pread
= (struct dri2_drawable
*) driFetchDrawable(context
, read
);
146 driReleaseDrawables(&pcp
->base
);
148 if (pdraw
== NULL
|| pread
== NULL
)
149 return GLXBadDrawable
;
151 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
152 pdraw
->driDrawable
, pread
->driDrawable
))
153 return GLXBadContext
;
155 /* If the server doesn't send invalidate events, we may miss a
156 * resize before the rendering starts. Invalidate the buffers now
157 * so the driver will recheck before rendering starts. */
158 pdp
= (struct dri2_display
*) psc
->base
.display
;
159 if (!pdp
->invalidateAvailable
) {
160 dri2InvalidateBuffers(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
162 dri2InvalidateBuffers(psc
->base
.dpy
, pread
->base
.xDrawable
);
169 dri2_unbind_context(struct glx_context
*context
, struct glx_context
*new)
171 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
172 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
174 (*psc
->core
->unbindContext
) (pcp
->driContext
);
177 static struct glx_context
*
178 dri2_create_context(struct glx_screen
*base
,
179 struct glx_config
*config_base
,
180 struct glx_context
*shareList
, int renderType
)
182 struct dri2_context
*pcp
, *pcp_shared
;
183 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
184 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
185 __DRIcontext
*shared
= NULL
;
188 pcp_shared
= (struct dri2_context
*) shareList
;
189 shared
= pcp_shared
->driContext
;
192 pcp
= Xmalloc(sizeof *pcp
);
196 memset(pcp
, 0, sizeof *pcp
);
197 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
203 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
204 config
->driConfig
, shared
, pcp
);
206 if (pcp
->driContext
== NULL
) {
211 pcp
->base
.vtable
= &dri2_context_vtable
;
217 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
219 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
220 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
221 struct glx_display
*dpyPriv
= psc
->base
.display
;
222 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
224 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
225 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
227 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
228 * now, as the application explicitly asked to destroy the GLX
229 * drawable. Otherwise, for legacy drawables, we let the DRI2
230 * drawable linger on the server, since there's no good way of
231 * knowing when the application is done with it. The server will
232 * destroy the DRI2 drawable when it destroys the X drawable or the
233 * client exits anyway. */
234 if (pdraw
->base
.xDrawable
!= pdraw
->base
.drawable
)
235 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
240 static __GLXDRIdrawable
*
241 dri2CreateDrawable(struct glx_screen
*base
, XID xDrawable
,
242 GLXDrawable drawable
, struct glx_config
*config_base
)
244 struct dri2_drawable
*pdraw
;
245 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
246 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
247 struct glx_display
*dpyPriv
;
248 struct dri2_display
*pdp
;
249 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
251 pdraw
= Xmalloc(sizeof(*pdraw
));
255 memset(pdraw
, 0, sizeof *pdraw
);
256 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
257 pdraw
->base
.xDrawable
= xDrawable
;
258 pdraw
->base
.drawable
= drawable
;
259 pdraw
->base
.psc
= &psc
->base
;
260 pdraw
->bufferCount
= 0;
261 pdraw
->swap_interval
= 1; /* default may be overridden below */
262 pdraw
->have_back
= 0;
265 psc
->config
->configQueryi(psc
->driScreen
,
266 "vblank_mode", &vblank_mode
);
268 switch (vblank_mode
) {
269 case DRI_CONF_VBLANK_NEVER
:
270 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
271 pdraw
->swap_interval
= 0;
273 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
274 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
276 pdraw
->swap_interval
= 1;
280 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
282 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
283 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
284 /* Create a new drawable */
286 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
287 config
->driConfig
, pdraw
);
289 if (!pdraw
->driDrawable
) {
290 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
295 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
296 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
297 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
303 #ifdef X_DRI2SwapInterval
305 * Make sure server has the same swap interval we do for the new
308 if (pdp
->swapAvailable
)
309 DRI2SwapInterval(psc
->base
.dpy
, xDrawable
, pdraw
->swap_interval
);
318 dri2DrawableGetMSC(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
319 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
321 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
324 ret
= DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
,
325 &dri2_ust
, &dri2_msc
, &dri2_sbc
);
339 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
340 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
342 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
345 ret
= DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
346 remainder
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
355 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
356 int64_t *msc
, int64_t *sbc
)
358 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
361 ret
= DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
,
362 target_sbc
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
370 #endif /* X_DRI2WaitMSC */
373 * dri2Throttle - Request driver throttling
375 * This function uses the DRI2 throttle extension to give the
376 * driver the opportunity to throttle on flush front, copysubbuffer
380 dri2Throttle(struct dri2_screen
*psc
,
381 struct dri2_drawable
*draw
,
382 enum __DRI2throttleReason reason
)
385 struct glx_context
*gc
= __glXGetCurrentContext();
386 struct dri2_context
*dri2Ctx
= (struct dri2_context
*)gc
;
388 (dri2Ctx
) ? dri2Ctx
->driContext
: NULL
;
390 psc
->throttle
->throttle(ctx
, draw
->driDrawable
, reason
);
395 __dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
396 int width
, int height
,
397 enum __DRI2throttleReason reason
)
399 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
400 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
402 XserverRegion region
;
404 /* Check we have the right attachments */
405 if (!priv
->have_back
)
409 xrect
.y
= priv
->height
- y
- height
;
411 xrect
.height
= height
;
415 (*psc
->f
->flush
) (priv
->driDrawable
);
418 dri2Throttle(psc
, priv
, reason
);
420 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
421 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
422 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
424 /* Refresh the fake front (if present) after we just damaged the real
427 if (priv
->have_fake_front
)
428 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
429 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
431 XFixesDestroyRegion(psc
->base
.dpy
, region
);
435 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
,
436 int width
, int height
)
438 __dri2CopySubBuffer(pdraw
, x
, y
, width
, height
,
439 __DRI2_THROTTLE_COPYSUBBUFFER
);
444 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
447 XserverRegion region
;
448 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
452 xrect
.width
= priv
->width
;
453 xrect
.height
= priv
->height
;
457 (*psc
->f
->flush
) (priv
->driDrawable
);
460 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
461 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
462 XFixesDestroyRegion(psc
->base
.dpy
, region
);
467 dri2_wait_x(struct glx_context
*gc
)
469 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
470 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
472 if (priv
== NULL
|| !priv
->have_fake_front
)
475 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
479 dri2_wait_gl(struct glx_context
*gc
)
481 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
482 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
484 if (priv
== NULL
|| !priv
->have_fake_front
)
487 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
491 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
493 struct glx_display
*priv
;
494 struct dri2_display
*pdp
;
495 struct glx_context
*gc
;
496 struct dri2_drawable
*pdraw
= loaderPrivate
;
497 struct dri2_screen
*psc
;
502 if (!pdraw
->base
.psc
)
505 psc
= (struct dri2_screen
*) pdraw
->base
.psc
;
507 priv
= __glXInitialize(psc
->base
.dpy
);
508 pdp
= (struct dri2_display
*) priv
->dri2Display
;
509 gc
= __glXGetCurrentContext();
511 dri2Throttle(psc
, pdraw
, __DRI2_THROTTLE_FLUSHFRONT
);
513 /* Old servers don't send invalidate events */
514 if (!pdp
->invalidateAvailable
)
515 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
522 dri2DestroyScreen(struct glx_screen
*base
)
524 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
526 /* Free the direct rendering per screen data */
527 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
528 driDestroyConfigs(psc
->driver_configs
);
534 * Process list of buffer received from the server
536 * Processes the list of buffers received in a reply from the server to either
537 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
540 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
545 pdraw
->bufferCount
= count
;
546 pdraw
->have_fake_front
= 0;
547 pdraw
->have_back
= 0;
549 /* This assumes the DRI2 buffer attachment tokens matches the
550 * __DRIbuffer tokens. */
551 for (i
= 0; i
< count
; i
++) {
552 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
553 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
554 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
555 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
556 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
557 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
558 pdraw
->have_fake_front
= 1;
559 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
560 pdraw
->have_back
= 1;
565 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
567 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
568 __GLXDRIdrawable
*pdraw
;
569 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
570 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
572 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
576 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
579 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
580 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
581 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
582 struct dri2_display
*pdp
=
583 (struct dri2_display
*)dpyPriv
->dri2Display
;
586 /* Check we have the right attachments */
587 if (!priv
->have_back
)
590 /* Old servers can't handle swapbuffers */
591 if (!pdp
->swapAvailable
) {
592 __dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
,
593 __DRI2_THROTTLE_SWAPBUFFER
);
595 #ifdef X_DRI2SwapBuffers
598 struct glx_context
*gc
= __glXGetCurrentContext();
601 (*psc
->f
->flush
)(priv
->driDrawable
);
606 dri2Throttle(psc
, priv
, __DRI2_THROTTLE_SWAPBUFFER
);
608 DRI2SwapBuffers(psc
->base
.dpy
, pdraw
->xDrawable
,
609 target_msc
, divisor
, remainder
, &ret
);
613 /* Old servers don't send invalidate events */
614 if (!pdp
->invalidateAvailable
)
615 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
621 dri2GetBuffers(__DRIdrawable
* driDrawable
,
622 int *width
, int *height
,
623 unsigned int *attachments
, int count
,
624 int *out_count
, void *loaderPrivate
)
626 struct dri2_drawable
*pdraw
= loaderPrivate
;
629 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
630 width
, height
, attachments
, count
, out_count
);
634 pdraw
->width
= *width
;
635 pdraw
->height
= *height
;
636 process_buffers(pdraw
, buffers
, *out_count
);
640 return pdraw
->buffers
;
644 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
645 int *width
, int *height
,
646 unsigned int *attachments
, int count
,
647 int *out_count
, void *loaderPrivate
)
649 struct dri2_drawable
*pdraw
= loaderPrivate
;
652 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
653 pdraw
->base
.xDrawable
,
654 width
, height
, attachments
,
659 pdraw
->width
= *width
;
660 pdraw
->height
= *height
;
661 process_buffers(pdraw
, buffers
, *out_count
);
665 return pdraw
->buffers
;
668 #ifdef X_DRI2SwapInterval
671 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
673 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
674 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
675 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
678 psc
->config
->configQueryi(psc
->driScreen
,
679 "vblank_mode", &vblank_mode
);
681 switch (vblank_mode
) {
682 case DRI_CONF_VBLANK_NEVER
:
683 return GLX_BAD_VALUE
;
684 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
686 return GLX_BAD_VALUE
;
692 DRI2SwapInterval(priv
->base
.psc
->dpy
, priv
->base
.xDrawable
, interval
);
693 priv
->swap_interval
= interval
;
699 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
701 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
703 return priv
->swap_interval
;
706 #endif /* X_DRI2SwapInterval */
708 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
709 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
711 dri2FlushFrontBuffer
,
712 dri2GetBuffersWithFormat
,
715 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
716 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
718 dri2FlushFrontBuffer
,
722 #ifdef __DRI_USE_INVALIDATE
723 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
724 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
729 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
731 __GLXDRIdrawable
*pdraw
=
732 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
733 struct dri2_screen
*psc
;
734 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
739 psc
= (struct dri2_screen
*) pdraw
->psc
;
741 #if __DRI2_FLUSH_VERSION >= 3
742 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
743 psc
->f
->invalidate(pdp
->driDrawable
);
748 dri2_bind_tex_image(Display
* dpy
,
749 GLXDrawable drawable
,
750 int buffer
, const int *attrib_list
)
752 struct glx_context
*gc
= __glXGetCurrentContext();
753 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
754 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
755 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
756 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
757 struct dri2_display
*pdp
=
758 (struct dri2_display
*) dpyPriv
->dri2Display
;
759 struct dri2_screen
*psc
;
762 psc
= (struct dri2_screen
*) base
->psc
;
764 #if __DRI2_FLUSH_VERSION >= 3
765 if (!pdp
->invalidateAvailable
&& psc
->f
&&
766 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
767 psc
->f
->invalidate(pdraw
->driDrawable
);
770 if (psc
->texBuffer
->base
.version
>= 2 &&
771 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
772 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
773 pdraw
->base
.textureTarget
,
774 pdraw
->base
.textureFormat
,
778 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
779 pdraw
->base
.textureTarget
,
786 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
788 #if __DRI_TEX_BUFFER_VERSION >= 3
789 struct glx_context
*gc
= __glXGetCurrentContext();
790 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
791 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
792 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
793 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
794 struct dri2_display
*pdp
=
795 (struct dri2_display
*) dpyPriv
->dri2Display
;
796 struct dri2_screen
*psc
;
799 psc
= (struct dri2_screen
*) base
->psc
;
801 if (psc
->texBuffer
->base
.version
>= 3 &&
802 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
803 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
804 pdraw
->base
.textureTarget
,
811 static const struct glx_context_vtable dri2_context_vtable
= {
812 dri2_destroy_context
,
819 dri2_release_tex_image
,
820 NULL
, /* get_proc_address */
824 dri2BindExtensions(struct dri2_screen
*psc
, const __DRIextension
**extensions
)
828 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
829 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
830 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
831 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
833 /* FIXME: if DRI2 version supports it... */
834 __glXEnableDirectExtension(&psc
->base
, "INTEL_swap_event");
836 for (i
= 0; extensions
[i
]; i
++) {
837 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
838 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
839 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
842 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
843 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
844 /* internal driver extension, no GL extension exposed */
847 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
848 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
850 if (((strcmp(extensions
[i
]->name
, __DRI2_THROTTLE
) == 0)))
851 psc
->throttle
= (__DRI2throttleExtension
*) extensions
[i
];
855 static const struct glx_screen_vtable dri2_screen_vtable
= {
859 static struct glx_screen
*
860 dri2CreateScreen(int screen
, struct glx_display
* priv
)
862 const __DRIconfig
**driver_configs
;
863 const __DRIextension
**extensions
;
864 const struct dri2_display
*const pdp
= (struct dri2_display
*)
866 struct dri2_screen
*psc
;
868 char *driverName
, *deviceName
;
872 psc
= Xmalloc(sizeof *psc
);
876 memset(psc
, 0, sizeof *psc
);
879 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
884 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
885 &driverName
, &deviceName
)) {
886 glx_screen_cleanup(&psc
->base
);
888 InfoMessageF("screen %d does not appear to be DRI2 capable\n", screen
);
892 psc
->driver
= driOpenDriver(driverName
);
893 if (psc
->driver
== NULL
) {
894 ErrorMessageF("driver pointer missing\n");
898 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
899 if (extensions
== NULL
) {
900 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
904 for (i
= 0; extensions
[i
]; i
++) {
905 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
906 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
907 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
908 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
911 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
912 ErrorMessageF("core dri or dri2 extension not found\n");
916 psc
->fd
= open(deviceName
, O_RDWR
);
918 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
922 if (drmGetMagic(psc
->fd
, &magic
)) {
923 ErrorMessageF("failed to get magic\n");
927 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
928 ErrorMessageF("failed to authenticate magic %d\n", magic
);
933 /* If the server does not support the protocol for
934 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
937 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
938 (const __DRIextension
**)
939 &pdp
->loader_extensions
[0],
940 &driver_configs
, psc
);
942 if (psc
->driScreen
== NULL
) {
943 ErrorMessageF("failed to create dri screen\n");
947 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
948 dri2BindExtensions(psc
, extensions
);
951 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
953 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
955 psc
->driver_configs
= driver_configs
;
957 psc
->base
.vtable
= &dri2_screen_vtable
;
959 psc
->base
.driScreen
= psp
;
960 psp
->destroyScreen
= dri2DestroyScreen
;
961 psp
->createDrawable
= dri2CreateDrawable
;
962 psp
->swapBuffers
= dri2SwapBuffers
;
963 psp
->getDrawableMSC
= NULL
;
964 psp
->waitForMSC
= NULL
;
965 psp
->waitForSBC
= NULL
;
966 psp
->setSwapInterval
= NULL
;
967 psp
->getSwapInterval
= NULL
;
969 if (pdp
->driMinor
>= 2) {
971 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
974 psp
->waitForMSC
= dri2WaitForMSC
;
975 psp
->waitForSBC
= dri2WaitForSBC
;
977 #ifdef X_DRI2SwapInterval
978 psp
->setSwapInterval
= dri2SetSwapInterval
;
979 psp
->getSwapInterval
= dri2GetSwapInterval
;
981 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
982 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
986 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
988 psp
->copySubBuffer
= dri2CopySubBuffer
;
989 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
1000 dlclose(psc
->driver
);
1003 glx_screen_cleanup(&psc
->base
);
1009 /* Called from __glXFreeDisplayPrivate.
1012 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
1014 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
1016 __glxHashDestroy(pdp
->dri2Hash
);
1020 _X_HIDDEN __GLXDRIdrawable
*
1021 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
1023 struct glx_display
*d
= __glXInitialize(dpy
);
1024 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
1025 __GLXDRIdrawable
*pdraw
;
1027 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
1034 * Allocate, initialize and return a __DRIdisplayPrivate object.
1035 * This is called from __glXInitialize() when we are given a new
1038 _X_HIDDEN __GLXDRIdisplay
*
1039 dri2CreateDisplay(Display
* dpy
)
1041 struct dri2_display
*pdp
;
1042 int eventBase
, errorBase
, i
;
1044 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
1047 pdp
= Xmalloc(sizeof *pdp
);
1051 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1057 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1058 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1060 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1061 pdp
->base
.createScreen
= dri2CreateScreen
;
1064 if (pdp
->driMinor
< 1)
1065 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1067 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1069 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1071 #ifdef __DRI_USE_INVALIDATE
1072 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1074 pdp
->loader_extensions
[i
++] = NULL
;
1076 pdp
->dri2Hash
= __glxHashCreate();
1077 if (pdp
->dri2Hash
== NULL
) {
1085 #endif /* GLX_DIRECT_RENDERING */