08a931a1827d5438379a65e9339a80e86ea12c3f
[mesa.git] / src / mesa / drivers / x11 / fakeglx.c
1 /* $Id: fakeglx.c,v 1.31 2000/04/05 22:09:58 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * This is an emulation of the GLX API which allows Mesa/GLX-based programs
30 * to run on X servers which do not have the real GLX extension.
31 *
32 * Thanks to the contributors:
33 *
34 * Initial version: Philip Brown (philb@CSUA.Berkeley.EDU)
35 * Better glXGetConfig() support: Armin Liebchen (liebchen@asylum.cs.utah.edu)
36 * Further visual-handling refinements: Wolfram Gloger
37 * (wmglo@Dent.MED.Uni-Muenchen.DE).
38 *
39 * Notes:
40 * Don't be fooled, stereo isn't supported yet.
41 */
42
43
44
45 #include "glxheader.h"
46 #include "glxapi.h"
47 #include "GL/xmesa.h"
48 #include "context.h"
49 #include "config.h"
50 #include "macros.h"
51 #include "types.h"
52 #include "xmesaP.h"
53
54
55 /* This indicates the client-side GLX API and GLX encoder version. */
56 #define CLIENT_MAJOR_VERSION 1
57 #define CLIENT_MINOR_VERSION 2
58
59 /* This indicates the server-side GLX decoder version.
60 * GLX 1.3 indicates OpenGL 1.2 support
61 */
62 #define SERVER_MAJOR_VERSION 1
63 #define SERVER_MINOR_VERSION 3
64
65 /* This is appended onto the glXGetClient/ServerString version strings. */
66 #define MESA_GLX_VERSION "Mesa 3.3"
67
68 /* Who implemented this GLX? */
69 #define VENDOR "Brian Paul"
70
71
72
73 /* Silence compiler warnings */
74 extern void Fake_glXDummyFunc( void );
75 void Fake_glXDummyFunc( void )
76 {
77 (void) kernel8;
78 (void) DitherValues;
79 (void) HPCR_DRGB;
80 (void) kernel1;
81 }
82
83
84
85 #define DONT_CARE -1
86
87
88
89 #define MAX_VISUALS 100
90 static XMesaVisual VisualTable[MAX_VISUALS];
91 static int NumVisuals = 0;
92
93
94
95 /*
96 * This struct and some code fragments borrowed
97 * from Mark Kilgard's GLUT library.
98 */
99 typedef struct _OverlayInfo {
100 /* Avoid 64-bit portability problems by being careful to use
101 longs due to the way XGetWindowProperty is specified. Note
102 that these parameters are passed as CARD32s over X
103 protocol. */
104 unsigned long overlay_visual;
105 long transparent_type;
106 long value;
107 long layer;
108 } OverlayInfo;
109
110
111
112 /* Macro to handle c_class vs class field name in XVisualInfo struct */
113 #if defined(__cplusplus) || defined(c_plusplus)
114 #define CLASS c_class
115 #else
116 #define CLASS class
117 #endif
118
119
120
121
122 /*
123 * Test if the given XVisualInfo is usable for Mesa rendering.
124 */
125 static GLboolean is_usable_visual( XVisualInfo *vinfo )
126 {
127 switch (vinfo->CLASS) {
128 case StaticGray:
129 case GrayScale:
130 /* Any StaticGray/GrayScale visual works in RGB or CI mode */
131 return GL_TRUE;
132 case StaticColor:
133 case PseudoColor:
134 /* Any StaticColor/PseudoColor visual of at least 4 bits */
135 if (vinfo->depth>=4) {
136 return GL_TRUE;
137 }
138 else {
139 return GL_FALSE;
140 }
141 case TrueColor:
142 case DirectColor:
143 /* Any depth of TrueColor or DirectColor works in RGB mode */
144 return GL_TRUE;
145 default:
146 /* This should never happen */
147 return GL_FALSE;
148 }
149 }
150
151
152
153 /*
154 * Return the level (overlay, normal, underlay) of a given XVisualInfo.
155 * Input: dpy - the X display
156 * vinfo - the XVisualInfo to test
157 * Return: level of the visual:
158 * 0 = normal planes
159 * >0 = overlay planes
160 * <0 = underlay planes
161 */
162 static int level_of_visual( Display *dpy, XVisualInfo *vinfo )
163 {
164 Atom overlayVisualsAtom;
165 OverlayInfo *overlay_info = NULL;
166 int numOverlaysPerScreen;
167 Status status;
168 Atom actualType;
169 int actualFormat;
170 unsigned long sizeData, bytesLeft;
171 int i;
172
173 /*
174 * The SERVER_OVERLAY_VISUALS property on the root window contains
175 * a list of overlay visuals. Get that list now.
176 */
177 overlayVisualsAtom = XInternAtom(dpy,"SERVER_OVERLAY_VISUALS", True);
178 if (overlayVisualsAtom == None) {
179 return 0;
180 }
181
182 status = XGetWindowProperty(dpy, RootWindow( dpy, vinfo->screen ),
183 overlayVisualsAtom, 0L, (long) 10000, False,
184 overlayVisualsAtom, &actualType, &actualFormat,
185 &sizeData, &bytesLeft,
186 (unsigned char **) &overlay_info );
187
188 if (status != Success || actualType != overlayVisualsAtom ||
189 actualFormat != 32 || sizeData < 4) {
190 /* something went wrong */
191 XFree((void *) overlay_info);
192 return 0;
193 }
194
195 /* search the overlay visual list for the visual ID of interest */
196 numOverlaysPerScreen = (int) (sizeData / 4);
197 for (i=0;i<numOverlaysPerScreen;i++) {
198 OverlayInfo *ov;
199 ov = overlay_info + i;
200 if (ov->overlay_visual==vinfo->visualid) {
201 /* found the visual */
202 if (/*ov->transparent_type==1 &&*/ ov->layer!=0) {
203 int level = ov->layer;
204 XFree((void *) overlay_info);
205 return level;
206 }
207 else {
208 XFree((void *) overlay_info);
209 return 0;
210 }
211 }
212 }
213
214 /* The visual ID was not found in the overlay list. */
215 XFree((void *) overlay_info);
216 return 0;
217 }
218
219
220
221
222 /*
223 * Given an XVisualInfo and RGB, Double, and Depth buffer flags, save the
224 * configuration in our list of GLX visuals.
225 */
226 static XMesaVisual
227 save_glx_visual( Display *dpy, XVisualInfo *vinfo,
228 GLboolean rgbFlag, GLboolean alphaFlag, GLboolean dbFlag,
229 GLboolean stereoFlag,
230 GLint depth_size, GLint stencil_size,
231 GLint accumRedSize, GLint accumGreenSize,
232 GLint accumBlueSize, GLint accumAlphaSize,
233 GLint level )
234 {
235 GLboolean ximageFlag = GL_TRUE;
236 XMesaVisual xmvis;
237 GLint i;
238 GLboolean comparePointers;
239
240 if (dbFlag) {
241 /* Check if the MESA_BACK_BUFFER env var is set */
242 char *backbuffer = getenv("MESA_BACK_BUFFER");
243 if (backbuffer) {
244 if (backbuffer[0]=='p' || backbuffer[0]=='P') {
245 ximageFlag = GL_FALSE;
246 }
247 else if (backbuffer[0]=='x' || backbuffer[0]=='X') {
248 ximageFlag = GL_TRUE;
249 }
250 else {
251 fprintf(stderr, "Mesa: invalid value for MESA_BACK_BUFFER ");
252 fprintf(stderr, "environment variable, using an XImage.\n");
253 }
254 }
255 }
256
257 /* Comparing IDs uses less memory but sometimes fails. */
258 /* XXX revisit this after 3.0 is finished. */
259 if (getenv("MESA_GLX_VISUAL_HACK"))
260 comparePointers = GL_TRUE;
261 else
262 comparePointers = GL_FALSE;
263
264 /* First check if a matching visual is already in the list */
265 for (i=0; i<NumVisuals; i++) {
266 XMesaVisual v = VisualTable[i];
267 if (v->display == dpy
268 && v->level == level
269 && v->ximage_flag == ximageFlag
270 && v->gl_visual->RGBAflag == rgbFlag
271 && v->gl_visual->DBflag == dbFlag
272 && v->gl_visual->StereoFlag == stereoFlag
273 && (v->gl_visual->AlphaBits > 0) == alphaFlag
274 && (v->gl_visual->DepthBits >= depth_size || depth_size == 0)
275 && (v->gl_visual->StencilBits >= stencil_size || stencil_size == 0)
276 && (v->gl_visual->AccumRedBits >= accumRedSize || accumRedSize == 0)
277 && (v->gl_visual->AccumGreenBits >= accumGreenSize || accumGreenSize == 0)
278 && (v->gl_visual->AccumBlueBits >= accumBlueSize || accumBlueSize == 0)
279 && (v->gl_visual->AccumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
280 /* now either compare XVisualInfo pointers or visual IDs */
281 if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
282 || (comparePointers && v->vishandle == vinfo)) {
283 return v;
284 }
285 }
286 }
287
288 /* Create a new visual and add it to the list. */
289
290 if (NumVisuals>=MAX_VISUALS) {
291 fprintf( stderr, "GLX Error: maximum number of visuals exceeded\n");
292 return NULL;
293 }
294
295 xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
296 stereoFlag, ximageFlag,
297 depth_size, stencil_size,
298 accumRedSize, accumBlueSize,
299 accumBlueSize, accumAlphaSize, 0, level,
300 GLX_NONE_EXT );
301 if (xmvis) {
302 VisualTable[NumVisuals] = xmvis;
303 NumVisuals++;
304 }
305 return xmvis;
306 }
307
308
309
310 /*
311 * Create a GLX visual from a regular XVisualInfo.
312 * This is called when Fake GLX is given an XVisualInfo which wasn't
313 * returned by glXChooseVisual. Since this is the first time we're
314 * considering this visual we'll take a guess at reasonable values
315 * for depth buffer size, stencil size, accum size, etc.
316 * This is the best we can do with a client-side emulation of GLX.
317 */
318 static XMesaVisual
319 create_glx_visual( Display *dpy, XVisualInfo *visinfo )
320 {
321 int vislevel;
322
323 vislevel = level_of_visual( dpy, visinfo );
324 if (vislevel) {
325 /* Configure this visual as a CI, single-buffered overlay */
326 return save_glx_visual( dpy, visinfo,
327 GL_FALSE, /* rgb */
328 GL_FALSE, /* alpha */
329 GL_FALSE, /* double */
330 GL_FALSE, /* stereo */
331 0, /* depth bits */
332 0, /* stencil bits */
333 0,0,0,0, /* accum bits */
334 vislevel /* level */
335 );
336 }
337 else if (is_usable_visual( visinfo )) {
338 /* Configure this visual as RGB, double-buffered, depth-buffered. */
339 /* This is surely wrong for some people's needs but what else */
340 /* can be done? They should use glXChooseVisual(). */
341 return save_glx_visual( dpy, visinfo,
342 GL_TRUE, /* rgb */
343 GL_FALSE, /* alpha */
344 GL_TRUE, /* double */
345 GL_FALSE, /* stereo */
346 DEFAULT_SOFTWARE_DEPTH_BITS,
347 8 * sizeof(GLstencil),
348 8 * sizeof(GLaccum), /* r */
349 8 * sizeof(GLaccum), /* g */
350 8 * sizeof(GLaccum), /* b */
351 8 * sizeof(GLaccum), /* a */
352 0 /* level */
353 );
354 }
355 else {
356 fprintf(stderr,"Mesa: error in glXCreateContext: bad visual\n");
357 return NULL;
358 }
359 }
360
361
362
363 /*
364 * Find the GLX visual associated with an XVisualInfo.
365 */
366 static XMesaVisual
367 find_glx_visual( Display *dpy, XVisualInfo *vinfo )
368 {
369 int i;
370
371 /* First try to match pointers */
372 for (i=0;i<NumVisuals;i++) {
373 if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
374 return VisualTable[i];
375 }
376 }
377 /* try to match visual id */
378 for (i=0;i<NumVisuals;i++) {
379 if (VisualTable[i]->display==dpy
380 && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
381 return VisualTable[i];
382 }
383 }
384 return NULL;
385 }
386
387
388
389 /*
390 * Return the transparent pixel value for a GLX visual.
391 * Input: glxvis - the glx_visual
392 * Return: a pixel value or -1 if no transparent pixel
393 */
394 static int transparent_pixel( XMesaVisual glxvis )
395 {
396 Display *dpy = glxvis->display;
397 XVisualInfo *vinfo = glxvis->visinfo;
398 Atom overlayVisualsAtom;
399 OverlayInfo *overlay_info = NULL;
400 int numOverlaysPerScreen;
401 Status status;
402 Atom actualType;
403 int actualFormat;
404 unsigned long sizeData, bytesLeft;
405 int i;
406
407 /*
408 * The SERVER_OVERLAY_VISUALS property on the root window contains
409 * a list of overlay visuals. Get that list now.
410 */
411 overlayVisualsAtom = XInternAtom(dpy,"SERVER_OVERLAY_VISUALS", True);
412 if (overlayVisualsAtom == None) {
413 return -1;
414 }
415
416 status = XGetWindowProperty(dpy, RootWindow( dpy, vinfo->screen ),
417 overlayVisualsAtom, 0L, (long) 10000, False,
418 overlayVisualsAtom, &actualType, &actualFormat,
419 &sizeData, &bytesLeft,
420 (unsigned char **) &overlay_info );
421
422 if (status != Success || actualType != overlayVisualsAtom ||
423 actualFormat != 32 || sizeData < 4) {
424 /* something went wrong */
425 XFree((void *) overlay_info);
426 return -1;
427 }
428
429 /* search the overlay visual list for the visual ID of interest */
430 numOverlaysPerScreen = (int) (sizeData / 4);
431 for (i=0;i<numOverlaysPerScreen;i++) {
432 OverlayInfo *ov;
433 ov = overlay_info + i;
434 if (ov->overlay_visual==vinfo->visualid) {
435 /* found it! */
436 if (ov->transparent_type==0) {
437 /* type 0 indicates no transparency */
438 XFree((void *) overlay_info);
439 return -1;
440 }
441 else {
442 /* ov->value is the transparent pixel */
443 XFree((void *) overlay_info);
444 return ov->value;
445 }
446 }
447 }
448
449 /* The visual ID was not found in the overlay list. */
450 XFree((void *) overlay_info);
451 return -1;
452 }
453
454
455
456 /*
457 * Return number of bits set in n.
458 */
459 static int bitcount( unsigned long n )
460 {
461 int bits;
462 for (bits=0; n>0; n=n>>1) {
463 if (n&1) {
464 bits++;
465 }
466 }
467 return bits;
468 }
469
470
471 /*
472 * Try to get an X visual which matches the given arguments.
473 */
474 static XVisualInfo *get_visual( Display *dpy, int scr,
475 unsigned int depth, int xclass )
476 {
477 XVisualInfo temp, *vis;
478 long mask;
479 int n;
480 unsigned int default_depth;
481 int default_class;
482
483 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
484 temp.screen = scr;
485 temp.depth = depth;
486 temp.CLASS = xclass;
487
488 default_depth = DefaultDepth(dpy,scr);
489 default_class = DefaultVisual(dpy,scr)->CLASS;
490
491 if (depth==default_depth && xclass==default_class) {
492 /* try to get root window's visual */
493 temp.visualid = DefaultVisual(dpy,scr)->visualid;
494 mask |= VisualIDMask;
495 }
496
497 vis = XGetVisualInfo( dpy, mask, &temp, &n );
498
499 /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
500 * An SGI Infinite Reality system, for example, can have 30bpp pixels:
501 * 10 bits per color channel. Mesa's limited to a max of 8 bits/channel.
502 */
503 if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
504 if (bitcount(vis->red_mask) <= 8
505 && bitcount(vis->green_mask) <= 8
506 && bitcount(vis->blue_mask) <= 8) {
507 return vis;
508 }
509 else {
510 XFree((void *) vis);
511 return NULL;
512 }
513 }
514
515 return vis;
516 }
517
518
519
520 /*
521 * Retrieve the value of the given environment variable and find
522 * the X visual which matches it.
523 * Input: dpy - the display
524 * screen - the screen number
525 * varname - the name of the environment variable
526 * Return: an XVisualInfo pointer to NULL if error.
527 */
528 static XVisualInfo *get_env_visual(Display *dpy, int scr, const char *varname)
529 {
530 char value[100], type[100];
531 int depth, xclass = -1;
532 XVisualInfo *vis;
533
534 if (!getenv( varname )) {
535 return NULL;
536 }
537
538 strncpy( value, getenv(varname), 100 );
539 value[99] = 0;
540
541 sscanf( value, "%s %d", type, &depth );
542
543 if (strcmp(type,"TrueColor")==0) xclass = TrueColor;
544 else if (strcmp(type,"DirectColor")==0) xclass = DirectColor;
545 else if (strcmp(type,"PseudoColor")==0) xclass = PseudoColor;
546 else if (strcmp(type,"StaticColor")==0) xclass = StaticColor;
547 else if (strcmp(type,"GrayScale")==0) xclass = GrayScale;
548 else if (strcmp(type,"StaticGray")==0) xclass = StaticGray;
549
550 if (xclass>-1 && depth>0) {
551 vis = get_visual( dpy, scr, depth, xclass );
552 if (vis) {
553 return vis;
554 }
555 }
556
557 fprintf( stderr, "Mesa: GLX unable to find visual class=%s, depth=%d.\n",
558 type, depth );
559 return NULL;
560 }
561
562
563
564 /*
565 * Select an X visual which satisfies the RGBA/CI flag and minimum depth.
566 * Input: dpy, screen - X display and screen number
567 * rgba - GL_TRUE = RGBA mode, GL_FALSE = CI mode
568 * min_depth - minimum visual depth
569 * preferred_class - preferred GLX visual class or DONT_CARE
570 * Return: pointer to an XVisualInfo or NULL.
571 */
572 static XVisualInfo *choose_x_visual( Display *dpy, int screen,
573 GLboolean rgba, int min_depth,
574 int preferred_class )
575 {
576 XVisualInfo *vis;
577 int xclass, visclass;
578 int depth;
579
580 if (rgba) {
581 Atom hp_cr_maps = XInternAtom(dpy, "_HP_RGB_SMOOTH_MAP_LIST", True);
582 /* First see if the MESA_RGB_VISUAL env var is defined */
583 vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
584 if (vis) {
585 return vis;
586 }
587 /* Otherwise, search for a suitable visual */
588 if (preferred_class==DONT_CARE) {
589 for (xclass=0;xclass<6;xclass++) {
590 switch (xclass) {
591 case 0: visclass = TrueColor; break;
592 case 1: visclass = DirectColor; break;
593 case 2: visclass = PseudoColor; break;
594 case 3: visclass = StaticColor; break;
595 case 4: visclass = GrayScale; break;
596 case 5: visclass = StaticGray; break;
597 }
598 if (min_depth==0) {
599 /* start with shallowest */
600 for (depth=0;depth<=32;depth++) {
601 if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
602 /* Special case: try to get 8-bit PseudoColor before */
603 /* 8-bit TrueColor */
604 vis = get_visual( dpy, screen, 8, PseudoColor );
605 if (vis) {
606 return vis;
607 }
608 }
609 vis = get_visual( dpy, screen, depth, visclass );
610 if (vis) {
611 return vis;
612 }
613 }
614 }
615 else {
616 /* start with deepest */
617 for (depth=32;depth>=min_depth;depth--) {
618 if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
619 /* Special case: try to get 8-bit PseudoColor before */
620 /* 8-bit TrueColor */
621 vis = get_visual( dpy, screen, 8, PseudoColor );
622 if (vis) {
623 return vis;
624 }
625 }
626 vis = get_visual( dpy, screen, depth, visclass );
627 if (vis) {
628 return vis;
629 }
630 }
631 }
632 }
633 }
634 else {
635 /* search for a specific visual class */
636 switch (preferred_class) {
637 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
638 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
639 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
640 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
641 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
642 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
643 default: return NULL;
644 }
645 if (min_depth==0) {
646 /* start with shallowest */
647 for (depth=0;depth<=32;depth++) {
648 vis = get_visual( dpy, screen, depth, visclass );
649 if (vis) {
650 return vis;
651 }
652 }
653 }
654 else {
655 /* start with deepest */
656 for (depth=32;depth>=min_depth;depth--) {
657 vis = get_visual( dpy, screen, depth, visclass );
658 if (vis) {
659 return vis;
660 }
661 }
662 }
663 }
664 }
665 else {
666 /* First see if the MESA_CI_VISUAL env var is defined */
667 vis = get_env_visual( dpy, screen, "MESA_CI_VISUAL" );
668 if (vis) {
669 return vis;
670 }
671 /* Otherwise, search for a suitable visual, starting with shallowest */
672 if (preferred_class==DONT_CARE) {
673 for (xclass=0;xclass<4;xclass++) {
674 switch (xclass) {
675 case 0: visclass = PseudoColor; break;
676 case 1: visclass = StaticColor; break;
677 case 2: visclass = GrayScale; break;
678 case 3: visclass = StaticGray; break;
679 }
680 /* try 8-bit up through 16-bit */
681 for (depth=8;depth<=16;depth++) {
682 vis = get_visual( dpy, screen, depth, visclass );
683 if (vis) {
684 return vis;
685 }
686 }
687 /* try min_depth up to 8-bit */
688 for (depth=min_depth;depth<8;depth++) {
689 vis = get_visual( dpy, screen, depth, visclass );
690 if (vis) {
691 return vis;
692 }
693 }
694 }
695 }
696 else {
697 /* search for a specific visual class */
698 switch (preferred_class) {
699 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
700 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
701 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
702 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
703 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
704 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
705 default: return NULL;
706 }
707 /* try 8-bit up through 16-bit */
708 for (depth=8;depth<=16;depth++) {
709 vis = get_visual( dpy, screen, depth, visclass );
710 if (vis) {
711 return vis;
712 }
713 }
714 /* try min_depth up to 8-bit */
715 for (depth=min_depth;depth<8;depth++) {
716 vis = get_visual( dpy, screen, depth, visclass );
717 if (vis) {
718 return vis;
719 }
720 }
721 }
722 }
723
724 /* didn't find a visual */
725 return NULL;
726 }
727
728
729
730 /*
731 * Find the deepest X over/underlay visual of at least min_depth.
732 * Input: dpy, screen - X display and screen number
733 * level - the over/underlay level
734 * trans_type - transparent pixel type: GLX_NONE_EXT,
735 * GLX_TRANSPARENT_RGB_EXT, GLX_TRANSPARENT_INDEX_EXT,
736 * or DONT_CARE
737 * trans_value - transparent pixel value or DONT_CARE
738 * min_depth - minimum visual depth
739 * preferred_class - preferred GLX visual class or DONT_CARE
740 * Return: pointer to an XVisualInfo or NULL.
741 */
742 static XVisualInfo *choose_x_overlay_visual( Display *dpy, int scr,
743 GLboolean rgbFlag,
744 int level, int trans_type,
745 int trans_value,
746 int min_depth,
747 int preferred_class )
748 {
749 Atom overlayVisualsAtom;
750 OverlayInfo *overlay_info;
751 int numOverlaysPerScreen;
752 Status status;
753 Atom actualType;
754 int actualFormat;
755 unsigned long sizeData, bytesLeft;
756 int i;
757 XVisualInfo *deepvis;
758 int deepest;
759
760 /*DEBUG int tt, tv; */
761
762 switch (preferred_class) {
763 case GLX_TRUE_COLOR_EXT: preferred_class = TrueColor; break;
764 case GLX_DIRECT_COLOR_EXT: preferred_class = DirectColor; break;
765 case GLX_PSEUDO_COLOR_EXT: preferred_class = PseudoColor; break;
766 case GLX_STATIC_COLOR_EXT: preferred_class = StaticColor; break;
767 case GLX_GRAY_SCALE_EXT: preferred_class = GrayScale; break;
768 case GLX_STATIC_GRAY_EXT: preferred_class = StaticGray; break;
769 default: preferred_class = DONT_CARE;
770 }
771
772 /*
773 * The SERVER_OVERLAY_VISUALS property on the root window contains
774 * a list of overlay visuals. Get that list now.
775 */
776 overlayVisualsAtom = XInternAtom(dpy,"SERVER_OVERLAY_VISUALS", True);
777 if (overlayVisualsAtom == (Atom) None) {
778 return NULL;
779 }
780
781 status = XGetWindowProperty(dpy, RootWindow( dpy, scr ),
782 overlayVisualsAtom, 0L, (long) 10000, False,
783 overlayVisualsAtom, &actualType, &actualFormat,
784 &sizeData, &bytesLeft,
785 (unsigned char **) &overlay_info );
786
787 if (status != Success || actualType != overlayVisualsAtom ||
788 actualFormat != 32 || sizeData < 4) {
789 /* something went wrong */
790 return NULL;
791 }
792
793 /* Search for the deepest overlay which satisifies all criteria. */
794 deepest = min_depth;
795 deepvis = NULL;
796
797 numOverlaysPerScreen = (int) (sizeData / 4);
798 for (i=0;i<numOverlaysPerScreen;i++) {
799 XVisualInfo *vislist, vistemplate;
800 int count;
801 OverlayInfo *ov;
802 ov = overlay_info + i;
803
804 if (ov->layer!=level) {
805 /* failed overlay level criteria */
806 continue;
807 }
808 if (!(trans_type==DONT_CARE
809 || (trans_type==GLX_TRANSPARENT_INDEX_EXT
810 && ov->transparent_type>0)
811 || (trans_type==GLX_NONE_EXT && ov->transparent_type==0))) {
812 /* failed transparent pixel type criteria */
813 continue;
814 }
815 if (trans_value!=DONT_CARE && trans_value!=ov->value) {
816 /* failed transparent pixel value criteria */
817 continue;
818 }
819
820 /* get XVisualInfo and check the depth */
821 vistemplate.visualid = ov->overlay_visual;
822 vistemplate.screen = scr;
823 vislist = XGetVisualInfo( dpy, VisualIDMask | VisualScreenMask,
824 &vistemplate, &count );
825
826 if (count!=1) {
827 /* something went wrong */
828 continue;
829 }
830 if (preferred_class!=DONT_CARE && preferred_class!=vislist->CLASS) {
831 /* wrong visual class */
832 continue;
833 }
834
835 /* if RGB was requested, make sure we have True/DirectColor */
836 if (rgbFlag && vislist->CLASS != TrueColor
837 && vislist->CLASS != DirectColor)
838 continue;
839
840 /* if CI was requested, make sure we have a color indexed visual */
841 if (!rgbFlag
842 && (vislist->CLASS == TrueColor || vislist->CLASS == DirectColor))
843 continue;
844
845 if (deepvis==NULL || vislist->depth > deepest) {
846 /* YES! found a satisfactory visual */
847 if (deepvis) {
848 XFree( deepvis );
849 }
850 deepest = vislist->depth;
851 deepvis = vislist;
852 /* DEBUG tt = ov->transparent_type;*/
853 /* DEBUG tv = ov->value; */
854 }
855 }
856
857 /*DEBUG
858 if (deepvis) {
859 printf("chose 0x%x: layer=%d depth=%d trans_type=%d trans_value=%d\n",
860 deepvis->visualid, level, deepvis->depth, tt, tv );
861 }
862 */
863 return deepvis;
864 }
865
866
867
868 static XVisualInfo *
869 Fake_glXChooseVisual( Display *dpy, int screen, int *list )
870 {
871 int *parselist;
872 XVisualInfo *vis;
873 int min_ci = 0;
874 int min_red=0, min_green=0, min_blue=0;
875 GLboolean rgb_flag = GL_FALSE;
876 GLboolean alpha_flag = GL_FALSE;
877 GLboolean double_flag = GL_FALSE;
878 GLboolean stereo_flag = GL_FALSE;
879 GLint depth_size = 0;
880 GLint stencil_size = 0;
881 GLint accumRedSize = 0;
882 GLint accumGreenSize = 0;
883 GLint accumBlueSize = 0;
884 GLint accumAlphaSize = 0;
885 int level = 0;
886 int visual_type = DONT_CARE;
887 int trans_type = DONT_CARE;
888 int trans_value = DONT_CARE;
889 GLint caveat = DONT_CARE;
890
891 parselist = list;
892
893 while (*parselist) {
894
895 switch (*parselist) {
896 case GLX_USE_GL:
897 /* ignore */
898 parselist++;
899 break;
900 case GLX_BUFFER_SIZE:
901 parselist++;
902 min_ci = *parselist++;
903 break;
904 case GLX_LEVEL:
905 parselist++;
906 level = *parselist++;
907 break;
908 case GLX_RGBA:
909 rgb_flag = GL_TRUE;
910 parselist++;
911 break;
912 case GLX_DOUBLEBUFFER:
913 double_flag = GL_TRUE;
914 parselist++;
915 break;
916 case GLX_STEREO:
917 stereo_flag = GL_TRUE;
918 return NULL;
919 case GLX_AUX_BUFFERS:
920 /* ignore */
921 parselist++;
922 parselist++;
923 break;
924 case GLX_RED_SIZE:
925 parselist++;
926 min_red = *parselist++;
927 break;
928 case GLX_GREEN_SIZE:
929 parselist++;
930 min_green = *parselist++;
931 break;
932 case GLX_BLUE_SIZE:
933 parselist++;
934 min_blue = *parselist++;
935 break;
936 case GLX_ALPHA_SIZE:
937 parselist++;
938 {
939 GLint size = *parselist++;
940 alpha_flag = size>0 ? 1 : 0;
941 }
942 break;
943 case GLX_DEPTH_SIZE:
944 parselist++;
945 depth_size = *parselist++;
946 break;
947 case GLX_STENCIL_SIZE:
948 parselist++;
949 stencil_size = *parselist++;
950 break;
951 case GLX_ACCUM_RED_SIZE:
952 parselist++;
953 {
954 GLint size = *parselist++;
955 accumRedSize = MAX2( accumRedSize, size );
956 }
957 break;
958 case GLX_ACCUM_GREEN_SIZE:
959 parselist++;
960 {
961 GLint size = *parselist++;
962 accumGreenSize = MAX2( accumGreenSize, size );
963 }
964 break;
965 case GLX_ACCUM_BLUE_SIZE:
966 parselist++;
967 {
968 GLint size = *parselist++;
969 accumBlueSize = MAX2( accumBlueSize, size );
970 }
971 break;
972 case GLX_ACCUM_ALPHA_SIZE:
973 parselist++;
974 {
975 GLint size = *parselist++;
976 accumAlphaSize = MAX2( accumAlphaSize, size );
977 }
978 break;
979
980 /*
981 * GLX_EXT_visual_info extension
982 */
983 case GLX_X_VISUAL_TYPE_EXT:
984 parselist++;
985 visual_type = *parselist++;
986 break;
987 case GLX_TRANSPARENT_TYPE_EXT:
988 parselist++;
989 trans_type = *parselist++;
990 break;
991 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
992 parselist++;
993 trans_value = *parselist++;
994 break;
995 case GLX_TRANSPARENT_RED_VALUE_EXT:
996 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
997 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
998 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
999 /* ignore */
1000 parselist++;
1001 parselist++;
1002 break;
1003
1004 /*
1005 * GLX_EXT_visual_info extension
1006 */
1007 case GLX_VISUAL_CAVEAT_EXT:
1008 parselist++;
1009 caveat = *parselist++; /* ignored for now */
1010 break;
1011
1012 case None:
1013 break;
1014 default:
1015 /* undefined attribute */
1016 return NULL;
1017 }
1018 }
1019
1020 /*
1021 * Since we're only simulating the GLX extension this function will never
1022 * find any real GL visuals. Instead, all we can do is try to find an RGB
1023 * or CI visual of appropriate depth. Other requested attributes such as
1024 * double buffering, depth buffer, etc. will be associated with the X
1025 * visual and stored in the VisualTable[].
1026 */
1027 if (level==0) {
1028 /* normal color planes */
1029 if (rgb_flag) {
1030 /* Get an RGB visual */
1031 int min_rgb = min_red + min_green + min_blue;
1032 if (min_rgb>1 && min_rgb<8) {
1033 /* a special case to be sure we can get a monochrome visual */
1034 min_rgb = 1;
1035 }
1036 vis = choose_x_visual( dpy, screen, rgb_flag, min_rgb, visual_type );
1037 }
1038 else {
1039 /* Get a color index visual */
1040 vis = choose_x_visual( dpy, screen, rgb_flag, min_ci, visual_type );
1041 accumRedSize = accumGreenSize = accumBlueSize = accumAlphaSize = 0;
1042 }
1043 }
1044 else {
1045 /* over/underlay planes */
1046 if (rgb_flag) {
1047 /* rgba overlay */
1048 int min_rgb = min_red + min_green + min_blue;
1049 if (min_rgb>1 && min_rgb<8) {
1050 /* a special case to be sure we can get a monochrome visual */
1051 min_rgb = 1;
1052 }
1053 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1054 trans_type, trans_value, min_rgb, visual_type );
1055 }
1056 else {
1057 /* color index overlay */
1058 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1059 trans_type, trans_value, min_ci, visual_type );
1060 }
1061 }
1062
1063 if (vis) {
1064 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1065 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1066 * largest depth buffer size, which is 32bits/value. However, we
1067 * return 16 to maintain performance with earlier versions of Mesa.
1068 */
1069 if (depth_size == 1)
1070 depth_size = DEFAULT_SOFTWARE_DEPTH_BITS;
1071 else if (depth_size > 24)
1072 depth_size = 31;
1073 else if (depth_size > 16)
1074 depth_size = 24;
1075 /* we only support one size of stencil and accum buffers. */
1076 if (stencil_size > 0)
1077 stencil_size = STENCIL_BITS;
1078 if (accumRedSize > 0)
1079 accumRedSize = ACCUM_BITS;
1080 if (accumGreenSize > 0)
1081 accumGreenSize = ACCUM_BITS;
1082 if (accumBlueSize > 0)
1083 accumBlueSize = ACCUM_BITS;
1084 if (accumAlphaSize > 0)
1085 accumAlphaSize = ACCUM_BITS;
1086 if (!save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1087 stereo_flag, depth_size, stencil_size,
1088 accumRedSize, accumGreenSize,
1089 accumBlueSize, accumAlphaSize,
1090 level ))
1091 return NULL;
1092 }
1093
1094 return vis;
1095 }
1096
1097
1098
1099
1100 static GLXContext
1101 Fake_glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1102 GLXContext share_list, Bool direct )
1103 {
1104 XMesaVisual glxvis;
1105 XMesaContext xmctx;
1106
1107 /* deallocate unused windows/buffers */
1108 XMesaGarbageCollect();
1109
1110 glxvis = find_glx_visual( dpy, visinfo );
1111 if (!glxvis) {
1112 /* This visual wasn't found with glXChooseVisual() */
1113 glxvis = create_glx_visual( dpy, visinfo );
1114 if (!glxvis) {
1115 /* unusable visual */
1116 return NULL;
1117 }
1118 }
1119
1120 xmctx = XMesaCreateContext( glxvis, (XMesaContext) share_list );
1121 if (xmctx) {
1122 /* set the direct/indirect flag */
1123 xmctx->direct = direct;
1124 }
1125 return (GLXContext) xmctx;
1126 }
1127
1128
1129 static GLXContext MakeCurrent_PrevContext = 0;
1130 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1131 static GLXDrawable MakeCurrent_PrevReadable = 0;
1132 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1133 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1134
1135 /* GLX 1.3 and later */
1136 static Bool
1137 Fake_glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1138 GLXDrawable read, GLXContext ctx )
1139 {
1140 if (ctx && draw && read) {
1141 XMesaBuffer drawBuffer, readBuffer;
1142 XMesaContext xmctx = (XMesaContext) ctx;
1143
1144 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1145 if (ctx == MakeCurrent_PrevContext
1146 && draw == MakeCurrent_PrevDrawable) {
1147 drawBuffer = MakeCurrent_PrevDrawBuffer;
1148 }
1149 else {
1150 drawBuffer = XMesaFindBuffer( dpy, draw );
1151 }
1152 if (!drawBuffer) {
1153 /* drawable must be a new window! */
1154 drawBuffer = XMesaCreateWindowBuffer2( xmctx->xm_visual, draw, ctx );
1155 if (!drawBuffer) {
1156 /* Out of memory, or context/drawable depth mismatch */
1157 return False;
1158 }
1159 }
1160
1161 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1162 if (ctx == MakeCurrent_PrevContext
1163 && read == MakeCurrent_PrevReadable) {
1164 readBuffer = MakeCurrent_PrevReadBuffer;
1165 }
1166 else {
1167 readBuffer = XMesaFindBuffer( dpy, read );
1168 }
1169 if (!readBuffer) {
1170 /* drawable must be a new window! */
1171 readBuffer = XMesaCreateWindowBuffer2( xmctx->xm_visual, read, ctx );
1172 if (!readBuffer) {
1173 /* Out of memory, or context/drawable depth mismatch */
1174 return False;
1175 }
1176 }
1177
1178 MakeCurrent_PrevContext = ctx;
1179 MakeCurrent_PrevDrawable = draw;
1180 MakeCurrent_PrevReadable = read;
1181 MakeCurrent_PrevDrawBuffer = drawBuffer;
1182 MakeCurrent_PrevReadBuffer = readBuffer;
1183
1184 /* Now make current! */
1185 return (Bool) XMesaMakeCurrent2((XMesaContext) ctx, drawBuffer, readBuffer);
1186 }
1187 else if (!ctx && !draw && !read) {
1188 /* release current context w/out assigning new one. */
1189 XMesaMakeCurrent( NULL, NULL );
1190 MakeCurrent_PrevContext = 0;
1191 MakeCurrent_PrevDrawable = 0;
1192 MakeCurrent_PrevReadable = 0;
1193 MakeCurrent_PrevDrawBuffer = 0;
1194 MakeCurrent_PrevReadBuffer = 0;
1195 return True;
1196 }
1197 else {
1198 /* The args must either all be non-zero or all zero.
1199 * This is an error.
1200 */
1201 return False;
1202 }
1203 }
1204
1205
1206
1207 static Bool
1208 Fake_glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1209 {
1210 return Fake_glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1211 }
1212
1213
1214
1215 static GLXPixmap
1216 Fake_glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1217 {
1218 XMesaVisual v;
1219 XMesaBuffer b;
1220
1221 v = find_glx_visual( dpy, visinfo );
1222 if (!v) {
1223 v = create_glx_visual( dpy, visinfo );
1224 if (!v) {
1225 /* unusable visual */
1226 return 0;
1227 }
1228 }
1229
1230 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1231 if (!b) {
1232 return 0;
1233 }
1234 return b->frontbuffer;
1235 }
1236
1237
1238 #ifdef GLX_MESA_pixmap_colormap
1239
1240 static GLXPixmap
1241 Fake_glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1242 Pixmap pixmap, Colormap cmap )
1243 {
1244 XMesaVisual v;
1245 XMesaBuffer b;
1246
1247 v = find_glx_visual( dpy, visinfo );
1248 if (!v) {
1249 v = create_glx_visual( dpy, visinfo );
1250 if (!v) {
1251 /* unusable visual */
1252 return 0;
1253 }
1254 }
1255
1256 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1257 if (!b) {
1258 return 0;
1259 }
1260 return b->frontbuffer;
1261 }
1262
1263 #endif
1264
1265
1266 static void
1267 Fake_glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1268 {
1269 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1270 if (b) {
1271 XMesaDestroyBuffer(b);
1272 }
1273 else if (getenv("MESA_DEBUG")) {
1274 fprintf( stderr, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1275 }
1276 }
1277
1278
1279 static void
1280 Fake_glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1281 unsigned long mask )
1282 {
1283 XMesaContext xm_src = (XMesaContext) src;
1284 XMesaContext xm_dst = (XMesaContext) dst;
1285 (void) dpy;
1286 gl_copy_context( xm_src->gl_ctx, xm_dst->gl_ctx, (GLuint) mask );
1287 }
1288
1289
1290
1291 static Bool
1292 Fake_glXQueryExtension( Display *dpy, int *errorb, int *event )
1293 {
1294 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1295 (void) dpy;
1296 (void) errorb;
1297 (void) event;
1298 return True;
1299 }
1300
1301
1302 extern void _kw_ungrab_all( Display *dpy );
1303 void _kw_ungrab_all( Display *dpy )
1304 {
1305 XUngrabPointer( dpy, CurrentTime );
1306 XUngrabKeyboard( dpy, CurrentTime );
1307 }
1308
1309
1310 static void
1311 Fake_glXDestroyContext( Display *dpy, GLXContext ctx )
1312 {
1313 (void) dpy;
1314 MakeCurrent_PrevContext = 0;
1315 MakeCurrent_PrevDrawable = 0;
1316 MakeCurrent_PrevReadable = 0;
1317 MakeCurrent_PrevDrawBuffer = 0;
1318 MakeCurrent_PrevReadBuffer = 0;
1319 XMesaDestroyContext( (XMesaContext) ctx );
1320 XMesaGarbageCollect();
1321 }
1322
1323
1324
1325 static Bool
1326 Fake_glXIsDirect( Display *dpy, GLXContext ctx )
1327 {
1328 (void) dpy;
1329 return ((XMesaContext) ctx)->direct;
1330 }
1331
1332
1333
1334 static void
1335 Fake_glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1336 {
1337 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1338
1339 if (buffer) {
1340 XMesaSwapBuffers(buffer);
1341 }
1342 else if (getenv("MESA_DEBUG")) {
1343 fprintf(stderr, "Mesa Warning: glXSwapBuffers: invalid drawable\n");
1344 }
1345 }
1346
1347
1348 static void
1349 Fake_glXCopySubBufferMESA( Display *dpy, GLXDrawable drawable,
1350 int x, int y, int width, int height )
1351 {
1352 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1353 if (buffer) {
1354 XMesaCopySubBuffer(buffer, x, y, width, height);
1355 }
1356 else if (getenv("MESA_DEBUG")) {
1357 fprintf(stderr, "Mesa Warning: glXCopySubBufferMESA: invalid drawable\n");
1358 }
1359 }
1360
1361
1362
1363 static Bool
1364 Fake_glXQueryVersion( Display *dpy, int *maj, int *min )
1365 {
1366 (void) dpy;
1367 /* Return GLX version, not Mesa version */
1368 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1369 *maj = CLIENT_MAJOR_VERSION;
1370 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1371 return True;
1372 }
1373
1374
1375
1376 /*
1377 * Query the GLX attributes of the given XVisualInfo.
1378 */
1379 static int
1380 Fake_glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1381 int attrib, int *value )
1382 {
1383 XMesaVisual glxvis;
1384
1385 glxvis = find_glx_visual( dpy, visinfo );
1386 if (!glxvis) {
1387 /* this visual wasn't obtained with glXChooseVisual */
1388 glxvis = create_glx_visual( dpy, visinfo );
1389 if (!glxvis) {
1390 /* this visual can't be used for GL rendering */
1391 if (attrib==GLX_USE_GL) {
1392 *value = (int) False;
1393 return 0;
1394 }
1395 else {
1396 /*fprintf( stderr, "Mesa: Error in glXGetConfig: bad visual\n");*/
1397 return GLX_BAD_VISUAL;
1398 }
1399 }
1400 }
1401
1402 switch(attrib) {
1403 case GLX_USE_GL:
1404 *value = (int) True;
1405 return 0;
1406 case GLX_BUFFER_SIZE:
1407 *value = visinfo->depth;
1408 return 0;
1409 case GLX_LEVEL:
1410 *value = glxvis->level;
1411 return 0;
1412 case GLX_RGBA:
1413 if (glxvis->gl_visual->RGBAflag) {
1414 *value = True;
1415 }
1416 else {
1417 *value = False;
1418 }
1419 return 0;
1420 case GLX_DOUBLEBUFFER:
1421 *value = (int) glxvis->gl_visual->DBflag;
1422 return 0;
1423 case GLX_STEREO:
1424 *value = (int) glxvis->gl_visual->StereoFlag;
1425 return 0;
1426 case GLX_AUX_BUFFERS:
1427 *value = (int) False;
1428 return 0;
1429 case GLX_RED_SIZE:
1430 *value = glxvis->gl_visual->RedBits;
1431 return 0;
1432 case GLX_GREEN_SIZE:
1433 *value = glxvis->gl_visual->GreenBits;
1434 return 0;
1435 case GLX_BLUE_SIZE:
1436 *value = glxvis->gl_visual->BlueBits;
1437 return 0;
1438 case GLX_ALPHA_SIZE:
1439 *value = glxvis->gl_visual->AlphaBits;
1440 return 0;
1441 case GLX_DEPTH_SIZE:
1442 *value = glxvis->gl_visual->DepthBits;
1443 return 0;
1444 case GLX_STENCIL_SIZE:
1445 *value = glxvis->gl_visual->StencilBits;
1446 return 0;
1447 case GLX_ACCUM_RED_SIZE:
1448 *value = glxvis->gl_visual->AccumRedBits;
1449 return 0;
1450 case GLX_ACCUM_GREEN_SIZE:
1451 *value = glxvis->gl_visual->AccumGreenBits;
1452 return 0;
1453 case GLX_ACCUM_BLUE_SIZE:
1454 *value = glxvis->gl_visual->AccumBlueBits;
1455 return 0;
1456 case GLX_ACCUM_ALPHA_SIZE:
1457 *value = glxvis->gl_visual->AccumAlphaBits;
1458 return 0;
1459
1460 /*
1461 * GLX_EXT_visual_info extension
1462 */
1463 case GLX_X_VISUAL_TYPE_EXT:
1464 switch (glxvis->visinfo->CLASS) {
1465 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1466 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1467 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1468 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1469 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1470 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1471 }
1472 return 0;
1473 case GLX_TRANSPARENT_TYPE_EXT:
1474 if (glxvis->level==0) {
1475 /* normal planes */
1476 *value = GLX_NONE_EXT;
1477 }
1478 else if (glxvis->level>0) {
1479 /* overlay */
1480 if (glxvis->gl_visual->RGBAflag) {
1481 *value = GLX_TRANSPARENT_RGB_EXT;
1482 }
1483 else {
1484 *value = GLX_TRANSPARENT_INDEX_EXT;
1485 }
1486 }
1487 else if (glxvis->level<0) {
1488 /* underlay */
1489 *value = GLX_NONE_EXT;
1490 }
1491 return 0;
1492 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1493 {
1494 int pixel = transparent_pixel( glxvis );
1495 if (pixel>=0) {
1496 *value = pixel;
1497 }
1498 /* else undefined */
1499 }
1500 return 0;
1501 case GLX_TRANSPARENT_RED_VALUE_EXT:
1502 /* undefined */
1503 return 0;
1504 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1505 /* undefined */
1506 return 0;
1507 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1508 /* undefined */
1509 return 0;
1510 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1511 /* undefined */
1512 return 0;
1513
1514 /*
1515 * GLX_EXT_visual_info extension
1516 */
1517 case GLX_VISUAL_CAVEAT_EXT:
1518 /* test for zero, just in case */
1519 if (glxvis->VisualCaveat > 0)
1520 *value = glxvis->VisualCaveat;
1521 else
1522 *value = GLX_NONE_EXT;
1523 return 0;
1524
1525 /*
1526 * Extensions
1527 */
1528 default:
1529 return GLX_BAD_ATTRIBUTE;
1530 }
1531 }
1532
1533
1534
1535 static void
1536 Fake_glXWaitGL( void )
1537 {
1538 XMesaContext xmesa = XMesaGetCurrentContext();
1539 XMesaFlush( xmesa );
1540 }
1541
1542
1543
1544 static void
1545 Fake_glXWaitX( void )
1546 {
1547 XMesaContext xmesa = XMesaGetCurrentContext();
1548 XMesaFlush( xmesa );
1549 }
1550
1551
1552 /*
1553 * Return the extensions string, which is 3Dfx-dependant.
1554 */
1555 static const char *get_extensions( void )
1556 {
1557 #ifdef FX
1558 const char *fx = getenv("MESA_GLX_FX");
1559 if (fx && fx[0] != 'd') {
1560 return "GLX_MESA_pixmap_colormap GLX_EXT_visual_info GLX_EXT_visual_rating GLX_MESA_release_buffers GLX_MESA_copy_sub_buffer GLX_SGI_video_sync GLX_MESA_set_3dfx_mode GLX_ARB_get_proc_address";
1561 }
1562 #endif
1563 return "GLX_MESA_pixmap_colormap GLX_EXT_visual_info GLX_EXT_visual_rating GLX_MESA_release_buffers GLX_MESA_copy_sub_buffer GLX_SGI_video_sync GLX_ARB_get_proc_address";
1564 }
1565
1566
1567
1568 /* GLX 1.1 and later */
1569 static const char *
1570 Fake_glXQueryExtensionsString( Display *dpy, int screen )
1571 {
1572 (void) dpy;
1573 (void) screen;
1574 return get_extensions();
1575 }
1576
1577
1578
1579 /* GLX 1.1 and later */
1580 static const char *
1581 Fake_glXQueryServerString( Display *dpy, int screen, int name )
1582 {
1583 static char version[1000];
1584 sprintf(version, "%d.%d %s", SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION,
1585 MESA_GLX_VERSION);
1586
1587 (void) dpy;
1588 (void) screen;
1589
1590 switch (name) {
1591 case GLX_EXTENSIONS:
1592 return get_extensions();
1593 case GLX_VENDOR:
1594 return VENDOR;
1595 case GLX_VERSION:
1596 return version;
1597 default:
1598 return NULL;
1599 }
1600 }
1601
1602
1603
1604 /* GLX 1.1 and later */
1605 static const char *
1606 Fake_glXGetClientString( Display *dpy, int name )
1607 {
1608 static char version[1000];
1609 sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION, CLIENT_MINOR_VERSION,
1610 MESA_GLX_VERSION);
1611
1612 (void) dpy;
1613
1614 switch (name) {
1615 case GLX_EXTENSIONS:
1616 return get_extensions();
1617 case GLX_VENDOR:
1618 return VENDOR;
1619 case GLX_VERSION:
1620 return version;
1621 default:
1622 return NULL;
1623 }
1624 }
1625
1626
1627
1628 /*
1629 * GLX 1.3 and later
1630 */
1631
1632 static GLXFBConfig
1633 Fake_glXChooseFBConfig( Display *dpy, int screen,
1634 const int *attribList, int *nitems )
1635 {
1636 (void) dpy;
1637 (void) screen;
1638 (void) attribList;
1639 (void) nitems;
1640 return 0;
1641 }
1642
1643
1644 static int
1645 Fake_glXGetFBConfigAttrib( Display *dpy, GLXFBConfig config,
1646 int attribute, int *value )
1647 {
1648 (void) dpy;
1649 (void) config;
1650 (void) attribute;
1651 (void) value;
1652 return 0;
1653 }
1654
1655
1656 static XVisualInfo *
1657 Fake_glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1658 {
1659 (void) dpy;
1660 (void) config;
1661 return 0;
1662 }
1663
1664
1665 static GLXWindow
1666 Fake_glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
1667 const int *attribList )
1668 {
1669 (void) dpy;
1670 (void) config;
1671 (void) win;
1672 (void) attribList;
1673 return 0;
1674 }
1675
1676
1677 static void
1678 Fake_glXDestroyWindow( Display *dpy, GLXWindow window )
1679 {
1680 (void) dpy;
1681 (void) window;
1682 return;
1683 }
1684
1685
1686 static GLXPixmap
1687 Fake_glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
1688 const int *attribList )
1689 {
1690 (void) dpy;
1691 (void) config;
1692 (void) pixmap;
1693 (void) attribList;
1694 return 0;
1695 }
1696
1697
1698 static void
1699 Fake_glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
1700 {
1701 (void) dpy;
1702 (void) pixmap;
1703 return;
1704 }
1705
1706
1707 static GLXPbuffer
1708 Fake_glXCreatePbuffer( Display *dpy, GLXFBConfig config,
1709 const int *attribList )
1710 {
1711 (void) dpy;
1712 (void) config;
1713 (void) attribList;
1714 return 0;
1715 }
1716
1717
1718 static void
1719 Fake_glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
1720 {
1721 (void) dpy;
1722 (void) pbuf;
1723 }
1724
1725
1726 static void
1727 Fake_glXQueryDrawable( Display *dpy, GLXDrawable draw, int attribute,
1728 unsigned int *value )
1729 {
1730 (void) dpy;
1731 (void) draw;
1732 (void) attribute;
1733 (void) value;
1734 }
1735
1736
1737 static GLXContext
1738 Fake_glXCreateNewContext( Display *dpy, GLXFBConfig config,
1739 int renderType, GLXContext shareList, Bool direct )
1740 {
1741 (void) dpy;
1742 (void) config;
1743 (void) renderType;
1744 (void) shareList;
1745 (void) direct;
1746 return 0;
1747 }
1748
1749
1750 static int
1751 Fake_glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
1752 {
1753 (void) dpy;
1754 (void) ctx;
1755 (void) attribute;
1756 (void) value;
1757 return 0;
1758 }
1759
1760
1761 static void
1762 Fake_glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
1763 {
1764 (void) dpy;
1765 (void) drawable;
1766 (void) mask;
1767 }
1768
1769
1770 static void
1771 Fake_glXGetSelectedEvent( Display *dpy, GLXDrawable drawable,
1772 unsigned long *mask )
1773 {
1774 (void) dpy;
1775 (void) drawable;
1776 (void) mask;
1777 }
1778
1779
1780
1781 /*
1782 * Release the depth, stencil, accum buffers attached to a GLXDrawable
1783 * (a window or pixmap) prior to destroying the GLXDrawable.
1784 */
1785 static Bool
1786 Fake_glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
1787 {
1788 XMesaBuffer b = XMesaFindBuffer(dpy, d);
1789 if (b) {
1790 XMesaDestroyBuffer(b);
1791 return True;
1792 }
1793 return False;
1794 }
1795
1796
1797 /*
1798 * GLX_MESA_set_3dfx_mode
1799 */
1800 static GLboolean
1801 Fake_glXSet3DfxModeMESA( GLint mode )
1802 {
1803 return XMesaSetFXmode( mode );
1804 }
1805
1806
1807 /*
1808 * GLX_SGI_video_sync
1809 */
1810
1811 #ifdef GLX_SGI_video_sync
1812
1813 static int
1814 Fake_glXGetVideoSyncSGI(unsigned int *count)
1815 {
1816 return 0;
1817 }
1818
1819
1820 static int
1821 Fake_glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1822 {
1823 return 0;
1824 }
1825
1826 #endif
1827
1828
1829
1830 extern void Fake_glXUseXFont( Font font, int first, int count, int listbase );
1831
1832
1833 extern struct _glxapi_table *_mesa_GetGLXDispatchTable(void);
1834 struct _glxapi_table *_mesa_GetGLXDispatchTable(void)
1835 {
1836 static struct _glxapi_table glx;
1837
1838 /* be sure our dispatch table size <= libGL's table */
1839 {
1840 int size = sizeof(struct _glxapi_table) / sizeof(void *);
1841 (void) size;
1842 assert(_glxapi_get_dispatch_table_size() >= size);
1843 }
1844
1845 /* initialize the whole table to no-ops */
1846 _glxapi_set_no_op_table(&glx);
1847
1848 /* now initialize the table with the functions I implement */
1849 glx.ChooseVisual = Fake_glXChooseVisual;
1850 glx.CopyContext = Fake_glXCopyContext;
1851 glx.CreateContext = Fake_glXCreateContext;
1852 glx.CreateGLXPixmap = Fake_glXCreateGLXPixmap;
1853 glx.DestroyContext = Fake_glXDestroyContext;
1854 glx.DestroyGLXPixmap = Fake_glXDestroyGLXPixmap;
1855 glx.GetConfig = Fake_glXGetConfig;
1856 /*glx.GetCurrentContext = Fake_glXGetCurrentContext;*/
1857 /*glx.GetCurrentDrawable = Fake_glXGetCurrentDrawable;*/
1858 glx.IsDirect = Fake_glXIsDirect;
1859 glx.MakeCurrent = Fake_glXMakeCurrent;
1860 glx.QueryExtension = Fake_glXQueryExtension;
1861 glx.QueryVersion = Fake_glXQueryVersion;
1862 glx.SwapBuffers = Fake_glXSwapBuffers;
1863 glx.UseXFont = Fake_glXUseXFont;
1864 glx.WaitGL = Fake_glXWaitGL;
1865 glx.WaitX = Fake_glXWaitX;
1866
1867 #ifdef _GLXAPI_VERSION_1_1
1868 glx.GetClientString = Fake_glXGetClientString;
1869 glx.QueryExtensionsString = Fake_glXQueryExtensionsString;
1870 glx.QueryServerString = Fake_glXQueryServerString;
1871 #endif
1872
1873 #ifdef _GLXAPI_VERSION_1_2
1874 /*glx.GetCurrentDisplay = Fake_glXGetCurrentDisplay;*/
1875 #endif
1876
1877 #ifdef _GLXAPI_VERSION_1_3
1878 glx.ChooseFBConfig = Fake_glXChooseFBConfig;
1879 glx.CreateNewContext = Fake_glXCreateNewContext;
1880 glx.CreatePbuffer = Fake_glXCreatePbuffer;
1881 glx.CreatePixmap = Fake_glXCreatePixmap;
1882 glx.CreateWindow = Fake_glXCreateWindow;
1883 glx.DestroyPbuffer = Fake_glXDestroyPbuffer;
1884 glx.DestroyPixmap = Fake_glXDestroyPixmap;
1885 glx.DestroyWindow = Fake_glXDestroyWindow;
1886 /*glx.GetCurrentReadDrawable = Fake_glXGetCurrentReadDrawable;*/
1887 glx.GetFBConfigAttrib = Fake_glXGetFBConfigAttrib;
1888 glx.GetSelectedEvent = Fake_glXGetSelectedEvent;
1889 glx.GetVisualFromFBConfig = Fake_glXGetVisualFromFBConfig;
1890 glx.MakeContextCurrent = Fake_glXMakeContextCurrent;
1891 glx.QueryContext = Fake_glXQueryContext;
1892 glx.QueryDrawable = Fake_glXQueryDrawable;
1893 glx.SelectEvent = Fake_glXSelectEvent;
1894 #endif
1895
1896 #ifdef _GLXAPI_SGI_video_sync
1897 glx.GetVideoSyncSGI = Fake_glXGetVideoSyncSGI;
1898 glx.WaitVideoSyncSGI = Fake_glXWaitVideoSyncSGI;
1899 #endif
1900
1901 #ifdef _GLXAPI_MESA_copy_sub_buffer
1902 glx.CopySubBufferMESA = Fake_glXCopySubBufferMESA;
1903 #endif
1904
1905 #ifdef _GLXAPI_MESA_release_buffers
1906 glx.ReleaseBuffersMESA = Fake_glXReleaseBuffersMESA;
1907 #endif
1908
1909 #ifdef _GLXAPI_MESA_pixmap_colormap
1910 glx.CreateGLXPixmapMESA = Fake_glXCreateGLXPixmapMESA;
1911 #endif
1912
1913 #ifdef _GLXAPI_MESA_set_3dfx_mode
1914 glx.Set3DfxModeMESA = Fake_glXSet3DfxModeMESA;
1915 #endif
1916
1917 return &glx;
1918 }