swr: update gallium driver docs
[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, GLint num_samples )
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->mesa_visual.samples == num_samples
233 && v->ximage_flag == ximageFlag
234 && v->mesa_visual.rgbMode == rgbFlag
235 && v->mesa_visual.doubleBufferMode == dbFlag
236 && v->mesa_visual.stereoMode == stereoFlag
237 && (v->mesa_visual.alphaBits > 0) == alphaFlag
238 && (v->mesa_visual.depthBits >= depth_size || depth_size == 0)
239 && (v->mesa_visual.stencilBits >= stencil_size || stencil_size == 0)
240 && (v->mesa_visual.accumRedBits >= accumRedSize || accumRedSize == 0)
241 && (v->mesa_visual.accumGreenBits >= accumGreenSize || accumGreenSize == 0)
242 && (v->mesa_visual.accumBlueBits >= accumBlueSize || accumBlueSize == 0)
243 && (v->mesa_visual.accumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
244 /* now either compare XVisualInfo pointers or visual IDs */
245 if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
246 || (comparePointers && v->vishandle == vinfo)) {
247 return v;
248 }
249 }
250 }
251
252 /* Create a new visual and add it to the list. */
253
254 xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
255 stereoFlag, ximageFlag,
256 depth_size, stencil_size,
257 accumRedSize, accumBlueSize,
258 accumBlueSize, accumAlphaSize, num_samples, level,
259 GLX_NONE_EXT );
260 if (xmvis) {
261 /* Save a copy of the pointer now so we can find this visual again
262 * if we need to search for it in find_glx_visual().
263 */
264 xmvis->vishandle = vinfo;
265 /* Allocate more space for additional visual */
266 VisualTable = realloc(VisualTable, sizeof(XMesaVisual) * (NumVisuals + 1));
267 /* add xmvis to the list */
268 VisualTable[NumVisuals] = xmvis;
269 NumVisuals++;
270 /* XXX minor hack, because XMesaCreateVisual doesn't support an
271 * aux buffers parameter.
272 */
273 xmvis->mesa_visual.numAuxBuffers = numAuxBuffers;
274 }
275 return xmvis;
276 }
277
278
279 /**
280 * Return the default number of bits for the Z buffer.
281 * If defined, use the MESA_GLX_DEPTH_BITS env var value.
282 * Otherwise, use the DEFAULT_SOFTWARE_DEPTH_BITS constant.
283 * XXX probably do the same thing for stencil, accum, etc.
284 */
285 static GLint
286 default_depth_bits(void)
287 {
288 int zBits;
289 const char *zEnv = getenv("MESA_GLX_DEPTH_BITS");
290 if (zEnv)
291 zBits = atoi(zEnv);
292 else
293 zBits = 24;
294 return zBits;
295 }
296
297 static GLint
298 default_alpha_bits(void)
299 {
300 int aBits;
301 const char *aEnv = getenv("MESA_GLX_ALPHA_BITS");
302 if (aEnv)
303 aBits = atoi(aEnv);
304 else
305 aBits = 0;
306 return aBits;
307 }
308
309 static GLint
310 default_accum_bits(void)
311 {
312 return 16;
313 }
314
315
316
317 /*
318 * Create a GLX visual from a regular XVisualInfo.
319 * This is called when Fake GLX is given an XVisualInfo which wasn't
320 * returned by glXChooseVisual. Since this is the first time we're
321 * considering this visual we'll take a guess at reasonable values
322 * for depth buffer size, stencil size, accum size, etc.
323 * This is the best we can do with a client-side emulation of GLX.
324 */
325 static XMesaVisual
326 create_glx_visual( Display *dpy, XVisualInfo *visinfo )
327 {
328 GLint zBits = default_depth_bits();
329 GLint accBits = default_accum_bits();
330 GLboolean alphaFlag = default_alpha_bits() > 0;
331
332 if (is_usable_visual( visinfo )) {
333 /* Configure this visual as RGB, double-buffered, depth-buffered. */
334 /* This is surely wrong for some people's needs but what else */
335 /* can be done? They should use glXChooseVisual(). */
336 return save_glx_visual( dpy, visinfo,
337 GL_TRUE, /* rgb */
338 alphaFlag, /* alpha */
339 GL_TRUE, /* double */
340 GL_FALSE, /* stereo */
341 zBits,
342 8, /* stencil bits */
343 accBits, /* r */
344 accBits, /* g */
345 accBits, /* b */
346 accBits, /* a */
347 0, /* level */
348 0, /* numAux */
349 0 /* numSamples */
350 );
351 }
352 else {
353 _mesa_warning(NULL, "Mesa: error in glXCreateContext: bad visual\n");
354 return NULL;
355 }
356 }
357
358
359
360 /*
361 * Find the GLX visual associated with an XVisualInfo.
362 */
363 static XMesaVisual
364 find_glx_visual( Display *dpy, XVisualInfo *vinfo )
365 {
366 int i;
367
368 /* try to match visual id */
369 for (i=0;i<NumVisuals;i++) {
370 if (VisualTable[i]->display==dpy
371 && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
372 return VisualTable[i];
373 }
374 }
375
376 /* if that fails, try to match pointers */
377 for (i=0;i<NumVisuals;i++) {
378 if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
379 return VisualTable[i];
380 }
381 }
382
383 return NULL;
384 }
385
386
387 /**
388 * Try to get an X visual which matches the given arguments.
389 */
390 static XVisualInfo *
391 get_visual( Display *dpy, int scr, unsigned int depth, int xclass )
392 {
393 XVisualInfo temp, *vis;
394 long mask;
395 int n;
396 unsigned int default_depth;
397 int default_class;
398
399 mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
400 temp.screen = scr;
401 temp.depth = depth;
402 temp.CLASS = xclass;
403
404 default_depth = DefaultDepth(dpy,scr);
405 default_class = DefaultVisual(dpy,scr)->CLASS;
406
407 if (depth==default_depth && xclass==default_class) {
408 /* try to get root window's visual */
409 temp.visualid = DefaultVisual(dpy,scr)->visualid;
410 mask |= VisualIDMask;
411 }
412
413 vis = XGetVisualInfo( dpy, mask, &temp, &n );
414
415 /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
416 * An SGI Infinite Reality system, for example, can have 30bpp pixels:
417 * 10 bits per color channel. Mesa's limited to a max of 8 bits/channel.
418 */
419 if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
420 if (_mesa_bitcount((GLuint) vis->red_mask ) <= 8 &&
421 _mesa_bitcount((GLuint) vis->green_mask) <= 8 &&
422 _mesa_bitcount((GLuint) vis->blue_mask ) <= 8) {
423 return vis;
424 }
425 else {
426 free((void *) vis);
427 return NULL;
428 }
429 }
430
431 return vis;
432 }
433
434
435 /*
436 * Retrieve the value of the given environment variable and find
437 * the X visual which matches it.
438 * Input: dpy - the display
439 * screen - the screen number
440 * varname - the name of the environment variable
441 * Return: an XVisualInfo pointer to NULL if error.
442 */
443 static XVisualInfo *
444 get_env_visual(Display *dpy, int scr, const char *varname)
445 {
446 char value[100], type[100];
447 int depth, xclass = -1;
448 XVisualInfo *vis;
449
450 if (!getenv( varname )) {
451 return NULL;
452 }
453
454 strncpy( value, getenv(varname), 100 );
455 value[99] = 0;
456
457 sscanf( value, "%s %d", type, &depth );
458
459 if (strcmp(type,"TrueColor")==0) xclass = TrueColor;
460 else if (strcmp(type,"DirectColor")==0) xclass = DirectColor;
461 else if (strcmp(type,"PseudoColor")==0) xclass = PseudoColor;
462 else if (strcmp(type,"StaticColor")==0) xclass = StaticColor;
463 else if (strcmp(type,"GrayScale")==0) xclass = GrayScale;
464 else if (strcmp(type,"StaticGray")==0) xclass = StaticGray;
465
466 if (xclass>-1 && depth>0) {
467 vis = get_visual( dpy, scr, depth, xclass );
468 if (vis) {
469 return vis;
470 }
471 }
472
473 _mesa_warning(NULL, "GLX unable to find visual class=%s, depth=%d.",
474 type, depth);
475
476 return NULL;
477 }
478
479
480
481 /*
482 * Select an X visual which satisfies the RGBA flag and minimum depth.
483 * Input: dpy,
484 * screen - X display and screen number
485 * min_depth - minimum visual depth
486 * preferred_class - preferred GLX visual class or DONT_CARE
487 * Return: pointer to an XVisualInfo or NULL.
488 */
489 static XVisualInfo *
490 choose_x_visual( Display *dpy, int screen, int min_depth,
491 int preferred_class )
492 {
493 XVisualInfo *vis;
494 int xclass, visclass = 0;
495 int depth;
496
497 /* First see if the MESA_RGB_VISUAL env var is defined */
498 vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
499 if (vis) {
500 return vis;
501 }
502 /* Otherwise, search for a suitable visual */
503 if (preferred_class==DONT_CARE) {
504 for (xclass=0;xclass<6;xclass++) {
505 switch (xclass) {
506 case 0: visclass = TrueColor; break;
507 case 1: visclass = DirectColor; break;
508 case 2: visclass = PseudoColor; break;
509 case 3: visclass = StaticColor; break;
510 case 4: visclass = GrayScale; break;
511 case 5: visclass = StaticGray; break;
512 }
513 if (min_depth==0) {
514 /* start with shallowest */
515 for (depth=0;depth<=32;depth++) {
516 if (visclass==TrueColor && depth==8) {
517 /* Special case: try to get 8-bit PseudoColor before */
518 /* 8-bit TrueColor */
519 vis = get_visual( dpy, screen, 8, PseudoColor );
520 if (vis) {
521 return vis;
522 }
523 }
524 vis = get_visual( dpy, screen, depth, visclass );
525 if (vis) {
526 return vis;
527 }
528 }
529 }
530 else {
531 /* start with deepest */
532 for (depth=32;depth>=min_depth;depth--) {
533 if (visclass==TrueColor && depth==8) {
534 /* Special case: try to get 8-bit PseudoColor before */
535 /* 8-bit TrueColor */
536 vis = get_visual( dpy, screen, 8, PseudoColor );
537 if (vis) {
538 return vis;
539 }
540 }
541 vis = get_visual( dpy, screen, depth, visclass );
542 if (vis) {
543 return vis;
544 }
545 }
546 }
547 }
548 }
549 else {
550 /* search for a specific visual class */
551 switch (preferred_class) {
552 case GLX_TRUE_COLOR_EXT: visclass = TrueColor; break;
553 case GLX_DIRECT_COLOR_EXT: visclass = DirectColor; break;
554 case GLX_PSEUDO_COLOR_EXT: visclass = PseudoColor; break;
555 case GLX_STATIC_COLOR_EXT: visclass = StaticColor; break;
556 case GLX_GRAY_SCALE_EXT: visclass = GrayScale; break;
557 case GLX_STATIC_GRAY_EXT: visclass = StaticGray; break;
558 default: return NULL;
559 }
560 if (min_depth==0) {
561 /* start with shallowest */
562 for (depth=0;depth<=32;depth++) {
563 vis = get_visual( dpy, screen, depth, visclass );
564 if (vis) {
565 return vis;
566 }
567 }
568 }
569 else {
570 /* start with deepest */
571 for (depth=32;depth>=min_depth;depth--) {
572 vis = get_visual( dpy, screen, depth, visclass );
573 if (vis) {
574 return vis;
575 }
576 }
577 }
578 }
579
580 /* didn't find a visual */
581 return NULL;
582 }
583
584
585
586
587 /**********************************************************************/
588 /*** Display-related functions ***/
589 /**********************************************************************/
590
591
592 /**
593 * Free all XMesaVisuals which are associated with the given display.
594 */
595 static void
596 destroy_visuals_on_display(Display *dpy)
597 {
598 int i;
599 for (i = 0; i < NumVisuals; i++) {
600 if (VisualTable[i]->display == dpy) {
601 /* remove this visual */
602 int j;
603 free(VisualTable[i]);
604 for (j = i; j < NumVisuals - 1; j++)
605 VisualTable[j] = VisualTable[j + 1];
606 NumVisuals--;
607 }
608 }
609 }
610
611
612 /**
613 * Called from XCloseDisplay() to let us free our display-related data.
614 */
615 static int
616 close_display_callback(Display *dpy, XExtCodes *codes)
617 {
618 xmesa_destroy_buffers_on_display(dpy);
619 destroy_visuals_on_display(dpy);
620 xmesa_close_display(dpy);
621 return 0;
622 }
623
624
625 /**
626 * Look for the named extension on given display and return a pointer
627 * to the _XExtension data, or NULL if extension not found.
628 */
629 static _XExtension *
630 lookup_extension(Display *dpy, const char *extName)
631 {
632 _XExtension *ext;
633 for (ext = dpy->ext_procs; ext; ext = ext->next) {
634 if (ext->name && strcmp(ext->name, extName) == 0) {
635 return ext;
636 }
637 }
638 return NULL;
639 }
640
641
642 /**
643 * Whenever we're given a new Display pointer, call this function to
644 * register our close_display_callback function.
645 */
646 static void
647 register_with_display(Display *dpy)
648 {
649 const char *extName = "MesaGLX";
650 _XExtension *ext;
651
652 ext = lookup_extension(dpy, extName);
653 if (!ext) {
654 XExtCodes *c = XAddExtension(dpy);
655 ext = dpy->ext_procs; /* new extension is at head of list */
656 assert(c->extension == ext->codes.extension);
657 (void) c;
658 ext->name = strdup(extName);
659 ext->close_display = close_display_callback;
660 }
661 }
662
663
664 /**
665 * Fake an error.
666 */
667 static int
668 generate_error(Display *dpy,
669 unsigned char error_code,
670 XID resourceid,
671 unsigned char minor_code,
672 Bool core)
673 {
674 XErrorHandler handler;
675 int major_opcode;
676 int first_event;
677 int first_error;
678 XEvent event;
679
680 handler = XSetErrorHandler(NULL);
681 XSetErrorHandler(handler);
682 if (!handler) {
683 return 0;
684 }
685
686 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_opcode, &first_event, &first_error)) {
687 major_opcode = 0;
688 first_event = 0;
689 first_error = 0;
690 }
691
692 if (!core) {
693 error_code += first_error;
694 }
695
696 memset(&event, 0, sizeof event);
697
698 event.xerror.type = X_Error;
699 event.xerror.display = dpy;
700 event.xerror.resourceid = resourceid;
701 event.xerror.serial = NextRequest(dpy) - 1;
702 event.xerror.error_code = error_code;
703 event.xerror.request_code = major_opcode;
704 event.xerror.minor_code = minor_code;
705
706 return handler(dpy, &event.xerror);
707 }
708
709
710 /**********************************************************************/
711 /*** Begin Fake GLX API Functions ***/
712 /**********************************************************************/
713
714
715 /**
716 * Helper used by glXChooseVisual and glXChooseFBConfig.
717 * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
718 * the later.
719 * In either case, the attribute list is terminated with the value 'None'.
720 */
721 static XMesaVisual
722 choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
723 {
724 const GLboolean rgbModeDefault = fbConfig;
725 const int *parselist;
726 XVisualInfo *vis;
727 int min_red=0, min_green=0, min_blue=0;
728 GLboolean rgb_flag = rgbModeDefault;
729 GLboolean alpha_flag = GL_FALSE;
730 GLboolean double_flag = GL_FALSE;
731 GLboolean stereo_flag = GL_FALSE;
732 GLint depth_size = 0;
733 GLint stencil_size = 0;
734 GLint accumRedSize = 0;
735 GLint accumGreenSize = 0;
736 GLint accumBlueSize = 0;
737 GLint accumAlphaSize = 0;
738 int level = 0;
739 int visual_type = DONT_CARE;
740 GLint caveat = DONT_CARE;
741 XMesaVisual xmvis = NULL;
742 int desiredVisualID = -1;
743 int numAux = 0;
744 GLint num_samples = 0;
745
746 xmesa_init( dpy );
747
748 parselist = list;
749
750 while (*parselist) {
751
752 if (fbConfig &&
753 parselist[1] == GLX_DONT_CARE &&
754 parselist[0] != GLX_LEVEL) {
755 /* For glXChooseFBConfig(), skip attributes whose value is
756 * GLX_DONT_CARE, unless it's GLX_LEVEL (which can legitimately be
757 * a negative value).
758 *
759 * From page 17 (23 of the pdf) of the GLX 1.4 spec:
760 * GLX DONT CARE may be specified for all attributes except GLX LEVEL.
761 */
762 parselist += 2;
763 continue;
764 }
765
766 switch (*parselist) {
767 case GLX_USE_GL:
768 if (fbConfig) {
769 /* invalid token */
770 return NULL;
771 }
772 else {
773 /* skip */
774 parselist++;
775 }
776 break;
777 case GLX_BUFFER_SIZE:
778 parselist++;
779 parselist++;
780 break;
781 case GLX_LEVEL:
782 parselist++;
783 level = *parselist++;
784 break;
785 case GLX_RGBA:
786 if (fbConfig) {
787 /* invalid token */
788 return NULL;
789 }
790 else {
791 rgb_flag = GL_TRUE;
792 parselist++;
793 }
794 break;
795 case GLX_DOUBLEBUFFER:
796 parselist++;
797 if (fbConfig) {
798 double_flag = *parselist++;
799 }
800 else {
801 double_flag = GL_TRUE;
802 }
803 break;
804 case GLX_STEREO:
805 parselist++;
806 if (fbConfig) {
807 stereo_flag = *parselist++;
808 }
809 else {
810 stereo_flag = GL_TRUE;
811 }
812 break;
813 case GLX_AUX_BUFFERS:
814 parselist++;
815 numAux = *parselist++;
816 if (numAux > MAX_AUX_BUFFERS)
817 return NULL;
818 break;
819 case GLX_RED_SIZE:
820 parselist++;
821 min_red = *parselist++;
822 break;
823 case GLX_GREEN_SIZE:
824 parselist++;
825 min_green = *parselist++;
826 break;
827 case GLX_BLUE_SIZE:
828 parselist++;
829 min_blue = *parselist++;
830 break;
831 case GLX_ALPHA_SIZE:
832 parselist++;
833 {
834 GLint size = *parselist++;
835 alpha_flag = size ? GL_TRUE : GL_FALSE;
836 }
837 break;
838 case GLX_DEPTH_SIZE:
839 parselist++;
840 depth_size = *parselist++;
841 break;
842 case GLX_STENCIL_SIZE:
843 parselist++;
844 stencil_size = *parselist++;
845 break;
846 case GLX_ACCUM_RED_SIZE:
847 parselist++;
848 {
849 GLint size = *parselist++;
850 accumRedSize = MAX2( accumRedSize, size );
851 }
852 break;
853 case GLX_ACCUM_GREEN_SIZE:
854 parselist++;
855 {
856 GLint size = *parselist++;
857 accumGreenSize = MAX2( accumGreenSize, size );
858 }
859 break;
860 case GLX_ACCUM_BLUE_SIZE:
861 parselist++;
862 {
863 GLint size = *parselist++;
864 accumBlueSize = MAX2( accumBlueSize, size );
865 }
866 break;
867 case GLX_ACCUM_ALPHA_SIZE:
868 parselist++;
869 {
870 GLint size = *parselist++;
871 accumAlphaSize = MAX2( accumAlphaSize, size );
872 }
873 break;
874
875 /*
876 * GLX_EXT_visual_info extension
877 */
878 case GLX_X_VISUAL_TYPE_EXT:
879 parselist++;
880 visual_type = *parselist++;
881 break;
882 case GLX_TRANSPARENT_TYPE_EXT:
883 parselist++;
884 parselist++;
885 break;
886 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
887 parselist++;
888 parselist++;
889 break;
890 case GLX_TRANSPARENT_RED_VALUE_EXT:
891 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
892 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
893 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
894 /* ignore */
895 parselist++;
896 parselist++;
897 break;
898
899 /*
900 * GLX_EXT_visual_info extension
901 */
902 case GLX_VISUAL_CAVEAT_EXT:
903 parselist++;
904 caveat = *parselist++; /* ignored for now */
905 break;
906
907 /*
908 * GLX_ARB_multisample
909 */
910 case GLX_SAMPLE_BUFFERS_ARB:
911 /* ignore */
912 parselist++;
913 parselist++;
914 break;
915 case GLX_SAMPLES_ARB:
916 parselist++;
917 num_samples = *parselist++;
918 break;
919
920 /*
921 * FBConfig attribs.
922 */
923 case GLX_RENDER_TYPE:
924 if (!fbConfig)
925 return NULL;
926 parselist++;
927 if (*parselist & GLX_RGBA_BIT) {
928 rgb_flag = GL_TRUE;
929 }
930 else if (*parselist & GLX_COLOR_INDEX_BIT) {
931 rgb_flag = GL_FALSE;
932 }
933 else if (*parselist == 0) {
934 rgb_flag = GL_TRUE;
935 }
936 parselist++;
937 break;
938 case GLX_DRAWABLE_TYPE:
939 if (!fbConfig)
940 return NULL;
941 parselist++;
942 if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
943 return NULL; /* bad bit */
944 }
945 parselist++;
946 break;
947 case GLX_FBCONFIG_ID:
948 case GLX_VISUAL_ID:
949 if (!fbConfig)
950 return NULL;
951 parselist++;
952 desiredVisualID = *parselist++;
953 break;
954 case GLX_X_RENDERABLE:
955 case GLX_MAX_PBUFFER_WIDTH:
956 case GLX_MAX_PBUFFER_HEIGHT:
957 case GLX_MAX_PBUFFER_PIXELS:
958 if (!fbConfig)
959 return NULL; /* invalid config option */
960 parselist += 2; /* ignore the parameter */
961 break;
962
963 case GLX_BIND_TO_TEXTURE_RGB_EXT:
964 parselist++; /*skip*/
965 break;
966 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
967 parselist++; /*skip*/
968 break;
969 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
970 parselist++; /*skip*/
971 break;
972 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
973 parselist++;
974 if (*parselist & ~(GLX_TEXTURE_1D_BIT_EXT |
975 GLX_TEXTURE_2D_BIT_EXT |
976 GLX_TEXTURE_RECTANGLE_BIT_EXT)) {
977 /* invalid bit */
978 return NULL;
979 }
980 break;
981 case GLX_Y_INVERTED_EXT:
982 parselist++; /*skip*/
983 break;
984
985 case None:
986 /* end of list */
987 break;
988
989 default:
990 /* undefined attribute */
991 _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
992 *parselist);
993 return NULL;
994 }
995 }
996
997 (void) caveat;
998
999
1000 /*
1001 * Since we're only simulating the GLX extension this function will never
1002 * find any real GL visuals. Instead, all we can do is try to find an RGB
1003 * or CI visual of appropriate depth. Other requested attributes such as
1004 * double buffering, depth buffer, etc. will be associated with the X
1005 * visual and stored in the VisualTable[].
1006 */
1007 if (desiredVisualID != -1) {
1008 /* try to get a specific visual, by visualID */
1009 XVisualInfo temp;
1010 int n;
1011 temp.visualid = desiredVisualID;
1012 temp.screen = screen;
1013 vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
1014 if (vis) {
1015 /* give the visual some useful GLX attributes */
1016 double_flag = GL_TRUE;
1017 rgb_flag = GL_TRUE;
1018 }
1019 }
1020 else if (level==0) {
1021 /* normal color planes */
1022 /* Get an RGB visual */
1023 int min_rgb = min_red + min_green + min_blue;
1024 if (min_rgb>1 && min_rgb<8) {
1025 /* a special case to be sure we can get a monochrome visual */
1026 min_rgb = 1;
1027 }
1028 vis = choose_x_visual( dpy, screen, min_rgb, visual_type );
1029 }
1030 else {
1031 _mesa_warning(NULL, "overlay not supported");
1032 return NULL;
1033 }
1034
1035 if (vis) {
1036 /* Note: we're not exactly obeying the glXChooseVisual rules here.
1037 * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1038 * largest depth buffer size, which is 32bits/value. Instead, we
1039 * return 16 to maintain performance with earlier versions of Mesa.
1040 */
1041 if (stencil_size > 0)
1042 depth_size = 24; /* if Z and stencil, always use 24+8 format */
1043 else if (depth_size > 24)
1044 depth_size = 32;
1045 else if (depth_size > 16)
1046 depth_size = 24;
1047 else if (depth_size > 0) {
1048 depth_size = default_depth_bits();
1049 }
1050
1051 if (!alpha_flag) {
1052 alpha_flag = default_alpha_bits() > 0;
1053 }
1054
1055 /* we only support one size of stencil and accum buffers. */
1056 if (stencil_size > 0)
1057 stencil_size = 8;
1058
1059 if (accumRedSize > 0 ||
1060 accumGreenSize > 0 ||
1061 accumBlueSize > 0 ||
1062 accumAlphaSize > 0) {
1063
1064 accumRedSize =
1065 accumGreenSize =
1066 accumBlueSize = default_accum_bits();
1067
1068 accumAlphaSize = alpha_flag ? accumRedSize : 0;
1069 }
1070
1071 xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1072 stereo_flag, depth_size, stencil_size,
1073 accumRedSize, accumGreenSize,
1074 accumBlueSize, accumAlphaSize, level, numAux,
1075 num_samples );
1076 }
1077
1078 return xmvis;
1079 }
1080
1081
1082 PUBLIC XVisualInfo *
1083 glXChooseVisual( Display *dpy, int screen, int *list )
1084 {
1085 XMesaVisual xmvis;
1086
1087 /* register ourselves as an extension on this display */
1088 register_with_display(dpy);
1089
1090 xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1091 if (xmvis) {
1092 /* create a new vishandle - the cached one may be stale */
1093 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1094 if (xmvis->vishandle) {
1095 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1096 }
1097 return xmvis->vishandle;
1098 }
1099 else
1100 return NULL;
1101 }
1102
1103
1104 /**
1105 * Helper function used by other glXCreateContext functions.
1106 */
1107 static GLXContext
1108 create_context(Display *dpy, XMesaVisual xmvis,
1109 XMesaContext shareCtx, Bool direct,
1110 unsigned major, unsigned minor,
1111 unsigned profileMask, unsigned contextFlags)
1112 {
1113 GLXContext glxCtx;
1114
1115 if (!dpy || !xmvis)
1116 return 0;
1117
1118 glxCtx = CALLOC_STRUCT(__GLXcontextRec);
1119 if (!glxCtx)
1120 return 0;
1121
1122 /* deallocate unused windows/buffers */
1123 #if 0
1124 XMesaGarbageCollect();
1125 #endif
1126
1127 glxCtx->xmesaContext = XMesaCreateContext(xmvis, shareCtx, major, minor,
1128 profileMask, contextFlags);
1129 if (!glxCtx->xmesaContext) {
1130 free(glxCtx);
1131 return NULL;
1132 }
1133
1134 glxCtx->isDirect = DEFAULT_DIRECT;
1135 glxCtx->currentDpy = dpy;
1136 glxCtx->xid = (XID) glxCtx; /* self pointer */
1137
1138 return glxCtx;
1139 }
1140
1141
1142 PUBLIC GLXContext
1143 glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1144 GLXContext shareCtx, Bool direct )
1145 {
1146 XMesaVisual xmvis;
1147
1148 xmvis = find_glx_visual( dpy, visinfo );
1149 if (!xmvis) {
1150 /* This visual wasn't found with glXChooseVisual() */
1151 xmvis = create_glx_visual( dpy, visinfo );
1152 if (!xmvis) {
1153 /* unusable visual */
1154 return NULL;
1155 }
1156 }
1157
1158 return create_context(dpy, xmvis,
1159 shareCtx ? shareCtx->xmesaContext : NULL,
1160 direct,
1161 1, 0, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, 0x0);
1162 }
1163
1164
1165 /* XXX these may have to be removed due to thread-safety issues. */
1166 static GLXContext MakeCurrent_PrevContext = 0;
1167 static GLXDrawable MakeCurrent_PrevDrawable = 0;
1168 static GLXDrawable MakeCurrent_PrevReadable = 0;
1169 static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1170 static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1171
1172
1173 /* GLX 1.3 and later */
1174 PUBLIC Bool
1175 glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1176 GLXDrawable read, GLXContext ctx )
1177 {
1178 GLXContext glxCtx = ctx;
1179 static boolean firsttime = 1, no_rast = 0;
1180
1181 if (firsttime) {
1182 no_rast = getenv("SP_NO_RAST") != NULL;
1183 firsttime = 0;
1184 }
1185
1186 if (ctx && draw && read) {
1187 XMesaBuffer drawBuffer, readBuffer;
1188 XMesaContext xmctx = glxCtx->xmesaContext;
1189
1190 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1191 if (ctx == MakeCurrent_PrevContext
1192 && draw == MakeCurrent_PrevDrawable) {
1193 drawBuffer = MakeCurrent_PrevDrawBuffer;
1194 }
1195 else {
1196 drawBuffer = XMesaFindBuffer( dpy, draw );
1197 }
1198 if (!drawBuffer) {
1199 /* drawable must be a new window! */
1200 drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1201 if (!drawBuffer) {
1202 /* Out of memory, or context/drawable depth mismatch */
1203 return False;
1204 }
1205 }
1206
1207 /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1208 if (ctx == MakeCurrent_PrevContext
1209 && read == MakeCurrent_PrevReadable) {
1210 readBuffer = MakeCurrent_PrevReadBuffer;
1211 }
1212 else {
1213 readBuffer = XMesaFindBuffer( dpy, read );
1214 }
1215 if (!readBuffer) {
1216 /* drawable must be a new window! */
1217 readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1218 if (!readBuffer) {
1219 /* Out of memory, or context/drawable depth mismatch */
1220 return False;
1221 }
1222 }
1223
1224 if (no_rast &&
1225 MakeCurrent_PrevContext == ctx &&
1226 MakeCurrent_PrevDrawable == draw &&
1227 MakeCurrent_PrevReadable == read &&
1228 MakeCurrent_PrevDrawBuffer == drawBuffer &&
1229 MakeCurrent_PrevReadBuffer == readBuffer)
1230 return True;
1231
1232 MakeCurrent_PrevContext = ctx;
1233 MakeCurrent_PrevDrawable = draw;
1234 MakeCurrent_PrevReadable = read;
1235 MakeCurrent_PrevDrawBuffer = drawBuffer;
1236 MakeCurrent_PrevReadBuffer = readBuffer;
1237
1238 /* Now make current! */
1239 if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1240 ctx->currentDpy = dpy;
1241 ctx->currentDrawable = draw;
1242 ctx->currentReadable = read;
1243 SetCurrentContext(ctx);
1244 return True;
1245 }
1246 else {
1247 return False;
1248 }
1249 }
1250 else if (!ctx && !draw && !read) {
1251 /* release current context w/out assigning new one. */
1252 XMesaMakeCurrent2( NULL, NULL, NULL );
1253 MakeCurrent_PrevContext = 0;
1254 MakeCurrent_PrevDrawable = 0;
1255 MakeCurrent_PrevReadable = 0;
1256 MakeCurrent_PrevDrawBuffer = 0;
1257 MakeCurrent_PrevReadBuffer = 0;
1258 SetCurrentContext(NULL);
1259 return True;
1260 }
1261 else {
1262 /* The args must either all be non-zero or all zero.
1263 * This is an error.
1264 */
1265 return False;
1266 }
1267 }
1268
1269
1270 PUBLIC Bool
1271 glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1272 {
1273 return glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1274 }
1275
1276
1277 PUBLIC GLXContext
1278 glXGetCurrentContext(void)
1279 {
1280 return GetCurrentContext();
1281 }
1282
1283
1284 PUBLIC Display *
1285 glXGetCurrentDisplay(void)
1286 {
1287 GLXContext glxCtx = glXGetCurrentContext();
1288
1289 return glxCtx ? glxCtx->currentDpy : NULL;
1290 }
1291
1292
1293 PUBLIC Display *
1294 glXGetCurrentDisplayEXT(void)
1295 {
1296 return glXGetCurrentDisplay();
1297 }
1298
1299
1300 PUBLIC GLXDrawable
1301 glXGetCurrentDrawable(void)
1302 {
1303 GLXContext gc = glXGetCurrentContext();
1304 return gc ? gc->currentDrawable : 0;
1305 }
1306
1307
1308 PUBLIC GLXDrawable
1309 glXGetCurrentReadDrawable(void)
1310 {
1311 GLXContext gc = glXGetCurrentContext();
1312 return gc ? gc->currentReadable : 0;
1313 }
1314
1315
1316 PUBLIC GLXDrawable
1317 glXGetCurrentReadDrawableSGI(void)
1318 {
1319 return glXGetCurrentReadDrawable();
1320 }
1321
1322
1323 PUBLIC GLXPixmap
1324 glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1325 {
1326 XMesaVisual v;
1327 XMesaBuffer b;
1328
1329 v = find_glx_visual( dpy, visinfo );
1330 if (!v) {
1331 v = create_glx_visual( dpy, visinfo );
1332 if (!v) {
1333 /* unusable visual */
1334 return 0;
1335 }
1336 }
1337
1338 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1339 if (!b) {
1340 return 0;
1341 }
1342 return b->ws.drawable;
1343 }
1344
1345
1346 /*** GLX_MESA_pixmap_colormap ***/
1347
1348 PUBLIC GLXPixmap
1349 glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1350 Pixmap pixmap, Colormap cmap )
1351 {
1352 XMesaVisual v;
1353 XMesaBuffer b;
1354
1355 v = find_glx_visual( dpy, visinfo );
1356 if (!v) {
1357 v = create_glx_visual( dpy, visinfo );
1358 if (!v) {
1359 /* unusable visual */
1360 return 0;
1361 }
1362 }
1363
1364 b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1365 if (!b) {
1366 return 0;
1367 }
1368 return b->ws.drawable;
1369 }
1370
1371
1372 PUBLIC void
1373 glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1374 {
1375 XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1376 if (b) {
1377 XMesaDestroyBuffer(b);
1378 }
1379 else if (getenv("MESA_DEBUG")) {
1380 _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1381 }
1382 }
1383
1384
1385 PUBLIC void
1386 glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1387 unsigned long mask )
1388 {
1389 XMesaContext xm_src = src->xmesaContext;
1390 XMesaContext xm_dst = dst->xmesaContext;
1391 (void) dpy;
1392 if (MakeCurrent_PrevContext == src) {
1393 glFlush();
1394 }
1395 XMesaCopyContext(xm_src, xm_dst, mask);
1396 }
1397
1398
1399 PUBLIC Bool
1400 glXQueryExtension( Display *dpy, int *errorBase, int *eventBase )
1401 {
1402 int op, ev, err;
1403 /* Mesa's GLX isn't really an X extension but we try to act like one. */
1404 if (!XQueryExtension(dpy, GLX_EXTENSION_NAME, &op, &ev, &err))
1405 ev = err = 0;
1406 if (errorBase)
1407 *errorBase = err;
1408 if (eventBase)
1409 *eventBase = ev;
1410 return True; /* we're faking GLX so always return success */
1411 }
1412
1413
1414 PUBLIC void
1415 glXDestroyContext( Display *dpy, GLXContext ctx )
1416 {
1417 if (ctx) {
1418 GLXContext glxCtx = ctx;
1419 (void) dpy;
1420 MakeCurrent_PrevContext = 0;
1421 MakeCurrent_PrevDrawable = 0;
1422 MakeCurrent_PrevReadable = 0;
1423 MakeCurrent_PrevDrawBuffer = 0;
1424 MakeCurrent_PrevReadBuffer = 0;
1425 XMesaDestroyContext( glxCtx->xmesaContext );
1426 XMesaGarbageCollect();
1427 free(glxCtx);
1428 }
1429 }
1430
1431
1432 PUBLIC Bool
1433 glXIsDirect( Display *dpy, GLXContext ctx )
1434 {
1435 return ctx ? ctx->isDirect : False;
1436 }
1437
1438
1439
1440 PUBLIC void
1441 glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1442 {
1443 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1444 static boolean firsttime = 1, no_rast = 0;
1445
1446 if (firsttime) {
1447 no_rast = getenv("SP_NO_RAST") != NULL;
1448 firsttime = 0;
1449 }
1450
1451 if (no_rast)
1452 return;
1453
1454 if (buffer) {
1455 XMesaSwapBuffers(buffer);
1456 }
1457 else if (getenv("MESA_DEBUG")) {
1458 _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1459 (int) drawable);
1460 }
1461 }
1462
1463
1464
1465 /*** GLX_MESA_copy_sub_buffer ***/
1466
1467 PUBLIC void
1468 glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
1469 int x, int y, int width, int height)
1470 {
1471 XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1472 if (buffer) {
1473 XMesaCopySubBuffer(buffer, x, y, width, height);
1474 }
1475 else if (getenv("MESA_DEBUG")) {
1476 _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1477 }
1478 }
1479
1480
1481 PUBLIC Bool
1482 glXQueryVersion( Display *dpy, int *maj, int *min )
1483 {
1484 (void) dpy;
1485 /* Return GLX version, not Mesa version */
1486 assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1487 *maj = CLIENT_MAJOR_VERSION;
1488 *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1489 return True;
1490 }
1491
1492
1493 /*
1494 * Query the GLX attributes of the given XVisualInfo.
1495 */
1496 static int
1497 get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1498 {
1499 assert(xmvis);
1500 switch(attrib) {
1501 case GLX_USE_GL:
1502 if (fbconfig)
1503 return GLX_BAD_ATTRIBUTE;
1504 *value = (int) True;
1505 return 0;
1506 case GLX_BUFFER_SIZE:
1507 *value = xmvis->visinfo->depth;
1508 return 0;
1509 case GLX_LEVEL:
1510 *value = xmvis->mesa_visual.level;
1511 return 0;
1512 case GLX_RGBA:
1513 if (fbconfig)
1514 return GLX_BAD_ATTRIBUTE;
1515 if (xmvis->mesa_visual.rgbMode) {
1516 *value = True;
1517 }
1518 else {
1519 *value = False;
1520 }
1521 return 0;
1522 case GLX_DOUBLEBUFFER:
1523 *value = (int) xmvis->mesa_visual.doubleBufferMode;
1524 return 0;
1525 case GLX_STEREO:
1526 *value = (int) xmvis->mesa_visual.stereoMode;
1527 return 0;
1528 case GLX_AUX_BUFFERS:
1529 *value = xmvis->mesa_visual.numAuxBuffers;
1530 return 0;
1531 case GLX_RED_SIZE:
1532 *value = xmvis->mesa_visual.redBits;
1533 return 0;
1534 case GLX_GREEN_SIZE:
1535 *value = xmvis->mesa_visual.greenBits;
1536 return 0;
1537 case GLX_BLUE_SIZE:
1538 *value = xmvis->mesa_visual.blueBits;
1539 return 0;
1540 case GLX_ALPHA_SIZE:
1541 *value = xmvis->mesa_visual.alphaBits;
1542 return 0;
1543 case GLX_DEPTH_SIZE:
1544 *value = xmvis->mesa_visual.depthBits;
1545 return 0;
1546 case GLX_STENCIL_SIZE:
1547 *value = xmvis->mesa_visual.stencilBits;
1548 return 0;
1549 case GLX_ACCUM_RED_SIZE:
1550 *value = xmvis->mesa_visual.accumRedBits;
1551 return 0;
1552 case GLX_ACCUM_GREEN_SIZE:
1553 *value = xmvis->mesa_visual.accumGreenBits;
1554 return 0;
1555 case GLX_ACCUM_BLUE_SIZE:
1556 *value = xmvis->mesa_visual.accumBlueBits;
1557 return 0;
1558 case GLX_ACCUM_ALPHA_SIZE:
1559 *value = xmvis->mesa_visual.accumAlphaBits;
1560 return 0;
1561
1562 /*
1563 * GLX_EXT_visual_info extension
1564 */
1565 case GLX_X_VISUAL_TYPE_EXT:
1566 switch (xmvis->visinfo->CLASS) {
1567 case StaticGray: *value = GLX_STATIC_GRAY_EXT; return 0;
1568 case GrayScale: *value = GLX_GRAY_SCALE_EXT; return 0;
1569 case StaticColor: *value = GLX_STATIC_GRAY_EXT; return 0;
1570 case PseudoColor: *value = GLX_PSEUDO_COLOR_EXT; return 0;
1571 case TrueColor: *value = GLX_TRUE_COLOR_EXT; return 0;
1572 case DirectColor: *value = GLX_DIRECT_COLOR_EXT; return 0;
1573 }
1574 return 0;
1575 case GLX_TRANSPARENT_TYPE_EXT:
1576 /* normal planes */
1577 *value = GLX_NONE_EXT;
1578 return 0;
1579 case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1580 /* undefined */
1581 return 0;
1582 case GLX_TRANSPARENT_RED_VALUE_EXT:
1583 /* undefined */
1584 return 0;
1585 case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1586 /* undefined */
1587 return 0;
1588 case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1589 /* undefined */
1590 return 0;
1591 case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1592 /* undefined */
1593 return 0;
1594
1595 /*
1596 * GLX_EXT_visual_info extension
1597 */
1598 case GLX_VISUAL_CAVEAT_EXT:
1599 /* test for zero, just in case */
1600 if (xmvis->mesa_visual.visualRating > 0)
1601 *value = xmvis->mesa_visual.visualRating;
1602 else
1603 *value = GLX_NONE_EXT;
1604 return 0;
1605
1606 /*
1607 * GLX_ARB_multisample
1608 */
1609 case GLX_SAMPLE_BUFFERS_ARB:
1610 *value = xmvis->mesa_visual.sampleBuffers;
1611 return 0;
1612 case GLX_SAMPLES_ARB:
1613 *value = xmvis->mesa_visual.samples;
1614 return 0;
1615
1616 /*
1617 * For FBConfigs:
1618 */
1619 case GLX_SCREEN_EXT:
1620 if (!fbconfig)
1621 return GLX_BAD_ATTRIBUTE;
1622 *value = xmvis->visinfo->screen;
1623 break;
1624 case GLX_DRAWABLE_TYPE: /*SGIX too */
1625 if (!fbconfig)
1626 return GLX_BAD_ATTRIBUTE;
1627 *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1628 break;
1629 case GLX_RENDER_TYPE_SGIX:
1630 if (!fbconfig)
1631 return GLX_BAD_ATTRIBUTE;
1632 if (xmvis->mesa_visual.rgbMode)
1633 *value = GLX_RGBA_BIT;
1634 else
1635 *value = GLX_COLOR_INDEX_BIT;
1636 break;
1637 case GLX_X_RENDERABLE_SGIX:
1638 if (!fbconfig)
1639 return GLX_BAD_ATTRIBUTE;
1640 *value = True; /* XXX really? */
1641 break;
1642 case GLX_FBCONFIG_ID_SGIX:
1643 if (!fbconfig)
1644 return GLX_BAD_ATTRIBUTE;
1645 *value = xmvis->visinfo->visualid;
1646 break;
1647 case GLX_MAX_PBUFFER_WIDTH:
1648 if (!fbconfig)
1649 return GLX_BAD_ATTRIBUTE;
1650 /* XXX should be same as ctx->Const.MaxRenderbufferSize */
1651 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1652 break;
1653 case GLX_MAX_PBUFFER_HEIGHT:
1654 if (!fbconfig)
1655 return GLX_BAD_ATTRIBUTE;
1656 *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1657 break;
1658 case GLX_MAX_PBUFFER_PIXELS:
1659 if (!fbconfig)
1660 return GLX_BAD_ATTRIBUTE;
1661 *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1662 DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1663 break;
1664 case GLX_VISUAL_ID:
1665 if (!fbconfig)
1666 return GLX_BAD_ATTRIBUTE;
1667 *value = xmvis->visinfo->visualid;
1668 break;
1669
1670 case GLX_BIND_TO_TEXTURE_RGB_EXT:
1671 *value = True; /*XXX*/
1672 break;
1673 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1674 /* XXX review */
1675 *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1676 break;
1677 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1678 *value = True; /*XXX*/
1679 break;
1680 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1681 *value = (GLX_TEXTURE_1D_BIT_EXT |
1682 GLX_TEXTURE_2D_BIT_EXT |
1683 GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1684 break;
1685 case GLX_Y_INVERTED_EXT:
1686 *value = True; /*XXX*/
1687 break;
1688
1689 default:
1690 return GLX_BAD_ATTRIBUTE;
1691 }
1692 return Success;
1693 }
1694
1695
1696 PUBLIC int
1697 glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1698 int attrib, int *value )
1699 {
1700 XMesaVisual xmvis;
1701 int k;
1702 if (!dpy || !visinfo)
1703 return GLX_BAD_ATTRIBUTE;
1704
1705 xmvis = find_glx_visual( dpy, visinfo );
1706 if (!xmvis) {
1707 /* this visual wasn't obtained with glXChooseVisual */
1708 xmvis = create_glx_visual( dpy, visinfo );
1709 if (!xmvis) {
1710 /* this visual can't be used for GL rendering */
1711 if (attrib==GLX_USE_GL) {
1712 *value = (int) False;
1713 return 0;
1714 }
1715 else {
1716 return GLX_BAD_VISUAL;
1717 }
1718 }
1719 }
1720
1721 k = get_config(xmvis, attrib, value, GL_FALSE);
1722 return k;
1723 }
1724
1725
1726 PUBLIC void
1727 glXWaitGL( void )
1728 {
1729 XMesaContext xmesa = XMesaGetCurrentContext();
1730 XMesaFlush( xmesa );
1731 }
1732
1733
1734
1735 PUBLIC void
1736 glXWaitX( void )
1737 {
1738 XMesaContext xmesa = XMesaGetCurrentContext();
1739 XMesaFlush( xmesa );
1740 }
1741
1742
1743 static const char *
1744 get_extensions( void )
1745 {
1746 return EXTENSIONS;
1747 }
1748
1749
1750
1751 /* GLX 1.1 and later */
1752 PUBLIC const char *
1753 glXQueryExtensionsString( Display *dpy, int screen )
1754 {
1755 (void) dpy;
1756 (void) screen;
1757 return get_extensions();
1758 }
1759
1760
1761
1762 /* GLX 1.1 and later */
1763 PUBLIC const char *
1764 glXQueryServerString( Display *dpy, int screen, int name )
1765 {
1766 static char version[1000];
1767 sprintf(version, "%d.%d %s",
1768 SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, xmesa_get_name());
1769
1770 (void) dpy;
1771 (void) screen;
1772
1773 switch (name) {
1774 case GLX_EXTENSIONS:
1775 return get_extensions();
1776 case GLX_VENDOR:
1777 return VENDOR;
1778 case GLX_VERSION:
1779 return version;
1780 default:
1781 return NULL;
1782 }
1783 }
1784
1785
1786
1787 /* GLX 1.1 and later */
1788 PUBLIC const char *
1789 glXGetClientString( Display *dpy, int name )
1790 {
1791 static char version[1000];
1792 sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
1793 CLIENT_MINOR_VERSION, xmesa_get_name());
1794
1795 (void) dpy;
1796
1797 switch (name) {
1798 case GLX_EXTENSIONS:
1799 return get_extensions();
1800 case GLX_VENDOR:
1801 return VENDOR;
1802 case GLX_VERSION:
1803 return version;
1804 default:
1805 return NULL;
1806 }
1807 }
1808
1809
1810
1811 /*
1812 * GLX 1.3 and later
1813 */
1814
1815
1816 PUBLIC int
1817 glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1818 int attribute, int *value)
1819 {
1820 XMesaVisual v = (XMesaVisual) config;
1821 (void) dpy;
1822 (void) config;
1823
1824 if (!dpy || !config || !value)
1825 return -1;
1826
1827 return get_config(v, attribute, value, GL_TRUE);
1828 }
1829
1830
1831 PUBLIC GLXFBConfig *
1832 glXGetFBConfigs( Display *dpy, int screen, int *nelements )
1833 {
1834 XVisualInfo *visuals, visTemplate;
1835 const long visMask = VisualScreenMask;
1836 int i;
1837
1838 /* Get list of all X visuals */
1839 visTemplate.screen = screen;
1840 visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
1841 if (*nelements > 0) {
1842 XMesaVisual *results = malloc(*nelements * sizeof(XMesaVisual));
1843 if (!results) {
1844 *nelements = 0;
1845 return NULL;
1846 }
1847 for (i = 0; i < *nelements; i++) {
1848 results[i] = create_glx_visual(dpy, visuals + i);
1849 if (!results[i]) {
1850 *nelements = i;
1851 break;
1852 }
1853 }
1854 return (GLXFBConfig *) results;
1855 }
1856 return NULL;
1857 }
1858
1859
1860 PUBLIC GLXFBConfig *
1861 glXChooseFBConfig(Display *dpy, int screen,
1862 const int *attribList, int *nitems)
1863 {
1864 XMesaVisual xmvis;
1865
1866 /* register ourselves as an extension on this display */
1867 register_with_display(dpy);
1868
1869 if (!attribList || !attribList[0]) {
1870 /* return list of all configs (per GLX_SGIX_fbconfig spec) */
1871 return glXGetFBConfigs(dpy, screen, nitems);
1872 }
1873
1874 xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
1875 if (xmvis) {
1876 GLXFBConfig *config = malloc(sizeof(XMesaVisual));
1877 if (!config) {
1878 *nitems = 0;
1879 return NULL;
1880 }
1881 *nitems = 1;
1882 config[0] = (GLXFBConfig) xmvis;
1883 return (GLXFBConfig *) config;
1884 }
1885 else {
1886 *nitems = 0;
1887 return NULL;
1888 }
1889 }
1890
1891
1892 PUBLIC XVisualInfo *
1893 glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
1894 {
1895 if (dpy && config) {
1896 XMesaVisual xmvis = (XMesaVisual) config;
1897 #if 0
1898 return xmvis->vishandle;
1899 #else
1900 /* create a new vishandle - the cached one may be stale */
1901 xmvis->vishandle = malloc(sizeof(XVisualInfo));
1902 if (xmvis->vishandle) {
1903 memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1904 }
1905 return xmvis->vishandle;
1906 #endif
1907 }
1908 else {
1909 return NULL;
1910 }
1911 }
1912
1913
1914 PUBLIC GLXWindow
1915 glXCreateWindow(Display *dpy, GLXFBConfig config, Window win,
1916 const int *attribList)
1917 {
1918 XMesaVisual xmvis = (XMesaVisual) config;
1919 XMesaBuffer xmbuf;
1920 if (!xmvis)
1921 return 0;
1922
1923 xmbuf = XMesaCreateWindowBuffer(xmvis, win);
1924 if (!xmbuf)
1925 return 0;
1926
1927 (void) dpy;
1928 (void) attribList; /* Ignored in GLX 1.3 */
1929
1930 return win; /* A hack for now */
1931 }
1932
1933
1934 PUBLIC void
1935 glXDestroyWindow( Display *dpy, GLXWindow window )
1936 {
1937 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable) window);
1938 if (b)
1939 XMesaDestroyBuffer(b);
1940 /* don't destroy X window */
1941 }
1942
1943
1944 /* XXX untested */
1945 PUBLIC GLXPixmap
1946 glXCreatePixmap(Display *dpy, GLXFBConfig config, Pixmap pixmap,
1947 const int *attribList)
1948 {
1949 XMesaVisual v = (XMesaVisual) config;
1950 XMesaBuffer b;
1951 const int *attr;
1952 int target = 0, format = 0, mipmap = 0;
1953 int value;
1954
1955 if (!dpy || !config || !pixmap)
1956 return 0;
1957
1958 for (attr = attribList; attr && *attr; attr++) {
1959 switch (*attr) {
1960 case GLX_TEXTURE_FORMAT_EXT:
1961 attr++;
1962 switch (*attr) {
1963 case GLX_TEXTURE_FORMAT_NONE_EXT:
1964 case GLX_TEXTURE_FORMAT_RGB_EXT:
1965 case GLX_TEXTURE_FORMAT_RGBA_EXT:
1966 format = *attr;
1967 break;
1968 default:
1969 /* error */
1970 return 0;
1971 }
1972 break;
1973 case GLX_TEXTURE_TARGET_EXT:
1974 attr++;
1975 switch (*attr) {
1976 case GLX_TEXTURE_1D_EXT:
1977 case GLX_TEXTURE_2D_EXT:
1978 case GLX_TEXTURE_RECTANGLE_EXT:
1979 target = *attr;
1980 break;
1981 default:
1982 /* error */
1983 return 0;
1984 }
1985 break;
1986 case GLX_MIPMAP_TEXTURE_EXT:
1987 attr++;
1988 if (*attr)
1989 mipmap = 1;
1990 break;
1991 default:
1992 /* error */
1993 return 0;
1994 }
1995 }
1996
1997 if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
1998 if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
1999 &value, GL_TRUE) != Success
2000 || !value) {
2001 return 0; /* error! */
2002 }
2003 }
2004 else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
2005 if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
2006 &value, GL_TRUE) != Success
2007 || !value) {
2008 return 0; /* error! */
2009 }
2010 }
2011 if (mipmap) {
2012 if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
2013 &value, GL_TRUE) != Success
2014 || !value) {
2015 return 0; /* error! */
2016 }
2017 }
2018 if (target == GLX_TEXTURE_1D_EXT) {
2019 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2020 &value, GL_TRUE) != Success
2021 || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
2022 return 0; /* error! */
2023 }
2024 }
2025 else if (target == GLX_TEXTURE_2D_EXT) {
2026 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2027 &value, GL_TRUE) != Success
2028 || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
2029 return 0; /* error! */
2030 }
2031 }
2032 if (target == GLX_TEXTURE_RECTANGLE_EXT) {
2033 if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2034 &value, GL_TRUE) != Success
2035 || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
2036 return 0; /* error! */
2037 }
2038 }
2039
2040 if (format || target || mipmap) {
2041 /* texture from pixmap */
2042 b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
2043 }
2044 else {
2045 b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
2046 }
2047 if (!b) {
2048 return 0;
2049 }
2050
2051 return pixmap;
2052 }
2053
2054
2055 PUBLIC void
2056 glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
2057 {
2058 XMesaBuffer b = XMesaFindBuffer(dpy, (Drawable)pixmap);
2059 if (b)
2060 XMesaDestroyBuffer(b);
2061 /* don't destroy X pixmap */
2062 }
2063
2064
2065 PUBLIC GLXPbuffer
2066 glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attribList)
2067 {
2068 XMesaVisual xmvis = (XMesaVisual) config;
2069 XMesaBuffer xmbuf;
2070 const int *attrib;
2071 int width = 0, height = 0;
2072 GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2073
2074 (void) dpy;
2075
2076 for (attrib = attribList; *attrib; attrib++) {
2077 switch (*attrib) {
2078 case GLX_PBUFFER_WIDTH:
2079 attrib++;
2080 width = *attrib;
2081 break;
2082 case GLX_PBUFFER_HEIGHT:
2083 attrib++;
2084 height = *attrib;
2085 break;
2086 case GLX_PRESERVED_CONTENTS:
2087 attrib++;
2088 preserveContents = *attrib;
2089 break;
2090 case GLX_LARGEST_PBUFFER:
2091 attrib++;
2092 useLargest = *attrib;
2093 break;
2094 default:
2095 return 0;
2096 }
2097 }
2098
2099 if (width == 0 || height == 0)
2100 return 0;
2101
2102 if (width > PBUFFER_MAX_SIZE || height > PBUFFER_MAX_SIZE) {
2103 /* If allocation would have failed and GLX_LARGEST_PBUFFER is set,
2104 * allocate the largest possible buffer.
2105 */
2106 if (useLargest) {
2107 width = PBUFFER_MAX_SIZE;
2108 height = PBUFFER_MAX_SIZE;
2109 }
2110 }
2111
2112 xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2113 /* A GLXPbuffer handle must be an X Drawable because that's what
2114 * glXMakeCurrent takes.
2115 */
2116 if (xmbuf) {
2117 xmbuf->largestPbuffer = useLargest;
2118 xmbuf->preservedContents = preserveContents;
2119 return (GLXPbuffer) xmbuf->ws.drawable;
2120 }
2121 else {
2122 return 0;
2123 }
2124 }
2125
2126
2127 PUBLIC void
2128 glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2129 {
2130 XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2131 if (b) {
2132 XMesaDestroyBuffer(b);
2133 }
2134 }
2135
2136
2137 PUBLIC void
2138 glXQueryDrawable(Display *dpy, GLXDrawable draw, int attribute,
2139 unsigned int *value)
2140 {
2141 GLuint width, height;
2142 XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2143 if (!xmbuf) {
2144 generate_error(dpy, GLXBadDrawable, draw, X_GLXGetDrawableAttributes, False);
2145 return;
2146 }
2147
2148 /* make sure buffer's dimensions are up to date */
2149 xmesa_get_window_size(dpy, xmbuf, &width, &height);
2150
2151 switch (attribute) {
2152 case GLX_WIDTH:
2153 *value = width;
2154 break;
2155 case GLX_HEIGHT:
2156 *value = height;
2157 break;
2158 case GLX_PRESERVED_CONTENTS:
2159 *value = xmbuf->preservedContents;
2160 break;
2161 case GLX_LARGEST_PBUFFER:
2162 *value = xmbuf->largestPbuffer;
2163 break;
2164 case GLX_FBCONFIG_ID:
2165 *value = xmbuf->xm_visual->visinfo->visualid;
2166 return;
2167 case GLX_TEXTURE_FORMAT_EXT:
2168 *value = xmbuf->TextureFormat;
2169 break;
2170 case GLX_TEXTURE_TARGET_EXT:
2171 *value = xmbuf->TextureTarget;
2172 break;
2173 case GLX_MIPMAP_TEXTURE_EXT:
2174 *value = xmbuf->TextureMipmap;
2175 break;
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 }