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