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