OSDN Git Service

c6dc134c5cf630e3906d236ae342ff051edee900
[android-x86/external-mesa.git] / src / gallium / state_trackers / glx / xlib / glx_api.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.6
4  *
5  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
6  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27
28 /**
29  * "Fake" GLX API implemented in terms of the XMesa*() functions.
30  */
31
32
33
34 #define GLX_GLXEXT_PROTOTYPES
35 #include "GL/glx.h"
36
37 #include "xm_api.h"
38
39
40 /* This indicates the client-side GLX API and GLX encoder version. */
41 #define CLIENT_MAJOR_VERSION 1
42 #define CLIENT_MINOR_VERSION 4  /* but don't have 1.3's pbuffers, etc yet */
43
44 /* This indicates the server-side GLX decoder version.
45  * GLX 1.4 indicates OpenGL 1.3 support
46  */
47 #define SERVER_MAJOR_VERSION 1
48 #define SERVER_MINOR_VERSION 4
49
50 /* Who implemented this GLX? */
51 #define VENDOR "Brian Paul"
52
53 #define EXTENSIONS \
54    "GLX_MESA_copy_sub_buffer " \
55    "GLX_MESA_pixmap_colormap " \
56    "GLX_MESA_release_buffers " \
57    "GLX_ARB_create_context " \
58    "GLX_ARB_create_context_profile " \
59    "GLX_ARB_get_proc_address " \
60    "GLX_EXT_texture_from_pixmap " \
61    "GLX_EXT_visual_info " \
62    "GLX_EXT_visual_rating " \
63    /*"GLX_SGI_video_sync "*/ \
64    "GLX_SGIX_fbconfig " \
65    "GLX_SGIX_pbuffer "
66
67 #define DEFAULT_DIRECT GL_TRUE
68
69
70 /** XXX this could be based on gallium's max texture size */
71 #define PBUFFER_MAX_SIZE 16384
72
73
74 /**
75  * The GLXContext typedef is defined as a pointer to this structure.
76  */
77 struct __GLXcontextRec
78 {
79    Display *currentDpy;
80    GLboolean isDirect;
81    GLXDrawable currentDrawable;
82    GLXDrawable currentReadable;
83    XID xid;
84
85    XMesaContext xmesaContext;
86 };
87
88
89
90 static pipe_tsd ContextTSD;
91
92 /** Set current context for calling thread */
93 static void
94 SetCurrentContext(GLXContext c)
95 {
96    pipe_tsd_set(&ContextTSD, c);
97 }
98
99 /** Get current context for calling thread */
100 static GLXContext
101 GetCurrentContext(void)
102 {
103    return pipe_tsd_get(&ContextTSD);
104 }
105
106
107
108 /**********************************************************************/
109 /***                       GLX Visual Code                          ***/
110 /**********************************************************************/
111
112 #define DONT_CARE -1
113
114
115 static XMesaVisual *VisualTable = NULL;
116 static int NumVisuals = 0;
117
118
119
120 /* Macro to handle c_class vs class field name in XVisualInfo struct */
121 #if defined(__cplusplus) || defined(c_plusplus)
122 #define CLASS c_class
123 #else
124 #define CLASS class
125 #endif
126
127
128
129 /*
130  * Test if the given XVisualInfo is usable for Mesa rendering.
131  */
132 static GLboolean
133 is_usable_visual( XVisualInfo *vinfo )
134 {
135    switch (vinfo->CLASS) {
136       case StaticGray:
137       case GrayScale:
138          /* Any StaticGray/GrayScale visual works in RGB or CI mode */
139          return GL_TRUE;
140       case StaticColor:
141       case PseudoColor:
142          /* Any StaticColor/PseudoColor visual of at least 4 bits */
143          if (vinfo->depth>=4) {
144             return GL_TRUE;
145          }
146          else {
147             return GL_FALSE;
148          }
149       case TrueColor:
150       case DirectColor:
151          /* Any depth of TrueColor or DirectColor works in RGB mode */
152          return GL_TRUE;
153       default:
154          /* This should never happen */
155          return GL_FALSE;
156    }
157 }
158
159
160 /*
161  * Given an XVisualInfo and RGB, Double, and Depth buffer flags, save the
162  * configuration in our list of GLX visuals.
163  */
164 static XMesaVisual
165 save_glx_visual( Display *dpy, XVisualInfo *vinfo,
166                  GLboolean rgbFlag, GLboolean alphaFlag, GLboolean dbFlag,
167                  GLboolean stereoFlag,
168                  GLint depth_size, GLint stencil_size,
169                  GLint accumRedSize, GLint accumGreenSize,
170                  GLint accumBlueSize, GLint accumAlphaSize,
171                  GLint level, GLint numAuxBuffers )
172 {
173    GLboolean ximageFlag = GL_TRUE;
174    XMesaVisual xmvis;
175    GLint i;
176    GLboolean comparePointers;
177
178    if (dbFlag) {
179       /* Check if the MESA_BACK_BUFFER env var is set */
180       char *backbuffer = _mesa_getenv("MESA_BACK_BUFFER");
181       if (backbuffer) {
182          if (backbuffer[0]=='p' || backbuffer[0]=='P') {
183             ximageFlag = GL_FALSE;
184          }
185          else if (backbuffer[0]=='x' || backbuffer[0]=='X') {
186             ximageFlag = GL_TRUE;
187          }
188          else {
189             _mesa_warning(NULL, "Mesa: invalid value for MESA_BACK_BUFFER environment variable, using an XImage.");
190          }
191       }
192    }
193
194    if (stereoFlag) {
195       /* stereo not supported */
196       return NULL;
197    }
198
199    if (stencil_size > 0 && depth_size > 0)
200       depth_size = 24;
201
202    /* Comparing IDs uses less memory but sometimes fails. */
203    /* XXX revisit this after 3.0 is finished. */
204    if (_mesa_getenv("MESA_GLX_VISUAL_HACK"))
205       comparePointers = GL_TRUE;
206    else
207       comparePointers = GL_FALSE;
208
209    /* Force the visual to have an alpha channel */
210    if (rgbFlag && _mesa_getenv("MESA_GLX_FORCE_ALPHA"))
211       alphaFlag = GL_TRUE;
212
213    /* First check if a matching visual is already in the list */
214    for (i=0; i<NumVisuals; i++) {
215       XMesaVisual v = VisualTable[i];
216       if (v->display == dpy
217           && v->mesa_visual.level == level
218           && v->mesa_visual.numAuxBuffers == numAuxBuffers
219           && v->ximage_flag == ximageFlag
220           && v->mesa_visual.rgbMode == rgbFlag
221           && v->mesa_visual.doubleBufferMode == dbFlag
222           && v->mesa_visual.stereoMode == stereoFlag
223           && (v->mesa_visual.alphaBits > 0) == alphaFlag
224           && (v->mesa_visual.depthBits >= depth_size || depth_size == 0)
225           && (v->mesa_visual.stencilBits >= stencil_size || stencil_size == 0)
226           && (v->mesa_visual.accumRedBits >= accumRedSize || accumRedSize == 0)
227           && (v->mesa_visual.accumGreenBits >= accumGreenSize || accumGreenSize == 0)
228           && (v->mesa_visual.accumBlueBits >= accumBlueSize || accumBlueSize == 0)
229           && (v->mesa_visual.accumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
230          /* now either compare XVisualInfo pointers or visual IDs */
231          if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
232              || (comparePointers && v->vishandle == vinfo)) {
233             return v;
234          }
235       }
236    }
237
238    /* Create a new visual and add it to the list. */
239
240    xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
241                               stereoFlag, ximageFlag,
242                               depth_size, stencil_size,
243                               accumRedSize, accumBlueSize,
244                               accumBlueSize, accumAlphaSize, 0, level,
245                               GLX_NONE_EXT );
246    if (xmvis) {
247       /* Save a copy of the pointer now so we can find this visual again
248        * if we need to search for it in find_glx_visual().
249        */
250       xmvis->vishandle = vinfo;
251       /* Allocate more space for additional visual */
252       VisualTable = (XMesaVisual *) _mesa_realloc( VisualTable, 
253                                    sizeof(XMesaVisual) * NumVisuals, 
254                                    sizeof(XMesaVisual) * (NumVisuals + 1));
255       /* add xmvis to the list */
256       VisualTable[NumVisuals] = xmvis;
257       NumVisuals++;
258       /* XXX minor hack, because XMesaCreateVisual doesn't support an
259        * aux buffers parameter.
260        */
261       xmvis->mesa_visual.numAuxBuffers = numAuxBuffers;
262    }
263    return xmvis;
264 }
265
266
267 /**
268  * Return the default number of bits for the Z buffer.
269  * If defined, use the MESA_GLX_DEPTH_BITS env var value.
270  * Otherwise, use the DEFAULT_SOFTWARE_DEPTH_BITS constant.
271  * XXX probably do the same thing for stencil, accum, etc.
272  */
273 static GLint
274 default_depth_bits(void)
275 {
276    int zBits;
277    const char *zEnv = _mesa_getenv("MESA_GLX_DEPTH_BITS");
278    if (zEnv)
279       zBits = atoi(zEnv);
280    else
281       zBits = 24;
282    return zBits;
283 }
284
285 static GLint
286 default_alpha_bits(void)
287 {
288    int aBits;
289    const char *aEnv = _mesa_getenv("MESA_GLX_ALPHA_BITS");
290    if (aEnv)
291       aBits = atoi(aEnv);
292    else
293       aBits = 0;
294    return aBits;
295 }
296
297 static GLint
298 default_accum_bits(void)
299 {
300    return 16;
301 }
302
303
304
305 /*
306  * Create a GLX visual from a regular XVisualInfo.
307  * This is called when Fake GLX is given an XVisualInfo which wasn't
308  * returned by glXChooseVisual.  Since this is the first time we're
309  * considering this visual we'll take a guess at reasonable values
310  * for depth buffer size, stencil size, accum size, etc.
311  * This is the best we can do with a client-side emulation of GLX.
312  */
313 static XMesaVisual
314 create_glx_visual( Display *dpy, XVisualInfo *visinfo )
315 {
316    GLint zBits = default_depth_bits();
317    GLint accBits = default_accum_bits();
318    GLboolean alphaFlag = default_alpha_bits() > 0;
319
320    if (is_usable_visual( visinfo )) {
321       /* Configure this visual as RGB, double-buffered, depth-buffered. */
322       /* This is surely wrong for some people's needs but what else */
323       /* can be done?  They should use glXChooseVisual(). */
324       return save_glx_visual( dpy, visinfo,
325                               GL_TRUE,   /* rgb */
326                               alphaFlag, /* alpha */
327                               GL_TRUE,   /* double */
328                               GL_FALSE,  /* stereo */
329                               zBits,
330                               8,       /* stencil bits */
331                               accBits, /* r */
332                               accBits, /* g */
333                               accBits, /* b */
334                               accBits, /* a */
335                               0,         /* level */
336                               0          /* numAux */
337          );
338    }
339    else {
340       _mesa_warning(NULL, "Mesa: error in glXCreateContext: bad visual\n");
341       return NULL;
342    }
343 }
344
345
346
347 /*
348  * Find the GLX visual associated with an XVisualInfo.
349  */
350 static XMesaVisual
351 find_glx_visual( Display *dpy, XVisualInfo *vinfo )
352 {
353    int i;
354
355    /* try to match visual id */
356    for (i=0;i<NumVisuals;i++) {
357       if (VisualTable[i]->display==dpy
358           && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
359          return VisualTable[i];
360       }
361    }
362
363    /* if that fails, try to match pointers */
364    for (i=0;i<NumVisuals;i++) {
365       if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
366          return VisualTable[i];
367       }
368    }
369
370    return NULL;
371 }
372
373
374 /**
375  * Try to get an X visual which matches the given arguments.
376  */
377 static XVisualInfo *
378 get_visual( Display *dpy, int scr, unsigned int depth, int xclass )
379 {
380    XVisualInfo temp, *vis;
381    long mask;
382    int n;
383    unsigned int default_depth;
384    int default_class;
385
386    mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
387    temp.screen = scr;
388    temp.depth = depth;
389    temp.CLASS = xclass;
390
391    default_depth = DefaultDepth(dpy,scr);
392    default_class = DefaultVisual(dpy,scr)->CLASS;
393
394    if (depth==default_depth && xclass==default_class) {
395       /* try to get root window's visual */
396       temp.visualid = DefaultVisual(dpy,scr)->visualid;
397       mask |= VisualIDMask;
398    }
399
400    vis = XGetVisualInfo( dpy, mask, &temp, &n );
401
402    /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
403     * An SGI Infinite Reality system, for example, can have 30bpp pixels:
404     * 10 bits per color channel.  Mesa's limited to a max of 8 bits/channel.
405     */
406    if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
407       if (_mesa_bitcount((GLuint) vis->red_mask  ) <= 8 &&
408           _mesa_bitcount((GLuint) vis->green_mask) <= 8 &&
409           _mesa_bitcount((GLuint) vis->blue_mask ) <= 8) {
410          return vis;
411       }
412       else {
413          free((void *) vis);
414          return NULL;
415       }
416    }
417    
418    return vis;
419 }
420
421
422 /*
423  * Retrieve the value of the given environment variable and find
424  * the X visual which matches it.
425  * Input:  dpy - the display
426  *         screen - the screen number
427  *         varname - the name of the environment variable
428  * Return:  an XVisualInfo pointer to NULL if error.
429  */
430 static XVisualInfo *
431 get_env_visual(Display *dpy, int scr, const char *varname)
432 {
433    char value[100], type[100];
434    int depth, xclass = -1;
435    XVisualInfo *vis;
436
437    if (!_mesa_getenv( varname )) {
438       return NULL;
439    }
440
441    strncpy( value, _mesa_getenv(varname), 100 );
442    value[99] = 0;
443
444    sscanf( value, "%s %d", type, &depth );
445
446    if (strcmp(type,"TrueColor")==0)          xclass = TrueColor;
447    else if (strcmp(type,"DirectColor")==0)   xclass = DirectColor;
448    else if (strcmp(type,"PseudoColor")==0)   xclass = PseudoColor;
449    else if (strcmp(type,"StaticColor")==0)   xclass = StaticColor;
450    else if (strcmp(type,"GrayScale")==0)     xclass = GrayScale;
451    else if (strcmp(type,"StaticGray")==0)    xclass = StaticGray;
452
453    if (xclass>-1 && depth>0) {
454       vis = get_visual( dpy, scr, depth, xclass );
455       if (vis) {
456          return vis;
457       }
458    }
459
460    _mesa_warning(NULL, "GLX unable to find visual class=%s, depth=%d.",
461                  type, depth);
462
463    return NULL;
464 }
465
466
467
468 /*
469  * Select an X visual which satisfies the RGBA flag and minimum depth.
470  * Input:  dpy, 
471  *         screen - X display and screen number
472  *         min_depth - minimum visual depth
473  *         preferred_class - preferred GLX visual class or DONT_CARE
474  * Return:  pointer to an XVisualInfo or NULL.
475  */
476 static XVisualInfo *
477 choose_x_visual( Display *dpy, int screen, int min_depth,
478                  int preferred_class )
479 {
480    XVisualInfo *vis;
481    int xclass, visclass = 0;
482    int depth;
483
484    /* First see if the MESA_RGB_VISUAL env var is defined */
485    vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
486    if (vis) {
487       return vis;
488    }
489    /* Otherwise, search for a suitable visual */
490    if (preferred_class==DONT_CARE) {
491       for (xclass=0;xclass<6;xclass++) {
492          switch (xclass) {
493          case 0:  visclass = TrueColor;    break;
494          case 1:  visclass = DirectColor;  break;
495          case 2:  visclass = PseudoColor;  break;
496          case 3:  visclass = StaticColor;  break;
497          case 4:  visclass = GrayScale;    break;
498          case 5:  visclass = StaticGray;   break;
499          }
500          if (min_depth==0) {
501             /* start with shallowest */
502             for (depth=0;depth<=32;depth++) {
503                if (visclass==TrueColor && depth==8) {
504                   /* Special case:  try to get 8-bit PseudoColor before */
505                   /* 8-bit TrueColor */
506                   vis = get_visual( dpy, screen, 8, PseudoColor );
507                   if (vis) {
508                      return vis;
509                   }
510                }
511                vis = get_visual( dpy, screen, depth, visclass );
512                if (vis) {
513                   return vis;
514                }
515             }
516          }
517          else {
518             /* start with deepest */
519             for (depth=32;depth>=min_depth;depth--) {
520                if (visclass==TrueColor && depth==8) {
521                   /* Special case:  try to get 8-bit PseudoColor before */
522                   /* 8-bit TrueColor */
523                   vis = get_visual( dpy, screen, 8, PseudoColor );
524                   if (vis) {
525                      return vis;
526                   }
527                }
528                vis = get_visual( dpy, screen, depth, visclass );
529                if (vis) {
530                   return vis;
531                }
532             }
533          }
534       }
535    }
536    else {
537       /* search for a specific visual class */
538       switch (preferred_class) {
539       case GLX_TRUE_COLOR_EXT:    visclass = TrueColor;    break;
540       case GLX_DIRECT_COLOR_EXT:  visclass = DirectColor;  break;
541       case GLX_PSEUDO_COLOR_EXT:  visclass = PseudoColor;  break;
542       case GLX_STATIC_COLOR_EXT:  visclass = StaticColor;  break;
543       case GLX_GRAY_SCALE_EXT:    visclass = GrayScale;    break;
544       case GLX_STATIC_GRAY_EXT:   visclass = StaticGray;   break;
545       default:   return NULL;
546       }
547       if (min_depth==0) {
548          /* start with shallowest */
549          for (depth=0;depth<=32;depth++) {
550             vis = get_visual( dpy, screen, depth, visclass );
551             if (vis) {
552                return vis;
553             }
554          }
555       }
556       else {
557          /* start with deepest */
558          for (depth=32;depth>=min_depth;depth--) {
559             vis = get_visual( dpy, screen, depth, visclass );
560             if (vis) {
561                return vis;
562             }
563          }
564       }
565    }
566
567    /* didn't find a visual */
568    return NULL;
569 }
570
571
572
573
574 /**********************************************************************/
575 /***             Display-related functions                          ***/
576 /**********************************************************************/
577
578
579 /**
580  * Free all XMesaVisuals which are associated with the given display.
581  */
582 static void
583 destroy_visuals_on_display(Display *dpy)
584 {
585    int i;
586    for (i = 0; i < NumVisuals; i++) {
587       if (VisualTable[i]->display == dpy) {
588          /* remove this visual */
589          int j;
590          free(VisualTable[i]);
591          for (j = i; j < NumVisuals - 1; j++)
592             VisualTable[j] = VisualTable[j + 1];
593          NumVisuals--;
594       }
595    }
596 }
597
598
599 /**
600  * Called from XCloseDisplay() to let us free our display-related data.
601  */
602 static int
603 close_display_callback(Display *dpy, XExtCodes *codes)
604 {
605    xmesa_destroy_buffers_on_display(dpy);
606    destroy_visuals_on_display(dpy);
607    return 0;
608 }
609
610
611 /**
612  * Look for the named extension on given display and return a pointer
613  * to the _XExtension data, or NULL if extension not found.
614  */
615 static _XExtension *
616 lookup_extension(Display *dpy, const char *extName)
617 {
618    _XExtension *ext;
619    for (ext = dpy->ext_procs; ext; ext = ext->next) {
620       if (ext->name && strcmp(ext->name, extName) == 0) {
621          return ext;
622       }
623    }
624    return NULL;
625 }
626
627
628 /**
629  * Whenever we're given a new Display pointer, call this function to
630  * register our close_display_callback function.
631  */
632 static void
633 register_with_display(Display *dpy)
634 {
635    const char *extName = "MesaGLX";
636    _XExtension *ext;
637
638    ext = lookup_extension(dpy, extName);
639    if (!ext) {
640       XExtCodes *c = XAddExtension(dpy);
641       ext = dpy->ext_procs;  /* new extension is at head of list */
642       assert(c->extension == ext->codes.extension);
643       (void) c;
644       ext->name = _mesa_strdup(extName);
645       ext->close_display = close_display_callback;
646    }
647 }
648
649
650 /**********************************************************************/
651 /***                  Begin Fake GLX API Functions                  ***/
652 /**********************************************************************/
653
654
655 /**
656  * Helper used by glXChooseVisual and glXChooseFBConfig.
657  * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
658  * the later.
659  * In either case, the attribute list is terminated with the value 'None'.
660  */
661 static XMesaVisual
662 choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
663 {
664    const GLboolean rgbModeDefault = fbConfig;
665    const int *parselist;
666    XVisualInfo *vis;
667    int min_red=0, min_green=0, min_blue=0;
668    GLboolean rgb_flag = rgbModeDefault;
669    GLboolean alpha_flag = GL_FALSE;
670    GLboolean double_flag = GL_FALSE;
671    GLboolean stereo_flag = GL_FALSE;
672    GLint depth_size = 0;
673    GLint stencil_size = 0;
674    GLint accumRedSize = 0;
675    GLint accumGreenSize = 0;
676    GLint accumBlueSize = 0;
677    GLint accumAlphaSize = 0;
678    int level = 0;
679    int visual_type = DONT_CARE;
680    GLint caveat = DONT_CARE;
681    XMesaVisual xmvis = NULL;
682    int desiredVisualID = -1;
683    int numAux = 0;
684
685    xmesa_init( dpy );
686
687    parselist = list;
688
689    while (*parselist) {
690
691       if (fbConfig &&
692           parselist[1] == GLX_DONT_CARE &&
693           parselist[0] != GLX_LEVEL) {
694          /* For glXChooseFBConfig(), skip attributes whose value is
695           * GLX_DONT_CARE, unless it's GLX_LEVEL (which can legitimately be
696           * a negative value).
697           *
698           * From page 17 (23 of the pdf) of the GLX 1.4 spec:
699           * GLX DONT CARE may be specified for all attributes except GLX LEVEL.
700           */
701          parselist += 2;
702          continue;
703       }
704
705       switch (*parselist) {
706          case GLX_USE_GL:
707             if (fbConfig) {
708                /* invalid token */
709                return NULL;
710             }
711             else {
712                /* skip */
713                parselist++;
714             }
715             break;
716          case GLX_BUFFER_SIZE:
717             parselist++;
718             parselist++;
719             break;
720          case GLX_LEVEL:
721             parselist++;
722             level = *parselist++;
723             break;
724          case GLX_RGBA:
725             if (fbConfig) {
726                /* invalid token */
727                return NULL;
728             }
729             else {
730                rgb_flag = GL_TRUE;
731                parselist++;
732             }
733             break;
734          case GLX_DOUBLEBUFFER:
735             parselist++;
736             if (fbConfig) {
737                double_flag = *parselist++;
738             }
739             else {
740                double_flag = GL_TRUE;
741             }
742             break;
743          case GLX_STEREO:
744             parselist++;
745             if (fbConfig) {
746                stereo_flag = *parselist++;
747             }
748             else {
749                stereo_flag = GL_TRUE;
750             }
751             break;
752          case GLX_AUX_BUFFERS:
753             parselist++;
754             numAux = *parselist++;
755             if (numAux > MAX_AUX_BUFFERS)
756                return NULL;
757             break;
758          case GLX_RED_SIZE:
759             parselist++;
760             min_red = *parselist++;
761             break;
762          case GLX_GREEN_SIZE:
763             parselist++;
764             min_green = *parselist++;
765             break;
766          case GLX_BLUE_SIZE:
767             parselist++;
768             min_blue = *parselist++;
769             break;
770          case GLX_ALPHA_SIZE:
771             parselist++;
772             {
773                GLint size = *parselist++;
774                alpha_flag = size ? GL_TRUE : GL_FALSE;
775             }
776             break;
777          case GLX_DEPTH_SIZE:
778             parselist++;
779             depth_size = *parselist++;
780             break;
781          case GLX_STENCIL_SIZE:
782             parselist++;
783             stencil_size = *parselist++;
784             break;
785          case GLX_ACCUM_RED_SIZE:
786             parselist++;
787             {
788                GLint size = *parselist++;
789                accumRedSize = MAX2( accumRedSize, size );
790             }
791             break;
792          case GLX_ACCUM_GREEN_SIZE:
793             parselist++;
794             {
795                GLint size = *parselist++;
796                accumGreenSize = MAX2( accumGreenSize, size );
797             }
798             break;
799          case GLX_ACCUM_BLUE_SIZE:
800             parselist++;
801             {
802                GLint size = *parselist++;
803                accumBlueSize = MAX2( accumBlueSize, size );
804             }
805             break;
806          case GLX_ACCUM_ALPHA_SIZE:
807             parselist++;
808             {
809                GLint size = *parselist++;
810                accumAlphaSize = MAX2( accumAlphaSize, size );
811             }
812             break;
813
814          /*
815           * GLX_EXT_visual_info extension
816           */
817          case GLX_X_VISUAL_TYPE_EXT:
818             parselist++;
819             visual_type = *parselist++;
820             break;
821          case GLX_TRANSPARENT_TYPE_EXT:
822             parselist++;
823             parselist++;
824             break;
825          case GLX_TRANSPARENT_INDEX_VALUE_EXT:
826             parselist++;
827             parselist++;
828             break;
829          case GLX_TRANSPARENT_RED_VALUE_EXT:
830          case GLX_TRANSPARENT_GREEN_VALUE_EXT:
831          case GLX_TRANSPARENT_BLUE_VALUE_EXT:
832          case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
833             /* ignore */
834             parselist++;
835             parselist++;
836             break;
837
838          /*
839           * GLX_EXT_visual_info extension
840           */
841          case GLX_VISUAL_CAVEAT_EXT:
842             parselist++;
843             caveat = *parselist++; /* ignored for now */
844             break;
845
846          /*
847           * GLX_ARB_multisample
848           */
849          case GLX_SAMPLE_BUFFERS_ARB:
850          case GLX_SAMPLES_ARB:
851             parselist++;
852             if (*parselist++ != 0) {
853                /* ms not supported */
854                return NULL;
855             }
856             break;
857
858          /*
859           * FBConfig attribs.
860           */
861          case GLX_RENDER_TYPE:
862             if (!fbConfig)
863                return NULL;
864             parselist++;
865             if (*parselist & GLX_RGBA_BIT) {
866                rgb_flag = GL_TRUE;
867             }
868             else if (*parselist & GLX_COLOR_INDEX_BIT) {
869                rgb_flag = GL_FALSE;
870             }
871             else if (*parselist == 0) {
872                rgb_flag = GL_TRUE;
873             }
874             parselist++;
875             break;
876          case GLX_DRAWABLE_TYPE:
877             if (!fbConfig)
878                return NULL;
879             parselist++;
880             if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
881                return NULL; /* bad bit */
882             }
883             parselist++;
884             break;
885          case GLX_FBCONFIG_ID:
886          case GLX_VISUAL_ID:
887             if (!fbConfig)
888                return NULL;
889             parselist++;
890             desiredVisualID = *parselist++;
891             break;
892          case GLX_X_RENDERABLE:
893          case GLX_MAX_PBUFFER_WIDTH:
894          case GLX_MAX_PBUFFER_HEIGHT:
895          case GLX_MAX_PBUFFER_PIXELS:
896             if (!fbConfig)
897                return NULL; /* invalid config option */
898             parselist += 2; /* ignore the parameter */
899             break;
900
901 #ifdef GLX_EXT_texture_from_pixmap
902          case GLX_BIND_TO_TEXTURE_RGB_EXT:
903             parselist++; /*skip*/
904             break;
905          case GLX_BIND_TO_TEXTURE_RGBA_EXT:
906             parselist++; /*skip*/
907             break;
908          case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
909             parselist++; /*skip*/
910             break;
911          case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
912             parselist++;
913             if (*parselist & ~(GLX_TEXTURE_1D_BIT_EXT |
914                                GLX_TEXTURE_2D_BIT_EXT |
915                                GLX_TEXTURE_RECTANGLE_BIT_EXT)) {
916                /* invalid bit */
917                return NULL;
918             }
919             break;
920          case GLX_Y_INVERTED_EXT:
921             parselist++; /*skip*/
922             break;
923 #endif
924
925          case None:
926             /* end of list */
927             break;
928
929          default:
930             /* undefined attribute */
931             _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
932                           *parselist);
933             return NULL;
934       }
935    }
936
937    (void) caveat;
938
939
940    /*
941     * Since we're only simulating the GLX extension this function will never
942     * find any real GL visuals.  Instead, all we can do is try to find an RGB
943     * or CI visual of appropriate depth.  Other requested attributes such as
944     * double buffering, depth buffer, etc. will be associated with the X
945     * visual and stored in the VisualTable[].
946     */
947    if (desiredVisualID != -1) {
948       /* try to get a specific visual, by visualID */
949       XVisualInfo temp;
950       int n;
951       temp.visualid = desiredVisualID;
952       temp.screen = screen;
953       vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
954       if (vis) {
955          /* give the visual some useful GLX attributes */
956          double_flag = GL_TRUE;
957          rgb_flag = GL_TRUE;
958       }
959    }
960    else if (level==0) {
961       /* normal color planes */
962       /* Get an RGB visual */
963       int min_rgb = min_red + min_green + min_blue;
964       if (min_rgb>1 && min_rgb<8) {
965          /* a special case to be sure we can get a monochrome visual */
966          min_rgb = 1;
967       }
968       vis = choose_x_visual( dpy, screen, min_rgb, visual_type );
969    }
970    else {
971       _mesa_warning(NULL, "overlay not supported");
972       return NULL;
973    }
974
975    if (vis) {
976       /* Note: we're not exactly obeying the glXChooseVisual rules here.
977        * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
978        * largest depth buffer size, which is 32bits/value.  Instead, we
979        * return 16 to maintain performance with earlier versions of Mesa.
980        */
981       if (stencil_size > 0)
982          depth_size = 24;  /* if Z and stencil, always use 24+8 format */
983       else if (depth_size > 24)
984          depth_size = 32;
985       else if (depth_size > 16)
986          depth_size = 24;
987       else if (depth_size > 0) {
988          depth_size = default_depth_bits();
989       }
990
991       if (!alpha_flag) {
992          alpha_flag = default_alpha_bits() > 0;
993       }
994
995       /* we only support one size of stencil and accum buffers. */
996       if (stencil_size > 0)
997          stencil_size = 8;
998
999       if (accumRedSize > 0 || 
1000           accumGreenSize > 0 || 
1001           accumBlueSize > 0 ||
1002           accumAlphaSize > 0) {
1003
1004          accumRedSize = 
1005             accumGreenSize = 
1006             accumBlueSize = default_accum_bits();
1007
1008          accumAlphaSize = alpha_flag ? accumRedSize : 0;
1009       }
1010
1011       xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1012                                stereo_flag, depth_size, stencil_size,
1013                                accumRedSize, accumGreenSize,
1014                                accumBlueSize, accumAlphaSize, level, numAux );
1015    }
1016
1017    return xmvis;
1018 }
1019
1020
1021 PUBLIC XVisualInfo *
1022 glXChooseVisual( Display *dpy, int screen, int *list )
1023 {
1024    XMesaVisual xmvis;
1025
1026    /* register ourselves as an extension on this display */
1027    register_with_display(dpy);
1028
1029    xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1030    if (xmvis) {
1031       /* create a new vishandle - the cached one may be stale */
1032       xmvis->vishandle = (XVisualInfo *) malloc(sizeof(XVisualInfo));
1033       if (xmvis->vishandle) {
1034          memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1035       }
1036       return xmvis->vishandle;
1037    }
1038    else
1039       return NULL;
1040 }
1041
1042
1043 /**
1044  * Helper function used by other glXCreateContext functions.
1045  */
1046 static GLXContext
1047 create_context(Display *dpy, XMesaVisual xmvis,
1048                XMesaContext shareCtx, Bool direct,
1049                unsigned major, unsigned minor,
1050                unsigned profileMask, unsigned contextFlags)
1051 {
1052    GLXContext glxCtx;
1053
1054    if (!dpy || !xmvis)
1055       return 0;
1056
1057    glxCtx = CALLOC_STRUCT(__GLXcontextRec);
1058    if (!glxCtx)
1059       return 0;
1060
1061    /* deallocate unused windows/buffers */
1062 #if 0
1063    XMesaGarbageCollect();
1064 #endif
1065
1066    glxCtx->xmesaContext = XMesaCreateContext(xmvis, shareCtx, major, minor,
1067                                              profileMask, contextFlags);
1068    if (!glxCtx->xmesaContext) {
1069       free(glxCtx);
1070       return NULL;
1071    }
1072
1073    glxCtx->isDirect = DEFAULT_DIRECT;
1074    glxCtx->currentDpy = dpy;
1075    glxCtx->xid = (XID) glxCtx;  /* self pointer */
1076
1077    return glxCtx;
1078 }
1079
1080
1081 PUBLIC GLXContext
1082 glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1083                   GLXContext shareCtx, Bool direct )
1084 {
1085    XMesaVisual xmvis;
1086
1087    xmvis = find_glx_visual( dpy, visinfo );
1088    if (!xmvis) {
1089       /* This visual wasn't found with glXChooseVisual() */
1090       xmvis = create_glx_visual( dpy, visinfo );
1091       if (!xmvis) {
1092          /* unusable visual */
1093          return NULL;
1094       }
1095    }
1096
1097    return create_context(dpy, xmvis,
1098                          shareCtx ? shareCtx->xmesaContext : NULL,
1099                          direct,
1100                          1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
1101 }
1102
1103
1104 /* XXX these may have to be removed due to thread-safety issues. */
1105 static GLXContext MakeCurrent_PrevContext = 0;
1106 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1107 static GLXDrawable MakeCurrent_PrevReadable = 0;
1108 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1109 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1110
1111
1112 /* GLX 1.3 and later */
1113 PUBLIC Bool
1114 glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1115                        GLXDrawable read, GLXContext ctx )
1116 {
1117    GLXContext glxCtx = ctx;
1118    static boolean firsttime = 1, no_rast = 0;
1119
1120    if (firsttime) {
1121       no_rast = getenv("SP_NO_RAST") != NULL;
1122       firsttime = 0;
1123    }
1124
1125    if (ctx && draw && read) {
1126       XMesaBuffer drawBuffer, readBuffer;
1127       XMesaContext xmctx = glxCtx->xmesaContext;
1128
1129       /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1130       if (ctx == MakeCurrent_PrevContext
1131           && draw == MakeCurrent_PrevDrawable) {
1132          drawBuffer = MakeCurrent_PrevDrawBuffer;
1133       }
1134       else {
1135          drawBuffer = XMesaFindBuffer( dpy, draw );
1136       }
1137       if (!drawBuffer) {
1138          /* drawable must be a new window! */
1139          drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1140          if (!drawBuffer) {
1141             /* Out of memory, or context/drawable depth mismatch */
1142             return False;
1143          }
1144       }
1145
1146       /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1147       if (ctx == MakeCurrent_PrevContext
1148           && read == MakeCurrent_PrevReadable) {
1149          readBuffer = MakeCurrent_PrevReadBuffer;
1150       }
1151       else {
1152          readBuffer = XMesaFindBuffer( dpy, read );
1153       }
1154       if (!readBuffer) {
1155          /* drawable must be a new window! */
1156          readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1157          if (!readBuffer) {
1158             /* Out of memory, or context/drawable depth mismatch */
1159             return False;
1160          }
1161       }
1162
1163       if (no_rast &&
1164           MakeCurrent_PrevContext == ctx &&
1165           MakeCurrent_PrevDrawable == draw &&
1166           MakeCurrent_PrevReadable == read &&
1167           MakeCurrent_PrevDrawBuffer == drawBuffer &&
1168           MakeCurrent_PrevReadBuffer == readBuffer)
1169          return True;
1170           
1171       MakeCurrent_PrevContext = ctx;
1172       MakeCurrent_PrevDrawable = draw;
1173       MakeCurrent_PrevReadable = read;
1174       MakeCurrent_PrevDrawBuffer = drawBuffer;
1175       MakeCurrent_PrevReadBuffer = readBuffer;
1176
1177       /* Now make current! */
1178       if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1179          ctx->currentDpy = dpy;
1180          ctx->currentDrawable = draw;
1181          ctx->currentReadable = read;
1182          SetCurrentContext(ctx);
1183          return True;
1184       }
1185       else {
1186          return False;
1187       }
1188    }
1189    else if (!ctx && !draw && !read) {
1190       /* release current context w/out assigning new one. */
1191       XMesaMakeCurrent2( NULL, NULL, NULL );
1192       MakeCurrent_PrevContext = 0;
1193       MakeCurrent_PrevDrawable = 0;
1194       MakeCurrent_PrevReadable = 0;
1195       MakeCurrent_PrevDrawBuffer = 0;
1196       MakeCurrent_PrevReadBuffer = 0;
1197       SetCurrentContext(NULL);
1198       return True;
1199    }
1200    else {
1201       /* The args must either all be non-zero or all zero.
1202        * This is an error.
1203        */
1204       return False;
1205    }
1206 }
1207
1208
1209 PUBLIC Bool
1210 glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1211 {
1212    return glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1213 }
1214
1215
1216 PUBLIC GLXContext
1217 glXGetCurrentContext(void)
1218 {
1219    return GetCurrentContext();
1220 }
1221
1222
1223 PUBLIC Display *
1224 glXGetCurrentDisplay(void)
1225 {
1226    GLXContext glxCtx = glXGetCurrentContext();
1227
1228    return glxCtx ? glxCtx->currentDpy : NULL;
1229 }
1230
1231
1232 PUBLIC Display *
1233 glXGetCurrentDisplayEXT(void)
1234 {
1235    return glXGetCurrentDisplay();
1236 }
1237
1238
1239 PUBLIC GLXDrawable
1240 glXGetCurrentDrawable(void)
1241 {
1242    GLXContext gc = glXGetCurrentContext();
1243    return gc ? gc->currentDrawable : 0;
1244 }
1245
1246
1247 PUBLIC GLXDrawable
1248 glXGetCurrentReadDrawable(void)
1249 {
1250    GLXContext gc = glXGetCurrentContext();
1251    return gc ? gc->currentReadable : 0;
1252 }
1253
1254
1255 PUBLIC GLXDrawable
1256 glXGetCurrentReadDrawableSGI(void)
1257 {
1258    return glXGetCurrentReadDrawable();
1259 }
1260
1261
1262 PUBLIC GLXPixmap
1263 glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1264 {
1265    XMesaVisual v;
1266    XMesaBuffer b;
1267
1268    v = find_glx_visual( dpy, visinfo );
1269    if (!v) {
1270       v = create_glx_visual( dpy, visinfo );
1271       if (!v) {
1272          /* unusable visual */
1273          return 0;
1274       }
1275    }
1276
1277    b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1278    if (!b) {
1279       return 0;
1280    }
1281    return b->ws.drawable;
1282 }
1283
1284
1285 /*** GLX_MESA_pixmap_colormap ***/
1286
1287 PUBLIC GLXPixmap
1288 glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1289                         Pixmap pixmap, Colormap cmap )
1290 {
1291    XMesaVisual v;
1292    XMesaBuffer b;
1293
1294    v = find_glx_visual( dpy, visinfo );
1295    if (!v) {
1296       v = create_glx_visual( dpy, visinfo );
1297       if (!v) {
1298          /* unusable visual */
1299          return 0;
1300       }
1301    }
1302
1303    b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1304    if (!b) {
1305       return 0;
1306    }
1307    return b->ws.drawable;
1308 }
1309
1310
1311 PUBLIC void
1312 glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1313 {
1314    XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1315    if (b) {
1316       XMesaDestroyBuffer(b);
1317    }
1318    else if (_mesa_getenv("MESA_DEBUG")) {
1319       _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1320    }
1321 }
1322
1323
1324 PUBLIC void
1325 glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1326                 unsigned long mask )
1327 {
1328    XMesaContext xm_src = src->xmesaContext;
1329    XMesaContext xm_dst = dst->xmesaContext;
1330    (void) dpy;
1331    if (MakeCurrent_PrevContext == src) {
1332       glFlush();
1333    }
1334    XMesaCopyContext(xm_src, xm_dst, mask);
1335 }
1336
1337
1338 PUBLIC Bool
1339 glXQueryExtension( Display *dpy, int *errorBase, int *eventBase )
1340 {
1341    int op, ev, err;
1342    /* Mesa's GLX isn't really an X extension but we try to act like one. */
1343    if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &op, &ev, &err))
1344       ev = err = 0;
1345    if (errorBase)
1346       *errorBase = err;
1347    if (eventBase)
1348       *eventBase = ev;
1349    return True; /* we're faking GLX so always return success */
1350 }
1351
1352
1353 PUBLIC void
1354 glXDestroyContext( Display *dpy, GLXContext ctx )
1355 {
1356    if (ctx) {
1357       GLXContext glxCtx = ctx;
1358       (void) dpy;
1359       MakeCurrent_PrevContext = 0;
1360       MakeCurrent_PrevDrawable = 0;
1361       MakeCurrent_PrevReadable = 0;
1362       MakeCurrent_PrevDrawBuffer = 0;
1363       MakeCurrent_PrevReadBuffer = 0;
1364       XMesaDestroyContext( glxCtx->xmesaContext );
1365       XMesaGarbageCollect();
1366       free(glxCtx);
1367    }
1368 }
1369
1370
1371 PUBLIC Bool
1372 glXIsDirect( Display *dpy, GLXContext ctx )
1373 {
1374    return ctx ? ctx->isDirect : False;
1375 }
1376
1377
1378
1379 PUBLIC void
1380 glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1381 {
1382    XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1383    static boolean firsttime = 1, no_rast = 0;
1384
1385    if (firsttime) {
1386       no_rast = getenv("SP_NO_RAST") != NULL;
1387       firsttime = 0;
1388    }
1389
1390    if (no_rast)
1391       return;
1392
1393    if (buffer) {
1394       XMesaSwapBuffers(buffer);
1395    }
1396    else if (_mesa_getenv("MESA_DEBUG")) {
1397       _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1398                     (int) drawable);
1399    }
1400 }
1401
1402
1403
1404 /*** GLX_MESA_copy_sub_buffer ***/
1405
1406 PUBLIC void
1407 glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
1408                      int x, int y, int width, int height)
1409 {
1410    XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1411    if (buffer) {
1412       XMesaCopySubBuffer(buffer, x, y, width, height);
1413    }
1414    else if (_mesa_getenv("MESA_DEBUG")) {
1415       _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1416    }
1417 }
1418
1419
1420 PUBLIC Bool
1421 glXQueryVersion( Display *dpy, int *maj, int *min )
1422 {
1423    (void) dpy;
1424    /* Return GLX version, not Mesa version */
1425    assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1426    *maj = CLIENT_MAJOR_VERSION;
1427    *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1428    return True;
1429 }
1430
1431
1432 /*
1433  * Query the GLX attributes of the given XVisualInfo.
1434  */
1435 static int
1436 get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1437 {
1438    ASSERT(xmvis);
1439    switch(attrib) {
1440       case GLX_USE_GL:
1441          if (fbconfig)
1442             return GLX_BAD_ATTRIBUTE;
1443          *value = (int) True;
1444          return 0;
1445       case GLX_BUFFER_SIZE:
1446          *value = xmvis->visinfo->depth;
1447          return 0;
1448       case GLX_LEVEL:
1449          *value = xmvis->mesa_visual.level;
1450          return 0;
1451       case GLX_RGBA:
1452          if (fbconfig)
1453             return GLX_BAD_ATTRIBUTE;
1454          if (xmvis->mesa_visual.rgbMode) {
1455             *value = True;
1456          }
1457          else {
1458             *value = False;
1459          }
1460          return 0;
1461       case GLX_DOUBLEBUFFER:
1462          *value = (int) xmvis->mesa_visual.doubleBufferMode;
1463          return 0;
1464       case GLX_STEREO:
1465          *value = (int) xmvis->mesa_visual.stereoMode;
1466          return 0;
1467       case GLX_AUX_BUFFERS:
1468          *value = xmvis->mesa_visual.numAuxBuffers;
1469          return 0;
1470       case GLX_RED_SIZE:
1471          *value = xmvis->mesa_visual.redBits;
1472          return 0;
1473       case GLX_GREEN_SIZE:
1474          *value = xmvis->mesa_visual.greenBits;
1475          return 0;
1476       case GLX_BLUE_SIZE:
1477          *value = xmvis->mesa_visual.blueBits;
1478          return 0;
1479       case GLX_ALPHA_SIZE:
1480          *value = xmvis->mesa_visual.alphaBits;
1481          return 0;
1482       case GLX_DEPTH_SIZE:
1483          *value = xmvis->mesa_visual.depthBits;
1484          return 0;
1485       case GLX_STENCIL_SIZE:
1486          *value = xmvis->mesa_visual.stencilBits;
1487          return 0;
1488       case GLX_ACCUM_RED_SIZE:
1489          *value = xmvis->mesa_visual.accumRedBits;
1490          return 0;
1491       case GLX_ACCUM_GREEN_SIZE:
1492          *value = xmvis->mesa_visual.accumGreenBits;
1493          return 0;
1494       case GLX_ACCUM_BLUE_SIZE:
1495          *value = xmvis->mesa_visual.accumBlueBits;
1496          return 0;
1497       case GLX_ACCUM_ALPHA_SIZE:
1498          *value = xmvis->mesa_visual.accumAlphaBits;
1499          return 0;
1500
1501       /*
1502        * GLX_EXT_visual_info extension
1503        */
1504       case GLX_X_VISUAL_TYPE_EXT:
1505          switch (xmvis->visinfo->CLASS) {
1506             case StaticGray:   *value = GLX_STATIC_GRAY_EXT;   return 0;
1507             case GrayScale:    *value = GLX_GRAY_SCALE_EXT;    return 0;
1508             case StaticColor:  *value = GLX_STATIC_GRAY_EXT;   return 0;
1509             case PseudoColor:  *value = GLX_PSEUDO_COLOR_EXT;  return 0;
1510             case TrueColor:    *value = GLX_TRUE_COLOR_EXT;    return 0;
1511             case DirectColor:  *value = GLX_DIRECT_COLOR_EXT;  return 0;
1512          }
1513          return 0;
1514       case GLX_TRANSPARENT_TYPE_EXT:
1515          /* normal planes */
1516          *value = GLX_NONE_EXT;
1517          return 0;
1518       case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1519          /* undefined */
1520          return 0;
1521       case GLX_TRANSPARENT_RED_VALUE_EXT:
1522          /* undefined */
1523          return 0;
1524       case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1525          /* undefined */
1526          return 0;
1527       case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1528          /* undefined */
1529          return 0;
1530       case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1531          /* undefined */
1532          return 0;
1533
1534       /*
1535        * GLX_EXT_visual_info extension
1536        */
1537       case GLX_VISUAL_CAVEAT_EXT:
1538          /* test for zero, just in case */
1539          if (xmvis->mesa_visual.visualRating > 0)
1540             *value = xmvis->mesa_visual.visualRating;
1541          else
1542             *value = GLX_NONE_EXT;
1543          return 0;
1544
1545       /*
1546        * GLX_ARB_multisample
1547        */
1548       case GLX_SAMPLE_BUFFERS_ARB:
1549          *value = 0;
1550          return 0;
1551       case GLX_SAMPLES_ARB:
1552          *value = 0;
1553          return 0;
1554
1555       /*
1556        * For FBConfigs:
1557        */
1558       case GLX_SCREEN_EXT:
1559          if (!fbconfig)
1560             return GLX_BAD_ATTRIBUTE;
1561          *value = xmvis->visinfo->screen;
1562          break;
1563       case GLX_DRAWABLE_TYPE: /*SGIX too */
1564          if (!fbconfig)
1565             return GLX_BAD_ATTRIBUTE;
1566          *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1567          break;
1568       case GLX_RENDER_TYPE_SGIX:
1569          if (!fbconfig)
1570             return GLX_BAD_ATTRIBUTE;
1571          if (xmvis->mesa_visual.rgbMode)
1572             *value = GLX_RGBA_BIT;
1573          else
1574             *value = GLX_COLOR_INDEX_BIT;
1575          break;
1576       case GLX_X_RENDERABLE_SGIX:
1577          if (!fbconfig)
1578             return GLX_BAD_ATTRIBUTE;
1579          *value = True; /* XXX really? */
1580          break;
1581       case GLX_FBCONFIG_ID_SGIX:
1582          if (!fbconfig)
1583             return GLX_BAD_ATTRIBUTE;
1584          *value = xmvis->visinfo->visualid;
1585          break;
1586       case GLX_MAX_PBUFFER_WIDTH:
1587          if (!fbconfig)
1588             return GLX_BAD_ATTRIBUTE;
1589          /* XXX or MAX_WIDTH? */
1590          *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1591          break;
1592       case GLX_MAX_PBUFFER_HEIGHT:
1593          if (!fbconfig)
1594             return GLX_BAD_ATTRIBUTE;
1595          *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1596          break;
1597       case GLX_MAX_PBUFFER_PIXELS:
1598          if (!fbconfig)
1599             return GLX_BAD_ATTRIBUTE;
1600          *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1601                   DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1602          break;
1603       case GLX_VISUAL_ID:
1604          if (!fbconfig)
1605             return GLX_BAD_ATTRIBUTE;
1606          *value = xmvis->visinfo->visualid;
1607          break;
1608
1609 #ifdef GLX_EXT_texture_from_pixmap
1610       case GLX_BIND_TO_TEXTURE_RGB_EXT:
1611          *value = True; /*XXX*/
1612          break;
1613       case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1614          /* XXX review */
1615          *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1616          break;
1617       case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1618          *value = True; /*XXX*/
1619          break;
1620       case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1621          *value = (GLX_TEXTURE_1D_BIT_EXT |
1622                    GLX_TEXTURE_2D_BIT_EXT |
1623                    GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1624          break;
1625       case GLX_Y_INVERTED_EXT:
1626          *value = True; /*XXX*/
1627          break;
1628 #endif
1629
1630       default:
1631          return GLX_BAD_ATTRIBUTE;
1632    }
1633    return Success;
1634 }
1635
1636
1637 PUBLIC int
1638 glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1639                    int attrib, int *value )
1640 {
1641    XMesaVisual xmvis;
1642    int k;
1643    if (!dpy || !visinfo)
1644       return GLX_BAD_ATTRIBUTE;
1645
1646    xmvis = find_glx_visual( dpy, visinfo );
1647    if (!xmvis) {
1648       /* this visual wasn't obtained with glXChooseVisual */
1649       xmvis = create_glx_visual( dpy, visinfo );
1650       if (!xmvis) {
1651          /* this visual can't be used for GL rendering */
1652          if (attrib==GLX_USE_GL) {
1653             *value = (int) False;
1654             return 0;
1655          }
1656          else {
1657             return GLX_BAD_VISUAL;
1658          }
1659       }
1660    }
1661
1662    k = get_config(xmvis, attrib, value, GL_FALSE);
1663    return k;
1664 }
1665
1666
1667 PUBLIC void
1668 glXWaitGL( void )
1669 {
1670    XMesaContext xmesa = XMesaGetCurrentContext();
1671    XMesaFlush( xmesa );
1672 }
1673
1674
1675
1676 PUBLIC void
1677 glXWaitX( void )
1678 {
1679    XMesaContext xmesa = XMesaGetCurrentContext();
1680    XMesaFlush( xmesa );
1681 }
1682
1683
1684 static const char *
1685 get_extensions( void )
1686 {
1687    return EXTENSIONS;
1688 }
1689
1690
1691
1692 /* GLX 1.1 and later */
1693 PUBLIC const char *
1694 glXQueryExtensionsString( Display *dpy, int screen )
1695 {
1696    (void) dpy;
1697    (void) screen;
1698    return get_extensions();
1699 }
1700
1701
1702
1703 /* GLX 1.1 and later */
1704 PUBLIC const char *
1705 glXQueryServerString( Display *dpy, int screen, int name )
1706 {
1707    static char version[1000];
1708    sprintf(version, "%d.%d %s",
1709            SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, xmesa_get_name());
1710
1711    (void) dpy;
1712    (void) screen;
1713
1714    switch (name) {
1715       case GLX_EXTENSIONS:
1716          return get_extensions();
1717       case GLX_VENDOR:
1718          return VENDOR;
1719       case GLX_VERSION:
1720          return version;
1721       default:
1722          return NULL;
1723    }
1724 }
1725
1726
1727
1728 /* GLX 1.1 and later */
1729 PUBLIC const char *
1730 glXGetClientString( Display *dpy, int name )
1731 {
1732    static char version[1000];
1733    sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
1734            CLIENT_MINOR_VERSION, xmesa_get_name());
1735
1736    (void) dpy;
1737
1738    switch (name) {
1739       case GLX_EXTENSIONS:
1740          return get_extensions();
1741       case GLX_VENDOR:
1742          return VENDOR;
1743       case GLX_VERSION:
1744          return version;
1745       default:
1746          return NULL;
1747    }
1748 }
1749
1750
1751
1752 /*
1753  * GLX 1.3 and later
1754  */
1755
1756
1757 PUBLIC int
1758 glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1759                      int attribute, int *value)
1760 {
1761    XMesaVisual v = (XMesaVisual) config;
1762    (void) dpy;
1763    (void) config;
1764
1765    if (!dpy || !config || !value)
1766       return -1;
1767
1768    return get_config(v, attribute, value, GL_TRUE);
1769 }
1770
1771
1772 PUBLIC GLXFBConfig *
1773 glXGetFBConfigs( Display *dpy, int screen, int *nelements )
1774 {
1775    XVisualInfo *visuals, visTemplate;
1776    const long visMask = VisualScreenMask;
1777    int i;
1778
1779    /* Get list of all X visuals */
1780    visTemplate.screen = screen;
1781    visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
1782    if (*nelements > 0) {
1783       XMesaVisual *results;
1784       results = (XMesaVisual *) malloc(*nelements * sizeof(XMesaVisual));
1785       if (!results) {
1786          *nelements = 0;
1787          return NULL;
1788       }
1789       for (i = 0; i < *nelements; i++) {
1790          results[i] = create_glx_visual(dpy, visuals + i);
1791          if (!results[i]) {
1792             *nelements = i;
1793             break;
1794          }
1795       }
1796       return (GLXFBConfig *) results;
1797    }
1798    return NULL;
1799 }
1800
1801
1802 PUBLIC GLXFBConfig *
1803 glXChooseFBConfig(Display *dpy, int screen,
1804                   const int *attribList, int *nitems)
1805 {
1806    XMesaVisual xmvis;
1807
1808    /* register ourselves as an extension on this display */
1809    register_with_display(dpy);
1810
1811    if (!attribList || !attribList[0]) {
1812       /* return list of all configs (per GLX_SGIX_fbconfig spec) */
1813       return glXGetFBConfigs(dpy, screen, nitems);
1814    }
1815
1816    xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
1817    if (xmvis) {
1818       GLXFBConfig *config = (GLXFBConfig *) malloc(sizeof(XMesaVisual));
1819       if (!config) {
1820          *nitems = 0;
1821          return NULL;
1822       }
1823       *nitems = 1;
1824       config[0] = (GLXFBConfig) xmvis;
1825       return (GLXFBConfig *) config;
1826    }
1827    else {
1828       *nitems = 0;
1829       return NULL;
1830    }
1831 }
1832
1833
1834 PUBLIC XVisualInfo *
1835 glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1836 {
1837    if (dpy && config) {
1838       XMesaVisual xmvis = (XMesaVisual) config;
1839 #if 0      
1840       return xmvis->vishandle;
1841 #else
1842       /* create a new vishandle - the cached one may be stale */
1843       xmvis->vishandle = (XVisualInfo *) malloc(sizeof(XVisualInfo));
1844       if (xmvis->vishandle) {
1845          memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1846       }
1847       return xmvis->vishandle;
1848 #endif
1849    }
1850    else {
1851       return NULL;
1852    }
1853 }
1854
1855
1856 PUBLIC GLXWindow
1857 glXCreateWindow(Display *dpy, GLXFBConfig config, Window win,
1858                 const int *attribList)
1859 {
1860    XMesaVisual xmvis = (XMesaVisual) config;
1861    XMesaBuffer xmbuf;
1862    if (!xmvis)
1863       return 0;
1864
1865    xmbuf = XMesaCreateWindowBuffer(xmvis, win);
1866    if (!xmbuf)
1867       return 0;
1868
1869    (void) dpy;
1870    (void) attribList;  /* Ignored in GLX 1.3 */
1871
1872    return win;  /* A hack for now */
1873 }
1874
1875
1876 PUBLIC void
1877 glXDestroyWindow( Display *dpy, GLXWindow window )
1878 {
1879    XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable) window);
1880    if (b)
1881       XMesaDestroyBuffer(b);
1882    /* don't destroy X window */
1883 }
1884
1885
1886 /* XXX untested */
1887 PUBLIC GLXPixmap
1888 glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap,
1889                 const int *attribList)
1890 {
1891    XMesaVisual v = (XMesaVisual) config;
1892    XMesaBuffer b;
1893    const int *attr;
1894    int target = 0, format = 0, mipmap = 0;
1895    int value;
1896
1897    if (!dpy || !config || !pixmap)
1898       return 0;
1899
1900    for (attr = attribList; attr && *attr; attr++) {
1901       switch (*attr) {
1902       case GLX_TEXTURE_FORMAT_EXT:
1903          attr++;
1904          switch (*attr) {
1905          case GLX_TEXTURE_FORMAT_NONE_EXT:
1906          case GLX_TEXTURE_FORMAT_RGB_EXT:
1907          case GLX_TEXTURE_FORMAT_RGBA_EXT:
1908             format = *attr;
1909             break;
1910          default:
1911             /* error */
1912             return 0;
1913          }
1914          break;
1915       case GLX_TEXTURE_TARGET_EXT:
1916          attr++;
1917          switch (*attr) {
1918          case GLX_TEXTURE_1D_EXT:
1919          case GLX_TEXTURE_2D_EXT:
1920          case GLX_TEXTURE_RECTANGLE_EXT:
1921             target = *attr;
1922             break;
1923          default:
1924             /* error */
1925             return 0;
1926          }
1927          break;
1928       case GLX_MIPMAP_TEXTURE_EXT:
1929          attr++;
1930          if (*attr)
1931             mipmap = 1;
1932          break;
1933       default:
1934          /* error */
1935          return 0;
1936       }
1937    }
1938
1939    if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
1940       if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
1941                      &value, GL_TRUE) != Success
1942           || !value) {
1943          return 0; /* error! */
1944       }
1945    }
1946    else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
1947       if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
1948                      &value, GL_TRUE) != Success
1949           || !value) {
1950          return 0; /* error! */
1951       }
1952    }
1953    if (mipmap) {
1954       if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
1955                      &value, GL_TRUE) != Success
1956           || !value) {
1957          return 0; /* error! */
1958       }
1959    }
1960    if (target == GLX_TEXTURE_1D_EXT) {
1961       if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
1962                      &value, GL_TRUE) != Success
1963           || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
1964          return 0; /* error! */
1965       }
1966    }
1967    else if (target == GLX_TEXTURE_2D_EXT) {
1968       if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
1969                      &value, GL_TRUE) != Success
1970           || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
1971          return 0; /* error! */
1972       }
1973    }
1974    if (target == GLX_TEXTURE_RECTANGLE_EXT) {
1975       if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
1976                      &value, GL_TRUE) != Success
1977           || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
1978          return 0; /* error! */
1979       }
1980    }
1981
1982    if (format || target || mipmap) {
1983       /* texture from pixmap */
1984       b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
1985    }
1986    else {
1987       b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1988    }
1989    if (!b) {
1990       return 0;
1991    }
1992
1993    return pixmap;
1994 }
1995
1996
1997 PUBLIC void
1998 glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
1999 {
2000    XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable)pixmap);
2001    if (b)
2002       XMesaDestroyBuffer(b);
2003    /* don't destroy X pixmap */
2004 }
2005
2006
2007 PUBLIC GLXPbuffer
2008 glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList)
2009 {
2010    XMesaVisual xmvis = (XMesaVisual) config;
2011    XMesaBuffer xmbuf;
2012    const int *attrib;
2013    int width = 0, height = 0;
2014    GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2015
2016    (void) dpy;
2017
2018    for (attrib = attribList; *attrib; attrib++) {
2019       switch (*attrib) {
2020          case GLX_PBUFFER_WIDTH:
2021             attrib++;
2022             width = *attrib;
2023             break;
2024          case GLX_PBUFFER_HEIGHT:
2025             attrib++;
2026             height = *attrib;
2027             break;
2028          case GLX_PRESERVED_CONTENTS:
2029             attrib++;
2030             preserveContents = *attrib;
2031             break;
2032          case GLX_LARGEST_PBUFFER:
2033             attrib++;
2034             useLargest = *attrib;
2035             break;
2036          default:
2037             return 0;
2038       }
2039    }
2040
2041    if (width == 0 || height == 0)
2042       return 0;
2043
2044    if (width > PBUFFER_MAX_SIZE || height > PBUFFER_MAX_SIZE) {
2045       /* If allocation would have failed and GLX_LARGEST_PBUFFER is set,
2046        * allocate the largest possible buffer.
2047        */
2048       if (useLargest) {
2049          width = PBUFFER_MAX_SIZE;
2050          height = PBUFFER_MAX_SIZE;
2051       }
2052    }
2053
2054    xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2055    /* A GLXPbuffer handle must be an X Drawable because that's what
2056     * glXMakeCurrent takes.
2057     */
2058    if (xmbuf) {
2059       xmbuf->largestPbuffer = useLargest;
2060       xmbuf->preservedContents = preserveContents;
2061       return (GLXPbuffer) xmbuf->ws.drawable;
2062    }
2063    else {
2064       return 0;
2065    }
2066 }
2067
2068
2069 PUBLIC void
2070 glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2071 {
2072    XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2073    if (b) {
2074       XMesaDestroyBuffer(b);
2075    }
2076 }
2077
2078
2079 PUBLIC void
2080 glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute,
2081                  unsigned int *value)
2082 {
2083    GLuint width, height;
2084    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2085    if (!xmbuf)
2086       return;
2087
2088    /* make sure buffer's dimensions are up to date */
2089    xmesa_get_window_size(dpy, xmbuf, &width, &height);
2090
2091    switch (attribute) {
2092       case GLX_WIDTH:
2093          *value = width;
2094          break;
2095       case GLX_HEIGHT:
2096          *value = height;
2097          break;
2098       case GLX_PRESERVED_CONTENTS:
2099          *value = xmbuf->preservedContents;
2100          break;
2101       case GLX_LARGEST_PBUFFER:
2102          *value = xmbuf->largestPbuffer;
2103          break;
2104       case GLX_FBCONFIG_ID:
2105          *value = xmbuf->xm_visual->visinfo->visualid;
2106          return;
2107 #ifdef GLX_EXT_texture_from_pixmap
2108       case GLX_TEXTURE_FORMAT_EXT:
2109          *value = xmbuf->TextureFormat;
2110          break;
2111       case GLX_TEXTURE_TARGET_EXT:
2112          *value = xmbuf->TextureTarget;
2113          break;
2114       case GLX_MIPMAP_TEXTURE_EXT:
2115          *value = xmbuf->TextureMipmap;
2116          break;
2117 #endif
2118
2119       default:
2120          return; /* raise BadValue error */
2121    }
2122 }
2123
2124
2125 PUBLIC GLXContext
2126 glXCreateNewContext( Display *dpy, GLXFBConfig config,
2127                      int renderType, GLXContext shareCtx, Bool direct )
2128 {
2129    XMesaVisual xmvis = (XMesaVisual) config;
2130
2131    if (!dpy || !config ||
2132        (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2133       return 0;
2134
2135    return create_context(dpy, xmvis,
2136                          shareCtx ? shareCtx->xmesaContext : NULL,
2137                          direct,
2138                          1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2139 }
2140
2141
2142 PUBLIC int
2143 glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
2144 {
2145    GLXContext glxCtx = ctx;
2146    XMesaContext xmctx = glxCtx->xmesaContext;
2147
2148    (void) dpy;
2149    (void) ctx;
2150
2151    switch (attribute) {
2152    case GLX_FBCONFIG_ID:
2153       *value = xmctx->xm_visual->visinfo->visualid;
2154       break;
2155    case GLX_RENDER_TYPE:
2156       if (xmctx->xm_visual->mesa_visual.rgbMode)
2157          *value = GLX_RGBA_TYPE;
2158       else
2159          *value = GLX_COLOR_INDEX_TYPE;
2160       break;
2161    case GLX_SCREEN:
2162       *value = 0;
2163       return Success;
2164    default:
2165       return GLX_BAD_ATTRIBUTE;
2166    }
2167    return 0;
2168 }
2169
2170
2171 PUBLIC void
2172 glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
2173 {
2174    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2175    if (xmbuf)
2176       xmbuf->selectedEvents = mask;
2177 }
2178
2179
2180 PUBLIC void
2181 glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
2182 {
2183    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2184    if (xmbuf)
2185       *mask = xmbuf->selectedEvents;
2186    else
2187       *mask = 0;
2188 }
2189
2190
2191
2192 /*** GLX_SGI_swap_control ***/
2193
2194 PUBLIC int
2195 glXSwapIntervalSGI(int interval)
2196 {
2197    (void) interval;
2198    return 0;
2199 }
2200
2201
2202
2203 /*** GLX_SGI_video_sync ***/
2204
2205 static unsigned int FrameCounter = 0;
2206
2207 PUBLIC int
2208 glXGetVideoSyncSGI(unsigned int *count)
2209 {
2210    /* this is a bogus implementation */
2211    *count = FrameCounter++;
2212    return 0;
2213 }
2214
2215 PUBLIC int
2216 glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2217 {
2218    if (divisor <= 0 || remainder < 0)
2219       return GLX_BAD_VALUE;
2220    /* this is a bogus implementation */
2221    FrameCounter++;
2222    while (FrameCounter % divisor != remainder)
2223       FrameCounter++;
2224    *count = FrameCounter;
2225    return 0;
2226 }
2227
2228
2229
2230 /*** GLX_SGI_make_current_read ***/
2231
2232 PUBLIC Bool
2233 glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read,
2234                       GLXContext ctx)
2235 {
2236    return glXMakeContextCurrent( dpy, draw, read, ctx );
2237 }
2238
2239 /* not used
2240 static GLXDrawable
2241 glXGetCurrentReadDrawableSGI(void)
2242 {
2243    return 0;
2244 }
2245 */
2246
2247
2248 /*** GLX_SGIX_video_source ***/
2249 #if defined(_VL_H)
2250
2251 PUBLIC GLXVideoSourceSGIX
2252 glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server,
2253                             VLPath path, int nodeClass, VLNode drainNode)
2254 {
2255    (void) dpy;
2256    (void) screen;
2257    (void) server;
2258    (void) path;
2259    (void) nodeClass;
2260    (void) drainNode;
2261    return 0;
2262 }
2263
2264 PUBLIC void
2265 glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
2266 {
2267    (void) dpy;
2268    (void) src;
2269 }
2270
2271 #endif
2272
2273
2274 /*** GLX_EXT_import_context ***/
2275
2276 PUBLIC void
2277 glXFreeContextEXT(Display *dpy, GLXContext context)
2278 {
2279    (void) dpy;
2280    (void) context;
2281 }
2282
2283 PUBLIC GLXContextID
2284 glXGetContextIDEXT(const GLXContext context)
2285 {
2286    (void) context;
2287    return 0;
2288 }
2289
2290 PUBLIC GLXContext
2291 glXImportContextEXT(Display *dpy, GLXContextID contextID)
2292 {
2293    (void) dpy;
2294    (void) contextID;
2295    return 0;
2296 }
2297
2298 PUBLIC int
2299 glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute,
2300                        int *value)
2301 {
2302    (void) dpy;
2303    (void) context;
2304    (void) attribute;
2305    (void) value;
2306    return 0;
2307 }
2308
2309
2310
2311 /*** GLX_SGIX_fbconfig ***/
2312
2313 PUBLIC int
2314 glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config,
2315                          int attribute, int *value)
2316 {
2317    return glXGetFBConfigAttrib(dpy, config, attribute, value);
2318 }
2319
2320 PUBLIC GLXFBConfigSGIX *
2321 glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list,
2322                       int *nelements)
2323 {
2324    return (GLXFBConfig *) glXChooseFBConfig(dpy, screen,
2325                                             attrib_list, nelements);
2326 }
2327
2328
2329 PUBLIC GLXPixmap
2330 glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2331                                  Pixmap pixmap)
2332 {
2333    XMesaVisual xmvis = (XMesaVisual) config;
2334    XMesaBuffer xmbuf = XMesaCreatePixmapBuffer(xmvis, pixmap, 0);
2335    return xmbuf->ws.drawable; /* need to return an X ID */
2336 }
2337
2338
2339 PUBLIC GLXContext
2340 glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2341                                int renderType, GLXContext shareCtx,
2342                                Bool direct)
2343 {
2344    XMesaVisual xmvis = (XMesaVisual) config;
2345
2346    if (!dpy || !config ||
2347        (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2348       return 0;
2349
2350    return create_context(dpy, xmvis,
2351                          shareCtx ? shareCtx->xmesaContext : NULL,
2352                          direct,
2353                          1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2354 }
2355
2356
2357 PUBLIC XVisualInfo *
2358 glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
2359 {
2360    return glXGetVisualFromFBConfig(dpy, config);
2361 }
2362
2363
2364 PUBLIC GLXFBConfigSGIX
2365 glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
2366 {
2367    XMesaVisual xmvis = find_glx_visual(dpy, vis);
2368    if (!xmvis) {
2369       /* This visual wasn't found with glXChooseVisual() */
2370       xmvis = create_glx_visual(dpy, vis);
2371    }
2372
2373    return (GLXFBConfigSGIX) xmvis;
2374 }
2375
2376
2377
2378 /*** GLX_SGIX_pbuffer ***/
2379
2380 PUBLIC GLXPbufferSGIX
2381 glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
2382                         unsigned int width, unsigned int height,
2383                         int *attribList)
2384 {
2385    XMesaVisual xmvis = (XMesaVisual) config;
2386    XMesaBuffer xmbuf;
2387    const int *attrib;
2388    GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2389
2390    (void) dpy;
2391
2392    for (attrib = attribList; attrib && *attrib; attrib++) {
2393       switch (*attrib) {
2394          case GLX_PRESERVED_CONTENTS_SGIX:
2395             attrib++;
2396             preserveContents = *attrib; /* ignored */
2397             break;
2398          case GLX_LARGEST_PBUFFER_SGIX:
2399             attrib++;
2400             useLargest = *attrib; /* ignored */
2401             break;
2402          default:
2403             return 0;
2404       }
2405    }
2406
2407    /* not used at this time */
2408    (void) useLargest;
2409    (void) preserveContents;
2410
2411    xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2412    /* A GLXPbuffer handle must be an X Drawable because that's what
2413     * glXMakeCurrent takes.
2414     */
2415    return (GLXPbuffer) xmbuf->ws.drawable;
2416 }
2417
2418
2419 PUBLIC void
2420 glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
2421 {
2422    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2423    if (xmbuf) {
2424       XMesaDestroyBuffer(xmbuf);
2425    }
2426 }
2427
2428
2429 PUBLIC int
2430 glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute,
2431                        unsigned int *value)
2432 {
2433    const XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2434
2435    if (!xmbuf) {
2436       /* Generate GLXBadPbufferSGIX for bad pbuffer */
2437       return 0;
2438    }
2439
2440    switch (attribute) {
2441       case GLX_PRESERVED_CONTENTS_SGIX:
2442          *value = True;
2443          break;
2444       case GLX_LARGEST_PBUFFER_SGIX:
2445          *value = xmesa_buffer_width(xmbuf) * xmesa_buffer_height(xmbuf);
2446          break;
2447       case GLX_WIDTH_SGIX:
2448          *value = xmesa_buffer_width(xmbuf);
2449          break;
2450       case GLX_HEIGHT_SGIX:
2451          *value = xmesa_buffer_height(xmbuf);
2452          break;
2453       case GLX_EVENT_MASK_SGIX:
2454          *value = 0;  /* XXX might be wrong */
2455          break;
2456       default:
2457          *value = 0;
2458    }
2459    return 0;
2460 }
2461
2462
2463 PUBLIC void
2464 glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
2465 {
2466    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2467    if (xmbuf) {
2468       /* Note: we'll never generate clobber events */
2469       xmbuf->selectedEvents = mask;
2470    }
2471 }
2472
2473
2474 PUBLIC void
2475 glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable,
2476                         unsigned long *mask)
2477 {
2478    XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2479    if (xmbuf) {
2480       *mask = xmbuf->selectedEvents;
2481    }
2482    else {
2483       *mask = 0;
2484    }
2485 }
2486
2487
2488
2489 /*** GLX_SGI_cushion ***/
2490
2491 PUBLIC void
2492 glXCushionSGI(Display *dpy, Window win, float cushion)
2493 {
2494    (void) dpy;
2495    (void) win;
2496    (void) cushion;
2497 }
2498
2499
2500
2501 /*** GLX_SGIX_video_resize ***/
2502
2503 PUBLIC int
2504 glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel,
2505                            Window window)
2506 {
2507    (void) dpy;
2508    (void) screen;
2509    (void) channel;
2510    (void) window;
2511    return 0;
2512 }
2513
2514 PUBLIC int
2515 glXChannelRectSGIX(Display *dpy, int screen, int channel,
2516                    int x, int y, int w, int h)
2517 {
2518    (void) dpy;
2519    (void) screen;
2520    (void) channel;
2521    (void) x;
2522    (void) y;
2523    (void) w;
2524    (void) h;
2525    return 0;
2526 }
2527
2528 PUBLIC int
2529 glXQueryChannelRectSGIX(Display *dpy, int screen, int channel,
2530                         int *x, int *y, int *w, int *h)
2531 {
2532    (void) dpy;
2533    (void) screen;
2534    (void) channel;
2535    (void) x;
2536    (void) y;
2537    (void) w;
2538    (void) h;
2539    return 0;
2540 }
2541
2542 PUBLIC int
2543 glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel,
2544                           int *dx, int *dy, int *dw, int *dh)
2545 {
2546    (void) dpy;
2547    (void) screen;
2548    (void) channel;
2549    (void) dx;
2550    (void) dy;
2551    (void) dw;
2552    (void) dh;
2553    return 0;
2554 }
2555
2556 PUBLIC int
2557 glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
2558 {
2559    (void) dpy;
2560    (void) screen;
2561    (void) channel;
2562    (void) synctype;
2563    return 0;
2564 }
2565
2566
2567
2568 /*** GLX_SGIX_dmbuffer **/
2569
2570 #if defined(_DM_BUFFER_H_)
2571 PUBLIC Bool
2572 glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer,
2573                           DMparams *params, DMbuffer dmbuffer)
2574 {
2575    (void) dpy;
2576    (void) pbuffer;
2577    (void) params;
2578    (void) dmbuffer;
2579    return False;
2580 }
2581 #endif
2582
2583
2584 /*** GLX_SGIX_swap_group ***/
2585
2586 PUBLIC void
2587 glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
2588 {
2589    (void) dpy;
2590    (void) drawable;
2591    (void) member;
2592 }
2593
2594
2595
2596 /*** GLX_SGIX_swap_barrier ***/
2597
2598 PUBLIC void
2599 glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
2600 {
2601    (void) dpy;
2602    (void) drawable;
2603    (void) barrier;
2604 }
2605
2606 PUBLIC Bool
2607 glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2608 {
2609    (void) dpy;
2610    (void) screen;
2611    (void) max;
2612    return False;
2613 }
2614
2615
2616
2617 /*** GLX_SUN_get_transparent_index ***/
2618
2619 PUBLIC Status
2620 glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay,
2621                           long *pTransparent)
2622 {
2623    (void) dpy;
2624    (void) overlay;
2625    (void) underlay;
2626    (void) pTransparent;
2627    return 0;
2628 }
2629
2630
2631
2632 /*** GLX_MESA_release_buffers ***/
2633
2634 /*
2635  * Release the depth, stencil, accum buffers attached to a GLXDrawable
2636  * (a window or pixmap) prior to destroying the GLXDrawable.
2637  */
2638 PUBLIC Bool
2639 glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2640 {
2641    XMesaBuffer b = XMesaFindBuffer(dpy, d);
2642    if (b) {
2643       XMesaDestroyBuffer(b);
2644       return True;
2645    }
2646    return False;
2647 }
2648
2649 /*** GLX_EXT_texture_from_pixmap ***/
2650
2651 PUBLIC void
2652 glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
2653                         const int *attrib_list)
2654 {
2655    XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2656    if (b)
2657       XMesaBindTexImage(dpy, b, buffer, attrib_list);
2658 }
2659
2660 PUBLIC void
2661 glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
2662 {
2663    XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2664    if (b)
2665       XMesaReleaseTexImage(dpy, b, buffer);
2666 }
2667
2668
2669
2670 /*** GLX_ARB_create_context ***/
2671
2672 GLXContext
2673 glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config,
2674                            GLXContext shareCtx, Bool direct,
2675                            const int *attrib_list)
2676 {
2677    XMesaVisual xmvis = (XMesaVisual) config;
2678    int majorVersion = 1, minorVersion = 0;
2679    int contextFlags = 0x0;
2680    int profileMask = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
2681    int renderType = GLX_RGBA_TYPE;
2682    unsigned i;
2683    Bool done = False;
2684    const int contextFlagsAll = (GLX_CONTEXT_DEBUG_BIT_ARB |
2685                                 GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
2686
2687    /* parse attrib_list */
2688    for (i = 0; !done && attrib_list && attrib_list[i]; i++) {
2689       switch (attrib_list[i]) {
2690       case GLX_CONTEXT_MAJOR_VERSION_ARB:
2691          majorVersion = attrib_list[++i];
2692          break;
2693       case GLX_CONTEXT_MINOR_VERSION_ARB:
2694          minorVersion = attrib_list[++i];
2695          break;
2696       case GLX_CONTEXT_FLAGS_ARB:
2697          contextFlags = attrib_list[++i];
2698          break;
2699       case GLX_CONTEXT_PROFILE_MASK_ARB:
2700          profileMask = attrib_list[++i];
2701          break;
2702       case GLX_RENDER_TYPE:
2703          renderType = attrib_list[++i];
2704          break;
2705       case 0:
2706          /* end of list */
2707          done = True;
2708          break;
2709       default:
2710          /* bad attribute */
2711          /* XXX generate BadValue X Error */
2712          return NULL;
2713       }
2714    }
2715
2716    /* check contextFlags */
2717    if (contextFlags & ~contextFlagsAll) {
2718       return NULL; /* generate BadValue X Error */
2719    }
2720
2721    /* check profileMask */
2722    if (profileMask != GLX_CONTEXT_CORE_PROFILE_BIT_ARB &&
2723        profileMask != GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB) {
2724       return NULL; /* generate BadValue X Error */
2725    }
2726
2727    /* check version (generate BadMatch if bad) */
2728    switch (majorVersion) {
2729    case 1:
2730       if (minorVersion < 0 || minorVersion > 5)
2731          return NULL;
2732       break;
2733    case 2:
2734       if (minorVersion < 0 || minorVersion > 1)
2735          return NULL;
2736       break;
2737    case 3:
2738       if (minorVersion < 0 || minorVersion > 2)
2739          return NULL;
2740       break;
2741    case 4:
2742       if (minorVersion < 0 || minorVersion > 0)
2743          return NULL;
2744       break;
2745    default:
2746       return NULL;
2747    }
2748
2749    if ((contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) &&
2750        majorVersion < 3)
2751       return NULL; /* generate GLXBadProfileARB */
2752
2753    if (renderType == GLX_COLOR_INDEX_TYPE && majorVersion >= 3)
2754       return NULL; /* generate BadMatch */
2755
2756    return create_context(dpy, xmvis,
2757                          shareCtx ? shareCtx->xmesaContext : NULL,
2758                          direct,
2759                          majorVersion, minorVersion,
2760                          profileMask, contextFlags);
2761 }