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