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