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 #ifdef GLX_DIRECT_RENDERING
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"
50 #include "../../mesa/drivers/dri/common/dri_util.h"
55 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
56 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
57 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
59 struct __GLXDRIdisplayPrivateRec
64 ** XFree86-DRI version information
70 int invalidateAvailable
;
73 struct __GLXDRIcontextPrivateRec
76 __DRIcontext
*driContext
;
77 __GLXscreenConfigs
*psc
;
80 struct __GLXDRIdrawablePrivateRec
82 __GLXDRIdrawable base
;
83 __DRIbuffer buffers
[5];
91 static void dri2WaitX(__GLXDRIdrawable
* pdraw
);
94 dri2DestroyContext(__GLXDRIcontext
* context
,
95 __GLXscreenConfigs
* psc
, Display
* dpy
)
97 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
98 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
100 (*core
->destroyContext
) (pcp
->driContext
);
106 dri2BindContext(__GLXDRIcontext
* context
,
107 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
109 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
110 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
112 return (*core
->bindContext
) (pcp
->driContext
,
113 draw
->driDrawable
, read
->driDrawable
);
117 dri2UnbindContext(__GLXDRIcontext
* context
)
119 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
120 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
122 (*core
->unbindContext
) (pcp
->driContext
);
125 static __GLXDRIcontext
*
126 dri2CreateContext(__GLXscreenConfigs
* psc
,
127 const __GLcontextModes
* mode
,
128 GLXContext gc
, GLXContext shareList
, int renderType
)
130 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
131 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
132 __DRIcontext
*shared
= NULL
;
135 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
136 shared
= pcp_shared
->driContext
;
139 pcp
= Xmalloc(sizeof *pcp
);
145 (*psc
->dri2
->createNewContext
) (psc
->__driScreen
,
146 config
->driConfig
, shared
, pcp
);
147 gc
->__driContext
= pcp
->driContext
;
149 if (pcp
->driContext
== NULL
) {
154 pcp
->base
.destroyContext
= dri2DestroyContext
;
155 pcp
->base
.bindContext
= dri2BindContext
;
156 pcp
->base
.unbindContext
= dri2UnbindContext
;
162 dri2DestroyDrawable(__GLXDRIdrawable
* pdraw
)
164 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
166 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
167 DRI2DestroyDrawable(pdraw
->psc
->dpy
, pdraw
->xDrawable
);
171 static __GLXDRIdrawable
*
172 dri2CreateDrawable(__GLXscreenConfigs
* psc
,
174 GLXDrawable drawable
, const __GLcontextModes
* modes
)
176 __GLXDRIdrawablePrivate
*pdraw
;
177 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
178 __GLXdisplayPrivate
*dpyPriv
;
179 __GLXDRIdisplayPrivate
*pdp
;
181 pdraw
= Xmalloc(sizeof(*pdraw
));
185 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
186 pdraw
->base
.xDrawable
= xDrawable
;
187 pdraw
->base
.drawable
= drawable
;
188 pdraw
->base
.psc
= psc
;
189 pdraw
->bufferCount
= 0;
190 pdraw
->swap_interval
= 1;
192 DRI2CreateDrawable(psc
->dpy
, xDrawable
);
194 dpyPriv
= __glXInitialize(psc
->dpy
);
195 pdp
= (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;;
196 /* Create a new drawable */
197 pdraw
->base
.driDrawable
=
198 (*psc
->dri2
->createNewDrawable
) (psc
->__driScreen
,
199 config
->driConfig
, pdraw
);
201 if (!pdraw
->base
.driDrawable
) {
202 DRI2DestroyDrawable(psc
->dpy
, xDrawable
);
208 * Make sure server has the same swap interval we do for the new
211 if (pdp
->swapAvailable
)
212 DRI2SwapInterval(psc
->dpy
, xDrawable
, pdraw
->swap_interval
);
220 dri2DrawableGetMSC(__GLXscreenConfigs
*psc
, __GLXDRIdrawable
*pdraw
,
221 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
223 return DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
, ust
, msc
, sbc
);
232 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
233 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
235 return DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
236 remainder
, ust
, msc
, sbc
);
240 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
241 int64_t *msc
, int64_t *sbc
)
243 return DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_sbc
, ust
, msc
,
247 #endif /* X_DRI2WaitMSC */
250 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
252 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
254 XserverRegion region
;
256 /* Check we have the right attachments */
257 if (!priv
->have_back
)
261 xrect
.y
= priv
->height
- y
- height
;
263 xrect
.height
= height
;
267 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
270 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
271 /* should get a fence ID back from here at some point */
272 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
273 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
274 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
276 /* Refresh the fake front (if present) after we just damaged the real
283 dri2WaitX(__GLXDRIdrawable
*pdraw
)
285 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
287 XserverRegion region
;
289 /* Check we have the right attachments */
290 if (!priv
->have_fake_front
)
295 xrect
.width
= priv
->width
;
296 xrect
.height
= priv
->height
;
300 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
303 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
304 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
305 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
306 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
310 dri2WaitGL(__GLXDRIdrawable
* pdraw
)
312 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
314 XserverRegion region
;
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 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
332 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
336 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
338 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
339 __GLXdisplayPrivate
*priv
= __glXInitialize(pdraw
->base
.psc
->dpy
);
340 __GLXDRIdisplayPrivate
*pdp
= (__GLXDRIdisplayPrivate
*)priv
->dri2Display
;
342 /* Old servers don't send invalidate events */
343 if (!pdp
->invalidateAvailable
)
344 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.drawable
);
346 dri2WaitGL(loaderPrivate
);
351 dri2DestroyScreen(__GLXscreenConfigs
* psc
)
353 /* Free the direct rendering per screen data */
354 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
356 psc
->__driScreen
= NULL
;
360 * Process list of buffer received from the server
362 * Processes the list of buffers received in a reply from the server to either
363 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
366 process_buffers(__GLXDRIdrawablePrivate
* pdraw
, DRI2Buffer
* buffers
,
371 pdraw
->bufferCount
= count
;
372 pdraw
->have_fake_front
= 0;
373 pdraw
->have_back
= 0;
375 /* This assumes the DRI2 buffer attachment tokens matches the
376 * __DRIbuffer tokens. */
377 for (i
= 0; i
< count
; i
++) {
378 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
379 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
380 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
381 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
382 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
383 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
384 pdraw
->have_fake_front
= 1;
385 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
386 pdraw
->have_back
= 1;
392 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
395 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
396 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
397 __GLXDRIdisplayPrivate
*pdp
=
398 (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;
403 (*pdraw
->psc
->f
->flush
)(pdraw
->driDrawable
);
406 /* Old servers don't send invalidate events */
407 if (!pdp
->invalidateAvailable
)
408 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->drawable
);
410 /* Old servers can't handle swapbuffers */
411 if (!pdp
->swapAvailable
) {
412 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
416 #ifdef X_DRI2SwapBuffers
417 DRI2SwapBuffers(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
425 dri2GetBuffers(__DRIdrawable
* driDrawable
,
426 int *width
, int *height
,
427 unsigned int *attachments
, int count
,
428 int *out_count
, void *loaderPrivate
)
430 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
433 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
434 width
, height
, attachments
, count
, out_count
);
438 pdraw
->width
= *width
;
439 pdraw
->height
= *height
;
440 process_buffers(pdraw
, buffers
, *out_count
);
444 return pdraw
->buffers
;
448 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
449 int *width
, int *height
,
450 unsigned int *attachments
, int count
,
451 int *out_count
, void *loaderPrivate
)
453 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
456 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
457 pdraw
->base
.xDrawable
,
458 width
, height
, attachments
,
463 pdraw
->width
= *width
;
464 pdraw
->height
= *height
;
465 process_buffers(pdraw
, buffers
, *out_count
);
469 return pdraw
->buffers
;
472 #ifdef X_DRI2SwapInterval
475 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
477 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
479 DRI2SwapInterval(priv
->base
.psc
->dpy
, pdraw
->xDrawable
, interval
);
480 priv
->swap_interval
= interval
;
484 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
486 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
488 return priv
->swap_interval
;
491 #endif /* X_DRI2SwapInterval */
493 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
494 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
496 dri2FlushFrontBuffer
,
497 dri2GetBuffersWithFormat
,
500 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
501 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
503 dri2FlushFrontBuffer
,
507 static const __DRIextension
*loader_extensions
[] = {
508 &dri2LoaderExtension
.base
,
509 &systemTimeExtension
.base
,
513 static const __DRIextension
*loader_extensions_old
[] = {
514 &dri2LoaderExtension_old
.base
,
515 &systemTimeExtension
.base
,
520 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
522 __GLXDRIdrawable
*pdraw
= GetGLXDRIDrawable(dpy
, drawable
, NULL
);
524 #if __DRI2_FLUSH_VERSION >= 3
525 if (pdraw
&& pdraw
->psc
->f
)
526 pdraw
->psc
->f
->invalidate(pdraw
->driDrawable
);
530 static __GLXDRIscreen
*
531 dri2CreateScreen(__GLXscreenConfigs
* psc
, int screen
,
532 __GLXdisplayPrivate
* priv
)
534 const __DRIconfig
**driver_configs
;
535 const __DRIextension
**extensions
;
536 const __GLXDRIdisplayPrivate
*const pdp
= (__GLXDRIdisplayPrivate
*)
539 char *driverName
, *deviceName
;
543 psp
= Xmalloc(sizeof *psp
);
547 if (!DRI2Connect(psc
->dpy
, RootWindow(psc
->dpy
, screen
),
548 &driverName
, &deviceName
)) {
553 psc
->driver
= driOpenDriver(driverName
);
554 if (psc
->driver
== NULL
) {
555 ErrorMessageF("driver pointer missing\n");
559 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
560 if (extensions
== NULL
) {
561 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
565 for (i
= 0; extensions
[i
]; i
++) {
566 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
567 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
568 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
569 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
572 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
573 ErrorMessageF("core dri or dri2 extension not found\n");
577 psc
->fd
= open(deviceName
, O_RDWR
);
579 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
583 if (drmGetMagic(psc
->fd
, &magic
)) {
584 ErrorMessageF("failed to get magic\n");
588 if (!DRI2Authenticate(psc
->dpy
, RootWindow(psc
->dpy
, screen
), magic
)) {
589 ErrorMessageF("failed to authenticate magic %d\n", magic
);
593 /* If the server does not support the protocol for
594 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
597 psc
->dri2
->createNewScreen(screen
, psc
->fd
, ((pdp
->driMinor
< 1)
598 ? loader_extensions_old
599 : loader_extensions
),
600 &driver_configs
, psc
);
602 if (psc
->__driScreen
== NULL
) {
603 ErrorMessageF("failed to create dri screen\n");
607 driBindCommonExtensions(psc
);
608 dri2BindExtensions(psc
);
610 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
611 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
613 psc
->driver_configs
= driver_configs
;
615 psp
->destroyScreen
= dri2DestroyScreen
;
616 psp
->createContext
= dri2CreateContext
;
617 psp
->createDrawable
= dri2CreateDrawable
;
618 psp
->swapBuffers
= dri2SwapBuffers
;
619 psp
->waitGL
= dri2WaitGL
;
620 psp
->waitX
= dri2WaitX
;
621 psp
->getDrawableMSC
= NULL
;
622 psp
->waitForMSC
= NULL
;
623 psp
->waitForSBC
= NULL
;
624 psp
->setSwapInterval
= NULL
;
625 psp
->getSwapInterval
= NULL
;
627 if (pdp
->driMinor
>= 2) {
629 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
632 psp
->waitForMSC
= dri2WaitForMSC
;
633 psp
->waitForSBC
= dri2WaitForSBC
;
635 #ifdef X_DRI2SwapInterval
636 psp
->setSwapInterval
= dri2SetSwapInterval
;
637 psp
->getSwapInterval
= dri2GetSwapInterval
;
639 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
640 __glXEnableDirectExtension(psc
, "GLX_OML_sync_control");
644 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
646 psp
->copySubBuffer
= dri2CopySubBuffer
;
647 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer");
659 /* FIXME: clean up here */
664 /* Called from __glXFreeDisplayPrivate.
667 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
673 * Allocate, initialize and return a __DRIdisplayPrivate object.
674 * This is called from __glXInitialize() when we are given a new
677 _X_HIDDEN __GLXDRIdisplay
*
678 dri2CreateDisplay(Display
* dpy
)
680 __GLXDRIdisplayPrivate
*pdp
;
681 int eventBase
, errorBase
;
683 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
686 pdp
= Xmalloc(sizeof *pdp
);
690 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
696 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
697 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
699 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
700 pdp
->base
.createScreen
= dri2CreateScreen
;
705 #endif /* GLX_DIRECT_RENDERING */