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