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
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 #ifdef X_DRI2SwapBuffers
385 DRI2SwapBuffers(pdraw->psc->dpy, pdraw->xDrawable, target_msc, divisor,
389 #if __DRI2_FLUSH_VERSION >= 2
391 (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
398 dri2GetBuffers(__DRIdrawable * driDrawable,
399 int *width, int *height,
400 unsigned int *attachments, int count,
401 int *out_count, void *loaderPrivate)
403 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
406 buffers = DRI2GetBuffers(pdraw->base.psc->dpy, pdraw->base.xDrawable,
407 width, height, attachments, count, out_count);
411 pdraw->width = *width;
412 pdraw->height = *height;
413 process_buffers(pdraw, buffers, *out_count);
417 return pdraw->buffers;
421 dri2GetBuffersWithFormat(__DRIdrawable * driDrawable,
422 int *width, int *height,
423 unsigned int *attachments, int count,
424 int *out_count, void *loaderPrivate)
426 __GLXDRIdrawablePrivate *pdraw = loaderPrivate;
429 buffers = DRI2GetBuffersWithFormat(pdraw->base.psc->dpy,
430 pdraw->base.xDrawable,
431 width, height, attachments,
436 pdraw->width = *width;
437 pdraw->height = *height;
438 process_buffers(pdraw, buffers, *out_count);
442 return pdraw->buffers;
446 dri2SetSwapInterval(__GLXDRIdrawable *pdraw, int interval)
448 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
450 DRI2SwapInterval(priv->base.psc->dpy, pdraw->xDrawable, interval);
451 priv->swap_interval = interval;
455 dri2GetSwapInterval(__GLXDRIdrawable *pdraw)
457 __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
459 return priv->swap_interval;
462 static const __DRIdri2LoaderExtension dri2LoaderExtension = {
463 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
465 dri2FlushFrontBuffer,
466 dri2GetBuffersWithFormat,
469 static const __DRIdri2LoaderExtension dri2LoaderExtension_old = {
470 {__DRI_DRI2_LOADER, __DRI_DRI2_LOADER_VERSION},
472 dri2FlushFrontBuffer,
476 static const __DRIextension *loader_extensions[] = {
477 &dri2LoaderExtension.base,
478 &systemTimeExtension.base,
482 static const __DRIextension *loader_extensions_old[] = {
483 &dri2LoaderExtension_old.base,
484 &systemTimeExtension.base,
488 static __GLXDRIscreen *
489 dri2CreateScreen(__GLXscreenConfigs * psc, int screen,
490 __GLXdisplayPrivate * priv)
492 const __DRIconfig **driver_configs;
493 const __DRIextension **extensions;
494 const __GLXDRIdisplayPrivate *const pdp = (__GLXDRIdisplayPrivate *)
497 char *driverName, *deviceName;
501 psp = Xmalloc(sizeof *psp);
505 /* Initialize per screen dynamic client GLX extensions */
506 psc->ext_list_first_time = GL_TRUE;
508 if (!DRI2Connect(psc->dpy, RootWindow(psc->dpy, screen),
509 &driverName, &deviceName)) {
514 psc->driver = driOpenDriver(driverName);
515 if (psc->driver == NULL) {
516 ErrorMessageF("driver pointer missing\n");
520 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
521 if (extensions == NULL) {
522 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
526 for (i = 0; extensions[i]; i++) {
527 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
528 psc->core = (__DRIcoreExtension *) extensions[i];
529 if (strcmp(extensions[i]->name, __DRI_DRI2) == 0)
530 psc->dri2 = (__DRIdri2Extension *) extensions[i];
533 if (psc->core == NULL || psc->dri2 == NULL) {
534 ErrorMessageF("core dri or dri2 extension not found\n");
538 psc->fd = open(deviceName, O_RDWR);
540 ErrorMessageF("failed to open drm device: %s\n", strerror(errno));
544 if (drmGetMagic(psc->fd, &magic)) {
545 ErrorMessageF("failed to get magic\n");
549 if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic)) {
550 ErrorMessageF("failed to authenticate magic %d\n", magic);
554 /* If the server does not support the protocol for
555 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
558 psc->dri2->createNewScreen(screen, psc->fd, ((pdp->driMinor < 1)
559 ? loader_extensions_old
560 : loader_extensions),
561 &driver_configs, psc);
563 if (psc->__driScreen == NULL) {
564 ErrorMessageF("failed to create dri screen\n");
568 driBindCommonExtensions(psc);
569 dri2BindExtensions(psc);
571 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
572 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
574 psc->driver_configs = driver_configs;
576 psp->destroyScreen = dri2DestroyScreen;
577 psp->createContext = dri2CreateContext;
578 psp->createDrawable = dri2CreateDrawable;
579 psp->swapBuffers = dri2SwapBuffers;
580 psp->waitGL = dri2WaitGL;
581 psp->waitX = dri2WaitX;
582 psp->getDrawableMSC = NULL;
583 psp->waitForMSC = NULL;
584 psp->waitForSBC = NULL;
585 psp->setSwapInterval = NULL;
586 psp->getSwapInterval = NULL;
588 if (pdp->driMinor >= 2) {
590 psp->getDrawableMSC = dri2DrawableGetMSC;
593 psp->waitForMSC = dri2WaitForMSC;
594 psp->waitForSBC = dri2WaitForSBC;
596 #ifdef X_DRI2SwapInterval
597 psp->setSwapInterval = dri2SetSwapInterval;
598 psp->getSwapInterval = dri2GetSwapInterval;
602 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
604 psp->copySubBuffer = dri2CopySubBuffer;
605 __glXEnableDirectExtension(psc, "GLX_MESA_copy_sub_buffer");
617 /* FIXME: clean up here */
622 /* Called from __glXFreeDisplayPrivate.
625 dri2DestroyDisplay(__GLXDRIdisplay * dpy)
631 * Allocate, initialize and return a __DRIdisplayPrivate object.
632 * This is called from __glXInitialize() when we are given a new
635 _X_HIDDEN __GLXDRIdisplay *
636 dri2CreateDisplay(Display * dpy)
638 __GLXDRIdisplayPrivate *pdp;
639 int eventBase, errorBase;
641 if (!DRI2QueryExtension(dpy, &eventBase, &errorBase))
644 pdp = Xmalloc(sizeof *pdp);
648 if (!DRI2QueryVersion(dpy, &pdp->driMajor, &pdp->driMinor)) {
654 pdp->swapAvailable = 0;
655 #ifdef X_DRI2SwapBuffers
656 if (pdp->driMinor >= 2)
657 pdp->swapAvailable = 1;
660 pdp->base.destroyDisplay = dri2DestroyDisplay;
661 pdp->base.createScreen = dri2CreateScreen;
666 #endif /* GLX_DIRECT_RENDERING */