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