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 __GLXscreenConfigs 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
;
98 __GLXDRIcontext dri_vtable
;
99 __DRIcontext
*driContext
;
104 __GLXDRIdrawable base
;
105 __DRIdrawable
*driDrawable
;
106 __DRIbuffer buffers
[5];
114 static const struct glx_context_vtable dri2_context_vtable
;
117 dri2_destroy_context(__GLXcontext
*context
)
119 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
120 struct dri2_screen
*psc
= (struct dri2_screen
*) context
->psc
;
122 glx_send_destroy_context(psc
->base
.dpy
, context
->xid
);
124 if (context
->extensions
)
125 XFree((char *) context
->extensions
);
127 GarbageCollectDRIDrawables(context
->psc
);
129 (*psc
->core
->destroyContext
) (pcp
->driContext
);
135 dri2BindContext(__GLXcontext
*context
,
136 __GLXDRIdrawable
*draw
, __GLXDRIdrawable
*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
*pdr
= (struct dri2_drawable
*) draw
;
141 struct dri2_drawable
*prd
= (struct dri2_drawable
*) read
;
143 return (*psc
->core
->bindContext
) (pcp
->driContext
,
144 pdr
->driDrawable
, prd
->driDrawable
);
148 dri2UnbindContext(__GLXcontext
*context
)
150 struct dri2_context
*pcp
= (struct dri2_context
*) context
;
151 struct dri2_screen
*psc
= (struct dri2_screen
*) pcp
->base
.psc
;
153 (*psc
->core
->unbindContext
) (pcp
->driContext
);
156 static __GLXcontext
*
157 dri2_create_context(__GLXscreenConfigs
*base
,
158 const __GLcontextModes
* mode
,
159 GLXContext shareList
, int renderType
)
161 struct dri2_context
*pcp
, *pcp_shared
;
162 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
163 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
164 __DRIcontext
*shared
= NULL
;
167 pcp_shared
= (struct dri2_context
*) shareList
->driContext
;
168 shared
= pcp_shared
->driContext
;
171 pcp
= Xmalloc(sizeof *pcp
);
175 memset(pcp
, 0, sizeof *pcp
);
176 if (!glx_context_init(&pcp
->base
, &psc
->base
, mode
)) {
182 (*psc
->dri2
->createNewContext
) (psc
->driScreen
,
183 config
->driConfig
, shared
, pcp
);
185 if (pcp
->driContext
== NULL
) {
190 pcp
->base
.vtable
= &dri2_context_vtable
;
191 pcp
->base
.driContext
= &pcp
->dri_vtable
;
192 pcp
->dri_vtable
.bindContext
= dri2BindContext
;
193 pcp
->dri_vtable
.unbindContext
= dri2UnbindContext
;
199 dri2DestroyDrawable(__GLXDRIdrawable
*base
)
201 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
202 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
203 __GLXdisplayPrivate
*dpyPriv
= psc
->base
.display
;
204 struct dri2_display
*pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;
206 __glxHashDelete(pdp
->dri2Hash
, pdraw
->base
.xDrawable
);
207 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
208 DRI2DestroyDrawable(psc
->base
.dpy
, pdraw
->base
.xDrawable
);
212 static __GLXDRIdrawable
*
213 dri2CreateDrawable(__GLXscreenConfigs
*base
, XID xDrawable
,
214 GLXDrawable drawable
, const __GLcontextModes
* modes
)
216 struct dri2_drawable
*pdraw
;
217 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
218 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
219 __GLXdisplayPrivate
*dpyPriv
;
220 struct dri2_display
*pdp
;
221 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
223 pdraw
= Xmalloc(sizeof(*pdraw
));
227 memset(pdraw
, 0, sizeof *pdraw
);
228 pdraw
->base
.destroyDrawable
= dri2DestroyDrawable
;
229 pdraw
->base
.xDrawable
= xDrawable
;
230 pdraw
->base
.drawable
= drawable
;
231 pdraw
->base
.psc
= &psc
->base
;
232 pdraw
->bufferCount
= 0;
233 pdraw
->swap_interval
= 1; /* default may be overridden below */
234 pdraw
->have_back
= 0;
237 psc
->config
->configQueryi(psc
->driScreen
,
238 "vblank_mode", &vblank_mode
);
240 switch (vblank_mode
) {
241 case DRI_CONF_VBLANK_NEVER
:
242 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
243 pdraw
->swap_interval
= 0;
245 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
246 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
248 pdraw
->swap_interval
= 1;
252 DRI2CreateDrawable(psc
->base
.dpy
, xDrawable
);
254 dpyPriv
= __glXInitialize(psc
->base
.dpy
);
255 pdp
= (struct dri2_display
*)dpyPriv
->dri2Display
;;
256 /* Create a new drawable */
258 (*psc
->dri2
->createNewDrawable
) (psc
->driScreen
,
259 config
->driConfig
, pdraw
);
261 if (!pdraw
->driDrawable
) {
262 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
267 if (__glxHashInsert(pdp
->dri2Hash
, xDrawable
, pdraw
)) {
268 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
269 DRI2DestroyDrawable(psc
->base
.dpy
, xDrawable
);
275 #ifdef X_DRI2SwapInterval
277 * Make sure server has the same swap interval we do for the new
280 if (pdp
->swapAvailable
)
281 DRI2SwapInterval(psc
->base
.dpy
, xDrawable
, pdraw
->swap_interval
);
290 dri2DrawableGetMSC(__GLXscreenConfigs
*psc
, __GLXDRIdrawable
*pdraw
,
291 int64_t *ust
, int64_t *msc
, int64_t *sbc
)
293 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
296 ret
= DRI2GetMSC(psc
->dpy
, pdraw
->xDrawable
,
297 &dri2_ust
, &dri2_msc
, &dri2_sbc
);
311 dri2WaitForMSC(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
312 int64_t remainder
, int64_t *ust
, int64_t *msc
, int64_t *sbc
)
314 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
317 ret
= DRI2WaitMSC(pdraw
->psc
->dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
318 remainder
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
327 dri2WaitForSBC(__GLXDRIdrawable
*pdraw
, int64_t target_sbc
, int64_t *ust
,
328 int64_t *msc
, int64_t *sbc
)
330 CARD64 dri2_ust
, dri2_msc
, dri2_sbc
;
333 ret
= DRI2WaitSBC(pdraw
->psc
->dpy
, pdraw
->xDrawable
,
334 target_sbc
, &dri2_ust
, &dri2_msc
, &dri2_sbc
);
342 #endif /* X_DRI2WaitMSC */
345 dri2CopySubBuffer(__GLXDRIdrawable
*pdraw
, int x
, int y
, int width
, int height
)
347 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
348 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
350 XserverRegion region
;
352 /* Check we have the right attachments */
353 if (!priv
->have_back
)
357 xrect
.y
= priv
->height
- y
- height
;
359 xrect
.height
= height
;
363 (*psc
->f
->flush
) (priv
->driDrawable
);
366 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
367 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
368 DRI2BufferFrontLeft
, DRI2BufferBackLeft
);
370 /* Refresh the fake front (if present) after we just damaged the real
373 if (priv
->have_fake_front
)
374 DRI2CopyRegion(psc
->base
.dpy
, pdraw
->xDrawable
, region
,
375 DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
377 XFixesDestroyRegion(psc
->base
.dpy
, region
);
381 dri2_copy_drawable(struct dri2_drawable
*priv
, int dest
, int src
)
384 XserverRegion region
;
385 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
389 xrect
.width
= priv
->width
;
390 xrect
.height
= priv
->height
;
394 (*psc
->f
->flush
) (priv
->driDrawable
);
397 region
= XFixesCreateRegion(psc
->base
.dpy
, &xrect
, 1);
398 DRI2CopyRegion(psc
->base
.dpy
, priv
->base
.xDrawable
, region
, dest
, src
);
399 XFixesDestroyRegion(psc
->base
.dpy
, region
);
404 dri2_wait_x(__GLXcontext
*gc
)
406 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
407 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
409 if (priv
== NULL
|| !priv
->have_fake_front
)
412 dri2_copy_drawable(priv
, DRI2BufferFakeFrontLeft
, DRI2BufferFrontLeft
);
416 dri2_wait_gl(__GLXcontext
*gc
)
418 struct dri2_drawable
*priv
= (struct dri2_drawable
*)
419 GetGLXDRIDrawable(gc
->currentDpy
, gc
->currentDrawable
);
421 if (priv
== NULL
|| !priv
->have_fake_front
)
424 dri2_copy_drawable(priv
, DRI2BufferFrontLeft
, DRI2BufferFakeFrontLeft
);
428 dri2FlushFrontBuffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
430 struct dri2_drawable
*pdraw
= loaderPrivate
;
431 __GLXdisplayPrivate
*priv
= __glXInitialize(pdraw
->base
.psc
->dpy
);
432 struct dri2_display
*pdp
= (struct dri2_display
*)priv
->dri2Display
;
433 GLXContext gc
= __glXGetCurrentContext();
435 /* Old servers don't send invalidate events */
436 if (!pdp
->invalidateAvailable
)
437 dri2InvalidateBuffers(priv
->dpy
, pdraw
->base
.drawable
);
444 dri2DestroyScreen(__GLXscreenConfigs
*base
)
446 struct dri2_screen
*psc
= (struct dri2_screen
*) base
;
448 /* Free the direct rendering per screen data */
449 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
450 driDestroyConfigs(psc
->driver_configs
);
456 * Process list of buffer received from the server
458 * Processes the list of buffers received in a reply from the server to either
459 * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
462 process_buffers(struct dri2_drawable
* pdraw
, DRI2Buffer
* buffers
,
467 pdraw
->bufferCount
= count
;
468 pdraw
->have_fake_front
= 0;
469 pdraw
->have_back
= 0;
471 /* This assumes the DRI2 buffer attachment tokens matches the
472 * __DRIbuffer tokens. */
473 for (i
= 0; i
< count
; i
++) {
474 pdraw
->buffers
[i
].attachment
= buffers
[i
].attachment
;
475 pdraw
->buffers
[i
].name
= buffers
[i
].name
;
476 pdraw
->buffers
[i
].pitch
= buffers
[i
].pitch
;
477 pdraw
->buffers
[i
].cpp
= buffers
[i
].cpp
;
478 pdraw
->buffers
[i
].flags
= buffers
[i
].flags
;
479 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_FAKE_FRONT_LEFT
)
480 pdraw
->have_fake_front
= 1;
481 if (pdraw
->buffers
[i
].attachment
== __DRI_BUFFER_BACK_LEFT
)
482 pdraw
->have_back
= 1;
488 dri2SwapBuffers(__GLXDRIdrawable
*pdraw
, int64_t target_msc
, int64_t divisor
,
491 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
492 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(priv
->base
.psc
->dpy
);
493 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
494 struct dri2_display
*pdp
=
495 (struct dri2_display
*)dpyPriv
->dri2Display
;
500 (*psc
->f
->flush
)(priv
->driDrawable
);
503 /* Old servers don't send invalidate events */
504 if (!pdp
->invalidateAvailable
)
505 dri2InvalidateBuffers(dpyPriv
->dpy
, pdraw
->drawable
);
507 /* Old servers can't handle swapbuffers */
508 if (!pdp
->swapAvailable
) {
509 dri2CopySubBuffer(pdraw
, 0, 0, priv
->width
, priv
->height
);
513 #ifdef X_DRI2SwapBuffers
514 DRI2SwapBuffers(psc
->base
.dpy
, pdraw
->xDrawable
, target_msc
, divisor
,
522 dri2GetBuffers(__DRIdrawable
* driDrawable
,
523 int *width
, int *height
,
524 unsigned int *attachments
, int count
,
525 int *out_count
, void *loaderPrivate
)
527 struct dri2_drawable
*pdraw
= loaderPrivate
;
530 buffers
= DRI2GetBuffers(pdraw
->base
.psc
->dpy
, pdraw
->base
.xDrawable
,
531 width
, height
, attachments
, count
, out_count
);
535 pdraw
->width
= *width
;
536 pdraw
->height
= *height
;
537 process_buffers(pdraw
, buffers
, *out_count
);
541 return pdraw
->buffers
;
545 dri2GetBuffersWithFormat(__DRIdrawable
* driDrawable
,
546 int *width
, int *height
,
547 unsigned int *attachments
, int count
,
548 int *out_count
, void *loaderPrivate
)
550 struct dri2_drawable
*pdraw
= loaderPrivate
;
553 buffers
= DRI2GetBuffersWithFormat(pdraw
->base
.psc
->dpy
,
554 pdraw
->base
.xDrawable
,
555 width
, height
, attachments
,
560 pdraw
->width
= *width
;
561 pdraw
->height
= *height
;
562 process_buffers(pdraw
, buffers
, *out_count
);
566 return pdraw
->buffers
;
569 #ifdef X_DRI2SwapInterval
572 dri2SetSwapInterval(__GLXDRIdrawable
*pdraw
, int interval
)
574 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
575 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
576 struct dri2_screen
*psc
= (struct dri2_screen
*) priv
->base
.psc
;
579 psc
->config
->configQueryi(psc
->driScreen
,
580 "vblank_mode", &vblank_mode
);
582 switch (vblank_mode
) {
583 case DRI_CONF_VBLANK_NEVER
:
584 return GLX_BAD_VALUE
;
585 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
587 return GLX_BAD_VALUE
;
593 DRI2SwapInterval(priv
->base
.psc
->dpy
, priv
->base
.xDrawable
, interval
);
594 priv
->swap_interval
= interval
;
600 dri2GetSwapInterval(__GLXDRIdrawable
*pdraw
)
602 struct dri2_drawable
*priv
= (struct dri2_drawable
*) pdraw
;
604 return priv
->swap_interval
;
607 #endif /* X_DRI2SwapInterval */
609 static const __DRIdri2LoaderExtension dri2LoaderExtension
= {
610 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
612 dri2FlushFrontBuffer
,
613 dri2GetBuffersWithFormat
,
616 static const __DRIdri2LoaderExtension dri2LoaderExtension_old
= {
617 {__DRI_DRI2_LOADER
, __DRI_DRI2_LOADER_VERSION
},
619 dri2FlushFrontBuffer
,
623 #ifdef __DRI_USE_INVALIDATE
624 static const __DRIuseInvalidateExtension dri2UseInvalidate
= {
625 { __DRI_USE_INVALIDATE
, __DRI_USE_INVALIDATE_VERSION
}
630 dri2InvalidateBuffers(Display
*dpy
, XID drawable
)
632 __GLXDRIdrawable
*pdraw
=
633 dri2GetGlxDrawableFromXDrawableId(dpy
, drawable
);
634 struct dri2_screen
*psc
= (struct dri2_screen
*) pdraw
->psc
;
635 struct dri2_drawable
*pdp
= (struct dri2_drawable
*) pdraw
;
637 #if __DRI2_FLUSH_VERSION >= 3
639 psc
->f
->invalidate(pdp
->driDrawable
);
644 dri2_bind_tex_image(Display
* dpy
,
645 GLXDrawable drawable
,
646 int buffer
, const int *attrib_list
)
648 GLXContext gc
= __glXGetCurrentContext();
649 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
650 __GLXdisplayPrivate
*dpyPriv
= __glXInitialize(dpy
);
651 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
652 struct dri2_display
*pdp
=
653 (struct dri2_display
*) dpyPriv
->dri2Display
;
654 struct dri2_screen
*psc
= (struct dri2_screen
*) base
->psc
;
655 struct dri2_context
*pcp
= (struct dri2_context
*) gc
->driContext
;
659 #if __DRI2_FLUSH_VERSION >= 3
660 if (!pdp
->invalidateAvailable
&& psc
->f
)
661 psc
->f
->invalidate(pdraw
->driDrawable
);
664 if (psc
->texBuffer
->base
.version
>= 2 &&
665 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
666 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
667 pdraw
->base
.textureTarget
,
668 pdraw
->base
.textureFormat
,
672 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
673 pdraw
->base
.textureTarget
,
680 dri2_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
684 static const struct glx_context_vtable dri2_context_vtable
= {
685 dri2_destroy_context
,
690 dri2_release_tex_image
,
694 dri2BindExtensions(struct dri2_screen
*psc
, const __DRIextension
**extensions
)
698 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_video_sync");
699 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_swap_control");
700 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_swap_control");
701 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
703 /* FIXME: if DRI2 version supports it... */
704 __glXEnableDirectExtension(&psc
->base
, "INTEL_swap_event");
706 for (i
= 0; extensions
[i
]; i
++) {
707 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
708 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
709 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
712 if ((strcmp(extensions
[i
]->name
, __DRI2_FLUSH
) == 0)) {
713 psc
->f
= (__DRI2flushExtension
*) extensions
[i
];
714 /* internal driver extension, no GL extension exposed */
717 if ((strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0))
718 psc
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
722 static const struct glx_screen_vtable dri2_screen_vtable
= {
726 static __GLXscreenConfigs
*
727 dri2CreateScreen(int screen
, __GLXdisplayPrivate
* priv
)
729 const __DRIconfig
**driver_configs
;
730 const __DRIextension
**extensions
;
731 const struct dri2_display
*const pdp
= (struct dri2_display
*)
733 struct dri2_screen
*psc
;
735 char *driverName
, *deviceName
;
739 psc
= Xmalloc(sizeof *psc
);
743 memset(psc
, 0, sizeof *psc
);
744 if (!glx_screen_init(&psc
->base
, screen
, priv
))
747 if (!DRI2Connect(priv
->dpy
, RootWindow(priv
->dpy
, screen
),
748 &driverName
, &deviceName
)) {
753 psc
->driver
= driOpenDriver(driverName
);
754 if (psc
->driver
== NULL
) {
755 ErrorMessageF("driver pointer missing\n");
759 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
760 if (extensions
== NULL
) {
761 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
765 for (i
= 0; extensions
[i
]; i
++) {
766 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
767 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
768 if (strcmp(extensions
[i
]->name
, __DRI_DRI2
) == 0)
769 psc
->dri2
= (__DRIdri2Extension
*) extensions
[i
];
772 if (psc
->core
== NULL
|| psc
->dri2
== NULL
) {
773 ErrorMessageF("core dri or dri2 extension not found\n");
777 psc
->fd
= open(deviceName
, O_RDWR
);
779 ErrorMessageF("failed to open drm device: %s\n", strerror(errno
));
783 if (drmGetMagic(psc
->fd
, &magic
)) {
784 ErrorMessageF("failed to get magic\n");
788 if (!DRI2Authenticate(priv
->dpy
, RootWindow(priv
->dpy
, screen
), magic
)) {
789 ErrorMessageF("failed to authenticate magic %d\n", magic
);
794 /* If the server does not support the protocol for
795 * DRI2GetBuffersWithFormat, don't supply that interface to the driver.
798 psc
->dri2
->createNewScreen(screen
, psc
->fd
,
799 (const __DRIextension
**)
800 &pdp
->loader_extensions
[0],
801 &driver_configs
, psc
);
803 if (psc
->driScreen
== NULL
) {
804 ErrorMessageF("failed to create dri screen\n");
808 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
809 dri2BindExtensions(psc
, extensions
);
812 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
814 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
816 psc
->driver_configs
= driver_configs
;
818 psc
->base
.vtable
= &dri2_screen_vtable
;
820 psc
->base
.driScreen
= psp
;
821 psp
->destroyScreen
= dri2DestroyScreen
;
822 psp
->createDrawable
= dri2CreateDrawable
;
823 psp
->swapBuffers
= dri2SwapBuffers
;
824 psp
->getDrawableMSC
= NULL
;
825 psp
->waitForMSC
= NULL
;
826 psp
->waitForSBC
= NULL
;
827 psp
->setSwapInterval
= NULL
;
828 psp
->getSwapInterval
= NULL
;
830 if (pdp
->driMinor
>= 2) {
832 psp
->getDrawableMSC
= dri2DrawableGetMSC
;
835 psp
->waitForMSC
= dri2WaitForMSC
;
836 psp
->waitForSBC
= dri2WaitForSBC
;
838 #ifdef X_DRI2SwapInterval
839 psp
->setSwapInterval
= dri2SetSwapInterval
;
840 psp
->getSwapInterval
= dri2GetSwapInterval
;
842 #if defined(X_DRI2GetMSC) && defined(X_DRI2WaitMSC) && defined(X_DRI2SwapInterval)
843 __glXEnableDirectExtension(&psc
->base
, "GLX_OML_sync_control");
847 /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
849 psp
->copySubBuffer
= dri2CopySubBuffer
;
850 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
862 /* FIXME: clean up here */
867 /* Called from __glXFreeDisplayPrivate.
870 dri2DestroyDisplay(__GLXDRIdisplay
* dpy
)
875 _X_HIDDEN __GLXDRIdrawable
*
876 dri2GetGlxDrawableFromXDrawableId(Display
*dpy
, XID id
)
878 __GLXdisplayPrivate
*d
= __glXInitialize(dpy
);
879 struct dri2_display
*pdp
= (struct dri2_display
*) d
->dri2Display
;
880 __GLXDRIdrawable
*pdraw
;
882 if (__glxHashLookup(pdp
->dri2Hash
, id
, (void *) &pdraw
) == 0)
889 * Allocate, initialize and return a __DRIdisplayPrivate object.
890 * This is called from __glXInitialize() when we are given a new
893 _X_HIDDEN __GLXDRIdisplay
*
894 dri2CreateDisplay(Display
* dpy
)
896 struct dri2_display
*pdp
;
897 int eventBase
, errorBase
, i
;
899 if (!DRI2QueryExtension(dpy
, &eventBase
, &errorBase
))
902 pdp
= Xmalloc(sizeof *pdp
);
906 if (!DRI2QueryVersion(dpy
, &pdp
->driMajor
, &pdp
->driMinor
)) {
912 pdp
->swapAvailable
= (pdp
->driMinor
>= 2);
913 pdp
->invalidateAvailable
= (pdp
->driMinor
>= 3);
915 pdp
->base
.destroyDisplay
= dri2DestroyDisplay
;
916 pdp
->base
.createScreen
= dri2CreateScreen
;
919 if (pdp
->driMinor
< 1)
920 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension_old
.base
;
922 pdp
->loader_extensions
[i
++] = &dri2LoaderExtension
.base
;
924 pdp
->loader_extensions
[i
++] = &systemTimeExtension
.base
;
926 #ifdef __DRI_USE_INVALIDATE
927 pdp
->loader_extensions
[i
++] = &dri2UseInvalidate
.base
;
929 pdp
->loader_extensions
[i
++] = NULL
;
931 pdp
->dri2Hash
= __glxHashCreate();
932 if (pdp
->dri2Hash
== NULL
) {
940 #endif /* GLX_DIRECT_RENDERING */