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 "glcontextmodes.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
72 struct __GLXDRIcontextPrivateRec
75 __DRIcontext
*driContext
;
76 __GLXscreenConfigs
*psc
;
79 struct __GLXDRIdrawablePrivateRec
81 __GLXDRIdrawable base
;
82 __DRIbuffer buffers
[5];
90 static void dri2WaitX(__GLXDRIdrawable
* pdraw
);
93 dri2DestroyContext(__GLXDRIcontext
* context
,
94 __GLXscreenConfigs
* psc
, Display
* dpy
)
96 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
97 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
99 (*core
->destroyContext
) (pcp
->driContext
);
105 dri2BindContext(__GLXDRIcontext
* context
,
106 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
108 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
109 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
111 return (*core
->bindContext
) (pcp
->driContext
,
112 draw
->driDrawable
, read
->driDrawable
);
116 dri2UnbindContext(__GLXDRIcontext
* context
)
118 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
119 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
121 (*core
->unbindContext
) (pcp
->driContext
);
124 static __GLXDRIcontext
*
125 dri2CreateContext(__GLXscreenConfigs
* psc
,
126 const __GLcontextModes
* mode
,
127 GLXContext gc
, GLXContext shareList
, int renderType
)
129 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
130 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
131 __DRIcontext
*shared
= NULL
;
134 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
135 shared
= pcp_shared
->driContext
;
138 pcp
= Xmalloc(sizeof *pcp
);
144 (*psc
->dri2
->createNewContext
) (psc
->__driScreen
,
145 config
->driConfig
, shared
, pcp
);
146 gc
->__driContext
= pcp
->driContext
;
148 if (pcp
->driContext
== NULL
) {
153 pcp
->base
.destroyContext
= dri2DestroyContext
;
154 pcp
->base
.bindContext
= dri2BindContext
;
155 pcp
->base
.unbindContext
= dri2UnbindContext
;
161 dri2DestroyDrawable(__GLXDRIdrawable
* pdraw
)
163 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
165 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
166 DRI2DestroyDrawable(pdraw
->psc
->dpy
, pdraw
->xDrawable
);
170 static __GLXDRIdrawable
*
171 dri2CreateDrawable(__GLXscreenConfigs
* psc
,
173 GLXDrawable drawable
, const __GLcontextModes
* modes
)
175 __GLXDRIdrawablePrivate
*pdraw
;
176 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
178 pdraw
= Xmalloc(sizeof(*pdraw
));
182 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
183 pdraw
->base
.xDrawable
= xDrawable
;
184 pdraw
->base
.drawable
= drawable
;
185 pdraw
->base
.psc
= psc
;
186 pdraw
->bufferCount
= 0;
188 DRI2CreateDrawable(psc
->dpy
, xDrawable
);
190 /* Create a new drawable */
191 pdraw
->base
.driDrawable
=
192 (*psc
->dri2
->createNewDrawable
) (psc
->__driScreen
,
193 config
->driConfig
, pdraw
);
195 if (!pdraw
->base
.driDrawable
) {
196 DRI2DestroyDrawable(psc
->dpy
, xDrawable
);
205 dri2DrawableGetMSC(__GLXscreenConfigs
*psc
, __GLXDRIdrawable
*pdraw
,
206 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
208 return DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
, ust
, msc
, sbc
);
212 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
213 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
215 return DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
216 remainder
, ust
, msc
, sbc
);
220 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
221 int64_t *msc
, int64_t *sbc
)
223 return DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_sbc
, ust
, msc
,
228 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
230 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
232 XserverRegion region
;
234 /* Check we have the right attachments */
235 if (!priv
->have_back
)
239 xrect
.y
= priv
->height
- y
- height
;
241 xrect
.height
= height
;
245 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
248 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
249 /* should get a fence ID back from here at some point */
250 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
251 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
252 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
254 /* Refresh the fake front (if present) after we just damaged the real
261 dri2WaitX(__GLXDRIdrawable
*pdraw
)
263 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
265 XserverRegion region
;
267 /* Check we have the right attachments */
268 if (!priv
->have_fake_front
)
273 xrect
.width
= priv
->width
;
274 xrect
.height
= priv
->height
;
278 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
281 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
282 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
283 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
284 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
288 dri2WaitGL(__GLXDRIdrawable
* pdraw
)
290 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
292 XserverRegion region
;
294 if (!priv
->have_fake_front
)
299 xrect
.width
= priv
->width
;
300 xrect
.height
= priv
->height
;
304 (*pdraw
->psc
->f
->flush
) (pdraw
->driDrawable
);
307 region
= XFixesCreateRegion(pdraw
->psc
->dpy
, &xrect
, 1);
308 DRI2CopyRegion(pdraw
->psc
->dpy
, pdraw
->xDrawable
, region
,
309 DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
310 XFixesDestroyRegion(pdraw
->psc
->dpy
, region
);
315 dri2FlushFrontBuffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
318 dri2WaitGL((__GLXDRIdrawable
*) loaderPrivate
);
323 dri2DestroyScreen(__GLXscreenConfigs
* psc
)
325 /* Free the direct rendering per screen data */
326 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
328 psc
->__driScreen
= NULL
;
332 * Process list of buffer received from the server
334 * Processes the list of buffers received in a reply from the server to either
335 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
338 process_buffers(__GLXDRIdrawablePrivate
* pdraw
, DRI2Buffer
* buffers
,
343 pdraw
->bufferCount
= count
;
344 pdraw
->have_fake_front
= 0;
345 pdraw
->have_back
= 0;
347 /* This assumes the DRI2 buffer attachment tokens matches the
348 * __DRIbuffer tokens. */
349 for (i
= 0; i
< count
; i
++) {
350 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
351 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
352 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
353 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
354 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
355 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
356 pdraw
->have_fake_front
= 1;
357 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
358 pdraw
->have_back
= 1;
364 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
367 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
368 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
369 __GLXDRIdisplayPrivate
*pdp
=
370 (__GLXDRIdisplayPrivate
*)dpyPriv
->dri2Display
;
375 (*pdraw
->psc
->f
->flush
)(pdraw
->driDrawable
);
378 /* Old servers can't handle swapbuffers */
379 if (!pdp
->swapAvailable
) {
380 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
384 DRI2SwapBuffers(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
387 #if __DRI2_FLUSH_VERSION >= 2
389 (*pdraw
->psc
->f
->flushInvalidate
)(pdraw
->driDrawable
);
396 dri2GetBuffers(__DRIdrawable
* driDrawable
,
397 int *width
, int *height
,
398 unsigned int *attachments
, int count
,
399 int *out_count
, void *loaderPrivate
)
401 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
404 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
405 width
, height
, attachments
, count
, out_count
);
409 pdraw
->width
= *width
;
410 pdraw
->height
= *height
;
411 process_buffers(pdraw
, buffers
, *out_count
);
415 return pdraw
->buffers
;
419 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
420 int *width
, int *height
,
421 unsigned int *attachments
, int count
,
422 int *out_count
, void *loaderPrivate
)
424 __GLXDRIdrawablePrivate
*pdraw
= loaderPrivate
;
427 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
428 pdraw
->base
.xDrawable
,
429 width
, height
, attachments
,
434 pdraw
->width
= *width
;
435 pdraw
->height
= *height
;
436 process_buffers(pdraw
, buffers
, *out_count
);
440 return pdraw
->buffers
;
444 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
446 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
448 DRI2SwapInterval(priv
->base
.psc
->dpy
, pdraw
->xDrawable
, interval
);
449 priv
->swap_interval
= interval
;
453 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
455 __GLXDRIdrawablePrivate
*priv
= (__GLXDRIdrawablePrivate
*) pdraw
;
457 return priv
->swap_interval
;
460 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
461 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
463 dri2FlushFrontBuffer
,
464 dri2GetBuffersWithFormat
,
467 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
468 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
470 dri2FlushFrontBuffer
,
474 static const __DRIextension
*loader_extensions
[] = {
475 &dri2LoaderExtension
.base
,
476 &systemTimeExtension
.base
,
480 static const __DRIextension
*loader_extensions_old
[] = {
481 &dri2LoaderExtension_old
.base
,
482 &systemTimeExtension
.base
,
486 static __GLXDRIscreen
*
487 dri2CreateScreen(__GLXscreenConfigs
* psc
, int screen
,
488 __GLXdisplayPrivate
* priv
)
490 const __DRIconfig
**driver_configs
;
491 const __DRIextension
**extensions
;
492 const __GLXDRIdisplayPrivate
*const pdp
= (__GLXDRIdisplayPrivate
*)
495 char *driverName
, *deviceName
;
499 psp
= Xmalloc(sizeof *psp
);
503 /* Initialize per screen dynamic client GLX extensions */
504 psc
->ext_list_first_time
= GL_TRUE
;
506 if (!DRI2Connect(psc
->dpy
, RootWindow(psc
->dpy
, screen
),
507 &driverName
, &deviceName
))
510 psc
->driver
= driOpenDriver(driverName
);
511 if (psc
->driver
== NULL
) {
512 ErrorMessageF("driver pointer missing\n");
516 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
517 if (extensions
== NULL
) {
518 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
522 for (i
= 0; extensions
[i
]; i
++) {
523 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
524 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
525 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
526 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
529 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
530 ErrorMessageF("core dri or dri2 extension not found\n");
534 psc
->fd
= open(deviceName
, O_RDWR
);
536 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
540 if (drmGetMagic(psc
->fd
, &magic
)) {
541 ErrorMessageF("failed to get magic\n");
545 if (!DRI2Authenticate(psc
->dpy
, RootWindow(psc
->dpy
, screen
), magic
)) {
546 ErrorMessageF("failed to authenticate magic %d\n", magic
);
550 /* If the server does not support the protocol for
551 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
554 psc
->dri2
->createNewScreen(screen
, psc
->fd
, ((pdp
->driMinor
< 1)
555 ? loader_extensions_old
556 : loader_extensions
),
557 &driver_configs
, psc
);
559 if (psc
->__driScreen
== NULL
) {
560 ErrorMessageF("failed to create dri screen\n");
564 driBindCommonExtensions(psc
);
565 dri2BindExtensions(psc
);
567 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
568 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
570 psc
->driver_configs
= driver_configs
;
572 psp
->destroyScreen
= dri2DestroyScreen
;
573 psp
->createContext
= dri2CreateContext
;
574 psp
->createDrawable
= dri2CreateDrawable
;
575 psp
->swapBuffers
= dri2SwapBuffers
;
576 psp
->waitGL
= dri2WaitGL
;
577 psp
->waitX
= dri2WaitX
;
578 if (pdp
->driMinor
>= 2) {
579 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
580 psp
->waitForMSC
= dri2WaitForMSC
;
581 psp
->waitForSBC
= dri2WaitForSBC
;
582 psp
->setSwapInterval
= dri2SetSwapInterval
;
583 psp
->getSwapInterval
= dri2GetSwapInterval
;
585 psp
->getDrawableMSC
= NULL
;
586 psp
->waitForMSC
= NULL
;
587 psp
->waitForSBC
= NULL
;
588 psp
->setSwapInterval
= NULL
;
589 psp
->getSwapInterval
= NULL
;
592 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
594 psp
->copySubBuffer
= dri2CopySubBuffer
;
595 __glXEnableDirectExtension(psc
, "GLX_MESA_copy_sub_buffer");
606 /* FIXME: clean up here */
611 /* Called from __glXFreeDisplayPrivate.
614 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
620 * Allocate, initialize and return a __DRIdisplayPrivate object.
621 * This is called from __glXInitialize() when we are given a new
624 _X_HIDDEN __GLXDRIdisplay
*
625 dri2CreateDisplay(Display
* dpy
)
627 __GLXDRIdisplayPrivate
*pdp
;
628 int eventBase
, errorBase
;
630 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
633 pdp
= Xmalloc(sizeof *pdp
);
637 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
643 pdp
->swapAvailable
= 0;
644 if (pdp
->driMinor
>= 2)
645 pdp
->swapAvailable
= 1;
647 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
648 pdp
->base
.createScreen
= dri2CreateScreen
;
653 #endif /* GLX_DIRECT_RENDERING */