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