08e01c76675f036d976fe067185df3bee8d5e875
[mesa.git] / src / mesa / drivers / x11 / fakeglx.c
1 /* $Id: fakeglx.c,v 1.27 2000/03/31 01:07:14 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 = XMesaCreateVisual2( 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
890 parselist = list;
891
892 while (*parselist) {
893
894 switch (*parselist) {
895 case GLX_USE_GL:
896 /* ignore */
897 parselist++;
898 break;
899 case GLX_BUFFER_SIZE:
900 parselist++;
901 min_ci = *parselist++;
902 break;
903 case GLX_LEVEL:
904 parselist++;
905 level = *parselist++;
906 break;
907 case GLX_RGBA:
908 rgb_flag = GL_TRUE;
909 parselist++;
910 break;
911 case GLX_DOUBLEBUFFER:
912 double_flag = GL_TRUE;
913 parselist++;
914 break;
915 case GLX_STEREO:
916 stereo_flag = GL_TRUE;
917 return NULL;
918 case GLX_AUX_BUFFERS:
919 /* ignore */
920 parselist++;
921 parselist++;
922 break;
923 case GLX_RED_SIZE:
924 parselist++;
925 min_red = *parselist++;
926 break;
927 case GLX_GREEN_SIZE:
928 parselist++;
929 min_green = *parselist++;
930 break;
931 case GLX_BLUE_SIZE:
932 parselist++;
933 min_blue = *parselist++;
934 break;
935 case GLX_ALPHA_SIZE:
936 parselist++;
937 {
938 GLint size = *parselist++;
939 alpha_flag = size>0 ? 1 : 0;
940 }
941 break;
942 case GLX_DEPTH_SIZE:
943 parselist++;
944 depth_size = *parselist++;
945 break;
946 case GLX_STENCIL_SIZE:
947 parselist++;
948 stencil_size = *parselist++;
949 break;
950 case GLX_ACCUM_RED_SIZE:
951 parselist++;
952 {
953 GLint size = *parselist++;
954 accumRedSize = MAX2( accumRedSize, size );
955 }
956 break;
957 case GLX_ACCUM_GREEN_SIZE:
958 parselist++;
959 {
960 GLint size = *parselist++;
961 accumGreenSize = MAX2( accumGreenSize, size );
962 }
963 break;
964 case GLX_ACCUM_BLUE_SIZE:
965 parselist++;
966 {
967 GLint size = *parselist++;
968 accumBlueSize = MAX2( accumBlueSize, size );
969 }
970 break;
971 case GLX_ACCUM_ALPHA_SIZE:
972 parselist++;
973 {
974 GLint size = *parselist++;
975 accumAlphaSize = MAX2( accumAlphaSize, size );
976 }
977 break;
978
979 /*
980 * GLX_EXT_visual_info extension
981 */
982 case GLX_X_VISUAL_TYPE_EXT:
983 parselist++;
984 visual_type = *parselist++;
985 break;
986 case GLX_TRANSPARENT_TYPE_EXT:
987 parselist++;
988 trans_type = *parselist++;
989 break;
990 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
991 parselist++;
992 trans_value = *parselist++;
993 break;
994 case GLX_TRANSPARENT_RED_VALUE_EXT:
995 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
996 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
997 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
998 /* ignore */
999 parselist++;
1000 parselist++;
1001 break;
1002
1003 case None:
1004 break;
1005 default:
1006 /* undefined attribute */
1007 return NULL;
1008 }
1009 }
1010
1011 /*
1012 * Since we're only simulating the GLX extension this function will never
1013 * find any real GL visuals. Instead, all we can do is try to find an RGB
1014 * or CI visual of appropriate depth. Other requested attributes such as
1015 * double buffering, depth buffer, etc. will be associated with the X
1016 * visual and stored in the VisualTable[].
1017 */
1018 if (level==0) {
1019 /* normal color planes */
1020 if (rgb_flag) {
1021 /* Get an RGB visual */
1022 int min_rgb = min_red + min_green + min_blue;
1023 if (min_rgb>1 && min_rgb<8) {
1024 /* a special case to be sure we can get a monochrome visual */
1025 min_rgb = 1;
1026 }
1027 vis = choose_x_visual( dpy, screen, rgb_flag, min_rgb, visual_type );
1028 }
1029 else {
1030 /* Get a color index visual */
1031 vis = choose_x_visual( dpy, screen, rgb_flag, min_ci, visual_type );
1032 accumRedSize = accumGreenSize = accumBlueSize = accumAlphaSize = 0;
1033 }
1034 }
1035 else {
1036 /* over/underlay planes */
1037 if (rgb_flag) {
1038 /* rgba overlay */
1039 int min_rgb = min_red + min_green + min_blue;
1040 if (min_rgb>1 && min_rgb<8) {
1041 /* a special case to be sure we can get a monochrome visual */
1042 min_rgb = 1;
1043 }
1044 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1045 trans_type, trans_value, min_rgb, visual_type );
1046 }
1047 else {
1048 /* color index overlay */
1049 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1050 trans_type, trans_value, min_ci, visual_type );
1051 }
1052 }
1053
1054 if (vis) {
1055 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1056 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1057 * largest depth buffer size, which is 32bits/value. However, we
1058 * return 16 to maintain performance with earlier versions of Mesa.
1059 */
1060 if (depth_size == 1)
1061 depth_size = DEFAULT_SOFTWARE_DEPTH_BITS;
1062 else if (depth_size > 24)
1063 depth_size = 31;
1064 else if (depth_size > 16)
1065 depth_size = 24;
1066 /* we only support one size of stencil and accum buffers. */
1067 if (stencil_size > 0)
1068 stencil_size = STENCIL_BITS;
1069 if (accumRedSize > 0)
1070 accumRedSize = ACCUM_BITS;
1071 if (accumGreenSize > 0)
1072 accumGreenSize = ACCUM_BITS;
1073 if (accumBlueSize > 0)
1074 accumBlueSize = ACCUM_BITS;
1075 if (accumAlphaSize > 0)
1076 accumAlphaSize = ACCUM_BITS;
1077 if (!save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1078 stereo_flag, depth_size, stencil_size,
1079 accumRedSize, accumGreenSize,
1080 accumBlueSize, accumAlphaSize,
1081 level ))
1082 return NULL;
1083 }
1084
1085 return vis;
1086 }
1087
1088
1089
1090
1091 static GLXContext
1092 Fake_glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1093 GLXContext share_list, Bool direct )
1094 {
1095 XMesaVisual glxvis;
1096 XMesaContext xmctx;
1097
1098 /* deallocate unused windows/buffers */
1099 XMesaGarbageCollect();
1100
1101 glxvis = find_glx_visual( dpy, visinfo );
1102 if (!glxvis) {
1103 /* This visual wasn't found with glXChooseVisual() */
1104 glxvis = create_glx_visual( dpy, visinfo );
1105 if (!glxvis) {
1106 /* unusable visual */
1107 return NULL;
1108 }
1109 }
1110
1111 xmctx = XMesaCreateContext( glxvis, (XMesaContext) share_list );
1112 if (xmctx) {
1113 /* set the direct/indirect flag */
1114 xmctx->direct = direct;
1115 }
1116 return (GLXContext) xmctx;
1117 }
1118
1119
1120 static GLXContext MakeCurrent_PrevContext = 0;
1121 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1122 static GLXDrawable MakeCurrent_PrevReadable = 0;
1123 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1124 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1125
1126 /* GLX 1.3 and later */
1127 static Bool
1128 Fake_glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1129 GLXDrawable read, GLXContext ctx )
1130 {
1131 if (ctx && draw && read) {
1132 XMesaBuffer drawBuffer, readBuffer;
1133 XMesaContext xmctx = (XMesaContext) ctx;
1134
1135 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1136 if (ctx == MakeCurrent_PrevContext
1137 && draw == MakeCurrent_PrevDrawable) {
1138 drawBuffer = MakeCurrent_PrevDrawBuffer;
1139 }
1140 else {
1141 drawBuffer = XMesaFindBuffer( dpy, draw );
1142 }
1143 if (!drawBuffer) {
1144 /* drawable must be a new window! */
1145 drawBuffer = XMesaCreateWindowBuffer2( xmctx->xm_visual, draw, ctx );
1146 if (!drawBuffer) {
1147 /* Out of memory, or context/drawable depth mismatch */
1148 return False;
1149 }
1150 }
1151
1152 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1153 if (ctx == MakeCurrent_PrevContext
1154 && read == MakeCurrent_PrevReadable) {
1155 readBuffer = MakeCurrent_PrevReadBuffer;
1156 }
1157 else {
1158 readBuffer = XMesaFindBuffer( dpy, read );
1159 }
1160 if (!readBuffer) {
1161 /* drawable must be a new window! */
1162 readBuffer = XMesaCreateWindowBuffer2( xmctx->xm_visual, read, ctx );
1163 if (!readBuffer) {
1164 /* Out of memory, or context/drawable depth mismatch */
1165 return False;
1166 }
1167 }
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 return (Bool) XMesaMakeCurrent2((XMesaContext) ctx, drawBuffer, readBuffer);
1177 }
1178 else if (!ctx && !draw && !read) {
1179 /* release current context w/out assigning new one. */
1180 XMesaMakeCurrent( NULL, NULL );
1181 MakeCurrent_PrevContext = 0;
1182 MakeCurrent_PrevDrawable = 0;
1183 MakeCurrent_PrevReadable = 0;
1184 MakeCurrent_PrevDrawBuffer = 0;
1185 MakeCurrent_PrevReadBuffer = 0;
1186 return True;
1187 }
1188 else {
1189 /* The args must either all be non-zero or all zero.
1190 * This is an error.
1191 */
1192 return False;
1193 }
1194 }
1195
1196
1197
1198 static Bool
1199 Fake_glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1200 {
1201 return Fake_glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1202 }
1203
1204
1205
1206 static GLXPixmap
1207 Fake_glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1208 {
1209 XMesaVisual v;
1210 XMesaBuffer b;
1211
1212 v = find_glx_visual( dpy, visinfo );
1213 if (!v) {
1214 v = create_glx_visual( dpy, visinfo );
1215 if (!v) {
1216 /* unusable visual */
1217 return 0;
1218 }
1219 }
1220
1221 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1222 if (!b) {
1223 return 0;
1224 }
1225 return b->frontbuffer;
1226 }
1227
1228
1229 #ifdef GLX_MESA_pixmap_colormap
1230
1231 static GLXPixmap
1232 Fake_glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1233 Pixmap pixmap, Colormap cmap )
1234 {
1235 XMesaVisual v;
1236 XMesaBuffer b;
1237
1238 v = find_glx_visual( dpy, visinfo );
1239 if (!v) {
1240 v = create_glx_visual( dpy, visinfo );
1241 if (!v) {
1242 /* unusable visual */
1243 return 0;
1244 }
1245 }
1246
1247 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1248 if (!b) {
1249 return 0;
1250 }
1251 return b->frontbuffer;
1252 }
1253
1254 #endif
1255
1256
1257 static void
1258 Fake_glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1259 {
1260 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1261 if (b) {
1262 XMesaDestroyBuffer(b);
1263 }
1264 else if (getenv("MESA_DEBUG")) {
1265 fprintf( stderr, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1266 }
1267 }
1268
1269
1270 static void
1271 Fake_glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1272 unsigned long mask )
1273 {
1274 XMesaContext xm_src = (XMesaContext) src;
1275 XMesaContext xm_dst = (XMesaContext) dst;
1276 (void) dpy;
1277 gl_copy_context( xm_src->gl_ctx, xm_dst->gl_ctx, (GLuint) mask );
1278 }
1279
1280
1281
1282 static Bool
1283 Fake_glXQueryExtension( Display *dpy, int *errorb, int *event )
1284 {
1285 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1286 (void) dpy;
1287 (void) errorb;
1288 (void) event;
1289 return True;
1290 }
1291
1292
1293 extern void _kw_ungrab_all( Display *dpy );
1294 void _kw_ungrab_all( Display *dpy )
1295 {
1296 XUngrabPointer( dpy, CurrentTime );
1297 XUngrabKeyboard( dpy, CurrentTime );
1298 }
1299
1300
1301 static void
1302 Fake_glXDestroyContext( Display *dpy, GLXContext ctx )
1303 {
1304 (void) dpy;
1305 MakeCurrent_PrevContext = 0;
1306 MakeCurrent_PrevDrawable = 0;
1307 MakeCurrent_PrevReadable = 0;
1308 MakeCurrent_PrevDrawBuffer = 0;
1309 MakeCurrent_PrevReadBuffer = 0;
1310 XMesaDestroyContext( (XMesaContext) ctx );
1311 XMesaGarbageCollect();
1312 }
1313
1314
1315
1316 static Bool
1317 Fake_glXIsDirect( Display *dpy, GLXContext ctx )
1318 {
1319 (void) dpy;
1320 return ((XMesaContext) ctx)->direct;
1321 }
1322
1323
1324
1325 static void
1326 Fake_glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1327 {
1328 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1329
1330 if (buffer) {
1331 XMesaSwapBuffers(buffer);
1332 }
1333 else if (getenv("MESA_DEBUG")) {
1334 fprintf(stderr, "Mesa Warning: glXSwapBuffers: invalid drawable\n");
1335 }
1336 }
1337
1338
1339 static void
1340 Fake_glXCopySubBufferMESA( Display *dpy, GLXDrawable drawable,
1341 int x, int y, int width, int height )
1342 {
1343 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1344 if (buffer) {
1345 XMesaCopySubBuffer(buffer, x, y, width, height);
1346 }
1347 else if (getenv("MESA_DEBUG")) {
1348 fprintf(stderr, "Mesa Warning: glXCopySubBufferMESA: invalid drawable\n");
1349 }
1350 }
1351
1352
1353
1354 static Bool
1355 Fake_glXQueryVersion( Display *dpy, int *maj, int *min )
1356 {
1357 (void) dpy;
1358 /* Return GLX version, not Mesa version */
1359 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1360 *maj = CLIENT_MAJOR_VERSION;
1361 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1362 return True;
1363 }
1364
1365
1366
1367 /*
1368 * Query the GLX attributes of the given XVisualInfo.
1369 */
1370 static int
1371 Fake_glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1372 int attrib, int *value )
1373 {
1374 XMesaVisual glxvis;
1375
1376 glxvis = find_glx_visual( dpy, visinfo );
1377 if (!glxvis) {
1378 /* this visual wasn't obtained with glXChooseVisual */
1379 glxvis = create_glx_visual( dpy, visinfo );
1380 if (!glxvis) {
1381 /* this visual can't be used for GL rendering */
1382 if (attrib==GLX_USE_GL) {
1383 *value = (int) False;
1384 return 0;
1385 }
1386 else {
1387 /*fprintf( stderr, "Mesa: Error in glXGetConfig: bad visual\n");*/
1388 return GLX_BAD_VISUAL;
1389 }
1390 }
1391 }
1392
1393 switch(attrib) {
1394 case GLX_USE_GL:
1395 *value = (int) True;
1396 return 0;
1397 case GLX_BUFFER_SIZE:
1398 *value = visinfo->depth;
1399 return 0;
1400 case GLX_LEVEL:
1401 *value = glxvis->level;
1402 return 0;
1403 case GLX_RGBA:
1404 if (glxvis->gl_visual->RGBAflag) {
1405 *value = True;
1406 }
1407 else {
1408 *value = False;
1409 }
1410 return 0;
1411 case GLX_DOUBLEBUFFER:
1412 *value = (int) glxvis->gl_visual->DBflag;
1413 return 0;
1414 case GLX_STEREO:
1415 *value = (int) glxvis->gl_visual->StereoFlag;
1416 return 0;
1417 case GLX_AUX_BUFFERS:
1418 *value = (int) False;
1419 return 0;
1420 case GLX_RED_SIZE:
1421 *value = glxvis->gl_visual->RedBits;
1422 return 0;
1423 case GLX_GREEN_SIZE:
1424 *value = glxvis->gl_visual->GreenBits;
1425 return 0;
1426 case GLX_BLUE_SIZE:
1427 *value = glxvis->gl_visual->BlueBits;
1428 return 0;
1429 case GLX_ALPHA_SIZE:
1430 *value = glxvis->gl_visual->AlphaBits;
1431 return 0;
1432 case GLX_DEPTH_SIZE:
1433 *value = glxvis->gl_visual->DepthBits;
1434 return 0;
1435 case GLX_STENCIL_SIZE:
1436 *value = glxvis->gl_visual->StencilBits;
1437 return 0;
1438 case GLX_ACCUM_RED_SIZE:
1439 *value = glxvis->gl_visual->AccumRedBits;
1440 return 0;
1441 case GLX_ACCUM_GREEN_SIZE:
1442 *value = glxvis->gl_visual->AccumGreenBits;
1443 return 0;
1444 case GLX_ACCUM_BLUE_SIZE:
1445 *value = glxvis->gl_visual->AccumBlueBits;
1446 return 0;
1447 case GLX_ACCUM_ALPHA_SIZE:
1448 *value = glxvis->gl_visual->AccumAlphaBits;
1449 return 0;
1450
1451 /*
1452 * GLX_EXT_visual_info extension
1453 */
1454 case GLX_X_VISUAL_TYPE_EXT:
1455 switch (glxvis->visinfo->CLASS) {
1456 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1457 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1458 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1459 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1460 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1461 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1462 }
1463 return 0;
1464 case GLX_TRANSPARENT_TYPE_EXT:
1465 if (glxvis->level==0) {
1466 /* normal planes */
1467 *value = GLX_NONE_EXT;
1468 }
1469 else if (glxvis->level>0) {
1470 /* overlay */
1471 if (glxvis->gl_visual->RGBAflag) {
1472 *value = GLX_TRANSPARENT_RGB_EXT;
1473 }
1474 else {
1475 *value = GLX_TRANSPARENT_INDEX_EXT;
1476 }
1477 }
1478 else if (glxvis->level<0) {
1479 /* underlay */
1480 *value = GLX_NONE_EXT;
1481 }
1482 return 0;
1483 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1484 {
1485 int pixel = transparent_pixel( glxvis );
1486 if (pixel>=0) {
1487 *value = pixel;
1488 }
1489 /* else undefined */
1490 }
1491 return 0;
1492 case GLX_TRANSPARENT_RED_VALUE_EXT:
1493 /* undefined */
1494 return 0;
1495 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1496 /* undefined */
1497 return 0;
1498 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1499 /* undefined */
1500 return 0;
1501 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1502 /* undefined */
1503 return 0;
1504
1505 /*
1506 * GLX_EXT_visual_info extension
1507 */
1508 case GLX_VISUAL_CAVEAT_EXT:
1509 return glxvis->VisualCaveat;
1510
1511 /*
1512 * Extensions
1513 */
1514 default:
1515 return GLX_BAD_ATTRIBUTE;
1516 }
1517 }
1518
1519
1520
1521 static void
1522 Fake_glXWaitGL( void )
1523 {
1524 XMesaContext xmesa = XMesaGetCurrentContext();
1525 XMesaFlush( xmesa );
1526 }
1527
1528
1529
1530 static void
1531 Fake_glXWaitX( void )
1532 {
1533 XMesaContext xmesa = XMesaGetCurrentContext();
1534 XMesaFlush( xmesa );
1535 }
1536
1537
1538 /*
1539 * Return the extensions string, which is 3Dfx-dependant.
1540 */
1541 static const char *get_extensions( void )
1542 {
1543 #ifdef FX
1544 const char *fx = getenv("MESA_GLX_FX");
1545 if (fx && fx[0] != 'd') {
1546 return "GLX_MESA_pixmap_colormap GLX_EXT_visual_info GLX_MESA_release_buffers GLX_MESA_copy_sub_buffer GLX_SGI_video_sync GLX_MESA_set_3dfx_mode GLX_ARB_get_proc_address";
1547 }
1548 #endif
1549 return "GLX_MESA_pixmap_colormap GLX_EXT_visual_info GLX_MESA_release_buffers GLX_MESA_copy_sub_buffer GLX_SGI_video_sync GLX_ARB_get_proc_address";
1550 }
1551
1552
1553
1554 /* GLX 1.1 and later */
1555 static const char *
1556 Fake_glXQueryExtensionsString( Display *dpy, int screen )
1557 {
1558 (void) dpy;
1559 (void) screen;
1560 return get_extensions();
1561 }
1562
1563
1564
1565 /* GLX 1.1 and later */
1566 static const char *
1567 Fake_glXQueryServerString( Display *dpy, int screen, int name )
1568 {
1569 static char version[1000];
1570 sprintf(version, "%d.%d %s", SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION,
1571 MESA_GLX_VERSION);
1572
1573 (void) dpy;
1574 (void) screen;
1575
1576 switch (name) {
1577 case GLX_EXTENSIONS:
1578 return get_extensions();
1579 case GLX_VENDOR:
1580 return VENDOR;
1581 case GLX_VERSION:
1582 return version;
1583 default:
1584 return NULL;
1585 }
1586 }
1587
1588
1589
1590 /* GLX 1.1 and later */
1591 static const char *
1592 Fake_glXGetClientString( Display *dpy, int name )
1593 {
1594 static char version[1000];
1595 sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION, CLIENT_MINOR_VERSION,
1596 MESA_GLX_VERSION);
1597
1598 (void) dpy;
1599
1600 switch (name) {
1601 case GLX_EXTENSIONS:
1602 return get_extensions();
1603 case GLX_VENDOR:
1604 return VENDOR;
1605 case GLX_VERSION:
1606 return version;
1607 default:
1608 return NULL;
1609 }
1610 }
1611
1612
1613
1614 /*
1615 * GLX 1.3 and later
1616 */
1617
1618 static GLXFBConfig
1619 Fake_glXChooseFBConfig( Display *dpy, int screen,
1620 const int *attribList, int *nitems )
1621 {
1622 (void) dpy;
1623 (void) screen;
1624 (void) attribList;
1625 (void) nitems;
1626 return 0;
1627 }
1628
1629
1630 static int
1631 Fake_glXGetFBConfigAttrib( Display *dpy, GLXFBConfig config,
1632 int attribute, int *value )
1633 {
1634 (void) dpy;
1635 (void) config;
1636 (void) attribute;
1637 (void) value;
1638 return 0;
1639 }
1640
1641
1642 static XVisualInfo *
1643 Fake_glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1644 {
1645 (void) dpy;
1646 (void) config;
1647 return 0;
1648 }
1649
1650
1651 static GLXWindow
1652 Fake_glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
1653 const int *attribList )
1654 {
1655 (void) dpy;
1656 (void) config;
1657 (void) win;
1658 (void) attribList;
1659 return 0;
1660 }
1661
1662
1663 static void
1664 Fake_glXDestroyWindow( Display *dpy, GLXWindow window )
1665 {
1666 (void) dpy;
1667 (void) window;
1668 return;
1669 }
1670
1671
1672 static GLXPixmap
1673 Fake_glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
1674 const int *attribList )
1675 {
1676 (void) dpy;
1677 (void) config;
1678 (void) pixmap;
1679 (void) attribList;
1680 return 0;
1681 }
1682
1683
1684 static void
1685 Fake_glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
1686 {
1687 (void) dpy;
1688 (void) pixmap;
1689 return;
1690 }
1691
1692
1693 static GLXPbuffer
1694 Fake_glXCreatePbuffer( Display *dpy, GLXFBConfig config,
1695 const int *attribList )
1696 {
1697 (void) dpy;
1698 (void) config;
1699 (void) attribList;
1700 return 0;
1701 }
1702
1703
1704 static void
1705 Fake_glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
1706 {
1707 (void) dpy;
1708 (void) pbuf;
1709 }
1710
1711
1712 static void
1713 Fake_glXQueryDrawable( Display *dpy, GLXDrawable draw, int attribute,
1714 unsigned int *value )
1715 {
1716 (void) dpy;
1717 (void) draw;
1718 (void) attribute;
1719 (void) value;
1720 }
1721
1722
1723 static GLXContext
1724 Fake_glXCreateNewContext( Display *dpy, GLXFBConfig config,
1725 int renderType, GLXContext shareList, Bool direct )
1726 {
1727 (void) dpy;
1728 (void) config;
1729 (void) renderType;
1730 (void) shareList;
1731 (void) direct;
1732 return 0;
1733 }
1734
1735
1736 static int
1737 Fake_glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
1738 {
1739 (void) dpy;
1740 (void) ctx;
1741 (void) attribute;
1742 (void) value;
1743 return 0;
1744 }
1745
1746
1747 static void
1748 Fake_glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
1749 {
1750 (void) dpy;
1751 (void) drawable;
1752 (void) mask;
1753 }
1754
1755
1756 static void
1757 Fake_glXGetSelectedEvent( Display *dpy, GLXDrawable drawable,
1758 unsigned long *mask )
1759 {
1760 (void) dpy;
1761 (void) drawable;
1762 (void) mask;
1763 }
1764
1765
1766
1767 /*
1768 * Release the depth, stencil, accum buffers attached to a GLXDrawable
1769 * (a window or pixmap) prior to destroying the GLXDrawable.
1770 */
1771 static Bool
1772 Fake_glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
1773 {
1774 XMesaBuffer b = XMesaFindBuffer(dpy, d);
1775 if (b) {
1776 XMesaDestroyBuffer(b);
1777 return True;
1778 }
1779 return False;
1780 }
1781
1782
1783 /*
1784 * GLX_MESA_set_3dfx_mode
1785 */
1786 static GLboolean
1787 Fake_glXSet3DfxModeMESA( GLint mode )
1788 {
1789 return XMesaSetFXmode( mode );
1790 }
1791
1792
1793 /*
1794 * GLX_SGI_video_sync
1795 */
1796
1797 #ifdef GLX_SGI_video_sync
1798
1799 static int
1800 Fake_glXGetVideoSyncSGI(unsigned int *count)
1801 {
1802 return 0;
1803 }
1804
1805
1806 static int
1807 Fake_glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1808 {
1809 return 0;
1810 }
1811
1812 #endif
1813
1814
1815
1816 extern void Fake_glXUseXFont( Font font, int first, int count, int listbase );
1817
1818
1819 extern struct _glxapi_table *_mesa_GetGLXDispatchTable(void);
1820 struct _glxapi_table *_mesa_GetGLXDispatchTable(void)
1821 {
1822 static struct _glxapi_table glx;
1823
1824 /* be sure our dispatch table size <= libGL's table */
1825 {
1826 int size = sizeof(struct _glxapi_table) / sizeof(void *);
1827 (void) size;
1828 assert(_glxapi_get_dispatch_table_size() >= size);
1829 }
1830
1831 /* initialize the whole table to no-ops */
1832 _glxapi_set_no_op_table(&glx);
1833
1834 /* now initialize the table with the functions I implement */
1835 glx.ChooseVisual = Fake_glXChooseVisual;
1836 glx.CopyContext = Fake_glXCopyContext;
1837 glx.CreateContext = Fake_glXCreateContext;
1838 glx.CreateGLXPixmap = Fake_glXCreateGLXPixmap;
1839 glx.DestroyContext = Fake_glXDestroyContext;
1840 glx.DestroyGLXPixmap = Fake_glXDestroyGLXPixmap;
1841 glx.GetConfig = Fake_glXGetConfig;
1842 /*glx.GetCurrentContext = Fake_glXGetCurrentContext;*/
1843 /*glx.GetCurrentDrawable = Fake_glXGetCurrentDrawable;*/
1844 glx.IsDirect = Fake_glXIsDirect;
1845 glx.MakeCurrent = Fake_glXMakeCurrent;
1846 glx.QueryExtension = Fake_glXQueryExtension;
1847 glx.QueryVersion = Fake_glXQueryVersion;
1848 glx.SwapBuffers = Fake_glXSwapBuffers;
1849 glx.UseXFont = Fake_glXUseXFont;
1850 glx.WaitGL = Fake_glXWaitGL;
1851 glx.WaitX = Fake_glXWaitX;
1852
1853 #ifdef _GLXAPI_VERSION_1_1
1854 glx.GetClientString = Fake_glXGetClientString;
1855 glx.QueryExtensionsString = Fake_glXQueryExtensionsString;
1856 glx.QueryServerString = Fake_glXQueryServerString;
1857 #endif
1858
1859 #ifdef _GLXAPI_VERSION_1_2
1860 /*glx.GetCurrentDisplay = Fake_glXGetCurrentDisplay;*/
1861 #endif
1862
1863 #ifdef _GLXAPI_VERSION_1_3
1864 glx.ChooseFBConfig = Fake_glXChooseFBConfig;
1865 glx.CreateNewContext = Fake_glXCreateNewContext;
1866 glx.CreatePbuffer = Fake_glXCreatePbuffer;
1867 glx.CreatePixmap = Fake_glXCreatePixmap;
1868 glx.CreateWindow = Fake_glXCreateWindow;
1869 glx.DestroyPbuffer = Fake_glXDestroyPbuffer;
1870 glx.DestroyPixmap = Fake_glXDestroyPixmap;
1871 glx.DestroyWindow = Fake_glXDestroyWindow;
1872 /*glx.GetCurrentReadDrawable = Fake_glXGetCurrentReadDrawable;*/
1873 glx.GetFBConfigAttrib = Fake_glXGetFBConfigAttrib;
1874 glx.GetSelectedEvent = Fake_glXGetSelectedEvent;
1875 glx.GetVisualFromFBConfig = Fake_glXGetVisualFromFBConfig;
1876 glx.MakeContextCurrent = Fake_glXMakeContextCurrent;
1877 glx.QueryContext = Fake_glXQueryContext;
1878 glx.QueryDrawable = Fake_glXQueryDrawable;
1879 glx.SelectEvent = Fake_glXSelectEvent;
1880 #endif
1881
1882 #ifdef _GLXAPI_SGI_video_sync
1883 glx.GetVideoSyncSGI = Fake_glXGetVideoSyncSGI;
1884 glx.WaitVideoSyncSGI = Fake_glXWaitVideoSyncSGI;
1885 #endif
1886
1887 #ifdef _GLXAPI_MESA_copy_sub_buffer
1888 glx.CopySubBufferMESA = Fake_glXCopySubBufferMESA;
1889 #endif
1890
1891 #ifdef _GLXAPI_MESA_release_buffers
1892 glx.ReleaseBuffersMESA = Fake_glXReleaseBuffersMESA;
1893 #endif
1894
1895 #ifdef _GLXAPI_MESA_pixmap_colormap
1896 glx.CreateGLXPixmapMESA = Fake_glXCreateGLXPixmapMESA;
1897 #endif
1898
1899 #ifdef _GLXAPI_MESA_set_3dfx_mode
1900 glx.Set3DfxModeMESA = Fake_glXSet3DfxModeMESA;
1901 #endif
1902
1903 return &glx;
1904 }