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