OSDN Git Service

Merge branch 'glsl-to-tgsi'
[android-x86/external-mesa.git] / src / glx / glxcmds.c
1 /*
2  * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3  * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
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:
11  *
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.
16  *
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
23  * SOFTWARE.
24  *
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.
29  */
30
31 /**
32  * \file glxcmds.c
33  * Client-side GLX interface.
34  */
35
36 #include "glxclient.h"
37 #include "glapi.h"
38 #include "glxextensions.h"
39 #include "indirect.h"
40
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"
46 #else
47 #include <sys/time.h>
48 #ifdef XF86VIDMODE
49 #include <X11/extensions/xf86vmode.h>
50 #endif
51 #include "xf86dri.h"
52 #endif
53 #else
54 #endif
55
56 #if defined(USE_XCB)
57 #include <X11/Xlib-xcb.h>
58 #include <xcb/xcb.h>
59 #include <xcb/glx.h>
60 #endif
61
62 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
63 static const char __glXGLXClientVersion[] = "1.4";
64
65 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
66
67 /**
68  * Get the __DRIdrawable for the drawable associated with a GLXContext
69  *
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.
75  */
76 _X_HIDDEN __GLXDRIdrawable *
77 GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
78 {
79    struct glx_display *priv = __glXInitialize(dpy);
80    __GLXDRIdrawable *pdraw;
81
82    if (priv == NULL)
83       return NULL;
84
85    if (__glxHashLookup(priv->drawHash, drawable, (void *) &pdraw) == 0)
86       return pdraw;
87
88    return NULL;
89 }
90
91 #endif
92
93 _X_HIDDEN struct glx_drawable *
94 GetGLXDrawable(Display *dpy, GLXDrawable drawable)
95 {
96    struct glx_display *priv = __glXInitialize(dpy);
97    struct glx_drawable *glxDraw;
98
99    if (priv == NULL)
100       return NULL;
101
102    if (__glxHashLookup(priv->glXDrawHash, drawable, (void *) &glxDraw) == 0)
103       return glxDraw;
104
105    return NULL;
106 }
107
108 _X_HIDDEN int
109 InitGLXDrawable(Display *dpy, struct glx_drawable *glxDraw, XID xDrawable,
110                 GLXDrawable drawable)
111 {
112    struct glx_display *priv = __glXInitialize(dpy);
113
114    if (!priv)
115       return -1;
116
117    glxDraw->xDrawable = xDrawable;
118    glxDraw->drawable = drawable;
119    glxDraw->lastEventSbc = 0;
120    glxDraw->eventSbcWrap = 0;
121
122    return __glxHashInsert(priv->glXDrawHash, drawable, glxDraw);
123 }
124
125 _X_HIDDEN void
126 DestroyGLXDrawable(Display *dpy, GLXDrawable drawable)
127 {
128    struct glx_display *priv = __glXInitialize(dpy);
129    struct glx_drawable *glxDraw;
130
131    if (!priv)
132       return;
133
134    glxDraw = GetGLXDrawable(dpy, drawable);
135    __glxHashDelete(priv->glXDrawHash, drawable);
136    free(glxDraw);
137 }
138
139 /**
140  * Get the GLX per-screen data structure associated with a GLX context.
141  *
142  * \param dpy   Display for which the GLX per-screen information is to be
143  *              retrieved.
144  * \param scrn  Screen on \c dpy for which the GLX per-screen information is
145  *              to be retrieved.
146  * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
147  *          specify a valid GLX screen, or NULL otherwise.
148  *
149  * \todo Should this function validate that \c scrn is within the screen
150  *       number range for \c dpy?
151  */
152
153 static struct glx_screen *
154 GetGLXScreenConfigs(Display * dpy, int scrn)
155 {
156    struct glx_display *const priv = __glXInitialize(dpy);
157
158    return (priv
159            && priv->screens !=
160            NULL) ? priv->screens[scrn] : NULL;
161 }
162
163
164 static int
165 GetGLXPrivScreenConfig(Display * dpy, int scrn, struct glx_display ** ppriv,
166                        struct glx_screen ** ppsc)
167 {
168    /* Initialize the extension, if needed .  This has the added value
169     * of initializing/allocating the display private
170     */
171
172    if (dpy == NULL) {
173       return GLX_NO_EXTENSION;
174    }
175
176    *ppriv = __glXInitialize(dpy);
177    if (*ppriv == NULL) {
178       return GLX_NO_EXTENSION;
179    }
180
181    /* Check screen number to see if its valid */
182    if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
183       return GLX_BAD_SCREEN;
184    }
185
186    /* Check to see if the GL is supported on this screen */
187    *ppsc = (*ppriv)->screens[scrn];
188    if ((*ppsc)->configs == NULL) {
189       /* No support for GL on this screen regardless of visual */
190       return GLX_BAD_VISUAL;
191    }
192
193    return Success;
194 }
195
196
197 /**
198  * Determine if a \c GLXFBConfig supplied by the application is valid.
199  *
200  * \param dpy     Application supplied \c Display pointer.
201  * \param config  Application supplied \c GLXFBConfig.
202  *
203  * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
204  *          \c struct glx_config structure is returned.  Otherwise, \c NULL
205  *          is returned.
206  */
207 static struct glx_config *
208 ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
209 {
210    struct glx_display *const priv = __glXInitialize(dpy);
211    int num_screens = ScreenCount(dpy);
212    unsigned i;
213    struct glx_config *config;
214
215    if (priv != NULL) {
216       for (i = 0; i < num_screens; i++) {
217          for (config = priv->screens[i]->configs; config != NULL;
218               config = config->next) {
219             if (config == (struct glx_config *) fbconfig) {
220                return config;
221             }
222          }
223       }
224    }
225
226    return NULL;
227 }
228
229 _X_HIDDEN Bool
230 glx_context_init(struct glx_context *gc,
231                  struct glx_screen *psc, struct glx_config *config)
232 {
233    gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
234    if (!gc->majorOpcode)
235       return GL_FALSE;
236
237    gc->screen = psc->scr;
238    gc->psc = psc;
239    gc->config = config;
240    gc->isDirect = GL_TRUE;
241    gc->currentContextTag = -1;
242
243    return GL_TRUE;
244 }
245
246
247 /**
248  * Create a new context.
249  *
250  * \param renderType   For FBConfigs, what is the rendering type?
251  */
252
253 static GLXContext
254 CreateContext(Display *dpy, int generic_id, struct glx_config *config,
255               GLXContext shareList_user, Bool allowDirect,
256               unsigned code, int renderType, int screen)
257 {
258    struct glx_context *gc;
259    struct glx_screen *psc;
260    struct glx_context *shareList = (struct glx_context *) shareList_user;
261    if (dpy == NULL)
262       return NULL;
263
264    psc = GetGLXScreenConfigs(dpy, screen);
265    if (psc == NULL)
266       return NULL;
267
268    if (generic_id == None)
269       return NULL;
270
271    gc = NULL;
272 #ifdef GLX_USE_APPLEGL
273    gc = applegl_create_context(psc, config, shareList, renderType);
274 #else
275    if (allowDirect && psc->vtable->create_context)
276       gc = psc->vtable->create_context(psc, config, shareList, renderType);
277    if (!gc)
278       gc = indirect_create_context(psc, config, shareList, renderType);
279 #endif
280    if (!gc)
281       return NULL;
282
283    LockDisplay(dpy);
284    switch (code) {
285    case X_GLXCreateContext: {
286       xGLXCreateContextReq *req;
287
288       /* Send the glXCreateContext request */
289       GetReq(GLXCreateContext, req);
290       req->reqType = gc->majorOpcode;
291       req->glxCode = X_GLXCreateContext;
292       req->context = gc->xid = XAllocID(dpy);
293       req->visual = generic_id;
294       req->screen = screen;
295       req->shareList = shareList ? shareList->xid : None;
296       req->isDirect = gc->isDirect;
297       break;
298    }
299
300    case X_GLXCreateNewContext: {
301       xGLXCreateNewContextReq *req;
302
303       /* Send the glXCreateNewContext request */
304       GetReq(GLXCreateNewContext, req);
305       req->reqType = gc->majorOpcode;
306       req->glxCode = X_GLXCreateNewContext;
307       req->context = gc->xid = XAllocID(dpy);
308       req->fbconfig = generic_id;
309       req->screen = screen;
310       req->renderType = renderType;
311       req->shareList = shareList ? shareList->xid : None;
312       req->isDirect = gc->isDirect;
313       break;
314    }
315
316    case X_GLXvop_CreateContextWithConfigSGIX: {
317       xGLXVendorPrivateWithReplyReq *vpreq;
318       xGLXCreateContextWithConfigSGIXReq *req;
319
320       /* Send the glXCreateNewContext request */
321       GetReqExtra(GLXVendorPrivateWithReply,
322                   sz_xGLXCreateContextWithConfigSGIXReq -
323                   sz_xGLXVendorPrivateWithReplyReq, vpreq);
324       req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
325       req->reqType = gc->majorOpcode;
326       req->glxCode = X_GLXVendorPrivateWithReply;
327       req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
328       req->context = gc->xid = XAllocID(dpy);
329       req->fbconfig = generic_id;
330       req->screen = screen;
331       req->renderType = renderType;
332       req->shareList = shareList ? shareList->xid : None;
333       req->isDirect = gc->isDirect;
334       break;
335    }
336
337    default:
338       /* What to do here?  This case is the sign of an internal error.  It
339        * should never be reachable.
340        */
341       break;
342    }
343
344    UnlockDisplay(dpy);
345    SyncHandle();
346
347    gc->imported = GL_FALSE;
348    gc->renderType = renderType;
349
350    return (GLXContext) gc;
351 }
352
353 _X_EXPORT GLXContext
354 glXCreateContext(Display * dpy, XVisualInfo * vis,
355                  GLXContext shareList, Bool allowDirect)
356 {
357    struct glx_config *config = NULL;
358    int renderType = 0;
359
360 #if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
361    struct glx_screen *const psc = GetGLXScreenConfigs(dpy, vis->screen);
362
363    config = glx_config_find_visual(psc->visuals, vis->visualid);
364    if (config == NULL) {
365       xError error;
366
367       error.errorCode = BadValue;
368       error.resourceID = vis->visualid;
369       error.sequenceNumber = dpy->request;
370       error.type = X_Error;
371       error.majorCode = __glXSetupForCommand(dpy);
372       error.minorCode = X_GLXCreateContext;
373       _XError(dpy, &error);
374       return None;
375    }
376
377    renderType = config->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
378 #endif
379
380    return CreateContext(dpy, vis->visualid, config, shareList, allowDirect,
381                         X_GLXCreateContext, renderType, vis->screen);
382 }
383
384 _X_HIDDEN void
385 glx_send_destroy_context(Display *dpy, XID xid)
386 {
387    CARD8 opcode = __glXSetupForCommand(dpy);
388    xGLXDestroyContextReq *req;
389
390    LockDisplay(dpy);
391    GetReq(GLXDestroyContext, req);
392    req->reqType = opcode;
393    req->glxCode = X_GLXDestroyContext;
394    req->context = xid;
395    UnlockDisplay(dpy);
396    SyncHandle();
397 }
398
399 /*
400 ** Destroy the named context
401 */
402
403 _X_EXPORT void
404 glXDestroyContext(Display * dpy, GLXContext ctx)
405 {
406    struct glx_context *gc = (struct glx_context *) ctx;
407
408    if (!gc)
409       return;
410
411    __glXLock();
412    if (gc->currentDpy) {
413       /* This context is bound to some thread.  According to the man page,
414        * we should not actually delete the context until it's unbound.
415        * Note that we set gc->xid = None above.  In MakeContextCurrent()
416        * we check for that and delete the context there.
417        */
418       if (!gc->imported)
419          glx_send_destroy_context(dpy, gc->xid);
420       gc->xid = None;
421       __glXUnlock();
422       return;
423    }
424    __glXUnlock();
425
426    gc->vtable->destroy(gc);
427 }
428
429 /*
430 ** Return the major and minor version #s for the GLX extension
431 */
432 _X_EXPORT Bool
433 glXQueryVersion(Display * dpy, int *major, int *minor)
434 {
435    struct glx_display *priv;
436
437    /* Init the extension.  This fetches the major and minor version. */
438    priv = __glXInitialize(dpy);
439    if (!priv)
440       return GL_FALSE;
441
442    if (major)
443       *major = priv->majorVersion;
444    if (minor)
445       *minor = priv->minorVersion;
446    return GL_TRUE;
447 }
448
449 /*
450 ** Query the existance of the GLX extension
451 */
452 _X_EXPORT Bool
453 glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
454 {
455    int major_op, erb, evb;
456    Bool rv;
457
458    rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
459    if (rv) {
460       if (errorBase)
461          *errorBase = erb;
462       if (eventBase)
463          *eventBase = evb;
464    }
465    return rv;
466 }
467
468 /*
469 ** Put a barrier in the token stream that forces the GL to finish its
470 ** work before X can proceed.
471 */
472 _X_EXPORT void
473 glXWaitGL(void)
474 {
475    struct glx_context *gc = __glXGetCurrentContext();
476
477    if (gc && gc->vtable->wait_gl)
478       gc->vtable->wait_gl(gc);
479 }
480
481 /*
482 ** Put a barrier in the token stream that forces X to finish its
483 ** work before GL can proceed.
484 */
485 _X_EXPORT void
486 glXWaitX(void)
487 {
488    struct glx_context *gc = __glXGetCurrentContext();
489
490    if (gc && gc->vtable->wait_x)
491       gc->vtable->wait_x(gc);
492 }
493
494 _X_EXPORT void
495 glXUseXFont(Font font, int first, int count, int listBase)
496 {
497    struct glx_context *gc = __glXGetCurrentContext();
498
499    if (gc && gc->vtable->use_x_font)
500       gc->vtable->use_x_font(gc, font, first, count, listBase);
501 }
502
503 /************************************************************************/
504
505 /*
506 ** Copy the source context to the destination context using the
507 ** attribute "mask".
508 */
509 _X_EXPORT void
510 glXCopyContext(Display * dpy, GLXContext source_user,
511                GLXContext dest_user, unsigned long mask)
512 {
513    struct glx_context *source = (struct glx_context *) source_user;
514    struct glx_context *dest = (struct glx_context *) dest_user;
515 #ifdef GLX_USE_APPLEGL
516    struct glx_context *gc = __glXGetCurrentContext();
517    int errorcode;
518    bool x11error;
519
520    if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
521                              mask, &errorcode, &x11error)) {
522       __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
523    }
524    
525 #else
526    xGLXCopyContextReq *req;
527    struct glx_context *gc = __glXGetCurrentContext();
528    GLXContextTag tag;
529    CARD8 opcode;
530
531    opcode = __glXSetupForCommand(dpy);
532    if (!opcode) {
533       return;
534    }
535
536 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
537    if (gc->isDirect) {
538       /* NOT_DONE: This does not work yet */
539    }
540 #endif
541
542    /*
543     ** If the source is the current context, send its tag so that the context
544     ** can be flushed before the copy.
545     */
546    if (source == gc && dpy == gc->currentDpy) {
547       tag = gc->currentContextTag;
548    }
549    else {
550       tag = 0;
551    }
552
553    /* Send the glXCopyContext request */
554    LockDisplay(dpy);
555    GetReq(GLXCopyContext, req);
556    req->reqType = opcode;
557    req->glxCode = X_GLXCopyContext;
558    req->source = source ? source->xid : None;
559    req->dest = dest ? dest->xid : None;
560    req->mask = mask;
561    req->contextTag = tag;
562    UnlockDisplay(dpy);
563    SyncHandle();
564 #endif /* GLX_USE_APPLEGL */
565 }
566
567
568 /**
569  * Determine if a context uses direct rendering.
570  *
571  * \param dpy        Display where the context was created.
572  * \param contextID  ID of the context to be tested.
573  *
574  * \returns \c GL_TRUE if the context is direct rendering or not.
575  */
576 static Bool
577 __glXIsDirect(Display * dpy, GLXContextID contextID)
578 {
579 #if !defined(USE_XCB)
580    xGLXIsDirectReq *req;
581    xGLXIsDirectReply reply;
582 #endif
583    CARD8 opcode;
584
585    opcode = __glXSetupForCommand(dpy);
586    if (!opcode) {
587       return GL_FALSE;
588    }
589
590 #ifdef USE_XCB
591    xcb_connection_t *c = XGetXCBConnection(dpy);
592    xcb_glx_is_direct_reply_t *reply = xcb_glx_is_direct_reply(c,
593                                                               xcb_glx_is_direct
594                                                               (c, contextID),
595                                                               NULL);
596
597    const Bool is_direct = reply->is_direct ? True : False;
598    free(reply);
599
600    return is_direct;
601 #else
602    /* Send the glXIsDirect request */
603    LockDisplay(dpy);
604    GetReq(GLXIsDirect, req);
605    req->reqType = opcode;
606    req->glxCode = X_GLXIsDirect;
607    req->context = contextID;
608    _XReply(dpy, (xReply *) & reply, 0, False);
609    UnlockDisplay(dpy);
610    SyncHandle();
611
612    return reply.isDirect;
613 #endif /* USE_XCB */
614 }
615
616 /**
617  * \todo
618  * Shouldn't this function \b always return \c GL_FALSE when
619  * \c GLX_DIRECT_RENDERING is not defined?  Do we really need to bother with
620  * the GLX protocol here at all?
621  */
622 _X_EXPORT Bool
623 glXIsDirect(Display * dpy, GLXContext gc_user)
624 {
625    struct glx_context *gc = (struct glx_context *) gc_user;
626
627    if (!gc) {
628       return GL_FALSE;
629    }
630    else if (gc->isDirect) {
631       return GL_TRUE;
632    }
633 #ifdef GLX_USE_APPLEGL  /* TODO: indirect on darwin */
634       return GL_FALSE;
635 #else
636    return __glXIsDirect(dpy, gc->xid);
637 #endif
638 }
639
640 _X_EXPORT GLXPixmap
641 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
642 {
643 #ifdef GLX_USE_APPLEGL
644    int screen = vis->screen;
645    struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
646    const struct glx_config *config;
647
648    config = glx_config_find_visual(psc->visuals, vis->visualid);
649    
650    if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, config))
651       return None;
652    
653    return pixmap;
654 #else
655    xGLXCreateGLXPixmapReq *req;
656    struct glx_drawable *glxDraw;
657    GLXPixmap xid;
658    CARD8 opcode;
659
660    opcode = __glXSetupForCommand(dpy);
661    if (!opcode) {
662       return None;
663    }
664
665    glxDraw = Xmalloc(sizeof(*glxDraw));
666    if (!glxDraw)
667       return None;
668
669    /* Send the glXCreateGLXPixmap request */
670    LockDisplay(dpy);
671    GetReq(GLXCreateGLXPixmap, req);
672    req->reqType = opcode;
673    req->glxCode = X_GLXCreateGLXPixmap;
674    req->screen = vis->screen;
675    req->visual = vis->visualid;
676    req->pixmap = pixmap;
677    req->glxpixmap = xid = XAllocID(dpy);
678    UnlockDisplay(dpy);
679    SyncHandle();
680
681    if (InitGLXDrawable(dpy, glxDraw, pixmap, req->glxpixmap)) {
682       free(glxDraw);
683       return None;
684    }
685
686 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
687    do {
688       /* FIXME: Maybe delay __DRIdrawable creation until the drawable
689        * is actually bound to a context... */
690
691       struct glx_display *const priv = __glXInitialize(dpy);
692       __GLXDRIdrawable *pdraw;
693       struct glx_screen *psc;
694       struct glx_config *config;
695
696       psc = priv->screens[vis->screen];
697       if (psc->driScreen == NULL)
698          return xid;
699
700       config = glx_config_find_visual(psc->visuals, vis->visualid);
701       pdraw = psc->driScreen->createDrawable(psc, pixmap, xid, config);
702       if (pdraw == NULL) {
703          fprintf(stderr, "failed to create pixmap\n");
704          xid = None;
705          break;
706       }
707
708       if (__glxHashInsert(priv->drawHash, xid, pdraw)) {
709          (*pdraw->destroyDrawable) (pdraw);
710          xid = None;
711          break;
712       }
713    } while (0);
714
715    if (xid == None) {
716       xGLXDestroyGLXPixmapReq *dreq;
717       LockDisplay(dpy);
718       GetReq(GLXDestroyGLXPixmap, dreq);
719       dreq->reqType = opcode;
720       dreq->glxCode = X_GLXDestroyGLXPixmap;
721       dreq->glxpixmap = xid;
722       UnlockDisplay(dpy);
723       SyncHandle();
724    }
725 #endif
726
727    return xid;
728 #endif
729 }
730
731 /*
732 ** Destroy the named pixmap
733 */
734 _X_EXPORT void
735 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
736 {
737 #ifdef GLX_USE_APPLEGL
738    if(apple_glx_pixmap_destroy(dpy, glxpixmap))
739       __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
740 #else
741    xGLXDestroyGLXPixmapReq *req;
742    CARD8 opcode;
743
744    opcode = __glXSetupForCommand(dpy);
745    if (!opcode) {
746       return;
747    }
748
749    /* Send the glXDestroyGLXPixmap request */
750    LockDisplay(dpy);
751    GetReq(GLXDestroyGLXPixmap, req);
752    req->reqType = opcode;
753    req->glxCode = X_GLXDestroyGLXPixmap;
754    req->glxpixmap = glxpixmap;
755    UnlockDisplay(dpy);
756    SyncHandle();
757
758    DestroyGLXDrawable(dpy, glxpixmap);
759
760 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
761    {
762       struct glx_display *const priv = __glXInitialize(dpy);
763       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
764
765       if (pdraw != NULL) {
766          (*pdraw->destroyDrawable) (pdraw);
767          __glxHashDelete(priv->drawHash, glxpixmap);
768       }
769    }
770 #endif
771 #endif /* GLX_USE_APPLEGL */
772 }
773
774 _X_EXPORT void
775 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
776 {
777 #ifdef GLX_USE_APPLEGL
778    struct glx_context * gc = __glXGetCurrentContext();
779    if(gc && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
780       apple_glx_swap_buffers(gc->driContext);
781    } else {
782       __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
783    }
784 #else
785    struct glx_context *gc;
786    GLXContextTag tag;
787    CARD8 opcode;
788 #ifdef USE_XCB
789    xcb_connection_t *c;
790 #else
791    xGLXSwapBuffersReq *req;
792 #endif
793
794    gc = __glXGetCurrentContext();
795
796 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
797    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
798
799    if (pdraw != NULL) {
800       if (gc && drawable == gc->currentDrawable) {
801          glFlush();
802       }
803
804       (*pdraw->psc->driScreen->swapBuffers)(pdraw, 0, 0, 0);
805       return;
806    }
807 #endif
808
809    opcode = __glXSetupForCommand(dpy);
810    if (!opcode) {
811       return;
812    }
813
814    /*
815     ** The calling thread may or may not have a current context.  If it
816     ** does, send the context tag so the server can do a flush.
817     */
818    if ((gc != NULL) && (dpy == gc->currentDpy) &&
819        ((drawable == gc->currentDrawable)
820         || (drawable == gc->currentReadable))) {
821       tag = gc->currentContextTag;
822    }
823    else {
824       tag = 0;
825    }
826
827 #ifdef USE_XCB
828    c = XGetXCBConnection(dpy);
829    xcb_glx_swap_buffers(c, tag, drawable);
830    xcb_flush(c);
831 #else
832    /* Send the glXSwapBuffers request */
833    LockDisplay(dpy);
834    GetReq(GLXSwapBuffers, req);
835    req->reqType = opcode;
836    req->glxCode = X_GLXSwapBuffers;
837    req->drawable = drawable;
838    req->contextTag = tag;
839    UnlockDisplay(dpy);
840    SyncHandle();
841    XFlush(dpy);
842 #endif /* USE_XCB */
843 #endif /* GLX_USE_APPLEGL */
844 }
845
846
847 /*
848 ** Return configuration information for the given display, screen and
849 ** visual combination.
850 */
851 _X_EXPORT int
852 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
853              int *value_return)
854 {
855    struct glx_display *priv;
856    struct glx_screen *psc;
857    struct glx_config *config;
858    int status;
859
860    status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
861    if (status == Success) {
862       config = glx_config_find_visual(psc->visuals, vis->visualid);
863
864       /* Lookup attribute after first finding a match on the visual */
865       if (config != NULL) {
866          return glx_config_get(config, attribute, value_return);
867       }
868
869       status = GLX_BAD_VISUAL;
870    }
871
872    /*
873     ** If we can't find the config for this visual, this visual is not
874     ** supported by the OpenGL implementation on the server.
875     */
876    if ((status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL)) {
877       *value_return = GL_FALSE;
878       status = Success;
879    }
880
881    return status;
882 }
883
884 /************************************************************************/
885
886 static void
887 init_fbconfig_for_chooser(struct glx_config * config,
888                           GLboolean fbconfig_style_tags)
889 {
890    memset(config, 0, sizeof(struct glx_config));
891    config->visualID = (XID) GLX_DONT_CARE;
892    config->visualType = GLX_DONT_CARE;
893
894    /* glXChooseFBConfig specifies different defaults for these two than
895     * glXChooseVisual.
896     */
897    if (fbconfig_style_tags) {
898       config->rgbMode = GL_TRUE;
899       config->doubleBufferMode = GLX_DONT_CARE;
900    }
901
902    config->visualRating = GLX_DONT_CARE;
903    config->transparentPixel = GLX_NONE;
904    config->transparentRed = GLX_DONT_CARE;
905    config->transparentGreen = GLX_DONT_CARE;
906    config->transparentBlue = GLX_DONT_CARE;
907    config->transparentAlpha = GLX_DONT_CARE;
908    config->transparentIndex = GLX_DONT_CARE;
909
910    config->drawableType = GLX_WINDOW_BIT;
911    config->renderType =
912       (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
913    config->xRenderable = GLX_DONT_CARE;
914    config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
915
916    config->swapMethod = GLX_DONT_CARE;
917 }
918
919 #define MATCH_DONT_CARE( param )        \
920   do {                                  \
921     if ( ((int) a-> param != (int) GLX_DONT_CARE)   \
922          && (a-> param != b-> param) ) {        \
923       return False;                             \
924     }                                           \
925   } while ( 0 )
926
927 #define MATCH_MINIMUM( param )                  \
928   do {                                          \
929     if ( ((int) a-> param != (int) GLX_DONT_CARE)       \
930          && (a-> param > b-> param) ) {         \
931       return False;                             \
932     }                                           \
933   } while ( 0 )
934
935 #define MATCH_EXACT( param )                    \
936   do {                                          \
937     if ( a-> param != b-> param) {              \
938       return False;                             \
939     }                                           \
940   } while ( 0 )
941
942 /* Test that all bits from a are contained in b */
943 #define MATCH_MASK(param)                       \
944   do {                                          \
945     if ((a->param & ~b->param) != 0)            \
946       return False;                             \
947   } while (0);
948
949 /**
950  * Determine if two GLXFBConfigs are compatible.
951  *
952  * \param a  Application specified config to test.
953  * \param b  Server specified config to test against \c a.
954  */
955 static Bool
956 fbconfigs_compatible(const struct glx_config * const a,
957                      const struct glx_config * const b)
958 {
959    MATCH_DONT_CARE(doubleBufferMode);
960    MATCH_DONT_CARE(visualType);
961    MATCH_DONT_CARE(visualRating);
962    MATCH_DONT_CARE(xRenderable);
963    MATCH_DONT_CARE(fbconfigID);
964    MATCH_DONT_CARE(swapMethod);
965
966    MATCH_MINIMUM(rgbBits);
967    MATCH_MINIMUM(numAuxBuffers);
968    MATCH_MINIMUM(redBits);
969    MATCH_MINIMUM(greenBits);
970    MATCH_MINIMUM(blueBits);
971    MATCH_MINIMUM(alphaBits);
972    MATCH_MINIMUM(depthBits);
973    MATCH_MINIMUM(stencilBits);
974    MATCH_MINIMUM(accumRedBits);
975    MATCH_MINIMUM(accumGreenBits);
976    MATCH_MINIMUM(accumBlueBits);
977    MATCH_MINIMUM(accumAlphaBits);
978    MATCH_MINIMUM(sampleBuffers);
979    MATCH_MINIMUM(maxPbufferWidth);
980    MATCH_MINIMUM(maxPbufferHeight);
981    MATCH_MINIMUM(maxPbufferPixels);
982    MATCH_MINIMUM(samples);
983
984    MATCH_DONT_CARE(stereoMode);
985    MATCH_EXACT(level);
986
987    MATCH_MASK(drawableType);
988    MATCH_MASK(renderType);
989
990    /* There is a bug in a few of the XFree86 DDX drivers.  They contain
991     * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
992     * Technically speaking, it is a bug in the DDX driver, but there is
993     * enough of an installed base to work around the problem here.  In any
994     * case, 0 is not a valid value of the transparent type, so we'll treat 0
995     * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
996     * 0 from the server to be a match to maintain backward compatibility with
997     * the (broken) drivers.
998     */
999
1000    if (a->transparentPixel != (int) GLX_DONT_CARE && a->transparentPixel != 0) {
1001       if (a->transparentPixel == GLX_NONE) {
1002          if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
1003             return False;
1004       }
1005       else {
1006          MATCH_EXACT(transparentPixel);
1007       }
1008
1009       switch (a->transparentPixel) {
1010       case GLX_TRANSPARENT_RGB:
1011          MATCH_DONT_CARE(transparentRed);
1012          MATCH_DONT_CARE(transparentGreen);
1013          MATCH_DONT_CARE(transparentBlue);
1014          MATCH_DONT_CARE(transparentAlpha);
1015          break;
1016
1017       case GLX_TRANSPARENT_INDEX:
1018          MATCH_DONT_CARE(transparentIndex);
1019          break;
1020
1021       default:
1022          break;
1023       }
1024    }
1025
1026    return True;
1027 }
1028
1029
1030 /* There's some trickly language in the GLX spec about how this is supposed
1031  * to work.  Basically, if a given component size is either not specified
1032  * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1033  * Well, that's really hard to do with the code as-is.  This behavior is
1034  * closer to correct, but still not technically right.
1035  */
1036 #define PREFER_LARGER_OR_ZERO(comp)             \
1037   do {                                          \
1038     if ( ((*a)-> comp) != ((*b)-> comp) ) {     \
1039       if ( ((*a)-> comp) == 0 ) {               \
1040         return -1;                              \
1041       }                                         \
1042       else if ( ((*b)-> comp) == 0 ) {          \
1043         return 1;                               \
1044       }                                         \
1045       else {                                    \
1046         return ((*b)-> comp) - ((*a)-> comp) ;  \
1047       }                                         \
1048     }                                           \
1049   } while( 0 )
1050
1051 #define PREFER_LARGER(comp)                     \
1052   do {                                          \
1053     if ( ((*a)-> comp) != ((*b)-> comp) ) {     \
1054       return ((*b)-> comp) - ((*a)-> comp) ;    \
1055     }                                           \
1056   } while( 0 )
1057
1058 #define PREFER_SMALLER(comp)                    \
1059   do {                                          \
1060     if ( ((*a)-> comp) != ((*b)-> comp) ) {     \
1061       return ((*a)-> comp) - ((*b)-> comp) ;    \
1062     }                                           \
1063   } while( 0 )
1064
1065 /**
1066  * Compare two GLXFBConfigs.  This function is intended to be used as the
1067  * compare function passed in to qsort.
1068  *
1069  * \returns If \c a is a "better" config, according to the specification of
1070  *          SGIX_fbconfig, a number less than zero is returned.  If \c b is
1071  *          better, then a number greater than zero is return.  If both are
1072  *          equal, zero is returned.
1073  * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1074  */
1075 static int
1076 fbconfig_compare(struct glx_config **a, struct glx_config **b)
1077 {
1078    /* The order of these comparisons must NOT change.  It is defined by
1079     * the GLX 1.3 spec and ARB_multisample.
1080     */
1081
1082    PREFER_SMALLER(visualSelectGroup);
1083
1084    /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1085     * GLX_NON_CONFORMANT_CONFIG.  It just so happens that this is the
1086     * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1087     */
1088    PREFER_SMALLER(visualRating);
1089
1090    /* This isn't quite right.  It is supposed to compare the sum of the
1091     * components the user specifically set minimums for.
1092     */
1093    PREFER_LARGER_OR_ZERO(redBits);
1094    PREFER_LARGER_OR_ZERO(greenBits);
1095    PREFER_LARGER_OR_ZERO(blueBits);
1096    PREFER_LARGER_OR_ZERO(alphaBits);
1097
1098    PREFER_SMALLER(rgbBits);
1099
1100    if (((*a)->doubleBufferMode != (*b)->doubleBufferMode)) {
1101       /* Prefer single-buffer.
1102        */
1103       return (!(*a)->doubleBufferMode) ? -1 : 1;
1104    }
1105
1106    PREFER_SMALLER(numAuxBuffers);
1107
1108    PREFER_LARGER_OR_ZERO(depthBits);
1109    PREFER_SMALLER(stencilBits);
1110
1111    /* This isn't quite right.  It is supposed to compare the sum of the
1112     * components the user specifically set minimums for.
1113     */
1114    PREFER_LARGER_OR_ZERO(accumRedBits);
1115    PREFER_LARGER_OR_ZERO(accumGreenBits);
1116    PREFER_LARGER_OR_ZERO(accumBlueBits);
1117    PREFER_LARGER_OR_ZERO(accumAlphaBits);
1118
1119    PREFER_SMALLER(visualType);
1120
1121    /* None of the multisample specs say where this comparison should happen,
1122     * so I put it near the end.
1123     */
1124    PREFER_SMALLER(sampleBuffers);
1125    PREFER_SMALLER(samples);
1126
1127    /* None of the pbuffer or fbconfig specs say that this comparison needs
1128     * to happen at all, but it seems like it should.
1129     */
1130    PREFER_LARGER(maxPbufferWidth);
1131    PREFER_LARGER(maxPbufferHeight);
1132    PREFER_LARGER(maxPbufferPixels);
1133
1134    return 0;
1135 }
1136
1137
1138 /**
1139  * Selects and sorts a subset of the supplied configs based on the attributes.
1140  * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1141  * and \c glXChooseFBConfigSGIX.
1142  *
1143  * \param configs   Array of pointers to possible configs.  The elements of
1144  *                  this array that do not meet the criteria will be set to
1145  *                  NULL.  The remaining elements will be sorted according to
1146  *                  the various visual / FBConfig selection rules.
1147  * \param num_configs  Number of elements in the \c configs array.
1148  * \param attribList   Attributes used select from \c configs.  This array is
1149  *                     terminated by a \c None tag.  The array can either take
1150  *                     the form expected by \c glXChooseVisual (where boolean
1151  *                     tags do not have a value) or by \c glXChooseFBConfig
1152  *                     (where every tag has a value).
1153  * \param fbconfig_style_tags  Selects whether \c attribList is in
1154  *                             \c glXChooseVisual style or
1155  *                             \c glXChooseFBConfig style.
1156  * \returns The number of valid elements left in \c configs.
1157  *
1158  * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1159  */
1160 static int
1161 choose_visual(struct glx_config ** configs, int num_configs,
1162               const int *attribList, GLboolean fbconfig_style_tags)
1163 {
1164    struct glx_config test_config;
1165    int base;
1166    int i;
1167
1168    /* This is a fairly direct implementation of the selection method
1169     * described by GLX_SGIX_fbconfig.  Start by culling out all the
1170     * configs that are not compatible with the selected parameter
1171     * list.
1172     */
1173
1174    init_fbconfig_for_chooser(&test_config, fbconfig_style_tags);
1175    __glXInitializeVisualConfigFromTags(&test_config, 512,
1176                                        (const INT32 *) attribList,
1177                                        GL_TRUE, fbconfig_style_tags);
1178
1179    base = 0;
1180    for (i = 0; i < num_configs; i++) {
1181       if (fbconfigs_compatible(&test_config, configs[i])) {
1182          configs[base] = configs[i];
1183          base++;
1184       }
1185    }
1186
1187    if (base == 0) {
1188       return 0;
1189    }
1190
1191    if (base < num_configs) {
1192       (void) memset(&configs[base], 0, sizeof(void *) * (num_configs - base));
1193    }
1194
1195    /* After the incompatible configs are removed, the resulting
1196     * list is sorted according to the rules set out in the various
1197     * specifications.
1198     */
1199
1200    qsort(configs, base, sizeof(struct glx_config *),
1201          (int (*)(const void *, const void *)) fbconfig_compare);
1202    return base;
1203 }
1204
1205
1206
1207
1208 /*
1209 ** Return the visual that best matches the template.  Return None if no
1210 ** visual matches the template.
1211 */
1212 _X_EXPORT XVisualInfo *
1213 glXChooseVisual(Display * dpy, int screen, int *attribList)
1214 {
1215    XVisualInfo *visualList = NULL;
1216    struct glx_display *priv;
1217    struct glx_screen *psc;
1218    struct glx_config test_config;
1219    struct glx_config *config;
1220    struct glx_config *best_config = NULL;
1221
1222    /*
1223     ** Get a list of all visuals, return if list is empty
1224     */
1225    if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1226       return None;
1227    }
1228
1229
1230    /*
1231     ** Build a template from the defaults and the attribute list
1232     ** Free visual list and return if an unexpected token is encountered
1233     */
1234    init_fbconfig_for_chooser(&test_config, GL_FALSE);
1235    __glXInitializeVisualConfigFromTags(&test_config, 512,
1236                                        (const INT32 *) attribList,
1237                                        GL_TRUE, GL_FALSE);
1238
1239    /*
1240     ** Eliminate visuals that don't meet minimum requirements
1241     ** Compute a score for those that do
1242     ** Remember which visual, if any, got the highest score
1243     ** If no visual is acceptable, return None
1244     ** Otherwise, create an XVisualInfo list with just the selected X visual
1245     ** and return this.
1246     */
1247    for (config = psc->visuals; config != NULL; config = config->next) {
1248       if (fbconfigs_compatible(&test_config, config)
1249           && ((best_config == NULL) ||
1250               (fbconfig_compare (&config, &best_config) < 0))) {
1251          XVisualInfo visualTemplate;
1252          XVisualInfo *newList;
1253          int i;
1254
1255          visualTemplate.screen = screen;
1256          visualTemplate.visualid = config->visualID;
1257          newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
1258                                   &visualTemplate, &i);
1259
1260          if (newList) {
1261             Xfree(visualList);
1262             visualList = newList;
1263             best_config = config;
1264          }
1265       }
1266    }
1267
1268 #ifdef GLX_USE_APPLEGL
1269    if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
1270       printf("visualid 0x%lx\n", visualList[0].visualid);
1271    }
1272 #endif
1273
1274    return visualList;
1275 }
1276
1277
1278 _X_EXPORT const char *
1279 glXQueryExtensionsString(Display * dpy, int screen)
1280 {
1281    struct glx_screen *psc;
1282    struct glx_display *priv;
1283
1284    if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1285       return NULL;
1286    }
1287
1288    if (!psc->effectiveGLXexts) {
1289       if (!psc->serverGLXexts) {
1290          psc->serverGLXexts =
1291             __glXQueryServerString(dpy, priv->majorOpcode, screen,
1292                                    GLX_EXTENSIONS);
1293       }
1294
1295       __glXCalculateUsableExtensions(psc,
1296 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
1297                                      (psc->driScreen != NULL),
1298 #else
1299                                      GL_FALSE,
1300 #endif
1301                                      priv->minorVersion);
1302    }
1303
1304    return psc->effectiveGLXexts;
1305 }
1306
1307 _X_EXPORT const char *
1308 glXGetClientString(Display * dpy, int name)
1309 {
1310    (void) dpy;
1311
1312    switch (name) {
1313    case GLX_VENDOR:
1314       return (__glXGLXClientVendorName);
1315    case GLX_VERSION:
1316       return (__glXGLXClientVersion);
1317    case GLX_EXTENSIONS:
1318       return (__glXGetClientExtensions());
1319    default:
1320       return NULL;
1321    }
1322 }
1323
1324 _X_EXPORT const char *
1325 glXQueryServerString(Display * dpy, int screen, int name)
1326 {
1327    struct glx_screen *psc;
1328    struct glx_display *priv;
1329    const char **str;
1330
1331
1332    if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1333       return NULL;
1334    }
1335
1336    switch (name) {
1337    case GLX_VENDOR:
1338       str = &priv->serverGLXvendor;
1339       break;
1340    case GLX_VERSION:
1341       str = &priv->serverGLXversion;
1342       break;
1343    case GLX_EXTENSIONS:
1344       str = &psc->serverGLXexts;
1345       break;
1346    default:
1347       return NULL;
1348    }
1349
1350    if (*str == NULL) {
1351       *str = __glXQueryServerString(dpy, priv->majorOpcode, screen, name);
1352    }
1353
1354    return *str;
1355 }
1356
1357 void
1358 __glXClientInfo(Display * dpy, int opcode)
1359 {
1360    char *ext_str = __glXGetClientGLExtensionString();
1361    int size = strlen(ext_str) + 1;
1362
1363 #ifdef USE_XCB
1364    xcb_connection_t *c = XGetXCBConnection(dpy);
1365    xcb_glx_client_info(c,
1366                        GLX_MAJOR_VERSION, GLX_MINOR_VERSION, size, ext_str);
1367 #else
1368    xGLXClientInfoReq *req;
1369
1370    /* Send the glXClientInfo request */
1371    LockDisplay(dpy);
1372    GetReq(GLXClientInfo, req);
1373    req->reqType = opcode;
1374    req->glxCode = X_GLXClientInfo;
1375    req->major = GLX_MAJOR_VERSION;
1376    req->minor = GLX_MINOR_VERSION;
1377
1378    req->length += (size + 3) >> 2;
1379    req->numbytes = size;
1380    Data(dpy, ext_str, size);
1381
1382    UnlockDisplay(dpy);
1383    SyncHandle();
1384 #endif /* USE_XCB */
1385
1386    Xfree(ext_str);
1387 }
1388
1389
1390 /*
1391 ** EXT_import_context
1392 */
1393
1394 _X_EXPORT Display *
1395 glXGetCurrentDisplay(void)
1396 {
1397    struct glx_context *gc = __glXGetCurrentContext();
1398    if (NULL == gc)
1399       return NULL;
1400    return gc->currentDpy;
1401 }
1402
1403 _X_EXPORT
1404 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1405           glXGetCurrentDisplay)
1406
1407 #ifndef GLX_USE_APPLEGL
1408 _X_EXPORT GLXContext
1409 glXImportContextEXT(Display *dpy, GLXContextID contextID)
1410 {
1411    struct glx_display *priv = __glXInitialize(dpy);
1412    struct glx_screen *psc;
1413    xGLXQueryContextReply reply;
1414    CARD8 opcode;
1415    struct glx_context *ctx;
1416    int propList[__GLX_MAX_CONTEXT_PROPS * 2], *pProp, nPropListBytes;
1417    int i, renderType;
1418    XID share;
1419    struct glx_config *mode;
1420
1421    if (contextID == None || __glXIsDirect(dpy, contextID))
1422       return NULL;
1423
1424    opcode = __glXSetupForCommand(dpy);
1425    if (!opcode)
1426       return 0;
1427
1428    /* Send the glXQueryContextInfoEXT request */
1429    LockDisplay(dpy);
1430
1431    if (priv->majorVersion > 1 || priv->minorVersion >= 3) {
1432       xGLXQueryContextReq *req;
1433
1434       GetReq(GLXQueryContext, req);
1435
1436       req->reqType = opcode;
1437       req->glxCode = X_GLXQueryContext;
1438       req->context = contextID;
1439    }
1440    else {
1441       xGLXVendorPrivateReq *vpreq;
1442       xGLXQueryContextInfoEXTReq *req;
1443
1444       GetReqExtra(GLXVendorPrivate,
1445                   sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1446                   vpreq);
1447       req = (xGLXQueryContextInfoEXTReq *) vpreq;
1448       req->reqType = opcode;
1449       req->glxCode = X_GLXVendorPrivateWithReply;
1450       req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1451       req->context = contextID;
1452    }
1453
1454    _XReply(dpy, (xReply *) & reply, 0, False);
1455
1456    if (reply.n <= __GLX_MAX_CONTEXT_PROPS)
1457       nPropListBytes = reply.n * 2 * sizeof propList[0];
1458    else
1459       nPropListBytes = 0;
1460    _XRead(dpy, (char *) propList, nPropListBytes);
1461    UnlockDisplay(dpy);
1462    SyncHandle();
1463
1464    /* Look up screen first so we can look up visuals/fbconfigs later */
1465    psc = NULL;
1466    for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1467       if (pProp[0] == GLX_SCREEN)
1468          psc = GetGLXScreenConfigs(dpy, pProp[1]);
1469    if (psc == NULL)
1470       return NULL;
1471
1472    share = None;
1473    mode = NULL;
1474    renderType = 0;
1475    pProp = propList;
1476
1477    for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1478       switch (pProp[0]) {
1479       case GLX_SHARE_CONTEXT_EXT:
1480          share = pProp[1];
1481          break;
1482       case GLX_VISUAL_ID_EXT:
1483          mode = glx_config_find_visual(psc->visuals, pProp[1]);
1484          break;
1485       case GLX_FBCONFIG_ID:
1486          mode = glx_config_find_fbconfig(psc->configs, pProp[1]);
1487          break;
1488       case GLX_RENDER_TYPE:
1489          renderType = pProp[1];
1490          break;
1491       }
1492
1493    if (mode == NULL)
1494       return NULL;
1495
1496    ctx = indirect_create_context(psc, mode, NULL, renderType);
1497    if (ctx == NULL)
1498       return NULL;
1499
1500    ctx->xid = contextID;
1501    ctx->imported = GL_TRUE;
1502    ctx->share_xid = share;
1503
1504    return (GLXContext) ctx;
1505 }
1506
1507 #endif
1508
1509 _X_EXPORT int
1510 glXQueryContext(Display * dpy, GLXContext ctx_user, int attribute, int *value)
1511 {
1512    struct glx_context *ctx = (struct glx_context *) ctx_user;
1513
1514    switch (attribute) {
1515       case GLX_SHARE_CONTEXT_EXT:
1516       *value = ctx->share_xid;
1517       break;
1518    case GLX_VISUAL_ID_EXT:
1519       *value = ctx->config ? ctx->config->visualID : None;
1520       break;
1521    case GLX_SCREEN:
1522       *value = ctx->screen;
1523       break;
1524    case GLX_FBCONFIG_ID:
1525       *value = ctx->config ? ctx->config->fbconfigID : None;
1526       break;
1527    case GLX_RENDER_TYPE:
1528       *value = ctx->renderType;
1529       break;
1530    default:
1531       return GLX_BAD_ATTRIBUTE;
1532    }
1533    return Success;
1534 }
1535
1536 _X_EXPORT
1537 GLX_ALIAS(int, glXQueryContextInfoEXT,
1538           (Display * dpy, GLXContext ctx, int attribute, int *value),
1539           (dpy, ctx, attribute, value), glXQueryContext)
1540
1541 _X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx_user)
1542 {
1543    struct glx_context *ctx = (struct glx_context *) ctx_user;
1544
1545    return ctx->xid;
1546 }
1547
1548 _X_EXPORT
1549 GLX_ALIAS_VOID(glXFreeContextEXT, (Display *dpy, GLXContext ctx), (dpy, ctx),
1550                glXDestroyContext);
1551
1552 _X_EXPORT GLXFBConfig *
1553 glXChooseFBConfig(Display * dpy, int screen,
1554                   const int *attribList, int *nitems)
1555 {
1556    struct glx_config **config_list;
1557    int list_size;
1558
1559
1560    config_list = (struct glx_config **)
1561       glXGetFBConfigs(dpy, screen, &list_size);
1562
1563    if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
1564       list_size = choose_visual(config_list, list_size, attribList, GL_TRUE);
1565       if (list_size == 0) {
1566          XFree(config_list);
1567          config_list = NULL;
1568       }
1569    }
1570
1571    *nitems = list_size;
1572    return (GLXFBConfig *) config_list;
1573 }
1574
1575
1576 _X_EXPORT GLXContext
1577 glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
1578                     int renderType, GLXContext shareList, Bool allowDirect)
1579 {
1580    struct glx_config *config = (struct glx_config *) fbconfig;
1581
1582    return CreateContext(dpy, config->fbconfigID, config, shareList,
1583                         allowDirect, X_GLXCreateNewContext, renderType,
1584                         config->screen);
1585 }
1586
1587
1588 _X_EXPORT GLXDrawable
1589 glXGetCurrentReadDrawable(void)
1590 {
1591    struct glx_context *gc = __glXGetCurrentContext();
1592
1593    return gc->currentReadable;
1594 }
1595
1596
1597 _X_EXPORT GLXFBConfig *
1598 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
1599 {
1600    struct glx_display *priv = __glXInitialize(dpy);
1601    struct glx_config **config_list = NULL;
1602    struct glx_config *config;
1603    unsigned num_configs = 0;
1604    int i;
1605
1606    *nelements = 0;
1607    if (priv && (priv->screens != NULL)
1608        && (screen >= 0) && (screen <= ScreenCount(dpy))
1609        && (priv->screens[screen]->configs != NULL)
1610        && (priv->screens[screen]->configs->fbconfigID
1611            != (int) GLX_DONT_CARE)) {
1612
1613       for (config = priv->screens[screen]->configs; config != NULL;
1614            config = config->next) {
1615          if (config->fbconfigID != (int) GLX_DONT_CARE) {
1616             num_configs++;
1617          }
1618       }
1619
1620       config_list = Xmalloc(num_configs * sizeof *config_list);
1621       if (config_list != NULL) {
1622          *nelements = num_configs;
1623          i = 0;
1624          for (config = priv->screens[screen]->configs; config != NULL;
1625               config = config->next) {
1626             if (config->fbconfigID != (int) GLX_DONT_CARE) {
1627                config_list[i] = config;
1628                i++;
1629             }
1630          }
1631       }
1632    }
1633
1634    return (GLXFBConfig *) config_list;
1635 }
1636
1637
1638 _X_EXPORT int
1639 glXGetFBConfigAttrib(Display * dpy, GLXFBConfig fbconfig,
1640                      int attribute, int *value)
1641 {
1642    struct glx_config *config = ValidateGLXFBConfig(dpy, fbconfig);
1643
1644    if (config == NULL)
1645       return GLXBadFBConfig;
1646
1647    return glx_config_get(config, attribute, value);
1648 }
1649
1650
1651 _X_EXPORT XVisualInfo *
1652 glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig fbconfig)
1653 {
1654    XVisualInfo visualTemplate;
1655    struct glx_config *config = (struct glx_config *) fbconfig;
1656    int count;
1657
1658    /*
1659     ** Get a list of all visuals, return if list is empty
1660     */
1661    visualTemplate.visualid = config->visualID;
1662    return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
1663 }
1664
1665 #ifndef GLX_USE_APPLEGL
1666 /*
1667 ** GLX_SGI_swap_control
1668 */
1669 static int
1670 __glXSwapIntervalSGI(int interval)
1671 {
1672    xGLXVendorPrivateReq *req;
1673    struct glx_context *gc = __glXGetCurrentContext();
1674    struct glx_screen *psc;
1675    Display *dpy;
1676    CARD32 *interval_ptr;
1677    CARD8 opcode;
1678
1679    if (gc == NULL) {
1680       return GLX_BAD_CONTEXT;
1681    }
1682
1683    if (interval <= 0) {
1684       return GLX_BAD_VALUE;
1685    }
1686
1687    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1688
1689 #ifdef GLX_DIRECT_RENDERING
1690    if (gc->isDirect && psc->driScreen && psc->driScreen->setSwapInterval) {
1691       __GLXDRIdrawable *pdraw =
1692          GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1693       psc->driScreen->setSwapInterval(pdraw, interval);
1694       return 0;
1695    }
1696 #endif
1697
1698    dpy = gc->currentDpy;
1699    opcode = __glXSetupForCommand(dpy);
1700    if (!opcode) {
1701       return 0;
1702    }
1703
1704    /* Send the glXSwapIntervalSGI request */
1705    LockDisplay(dpy);
1706    GetReqExtra(GLXVendorPrivate, sizeof(CARD32), req);
1707    req->reqType = opcode;
1708    req->glxCode = X_GLXVendorPrivate;
1709    req->vendorCode = X_GLXvop_SwapIntervalSGI;
1710    req->contextTag = gc->currentContextTag;
1711
1712    interval_ptr = (CARD32 *) (req + 1);
1713    *interval_ptr = interval;
1714
1715    UnlockDisplay(dpy);
1716    SyncHandle();
1717    XFlush(dpy);
1718
1719    return 0;
1720 }
1721
1722
1723 /*
1724 ** GLX_MESA_swap_control
1725 */
1726 static int
1727 __glXSwapIntervalMESA(unsigned int interval)
1728 {
1729 #ifdef GLX_DIRECT_RENDERING
1730    struct glx_context *gc = __glXGetCurrentContext();
1731
1732    if (gc != NULL && gc->isDirect) {
1733       struct glx_screen *psc;
1734
1735       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1736       if (psc->driScreen && psc->driScreen->setSwapInterval) {
1737          __GLXDRIdrawable *pdraw =
1738             GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1739          return psc->driScreen->setSwapInterval(pdraw, interval);
1740       }
1741    }
1742 #endif
1743
1744    return GLX_BAD_CONTEXT;
1745 }
1746
1747
1748 static int
1749 __glXGetSwapIntervalMESA(void)
1750 {
1751 #ifdef GLX_DIRECT_RENDERING
1752    struct glx_context *gc = __glXGetCurrentContext();
1753
1754    if (gc != NULL && gc->isDirect) {
1755       struct glx_screen *psc;
1756
1757       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1758       if (psc->driScreen && psc->driScreen->getSwapInterval) {
1759          __GLXDRIdrawable *pdraw =
1760             GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1761          return psc->driScreen->getSwapInterval(pdraw);
1762       }
1763    }
1764 #endif
1765
1766    return 0;
1767 }
1768
1769
1770 /*
1771 ** GLX_SGI_video_sync
1772 */
1773 static int
1774 __glXGetVideoSyncSGI(unsigned int *count)
1775 {
1776    int64_t ust, msc, sbc;
1777    int ret;
1778    struct glx_context *gc = __glXGetCurrentContext();
1779    struct glx_screen *psc;
1780 #ifdef GLX_DIRECT_RENDERING
1781    __GLXDRIdrawable *pdraw;
1782 #endif
1783
1784    if (!gc)
1785       return GLX_BAD_CONTEXT;
1786
1787 #ifdef GLX_DIRECT_RENDERING
1788    if (!gc->isDirect)
1789       return GLX_BAD_CONTEXT;
1790 #endif
1791
1792    psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
1793 #ifdef GLX_DIRECT_RENDERING
1794    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1795 #endif
1796
1797    /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1798     * FIXME: there should be a GLX encoding for this call.  I can find no
1799     * FIXME: documentation for the GLX encoding.
1800     */
1801 #ifdef GLX_DIRECT_RENDERING
1802    if (psc->driScreen && psc->driScreen->getDrawableMSC) {
1803       ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
1804       *count = (unsigned) msc;
1805       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1806    }
1807 #endif
1808
1809    return GLX_BAD_CONTEXT;
1810 }
1811
1812 static int
1813 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1814 {
1815    struct glx_context *gc = __glXGetCurrentContext();
1816    struct glx_screen *psc;
1817 #ifdef GLX_DIRECT_RENDERING
1818    __GLXDRIdrawable *pdraw;
1819 #endif
1820    int64_t ust, msc, sbc;
1821    int ret;
1822
1823    if (divisor <= 0 || remainder < 0)
1824       return GLX_BAD_VALUE;
1825
1826    if (!gc)
1827       return GLX_BAD_CONTEXT;
1828
1829 #ifdef GLX_DIRECT_RENDERING
1830    if (!gc->isDirect)
1831       return GLX_BAD_CONTEXT;
1832 #endif
1833
1834    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1835 #ifdef GLX_DIRECT_RENDERING
1836    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1837 #endif
1838
1839 #ifdef GLX_DIRECT_RENDERING
1840    if (psc->driScreen && psc->driScreen->waitForMSC) {
1841       ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
1842                                        &sbc);
1843       *count = (unsigned) msc;
1844       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1845    }
1846 #endif
1847
1848    return GLX_BAD_CONTEXT;
1849 }
1850
1851 #endif /* GLX_USE_APPLEGL */
1852
1853 /*
1854 ** GLX_SGIX_fbconfig
1855 ** Many of these functions are aliased to GLX 1.3 entry points in the 
1856 ** GLX_functions table.
1857 */
1858
1859 _X_EXPORT
1860 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
1861           (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
1862           (dpy, config, attribute, value), glXGetFBConfigAttrib)
1863
1864 _X_EXPORT GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
1865                  (Display * dpy, int screen, int *attrib_list,
1866                   int *nelements), (dpy, screen, attrib_list, nelements),
1867                  glXChooseFBConfig)
1868
1869 _X_EXPORT GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
1870                  (Display * dpy, GLXFBConfigSGIX config),
1871                  (dpy, config), glXGetVisualFromFBConfig)
1872
1873 _X_EXPORT GLXPixmap
1874 glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
1875                                  GLXFBConfigSGIX fbconfig,
1876                                  Pixmap pixmap)
1877 {
1878 #ifndef GLX_USE_APPLEGL
1879    xGLXVendorPrivateWithReplyReq *vpreq;
1880    xGLXCreateGLXPixmapWithConfigSGIXReq *req;
1881    GLXPixmap xid = None;
1882    CARD8 opcode;
1883    struct glx_screen *psc;
1884 #endif
1885    struct glx_config *config = (struct glx_config *) fbconfig;
1886
1887
1888    if ((dpy == NULL) || (config == NULL)) {
1889       return None;
1890    }
1891 #ifdef GLX_USE_APPLEGL
1892    if(apple_glx_pixmap_create(dpy, config->screen, pixmap, config))
1893       return None;
1894    return pixmap;
1895 #else
1896
1897    psc = GetGLXScreenConfigs(dpy, config->screen);
1898    if ((psc != NULL)
1899        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1900       opcode = __glXSetupForCommand(dpy);
1901       if (!opcode) {
1902          return None;
1903       }
1904
1905       /* Send the glXCreateGLXPixmapWithConfigSGIX request */
1906       LockDisplay(dpy);
1907       GetReqExtra(GLXVendorPrivateWithReply,
1908                   sz_xGLXCreateGLXPixmapWithConfigSGIXReq -
1909                   sz_xGLXVendorPrivateWithReplyReq, vpreq);
1910       req = (xGLXCreateGLXPixmapWithConfigSGIXReq *) vpreq;
1911       req->reqType = opcode;
1912       req->glxCode = X_GLXVendorPrivateWithReply;
1913       req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
1914       req->screen = config->screen;
1915       req->fbconfig = config->fbconfigID;
1916       req->pixmap = pixmap;
1917       req->glxpixmap = xid = XAllocID(dpy);
1918       UnlockDisplay(dpy);
1919       SyncHandle();
1920    }
1921
1922    return xid;
1923 #endif
1924 }
1925
1926 _X_EXPORT GLXContext
1927 glXCreateContextWithConfigSGIX(Display * dpy,
1928                                GLXFBConfigSGIX fbconfig, int renderType,
1929                                GLXContext shareList, Bool allowDirect)
1930 {
1931    GLXContext gc = NULL;
1932    struct glx_config *config = (struct glx_config *) fbconfig;
1933    struct glx_screen *psc;
1934
1935
1936    if ((dpy == NULL) || (config == NULL)) {
1937       return None;
1938    }
1939
1940    psc = GetGLXScreenConfigs(dpy, config->screen);
1941    if ((psc != NULL)
1942        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1943       gc = CreateContext(dpy, config->fbconfigID, config, shareList,
1944                          allowDirect,
1945                          X_GLXvop_CreateContextWithConfigSGIX, renderType,
1946                          config->screen);
1947    }
1948
1949    return gc;
1950 }
1951
1952
1953 _X_EXPORT GLXFBConfigSGIX
1954 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
1955 {
1956    struct glx_display *priv;
1957    struct glx_screen *psc = NULL;
1958
1959    if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) == Success)
1960        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
1961        && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
1962       return (GLXFBConfigSGIX) glx_config_find_visual(psc->configs,
1963                                                       vis->visualid);
1964    }
1965
1966    return NULL;
1967 }
1968
1969 #ifndef GLX_USE_APPLEGL
1970 /*
1971 ** GLX_SGIX_swap_group
1972 */
1973 static void
1974 __glXJoinSwapGroupSGIX(Display * dpy, GLXDrawable drawable,
1975                        GLXDrawable member)
1976 {
1977    (void) dpy;
1978    (void) drawable;
1979    (void) member;
1980 }
1981
1982
1983 /*
1984 ** GLX_SGIX_swap_barrier
1985 */
1986 static void
1987 __glXBindSwapBarrierSGIX(Display * dpy, GLXDrawable drawable, int barrier)
1988 {
1989    (void) dpy;
1990    (void) drawable;
1991    (void) barrier;
1992 }
1993
1994 static Bool
1995 __glXQueryMaxSwapBarriersSGIX(Display * dpy, int screen, int *max)
1996 {
1997    (void) dpy;
1998    (void) screen;
1999    (void) max;
2000    return False;
2001 }
2002
2003
2004 /*
2005 ** GLX_OML_sync_control
2006 */
2007 static Bool
2008 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
2009                       int64_t * ust, int64_t * msc, int64_t * sbc)
2010 {
2011    struct glx_display * const priv = __glXInitialize(dpy);
2012    int ret;
2013 #ifdef GLX_DIRECT_RENDERING
2014    __GLXDRIdrawable *pdraw;
2015 #endif
2016    struct glx_screen *psc;
2017
2018    if (!priv)
2019       return False;
2020
2021 #ifdef GLX_DIRECT_RENDERING
2022    pdraw = GetGLXDRIDrawable(dpy, drawable);
2023    psc = pdraw ? pdraw->psc : NULL;
2024    if (pdraw && psc->driScreen->getDrawableMSC) {
2025       ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
2026       return ret;
2027    }
2028 #endif
2029
2030    return False;
2031 }
2032
2033 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2034 _X_HIDDEN GLboolean
2035 __glxGetMscRate(__GLXDRIdrawable *glxDraw,
2036                 int32_t * numerator, int32_t * denominator)
2037 {
2038 #ifdef XF86VIDMODE
2039    struct glx_screen *psc;
2040    XF86VidModeModeLine mode_line;
2041    int dot_clock;
2042    int i;
2043
2044    psc = glxDraw->psc;
2045    if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
2046        XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
2047       unsigned n = dot_clock * 1000;
2048       unsigned d = mode_line.vtotal * mode_line.htotal;
2049
2050 # define V_INTERLACE 0x010
2051 # define V_DBLSCAN   0x020
2052
2053       if (mode_line.flags & V_INTERLACE)
2054          n *= 2;
2055       else if (mode_line.flags & V_DBLSCAN)
2056          d *= 2;
2057
2058       /* The OML_sync_control spec requires that if the refresh rate is a
2059        * whole number, that the returned numerator be equal to the refresh
2060        * rate and the denominator be 1.
2061        */
2062
2063       if (n % d == 0) {
2064          n /= d;
2065          d = 1;
2066       }
2067       else {
2068          static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2069
2070          /* This is a poor man's way to reduce a fraction.  It's far from
2071           * perfect, but it will work well enough for this situation.
2072           */
2073
2074          for (i = 0; f[i] != 0; i++) {
2075             while (n % f[i] == 0 && d % f[i] == 0) {
2076                d /= f[i];
2077                n /= f[i];
2078             }
2079          }
2080       }
2081
2082       *numerator = n;
2083       *denominator = d;
2084
2085       return True;
2086    }
2087    else
2088 #endif
2089
2090    return False;
2091 }
2092 #endif
2093
2094 /**
2095  * Determine the refresh rate of the specified drawable and display.
2096  *
2097  * \param dpy          Display whose refresh rate is to be determined.
2098  * \param drawable     Drawable whose refresh rate is to be determined.
2099  * \param numerator    Numerator of the refresh rate.
2100  * \param demoninator  Denominator of the refresh rate.
2101  * \return  If the refresh rate for the specified display and drawable could
2102  *          be calculated, True is returned.  Otherwise False is returned.
2103  *
2104  * \note This function is implemented entirely client-side.  A lot of other
2105  *       functionality is required to export GLX_OML_sync_control, so on
2106  *       XFree86 this function can be called for direct-rendering contexts
2107  *       when GLX_OML_sync_control appears in the client extension string.
2108  */
2109
2110 _X_HIDDEN GLboolean
2111 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
2112                    int32_t * numerator, int32_t * denominator)
2113 {
2114 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2115    __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
2116
2117    if (draw == NULL)
2118       return False;
2119
2120    return __glxGetMscRate(draw, numerator, denominator);
2121 #else
2122    (void) dpy;
2123    (void) drawable;
2124    (void) numerator;
2125    (void) denominator;
2126 #endif
2127    return False;
2128 }
2129
2130
2131 static int64_t
2132 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
2133                        int64_t target_msc, int64_t divisor, int64_t remainder)
2134 {
2135    struct glx_context *gc = __glXGetCurrentContext();
2136 #ifdef GLX_DIRECT_RENDERING
2137    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2138    struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2139 #endif
2140
2141    if (!gc) /* no GLX for this */
2142       return -1;
2143
2144 #ifdef GLX_DIRECT_RENDERING
2145    if (!pdraw || !gc->isDirect)
2146       return -1;
2147 #endif
2148
2149    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2150     * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2151     * of -1 if the function failed because of errors detected in the input
2152     * parameters"
2153     */
2154    if (divisor < 0 || remainder < 0 || target_msc < 0)
2155       return -1;
2156    if (divisor > 0 && remainder >= divisor)
2157       return -1;
2158
2159    if (target_msc == 0 && divisor == 0 && remainder == 0)
2160       remainder = 1;
2161
2162 #ifdef GLX_DIRECT_RENDERING
2163    if (psc->driScreen && psc->driScreen->swapBuffers)
2164       return (*psc->driScreen->swapBuffers)(pdraw, target_msc, divisor,
2165                                             remainder);
2166 #endif
2167
2168    return -1;
2169 }
2170
2171
2172 static Bool
2173 __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2174                    int64_t target_msc, int64_t divisor,
2175                    int64_t remainder, int64_t * ust,
2176                    int64_t * msc, int64_t * sbc)
2177 {
2178 #ifdef GLX_DIRECT_RENDERING
2179    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2180    struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2181    int ret;
2182 #endif
2183
2184
2185    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2186     * error", but the return type in the spec is Bool.
2187     */
2188    if (divisor < 0 || remainder < 0 || target_msc < 0)
2189       return False;
2190    if (divisor > 0 && remainder >= divisor)
2191       return False;
2192
2193 #ifdef GLX_DIRECT_RENDERING
2194    if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
2195       ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
2196                                        ust, msc, sbc);
2197       return ret;
2198    }
2199 #endif
2200
2201    return False;
2202 }
2203
2204
2205 static Bool
2206 __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2207                    int64_t target_sbc, int64_t * ust,
2208                    int64_t * msc, int64_t * sbc)
2209 {
2210 #ifdef GLX_DIRECT_RENDERING
2211    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2212    struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2213    int ret;
2214 #endif
2215
2216    /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2217     * error", but the return type in the spec is Bool.
2218     */
2219    if (target_sbc < 0)
2220       return False;
2221
2222 #ifdef GLX_DIRECT_RENDERING
2223    if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
2224       ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
2225       return ret;
2226    }
2227 #endif
2228
2229    return False;
2230 }
2231
2232 /*@}*/
2233
2234
2235 /**
2236  * Mesa extension stubs.  These will help reduce portability problems.
2237  */
2238 /*@{*/
2239
2240 /**
2241  * Release all buffers associated with the specified GLX drawable.
2242  *
2243  * \todo
2244  * This function was intended for stand-alone Mesa.  The issue there is that
2245  * the library doesn't get any notification when a window is closed.  In
2246  * DRI there is a similar but slightly different issue.  When GLX 1.3 is
2247  * supported, there are 3 different functions to destroy a drawable.  It
2248  * should be possible to create GLX protocol (or have it determine which
2249  * protocol to use based on the type of the drawable) to have one function
2250  * do the work of 3.  For the direct-rendering case, this function could
2251  * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2252  * This would reduce the frequency with which \c __driGarbageCollectDrawables
2253  * would need to be used.  This really should be done as part of the new DRI
2254  * interface work.
2255  *
2256  * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2257  *     __driGarbageCollectDrawables
2258  *     glXDestroyGLXPixmap
2259  *     glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2260  *     glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2261  */
2262 static Bool
2263 __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
2264 {
2265    (void) dpy;
2266    (void) d;
2267    return False;
2268 }
2269
2270
2271 _X_EXPORT GLXPixmap
2272 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
2273                        Pixmap pixmap, Colormap cmap)
2274 {
2275    (void) dpy;
2276    (void) visual;
2277    (void) pixmap;
2278    (void) cmap;
2279    return 0;
2280 }
2281
2282 /*@}*/
2283
2284
2285 /**
2286  * GLX_MESA_copy_sub_buffer
2287  */
2288 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2289 static void
2290 __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
2291                        int x, int y, int width, int height)
2292 {
2293    xGLXVendorPrivateReq *req;
2294    struct glx_context *gc;
2295    GLXContextTag tag;
2296    CARD32 *drawable_ptr;
2297    INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2298    CARD8 opcode;
2299
2300 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2301    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2302    if (pdraw != NULL) {
2303       struct glx_screen *psc = pdraw->psc;
2304       if (psc->driScreen->copySubBuffer != NULL) {
2305          glFlush();
2306          (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height);
2307       }
2308
2309       return;
2310    }
2311 #endif
2312
2313    opcode = __glXSetupForCommand(dpy);
2314    if (!opcode)
2315       return;
2316
2317    /*
2318     ** The calling thread may or may not have a current context.  If it
2319     ** does, send the context tag so the server can do a flush.
2320     */
2321    gc = __glXGetCurrentContext();
2322    if ((gc != NULL) && (dpy == gc->currentDpy) &&
2323        ((drawable == gc->currentDrawable) ||
2324         (drawable == gc->currentReadable))) {
2325       tag = gc->currentContextTag;
2326    }
2327    else {
2328       tag = 0;
2329    }
2330
2331    LockDisplay(dpy);
2332    GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4, req);
2333    req->reqType = opcode;
2334    req->glxCode = X_GLXVendorPrivate;
2335    req->vendorCode = X_GLXvop_CopySubBufferMESA;
2336    req->contextTag = tag;
2337
2338    drawable_ptr = (CARD32 *) (req + 1);
2339    x_ptr = (INT32 *) (drawable_ptr + 1);
2340    y_ptr = (INT32 *) (drawable_ptr + 2);
2341    w_ptr = (INT32 *) (drawable_ptr + 3);
2342    h_ptr = (INT32 *) (drawable_ptr + 4);
2343
2344    *drawable_ptr = drawable;
2345    *x_ptr = x;
2346    *y_ptr = y;
2347    *w_ptr = width;
2348    *h_ptr = height;
2349
2350    UnlockDisplay(dpy);
2351    SyncHandle();
2352 }
2353
2354 /*@{*/
2355 static void
2356 __glXBindTexImageEXT(Display * dpy,
2357                      GLXDrawable drawable, int buffer, const int *attrib_list)
2358 {
2359    struct glx_context *gc = __glXGetCurrentContext();
2360
2361    if (gc == NULL || gc->vtable->bind_tex_image == NULL)
2362       return;
2363
2364    gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
2365 }
2366
2367 static void
2368 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
2369 {
2370    struct glx_context *gc = __glXGetCurrentContext();
2371
2372    if (gc == NULL || gc->vtable->release_tex_image == NULL)
2373       return;
2374
2375    gc->vtable->release_tex_image(dpy, drawable, buffer);
2376 }
2377
2378 /*@}*/
2379
2380 #endif /* GLX_USE_APPLEGL */
2381
2382 /**
2383  * \c strdup is actually not a standard ANSI C or POSIX routine.
2384  * Irix will not define it if ANSI mode is in effect.
2385  *
2386  * \sa strdup
2387  */
2388 _X_HIDDEN char *
2389 __glXstrdup(const char *str)
2390 {
2391    char *copy;
2392    copy = (char *) Xmalloc(strlen(str) + 1);
2393    if (!copy)
2394       return NULL;
2395    strcpy(copy, str);
2396    return copy;
2397 }
2398
2399 /*
2400 ** glXGetProcAddress support
2401 */
2402
2403 struct name_address_pair
2404 {
2405    const char *Name;
2406    GLvoid *Address;
2407 };
2408
2409 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2410 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2411
2412 static const struct name_address_pair GLX_functions[] = {
2413    /*** GLX_VERSION_1_0 ***/
2414    GLX_FUNCTION(glXChooseVisual),
2415    GLX_FUNCTION(glXCopyContext),
2416    GLX_FUNCTION(glXCreateContext),
2417    GLX_FUNCTION(glXCreateGLXPixmap),
2418    GLX_FUNCTION(glXDestroyContext),
2419    GLX_FUNCTION(glXDestroyGLXPixmap),
2420    GLX_FUNCTION(glXGetConfig),
2421    GLX_FUNCTION(glXGetCurrentContext),
2422    GLX_FUNCTION(glXGetCurrentDrawable),
2423    GLX_FUNCTION(glXIsDirect),
2424    GLX_FUNCTION(glXMakeCurrent),
2425    GLX_FUNCTION(glXQueryExtension),
2426    GLX_FUNCTION(glXQueryVersion),
2427    GLX_FUNCTION(glXSwapBuffers),
2428    GLX_FUNCTION(glXUseXFont),
2429    GLX_FUNCTION(glXWaitGL),
2430    GLX_FUNCTION(glXWaitX),
2431
2432    /*** GLX_VERSION_1_1 ***/
2433    GLX_FUNCTION(glXGetClientString),
2434    GLX_FUNCTION(glXQueryExtensionsString),
2435    GLX_FUNCTION(glXQueryServerString),
2436
2437    /*** GLX_VERSION_1_2 ***/
2438    GLX_FUNCTION(glXGetCurrentDisplay),
2439
2440    /*** GLX_VERSION_1_3 ***/
2441    GLX_FUNCTION(glXChooseFBConfig),
2442    GLX_FUNCTION(glXCreateNewContext),
2443    GLX_FUNCTION(glXCreatePbuffer),
2444    GLX_FUNCTION(glXCreatePixmap),
2445    GLX_FUNCTION(glXCreateWindow),
2446    GLX_FUNCTION(glXDestroyPbuffer),
2447    GLX_FUNCTION(glXDestroyPixmap),
2448    GLX_FUNCTION(glXDestroyWindow),
2449    GLX_FUNCTION(glXGetCurrentReadDrawable),
2450    GLX_FUNCTION(glXGetFBConfigAttrib),
2451    GLX_FUNCTION(glXGetFBConfigs),
2452    GLX_FUNCTION(glXGetSelectedEvent),
2453    GLX_FUNCTION(glXGetVisualFromFBConfig),
2454    GLX_FUNCTION(glXMakeContextCurrent),
2455    GLX_FUNCTION(glXQueryContext),
2456    GLX_FUNCTION(glXQueryDrawable),
2457    GLX_FUNCTION(glXSelectEvent),
2458
2459 #ifndef GLX_USE_APPLEGL
2460    /*** GLX_SGI_swap_control ***/
2461    GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
2462
2463    /*** GLX_SGI_video_sync ***/
2464    GLX_FUNCTION2(glXGetVideoSyncSGI, __glXGetVideoSyncSGI),
2465    GLX_FUNCTION2(glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI),
2466
2467    /*** GLX_SGI_make_current_read ***/
2468    GLX_FUNCTION2(glXMakeCurrentReadSGI, glXMakeContextCurrent),
2469    GLX_FUNCTION2(glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable),
2470
2471    /*** GLX_EXT_import_context ***/
2472    GLX_FUNCTION(glXFreeContextEXT),
2473    GLX_FUNCTION(glXGetContextIDEXT),
2474    GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
2475    GLX_FUNCTION(glXImportContextEXT),
2476    GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
2477 #endif
2478
2479    /*** GLX_SGIX_fbconfig ***/
2480    GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
2481    GLX_FUNCTION2(glXChooseFBConfigSGIX, glXChooseFBConfig),
2482    GLX_FUNCTION(glXCreateGLXPixmapWithConfigSGIX),
2483    GLX_FUNCTION(glXCreateContextWithConfigSGIX),
2484    GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
2485    GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
2486
2487 #ifndef GLX_USE_APPLEGL
2488    /*** GLX_SGIX_pbuffer ***/
2489    GLX_FUNCTION(glXCreateGLXPbufferSGIX),
2490    GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
2491    GLX_FUNCTION(glXQueryGLXPbufferSGIX),
2492    GLX_FUNCTION(glXSelectEventSGIX),
2493    GLX_FUNCTION(glXGetSelectedEventSGIX),
2494
2495    /*** GLX_SGIX_swap_group ***/
2496    GLX_FUNCTION2(glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX),
2497
2498    /*** GLX_SGIX_swap_barrier ***/
2499    GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
2500    GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
2501
2502    /*** GLX_MESA_copy_sub_buffer ***/
2503    GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
2504
2505    /*** GLX_MESA_pixmap_colormap ***/
2506    GLX_FUNCTION(glXCreateGLXPixmapMESA),
2507
2508    /*** GLX_MESA_release_buffers ***/
2509    GLX_FUNCTION2(glXReleaseBuffersMESA, __glXReleaseBuffersMESA),
2510
2511    /*** GLX_MESA_swap_control ***/
2512    GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
2513    GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
2514 #endif
2515
2516    /*** GLX_ARB_get_proc_address ***/
2517    GLX_FUNCTION(glXGetProcAddressARB),
2518
2519    /*** GLX 1.4 ***/
2520    GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
2521
2522 #ifndef GLX_USE_APPLEGL
2523    /*** GLX_OML_sync_control ***/
2524    GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
2525    GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
2526    GLX_FUNCTION2(glXSwapBuffersMscOML, __glXSwapBuffersMscOML),
2527    GLX_FUNCTION2(glXGetMscRateOML, __glXGetMscRateOML),
2528    GLX_FUNCTION2(glXGetSyncValuesOML, __glXGetSyncValuesOML),
2529
2530    /*** GLX_EXT_texture_from_pixmap ***/
2531    GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
2532    GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
2533 #endif
2534
2535 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2536    /*** DRI configuration ***/
2537    GLX_FUNCTION(glXGetScreenDriver),
2538    GLX_FUNCTION(glXGetDriverConfig),
2539 #endif
2540
2541    {NULL, NULL}                 /* end of list */
2542 };
2543
2544 static const GLvoid *
2545 get_glx_proc_address(const char *funcName)
2546 {
2547    GLuint i;
2548
2549    /* try static functions */
2550    for (i = 0; GLX_functions[i].Name; i++) {
2551       if (strcmp(GLX_functions[i].Name, funcName) == 0)
2552          return GLX_functions[i].Address;
2553    }
2554
2555    return NULL;
2556 }
2557
2558 /**
2559  * Get the address of a named GL function.  This is the pre-GLX 1.4 name for
2560  * \c glXGetProcAddress.
2561  *
2562  * \param procName  Name of a GL or GLX function.
2563  * \returns         A pointer to the named function
2564  *
2565  * \sa glXGetProcAddress
2566  */
2567 _X_EXPORT void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
2568 {
2569    typedef void (*gl_function) (void);
2570    gl_function f;
2571
2572
2573    /* Search the table of GLX and internal functions first.  If that
2574     * fails and the supplied name could be a valid core GL name, try
2575     * searching the core GL function table.  This check is done to prevent
2576     * DRI based drivers from searching the core GL function table for
2577     * internal API functions.
2578     */
2579    f = (gl_function) get_glx_proc_address((const char *) procName);
2580    if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2581        && (procName[2] != 'X')) {
2582 #ifdef GLX_SHARED_GLAPI
2583       f = (gl_function) __indirect_get_proc_address((const char *) procName);
2584 #endif
2585       if (!f)
2586          f = (gl_function) _glapi_get_proc_address((const char *) procName);
2587       if (!f) {
2588          struct glx_context *gc = __glXGetCurrentContext();
2589       
2590          if (gc != NULL && gc->vtable->get_proc_address != NULL)
2591             f = gc->vtable->get_proc_address((const char *) procName);
2592       }
2593    }
2594    return f;
2595 }
2596
2597 /**
2598  * Get the address of a named GL function.  This is the GLX 1.4 name for
2599  * \c glXGetProcAddressARB.
2600  *
2601  * \param procName  Name of a GL or GLX function.
2602  * \returns         A pointer to the named function
2603  *
2604  * \sa glXGetProcAddressARB
2605  */
2606 _X_EXPORT void (*glXGetProcAddress(const GLubyte * procName)) (void)
2607 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
2608    __attribute__ ((alias("glXGetProcAddressARB")));
2609 #else
2610 {
2611    return glXGetProcAddressARB(procName);
2612 }
2613 #endif /* __GNUC__ */
2614
2615
2616 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2617 /**
2618  * Get the unadjusted system time (UST).  Currently, the UST is measured in
2619  * microseconds since Epoc.  The actual resolution of the UST may vary from
2620  * system to system, and the units may vary from release to release.
2621  * Drivers should not call this function directly.  They should instead use
2622  * \c glXGetProcAddress to obtain a pointer to the function.
2623  *
2624  * \param ust Location to store the 64-bit UST
2625  * \returns Zero on success or a negative errno value on failure.
2626  *
2627  * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2628  *
2629  * \since Internal API version 20030317.
2630  */
2631 _X_HIDDEN int
2632 __glXGetUST(int64_t * ust)
2633 {
2634    struct timeval tv;
2635
2636    if (ust == NULL) {
2637       return -EFAULT;
2638    }
2639
2640    if (gettimeofday(&tv, NULL) == 0) {
2641       ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2642       return 0;
2643    }
2644    else {
2645       return -errno;
2646    }
2647 }
2648 #endif /* GLX_DIRECT_RENDERING */