ea3585258d8605b57986ce10f03d854c2331b170
[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 "main/context.h"
46 #include "main/config.h"
47 #include "main/macros.h"
48 #include "main/imports.h"
49 #include "main/mtypes.h"
50 #include "main/version.h"
51 #include "xfonts.h"
52 #include "xmesaP.h"
53
54 #ifdef __VMS
55 #define _mesa_sprintf sprintf
56 #endif
57
58 /* This indicates the client-side GLX API and GLX encoder version. */
59 #define CLIENT_MAJOR_VERSION 1
60 #define CLIENT_MINOR_VERSION 4 /* but don't have 1.3's pbuffers, etc yet */
61
62 /* This indicates the server-side GLX decoder version.
63 * GLX 1.4 indicates OpenGL 1.3 support
64 */
65 #define SERVER_MAJOR_VERSION 1
66 #define SERVER_MINOR_VERSION 4
67
68 /* This is appended onto the glXGetClient/ServerString version strings. */
69 #define MESA_GLX_VERSION "Mesa " MESA_VERSION_STRING
70
71 /* Who implemented this GLX? */
72 #define VENDOR "Brian Paul"
73
74 #define EXTENSIONS \
75 "GLX_MESA_set_3dfx_mode " \
76 "GLX_MESA_copy_sub_buffer " \
77 "GLX_MESA_pixmap_colormap " \
78 "GLX_MESA_release_buffers " \
79 "GLX_ARB_get_proc_address " \
80 "GLX_EXT_texture_from_pixmap " \
81 "GLX_EXT_visual_info " \
82 "GLX_EXT_visual_rating " \
83 /*"GLX_SGI_video_sync "*/ \
84 "GLX_SGIX_fbconfig " \
85 "GLX_SGIX_pbuffer "
86
87 /*
88 * Our fake GLX context will contain a "real" GLX context and an XMesa context.
89 *
90 * Note that a pointer to a __GLXcontext is a pointer to a fake_glx_context,
91 * and vice versa.
92 *
93 * We really just need this structure in order to make the libGL functions
94 * glXGetCurrentContext(), glXGetCurrentDrawable() and glXGetCurrentDisplay()
95 * work correctly.
96 */
97 struct fake_glx_context {
98 __GLXcontext glxContext; /* this MUST be first! */
99 XMesaContext xmesaContext;
100 };
101
102
103
104 /**********************************************************************/
105 /*** GLX Visual Code ***/
106 /**********************************************************************/
107
108 #define DONT_CARE -1
109
110
111 static XMesaVisual *VisualTable = NULL;
112 static int NumVisuals = 0;
113
114
115 /*
116 * This struct and some code fragments borrowed
117 * from Mark Kilgard's GLUT library.
118 */
119 typedef struct _OverlayInfo {
120 /* Avoid 64-bit portability problems by being careful to use
121 longs due to the way XGetWindowProperty is specified. Note
122 that these parameters are passed as CARD32s over X
123 protocol. */
124 unsigned long overlay_visual;
125 long transparent_type;
126 long value;
127 long layer;
128 } OverlayInfo;
129
130
131
132 /* Macro to handle c_class vs class field name in XVisualInfo struct */
133 #if defined(__cplusplus) || defined(c_plusplus)
134 #define CLASS c_class
135 #else
136 #define CLASS class
137 #endif
138
139
140
141 /*
142 * Test if the given XVisualInfo is usable for Mesa rendering.
143 */
144 static GLboolean
145 is_usable_visual( XVisualInfo *vinfo )
146 {
147 switch (vinfo->CLASS) {
148 case StaticGray:
149 case GrayScale:
150 /* Any StaticGray/GrayScale visual works in RGB or CI mode */
151 return GL_TRUE;
152 case StaticColor:
153 case PseudoColor:
154 /* Any StaticColor/PseudoColor visual of at least 4 bits */
155 if (vinfo->depth>=4) {
156 return GL_TRUE;
157 }
158 else {
159 return GL_FALSE;
160 }
161 case TrueColor:
162 case DirectColor:
163 /* Any depth of TrueColor or DirectColor works in RGB mode */
164 return GL_TRUE;
165 default:
166 /* This should never happen */
167 return GL_FALSE;
168 }
169 }
170
171
172
173 /**
174 * Get an array OverlayInfo records for specified screen.
175 * \param dpy the display
176 * \param screen screen number
177 * \param numOverlays returns numver of OverlayInfo records
178 * \return pointer to OverlayInfo array, free with XFree()
179 */
180 static OverlayInfo *
181 GetOverlayInfo(Display *dpy, int screen, int *numOverlays)
182 {
183 Atom overlayVisualsAtom;
184 Atom actualType;
185 Status status;
186 unsigned char *ovInfo;
187 unsigned long sizeData, bytesLeft;
188 int actualFormat;
189
190 /*
191 * The SERVER_OVERLAY_VISUALS property on the root window contains
192 * a list of overlay visuals. Get that list now.
193 */
194 overlayVisualsAtom = XInternAtom(dpy,"SERVER_OVERLAY_VISUALS", True);
195 if (overlayVisualsAtom == None) {
196 return 0;
197 }
198
199 status = XGetWindowProperty(dpy, RootWindow(dpy, screen),
200 overlayVisualsAtom, 0L, (long) 10000, False,
201 overlayVisualsAtom, &actualType, &actualFormat,
202 &sizeData, &bytesLeft,
203 &ovInfo);
204
205 if (status != Success || actualType != overlayVisualsAtom ||
206 actualFormat != 32 || sizeData < 4) {
207 /* something went wrong */
208 XFree((void *) ovInfo);
209 *numOverlays = 0;
210 return NULL;
211 }
212
213 *numOverlays = sizeData / 4;
214 return (OverlayInfo *) ovInfo;
215 }
216
217
218
219 /**
220 * Return the level (overlay, normal, underlay) of a given XVisualInfo.
221 * Input: dpy - the X display
222 * vinfo - the XVisualInfo to test
223 * Return: level of the visual:
224 * 0 = normal planes
225 * >0 = overlay planes
226 * <0 = underlay planes
227 */
228 static int
229 level_of_visual( Display *dpy, XVisualInfo *vinfo )
230 {
231 OverlayInfo *overlay_info;
232 int numOverlaysPerScreen, i;
233
234 overlay_info = GetOverlayInfo(dpy, vinfo->screen, &numOverlaysPerScreen);
235 if (!overlay_info) {
236 return 0;
237 }
238
239 /* search the overlay visual list for the visual ID of interest */
240 for (i = 0; i < numOverlaysPerScreen; i++) {
241 const OverlayInfo *ov = overlay_info + i;
242 if (ov->overlay_visual == vinfo->visualid) {
243 /* found the visual */
244 if (/*ov->transparent_type==1 &&*/ ov->layer!=0) {
245 int level = ov->layer;
246 XFree((void *) overlay_info);
247 return level;
248 }
249 else {
250 XFree((void *) overlay_info);
251 return 0;
252 }
253 }
254 }
255
256 /* The visual ID was not found in the overlay list. */
257 XFree((void *) overlay_info);
258 return 0;
259 }
260
261
262
263
264 /*
265 * Given an XVisualInfo and RGB, Double, and Depth buffer flags, save the
266 * configuration in our list of GLX visuals.
267 */
268 static XMesaVisual
269 save_glx_visual( Display *dpy, XVisualInfo *vinfo,
270 GLboolean rgbFlag, GLboolean alphaFlag, GLboolean dbFlag,
271 GLboolean stereoFlag,
272 GLint depth_size, GLint stencil_size,
273 GLint accumRedSize, GLint accumGreenSize,
274 GLint accumBlueSize, GLint accumAlphaSize,
275 GLint level, GLint numAuxBuffers )
276 {
277 GLboolean ximageFlag = GL_TRUE;
278 XMesaVisual xmvis;
279 GLint i;
280 GLboolean comparePointers;
281
282 if (dbFlag) {
283 /* Check if the MESA_BACK_BUFFER env var is set */
284 char *backbuffer = _mesa_getenv("MESA_BACK_BUFFER");
285 if (backbuffer) {
286 if (backbuffer[0]=='p' || backbuffer[0]=='P') {
287 ximageFlag = GL_FALSE;
288 }
289 else if (backbuffer[0]=='x' || backbuffer[0]=='X') {
290 ximageFlag = GL_TRUE;
291 }
292 else {
293 _mesa_warning(NULL, "Mesa: invalid value for MESA_BACK_BUFFER environment variable, using an XImage.");
294 }
295 }
296 }
297
298 if (stereoFlag) {
299 /* stereo not supported */
300 return NULL;
301 }
302
303 /* Comparing IDs uses less memory but sometimes fails. */
304 /* XXX revisit this after 3.0 is finished. */
305 if (_mesa_getenv("MESA_GLX_VISUAL_HACK"))
306 comparePointers = GL_TRUE;
307 else
308 comparePointers = GL_FALSE;
309
310 /* Force the visual to have an alpha channel */
311 if (rgbFlag && _mesa_getenv("MESA_GLX_FORCE_ALPHA"))
312 alphaFlag = GL_TRUE;
313
314 /* First check if a matching visual is already in the list */
315 for (i=0; i<NumVisuals; i++) {
316 XMesaVisual v = VisualTable[i];
317 if (v->display == dpy
318 && v->mesa_visual.level == level
319 && v->mesa_visual.numAuxBuffers == numAuxBuffers
320 && v->ximage_flag == ximageFlag
321 && v->mesa_visual.rgbMode == rgbFlag
322 && v->mesa_visual.doubleBufferMode == dbFlag
323 && v->mesa_visual.stereoMode == stereoFlag
324 && (v->mesa_visual.alphaBits > 0) == alphaFlag
325 && (v->mesa_visual.depthBits >= depth_size || depth_size == 0)
326 && (v->mesa_visual.stencilBits >= stencil_size || stencil_size == 0)
327 && (v->mesa_visual.accumRedBits >= accumRedSize || accumRedSize == 0)
328 && (v->mesa_visual.accumGreenBits >= accumGreenSize || accumGreenSize == 0)
329 && (v->mesa_visual.accumBlueBits >= accumBlueSize || accumBlueSize == 0)
330 && (v->mesa_visual.accumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
331 /* now either compare XVisualInfo pointers or visual IDs */
332 if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
333 || (comparePointers && v->vishandle == vinfo)) {
334 return v;
335 }
336 }
337 }
338
339 /* Create a new visual and add it to the list. */
340
341 xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
342 stereoFlag, ximageFlag,
343 depth_size, stencil_size,
344 accumRedSize, accumBlueSize,
345 accumBlueSize, accumAlphaSize, 0, level,
346 GLX_NONE_EXT );
347 if (xmvis) {
348 /* Save a copy of the pointer now so we can find this visual again
349 * if we need to search for it in find_glx_visual().
350 */
351 xmvis->vishandle = vinfo;
352 /* Allocate more space for additional visual */
353 VisualTable = (XMesaVisual *) _mesa_realloc( VisualTable,
354 sizeof(XMesaVisual) * NumVisuals,
355 sizeof(XMesaVisual) * (NumVisuals + 1));
356 /* add xmvis to the list */
357 VisualTable[NumVisuals] = xmvis;
358 NumVisuals++;
359 /* XXX minor hack, because XMesaCreateVisual doesn't support an
360 * aux buffers parameter.
361 */
362 xmvis->mesa_visual.numAuxBuffers = numAuxBuffers;
363 }
364 return xmvis;
365 }
366
367
368 /**
369 * Return the default number of bits for the Z buffer.
370 * If defined, use the MESA_GLX_DEPTH_BITS env var value.
371 * Otherwise, use the DEFAULT_SOFTWARE_DEPTH_BITS constant.
372 * XXX probably do the same thing for stencil, accum, etc.
373 */
374 static GLint
375 default_depth_bits(void)
376 {
377 int zBits;
378 const char *zEnv = _mesa_getenv("MESA_GLX_DEPTH_BITS");
379 if (zEnv)
380 zBits = _mesa_atoi(zEnv);
381 else
382 zBits = DEFAULT_SOFTWARE_DEPTH_BITS;
383 return zBits;
384 }
385
386 static GLint
387 default_alpha_bits(void)
388 {
389 int aBits;
390 const char *aEnv = _mesa_getenv("MESA_GLX_ALPHA_BITS");
391 if (aEnv)
392 aBits = _mesa_atoi(aEnv);
393 else
394 aBits = 0;
395 return aBits;
396 }
397
398 static GLint
399 default_accum_bits(void)
400 {
401 return 16;
402 }
403
404
405
406 /*
407 * Create a GLX visual from a regular XVisualInfo.
408 * This is called when Fake GLX is given an XVisualInfo which wasn't
409 * returned by glXChooseVisual. Since this is the first time we're
410 * considering this visual we'll take a guess at reasonable values
411 * for depth buffer size, stencil size, accum size, etc.
412 * This is the best we can do with a client-side emulation of GLX.
413 */
414 static XMesaVisual
415 create_glx_visual( Display *dpy, XVisualInfo *visinfo )
416 {
417 int vislevel;
418 GLint zBits = default_depth_bits();
419 GLint accBits = default_accum_bits();
420 GLboolean alphaFlag = default_alpha_bits() > 0;
421
422 vislevel = level_of_visual( dpy, visinfo );
423 if (vislevel) {
424 /* Configure this visual as a CI, single-buffered overlay */
425 return save_glx_visual( dpy, visinfo,
426 GL_FALSE, /* rgb */
427 GL_FALSE, /* alpha */
428 GL_FALSE, /* double */
429 GL_FALSE, /* stereo */
430 0, /* depth bits */
431 0, /* stencil bits */
432 0,0,0,0, /* accum bits */
433 vislevel, /* level */
434 0 /* numAux */
435 );
436 }
437 else if (is_usable_visual( visinfo )) {
438 if (_mesa_getenv("MESA_GLX_FORCE_CI")) {
439 /* Configure this visual as a COLOR INDEX visual. */
440 return save_glx_visual( dpy, visinfo,
441 GL_FALSE, /* rgb */
442 GL_FALSE, /* alpha */
443 GL_TRUE, /* double */
444 GL_FALSE, /* stereo */
445 zBits,
446 STENCIL_BITS,
447 0, 0, 0, 0, /* accum bits */
448 0, /* level */
449 0 /* numAux */
450 );
451 }
452 else {
453 /* Configure this visual as RGB, double-buffered, depth-buffered. */
454 /* This is surely wrong for some people's needs but what else */
455 /* can be done? They should use glXChooseVisual(). */
456 return save_glx_visual( dpy, visinfo,
457 GL_TRUE, /* rgb */
458 alphaFlag, /* alpha */
459 GL_TRUE, /* double */
460 GL_FALSE, /* stereo */
461 zBits,
462 STENCIL_BITS,
463 accBits, /* r */
464 accBits, /* g */
465 accBits, /* b */
466 accBits, /* a */
467 0, /* level */
468 0 /* numAux */
469 );
470 }
471 }
472 else {
473 _mesa_warning(NULL, "Mesa: error in glXCreateContext: bad visual\n");
474 return NULL;
475 }
476 }
477
478
479
480 /*
481 * Find the GLX visual associated with an XVisualInfo.
482 */
483 static XMesaVisual
484 find_glx_visual( Display *dpy, XVisualInfo *vinfo )
485 {
486 int i;
487
488 /* try to match visual id */
489 for (i=0;i<NumVisuals;i++) {
490 if (VisualTable[i]->display==dpy
491 && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
492 return VisualTable[i];
493 }
494 }
495
496 /* if that fails, try to match pointers */
497 for (i=0;i<NumVisuals;i++) {
498 if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
499 return VisualTable[i];
500 }
501 }
502
503 return NULL;
504 }
505
506
507
508 /**
509 * Return the transparent pixel value for a GLX visual.
510 * Input: glxvis - the glx_visual
511 * Return: a pixel value or -1 if no transparent pixel
512 */
513 static int
514 transparent_pixel( XMesaVisual glxvis )
515 {
516 Display *dpy = glxvis->display;
517 XVisualInfo *vinfo = glxvis->visinfo;
518 OverlayInfo *overlay_info;
519 int numOverlaysPerScreen, i;
520
521 overlay_info = GetOverlayInfo(dpy, vinfo->screen, &numOverlaysPerScreen);
522 if (!overlay_info) {
523 return -1;
524 }
525
526 for (i = 0; i < numOverlaysPerScreen; i++) {
527 const OverlayInfo *ov = overlay_info + i;
528 if (ov->overlay_visual == vinfo->visualid) {
529 /* found it! */
530 if (ov->transparent_type == 0) {
531 /* type 0 indicates no transparency */
532 XFree((void *) overlay_info);
533 return -1;
534 }
535 else {
536 /* ov->value is the transparent pixel */
537 XFree((void *) overlay_info);
538 return ov->value;
539 }
540 }
541 }
542
543 /* The visual ID was not found in the overlay list. */
544 XFree((void *) overlay_info);
545 return -1;
546 }
547
548
549
550 /**
551 * Try to get an X visual which matches the given arguments.
552 */
553 static XVisualInfo *
554 get_visual( Display *dpy, int scr, unsigned int depth, int xclass )
555 {
556 XVisualInfo temp, *vis;
557 long mask;
558 int n;
559 unsigned int default_depth;
560 int default_class;
561
562 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
563 temp.screen = scr;
564 temp.depth = depth;
565 temp.CLASS = xclass;
566
567 default_depth = DefaultDepth(dpy,scr);
568 default_class = DefaultVisual(dpy,scr)->CLASS;
569
570 if (depth==default_depth && xclass==default_class) {
571 /* try to get root window's visual */
572 temp.visualid = DefaultVisual(dpy,scr)->visualid;
573 mask |= VisualIDMask;
574 }
575
576 vis = XGetVisualInfo( dpy, mask, &temp, &n );
577
578 /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
579 * An SGI Infinite Reality system, for example, can have 30bpp pixels:
580 * 10 bits per color channel. Mesa's limited to a max of 8 bits/channel.
581 */
582 if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
583 if (_mesa_bitcount((GLuint) vis->red_mask ) <= 8 &&
584 _mesa_bitcount((GLuint) vis->green_mask) <= 8 &&
585 _mesa_bitcount((GLuint) vis->blue_mask ) <= 8) {
586 return vis;
587 }
588 else {
589 XFree((void *) vis);
590 return NULL;
591 }
592 }
593
594 return vis;
595 }
596
597
598
599 /*
600 * Retrieve the value of the given environment variable and find
601 * the X visual which matches it.
602 * Input: dpy - the display
603 * screen - the screen number
604 * varname - the name of the environment variable
605 * Return: an XVisualInfo pointer to NULL if error.
606 */
607 static XVisualInfo *
608 get_env_visual(Display *dpy, int scr, const char *varname)
609 {
610 char value[100], type[100];
611 int depth, xclass = -1;
612 XVisualInfo *vis;
613
614 if (!_mesa_getenv( varname )) {
615 return NULL;
616 }
617
618 _mesa_strncpy( value, _mesa_getenv(varname), 100 );
619 value[99] = 0;
620
621 sscanf( value, "%s %d", type, &depth );
622
623 if (_mesa_strcmp(type,"TrueColor")==0) xclass = TrueColor;
624 else if (_mesa_strcmp(type,"DirectColor")==0) xclass = DirectColor;
625 else if (_mesa_strcmp(type,"PseudoColor")==0) xclass = PseudoColor;
626 else if (_mesa_strcmp(type,"StaticColor")==0) xclass = StaticColor;
627 else if (_mesa_strcmp(type,"GrayScale")==0) xclass = GrayScale;
628 else if (_mesa_strcmp(type,"StaticGray")==0) xclass = StaticGray;
629
630 if (xclass>-1 && depth>0) {
631 vis = get_visual( dpy, scr, depth, xclass );
632 if (vis) {
633 return vis;
634 }
635 }
636
637 _mesa_warning(NULL, "GLX unable to find visual class=%s, depth=%d.",
638 type, depth);
639
640 return NULL;
641 }
642
643
644
645 /*
646 * Select an X visual which satisfies the RGBA/CI flag and minimum depth.
647 * Input: dpy, screen - X display and screen number
648 * rgba - GL_TRUE = RGBA mode, GL_FALSE = CI mode
649 * min_depth - minimum visual depth
650 * preferred_class - preferred GLX visual class or DONT_CARE
651 * Return: pointer to an XVisualInfo or NULL.
652 */
653 static XVisualInfo *
654 choose_x_visual( Display *dpy, int screen, GLboolean rgba, int min_depth,
655 int preferred_class )
656 {
657 XVisualInfo *vis;
658 int xclass, visclass = 0;
659 int depth;
660
661 if (rgba) {
662 Atom hp_cr_maps = XInternAtom(dpy, "_HP_RGB_SMOOTH_MAP_LIST", True);
663 /* First see if the MESA_RGB_VISUAL env var is defined */
664 vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
665 if (vis) {
666 return vis;
667 }
668 /* Otherwise, search for a suitable visual */
669 if (preferred_class==DONT_CARE) {
670 for (xclass=0;xclass<6;xclass++) {
671 switch (xclass) {
672 case 0: visclass = TrueColor; break;
673 case 1: visclass = DirectColor; break;
674 case 2: visclass = PseudoColor; break;
675 case 3: visclass = StaticColor; break;
676 case 4: visclass = GrayScale; break;
677 case 5: visclass = StaticGray; break;
678 }
679 if (min_depth==0) {
680 /* start with shallowest */
681 for (depth=0;depth<=32;depth++) {
682 if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
683 /* Special case: try to get 8-bit PseudoColor before */
684 /* 8-bit TrueColor */
685 vis = get_visual( dpy, screen, 8, PseudoColor );
686 if (vis) {
687 return vis;
688 }
689 }
690 vis = get_visual( dpy, screen, depth, visclass );
691 if (vis) {
692 return vis;
693 }
694 }
695 }
696 else {
697 /* start with deepest */
698 for (depth=32;depth>=min_depth;depth--) {
699 if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
700 /* Special case: try to get 8-bit PseudoColor before */
701 /* 8-bit TrueColor */
702 vis = get_visual( dpy, screen, 8, PseudoColor );
703 if (vis) {
704 return vis;
705 }
706 }
707 vis = get_visual( dpy, screen, depth, visclass );
708 if (vis) {
709 return vis;
710 }
711 }
712 }
713 }
714 }
715 else {
716 /* search for a specific visual class */
717 switch (preferred_class) {
718 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
719 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
720 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
721 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
722 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
723 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
724 default: return NULL;
725 }
726 if (min_depth==0) {
727 /* start with shallowest */
728 for (depth=0;depth<=32;depth++) {
729 vis = get_visual( dpy, screen, depth, visclass );
730 if (vis) {
731 return vis;
732 }
733 }
734 }
735 else {
736 /* start with deepest */
737 for (depth=32;depth>=min_depth;depth--) {
738 vis = get_visual( dpy, screen, depth, visclass );
739 if (vis) {
740 return vis;
741 }
742 }
743 }
744 }
745 }
746 else {
747 /* First see if the MESA_CI_VISUAL env var is defined */
748 vis = get_env_visual( dpy, screen, "MESA_CI_VISUAL" );
749 if (vis) {
750 return vis;
751 }
752 /* Otherwise, search for a suitable visual, starting with shallowest */
753 if (preferred_class==DONT_CARE) {
754 for (xclass=0;xclass<4;xclass++) {
755 switch (xclass) {
756 case 0: visclass = PseudoColor; break;
757 case 1: visclass = StaticColor; break;
758 case 2: visclass = GrayScale; break;
759 case 3: visclass = StaticGray; break;
760 }
761 /* try 8-bit up through 16-bit */
762 for (depth=8;depth<=16;depth++) {
763 vis = get_visual( dpy, screen, depth, visclass );
764 if (vis) {
765 return vis;
766 }
767 }
768 /* try min_depth up to 8-bit */
769 for (depth=min_depth;depth<8;depth++) {
770 vis = get_visual( dpy, screen, depth, visclass );
771 if (vis) {
772 return vis;
773 }
774 }
775 }
776 }
777 else {
778 /* search for a specific visual class */
779 switch (preferred_class) {
780 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
781 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
782 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
783 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
784 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
785 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
786 default: return NULL;
787 }
788 /* try 8-bit up through 16-bit */
789 for (depth=8;depth<=16;depth++) {
790 vis = get_visual( dpy, screen, depth, visclass );
791 if (vis) {
792 return vis;
793 }
794 }
795 /* try min_depth up to 8-bit */
796 for (depth=min_depth;depth<8;depth++) {
797 vis = get_visual( dpy, screen, depth, visclass );
798 if (vis) {
799 return vis;
800 }
801 }
802 }
803 }
804
805 /* didn't find a visual */
806 return NULL;
807 }
808
809
810
811 /*
812 * Find the deepest X over/underlay visual of at least min_depth.
813 * Input: dpy, screen - X display and screen number
814 * level - the over/underlay level
815 * trans_type - transparent pixel type: GLX_NONE_EXT,
816 * GLX_TRANSPARENT_RGB_EXT, GLX_TRANSPARENT_INDEX_EXT,
817 * or DONT_CARE
818 * trans_value - transparent pixel value or DONT_CARE
819 * min_depth - minimum visual depth
820 * preferred_class - preferred GLX visual class or DONT_CARE
821 * Return: pointer to an XVisualInfo or NULL.
822 */
823 static XVisualInfo *
824 choose_x_overlay_visual( Display *dpy, int scr, GLboolean rgbFlag,
825 int level, int trans_type, int trans_value,
826 int min_depth, int preferred_class )
827 {
828 OverlayInfo *overlay_info;
829 int numOverlaysPerScreen;
830 int i;
831 XVisualInfo *deepvis;
832 int deepest;
833
834 /*DEBUG int tt, tv; */
835
836 switch (preferred_class) {
837 case GLX_TRUE_COLOR_EXT: preferred_class = TrueColor; break;
838 case GLX_DIRECT_COLOR_EXT: preferred_class = DirectColor; break;
839 case GLX_PSEUDO_COLOR_EXT: preferred_class = PseudoColor; break;
840 case GLX_STATIC_COLOR_EXT: preferred_class = StaticColor; break;
841 case GLX_GRAY_SCALE_EXT: preferred_class = GrayScale; break;
842 case GLX_STATIC_GRAY_EXT: preferred_class = StaticGray; break;
843 default: preferred_class = DONT_CARE;
844 }
845
846 overlay_info = GetOverlayInfo(dpy, scr, &numOverlaysPerScreen);
847 if (!overlay_info) {
848 return NULL;
849 }
850
851 /* Search for the deepest overlay which satisifies all criteria. */
852 deepest = min_depth;
853 deepvis = NULL;
854
855 for (i = 0; i < numOverlaysPerScreen; i++) {
856 const OverlayInfo *ov = overlay_info + i;
857 XVisualInfo *vislist, vistemplate;
858 int count;
859
860 if (ov->layer!=level) {
861 /* failed overlay level criteria */
862 continue;
863 }
864 if (!(trans_type==DONT_CARE
865 || (trans_type==GLX_TRANSPARENT_INDEX_EXT
866 && ov->transparent_type>0)
867 || (trans_type==GLX_NONE_EXT && ov->transparent_type==0))) {
868 /* failed transparent pixel type criteria */
869 continue;
870 }
871 if (trans_value!=DONT_CARE && trans_value!=ov->value) {
872 /* failed transparent pixel value criteria */
873 continue;
874 }
875
876 /* get XVisualInfo and check the depth */
877 vistemplate.visualid = ov->overlay_visual;
878 vistemplate.screen = scr;
879 vislist = XGetVisualInfo( dpy, VisualIDMask | VisualScreenMask,
880 &vistemplate, &count );
881
882 if (count!=1) {
883 /* something went wrong */
884 continue;
885 }
886 if (preferred_class!=DONT_CARE && preferred_class!=vislist->CLASS) {
887 /* wrong visual class */
888 continue;
889 }
890
891 /* if RGB was requested, make sure we have True/DirectColor */
892 if (rgbFlag && vislist->CLASS != TrueColor
893 && vislist->CLASS != DirectColor)
894 continue;
895
896 /* if CI was requested, make sure we have a color indexed visual */
897 if (!rgbFlag
898 && (vislist->CLASS == TrueColor || vislist->CLASS == DirectColor))
899 continue;
900
901 if (deepvis==NULL || vislist->depth > deepest) {
902 /* YES! found a satisfactory visual */
903 if (deepvis) {
904 XFree( deepvis );
905 }
906 deepest = vislist->depth;
907 deepvis = vislist;
908 /* DEBUG tt = ov->transparent_type;*/
909 /* DEBUG tv = ov->value; */
910 }
911 }
912
913 /*DEBUG
914 if (deepvis) {
915 printf("chose 0x%x: layer=%d depth=%d trans_type=%d trans_value=%d\n",
916 deepvis->visualid, level, deepvis->depth, tt, tv );
917 }
918 */
919 return deepvis;
920 }
921
922
923 /**********************************************************************/
924 /*** Display-related functions ***/
925 /**********************************************************************/
926
927
928 /**
929 * Free all XMesaVisuals which are associated with the given display.
930 */
931 static void
932 destroy_visuals_on_display(Display *dpy)
933 {
934 int i;
935 for (i = 0; i < NumVisuals; i++) {
936 if (VisualTable[i]->display == dpy) {
937 /* remove this visual */
938 int j;
939 free(VisualTable[i]);
940 for (j = i; j < NumVisuals - 1; j++)
941 VisualTable[j] = VisualTable[j + 1];
942 NumVisuals--;
943 }
944 }
945 }
946
947
948 /**
949 * Called from XCloseDisplay() to let us free our display-related data.
950 */
951 static int
952 close_display_callback(Display *dpy, XExtCodes *codes)
953 {
954 destroy_visuals_on_display(dpy);
955 xmesa_destroy_buffers_on_display(dpy);
956 return 0;
957 }
958
959
960 /**
961 * Look for the named extension on given display and return a pointer
962 * to the _XExtension data, or NULL if extension not found.
963 */
964 static _XExtension *
965 lookup_extension(Display *dpy, const char *extName)
966 {
967 _XExtension *ext;
968 for (ext = dpy->ext_procs; ext; ext = ext->next) {
969 if (ext->name && strcmp(ext->name, extName) == 0) {
970 return ext;
971 }
972 }
973 return NULL;
974 }
975
976
977 /**
978 * Whenever we're given a new Display pointer, call this function to
979 * register our close_display_callback function.
980 */
981 static void
982 register_with_display(Display *dpy)
983 {
984 const char *extName = "MesaGLX";
985 _XExtension *ext;
986
987 ext = lookup_extension(dpy, extName);
988 if (!ext) {
989 XExtCodes *c = XAddExtension(dpy);
990 ext = dpy->ext_procs; /* new extension is at head of list */
991 assert(c->extension == ext->codes.extension);
992 ext->name = _mesa_strdup(extName);
993 ext->close_display = close_display_callback;
994 }
995 }
996
997
998 /**********************************************************************/
999 /*** Begin Fake GLX API Functions ***/
1000 /**********************************************************************/
1001
1002
1003 /**
1004 * Helper used by glXChooseVisual and glXChooseFBConfig.
1005 * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
1006 * the later.
1007 * In either case, the attribute list is terminated with the value 'None'.
1008 */
1009 static XMesaVisual
1010 choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
1011 {
1012 const GLboolean rgbModeDefault = fbConfig;
1013 const int *parselist;
1014 XVisualInfo *vis;
1015 int min_ci = 0;
1016 int min_red=0, min_green=0, min_blue=0;
1017 GLboolean rgb_flag = rgbModeDefault;
1018 GLboolean alpha_flag = GL_FALSE;
1019 GLboolean double_flag = GL_FALSE;
1020 GLboolean stereo_flag = GL_FALSE;
1021 GLint depth_size = 0;
1022 GLint stencil_size = 0;
1023 GLint accumRedSize = 0;
1024 GLint accumGreenSize = 0;
1025 GLint accumBlueSize = 0;
1026 GLint accumAlphaSize = 0;
1027 int level = 0;
1028 int visual_type = DONT_CARE;
1029 int trans_type = DONT_CARE;
1030 int trans_value = DONT_CARE;
1031 GLint caveat = DONT_CARE;
1032 XMesaVisual xmvis = NULL;
1033 int desiredVisualID = -1;
1034 int numAux = 0;
1035
1036 parselist = list;
1037
1038 while (*parselist) {
1039
1040 switch (*parselist) {
1041 case GLX_USE_GL:
1042 if (fbConfig) {
1043 /* invalid token */
1044 return NULL;
1045 }
1046 else {
1047 /* skip */
1048 parselist++;
1049 }
1050 break;
1051 case GLX_BUFFER_SIZE:
1052 parselist++;
1053 min_ci = *parselist++;
1054 break;
1055 case GLX_LEVEL:
1056 parselist++;
1057 level = *parselist++;
1058 break;
1059 case GLX_RGBA:
1060 if (fbConfig) {
1061 /* invalid token */
1062 return NULL;
1063 }
1064 else {
1065 rgb_flag = GL_TRUE;
1066 parselist++;
1067 }
1068 break;
1069 case GLX_DOUBLEBUFFER:
1070 parselist++;
1071 if (fbConfig) {
1072 double_flag = *parselist++;
1073 }
1074 else {
1075 double_flag = GL_TRUE;
1076 }
1077 break;
1078 case GLX_STEREO:
1079 parselist++;
1080 if (fbConfig) {
1081 stereo_flag = *parselist++;
1082 }
1083 else {
1084 stereo_flag = GL_TRUE;
1085 }
1086 break;
1087 case GLX_AUX_BUFFERS:
1088 parselist++;
1089 numAux = *parselist++;
1090 if (numAux > MAX_AUX_BUFFERS)
1091 return NULL;
1092 break;
1093 case GLX_RED_SIZE:
1094 parselist++;
1095 min_red = *parselist++;
1096 break;
1097 case GLX_GREEN_SIZE:
1098 parselist++;
1099 min_green = *parselist++;
1100 break;
1101 case GLX_BLUE_SIZE:
1102 parselist++;
1103 min_blue = *parselist++;
1104 break;
1105 case GLX_ALPHA_SIZE:
1106 parselist++;
1107 {
1108 GLint size = *parselist++;
1109 alpha_flag = size ? GL_TRUE : GL_FALSE;
1110 }
1111 break;
1112 case GLX_DEPTH_SIZE:
1113 parselist++;
1114 depth_size = *parselist++;
1115 break;
1116 case GLX_STENCIL_SIZE:
1117 parselist++;
1118 stencil_size = *parselist++;
1119 break;
1120 case GLX_ACCUM_RED_SIZE:
1121 parselist++;
1122 {
1123 GLint size = *parselist++;
1124 accumRedSize = MAX2( accumRedSize, size );
1125 }
1126 break;
1127 case GLX_ACCUM_GREEN_SIZE:
1128 parselist++;
1129 {
1130 GLint size = *parselist++;
1131 accumGreenSize = MAX2( accumGreenSize, size );
1132 }
1133 break;
1134 case GLX_ACCUM_BLUE_SIZE:
1135 parselist++;
1136 {
1137 GLint size = *parselist++;
1138 accumBlueSize = MAX2( accumBlueSize, size );
1139 }
1140 break;
1141 case GLX_ACCUM_ALPHA_SIZE:
1142 parselist++;
1143 {
1144 GLint size = *parselist++;
1145 accumAlphaSize = MAX2( accumAlphaSize, size );
1146 }
1147 break;
1148
1149 /*
1150 * GLX_EXT_visual_info extension
1151 */
1152 case GLX_X_VISUAL_TYPE_EXT:
1153 parselist++;
1154 visual_type = *parselist++;
1155 break;
1156 case GLX_TRANSPARENT_TYPE_EXT:
1157 parselist++;
1158 trans_type = *parselist++;
1159 break;
1160 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1161 parselist++;
1162 trans_value = *parselist++;
1163 break;
1164 case GLX_TRANSPARENT_RED_VALUE_EXT:
1165 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1166 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1167 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1168 /* ignore */
1169 parselist++;
1170 parselist++;
1171 break;
1172
1173 /*
1174 * GLX_EXT_visual_info extension
1175 */
1176 case GLX_VISUAL_CAVEAT_EXT:
1177 parselist++;
1178 caveat = *parselist++; /* ignored for now */
1179 break;
1180
1181 /*
1182 * GLX_ARB_multisample
1183 */
1184 case GLX_SAMPLE_BUFFERS_ARB:
1185 case GLX_SAMPLES_ARB:
1186 parselist++;
1187 if (*parselist++ != 0)
1188 /* ms not supported */
1189 return NULL;
1190 break;
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 }