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
60 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
61 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
62 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
64 struct __GLXDRIdisplayPrivateRec
69 ** XFree86-DRI version information
75 int invalidateAvailable
;
77 const __DRIextension
*loader_extensions
[4];
80 struct __GLXDRIcontextPrivateRec
83 __DRIcontext
*driContext
;
84 __GLXscreenConfigs
*psc
;
87 struct __GLXDRIdrawablePrivateRec
89 __GLXDRIdrawable base
;
90 __DRIbuffer buffers
[5];
98 static void dri2WaitX(__GLXDRIdrawable
* pdraw
);
101 dri2DestroyContext(__GLXDRIcontext
* context
,
102 __GLXscreenConfigs
* psc
, Display
* dpy
)
104 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
105 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
107 (*core
->destroyContext
) (pcp
->driContext
);
113 dri2BindContext(__GLXDRIcontext
* context
,
114 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
116 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
117 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
119 return (*core
->bindContext
) (pcp
->driContext
,
120 draw
->driDrawable
, read
->driDrawable
);
124 dri2UnbindContext(__GLXDRIcontext
* context
)
126 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
127 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
129 (*core
->unbindContext
) (pcp
->driContext
);
132 static __GLXDRIcontext
*
133 dri2CreateContext(__GLXscreenConfigs
* psc
,
134 const __GLcontextModes
* mode
,
135 GLXContext gc
, GLXContext shareList
, int renderType
)
137 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
138 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
139 __DRIcontext
*shared
= NULL
;
142 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
143 shared
= pcp_shared
->driContext
;
146 pcp
= Xmalloc(sizeof *pcp
);
152 (*psc
->dri2
->createNewContext
) (psc
->__driScreen
,
153 config
->driConfig
, shared
, pcp
);
154 gc
->__driContext
= pcp
->driContext
;
156 if (pcp
->driContext
== NULL
) {
161 pcp
->base
.destroyContext
= dri2DestroyContext
;
162 pcp
->base
.bindContext
= dri2BindContext
;
163 pcp
->base
.unbindContext
= dri2UnbindContext
;
169 dri2DestroyDrawable(__GLXDRIdrawable
* pdraw
)
171 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
173 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
174 DRI2DestroyDrawable(pdraw
->psc
->dpy
, pdraw
->xDrawable
);
178 static __GLXDRIdrawable
*
179 dri2CreateDrawable(__GLXscreenConfigs
* psc
,
181 GLXDrawable drawable
, const __GLcontextModes
* modes
)
183 __GLXDRIdrawablePrivate
*pdraw
;
184 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
185 __GLXdisplayPrivate
*dpyPriv
;
186 __GLXDRIdisplayPrivate
*pdp
;
187 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
189 pdraw
= Xmalloc(sizeof(*pdraw
));
193 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
194 pdraw
->base
.xDrawable
= xDrawable
;
195 pdraw
->base
.drawable
= drawable
;
196 pdraw
->base
.psc
= psc
;
197 pdraw
->bufferCount
= 0;
198 pdraw
->swap_interval
= 1; /* default may be overridden below */
199 pdraw
->have_back
= 0;
202 psc
->config
->configQueryi(psc
->__driScreen
, "vblank_mode", &vblank_mode
);
204 switch (vblank_mode
) {
205 case DRI_CONF_VBLANK_NEVER
:
206 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
207 pdraw
->swap_interval
= 0;
209 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
210 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
212 pdraw
->swap_interval
= 1;
216 DRI2CreateDrawable(psc
->dpy
, xDrawable
);
218 dpyPriv
= __glXInitialize(psc
->dpy
);
219 pdp
= (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;;
220 /* Create a new drawable */
221 pdraw
->base
.driDrawable
=
222 (*psc
->dri2
->createNewDrawable
) (psc
->__driScreen
,
223 config
->driConfig
, pdraw
);
225 if (!pdraw
->base
.driDrawable
) {
226 DRI2DestroyDrawable(psc
->dpy
, xDrawable
);
231 #ifdef X_DRI2SwapInterval
233 * Make sure server has the same swap interval we do for the new
236 if (pdp
->swapAvailable
)
237 DRI2SwapInterval(psc
->dpy
, xDrawable
, pdraw
->swap_interval
);
246 dri2DrawableGetMSC(__GLXscreenConfigs
*psc
, __GLXDRIdrawable
*pdraw
,
247 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
249 return DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
, ust
, msc
, sbc
);
258 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
259 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
261 return DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
262 remainder
, ust
, msc
, sbc
);
266 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
267 int64_t *msc
, int64_t *sbc
)
269 return DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_sbc
, ust
, msc
,
273 #endif /* X_DRI2WaitMSC */
276 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
278 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
280 XserverRegion region
;
282 /* Check we have the right attachments */
283 if (!priv
->have_back
)
287 xrect
.y
= priv
->height
- y
- height
;
289 xrect
.height
= height
;
293 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
296 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
297 /* should get a fence ID back from here at some point */
298 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
299 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
300 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
302 /* Refresh the fake front (if present) after we just damaged the real
309 dri2WaitX(__GLXDRIdrawable
*pdraw
)
311 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
313 XserverRegion region
;
315 /* Check we have the right attachments */
316 if (!priv
->have_fake_front
)
321 xrect
.width
= priv
->width
;
322 xrect
.height
= priv
->height
;
326 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
329 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
330 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
331 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
332 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
336 dri2WaitGL(__GLXDRIdrawable
* pdraw
)
338 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
340 XserverRegion region
;
342 if (!priv
->have_fake_front
)
347 xrect
.width
= priv
->width
;
348 xrect
.height
= priv
->height
;
352 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
355 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
356 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
357 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
358 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
362 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
364 dri2WaitGL(loaderPrivate
);
369 dri2DestroyScreen(__GLXscreenConfigs
* psc
)
371 /* Free the direct rendering per screen data */
372 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
374 psc
->__driScreen
= NULL
;
378 * Process list of buffer received from the server
380 * Processes the list of buffers received in a reply from the server to either
381 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
384 process_buffers(__GLXDRIdrawablePrivate
* pdraw
, DRI2Buffer
* buffers
,
389 pdraw
->bufferCount
= count
;
390 pdraw
->have_fake_front
= 0;
391 pdraw
->have_back
= 0;
393 /* This assumes the DRI2 buffer attachment tokens matches the
394 * __DRIbuffer tokens. */
395 for (i
= 0; i
< count
; i
++) {
396 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
397 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
398 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
399 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
400 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
401 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
402 pdraw
->have_fake_front
= 1;
403 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
404 pdraw
->have_back
= 1;
410 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
413 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
414 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
415 __GLXDRIdisplayPrivate
*pdp
=
416 (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;
421 (*pdraw
->psc
->f
->flush
)(pdraw
->driDrawable
);
424 /* Old servers can't handle swapbuffers */
425 if (!pdp
->swapAvailable
) {
426 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
430 /* Old servers don't send invalidate events */
431 if (!pdp
->invalidateAvailable
)
432 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->drawable
);
434 #ifdef X_DRI2SwapBuffers
435 DRI2SwapBuffers(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
443 dri2GetBuffers(__DRIdrawable
* driDrawable
,
444 int *width
, int *height
,
445 unsigned int *attachments
, int count
,
446 int *out_count
, void *loaderPrivate
)
448 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
451 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
452 width
, height
, attachments
, count
, out_count
);
456 pdraw
->width
= *width
;
457 pdraw
->height
= *height
;
458 process_buffers(pdraw
, buffers
, *out_count
);
462 return pdraw
->buffers
;
466 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
467 int *width
, int *height
,
468 unsigned int *attachments
, int count
,
469 int *out_count
, void *loaderPrivate
)
471 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
474 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
475 pdraw
->base
.xDrawable
,
476 width
, height
, attachments
,
481 pdraw
->width
= *width
;
482 pdraw
->height
= *height
;
483 process_buffers(pdraw
, buffers
, *out_count
);
487 return pdraw
->buffers
;
490 #ifdef X_DRI2SwapInterval
493 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
495 __GLXscreenConfigs
*psc
= pdraw
->psc
;
496 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
497 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
500 psc
->config
->configQueryi(psc
->__driScreen
, "vblank_mode", &vblank_mode
);
502 switch (vblank_mode
) {
503 case DRI_CONF_VBLANK_NEVER
:
505 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
513 DRI2SwapInterval(priv
->base
.psc
->dpy
, pdraw
->xDrawable
, interval
);
514 priv
->swap_interval
= interval
;
518 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
520 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
522 return priv
->swap_interval
;
525 #endif /* X_DRI2SwapInterval */
527 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
528 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
530 dri2FlushFrontBuffer
,
531 dri2GetBuffersWithFormat
,
534 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
535 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
537 dri2FlushFrontBuffer
,
541 #ifdef __DRI_USE_INVALIDATE
542 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
543 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
548 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
550 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, NULL
);
552 #if __DRI2_FLUSH_VERSION >= 3
553 if (pdraw
&& pdraw
->psc
->f
)
554 pdraw
->psc
->f
->invalidate(pdraw
->driDrawable
);
558 static __GLXDRIscreen
*
559 dri2CreateScreen(__GLXscreenConfigs
* psc
, int screen
,
560 __GLXdisplayPrivate
* priv
)
562 const __DRIconfig
**driver_configs
;
563 const __DRIextension
**extensions
;
564 const __GLXDRIdisplayPrivate
*const pdp
= (__GLXDRIdisplayPrivate
*)
567 char *driverName
, *deviceName
;
571 psp
= Xmalloc(sizeof *psp
);
575 if (!DRI2Connect(psc
->dpy
, RootWindow(psc
->dpy
, screen
),
576 &driverName
, &deviceName
)) {
581 psc
->driver
= driOpenDriver(driverName
);
582 if (psc
->driver
== NULL
) {
583 ErrorMessageF("driver pointer missing\n");
587 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
588 if (extensions
== NULL
) {
589 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
593 for (i
= 0; extensions
[i
]; i
++) {
594 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
595 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
596 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
597 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
600 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
601 ErrorMessageF("core dri or dri2 extension not found\n");
605 psc
->fd
= open(deviceName
, O_RDWR
);
607 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
611 if (drmGetMagic(psc
->fd
, &magic
)) {
612 ErrorMessageF("failed to get magic\n");
616 if (!DRI2Authenticate(psc
->dpy
, RootWindow(psc
->dpy
, screen
), magic
)) {
617 ErrorMessageF("failed to authenticate magic %d\n", magic
);
622 /* If the server does not support the protocol for
623 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
626 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
627 (const __DRIextension
**)
628 &pdp
->loader_extensions
[0],
629 &driver_configs
, psc
);
631 if (psc
->__driScreen
== NULL
) {
632 ErrorMessageF("failed to create dri screen\n");
636 driBindCommonExtensions(psc
);
637 dri2BindExtensions(psc
);
639 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
640 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
642 psc
->driver_configs
= driver_configs
;
644 psp
->destroyScreen
= dri2DestroyScreen
;
645 psp
->createContext
= dri2CreateContext
;
646 psp
->createDrawable
= dri2CreateDrawable
;
647 psp
->swapBuffers
= dri2SwapBuffers
;
648 psp
->waitGL
= dri2WaitGL
;
649 psp
->waitX
= dri2WaitX
;
650 psp
->getDrawableMSC
= NULL
;
651 psp
->waitForMSC
= NULL
;
652 psp
->waitForSBC
= NULL
;
653 psp
->setSwapInterval
= NULL
;
654 psp
->getSwapInterval
= NULL
;
656 if (pdp
->driMinor
>= 2) {
658 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
661 psp
->waitForMSC
= dri2WaitForMSC
;
662 psp
->waitForSBC
= dri2WaitForSBC
;
664 #ifdef X_DRI2SwapInterval
665 psp
->setSwapInterval
= dri2SetSwapInterval
;
666 psp
->getSwapInterval
= dri2GetSwapInterval
;
668 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
669 __glXEnableDirectExtension(psc
, "GLX_OML_sync_control");
673 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
675 psp
->copySubBuffer
= dri2CopySubBuffer
;
676 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer");
688 /* FIXME: clean up here */
693 /* Called from __glXFreeDisplayPrivate.
696 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
702 * Allocate, initialize and return a __DRIdisplayPrivate object.
703 * This is called from __glXInitialize() when we are given a new
706 _X_HIDDEN __GLXDRIdisplay
*
707 dri2CreateDisplay(Display
* dpy
)
709 __GLXDRIdisplayPrivate
*pdp
;
710 int eventBase
, errorBase
, i
;
712 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
715 pdp
= Xmalloc(sizeof *pdp
);
719 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
725 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
726 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
728 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
729 pdp
->base
.createScreen
= dri2CreateScreen
;
732 if (pdp
->driMinor
< 1)
733 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
735 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
737 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
739 #ifdef __DRI_USE_INVALIDATE
740 if (pdp
->invalidateAvailable
)
741 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
743 pdp
->loader_extensions
[i
++] = NULL
;
748 #endif /* GLX_DIRECT_RENDERING */