st/xlib: remove always true ifdef GLX_EXTENSION guards
[mesa.git] / src / gallium / state_trackers / glx / xlib / glx_api.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /**
28 * "Fake" GLX API implemented in terms of the XMesa*() functions.
29 */
30
31
32
33 #define GLX_GLXEXT_PROTOTYPES
34 #include "GL/glx.h"
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <X11/Xmd.h>
39 #include <GL/glxproto.h>
40
41 #include "xm_api.h"
42
43 /* An "Atrribs/Attribs" typo was fixed in glxproto.h in Nov 2014.
44 * This is in case we don't have the updated header.
45 */
46 #if !defined(X_GLXCreateContextAttribsARB) && \
47 defined(X_GLXCreateContextAtrribsARB)
48 #define X_GLXCreateContextAttribsARB X_GLXCreateContextAtrribsARB
49 #endif
50
51 /* This indicates the client-side GLX API and GLX encoder version. */
52 #define CLIENT_MAJOR_VERSION 1
53 #define CLIENT_MINOR_VERSION 4 /* but don't have 1.3's pbuffers, etc yet */
54
55 /* This indicates the server-side GLX decoder version.
56 * GLX 1.4 indicates OpenGL 1.3 support
57 */
58 #define SERVER_MAJOR_VERSION 1
59 #define SERVER_MINOR_VERSION 4
60
61 /* Who implemented this GLX? */
62 #define VENDOR "Brian Paul"
63
64 #define EXTENSIONS \
65 "GLX_MESA_copy_sub_buffer " \
66 "GLX_MESA_pixmap_colormap " \
67 "GLX_MESA_release_buffers " \
68 "GLX_ARB_create_context " \
69 "GLX_ARB_create_context_profile " \
70 "GLX_ARB_get_proc_address " \
71 "GLX_EXT_create_context_es_profile " \
72 "GLX_EXT_create_context_es2_profile " \
73 "GLX_EXT_texture_from_pixmap " \
74 "GLX_EXT_visual_info " \
75 "GLX_EXT_visual_rating " \
76 /*"GLX_SGI_video_sync "*/ \
77 "GLX_SGIX_fbconfig " \
78 "GLX_SGIX_pbuffer "
79
80 #define DEFAULT_DIRECT GL_TRUE
81
82
83 /** XXX this could be based on gallium's max texture size */
84 #define PBUFFER_MAX_SIZE 16384
85
86
87 /**
88 * The GLXContext typedef is defined as a pointer to this structure.
89 */
90 struct __GLXcontextRec
91 {
92 Display *currentDpy;
93 GLboolean isDirect;
94 GLXDrawable currentDrawable;
95 GLXDrawable currentReadable;
96 XID xid;
97
98 XMesaContext xmesaContext;
99 };
100
101
102
103 static pipe_tsd ContextTSD;
104
105 /** Set current context for calling thread */
106 static void
107 SetCurrentContext(GLXContext c)
108 {
109 pipe_tsd_set(&ContextTSD, c);
110 }
111
112 /** Get current context for calling thread */
113 static GLXContext
114 GetCurrentContext(void)
115 {
116 return pipe_tsd_get(&ContextTSD);
117 }
118
119
120
121 /**********************************************************************/
122 /*** GLX Visual Code ***/
123 /**********************************************************************/
124
125 #define DONT_CARE -1
126
127
128 static XMesaVisual *VisualTable = NULL;
129 static int NumVisuals = 0;
130
131
132
133 /* Macro to handle c_class vs class field name in XVisualInfo struct */
134 #if defined(__cplusplus) || defined(c_plusplus)
135 #define CLASS c_class
136 #else
137 #define CLASS class
138 #endif
139
140
141
142 /*
143 * Test if the given XVisualInfo is usable for Mesa rendering.
144 */
145 static GLboolean
146 is_usable_visual( XVisualInfo *vinfo )
147 {
148 switch (vinfo->CLASS) {
149 case StaticGray:
150 case GrayScale:
151 /* Any StaticGray/GrayScale visual works in RGB or CI mode */
152 return GL_TRUE;
153 case StaticColor:
154 case PseudoColor:
155 /* Any StaticColor/PseudoColor visual of at least 4 bits */
156 if (vinfo->depth>=4) {
157 return GL_TRUE;
158 }
159 else {
160 return GL_FALSE;
161 }
162 case TrueColor:
163 case DirectColor:
164 /* Any depth of TrueColor or DirectColor works in RGB mode */
165 return GL_TRUE;
166 default:
167 /* This should never happen */
168 return GL_FALSE;
169 }
170 }
171
172
173 /*
174 * Given an XVisualInfo and RGB, Double, and Depth buffer flags, save the
175 * configuration in our list of GLX visuals.
176 */
177 static XMesaVisual
178 save_glx_visual( Display *dpy, XVisualInfo *vinfo,
179 GLboolean rgbFlag, GLboolean alphaFlag, GLboolean dbFlag,
180 GLboolean stereoFlag,
181 GLint depth_size, GLint stencil_size,
182 GLint accumRedSize, GLint accumGreenSize,
183 GLint accumBlueSize, GLint accumAlphaSize,
184 GLint level, GLint numAuxBuffers )
185 {
186 GLboolean ximageFlag = GL_TRUE;
187 XMesaVisual xmvis;
188 GLint i;
189 GLboolean comparePointers;
190
191 if (dbFlag) {
192 /* Check if the MESA_BACK_BUFFER env var is set */
193 char *backbuffer = getenv("MESA_BACK_BUFFER");
194 if (backbuffer) {
195 if (backbuffer[0]=='p' || backbuffer[0]=='P') {
196 ximageFlag = GL_FALSE;
197 }
198 else if (backbuffer[0]=='x' || backbuffer[0]=='X') {
199 ximageFlag = GL_TRUE;
200 }
201 else {
202 _mesa_warning(NULL, "Mesa: invalid value for MESA_BACK_BUFFER environment variable, using an XImage.");
203 }
204 }
205 }
206
207 if (stereoFlag) {
208 /* stereo not supported */
209 return NULL;
210 }
211
212 if (stencil_size > 0 && depth_size > 0)
213 depth_size = 24;
214
215 /* Comparing IDs uses less memory but sometimes fails. */
216 /* XXX revisit this after 3.0 is finished. */
217 if (getenv("MESA_GLX_VISUAL_HACK"))
218 comparePointers = GL_TRUE;
219 else
220 comparePointers = GL_FALSE;
221
222 /* Force the visual to have an alpha channel */
223 if (rgbFlag && getenv("MESA_GLX_FORCE_ALPHA"))
224 alphaFlag = GL_TRUE;
225
226 /* First check if a matching visual is already in the list */
227 for (i=0; i<NumVisuals; i++) {
228 XMesaVisual v = VisualTable[i];
229 if (v->display == dpy
230 && v->mesa_visual.level == level
231 && v->mesa_visual.numAuxBuffers == numAuxBuffers
232 && v->ximage_flag == ximageFlag
233 && v->mesa_visual.rgbMode == rgbFlag
234 && v->mesa_visual.doubleBufferMode == dbFlag
235 && v->mesa_visual.stereoMode == stereoFlag
236 && (v->mesa_visual.alphaBits > 0) == alphaFlag
237 && (v->mesa_visual.depthBits >= depth_size || depth_size == 0)
238 && (v->mesa_visual.stencilBits >= stencil_size || stencil_size == 0)
239 && (v->mesa_visual.accumRedBits >= accumRedSize || accumRedSize == 0)
240 && (v->mesa_visual.accumGreenBits >= accumGreenSize || accumGreenSize == 0)
241 && (v->mesa_visual.accumBlueBits >= accumBlueSize || accumBlueSize == 0)
242 && (v->mesa_visual.accumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
243 /* now either compare XVisualInfo pointers or visual IDs */
244 if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
245 || (comparePointers && v->vishandle == vinfo)) {
246 return v;
247 }
248 }
249 }
250
251 /* Create a new visual and add it to the list. */
252
253 xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
254 stereoFlag, ximageFlag,
255 depth_size, stencil_size,
256 accumRedSize, accumBlueSize,
257 accumBlueSize, accumAlphaSize, 0, level,
258 GLX_NONE_EXT );
259 if (xmvis) {
260 /* Save a copy of the pointer now so we can find this visual again
261 * if we need to search for it in find_glx_visual().
262 */
263 xmvis->vishandle = vinfo;
264 /* Allocate more space for additional visual */
265 VisualTable = realloc(VisualTable, sizeof(XMesaVisual) * (NumVisuals + 1));
266 /* add xmvis to the list */
267 VisualTable[NumVisuals] = xmvis;
268 NumVisuals++;
269 /* XXX minor hack, because XMesaCreateVisual doesn't support an
270 * aux buffers parameter.
271 */
272 xmvis->mesa_visual.numAuxBuffers = numAuxBuffers;
273 }
274 return xmvis;
275 }
276
277
278 /**
279 * Return the default number of bits for the Z buffer.
280 * If defined, use the MESA_GLX_DEPTH_BITS env var value.
281 * Otherwise, use the DEFAULT_SOFTWARE_DEPTH_BITS constant.
282 * XXX probably do the same thing for stencil, accum, etc.
283 */
284 static GLint
285 default_depth_bits(void)
286 {
287 int zBits;
288 const char *zEnv = getenv("MESA_GLX_DEPTH_BITS");
289 if (zEnv)
290 zBits = atoi(zEnv);
291 else
292 zBits = 24;
293 return zBits;
294 }
295
296 static GLint
297 default_alpha_bits(void)
298 {
299 int aBits;
300 const char *aEnv = getenv("MESA_GLX_ALPHA_BITS");
301 if (aEnv)
302 aBits = atoi(aEnv);
303 else
304 aBits = 0;
305 return aBits;
306 }
307
308 static GLint
309 default_accum_bits(void)
310 {
311 return 16;
312 }
313
314
315
316 /*
317 * Create a GLX visual from a regular XVisualInfo.
318 * This is called when Fake GLX is given an XVisualInfo which wasn't
319 * returned by glXChooseVisual. Since this is the first time we're
320 * considering this visual we'll take a guess at reasonable values
321 * for depth buffer size, stencil size, accum size, etc.
322 * This is the best we can do with a client-side emulation of GLX.
323 */
324 static XMesaVisual
325 create_glx_visual( Display *dpy, XVisualInfo *visinfo )
326 {
327 GLint zBits = default_depth_bits();
328 GLint accBits = default_accum_bits();
329 GLboolean alphaFlag = default_alpha_bits() > 0;
330
331 if (is_usable_visual( visinfo )) {
332 /* Configure this visual as RGB, double-buffered, depth-buffered. */
333 /* This is surely wrong for some people's needs but what else */
334 /* can be done? They should use glXChooseVisual(). */
335 return save_glx_visual( dpy, visinfo,
336 GL_TRUE, /* rgb */
337 alphaFlag, /* alpha */
338 GL_TRUE, /* double */
339 GL_FALSE, /* stereo */
340 zBits,
341 8, /* stencil bits */
342 accBits, /* r */
343 accBits, /* g */
344 accBits, /* b */
345 accBits, /* a */
346 0, /* level */
347 0 /* numAux */
348 );
349 }
350 else {
351 _mesa_warning(NULL, "Mesa: error in glXCreateContext: bad visual\n");
352 return NULL;
353 }
354 }
355
356
357
358 /*
359 * Find the GLX visual associated with an XVisualInfo.
360 */
361 static XMesaVisual
362 find_glx_visual( Display *dpy, XVisualInfo *vinfo )
363 {
364 int i;
365
366 /* try to match visual id */
367 for (i=0;i<NumVisuals;i++) {
368 if (VisualTable[i]->display==dpy
369 && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
370 return VisualTable[i];
371 }
372 }
373
374 /* if that fails, try to match pointers */
375 for (i=0;i<NumVisuals;i++) {
376 if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
377 return VisualTable[i];
378 }
379 }
380
381 return NULL;
382 }
383
384
385 /**
386 * Try to get an X visual which matches the given arguments.
387 */
388 static XVisualInfo *
389 get_visual( Display *dpy, int scr, unsigned int depth, int xclass )
390 {
391 XVisualInfo temp, *vis;
392 long mask;
393 int n;
394 unsigned int default_depth;
395 int default_class;
396
397 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
398 temp.screen = scr;
399 temp.depth = depth;
400 temp.CLASS = xclass;
401
402 default_depth = DefaultDepth(dpy,scr);
403 default_class = DefaultVisual(dpy,scr)->CLASS;
404
405 if (depth==default_depth && xclass==default_class) {
406 /* try to get root window's visual */
407 temp.visualid = DefaultVisual(dpy,scr)->visualid;
408 mask |= VisualIDMask;
409 }
410
411 vis = XGetVisualInfo( dpy, mask, &temp, &n );
412
413 /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
414 * An SGI Infinite Reality system, for example, can have 30bpp pixels:
415 * 10 bits per color channel. Mesa's limited to a max of 8 bits/channel.
416 */
417 if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
418 if (_mesa_bitcount((GLuint) vis->red_mask ) <= 8 &&
419 _mesa_bitcount((GLuint) vis->green_mask) <= 8 &&
420 _mesa_bitcount((GLuint) vis->blue_mask ) <= 8) {
421 return vis;
422 }
423 else {
424 free((void *) vis);
425 return NULL;
426 }
427 }
428
429 return vis;
430 }
431
432
433 /*
434 * Retrieve the value of the given environment variable and find
435 * the X visual which matches it.
436 * Input: dpy - the display
437 * screen - the screen number
438 * varname - the name of the environment variable
439 * Return: an XVisualInfo pointer to NULL if error.
440 */
441 static XVisualInfo *
442 get_env_visual(Display *dpy, int scr, const char *varname)
443 {
444 char value[100], type[100];
445 int depth, xclass = -1;
446 XVisualInfo *vis;
447
448 if (!getenv( varname )) {
449 return NULL;
450 }
451
452 strncpy( value, getenv(varname), 100 );
453 value[99] = 0;
454
455 sscanf( value, "%s %d", type, &depth );
456
457 if (strcmp(type,"TrueColor")==0) xclass = TrueColor;
458 else if (strcmp(type,"DirectColor")==0) xclass = DirectColor;
459 else if (strcmp(type,"PseudoColor")==0) xclass = PseudoColor;
460 else if (strcmp(type,"StaticColor")==0) xclass = StaticColor;
461 else if (strcmp(type,"GrayScale")==0) xclass = GrayScale;
462 else if (strcmp(type,"StaticGray")==0) xclass = StaticGray;
463
464 if (xclass>-1 && depth>0) {
465 vis = get_visual( dpy, scr, depth, xclass );
466 if (vis) {
467 return vis;
468 }
469 }
470
471 _mesa_warning(NULL, "GLX unable to find visual class=%s, depth=%d.",
472 type, depth);
473
474 return NULL;
475 }
476
477
478
479 /*
480 * Select an X visual which satisfies the RGBA flag and minimum depth.
481 * Input: dpy,
482 * screen - X display and screen number
483 * min_depth - minimum visual depth
484 * preferred_class - preferred GLX visual class or DONT_CARE
485 * Return: pointer to an XVisualInfo or NULL.
486 */
487 static XVisualInfo *
488 choose_x_visual( Display *dpy, int screen, int min_depth,
489 int preferred_class )
490 {
491 XVisualInfo *vis;
492 int xclass, visclass = 0;
493 int depth;
494
495 /* First see if the MESA_RGB_VISUAL env var is defined */
496 vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
497 if (vis) {
498 return vis;
499 }
500 /* Otherwise, search for a suitable visual */
501 if (preferred_class==DONT_CARE) {
502 for (xclass=0;xclass<6;xclass++) {
503 switch (xclass) {
504 case 0: visclass = TrueColor; break;
505 case 1: visclass = DirectColor; break;
506 case 2: visclass = PseudoColor; break;
507 case 3: visclass = StaticColor; break;
508 case 4: visclass = GrayScale; break;
509 case 5: visclass = StaticGray; break;
510 }
511 if (min_depth==0) {
512 /* start with shallowest */
513 for (depth=0;depth<=32;depth++) {
514 if (visclass==TrueColor && depth==8) {
515 /* Special case: try to get 8-bit PseudoColor before */
516 /* 8-bit TrueColor */
517 vis = get_visual( dpy, screen, 8, PseudoColor );
518 if (vis) {
519 return vis;
520 }
521 }
522 vis = get_visual( dpy, screen, depth, visclass );
523 if (vis) {
524 return vis;
525 }
526 }
527 }
528 else {
529 /* start with deepest */
530 for (depth=32;depth>=min_depth;depth--) {
531 if (visclass==TrueColor && depth==8) {
532 /* Special case: try to get 8-bit PseudoColor before */
533 /* 8-bit TrueColor */
534 vis = get_visual( dpy, screen, 8, PseudoColor );
535 if (vis) {
536 return vis;
537 }
538 }
539 vis = get_visual( dpy, screen, depth, visclass );
540 if (vis) {
541 return vis;
542 }
543 }
544 }
545 }
546 }
547 else {
548 /* search for a specific visual class */
549 switch (preferred_class) {
550 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
551 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
552 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
553 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
554 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
555 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
556 default: return NULL;
557 }
558 if (min_depth==0) {
559 /* start with shallowest */
560 for (depth=0;depth<=32;depth++) {
561 vis = get_visual( dpy, screen, depth, visclass );
562 if (vis) {
563 return vis;
564 }
565 }
566 }
567 else {
568 /* start with deepest */
569 for (depth=32;depth>=min_depth;depth--) {
570 vis = get_visual( dpy, screen, depth, visclass );
571 if (vis) {
572 return vis;
573 }
574 }
575 }
576 }
577
578 /* didn't find a visual */
579 return NULL;
580 }
581
582
583
584
585 /**********************************************************************/
586 /*** Display-related functions ***/
587 /**********************************************************************/
588
589
590 /**
591 * Free all XMesaVisuals which are associated with the given display.
592 */
593 static void
594 destroy_visuals_on_display(Display *dpy)
595 {
596 int i;
597 for (i = 0; i < NumVisuals; i++) {
598 if (VisualTable[i]->display == dpy) {
599 /* remove this visual */
600 int j;
601 free(VisualTable[i]);
602 for (j = i; j < NumVisuals - 1; j++)
603 VisualTable[j] = VisualTable[j + 1];
604 NumVisuals--;
605 }
606 }
607 }
608
609
610 /**
611 * Called from XCloseDisplay() to let us free our display-related data.
612 */
613 static int
614 close_display_callback(Display *dpy, XExtCodes *codes)
615 {
616 xmesa_destroy_buffers_on_display(dpy);
617 destroy_visuals_on_display(dpy);
618 xmesa_close_display(dpy);
619 return 0;
620 }
621
622
623 /**
624 * Look for the named extension on given display and return a pointer
625 * to the _XExtension data, or NULL if extension not found.
626 */
627 static _XExtension *
628 lookup_extension(Display *dpy, const char *extName)
629 {
630 _XExtension *ext;
631 for (ext = dpy->ext_procs; ext; ext = ext->next) {
632 if (ext->name && strcmp(ext->name, extName) == 0) {
633 return ext;
634 }
635 }
636 return NULL;
637 }
638
639
640 /**
641 * Whenever we're given a new Display pointer, call this function to
642 * register our close_display_callback function.
643 */
644 static void
645 register_with_display(Display *dpy)
646 {
647 const char *extName = "MesaGLX";
648 _XExtension *ext;
649
650 ext = lookup_extension(dpy, extName);
651 if (!ext) {
652 XExtCodes *c = XAddExtension(dpy);
653 ext = dpy->ext_procs; /* new extension is at head of list */
654 assert(c->extension == ext->codes.extension);
655 (void) c;
656 ext->name = strdup(extName);
657 ext->close_display = close_display_callback;
658 }
659 }
660
661
662 /**
663 * Fake an error.
664 */
665 static int
666 generate_error(Display *dpy,
667 unsigned char error_code,
668 XID resourceid,
669 unsigned char minor_code,
670 Bool core)
671 {
672 XErrorHandler handler;
673 int major_opcode;
674 int first_event;
675 int first_error;
676 XEvent event;
677
678 handler = XSetErrorHandler(NULL);
679 XSetErrorHandler(handler);
680 if (!handler) {
681 return 0;
682 }
683
684 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_opcode, &first_event, &first_error)) {
685 major_opcode = 0;
686 first_event = 0;
687 first_error = 0;
688 }
689
690 if (!core) {
691 error_code += first_error;
692 }
693
694 memset(&event, 0, sizeof event);
695
696 event.xerror.type = X_Error;
697 event.xerror.display = dpy;
698 event.xerror.resourceid = resourceid;
699 event.xerror.serial = NextRequest(dpy) - 1;
700 event.xerror.error_code = error_code;
701 event.xerror.request_code = major_opcode;
702 event.xerror.minor_code = minor_code;
703
704 return handler(dpy, &event.xerror);
705 }
706
707
708 /**********************************************************************/
709 /*** Begin Fake GLX API Functions ***/
710 /**********************************************************************/
711
712
713 /**
714 * Helper used by glXChooseVisual and glXChooseFBConfig.
715 * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
716 * the later.
717 * In either case, the attribute list is terminated with the value 'None'.
718 */
719 static XMesaVisual
720 choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
721 {
722 const GLboolean rgbModeDefault = fbConfig;
723 const int *parselist;
724 XVisualInfo *vis;
725 int min_red=0, min_green=0, min_blue=0;
726 GLboolean rgb_flag = rgbModeDefault;
727 GLboolean alpha_flag = GL_FALSE;
728 GLboolean double_flag = GL_FALSE;
729 GLboolean stereo_flag = GL_FALSE;
730 GLint depth_size = 0;
731 GLint stencil_size = 0;
732 GLint accumRedSize = 0;
733 GLint accumGreenSize = 0;
734 GLint accumBlueSize = 0;
735 GLint accumAlphaSize = 0;
736 int level = 0;
737 int visual_type = DONT_CARE;
738 GLint caveat = DONT_CARE;
739 XMesaVisual xmvis = NULL;
740 int desiredVisualID = -1;
741 int numAux = 0;
742
743 xmesa_init( dpy );
744
745 parselist = list;
746
747 while (*parselist) {
748
749 if (fbConfig &&
750 parselist[1] == GLX_DONT_CARE &&
751 parselist[0] != GLX_LEVEL) {
752 /* For glXChooseFBConfig(), skip attributes whose value is
753 * GLX_DONT_CARE, unless it's GLX_LEVEL (which can legitimately be
754 * a negative value).
755 *
756 * From page 17 (23 of the pdf) of the GLX 1.4 spec:
757 * GLX DONT CARE may be specified for all attributes except GLX LEVEL.
758 */
759 parselist += 2;
760 continue;
761 }
762
763 switch (*parselist) {
764 case GLX_USE_GL:
765 if (fbConfig) {
766 /* invalid token */
767 return NULL;
768 }
769 else {
770 /* skip */
771 parselist++;
772 }
773 break;
774 case GLX_BUFFER_SIZE:
775 parselist++;
776 parselist++;
777 break;
778 case GLX_LEVEL:
779 parselist++;
780 level = *parselist++;
781 break;
782 case GLX_RGBA:
783 if (fbConfig) {
784 /* invalid token */
785 return NULL;
786 }
787 else {
788 rgb_flag = GL_TRUE;
789 parselist++;
790 }
791 break;
792 case GLX_DOUBLEBUFFER:
793 parselist++;
794 if (fbConfig) {
795 double_flag = *parselist++;
796 }
797 else {
798 double_flag = GL_TRUE;
799 }
800 break;
801 case GLX_STEREO:
802 parselist++;
803 if (fbConfig) {
804 stereo_flag = *parselist++;
805 }
806 else {
807 stereo_flag = GL_TRUE;
808 }
809 break;
810 case GLX_AUX_BUFFERS:
811 parselist++;
812 numAux = *parselist++;
813 if (numAux > MAX_AUX_BUFFERS)
814 return NULL;
815 break;
816 case GLX_RED_SIZE:
817 parselist++;
818 min_red = *parselist++;
819 break;
820 case GLX_GREEN_SIZE:
821 parselist++;
822 min_green = *parselist++;
823 break;
824 case GLX_BLUE_SIZE:
825 parselist++;
826 min_blue = *parselist++;
827 break;
828 case GLX_ALPHA_SIZE:
829 parselist++;
830 {
831 GLint size = *parselist++;
832 alpha_flag = size ? GL_TRUE : GL_FALSE;
833 }
834 break;
835 case GLX_DEPTH_SIZE:
836 parselist++;
837 depth_size = *parselist++;
838 break;
839 case GLX_STENCIL_SIZE:
840 parselist++;
841 stencil_size = *parselist++;
842 break;
843 case GLX_ACCUM_RED_SIZE:
844 parselist++;
845 {
846 GLint size = *parselist++;
847 accumRedSize = MAX2( accumRedSize, size );
848 }
849 break;
850 case GLX_ACCUM_GREEN_SIZE:
851 parselist++;
852 {
853 GLint size = *parselist++;
854 accumGreenSize = MAX2( accumGreenSize, size );
855 }
856 break;
857 case GLX_ACCUM_BLUE_SIZE:
858 parselist++;
859 {
860 GLint size = *parselist++;
861 accumBlueSize = MAX2( accumBlueSize, size );
862 }
863 break;
864 case GLX_ACCUM_ALPHA_SIZE:
865 parselist++;
866 {
867 GLint size = *parselist++;
868 accumAlphaSize = MAX2( accumAlphaSize, size );
869 }
870 break;
871
872 /*
873 * GLX_EXT_visual_info extension
874 */
875 case GLX_X_VISUAL_TYPE_EXT:
876 parselist++;
877 visual_type = *parselist++;
878 break;
879 case GLX_TRANSPARENT_TYPE_EXT:
880 parselist++;
881 parselist++;
882 break;
883 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
884 parselist++;
885 parselist++;
886 break;
887 case GLX_TRANSPARENT_RED_VALUE_EXT:
888 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
889 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
890 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
891 /* ignore */
892 parselist++;
893 parselist++;
894 break;
895
896 /*
897 * GLX_EXT_visual_info extension
898 */
899 case GLX_VISUAL_CAVEAT_EXT:
900 parselist++;
901 caveat = *parselist++; /* ignored for now */
902 break;
903
904 /*
905 * GLX_ARB_multisample
906 */
907 case GLX_SAMPLE_BUFFERS_ARB:
908 case GLX_SAMPLES_ARB:
909 parselist++;
910 if (*parselist++ != 0) {
911 /* ms not supported */
912 return NULL;
913 }
914 break;
915
916 /*
917 * FBConfig attribs.
918 */
919 case GLX_RENDER_TYPE:
920 if (!fbConfig)
921 return NULL;
922 parselist++;
923 if (*parselist & GLX_RGBA_BIT) {
924 rgb_flag = GL_TRUE;
925 }
926 else if (*parselist & GLX_COLOR_INDEX_BIT) {
927 rgb_flag = GL_FALSE;
928 }
929 else if (*parselist == 0) {
930 rgb_flag = GL_TRUE;
931 }
932 parselist++;
933 break;
934 case GLX_DRAWABLE_TYPE:
935 if (!fbConfig)
936 return NULL;
937 parselist++;
938 if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
939 return NULL; /* bad bit */
940 }
941 parselist++;
942 break;
943 case GLX_FBCONFIG_ID:
944 case GLX_VISUAL_ID:
945 if (!fbConfig)
946 return NULL;
947 parselist++;
948 desiredVisualID = *parselist++;
949 break;
950 case GLX_X_RENDERABLE:
951 case GLX_MAX_PBUFFER_WIDTH:
952 case GLX_MAX_PBUFFER_HEIGHT:
953 case GLX_MAX_PBUFFER_PIXELS:
954 if (!fbConfig)
955 return NULL; /* invalid config option */
956 parselist += 2; /* ignore the parameter */
957 break;
958
959 case GLX_BIND_TO_TEXTURE_RGB_EXT:
960 parselist++; /*skip*/
961 break;
962 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
963 parselist++; /*skip*/
964 break;
965 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
966 parselist++; /*skip*/
967 break;
968 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
969 parselist++;
970 if (*parselist & ~(GLX_TEXTURE_1D_BIT_EXT |
971 GLX_TEXTURE_2D_BIT_EXT |
972 GLX_TEXTURE_RECTANGLE_BIT_EXT)) {
973 /* invalid bit */
974 return NULL;
975 }
976 break;
977 case GLX_Y_INVERTED_EXT:
978 parselist++; /*skip*/
979 break;
980
981 case None:
982 /* end of list */
983 break;
984
985 default:
986 /* undefined attribute */
987 _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
988 *parselist);
989 return NULL;
990 }
991 }
992
993 (void) caveat;
994
995
996 /*
997 * Since we're only simulating the GLX extension this function will never
998 * find any real GL visuals. Instead, all we can do is try to find an RGB
999 * or CI visual of appropriate depth. Other requested attributes such as
1000 * double buffering, depth buffer, etc. will be associated with the X
1001 * visual and stored in the VisualTable[].
1002 */
1003 if (desiredVisualID != -1) {
1004 /* try to get a specific visual, by visualID */
1005 XVisualInfo temp;
1006 int n;
1007 temp.visualid = desiredVisualID;
1008 temp.screen = screen;
1009 vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
1010 if (vis) {
1011 /* give the visual some useful GLX attributes */
1012 double_flag = GL_TRUE;
1013 rgb_flag = GL_TRUE;
1014 }
1015 }
1016 else if (level==0) {
1017 /* normal color planes */
1018 /* Get an RGB visual */
1019 int min_rgb = min_red + min_green + min_blue;
1020 if (min_rgb>1 && min_rgb<8) {
1021 /* a special case to be sure we can get a monochrome visual */
1022 min_rgb = 1;
1023 }
1024 vis = choose_x_visual( dpy, screen, min_rgb, visual_type );
1025 }
1026 else {
1027 _mesa_warning(NULL, "overlay not supported");
1028 return NULL;
1029 }
1030
1031 if (vis) {
1032 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1033 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1034 * largest depth buffer size, which is 32bits/value. Instead, we
1035 * return 16 to maintain performance with earlier versions of Mesa.
1036 */
1037 if (stencil_size > 0)
1038 depth_size = 24; /* if Z and stencil, always use 24+8 format */
1039 else if (depth_size > 24)
1040 depth_size = 32;
1041 else if (depth_size > 16)
1042 depth_size = 24;
1043 else if (depth_size > 0) {
1044 depth_size = default_depth_bits();
1045 }
1046
1047 if (!alpha_flag) {
1048 alpha_flag = default_alpha_bits() > 0;
1049 }
1050
1051 /* we only support one size of stencil and accum buffers. */
1052 if (stencil_size > 0)
1053 stencil_size = 8;
1054
1055 if (accumRedSize > 0 ||
1056 accumGreenSize > 0 ||
1057 accumBlueSize > 0 ||
1058 accumAlphaSize > 0) {
1059
1060 accumRedSize =
1061 accumGreenSize =
1062 accumBlueSize = default_accum_bits();
1063
1064 accumAlphaSize = alpha_flag ? accumRedSize : 0;
1065 }
1066
1067 xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1068 stereo_flag, depth_size, stencil_size,
1069 accumRedSize, accumGreenSize,
1070 accumBlueSize, accumAlphaSize, level, numAux );
1071 }
1072
1073 return xmvis;
1074 }
1075
1076
1077 PUBLIC XVisualInfo *
1078 glXChooseVisual( Display *dpy, int screen, int *list )
1079 {
1080 XMesaVisual xmvis;
1081
1082 /* register ourselves as an extension on this display */
1083 register_with_display(dpy);
1084
1085 xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1086 if (xmvis) {
1087 /* create a new vishandle - the cached one may be stale */
1088 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1089 if (xmvis->vishandle) {
1090 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1091 }
1092 return xmvis->vishandle;
1093 }
1094 else
1095 return NULL;
1096 }
1097
1098
1099 /**
1100 * Helper function used by other glXCreateContext functions.
1101 */
1102 static GLXContext
1103 create_context(Display *dpy, XMesaVisual xmvis,
1104 XMesaContext shareCtx, Bool direct,
1105 unsigned major, unsigned minor,
1106 unsigned profileMask, unsigned contextFlags)
1107 {
1108 GLXContext glxCtx;
1109
1110 if (!dpy || !xmvis)
1111 return 0;
1112
1113 glxCtx = CALLOC_STRUCT(__GLXcontextRec);
1114 if (!glxCtx)
1115 return 0;
1116
1117 /* deallocate unused windows/buffers */
1118 #if 0
1119 XMesaGarbageCollect();
1120 #endif
1121
1122 glxCtx->xmesaContext = XMesaCreateContext(xmvis, shareCtx, major, minor,
1123 profileMask, contextFlags);
1124 if (!glxCtx->xmesaContext) {
1125 free(glxCtx);
1126 return NULL;
1127 }
1128
1129 glxCtx->isDirect = DEFAULT_DIRECT;
1130 glxCtx->currentDpy = dpy;
1131 glxCtx->xid = (XID) glxCtx; /* self pointer */
1132
1133 return glxCtx;
1134 }
1135
1136
1137 PUBLIC GLXContext
1138 glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1139 GLXContext shareCtx, Bool direct )
1140 {
1141 XMesaVisual xmvis;
1142
1143 xmvis = find_glx_visual( dpy, visinfo );
1144 if (!xmvis) {
1145 /* This visual wasn't found with glXChooseVisual() */
1146 xmvis = create_glx_visual( dpy, visinfo );
1147 if (!xmvis) {
1148 /* unusable visual */
1149 return NULL;
1150 }
1151 }
1152
1153 return create_context(dpy, xmvis,
1154 shareCtx ? shareCtx->xmesaContext : NULL,
1155 direct,
1156 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
1157 }
1158
1159
1160 /* XXX these may have to be removed due to thread-safety issues. */
1161 static GLXContext MakeCurrent_PrevContext = 0;
1162 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1163 static GLXDrawable MakeCurrent_PrevReadable = 0;
1164 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1165 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1166
1167
1168 /* GLX 1.3 and later */
1169 PUBLIC Bool
1170 glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1171 GLXDrawable read, GLXContext ctx )
1172 {
1173 GLXContext glxCtx = ctx;
1174 static boolean firsttime = 1, no_rast = 0;
1175
1176 if (firsttime) {
1177 no_rast = getenv("SP_NO_RAST") != NULL;
1178 firsttime = 0;
1179 }
1180
1181 if (ctx && draw && read) {
1182 XMesaBuffer drawBuffer, readBuffer;
1183 XMesaContext xmctx = glxCtx->xmesaContext;
1184
1185 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1186 if (ctx == MakeCurrent_PrevContext
1187 && draw == MakeCurrent_PrevDrawable) {
1188 drawBuffer = MakeCurrent_PrevDrawBuffer;
1189 }
1190 else {
1191 drawBuffer = XMesaFindBuffer( dpy, draw );
1192 }
1193 if (!drawBuffer) {
1194 /* drawable must be a new window! */
1195 drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1196 if (!drawBuffer) {
1197 /* Out of memory, or context/drawable depth mismatch */
1198 return False;
1199 }
1200 }
1201
1202 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1203 if (ctx == MakeCurrent_PrevContext
1204 && read == MakeCurrent_PrevReadable) {
1205 readBuffer = MakeCurrent_PrevReadBuffer;
1206 }
1207 else {
1208 readBuffer = XMesaFindBuffer( dpy, read );
1209 }
1210 if (!readBuffer) {
1211 /* drawable must be a new window! */
1212 readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1213 if (!readBuffer) {
1214 /* Out of memory, or context/drawable depth mismatch */
1215 return False;
1216 }
1217 }
1218
1219 if (no_rast &&
1220 MakeCurrent_PrevContext == ctx &&
1221 MakeCurrent_PrevDrawable == draw &&
1222 MakeCurrent_PrevReadable == read &&
1223 MakeCurrent_PrevDrawBuffer == drawBuffer &&
1224 MakeCurrent_PrevReadBuffer == readBuffer)
1225 return True;
1226
1227 MakeCurrent_PrevContext = ctx;
1228 MakeCurrent_PrevDrawable = draw;
1229 MakeCurrent_PrevReadable = read;
1230 MakeCurrent_PrevDrawBuffer = drawBuffer;
1231 MakeCurrent_PrevReadBuffer = readBuffer;
1232
1233 /* Now make current! */
1234 if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1235 ctx->currentDpy = dpy;
1236 ctx->currentDrawable = draw;
1237 ctx->currentReadable = read;
1238 SetCurrentContext(ctx);
1239 return True;
1240 }
1241 else {
1242 return False;
1243 }
1244 }
1245 else if (!ctx && !draw && !read) {
1246 /* release current context w/out assigning new one. */
1247 XMesaMakeCurrent2( NULL, NULL, NULL );
1248 MakeCurrent_PrevContext = 0;
1249 MakeCurrent_PrevDrawable = 0;
1250 MakeCurrent_PrevReadable = 0;
1251 MakeCurrent_PrevDrawBuffer = 0;
1252 MakeCurrent_PrevReadBuffer = 0;
1253 SetCurrentContext(NULL);
1254 return True;
1255 }
1256 else {
1257 /* The args must either all be non-zero or all zero.
1258 * This is an error.
1259 */
1260 return False;
1261 }
1262 }
1263
1264
1265 PUBLIC Bool
1266 glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1267 {
1268 return glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1269 }
1270
1271
1272 PUBLIC GLXContext
1273 glXGetCurrentContext(void)
1274 {
1275 return GetCurrentContext();
1276 }
1277
1278
1279 PUBLIC Display *
1280 glXGetCurrentDisplay(void)
1281 {
1282 GLXContext glxCtx = glXGetCurrentContext();
1283
1284 return glxCtx ? glxCtx->currentDpy : NULL;
1285 }
1286
1287
1288 PUBLIC Display *
1289 glXGetCurrentDisplayEXT(void)
1290 {
1291 return glXGetCurrentDisplay();
1292 }
1293
1294
1295 PUBLIC GLXDrawable
1296 glXGetCurrentDrawable(void)
1297 {
1298 GLXContext gc = glXGetCurrentContext();
1299 return gc ? gc->currentDrawable : 0;
1300 }
1301
1302
1303 PUBLIC GLXDrawable
1304 glXGetCurrentReadDrawable(void)
1305 {
1306 GLXContext gc = glXGetCurrentContext();
1307 return gc ? gc->currentReadable : 0;
1308 }
1309
1310
1311 PUBLIC GLXDrawable
1312 glXGetCurrentReadDrawableSGI(void)
1313 {
1314 return glXGetCurrentReadDrawable();
1315 }
1316
1317
1318 PUBLIC GLXPixmap
1319 glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1320 {
1321 XMesaVisual v;
1322 XMesaBuffer b;
1323
1324 v = find_glx_visual( dpy, visinfo );
1325 if (!v) {
1326 v = create_glx_visual( dpy, visinfo );
1327 if (!v) {
1328 /* unusable visual */
1329 return 0;
1330 }
1331 }
1332
1333 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1334 if (!b) {
1335 return 0;
1336 }
1337 return b->ws.drawable;
1338 }
1339
1340
1341 /*** GLX_MESA_pixmap_colormap ***/
1342
1343 PUBLIC GLXPixmap
1344 glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1345 Pixmap pixmap, Colormap cmap )
1346 {
1347 XMesaVisual v;
1348 XMesaBuffer b;
1349
1350 v = find_glx_visual( dpy, visinfo );
1351 if (!v) {
1352 v = create_glx_visual( dpy, visinfo );
1353 if (!v) {
1354 /* unusable visual */
1355 return 0;
1356 }
1357 }
1358
1359 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1360 if (!b) {
1361 return 0;
1362 }
1363 return b->ws.drawable;
1364 }
1365
1366
1367 PUBLIC void
1368 glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1369 {
1370 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1371 if (b) {
1372 XMesaDestroyBuffer(b);
1373 }
1374 else if (getenv("MESA_DEBUG")) {
1375 _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1376 }
1377 }
1378
1379
1380 PUBLIC void
1381 glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1382 unsigned long mask )
1383 {
1384 XMesaContext xm_src = src->xmesaContext;
1385 XMesaContext xm_dst = dst->xmesaContext;
1386 (void) dpy;
1387 if (MakeCurrent_PrevContext == src) {
1388 glFlush();
1389 }
1390 XMesaCopyContext(xm_src, xm_dst, mask);
1391 }
1392
1393
1394 PUBLIC Bool
1395 glXQueryExtension( Display *dpy, int *errorBase, int *eventBase )
1396 {
1397 int op, ev, err;
1398 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1399 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &op, &ev, &err))
1400 ev = err = 0;
1401 if (errorBase)
1402 *errorBase = err;
1403 if (eventBase)
1404 *eventBase = ev;
1405 return True; /* we're faking GLX so always return success */
1406 }
1407
1408
1409 PUBLIC void
1410 glXDestroyContext( Display *dpy, GLXContext ctx )
1411 {
1412 if (ctx) {
1413 GLXContext glxCtx = ctx;
1414 (void) dpy;
1415 MakeCurrent_PrevContext = 0;
1416 MakeCurrent_PrevDrawable = 0;
1417 MakeCurrent_PrevReadable = 0;
1418 MakeCurrent_PrevDrawBuffer = 0;
1419 MakeCurrent_PrevReadBuffer = 0;
1420 XMesaDestroyContext( glxCtx->xmesaContext );
1421 XMesaGarbageCollect();
1422 free(glxCtx);
1423 }
1424 }
1425
1426
1427 PUBLIC Bool
1428 glXIsDirect( Display *dpy, GLXContext ctx )
1429 {
1430 return ctx ? ctx->isDirect : False;
1431 }
1432
1433
1434
1435 PUBLIC void
1436 glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1437 {
1438 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1439 static boolean firsttime = 1, no_rast = 0;
1440
1441 if (firsttime) {
1442 no_rast = getenv("SP_NO_RAST") != NULL;
1443 firsttime = 0;
1444 }
1445
1446 if (no_rast)
1447 return;
1448
1449 if (buffer) {
1450 XMesaSwapBuffers(buffer);
1451 }
1452 else if (getenv("MESA_DEBUG")) {
1453 _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1454 (int) drawable);
1455 }
1456 }
1457
1458
1459
1460 /*** GLX_MESA_copy_sub_buffer ***/
1461
1462 PUBLIC void
1463 glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
1464 int x, int y, int width, int height)
1465 {
1466 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1467 if (buffer) {
1468 XMesaCopySubBuffer(buffer, x, y, width, height);
1469 }
1470 else if (getenv("MESA_DEBUG")) {
1471 _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1472 }
1473 }
1474
1475
1476 PUBLIC Bool
1477 glXQueryVersion( Display *dpy, int *maj, int *min )
1478 {
1479 (void) dpy;
1480 /* Return GLX version, not Mesa version */
1481 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1482 *maj = CLIENT_MAJOR_VERSION;
1483 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1484 return True;
1485 }
1486
1487
1488 /*
1489 * Query the GLX attributes of the given XVisualInfo.
1490 */
1491 static int
1492 get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1493 {
1494 assert(xmvis);
1495 switch(attrib) {
1496 case GLX_USE_GL:
1497 if (fbconfig)
1498 return GLX_BAD_ATTRIBUTE;
1499 *value = (int) True;
1500 return 0;
1501 case GLX_BUFFER_SIZE:
1502 *value = xmvis->visinfo->depth;
1503 return 0;
1504 case GLX_LEVEL:
1505 *value = xmvis->mesa_visual.level;
1506 return 0;
1507 case GLX_RGBA:
1508 if (fbconfig)
1509 return GLX_BAD_ATTRIBUTE;
1510 if (xmvis->mesa_visual.rgbMode) {
1511 *value = True;
1512 }
1513 else {
1514 *value = False;
1515 }
1516 return 0;
1517 case GLX_DOUBLEBUFFER:
1518 *value = (int) xmvis->mesa_visual.doubleBufferMode;
1519 return 0;
1520 case GLX_STEREO:
1521 *value = (int) xmvis->mesa_visual.stereoMode;
1522 return 0;
1523 case GLX_AUX_BUFFERS:
1524 *value = xmvis->mesa_visual.numAuxBuffers;
1525 return 0;
1526 case GLX_RED_SIZE:
1527 *value = xmvis->mesa_visual.redBits;
1528 return 0;
1529 case GLX_GREEN_SIZE:
1530 *value = xmvis->mesa_visual.greenBits;
1531 return 0;
1532 case GLX_BLUE_SIZE:
1533 *value = xmvis->mesa_visual.blueBits;
1534 return 0;
1535 case GLX_ALPHA_SIZE:
1536 *value = xmvis->mesa_visual.alphaBits;
1537 return 0;
1538 case GLX_DEPTH_SIZE:
1539 *value = xmvis->mesa_visual.depthBits;
1540 return 0;
1541 case GLX_STENCIL_SIZE:
1542 *value = xmvis->mesa_visual.stencilBits;
1543 return 0;
1544 case GLX_ACCUM_RED_SIZE:
1545 *value = xmvis->mesa_visual.accumRedBits;
1546 return 0;
1547 case GLX_ACCUM_GREEN_SIZE:
1548 *value = xmvis->mesa_visual.accumGreenBits;
1549 return 0;
1550 case GLX_ACCUM_BLUE_SIZE:
1551 *value = xmvis->mesa_visual.accumBlueBits;
1552 return 0;
1553 case GLX_ACCUM_ALPHA_SIZE:
1554 *value = xmvis->mesa_visual.accumAlphaBits;
1555 return 0;
1556
1557 /*
1558 * GLX_EXT_visual_info extension
1559 */
1560 case GLX_X_VISUAL_TYPE_EXT:
1561 switch (xmvis->visinfo->CLASS) {
1562 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1563 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1564 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1565 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1566 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1567 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1568 }
1569 return 0;
1570 case GLX_TRANSPARENT_TYPE_EXT:
1571 /* normal planes */
1572 *value = GLX_NONE_EXT;
1573 return 0;
1574 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1575 /* undefined */
1576 return 0;
1577 case GLX_TRANSPARENT_RED_VALUE_EXT:
1578 /* undefined */
1579 return 0;
1580 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1581 /* undefined */
1582 return 0;
1583 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1584 /* undefined */
1585 return 0;
1586 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1587 /* undefined */
1588 return 0;
1589
1590 /*
1591 * GLX_EXT_visual_info extension
1592 */
1593 case GLX_VISUAL_CAVEAT_EXT:
1594 /* test for zero, just in case */
1595 if (xmvis->mesa_visual.visualRating > 0)
1596 *value = xmvis->mesa_visual.visualRating;
1597 else
1598 *value = GLX_NONE_EXT;
1599 return 0;
1600
1601 /*
1602 * GLX_ARB_multisample
1603 */
1604 case GLX_SAMPLE_BUFFERS_ARB:
1605 *value = 0;
1606 return 0;
1607 case GLX_SAMPLES_ARB:
1608 *value = 0;
1609 return 0;
1610
1611 /*
1612 * For FBConfigs:
1613 */
1614 case GLX_SCREEN_EXT:
1615 if (!fbconfig)
1616 return GLX_BAD_ATTRIBUTE;
1617 *value = xmvis->visinfo->screen;
1618 break;
1619 case GLX_DRAWABLE_TYPE: /*SGIX too */
1620 if (!fbconfig)
1621 return GLX_BAD_ATTRIBUTE;
1622 *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1623 break;
1624 case GLX_RENDER_TYPE_SGIX:
1625 if (!fbconfig)
1626 return GLX_BAD_ATTRIBUTE;
1627 if (xmvis->mesa_visual.rgbMode)
1628 *value = GLX_RGBA_BIT;
1629 else
1630 *value = GLX_COLOR_INDEX_BIT;
1631 break;
1632 case GLX_X_RENDERABLE_SGIX:
1633 if (!fbconfig)
1634 return GLX_BAD_ATTRIBUTE;
1635 *value = True; /* XXX really? */
1636 break;
1637 case GLX_FBCONFIG_ID_SGIX:
1638 if (!fbconfig)
1639 return GLX_BAD_ATTRIBUTE;
1640 *value = xmvis->visinfo->visualid;
1641 break;
1642 case GLX_MAX_PBUFFER_WIDTH:
1643 if (!fbconfig)
1644 return GLX_BAD_ATTRIBUTE;
1645 /* XXX should be same as ctx->Const.MaxRenderbufferSize */
1646 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1647 break;
1648 case GLX_MAX_PBUFFER_HEIGHT:
1649 if (!fbconfig)
1650 return GLX_BAD_ATTRIBUTE;
1651 *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1652 break;
1653 case GLX_MAX_PBUFFER_PIXELS:
1654 if (!fbconfig)
1655 return GLX_BAD_ATTRIBUTE;
1656 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1657 DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1658 break;
1659 case GLX_VISUAL_ID:
1660 if (!fbconfig)
1661 return GLX_BAD_ATTRIBUTE;
1662 *value = xmvis->visinfo->visualid;
1663 break;
1664
1665 case GLX_BIND_TO_TEXTURE_RGB_EXT:
1666 *value = True; /*XXX*/
1667 break;
1668 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1669 /* XXX review */
1670 *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1671 break;
1672 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1673 *value = True; /*XXX*/
1674 break;
1675 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1676 *value = (GLX_TEXTURE_1D_BIT_EXT |
1677 GLX_TEXTURE_2D_BIT_EXT |
1678 GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1679 break;
1680 case GLX_Y_INVERTED_EXT:
1681 *value = True; /*XXX*/
1682 break;
1683
1684 default:
1685 return GLX_BAD_ATTRIBUTE;
1686 }
1687 return Success;
1688 }
1689
1690
1691 PUBLIC int
1692 glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1693 int attrib, int *value )
1694 {
1695 XMesaVisual xmvis;
1696 int k;
1697 if (!dpy || !visinfo)
1698 return GLX_BAD_ATTRIBUTE;
1699
1700 xmvis = find_glx_visual( dpy, visinfo );
1701 if (!xmvis) {
1702 /* this visual wasn't obtained with glXChooseVisual */
1703 xmvis = create_glx_visual( dpy, visinfo );
1704 if (!xmvis) {
1705 /* this visual can't be used for GL rendering */
1706 if (attrib==GLX_USE_GL) {
1707 *value = (int) False;
1708 return 0;
1709 }
1710 else {
1711 return GLX_BAD_VISUAL;
1712 }
1713 }
1714 }
1715
1716 k = get_config(xmvis, attrib, value, GL_FALSE);
1717 return k;
1718 }
1719
1720
1721 PUBLIC void
1722 glXWaitGL( void )
1723 {
1724 XMesaContext xmesa = XMesaGetCurrentContext();
1725 XMesaFlush( xmesa );
1726 }
1727
1728
1729
1730 PUBLIC void
1731 glXWaitX( void )
1732 {
1733 XMesaContext xmesa = XMesaGetCurrentContext();
1734 XMesaFlush( xmesa );
1735 }
1736
1737
1738 static const char *
1739 get_extensions( void )
1740 {
1741 return EXTENSIONS;
1742 }
1743
1744
1745
1746 /* GLX 1.1 and later */
1747 PUBLIC const char *
1748 glXQueryExtensionsString( Display *dpy, int screen )
1749 {
1750 (void) dpy;
1751 (void) screen;
1752 return get_extensions();
1753 }
1754
1755
1756
1757 /* GLX 1.1 and later */
1758 PUBLIC const char *
1759 glXQueryServerString( Display *dpy, int screen, int name )
1760 {
1761 static char version[1000];
1762 sprintf(version, "%d.%d %s",
1763 SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, xmesa_get_name());
1764
1765 (void) dpy;
1766 (void) screen;
1767
1768 switch (name) {
1769 case GLX_EXTENSIONS:
1770 return get_extensions();
1771 case GLX_VENDOR:
1772 return VENDOR;
1773 case GLX_VERSION:
1774 return version;
1775 default:
1776 return NULL;
1777 }
1778 }
1779
1780
1781
1782 /* GLX 1.1 and later */
1783 PUBLIC const char *
1784 glXGetClientString( Display *dpy, int name )
1785 {
1786 static char version[1000];
1787 sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
1788 CLIENT_MINOR_VERSION, xmesa_get_name());
1789
1790 (void) dpy;
1791
1792 switch (name) {
1793 case GLX_EXTENSIONS:
1794 return get_extensions();
1795 case GLX_VENDOR:
1796 return VENDOR;
1797 case GLX_VERSION:
1798 return version;
1799 default:
1800 return NULL;
1801 }
1802 }
1803
1804
1805
1806 /*
1807 * GLX 1.3 and later
1808 */
1809
1810
1811 PUBLIC int
1812 glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1813 int attribute, int *value)
1814 {
1815 XMesaVisual v = (XMesaVisual) config;
1816 (void) dpy;
1817 (void) config;
1818
1819 if (!dpy || !config || !value)
1820 return -1;
1821
1822 return get_config(v, attribute, value, GL_TRUE);
1823 }
1824
1825
1826 PUBLIC GLXFBConfig *
1827 glXGetFBConfigs( Display *dpy, int screen, int *nelements )
1828 {
1829 XVisualInfo *visuals, visTemplate;
1830 const long visMask = VisualScreenMask;
1831 int i;
1832
1833 /* Get list of all X visuals */
1834 visTemplate.screen = screen;
1835 visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
1836 if (*nelements > 0) {
1837 XMesaVisual *results = malloc(*nelements * sizeof(XMesaVisual));
1838 if (!results) {
1839 *nelements = 0;
1840 return NULL;
1841 }
1842 for (i = 0; i < *nelements; i++) {
1843 results[i] = create_glx_visual(dpy, visuals + i);
1844 if (!results[i]) {
1845 *nelements = i;
1846 break;
1847 }
1848 }
1849 return (GLXFBConfig *) results;
1850 }
1851 return NULL;
1852 }
1853
1854
1855 PUBLIC GLXFBConfig *
1856 glXChooseFBConfig(Display *dpy, int screen,
1857 const int *attribList, int *nitems)
1858 {
1859 XMesaVisual xmvis;
1860
1861 /* register ourselves as an extension on this display */
1862 register_with_display(dpy);
1863
1864 if (!attribList || !attribList[0]) {
1865 /* return list of all configs (per GLX_SGIX_fbconfig spec) */
1866 return glXGetFBConfigs(dpy, screen, nitems);
1867 }
1868
1869 xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
1870 if (xmvis) {
1871 GLXFBConfig *config = malloc(sizeof(XMesaVisual));
1872 if (!config) {
1873 *nitems = 0;
1874 return NULL;
1875 }
1876 *nitems = 1;
1877 config[0] = (GLXFBConfig) xmvis;
1878 return (GLXFBConfig *) config;
1879 }
1880 else {
1881 *nitems = 0;
1882 return NULL;
1883 }
1884 }
1885
1886
1887 PUBLIC XVisualInfo *
1888 glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1889 {
1890 if (dpy && config) {
1891 XMesaVisual xmvis = (XMesaVisual) config;
1892 #if 0
1893 return xmvis->vishandle;
1894 #else
1895 /* create a new vishandle - the cached one may be stale */
1896 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1897 if (xmvis->vishandle) {
1898 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1899 }
1900 return xmvis->vishandle;
1901 #endif
1902 }
1903 else {
1904 return NULL;
1905 }
1906 }
1907
1908
1909 PUBLIC GLXWindow
1910 glXCreateWindow(Display *dpy, GLXFBConfig config, Window win,
1911 const int *attribList)
1912 {
1913 XMesaVisual xmvis = (XMesaVisual) config;
1914 XMesaBuffer xmbuf;
1915 if (!xmvis)
1916 return 0;
1917
1918 xmbuf = XMesaCreateWindowBuffer(xmvis, win);
1919 if (!xmbuf)
1920 return 0;
1921
1922 (void) dpy;
1923 (void) attribList; /* Ignored in GLX 1.3 */
1924
1925 return win; /* A hack for now */
1926 }
1927
1928
1929 PUBLIC void
1930 glXDestroyWindow( Display *dpy, GLXWindow window )
1931 {
1932 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable) window);
1933 if (b)
1934 XMesaDestroyBuffer(b);
1935 /* don't destroy X window */
1936 }
1937
1938
1939 /* XXX untested */
1940 PUBLIC GLXPixmap
1941 glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap,
1942 const int *attribList)
1943 {
1944 XMesaVisual v = (XMesaVisual) config;
1945 XMesaBuffer b;
1946 const int *attr;
1947 int target = 0, format = 0, mipmap = 0;
1948 int value;
1949
1950 if (!dpy || !config || !pixmap)
1951 return 0;
1952
1953 for (attr = attribList; attr && *attr; attr++) {
1954 switch (*attr) {
1955 case GLX_TEXTURE_FORMAT_EXT:
1956 attr++;
1957 switch (*attr) {
1958 case GLX_TEXTURE_FORMAT_NONE_EXT:
1959 case GLX_TEXTURE_FORMAT_RGB_EXT:
1960 case GLX_TEXTURE_FORMAT_RGBA_EXT:
1961 format = *attr;
1962 break;
1963 default:
1964 /* error */
1965 return 0;
1966 }
1967 break;
1968 case GLX_TEXTURE_TARGET_EXT:
1969 attr++;
1970 switch (*attr) {
1971 case GLX_TEXTURE_1D_EXT:
1972 case GLX_TEXTURE_2D_EXT:
1973 case GLX_TEXTURE_RECTANGLE_EXT:
1974 target = *attr;
1975 break;
1976 default:
1977 /* error */
1978 return 0;
1979 }
1980 break;
1981 case GLX_MIPMAP_TEXTURE_EXT:
1982 attr++;
1983 if (*attr)
1984 mipmap = 1;
1985 break;
1986 default:
1987 /* error */
1988 return 0;
1989 }
1990 }
1991
1992 if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
1993 if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
1994 &value, GL_TRUE) != Success
1995 || !value) {
1996 return 0; /* error! */
1997 }
1998 }
1999 else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
2000 if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
2001 &value, GL_TRUE) != Success
2002 || !value) {
2003 return 0; /* error! */
2004 }
2005 }
2006 if (mipmap) {
2007 if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
2008 &value, GL_TRUE) != Success
2009 || !value) {
2010 return 0; /* error! */
2011 }
2012 }
2013 if (target == GLX_TEXTURE_1D_EXT) {
2014 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2015 &value, GL_TRUE) != Success
2016 || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
2017 return 0; /* error! */
2018 }
2019 }
2020 else if (target == GLX_TEXTURE_2D_EXT) {
2021 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2022 &value, GL_TRUE) != Success
2023 || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
2024 return 0; /* error! */
2025 }
2026 }
2027 if (target == GLX_TEXTURE_RECTANGLE_EXT) {
2028 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2029 &value, GL_TRUE) != Success
2030 || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
2031 return 0; /* error! */
2032 }
2033 }
2034
2035 if (format || target || mipmap) {
2036 /* texture from pixmap */
2037 b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
2038 }
2039 else {
2040 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
2041 }
2042 if (!b) {
2043 return 0;
2044 }
2045
2046 return pixmap;
2047 }
2048
2049
2050 PUBLIC void
2051 glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
2052 {
2053 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable)pixmap);
2054 if (b)
2055 XMesaDestroyBuffer(b);
2056 /* don't destroy X pixmap */
2057 }
2058
2059
2060 PUBLIC GLXPbuffer
2061 glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList)
2062 {
2063 XMesaVisual xmvis = (XMesaVisual) config;
2064 XMesaBuffer xmbuf;
2065 const int *attrib;
2066 int width = 0, height = 0;
2067 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2068
2069 (void) dpy;
2070
2071 for (attrib = attribList; *attrib; attrib++) {
2072 switch (*attrib) {
2073 case GLX_PBUFFER_WIDTH:
2074 attrib++;
2075 width = *attrib;
2076 break;
2077 case GLX_PBUFFER_HEIGHT:
2078 attrib++;
2079 height = *attrib;
2080 break;
2081 case GLX_PRESERVED_CONTENTS:
2082 attrib++;
2083 preserveContents = *attrib;
2084 break;
2085 case GLX_LARGEST_PBUFFER:
2086 attrib++;
2087 useLargest = *attrib;
2088 break;
2089 default:
2090 return 0;
2091 }
2092 }
2093
2094 if (width == 0 || height == 0)
2095 return 0;
2096
2097 if (width > PBUFFER_MAX_SIZE || height > PBUFFER_MAX_SIZE) {
2098 /* If allocation would have failed and GLX_LARGEST_PBUFFER is set,
2099 * allocate the largest possible buffer.
2100 */
2101 if (useLargest) {
2102 width = PBUFFER_MAX_SIZE;
2103 height = PBUFFER_MAX_SIZE;
2104 }
2105 }
2106
2107 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2108 /* A GLXPbuffer handle must be an X Drawable because that's what
2109 * glXMakeCurrent takes.
2110 */
2111 if (xmbuf) {
2112 xmbuf->largestPbuffer = useLargest;
2113 xmbuf->preservedContents = preserveContents;
2114 return (GLXPbuffer) xmbuf->ws.drawable;
2115 }
2116 else {
2117 return 0;
2118 }
2119 }
2120
2121
2122 PUBLIC void
2123 glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2124 {
2125 XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2126 if (b) {
2127 XMesaDestroyBuffer(b);
2128 }
2129 }
2130
2131
2132 PUBLIC void
2133 glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute,
2134 unsigned int *value)
2135 {
2136 GLuint width, height;
2137 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2138 if (!xmbuf) {
2139 generate_error(dpy, GLXBadDrawable, draw, X_GLXGetDrawableAttributes, False);
2140 return;
2141 }
2142
2143 /* make sure buffer's dimensions are up to date */
2144 xmesa_get_window_size(dpy, xmbuf, &width, &height);
2145
2146 switch (attribute) {
2147 case GLX_WIDTH:
2148 *value = width;
2149 break;
2150 case GLX_HEIGHT:
2151 *value = height;
2152 break;
2153 case GLX_PRESERVED_CONTENTS:
2154 *value = xmbuf->preservedContents;
2155 break;
2156 case GLX_LARGEST_PBUFFER:
2157 *value = xmbuf->largestPbuffer;
2158 break;
2159 case GLX_FBCONFIG_ID:
2160 *value = xmbuf->xm_visual->visinfo->visualid;
2161 return;
2162 case GLX_TEXTURE_FORMAT_EXT:
2163 *value = xmbuf->TextureFormat;
2164 break;
2165 case GLX_TEXTURE_TARGET_EXT:
2166 *value = xmbuf->TextureTarget;
2167 break;
2168 case GLX_MIPMAP_TEXTURE_EXT:
2169 *value = xmbuf->TextureMipmap;
2170 break;
2171
2172 default:
2173 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, true);
2174 return;
2175 }
2176 }
2177
2178
2179 PUBLIC GLXContext
2180 glXCreateNewContext( Display *dpy, GLXFBConfig config,
2181 int renderType, GLXContext shareCtx, Bool direct )
2182 {
2183 XMesaVisual xmvis = (XMesaVisual) config;
2184
2185 if (!dpy || !config ||
2186 (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2187 return 0;
2188
2189 return create_context(dpy, xmvis,
2190 shareCtx ? shareCtx->xmesaContext : NULL,
2191 direct,
2192 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2193 }
2194
2195
2196 PUBLIC int
2197 glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
2198 {
2199 GLXContext glxCtx = ctx;
2200 XMesaContext xmctx = glxCtx->xmesaContext;
2201
2202 (void) dpy;
2203 (void) ctx;
2204
2205 switch (attribute) {
2206 case GLX_FBCONFIG_ID:
2207 *value = xmctx->xm_visual->visinfo->visualid;
2208 break;
2209 case GLX_RENDER_TYPE:
2210 if (xmctx->xm_visual->mesa_visual.rgbMode)
2211 *value = GLX_RGBA_TYPE;
2212 else
2213 *value = GLX_COLOR_INDEX_TYPE;
2214 break;
2215 case GLX_SCREEN:
2216 *value = 0;
2217 return Success;
2218 default:
2219 return GLX_BAD_ATTRIBUTE;
2220 }
2221 return 0;
2222 }
2223
2224
2225 PUBLIC void
2226 glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
2227 {
2228 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2229 if (xmbuf)
2230 xmbuf->selectedEvents = mask;
2231 }
2232
2233
2234 PUBLIC void
2235 glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
2236 {
2237 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2238 if (xmbuf)
2239 *mask = xmbuf->selectedEvents;
2240 else
2241 *mask = 0;
2242 }
2243
2244
2245
2246 /*** GLX_SGI_swap_control ***/
2247
2248 PUBLIC int
2249 glXSwapIntervalSGI(int interval)
2250 {
2251 (void) interval;
2252 return 0;
2253 }
2254
2255
2256
2257 /*** GLX_SGI_video_sync ***/
2258
2259 static unsigned int FrameCounter = 0;
2260
2261 PUBLIC int
2262 glXGetVideoSyncSGI(unsigned int *count)
2263 {
2264 /* this is a bogus implementation */
2265 *count = FrameCounter++;
2266 return 0;
2267 }
2268
2269 PUBLIC int
2270 glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2271 {
2272 if (divisor <= 0 || remainder < 0)
2273 return GLX_BAD_VALUE;
2274 /* this is a bogus implementation */
2275 FrameCounter++;
2276 while (FrameCounter % divisor != remainder)
2277 FrameCounter++;
2278 *count = FrameCounter;
2279 return 0;
2280 }
2281
2282
2283
2284 /*** GLX_SGI_make_current_read ***/
2285
2286 PUBLIC Bool
2287 glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read,
2288 GLXContext ctx)
2289 {
2290 return glXMakeContextCurrent( dpy, draw, read, ctx );
2291 }
2292
2293 /* not used
2294 static GLXDrawable
2295 glXGetCurrentReadDrawableSGI(void)
2296 {
2297 return 0;
2298 }
2299 */
2300
2301
2302 /*** GLX_SGIX_video_source ***/
2303 #if defined(_VL_H)
2304
2305 PUBLIC GLXVideoSourceSGIX
2306 glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server,
2307 VLPath path, int nodeClass, VLNode drainNode)
2308 {
2309 (void) dpy;
2310 (void) screen;
2311 (void) server;
2312 (void) path;
2313 (void) nodeClass;
2314 (void) drainNode;
2315 return 0;
2316 }
2317
2318 PUBLIC void
2319 glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
2320 {
2321 (void) dpy;
2322 (void) src;
2323 }
2324
2325 #endif
2326
2327
2328 /*** GLX_EXT_import_context ***/
2329
2330 PUBLIC void
2331 glXFreeContextEXT(Display *dpy, GLXContext context)
2332 {
2333 (void) dpy;
2334 (void) context;
2335 }
2336
2337 PUBLIC GLXContextID
2338 glXGetContextIDEXT(const GLXContext context)
2339 {
2340 (void) context;
2341 return 0;
2342 }
2343
2344 PUBLIC GLXContext
2345 glXImportContextEXT(Display *dpy, GLXContextID contextID)
2346 {
2347 (void) dpy;
2348 (void) contextID;
2349 return 0;
2350 }
2351
2352 PUBLIC int
2353 glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute,
2354 int *value)
2355 {
2356 (void) dpy;
2357 (void) context;
2358 (void) attribute;
2359 (void) value;
2360 return 0;
2361 }
2362
2363
2364
2365 /*** GLX_SGIX_fbconfig ***/
2366
2367 PUBLIC int
2368 glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config,
2369 int attribute, int *value)
2370 {
2371 return glXGetFBConfigAttrib(dpy, config, attribute, value);
2372 }
2373
2374 PUBLIC GLXFBConfigSGIX *
2375 glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list,
2376 int *nelements)
2377 {
2378 return (GLXFBConfig *) glXChooseFBConfig(dpy, screen,
2379 attrib_list, nelements);
2380 }
2381
2382
2383 PUBLIC GLXPixmap
2384 glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2385 Pixmap pixmap)
2386 {
2387 XMesaVisual xmvis = (XMesaVisual) config;
2388 XMesaBuffer xmbuf = XMesaCreatePixmapBuffer(xmvis, pixmap, 0);
2389 return xmbuf->ws.drawable; /* need to return an X ID */
2390 }
2391
2392
2393 PUBLIC GLXContext
2394 glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2395 int renderType, GLXContext shareCtx,
2396 Bool direct)
2397 {
2398 XMesaVisual xmvis = (XMesaVisual) config;
2399
2400 if (!dpy || !config ||
2401 (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2402 return 0;
2403
2404 return create_context(dpy, xmvis,
2405 shareCtx ? shareCtx->xmesaContext : NULL,
2406 direct,
2407 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2408 }
2409
2410
2411 PUBLIC XVisualInfo *
2412 glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
2413 {
2414 return glXGetVisualFromFBConfig(dpy, config);
2415 }
2416
2417
2418 PUBLIC GLXFBConfigSGIX
2419 glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
2420 {
2421 XMesaVisual xmvis = find_glx_visual(dpy, vis);
2422 if (!xmvis) {
2423 /* This visual wasn't found with glXChooseVisual() */
2424 xmvis = create_glx_visual(dpy, vis);
2425 }
2426
2427 return (GLXFBConfigSGIX) xmvis;
2428 }
2429
2430
2431
2432 /*** GLX_SGIX_pbuffer ***/
2433
2434 PUBLIC GLXPbufferSGIX
2435 glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
2436 unsigned int width, unsigned int height,
2437 int *attribList)
2438 {
2439 XMesaVisual xmvis = (XMesaVisual) config;
2440 XMesaBuffer xmbuf;
2441 const int *attrib;
2442 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2443
2444 (void) dpy;
2445
2446 for (attrib = attribList; attrib && *attrib; attrib++) {
2447 switch (*attrib) {
2448 case GLX_PRESERVED_CONTENTS_SGIX:
2449 attrib++;
2450 preserveContents = *attrib; /* ignored */
2451 break;
2452 case GLX_LARGEST_PBUFFER_SGIX:
2453 attrib++;
2454 useLargest = *attrib; /* ignored */
2455 break;
2456 default:
2457 return 0;
2458 }
2459 }
2460
2461 /* not used at this time */
2462 (void) useLargest;
2463 (void) preserveContents;
2464
2465 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2466 /* A GLXPbuffer handle must be an X Drawable because that's what
2467 * glXMakeCurrent takes.
2468 */
2469 return (GLXPbuffer) xmbuf->ws.drawable;
2470 }
2471
2472
2473 PUBLIC void
2474 glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
2475 {
2476 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2477 if (xmbuf) {
2478 XMesaDestroyBuffer(xmbuf);
2479 }
2480 }
2481
2482
2483 PUBLIC int
2484 glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute,
2485 unsigned int *value)
2486 {
2487 const XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2488
2489 if (!xmbuf) {
2490 /* Generate GLXBadPbufferSGIX for bad pbuffer */
2491 return 0;
2492 }
2493
2494 switch (attribute) {
2495 case GLX_PRESERVED_CONTENTS_SGIX:
2496 *value = True;
2497 break;
2498 case GLX_LARGEST_PBUFFER_SGIX:
2499 *value = xmesa_buffer_width(xmbuf) * xmesa_buffer_height(xmbuf);
2500 break;
2501 case GLX_WIDTH_SGIX:
2502 *value = xmesa_buffer_width(xmbuf);
2503 break;
2504 case GLX_HEIGHT_SGIX:
2505 *value = xmesa_buffer_height(xmbuf);
2506 break;
2507 case GLX_EVENT_MASK_SGIX:
2508 *value = 0; /* XXX might be wrong */
2509 break;
2510 default:
2511 *value = 0;
2512 }
2513 return 0;
2514 }
2515
2516
2517 PUBLIC void
2518 glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
2519 {
2520 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2521 if (xmbuf) {
2522 /* Note: we'll never generate clobber events */
2523 xmbuf->selectedEvents = mask;
2524 }
2525 }
2526
2527
2528 PUBLIC void
2529 glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable,
2530 unsigned long *mask)
2531 {
2532 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2533 if (xmbuf) {
2534 *mask = xmbuf->selectedEvents;
2535 }
2536 else {
2537 *mask = 0;
2538 }
2539 }
2540
2541
2542
2543 /*** GLX_SGI_cushion ***/
2544
2545 PUBLIC void
2546 glXCushionSGI(Display *dpy, Window win, float cushion)
2547 {
2548 (void) dpy;
2549 (void) win;
2550 (void) cushion;
2551 }
2552
2553
2554
2555 /*** GLX_SGIX_video_resize ***/
2556
2557 PUBLIC int
2558 glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel,
2559 Window window)
2560 {
2561 (void) dpy;
2562 (void) screen;
2563 (void) channel;
2564 (void) window;
2565 return 0;
2566 }
2567
2568 PUBLIC int
2569 glXChannelRectSGIX(Display *dpy, int screen, int channel,
2570 int x, int y, int w, int h)
2571 {
2572 (void) dpy;
2573 (void) screen;
2574 (void) channel;
2575 (void) x;
2576 (void) y;
2577 (void) w;
2578 (void) h;
2579 return 0;
2580 }
2581
2582 PUBLIC int
2583 glXQueryChannelRectSGIX(Display *dpy, int screen, int channel,
2584 int *x, int *y, int *w, int *h)
2585 {
2586 (void) dpy;
2587 (void) screen;
2588 (void) channel;
2589 (void) x;
2590 (void) y;
2591 (void) w;
2592 (void) h;
2593 return 0;
2594 }
2595
2596 PUBLIC int
2597 glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel,
2598 int *dx, int *dy, int *dw, int *dh)
2599 {
2600 (void) dpy;
2601 (void) screen;
2602 (void) channel;
2603 (void) dx;
2604 (void) dy;
2605 (void) dw;
2606 (void) dh;
2607 return 0;
2608 }
2609
2610 PUBLIC int
2611 glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
2612 {
2613 (void) dpy;
2614 (void) screen;
2615 (void) channel;
2616 (void) synctype;
2617 return 0;
2618 }
2619
2620
2621
2622 /*** GLX_SGIX_dmbuffer **/
2623
2624 #if defined(_DM_BUFFER_H_)
2625 PUBLIC Bool
2626 glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer,
2627 DMparams *params, DMbuffer dmbuffer)
2628 {
2629 (void) dpy;
2630 (void) pbuffer;
2631 (void) params;
2632 (void) dmbuffer;
2633 return False;
2634 }
2635 #endif
2636
2637
2638 /*** GLX_SGIX_swap_group ***/
2639
2640 PUBLIC void
2641 glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
2642 {
2643 (void) dpy;
2644 (void) drawable;
2645 (void) member;
2646 }
2647
2648
2649
2650 /*** GLX_SGIX_swap_barrier ***/
2651
2652 PUBLIC void
2653 glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
2654 {
2655 (void) dpy;
2656 (void) drawable;
2657 (void) barrier;
2658 }
2659
2660 PUBLIC Bool
2661 glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2662 {
2663 (void) dpy;
2664 (void) screen;
2665 (void) max;
2666 return False;
2667 }
2668
2669
2670
2671 /*** GLX_SUN_get_transparent_index ***/
2672
2673 PUBLIC Status
2674 glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay,
2675 long *pTransparent)
2676 {
2677 (void) dpy;
2678 (void) overlay;
2679 (void) underlay;
2680 (void) pTransparent;
2681 return 0;
2682 }
2683
2684
2685
2686 /*** GLX_MESA_release_buffers ***/
2687
2688 /*
2689 * Release the depth, stencil, accum buffers attached to a GLXDrawable
2690 * (a window or pixmap) prior to destroying the GLXDrawable.
2691 */
2692 PUBLIC Bool
2693 glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2694 {
2695 XMesaBuffer b = XMesaFindBuffer(dpy, d);
2696 if (b) {
2697 XMesaDestroyBuffer(b);
2698 return True;
2699 }
2700 return False;
2701 }
2702
2703 /*** GLX_EXT_texture_from_pixmap ***/
2704
2705 PUBLIC void
2706 glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
2707 const int *attrib_list)
2708 {
2709 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2710 if (b)
2711 XMesaBindTexImage(dpy, b, buffer, attrib_list);
2712 }
2713
2714 PUBLIC void
2715 glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
2716 {
2717 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2718 if (b)
2719 XMesaReleaseTexImage(dpy, b, buffer);
2720 }
2721
2722
2723
2724 /*** GLX_ARB_create_context ***/
2725
2726
2727 GLXContext
2728 glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config,
2729 GLXContext shareCtx, Bool direct,
2730 const int *attrib_list)
2731 {
2732 XMesaVisual xmvis = (XMesaVisual) config;
2733 int majorVersion = 1, minorVersion = 0;
2734 int contextFlags = 0x0;
2735 int profileMask = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
2736 int renderType = GLX_RGBA_TYPE;
2737 unsigned i;
2738 Bool done = False;
2739 const int contextFlagsAll = (GLX_CONTEXT_DEBUG_BIT_ARB |
2740 GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
2741 GLXContext ctx;
2742
2743 /* parse attrib_list */
2744 for (i = 0; !done && attrib_list && attrib_list[i]; i++) {
2745 switch (attrib_list[i]) {
2746 case GLX_CONTEXT_MAJOR_VERSION_ARB:
2747 majorVersion = attrib_list[++i];
2748 break;
2749 case GLX_CONTEXT_MINOR_VERSION_ARB:
2750 minorVersion = attrib_list[++i];
2751 break;
2752 case GLX_CONTEXT_FLAGS_ARB:
2753 contextFlags = attrib_list[++i];
2754 break;
2755 case GLX_CONTEXT_PROFILE_MASK_ARB:
2756 profileMask = attrib_list[++i];
2757 break;
2758 case GLX_RENDER_TYPE:
2759 renderType = attrib_list[++i];
2760 break;
2761 case 0:
2762 /* end of list */
2763 done = True;
2764 break;
2765 default:
2766 /* bad attribute */
2767 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2768 return NULL;
2769 }
2770 }
2771
2772 /* check contextFlags */
2773 if (contextFlags & ~contextFlagsAll) {
2774 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2775 return NULL;
2776 }
2777
2778 /* check profileMask */
2779 if (profileMask != GLX_CONTEXT_CORE_PROFILE_BIT_ARB &&
2780 profileMask != GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB &&
2781 profileMask != GLX_CONTEXT_ES_PROFILE_BIT_EXT) {
2782 generate_error(dpy, GLXBadProfileARB, 0, X_GLXCreateContextAttribsARB, False);
2783 return NULL;
2784 }
2785
2786 /* check renderType */
2787 if (renderType != GLX_RGBA_TYPE &&
2788 renderType != GLX_COLOR_INDEX_TYPE) {
2789 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2790 return NULL;
2791 }
2792
2793 /* check version */
2794 if (majorVersion <= 0 ||
2795 minorVersion < 0 ||
2796 (profileMask != GLX_CONTEXT_ES_PROFILE_BIT_EXT &&
2797 ((majorVersion == 1 && minorVersion > 5) ||
2798 (majorVersion == 2 && minorVersion > 1) ||
2799 (majorVersion == 3 && minorVersion > 3) ||
2800 (majorVersion == 4 && minorVersion > 5) ||
2801 majorVersion > 4))) {
2802 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2803 return NULL;
2804 }
2805 if (profileMask == GLX_CONTEXT_ES_PROFILE_BIT_EXT &&
2806 ((majorVersion == 1 && minorVersion > 1) ||
2807 (majorVersion == 2 && minorVersion > 0) ||
2808 (majorVersion == 3 && minorVersion > 1) ||
2809 majorVersion > 3)) {
2810 /* GLX_EXT_create_context_es2_profile says nothing to justifying a
2811 * different error code for invalid ES versions, but this is what NVIDIA
2812 * does and piglit expects.
2813 */
2814 generate_error(dpy, GLXBadProfileARB, 0, X_GLXCreateContextAttribsARB, False);
2815 return NULL;
2816 }
2817
2818 if ((contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) &&
2819 majorVersion < 3) {
2820 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2821 return NULL;
2822 }
2823
2824 if (renderType == GLX_COLOR_INDEX_TYPE && majorVersion >= 3) {
2825 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2826 return NULL;
2827 }
2828
2829 ctx = create_context(dpy, xmvis,
2830 shareCtx ? shareCtx->xmesaContext : NULL,
2831 direct,
2832 majorVersion, minorVersion,
2833 profileMask, contextFlags);
2834 if (!ctx) {
2835 generate_error(dpy, GLXBadFBConfig, 0, X_GLXCreateContextAttribsARB, False);
2836 }
2837
2838 return ctx;
2839 }