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/extensions/Xdamage.h>
39 #include "glxclient.h"
40 #include <X11/extensions/dri2proto.h>
45 #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 __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 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
375 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
376 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
378 XserverRegion region
;
380 /* Check we have the right attachments */
381 if (!priv
->have_back
)
385 xrect
.y
= priv
->height
- y
- height
;
387 xrect
.height
= height
;
391 (*psc
->f
->flush
) (priv
->driDrawable
);
394 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
395 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
396 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
398 /* Refresh the fake front (if present) after we just damaged the real
401 if (priv
->have_fake_front
)
402 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
403 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
405 XFixesDestroyRegion(psc
->base
.dpy
, region
);
409 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
412 XserverRegion region
;
413 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
417 xrect
.width
= priv
->width
;
418 xrect
.height
= priv
->height
;
422 (*psc
->f
->flush
) (priv
->driDrawable
);
425 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
426 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
427 XFixesDestroyRegion(psc
->base
.dpy
, region
);
432 dri2_wait_x(struct glx_context
*gc
)
434 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
435 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
437 if (priv
== NULL
|| !priv
->have_fake_front
)
440 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
444 dri2_wait_gl(struct glx_context
*gc
)
446 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
447 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
449 if (priv
== NULL
|| !priv
->have_fake_front
)
452 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
456 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
458 struct dri2_drawable
*pdraw
= loaderPrivate
;
462 if (!pdraw
->base
.psc
)
465 struct glx_display
*priv
= __glXInitialize(pdraw
->base
.psc
->dpy
);
466 struct dri2_display
*pdp
= (struct dri2_display
*)priv
->dri2Display
;
467 struct glx_context
*gc
= __glXGetCurrentContext();
469 /* Old servers don't send invalidate events */
470 if (!pdp
->invalidateAvailable
)
471 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
478 dri2DestroyScreen(struct glx_screen
*base
)
480 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
482 /* Free the direct rendering per screen data */
483 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
484 driDestroyConfigs(psc
->driver_configs
);
490 * Process list of buffer received from the server
492 * Processes the list of buffers received in a reply from the server to either
493 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
496 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
501 pdraw
->bufferCount
= count
;
502 pdraw
->have_fake_front
= 0;
503 pdraw
->have_back
= 0;
505 /* This assumes the DRI2 buffer attachment tokens matches the
506 * __DRIbuffer tokens. */
507 for (i
= 0; i
< count
; i
++) {
508 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
509 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
510 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
511 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
512 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
513 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
514 pdraw
->have_fake_front
= 1;
515 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
516 pdraw
->have_back
= 1;
521 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
523 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
524 __GLXDRIdrawable
*pdraw
;
525 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
526 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
528 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
532 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
535 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
536 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
537 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
538 struct dri2_display
*pdp
=
539 (struct dri2_display
*)dpyPriv
->dri2Display
;
544 struct glx_context
*gc
= __glXGetCurrentContext();
547 (*psc
->f
->flush
)(priv
->driDrawable
);
552 /* Old servers don't send invalidate events */
553 if (!pdp
->invalidateAvailable
)
554 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
556 /* Old servers can't handle swapbuffers */
557 if (!pdp
->swapAvailable
) {
558 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
562 #ifdef X_DRI2SwapBuffers
563 DRI2SwapBuffers(psc
->base
.dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
571 dri2GetBuffers(__DRIdrawable
* driDrawable
,
572 int *width
, int *height
,
573 unsigned int *attachments
, int count
,
574 int *out_count
, void *loaderPrivate
)
576 struct dri2_drawable
*pdraw
= loaderPrivate
;
579 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
580 width
, height
, attachments
, count
, out_count
);
584 pdraw
->width
= *width
;
585 pdraw
->height
= *height
;
586 process_buffers(pdraw
, buffers
, *out_count
);
590 return pdraw
->buffers
;
594 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
595 int *width
, int *height
,
596 unsigned int *attachments
, int count
,
597 int *out_count
, void *loaderPrivate
)
599 struct dri2_drawable
*pdraw
= loaderPrivate
;
602 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
603 pdraw
->base
.xDrawable
,
604 width
, height
, attachments
,
609 pdraw
->width
= *width
;
610 pdraw
->height
= *height
;
611 process_buffers(pdraw
, buffers
, *out_count
);
615 return pdraw
->buffers
;
618 #ifdef X_DRI2SwapInterval
621 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
623 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
624 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
625 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
628 psc
->config
->configQueryi(psc
->driScreen
,
629 "vblank_mode", &vblank_mode
);
631 switch (vblank_mode
) {
632 case DRI_CONF_VBLANK_NEVER
:
633 return GLX_BAD_VALUE
;
634 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
636 return GLX_BAD_VALUE
;
642 DRI2SwapInterval(priv
->base
.psc
->dpy
, priv
->base
.xDrawable
, interval
);
643 priv
->swap_interval
= interval
;
649 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
651 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
653 return priv
->swap_interval
;
656 #endif /* X_DRI2SwapInterval */
658 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
659 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
661 dri2FlushFrontBuffer
,
662 dri2GetBuffersWithFormat
,
665 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
666 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
668 dri2FlushFrontBuffer
,
672 #ifdef __DRI_USE_INVALIDATE
673 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
674 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
679 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
681 __GLXDRIdrawable
*pdraw
=
682 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
683 struct dri2_screen
*psc
;
684 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
689 psc
= (struct dri2_screen
*) pdraw
->psc
;
691 #if __DRI2_FLUSH_VERSION >= 3
692 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
693 psc
->f
->invalidate(pdp
->driDrawable
);
698 dri2_bind_tex_image(Display
* dpy
,
699 GLXDrawable drawable
,
700 int buffer
, const int *attrib_list
)
702 struct glx_context
*gc
= __glXGetCurrentContext();
703 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
704 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
705 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
706 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
707 struct dri2_display
*pdp
=
708 (struct dri2_display
*) dpyPriv
->dri2Display
;
709 struct dri2_screen
*psc
;
712 psc
= (struct dri2_screen
*) base
->psc
;
714 #if __DRI2_FLUSH_VERSION >= 3
715 if (!pdp
->invalidateAvailable
&& psc
->f
&&
716 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
717 psc
->f
->invalidate(pdraw
->driDrawable
);
720 if (psc
->texBuffer
->base
.version
>= 2 &&
721 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
722 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
723 pdraw
->base
.textureTarget
,
724 pdraw
->base
.textureFormat
,
728 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
729 pdraw
->base
.textureTarget
,
736 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
738 #if __DRI_TEX_BUFFER_VERSION >= 3
739 struct glx_context
*gc
= __glXGetCurrentContext();
740 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
741 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
742 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
743 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
744 struct dri2_display
*pdp
=
745 (struct dri2_display
*) dpyPriv
->dri2Display
;
746 struct dri2_screen
*psc
;
749 psc
= (struct dri2_screen
*) base
->psc
;
751 if (psc
->texBuffer
->base
.version
>= 3 &&
752 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
753 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
754 pdraw
->base
.textureTarget
,
761 static const struct glx_context_vtable dri2_context_vtable
= {
762 dri2_destroy_context
,
769 dri2_release_tex_image
,
773 dri2BindExtensions(struct dri2_screen
*psc
, const __DRIextension
**extensions
)
777 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
778 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
779 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
780 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
782 /* FIXME: if DRI2 version supports it... */
783 __glXEnableDirectExtension(&psc
->base
, "INTEL_swap_event");
785 for (i
= 0; extensions
[i
]; i
++) {
786 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
787 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
788 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
791 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
792 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
793 /* internal driver extension, no GL extension exposed */
796 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
797 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
801 static const struct glx_screen_vtable dri2_screen_vtable
= {
805 static struct glx_screen
*
806 dri2CreateScreen(int screen
, struct glx_display
* priv
)
808 const __DRIconfig
**driver_configs
;
809 const __DRIextension
**extensions
;
810 const struct dri2_display
*const pdp
= (struct dri2_display
*)
812 struct dri2_screen
*psc
;
814 char *driverName
, *deviceName
;
818 psc
= Xmalloc(sizeof *psc
);
822 memset(psc
, 0, sizeof *psc
);
825 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
830 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
831 &driverName
, &deviceName
)) {
832 glx_screen_cleanup(&psc
->base
);
837 psc
->driver
= driOpenDriver(driverName
);
838 if (psc
->driver
== NULL
) {
839 ErrorMessageF("driver pointer missing\n");
843 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
844 if (extensions
== NULL
) {
845 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
849 for (i
= 0; extensions
[i
]; i
++) {
850 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
851 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
852 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
853 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
856 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
857 ErrorMessageF("core dri or dri2 extension not found\n");
861 psc
->fd
= open(deviceName
, O_RDWR
);
863 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
867 if (drmGetMagic(psc
->fd
, &magic
)) {
868 ErrorMessageF("failed to get magic\n");
872 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
873 ErrorMessageF("failed to authenticate magic %d\n", magic
);
878 /* If the server does not support the protocol for
879 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
882 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
883 (const __DRIextension
**)
884 &pdp
->loader_extensions
[0],
885 &driver_configs
, psc
);
887 if (psc
->driScreen
== NULL
) {
888 ErrorMessageF("failed to create dri screen\n");
892 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
893 dri2BindExtensions(psc
, extensions
);
896 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
898 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
900 psc
->driver_configs
= driver_configs
;
902 psc
->base
.vtable
= &dri2_screen_vtable
;
904 psc
->base
.driScreen
= psp
;
905 psp
->destroyScreen
= dri2DestroyScreen
;
906 psp
->createDrawable
= dri2CreateDrawable
;
907 psp
->swapBuffers
= dri2SwapBuffers
;
908 psp
->getDrawableMSC
= NULL
;
909 psp
->waitForMSC
= NULL
;
910 psp
->waitForSBC
= NULL
;
911 psp
->setSwapInterval
= NULL
;
912 psp
->getSwapInterval
= NULL
;
914 if (pdp
->driMinor
>= 2) {
916 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
919 psp
->waitForMSC
= dri2WaitForMSC
;
920 psp
->waitForSBC
= dri2WaitForSBC
;
922 #ifdef X_DRI2SwapInterval
923 psp
->setSwapInterval
= dri2SetSwapInterval
;
924 psp
->getSwapInterval
= dri2GetSwapInterval
;
926 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
927 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
931 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
933 psp
->copySubBuffer
= dri2CopySubBuffer
;
934 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
945 dlclose(psc
->driver
);
948 glx_screen_cleanup(&psc
->base
);
954 /* Called from __glXFreeDisplayPrivate.
957 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
959 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
961 __glxHashDestroy(pdp
->dri2Hash
);
965 _X_HIDDEN __GLXDRIdrawable
*
966 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
968 struct glx_display
*d
= __glXInitialize(dpy
);
969 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
970 __GLXDRIdrawable
*pdraw
;
972 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
979 * Allocate, initialize and return a __DRIdisplayPrivate object.
980 * This is called from __glXInitialize() when we are given a new
983 _X_HIDDEN __GLXDRIdisplay
*
984 dri2CreateDisplay(Display
* dpy
)
986 struct dri2_display
*pdp
;
987 int eventBase
, errorBase
, i
;
989 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
992 pdp
= Xmalloc(sizeof *pdp
);
996 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1002 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1003 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1005 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1006 pdp
->base
.createScreen
= dri2CreateScreen
;
1009 if (pdp
->driMinor
< 1)
1010 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1012 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1014 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1016 #ifdef __DRI_USE_INVALIDATE
1017 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1019 pdp
->loader_extensions
[i
++] = NULL
;
1021 pdp
->dri2Hash
= __glxHashCreate();
1022 if (pdp
->dri2Hash
== NULL
) {
1030 #endif /* GLX_DIRECT_RENDERING */