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 if (pdraw
== NULL
|| pread
== NULL
)
147 return GLXBadDrawable
;
149 if (!(*psc
->core
->bindContext
) (pcp
->driContext
,
150 pdraw
->driDrawable
, pread
->driDrawable
))
151 return GLXBadContext
;
153 /* If the server doesn't send invalidate events, we may miss a
154 * resize before the rendering starts. Invalidate the buffers now
155 * so the driver will recheck before rendering starts. */
156 pdp
= (struct dri2_display
*) psc
->base
.display
;
157 if (!pdp
->invalidateAvailable
) {
158 dri2InvalidateBuffers(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
160 dri2InvalidateBuffers(psc
->base
.dpy
, pread
->base
.xDrawable
);
167 dri2_unbind_context(struct glx_context
*context
, struct glx_context
*new)
169 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
170 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
172 (*psc
->core
->unbindContext
) (pcp
->driContext
);
175 driReleaseDrawables(&pcp
->base
);
178 static struct glx_context
*
179 dri2_create_context(struct glx_screen
*base
,
180 struct glx_config
*config_base
,
181 struct glx_context
*shareList
, int renderType
)
183 struct dri2_context
*pcp
, *pcp_shared
;
184 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
185 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
186 __DRIcontext
*shared
= NULL
;
189 pcp_shared
= (struct dri2_context
*) shareList
;
190 shared
= pcp_shared
->driContext
;
193 pcp
= Xmalloc(sizeof *pcp
);
197 memset(pcp
, 0, sizeof *pcp
);
198 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
204 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
205 config
->driConfig
, shared
, pcp
);
207 if (pcp
->driContext
== NULL
) {
212 pcp
->base
.vtable
= &dri2_context_vtable
;
218 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
220 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
221 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
222 struct glx_display
*dpyPriv
= psc
->base
.display
;
223 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
225 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
226 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
228 /* If it's a GLX 1.3 drawables, we can destroy the DRI2 drawable
229 * now, as the application explicitly asked to destroy the GLX
230 * drawable. Otherwise, for legacy drawables, we let the DRI2
231 * drawable linger on the server, since there's no good way of
232 * knowing when the application is done with it. The server will
233 * destroy the DRI2 drawable when it destroys the X drawable or the
234 * client exits anyway. */
235 if (pdraw
->base
.xDrawable
!= pdraw
->base
.drawable
)
236 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
241 static __GLXDRIdrawable
*
242 dri2CreateDrawable(struct glx_screen
*base
, XID xDrawable
,
243 GLXDrawable drawable
, struct glx_config
*config_base
)
245 struct dri2_drawable
*pdraw
;
246 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
247 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
248 struct glx_display
*dpyPriv
;
249 struct dri2_display
*pdp
;
250 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
252 pdraw
= Xmalloc(sizeof(*pdraw
));
256 memset(pdraw
, 0, sizeof *pdraw
);
257 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
258 pdraw
->base
.xDrawable
= xDrawable
;
259 pdraw
->base
.drawable
= drawable
;
260 pdraw
->base
.psc
= &psc
->base
;
261 pdraw
->bufferCount
= 0;
262 pdraw
->swap_interval
= 1; /* default may be overridden below */
263 pdraw
->have_back
= 0;
266 psc
->config
->configQueryi(psc
->driScreen
,
267 "vblank_mode", &vblank_mode
);
269 switch (vblank_mode
) {
270 case DRI_CONF_VBLANK_NEVER
:
271 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
272 pdraw
->swap_interval
= 0;
274 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
275 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
277 pdraw
->swap_interval
= 1;
281 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
283 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
284 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
285 /* Create a new drawable */
287 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
288 config
->driConfig
, pdraw
);
290 if (!pdraw
->driDrawable
) {
291 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
296 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
297 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
298 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
304 #ifdef X_DRI2SwapInterval
306 * Make sure server has the same swap interval we do for the new
309 if (pdp
->swapAvailable
)
310 DRI2SwapInterval(psc
->base
.dpy
, xDrawable
, pdraw
->swap_interval
);
319 dri2DrawableGetMSC(struct glx_screen
*psc
, __GLXDRIdrawable
*pdraw
,
320 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
322 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
325 ret
= DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
,
326 &dri2_ust
, &dri2_msc
, &dri2_sbc
);
340 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
341 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
343 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
346 ret
= DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
347 remainder
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
356 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
357 int64_t *msc
, int64_t *sbc
)
359 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
362 ret
= DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
,
363 target_sbc
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
371 #endif /* X_DRI2WaitMSC */
374 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
376 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
377 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
379 XserverRegion region
;
381 /* Check we have the right attachments */
382 if (!priv
->have_back
)
386 xrect
.y
= priv
->height
- y
- height
;
388 xrect
.height
= height
;
392 (*psc
->f
->flush
) (priv
->driDrawable
);
395 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
396 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
397 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
399 /* Refresh the fake front (if present) after we just damaged the real
402 if (priv
->have_fake_front
)
403 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
404 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
406 XFixesDestroyRegion(psc
->base
.dpy
, region
);
410 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
413 XserverRegion region
;
414 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
418 xrect
.width
= priv
->width
;
419 xrect
.height
= priv
->height
;
423 (*psc
->f
->flush
) (priv
->driDrawable
);
426 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
427 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
428 XFixesDestroyRegion(psc
->base
.dpy
, region
);
433 dri2_wait_x(struct glx_context
*gc
)
435 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
436 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
438 if (priv
== NULL
|| !priv
->have_fake_front
)
441 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
445 dri2_wait_gl(struct glx_context
*gc
)
447 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
448 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
450 if (priv
== NULL
|| !priv
->have_fake_front
)
453 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
457 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
459 struct dri2_drawable
*pdraw
= loaderPrivate
;
463 if (!pdraw
->base
.psc
)
466 struct glx_display
*priv
= __glXInitialize(pdraw
->base
.psc
->dpy
);
467 struct dri2_display
*pdp
= (struct dri2_display
*)priv
->dri2Display
;
468 struct glx_context
*gc
= __glXGetCurrentContext();
470 /* Old servers don't send invalidate events */
471 if (!pdp
->invalidateAvailable
)
472 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.xDrawable
);
479 dri2DestroyScreen(struct glx_screen
*base
)
481 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
483 /* Free the direct rendering per screen data */
484 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
485 driDestroyConfigs(psc
->driver_configs
);
491 * Process list of buffer received from the server
493 * Processes the list of buffers received in a reply from the server to either
494 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
497 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
502 pdraw
->bufferCount
= count
;
503 pdraw
->have_fake_front
= 0;
504 pdraw
->have_back
= 0;
506 /* This assumes the DRI2 buffer attachment tokens matches the
507 * __DRIbuffer tokens. */
508 for (i
= 0; i
< count
; i
++) {
509 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
510 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
511 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
512 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
513 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
514 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
515 pdraw
->have_fake_front
= 1;
516 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
517 pdraw
->have_back
= 1;
522 unsigned dri2GetSwapEventType(Display
* dpy
, XID drawable
)
524 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
525 __GLXDRIdrawable
*pdraw
;
526 pdraw
= dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
527 if (!pdraw
|| !(pdraw
->eventMask
& GLX_BUFFER_SWAP_COMPLETE_INTEL_MASK
))
529 return glx_dpy
->codes
->first_event
+ GLX_BufferSwapComplete
;
533 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
536 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
537 struct glx_display
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
538 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
539 struct dri2_display
*pdp
=
540 (struct dri2_display
*)dpyPriv
->dri2Display
;
545 struct glx_context
*gc
= __glXGetCurrentContext();
548 (*psc
->f
->flush
)(priv
->driDrawable
);
553 /* Old servers don't send invalidate events */
554 if (!pdp
->invalidateAvailable
)
555 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->xDrawable
);
557 /* Old servers can't handle swapbuffers */
558 if (!pdp
->swapAvailable
) {
559 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
563 #ifdef X_DRI2SwapBuffers
564 DRI2SwapBuffers(psc
->base
.dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
572 dri2GetBuffers(__DRIdrawable
* driDrawable
,
573 int *width
, int *height
,
574 unsigned int *attachments
, int count
,
575 int *out_count
, void *loaderPrivate
)
577 struct dri2_drawable
*pdraw
= loaderPrivate
;
580 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
581 width
, height
, attachments
, count
, out_count
);
585 pdraw
->width
= *width
;
586 pdraw
->height
= *height
;
587 process_buffers(pdraw
, buffers
, *out_count
);
591 return pdraw
->buffers
;
595 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
596 int *width
, int *height
,
597 unsigned int *attachments
, int count
,
598 int *out_count
, void *loaderPrivate
)
600 struct dri2_drawable
*pdraw
= loaderPrivate
;
603 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
604 pdraw
->base
.xDrawable
,
605 width
, height
, attachments
,
610 pdraw
->width
= *width
;
611 pdraw
->height
= *height
;
612 process_buffers(pdraw
, buffers
, *out_count
);
616 return pdraw
->buffers
;
619 #ifdef X_DRI2SwapInterval
622 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
624 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
625 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
626 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
629 psc
->config
->configQueryi(psc
->driScreen
,
630 "vblank_mode", &vblank_mode
);
632 switch (vblank_mode
) {
633 case DRI_CONF_VBLANK_NEVER
:
634 return GLX_BAD_VALUE
;
635 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
637 return GLX_BAD_VALUE
;
643 DRI2SwapInterval(priv
->base
.psc
->dpy
, priv
->base
.xDrawable
, interval
);
644 priv
->swap_interval
= interval
;
650 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
652 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
654 return priv
->swap_interval
;
657 #endif /* X_DRI2SwapInterval */
659 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
660 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
662 dri2FlushFrontBuffer
,
663 dri2GetBuffersWithFormat
,
666 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
667 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
669 dri2FlushFrontBuffer
,
673 #ifdef __DRI_USE_INVALIDATE
674 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
675 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
680 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
682 __GLXDRIdrawable
*pdraw
=
683 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
684 struct dri2_screen
*psc
;
685 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
690 psc
= (struct dri2_screen
*) pdraw
->psc
;
692 #if __DRI2_FLUSH_VERSION >= 3
693 if (pdraw
&& psc
->f
&& psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
694 psc
->f
->invalidate(pdp
->driDrawable
);
699 dri2_bind_tex_image(Display
* dpy
,
700 GLXDrawable drawable
,
701 int buffer
, const int *attrib_list
)
703 struct glx_context
*gc
= __glXGetCurrentContext();
704 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
705 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
706 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
707 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
708 struct dri2_display
*pdp
=
709 (struct dri2_display
*) dpyPriv
->dri2Display
;
710 struct dri2_screen
*psc
;
713 psc
= (struct dri2_screen
*) base
->psc
;
715 #if __DRI2_FLUSH_VERSION >= 3
716 if (!pdp
->invalidateAvailable
&& psc
->f
&&
717 psc
->f
->base
.version
>= 3 && psc
->f
->invalidate
)
718 psc
->f
->invalidate(pdraw
->driDrawable
);
721 if (psc
->texBuffer
->base
.version
>= 2 &&
722 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
723 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
724 pdraw
->base
.textureTarget
,
725 pdraw
->base
.textureFormat
,
729 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
730 pdraw
->base
.textureTarget
,
737 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
739 #if __DRI_TEX_BUFFER_VERSION >= 3
740 struct glx_context
*gc
= __glXGetCurrentContext();
741 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
742 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
743 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
744 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
745 struct dri2_display
*pdp
=
746 (struct dri2_display
*) dpyPriv
->dri2Display
;
747 struct dri2_screen
*psc
;
750 psc
= (struct dri2_screen
*) base
->psc
;
752 if (psc
->texBuffer
->base
.version
>= 3 &&
753 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
754 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
755 pdraw
->base
.textureTarget
,
762 static const struct glx_context_vtable dri2_context_vtable
= {
763 dri2_destroy_context
,
770 dri2_release_tex_image
,
774 dri2BindExtensions(struct dri2_screen
*psc
, const __DRIextension
**extensions
)
778 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
779 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
780 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
781 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
783 /* FIXME: if DRI2 version supports it... */
784 __glXEnableDirectExtension(&psc
->base
, "INTEL_swap_event");
786 for (i
= 0; extensions
[i
]; i
++) {
787 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
788 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
789 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
792 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
793 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
794 /* internal driver extension, no GL extension exposed */
797 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
798 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
802 static const struct glx_screen_vtable dri2_screen_vtable
= {
806 static struct glx_screen
*
807 dri2CreateScreen(int screen
, struct glx_display
* priv
)
809 const __DRIconfig
**driver_configs
;
810 const __DRIextension
**extensions
;
811 const struct dri2_display
*const pdp
= (struct dri2_display
*)
813 struct dri2_screen
*psc
;
815 char *driverName
, *deviceName
;
819 psc
= Xmalloc(sizeof *psc
);
823 memset(psc
, 0, sizeof *psc
);
826 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
831 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
832 &driverName
, &deviceName
)) {
833 glx_screen_cleanup(&psc
->base
);
838 psc
->driver
= driOpenDriver(driverName
);
839 if (psc
->driver
== NULL
) {
840 ErrorMessageF("driver pointer missing\n");
844 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
845 if (extensions
== NULL
) {
846 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
850 for (i
= 0; extensions
[i
]; i
++) {
851 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
852 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
853 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
854 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
857 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
858 ErrorMessageF("core dri or dri2 extension not found\n");
862 psc
->fd
= open(deviceName
, O_RDWR
);
864 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
868 if (drmGetMagic(psc
->fd
, &magic
)) {
869 ErrorMessageF("failed to get magic\n");
873 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
874 ErrorMessageF("failed to authenticate magic %d\n", magic
);
879 /* If the server does not support the protocol for
880 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
883 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
884 (const __DRIextension
**)
885 &pdp
->loader_extensions
[0],
886 &driver_configs
, psc
);
888 if (psc
->driScreen
== NULL
) {
889 ErrorMessageF("failed to create dri screen\n");
893 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
894 dri2BindExtensions(psc
, extensions
);
897 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
899 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
901 psc
->driver_configs
= driver_configs
;
903 psc
->base
.vtable
= &dri2_screen_vtable
;
905 psc
->base
.driScreen
= psp
;
906 psp
->destroyScreen
= dri2DestroyScreen
;
907 psp
->createDrawable
= dri2CreateDrawable
;
908 psp
->swapBuffers
= dri2SwapBuffers
;
909 psp
->getDrawableMSC
= NULL
;
910 psp
->waitForMSC
= NULL
;
911 psp
->waitForSBC
= NULL
;
912 psp
->setSwapInterval
= NULL
;
913 psp
->getSwapInterval
= NULL
;
915 if (pdp
->driMinor
>= 2) {
917 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
920 psp
->waitForMSC
= dri2WaitForMSC
;
921 psp
->waitForSBC
= dri2WaitForSBC
;
923 #ifdef X_DRI2SwapInterval
924 psp
->setSwapInterval
= dri2SetSwapInterval
;
925 psp
->getSwapInterval
= dri2GetSwapInterval
;
927 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
928 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
932 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
934 psp
->copySubBuffer
= dri2CopySubBuffer
;
935 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
946 dlclose(psc
->driver
);
949 glx_screen_cleanup(&psc
->base
);
955 /* Called from __glXFreeDisplayPrivate.
958 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
960 struct dri2_display
*pdp
= (struct dri2_display
*) dpy
;
962 __glxHashDestroy(pdp
->dri2Hash
);
966 _X_HIDDEN __GLXDRIdrawable
*
967 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
969 struct glx_display
*d
= __glXInitialize(dpy
);
970 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
971 __GLXDRIdrawable
*pdraw
;
973 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
980 * Allocate, initialize and return a __DRIdisplayPrivate object.
981 * This is called from __glXInitialize() when we are given a new
984 _X_HIDDEN __GLXDRIdisplay
*
985 dri2CreateDisplay(Display
* dpy
)
987 struct dri2_display
*pdp
;
988 int eventBase
, errorBase
, i
;
990 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
993 pdp
= Xmalloc(sizeof *pdp
);
997 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
1003 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
1004 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
1006 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
1007 pdp
->base
.createScreen
= dri2CreateScreen
;
1010 if (pdp
->driMinor
< 1)
1011 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
1013 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
1015 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
1017 #ifdef __DRI_USE_INVALIDATE
1018 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
1020 pdp
->loader_extensions
[i
++] = NULL
;
1022 pdp
->dri2Hash
= __glxHashCreate();
1023 if (pdp
->dri2Hash
== NULL
) {
1031 #endif /* GLX_DIRECT_RENDERING */