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