glxapi.c revision c1f859d4
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * 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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26/*
27 * This is the GLX API dispatcher.  Calls to the glX* functions are
28 * either routed to the real GLX encoders or to Mesa's pseudo-GLX functions.
29 * See the glxapi.h file for more details.
30 */
31
32
33#include <assert.h>
34#include <stdlib.h>
35#include <stdio.h>
36#include <string.h>
37#include "main/glheader.h"
38#include "glapi/glapi.h"
39#include "glxapi.h"
40
41
42extern struct _glxapi_table *_real_GetGLXDispatchTable(void);
43extern struct _glxapi_table *_mesa_GetGLXDispatchTable(void);
44
45
46struct display_dispatch {
47   Display *Dpy;
48   struct _glxapi_table *Table;
49   struct display_dispatch *Next;
50};
51
52static struct display_dispatch *DispatchList = NULL;
53
54
55/* Display -> Dispatch caching */
56static Display *prevDisplay = NULL;
57static struct _glxapi_table *prevTable = NULL;
58
59
60static struct _glxapi_table *
61get_dispatch(Display *dpy)
62{
63   if (!dpy)
64      return NULL;
65
66   /* search list of display/dispatch pairs for this display */
67   {
68      const struct display_dispatch *d = DispatchList;
69      while (d) {
70         if (d->Dpy == dpy) {
71            prevDisplay = dpy;
72            prevTable = d->Table;
73            return d->Table;  /* done! */
74         }
75         d = d->Next;
76      }
77   }
78
79   /* A new display, determine if we should use real GLX
80    * or Mesa's pseudo-GLX.
81    */
82   {
83      struct _glxapi_table *t = _mesa_GetGLXDispatchTable();
84
85      if (t) {
86         struct display_dispatch *d;
87         d = (struct display_dispatch *) malloc(sizeof(struct display_dispatch));
88         if (d) {
89            d->Dpy = dpy;
90            d->Table = t;
91            /* insert at head of list */
92            d->Next = DispatchList;
93            DispatchList = d;
94            /* update cache */
95            prevDisplay = dpy;
96            prevTable = t;
97            return t;
98         }
99      }
100   }
101
102   /* If we get here that means we can't use real GLX on this display
103    * and the Mesa pseudo-GLX software renderer wasn't compiled in.
104    * Or, we ran out of memory!
105    */
106   return NULL;
107}
108
109
110/* Don't use the GET_DISPATCH defined in glthread.h */
111#undef GET_DISPATCH
112
113#define GET_DISPATCH(DPY, TABLE)	\
114   if (DPY == prevDisplay) {		\
115      TABLE = prevTable;		\
116   }					\
117   else if (!DPY) {			\
118      TABLE = NULL;			\
119   }					\
120   else {				\
121      TABLE = get_dispatch(DPY);	\
122   }
123
124
125
126
127/**
128 * GLX API current context.
129 */
130#if defined(GLX_USE_TLS)
131PUBLIC __thread void * CurrentContext
132    __attribute__((tls_model("initial-exec")));
133#elif defined(THREADS)
134static _glthread_TSD ContextTSD;         /**< Per-thread context pointer */
135#else
136static GLXContext CurrentContext = 0;
137#endif
138
139
140static void
141SetCurrentContext(GLXContext c)
142{
143#if defined(GLX_USE_TLS)
144   CurrentContext = c;
145#elif defined(THREADS)
146   _glthread_SetTSD(&ContextTSD, c);
147#else
148   CurrentContext = c;
149#endif
150}
151
152
153/*
154 * GLX API entrypoints
155 */
156
157/*** GLX_VERSION_1_0 ***/
158
159XVisualInfo PUBLIC *
160glXChooseVisual(Display *dpy, int screen, int *list)
161{
162   struct _glxapi_table *t;
163   GET_DISPATCH(dpy, t);
164   if (!t)
165      return NULL;
166   return (t->ChooseVisual)(dpy, screen, list);
167}
168
169
170void PUBLIC
171glXCopyContext(Display *dpy, GLXContext src, GLXContext dst, unsigned long mask)
172{
173   struct _glxapi_table *t;
174   GET_DISPATCH(dpy, t);
175   if (!t)
176      return;
177   (t->CopyContext)(dpy, src, dst, mask);
178}
179
180
181GLXContext PUBLIC
182glXCreateContext(Display *dpy, XVisualInfo *visinfo, GLXContext shareList, Bool direct)
183{
184   struct _glxapi_table *t;
185   GET_DISPATCH(dpy, t);
186   if (!t)
187      return 0;
188   return (t->CreateContext)(dpy, visinfo, shareList, direct);
189}
190
191
192GLXPixmap PUBLIC
193glXCreateGLXPixmap(Display *dpy, XVisualInfo *visinfo, Pixmap pixmap)
194{
195   struct _glxapi_table *t;
196   GET_DISPATCH(dpy, t);
197   if (!t)
198      return 0;
199   return (t->CreateGLXPixmap)(dpy, visinfo, pixmap);
200}
201
202
203void PUBLIC
204glXDestroyContext(Display *dpy, GLXContext ctx)
205{
206   struct _glxapi_table *t;
207   GET_DISPATCH(dpy, t);
208   if (!t)
209      return;
210   if (glXGetCurrentContext() == ctx)
211      SetCurrentContext(NULL);
212   (t->DestroyContext)(dpy, ctx);
213}
214
215
216void PUBLIC
217glXDestroyGLXPixmap(Display *dpy, GLXPixmap pixmap)
218{
219   struct _glxapi_table *t;
220   GET_DISPATCH(dpy, t);
221   if (!t)
222      return;
223   (t->DestroyGLXPixmap)(dpy, pixmap);
224}
225
226
227int PUBLIC
228glXGetConfig(Display *dpy, XVisualInfo *visinfo, int attrib, int *value)
229{
230   struct _glxapi_table *t;
231   GET_DISPATCH(dpy, t);
232   if (!t)
233      return GLX_NO_EXTENSION;
234   return (t->GetConfig)(dpy, visinfo, attrib, value);
235}
236
237
238GLXContext PUBLIC
239glXGetCurrentContext(void)
240{
241#if defined(GLX_USE_TLS)
242   return CurrentContext;
243#elif defined(THREADS)
244   return (GLXContext) _glthread_GetTSD(&ContextTSD);
245#else
246   return CurrentContext;
247#endif
248}
249
250
251GLXDrawable PUBLIC
252glXGetCurrentDrawable(void)
253{
254   __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext();
255   return gc ? gc->currentDrawable : 0;
256}
257
258
259Bool PUBLIC
260glXIsDirect(Display *dpy, GLXContext ctx)
261{
262   struct _glxapi_table *t;
263   GET_DISPATCH(dpy, t);
264   if (!t)
265      return False;
266   return (t->IsDirect)(dpy, ctx);
267}
268
269
270Bool PUBLIC
271glXMakeCurrent(Display *dpy, GLXDrawable drawable, GLXContext ctx)
272{
273   Bool b;
274   struct _glxapi_table *t;
275   GET_DISPATCH(dpy, t);
276   if (!t) {
277      return False;
278   }
279   b = (*t->MakeCurrent)(dpy, drawable, ctx);
280   if (b) {
281      SetCurrentContext(ctx);
282   }
283   return b;
284}
285
286
287Bool PUBLIC
288glXQueryExtension(Display *dpy, int *errorb, int *event)
289{
290   struct _glxapi_table *t;
291   GET_DISPATCH(dpy, t);
292   if (!t)
293      return False;
294   return (t->QueryExtension)(dpy, errorb, event);
295}
296
297
298Bool PUBLIC
299glXQueryVersion(Display *dpy, int *maj, int *min)
300{
301   struct _glxapi_table *t;
302   GET_DISPATCH(dpy, t);
303   if (!t)
304      return False;
305   return (t->QueryVersion)(dpy, maj, min);
306}
307
308
309void PUBLIC
310glXSwapBuffers(Display *dpy, GLXDrawable drawable)
311{
312   struct _glxapi_table *t;
313   GET_DISPATCH(dpy, t);
314   if (!t)
315      return;
316   (t->SwapBuffers)(dpy, drawable);
317}
318
319
320void PUBLIC
321glXUseXFont(Font font, int first, int count, int listBase)
322{
323   struct _glxapi_table *t;
324   Display *dpy = glXGetCurrentDisplay();
325   GET_DISPATCH(dpy, t);
326   if (!t)
327      return;
328   (t->UseXFont)(font, first, count, listBase);
329}
330
331
332void PUBLIC
333glXWaitGL(void)
334{
335   struct _glxapi_table *t;
336   Display *dpy = glXGetCurrentDisplay();
337   GET_DISPATCH(dpy, t);
338   if (!t)
339      return;
340   (t->WaitGL)();
341}
342
343
344void PUBLIC
345glXWaitX(void)
346{
347   struct _glxapi_table *t;
348   Display *dpy = glXGetCurrentDisplay();
349   GET_DISPATCH(dpy, t);
350   if (!t)
351      return;
352   (t->WaitX)();
353}
354
355
356
357/*** GLX_VERSION_1_1 ***/
358
359const char PUBLIC *
360glXGetClientString(Display *dpy, int name)
361{
362   struct _glxapi_table *t;
363   GET_DISPATCH(dpy, t);
364   if (!t)
365      return NULL;
366   return (t->GetClientString)(dpy, name);
367}
368
369
370const char PUBLIC *
371glXQueryExtensionsString(Display *dpy, int screen)
372{
373   struct _glxapi_table *t;
374   GET_DISPATCH(dpy, t);
375   if (!t)
376      return NULL;
377   return (t->QueryExtensionsString)(dpy, screen);
378}
379
380
381const char PUBLIC *
382glXQueryServerString(Display *dpy, int screen, int name)
383{
384   struct _glxapi_table *t;
385   GET_DISPATCH(dpy, t);
386   if (!t)
387      return NULL;
388   return (t->QueryServerString)(dpy, screen, name);
389}
390
391
392/*** GLX_VERSION_1_2 ***/
393
394Display PUBLIC *
395glXGetCurrentDisplay(void)
396{
397   /* Same code as in libGL's glxext.c */
398   __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext();
399   if (NULL == gc) return NULL;
400   return gc->currentDpy;
401}
402
403
404
405/*** GLX_VERSION_1_3 ***/
406
407GLXFBConfig PUBLIC *
408glXChooseFBConfig(Display *dpy, int screen, const int *attribList, int *nitems)
409{
410   struct _glxapi_table *t;
411   GET_DISPATCH(dpy, t);
412   if (!t)
413      return 0;
414   return (t->ChooseFBConfig)(dpy, screen, attribList, nitems);
415}
416
417
418GLXContext PUBLIC
419glXCreateNewContext(Display *dpy, GLXFBConfig config, int renderType, GLXContext shareList, Bool direct)
420{
421   struct _glxapi_table *t;
422   GET_DISPATCH(dpy, t);
423   if (!t)
424      return 0;
425   return (t->CreateNewContext)(dpy, config, renderType, shareList, direct);
426}
427
428
429GLXPbuffer PUBLIC
430glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList)
431{
432   struct _glxapi_table *t;
433   GET_DISPATCH(dpy, t);
434   if (!t)
435      return 0;
436   return (t->CreatePbuffer)(dpy, config, attribList);
437}
438
439
440GLXPixmap PUBLIC
441glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attribList)
442{
443   struct _glxapi_table *t;
444   GET_DISPATCH(dpy, t);
445   if (!t)
446      return 0;
447   return (t->CreatePixmap)(dpy, config, pixmap, attribList);
448}
449
450
451GLXWindow PUBLIC
452glXCreateWindow(Display *dpy, GLXFBConfig config, Window win, const int *attribList)
453{
454   struct _glxapi_table *t;
455   GET_DISPATCH(dpy, t);
456   if (!t)
457      return 0;
458   return (t->CreateWindow)(dpy, config, win, attribList);
459}
460
461
462void PUBLIC
463glXDestroyPbuffer(Display *dpy, GLXPbuffer pbuf)
464{
465   struct _glxapi_table *t;
466   GET_DISPATCH(dpy, t);
467   if (!t)
468      return;
469   (t->DestroyPbuffer)(dpy, pbuf);
470}
471
472
473void PUBLIC
474glXDestroyPixmap(Display *dpy, GLXPixmap pixmap)
475{
476   struct _glxapi_table *t;
477   GET_DISPATCH(dpy, t);
478   if (!t)
479      return;
480   (t->DestroyPixmap)(dpy, pixmap);
481}
482
483
484void PUBLIC
485glXDestroyWindow(Display *dpy, GLXWindow window)
486{
487   struct _glxapi_table *t;
488   GET_DISPATCH(dpy, t);
489   if (!t)
490      return;
491   (t->DestroyWindow)(dpy, window);
492}
493
494
495GLXDrawable PUBLIC
496glXGetCurrentReadDrawable(void)
497{
498   __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext();
499   return gc ? gc->currentReadable : 0;
500}
501
502
503int PUBLIC
504glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config, int attribute, int *value)
505{
506   struct _glxapi_table *t;
507   GET_DISPATCH(dpy, t);
508   if (!t)
509      return GLX_NO_EXTENSION;
510   return (t->GetFBConfigAttrib)(dpy, config, attribute, value);
511}
512
513
514GLXFBConfig PUBLIC *
515glXGetFBConfigs(Display *dpy, int screen, int *nelements)
516{
517   struct _glxapi_table *t;
518   GET_DISPATCH(dpy, t);
519   if (!t)
520      return 0;
521   return (t->GetFBConfigs)(dpy, screen, nelements);
522}
523
524void PUBLIC
525glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
526{
527   struct _glxapi_table *t;
528   GET_DISPATCH(dpy, t);
529   if (!t)
530      return;
531   (t->GetSelectedEvent)(dpy, drawable, mask);
532}
533
534
535XVisualInfo PUBLIC *
536glXGetVisualFromFBConfig(Display *dpy, GLXFBConfig config)
537{
538   struct _glxapi_table *t;
539   GET_DISPATCH(dpy, t);
540   if (!t)
541      return NULL;
542   return (t->GetVisualFromFBConfig)(dpy, config);
543}
544
545
546Bool PUBLIC
547glXMakeContextCurrent(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx)
548{
549   Bool b;
550   struct _glxapi_table *t;
551   GET_DISPATCH(dpy, t);
552   if (!t)
553      return False;
554   b = (t->MakeContextCurrent)(dpy, draw, read, ctx);
555   if (b) {
556      SetCurrentContext(ctx);
557   }
558   return b;
559}
560
561
562int PUBLIC
563glXQueryContext(Display *dpy, GLXContext ctx, int attribute, int *value)
564{
565   struct _glxapi_table *t;
566   GET_DISPATCH(dpy, t);
567   assert(t);
568   if (!t)
569      return 0; /* XXX correct? */
570   return (t->QueryContext)(dpy, ctx, attribute, value);
571}
572
573
574void PUBLIC
575glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute, unsigned int *value)
576{
577   struct _glxapi_table *t;
578   GET_DISPATCH(dpy, t);
579   if (!t)
580      return;
581   (t->QueryDrawable)(dpy, draw, attribute, value);
582}
583
584
585void PUBLIC
586glXSelectEvent(Display *dpy, GLXDrawable drawable, unsigned long mask)
587{
588   struct _glxapi_table *t;
589   GET_DISPATCH(dpy, t);
590   if (!t)
591      return;
592   (t->SelectEvent)(dpy, drawable, mask);
593}
594
595
596
597/*** GLX_SGI_swap_control ***/
598
599int PUBLIC
600glXSwapIntervalSGI(int interval)
601{
602   struct _glxapi_table *t;
603   Display *dpy = glXGetCurrentDisplay();
604   GET_DISPATCH(dpy, t);
605   if (!t)
606      return 0;
607   return (t->SwapIntervalSGI)(interval);
608}
609
610
611
612/*** GLX_SGI_video_sync ***/
613
614int PUBLIC
615glXGetVideoSyncSGI(unsigned int *count)
616{
617   struct _glxapi_table *t;
618   Display *dpy = glXGetCurrentDisplay();
619   GET_DISPATCH(dpy, t);
620   if (!t || !glXGetCurrentContext())
621      return GLX_BAD_CONTEXT;
622   return (t->GetVideoSyncSGI)(count);
623}
624
625int PUBLIC
626glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
627{
628   struct _glxapi_table *t;
629   Display *dpy = glXGetCurrentDisplay();
630   GET_DISPATCH(dpy, t);
631   if (!t || !glXGetCurrentContext())
632      return GLX_BAD_CONTEXT;
633   return (t->WaitVideoSyncSGI)(divisor, remainder, count);
634}
635
636
637
638/*** GLX_SGI_make_current_read ***/
639
640Bool PUBLIC
641glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx)
642{
643   struct _glxapi_table *t;
644   GET_DISPATCH(dpy, t);
645   if (!t)
646      return False;
647   return (t->MakeCurrentReadSGI)(dpy, draw, read, ctx);
648}
649
650GLXDrawable PUBLIC
651glXGetCurrentReadDrawableSGI(void)
652{
653   return glXGetCurrentReadDrawable();
654}
655
656
657#if defined(_VL_H)
658
659GLXVideoSourceSGIX PUBLIC
660glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server, VLPath path, int nodeClass, VLNode drainNode)
661{
662   struct _glxapi_table *t;
663   GET_DISPATCH(dpy, t);
664   if (!t)
665      return 0;
666   return (t->CreateGLXVideoSourceSGIX)(dpy, screen, server, path, nodeClass, drainNode);
667}
668
669void PUBLIC
670glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
671{
672   struct _glxapi_table *t;
673   GET_DISPATCH(dpy, t);
674   if (!t)
675      return 0;
676   return (t->DestroyGLXVideoSourceSGIX)(dpy, src);
677}
678
679#endif
680
681
682/*** GLX_EXT_import_context ***/
683
684void PUBLIC
685glXFreeContextEXT(Display *dpy, GLXContext context)
686{
687   struct _glxapi_table *t;
688   GET_DISPATCH(dpy, t);
689   if (!t)
690      return;
691   (t->FreeContextEXT)(dpy, context);
692}
693
694GLXContextID PUBLIC
695glXGetContextIDEXT(const GLXContext context)
696{
697   return ((__GLXcontext *) context)->xid;
698}
699
700Display PUBLIC *
701glXGetCurrentDisplayEXT(void)
702{
703   return glXGetCurrentDisplay();
704}
705
706GLXContext PUBLIC
707glXImportContextEXT(Display *dpy, GLXContextID contextID)
708{
709   struct _glxapi_table *t;
710   GET_DISPATCH(dpy, t);
711   if (!t)
712      return 0;
713   return (t->ImportContextEXT)(dpy, contextID);
714}
715
716int PUBLIC
717glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute,int *value)
718{
719   struct _glxapi_table *t;
720   GET_DISPATCH(dpy, t);
721   if (!t)
722      return 0;  /* XXX ok? */
723   return (t->QueryContextInfoEXT)(dpy, context, attribute, value);
724}
725
726
727
728/*** GLX_SGIX_fbconfig ***/
729
730int PUBLIC
731glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config, int attribute, int *value)
732{
733   struct _glxapi_table *t;
734   GET_DISPATCH(dpy, t);
735   if (!t)
736      return 0;
737   return (t->GetFBConfigAttribSGIX)(dpy, config, attribute, value);
738}
739
740GLXFBConfigSGIX PUBLIC *
741glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list, int *nelements)
742{
743   struct _glxapi_table *t;
744   GET_DISPATCH(dpy, t);
745   if (!t)
746      return 0;
747   return (t->ChooseFBConfigSGIX)(dpy, screen, attrib_list, nelements);
748}
749
750GLXPixmap PUBLIC
751glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, Pixmap pixmap)
752{
753   struct _glxapi_table *t;
754   GET_DISPATCH(dpy, t);
755   if (!t)
756      return 0;
757   return (t->CreateGLXPixmapWithConfigSGIX)(dpy, config, pixmap);
758}
759
760GLXContext PUBLIC
761glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, int render_type, GLXContext share_list, Bool direct)
762{
763   struct _glxapi_table *t;
764   GET_DISPATCH(dpy, t);
765   if (!t)
766      return 0;
767   return (t->CreateContextWithConfigSGIX)(dpy, config, render_type, share_list, direct);
768}
769
770XVisualInfo PUBLIC *
771glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
772{
773   struct _glxapi_table *t;
774   GET_DISPATCH(dpy, t);
775   if (!t)
776      return 0;
777   return (t->GetVisualFromFBConfigSGIX)(dpy, config);
778}
779
780GLXFBConfigSGIX PUBLIC
781glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
782{
783   struct _glxapi_table *t;
784   GET_DISPATCH(dpy, t);
785   if (!t)
786      return 0;
787   return (t->GetFBConfigFromVisualSGIX)(dpy, vis);
788}
789
790
791
792/*** GLX_SGIX_pbuffer ***/
793
794GLXPbufferSGIX PUBLIC
795glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config, unsigned int width, unsigned int height, int *attrib_list)
796{
797   struct _glxapi_table *t;
798   GET_DISPATCH(dpy, t);
799   if (!t)
800      return 0;
801   return (t->CreateGLXPbufferSGIX)(dpy, config, width, height, attrib_list);
802}
803
804void PUBLIC
805glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
806{
807   struct _glxapi_table *t;
808   GET_DISPATCH(dpy, t);
809   if (!t)
810      return;
811   (t->DestroyGLXPbufferSGIX)(dpy, pbuf);
812}
813
814int PUBLIC
815glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute, unsigned int *value)
816{
817   struct _glxapi_table *t;
818   GET_DISPATCH(dpy, t);
819   if (!t)
820      return 0;
821   return (t->QueryGLXPbufferSGIX)(dpy, pbuf, attribute, value);
822}
823
824void PUBLIC
825glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
826{
827   struct _glxapi_table *t;
828   GET_DISPATCH(dpy, t);
829   if (!t)
830      return;
831   (t->SelectEventSGIX)(dpy, drawable, mask);
832}
833
834void PUBLIC
835glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long *mask)
836{
837   struct _glxapi_table *t;
838   GET_DISPATCH(dpy, t);
839   if (!t)
840      return;
841   (t->GetSelectedEventSGIX)(dpy, drawable, mask);
842}
843
844
845
846/*** GLX_SGI_cushion ***/
847
848void PUBLIC
849glXCushionSGI(Display *dpy, Window win, float cushion)
850{
851   struct _glxapi_table *t;
852   GET_DISPATCH(dpy, t);
853   if (!t)
854      return;
855   (t->CushionSGI)(dpy, win, cushion);
856}
857
858
859
860/*** GLX_SGIX_video_resize ***/
861
862int PUBLIC
863glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel , Window window)
864{
865   struct _glxapi_table *t;
866   GET_DISPATCH(dpy, t);
867   if (!t)
868      return 0;
869   return (t->BindChannelToWindowSGIX)(dpy, screen, channel, window);
870}
871
872int PUBLIC
873glXChannelRectSGIX(Display *dpy, int screen, int channel, int x, int y, int w, int h)
874{
875   struct _glxapi_table *t;
876   GET_DISPATCH(dpy, t);
877   if (!t)
878      return 0;
879   return (t->ChannelRectSGIX)(dpy, screen, channel, x, y, w, h);
880}
881
882int PUBLIC
883glXQueryChannelRectSGIX(Display *dpy, int screen, int channel, int *x, int *y, int *w, int *h)
884{
885   struct _glxapi_table *t;
886   GET_DISPATCH(dpy, t);
887   if (!t)
888      return 0;
889   return (t->QueryChannelRectSGIX)(dpy, screen, channel, x, y, w, h);
890}
891
892int PUBLIC
893glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel, int *dx, int *dy, int *dw, int *dh)
894{
895   struct _glxapi_table *t;
896   GET_DISPATCH(dpy, t);
897   if (!t)
898      return 0;
899   return (t->QueryChannelDeltasSGIX)(dpy, screen, channel, dx, dy, dw, dh);
900}
901
902int PUBLIC
903glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
904{
905   struct _glxapi_table *t;
906   GET_DISPATCH(dpy, t);
907   if (!t)
908      return 0;
909   return (t->ChannelRectSyncSGIX)(dpy, screen, channel, synctype);
910}
911
912
913
914#if defined(_DM_BUFFER_H_)
915
916Bool PUBLIC
917glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer, DMparams *params, DMbuffer dmbuffer)
918{
919   struct _glxapi_table *t;
920   GET_DISPATCH(dpy, t);
921   if (!t)
922      return False;
923   return (t->AssociateDMPbufferSGIX)(dpy, pbuffer, params, dmbuffer);
924}
925
926#endif
927
928
929/*** GLX_SGIX_swap_group ***/
930
931void PUBLIC
932glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
933{
934   struct _glxapi_table *t;
935   GET_DISPATCH(dpy, t);
936   if (!t)
937      return;
938   (*t->JoinSwapGroupSGIX)(dpy, drawable, member);
939}
940
941
942/*** GLX_SGIX_swap_barrier ***/
943
944void PUBLIC
945glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
946{
947   struct _glxapi_table *t;
948   GET_DISPATCH(dpy, t);
949   if (!t)
950      return;
951   (*t->BindSwapBarrierSGIX)(dpy, drawable, barrier);
952}
953
954Bool PUBLIC
955glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
956{
957   struct _glxapi_table *t;
958   GET_DISPATCH(dpy, t);
959   if (!t)
960      return False;
961   return (*t->QueryMaxSwapBarriersSGIX)(dpy, screen, max);
962}
963
964
965
966/*** GLX_SUN_get_transparent_index ***/
967
968Status PUBLIC
969glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay, long *pTransparent)
970{
971   struct _glxapi_table *t;
972   GET_DISPATCH(dpy, t);
973   if (!t)
974      return False;
975   return (*t->GetTransparentIndexSUN)(dpy, overlay, underlay, pTransparent);
976}
977
978
979
980/*** GLX_MESA_copy_sub_buffer ***/
981
982void PUBLIC
983glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable, int x, int y, int width, int height)
984{
985   struct _glxapi_table *t;
986   GET_DISPATCH(dpy, t);
987   if (!t)
988      return;
989   (t->CopySubBufferMESA)(dpy, drawable, x, y, width, height);
990}
991
992
993
994/*** GLX_MESA_release_buffers ***/
995
996Bool PUBLIC
997glXReleaseBuffersMESA(Display *dpy, Window w)
998{
999   struct _glxapi_table *t;
1000   GET_DISPATCH(dpy, t);
1001   if (!t)
1002      return False;
1003   return (t->ReleaseBuffersMESA)(dpy, w);
1004}
1005
1006
1007
1008/*** GLX_MESA_pixmap_colormap ***/
1009
1010GLXPixmap PUBLIC
1011glXCreateGLXPixmapMESA(Display *dpy, XVisualInfo *visinfo, Pixmap pixmap, Colormap cmap)
1012{
1013   struct _glxapi_table *t;
1014   GET_DISPATCH(dpy, t);
1015   if (!t)
1016      return 0;
1017   return (t->CreateGLXPixmapMESA)(dpy, visinfo, pixmap, cmap);
1018}
1019
1020
1021
1022/*** GLX_MESA_set_3dfx_mode ***/
1023
1024Bool PUBLIC
1025glXSet3DfxModeMESA(int mode)
1026{
1027   struct _glxapi_table *t;
1028   Display *dpy = glXGetCurrentDisplay();
1029   GET_DISPATCH(dpy, t);
1030   if (!t)
1031      return False;
1032   return (t->Set3DfxModeMESA)(mode);
1033}
1034
1035
1036
1037/*** GLX_NV_vertex_array_range ***/
1038
1039void PUBLIC *
1040glXAllocateMemoryNV( GLsizei size,
1041                     GLfloat readFrequency,
1042                     GLfloat writeFrequency,
1043                     GLfloat priority )
1044{
1045   struct _glxapi_table *t;
1046   Display *dpy = glXGetCurrentDisplay();
1047   GET_DISPATCH(dpy, t);
1048   if (!t)
1049      return NULL;
1050   return (t->AllocateMemoryNV)(size, readFrequency, writeFrequency, priority);
1051}
1052
1053
1054void PUBLIC
1055glXFreeMemoryNV( GLvoid *pointer )
1056{
1057   struct _glxapi_table *t;
1058   Display *dpy = glXGetCurrentDisplay();
1059   GET_DISPATCH(dpy, t);
1060   if (!t)
1061      return;
1062   (t->FreeMemoryNV)(pointer);
1063}
1064
1065
1066
1067
1068/*** GLX_MESA_agp_offset */
1069
1070GLuint PUBLIC
1071glXGetAGPOffsetMESA( const GLvoid *pointer )
1072{
1073   struct _glxapi_table *t;
1074   Display *dpy = glXGetCurrentDisplay();
1075   GET_DISPATCH(dpy, t);
1076   if (!t)
1077      return ~0;
1078   return (t->GetAGPOffsetMESA)(pointer);
1079}
1080
1081
1082/*** GLX_MESA_allocate_memory */
1083
1084void *
1085glXAllocateMemoryMESA(Display *dpy, int scrn, size_t size,
1086                      float readfreq, float writefreq, float priority)
1087{
1088   /* dummy */
1089   return NULL;
1090}
1091
1092void
1093glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
1094{
1095   /* dummy */
1096}
1097
1098
1099GLuint
1100glXGetMemoryOffsetMESA(Display *dpy, int scrn, const void *pointer)
1101{
1102   /* dummy */
1103   return 0;
1104}
1105
1106
1107/*** GLX_EXT_texture_from_pixmap */
1108
1109void
1110glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
1111                   const int *attrib_list)
1112{
1113   struct _glxapi_table *t;
1114   GET_DISPATCH(dpy, t);
1115   if (t)
1116      t->BindTexImageEXT(dpy, drawable, buffer, attrib_list);
1117}
1118
1119void
1120glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
1121{
1122   struct _glxapi_table *t;
1123   GET_DISPATCH(dpy, t);
1124   if (t)
1125      t->ReleaseTexImageEXT(dpy, drawable, buffer);
1126}
1127
1128
1129/**********************************************************************/
1130/* GLX API management functions                                       */
1131/**********************************************************************/
1132
1133
1134const char *
1135_glxapi_get_version(void)
1136{
1137   return "1.3";
1138}
1139
1140
1141/*
1142 * Return array of extension strings.
1143 */
1144const char **
1145_glxapi_get_extensions(void)
1146{
1147   static const char *extensions[] = {
1148#ifdef GLX_EXT_import_context
1149      "GLX_EXT_import_context",
1150#endif
1151#ifdef GLX_SGI_video_sync
1152      "GLX_SGI_video_sync",
1153#endif
1154#ifdef GLX_MESA_copy_sub_buffer
1155      "GLX_MESA_copy_sub_buffer",
1156#endif
1157#ifdef GLX_MESA_release_buffers
1158      "GLX_MESA_release_buffers",
1159#endif
1160#ifdef GLX_MESA_pixmap_colormap
1161      "GLX_MESA_pixmap_colormap",
1162#endif
1163#ifdef GLX_MESA_set_3dfx_mode
1164      "GLX_MESA_set_3dfx_mode",
1165#endif
1166#ifdef GLX_SGIX_fbconfig
1167      "GLX_SGIX_fbconfig",
1168#endif
1169#ifdef GLX_SGIX_pbuffer
1170      "GLX_SGIX_pbuffer",
1171#endif
1172#ifdef GLX_EXT_texture_from_pixmap
1173      "GLX_EXT_texture_from_pixmap",
1174#endif
1175      NULL
1176   };
1177   return extensions;
1178}
1179
1180
1181/*
1182 * Return size of the GLX dispatch table, in entries, not bytes.
1183 */
1184GLuint
1185_glxapi_get_dispatch_table_size(void)
1186{
1187   return sizeof(struct _glxapi_table) / sizeof(void *);
1188}
1189
1190
1191static int
1192generic_no_op_func(void)
1193{
1194   return 0;
1195}
1196
1197
1198/*
1199 * Initialize all functions in given dispatch table to be no-ops
1200 */
1201void
1202_glxapi_set_no_op_table(struct _glxapi_table *t)
1203{
1204   typedef int (*nop_func)(void);
1205   nop_func *dispatch = (nop_func *) t;
1206   GLuint n = _glxapi_get_dispatch_table_size();
1207   GLuint i;
1208   for (i = 0; i < n; i++) {
1209      dispatch[i] = generic_no_op_func;
1210   }
1211}
1212
1213
1214struct name_address_pair {
1215   const char *Name;
1216   __GLXextFuncPtr Address;
1217};
1218
1219static struct name_address_pair GLX_functions[] = {
1220   /*** GLX_VERSION_1_0 ***/
1221   { "glXChooseVisual", (__GLXextFuncPtr) glXChooseVisual },
1222   { "glXCopyContext", (__GLXextFuncPtr) glXCopyContext },
1223   { "glXCreateContext", (__GLXextFuncPtr) glXCreateContext },
1224   { "glXCreateGLXPixmap", (__GLXextFuncPtr) glXCreateGLXPixmap },
1225   { "glXDestroyContext", (__GLXextFuncPtr) glXDestroyContext },
1226   { "glXDestroyGLXPixmap", (__GLXextFuncPtr) glXDestroyGLXPixmap },
1227   { "glXGetConfig", (__GLXextFuncPtr) glXGetConfig },
1228   { "glXGetCurrentContext", (__GLXextFuncPtr) glXGetCurrentContext },
1229   { "glXGetCurrentDrawable", (__GLXextFuncPtr) glXGetCurrentDrawable },
1230   { "glXIsDirect", (__GLXextFuncPtr) glXIsDirect },
1231   { "glXMakeCurrent", (__GLXextFuncPtr) glXMakeCurrent },
1232   { "glXQueryExtension", (__GLXextFuncPtr) glXQueryExtension },
1233   { "glXQueryVersion", (__GLXextFuncPtr) glXQueryVersion },
1234   { "glXSwapBuffers", (__GLXextFuncPtr) glXSwapBuffers },
1235   { "glXUseXFont", (__GLXextFuncPtr) glXUseXFont },
1236   { "glXWaitGL", (__GLXextFuncPtr) glXWaitGL },
1237   { "glXWaitX", (__GLXextFuncPtr) glXWaitX },
1238
1239   /*** GLX_VERSION_1_1 ***/
1240   { "glXGetClientString", (__GLXextFuncPtr) glXGetClientString },
1241   { "glXQueryExtensionsString", (__GLXextFuncPtr) glXQueryExtensionsString },
1242   { "glXQueryServerString", (__GLXextFuncPtr) glXQueryServerString },
1243
1244   /*** GLX_VERSION_1_2 ***/
1245   { "glXGetCurrentDisplay", (__GLXextFuncPtr) glXGetCurrentDisplay },
1246
1247   /*** GLX_VERSION_1_3 ***/
1248   { "glXChooseFBConfig", (__GLXextFuncPtr) glXChooseFBConfig },
1249   { "glXCreateNewContext", (__GLXextFuncPtr) glXCreateNewContext },
1250   { "glXCreatePbuffer", (__GLXextFuncPtr) glXCreatePbuffer },
1251   { "glXCreatePixmap", (__GLXextFuncPtr) glXCreatePixmap },
1252   { "glXCreateWindow", (__GLXextFuncPtr) glXCreateWindow },
1253   { "glXDestroyPbuffer", (__GLXextFuncPtr) glXDestroyPbuffer },
1254   { "glXDestroyPixmap", (__GLXextFuncPtr) glXDestroyPixmap },
1255   { "glXDestroyWindow", (__GLXextFuncPtr) glXDestroyWindow },
1256   { "glXGetCurrentReadDrawable", (__GLXextFuncPtr) glXGetCurrentReadDrawable },
1257   { "glXGetFBConfigAttrib", (__GLXextFuncPtr) glXGetFBConfigAttrib },
1258   { "glXGetFBConfigs", (__GLXextFuncPtr) glXGetFBConfigs },
1259   { "glXGetSelectedEvent", (__GLXextFuncPtr) glXGetSelectedEvent },
1260   { "glXGetVisualFromFBConfig", (__GLXextFuncPtr) glXGetVisualFromFBConfig },
1261   { "glXMakeContextCurrent", (__GLXextFuncPtr) glXMakeContextCurrent },
1262   { "glXQueryContext", (__GLXextFuncPtr) glXQueryContext },
1263   { "glXQueryDrawable", (__GLXextFuncPtr) glXQueryDrawable },
1264   { "glXSelectEvent", (__GLXextFuncPtr) glXSelectEvent },
1265
1266   /*** GLX_VERSION_1_4 ***/
1267   { "glXGetProcAddress", (__GLXextFuncPtr) glXGetProcAddress },
1268
1269   /*** GLX_SGI_swap_control ***/
1270   { "glXSwapIntervalSGI", (__GLXextFuncPtr) glXSwapIntervalSGI },
1271
1272   /*** GLX_SGI_video_sync ***/
1273   { "glXGetVideoSyncSGI", (__GLXextFuncPtr) glXGetVideoSyncSGI },
1274   { "glXWaitVideoSyncSGI", (__GLXextFuncPtr) glXWaitVideoSyncSGI },
1275
1276   /*** GLX_SGI_make_current_read ***/
1277   { "glXMakeCurrentReadSGI", (__GLXextFuncPtr) glXMakeCurrentReadSGI },
1278   { "glXGetCurrentReadDrawableSGI", (__GLXextFuncPtr) glXGetCurrentReadDrawableSGI },
1279
1280   /*** GLX_SGIX_video_source ***/
1281#if defined(_VL_H)
1282   { "glXCreateGLXVideoSourceSGIX", (__GLXextFuncPtr) glXCreateGLXVideoSourceSGIX },
1283   { "glXDestroyGLXVideoSourceSGIX", (__GLXextFuncPtr) glXDestroyGLXVideoSourceSGIX },
1284#endif
1285
1286   /*** GLX_EXT_import_context ***/
1287   { "glXFreeContextEXT", (__GLXextFuncPtr) glXFreeContextEXT },
1288   { "glXGetContextIDEXT", (__GLXextFuncPtr) glXGetContextIDEXT },
1289   { "glXGetCurrentDisplayEXT", (__GLXextFuncPtr) glXGetCurrentDisplayEXT },
1290   { "glXImportContextEXT", (__GLXextFuncPtr) glXImportContextEXT },
1291   { "glXQueryContextInfoEXT", (__GLXextFuncPtr) glXQueryContextInfoEXT },
1292
1293   /*** GLX_SGIX_fbconfig ***/
1294   { "glXGetFBConfigAttribSGIX", (__GLXextFuncPtr) glXGetFBConfigAttribSGIX },
1295   { "glXChooseFBConfigSGIX", (__GLXextFuncPtr) glXChooseFBConfigSGIX },
1296   { "glXCreateGLXPixmapWithConfigSGIX", (__GLXextFuncPtr) glXCreateGLXPixmapWithConfigSGIX },
1297   { "glXCreateContextWithConfigSGIX", (__GLXextFuncPtr) glXCreateContextWithConfigSGIX },
1298   { "glXGetVisualFromFBConfigSGIX", (__GLXextFuncPtr) glXGetVisualFromFBConfigSGIX },
1299   { "glXGetFBConfigFromVisualSGIX", (__GLXextFuncPtr) glXGetFBConfigFromVisualSGIX },
1300
1301   /*** GLX_SGIX_pbuffer ***/
1302   { "glXCreateGLXPbufferSGIX", (__GLXextFuncPtr) glXCreateGLXPbufferSGIX },
1303   { "glXDestroyGLXPbufferSGIX", (__GLXextFuncPtr) glXDestroyGLXPbufferSGIX },
1304   { "glXQueryGLXPbufferSGIX", (__GLXextFuncPtr) glXQueryGLXPbufferSGIX },
1305   { "glXSelectEventSGIX", (__GLXextFuncPtr) glXSelectEventSGIX },
1306   { "glXGetSelectedEventSGIX", (__GLXextFuncPtr) glXGetSelectedEventSGIX },
1307
1308   /*** GLX_SGI_cushion ***/
1309   { "glXCushionSGI", (__GLXextFuncPtr) glXCushionSGI },
1310
1311   /*** GLX_SGIX_video_resize ***/
1312   { "glXBindChannelToWindowSGIX", (__GLXextFuncPtr) glXBindChannelToWindowSGIX },
1313   { "glXChannelRectSGIX", (__GLXextFuncPtr) glXChannelRectSGIX },
1314   { "glXQueryChannelRectSGIX", (__GLXextFuncPtr) glXQueryChannelRectSGIX },
1315   { "glXQueryChannelDeltasSGIX", (__GLXextFuncPtr) glXQueryChannelDeltasSGIX },
1316   { "glXChannelRectSyncSGIX", (__GLXextFuncPtr) glXChannelRectSyncSGIX },
1317
1318   /*** GLX_SGIX_dmbuffer **/
1319#if defined(_DM_BUFFER_H_)
1320   { "glXAssociateDMPbufferSGIX", (__GLXextFuncPtr) glXAssociateDMPbufferSGIX },
1321#endif
1322
1323   /*** GLX_SGIX_swap_group ***/
1324   { "glXJoinSwapGroupSGIX", (__GLXextFuncPtr) glXJoinSwapGroupSGIX },
1325
1326   /*** GLX_SGIX_swap_barrier ***/
1327   { "glXBindSwapBarrierSGIX", (__GLXextFuncPtr) glXBindSwapBarrierSGIX },
1328   { "glXQueryMaxSwapBarriersSGIX", (__GLXextFuncPtr) glXQueryMaxSwapBarriersSGIX },
1329
1330   /*** GLX_SUN_get_transparent_index ***/
1331   { "glXGetTransparentIndexSUN", (__GLXextFuncPtr) glXGetTransparentIndexSUN },
1332
1333   /*** GLX_MESA_copy_sub_buffer ***/
1334   { "glXCopySubBufferMESA", (__GLXextFuncPtr) glXCopySubBufferMESA },
1335
1336   /*** GLX_MESA_pixmap_colormap ***/
1337   { "glXCreateGLXPixmapMESA", (__GLXextFuncPtr) glXCreateGLXPixmapMESA },
1338
1339   /*** GLX_MESA_release_buffers ***/
1340   { "glXReleaseBuffersMESA", (__GLXextFuncPtr) glXReleaseBuffersMESA },
1341
1342   /*** GLX_MESA_set_3dfx_mode ***/
1343   { "glXSet3DfxModeMESA", (__GLXextFuncPtr) glXSet3DfxModeMESA },
1344
1345   /*** GLX_ARB_get_proc_address ***/
1346   { "glXGetProcAddressARB", (__GLXextFuncPtr) glXGetProcAddressARB },
1347
1348   /*** GLX_NV_vertex_array_range ***/
1349   { "glXAllocateMemoryNV", (__GLXextFuncPtr) glXAllocateMemoryNV },
1350   { "glXFreeMemoryNV", (__GLXextFuncPtr) glXFreeMemoryNV },
1351
1352   /*** GLX_MESA_agp_offset ***/
1353   { "glXGetAGPOffsetMESA", (__GLXextFuncPtr) glXGetAGPOffsetMESA },
1354
1355   /*** GLX_MESA_allocate_memory ***/
1356   { "glXAllocateMemoryMESA", (__GLXextFuncPtr) glXAllocateMemoryMESA },
1357   { "glXFreeMemoryMESA", (__GLXextFuncPtr) glXFreeMemoryMESA },
1358   { "glXGetMemoryOffsetMESA", (__GLXextFuncPtr) glXGetMemoryOffsetMESA },
1359
1360   /*** GLX_EXT_texture_from_pixmap ***/
1361   { "glXBindTexImageEXT", (__GLXextFuncPtr) glXBindTexImageEXT },
1362   { "glXReleaseTexImageEXT", (__GLXextFuncPtr) glXReleaseTexImageEXT },
1363
1364   { NULL, NULL }   /* end of list */
1365};
1366
1367
1368
1369/*
1370 * Return address of named glX function, or NULL if not found.
1371 */
1372__GLXextFuncPtr
1373_glxapi_get_proc_address(const char *funcName)
1374{
1375   GLuint i;
1376   for (i = 0; GLX_functions[i].Name; i++) {
1377#ifdef MANGLE
1378      /* skip the "m" prefix on the name */
1379      if (strcmp(GLX_functions[i].Name, funcName+1) == 0)
1380#else
1381      if (strcmp(GLX_functions[i].Name, funcName) == 0)
1382#endif
1383         return GLX_functions[i].Address;
1384   }
1385   return NULL;
1386}
1387
1388
1389
1390/*
1391 * This function does not get dispatched through the dispatch table
1392 * since it's really a "meta" function.
1393 */
1394__GLXextFuncPtr
1395glXGetProcAddressARB(const GLubyte *procName)
1396{
1397   __GLXextFuncPtr f;
1398
1399   f = _glxapi_get_proc_address((const char *) procName);
1400   if (f) {
1401      return f;
1402   }
1403
1404   f = (__GLXextFuncPtr) _glapi_get_proc_address((const char *) procName);
1405   return f;
1406}
1407
1408
1409/* GLX 1.4 */
1410void (*glXGetProcAddress(const GLubyte *procName))()
1411{
1412   return glXGetProcAddressARB(procName);
1413}
1414