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