2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
33 * Client-side GLX interface.
36 #include "glxclient.h"
38 #include "glxextensions.h"
41 #ifdef GLX_DIRECT_RENDERING
42 #ifdef GLX_USE_APPLEGL
43 #include "apple_glx_context.h"
44 #include "apple_glx.h"
45 #include "glx_error.h"
49 #include <X11/extensions/xf86vmode.h>
57 #include <X11/Xlib-xcb.h>
62 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
63 static const char __glXGLXClientVersion[] = "1.4";
65 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
68 * Get the __DRIdrawable for the drawable associated with a GLXContext
70 * \param dpy The display associated with \c drawable.
71 * \param drawable GLXDrawable whose __DRIdrawable part is to be retrieved.
72 * \param scrn_num If non-NULL, the drawables screen is stored there
73 * \returns A pointer to the context's __DRIdrawable on success, or NULL if
74 * the drawable is not associated with a direct-rendering context.
76 _X_HIDDEN __GLXDRIdrawable *
77 GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
79 struct glx_display *priv = __glXInitialize(dpy);
80 __GLXDRIdrawable *pdraw;
85 if (__glxHashLookup(priv->drawHash, drawable, (void *) &pdraw) == 0)
95 * Get the GLX per-screen data structure associated with a GLX context.
97 * \param dpy Display for which the GLX per-screen information is to be
99 * \param scrn Screen on \c dpy for which the GLX per-screen information is
101 * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
102 * specify a valid GLX screen, or NULL otherwise.
104 * \todo Should this function validate that \c scrn is within the screen
105 * number range for \c dpy?
108 static struct glx_screen *
109 GetGLXScreenConfigs(Display * dpy, int scrn)
111 struct glx_display *const priv = __glXInitialize(dpy);
115 NULL) ? priv->screens[scrn] : NULL;
120 GetGLXPrivScreenConfig(Display * dpy, int scrn, struct glx_display ** ppriv,
121 struct glx_screen ** ppsc)
123 /* Initialize the extension, if needed . This has the added value
124 * of initializing/allocating the display private
128 return GLX_NO_EXTENSION;
131 *ppriv = __glXInitialize(dpy);
132 if (*ppriv == NULL) {
133 return GLX_NO_EXTENSION;
136 /* Check screen number to see if its valid */
137 if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
138 return GLX_BAD_SCREEN;
141 /* Check to see if the GL is supported on this screen */
142 *ppsc = (*ppriv)->screens[scrn];
143 if ((*ppsc)->configs == NULL) {
144 /* No support for GL on this screen regardless of visual */
145 return GLX_BAD_VISUAL;
153 * Determine if a \c GLXFBConfig supplied by the application is valid.
155 * \param dpy Application supplied \c Display pointer.
156 * \param config Application supplied \c GLXFBConfig.
158 * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
159 * \c struct glx_config structure is returned. Otherwise, \c NULL
162 static struct glx_config *
163 ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
165 struct glx_display *const priv = __glXInitialize(dpy);
166 int num_screens = ScreenCount(dpy);
168 struct glx_config *config;
171 for (i = 0; i < num_screens; i++) {
172 for (config = priv->screens[i]->configs; config != NULL;
173 config = config->next) {
174 if (config == (struct glx_config *) fbconfig) {
185 glx_context_init(struct glx_context *gc,
186 struct glx_screen *psc, struct glx_config *config)
188 gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
189 if (!gc->majorOpcode)
192 gc->screen = psc->scr;
195 gc->isDirect = GL_TRUE;
196 gc->currentContextTag = -1;
203 * Create a new context.
205 * \param renderType For FBConfigs, what is the rendering type?
209 CreateContext(Display *dpy, int generic_id, struct glx_config *config,
210 GLXContext shareList_user, Bool allowDirect,
211 unsigned code, int renderType, int screen)
213 struct glx_context *gc;
214 struct glx_screen *psc;
215 struct glx_context *shareList = (struct glx_context *) shareList_user;
219 psc = GetGLXScreenConfigs(dpy, screen);
223 if (generic_id == None)
227 #ifdef GLX_USE_APPLEGL
228 gc = applegl_create_context(psc, config, shareList, renderType);
230 if (allowDirect && psc->vtable->create_context)
231 gc = psc->vtable->create_context(psc, config, shareList, renderType);
233 gc = indirect_create_context(psc, config, shareList, renderType);
240 case X_GLXCreateContext: {
241 xGLXCreateContextReq *req;
243 /* Send the glXCreateContext request */
244 GetReq(GLXCreateContext, req);
245 req->reqType = gc->majorOpcode;
246 req->glxCode = X_GLXCreateContext;
247 req->context = gc->xid = XAllocID(dpy);
248 req->visual = generic_id;
249 req->screen = screen;
250 req->shareList = shareList ? shareList->xid : None;
251 req->isDirect = gc->isDirect;
255 case X_GLXCreateNewContext: {
256 xGLXCreateNewContextReq *req;
258 /* Send the glXCreateNewContext request */
259 GetReq(GLXCreateNewContext, req);
260 req->reqType = gc->majorOpcode;
261 req->glxCode = X_GLXCreateNewContext;
262 req->context = gc->xid = XAllocID(dpy);
263 req->fbconfig = generic_id;
264 req->screen = screen;
265 req->renderType = renderType;
266 req->shareList = shareList ? shareList->xid : None;
267 req->isDirect = gc->isDirect;
271 case X_GLXvop_CreateContextWithConfigSGIX: {
272 xGLXVendorPrivateWithReplyReq *vpreq;
273 xGLXCreateContextWithConfigSGIXReq *req;
275 /* Send the glXCreateNewContext request */
276 GetReqExtra(GLXVendorPrivateWithReply,
277 sz_xGLXCreateContextWithConfigSGIXReq -
278 sz_xGLXVendorPrivateWithReplyReq, vpreq);
279 req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
280 req->reqType = gc->majorOpcode;
281 req->glxCode = X_GLXVendorPrivateWithReply;
282 req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
283 req->context = gc->xid = XAllocID(dpy);
284 req->fbconfig = generic_id;
285 req->screen = screen;
286 req->renderType = renderType;
287 req->shareList = shareList ? shareList->xid : None;
288 req->isDirect = gc->isDirect;
293 /* What to do here? This case is the sign of an internal error. It
294 * should never be reachable.
302 gc->imported = GL_FALSE;
303 gc->renderType = renderType;
305 return (GLXContext) gc;
309 glXCreateContext(Display * dpy, XVisualInfo * vis,
310 GLXContext shareList, Bool allowDirect)
312 struct glx_config *config = NULL;
315 #if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
316 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, vis->screen);
318 config = glx_config_find_visual(psc->visuals, vis->visualid);
319 if (config == NULL) {
322 error.errorCode = BadValue;
323 error.resourceID = vis->visualid;
324 error.sequenceNumber = dpy->request;
325 error.type = X_Error;
326 error.majorCode = __glXSetupForCommand(dpy);
327 error.minorCode = X_GLXCreateContext;
328 _XError(dpy, &error);
332 renderType = config->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
335 return CreateContext(dpy, vis->visualid, config, shareList, allowDirect,
336 X_GLXCreateContext, renderType, vis->screen);
340 glx_send_destroy_context(Display *dpy, XID xid)
342 CARD8 opcode = __glXSetupForCommand(dpy);
343 xGLXDestroyContextReq *req;
346 GetReq(GLXDestroyContext, req);
347 req->reqType = opcode;
348 req->glxCode = X_GLXDestroyContext;
355 ** Destroy the named context
359 glXDestroyContext(Display * dpy, GLXContext ctx)
361 struct glx_context *gc = (struct glx_context *) ctx;
367 if (gc->currentDpy) {
368 /* This context is bound to some thread. According to the man page,
369 * we should not actually delete the context until it's unbound.
370 * Note that we set gc->xid = None above. In MakeContextCurrent()
371 * we check for that and delete the context there.
374 glx_send_destroy_context(dpy, gc->xid);
381 gc->vtable->destroy(gc);
385 ** Return the major and minor version #s for the GLX extension
388 glXQueryVersion(Display * dpy, int *major, int *minor)
390 struct glx_display *priv;
392 /* Init the extension. This fetches the major and minor version. */
393 priv = __glXInitialize(dpy);
398 *major = priv->majorVersion;
400 *minor = priv->minorVersion;
405 ** Query the existance of the GLX extension
408 glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
410 int major_op, erb, evb;
413 rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
424 ** Put a barrier in the token stream that forces the GL to finish its
425 ** work before X can proceed.
430 struct glx_context *gc = __glXGetCurrentContext();
432 if (gc && gc->vtable->wait_gl)
433 gc->vtable->wait_gl(gc);
437 ** Put a barrier in the token stream that forces X to finish its
438 ** work before GL can proceed.
443 struct glx_context *gc = __glXGetCurrentContext();
445 if (gc && gc->vtable->wait_x)
446 gc->vtable->wait_x(gc);
450 glXUseXFont(Font font, int first, int count, int listBase)
452 struct glx_context *gc = __glXGetCurrentContext();
454 if (gc && gc->vtable->use_x_font)
455 gc->vtable->use_x_font(gc, font, first, count, listBase);
458 /************************************************************************/
461 ** Copy the source context to the destination context using the
465 glXCopyContext(Display * dpy, GLXContext source_user,
466 GLXContext dest_user, unsigned long mask)
468 struct glx_context *source = (struct glx_context *) source_user;
469 struct glx_context *dest = (struct glx_context *) dest_user;
470 #ifdef GLX_USE_APPLEGL
471 struct glx_context *gc = __glXGetCurrentContext();
475 if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
476 mask, &errorcode, &x11error)) {
477 __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
481 xGLXCopyContextReq *req;
482 struct glx_context *gc = __glXGetCurrentContext();
486 opcode = __glXSetupForCommand(dpy);
491 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
493 /* NOT_DONE: This does not work yet */
498 ** If the source is the current context, send its tag so that the context
499 ** can be flushed before the copy.
501 if (source == gc && dpy == gc->currentDpy) {
502 tag = gc->currentContextTag;
508 /* Send the glXCopyContext request */
510 GetReq(GLXCopyContext, req);
511 req->reqType = opcode;
512 req->glxCode = X_GLXCopyContext;
513 req->source = source ? source->xid : None;
514 req->dest = dest ? dest->xid : None;
516 req->contextTag = tag;
519 #endif /* GLX_USE_APPLEGL */
524 * Determine if a context uses direct rendering.
526 * \param dpy Display where the context was created.
527 * \param contextID ID of the context to be tested.
529 * \returns \c GL_TRUE if the context is direct rendering or not.
532 __glXIsDirect(Display * dpy, GLXContextID contextID)
534 #if !defined(USE_XCB)
535 xGLXIsDirectReq *req;
536 xGLXIsDirectReply reply;
540 opcode = __glXSetupForCommand(dpy);
546 xcb_connection_t *c = XGetXCBConnection(dpy);
547 xcb_glx_is_direct_reply_t *reply = xcb_glx_is_direct_reply(c,
552 const Bool is_direct = reply->is_direct ? True : False;
557 /* Send the glXIsDirect request */
559 GetReq(GLXIsDirect, req);
560 req->reqType = opcode;
561 req->glxCode = X_GLXIsDirect;
562 req->context = contextID;
563 _XReply(dpy, (xReply *) & reply, 0, False);
567 return reply.isDirect;
573 * Shouldn't this function \b always return \c GL_FALSE when
574 * \c GLX_DIRECT_RENDERING is not defined? Do we really need to bother with
575 * the GLX protocol here at all?
578 glXIsDirect(Display * dpy, GLXContext gc_user)
580 struct glx_context *gc = (struct glx_context *) gc_user;
585 else if (gc->isDirect) {
588 #ifdef GLX_USE_APPLEGL /* TODO: indirect on darwin */
591 return __glXIsDirect(dpy, gc->xid);
596 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
598 #ifdef GLX_USE_APPLEGL
599 int screen = vis->screen;
600 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
601 const struct glx_config *config;
603 config = glx_config_find_visual(psc->visuals, vis->visualid);
605 if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, config))
610 xGLXCreateGLXPixmapReq *req;
614 opcode = __glXSetupForCommand(dpy);
619 /* Send the glXCreateGLXPixmap request */
621 GetReq(GLXCreateGLXPixmap, req);
622 req->reqType = opcode;
623 req->glxCode = X_GLXCreateGLXPixmap;
624 req->screen = vis->screen;
625 req->visual = vis->visualid;
626 req->pixmap = pixmap;
627 req->glxpixmap = xid = XAllocID(dpy);
631 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
633 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
634 * is actually bound to a context... */
636 struct glx_display *const priv = __glXInitialize(dpy);
637 __GLXDRIdrawable *pdraw;
638 struct glx_screen *psc;
639 struct glx_config *config;
641 psc = priv->screens[vis->screen];
642 if (psc->driScreen == NULL)
644 config = glx_config_find_visual(psc->visuals, vis->visualid);
645 pdraw = psc->driScreen->createDrawable(psc, pixmap, xid, config);
647 fprintf(stderr, "failed to create pixmap\n");
651 if (__glxHashInsert(priv->drawHash, xid, pdraw)) {
652 (*pdraw->destroyDrawable) (pdraw);
653 return None; /* FIXME: Check what we're supposed to do here... */
663 ** Destroy the named pixmap
666 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
668 #ifdef GLX_USE_APPLEGL
669 if(apple_glx_pixmap_destroy(dpy, glxpixmap))
670 __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
672 xGLXDestroyGLXPixmapReq *req;
675 opcode = __glXSetupForCommand(dpy);
680 /* Send the glXDestroyGLXPixmap request */
682 GetReq(GLXDestroyGLXPixmap, req);
683 req->reqType = opcode;
684 req->glxCode = X_GLXDestroyGLXPixmap;
685 req->glxpixmap = glxpixmap;
689 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
691 struct glx_display *const priv = __glXInitialize(dpy);
692 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
695 (*pdraw->destroyDrawable) (pdraw);
696 __glxHashDelete(priv->drawHash, glxpixmap);
700 #endif /* GLX_USE_APPLEGL */
704 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
706 #ifdef GLX_USE_APPLEGL
707 struct glx_context * gc = __glXGetCurrentContext();
708 if(gc && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
709 apple_glx_swap_buffers(gc->driContext);
711 __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
714 struct glx_context *gc;
720 xGLXSwapBuffersReq *req;
723 gc = __glXGetCurrentContext();
725 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
726 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
729 if (gc && drawable == gc->currentDrawable) {
733 (*pdraw->psc->driScreen->swapBuffers)(pdraw, 0, 0, 0);
738 opcode = __glXSetupForCommand(dpy);
744 ** The calling thread may or may not have a current context. If it
745 ** does, send the context tag so the server can do a flush.
747 if ((gc != NULL) && (dpy == gc->currentDpy) &&
748 ((drawable == gc->currentDrawable)
749 || (drawable == gc->currentReadable))) {
750 tag = gc->currentContextTag;
757 c = XGetXCBConnection(dpy);
758 xcb_glx_swap_buffers(c, tag, drawable);
761 /* Send the glXSwapBuffers request */
763 GetReq(GLXSwapBuffers, req);
764 req->reqType = opcode;
765 req->glxCode = X_GLXSwapBuffers;
766 req->drawable = drawable;
767 req->contextTag = tag;
772 #endif /* GLX_USE_APPLEGL */
777 ** Return configuration information for the given display, screen and
778 ** visual combination.
781 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
784 struct glx_display *priv;
785 struct glx_screen *psc;
786 struct glx_config *config;
789 status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
790 if (status == Success) {
791 config = glx_config_find_visual(psc->visuals, vis->visualid);
793 /* Lookup attribute after first finding a match on the visual */
794 if (config != NULL) {
795 return glx_config_get(config, attribute, value_return);
798 status = GLX_BAD_VISUAL;
802 ** If we can't find the config for this visual, this visual is not
803 ** supported by the OpenGL implementation on the server.
805 if ((status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL)) {
806 *value_return = GL_FALSE;
813 /************************************************************************/
816 init_fbconfig_for_chooser(struct glx_config * config,
817 GLboolean fbconfig_style_tags)
819 memset(config, 0, sizeof(struct glx_config));
820 config->visualID = (XID) GLX_DONT_CARE;
821 config->visualType = GLX_DONT_CARE;
823 /* glXChooseFBConfig specifies different defaults for these two than
826 if (fbconfig_style_tags) {
827 config->rgbMode = GL_TRUE;
828 config->doubleBufferMode = GLX_DONT_CARE;
831 config->visualRating = GLX_DONT_CARE;
832 config->transparentPixel = GLX_NONE;
833 config->transparentRed = GLX_DONT_CARE;
834 config->transparentGreen = GLX_DONT_CARE;
835 config->transparentBlue = GLX_DONT_CARE;
836 config->transparentAlpha = GLX_DONT_CARE;
837 config->transparentIndex = GLX_DONT_CARE;
839 config->drawableType = GLX_WINDOW_BIT;
841 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
842 config->xRenderable = GLX_DONT_CARE;
843 config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
845 config->swapMethod = GLX_DONT_CARE;
848 #define MATCH_DONT_CARE( param ) \
850 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
851 && (a-> param != b-> param) ) { \
856 #define MATCH_MINIMUM( param ) \
858 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
859 && (a-> param > b-> param) ) { \
864 #define MATCH_EXACT( param ) \
866 if ( a-> param != b-> param) { \
871 /* Test that all bits from a are contained in b */
872 #define MATCH_MASK(param) \
874 if ((a->param & ~b->param) != 0) \
879 * Determine if two GLXFBConfigs are compatible.
881 * \param a Application specified config to test.
882 * \param b Server specified config to test against \c a.
885 fbconfigs_compatible(const struct glx_config * const a,
886 const struct glx_config * const b)
888 MATCH_DONT_CARE(doubleBufferMode);
889 MATCH_DONT_CARE(visualType);
890 MATCH_DONT_CARE(visualRating);
891 MATCH_DONT_CARE(xRenderable);
892 MATCH_DONT_CARE(fbconfigID);
893 MATCH_DONT_CARE(swapMethod);
895 MATCH_MINIMUM(rgbBits);
896 MATCH_MINIMUM(numAuxBuffers);
897 MATCH_MINIMUM(redBits);
898 MATCH_MINIMUM(greenBits);
899 MATCH_MINIMUM(blueBits);
900 MATCH_MINIMUM(alphaBits);
901 MATCH_MINIMUM(depthBits);
902 MATCH_MINIMUM(stencilBits);
903 MATCH_MINIMUM(accumRedBits);
904 MATCH_MINIMUM(accumGreenBits);
905 MATCH_MINIMUM(accumBlueBits);
906 MATCH_MINIMUM(accumAlphaBits);
907 MATCH_MINIMUM(sampleBuffers);
908 MATCH_MINIMUM(maxPbufferWidth);
909 MATCH_MINIMUM(maxPbufferHeight);
910 MATCH_MINIMUM(maxPbufferPixels);
911 MATCH_MINIMUM(samples);
913 MATCH_DONT_CARE(stereoMode);
916 MATCH_MASK(drawableType);
917 MATCH_MASK(renderType);
919 /* There is a bug in a few of the XFree86 DDX drivers. They contain
920 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
921 * Technically speaking, it is a bug in the DDX driver, but there is
922 * enough of an installed base to work around the problem here. In any
923 * case, 0 is not a valid value of the transparent type, so we'll treat 0
924 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
925 * 0 from the server to be a match to maintain backward compatibility with
926 * the (broken) drivers.
929 if (a->transparentPixel != (int) GLX_DONT_CARE && a->transparentPixel != 0) {
930 if (a->transparentPixel == GLX_NONE) {
931 if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
935 MATCH_EXACT(transparentPixel);
938 switch (a->transparentPixel) {
939 case GLX_TRANSPARENT_RGB:
940 MATCH_DONT_CARE(transparentRed);
941 MATCH_DONT_CARE(transparentGreen);
942 MATCH_DONT_CARE(transparentBlue);
943 MATCH_DONT_CARE(transparentAlpha);
946 case GLX_TRANSPARENT_INDEX:
947 MATCH_DONT_CARE(transparentIndex);
959 /* There's some trickly language in the GLX spec about how this is supposed
960 * to work. Basically, if a given component size is either not specified
961 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
962 * Well, that's really hard to do with the code as-is. This behavior is
963 * closer to correct, but still not technically right.
965 #define PREFER_LARGER_OR_ZERO(comp) \
967 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
968 if ( ((*a)-> comp) == 0 ) { \
971 else if ( ((*b)-> comp) == 0 ) { \
975 return ((*b)-> comp) - ((*a)-> comp) ; \
980 #define PREFER_LARGER(comp) \
982 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
983 return ((*b)-> comp) - ((*a)-> comp) ; \
987 #define PREFER_SMALLER(comp) \
989 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
990 return ((*a)-> comp) - ((*b)-> comp) ; \
995 * Compare two GLXFBConfigs. This function is intended to be used as the
996 * compare function passed in to qsort.
998 * \returns If \c a is a "better" config, according to the specification of
999 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1000 * better, then a number greater than zero is return. If both are
1001 * equal, zero is returned.
1002 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1005 fbconfig_compare(struct glx_config **a, struct glx_config **b)
1007 /* The order of these comparisons must NOT change. It is defined by
1008 * the GLX 1.3 spec and ARB_multisample.
1011 PREFER_SMALLER(visualSelectGroup);
1013 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1014 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1015 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1017 PREFER_SMALLER(visualRating);
1019 /* This isn't quite right. It is supposed to compare the sum of the
1020 * components the user specifically set minimums for.
1022 PREFER_LARGER_OR_ZERO(redBits);
1023 PREFER_LARGER_OR_ZERO(greenBits);
1024 PREFER_LARGER_OR_ZERO(blueBits);
1025 PREFER_LARGER_OR_ZERO(alphaBits);
1027 PREFER_SMALLER(rgbBits);
1029 if (((*a)->doubleBufferMode != (*b)->doubleBufferMode)) {
1030 /* Prefer single-buffer.
1032 return (!(*a)->doubleBufferMode) ? -1 : 1;
1035 PREFER_SMALLER(numAuxBuffers);
1037 PREFER_LARGER_OR_ZERO(depthBits);
1038 PREFER_SMALLER(stencilBits);
1040 /* This isn't quite right. It is supposed to compare the sum of the
1041 * components the user specifically set minimums for.
1043 PREFER_LARGER_OR_ZERO(accumRedBits);
1044 PREFER_LARGER_OR_ZERO(accumGreenBits);
1045 PREFER_LARGER_OR_ZERO(accumBlueBits);
1046 PREFER_LARGER_OR_ZERO(accumAlphaBits);
1048 PREFER_SMALLER(visualType);
1050 /* None of the multisample specs say where this comparison should happen,
1051 * so I put it near the end.
1053 PREFER_SMALLER(sampleBuffers);
1054 PREFER_SMALLER(samples);
1056 /* None of the pbuffer or fbconfig specs say that this comparison needs
1057 * to happen at all, but it seems like it should.
1059 PREFER_LARGER(maxPbufferWidth);
1060 PREFER_LARGER(maxPbufferHeight);
1061 PREFER_LARGER(maxPbufferPixels);
1068 * Selects and sorts a subset of the supplied configs based on the attributes.
1069 * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1070 * and \c glXChooseFBConfigSGIX.
1072 * \param configs Array of pointers to possible configs. The elements of
1073 * this array that do not meet the criteria will be set to
1074 * NULL. The remaining elements will be sorted according to
1075 * the various visual / FBConfig selection rules.
1076 * \param num_configs Number of elements in the \c configs array.
1077 * \param attribList Attributes used select from \c configs. This array is
1078 * terminated by a \c None tag. The array can either take
1079 * the form expected by \c glXChooseVisual (where boolean
1080 * tags do not have a value) or by \c glXChooseFBConfig
1081 * (where every tag has a value).
1082 * \param fbconfig_style_tags Selects whether \c attribList is in
1083 * \c glXChooseVisual style or
1084 * \c glXChooseFBConfig style.
1085 * \returns The number of valid elements left in \c configs.
1087 * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1090 choose_visual(struct glx_config ** configs, int num_configs,
1091 const int *attribList, GLboolean fbconfig_style_tags)
1093 struct glx_config test_config;
1097 /* This is a fairly direct implementation of the selection method
1098 * described by GLX_SGIX_fbconfig. Start by culling out all the
1099 * configs that are not compatible with the selected parameter
1103 init_fbconfig_for_chooser(&test_config, fbconfig_style_tags);
1104 __glXInitializeVisualConfigFromTags(&test_config, 512,
1105 (const INT32 *) attribList,
1106 GL_TRUE, fbconfig_style_tags);
1109 for (i = 0; i < num_configs; i++) {
1110 if (fbconfigs_compatible(&test_config, configs[i])) {
1111 configs[base] = configs[i];
1120 if (base < num_configs) {
1121 (void) memset(&configs[base], 0, sizeof(void *) * (num_configs - base));
1124 /* After the incompatible configs are removed, the resulting
1125 * list is sorted according to the rules set out in the various
1129 qsort(configs, base, sizeof(struct glx_config *),
1130 (int (*)(const void *, const void *)) fbconfig_compare);
1138 ** Return the visual that best matches the template. Return None if no
1139 ** visual matches the template.
1141 _X_EXPORT XVisualInfo *
1142 glXChooseVisual(Display * dpy, int screen, int *attribList)
1144 XVisualInfo *visualList = NULL;
1145 struct glx_display *priv;
1146 struct glx_screen *psc;
1147 struct glx_config test_config;
1148 struct glx_config *config;
1149 struct glx_config *best_config = NULL;
1152 ** Get a list of all visuals, return if list is empty
1154 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1160 ** Build a template from the defaults and the attribute list
1161 ** Free visual list and return if an unexpected token is encountered
1163 init_fbconfig_for_chooser(&test_config, GL_FALSE);
1164 __glXInitializeVisualConfigFromTags(&test_config, 512,
1165 (const INT32 *) attribList,
1169 ** Eliminate visuals that don't meet minimum requirements
1170 ** Compute a score for those that do
1171 ** Remember which visual, if any, got the highest score
1172 ** If no visual is acceptable, return None
1173 ** Otherwise, create an XVisualInfo list with just the selected X visual
1176 for (config = psc->visuals; config != NULL; config = config->next) {
1177 if (fbconfigs_compatible(&test_config, config)
1178 && ((best_config == NULL) ||
1179 (fbconfig_compare (&config, &best_config) < 0))) {
1180 XVisualInfo visualTemplate;
1181 XVisualInfo *newList;
1184 visualTemplate.screen = screen;
1185 visualTemplate.visualid = config->visualID;
1186 newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
1187 &visualTemplate, &i);
1191 visualList = newList;
1192 best_config = config;
1197 #ifdef GLX_USE_APPLEGL
1198 if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
1199 printf("visualid 0x%lx\n", visualList[0].visualid);
1207 _X_EXPORT const char *
1208 glXQueryExtensionsString(Display * dpy, int screen)
1210 struct glx_screen *psc;
1211 struct glx_display *priv;
1213 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1217 if (!psc->effectiveGLXexts) {
1218 if (!psc->serverGLXexts) {
1219 psc->serverGLXexts =
1220 __glXQueryServerString(dpy, priv->majorOpcode, screen,
1224 __glXCalculateUsableExtensions(psc,
1225 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
1226 (psc->driScreen != NULL),
1230 priv->minorVersion);
1233 return psc->effectiveGLXexts;
1236 _X_EXPORT const char *
1237 glXGetClientString(Display * dpy, int name)
1243 return (__glXGLXClientVendorName);
1245 return (__glXGLXClientVersion);
1246 case GLX_EXTENSIONS:
1247 return (__glXGetClientExtensions());
1253 _X_EXPORT const char *
1254 glXQueryServerString(Display * dpy, int screen, int name)
1256 struct glx_screen *psc;
1257 struct glx_display *priv;
1261 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1267 str = &priv->serverGLXvendor;
1270 str = &priv->serverGLXversion;
1272 case GLX_EXTENSIONS:
1273 str = &psc->serverGLXexts;
1280 *str = __glXQueryServerString(dpy, priv->majorOpcode, screen, name);
1287 __glXClientInfo(Display * dpy, int opcode)
1289 char *ext_str = __glXGetClientGLExtensionString();
1290 int size = strlen(ext_str) + 1;
1293 xcb_connection_t *c = XGetXCBConnection(dpy);
1294 xcb_glx_client_info(c,
1295 GLX_MAJOR_VERSION, GLX_MINOR_VERSION, size, ext_str);
1297 xGLXClientInfoReq *req;
1299 /* Send the glXClientInfo request */
1301 GetReq(GLXClientInfo, req);
1302 req->reqType = opcode;
1303 req->glxCode = X_GLXClientInfo;
1304 req->major = GLX_MAJOR_VERSION;
1305 req->minor = GLX_MINOR_VERSION;
1307 req->length += (size + 3) >> 2;
1308 req->numbytes = size;
1309 Data(dpy, ext_str, size);
1313 #endif /* USE_XCB */
1320 ** EXT_import_context
1324 glXGetCurrentDisplay(void)
1326 struct glx_context *gc = __glXGetCurrentContext();
1329 return gc->currentDpy;
1333 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1334 glXGetCurrentDisplay)
1336 #ifndef GLX_USE_APPLEGL
1337 _X_EXPORT GLXContext
1338 glXImportContextEXT(Display *dpy, GLXContextID contextID)
1340 struct glx_display *priv = __glXInitialize(dpy);
1341 struct glx_screen *psc;
1342 xGLXQueryContextReply reply;
1344 struct glx_context *ctx;
1345 int propList[__GLX_MAX_CONTEXT_PROPS * 2], *pProp, nPropListBytes;
1348 struct glx_config *mode;
1350 if (contextID == None || __glXIsDirect(dpy, contextID))
1353 opcode = __glXSetupForCommand(dpy);
1357 /* Send the glXQueryContextInfoEXT request */
1360 if (priv->majorVersion > 1 || priv->minorVersion >= 3) {
1361 xGLXQueryContextReq *req;
1363 GetReq(GLXQueryContext, req);
1365 req->reqType = opcode;
1366 req->glxCode = X_GLXQueryContext;
1367 req->context = contextID;
1370 xGLXVendorPrivateReq *vpreq;
1371 xGLXQueryContextInfoEXTReq *req;
1373 GetReqExtra(GLXVendorPrivate,
1374 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1376 req = (xGLXQueryContextInfoEXTReq *) vpreq;
1377 req->reqType = opcode;
1378 req->glxCode = X_GLXVendorPrivateWithReply;
1379 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1380 req->context = contextID;
1383 _XReply(dpy, (xReply *) & reply, 0, False);
1385 if (reply.n <= __GLX_MAX_CONTEXT_PROPS)
1386 nPropListBytes = reply.n * 2 * sizeof propList[0];
1389 _XRead(dpy, (char *) propList, nPropListBytes);
1393 /* Look up screen first so we can look up visuals/fbconfigs later */
1395 for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1396 if (pProp[0] == GLX_SCREEN)
1397 psc = GetGLXScreenConfigs(dpy, pProp[1]);
1406 for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1408 case GLX_SHARE_CONTEXT_EXT:
1411 case GLX_VISUAL_ID_EXT:
1412 mode = glx_config_find_visual(psc->visuals, pProp[1]);
1414 case GLX_FBCONFIG_ID:
1415 mode = glx_config_find_fbconfig(psc->configs, pProp[1]);
1417 case GLX_RENDER_TYPE:
1418 renderType = pProp[1];
1425 ctx = indirect_create_context(psc, mode, NULL, renderType);
1429 ctx->xid = contextID;
1430 ctx->imported = GL_TRUE;
1431 ctx->share_xid = share;
1433 return (GLXContext) ctx;
1439 glXQueryContext(Display * dpy, GLXContext ctx_user, int attribute, int *value)
1441 struct glx_context *ctx = (struct glx_context *) ctx_user;
1443 switch (attribute) {
1444 case GLX_SHARE_CONTEXT_EXT:
1445 *value = ctx->share_xid;
1447 case GLX_VISUAL_ID_EXT:
1448 *value = ctx->config ? ctx->config->visualID : None;
1451 *value = ctx->screen;
1453 case GLX_FBCONFIG_ID:
1454 *value = ctx->config ? ctx->config->fbconfigID : None;
1456 case GLX_RENDER_TYPE:
1457 *value = ctx->renderType;
1460 return GLX_BAD_ATTRIBUTE;
1466 GLX_ALIAS(int, glXQueryContextInfoEXT,
1467 (Display * dpy, GLXContext ctx, int attribute, int *value),
1468 (dpy, ctx, attribute, value), glXQueryContext)
1470 _X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx_user)
1472 struct glx_context *ctx = (struct glx_context *) ctx_user;
1478 GLX_ALIAS_VOID(glXFreeContextEXT, (Display *dpy, GLXContext ctx), (dpy, ctx),
1481 _X_EXPORT GLXFBConfig *
1482 glXChooseFBConfig(Display * dpy, int screen,
1483 const int *attribList, int *nitems)
1485 struct glx_config **config_list;
1489 config_list = (struct glx_config **)
1490 glXGetFBConfigs(dpy, screen, &list_size);
1492 if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
1493 list_size = choose_visual(config_list, list_size, attribList, GL_TRUE);
1494 if (list_size == 0) {
1500 *nitems = list_size;
1501 return (GLXFBConfig *) config_list;
1505 _X_EXPORT GLXContext
1506 glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
1507 int renderType, GLXContext shareList, Bool allowDirect)
1509 struct glx_config *config = (struct glx_config *) fbconfig;
1511 return CreateContext(dpy, config->fbconfigID, config, shareList,
1512 allowDirect, X_GLXCreateNewContext, renderType,
1517 _X_EXPORT GLXDrawable
1518 glXGetCurrentReadDrawable(void)
1520 struct glx_context *gc = __glXGetCurrentContext();
1522 return gc->currentReadable;
1526 _X_EXPORT GLXFBConfig *
1527 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
1529 struct glx_display *priv = __glXInitialize(dpy);
1530 struct glx_config **config_list = NULL;
1531 struct glx_config *config;
1532 unsigned num_configs = 0;
1536 if (priv && (priv->screens != NULL)
1537 && (screen >= 0) && (screen <= ScreenCount(dpy))
1538 && (priv->screens[screen]->configs != NULL)
1539 && (priv->screens[screen]->configs->fbconfigID
1540 != (int) GLX_DONT_CARE)) {
1542 for (config = priv->screens[screen]->configs; config != NULL;
1543 config = config->next) {
1544 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1549 config_list = Xmalloc(num_configs * sizeof *config_list);
1550 if (config_list != NULL) {
1551 *nelements = num_configs;
1553 for (config = priv->screens[screen]->configs; config != NULL;
1554 config = config->next) {
1555 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1556 config_list[i] = config;
1563 return (GLXFBConfig *) config_list;
1568 glXGetFBConfigAttrib(Display * dpy, GLXFBConfig fbconfig,
1569 int attribute, int *value)
1571 struct glx_config *config = ValidateGLXFBConfig(dpy, fbconfig);
1574 return GLXBadFBConfig;
1576 return glx_config_get(config, attribute, value);
1580 _X_EXPORT XVisualInfo *
1581 glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig fbconfig)
1583 XVisualInfo visualTemplate;
1584 struct glx_config *config = (struct glx_config *) fbconfig;
1588 ** Get a list of all visuals, return if list is empty
1590 visualTemplate.visualid = config->visualID;
1591 return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
1594 #ifndef GLX_USE_APPLEGL
1596 ** GLX_SGI_swap_control
1599 __glXSwapIntervalSGI(int interval)
1601 xGLXVendorPrivateReq *req;
1602 struct glx_context *gc = __glXGetCurrentContext();
1603 struct glx_screen *psc;
1605 CARD32 *interval_ptr;
1609 return GLX_BAD_CONTEXT;
1612 if (interval <= 0) {
1613 return GLX_BAD_VALUE;
1616 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1618 #ifdef GLX_DIRECT_RENDERING
1619 if (gc->isDirect && psc->driScreen && psc->driScreen->setSwapInterval) {
1620 __GLXDRIdrawable *pdraw =
1621 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1622 psc->driScreen->setSwapInterval(pdraw, interval);
1627 dpy = gc->currentDpy;
1628 opcode = __glXSetupForCommand(dpy);
1633 /* Send the glXSwapIntervalSGI request */
1635 GetReqExtra(GLXVendorPrivate, sizeof(CARD32), req);
1636 req->reqType = opcode;
1637 req->glxCode = X_GLXVendorPrivate;
1638 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1639 req->contextTag = gc->currentContextTag;
1641 interval_ptr = (CARD32 *) (req + 1);
1642 *interval_ptr = interval;
1653 ** GLX_MESA_swap_control
1656 __glXSwapIntervalMESA(unsigned int interval)
1658 #ifdef GLX_DIRECT_RENDERING
1659 struct glx_context *gc = __glXGetCurrentContext();
1661 if (gc != NULL && gc->isDirect) {
1662 struct glx_screen *psc;
1664 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1665 if (psc->driScreen && psc->driScreen->setSwapInterval) {
1666 __GLXDRIdrawable *pdraw =
1667 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1668 return psc->driScreen->setSwapInterval(pdraw, interval);
1673 return GLX_BAD_CONTEXT;
1678 __glXGetSwapIntervalMESA(void)
1680 #ifdef GLX_DIRECT_RENDERING
1681 struct glx_context *gc = __glXGetCurrentContext();
1683 if (gc != NULL && gc->isDirect) {
1684 struct glx_screen *psc;
1686 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1687 if (psc->driScreen && psc->driScreen->getSwapInterval) {
1688 __GLXDRIdrawable *pdraw =
1689 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1690 return psc->driScreen->getSwapInterval(pdraw);
1700 ** GLX_SGI_video_sync
1703 __glXGetVideoSyncSGI(unsigned int *count)
1705 int64_t ust, msc, sbc;
1707 struct glx_context *gc = __glXGetCurrentContext();
1708 struct glx_screen *psc;
1709 #ifdef GLX_DIRECT_RENDERING
1710 __GLXDRIdrawable *pdraw;
1714 return GLX_BAD_CONTEXT;
1716 #ifdef GLX_DIRECT_RENDERING
1718 return GLX_BAD_CONTEXT;
1721 psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
1722 #ifdef GLX_DIRECT_RENDERING
1723 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1726 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1727 * FIXME: there should be a GLX encoding for this call. I can find no
1728 * FIXME: documentation for the GLX encoding.
1730 #ifdef GLX_DIRECT_RENDERING
1731 if (psc->driScreen && psc->driScreen->getDrawableMSC) {
1732 ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
1733 *count = (unsigned) msc;
1734 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1738 return GLX_BAD_CONTEXT;
1742 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1744 struct glx_context *gc = __glXGetCurrentContext();
1745 struct glx_screen *psc;
1746 #ifdef GLX_DIRECT_RENDERING
1747 __GLXDRIdrawable *pdraw;
1749 int64_t ust, msc, sbc;
1752 if (divisor <= 0 || remainder < 0)
1753 return GLX_BAD_VALUE;
1756 return GLX_BAD_CONTEXT;
1758 #ifdef GLX_DIRECT_RENDERING
1760 return GLX_BAD_CONTEXT;
1763 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1764 #ifdef GLX_DIRECT_RENDERING
1765 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1768 #ifdef GLX_DIRECT_RENDERING
1769 if (psc->driScreen && psc->driScreen->waitForMSC) {
1770 ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
1772 *count = (unsigned) msc;
1773 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1777 return GLX_BAD_CONTEXT;
1780 #endif /* GLX_USE_APPLEGL */
1783 ** GLX_SGIX_fbconfig
1784 ** Many of these functions are aliased to GLX 1.3 entry points in the
1785 ** GLX_functions table.
1789 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
1790 (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
1791 (dpy, config, attribute, value), glXGetFBConfigAttrib)
1793 _X_EXPORT GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
1794 (Display * dpy, int screen, int *attrib_list,
1795 int *nelements), (dpy, screen, attrib_list, nelements),
1798 _X_EXPORT GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
1799 (Display * dpy, GLXFBConfigSGIX config),
1800 (dpy, config), glXGetVisualFromFBConfig)
1803 glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
1804 GLXFBConfigSGIX fbconfig,
1807 #ifndef GLX_USE_APPLEGL
1808 xGLXVendorPrivateWithReplyReq *vpreq;
1809 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
1810 GLXPixmap xid = None;
1812 struct glx_screen *psc;
1814 struct glx_config *config = (struct glx_config *) fbconfig;
1817 if ((dpy == NULL) || (config == NULL)) {
1820 #ifdef GLX_USE_APPLEGL
1821 if(apple_glx_pixmap_create(dpy, config->screen, pixmap, config))
1826 psc = GetGLXScreenConfigs(dpy, config->screen);
1828 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1829 opcode = __glXSetupForCommand(dpy);
1834 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
1836 GetReqExtra(GLXVendorPrivateWithReply,
1837 sz_xGLXCreateGLXPixmapWithConfigSGIXReq -
1838 sz_xGLXVendorPrivateWithReplyReq, vpreq);
1839 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *) vpreq;
1840 req->reqType = opcode;
1841 req->glxCode = X_GLXVendorPrivateWithReply;
1842 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
1843 req->screen = config->screen;
1844 req->fbconfig = config->fbconfigID;
1845 req->pixmap = pixmap;
1846 req->glxpixmap = xid = XAllocID(dpy);
1855 _X_EXPORT GLXContext
1856 glXCreateContextWithConfigSGIX(Display * dpy,
1857 GLXFBConfigSGIX fbconfig, int renderType,
1858 GLXContext shareList, Bool allowDirect)
1860 GLXContext gc = NULL;
1861 struct glx_config *config = (struct glx_config *) fbconfig;
1862 struct glx_screen *psc;
1865 if ((dpy == NULL) || (config == NULL)) {
1869 psc = GetGLXScreenConfigs(dpy, config->screen);
1871 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1872 gc = CreateContext(dpy, config->fbconfigID, config, shareList,
1874 X_GLXvop_CreateContextWithConfigSGIX, renderType,
1882 _X_EXPORT GLXFBConfigSGIX
1883 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
1885 struct glx_display *priv;
1886 struct glx_screen *psc = NULL;
1888 if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) == Success)
1889 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
1890 && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
1891 return (GLXFBConfigSGIX) glx_config_find_visual(psc->configs,
1898 #ifndef GLX_USE_APPLEGL
1900 ** GLX_SGIX_swap_group
1903 __glXJoinSwapGroupSGIX(Display * dpy, GLXDrawable drawable,
1913 ** GLX_SGIX_swap_barrier
1916 __glXBindSwapBarrierSGIX(Display * dpy, GLXDrawable drawable, int barrier)
1924 __glXQueryMaxSwapBarriersSGIX(Display * dpy, int screen, int *max)
1934 ** GLX_OML_sync_control
1937 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
1938 int64_t * ust, int64_t * msc, int64_t * sbc)
1940 struct glx_display * const priv = __glXInitialize(dpy);
1942 #ifdef GLX_DIRECT_RENDERING
1943 __GLXDRIdrawable *pdraw;
1945 struct glx_screen *psc;
1950 #ifdef GLX_DIRECT_RENDERING
1951 pdraw = GetGLXDRIDrawable(dpy, drawable);
1952 psc = pdraw ? pdraw->psc : NULL;
1953 if (pdraw && psc->driScreen->getDrawableMSC) {
1954 ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
1962 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
1964 __glxGetMscRate(__GLXDRIdrawable *glxDraw,
1965 int32_t * numerator, int32_t * denominator)
1968 struct glx_screen *psc;
1969 XF86VidModeModeLine mode_line;
1974 if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
1975 XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
1976 unsigned n = dot_clock * 1000;
1977 unsigned d = mode_line.vtotal * mode_line.htotal;
1979 # define V_INTERLACE 0x010
1980 # define V_DBLSCAN 0x020
1982 if (mode_line.flags & V_INTERLACE)
1984 else if (mode_line.flags & V_DBLSCAN)
1987 /* The OML_sync_control spec requires that if the refresh rate is a
1988 * whole number, that the returned numerator be equal to the refresh
1989 * rate and the denominator be 1.
1997 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
1999 /* This is a poor man's way to reduce a fraction. It's far from
2000 * perfect, but it will work well enough for this situation.
2003 for (i = 0; f[i] != 0; i++) {
2004 while (n % f[i] == 0 && d % f[i] == 0) {
2024 * Determine the refresh rate of the specified drawable and display.
2026 * \param dpy Display whose refresh rate is to be determined.
2027 * \param drawable Drawable whose refresh rate is to be determined.
2028 * \param numerator Numerator of the refresh rate.
2029 * \param demoninator Denominator of the refresh rate.
2030 * \return If the refresh rate for the specified display and drawable could
2031 * be calculated, True is returned. Otherwise False is returned.
2033 * \note This function is implemented entirely client-side. A lot of other
2034 * functionality is required to export GLX_OML_sync_control, so on
2035 * XFree86 this function can be called for direct-rendering contexts
2036 * when GLX_OML_sync_control appears in the client extension string.
2040 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
2041 int32_t * numerator, int32_t * denominator)
2043 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2044 __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
2049 return __glxGetMscRate(draw, numerator, denominator);
2061 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
2062 int64_t target_msc, int64_t divisor, int64_t remainder)
2064 struct glx_context *gc = __glXGetCurrentContext();
2065 #ifdef GLX_DIRECT_RENDERING
2066 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2067 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2070 if (!gc) /* no GLX for this */
2073 #ifdef GLX_DIRECT_RENDERING
2074 if (!pdraw || !gc->isDirect)
2078 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2079 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2080 * of -1 if the function failed because of errors detected in the input
2083 if (divisor < 0 || remainder < 0 || target_msc < 0)
2085 if (divisor > 0 && remainder >= divisor)
2088 if (target_msc == 0 && divisor == 0 && remainder == 0)
2091 #ifdef GLX_DIRECT_RENDERING
2092 if (psc->driScreen && psc->driScreen->swapBuffers)
2093 return (*psc->driScreen->swapBuffers)(pdraw, target_msc, divisor,
2102 __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2103 int64_t target_msc, int64_t divisor,
2104 int64_t remainder, int64_t * ust,
2105 int64_t * msc, int64_t * sbc)
2107 #ifdef GLX_DIRECT_RENDERING
2108 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2109 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2114 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2115 * error", but the return type in the spec is Bool.
2117 if (divisor < 0 || remainder < 0 || target_msc < 0)
2119 if (divisor > 0 && remainder >= divisor)
2122 #ifdef GLX_DIRECT_RENDERING
2123 if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
2124 ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
2135 __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2136 int64_t target_sbc, int64_t * ust,
2137 int64_t * msc, int64_t * sbc)
2139 #ifdef GLX_DIRECT_RENDERING
2140 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2141 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2145 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2146 * error", but the return type in the spec is Bool.
2151 #ifdef GLX_DIRECT_RENDERING
2152 if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
2153 ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
2165 * Mesa extension stubs. These will help reduce portability problems.
2170 * Release all buffers associated with the specified GLX drawable.
2173 * This function was intended for stand-alone Mesa. The issue there is that
2174 * the library doesn't get any notification when a window is closed. In
2175 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2176 * supported, there are 3 different functions to destroy a drawable. It
2177 * should be possible to create GLX protocol (or have it determine which
2178 * protocol to use based on the type of the drawable) to have one function
2179 * do the work of 3. For the direct-rendering case, this function could
2180 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2181 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2182 * would need to be used. This really should be done as part of the new DRI
2185 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2186 * __driGarbageCollectDrawables
2187 * glXDestroyGLXPixmap
2188 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2189 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2192 __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
2201 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
2202 Pixmap pixmap, Colormap cmap)
2215 * GLX_MESA_copy_sub_buffer
2217 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2219 __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
2220 int x, int y, int width, int height)
2222 xGLXVendorPrivateReq *req;
2223 struct glx_context *gc;
2225 CARD32 *drawable_ptr;
2226 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2229 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2230 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2231 if (pdraw != NULL) {
2232 struct glx_screen *psc = pdraw->psc;
2233 if (psc->driScreen->copySubBuffer != NULL) {
2235 (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height);
2242 opcode = __glXSetupForCommand(dpy);
2247 ** The calling thread may or may not have a current context. If it
2248 ** does, send the context tag so the server can do a flush.
2250 gc = __glXGetCurrentContext();
2251 if ((gc != NULL) && (dpy == gc->currentDpy) &&
2252 ((drawable == gc->currentDrawable) ||
2253 (drawable == gc->currentReadable))) {
2254 tag = gc->currentContextTag;
2261 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4, req);
2262 req->reqType = opcode;
2263 req->glxCode = X_GLXVendorPrivate;
2264 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2265 req->contextTag = tag;
2267 drawable_ptr = (CARD32 *) (req + 1);
2268 x_ptr = (INT32 *) (drawable_ptr + 1);
2269 y_ptr = (INT32 *) (drawable_ptr + 2);
2270 w_ptr = (INT32 *) (drawable_ptr + 3);
2271 h_ptr = (INT32 *) (drawable_ptr + 4);
2273 *drawable_ptr = drawable;
2285 __glXBindTexImageEXT(Display * dpy,
2286 GLXDrawable drawable, int buffer, const int *attrib_list)
2288 struct glx_context *gc = __glXGetCurrentContext();
2290 if (gc == NULL || gc->vtable->bind_tex_image == NULL)
2293 gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
2297 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
2299 struct glx_context *gc = __glXGetCurrentContext();
2301 if (gc == NULL || gc->vtable->release_tex_image == NULL)
2304 gc->vtable->release_tex_image(dpy, drawable, buffer);
2309 #endif /* GLX_USE_APPLEGL */
2312 * \c strdup is actually not a standard ANSI C or POSIX routine.
2313 * Irix will not define it if ANSI mode is in effect.
2318 __glXstrdup(const char *str)
2321 copy = (char *) Xmalloc(strlen(str) + 1);
2329 ** glXGetProcAddress support
2332 struct name_address_pair
2338 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2339 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2341 static const struct name_address_pair GLX_functions[] = {
2342 /*** GLX_VERSION_1_0 ***/
2343 GLX_FUNCTION(glXChooseVisual),
2344 GLX_FUNCTION(glXCopyContext),
2345 GLX_FUNCTION(glXCreateContext),
2346 GLX_FUNCTION(glXCreateGLXPixmap),
2347 GLX_FUNCTION(glXDestroyContext),
2348 GLX_FUNCTION(glXDestroyGLXPixmap),
2349 GLX_FUNCTION(glXGetConfig),
2350 GLX_FUNCTION(glXGetCurrentContext),
2351 GLX_FUNCTION(glXGetCurrentDrawable),
2352 GLX_FUNCTION(glXIsDirect),
2353 GLX_FUNCTION(glXMakeCurrent),
2354 GLX_FUNCTION(glXQueryExtension),
2355 GLX_FUNCTION(glXQueryVersion),
2356 GLX_FUNCTION(glXSwapBuffers),
2357 GLX_FUNCTION(glXUseXFont),
2358 GLX_FUNCTION(glXWaitGL),
2359 GLX_FUNCTION(glXWaitX),
2361 /*** GLX_VERSION_1_1 ***/
2362 GLX_FUNCTION(glXGetClientString),
2363 GLX_FUNCTION(glXQueryExtensionsString),
2364 GLX_FUNCTION(glXQueryServerString),
2366 /*** GLX_VERSION_1_2 ***/
2367 GLX_FUNCTION(glXGetCurrentDisplay),
2369 /*** GLX_VERSION_1_3 ***/
2370 GLX_FUNCTION(glXChooseFBConfig),
2371 GLX_FUNCTION(glXCreateNewContext),
2372 GLX_FUNCTION(glXCreatePbuffer),
2373 GLX_FUNCTION(glXCreatePixmap),
2374 GLX_FUNCTION(glXCreateWindow),
2375 GLX_FUNCTION(glXDestroyPbuffer),
2376 GLX_FUNCTION(glXDestroyPixmap),
2377 GLX_FUNCTION(glXDestroyWindow),
2378 GLX_FUNCTION(glXGetCurrentReadDrawable),
2379 GLX_FUNCTION(glXGetFBConfigAttrib),
2380 GLX_FUNCTION(glXGetFBConfigs),
2381 GLX_FUNCTION(glXGetSelectedEvent),
2382 GLX_FUNCTION(glXGetVisualFromFBConfig),
2383 GLX_FUNCTION(glXMakeContextCurrent),
2384 GLX_FUNCTION(glXQueryContext),
2385 GLX_FUNCTION(glXQueryDrawable),
2386 GLX_FUNCTION(glXSelectEvent),
2388 #ifndef GLX_USE_APPLEGL
2389 /*** GLX_SGI_swap_control ***/
2390 GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
2392 /*** GLX_SGI_video_sync ***/
2393 GLX_FUNCTION2(glXGetVideoSyncSGI, __glXGetVideoSyncSGI),
2394 GLX_FUNCTION2(glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI),
2396 /*** GLX_SGI_make_current_read ***/
2397 GLX_FUNCTION2(glXMakeCurrentReadSGI, glXMakeContextCurrent),
2398 GLX_FUNCTION2(glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable),
2400 /*** GLX_EXT_import_context ***/
2401 GLX_FUNCTION(glXFreeContextEXT),
2402 GLX_FUNCTION(glXGetContextIDEXT),
2403 GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
2404 GLX_FUNCTION(glXImportContextEXT),
2405 GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
2408 /*** GLX_SGIX_fbconfig ***/
2409 GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
2410 GLX_FUNCTION2(glXChooseFBConfigSGIX, glXChooseFBConfig),
2411 GLX_FUNCTION(glXCreateGLXPixmapWithConfigSGIX),
2412 GLX_FUNCTION(glXCreateContextWithConfigSGIX),
2413 GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
2414 GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
2416 #ifndef GLX_USE_APPLEGL
2417 /*** GLX_SGIX_pbuffer ***/
2418 GLX_FUNCTION(glXCreateGLXPbufferSGIX),
2419 GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
2420 GLX_FUNCTION(glXQueryGLXPbufferSGIX),
2421 GLX_FUNCTION(glXSelectEventSGIX),
2422 GLX_FUNCTION(glXGetSelectedEventSGIX),
2424 /*** GLX_SGIX_swap_group ***/
2425 GLX_FUNCTION2(glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX),
2427 /*** GLX_SGIX_swap_barrier ***/
2428 GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
2429 GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
2431 /*** GLX_MESA_copy_sub_buffer ***/
2432 GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
2434 /*** GLX_MESA_pixmap_colormap ***/
2435 GLX_FUNCTION(glXCreateGLXPixmapMESA),
2437 /*** GLX_MESA_release_buffers ***/
2438 GLX_FUNCTION2(glXReleaseBuffersMESA, __glXReleaseBuffersMESA),
2440 /*** GLX_MESA_swap_control ***/
2441 GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
2442 GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
2445 /*** GLX_ARB_get_proc_address ***/
2446 GLX_FUNCTION(glXGetProcAddressARB),
2449 GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
2451 #ifndef GLX_USE_APPLEGL
2452 /*** GLX_OML_sync_control ***/
2453 GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
2454 GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
2455 GLX_FUNCTION2(glXSwapBuffersMscOML, __glXSwapBuffersMscOML),
2456 GLX_FUNCTION2(glXGetMscRateOML, __glXGetMscRateOML),
2457 GLX_FUNCTION2(glXGetSyncValuesOML, __glXGetSyncValuesOML),
2459 /*** GLX_EXT_texture_from_pixmap ***/
2460 GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
2461 GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
2464 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2465 /*** DRI configuration ***/
2466 GLX_FUNCTION(glXGetScreenDriver),
2467 GLX_FUNCTION(glXGetDriverConfig),
2470 {NULL, NULL} /* end of list */
2473 static const GLvoid *
2474 get_glx_proc_address(const char *funcName)
2478 /* try static functions */
2479 for (i = 0; GLX_functions[i].Name; i++) {
2480 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2481 return GLX_functions[i].Address;
2488 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2489 * \c glXGetProcAddress.
2491 * \param procName Name of a GL or GLX function.
2492 * \returns A pointer to the named function
2494 * \sa glXGetProcAddress
2496 _X_EXPORT void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
2498 typedef void (*gl_function) (void);
2502 /* Search the table of GLX and internal functions first. If that
2503 * fails and the supplied name could be a valid core GL name, try
2504 * searching the core GL function table. This check is done to prevent
2505 * DRI based drivers from searching the core GL function table for
2506 * internal API functions.
2508 f = (gl_function) get_glx_proc_address((const char *) procName);
2509 if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2510 && (procName[2] != 'X')) {
2511 #ifdef GLX_SHARED_GLAPI
2512 f = (gl_function) __indirect_get_proc_address((const char *) procName);
2515 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2517 struct glx_context *gc = __glXGetCurrentContext();
2519 if (gc != NULL && gc->vtable->get_proc_address != NULL)
2520 f = gc->vtable->get_proc_address((const char *) procName);
2527 * Get the address of a named GL function. This is the GLX 1.4 name for
2528 * \c glXGetProcAddressARB.
2530 * \param procName Name of a GL or GLX function.
2531 * \returns A pointer to the named function
2533 * \sa glXGetProcAddressARB
2535 _X_EXPORT void (*glXGetProcAddress(const GLubyte * procName)) (void)
2536 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
2537 __attribute__ ((alias("glXGetProcAddressARB")));
2540 return glXGetProcAddressARB(procName);
2542 #endif /* __GNUC__ */
2545 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2547 * Get the unadjusted system time (UST). Currently, the UST is measured in
2548 * microseconds since Epoc. The actual resolution of the UST may vary from
2549 * system to system, and the units may vary from release to release.
2550 * Drivers should not call this function directly. They should instead use
2551 * \c glXGetProcAddress to obtain a pointer to the function.
2553 * \param ust Location to store the 64-bit UST
2554 * \returns Zero on success or a negative errno value on failure.
2556 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2558 * \since Internal API version 20030317.
2561 __glXGetUST(int64_t * ust)
2569 if (gettimeofday(&tv, NULL) == 0) {
2570 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2577 #endif /* GLX_DIRECT_RENDERING */