mesa/xlib: return 0 for errorBase, eventBase in glXQueryExtension()
[mesa.git] / src / mesa / drivers / x11 / fakeglx.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * This is an emulation of the GLX API which allows Mesa/GLX-based programs
29 * to run on X servers which do not have the real GLX extension.
30 *
31 * Thanks to the contributors:
32 *
33 * Initial version: Philip Brown (phil@bolthole.com)
34 * Better glXGetConfig() support: Armin Liebchen (liebchen@asylum.cs.utah.edu)
35 * Further visual-handling refinements: Wolfram Gloger
36 * (wmglo@Dent.MED.Uni-Muenchen.DE).
37 *
38 * Notes:
39 * Don't be fooled, stereo isn't supported yet.
40 */
41
42
43
44 #include "glxheader.h"
45 #include "glxapi.h"
46 #include "main/context.h"
47 #include "main/config.h"
48 #include "main/macros.h"
49 #include "main/imports.h"
50 #include "main/mtypes.h"
51 #include "main/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 case GLX_SAMPLES_ARB:
1187 parselist++;
1188 if (*parselist++ != 0)
1189 /* ms not supported */
1190 return NULL;
1191 break;
1192
1193 /*
1194 * FBConfig attribs.
1195 */
1196 case GLX_RENDER_TYPE:
1197 if (!fbConfig)
1198 return NULL;
1199 parselist++;
1200 if (*parselist & GLX_RGBA_BIT) {
1201 rgb_flag = GL_TRUE;
1202 }
1203 else if (*parselist & GLX_COLOR_INDEX_BIT) {
1204 rgb_flag = GL_FALSE;
1205 }
1206 else if (*parselist == 0) {
1207 rgb_flag = GL_TRUE;
1208 }
1209 parselist++;
1210 break;
1211 case GLX_DRAWABLE_TYPE:
1212 if (!fbConfig)
1213 return NULL;
1214 parselist++;
1215 if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
1216 return NULL; /* bad bit */
1217 }
1218 parselist++;
1219 break;
1220 case GLX_FBCONFIG_ID:
1221 if (!fbConfig)
1222 return NULL;
1223 parselist++;
1224 desiredVisualID = *parselist++;
1225 break;
1226 case GLX_X_RENDERABLE:
1227 if (!fbConfig)
1228 return NULL;
1229 parselist += 2;
1230 /* ignore */
1231 break;
1232
1233 #ifdef GLX_EXT_texture_from_pixmap
1234 case GLX_BIND_TO_TEXTURE_RGB_EXT:
1235 parselist++; /*skip*/
1236 break;
1237 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1238 parselist++; /*skip*/
1239 break;
1240 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1241 parselist++; /*skip*/
1242 break;
1243 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1244 parselist++;
1245 if (*parselist & ~(GLX_TEXTURE_1D_BIT_EXT |
1246 GLX_TEXTURE_2D_BIT_EXT |
1247 GLX_TEXTURE_RECTANGLE_BIT_EXT)) {
1248 /* invalid bit */
1249 return NULL;
1250 }
1251 break;
1252 case GLX_Y_INVERTED_EXT:
1253 parselist++; /*skip*/
1254 break;
1255 #endif
1256
1257 case None:
1258 /* end of list */
1259 break;
1260
1261 default:
1262 /* undefined attribute */
1263 _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
1264 *parselist);
1265 return NULL;
1266 }
1267 }
1268
1269 (void) caveat;
1270
1271 /*
1272 * Since we're only simulating the GLX extension this function will never
1273 * find any real GL visuals. Instead, all we can do is try to find an RGB
1274 * or CI visual of appropriate depth. Other requested attributes such as
1275 * double buffering, depth buffer, etc. will be associated with the X
1276 * visual and stored in the VisualTable[].
1277 */
1278 if (desiredVisualID != -1) {
1279 /* try to get a specific visual, by visualID */
1280 XVisualInfo temp;
1281 int n;
1282 temp.visualid = desiredVisualID;
1283 temp.screen = screen;
1284 vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
1285 if (vis) {
1286 /* give the visual some useful GLX attributes */
1287 double_flag = GL_TRUE;
1288 if (vis->depth > 8)
1289 rgb_flag = GL_TRUE;
1290 depth_size = default_depth_bits();
1291 stencil_size = STENCIL_BITS;
1292 /* XXX accum??? */
1293 }
1294 }
1295 else if (level==0) {
1296 /* normal color planes */
1297 if (rgb_flag) {
1298 /* Get an RGB visual */
1299 int min_rgb = min_red + min_green + min_blue;
1300 if (min_rgb>1 && min_rgb<8) {
1301 /* a special case to be sure we can get a monochrome visual */
1302 min_rgb = 1;
1303 }
1304 vis = choose_x_visual( dpy, screen, rgb_flag, min_rgb, visual_type );
1305 }
1306 else {
1307 /* Get a color index visual */
1308 vis = choose_x_visual( dpy, screen, rgb_flag, min_ci, visual_type );
1309 accumRedSize = accumGreenSize = accumBlueSize = accumAlphaSize = 0;
1310 }
1311 }
1312 else {
1313 /* over/underlay planes */
1314 if (rgb_flag) {
1315 /* rgba overlay */
1316 int min_rgb = min_red + min_green + min_blue;
1317 if (min_rgb>1 && min_rgb<8) {
1318 /* a special case to be sure we can get a monochrome visual */
1319 min_rgb = 1;
1320 }
1321 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1322 trans_type, trans_value, min_rgb, visual_type );
1323 }
1324 else {
1325 /* color index overlay */
1326 vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1327 trans_type, trans_value, min_ci, visual_type );
1328 }
1329 }
1330
1331 if (vis) {
1332 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1333 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1334 * largest depth buffer size, which is 32bits/value. Instead, we
1335 * return 16 to maintain performance with earlier versions of Mesa.
1336 */
1337 if (depth_size > 24)
1338 depth_size = 32;
1339 else if (depth_size > 16)
1340 depth_size = 24;
1341 else if (depth_size > 0) {
1342 depth_size = default_depth_bits();
1343 }
1344
1345 if (!alpha_flag) {
1346 alpha_flag = default_alpha_bits() > 0;
1347 }
1348
1349 /* we only support one size of stencil and accum buffers. */
1350 if (stencil_size > 0)
1351 stencil_size = STENCIL_BITS;
1352 if (accumRedSize > 0 || accumGreenSize > 0 || accumBlueSize > 0 ||
1353 accumAlphaSize > 0) {
1354 accumRedSize =
1355 accumGreenSize =
1356 accumBlueSize = default_accum_bits();
1357 accumAlphaSize = alpha_flag ? accumRedSize : 0;
1358 }
1359
1360 xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1361 stereo_flag, depth_size, stencil_size,
1362 accumRedSize, accumGreenSize,
1363 accumBlueSize, accumAlphaSize, level, numAux );
1364 }
1365
1366 return xmvis;
1367 }
1368
1369
1370 static XVisualInfo *
1371 Fake_glXChooseVisual( Display *dpy, int screen, int *list )
1372 {
1373 XMesaVisual xmvis;
1374
1375 /* register ourselves as an extension on this display */
1376 register_with_display(dpy);
1377
1378 xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1379 if (xmvis) {
1380 #if 0
1381 return xmvis->vishandle;
1382 #else
1383 /* create a new vishandle - the cached one may be stale */
1384 xmvis->vishandle = (XVisualInfo *) _mesa_malloc(sizeof(XVisualInfo));
1385 if (xmvis->vishandle) {
1386 _mesa_memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1387 }
1388 return xmvis->vishandle;
1389 #endif
1390 }
1391 else
1392 return NULL;
1393 }
1394
1395
1396 /**
1397 * Init basic fields of a new fake_glx_context.
1398 */
1399 static void
1400 init_glx_context(struct fake_glx_context *glxCtx, Display *dpy)
1401 {
1402 /* Always return True. See if anyone's confused... */
1403 GLboolean direct = GL_TRUE;
1404
1405 glxCtx->xmesaContext->direct = direct;
1406 glxCtx->glxContext.isDirect = direct;
1407 glxCtx->glxContext.currentDpy = dpy;
1408 glxCtx->glxContext.xid = (XID) glxCtx; /* self pointer */
1409
1410 assert((void *) glxCtx == (void *) &(glxCtx->glxContext));
1411 }
1412
1413
1414
1415 static GLXContext
1416 Fake_glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1417 GLXContext share_list, Bool direct )
1418 {
1419 XMesaVisual xmvis;
1420 struct fake_glx_context *glxCtx;
1421 struct fake_glx_context *shareCtx = (struct fake_glx_context *) share_list;
1422
1423 if (!dpy || !visinfo)
1424 return 0;
1425
1426 glxCtx = CALLOC_STRUCT(fake_glx_context);
1427 if (!glxCtx)
1428 return 0;
1429
1430 /* deallocate unused windows/buffers */
1431 #if 0
1432 XMesaGarbageCollect();
1433 #endif
1434
1435 xmvis = find_glx_visual( dpy, visinfo );
1436 if (!xmvis) {
1437 /* This visual wasn't found with glXChooseVisual() */
1438 xmvis = create_glx_visual( dpy, visinfo );
1439 if (!xmvis) {
1440 /* unusable visual */
1441 _mesa_free(glxCtx);
1442 return NULL;
1443 }
1444 }
1445
1446 glxCtx->xmesaContext = XMesaCreateContext(xmvis,
1447 shareCtx ? shareCtx->xmesaContext : NULL);
1448 if (!glxCtx->xmesaContext) {
1449 _mesa_free(glxCtx);
1450 return NULL;
1451 }
1452
1453 init_glx_context(glxCtx, dpy);
1454
1455 return (GLXContext) glxCtx;
1456 }
1457
1458
1459 /* XXX these may have to be removed due to thread-safety issues. */
1460 static GLXContext MakeCurrent_PrevContext = 0;
1461 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1462 static GLXDrawable MakeCurrent_PrevReadable = 0;
1463 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1464 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1465
1466
1467 /* GLX 1.3 and later */
1468 static Bool
1469 Fake_glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1470 GLXDrawable read, GLXContext ctx )
1471 {
1472 struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1473
1474 if (ctx && draw && read) {
1475 XMesaBuffer drawBuffer, readBuffer;
1476 XMesaContext xmctx = glxCtx->xmesaContext;
1477
1478 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1479 if (ctx == MakeCurrent_PrevContext
1480 && draw == MakeCurrent_PrevDrawable) {
1481 drawBuffer = MakeCurrent_PrevDrawBuffer;
1482 }
1483 else {
1484 drawBuffer = XMesaFindBuffer( dpy, draw );
1485 }
1486 if (!drawBuffer) {
1487 /* drawable must be a new window! */
1488 drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1489 if (!drawBuffer) {
1490 /* Out of memory, or context/drawable depth mismatch */
1491 return False;
1492 }
1493 #ifdef FX
1494 FXcreateContext( xmctx->xm_visual, draw, xmctx, drawBuffer );
1495 #endif
1496 }
1497
1498 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1499 if (ctx == MakeCurrent_PrevContext
1500 && read == MakeCurrent_PrevReadable) {
1501 readBuffer = MakeCurrent_PrevReadBuffer;
1502 }
1503 else {
1504 readBuffer = XMesaFindBuffer( dpy, read );
1505 }
1506 if (!readBuffer) {
1507 /* drawable must be a new window! */
1508 readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1509 if (!readBuffer) {
1510 /* Out of memory, or context/drawable depth mismatch */
1511 return False;
1512 }
1513 #ifdef FX
1514 FXcreateContext( xmctx->xm_visual, read, xmctx, readBuffer );
1515 #endif
1516 }
1517
1518 MakeCurrent_PrevContext = ctx;
1519 MakeCurrent_PrevDrawable = draw;
1520 MakeCurrent_PrevReadable = read;
1521 MakeCurrent_PrevDrawBuffer = drawBuffer;
1522 MakeCurrent_PrevReadBuffer = readBuffer;
1523
1524 /* Now make current! */
1525 if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1526 ((__GLXcontext *) ctx)->currentDpy = dpy;
1527 ((__GLXcontext *) ctx)->currentDrawable = draw;
1528 ((__GLXcontext *) ctx)->currentReadable = read;
1529 return True;
1530 }
1531 else {
1532 return False;
1533 }
1534 }
1535 else if (!ctx && !draw && !read) {
1536 /* release current context w/out assigning new one. */
1537 XMesaMakeCurrent( NULL, NULL );
1538 MakeCurrent_PrevContext = 0;
1539 MakeCurrent_PrevDrawable = 0;
1540 MakeCurrent_PrevReadable = 0;
1541 MakeCurrent_PrevDrawBuffer = 0;
1542 MakeCurrent_PrevReadBuffer = 0;
1543 return True;
1544 }
1545 else {
1546 /* The args must either all be non-zero or all zero.
1547 * This is an error.
1548 */
1549 return False;
1550 }
1551 }
1552
1553
1554 static Bool
1555 Fake_glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1556 {
1557 return Fake_glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1558 }
1559
1560
1561 static GLXPixmap
1562 Fake_glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1563 {
1564 XMesaVisual v;
1565 XMesaBuffer b;
1566
1567 v = find_glx_visual( dpy, visinfo );
1568 if (!v) {
1569 v = create_glx_visual( dpy, visinfo );
1570 if (!v) {
1571 /* unusable visual */
1572 return 0;
1573 }
1574 }
1575
1576 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1577 if (!b) {
1578 return 0;
1579 }
1580 return b->frontxrb->pixmap;
1581 }
1582
1583
1584 /*** GLX_MESA_pixmap_colormap ***/
1585
1586 static GLXPixmap
1587 Fake_glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1588 Pixmap pixmap, Colormap cmap )
1589 {
1590 XMesaVisual v;
1591 XMesaBuffer b;
1592
1593 v = find_glx_visual( dpy, visinfo );
1594 if (!v) {
1595 v = create_glx_visual( dpy, visinfo );
1596 if (!v) {
1597 /* unusable visual */
1598 return 0;
1599 }
1600 }
1601
1602 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1603 if (!b) {
1604 return 0;
1605 }
1606 return b->frontxrb->pixmap;
1607 }
1608
1609
1610 static void
1611 Fake_glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1612 {
1613 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1614 if (b) {
1615 XMesaDestroyBuffer(b);
1616 }
1617 else if (_mesa_getenv("MESA_DEBUG")) {
1618 _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1619 }
1620 }
1621
1622
1623 static void
1624 Fake_glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1625 unsigned long mask )
1626 {
1627 struct fake_glx_context *fakeSrc = (struct fake_glx_context *) src;
1628 struct fake_glx_context *fakeDst = (struct fake_glx_context *) dst;
1629 XMesaContext xm_src = fakeSrc->xmesaContext;
1630 XMesaContext xm_dst = fakeDst->xmesaContext;
1631 (void) dpy;
1632 if (MakeCurrent_PrevContext == src) {
1633 _mesa_Flush();
1634 }
1635 _mesa_copy_context( &(xm_src->mesa), &(xm_dst->mesa), (GLuint) mask );
1636 }
1637
1638
1639 static Bool
1640 Fake_glXQueryExtension( Display *dpy, int *errorBase, int *eventBase )
1641 {
1642 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1643 (void) dpy;
1644 if (errorBase)
1645 *errorBase = 0;
1646 if (eventBase)
1647 *eventBase = 0;
1648 return True;
1649 }
1650
1651
1652 extern void _kw_ungrab_all( Display *dpy );
1653 void _kw_ungrab_all( Display *dpy )
1654 {
1655 XUngrabPointer( dpy, CurrentTime );
1656 XUngrabKeyboard( dpy, CurrentTime );
1657 }
1658
1659
1660 static void
1661 Fake_glXDestroyContext( Display *dpy, GLXContext ctx )
1662 {
1663 struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1664 (void) dpy;
1665 MakeCurrent_PrevContext = 0;
1666 MakeCurrent_PrevDrawable = 0;
1667 MakeCurrent_PrevReadable = 0;
1668 MakeCurrent_PrevDrawBuffer = 0;
1669 MakeCurrent_PrevReadBuffer = 0;
1670 XMesaDestroyContext( glxCtx->xmesaContext );
1671 XMesaGarbageCollect();
1672 _mesa_free(glxCtx);
1673 }
1674
1675
1676 static Bool
1677 Fake_glXIsDirect( Display *dpy, GLXContext ctx )
1678 {
1679 struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1680 (void) dpy;
1681 return glxCtx->xmesaContext->direct;
1682 }
1683
1684
1685
1686 static void
1687 Fake_glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1688 {
1689 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1690
1691 if (buffer) {
1692 XMesaSwapBuffers(buffer);
1693 }
1694 else if (_mesa_getenv("MESA_DEBUG")) {
1695 _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1696 (int) drawable);
1697 }
1698 }
1699
1700
1701
1702 /*** GLX_MESA_copy_sub_buffer ***/
1703
1704 static void
1705 Fake_glXCopySubBufferMESA( Display *dpy, GLXDrawable drawable,
1706 int x, int y, int width, int height )
1707 {
1708 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1709 if (buffer) {
1710 XMesaCopySubBuffer(buffer, x, y, width, height);
1711 }
1712 else if (_mesa_getenv("MESA_DEBUG")) {
1713 _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1714 }
1715 }
1716
1717
1718 static Bool
1719 Fake_glXQueryVersion( Display *dpy, int *maj, int *min )
1720 {
1721 (void) dpy;
1722 /* Return GLX version, not Mesa version */
1723 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1724 *maj = CLIENT_MAJOR_VERSION;
1725 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1726 return True;
1727 }
1728
1729
1730 /*
1731 * Query the GLX attributes of the given XVisualInfo.
1732 */
1733 static int
1734 get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1735 {
1736 ASSERT(xmvis);
1737 switch(attrib) {
1738 case GLX_USE_GL:
1739 if (fbconfig)
1740 return GLX_BAD_ATTRIBUTE;
1741 *value = (int) True;
1742 return 0;
1743 case GLX_BUFFER_SIZE:
1744 *value = xmvis->visinfo->depth;
1745 return 0;
1746 case GLX_LEVEL:
1747 *value = xmvis->mesa_visual.level;
1748 return 0;
1749 case GLX_RGBA:
1750 if (fbconfig)
1751 return GLX_BAD_ATTRIBUTE;
1752 if (xmvis->mesa_visual.rgbMode) {
1753 *value = True;
1754 }
1755 else {
1756 *value = False;
1757 }
1758 return 0;
1759 case GLX_DOUBLEBUFFER:
1760 *value = (int) xmvis->mesa_visual.doubleBufferMode;
1761 return 0;
1762 case GLX_STEREO:
1763 *value = (int) xmvis->mesa_visual.stereoMode;
1764 return 0;
1765 case GLX_AUX_BUFFERS:
1766 *value = xmvis->mesa_visual.numAuxBuffers;
1767 return 0;
1768 case GLX_RED_SIZE:
1769 *value = xmvis->mesa_visual.redBits;
1770 return 0;
1771 case GLX_GREEN_SIZE:
1772 *value = xmvis->mesa_visual.greenBits;
1773 return 0;
1774 case GLX_BLUE_SIZE:
1775 *value = xmvis->mesa_visual.blueBits;
1776 return 0;
1777 case GLX_ALPHA_SIZE:
1778 *value = xmvis->mesa_visual.alphaBits;
1779 return 0;
1780 case GLX_DEPTH_SIZE:
1781 *value = xmvis->mesa_visual.depthBits;
1782 return 0;
1783 case GLX_STENCIL_SIZE:
1784 *value = xmvis->mesa_visual.stencilBits;
1785 return 0;
1786 case GLX_ACCUM_RED_SIZE:
1787 *value = xmvis->mesa_visual.accumRedBits;
1788 return 0;
1789 case GLX_ACCUM_GREEN_SIZE:
1790 *value = xmvis->mesa_visual.accumGreenBits;
1791 return 0;
1792 case GLX_ACCUM_BLUE_SIZE:
1793 *value = xmvis->mesa_visual.accumBlueBits;
1794 return 0;
1795 case GLX_ACCUM_ALPHA_SIZE:
1796 *value = xmvis->mesa_visual.accumAlphaBits;
1797 return 0;
1798
1799 /*
1800 * GLX_EXT_visual_info extension
1801 */
1802 case GLX_X_VISUAL_TYPE_EXT:
1803 switch (xmvis->visinfo->CLASS) {
1804 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1805 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1806 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1807 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1808 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1809 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1810 }
1811 return 0;
1812 case GLX_TRANSPARENT_TYPE_EXT:
1813 if (xmvis->mesa_visual.level==0) {
1814 /* normal planes */
1815 *value = GLX_NONE_EXT;
1816 }
1817 else if (xmvis->mesa_visual.level>0) {
1818 /* overlay */
1819 if (xmvis->mesa_visual.rgbMode) {
1820 *value = GLX_TRANSPARENT_RGB_EXT;
1821 }
1822 else {
1823 *value = GLX_TRANSPARENT_INDEX_EXT;
1824 }
1825 }
1826 else if (xmvis->mesa_visual.level<0) {
1827 /* underlay */
1828 *value = GLX_NONE_EXT;
1829 }
1830 return 0;
1831 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1832 {
1833 int pixel = transparent_pixel( xmvis );
1834 if (pixel>=0) {
1835 *value = pixel;
1836 }
1837 /* else undefined */
1838 }
1839 return 0;
1840 case GLX_TRANSPARENT_RED_VALUE_EXT:
1841 /* undefined */
1842 return 0;
1843 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1844 /* undefined */
1845 return 0;
1846 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1847 /* undefined */
1848 return 0;
1849 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1850 /* undefined */
1851 return 0;
1852
1853 /*
1854 * GLX_EXT_visual_info extension
1855 */
1856 case GLX_VISUAL_CAVEAT_EXT:
1857 /* test for zero, just in case */
1858 if (xmvis->mesa_visual.visualRating > 0)
1859 *value = xmvis->mesa_visual.visualRating;
1860 else
1861 *value = GLX_NONE_EXT;
1862 return 0;
1863
1864 /*
1865 * GLX_ARB_multisample
1866 */
1867 case GLX_SAMPLE_BUFFERS_ARB:
1868 *value = 0;
1869 return 0;
1870 case GLX_SAMPLES_ARB:
1871 *value = 0;
1872 return 0;
1873
1874 /*
1875 * For FBConfigs:
1876 */
1877 case GLX_SCREEN_EXT:
1878 if (!fbconfig)
1879 return GLX_BAD_ATTRIBUTE;
1880 *value = xmvis->visinfo->screen;
1881 break;
1882 case GLX_DRAWABLE_TYPE: /*SGIX too */
1883 if (!fbconfig)
1884 return GLX_BAD_ATTRIBUTE;
1885 *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1886 break;
1887 case GLX_RENDER_TYPE_SGIX:
1888 if (!fbconfig)
1889 return GLX_BAD_ATTRIBUTE;
1890 if (xmvis->mesa_visual.rgbMode)
1891 *value = GLX_RGBA_BIT;
1892 else
1893 *value = GLX_COLOR_INDEX_BIT;
1894 break;
1895 case GLX_X_RENDERABLE_SGIX:
1896 if (!fbconfig)
1897 return GLX_BAD_ATTRIBUTE;
1898 *value = True; /* XXX really? */
1899 break;
1900 case GLX_FBCONFIG_ID_SGIX:
1901 if (!fbconfig)
1902 return GLX_BAD_ATTRIBUTE;
1903 *value = xmvis->visinfo->visualid;
1904 break;
1905 case GLX_MAX_PBUFFER_WIDTH:
1906 if (!fbconfig)
1907 return GLX_BAD_ATTRIBUTE;
1908 /* XXX or MAX_WIDTH? */
1909 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1910 break;
1911 case GLX_MAX_PBUFFER_HEIGHT:
1912 if (!fbconfig)
1913 return GLX_BAD_ATTRIBUTE;
1914 *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1915 break;
1916 case GLX_MAX_PBUFFER_PIXELS:
1917 if (!fbconfig)
1918 return GLX_BAD_ATTRIBUTE;
1919 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1920 DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1921 break;
1922 case GLX_VISUAL_ID:
1923 if (!fbconfig)
1924 return GLX_BAD_ATTRIBUTE;
1925 *value = xmvis->visinfo->visualid;
1926 break;
1927
1928 #ifdef GLX_EXT_texture_from_pixmap
1929 case GLX_BIND_TO_TEXTURE_RGB_EXT:
1930 *value = True; /*XXX*/
1931 break;
1932 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1933 /* XXX review */
1934 *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1935 break;
1936 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1937 *value = True; /*XXX*/
1938 break;
1939 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1940 *value = (GLX_TEXTURE_1D_BIT_EXT |
1941 GLX_TEXTURE_2D_BIT_EXT |
1942 GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1943 break;
1944 case GLX_Y_INVERTED_EXT:
1945 *value = True; /*XXX*/
1946 break;
1947 #endif
1948
1949 default:
1950 return GLX_BAD_ATTRIBUTE;
1951 }
1952 return Success;
1953 }
1954
1955
1956 static int
1957 Fake_glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1958 int attrib, int *value )
1959 {
1960 XMesaVisual xmvis;
1961 int k;
1962 if (!dpy || !visinfo)
1963 return GLX_BAD_ATTRIBUTE;
1964
1965 xmvis = find_glx_visual( dpy, visinfo );
1966 if (!xmvis) {
1967 /* this visual wasn't obtained with glXChooseVisual */
1968 xmvis = create_glx_visual( dpy, visinfo );
1969 if (!xmvis) {
1970 /* this visual can't be used for GL rendering */
1971 if (attrib==GLX_USE_GL) {
1972 *value = (int) False;
1973 return 0;
1974 }
1975 else {
1976 return GLX_BAD_VISUAL;
1977 }
1978 }
1979 }
1980
1981 k = get_config(xmvis, attrib, value, GL_FALSE);
1982 return k;
1983 }
1984
1985
1986 static void
1987 Fake_glXWaitGL( void )
1988 {
1989 XMesaContext xmesa = XMesaGetCurrentContext();
1990 XMesaFlush( xmesa );
1991 }
1992
1993
1994
1995 static void
1996 Fake_glXWaitX( void )
1997 {
1998 XMesaContext xmesa = XMesaGetCurrentContext();
1999 XMesaFlush( xmesa );
2000 }
2001
2002
2003 static const char *
2004 get_extensions( void )
2005 {
2006 #ifdef FX
2007 const char *fx = _mesa_getenv("MESA_GLX_FX");
2008 if (fx && fx[0] != 'd') {
2009 return EXTENSIONS;
2010 }
2011 #endif
2012 return EXTENSIONS + 23; /* skip "GLX_MESA_set_3dfx_mode" */
2013 }
2014
2015
2016
2017 /* GLX 1.1 and later */
2018 static const char *
2019 Fake_glXQueryExtensionsString( Display *dpy, int screen )
2020 {
2021 (void) dpy;
2022 (void) screen;
2023 return get_extensions();
2024 }
2025
2026
2027
2028 /* GLX 1.1 and later */
2029 static const char *
2030 Fake_glXQueryServerString( Display *dpy, int screen, int name )
2031 {
2032 static char version[1000];
2033 _mesa_sprintf(version, "%d.%d %s",
2034 SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, MESA_GLX_VERSION);
2035
2036 (void) dpy;
2037 (void) screen;
2038
2039 switch (name) {
2040 case GLX_EXTENSIONS:
2041 return get_extensions();
2042 case GLX_VENDOR:
2043 return VENDOR;
2044 case GLX_VERSION:
2045 return version;
2046 default:
2047 return NULL;
2048 }
2049 }
2050
2051
2052
2053 /* GLX 1.1 and later */
2054 static const char *
2055 Fake_glXGetClientString( Display *dpy, int name )
2056 {
2057 static char version[1000];
2058 _mesa_sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
2059 CLIENT_MINOR_VERSION, MESA_GLX_VERSION);
2060
2061 (void) dpy;
2062
2063 switch (name) {
2064 case GLX_EXTENSIONS:
2065 return get_extensions();
2066 case GLX_VENDOR:
2067 return VENDOR;
2068 case GLX_VERSION:
2069 return version;
2070 default:
2071 return NULL;
2072 }
2073 }
2074
2075
2076
2077 /*
2078 * GLX 1.3 and later
2079 */
2080
2081
2082 static int
2083 Fake_glXGetFBConfigAttrib( Display *dpy, GLXFBConfig config,
2084 int attribute, int *value )
2085 {
2086 XMesaVisual v = (XMesaVisual) config;
2087 (void) dpy;
2088 (void) config;
2089
2090 if (!dpy || !config || !value)
2091 return -1;
2092
2093 return get_config(v, attribute, value, GL_TRUE);
2094 }
2095
2096
2097 static GLXFBConfig *
2098 Fake_glXGetFBConfigs( Display *dpy, int screen, int *nelements )
2099 {
2100 XVisualInfo *visuals, visTemplate;
2101 const long visMask = VisualScreenMask;
2102 int i;
2103
2104 /* Get list of all X visuals */
2105 visTemplate.screen = screen;
2106 visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
2107 if (*nelements > 0) {
2108 XMesaVisual *results;
2109 results = (XMesaVisual *) _mesa_malloc(*nelements * sizeof(XMesaVisual));
2110 if (!results) {
2111 *nelements = 0;
2112 return NULL;
2113 }
2114 for (i = 0; i < *nelements; i++) {
2115 results[i] = create_glx_visual(dpy, visuals + i);
2116 }
2117 return (GLXFBConfig *) results;
2118 }
2119 return NULL;
2120 }
2121
2122
2123 static GLXFBConfig *
2124 Fake_glXChooseFBConfig( Display *dpy, int screen,
2125 const int *attribList, int *nitems )
2126 {
2127 XMesaVisual xmvis;
2128
2129 if (!attribList || !attribList[0]) {
2130 /* return list of all configs (per GLX_SGIX_fbconfig spec) */
2131 return Fake_glXGetFBConfigs(dpy, screen, nitems);
2132 }
2133
2134 xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
2135 if (xmvis) {
2136 GLXFBConfig *config = (GLXFBConfig *) _mesa_malloc(sizeof(XMesaVisual));
2137 if (!config) {
2138 *nitems = 0;
2139 return NULL;
2140 }
2141 *nitems = 1;
2142 config[0] = (GLXFBConfig) xmvis;
2143 return (GLXFBConfig *) config;
2144 }
2145 else {
2146 *nitems = 0;
2147 return NULL;
2148 }
2149 }
2150
2151
2152 static XVisualInfo *
2153 Fake_glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
2154 {
2155 if (dpy && config) {
2156 XMesaVisual xmvis = (XMesaVisual) config;
2157 #if 0
2158 return xmvis->vishandle;
2159 #else
2160 /* create a new vishandle - the cached one may be stale */
2161 xmvis->vishandle = (XVisualInfo *) _mesa_malloc(sizeof(XVisualInfo));
2162 if (xmvis->vishandle) {
2163 _mesa_memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
2164 }
2165 return xmvis->vishandle;
2166 #endif
2167 }
2168 else {
2169 return NULL;
2170 }
2171 }
2172
2173
2174 static GLXWindow
2175 Fake_glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
2176 const int *attribList )
2177 {
2178 XMesaVisual xmvis = (XMesaVisual) config;
2179 XMesaBuffer xmbuf;
2180 if (!xmvis)
2181 return 0;
2182
2183 xmbuf = XMesaCreateWindowBuffer(xmvis, win);
2184 if (!xmbuf)
2185 return 0;
2186
2187 #ifdef FX
2188 /* XXX this will segfault if actually called */
2189 FXcreateContext(xmvis, win, NULL, xmbuf);
2190 #endif
2191
2192 (void) dpy;
2193 (void) attribList; /* Ignored in GLX 1.3 */
2194
2195 return win; /* A hack for now */
2196 }
2197
2198
2199 static void
2200 Fake_glXDestroyWindow( Display *dpy, GLXWindow window )
2201 {
2202 XMesaBuffer b = XMesaFindBuffer(dpy, (XMesaDrawable) window);
2203 if (b)
2204 XMesaDestroyBuffer(b);
2205 /* don't destroy X window */
2206 }
2207
2208
2209 /* XXX untested */
2210 static GLXPixmap
2211 Fake_glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
2212 const int *attribList )
2213 {
2214 XMesaVisual v = (XMesaVisual) config;
2215 XMesaBuffer b;
2216 const int *attr;
2217 int target = 0, format = 0, mipmap = 0;
2218 int value;
2219
2220 if (!dpy || !config || !pixmap)
2221 return 0;
2222
2223 for (attr = attribList; attr && *attr; attr++) {
2224 switch (*attr) {
2225 case GLX_TEXTURE_FORMAT_EXT:
2226 attr++;
2227 switch (*attr) {
2228 case GLX_TEXTURE_FORMAT_NONE_EXT:
2229 case GLX_TEXTURE_FORMAT_RGB_EXT:
2230 case GLX_TEXTURE_FORMAT_RGBA_EXT:
2231 format = *attr;
2232 break;
2233 default:
2234 /* error */
2235 return 0;
2236 }
2237 break;
2238 case GLX_TEXTURE_TARGET_EXT:
2239 attr++;
2240 switch (*attr) {
2241 case GLX_TEXTURE_1D_EXT:
2242 case GLX_TEXTURE_2D_EXT:
2243 case GLX_TEXTURE_RECTANGLE_EXT:
2244 target = *attr;
2245 break;
2246 default:
2247 /* error */
2248 return 0;
2249 }
2250 break;
2251 case GLX_MIPMAP_TEXTURE_EXT:
2252 attr++;
2253 if (*attr)
2254 mipmap = 1;
2255 break;
2256 default:
2257 /* error */
2258 return 0;
2259 }
2260 }
2261
2262 if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
2263 if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
2264 &value, GL_TRUE) != Success
2265 || !value) {
2266 return 0; /* error! */
2267 }
2268 }
2269 else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
2270 if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
2271 &value, GL_TRUE) != Success
2272 || !value) {
2273 return 0; /* error! */
2274 }
2275 }
2276 if (mipmap) {
2277 if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
2278 &value, GL_TRUE) != Success
2279 || !value) {
2280 return 0; /* error! */
2281 }
2282 }
2283 if (target == GLX_TEXTURE_1D_EXT) {
2284 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2285 &value, GL_TRUE) != Success
2286 || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
2287 return 0; /* error! */
2288 }
2289 }
2290 else if (target == GLX_TEXTURE_2D_EXT) {
2291 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2292 &value, GL_TRUE) != Success
2293 || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
2294 return 0; /* error! */
2295 }
2296 }
2297 if (target == GLX_TEXTURE_RECTANGLE_EXT) {
2298 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2299 &value, GL_TRUE) != Success
2300 || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
2301 return 0; /* error! */
2302 }
2303 }
2304
2305 if (format || target || mipmap) {
2306 /* texture from pixmap */
2307 b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
2308 }
2309 else {
2310 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
2311 }
2312 if (!b) {
2313 return 0;
2314 }
2315
2316 return pixmap;
2317 }
2318
2319
2320 static void
2321 Fake_glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
2322 {
2323 XMesaBuffer b = XMesaFindBuffer(dpy, (XMesaDrawable)pixmap);
2324 if (b)
2325 XMesaDestroyBuffer(b);
2326 /* don't destroy X pixmap */
2327 }
2328
2329
2330 static GLXPbuffer
2331 Fake_glXCreatePbuffer( Display *dpy, GLXFBConfig config,
2332 const int *attribList )
2333 {
2334 XMesaVisual xmvis = (XMesaVisual) config;
2335 XMesaBuffer xmbuf;
2336 const int *attrib;
2337 int width = 0, height = 0;
2338 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2339
2340 (void) dpy;
2341
2342 for (attrib = attribList; *attrib; attrib++) {
2343 switch (*attrib) {
2344 case GLX_PBUFFER_WIDTH:
2345 attrib++;
2346 width = *attrib;
2347 break;
2348 case GLX_PBUFFER_HEIGHT:
2349 attrib++;
2350 height = *attrib;
2351 break;
2352 case GLX_PRESERVED_CONTENTS:
2353 attrib++;
2354 preserveContents = *attrib; /* ignored */
2355 break;
2356 case GLX_LARGEST_PBUFFER:
2357 attrib++;
2358 useLargest = *attrib; /* ignored */
2359 break;
2360 default:
2361 return 0;
2362 }
2363 }
2364
2365 /* not used at this time */
2366 (void) useLargest;
2367 (void) preserveContents;
2368
2369 if (width == 0 || height == 0)
2370 return 0;
2371
2372 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2373 /* A GLXPbuffer handle must be an X Drawable because that's what
2374 * glXMakeCurrent takes.
2375 */
2376 if (xmbuf)
2377 return (GLXPbuffer) xmbuf->frontxrb->pixmap;
2378 else
2379 return 0;
2380 }
2381
2382
2383 static void
2384 Fake_glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2385 {
2386 XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2387 if (b) {
2388 XMesaDestroyBuffer(b);
2389 }
2390 }
2391
2392
2393 static void
2394 Fake_glXQueryDrawable( Display *dpy, GLXDrawable draw, int attribute,
2395 unsigned int *value )
2396 {
2397 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2398 if (!xmbuf)
2399 return;
2400
2401 switch (attribute) {
2402 case GLX_WIDTH:
2403 *value = xmbuf->mesa_buffer.Width;
2404 break;
2405 case GLX_HEIGHT:
2406 *value = xmbuf->mesa_buffer.Height;
2407 break;
2408 case GLX_PRESERVED_CONTENTS:
2409 *value = True;
2410 break;
2411 case GLX_LARGEST_PBUFFER:
2412 *value = xmbuf->mesa_buffer.Width * xmbuf->mesa_buffer.Height;
2413 break;
2414 case GLX_FBCONFIG_ID:
2415 *value = xmbuf->xm_visual->visinfo->visualid;
2416 return;
2417 #ifdef GLX_EXT_texture_from_pixmap
2418 case GLX_TEXTURE_FORMAT_EXT:
2419 *value = xmbuf->TextureFormat;
2420 break;
2421 case GLX_TEXTURE_TARGET_EXT:
2422 *value = xmbuf->TextureTarget;
2423 break;
2424 case GLX_MIPMAP_TEXTURE_EXT:
2425 *value = xmbuf->TextureMipmap;
2426 break;
2427 #endif
2428
2429 default:
2430 return; /* raise BadValue error */
2431 }
2432 }
2433
2434
2435 static GLXContext
2436 Fake_glXCreateNewContext( Display *dpy, GLXFBConfig config,
2437 int renderType, GLXContext shareList, Bool direct )
2438 {
2439 struct fake_glx_context *glxCtx;
2440 struct fake_glx_context *shareCtx = (struct fake_glx_context *) shareList;
2441 XMesaVisual xmvis = (XMesaVisual) config;
2442
2443 if (!dpy || !config ||
2444 (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2445 return 0;
2446
2447 glxCtx = CALLOC_STRUCT(fake_glx_context);
2448 if (!glxCtx)
2449 return 0;
2450
2451 /* deallocate unused windows/buffers */
2452 XMesaGarbageCollect();
2453
2454 glxCtx->xmesaContext = XMesaCreateContext(xmvis,
2455 shareCtx ? shareCtx->xmesaContext : NULL);
2456 if (!glxCtx->xmesaContext) {
2457 _mesa_free(glxCtx);
2458 return NULL;
2459 }
2460
2461 init_glx_context(glxCtx, dpy);
2462
2463 return (GLXContext) glxCtx;
2464 }
2465
2466
2467 static int
2468 Fake_glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
2469 {
2470 struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
2471 XMesaContext xmctx = glxCtx->xmesaContext;
2472
2473 (void) dpy;
2474 (void) ctx;
2475
2476 switch (attribute) {
2477 case GLX_FBCONFIG_ID:
2478 *value = xmctx->xm_visual->visinfo->visualid;
2479 break;
2480 case GLX_RENDER_TYPE:
2481 if (xmctx->xm_visual->mesa_visual.rgbMode)
2482 *value = GLX_RGBA_TYPE;
2483 else
2484 *value = GLX_COLOR_INDEX_TYPE;
2485 break;
2486 case GLX_SCREEN:
2487 *value = 0;
2488 return Success;
2489 default:
2490 return GLX_BAD_ATTRIBUTE;
2491 }
2492 return 0;
2493 }
2494
2495
2496 static void
2497 Fake_glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
2498 {
2499 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2500 if (xmbuf)
2501 xmbuf->selectedEvents = mask;
2502 }
2503
2504
2505 static void
2506 Fake_glXGetSelectedEvent( Display *dpy, GLXDrawable drawable,
2507 unsigned long *mask )
2508 {
2509 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2510 if (xmbuf)
2511 *mask = xmbuf->selectedEvents;
2512 else
2513 *mask = 0;
2514 }
2515
2516
2517
2518 /*** GLX_SGI_swap_control ***/
2519
2520 static int
2521 Fake_glXSwapIntervalSGI(int interval)
2522 {
2523 (void) interval;
2524 return 0;
2525 }
2526
2527
2528
2529 /*** GLX_SGI_video_sync ***/
2530
2531 static unsigned int FrameCounter = 0;
2532
2533 static int
2534 Fake_glXGetVideoSyncSGI(unsigned int *count)
2535 {
2536 /* this is a bogus implementation */
2537 *count = FrameCounter++;
2538 return 0;
2539 }
2540
2541 static int
2542 Fake_glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2543 {
2544 if (divisor <= 0 || remainder < 0)
2545 return GLX_BAD_VALUE;
2546 /* this is a bogus implementation */
2547 FrameCounter++;
2548 while (FrameCounter % divisor != remainder)
2549 FrameCounter++;
2550 *count = FrameCounter;
2551 return 0;
2552 }
2553
2554
2555
2556 /*** GLX_SGI_make_current_read ***/
2557
2558 static Bool
2559 Fake_glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx)
2560 {
2561 return Fake_glXMakeContextCurrent( dpy, draw, read, ctx );
2562 }
2563
2564 /* not used
2565 static GLXDrawable
2566 Fake_glXGetCurrentReadDrawableSGI(void)
2567 {
2568 return 0;
2569 }
2570 */
2571
2572
2573 /*** GLX_SGIX_video_source ***/
2574 #if defined(_VL_H)
2575
2576 static GLXVideoSourceSGIX
2577 Fake_glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server, VLPath path, int nodeClass, VLNode drainNode)
2578 {
2579 (void) dpy;
2580 (void) screen;
2581 (void) server;
2582 (void) path;
2583 (void) nodeClass;
2584 (void) drainNode;
2585 return 0;
2586 }
2587
2588 static void
2589 Fake_glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
2590 {
2591 (void) dpy;
2592 (void) src;
2593 }
2594
2595 #endif
2596
2597
2598 /*** GLX_EXT_import_context ***/
2599
2600 static void
2601 Fake_glXFreeContextEXT(Display *dpy, GLXContext context)
2602 {
2603 (void) dpy;
2604 (void) context;
2605 }
2606
2607 static GLXContextID
2608 Fake_glXGetContextIDEXT(const GLXContext context)
2609 {
2610 (void) context;
2611 return 0;
2612 }
2613
2614 static GLXContext
2615 Fake_glXImportContextEXT(Display *dpy, GLXContextID contextID)
2616 {
2617 (void) dpy;
2618 (void) contextID;
2619 return 0;
2620 }
2621
2622 static int
2623 Fake_glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute, int *value)
2624 {
2625 (void) dpy;
2626 (void) context;
2627 (void) attribute;
2628 (void) value;
2629 return 0;
2630 }
2631
2632
2633
2634 /*** GLX_SGIX_fbconfig ***/
2635
2636 static int
2637 Fake_glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config, int attribute, int *value)
2638 {
2639 return Fake_glXGetFBConfigAttrib(dpy, config, attribute, value);
2640 }
2641
2642 static GLXFBConfigSGIX *
2643 Fake_glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list, int *nelements)
2644 {
2645 return (GLXFBConfig *) Fake_glXChooseFBConfig(dpy, screen, attrib_list, nelements);
2646 }
2647
2648
2649 static GLXPixmap
2650 Fake_glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, Pixmap pixmap)
2651 {
2652 XMesaVisual xmvis = (XMesaVisual) config;
2653 XMesaBuffer xmbuf = XMesaCreatePixmapBuffer(xmvis, pixmap, 0);
2654 return xmbuf->frontxrb->pixmap; /* need to return an X ID */
2655 }
2656
2657
2658 static GLXContext
2659 Fake_glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, int render_type, GLXContext share_list, Bool direct)
2660 {
2661 XMesaVisual xmvis = (XMesaVisual) config;
2662 struct fake_glx_context *glxCtx;
2663 struct fake_glx_context *shareCtx = (struct fake_glx_context *) share_list;
2664
2665 glxCtx = CALLOC_STRUCT(fake_glx_context);
2666 if (!glxCtx)
2667 return 0;
2668
2669 /* deallocate unused windows/buffers */
2670 XMesaGarbageCollect();
2671
2672 glxCtx->xmesaContext = XMesaCreateContext(xmvis,
2673 shareCtx ? shareCtx->xmesaContext : NULL);
2674 if (!glxCtx->xmesaContext) {
2675 _mesa_free(glxCtx);
2676 return NULL;
2677 }
2678
2679 init_glx_context(glxCtx, dpy);
2680
2681 return (GLXContext) glxCtx;
2682 }
2683
2684
2685 static XVisualInfo *
2686 Fake_glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
2687 {
2688 return Fake_glXGetVisualFromFBConfig(dpy, config);
2689 }
2690
2691
2692 static GLXFBConfigSGIX
2693 Fake_glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
2694 {
2695 XMesaVisual xmvis = find_glx_visual(dpy, vis);
2696 if (!xmvis) {
2697 /* This visual wasn't found with glXChooseVisual() */
2698 xmvis = create_glx_visual(dpy, vis);
2699 }
2700
2701 return (GLXFBConfigSGIX) xmvis;
2702 }
2703
2704
2705
2706 /*** GLX_SGIX_pbuffer ***/
2707
2708 static GLXPbufferSGIX
2709 Fake_glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
2710 unsigned int width, unsigned int height,
2711 int *attribList)
2712 {
2713 XMesaVisual xmvis = (XMesaVisual) config;
2714 XMesaBuffer xmbuf;
2715 const int *attrib;
2716 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2717
2718 (void) dpy;
2719
2720 for (attrib = attribList; attrib && *attrib; attrib++) {
2721 switch (*attrib) {
2722 case GLX_PRESERVED_CONTENTS_SGIX:
2723 attrib++;
2724 preserveContents = *attrib; /* ignored */
2725 break;
2726 case GLX_LARGEST_PBUFFER_SGIX:
2727 attrib++;
2728 useLargest = *attrib; /* ignored */
2729 break;
2730 default:
2731 return 0;
2732 }
2733 }
2734
2735 /* not used at this time */
2736 (void) useLargest;
2737 (void) preserveContents;
2738
2739 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2740 /* A GLXPbuffer handle must be an X Drawable because that's what
2741 * glXMakeCurrent takes.
2742 */
2743 return (GLXPbuffer) xmbuf->frontxrb->pixmap;
2744 }
2745
2746
2747 static void
2748 Fake_glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
2749 {
2750 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2751 if (xmbuf) {
2752 XMesaDestroyBuffer(xmbuf);
2753 }
2754 }
2755
2756
2757 static int
2758 Fake_glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute, unsigned int *value)
2759 {
2760 const XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2761
2762 if (!xmbuf) {
2763 /* Generate GLXBadPbufferSGIX for bad pbuffer */
2764 return 0;
2765 }
2766
2767 switch (attribute) {
2768 case GLX_PRESERVED_CONTENTS_SGIX:
2769 *value = True;
2770 break;
2771 case GLX_LARGEST_PBUFFER_SGIX:
2772 *value = xmbuf->mesa_buffer.Width * xmbuf->mesa_buffer.Height;
2773 break;
2774 case GLX_WIDTH_SGIX:
2775 *value = xmbuf->mesa_buffer.Width;
2776 break;
2777 case GLX_HEIGHT_SGIX:
2778 *value = xmbuf->mesa_buffer.Height;
2779 break;
2780 case GLX_EVENT_MASK_SGIX:
2781 *value = 0; /* XXX might be wrong */
2782 break;
2783 default:
2784 *value = 0;
2785 }
2786 return 0;
2787 }
2788
2789
2790 static void
2791 Fake_glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
2792 {
2793 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2794 if (xmbuf) {
2795 /* Note: we'll never generate clobber events */
2796 xmbuf->selectedEvents = mask;
2797 }
2798 }
2799
2800
2801 static void
2802 Fake_glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long *mask)
2803 {
2804 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2805 if (xmbuf) {
2806 *mask = xmbuf->selectedEvents;
2807 }
2808 else {
2809 *mask = 0;
2810 }
2811 }
2812
2813
2814
2815 /*** GLX_SGI_cushion ***/
2816
2817 static void
2818 Fake_glXCushionSGI(Display *dpy, Window win, float cushion)
2819 {
2820 (void) dpy;
2821 (void) win;
2822 (void) cushion;
2823 }
2824
2825
2826
2827 /*** GLX_SGIX_video_resize ***/
2828
2829 static int
2830 Fake_glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel , Window window)
2831 {
2832 (void) dpy;
2833 (void) screen;
2834 (void) channel;
2835 (void) window;
2836 return 0;
2837 }
2838
2839 static int
2840 Fake_glXChannelRectSGIX(Display *dpy, int screen, int channel, int x, int y, int w, int h)
2841 {
2842 (void) dpy;
2843 (void) screen;
2844 (void) channel;
2845 (void) x;
2846 (void) y;
2847 (void) w;
2848 (void) h;
2849 return 0;
2850 }
2851
2852 static int
2853 Fake_glXQueryChannelRectSGIX(Display *dpy, int screen, int channel, int *x, int *y, int *w, int *h)
2854 {
2855 (void) dpy;
2856 (void) screen;
2857 (void) channel;
2858 (void) x;
2859 (void) y;
2860 (void) w;
2861 (void) h;
2862 return 0;
2863 }
2864
2865 static int
2866 Fake_glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel, int *dx, int *dy, int *dw, int *dh)
2867 {
2868 (void) dpy;
2869 (void) screen;
2870 (void) channel;
2871 (void) dx;
2872 (void) dy;
2873 (void) dw;
2874 (void) dh;
2875 return 0;
2876 }
2877
2878 static int
2879 Fake_glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
2880 {
2881 (void) dpy;
2882 (void) screen;
2883 (void) channel;
2884 (void) synctype;
2885 return 0;
2886 }
2887
2888
2889
2890 /*** GLX_SGIX_dmbuffer **/
2891
2892 #if defined(_DM_BUFFER_H_)
2893 static Bool
2894 Fake_glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer, DMparams *params, DMbuffer dmbuffer)
2895 {
2896 (void) dpy;
2897 (void) pbuffer;
2898 (void) params;
2899 (void) dmbuffer;
2900 return False;
2901 }
2902 #endif
2903
2904
2905 /*** GLX_SGIX_swap_group ***/
2906
2907 static void
2908 Fake_glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
2909 {
2910 (void) dpy;
2911 (void) drawable;
2912 (void) member;
2913 }
2914
2915
2916
2917 /*** GLX_SGIX_swap_barrier ***/
2918
2919 static void
2920 Fake_glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
2921 {
2922 (void) dpy;
2923 (void) drawable;
2924 (void) barrier;
2925 }
2926
2927 static Bool
2928 Fake_glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2929 {
2930 (void) dpy;
2931 (void) screen;
2932 (void) max;
2933 return False;
2934 }
2935
2936
2937
2938 /*** GLX_SUN_get_transparent_index ***/
2939
2940 static Status
2941 Fake_glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay, long *pTransparent)
2942 {
2943 (void) dpy;
2944 (void) overlay;
2945 (void) underlay;
2946 (void) pTransparent;
2947 return 0;
2948 }
2949
2950
2951
2952 /*** GLX_MESA_release_buffers ***/
2953
2954 /*
2955 * Release the depth, stencil, accum buffers attached to a GLXDrawable
2956 * (a window or pixmap) prior to destroying the GLXDrawable.
2957 */
2958 static Bool
2959 Fake_glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2960 {
2961 XMesaBuffer b = XMesaFindBuffer(dpy, d);
2962 if (b) {
2963 XMesaDestroyBuffer(b);
2964 return True;
2965 }
2966 return False;
2967 }
2968
2969
2970
2971 /*** GLX_MESA_set_3dfx_mode ***/
2972
2973 static Bool
2974 Fake_glXSet3DfxModeMESA( int mode )
2975 {
2976 return XMesaSetFXmode( mode );
2977 }
2978
2979
2980
2981 /*** GLX_NV_vertex_array range ***/
2982 static void *
2983 Fake_glXAllocateMemoryNV( GLsizei size,
2984 GLfloat readFrequency,
2985 GLfloat writeFrequency,
2986 GLfloat priority )
2987 {
2988 (void) size;
2989 (void) readFrequency;
2990 (void) writeFrequency;
2991 (void) priority;
2992 return NULL;
2993 }
2994
2995
2996 static void
2997 Fake_glXFreeMemoryNV( GLvoid *pointer )
2998 {
2999 (void) pointer;
3000 }
3001
3002
3003 /*** GLX_MESA_agp_offset ***/
3004
3005 static GLuint
3006 Fake_glXGetAGPOffsetMESA( const GLvoid *pointer )
3007 {
3008 (void) pointer;
3009 return ~0;
3010 }
3011
3012
3013 /*** GLX_EXT_texture_from_pixmap ***/
3014
3015 static void
3016 Fake_glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
3017 const int *attrib_list)
3018 {
3019 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
3020 if (b)
3021 XMesaBindTexImage(dpy, b, buffer, attrib_list);
3022 }
3023
3024 static void
3025 Fake_glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
3026 {
3027 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
3028 if (b)
3029 XMesaReleaseTexImage(dpy, b, buffer);
3030 }
3031
3032
3033 /* silence warning */
3034 extern struct _glxapi_table *_mesa_GetGLXDispatchTable(void);
3035
3036
3037 /**
3038 * Create a new GLX API dispatch table with its function pointers
3039 * initialized to point to Mesa's "fake" GLX API functions.
3040 * Note: there's a similar function (_real_GetGLXDispatchTable) that
3041 * returns a new dispatch table with all pointers initalized to point
3042 * to "real" GLX functions (which understand GLX wire protocol, etc).
3043 */
3044 struct _glxapi_table *
3045 _mesa_GetGLXDispatchTable(void)
3046 {
3047 static struct _glxapi_table glx;
3048
3049 /* be sure our dispatch table size <= libGL's table */
3050 {
3051 GLuint size = sizeof(struct _glxapi_table) / sizeof(void *);
3052 (void) size;
3053 assert(_glxapi_get_dispatch_table_size() >= size);
3054 }
3055
3056 /* initialize the whole table to no-ops */
3057 _glxapi_set_no_op_table(&glx);
3058
3059 /* now initialize the table with the functions I implement */
3060 glx.ChooseVisual = Fake_glXChooseVisual;
3061 glx.CopyContext = Fake_glXCopyContext;
3062 glx.CreateContext = Fake_glXCreateContext;
3063 glx.CreateGLXPixmap = Fake_glXCreateGLXPixmap;
3064 glx.DestroyContext = Fake_glXDestroyContext;
3065 glx.DestroyGLXPixmap = Fake_glXDestroyGLXPixmap;
3066 glx.GetConfig = Fake_glXGetConfig;
3067 /*glx.GetCurrentContext = Fake_glXGetCurrentContext;*/
3068 /*glx.GetCurrentDrawable = Fake_glXGetCurrentDrawable;*/
3069 glx.IsDirect = Fake_glXIsDirect;
3070 glx.MakeCurrent = Fake_glXMakeCurrent;
3071 glx.QueryExtension = Fake_glXQueryExtension;
3072 glx.QueryVersion = Fake_glXQueryVersion;
3073 glx.SwapBuffers = Fake_glXSwapBuffers;
3074 glx.UseXFont = Fake_glXUseXFont;
3075 glx.WaitGL = Fake_glXWaitGL;
3076 glx.WaitX = Fake_glXWaitX;
3077
3078 /*** GLX_VERSION_1_1 ***/
3079 glx.GetClientString = Fake_glXGetClientString;
3080 glx.QueryExtensionsString = Fake_glXQueryExtensionsString;
3081 glx.QueryServerString = Fake_glXQueryServerString;
3082
3083 /*** GLX_VERSION_1_2 ***/
3084 /*glx.GetCurrentDisplay = Fake_glXGetCurrentDisplay;*/
3085
3086 /*** GLX_VERSION_1_3 ***/
3087 glx.ChooseFBConfig = Fake_glXChooseFBConfig;
3088 glx.CreateNewContext = Fake_glXCreateNewContext;
3089 glx.CreatePbuffer = Fake_glXCreatePbuffer;
3090 glx.CreatePixmap = Fake_glXCreatePixmap;
3091 glx.CreateWindow = Fake_glXCreateWindow;
3092 glx.DestroyPbuffer = Fake_glXDestroyPbuffer;
3093 glx.DestroyPixmap = Fake_glXDestroyPixmap;
3094 glx.DestroyWindow = Fake_glXDestroyWindow;
3095 /*glx.GetCurrentReadDrawable = Fake_glXGetCurrentReadDrawable;*/
3096 glx.GetFBConfigAttrib = Fake_glXGetFBConfigAttrib;
3097 glx.GetFBConfigs = Fake_glXGetFBConfigs;
3098 glx.GetSelectedEvent = Fake_glXGetSelectedEvent;
3099 glx.GetVisualFromFBConfig = Fake_glXGetVisualFromFBConfig;
3100 glx.MakeContextCurrent = Fake_glXMakeContextCurrent;
3101 glx.QueryContext = Fake_glXQueryContext;
3102 glx.QueryDrawable = Fake_glXQueryDrawable;
3103 glx.SelectEvent = Fake_glXSelectEvent;
3104
3105 /*** GLX_SGI_swap_control ***/
3106 glx.SwapIntervalSGI = Fake_glXSwapIntervalSGI;
3107
3108 /*** GLX_SGI_video_sync ***/
3109 glx.GetVideoSyncSGI = Fake_glXGetVideoSyncSGI;
3110 glx.WaitVideoSyncSGI = Fake_glXWaitVideoSyncSGI;
3111
3112 /*** GLX_SGI_make_current_read ***/
3113 glx.MakeCurrentReadSGI = Fake_glXMakeCurrentReadSGI;
3114 /*glx.GetCurrentReadDrawableSGI = Fake_glXGetCurrentReadDrawableSGI;*/
3115
3116 /*** GLX_SGIX_video_source ***/
3117 #if defined(_VL_H)
3118 glx.CreateGLXVideoSourceSGIX = Fake_glXCreateGLXVideoSourceSGIX;
3119 glx.DestroyGLXVideoSourceSGIX = Fake_glXDestroyGLXVideoSourceSGIX;
3120 #endif
3121
3122 /*** GLX_EXT_import_context ***/
3123 glx.FreeContextEXT = Fake_glXFreeContextEXT;
3124 glx.GetContextIDEXT = Fake_glXGetContextIDEXT;
3125 /*glx.GetCurrentDisplayEXT = Fake_glXGetCurrentDisplayEXT;*/
3126 glx.ImportContextEXT = Fake_glXImportContextEXT;
3127 glx.QueryContextInfoEXT = Fake_glXQueryContextInfoEXT;
3128
3129 /*** GLX_SGIX_fbconfig ***/
3130 glx.GetFBConfigAttribSGIX = Fake_glXGetFBConfigAttribSGIX;
3131 glx.ChooseFBConfigSGIX = Fake_glXChooseFBConfigSGIX;
3132 glx.CreateGLXPixmapWithConfigSGIX = Fake_glXCreateGLXPixmapWithConfigSGIX;
3133 glx.CreateContextWithConfigSGIX = Fake_glXCreateContextWithConfigSGIX;
3134 glx.GetVisualFromFBConfigSGIX = Fake_glXGetVisualFromFBConfigSGIX;
3135 glx.GetFBConfigFromVisualSGIX = Fake_glXGetFBConfigFromVisualSGIX;
3136
3137 /*** GLX_SGIX_pbuffer ***/
3138 glx.CreateGLXPbufferSGIX = Fake_glXCreateGLXPbufferSGIX;
3139 glx.DestroyGLXPbufferSGIX = Fake_glXDestroyGLXPbufferSGIX;
3140 glx.QueryGLXPbufferSGIX = Fake_glXQueryGLXPbufferSGIX;
3141 glx.SelectEventSGIX = Fake_glXSelectEventSGIX;
3142 glx.GetSelectedEventSGIX = Fake_glXGetSelectedEventSGIX;
3143
3144 /*** GLX_SGI_cushion ***/
3145 glx.CushionSGI = Fake_glXCushionSGI;
3146
3147 /*** GLX_SGIX_video_resize ***/
3148 glx.BindChannelToWindowSGIX = Fake_glXBindChannelToWindowSGIX;
3149 glx.ChannelRectSGIX = Fake_glXChannelRectSGIX;
3150 glx.QueryChannelRectSGIX = Fake_glXQueryChannelRectSGIX;
3151 glx.QueryChannelDeltasSGIX = Fake_glXQueryChannelDeltasSGIX;
3152 glx.ChannelRectSyncSGIX = Fake_glXChannelRectSyncSGIX;
3153
3154 /*** GLX_SGIX_dmbuffer **/
3155 #if defined(_DM_BUFFER_H_)
3156 glx.AssociateDMPbufferSGIX = NULL;
3157 #endif
3158
3159 /*** GLX_SGIX_swap_group ***/
3160 glx.JoinSwapGroupSGIX = Fake_glXJoinSwapGroupSGIX;
3161
3162 /*** GLX_SGIX_swap_barrier ***/
3163 glx.BindSwapBarrierSGIX = Fake_glXBindSwapBarrierSGIX;
3164 glx.QueryMaxSwapBarriersSGIX = Fake_glXQueryMaxSwapBarriersSGIX;
3165
3166 /*** GLX_SUN_get_transparent_index ***/
3167 glx.GetTransparentIndexSUN = Fake_glXGetTransparentIndexSUN;
3168
3169 /*** GLX_MESA_copy_sub_buffer ***/
3170 glx.CopySubBufferMESA = Fake_glXCopySubBufferMESA;
3171
3172 /*** GLX_MESA_release_buffers ***/
3173 glx.ReleaseBuffersMESA = Fake_glXReleaseBuffersMESA;
3174
3175 /*** GLX_MESA_pixmap_colormap ***/
3176 glx.CreateGLXPixmapMESA = Fake_glXCreateGLXPixmapMESA;
3177
3178 /*** GLX_MESA_set_3dfx_mode ***/
3179 glx.Set3DfxModeMESA = Fake_glXSet3DfxModeMESA;
3180
3181 /*** GLX_NV_vertex_array_range ***/
3182 glx.AllocateMemoryNV = Fake_glXAllocateMemoryNV;
3183 glx.FreeMemoryNV = Fake_glXFreeMemoryNV;
3184
3185 /*** GLX_MESA_agp_offset ***/
3186 glx.GetAGPOffsetMESA = Fake_glXGetAGPOffsetMESA;
3187
3188 /*** GLX_EXT_texture_from_pixmap ***/
3189 glx.BindTexImageEXT = Fake_glXBindTexImageEXT;
3190 glx.ReleaseTexImageEXT = Fake_glXReleaseTexImageEXT;
3191
3192 return &glx;
3193 }