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