Merge branch 'wip/nir-vtn' into vulkan
[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 return 0;
619 }
620
621
622 /**
623 * Look for the named extension on given display and return a pointer
624 * to the _XExtension data, or NULL if extension not found.
625 */
626 static _XExtension *
627 lookup_extension(Display *dpy, const char *extName)
628 {
629 _XExtension *ext;
630 for (ext = dpy->ext_procs; ext; ext = ext->next) {
631 if (ext->name && strcmp(ext->name, extName) == 0) {
632 return ext;
633 }
634 }
635 return NULL;
636 }
637
638
639 /**
640 * Whenever we're given a new Display pointer, call this function to
641 * register our close_display_callback function.
642 */
643 static void
644 register_with_display(Display *dpy)
645 {
646 const char *extName = "MesaGLX";
647 _XExtension *ext;
648
649 ext = lookup_extension(dpy, extName);
650 if (!ext) {
651 XExtCodes *c = XAddExtension(dpy);
652 ext = dpy->ext_procs; /* new extension is at head of list */
653 assert(c->extension == ext->codes.extension);
654 (void) c;
655 ext->name = strdup(extName);
656 ext->close_display = close_display_callback;
657 }
658 }
659
660
661 /**
662 * Fake an error.
663 */
664 static int
665 generate_error(Display *dpy,
666 unsigned char error_code,
667 XID resourceid,
668 unsigned char minor_code,
669 Bool core)
670 {
671 XErrorHandler handler;
672 int major_opcode;
673 int first_event;
674 int first_error;
675 XEvent event;
676
677 handler = XSetErrorHandler(NULL);
678 XSetErrorHandler(handler);
679 if (!handler) {
680 return 0;
681 }
682
683 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_opcode, &first_event, &first_error)) {
684 major_opcode = 0;
685 first_event = 0;
686 first_error = 0;
687 }
688
689 if (!core) {
690 error_code += first_error;
691 }
692
693 memset(&event, 0, sizeof event);
694
695 event.xerror.type = X_Error;
696 event.xerror.display = dpy;
697 event.xerror.resourceid = resourceid;
698 event.xerror.serial = NextRequest(dpy) - 1;
699 event.xerror.error_code = error_code;
700 event.xerror.request_code = major_opcode;
701 event.xerror.minor_code = minor_code;
702
703 return handler(dpy, &event.xerror);
704 }
705
706
707 /**********************************************************************/
708 /*** Begin Fake GLX API Functions ***/
709 /**********************************************************************/
710
711
712 /**
713 * Helper used by glXChooseVisual and glXChooseFBConfig.
714 * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
715 * the later.
716 * In either case, the attribute list is terminated with the value 'None'.
717 */
718 static XMesaVisual
719 choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
720 {
721 const GLboolean rgbModeDefault = fbConfig;
722 const int *parselist;
723 XVisualInfo *vis;
724 int min_red=0, min_green=0, min_blue=0;
725 GLboolean rgb_flag = rgbModeDefault;
726 GLboolean alpha_flag = GL_FALSE;
727 GLboolean double_flag = GL_FALSE;
728 GLboolean stereo_flag = GL_FALSE;
729 GLint depth_size = 0;
730 GLint stencil_size = 0;
731 GLint accumRedSize = 0;
732 GLint accumGreenSize = 0;
733 GLint accumBlueSize = 0;
734 GLint accumAlphaSize = 0;
735 int level = 0;
736 int visual_type = DONT_CARE;
737 GLint caveat = DONT_CARE;
738 XMesaVisual xmvis = NULL;
739 int desiredVisualID = -1;
740 int numAux = 0;
741
742 xmesa_init( dpy );
743
744 parselist = list;
745
746 while (*parselist) {
747
748 if (fbConfig &&
749 parselist[1] == GLX_DONT_CARE &&
750 parselist[0] != GLX_LEVEL) {
751 /* For glXChooseFBConfig(), skip attributes whose value is
752 * GLX_DONT_CARE, unless it's GLX_LEVEL (which can legitimately be
753 * a negative value).
754 *
755 * From page 17 (23 of the pdf) of the GLX 1.4 spec:
756 * GLX DONT CARE may be specified for all attributes except GLX LEVEL.
757 */
758 parselist += 2;
759 continue;
760 }
761
762 switch (*parselist) {
763 case GLX_USE_GL:
764 if (fbConfig) {
765 /* invalid token */
766 return NULL;
767 }
768 else {
769 /* skip */
770 parselist++;
771 }
772 break;
773 case GLX_BUFFER_SIZE:
774 parselist++;
775 parselist++;
776 break;
777 case GLX_LEVEL:
778 parselist++;
779 level = *parselist++;
780 break;
781 case GLX_RGBA:
782 if (fbConfig) {
783 /* invalid token */
784 return NULL;
785 }
786 else {
787 rgb_flag = GL_TRUE;
788 parselist++;
789 }
790 break;
791 case GLX_DOUBLEBUFFER:
792 parselist++;
793 if (fbConfig) {
794 double_flag = *parselist++;
795 }
796 else {
797 double_flag = GL_TRUE;
798 }
799 break;
800 case GLX_STEREO:
801 parselist++;
802 if (fbConfig) {
803 stereo_flag = *parselist++;
804 }
805 else {
806 stereo_flag = GL_TRUE;
807 }
808 break;
809 case GLX_AUX_BUFFERS:
810 parselist++;
811 numAux = *parselist++;
812 if (numAux > MAX_AUX_BUFFERS)
813 return NULL;
814 break;
815 case GLX_RED_SIZE:
816 parselist++;
817 min_red = *parselist++;
818 break;
819 case GLX_GREEN_SIZE:
820 parselist++;
821 min_green = *parselist++;
822 break;
823 case GLX_BLUE_SIZE:
824 parselist++;
825 min_blue = *parselist++;
826 break;
827 case GLX_ALPHA_SIZE:
828 parselist++;
829 {
830 GLint size = *parselist++;
831 alpha_flag = size ? GL_TRUE : GL_FALSE;
832 }
833 break;
834 case GLX_DEPTH_SIZE:
835 parselist++;
836 depth_size = *parselist++;
837 break;
838 case GLX_STENCIL_SIZE:
839 parselist++;
840 stencil_size = *parselist++;
841 break;
842 case GLX_ACCUM_RED_SIZE:
843 parselist++;
844 {
845 GLint size = *parselist++;
846 accumRedSize = MAX2( accumRedSize, size );
847 }
848 break;
849 case GLX_ACCUM_GREEN_SIZE:
850 parselist++;
851 {
852 GLint size = *parselist++;
853 accumGreenSize = MAX2( accumGreenSize, size );
854 }
855 break;
856 case GLX_ACCUM_BLUE_SIZE:
857 parselist++;
858 {
859 GLint size = *parselist++;
860 accumBlueSize = MAX2( accumBlueSize, size );
861 }
862 break;
863 case GLX_ACCUM_ALPHA_SIZE:
864 parselist++;
865 {
866 GLint size = *parselist++;
867 accumAlphaSize = MAX2( accumAlphaSize, size );
868 }
869 break;
870
871 /*
872 * GLX_EXT_visual_info extension
873 */
874 case GLX_X_VISUAL_TYPE_EXT:
875 parselist++;
876 visual_type = *parselist++;
877 break;
878 case GLX_TRANSPARENT_TYPE_EXT:
879 parselist++;
880 parselist++;
881 break;
882 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
883 parselist++;
884 parselist++;
885 break;
886 case GLX_TRANSPARENT_RED_VALUE_EXT:
887 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
888 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
889 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
890 /* ignore */
891 parselist++;
892 parselist++;
893 break;
894
895 /*
896 * GLX_EXT_visual_info extension
897 */
898 case GLX_VISUAL_CAVEAT_EXT:
899 parselist++;
900 caveat = *parselist++; /* ignored for now */
901 break;
902
903 /*
904 * GLX_ARB_multisample
905 */
906 case GLX_SAMPLE_BUFFERS_ARB:
907 case GLX_SAMPLES_ARB:
908 parselist++;
909 if (*parselist++ != 0) {
910 /* ms not supported */
911 return NULL;
912 }
913 break;
914
915 /*
916 * FBConfig attribs.
917 */
918 case GLX_RENDER_TYPE:
919 if (!fbConfig)
920 return NULL;
921 parselist++;
922 if (*parselist & GLX_RGBA_BIT) {
923 rgb_flag = GL_TRUE;
924 }
925 else if (*parselist & GLX_COLOR_INDEX_BIT) {
926 rgb_flag = GL_FALSE;
927 }
928 else if (*parselist == 0) {
929 rgb_flag = GL_TRUE;
930 }
931 parselist++;
932 break;
933 case GLX_DRAWABLE_TYPE:
934 if (!fbConfig)
935 return NULL;
936 parselist++;
937 if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
938 return NULL; /* bad bit */
939 }
940 parselist++;
941 break;
942 case GLX_FBCONFIG_ID:
943 case GLX_VISUAL_ID:
944 if (!fbConfig)
945 return NULL;
946 parselist++;
947 desiredVisualID = *parselist++;
948 break;
949 case GLX_X_RENDERABLE:
950 case GLX_MAX_PBUFFER_WIDTH:
951 case GLX_MAX_PBUFFER_HEIGHT:
952 case GLX_MAX_PBUFFER_PIXELS:
953 if (!fbConfig)
954 return NULL; /* invalid config option */
955 parselist += 2; /* ignore the parameter */
956 break;
957
958 #ifdef GLX_EXT_texture_from_pixmap
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 #endif
981
982 case None:
983 /* end of list */
984 break;
985
986 default:
987 /* undefined attribute */
988 _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
989 *parselist);
990 return NULL;
991 }
992 }
993
994 (void) caveat;
995
996
997 /*
998 * Since we're only simulating the GLX extension this function will never
999 * find any real GL visuals. Instead, all we can do is try to find an RGB
1000 * or CI visual of appropriate depth. Other requested attributes such as
1001 * double buffering, depth buffer, etc. will be associated with the X
1002 * visual and stored in the VisualTable[].
1003 */
1004 if (desiredVisualID != -1) {
1005 /* try to get a specific visual, by visualID */
1006 XVisualInfo temp;
1007 int n;
1008 temp.visualid = desiredVisualID;
1009 temp.screen = screen;
1010 vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
1011 if (vis) {
1012 /* give the visual some useful GLX attributes */
1013 double_flag = GL_TRUE;
1014 rgb_flag = GL_TRUE;
1015 }
1016 }
1017 else if (level==0) {
1018 /* normal color planes */
1019 /* Get an RGB visual */
1020 int min_rgb = min_red + min_green + min_blue;
1021 if (min_rgb>1 && min_rgb<8) {
1022 /* a special case to be sure we can get a monochrome visual */
1023 min_rgb = 1;
1024 }
1025 vis = choose_x_visual( dpy, screen, min_rgb, visual_type );
1026 }
1027 else {
1028 _mesa_warning(NULL, "overlay not supported");
1029 return NULL;
1030 }
1031
1032 if (vis) {
1033 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1034 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1035 * largest depth buffer size, which is 32bits/value. Instead, we
1036 * return 16 to maintain performance with earlier versions of Mesa.
1037 */
1038 if (stencil_size > 0)
1039 depth_size = 24; /* if Z and stencil, always use 24+8 format */
1040 else if (depth_size > 24)
1041 depth_size = 32;
1042 else if (depth_size > 16)
1043 depth_size = 24;
1044 else if (depth_size > 0) {
1045 depth_size = default_depth_bits();
1046 }
1047
1048 if (!alpha_flag) {
1049 alpha_flag = default_alpha_bits() > 0;
1050 }
1051
1052 /* we only support one size of stencil and accum buffers. */
1053 if (stencil_size > 0)
1054 stencil_size = 8;
1055
1056 if (accumRedSize > 0 ||
1057 accumGreenSize > 0 ||
1058 accumBlueSize > 0 ||
1059 accumAlphaSize > 0) {
1060
1061 accumRedSize =
1062 accumGreenSize =
1063 accumBlueSize = default_accum_bits();
1064
1065 accumAlphaSize = alpha_flag ? accumRedSize : 0;
1066 }
1067
1068 xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1069 stereo_flag, depth_size, stencil_size,
1070 accumRedSize, accumGreenSize,
1071 accumBlueSize, accumAlphaSize, level, numAux );
1072 }
1073
1074 return xmvis;
1075 }
1076
1077
1078 PUBLIC XVisualInfo *
1079 glXChooseVisual( Display *dpy, int screen, int *list )
1080 {
1081 XMesaVisual xmvis;
1082
1083 /* register ourselves as an extension on this display */
1084 register_with_display(dpy);
1085
1086 xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1087 if (xmvis) {
1088 /* create a new vishandle - the cached one may be stale */
1089 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1090 if (xmvis->vishandle) {
1091 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1092 }
1093 return xmvis->vishandle;
1094 }
1095 else
1096 return NULL;
1097 }
1098
1099
1100 /**
1101 * Helper function used by other glXCreateContext functions.
1102 */
1103 static GLXContext
1104 create_context(Display *dpy, XMesaVisual xmvis,
1105 XMesaContext shareCtx, Bool direct,
1106 unsigned major, unsigned minor,
1107 unsigned profileMask, unsigned contextFlags)
1108 {
1109 GLXContext glxCtx;
1110
1111 if (!dpy || !xmvis)
1112 return 0;
1113
1114 glxCtx = CALLOC_STRUCT(__GLXcontextRec);
1115 if (!glxCtx)
1116 return 0;
1117
1118 /* deallocate unused windows/buffers */
1119 #if 0
1120 XMesaGarbageCollect();
1121 #endif
1122
1123 glxCtx->xmesaContext = XMesaCreateContext(xmvis, shareCtx, major, minor,
1124 profileMask, contextFlags);
1125 if (!glxCtx->xmesaContext) {
1126 free(glxCtx);
1127 return NULL;
1128 }
1129
1130 glxCtx->isDirect = DEFAULT_DIRECT;
1131 glxCtx->currentDpy = dpy;
1132 glxCtx->xid = (XID) glxCtx; /* self pointer */
1133
1134 return glxCtx;
1135 }
1136
1137
1138 PUBLIC GLXContext
1139 glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1140 GLXContext shareCtx, Bool direct )
1141 {
1142 XMesaVisual xmvis;
1143
1144 xmvis = find_glx_visual( dpy, visinfo );
1145 if (!xmvis) {
1146 /* This visual wasn't found with glXChooseVisual() */
1147 xmvis = create_glx_visual( dpy, visinfo );
1148 if (!xmvis) {
1149 /* unusable visual */
1150 return NULL;
1151 }
1152 }
1153
1154 return create_context(dpy, xmvis,
1155 shareCtx ? shareCtx->xmesaContext : NULL,
1156 direct,
1157 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
1158 }
1159
1160
1161 /* XXX these may have to be removed due to thread-safety issues. */
1162 static GLXContext MakeCurrent_PrevContext = 0;
1163 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1164 static GLXDrawable MakeCurrent_PrevReadable = 0;
1165 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1166 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1167
1168
1169 /* GLX 1.3 and later */
1170 PUBLIC Bool
1171 glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1172 GLXDrawable read, GLXContext ctx )
1173 {
1174 GLXContext glxCtx = ctx;
1175 static boolean firsttime = 1, no_rast = 0;
1176
1177 if (firsttime) {
1178 no_rast = getenv("SP_NO_RAST") != NULL;
1179 firsttime = 0;
1180 }
1181
1182 if (ctx && draw && read) {
1183 XMesaBuffer drawBuffer, readBuffer;
1184 XMesaContext xmctx = glxCtx->xmesaContext;
1185
1186 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1187 if (ctx == MakeCurrent_PrevContext
1188 && draw == MakeCurrent_PrevDrawable) {
1189 drawBuffer = MakeCurrent_PrevDrawBuffer;
1190 }
1191 else {
1192 drawBuffer = XMesaFindBuffer( dpy, draw );
1193 }
1194 if (!drawBuffer) {
1195 /* drawable must be a new window! */
1196 drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1197 if (!drawBuffer) {
1198 /* Out of memory, or context/drawable depth mismatch */
1199 return False;
1200 }
1201 }
1202
1203 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1204 if (ctx == MakeCurrent_PrevContext
1205 && read == MakeCurrent_PrevReadable) {
1206 readBuffer = MakeCurrent_PrevReadBuffer;
1207 }
1208 else {
1209 readBuffer = XMesaFindBuffer( dpy, read );
1210 }
1211 if (!readBuffer) {
1212 /* drawable must be a new window! */
1213 readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1214 if (!readBuffer) {
1215 /* Out of memory, or context/drawable depth mismatch */
1216 return False;
1217 }
1218 }
1219
1220 if (no_rast &&
1221 MakeCurrent_PrevContext == ctx &&
1222 MakeCurrent_PrevDrawable == draw &&
1223 MakeCurrent_PrevReadable == read &&
1224 MakeCurrent_PrevDrawBuffer == drawBuffer &&
1225 MakeCurrent_PrevReadBuffer == readBuffer)
1226 return True;
1227
1228 MakeCurrent_PrevContext = ctx;
1229 MakeCurrent_PrevDrawable = draw;
1230 MakeCurrent_PrevReadable = read;
1231 MakeCurrent_PrevDrawBuffer = drawBuffer;
1232 MakeCurrent_PrevReadBuffer = readBuffer;
1233
1234 /* Now make current! */
1235 if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1236 ctx->currentDpy = dpy;
1237 ctx->currentDrawable = draw;
1238 ctx->currentReadable = read;
1239 SetCurrentContext(ctx);
1240 return True;
1241 }
1242 else {
1243 return False;
1244 }
1245 }
1246 else if (!ctx && !draw && !read) {
1247 /* release current context w/out assigning new one. */
1248 XMesaMakeCurrent2( NULL, NULL, NULL );
1249 MakeCurrent_PrevContext = 0;
1250 MakeCurrent_PrevDrawable = 0;
1251 MakeCurrent_PrevReadable = 0;
1252 MakeCurrent_PrevDrawBuffer = 0;
1253 MakeCurrent_PrevReadBuffer = 0;
1254 SetCurrentContext(NULL);
1255 return True;
1256 }
1257 else {
1258 /* The args must either all be non-zero or all zero.
1259 * This is an error.
1260 */
1261 return False;
1262 }
1263 }
1264
1265
1266 PUBLIC Bool
1267 glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1268 {
1269 return glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1270 }
1271
1272
1273 PUBLIC GLXContext
1274 glXGetCurrentContext(void)
1275 {
1276 return GetCurrentContext();
1277 }
1278
1279
1280 PUBLIC Display *
1281 glXGetCurrentDisplay(void)
1282 {
1283 GLXContext glxCtx = glXGetCurrentContext();
1284
1285 return glxCtx ? glxCtx->currentDpy : NULL;
1286 }
1287
1288
1289 PUBLIC Display *
1290 glXGetCurrentDisplayEXT(void)
1291 {
1292 return glXGetCurrentDisplay();
1293 }
1294
1295
1296 PUBLIC GLXDrawable
1297 glXGetCurrentDrawable(void)
1298 {
1299 GLXContext gc = glXGetCurrentContext();
1300 return gc ? gc->currentDrawable : 0;
1301 }
1302
1303
1304 PUBLIC GLXDrawable
1305 glXGetCurrentReadDrawable(void)
1306 {
1307 GLXContext gc = glXGetCurrentContext();
1308 return gc ? gc->currentReadable : 0;
1309 }
1310
1311
1312 PUBLIC GLXDrawable
1313 glXGetCurrentReadDrawableSGI(void)
1314 {
1315 return glXGetCurrentReadDrawable();
1316 }
1317
1318
1319 PUBLIC GLXPixmap
1320 glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1321 {
1322 XMesaVisual v;
1323 XMesaBuffer b;
1324
1325 v = find_glx_visual( dpy, visinfo );
1326 if (!v) {
1327 v = create_glx_visual( dpy, visinfo );
1328 if (!v) {
1329 /* unusable visual */
1330 return 0;
1331 }
1332 }
1333
1334 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1335 if (!b) {
1336 return 0;
1337 }
1338 return b->ws.drawable;
1339 }
1340
1341
1342 /*** GLX_MESA_pixmap_colormap ***/
1343
1344 PUBLIC GLXPixmap
1345 glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1346 Pixmap pixmap, Colormap cmap )
1347 {
1348 XMesaVisual v;
1349 XMesaBuffer b;
1350
1351 v = find_glx_visual( dpy, visinfo );
1352 if (!v) {
1353 v = create_glx_visual( dpy, visinfo );
1354 if (!v) {
1355 /* unusable visual */
1356 return 0;
1357 }
1358 }
1359
1360 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1361 if (!b) {
1362 return 0;
1363 }
1364 return b->ws.drawable;
1365 }
1366
1367
1368 PUBLIC void
1369 glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1370 {
1371 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1372 if (b) {
1373 XMesaDestroyBuffer(b);
1374 }
1375 else if (getenv("MESA_DEBUG")) {
1376 _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1377 }
1378 }
1379
1380
1381 PUBLIC void
1382 glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1383 unsigned long mask )
1384 {
1385 XMesaContext xm_src = src->xmesaContext;
1386 XMesaContext xm_dst = dst->xmesaContext;
1387 (void) dpy;
1388 if (MakeCurrent_PrevContext == src) {
1389 glFlush();
1390 }
1391 XMesaCopyContext(xm_src, xm_dst, mask);
1392 }
1393
1394
1395 PUBLIC Bool
1396 glXQueryExtension( Display *dpy, int *errorBase, int *eventBase )
1397 {
1398 int op, ev, err;
1399 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1400 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &op, &ev, &err))
1401 ev = err = 0;
1402 if (errorBase)
1403 *errorBase = err;
1404 if (eventBase)
1405 *eventBase = ev;
1406 return True; /* we're faking GLX so always return success */
1407 }
1408
1409
1410 PUBLIC void
1411 glXDestroyContext( Display *dpy, GLXContext ctx )
1412 {
1413 if (ctx) {
1414 GLXContext glxCtx = ctx;
1415 (void) dpy;
1416 MakeCurrent_PrevContext = 0;
1417 MakeCurrent_PrevDrawable = 0;
1418 MakeCurrent_PrevReadable = 0;
1419 MakeCurrent_PrevDrawBuffer = 0;
1420 MakeCurrent_PrevReadBuffer = 0;
1421 XMesaDestroyContext( glxCtx->xmesaContext );
1422 XMesaGarbageCollect();
1423 free(glxCtx);
1424 }
1425 }
1426
1427
1428 PUBLIC Bool
1429 glXIsDirect( Display *dpy, GLXContext ctx )
1430 {
1431 return ctx ? ctx->isDirect : False;
1432 }
1433
1434
1435
1436 PUBLIC void
1437 glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1438 {
1439 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1440 static boolean firsttime = 1, no_rast = 0;
1441
1442 if (firsttime) {
1443 no_rast = getenv("SP_NO_RAST") != NULL;
1444 firsttime = 0;
1445 }
1446
1447 if (no_rast)
1448 return;
1449
1450 if (buffer) {
1451 XMesaSwapBuffers(buffer);
1452 }
1453 else if (getenv("MESA_DEBUG")) {
1454 _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1455 (int) drawable);
1456 }
1457 }
1458
1459
1460
1461 /*** GLX_MESA_copy_sub_buffer ***/
1462
1463 PUBLIC void
1464 glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
1465 int x, int y, int width, int height)
1466 {
1467 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1468 if (buffer) {
1469 XMesaCopySubBuffer(buffer, x, y, width, height);
1470 }
1471 else if (getenv("MESA_DEBUG")) {
1472 _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1473 }
1474 }
1475
1476
1477 PUBLIC Bool
1478 glXQueryVersion( Display *dpy, int *maj, int *min )
1479 {
1480 (void) dpy;
1481 /* Return GLX version, not Mesa version */
1482 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1483 *maj = CLIENT_MAJOR_VERSION;
1484 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1485 return True;
1486 }
1487
1488
1489 /*
1490 * Query the GLX attributes of the given XVisualInfo.
1491 */
1492 static int
1493 get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1494 {
1495 assert(xmvis);
1496 switch(attrib) {
1497 case GLX_USE_GL:
1498 if (fbconfig)
1499 return GLX_BAD_ATTRIBUTE;
1500 *value = (int) True;
1501 return 0;
1502 case GLX_BUFFER_SIZE:
1503 *value = xmvis->visinfo->depth;
1504 return 0;
1505 case GLX_LEVEL:
1506 *value = xmvis->mesa_visual.level;
1507 return 0;
1508 case GLX_RGBA:
1509 if (fbconfig)
1510 return GLX_BAD_ATTRIBUTE;
1511 if (xmvis->mesa_visual.rgbMode) {
1512 *value = True;
1513 }
1514 else {
1515 *value = False;
1516 }
1517 return 0;
1518 case GLX_DOUBLEBUFFER:
1519 *value = (int) xmvis->mesa_visual.doubleBufferMode;
1520 return 0;
1521 case GLX_STEREO:
1522 *value = (int) xmvis->mesa_visual.stereoMode;
1523 return 0;
1524 case GLX_AUX_BUFFERS:
1525 *value = xmvis->mesa_visual.numAuxBuffers;
1526 return 0;
1527 case GLX_RED_SIZE:
1528 *value = xmvis->mesa_visual.redBits;
1529 return 0;
1530 case GLX_GREEN_SIZE:
1531 *value = xmvis->mesa_visual.greenBits;
1532 return 0;
1533 case GLX_BLUE_SIZE:
1534 *value = xmvis->mesa_visual.blueBits;
1535 return 0;
1536 case GLX_ALPHA_SIZE:
1537 *value = xmvis->mesa_visual.alphaBits;
1538 return 0;
1539 case GLX_DEPTH_SIZE:
1540 *value = xmvis->mesa_visual.depthBits;
1541 return 0;
1542 case GLX_STENCIL_SIZE:
1543 *value = xmvis->mesa_visual.stencilBits;
1544 return 0;
1545 case GLX_ACCUM_RED_SIZE:
1546 *value = xmvis->mesa_visual.accumRedBits;
1547 return 0;
1548 case GLX_ACCUM_GREEN_SIZE:
1549 *value = xmvis->mesa_visual.accumGreenBits;
1550 return 0;
1551 case GLX_ACCUM_BLUE_SIZE:
1552 *value = xmvis->mesa_visual.accumBlueBits;
1553 return 0;
1554 case GLX_ACCUM_ALPHA_SIZE:
1555 *value = xmvis->mesa_visual.accumAlphaBits;
1556 return 0;
1557
1558 /*
1559 * GLX_EXT_visual_info extension
1560 */
1561 case GLX_X_VISUAL_TYPE_EXT:
1562 switch (xmvis->visinfo->CLASS) {
1563 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1564 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1565 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1566 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1567 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1568 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1569 }
1570 return 0;
1571 case GLX_TRANSPARENT_TYPE_EXT:
1572 /* normal planes */
1573 *value = GLX_NONE_EXT;
1574 return 0;
1575 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1576 /* undefined */
1577 return 0;
1578 case GLX_TRANSPARENT_RED_VALUE_EXT:
1579 /* undefined */
1580 return 0;
1581 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1582 /* undefined */
1583 return 0;
1584 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1585 /* undefined */
1586 return 0;
1587 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1588 /* undefined */
1589 return 0;
1590
1591 /*
1592 * GLX_EXT_visual_info extension
1593 */
1594 case GLX_VISUAL_CAVEAT_EXT:
1595 /* test for zero, just in case */
1596 if (xmvis->mesa_visual.visualRating > 0)
1597 *value = xmvis->mesa_visual.visualRating;
1598 else
1599 *value = GLX_NONE_EXT;
1600 return 0;
1601
1602 /*
1603 * GLX_ARB_multisample
1604 */
1605 case GLX_SAMPLE_BUFFERS_ARB:
1606 *value = 0;
1607 return 0;
1608 case GLX_SAMPLES_ARB:
1609 *value = 0;
1610 return 0;
1611
1612 /*
1613 * For FBConfigs:
1614 */
1615 case GLX_SCREEN_EXT:
1616 if (!fbconfig)
1617 return GLX_BAD_ATTRIBUTE;
1618 *value = xmvis->visinfo->screen;
1619 break;
1620 case GLX_DRAWABLE_TYPE: /*SGIX too */
1621 if (!fbconfig)
1622 return GLX_BAD_ATTRIBUTE;
1623 *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1624 break;
1625 case GLX_RENDER_TYPE_SGIX:
1626 if (!fbconfig)
1627 return GLX_BAD_ATTRIBUTE;
1628 if (xmvis->mesa_visual.rgbMode)
1629 *value = GLX_RGBA_BIT;
1630 else
1631 *value = GLX_COLOR_INDEX_BIT;
1632 break;
1633 case GLX_X_RENDERABLE_SGIX:
1634 if (!fbconfig)
1635 return GLX_BAD_ATTRIBUTE;
1636 *value = True; /* XXX really? */
1637 break;
1638 case GLX_FBCONFIG_ID_SGIX:
1639 if (!fbconfig)
1640 return GLX_BAD_ATTRIBUTE;
1641 *value = xmvis->visinfo->visualid;
1642 break;
1643 case GLX_MAX_PBUFFER_WIDTH:
1644 if (!fbconfig)
1645 return GLX_BAD_ATTRIBUTE;
1646 /* XXX should be same as ctx->Const.MaxRenderbufferSize */
1647 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1648 break;
1649 case GLX_MAX_PBUFFER_HEIGHT:
1650 if (!fbconfig)
1651 return GLX_BAD_ATTRIBUTE;
1652 *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1653 break;
1654 case GLX_MAX_PBUFFER_PIXELS:
1655 if (!fbconfig)
1656 return GLX_BAD_ATTRIBUTE;
1657 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1658 DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1659 break;
1660 case GLX_VISUAL_ID:
1661 if (!fbconfig)
1662 return GLX_BAD_ATTRIBUTE;
1663 *value = xmvis->visinfo->visualid;
1664 break;
1665
1666 #ifdef GLX_EXT_texture_from_pixmap
1667 case GLX_BIND_TO_TEXTURE_RGB_EXT:
1668 *value = True; /*XXX*/
1669 break;
1670 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1671 /* XXX review */
1672 *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1673 break;
1674 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1675 *value = True; /*XXX*/
1676 break;
1677 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1678 *value = (GLX_TEXTURE_1D_BIT_EXT |
1679 GLX_TEXTURE_2D_BIT_EXT |
1680 GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1681 break;
1682 case GLX_Y_INVERTED_EXT:
1683 *value = True; /*XXX*/
1684 break;
1685 #endif
1686
1687 default:
1688 return GLX_BAD_ATTRIBUTE;
1689 }
1690 return Success;
1691 }
1692
1693
1694 PUBLIC int
1695 glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1696 int attrib, int *value )
1697 {
1698 XMesaVisual xmvis;
1699 int k;
1700 if (!dpy || !visinfo)
1701 return GLX_BAD_ATTRIBUTE;
1702
1703 xmvis = find_glx_visual( dpy, visinfo );
1704 if (!xmvis) {
1705 /* this visual wasn't obtained with glXChooseVisual */
1706 xmvis = create_glx_visual( dpy, visinfo );
1707 if (!xmvis) {
1708 /* this visual can't be used for GL rendering */
1709 if (attrib==GLX_USE_GL) {
1710 *value = (int) False;
1711 return 0;
1712 }
1713 else {
1714 return GLX_BAD_VISUAL;
1715 }
1716 }
1717 }
1718
1719 k = get_config(xmvis, attrib, value, GL_FALSE);
1720 return k;
1721 }
1722
1723
1724 PUBLIC void
1725 glXWaitGL( void )
1726 {
1727 XMesaContext xmesa = XMesaGetCurrentContext();
1728 XMesaFlush( xmesa );
1729 }
1730
1731
1732
1733 PUBLIC void
1734 glXWaitX( void )
1735 {
1736 XMesaContext xmesa = XMesaGetCurrentContext();
1737 XMesaFlush( xmesa );
1738 }
1739
1740
1741 static const char *
1742 get_extensions( void )
1743 {
1744 return EXTENSIONS;
1745 }
1746
1747
1748
1749 /* GLX 1.1 and later */
1750 PUBLIC const char *
1751 glXQueryExtensionsString( Display *dpy, int screen )
1752 {
1753 (void) dpy;
1754 (void) screen;
1755 return get_extensions();
1756 }
1757
1758
1759
1760 /* GLX 1.1 and later */
1761 PUBLIC const char *
1762 glXQueryServerString( Display *dpy, int screen, int name )
1763 {
1764 static char version[1000];
1765 sprintf(version, "%d.%d %s",
1766 SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, xmesa_get_name());
1767
1768 (void) dpy;
1769 (void) screen;
1770
1771 switch (name) {
1772 case GLX_EXTENSIONS:
1773 return get_extensions();
1774 case GLX_VENDOR:
1775 return VENDOR;
1776 case GLX_VERSION:
1777 return version;
1778 default:
1779 return NULL;
1780 }
1781 }
1782
1783
1784
1785 /* GLX 1.1 and later */
1786 PUBLIC const char *
1787 glXGetClientString( Display *dpy, int name )
1788 {
1789 static char version[1000];
1790 sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
1791 CLIENT_MINOR_VERSION, xmesa_get_name());
1792
1793 (void) dpy;
1794
1795 switch (name) {
1796 case GLX_EXTENSIONS:
1797 return get_extensions();
1798 case GLX_VENDOR:
1799 return VENDOR;
1800 case GLX_VERSION:
1801 return version;
1802 default:
1803 return NULL;
1804 }
1805 }
1806
1807
1808
1809 /*
1810 * GLX 1.3 and later
1811 */
1812
1813
1814 PUBLIC int
1815 glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1816 int attribute, int *value)
1817 {
1818 XMesaVisual v = (XMesaVisual) config;
1819 (void) dpy;
1820 (void) config;
1821
1822 if (!dpy || !config || !value)
1823 return -1;
1824
1825 return get_config(v, attribute, value, GL_TRUE);
1826 }
1827
1828
1829 PUBLIC GLXFBConfig *
1830 glXGetFBConfigs( Display *dpy, int screen, int *nelements )
1831 {
1832 XVisualInfo *visuals, visTemplate;
1833 const long visMask = VisualScreenMask;
1834 int i;
1835
1836 /* Get list of all X visuals */
1837 visTemplate.screen = screen;
1838 visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
1839 if (*nelements > 0) {
1840 XMesaVisual *results = malloc(*nelements * sizeof(XMesaVisual));
1841 if (!results) {
1842 *nelements = 0;
1843 return NULL;
1844 }
1845 for (i = 0; i < *nelements; i++) {
1846 results[i] = create_glx_visual(dpy, visuals + i);
1847 if (!results[i]) {
1848 *nelements = i;
1849 break;
1850 }
1851 }
1852 return (GLXFBConfig *) results;
1853 }
1854 return NULL;
1855 }
1856
1857
1858 PUBLIC GLXFBConfig *
1859 glXChooseFBConfig(Display *dpy, int screen,
1860 const int *attribList, int *nitems)
1861 {
1862 XMesaVisual xmvis;
1863
1864 /* register ourselves as an extension on this display */
1865 register_with_display(dpy);
1866
1867 if (!attribList || !attribList[0]) {
1868 /* return list of all configs (per GLX_SGIX_fbconfig spec) */
1869 return glXGetFBConfigs(dpy, screen, nitems);
1870 }
1871
1872 xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
1873 if (xmvis) {
1874 GLXFBConfig *config = malloc(sizeof(XMesaVisual));
1875 if (!config) {
1876 *nitems = 0;
1877 return NULL;
1878 }
1879 *nitems = 1;
1880 config[0] = (GLXFBConfig) xmvis;
1881 return (GLXFBConfig *) config;
1882 }
1883 else {
1884 *nitems = 0;
1885 return NULL;
1886 }
1887 }
1888
1889
1890 PUBLIC XVisualInfo *
1891 glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1892 {
1893 if (dpy && config) {
1894 XMesaVisual xmvis = (XMesaVisual) config;
1895 #if 0
1896 return xmvis->vishandle;
1897 #else
1898 /* create a new vishandle - the cached one may be stale */
1899 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1900 if (xmvis->vishandle) {
1901 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1902 }
1903 return xmvis->vishandle;
1904 #endif
1905 }
1906 else {
1907 return NULL;
1908 }
1909 }
1910
1911
1912 PUBLIC GLXWindow
1913 glXCreateWindow(Display *dpy, GLXFBConfig config, Window win,
1914 const int *attribList)
1915 {
1916 XMesaVisual xmvis = (XMesaVisual) config;
1917 XMesaBuffer xmbuf;
1918 if (!xmvis)
1919 return 0;
1920
1921 xmbuf = XMesaCreateWindowBuffer(xmvis, win);
1922 if (!xmbuf)
1923 return 0;
1924
1925 (void) dpy;
1926 (void) attribList; /* Ignored in GLX 1.3 */
1927
1928 return win; /* A hack for now */
1929 }
1930
1931
1932 PUBLIC void
1933 glXDestroyWindow( Display *dpy, GLXWindow window )
1934 {
1935 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable) window);
1936 if (b)
1937 XMesaDestroyBuffer(b);
1938 /* don't destroy X window */
1939 }
1940
1941
1942 /* XXX untested */
1943 PUBLIC GLXPixmap
1944 glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap,
1945 const int *attribList)
1946 {
1947 XMesaVisual v = (XMesaVisual) config;
1948 XMesaBuffer b;
1949 const int *attr;
1950 int target = 0, format = 0, mipmap = 0;
1951 int value;
1952
1953 if (!dpy || !config || !pixmap)
1954 return 0;
1955
1956 for (attr = attribList; attr && *attr; attr++) {
1957 switch (*attr) {
1958 case GLX_TEXTURE_FORMAT_EXT:
1959 attr++;
1960 switch (*attr) {
1961 case GLX_TEXTURE_FORMAT_NONE_EXT:
1962 case GLX_TEXTURE_FORMAT_RGB_EXT:
1963 case GLX_TEXTURE_FORMAT_RGBA_EXT:
1964 format = *attr;
1965 break;
1966 default:
1967 /* error */
1968 return 0;
1969 }
1970 break;
1971 case GLX_TEXTURE_TARGET_EXT:
1972 attr++;
1973 switch (*attr) {
1974 case GLX_TEXTURE_1D_EXT:
1975 case GLX_TEXTURE_2D_EXT:
1976 case GLX_TEXTURE_RECTANGLE_EXT:
1977 target = *attr;
1978 break;
1979 default:
1980 /* error */
1981 return 0;
1982 }
1983 break;
1984 case GLX_MIPMAP_TEXTURE_EXT:
1985 attr++;
1986 if (*attr)
1987 mipmap = 1;
1988 break;
1989 default:
1990 /* error */
1991 return 0;
1992 }
1993 }
1994
1995 if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
1996 if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
1997 &value, GL_TRUE) != Success
1998 || !value) {
1999 return 0; /* error! */
2000 }
2001 }
2002 else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
2003 if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
2004 &value, GL_TRUE) != Success
2005 || !value) {
2006 return 0; /* error! */
2007 }
2008 }
2009 if (mipmap) {
2010 if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
2011 &value, GL_TRUE) != Success
2012 || !value) {
2013 return 0; /* error! */
2014 }
2015 }
2016 if (target == GLX_TEXTURE_1D_EXT) {
2017 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2018 &value, GL_TRUE) != Success
2019 || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
2020 return 0; /* error! */
2021 }
2022 }
2023 else if (target == GLX_TEXTURE_2D_EXT) {
2024 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2025 &value, GL_TRUE) != Success
2026 || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
2027 return 0; /* error! */
2028 }
2029 }
2030 if (target == GLX_TEXTURE_RECTANGLE_EXT) {
2031 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2032 &value, GL_TRUE) != Success
2033 || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
2034 return 0; /* error! */
2035 }
2036 }
2037
2038 if (format || target || mipmap) {
2039 /* texture from pixmap */
2040 b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
2041 }
2042 else {
2043 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
2044 }
2045 if (!b) {
2046 return 0;
2047 }
2048
2049 return pixmap;
2050 }
2051
2052
2053 PUBLIC void
2054 glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
2055 {
2056 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable)pixmap);
2057 if (b)
2058 XMesaDestroyBuffer(b);
2059 /* don't destroy X pixmap */
2060 }
2061
2062
2063 PUBLIC GLXPbuffer
2064 glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList)
2065 {
2066 XMesaVisual xmvis = (XMesaVisual) config;
2067 XMesaBuffer xmbuf;
2068 const int *attrib;
2069 int width = 0, height = 0;
2070 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2071
2072 (void) dpy;
2073
2074 for (attrib = attribList; *attrib; attrib++) {
2075 switch (*attrib) {
2076 case GLX_PBUFFER_WIDTH:
2077 attrib++;
2078 width = *attrib;
2079 break;
2080 case GLX_PBUFFER_HEIGHT:
2081 attrib++;
2082 height = *attrib;
2083 break;
2084 case GLX_PRESERVED_CONTENTS:
2085 attrib++;
2086 preserveContents = *attrib;
2087 break;
2088 case GLX_LARGEST_PBUFFER:
2089 attrib++;
2090 useLargest = *attrib;
2091 break;
2092 default:
2093 return 0;
2094 }
2095 }
2096
2097 if (width == 0 || height == 0)
2098 return 0;
2099
2100 if (width > PBUFFER_MAX_SIZE || height > PBUFFER_MAX_SIZE) {
2101 /* If allocation would have failed and GLX_LARGEST_PBUFFER is set,
2102 * allocate the largest possible buffer.
2103 */
2104 if (useLargest) {
2105 width = PBUFFER_MAX_SIZE;
2106 height = PBUFFER_MAX_SIZE;
2107 }
2108 }
2109
2110 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2111 /* A GLXPbuffer handle must be an X Drawable because that's what
2112 * glXMakeCurrent takes.
2113 */
2114 if (xmbuf) {
2115 xmbuf->largestPbuffer = useLargest;
2116 xmbuf->preservedContents = preserveContents;
2117 return (GLXPbuffer) xmbuf->ws.drawable;
2118 }
2119 else {
2120 return 0;
2121 }
2122 }
2123
2124
2125 PUBLIC void
2126 glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2127 {
2128 XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2129 if (b) {
2130 XMesaDestroyBuffer(b);
2131 }
2132 }
2133
2134
2135 PUBLIC void
2136 glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute,
2137 unsigned int *value)
2138 {
2139 GLuint width, height;
2140 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2141 if (!xmbuf) {
2142 generate_error(dpy, GLXBadDrawable, draw, X_GLXGetDrawableAttributes, False);
2143 return;
2144 }
2145
2146 /* make sure buffer's dimensions are up to date */
2147 xmesa_get_window_size(dpy, xmbuf, &width, &height);
2148
2149 switch (attribute) {
2150 case GLX_WIDTH:
2151 *value = width;
2152 break;
2153 case GLX_HEIGHT:
2154 *value = height;
2155 break;
2156 case GLX_PRESERVED_CONTENTS:
2157 *value = xmbuf->preservedContents;
2158 break;
2159 case GLX_LARGEST_PBUFFER:
2160 *value = xmbuf->largestPbuffer;
2161 break;
2162 case GLX_FBCONFIG_ID:
2163 *value = xmbuf->xm_visual->visinfo->visualid;
2164 return;
2165 #ifdef GLX_EXT_texture_from_pixmap
2166 case GLX_TEXTURE_FORMAT_EXT:
2167 *value = xmbuf->TextureFormat;
2168 break;
2169 case GLX_TEXTURE_TARGET_EXT:
2170 *value = xmbuf->TextureTarget;
2171 break;
2172 case GLX_MIPMAP_TEXTURE_EXT:
2173 *value = xmbuf->TextureMipmap;
2174 break;
2175 #endif
2176
2177 default:
2178 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, true);
2179 return;
2180 }
2181 }
2182
2183
2184 PUBLIC GLXContext
2185 glXCreateNewContext( Display *dpy, GLXFBConfig config,
2186 int renderType, GLXContext shareCtx, Bool direct )
2187 {
2188 XMesaVisual xmvis = (XMesaVisual) config;
2189
2190 if (!dpy || !config ||
2191 (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2192 return 0;
2193
2194 return create_context(dpy, xmvis,
2195 shareCtx ? shareCtx->xmesaContext : NULL,
2196 direct,
2197 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2198 }
2199
2200
2201 PUBLIC int
2202 glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
2203 {
2204 GLXContext glxCtx = ctx;
2205 XMesaContext xmctx = glxCtx->xmesaContext;
2206
2207 (void) dpy;
2208 (void) ctx;
2209
2210 switch (attribute) {
2211 case GLX_FBCONFIG_ID:
2212 *value = xmctx->xm_visual->visinfo->visualid;
2213 break;
2214 case GLX_RENDER_TYPE:
2215 if (xmctx->xm_visual->mesa_visual.rgbMode)
2216 *value = GLX_RGBA_TYPE;
2217 else
2218 *value = GLX_COLOR_INDEX_TYPE;
2219 break;
2220 case GLX_SCREEN:
2221 *value = 0;
2222 return Success;
2223 default:
2224 return GLX_BAD_ATTRIBUTE;
2225 }
2226 return 0;
2227 }
2228
2229
2230 PUBLIC void
2231 glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
2232 {
2233 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2234 if (xmbuf)
2235 xmbuf->selectedEvents = mask;
2236 }
2237
2238
2239 PUBLIC void
2240 glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
2241 {
2242 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2243 if (xmbuf)
2244 *mask = xmbuf->selectedEvents;
2245 else
2246 *mask = 0;
2247 }
2248
2249
2250
2251 /*** GLX_SGI_swap_control ***/
2252
2253 PUBLIC int
2254 glXSwapIntervalSGI(int interval)
2255 {
2256 (void) interval;
2257 return 0;
2258 }
2259
2260
2261
2262 /*** GLX_SGI_video_sync ***/
2263
2264 static unsigned int FrameCounter = 0;
2265
2266 PUBLIC int
2267 glXGetVideoSyncSGI(unsigned int *count)
2268 {
2269 /* this is a bogus implementation */
2270 *count = FrameCounter++;
2271 return 0;
2272 }
2273
2274 PUBLIC int
2275 glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2276 {
2277 if (divisor <= 0 || remainder < 0)
2278 return GLX_BAD_VALUE;
2279 /* this is a bogus implementation */
2280 FrameCounter++;
2281 while (FrameCounter % divisor != remainder)
2282 FrameCounter++;
2283 *count = FrameCounter;
2284 return 0;
2285 }
2286
2287
2288
2289 /*** GLX_SGI_make_current_read ***/
2290
2291 PUBLIC Bool
2292 glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read,
2293 GLXContext ctx)
2294 {
2295 return glXMakeContextCurrent( dpy, draw, read, ctx );
2296 }
2297
2298 /* not used
2299 static GLXDrawable
2300 glXGetCurrentReadDrawableSGI(void)
2301 {
2302 return 0;
2303 }
2304 */
2305
2306
2307 /*** GLX_SGIX_video_source ***/
2308 #if defined(_VL_H)
2309
2310 PUBLIC GLXVideoSourceSGIX
2311 glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server,
2312 VLPath path, int nodeClass, VLNode drainNode)
2313 {
2314 (void) dpy;
2315 (void) screen;
2316 (void) server;
2317 (void) path;
2318 (void) nodeClass;
2319 (void) drainNode;
2320 return 0;
2321 }
2322
2323 PUBLIC void
2324 glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
2325 {
2326 (void) dpy;
2327 (void) src;
2328 }
2329
2330 #endif
2331
2332
2333 /*** GLX_EXT_import_context ***/
2334
2335 PUBLIC void
2336 glXFreeContextEXT(Display *dpy, GLXContext context)
2337 {
2338 (void) dpy;
2339 (void) context;
2340 }
2341
2342 PUBLIC GLXContextID
2343 glXGetContextIDEXT(const GLXContext context)
2344 {
2345 (void) context;
2346 return 0;
2347 }
2348
2349 PUBLIC GLXContext
2350 glXImportContextEXT(Display *dpy, GLXContextID contextID)
2351 {
2352 (void) dpy;
2353 (void) contextID;
2354 return 0;
2355 }
2356
2357 PUBLIC int
2358 glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute,
2359 int *value)
2360 {
2361 (void) dpy;
2362 (void) context;
2363 (void) attribute;
2364 (void) value;
2365 return 0;
2366 }
2367
2368
2369
2370 /*** GLX_SGIX_fbconfig ***/
2371
2372 PUBLIC int
2373 glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config,
2374 int attribute, int *value)
2375 {
2376 return glXGetFBConfigAttrib(dpy, config, attribute, value);
2377 }
2378
2379 PUBLIC GLXFBConfigSGIX *
2380 glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list,
2381 int *nelements)
2382 {
2383 return (GLXFBConfig *) glXChooseFBConfig(dpy, screen,
2384 attrib_list, nelements);
2385 }
2386
2387
2388 PUBLIC GLXPixmap
2389 glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2390 Pixmap pixmap)
2391 {
2392 XMesaVisual xmvis = (XMesaVisual) config;
2393 XMesaBuffer xmbuf = XMesaCreatePixmapBuffer(xmvis, pixmap, 0);
2394 return xmbuf->ws.drawable; /* need to return an X ID */
2395 }
2396
2397
2398 PUBLIC GLXContext
2399 glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config,
2400 int renderType, GLXContext shareCtx,
2401 Bool direct)
2402 {
2403 XMesaVisual xmvis = (XMesaVisual) config;
2404
2405 if (!dpy || !config ||
2406 (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2407 return 0;
2408
2409 return create_context(dpy, xmvis,
2410 shareCtx ? shareCtx->xmesaContext : NULL,
2411 direct,
2412 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
2413 }
2414
2415
2416 PUBLIC XVisualInfo *
2417 glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
2418 {
2419 return glXGetVisualFromFBConfig(dpy, config);
2420 }
2421
2422
2423 PUBLIC GLXFBConfigSGIX
2424 glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
2425 {
2426 XMesaVisual xmvis = find_glx_visual(dpy, vis);
2427 if (!xmvis) {
2428 /* This visual wasn't found with glXChooseVisual() */
2429 xmvis = create_glx_visual(dpy, vis);
2430 }
2431
2432 return (GLXFBConfigSGIX) xmvis;
2433 }
2434
2435
2436
2437 /*** GLX_SGIX_pbuffer ***/
2438
2439 PUBLIC GLXPbufferSGIX
2440 glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
2441 unsigned int width, unsigned int height,
2442 int *attribList)
2443 {
2444 XMesaVisual xmvis = (XMesaVisual) config;
2445 XMesaBuffer xmbuf;
2446 const int *attrib;
2447 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2448
2449 (void) dpy;
2450
2451 for (attrib = attribList; attrib && *attrib; attrib++) {
2452 switch (*attrib) {
2453 case GLX_PRESERVED_CONTENTS_SGIX:
2454 attrib++;
2455 preserveContents = *attrib; /* ignored */
2456 break;
2457 case GLX_LARGEST_PBUFFER_SGIX:
2458 attrib++;
2459 useLargest = *attrib; /* ignored */
2460 break;
2461 default:
2462 return 0;
2463 }
2464 }
2465
2466 /* not used at this time */
2467 (void) useLargest;
2468 (void) preserveContents;
2469
2470 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2471 /* A GLXPbuffer handle must be an X Drawable because that's what
2472 * glXMakeCurrent takes.
2473 */
2474 return (GLXPbuffer) xmbuf->ws.drawable;
2475 }
2476
2477
2478 PUBLIC void
2479 glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
2480 {
2481 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2482 if (xmbuf) {
2483 XMesaDestroyBuffer(xmbuf);
2484 }
2485 }
2486
2487
2488 PUBLIC int
2489 glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute,
2490 unsigned int *value)
2491 {
2492 const XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2493
2494 if (!xmbuf) {
2495 /* Generate GLXBadPbufferSGIX for bad pbuffer */
2496 return 0;
2497 }
2498
2499 switch (attribute) {
2500 case GLX_PRESERVED_CONTENTS_SGIX:
2501 *value = True;
2502 break;
2503 case GLX_LARGEST_PBUFFER_SGIX:
2504 *value = xmesa_buffer_width(xmbuf) * xmesa_buffer_height(xmbuf);
2505 break;
2506 case GLX_WIDTH_SGIX:
2507 *value = xmesa_buffer_width(xmbuf);
2508 break;
2509 case GLX_HEIGHT_SGIX:
2510 *value = xmesa_buffer_height(xmbuf);
2511 break;
2512 case GLX_EVENT_MASK_SGIX:
2513 *value = 0; /* XXX might be wrong */
2514 break;
2515 default:
2516 *value = 0;
2517 }
2518 return 0;
2519 }
2520
2521
2522 PUBLIC void
2523 glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
2524 {
2525 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2526 if (xmbuf) {
2527 /* Note: we'll never generate clobber events */
2528 xmbuf->selectedEvents = mask;
2529 }
2530 }
2531
2532
2533 PUBLIC void
2534 glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable,
2535 unsigned long *mask)
2536 {
2537 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2538 if (xmbuf) {
2539 *mask = xmbuf->selectedEvents;
2540 }
2541 else {
2542 *mask = 0;
2543 }
2544 }
2545
2546
2547
2548 /*** GLX_SGI_cushion ***/
2549
2550 PUBLIC void
2551 glXCushionSGI(Display *dpy, Window win, float cushion)
2552 {
2553 (void) dpy;
2554 (void) win;
2555 (void) cushion;
2556 }
2557
2558
2559
2560 /*** GLX_SGIX_video_resize ***/
2561
2562 PUBLIC int
2563 glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel,
2564 Window window)
2565 {
2566 (void) dpy;
2567 (void) screen;
2568 (void) channel;
2569 (void) window;
2570 return 0;
2571 }
2572
2573 PUBLIC int
2574 glXChannelRectSGIX(Display *dpy, int screen, int channel,
2575 int x, int y, int w, int h)
2576 {
2577 (void) dpy;
2578 (void) screen;
2579 (void) channel;
2580 (void) x;
2581 (void) y;
2582 (void) w;
2583 (void) h;
2584 return 0;
2585 }
2586
2587 PUBLIC int
2588 glXQueryChannelRectSGIX(Display *dpy, int screen, int channel,
2589 int *x, int *y, int *w, int *h)
2590 {
2591 (void) dpy;
2592 (void) screen;
2593 (void) channel;
2594 (void) x;
2595 (void) y;
2596 (void) w;
2597 (void) h;
2598 return 0;
2599 }
2600
2601 PUBLIC int
2602 glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel,
2603 int *dx, int *dy, int *dw, int *dh)
2604 {
2605 (void) dpy;
2606 (void) screen;
2607 (void) channel;
2608 (void) dx;
2609 (void) dy;
2610 (void) dw;
2611 (void) dh;
2612 return 0;
2613 }
2614
2615 PUBLIC int
2616 glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
2617 {
2618 (void) dpy;
2619 (void) screen;
2620 (void) channel;
2621 (void) synctype;
2622 return 0;
2623 }
2624
2625
2626
2627 /*** GLX_SGIX_dmbuffer **/
2628
2629 #if defined(_DM_BUFFER_H_)
2630 PUBLIC Bool
2631 glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer,
2632 DMparams *params, DMbuffer dmbuffer)
2633 {
2634 (void) dpy;
2635 (void) pbuffer;
2636 (void) params;
2637 (void) dmbuffer;
2638 return False;
2639 }
2640 #endif
2641
2642
2643 /*** GLX_SGIX_swap_group ***/
2644
2645 PUBLIC void
2646 glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
2647 {
2648 (void) dpy;
2649 (void) drawable;
2650 (void) member;
2651 }
2652
2653
2654
2655 /*** GLX_SGIX_swap_barrier ***/
2656
2657 PUBLIC void
2658 glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
2659 {
2660 (void) dpy;
2661 (void) drawable;
2662 (void) barrier;
2663 }
2664
2665 PUBLIC Bool
2666 glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2667 {
2668 (void) dpy;
2669 (void) screen;
2670 (void) max;
2671 return False;
2672 }
2673
2674
2675
2676 /*** GLX_SUN_get_transparent_index ***/
2677
2678 PUBLIC Status
2679 glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay,
2680 long *pTransparent)
2681 {
2682 (void) dpy;
2683 (void) overlay;
2684 (void) underlay;
2685 (void) pTransparent;
2686 return 0;
2687 }
2688
2689
2690
2691 /*** GLX_MESA_release_buffers ***/
2692
2693 /*
2694 * Release the depth, stencil, accum buffers attached to a GLXDrawable
2695 * (a window or pixmap) prior to destroying the GLXDrawable.
2696 */
2697 PUBLIC Bool
2698 glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2699 {
2700 XMesaBuffer b = XMesaFindBuffer(dpy, d);
2701 if (b) {
2702 XMesaDestroyBuffer(b);
2703 return True;
2704 }
2705 return False;
2706 }
2707
2708 /*** GLX_EXT_texture_from_pixmap ***/
2709
2710 PUBLIC void
2711 glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
2712 const int *attrib_list)
2713 {
2714 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2715 if (b)
2716 XMesaBindTexImage(dpy, b, buffer, attrib_list);
2717 }
2718
2719 PUBLIC void
2720 glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
2721 {
2722 XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
2723 if (b)
2724 XMesaReleaseTexImage(dpy, b, buffer);
2725 }
2726
2727
2728
2729 /*** GLX_ARB_create_context ***/
2730
2731
2732 GLXContext
2733 glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config,
2734 GLXContext shareCtx, Bool direct,
2735 const int *attrib_list)
2736 {
2737 XMesaVisual xmvis = (XMesaVisual) config;
2738 int majorVersion = 1, minorVersion = 0;
2739 int contextFlags = 0x0;
2740 int profileMask = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
2741 int renderType = GLX_RGBA_TYPE;
2742 unsigned i;
2743 Bool done = False;
2744 const int contextFlagsAll = (GLX_CONTEXT_DEBUG_BIT_ARB |
2745 GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
2746 GLXContext ctx;
2747
2748 /* parse attrib_list */
2749 for (i = 0; !done && attrib_list && attrib_list[i]; i++) {
2750 switch (attrib_list[i]) {
2751 case GLX_CONTEXT_MAJOR_VERSION_ARB:
2752 majorVersion = attrib_list[++i];
2753 break;
2754 case GLX_CONTEXT_MINOR_VERSION_ARB:
2755 minorVersion = attrib_list[++i];
2756 break;
2757 case GLX_CONTEXT_FLAGS_ARB:
2758 contextFlags = attrib_list[++i];
2759 break;
2760 case GLX_CONTEXT_PROFILE_MASK_ARB:
2761 profileMask = attrib_list[++i];
2762 break;
2763 case GLX_RENDER_TYPE:
2764 renderType = attrib_list[++i];
2765 break;
2766 case 0:
2767 /* end of list */
2768 done = True;
2769 break;
2770 default:
2771 /* bad attribute */
2772 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2773 return NULL;
2774 }
2775 }
2776
2777 /* check contextFlags */
2778 if (contextFlags & ~contextFlagsAll) {
2779 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2780 return NULL;
2781 }
2782
2783 /* check profileMask */
2784 if (profileMask != GLX_CONTEXT_CORE_PROFILE_BIT_ARB &&
2785 profileMask != GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB &&
2786 profileMask != GLX_CONTEXT_ES_PROFILE_BIT_EXT) {
2787 generate_error(dpy, GLXBadProfileARB, 0, X_GLXCreateContextAttribsARB, False);
2788 return NULL;
2789 }
2790
2791 /* check renderType */
2792 if (renderType != GLX_RGBA_TYPE &&
2793 renderType != GLX_COLOR_INDEX_TYPE) {
2794 generate_error(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
2795 return NULL;
2796 }
2797
2798 /* check version */
2799 if (majorVersion <= 0 ||
2800 minorVersion < 0 ||
2801 (profileMask != GLX_CONTEXT_ES_PROFILE_BIT_EXT &&
2802 ((majorVersion == 1 && minorVersion > 5) ||
2803 (majorVersion == 2 && minorVersion > 1) ||
2804 (majorVersion == 3 && minorVersion > 3) ||
2805 (majorVersion == 4 && minorVersion > 5) ||
2806 majorVersion > 4))) {
2807 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2808 return NULL;
2809 }
2810 if (profileMask == GLX_CONTEXT_ES_PROFILE_BIT_EXT &&
2811 ((majorVersion == 1 && minorVersion > 1) ||
2812 (majorVersion == 2 && minorVersion > 0) ||
2813 (majorVersion == 3 && minorVersion > 1) ||
2814 majorVersion > 3)) {
2815 /* GLX_EXT_create_context_es2_profile says nothing to justifying a
2816 * different error code for invalid ES versions, but this is what NVIDIA
2817 * does and piglit expects.
2818 */
2819 generate_error(dpy, GLXBadProfileARB, 0, X_GLXCreateContextAttribsARB, False);
2820 return NULL;
2821 }
2822
2823 if ((contextFlags & GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB) &&
2824 majorVersion < 3) {
2825 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2826 return NULL;
2827 }
2828
2829 if (renderType == GLX_COLOR_INDEX_TYPE && majorVersion >= 3) {
2830 generate_error(dpy, BadMatch, 0, X_GLXCreateContextAttribsARB, True);
2831 return NULL;
2832 }
2833
2834 ctx = create_context(dpy, xmvis,
2835 shareCtx ? shareCtx->xmesaContext : NULL,
2836 direct,
2837 majorVersion, minorVersion,
2838 profileMask, contextFlags);
2839 if (!ctx) {
2840 generate_error(dpy, GLXBadFBConfig, 0, X_GLXCreateContextAttribsARB, False);
2841 }
2842
2843 return ctx;
2844 }