OSDN Git Service

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