Enable client-side GLX support for texture compression extensions.
[mesa.git] / src / glx / x11 / glxcmds.c
1 /* $XFree86: xc/lib/GL/glx/glxcmds.c,v 1.30 2004/01/30 20:33:06 alanh Exp $ */
2 /*
3 ** License Applicability. Except to the extent portions of this file are
4 ** made subject to an alternative license as permitted in the SGI Free
5 ** Software License B, Version 1.1 (the "License"), the contents of this
6 ** file are subject only to the provisions of the License. You may not use
7 ** this file except in compliance with the License. You may obtain a copy
8 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
9 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 **
11 ** http://oss.sgi.com/projects/FreeB
12 **
13 ** Note that, as provided in the License, the Software is distributed on an
14 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
15 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
16 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
17 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 **
19 ** Original Code. The Original Code is: OpenGL Sample Implementation,
20 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
21 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
22 ** Copyright in any portions created by third parties is as indicated
23 ** elsewhere herein. All Rights Reserved.
24 **
25 ** Additional Notice Provisions: The application programming interfaces
26 ** established by SGI in conjunction with the Original Code are The
27 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
28 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
29 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
30 ** Window System(R) (Version 1.3), released October 19, 1998. This software
31 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
32 ** published by SGI, but has not been independently verified as being
33 ** compliant with the OpenGL(R) version 1.2.1 Specification.
34 **
35 */
36
37 /**
38 * \file glxcmds.c
39 * Client-side GLX interface.
40 */
41
42 #include <inttypes.h>
43 #include "glxclient.h"
44 #include <extutil.h>
45 #include <Xext.h>
46 #include <assert.h>
47 #include <string.h>
48 #include "glapi.h"
49 #ifdef GLX_DIRECT_RENDERING
50 #include "indirect_init.h"
51 #include "xf86vmode.h"
52 #endif
53 #include "glxextensions.h"
54 #include "glcontextmodes.h"
55 #include "glheader.h"
56 #include <sys/time.h>
57
58 #ifdef IN_DOXYGEN
59 #define GLX_PREFIX(x) x
60 #endif /* IN_DOXYGEN */
61
62 static const char __glXGLXClientVendorName[] = "SGI";
63 static const char __glXGLXClientVersion[] = "1.4";
64
65
66 #if defined(GLX_DIRECT_RENDERING)
67 #include "xf86dri.h"
68
69 static Bool __glXWindowExists(Display *dpy, GLXDrawable draw);
70
71 static void * DriverCreateContextWrapper( const __GLXscreenConfigs *psc,
72 Display *dpy, XVisualInfo *vis, void *shared, __DRIcontext *ctx,
73 const __GLcontextModes *fbconfig, int render_type );
74
75 #ifndef DRI_NEW_INTERFACE_ONLY
76 static Bool dummyBindContext2( Display *dpy, int scrn,
77 GLXDrawable draw, GLXDrawable read, GLXContext gc );
78
79 static Bool dummyUnbindContext2( Display *dpy, int scrn,
80 GLXDrawable draw, GLXDrawable read, GLXContext gc );
81
82 /****************************************************************************/
83
84 /**
85 * Used as glue when a driver does not support
86 * \c __DRIcontextRec::bindContext2.
87 *
88 * XXX .bindContext is only defined as a function pointer if
89 * !DRI_NEW_INTERFACE_ONLY.
90 *
91 * \sa DriverCreateContextWrapper, __DRIcontextRec::bindContext2
92 */
93 static Bool dummyBindContext2( Display *dpy, int scrn,
94 GLXDrawable draw, GLXDrawable read,
95 GLXContext gc )
96 {
97 assert( draw == read );
98 return (*gc->driContext.bindContext)( dpy, scrn, draw, gc );
99 }
100
101 /**
102 * Used as glue when a driver does not support
103 * \c __DRIcontextRec::unbindContext2.
104 *
105 * XXX .unbindContext is only defined as a function pointer if
106 * !DRI_NEW_INTERFACE_ONLY.
107 *
108 * \sa DriverCreateContextWrapper, __DRIcontextRec::unbindContext2
109 */
110 static Bool dummyUnbindContext2( Display *dpy, int scrn,
111 GLXDrawable draw, GLXDrawable read,
112 GLXContext gc )
113 {
114 assert( draw == read );
115 return (*gc->driContext.unbindContext)( dpy, scrn, draw, gc, GL_FALSE );
116 }
117 #endif /* DRI_NEW_INTERFACE_ONLY */
118
119
120 /****************************************************************************/
121 /**
122 * Wrap the call to the driver's \c createContext function.
123 *
124 * The \c createContext function is wrapped because not all drivers support
125 * the "new" \c unbindContext2 and \c bindContext2 interfaces. libGL should
126 * not have to check to see which functions the driver supports. Instead,
127 * if either function is not supported it is wrapped. The wrappers test to
128 * make sure that both drawables are the same and pass control to the old
129 * interface.
130 *
131 * \sa dummyBindContext2, dummyUnbindContext2,
132 * __DRIcontextRec::bindContext2, __DRIcontextRec::unbindContext2
133 */
134
135 static void * DriverCreateContextWrapper( const __GLXscreenConfigs *psc,
136 Display *dpy, XVisualInfo *vis,
137 void *shared,
138 __DRIcontext *ctx,
139 const __GLcontextModes *modes,
140 int render_type )
141 {
142 void * ctx_priv = NULL;
143
144 if ( psc->driScreen.createNewContext != NULL ) {
145 assert( modes != NULL );
146 ctx_priv = (*psc->driScreen.createNewContext)(dpy, modes, render_type,
147 shared, ctx);
148
149 /* If the driver supports the createNewContext interface, then
150 * it MUST also support either the bindContext2 / unbindContext2
151 * interface or the bindContext3 / unbindContext3 interface.
152 */
153
154 assert( (ctx_priv == NULL) || (ctx->unbindContext2 != NULL)
155 || (ctx->unbindContext3 != NULL) );
156 assert( (ctx_priv == NULL) || (ctx->bindContext2 != NULL)
157 || (ctx->bindContext3 != NULL) );
158 }
159 #ifndef DRI_NEW_INTERFACE_ONLY
160 else {
161 if ( vis != NULL ) {
162 ctx_priv = (*psc->driScreen.createContext)(dpy, vis, shared, ctx);
163
164 if ( ctx_priv != NULL ) {
165 if ( ctx->unbindContext2 == NULL ) {
166 ctx->unbindContext2 = dummyUnbindContext2;
167 }
168
169 if ( ctx->bindContext2 == NULL ) {
170 ctx->bindContext2 = dummyBindContext2;
171 }
172 }
173 }
174 }
175 #endif
176
177 return ctx_priv;
178 }
179 #endif
180
181
182 /****************************************************************************/
183 /**
184 * Get the __DRIdrawable for the drawable associated with a GLXContext
185 *
186 * \param dpy The display associated with \c drawable.
187 * \param drawable GLXDrawable whose __DRIdrawable part is to be retrieved.
188 * \returns A pointer to the context's __DRIdrawable on success, or NULL if
189 * the drawable is not associated with a direct-rendering context.
190 */
191
192 #ifdef GLX_DIRECT_RENDERING
193 static __DRIdrawable *
194 GetDRIDrawable( Display *dpy, GLXDrawable drawable, int * const scrn_num )
195 {
196 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
197
198 if ( (priv != NULL) && (priv->driDisplay.private != NULL) ) {
199 const unsigned screen_count = ScreenCount(dpy);
200 unsigned i;
201
202 for ( i = 0 ; i < screen_count ; i++ ) {
203 __DRIscreen * const psc = &priv->screenConfigs[i].driScreen;
204 __DRIdrawable * const pdraw = (psc->private != NULL)
205 ? (*psc->getDrawable)(dpy, drawable, psc->private) : NULL;
206
207 if ( pdraw != NULL ) {
208 if ( scrn_num != NULL ) {
209 *scrn_num = i;
210 }
211 return pdraw;
212 }
213 }
214 }
215
216 return NULL;
217 }
218 #endif
219
220
221 /**
222 * Get the GLX per-screen data structure associated with a GLX context.
223 *
224 * \param dpy Display for which the GLX per-screen information is to be
225 * retrieved.
226 * \param scrn Screen on \c dpy for which the GLX per-screen information is
227 * to be retrieved.
228 * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
229 * specify a valid GLX screen, or NULL otherwise.
230 *
231 * \todo Should this function validate that \c scrn is within the screen
232 * number range for \c dpy?
233 */
234
235 static __GLXscreenConfigs *
236 GetGLXScreenConfigs(Display *dpy, int scrn)
237 {
238 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
239
240 return (priv->screenConfigs != NULL) ? &priv->screenConfigs[scrn] : NULL;
241 }
242
243
244 static int
245 GetGLXPrivScreenConfig( Display *dpy, int scrn, __GLXdisplayPrivate ** ppriv,
246 __GLXscreenConfigs ** ppsc )
247 {
248 /* Initialize the extension, if needed . This has the added value
249 * of initializing/allocating the display private
250 */
251
252 if ( dpy == NULL ) {
253 return GLX_NO_EXTENSION;
254 }
255
256 *ppriv = __glXInitialize(dpy);
257 if ( *ppriv == NULL ) {
258 return GLX_NO_EXTENSION;
259 }
260
261 /* Check screen number to see if its valid */
262 if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
263 return GLX_BAD_SCREEN;
264 }
265
266 /* Check to see if the GL is supported on this screen */
267 *ppsc = &((*ppriv)->screenConfigs[scrn]);
268 if ( (*ppsc)->configs == NULL ) {
269 /* No support for GL on this screen regardless of visual */
270 return GLX_BAD_VISUAL;
271 }
272
273 return Success;
274 }
275
276
277 /**
278 * Determine if a \c GLXFBConfig supplied by the application is valid.
279 *
280 * \param dpy Application supplied \c Display pointer.
281 * \param config Application supplied \c GLXFBConfig.
282 *
283 * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
284 * \c __GLcontextModes structure is returned. Otherwise, \c NULL
285 * is returned.
286 */
287 static __GLcontextModes *
288 ValidateGLXFBConfig( Display * dpy, GLXFBConfig config )
289 {
290 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
291 const unsigned num_screens = ScreenCount(dpy);
292 unsigned i;
293 const __GLcontextModes * modes;
294
295
296 if ( priv != NULL ) {
297 for ( i = 0 ; i < num_screens ; i++ ) {
298 for ( modes = priv->screenConfigs[i].configs
299 ; modes != NULL
300 ; modes = modes->next ) {
301 if ( modes == (__GLcontextModes *) config ) {
302 return (__GLcontextModes *) config;
303 }
304 }
305 }
306 }
307
308 return NULL;
309 }
310
311
312 /**
313 * \todo It should be possible to move the allocate of \c client_state_private
314 * later in the function for direct-rendering contexts. Direct-rendering
315 * contexts don't need to track client state, so they don't need that memory
316 * at all.
317 *
318 * \todo Eliminate \c __glXInitVertexArrayState. Replace it with a new
319 * function called \c __glXAllocateClientState that allocates the memory and
320 * does all the initialization (including the pixel pack / unpack).
321 */
322 static
323 GLXContext AllocateGLXContext( Display *dpy )
324 {
325 GLXContext gc;
326 int bufSize;
327 CARD8 opcode;
328 __GLXattribute *state;
329
330 if (!dpy)
331 return NULL;
332
333 opcode = __glXSetupForCommand(dpy);
334 if (!opcode) {
335 return NULL;
336 }
337
338 /* Allocate our context record */
339 gc = (GLXContext) Xmalloc(sizeof(struct __GLXcontextRec));
340 if (!gc) {
341 /* Out of memory */
342 return NULL;
343 }
344 memset(gc, 0, sizeof(struct __GLXcontextRec));
345
346 state = Xmalloc(sizeof(struct __GLXattributeRec));
347 if (state == NULL) {
348 /* Out of memory */
349 Xfree(gc);
350 return NULL;
351 }
352 gc->client_state_private = state;
353 memset(gc->client_state_private, 0, sizeof(struct __GLXattributeRec));
354 state->NoDrawArraysProtocol = (getenv("LIBGL_NO_DRAWARRAYS") != NULL);
355
356 /*
357 ** Create a temporary buffer to hold GLX rendering commands. The size
358 ** of the buffer is selected so that the maximum number of GLX rendering
359 ** commands can fit in a single X packet and still have room in the X
360 ** packet for the GLXRenderReq header.
361 */
362
363 bufSize = (XMaxRequestSize(dpy) * 4) - sz_xGLXRenderReq;
364 gc->buf = (GLubyte *) Xmalloc(bufSize);
365 if (!gc->buf) {
366 Xfree(gc->client_state_private);
367 Xfree(gc);
368 return NULL;
369 }
370 gc->bufSize = bufSize;
371
372 /* Fill in the new context */
373 gc->renderMode = GL_RENDER;
374
375 state->storePack.alignment = 4;
376 state->storeUnpack.alignment = 4;
377
378 __glXInitVertexArrayState(gc);
379
380 gc->attributes.stackPointer = &gc->attributes.stack[0];
381
382 /*
383 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
384 ** Other code uses the fastImageUnpack bit, but it is never set
385 ** to GL_TRUE.
386 */
387 gc->fastImageUnpack = GL_FALSE;
388 gc->fillImage = __glFillImage;
389 gc->isDirect = GL_FALSE;
390 gc->pc = gc->buf;
391 gc->bufEnd = gc->buf + bufSize;
392 if (__glXDebug) {
393 /*
394 ** Set limit register so that there will be one command per packet
395 */
396 gc->limit = gc->buf;
397 } else {
398 gc->limit = gc->buf + bufSize - __GLX_BUFFER_LIMIT_SIZE;
399 }
400 gc->createDpy = dpy;
401 gc->majorOpcode = opcode;
402
403 /*
404 ** Constrain the maximum drawing command size allowed to be
405 ** transfered using the X_GLXRender protocol request. First
406 ** constrain by a software limit, then constrain by the protocl
407 ** limit.
408 */
409 if (bufSize > __GLX_RENDER_CMD_SIZE_LIMIT) {
410 bufSize = __GLX_RENDER_CMD_SIZE_LIMIT;
411 }
412 if (bufSize > __GLX_MAX_RENDER_CMD_SIZE) {
413 bufSize = __GLX_MAX_RENDER_CMD_SIZE;
414 }
415 gc->maxSmallRenderCommandSize = bufSize;
416 return gc;
417 }
418
419
420 /**
421 * Create a new context. Exactly one of \c vis and \c fbconfig should be
422 * non-NULL.
423 *
424 * \param use_glx_1_3 For FBConfigs, should GLX 1.3 protocol or
425 * SGIX_fbconfig protocol be used?
426 * \param renderType For FBConfigs, what is the rendering type?
427 */
428
429 static GLXContext
430 CreateContext(Display *dpy, XVisualInfo *vis,
431 const __GLcontextModes * const fbconfig,
432 GLXContext shareList,
433 Bool allowDirect, GLXContextID contextID,
434 Bool use_glx_1_3, int renderType)
435 {
436 GLXContext gc;
437
438 if ( dpy == NULL )
439 return NULL;
440
441 gc = AllocateGLXContext(dpy);
442 if (!gc)
443 return NULL;
444
445 if (None == contextID) {
446 if ( (vis == NULL) && (fbconfig == NULL) )
447 return NULL;
448
449 #ifdef GLX_DIRECT_RENDERING
450 if (allowDirect) {
451 int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
452 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
453 const __GLcontextModes * mode;
454
455 /* The value of fbconfig cannot change because it is tested
456 * later in the function.
457 */
458 if ( fbconfig == NULL ) {
459 /* FIXME: Is it possible for the __GLcontextModes structure
460 * FIXME: to not be found?
461 */
462 mode = _gl_context_modes_find_visual( psc->configs,
463 vis->visualid );
464 assert( mode != NULL );
465 assert( mode->screen == screen );
466 }
467 else {
468 mode = fbconfig;
469 }
470
471 if (psc && psc->driScreen.private) {
472 void * const shared = (shareList != NULL)
473 ? shareList->driContext.private : NULL;
474 gc->driContext.private =
475 DriverCreateContextWrapper( psc, dpy, vis, shared,
476 &gc->driContext, mode,
477 renderType );
478 if (gc->driContext.private) {
479 gc->isDirect = GL_TRUE;
480 gc->screen = mode->screen;
481 gc->vid = mode->visualID;
482 gc->fbconfigID = mode->fbconfigID;
483 gc->driContext.mode = mode;
484 }
485 }
486 }
487 #endif
488
489 LockDisplay(dpy);
490 if ( fbconfig == NULL ) {
491 xGLXCreateContextReq *req;
492
493 /* Send the glXCreateContext request */
494 GetReq(GLXCreateContext,req);
495 req->reqType = gc->majorOpcode;
496 req->glxCode = X_GLXCreateContext;
497 req->context = gc->xid = XAllocID(dpy);
498 req->visual = vis->visualid;
499 req->screen = vis->screen;
500 req->shareList = shareList ? shareList->xid : None;
501 req->isDirect = gc->isDirect;
502 }
503 else if ( use_glx_1_3 ) {
504 xGLXCreateNewContextReq *req;
505
506 /* Send the glXCreateNewContext request */
507 GetReq(GLXCreateNewContext,req);
508 req->reqType = gc->majorOpcode;
509 req->glxCode = X_GLXCreateNewContext;
510 req->context = gc->xid = XAllocID(dpy);
511 req->fbconfig = fbconfig->fbconfigID;
512 req->screen = fbconfig->screen;
513 req->renderType = renderType;
514 req->shareList = shareList ? shareList->xid : None;
515 req->isDirect = gc->isDirect;
516 }
517 else {
518 xGLXVendorPrivateWithReplyReq *vpreq;
519 xGLXCreateContextWithConfigSGIXReq *req;
520
521 /* Send the glXCreateNewContext request */
522 GetReqExtra(GLXVendorPrivateWithReply,
523 sz_xGLXCreateContextWithConfigSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
524 req = (xGLXCreateContextWithConfigSGIXReq *)vpreq;
525 req->reqType = gc->majorOpcode;
526 req->glxCode = X_GLXVendorPrivateWithReply;
527 req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
528 req->context = gc->xid = XAllocID(dpy);
529 req->fbconfig = fbconfig->fbconfigID;
530 req->screen = fbconfig->screen;
531 req->renderType = renderType;
532 req->shareList = shareList ? shareList->xid : None;
533 req->isDirect = gc->isDirect;
534 }
535
536 UnlockDisplay(dpy);
537 SyncHandle();
538 gc->imported = GL_FALSE;
539 }
540 else {
541 gc->xid = contextID;
542 gc->imported = GL_TRUE;
543 }
544
545 return gc;
546 }
547
548 PUBLIC GLXContext GLX_PREFIX(glXCreateContext)(Display *dpy, XVisualInfo *vis,
549 GLXContext shareList,
550 Bool allowDirect)
551 {
552 return CreateContext(dpy, vis, NULL, shareList, allowDirect, None,
553 False, 0);
554 }
555
556 void __glXFreeContext(__GLXcontext *gc)
557 {
558 if (gc->vendor) XFree((char *) gc->vendor);
559 if (gc->renderer) XFree((char *) gc->renderer);
560 if (gc->version) XFree((char *) gc->version);
561 if (gc->extensions) XFree((char *) gc->extensions);
562 __glFreeAttributeState(gc);
563 XFree((char *) gc->buf);
564 Xfree((char *) gc->client_state_private);
565 XFree((char *) gc);
566
567 }
568
569 /*
570 ** Destroy the named context
571 */
572 static void
573 DestroyContext(Display *dpy, GLXContext gc)
574 {
575 xGLXDestroyContextReq *req;
576 GLXContextID xid;
577 CARD8 opcode;
578 GLboolean imported;
579
580 opcode = __glXSetupForCommand(dpy);
581 if (!opcode || !gc) {
582 return;
583 }
584
585 __glXLock();
586 xid = gc->xid;
587 imported = gc->imported;
588 gc->xid = None;
589
590 #ifdef GLX_DIRECT_RENDERING
591 /* Destroy the direct rendering context */
592 if (gc->isDirect) {
593 if (gc->driContext.private) {
594 (*gc->driContext.destroyContext)(dpy, gc->screen,
595 gc->driContext.private);
596 gc->driContext.private = NULL;
597 }
598 }
599 #endif
600
601 if (gc->currentDpy) {
602 /* Have to free later cuz it's in use now */
603 __glXUnlock();
604 } else {
605 /* Destroy the handle if not current to anybody */
606 __glXUnlock();
607 __glXFreeContext(gc);
608 }
609
610 if (!imported) {
611 /*
612 ** This dpy also created the server side part of the context.
613 ** Send the glXDestroyContext request.
614 */
615 LockDisplay(dpy);
616 GetReq(GLXDestroyContext,req);
617 req->reqType = opcode;
618 req->glxCode = X_GLXDestroyContext;
619 req->context = xid;
620 UnlockDisplay(dpy);
621 SyncHandle();
622 }
623 }
624
625 PUBLIC void GLX_PREFIX(glXDestroyContext)(Display *dpy, GLXContext gc)
626 {
627 DestroyContext(dpy, gc);
628 }
629
630 /*
631 ** Return the major and minor version #s for the GLX extension
632 */
633 PUBLIC Bool GLX_PREFIX(glXQueryVersion)(Display *dpy, int *major, int *minor)
634 {
635 __GLXdisplayPrivate *priv;
636
637 /* Init the extension. This fetches the major and minor version. */
638 priv = __glXInitialize(dpy);
639 if (!priv) return GL_FALSE;
640
641 if (major) *major = priv->majorVersion;
642 if (minor) *minor = priv->minorVersion;
643 return GL_TRUE;
644 }
645
646 /*
647 ** Query the existance of the GLX extension
648 */
649 PUBLIC Bool GLX_PREFIX(glXQueryExtension)(Display *dpy, int *errorBase,
650 int *eventBase)
651 {
652 int major_op, erb, evb;
653 Bool rv;
654
655 rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
656 if (rv) {
657 if (errorBase) *errorBase = erb;
658 if (eventBase) *eventBase = evb;
659 }
660 return rv;
661 }
662
663 /*
664 ** Put a barrier in the token stream that forces the GL to finish its
665 ** work before X can proceed.
666 */
667 PUBLIC void GLX_PREFIX(glXWaitGL)(void)
668 {
669 xGLXWaitGLReq *req;
670 GLXContext gc = __glXGetCurrentContext();
671 Display *dpy = gc->currentDpy;
672
673 if (!dpy) return;
674
675 /* Flush any pending commands out */
676 __glXFlushRenderBuffer(gc, gc->pc);
677
678 #ifdef GLX_DIRECT_RENDERING
679 if (gc->isDirect) {
680 /* This bit of ugliness unwraps the glFinish function */
681 #ifdef glFinish
682 #undef glFinish
683 #endif
684 glFinish();
685 return;
686 }
687 #endif
688
689 /* Send the glXWaitGL request */
690 LockDisplay(dpy);
691 GetReq(GLXWaitGL,req);
692 req->reqType = gc->majorOpcode;
693 req->glxCode = X_GLXWaitGL;
694 req->contextTag = gc->currentContextTag;
695 UnlockDisplay(dpy);
696 SyncHandle();
697 }
698
699 /*
700 ** Put a barrier in the token stream that forces X to finish its
701 ** work before GL can proceed.
702 */
703 PUBLIC void GLX_PREFIX(glXWaitX)(void)
704 {
705 xGLXWaitXReq *req;
706 GLXContext gc = __glXGetCurrentContext();
707 Display *dpy = gc->currentDpy;
708
709 if (!dpy) return;
710
711 /* Flush any pending commands out */
712 __glXFlushRenderBuffer(gc, gc->pc);
713
714 #ifdef GLX_DIRECT_RENDERING
715 if (gc->isDirect) {
716 XSync(dpy, False);
717 return;
718 }
719 #endif
720
721 /*
722 ** Send the glXWaitX request.
723 */
724 LockDisplay(dpy);
725 GetReq(GLXWaitX,req);
726 req->reqType = gc->majorOpcode;
727 req->glxCode = X_GLXWaitX;
728 req->contextTag = gc->currentContextTag;
729 UnlockDisplay(dpy);
730 SyncHandle();
731 }
732
733 PUBLIC void GLX_PREFIX(glXUseXFont)(Font font, int first, int count,
734 int listBase)
735 {
736 xGLXUseXFontReq *req;
737 GLXContext gc = __glXGetCurrentContext();
738 Display *dpy = gc->currentDpy;
739
740 if (!dpy) return;
741
742 /* Flush any pending commands out */
743 (void) __glXFlushRenderBuffer(gc, gc->pc);
744
745 #ifdef GLX_DIRECT_RENDERING
746 if (gc->isDirect) {
747 DRI_glXUseXFont(font, first, count, listBase);
748 return;
749 }
750 #endif
751
752 /* Send the glXUseFont request */
753 LockDisplay(dpy);
754 GetReq(GLXUseXFont,req);
755 req->reqType = gc->majorOpcode;
756 req->glxCode = X_GLXUseXFont;
757 req->contextTag = gc->currentContextTag;
758 req->font = font;
759 req->first = first;
760 req->count = count;
761 req->listBase = listBase;
762 UnlockDisplay(dpy);
763 SyncHandle();
764 }
765
766 /************************************************************************/
767
768 /*
769 ** Copy the source context to the destination context using the
770 ** attribute "mask".
771 */
772 PUBLIC void GLX_PREFIX(glXCopyContext)(Display *dpy, GLXContext source,
773 GLXContext dest, unsigned long mask)
774 {
775 xGLXCopyContextReq *req;
776 GLXContext gc = __glXGetCurrentContext();
777 GLXContextTag tag;
778 CARD8 opcode;
779
780 opcode = __glXSetupForCommand(dpy);
781 if (!opcode) {
782 return;
783 }
784
785 #ifdef GLX_DIRECT_RENDERING
786 if (gc->isDirect) {
787 /* NOT_DONE: This does not work yet */
788 }
789 #endif
790
791 /*
792 ** If the source is the current context, send its tag so that the context
793 ** can be flushed before the copy.
794 */
795 if (source == gc && dpy == gc->currentDpy) {
796 tag = gc->currentContextTag;
797 } else {
798 tag = 0;
799 }
800
801 /* Send the glXCopyContext request */
802 LockDisplay(dpy);
803 GetReq(GLXCopyContext,req);
804 req->reqType = opcode;
805 req->glxCode = X_GLXCopyContext;
806 req->source = source ? source->xid : None;
807 req->dest = dest ? dest->xid : None;
808 req->mask = mask;
809 req->contextTag = tag;
810 UnlockDisplay(dpy);
811 SyncHandle();
812 }
813
814
815 /**
816 * Determine if a context uses direct rendering.
817 *
818 * \param dpy Display where the context was created.
819 * \param contextID ID of the context to be tested.
820 *
821 * \returns \c GL_TRUE if the context is direct rendering or not.
822 */
823 static Bool __glXIsDirect(Display *dpy, GLXContextID contextID)
824 {
825 xGLXIsDirectReq *req;
826 xGLXIsDirectReply reply;
827 CARD8 opcode;
828
829 opcode = __glXSetupForCommand(dpy);
830 if (!opcode) {
831 return GL_FALSE;
832 }
833
834 /* Send the glXIsDirect request */
835 LockDisplay(dpy);
836 GetReq(GLXIsDirect,req);
837 req->reqType = opcode;
838 req->glxCode = X_GLXIsDirect;
839 req->context = contextID;
840 _XReply(dpy, (xReply*) &reply, 0, False);
841 UnlockDisplay(dpy);
842 SyncHandle();
843
844 return reply.isDirect;
845 }
846
847 PUBLIC Bool GLX_PREFIX(glXIsDirect)(Display *dpy, GLXContext gc)
848 {
849 if (!gc) {
850 return GL_FALSE;
851 #ifdef GLX_DIRECT_RENDERING
852 } else if (gc->isDirect) {
853 return GL_TRUE;
854 #endif
855 }
856 return __glXIsDirect(dpy, gc->xid);
857 }
858
859 PUBLIC GLXPixmap GLX_PREFIX(glXCreateGLXPixmap)(Display *dpy, XVisualInfo *vis,
860 Pixmap pixmap)
861 {
862 xGLXCreateGLXPixmapReq *req;
863 GLXPixmap xid;
864 CARD8 opcode;
865
866 opcode = __glXSetupForCommand(dpy);
867 if (!opcode) {
868 return None;
869 }
870
871 /* Send the glXCreateGLXPixmap request */
872 LockDisplay(dpy);
873 GetReq(GLXCreateGLXPixmap,req);
874 req->reqType = opcode;
875 req->glxCode = X_GLXCreateGLXPixmap;
876 req->screen = vis->screen;
877 req->visual = vis->visualid;
878 req->pixmap = pixmap;
879 req->glxpixmap = xid = XAllocID(dpy);
880 UnlockDisplay(dpy);
881 SyncHandle();
882 return xid;
883 }
884
885 /*
886 ** Destroy the named pixmap
887 */
888 PUBLIC void GLX_PREFIX(glXDestroyGLXPixmap)(Display *dpy, GLXPixmap glxpixmap)
889 {
890 xGLXDestroyGLXPixmapReq *req;
891 CARD8 opcode;
892
893 opcode = __glXSetupForCommand(dpy);
894 if (!opcode) {
895 return;
896 }
897
898 /* Send the glXDestroyGLXPixmap request */
899 LockDisplay(dpy);
900 GetReq(GLXDestroyGLXPixmap,req);
901 req->reqType = opcode;
902 req->glxCode = X_GLXDestroyGLXPixmap;
903 req->glxpixmap = glxpixmap;
904 UnlockDisplay(dpy);
905 SyncHandle();
906 }
907
908 PUBLIC void GLX_PREFIX(glXSwapBuffers)(Display *dpy, GLXDrawable drawable)
909 {
910 xGLXSwapBuffersReq *req;
911 GLXContext gc;
912 GLXContextTag tag;
913 CARD8 opcode;
914 #ifdef GLX_DIRECT_RENDERING
915 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, NULL );
916
917 if ( pdraw != NULL ) {
918 (*pdraw->swapBuffers)(dpy, pdraw->private);
919 return;
920 }
921 #endif
922
923 opcode = __glXSetupForCommand(dpy);
924 if (!opcode) {
925 return;
926 }
927
928 /*
929 ** The calling thread may or may not have a current context. If it
930 ** does, send the context tag so the server can do a flush.
931 */
932 gc = __glXGetCurrentContext();
933 if ((gc != NULL) && (dpy == gc->currentDpy) &&
934 ((drawable == gc->currentDrawable) || (drawable == gc->currentReadable)) ) {
935 tag = gc->currentContextTag;
936 } else {
937 tag = 0;
938 }
939
940 /* Send the glXSwapBuffers request */
941 LockDisplay(dpy);
942 GetReq(GLXSwapBuffers,req);
943 req->reqType = opcode;
944 req->glxCode = X_GLXSwapBuffers;
945 req->drawable = drawable;
946 req->contextTag = tag;
947 UnlockDisplay(dpy);
948 SyncHandle();
949 XFlush(dpy);
950 }
951
952
953 /*
954 ** Return configuration information for the given display, screen and
955 ** visual combination.
956 */
957 PUBLIC int GLX_PREFIX(glXGetConfig)(Display *dpy, XVisualInfo *vis,
958 int attribute, int *value_return)
959 {
960 __GLXdisplayPrivate *priv;
961 __GLXscreenConfigs *psc;
962 int status;
963
964 status = GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc );
965 if ( status == Success ) {
966 const __GLcontextModes * const modes = _gl_context_modes_find_visual(
967 psc->configs, vis->visualid );
968
969 /* Lookup attribute after first finding a match on the visual */
970 if ( modes != NULL ) {
971 return _gl_get_context_mode_data( modes, attribute, value_return );
972 }
973
974 status = GLX_BAD_VISUAL;
975 }
976
977 /*
978 ** If we can't find the config for this visual, this visual is not
979 ** supported by the OpenGL implementation on the server.
980 */
981 if ( (status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL) ) {
982 *value_return = GL_FALSE;
983 status = Success;
984 }
985
986 return status;
987 }
988
989 /************************************************************************/
990
991 static void
992 init_fbconfig_for_chooser( __GLcontextModes * config,
993 GLboolean fbconfig_style_tags )
994 {
995 memset( config, 0, sizeof( __GLcontextModes ) );
996 config->visualID = (XID) GLX_DONT_CARE;
997 config->visualType = GLX_DONT_CARE;
998
999 /* glXChooseFBConfig specifies different defaults for these two than
1000 * glXChooseVisual.
1001 */
1002 if ( fbconfig_style_tags ) {
1003 config->rgbMode = GL_TRUE;
1004 config->doubleBufferMode = GLX_DONT_CARE;
1005 }
1006
1007 config->visualRating = GLX_DONT_CARE;
1008 config->transparentPixel = GLX_NONE;
1009 config->transparentRed = GLX_DONT_CARE;
1010 config->transparentGreen = GLX_DONT_CARE;
1011 config->transparentBlue = GLX_DONT_CARE;
1012 config->transparentAlpha = GLX_DONT_CARE;
1013 config->transparentIndex = GLX_DONT_CARE;
1014
1015 config->drawableType = GLX_WINDOW_BIT;
1016 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
1017 config->xRenderable = GLX_DONT_CARE;
1018 config->fbconfigID = (GLXFBConfigID)(GLX_DONT_CARE);
1019
1020 config->swapMethod = GLX_DONT_CARE;
1021 }
1022
1023 #define MATCH_DONT_CARE( param ) \
1024 do { \
1025 if ( (a-> param != GLX_DONT_CARE) \
1026 && (a-> param != b-> param) ) { \
1027 return False; \
1028 } \
1029 } while ( 0 )
1030
1031 #define MATCH_MINIMUM( param ) \
1032 do { \
1033 if ( (a-> param != GLX_DONT_CARE) \
1034 && (a-> param > b-> param) ) { \
1035 return False; \
1036 } \
1037 } while ( 0 )
1038
1039 #define MATCH_EXACT( param ) \
1040 do { \
1041 if ( a-> param != b-> param) { \
1042 return False; \
1043 } \
1044 } while ( 0 )
1045
1046 /**
1047 * Determine if two GLXFBConfigs are compatible.
1048 *
1049 * \param a Application specified config to test.
1050 * \param b Server specified config to test against \c a.
1051 */
1052 static Bool
1053 fbconfigs_compatible( const __GLcontextModes * const a,
1054 const __GLcontextModes * const b )
1055 {
1056 MATCH_DONT_CARE( doubleBufferMode );
1057 MATCH_DONT_CARE( visualType );
1058 MATCH_DONT_CARE( visualRating );
1059 MATCH_DONT_CARE( xRenderable );
1060 MATCH_DONT_CARE( fbconfigID );
1061 MATCH_DONT_CARE( swapMethod );
1062
1063 MATCH_MINIMUM( rgbBits );
1064 MATCH_MINIMUM( numAuxBuffers );
1065 MATCH_MINIMUM( redBits );
1066 MATCH_MINIMUM( greenBits );
1067 MATCH_MINIMUM( blueBits );
1068 MATCH_MINIMUM( alphaBits );
1069 MATCH_MINIMUM( depthBits );
1070 MATCH_MINIMUM( stencilBits );
1071 MATCH_MINIMUM( accumRedBits );
1072 MATCH_MINIMUM( accumGreenBits );
1073 MATCH_MINIMUM( accumBlueBits );
1074 MATCH_MINIMUM( accumAlphaBits );
1075 MATCH_MINIMUM( sampleBuffers );
1076 MATCH_MINIMUM( maxPbufferWidth );
1077 MATCH_MINIMUM( maxPbufferHeight );
1078 MATCH_MINIMUM( maxPbufferPixels );
1079 MATCH_MINIMUM( samples );
1080
1081 MATCH_DONT_CARE( stereoMode );
1082 MATCH_EXACT( level );
1083
1084 if ( ((a->drawableType & b->drawableType) == 0)
1085 || ((a->renderType & b->renderType) == 0) ) {
1086 return False;
1087 }
1088
1089
1090 /* There is a bug in a few of the XFree86 DDX drivers. They contain
1091 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
1092 * Technically speaking, it is a bug in the DDX driver, but there is
1093 * enough of an installed base to work around the problem here. In any
1094 * case, 0 is not a valid value of the transparent type, so we'll treat 0
1095 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
1096 * 0 from the server to be a match to maintain backward compatibility with
1097 * the (broken) drivers.
1098 */
1099
1100 if ( a->transparentPixel != GLX_DONT_CARE
1101 && a->transparentPixel != 0 ) {
1102 if ( a->transparentPixel == GLX_NONE ) {
1103 if ( b->transparentPixel != GLX_NONE && b->transparentPixel != 0 )
1104 return False;
1105 } else {
1106 MATCH_EXACT( transparentPixel );
1107 }
1108
1109 switch ( a->transparentPixel ) {
1110 case GLX_TRANSPARENT_RGB:
1111 MATCH_DONT_CARE( transparentRed );
1112 MATCH_DONT_CARE( transparentGreen );
1113 MATCH_DONT_CARE( transparentBlue );
1114 MATCH_DONT_CARE( transparentAlpha );
1115 break;
1116
1117 case GLX_TRANSPARENT_INDEX:
1118 MATCH_DONT_CARE( transparentIndex );
1119 break;
1120
1121 default:
1122 break;
1123 }
1124 }
1125
1126 return True;
1127 }
1128
1129
1130 /* There's some trickly language in the GLX spec about how this is supposed
1131 * to work. Basically, if a given component size is either not specified
1132 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1133 * Well, that's really hard to do with the code as-is. This behavior is
1134 * closer to correct, but still not technically right.
1135 */
1136 #define PREFER_LARGER_OR_ZERO(comp) \
1137 do { \
1138 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1139 if ( ((*a)-> comp) == 0 ) { \
1140 return -1; \
1141 } \
1142 else if ( ((*b)-> comp) == 0 ) { \
1143 return 1; \
1144 } \
1145 else { \
1146 return ((*b)-> comp) - ((*a)-> comp) ; \
1147 } \
1148 } \
1149 } while( 0 )
1150
1151 #define PREFER_LARGER(comp) \
1152 do { \
1153 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1154 return ((*b)-> comp) - ((*a)-> comp) ; \
1155 } \
1156 } while( 0 )
1157
1158 #define PREFER_SMALLER(comp) \
1159 do { \
1160 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1161 return ((*a)-> comp) - ((*b)-> comp) ; \
1162 } \
1163 } while( 0 )
1164
1165 /**
1166 * Compare two GLXFBConfigs. This function is intended to be used as the
1167 * compare function passed in to qsort.
1168 *
1169 * \returns If \c a is a "better" config, according to the specification of
1170 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1171 * better, then a number greater than zero is return. If both are
1172 * equal, zero is returned.
1173 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1174 */
1175 static int
1176 fbconfig_compare( const __GLcontextModes * const * const a,
1177 const __GLcontextModes * const * const b )
1178 {
1179 /* The order of these comparisons must NOT change. It is defined by
1180 * the GLX 1.3 spec and ARB_multisample.
1181 */
1182
1183 PREFER_SMALLER( visualSelectGroup );
1184
1185 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1186 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1187 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1188 */
1189 PREFER_SMALLER( visualRating );
1190
1191 /* This isn't quite right. It is supposed to compare the sum of the
1192 * components the user specifically set minimums for.
1193 */
1194 PREFER_LARGER_OR_ZERO( redBits );
1195 PREFER_LARGER_OR_ZERO( greenBits );
1196 PREFER_LARGER_OR_ZERO( blueBits );
1197 PREFER_LARGER_OR_ZERO( alphaBits );
1198
1199 PREFER_SMALLER( rgbBits );
1200
1201 if ( ((*a)->doubleBufferMode != (*b)->doubleBufferMode) ) {
1202 /* Prefer single-buffer.
1203 */
1204 return ( !(*a)->doubleBufferMode ) ? -1 : 1;
1205 }
1206
1207 PREFER_SMALLER( numAuxBuffers );
1208
1209 PREFER_LARGER_OR_ZERO( depthBits );
1210 PREFER_SMALLER( stencilBits );
1211
1212 /* This isn't quite right. It is supposed to compare the sum of the
1213 * components the user specifically set minimums for.
1214 */
1215 PREFER_LARGER_OR_ZERO( accumRedBits );
1216 PREFER_LARGER_OR_ZERO( accumGreenBits );
1217 PREFER_LARGER_OR_ZERO( accumBlueBits );
1218 PREFER_LARGER_OR_ZERO( accumAlphaBits );
1219
1220 PREFER_SMALLER( visualType );
1221
1222 /* None of the multisample specs say where this comparison should happen,
1223 * so I put it near the end.
1224 */
1225 PREFER_SMALLER( sampleBuffers );
1226 PREFER_SMALLER( samples );
1227
1228 /* None of the pbuffer or fbconfig specs say that this comparison needs
1229 * to happen at all, but it seems like it should.
1230 */
1231 PREFER_LARGER( maxPbufferWidth );
1232 PREFER_LARGER( maxPbufferHeight );
1233 PREFER_LARGER( maxPbufferPixels );
1234
1235 return 0;
1236 }
1237
1238
1239 /**
1240 * Selects and sorts a subset of the supplied configs based on the attributes.
1241 * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1242 * and \c glXChooseFBConfigSGIX.
1243 *
1244 * \param configs Array of pointers to possible configs. The elements of
1245 * this array that do not meet the criteria will be set to
1246 * NULL. The remaining elements will be sorted according to
1247 * the various visual / FBConfig selection rules.
1248 * \param num_configs Number of elements in the \c configs array.
1249 * \param attribList Attributes used select from \c configs. This array is
1250 * terminated by a \c None tag. The array can either take
1251 * the form expected by \c glXChooseVisual (where boolean
1252 * tags do not have a value) or by \c glXChooseFBConfig
1253 * (where every tag has a value).
1254 * \param fbconfig_style_tags Selects whether \c attribList is in
1255 * \c glXChooseVisual style or
1256 * \c glXChooseFBConfig style.
1257 * \returns The number of valid elements left in \c configs.
1258 *
1259 * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1260 */
1261 static int
1262 choose_visual( __GLcontextModes ** configs, int num_configs,
1263 const int *attribList, GLboolean fbconfig_style_tags )
1264 {
1265 __GLcontextModes test_config;
1266 int base;
1267 int i;
1268
1269 /* This is a fairly direct implementation of the selection method
1270 * described by GLX_SGIX_fbconfig. Start by culling out all the
1271 * configs that are not compatible with the selected parameter
1272 * list.
1273 */
1274
1275 init_fbconfig_for_chooser( & test_config, fbconfig_style_tags );
1276 __glXInitializeVisualConfigFromTags( & test_config, 512,
1277 (const INT32 *) attribList,
1278 GL_TRUE, fbconfig_style_tags );
1279
1280 base = 0;
1281 for ( i = 0 ; i < num_configs ; i++ ) {
1282 if ( fbconfigs_compatible( & test_config, configs[i] ) ) {
1283 configs[ base ] = configs[ i ];
1284 base++;
1285 }
1286 }
1287
1288 if ( base == 0 ) {
1289 return 0;
1290 }
1291
1292 if ( base < num_configs ) {
1293 (void) memset( & configs[ base ], 0,
1294 sizeof( void * ) * (num_configs - base) );
1295 }
1296
1297 /* After the incompatible configs are removed, the resulting
1298 * list is sorted according to the rules set out in the various
1299 * specifications.
1300 */
1301
1302 qsort( configs, base, sizeof( __GLcontextModes * ),
1303 (int (*)(const void*, const void*)) fbconfig_compare );
1304 return base;
1305 }
1306
1307
1308
1309
1310 /*
1311 ** Return the visual that best matches the template. Return None if no
1312 ** visual matches the template.
1313 */
1314 PUBLIC XVisualInfo *GLX_PREFIX(glXChooseVisual)(Display *dpy, int screen,
1315 int *attribList)
1316 {
1317 XVisualInfo *visualList = NULL;
1318 __GLXdisplayPrivate *priv;
1319 __GLXscreenConfigs *psc;
1320 __GLcontextModes test_config;
1321 __GLcontextModes *modes;
1322 const __GLcontextModes *best_config = NULL;
1323
1324 /*
1325 ** Get a list of all visuals, return if list is empty
1326 */
1327 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1328 return None;
1329 }
1330
1331
1332 /*
1333 ** Build a template from the defaults and the attribute list
1334 ** Free visual list and return if an unexpected token is encountered
1335 */
1336 init_fbconfig_for_chooser( & test_config, GL_FALSE );
1337 __glXInitializeVisualConfigFromTags( & test_config, 512,
1338 (const INT32 *) attribList,
1339 GL_TRUE, GL_FALSE );
1340
1341 /*
1342 ** Eliminate visuals that don't meet minimum requirements
1343 ** Compute a score for those that do
1344 ** Remember which visual, if any, got the highest score
1345 */
1346 for ( modes = psc->configs ; modes != NULL ; modes = modes->next ) {
1347 if ( fbconfigs_compatible( & test_config, modes )
1348 && ((best_config == NULL)
1349 || (fbconfig_compare( (const __GLcontextModes * const * const)&modes, &best_config ) < 0)) ) {
1350 best_config = modes;
1351 }
1352 }
1353
1354 /*
1355 ** If no visual is acceptable, return None
1356 ** Otherwise, create an XVisualInfo list with just the selected X visual
1357 ** and return this.
1358 */
1359 if (best_config != NULL) {
1360 XVisualInfo visualTemplate;
1361 int i;
1362
1363 visualTemplate.screen = screen;
1364 visualTemplate.visualid = best_config->visualID;
1365 visualList = XGetVisualInfo( dpy, VisualScreenMask|VisualIDMask,
1366 &visualTemplate, &i );
1367 }
1368
1369 return visualList;
1370 }
1371
1372
1373 PUBLIC const char *GLX_PREFIX(glXQueryExtensionsString)( Display *dpy,
1374 int screen )
1375 {
1376 __GLXscreenConfigs *psc;
1377 __GLXdisplayPrivate *priv;
1378
1379 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1380 return NULL;
1381 }
1382
1383 if (!psc->effectiveGLXexts) {
1384 if (!psc->serverGLXexts) {
1385 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
1386 X_GLXQueryServerString,
1387 screen, GLX_EXTENSIONS);
1388 }
1389
1390 __glXCalculateUsableExtensions(psc,
1391 #ifdef GLX_DIRECT_RENDERING
1392 (priv->driDisplay.private != NULL),
1393 #else
1394 GL_FALSE,
1395 #endif
1396 priv->minorVersion);
1397 }
1398
1399 return psc->effectiveGLXexts;
1400 }
1401
1402 PUBLIC const char *GLX_PREFIX(glXGetClientString)( Display *dpy, int name )
1403 {
1404 switch(name) {
1405 case GLX_VENDOR:
1406 return (__glXGLXClientVendorName);
1407 case GLX_VERSION:
1408 return (__glXGLXClientVersion);
1409 case GLX_EXTENSIONS:
1410 return (__glXGetClientExtensions());
1411 default:
1412 return NULL;
1413 }
1414 }
1415
1416 PUBLIC const char *GLX_PREFIX(glXQueryServerString)( Display *dpy, int screen,
1417 int name )
1418 {
1419 __GLXscreenConfigs *psc;
1420 __GLXdisplayPrivate *priv;
1421 const char ** str;
1422
1423
1424 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1425 return NULL;
1426 }
1427
1428 switch(name) {
1429 case GLX_VENDOR:
1430 str = & priv->serverGLXvendor;
1431 break;
1432 case GLX_VERSION:
1433 str = & priv->serverGLXversion;
1434 break;
1435 case GLX_EXTENSIONS:
1436 str = & psc->serverGLXexts;
1437 break;
1438 default:
1439 return NULL;
1440 }
1441
1442 if ( *str == NULL ) {
1443 *str = __glXGetStringFromServer(dpy, priv->majorOpcode,
1444 X_GLXQueryServerString, screen, name);
1445 }
1446
1447 return *str;
1448 }
1449
1450 void __glXClientInfo ( Display *dpy, int opcode )
1451 {
1452 xGLXClientInfoReq *req;
1453 int size;
1454 char * ext_str = __glXGetClientGLExtensionString();
1455
1456 /* Send the glXClientInfo request */
1457 LockDisplay(dpy);
1458 GetReq(GLXClientInfo,req);
1459 req->reqType = opcode;
1460 req->glxCode = X_GLXClientInfo;
1461 req->major = GLX_MAJOR_VERSION;
1462 req->minor = GLX_MINOR_VERSION;
1463
1464 size = strlen( ext_str ) + 1;
1465 req->length += (size + 3) >> 2;
1466 req->numbytes = size;
1467 Data(dpy, ext_str, size);
1468
1469 UnlockDisplay(dpy);
1470 SyncHandle();
1471
1472 Xfree( ext_str );
1473 }
1474
1475
1476 /*
1477 ** EXT_import_context
1478 */
1479
1480 PUBLIC Display *glXGetCurrentDisplay(void)
1481 {
1482 GLXContext gc = __glXGetCurrentContext();
1483 if (NULL == gc) return NULL;
1484 return gc->currentDpy;
1485 }
1486
1487 PUBLIC GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1488 glXGetCurrentDisplay)
1489
1490 /**
1491 * Used internally by libGL to send \c xGLXQueryContextinfoExtReq requests
1492 * to the X-server.
1493 *
1494 * \param dpy Display where \c ctx was created.
1495 * \param ctx Context to query.
1496 * \returns \c Success on success. \c GLX_BAD_CONTEXT if \c ctx is invalid,
1497 * or zero if the request failed due to internal problems (i.e.,
1498 * unable to allocate temporary memory, etc.)
1499 *
1500 * \note
1501 * This function dynamically determines whether to use the EXT_import_context
1502 * version of the protocol or the GLX 1.3 version of the protocol.
1503 */
1504 static int __glXQueryContextInfo(Display *dpy, GLXContext ctx)
1505 {
1506 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1507 xGLXQueryContextReply reply;
1508 CARD8 opcode;
1509 GLuint numValues;
1510 int retval;
1511
1512 if (ctx == NULL) {
1513 return GLX_BAD_CONTEXT;
1514 }
1515 opcode = __glXSetupForCommand(dpy);
1516 if (!opcode) {
1517 return 0;
1518 }
1519
1520 /* Send the glXQueryContextInfoEXT request */
1521 LockDisplay(dpy);
1522
1523 if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
1524 xGLXQueryContextReq *req;
1525
1526 GetReq(GLXQueryContext, req);
1527
1528 req->reqType = opcode;
1529 req->glxCode = X_GLXQueryContext;
1530 req->context = (unsigned int)(ctx->xid);
1531 }
1532 else {
1533 xGLXVendorPrivateReq *vpreq;
1534 xGLXQueryContextInfoEXTReq *req;
1535
1536 GetReqExtra( GLXVendorPrivate,
1537 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1538 vpreq );
1539 req = (xGLXQueryContextInfoEXTReq *)vpreq;
1540 req->reqType = opcode;
1541 req->glxCode = X_GLXVendorPrivateWithReply;
1542 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1543 req->context = (unsigned int)(ctx->xid);
1544 }
1545
1546 _XReply(dpy, (xReply*) &reply, 0, False);
1547
1548 numValues = reply.n;
1549 if (numValues == 0)
1550 retval = Success;
1551 else if (numValues > __GLX_MAX_CONTEXT_PROPS)
1552 retval = 0;
1553 else
1554 {
1555 int *propList, *pProp;
1556 int nPropListBytes;
1557 int i;
1558
1559 nPropListBytes = numValues << 3;
1560 propList = (int *) Xmalloc(nPropListBytes);
1561 if (NULL == propList) {
1562 retval = 0;
1563 } else {
1564 _XRead(dpy, (char *)propList, nPropListBytes);
1565 pProp = propList;
1566 for (i=0; i < numValues; i++) {
1567 switch (*pProp++) {
1568 case GLX_SHARE_CONTEXT_EXT:
1569 ctx->share_xid = *pProp++;
1570 break;
1571 case GLX_VISUAL_ID_EXT:
1572 ctx->vid = *pProp++;
1573 break;
1574 case GLX_SCREEN:
1575 ctx->screen = *pProp++;
1576 break;
1577 case GLX_FBCONFIG_ID:
1578 ctx->fbconfigID = *pProp++;
1579 break;
1580 case GLX_RENDER_TYPE:
1581 ctx->renderType = *pProp++;
1582 break;
1583 default:
1584 pProp++;
1585 continue;
1586 }
1587 }
1588 Xfree((char *)propList);
1589 retval = Success;
1590 }
1591 }
1592 UnlockDisplay(dpy);
1593 SyncHandle();
1594 return retval;
1595 }
1596
1597 PUBLIC int
1598 GLX_PREFIX(glXQueryContext)(Display *dpy, GLXContext ctx,
1599 int attribute, int *value)
1600 {
1601 int retVal;
1602
1603 /* get the information from the server if we don't have it already */
1604 if (!ctx->isDirect && (ctx->vid == None)) {
1605 retVal = __glXQueryContextInfo(dpy, ctx);
1606 if (Success != retVal) return retVal;
1607 }
1608 switch (attribute) {
1609 case GLX_SHARE_CONTEXT_EXT:
1610 *value = (int)(ctx->share_xid);
1611 break;
1612 case GLX_VISUAL_ID_EXT:
1613 *value = (int)(ctx->vid);
1614 break;
1615 case GLX_SCREEN:
1616 *value = (int)(ctx->screen);
1617 break;
1618 case GLX_FBCONFIG_ID:
1619 *value = (int)(ctx->fbconfigID);
1620 break;
1621 case GLX_RENDER_TYPE:
1622 *value = (int)(ctx->renderType);
1623 break;
1624 default:
1625 return GLX_BAD_ATTRIBUTE;
1626 }
1627 return Success;
1628 }
1629
1630 PUBLIC GLX_ALIAS( int, glXQueryContextInfoEXT,
1631 (Display *dpy, GLXContext ctx, int attribute, int *value),
1632 (dpy, ctx, attribute, value),
1633 glXQueryContext )
1634
1635 PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx)
1636 {
1637 return ctx->xid;
1638 }
1639
1640 PUBLIC GLXContext GLX_PREFIX(glXImportContextEXT)(Display *dpy,
1641 GLXContextID contextID)
1642 {
1643 GLXContext ctx;
1644
1645 if (contextID == None) {
1646 return NULL;
1647 }
1648 if (__glXIsDirect(dpy, contextID)) {
1649 return NULL;
1650 }
1651
1652 ctx = CreateContext(dpy, NULL, NULL, NULL, False, contextID, False, 0);
1653 if (NULL != ctx) {
1654 if (Success != __glXQueryContextInfo(dpy, ctx)) {
1655 return NULL;
1656 }
1657 }
1658 return ctx;
1659 }
1660
1661 PUBLIC void GLX_PREFIX(glXFreeContextEXT)(Display *dpy, GLXContext ctx)
1662 {
1663 DestroyContext(dpy, ctx);
1664 }
1665
1666
1667
1668 /*
1669 * GLX 1.3 functions - these are just stubs for now!
1670 */
1671
1672 PUBLIC GLXFBConfig *GLX_PREFIX(glXChooseFBConfig)(Display *dpy, int screen,
1673 const int *attribList,
1674 int *nitems)
1675 {
1676 __GLcontextModes ** config_list;
1677 int list_size;
1678
1679
1680 config_list = (__GLcontextModes **)
1681 GLX_PREFIX(glXGetFBConfigs)( dpy, screen, & list_size );
1682
1683 if ( (config_list != NULL) && (list_size > 0) ) {
1684 list_size = choose_visual( config_list, list_size, attribList,
1685 GL_TRUE );
1686 if ( list_size == 0 ) {
1687 XFree( config_list );
1688 config_list = NULL;
1689 }
1690 }
1691
1692 *nitems = list_size;
1693 return (GLXFBConfig *) config_list;
1694 }
1695
1696
1697 PUBLIC GLXContext GLX_PREFIX(glXCreateNewContext)(Display *dpy,
1698 GLXFBConfig config,
1699 int renderType,
1700 GLXContext shareList,
1701 Bool allowDirect)
1702 {
1703 return CreateContext( dpy, NULL, (__GLcontextModes *) config, shareList,
1704 allowDirect, None, True, renderType );
1705 }
1706
1707
1708 PUBLIC GLXDrawable GLX_PREFIX(glXGetCurrentReadDrawable)(void)
1709 {
1710 GLXContext gc = __glXGetCurrentContext();
1711 return gc->currentReadable;
1712 }
1713
1714
1715 PUBLIC GLXFBConfig *GLX_PREFIX(glXGetFBConfigs)(Display *dpy, int screen,
1716 int *nelements)
1717 {
1718 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1719 __GLcontextModes ** config = NULL;
1720 int i;
1721
1722 if ( (priv->screenConfigs != NULL)
1723 && (screen >= 0) && (screen <= ScreenCount(dpy))
1724 && (priv->screenConfigs[screen].configs != NULL)
1725 && (priv->screenConfigs[screen].configs->fbconfigID != GLX_DONT_CARE) ) {
1726 unsigned num_configs = 0;
1727 __GLcontextModes * modes;
1728
1729
1730 for ( modes = priv->screenConfigs[screen].configs
1731 ; modes != NULL
1732 ; modes = modes->next ) {
1733 if ( modes->fbconfigID != GLX_DONT_CARE ) {
1734 num_configs++;
1735 }
1736 }
1737
1738 config = (__GLcontextModes **) Xmalloc( sizeof(__GLcontextModes *)
1739 * num_configs );
1740 if ( config != NULL ) {
1741 *nelements = num_configs;
1742 i = 0;
1743 for ( modes = priv->screenConfigs[screen].configs
1744 ; modes != NULL
1745 ; modes = modes->next ) {
1746 config[i] = modes;
1747 i++;
1748 }
1749 }
1750 }
1751 return (GLXFBConfig *) config;
1752 }
1753
1754
1755 PUBLIC int GLX_PREFIX(glXGetFBConfigAttrib)(Display *dpy, GLXFBConfig config,
1756 int attribute, int *value)
1757 {
1758 __GLcontextModes * const modes = ValidateGLXFBConfig( dpy, config );
1759
1760 return (modes != NULL)
1761 ? _gl_get_context_mode_data( modes, attribute, value )
1762 : GLXBadFBConfig;
1763 }
1764
1765
1766 PUBLIC XVisualInfo *GLX_PREFIX(glXGetVisualFromFBConfig)(Display *dpy,
1767 GLXFBConfig config)
1768 {
1769 XVisualInfo visualTemplate;
1770 __GLcontextModes * fbconfig = (__GLcontextModes *) config;
1771 int count;
1772
1773 /*
1774 ** Get a list of all visuals, return if list is empty
1775 */
1776 visualTemplate.visualid = fbconfig->visualID;
1777 return XGetVisualInfo(dpy,VisualIDMask,&visualTemplate,&count);
1778 }
1779
1780
1781 /*
1782 ** GLX_SGI_make_current_read
1783 */
1784
1785 PUBLIC GLX_ALIAS(GLXDrawable, glXGetCurrentReadDrawableSGI, (void), (),
1786 glXGetCurrentReadDrawable)
1787
1788
1789 /*
1790 ** GLX_SGI_swap_control
1791 */
1792 PUBLIC int GLX_PREFIX(glXSwapIntervalSGI)(int interval)
1793 {
1794 xGLXVendorPrivateReq *req;
1795 GLXContext gc = __glXGetCurrentContext();
1796 Display * dpy;
1797 CARD32 * interval_ptr;
1798 CARD8 opcode;
1799
1800 if ( gc == NULL ) {
1801 return GLX_BAD_CONTEXT;
1802 }
1803
1804 if ( interval <= 0 ) {
1805 return GLX_BAD_VALUE;
1806 }
1807
1808 #ifdef GLX_DIRECT_RENDERING
1809 if ( gc->isDirect ) {
1810 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1811 gc->screen );
1812 __DRIdrawable * const pdraw = GetDRIDrawable( gc->currentDpy,
1813 gc->currentDrawable,
1814 NULL );
1815 if ( __glXExtensionBitIsEnabled( psc, SGI_swap_control_bit )
1816 && (pdraw != NULL) ) {
1817 pdraw->swap_interval = interval;
1818 return 0;
1819 }
1820 else {
1821 return GLX_BAD_CONTEXT;
1822 }
1823 }
1824 #endif
1825 dpy = gc->currentDpy;
1826 opcode = __glXSetupForCommand(dpy);
1827 if (!opcode) {
1828 return 0;
1829 }
1830
1831 /* Send the glXSwapIntervalSGI request */
1832 LockDisplay(dpy);
1833 GetReqExtra(GLXVendorPrivate,sizeof(CARD32),req);
1834 req->reqType = opcode;
1835 req->glxCode = X_GLXVendorPrivate;
1836 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1837 req->contextTag = gc->currentContextTag;
1838
1839 interval_ptr = (CARD32 *) req + 1;
1840 *interval_ptr = interval;
1841
1842 UnlockDisplay(dpy);
1843 SyncHandle();
1844 XFlush(dpy);
1845
1846 return 0;
1847 }
1848
1849
1850 /*
1851 ** GLX_MESA_swap_control
1852 */
1853 PUBLIC GLint GLX_PREFIX(glXSwapIntervalMESA)(unsigned interval)
1854 {
1855 #ifdef GLX_DIRECT_RENDERING
1856 GLXContext gc = __glXGetCurrentContext();
1857
1858 if ( interval < 0 ) {
1859 return GLX_BAD_VALUE;
1860 }
1861
1862 if ( (gc != NULL) && gc->isDirect ) {
1863 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1864 gc->screen );
1865
1866 if ( (psc != NULL) && (psc->driScreen.private != NULL)
1867 && __glXExtensionBitIsEnabled( psc, MESA_swap_control_bit ) ) {
1868 __DRIdrawable * const pdraw =
1869 (*psc->driScreen.getDrawable)(gc->currentDpy,
1870 gc->currentDrawable,
1871 psc->driScreen.private);
1872 if ( pdraw != NULL ) {
1873 pdraw->swap_interval = interval;
1874 return 0;
1875 }
1876 }
1877 }
1878 #else
1879 (void) interval;
1880 #endif
1881
1882 return GLX_BAD_CONTEXT;
1883 }
1884
1885 PUBLIC GLint GLX_PREFIX(glXGetSwapIntervalMESA)( void )
1886 {
1887 #ifdef GLX_DIRECT_RENDERING
1888 GLXContext gc = __glXGetCurrentContext();
1889
1890 if ( (gc != NULL) && gc->isDirect ) {
1891 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1892 gc->screen );
1893
1894 if ( (psc != NULL) && (psc->driScreen.private != NULL)
1895 && __glXExtensionBitIsEnabled( psc, MESA_swap_control_bit ) ) {
1896 __DRIdrawable * const pdraw =
1897 (*psc->driScreen.getDrawable)(gc->currentDpy,
1898 gc->currentDrawable,
1899 psc->driScreen.private);
1900 if ( pdraw != NULL ) {
1901 return pdraw->swap_interval;
1902 }
1903 }
1904 }
1905 #endif
1906
1907 return 0;
1908 }
1909
1910
1911 /*
1912 ** GLX_MESA_swap_frame_usage
1913 */
1914
1915 PUBLIC GLint GLX_PREFIX(glXBeginFrameTrackingMESA)(Display *dpy,
1916 GLXDrawable drawable)
1917 {
1918 int status = GLX_BAD_CONTEXT;
1919 #ifdef GLX_DIRECT_RENDERING
1920 int screen;
1921 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1922 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1923
1924 if ( (pdraw != NULL) && (pdraw->frameTracking != NULL)
1925 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1926 status = pdraw->frameTracking( dpy, pdraw->private, GL_TRUE );
1927 }
1928 #else
1929 (void) dpy;
1930 (void) drawable;
1931 #endif
1932 return status;
1933 }
1934
1935
1936 PUBLIC GLint GLX_PREFIX(glXEndFrameTrackingMESA)(Display *dpy,
1937 GLXDrawable drawable)
1938 {
1939 int status = GLX_BAD_CONTEXT;
1940 #ifdef GLX_DIRECT_RENDERING
1941 int screen;
1942 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1943 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1944
1945 if ( (pdraw != NULL) && (pdraw->frameTracking != NULL)
1946 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1947 status = pdraw->frameTracking( dpy, pdraw->private, GL_FALSE );
1948 }
1949 #else
1950 (void) dpy;
1951 (void) drawable;
1952 #endif
1953 return status;
1954 }
1955
1956
1957 PUBLIC GLint GLX_PREFIX(glXGetFrameUsageMESA)(Display *dpy,
1958 GLXDrawable drawable,
1959 GLfloat *usage)
1960 {
1961 int status = GLX_BAD_CONTEXT;
1962 #ifdef GLX_DIRECT_RENDERING
1963 int screen;
1964 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1965 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1966
1967 if ( (pdraw != NULL ) && (pdraw->queryFrameTracking != NULL)
1968 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1969 int64_t sbc, missedFrames;
1970 float lastMissedUsage;
1971
1972 status = pdraw->queryFrameTracking( dpy, pdraw->private, &sbc,
1973 &missedFrames, &lastMissedUsage,
1974 usage );
1975 }
1976 #else
1977 (void) dpy;
1978 (void) drawable;
1979 (void) usage;
1980 #endif
1981 return status;
1982 }
1983
1984
1985 PUBLIC GLint GLX_PREFIX(glXQueryFrameTrackingMESA)(Display *dpy,
1986 GLXDrawable drawable,
1987 int64_t *sbc,
1988 int64_t *missedFrames,
1989 GLfloat *lastMissedUsage)
1990 {
1991 int status = GLX_BAD_CONTEXT;
1992 #ifdef GLX_DIRECT_RENDERING
1993 int screen;
1994 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1995 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1996
1997 if ( (pdraw != NULL ) && (pdraw->queryFrameTracking != NULL)
1998 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1999 float usage;
2000
2001 status = pdraw->queryFrameTracking( dpy, pdraw->private, sbc,
2002 missedFrames, lastMissedUsage,
2003 & usage );
2004 }
2005 #else
2006 (void) dpy;
2007 (void) drawable;
2008 (void) sbc;
2009 (void) missedFrames;
2010 (void) lastMissedUsage;
2011 #endif
2012 return status;
2013 }
2014
2015
2016 /*
2017 ** GLX_SGI_video_sync
2018 */
2019 PUBLIC int GLX_PREFIX(glXGetVideoSyncSGI)(unsigned int *count)
2020 {
2021 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
2022 * FIXME: there should be a GLX encoding for this call. I can find no
2023 * FIXME: documentation for the GLX encoding.
2024 */
2025 #ifdef GLX_DIRECT_RENDERING
2026 GLXContext gc = __glXGetCurrentContext();
2027
2028
2029 if ( (gc != NULL) && gc->isDirect ) {
2030 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
2031 gc->screen );
2032 if ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
2033 && psc->driScreen.private && psc->driScreen.getMSC) {
2034 int ret;
2035 int64_t temp;
2036
2037 ret = psc->driScreen.getMSC( psc->driScreen.private, & temp );
2038 *count = (unsigned) temp;
2039 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
2040 }
2041 }
2042 #else
2043 (void) count;
2044 #endif
2045 return GLX_BAD_CONTEXT;
2046 }
2047
2048 PUBLIC int GLX_PREFIX(glXWaitVideoSyncSGI)(int divisor, int remainder,
2049 unsigned int *count)
2050 {
2051 #ifdef GLX_DIRECT_RENDERING
2052 GLXContext gc = __glXGetCurrentContext();
2053
2054 if ( divisor <= 0 || remainder < 0 )
2055 return GLX_BAD_VALUE;
2056
2057 if ( (gc != NULL) && gc->isDirect ) {
2058 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
2059 gc->screen );
2060 if ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
2061 && psc->driScreen.private ) {
2062 __DRIdrawable * const pdraw =
2063 (*psc->driScreen.getDrawable)(gc->currentDpy,
2064 gc->currentDrawable,
2065 psc->driScreen.private);
2066 if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL) ) {
2067 int ret;
2068 int64_t msc;
2069 int64_t sbc;
2070
2071 ret = (*pdraw->waitForMSC)( gc->currentDpy, pdraw->private,
2072 0, divisor, remainder,
2073 & msc, & sbc );
2074 *count = (unsigned) msc;
2075 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
2076 }
2077 }
2078 }
2079 #else
2080 (void) count;
2081 #endif
2082 return GLX_BAD_CONTEXT;
2083 }
2084
2085
2086 /*
2087 ** GLX_SGIS_video_source
2088 */
2089 #if defined(_VL_H)
2090
2091 PUBLIC GLXVideoSourceSGIX GLX_PREFIX(glXCreateGLXVideoSourceSGIX)(Display *dpy,
2092 int screen, VLServer server, VLPath path,
2093 int nodeClass, VLNode drainNode)
2094 {
2095 (void) dpy;
2096 (void) screen;
2097 (void) server;
2098 (void) path;
2099 (void) nodeClass;
2100 (void) drainNode;
2101 return 0;
2102 }
2103
2104 PUBLIC void GLX_PREFIX(glXDestroyGLXVideoSourceSGIX)(Display *dpy,
2105 GLXVideoSourceSGIX src)
2106 {
2107 (void) dpy;
2108 (void) src;
2109 }
2110
2111 #endif
2112
2113
2114 /*
2115 ** GLX_SGIX_fbconfig
2116 ** Many of these functions are aliased to GLX 1.3 entry points in the
2117 ** GLX_functions table.
2118 */
2119
2120 PUBLIC GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
2121 (Display *dpy, GLXFBConfigSGIX config, int attribute, int *value),
2122 (dpy, config, attribute, value),
2123 glXGetFBConfigAttrib)
2124
2125 PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
2126 (Display *dpy, int screen, int *attrib_list, int *nelements),
2127 (dpy, screen, attrib_list, nelements),
2128 glXChooseFBConfig)
2129
2130 PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
2131 (Display * dpy, GLXFBConfigSGIX config),
2132 (dpy, config),
2133 glXGetVisualFromFBConfig)
2134
2135 PUBLIC GLXPixmap GLX_PREFIX(glXCreateGLXPixmapWithConfigSGIX)(Display *dpy,
2136 GLXFBConfigSGIX config, Pixmap pixmap)
2137 {
2138 xGLXVendorPrivateWithReplyReq *vpreq;
2139 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
2140 GLXPixmap xid = None;
2141 CARD8 opcode;
2142 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2143 __GLXscreenConfigs * psc;
2144
2145
2146 if ( (dpy == NULL) || (config == NULL) ) {
2147 return None;
2148 }
2149
2150 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2151 if ( (psc != NULL)
2152 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2153 opcode = __glXSetupForCommand(dpy);
2154 if (!opcode) {
2155 return None;
2156 }
2157
2158 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
2159 LockDisplay(dpy);
2160 GetReqExtra(GLXVendorPrivateWithReply,
2161 sz_xGLXCreateGLXPixmapWithConfigSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
2162 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *)vpreq;
2163 req->reqType = opcode;
2164 req->glxCode = X_GLXVendorPrivateWithReply;
2165 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
2166 req->screen = fbconfig->screen;
2167 req->fbconfig = fbconfig->fbconfigID;
2168 req->pixmap = pixmap;
2169 req->glxpixmap = xid = XAllocID(dpy);
2170 UnlockDisplay(dpy);
2171 SyncHandle();
2172 }
2173
2174 return xid;
2175 }
2176
2177 PUBLIC GLXContext GLX_PREFIX(glXCreateContextWithConfigSGIX)(Display *dpy,
2178 GLXFBConfigSGIX config, int renderType,
2179 GLXContext shareList, Bool allowDirect)
2180 {
2181 GLXContext gc = NULL;
2182 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2183 __GLXscreenConfigs * psc;
2184
2185
2186 if ( (dpy == NULL) || (config == NULL) ) {
2187 return None;
2188 }
2189
2190 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2191 if ( (psc != NULL)
2192 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2193 gc = CreateContext( dpy, NULL, (__GLcontextModes *) config, shareList,
2194 allowDirect, None, False, renderType );
2195 }
2196
2197 return gc;
2198 }
2199
2200
2201 PUBLIC GLXFBConfigSGIX GLX_PREFIX(glXGetFBConfigFromVisualSGIX)(Display *dpy,
2202 XVisualInfo *vis)
2203 {
2204 __GLXdisplayPrivate *priv;
2205 __GLXscreenConfigs *psc;
2206
2207 if ( (GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc ) != Success)
2208 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit )
2209 && (psc->configs->fbconfigID != GLX_DONT_CARE) ) {
2210 return (GLXFBConfigSGIX) _gl_context_modes_find_visual( psc->configs,
2211 vis->visualid );
2212 }
2213
2214 return NULL;
2215 }
2216
2217
2218 /*
2219 ** GLX_SGI_cushion
2220 */
2221 PUBLIC void GLX_PREFIX(glXCushionSGI)(Display *dpy, Window win, float cushion)
2222 {
2223 (void) dpy;
2224 (void) win;
2225 (void) cushion;
2226 }
2227
2228
2229 /*
2230 ** GLX_SGIX_video_resize
2231 */
2232 PUBLIC int GLX_PREFIX(glXBindChannelToWindowSGIX)(Display *dpy, int screen,
2233 int channel , Window window)
2234 {
2235 (void) dpy;
2236 (void) screen;
2237 (void) channel;
2238 (void) window;
2239 return 0;
2240 }
2241
2242 PUBLIC int GLX_PREFIX(glXChannelRectSGIX)(Display *dpy, int screen, int channel,
2243 int x, int y, int w, int h)
2244 {
2245 (void) dpy;
2246 (void) screen;
2247 (void) channel;
2248 (void) x;
2249 (void) y;
2250 (void) w;
2251 (void) h;
2252 return 0;
2253 }
2254
2255 PUBLIC int GLX_PREFIX(glXQueryChannelRectSGIX)(Display *dpy, int screen,
2256 int channel, int *x, int *y,
2257 int *w, int *h)
2258 {
2259 (void) dpy;
2260 (void) screen;
2261 (void) channel;
2262 (void) x;
2263 (void) y;
2264 (void) w;
2265 (void) h;
2266 return 0;
2267 }
2268
2269 int GLX_PREFIX(glXQueryChannelDeltasSGIX)(Display *dpy, int screen, int channel,
2270 int *dx, int *dy, int *dw, int *dh)
2271 {
2272 (void) dpy;
2273 (void) screen;
2274 (void) channel;
2275 (void) dx;
2276 (void) dy;
2277 (void) dw;
2278 (void) dh;
2279 return 0;
2280 }
2281
2282 PUBLIC int GLX_PREFIX(glXChannelRectSyncSGIX)(Display *dpy, int screen,
2283 int channel, GLenum synctype)
2284 {
2285 (void) dpy;
2286 (void) screen;
2287 (void) channel;
2288 (void) synctype;
2289 return 0;
2290 }
2291
2292
2293 #if defined(_DM_BUFFER_H_)
2294
2295 PUBLIC Bool GLX_PREFIX(glXAssociateDMPbufferSGIX)(Display *dpy,
2296 GLXPbufferSGIX pbuffer,
2297 DMparams *params,
2298 DMbuffer dmbuffer)
2299 {
2300 (void) dpy;
2301 (void) pbuffer;
2302 (void) params;
2303 (void) dmbuffer;
2304 return False;
2305 }
2306
2307 #endif
2308
2309
2310 /*
2311 ** GLX_SGIX_swap_group
2312 */
2313 PUBLIC void GLX_PREFIX(glXJoinSwapGroupSGIX)(Display *dpy, GLXDrawable drawable,
2314 GLXDrawable member)
2315 {
2316 (void) dpy;
2317 (void) drawable;
2318 (void) member;
2319 }
2320
2321
2322 /*
2323 ** GLX_SGIX_swap_barrier
2324 */
2325 PUBLIC void GLX_PREFIX(glXBindSwapBarrierSGIX)(Display *dpy,
2326 GLXDrawable drawable,
2327 int barrier)
2328 {
2329 (void) dpy;
2330 (void) drawable;
2331 (void) barrier;
2332 }
2333
2334 PUBLIC Bool GLX_PREFIX(glXQueryMaxSwapBarriersSGIX)(Display *dpy, int screen,
2335 int *max)
2336 {
2337 (void) dpy;
2338 (void) screen;
2339 (void) max;
2340 return False;
2341 }
2342
2343
2344 /*
2345 ** GLX_SUN_get_transparent_index
2346 */
2347 PUBLIC Status GLX_PREFIX(glXGetTransparentIndexSUN)(Display *dpy,
2348 Window overlay,
2349 Window underlay,
2350 long *pTransparent)
2351 {
2352 (void) dpy;
2353 (void) overlay;
2354 (void) underlay;
2355 (void) pTransparent;
2356 return 0;
2357 }
2358
2359
2360 /*
2361 ** GLX_OML_sync_control
2362 */
2363 PUBLIC Bool GLX_PREFIX(glXGetSyncValuesOML)(Display *dpy, GLXDrawable drawable,
2364 int64_t *ust, int64_t *msc, int64_t *sbc)
2365 {
2366 #ifdef GLX_DIRECT_RENDERING
2367 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2368
2369 if ( priv != NULL ) {
2370 int i;
2371 __DRIdrawable * const pdraw = GetDRIDrawable( dpy, drawable, & i );
2372 __GLXscreenConfigs * const psc = &priv->screenConfigs[i];
2373
2374 assert( (pdraw == NULL) || (i != -1) );
2375 return ( (pdraw && pdraw->getSBC && psc->driScreen.getMSC)
2376 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )
2377 && ((*psc->driScreen.getMSC)( psc->driScreen.private, msc ) == 0)
2378 && ((*pdraw->getSBC)( dpy, psc->driScreen.private, sbc ) == 0)
2379 && (__glXGetUST( ust ) == 0) );
2380 }
2381 #else
2382 (void) dpy;
2383 (void) drawable;
2384 (void) ust;
2385 (void) msc;
2386 (void) sbc;
2387 #endif
2388 return False;
2389 }
2390
2391
2392 /**
2393 * Determine the refresh rate of the specified drawable and display.
2394 *
2395 * \param dpy Display whose refresh rate is to be determined.
2396 * \param drawable Drawable whose refresh rate is to be determined.
2397 * \param numerator Numerator of the refresh rate.
2398 * \param demoninator Denominator of the refresh rate.
2399 * \return If the refresh rate for the specified display and drawable could
2400 * be calculated, True is returned. Otherwise False is returned.
2401 *
2402 * \note This function is implemented entirely client-side. A lot of other
2403 * functionality is required to export GLX_OML_sync_control, so on
2404 * XFree86 this function can be called for direct-rendering contexts
2405 * when GLX_OML_sync_control appears in the client extension string.
2406 */
2407
2408 PUBLIC Bool GLX_PREFIX(glXGetMscRateOML)(Display * dpy, GLXDrawable drawable,
2409 int32_t * numerator, int32_t * denominator)
2410 {
2411 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2412 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2413
2414
2415 if ( priv != NULL ) {
2416 XF86VidModeModeLine mode_line;
2417 int dot_clock;
2418 int screen_num;
2419 int i;
2420
2421
2422 GetDRIDrawable( dpy, drawable, & screen_num );
2423 if ( (screen_num != -1)
2424 && XF86VidModeQueryVersion( dpy, & i, & i )
2425 && XF86VidModeGetModeLine( dpy, screen_num, & dot_clock,
2426 & mode_line ) ) {
2427 unsigned n = dot_clock * 1000;
2428 unsigned d = mode_line.vtotal * mode_line.htotal;
2429
2430 # define V_INTERLACE 0x010
2431 # define V_DBLSCAN 0x020
2432
2433 if ( (mode_line.flags & V_INTERLACE) ) {
2434 n *= 2;
2435 }
2436 else if ( (mode_line.flags & V_DBLSCAN) ) {
2437 d *= 2;
2438 }
2439
2440 /* The OML_sync_control spec requires that if the refresh rate is a
2441 * whole number, that the returned numerator be equal to the refresh
2442 * rate and the denominator be 1.
2443 */
2444
2445 if ( (n % d) == 0 ) {
2446 n /= d;
2447 d = 1;
2448 }
2449 else {
2450 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2451
2452
2453 /* This is a poor man's way to reduce a fraction. It's far from
2454 * perfect, but it will work well enough for this situation.
2455 */
2456
2457 for ( i = 0 ; f[i] != 0 ; i++ ) {
2458 while ( ((n % f[i]) == 0) && ((d % f[i]) == 0) ) {
2459 d /= f[i];
2460 n /= f[i];
2461 }
2462 }
2463 }
2464
2465 *numerator = n;
2466 *denominator = d;
2467
2468 (void) drawable;
2469 return True;
2470 }
2471 }
2472 #else
2473 (void) dpy;
2474 (void) drawable;
2475 (void) numerator;
2476 (void) denominator;
2477 #endif
2478 return False;
2479 }
2480
2481
2482 PUBLIC int64_t GLX_PREFIX(glXSwapBuffersMscOML)(Display *dpy,
2483 GLXDrawable drawable,
2484 int64_t target_msc,
2485 int64_t divisor,
2486 int64_t remainder)
2487 {
2488 #ifdef GLX_DIRECT_RENDERING
2489 int screen;
2490 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2491 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2492
2493 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2494 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2495 * of -1 if the function failed because of errors detected in the input
2496 * parameters"
2497 */
2498 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2499 return -1;
2500 if ( divisor > 0 && remainder >= divisor )
2501 return -1;
2502
2503 if ( (pdraw != NULL) && (pdraw->swapBuffersMSC != NULL)
2504 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
2505 return (*pdraw->swapBuffersMSC)(dpy, pdraw->private, target_msc,
2506 divisor, remainder);
2507 }
2508 #else
2509 (void) dpy;
2510 (void) drawable;
2511 (void) target_msc;
2512 (void) divisor;
2513 (void) remainder;
2514 #endif
2515 return 0;
2516 }
2517
2518
2519 PUBLIC Bool GLX_PREFIX(glXWaitForMscOML)(Display * dpy, GLXDrawable drawable,
2520 int64_t target_msc, int64_t divisor,
2521 int64_t remainder, int64_t *ust,
2522 int64_t *msc, int64_t *sbc)
2523 {
2524 #ifdef GLX_DIRECT_RENDERING
2525 int screen;
2526 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2527 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2528 int ret;
2529
2530 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2531 * error", but the return type in the spec is Bool.
2532 */
2533 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2534 return False;
2535 if ( divisor > 0 && remainder >= divisor )
2536 return False;
2537
2538 if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL)
2539 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
2540 ret = (*pdraw->waitForMSC)( dpy, pdraw->private, target_msc,
2541 divisor, remainder, msc, sbc );
2542
2543 /* __glXGetUST returns zero on success and non-zero on failure.
2544 * This function returns True on success and False on failure.
2545 */
2546 return ( (ret == 0) && (__glXGetUST( ust ) == 0) );
2547 }
2548 #else
2549 (void) dpy;
2550 (void) drawable;
2551 (void) target_msc;
2552 (void) divisor;
2553 (void) remainder;
2554 (void) ust;
2555 (void) msc;
2556 (void) sbc;
2557 #endif
2558 return False;
2559 }
2560
2561
2562 PUBLIC Bool GLX_PREFIX(glXWaitForSbcOML)(Display * dpy, GLXDrawable drawable,
2563 int64_t target_sbc, int64_t *ust,
2564 int64_t *msc, int64_t *sbc )
2565 {
2566 #ifdef GLX_DIRECT_RENDERING
2567 int screen;
2568 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2569 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2570 int ret;
2571
2572 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2573 * error", but the return type in the spec is Bool.
2574 */
2575 if ( target_sbc < 0 )
2576 return False;
2577
2578 if ( (pdraw != NULL) && (pdraw->waitForSBC != NULL)
2579 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )) {
2580 ret = (*pdraw->waitForSBC)( dpy, pdraw->private, target_sbc, msc, sbc );
2581
2582 /* __glXGetUST returns zero on success and non-zero on failure.
2583 * This function returns True on success and False on failure.
2584 */
2585 return( (ret == 0) && (__glXGetUST( ust ) == 0) );
2586 }
2587 #else
2588 (void) dpy;
2589 (void) drawable;
2590 (void) target_sbc;
2591 (void) ust;
2592 (void) msc;
2593 (void) sbc;
2594 #endif
2595 return False;
2596 }
2597
2598
2599 /**
2600 * GLX_MESA_allocate_memory
2601 */
2602 /*@{*/
2603
2604 PUBLIC void *GLX_PREFIX(glXAllocateMemoryMESA)(Display *dpy, int scrn,
2605 size_t size, float readFreq,
2606 float writeFreq, float priority)
2607 {
2608 #ifdef GLX_DIRECT_RENDERING
2609 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2610
2611 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2612 if (psc && psc->driScreen.private && psc->driScreen.allocateMemory) {
2613 return (*psc->driScreen.allocateMemory)( dpy, scrn, size,
2614 readFreq, writeFreq,
2615 priority );
2616 }
2617 }
2618 #else
2619 (void) dpy;
2620 (void) scrn;
2621 (void) size;
2622 (void) readFreq;
2623 (void) writeFreq;
2624 (void) priority;
2625 #endif /* GLX_DIRECT_RENDERING */
2626
2627 return NULL;
2628 }
2629
2630
2631 PUBLIC void GLX_PREFIX(glXFreeMemoryMESA)(Display *dpy, int scrn, void *pointer)
2632 {
2633 #ifdef GLX_DIRECT_RENDERING
2634 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2635
2636 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2637 if (psc && psc->driScreen.private && psc->driScreen.freeMemory) {
2638 (*psc->driScreen.freeMemory)( dpy, scrn, pointer );
2639 }
2640 }
2641 #else
2642 (void) dpy;
2643 (void) scrn;
2644 (void) pointer;
2645 #endif /* GLX_DIRECT_RENDERING */
2646 }
2647
2648
2649 PUBLIC GLuint GLX_PREFIX(glXGetMemoryOffsetMESA)( Display *dpy, int scrn,
2650 const void *pointer )
2651 {
2652 #ifdef GLX_DIRECT_RENDERING
2653 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2654
2655 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2656 if (psc && psc->driScreen.private && psc->driScreen.memoryOffset) {
2657 return (*psc->driScreen.memoryOffset)( dpy, scrn, pointer );
2658 }
2659 }
2660 #else
2661 (void) dpy;
2662 (void) scrn;
2663 (void) pointer;
2664 #endif /* GLX_DIRECT_RENDERING */
2665
2666 return ~0L;
2667 }
2668 /*@}*/
2669
2670
2671 /**
2672 * Mesa extension stubs. These will help reduce portability problems.
2673 */
2674 /*@{*/
2675
2676 /**
2677 * Release all buffers associated with the specified GLX drawable.
2678 *
2679 * \todo
2680 * This function was intended for stand-alone Mesa. The issue there is that
2681 * the library doesn't get any notification when a window is closed. In
2682 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2683 * supported, there are 3 different functions to destroy a drawable. It
2684 * should be possible to create GLX protocol (or have it determine which
2685 * protocol to use based on the type of the drawable) to have one function
2686 * do the work of 3. For the direct-rendering case, this function could
2687 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2688 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2689 * would need to be used. This really should be done as part of the new DRI
2690 * interface work.
2691 *
2692 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2693 * __driGarbageCollectDrawables
2694 * glXDestroyGLXPixmap
2695 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2696 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2697 */
2698 PUBLIC Bool GLX_PREFIX(glXReleaseBuffersMESA)( Display *dpy, GLXDrawable d )
2699 {
2700 (void) dpy;
2701 (void) d;
2702 return False;
2703 }
2704
2705
2706 PUBLIC GLXPixmap GLX_PREFIX(glXCreateGLXPixmapMESA)( Display *dpy,
2707 XVisualInfo *visual,
2708 Pixmap pixmap,
2709 Colormap cmap )
2710 {
2711 (void) dpy;
2712 (void) visual;
2713 (void) pixmap;
2714 (void) cmap;
2715 return 0;
2716 }
2717
2718
2719 PUBLIC void GLX_PREFIX(glXCopySubBufferMESA)(Display *dpy, GLXDrawable drawable,
2720 int x, int y,
2721 int width, int height)
2722 {
2723 (void) dpy;
2724 (void) drawable;
2725 (void) x;
2726 (void) y;
2727 (void) width;
2728 (void) height;
2729 }
2730
2731
2732 PUBLIC Bool GLX_PREFIX(glXSet3DfxModeMESA)( int mode )
2733 {
2734 (void) mode;
2735 return GL_FALSE;
2736 }
2737 /*@}*/
2738
2739
2740
2741 /**
2742 * \c strdup is actually not a standard ANSI C or POSIX routine.
2743 * Irix will not define it if ANSI mode is in effect.
2744 *
2745 * \sa strdup
2746 */
2747 char *
2748 __glXstrdup(const char *str)
2749 {
2750 char *copy;
2751 copy = (char *) Xmalloc(strlen(str) + 1);
2752 if (!copy)
2753 return NULL;
2754 strcpy(copy, str);
2755 return copy;
2756 }
2757
2758 /*
2759 ** glXGetProcAddress support
2760 */
2761
2762 struct name_address_pair {
2763 const char *Name;
2764 GLvoid *Address;
2765 };
2766
2767 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2768 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2769
2770 static const struct name_address_pair GLX_functions[] = {
2771 /*** GLX_VERSION_1_0 ***/
2772 GLX_FUNCTION( glXChooseVisual ),
2773 GLX_FUNCTION( glXCopyContext ),
2774 GLX_FUNCTION( glXCreateContext ),
2775 GLX_FUNCTION( glXCreateGLXPixmap ),
2776 GLX_FUNCTION( glXDestroyContext ),
2777 GLX_FUNCTION( glXDestroyGLXPixmap ),
2778 GLX_FUNCTION( glXGetConfig ),
2779 GLX_FUNCTION( glXGetCurrentContext ),
2780 GLX_FUNCTION( glXGetCurrentDrawable ),
2781 GLX_FUNCTION( glXIsDirect ),
2782 GLX_FUNCTION( glXMakeCurrent ),
2783 GLX_FUNCTION( glXQueryExtension ),
2784 GLX_FUNCTION( glXQueryVersion ),
2785 GLX_FUNCTION( glXSwapBuffers ),
2786 GLX_FUNCTION( glXUseXFont ),
2787 GLX_FUNCTION( glXWaitGL ),
2788 GLX_FUNCTION( glXWaitX ),
2789
2790 /*** GLX_VERSION_1_1 ***/
2791 GLX_FUNCTION( glXGetClientString ),
2792 GLX_FUNCTION( glXQueryExtensionsString ),
2793 GLX_FUNCTION( glXQueryServerString ),
2794
2795 /*** GLX_VERSION_1_2 ***/
2796 GLX_FUNCTION( glXGetCurrentDisplay ),
2797
2798 /*** GLX_VERSION_1_3 ***/
2799 GLX_FUNCTION( glXChooseFBConfig ),
2800 GLX_FUNCTION( glXCreateNewContext ),
2801 GLX_FUNCTION( glXCreatePbuffer ),
2802 GLX_FUNCTION( glXCreatePixmap ),
2803 GLX_FUNCTION( glXCreateWindow ),
2804 GLX_FUNCTION( glXDestroyPbuffer ),
2805 GLX_FUNCTION( glXDestroyPixmap ),
2806 GLX_FUNCTION( glXDestroyWindow ),
2807 GLX_FUNCTION( glXGetCurrentReadDrawable ),
2808 GLX_FUNCTION( glXGetFBConfigAttrib ),
2809 GLX_FUNCTION( glXGetFBConfigs ),
2810 GLX_FUNCTION( glXGetSelectedEvent ),
2811 GLX_FUNCTION( glXGetVisualFromFBConfig ),
2812 GLX_FUNCTION( glXMakeContextCurrent ),
2813 GLX_FUNCTION( glXQueryContext ),
2814 GLX_FUNCTION( glXQueryDrawable ),
2815 GLX_FUNCTION( glXSelectEvent ),
2816
2817 /*** GLX_SGI_swap_control ***/
2818 GLX_FUNCTION( glXSwapIntervalSGI ),
2819
2820 /*** GLX_SGI_video_sync ***/
2821 GLX_FUNCTION( glXGetVideoSyncSGI ),
2822 GLX_FUNCTION( glXWaitVideoSyncSGI ),
2823
2824 /*** GLX_SGI_make_current_read ***/
2825 GLX_FUNCTION2( glXMakeCurrentReadSGI, glXMakeContextCurrent ),
2826 GLX_FUNCTION2( glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable ),
2827
2828 /*** GLX_SGIX_video_source ***/
2829 #if defined(_VL_H)
2830 GLX_FUNCTION( glXCreateGLXVideoSourceSGIX ),
2831 GLX_FUNCTION( glXDestroyGLXVideoSourceSGIX ),
2832 #endif
2833
2834 /*** GLX_EXT_import_context ***/
2835 GLX_FUNCTION( glXFreeContextEXT ),
2836 GLX_FUNCTION( glXGetContextIDEXT ),
2837 GLX_FUNCTION2( glXGetCurrentDisplayEXT, glXGetCurrentDisplay ),
2838 GLX_FUNCTION( glXImportContextEXT ),
2839 GLX_FUNCTION2( glXQueryContextInfoEXT, glXQueryContext ),
2840
2841 /*** GLX_SGIX_fbconfig ***/
2842 GLX_FUNCTION2( glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib ),
2843 GLX_FUNCTION2( glXChooseFBConfigSGIX, glXChooseFBConfig ),
2844 GLX_FUNCTION( glXCreateGLXPixmapWithConfigSGIX ),
2845 GLX_FUNCTION( glXCreateContextWithConfigSGIX ),
2846 GLX_FUNCTION2( glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig ),
2847 GLX_FUNCTION( glXGetFBConfigFromVisualSGIX ),
2848
2849 /*** GLX_SGIX_pbuffer ***/
2850 GLX_FUNCTION( glXCreateGLXPbufferSGIX ),
2851 GLX_FUNCTION( glXDestroyGLXPbufferSGIX ),
2852 GLX_FUNCTION( glXQueryGLXPbufferSGIX ),
2853 GLX_FUNCTION( glXSelectEventSGIX ),
2854 GLX_FUNCTION( glXGetSelectedEventSGIX ),
2855
2856 /*** GLX_SGI_cushion ***/
2857 GLX_FUNCTION( glXCushionSGI ),
2858
2859 /*** GLX_SGIX_video_resize ***/
2860 GLX_FUNCTION( glXBindChannelToWindowSGIX ),
2861 GLX_FUNCTION( glXChannelRectSGIX ),
2862 GLX_FUNCTION( glXQueryChannelRectSGIX ),
2863 GLX_FUNCTION( glXQueryChannelDeltasSGIX ),
2864 GLX_FUNCTION( glXChannelRectSyncSGIX ),
2865
2866 /*** GLX_SGIX_dmbuffer **/
2867 #if defined(_DM_BUFFER_H_)
2868 GLX_FUNCTION( glXAssociateDMPbufferSGIX ),
2869 #endif
2870
2871 /*** GLX_SGIX_swap_group ***/
2872 GLX_FUNCTION( glXJoinSwapGroupSGIX ),
2873
2874 /*** GLX_SGIX_swap_barrier ***/
2875 GLX_FUNCTION( glXBindSwapBarrierSGIX ),
2876 GLX_FUNCTION( glXQueryMaxSwapBarriersSGIX ),
2877
2878 /*** GLX_SUN_get_transparent_index ***/
2879 GLX_FUNCTION( glXGetTransparentIndexSUN ),
2880
2881 /*** GLX_MESA_allocate_memory ***/
2882 GLX_FUNCTION( glXAllocateMemoryMESA ),
2883 GLX_FUNCTION( glXFreeMemoryMESA ),
2884 GLX_FUNCTION( glXGetMemoryOffsetMESA ),
2885
2886 /*** GLX_MESA_copy_sub_buffer ***/
2887 GLX_FUNCTION( glXCopySubBufferMESA ),
2888
2889 /*** GLX_MESA_pixmap_colormap ***/
2890 GLX_FUNCTION( glXCreateGLXPixmapMESA ),
2891
2892 /*** GLX_MESA_release_buffers ***/
2893 GLX_FUNCTION( glXReleaseBuffersMESA ),
2894
2895 /*** GLX_MESA_set_3dfx_mode ***/
2896 GLX_FUNCTION( glXSet3DfxModeMESA ),
2897
2898 /*** GLX_MESA_swap_control ***/
2899 GLX_FUNCTION( glXSwapIntervalMESA ),
2900 GLX_FUNCTION( glXGetSwapIntervalMESA ),
2901
2902 /*** GLX_MESA_swap_frame_usage ***/
2903 GLX_FUNCTION( glXBeginFrameTrackingMESA ),
2904 GLX_FUNCTION( glXEndFrameTrackingMESA ),
2905 GLX_FUNCTION( glXGetFrameUsageMESA ),
2906 GLX_FUNCTION( glXQueryFrameTrackingMESA ),
2907
2908 /*** GLX_ARB_get_proc_address ***/
2909 GLX_FUNCTION( glXGetProcAddressARB ),
2910
2911 /*** GLX 1.4 ***/
2912 GLX_FUNCTION2( glXGetProcAddress, glXGetProcAddressARB ),
2913
2914 /*** GLX_OML_sync_control ***/
2915 GLX_FUNCTION( glXWaitForSbcOML ),
2916 GLX_FUNCTION( glXWaitForMscOML ),
2917 GLX_FUNCTION( glXSwapBuffersMscOML ),
2918 GLX_FUNCTION( glXGetMscRateOML ),
2919 GLX_FUNCTION( glXGetSyncValuesOML ),
2920
2921 #ifdef GLX_DIRECT_RENDERING
2922 /***
2923 *** Internal functions useful to DRI drivers
2924 *** With this, the DRI drivers shouldn't need dlopen()/dlsym() to
2925 *** access internal libGL functions which may or may not exist.
2926 ***/
2927 GLX_FUNCTION( __glXInitialize ),
2928 GLX_FUNCTION( __glXFindDRIScreen ),
2929 GLX_FUNCTION( __glXGetInternalVersion ),
2930 GLX_FUNCTION( __glXWindowExists ),
2931 GLX_FUNCTION2( __glXCreateContextWithConfig, XF86DRICreateContextWithConfig ),
2932 GLX_FUNCTION2( __glXGetDrawableInfo, XF86DRIGetDrawableInfo ),
2933
2934 /*** DRI configuration ***/
2935 GLX_FUNCTION( glXGetScreenDriver ),
2936 GLX_FUNCTION( glXGetDriverConfig ),
2937
2938 GLX_FUNCTION( __glXScrEnableExtension ),
2939
2940 GLX_FUNCTION( __glXGetUST ),
2941
2942 GLX_FUNCTION2( __glXCreateContextModes, _gl_context_modes_create ),
2943 GLX_FUNCTION2( __glXDestroyContextModes, _gl_context_modes_destroy ),
2944 #endif
2945
2946 { NULL, NULL } /* end of list */
2947 };
2948
2949
2950 static const GLvoid *
2951 get_glx_proc_address(const char *funcName)
2952 {
2953 GLuint i;
2954
2955 /* try static functions */
2956 for (i = 0; GLX_functions[i].Name; i++) {
2957 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2958 return GLX_functions[i].Address;
2959 }
2960
2961 return NULL;
2962 }
2963
2964
2965 #ifndef GLX_BUILT_IN_XMESA
2966 /**
2967 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2968 * \c glXGetProcAddress.
2969 *
2970 * \param procName Name of a GL or GLX function.
2971 * \returns A pointer to the named function
2972 *
2973 * \sa glXGetProcAddress
2974 */
2975 PUBLIC void (*glXGetProcAddressARB(const GLubyte *procName))( void )
2976 {
2977 typedef void (*gl_function)( void );
2978 gl_function f;
2979
2980
2981 /* Search the table of GLX and internal functions first. If that
2982 * fails and the supplied name could be a valid core GL name, try
2983 * searching the core GL function table. This check is done to prevent
2984 * DRI based drivers from searching the core GL function table for
2985 * internal API functions.
2986 */
2987
2988 f = (gl_function) get_glx_proc_address((const char *) procName);
2989 if ( (f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2990 && (procName[2] != 'X') ) {
2991 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2992 }
2993
2994 return f;
2995 }
2996
2997 /**
2998 * Get the address of a named GL function. This is the GLX 1.4 name for
2999 * \c glXGetProcAddressARB.
3000 *
3001 * \param procName Name of a GL or GLX function.
3002 * \returns A pointer to the named function
3003 *
3004 * \sa glXGetProcAddressARB
3005 */
3006 PUBLIC void (*glXGetProcAddress(const GLubyte *procName))( void )
3007 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
3008 __attribute__ ((alias ("glXGetProcAddressARB")));
3009 #else
3010 {
3011 return glXGetProcAddressARB(procName);
3012 }
3013 #endif /* __GNUC__ */
3014 #endif /* GLX_BUILT_IN_XMESA */
3015
3016
3017 #ifdef GLX_DIRECT_RENDERING
3018 /**
3019 * Retrieves the verion of the internal libGL API in YYYYMMDD format. This
3020 * might be used by the DRI drivers to determine how new libGL is at runtime.
3021 * Drivers should not call this function directly. They should instead use
3022 * \c glXGetProcAddress to obtain a pointer to the function.
3023 *
3024 * \returns An 8-digit decimal number representing the internal libGL API in
3025 * YYYYMMDD format.
3026 *
3027 * \sa glXGetProcAddress, PFNGLXGETINTERNALVERSIONPROC
3028 *
3029 * \since Internal API version 20021121.
3030 */
3031 int __glXGetInternalVersion(void)
3032 {
3033 /* History:
3034 * 20021121 - Initial version
3035 * 20021128 - Added __glXWindowExists() function
3036 * 20021207 - Added support for dynamic GLX extensions,
3037 * GLX_SGI_swap_control, GLX_SGI_video_sync,
3038 * GLX_OML_sync_control, and GLX_MESA_swap_control.
3039 * Never officially released. Do NOT test against
3040 * this version. Use 20030317 instead.
3041 * 20030317 - Added support GLX_SGIX_fbconfig,
3042 * GLX_MESA_swap_frame_usage, GLX_OML_swap_method,
3043 * GLX_{ARB,SGIS}_multisample, and
3044 * GLX_SGIX_visual_select_group.
3045 * 20030606 - Added support for GLX_SGI_make_current_read.
3046 * 20030813 - Made support for dynamic extensions multi-head aware.
3047 * 20030818 - Added support for GLX_MESA_allocate_memory in place of the
3048 * deprecated GLX_NV_vertex_array_range & GLX_MESA_agp_offset
3049 * interfaces.
3050 * 20031201 - Added support for the first round of DRI interface changes.
3051 * Do NOT test against this version! It has binary
3052 * compatibility bugs, use 20040317 instead.
3053 * 20040317 - Added the 'mode' field to __DRIcontextRec.
3054 * 20040415 - Added support for bindContext3 and unbindContext3.
3055 * 20040602 - Add __glXGetDrawableInfo. I though that was there
3056 * months ago. :(
3057 */
3058 return 20040602;
3059 }
3060
3061
3062
3063 static Bool windowExistsFlag;
3064
3065 static int windowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
3066 {
3067 if (xerr->error_code == BadWindow) {
3068 windowExistsFlag = GL_FALSE;
3069 }
3070 return 0;
3071 }
3072
3073 /**
3074 * Determine if a window associated with a \c GLXDrawable exists on the
3075 * X-server. This function is not used internally by libGL. It is provided
3076 * as a utility function for DRI drivers.
3077 * Drivers should not call this function directly. They should instead use
3078 * \c glXGetProcAddress to obtain a pointer to the function.
3079 *
3080 * \param dpy Display associated with the drawable to be queried.
3081 * \param draw \c GLXDrawable to test.
3082 *
3083 * \returns \c GL_TRUE if a window exists that is associated with \c draw,
3084 * otherwise \c GL_FALSE is returned.
3085 *
3086 * \warning This function is not currently thread-safe.
3087 *
3088 * \sa glXGetProcAddress
3089 *
3090 * \since Internal API version 20021128.
3091 */
3092 static Bool __glXWindowExists(Display *dpy, GLXDrawable draw)
3093 {
3094 XWindowAttributes xwa;
3095 int (*oldXErrorHandler)(Display *, XErrorEvent *);
3096
3097 XSync(dpy, GL_FALSE);
3098 windowExistsFlag = GL_TRUE;
3099 oldXErrorHandler = XSetErrorHandler(windowExistsErrorHandler);
3100 XGetWindowAttributes(dpy, draw, &xwa); /* dummy request */
3101 XSetErrorHandler(oldXErrorHandler);
3102 return windowExistsFlag;
3103 }
3104
3105
3106 /**
3107 * Get the unadjusted system time (UST). Currently, the UST is measured in
3108 * microseconds since Epoc. The actual resolution of the UST may vary from
3109 * system to system, and the units may vary from release to release.
3110 * Drivers should not call this function directly. They should instead use
3111 * \c glXGetProcAddress to obtain a pointer to the function.
3112 *
3113 * \param ust Location to store the 64-bit UST
3114 * \returns Zero on success or a negative errno value on failure.
3115 *
3116 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
3117 *
3118 * \since Internal API version 20030317.
3119 */
3120 int __glXGetUST( int64_t * ust )
3121 {
3122 struct timeval tv;
3123
3124 if ( ust == NULL ) {
3125 return -EFAULT;
3126 }
3127
3128 if ( gettimeofday( & tv, NULL ) == 0 ) {
3129 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
3130 return 0;
3131 } else {
3132 return -errno;
3133 }
3134 }
3135 #endif /* GLX_DIRECT_RENDERING */