1 /* $XFree86: xc/lib/GL/glx/glxext.c,v 1.22 2003/12/08 17:35:28 dawes Exp $ */
4 ** License Applicability. Except to the extent portions of this file are
5 ** made subject to an alternative license as permitted in the SGI Free
6 ** Software License B, Version 1.1 (the "License"), the contents of this
7 ** file are subject only to the provisions of the License. You may not use
8 ** this file except in compliance with the License. You may obtain a copy
9 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
10 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
12 ** http://oss.sgi.com/projects/FreeB
14 ** Note that, as provided in the License, the Software is distributed on an
15 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
16 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
17 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
18 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
20 ** Original Code. The Original Code is: OpenGL Sample Implementation,
21 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
22 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
23 ** Copyright in any portions created by third parties is as indicated
24 ** elsewhere herein. All Rights Reserved.
26 ** Additional Notice Provisions: The application programming interfaces
27 ** established by SGI in conjunction with the Original Code are The
28 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
29 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
30 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
31 ** Window System(R) (Version 1.3), released October 19, 1998. This software
32 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
33 ** published by SGI, but has not been independently verified as being
34 ** compliant with the OpenGL(R) version 1.2.1 Specification.
40 * GLX protocol interface boot-strap code.
42 * Direct rendering support added by Precision Insight, Inc.
44 * \author Kevin E. Martin <kevin@precisioninsight.com>
47 #include "glxclient.h"
49 #include <X11/extensions/Xext.h>
50 #include <X11/extensions/extutil.h>
51 #include <X11/extensions/Xfixes.h>
52 #include <X11/extensions/Xdamage.h>
54 #include "indirect_init.h"
56 #include "glxextensions.h"
57 #include "glcontextmodes.h"
60 #ifdef GLX_DIRECT_RENDERING
69 #include <X11/Xlib-xcb.h>
76 void __glXDumpDrawBuffer(__GLXcontext
*ctx
);
81 * This is where our dispatch table's bounds are.
82 * And the static mesa_init is taken directly from
83 * Mesa's 'sparc.c' initializer.
85 * We need something like this here, because this version
86 * of openGL/glx never initializes a Mesa context, and so
87 * the address of the dispatch table pointer never gets stuffed
88 * into the dispatch jump table otherwise.
90 * It matters only on SPARC, and only if you are using assembler
91 * code instead of C-code indirect dispatch.
95 extern unsigned int _mesa_sparc_glapi_begin
;
96 extern unsigned int _mesa_sparc_glapi_end
;
97 extern void __glapi_sparc_icache_flush(unsigned int *);
98 static void _glx_mesa_init_sparc_glapi_relocs(void);
99 static int _mesa_sparc_needs_init
= 1;
100 #define INIT_MESA_SPARC { \
101 if(_mesa_sparc_needs_init) { \
102 _glx_mesa_init_sparc_glapi_relocs(); \
103 _mesa_sparc_needs_init = 0; \
107 #define INIT_MESA_SPARC
110 static Bool
MakeContextCurrent(Display
*dpy
, GLXDrawable draw
,
111 GLXDrawable read
, GLXContext gc
);
114 ** We setup some dummy structures here so that the API can be used
115 ** even if no context is current.
118 static GLubyte dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
];
121 ** Dummy context used by small commands when there is no current context.
123 ** gl and glx entry points are designed to operate as nop's when using
124 ** the dummy context structure.
126 static __GLXcontext dummyContext
= {
130 &dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
],
136 ** All indirect rendering contexts will share the same indirect dispatch table.
138 static __GLapi
*IndirectAPI
= NULL
;
142 * Current context management and locking
145 #if defined( USE_XTHREADS )
148 static GLboolean TSDinitialized
= GL_FALSE
;
149 static xthread_key_t ContextTSD
;
151 _X_HIDDEN __GLXcontext
*__glXGetCurrentContext(void)
153 if (!TSDinitialized
) {
154 xthread_key_create(&ContextTSD
, NULL
);
155 TSDinitialized
= GL_TRUE
;
156 return &dummyContext
;
160 xthread_get_specific(ContextTSD
, &p
);
162 return &dummyContext
;
164 return (__GLXcontext
*) p
;
168 _X_HIDDEN
void __glXSetCurrentContext(__GLXcontext
*c
)
170 if (!TSDinitialized
) {
171 xthread_key_create(&ContextTSD
, NULL
);
172 TSDinitialized
= GL_TRUE
;
174 xthread_set_specific(ContextTSD
, c
);
178 /* Used by the __glXLock() and __glXUnlock() macros */
179 _X_HIDDEN xmutex_rec __glXmutex
;
181 #elif defined( PTHREADS )
183 _X_HIDDEN pthread_mutex_t __glXmutex
= PTHREAD_MUTEX_INITIALIZER
;
185 # if defined( GLX_USE_TLS )
188 * Per-thread GLX context pointer.
190 * \c __glXSetCurrentContext is written is such a way that this pointer can
191 * \b never be \c NULL. This is important! Because of this
192 * \c __glXGetCurrentContext can be implemented as trivial macro.
194 __thread
void * __glX_tls_Context
__attribute__((tls_model("initial-exec")))
197 _X_HIDDEN
void __glXSetCurrentContext( __GLXcontext
* c
)
199 __glX_tls_Context
= (c
!= NULL
) ? c
: &dummyContext
;
204 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
207 * Per-thread data key.
209 * Once \c init_thread_data has been called, the per-thread data key will
210 * take a value of \c NULL. As each new thread is created the default
211 * value, in that thread, will be \c NULL.
213 static pthread_key_t ContextTSD
;
216 * Initialize the per-thread data key.
218 * This function is called \b exactly once per-process (not per-thread!) to
219 * initialize the per-thread data key. This is ideally done using the
220 * \c pthread_once mechanism.
222 static void init_thread_data( void )
224 if ( pthread_key_create( & ContextTSD
, NULL
) != 0 ) {
225 perror( "pthread_key_create" );
230 _X_HIDDEN
void __glXSetCurrentContext( __GLXcontext
* c
)
232 pthread_once( & once_control
, init_thread_data
);
233 pthread_setspecific( ContextTSD
, c
);
236 _X_HIDDEN __GLXcontext
* __glXGetCurrentContext( void )
240 pthread_once( & once_control
, init_thread_data
);
242 v
= pthread_getspecific( ContextTSD
);
243 return (v
== NULL
) ? & dummyContext
: (__GLXcontext
*) v
;
246 # endif /* defined( GLX_USE_TLS ) */
248 #elif defined( THREADS )
250 #error Unknown threading method specified.
254 /* not thread safe */
255 _X_HIDDEN __GLXcontext
*__glXcurrentContext
= &dummyContext
;
261 ** You can set this cell to 1 to force the gl drawing stuff to be
262 ** one command per packet
264 _X_HIDDEN
int __glXDebug
= 0;
266 /* Extension required boiler plate */
268 static char *__glXExtensionName
= GLX_EXTENSION_NAME
;
269 XExtensionInfo
*__glXExtensionInfo
= NULL
;
271 static /* const */ char *error_list
[] = {
273 "GLXBadContextState",
277 "GLXBadCurrentWindow",
278 "GLXBadRenderRequest",
279 "GLXBadLargeRequest",
280 "GLXUnsupportedPrivateRequest",
283 "GLXBadCurrentDrawable",
287 static int __glXCloseDisplay(Display
*dpy
, XExtCodes
*codes
)
291 gc
= __glXGetCurrentContext();
292 if (dpy
== gc
->currentDpy
) {
293 __glXSetCurrentContext(&dummyContext
);
294 #ifdef GLX_DIRECT_RENDERING
295 _glapi_set_dispatch(NULL
); /* no-op functions */
297 __glXFreeContext(gc
);
300 return XextRemoveDisplay(__glXExtensionInfo
, dpy
);
304 static XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
305 __GLX_NUMBER_ERRORS
, error_list
)
307 static /* const */ XExtensionHooks __glXExtensionHooks
= {
308 NULL
, /* create_gc */
312 NULL
, /* create_font */
313 NULL
, /* free_font */
314 __glXCloseDisplay
, /* close_display */
315 NULL
, /* wire_to_event */
316 NULL
, /* event_to_wire */
318 __glXErrorString
, /* error_string */
322 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay
, __glXExtensionInfo
,
323 __glXExtensionName
, &__glXExtensionHooks
,
324 __GLX_NUMBER_EVENTS
, NULL
)
326 /************************************************************************/
329 ** Free the per screen configs data as well as the array of
330 ** __glXScreenConfigs.
332 static void FreeScreenConfigs(__GLXdisplayPrivate
*priv
)
334 __GLXscreenConfigs
*psc
;
337 /* Free screen configuration information */
338 psc
= priv
->screenConfigs
;
339 screens
= ScreenCount(priv
->dpy
);
340 for (i
= 0; i
< screens
; i
++, psc
++) {
342 _gl_context_modes_destroy( psc
->configs
);
343 if(psc
->effectiveGLXexts
)
344 Xfree(psc
->effectiveGLXexts
);
346 psc
->configs
= NULL
; /* NOTE: just for paranoia */
348 Xfree((char*) psc
->serverGLXexts
);
350 #ifdef GLX_DIRECT_RENDERING
351 psc
->driScreen
->destroyScreen(psc
);
354 XFree((char*) priv
->screenConfigs
);
358 ** Release the private memory referred to in a display private
359 ** structure. The caller will free the extension structure.
361 static int __glXFreeDisplayPrivate(XExtData
*extension
)
363 __GLXdisplayPrivate
*priv
;
365 priv
= (__GLXdisplayPrivate
*) extension
->private_data
;
366 FreeScreenConfigs(priv
);
367 if(priv
->serverGLXvendor
) {
368 Xfree((char*)priv
->serverGLXvendor
);
369 priv
->serverGLXvendor
= 0x0; /* to protect against double free's */
371 if(priv
->serverGLXversion
) {
372 Xfree((char*)priv
->serverGLXversion
);
373 priv
->serverGLXversion
= 0x0; /* to protect against double free's */
376 #ifdef GLX_DIRECT_RENDERING
377 /* Free the direct rendering per display data */
378 if (priv
->driDisplay
)
379 (*priv
->driDisplay
->destroyDisplay
)(priv
->driDisplay
);
380 priv
->driDisplay
= NULL
;
387 /************************************************************************/
390 ** Query the version of the GLX extension. This procedure works even if
391 ** the client extension is not completely set up.
393 static Bool
QueryVersion(Display
*dpy
, int opcode
, int *major
, int *minor
)
395 xGLXQueryVersionReq
*req
;
396 xGLXQueryVersionReply reply
;
398 /* Send the glXQueryVersion request */
400 GetReq(GLXQueryVersion
,req
);
401 req
->reqType
= opcode
;
402 req
->glxCode
= X_GLXQueryVersion
;
403 req
->majorVersion
= GLX_MAJOR_VERSION
;
404 req
->minorVersion
= GLX_MINOR_VERSION
;
405 _XReply(dpy
, (xReply
*) &reply
, 0, False
);
409 if (reply
.majorVersion
!= GLX_MAJOR_VERSION
) {
411 ** The server does not support the same major release as this
416 *major
= reply
.majorVersion
;
417 *minor
= min(reply
.minorVersion
, GLX_MINOR_VERSION
);
423 __glXInitializeVisualConfigFromTags( __GLcontextModes
*config
, int count
,
424 const INT32
*bp
, Bool tagged_only
,
425 Bool fbconfig_style_tags
)
430 /* Copy in the first set of properties */
431 config
->visualID
= *bp
++;
433 config
->visualType
= _gl_convert_from_x_visual_type( *bp
++ );
435 config
->rgbMode
= *bp
++;
437 config
->redBits
= *bp
++;
438 config
->greenBits
= *bp
++;
439 config
->blueBits
= *bp
++;
440 config
->alphaBits
= *bp
++;
441 config
->accumRedBits
= *bp
++;
442 config
->accumGreenBits
= *bp
++;
443 config
->accumBlueBits
= *bp
++;
444 config
->accumAlphaBits
= *bp
++;
446 config
->doubleBufferMode
= *bp
++;
447 config
->stereoMode
= *bp
++;
449 config
->rgbBits
= *bp
++;
450 config
->depthBits
= *bp
++;
451 config
->stencilBits
= *bp
++;
452 config
->numAuxBuffers
= *bp
++;
453 config
->level
= *bp
++;
455 count
-= __GLX_MIN_CONFIG_PROPS
;
459 ** Additional properties may be in a list at the end
460 ** of the reply. They are in pairs of property type
461 ** and property value.
464 #define FETCH_OR_SET(tag) \
465 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
467 for (i
= 0; i
< count
; i
+= 2 ) {
470 FETCH_OR_SET( rgbMode
);
472 case GLX_BUFFER_SIZE
:
473 config
->rgbBits
= *bp
++;
476 config
->level
= *bp
++;
478 case GLX_DOUBLEBUFFER
:
479 FETCH_OR_SET( doubleBufferMode
);
482 FETCH_OR_SET( stereoMode
);
484 case GLX_AUX_BUFFERS
:
485 config
->numAuxBuffers
= *bp
++;
488 config
->redBits
= *bp
++;
491 config
->greenBits
= *bp
++;
494 config
->blueBits
= *bp
++;
497 config
->alphaBits
= *bp
++;
500 config
->depthBits
= *bp
++;
502 case GLX_STENCIL_SIZE
:
503 config
->stencilBits
= *bp
++;
505 case GLX_ACCUM_RED_SIZE
:
506 config
->accumRedBits
= *bp
++;
508 case GLX_ACCUM_GREEN_SIZE
:
509 config
->accumGreenBits
= *bp
++;
511 case GLX_ACCUM_BLUE_SIZE
:
512 config
->accumBlueBits
= *bp
++;
514 case GLX_ACCUM_ALPHA_SIZE
:
515 config
->accumAlphaBits
= *bp
++;
517 case GLX_VISUAL_CAVEAT_EXT
:
518 config
->visualRating
= *bp
++;
520 case GLX_X_VISUAL_TYPE
:
521 config
->visualType
= *bp
++;
523 case GLX_TRANSPARENT_TYPE
:
524 config
->transparentPixel
= *bp
++;
526 case GLX_TRANSPARENT_INDEX_VALUE
:
527 config
->transparentIndex
= *bp
++;
529 case GLX_TRANSPARENT_RED_VALUE
:
530 config
->transparentRed
= *bp
++;
532 case GLX_TRANSPARENT_GREEN_VALUE
:
533 config
->transparentGreen
= *bp
++;
535 case GLX_TRANSPARENT_BLUE_VALUE
:
536 config
->transparentBlue
= *bp
++;
538 case GLX_TRANSPARENT_ALPHA_VALUE
:
539 config
->transparentAlpha
= *bp
++;
542 config
->visualID
= *bp
++;
544 case GLX_DRAWABLE_TYPE
:
545 config
->drawableType
= *bp
++;
547 case GLX_RENDER_TYPE
:
548 config
->renderType
= *bp
++;
550 case GLX_X_RENDERABLE
:
551 config
->xRenderable
= *bp
++;
553 case GLX_FBCONFIG_ID
:
554 config
->fbconfigID
= *bp
++;
556 case GLX_MAX_PBUFFER_WIDTH
:
557 config
->maxPbufferWidth
= *bp
++;
559 case GLX_MAX_PBUFFER_HEIGHT
:
560 config
->maxPbufferHeight
= *bp
++;
562 case GLX_MAX_PBUFFER_PIXELS
:
563 config
->maxPbufferPixels
= *bp
++;
565 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
566 config
->optimalPbufferWidth
= *bp
++;
568 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
569 config
->optimalPbufferHeight
= *bp
++;
571 case GLX_VISUAL_SELECT_GROUP_SGIX
:
572 config
->visualSelectGroup
= *bp
++;
574 case GLX_SWAP_METHOD_OML
:
575 config
->swapMethod
= *bp
++;
577 case GLX_SAMPLE_BUFFERS_SGIS
:
578 config
->sampleBuffers
= *bp
++;
580 case GLX_SAMPLES_SGIS
:
581 config
->samples
= *bp
++;
583 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
584 config
->bindToTextureRgb
= *bp
++;
586 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
587 config
->bindToTextureRgba
= *bp
++;
589 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
590 config
->bindToMipmapTexture
= *bp
++;
592 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
593 config
->bindToTextureTargets
= *bp
++;
595 case GLX_Y_INVERTED_EXT
:
596 config
->yInverted
= *bp
++;
606 config
->renderType
= (config
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
608 config
->haveAccumBuffer
= ((config
->accumRedBits
+
609 config
->accumGreenBits
+
610 config
->accumBlueBits
+
611 config
->accumAlphaBits
) > 0);
612 config
->haveDepthBuffer
= (config
->depthBits
> 0);
613 config
->haveStencilBuffer
= (config
->stencilBits
> 0);
616 static __GLcontextModes
*
617 createConfigsFromProperties(Display
*dpy
, int nvisuals
, int nprops
,
618 int screen
, GLboolean tagged_only
)
620 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
622 __GLcontextModes
*modes
, *m
;
628 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
630 /* Check number of properties */
631 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
634 /* Allocate memory for our config structure */
635 modes
= _gl_context_modes_create(nvisuals
, sizeof(__GLcontextModes
));
639 prop_size
= nprops
* __GLX_SIZE_INT32
;
640 if (prop_size
<= sizeof(buf
))
643 props
= Xmalloc(prop_size
);
645 /* Read each config structure and convert it into our format */
647 for (i
= 0; i
< nvisuals
; i
++) {
648 _XRead(dpy
, (char *)props
, prop_size
);
649 /* Older X servers don't send this so we default it here. */
650 m
->drawableType
= GLX_WINDOW_BIT
;
651 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
652 tagged_only
, GL_TRUE
);
664 getVisualConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
, int screen
)
666 xGLXGetVisualConfigsReq
*req
;
667 __GLXscreenConfigs
*psc
;
668 xGLXGetVisualConfigsReply reply
;
672 psc
= priv
->screenConfigs
+ screen
;
674 GetReq(GLXGetVisualConfigs
, req
);
675 req
->reqType
= priv
->majorOpcode
;
676 req
->glxCode
= X_GLXGetVisualConfigs
;
677 req
->screen
= screen
;
679 if (!_XReply(dpy
, (xReply
*) &reply
, 0, False
))
682 psc
->visuals
= createConfigsFromProperties(dpy
,
689 return psc
->visuals
!= NULL
;
693 getFBConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
, int screen
)
695 xGLXGetFBConfigsReq
*fb_req
;
696 xGLXGetFBConfigsSGIXReq
*sgi_req
;
697 xGLXVendorPrivateWithReplyReq
*vpreq
;
698 xGLXGetFBConfigsReply reply
;
699 __GLXscreenConfigs
*psc
;
701 psc
= priv
->screenConfigs
+ screen
;
702 psc
->serverGLXexts
= __glXGetStringFromServer(dpy
, priv
->majorOpcode
,
703 X_GLXQueryServerString
,
704 screen
, GLX_EXTENSIONS
);
709 if (atof(priv
->serverGLXversion
) >= 1.3) {
710 GetReq(GLXGetFBConfigs
, fb_req
);
711 fb_req
->reqType
= priv
->majorOpcode
;
712 fb_req
->glxCode
= X_GLXGetFBConfigs
;
713 fb_req
->screen
= screen
;
714 } else if (strstr(psc
->serverGLXexts
, "GLX_SGIX_fbconfig") != NULL
) {
715 GetReqExtra(GLXVendorPrivateWithReply
,
716 sz_xGLXGetFBConfigsSGIXReq
+
717 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
718 sgi_req
= (xGLXGetFBConfigsSGIXReq
*) vpreq
;
719 sgi_req
->reqType
= priv
->majorOpcode
;
720 sgi_req
->glxCode
= X_GLXVendorPrivateWithReply
;
721 sgi_req
->vendorCode
= X_GLXvop_GetFBConfigsSGIX
;
722 sgi_req
->screen
= screen
;
726 if (!_XReply(dpy
, (xReply
*) &reply
, 0, False
))
729 psc
->configs
= createConfigsFromProperties(dpy
,
731 reply
.numAttribs
* 2,
736 return psc
->configs
!= NULL
;
740 ** Allocate the memory for the per screen configs for each screen.
741 ** If that works then fetch the per screen configs data.
743 static Bool
AllocAndFetchScreenConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
)
745 __GLXscreenConfigs
*psc
;
749 ** First allocate memory for the array of per screen configs.
751 screens
= ScreenCount(dpy
);
752 psc
= (__GLXscreenConfigs
*) Xmalloc(screens
* sizeof(__GLXscreenConfigs
));
756 memset(psc
, 0, screens
* sizeof(__GLXscreenConfigs
));
757 priv
->screenConfigs
= psc
;
759 priv
->serverGLXversion
= __glXGetStringFromServer(dpy
, priv
->majorOpcode
,
760 X_GLXQueryServerString
,
762 if ( priv
->serverGLXversion
== NULL
) {
763 FreeScreenConfigs(priv
);
767 for (i
= 0; i
< screens
; i
++, psc
++) {
768 getVisualConfigs(dpy
, priv
, i
);
769 getFBConfigs(dpy
, priv
, i
);
773 #ifdef GLX_DIRECT_RENDERING
774 if (priv
->driDisplay
)
775 psc
->driScreen
= (*priv
->driDisplay
->createScreen
)(psc
, i
, priv
);
783 ** Initialize the client side extension code.
785 _X_HIDDEN __GLXdisplayPrivate
*__glXInitialize(Display
* dpy
)
787 XExtDisplayInfo
*info
= __glXFindDisplay(dpy
);
788 XExtData
**privList
, *private, *found
;
789 __GLXdisplayPrivate
*dpyPriv
;
790 XEDataObject dataObj
;
793 #if defined(USE_XTHREADS)
795 static int firstCall
= 1;
797 /* initialize the GLX mutexes */
798 xmutex_init(&__glXmutex
);
805 /* The one and only long long lock */
808 if (!XextHasExtension(info
)) {
809 /* No GLX extension supported by this server. Oh well. */
811 XMissingExtension(dpy
, __glXExtensionName
);
815 /* See if a display private already exists. If so, return it */
816 dataObj
.display
= dpy
;
817 privList
= XEHeadOfExtensionList(dataObj
);
818 found
= XFindOnExtensionList(privList
, info
->codes
->extension
);
821 return (__GLXdisplayPrivate
*) found
->private_data
;
824 /* See if the versions are compatible */
825 if (!QueryVersion(dpy
, info
->codes
->major_opcode
, &major
, &minor
)) {
826 /* The client and server do not agree on versions. Punt. */
832 ** Allocate memory for all the pieces needed for this buffer.
834 private = (XExtData
*) Xmalloc(sizeof(XExtData
));
839 dpyPriv
= (__GLXdisplayPrivate
*) Xcalloc(1, sizeof(__GLXdisplayPrivate
));
842 Xfree((char*) private);
847 ** Init the display private and then read in the screen config
848 ** structures from the server.
850 dpyPriv
->majorOpcode
= info
->codes
->major_opcode
;
851 dpyPriv
->majorVersion
= major
;
852 dpyPriv
->minorVersion
= minor
;
855 dpyPriv
->serverGLXvendor
= 0x0;
856 dpyPriv
->serverGLXversion
= 0x0;
858 #ifdef GLX_DIRECT_RENDERING
860 ** Initialize the direct rendering per display data and functions.
861 ** Note: This _must_ be done before calling any other DRI routines
862 ** (e.g., those called in AllocAndFetchScreenConfigs).
864 if (getenv("LIBGL_ALWAYS_INDIRECT") == NULL
) {
865 dpyPriv
->driDisplay
= driCreateDisplay(dpy
);
869 if (!AllocAndFetchScreenConfigs(dpy
, dpyPriv
)) {
871 Xfree((char*) dpyPriv
);
872 Xfree((char*) private);
877 ** Fill in the private structure. This is the actual structure that
878 ** hangs off of the Display structure. Our private structure is
879 ** referred to by this structure. Got that?
881 private->number
= info
->codes
->extension
;
883 private->free_private
= __glXFreeDisplayPrivate
;
884 private->private_data
= (char *) dpyPriv
;
885 XAddToExtensionList(privList
, private);
887 if (dpyPriv
->majorVersion
== 1 && dpyPriv
->minorVersion
>= 1) {
888 __glXClientInfo(dpy
, dpyPriv
->majorOpcode
);
896 ** Setup for sending a GLX command on dpy. Make sure the extension is
897 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
899 _X_HIDDEN CARD8
__glXSetupForCommand(Display
*dpy
)
902 __GLXdisplayPrivate
*priv
;
904 /* If this thread has a current context, flush its rendering commands */
905 gc
= __glXGetCurrentContext();
906 if (gc
->currentDpy
) {
907 /* Flush rendering buffer of the current context, if any */
908 (void) __glXFlushRenderBuffer(gc
, gc
->pc
);
910 if (gc
->currentDpy
== dpy
) {
911 /* Use opcode from gc because its right */
913 return gc
->majorOpcode
;
916 ** Have to get info about argument dpy because it might be to
917 ** a different server
922 /* Forced to lookup extension via the slow initialize route */
923 priv
= __glXInitialize(dpy
);
927 return priv
->majorOpcode
;
931 * Flush the drawing command transport buffer.
933 * \param ctx Context whose transport buffer is to be flushed.
934 * \param pc Pointer to first unused buffer location.
937 * Modify this function to use \c ctx->pc instead of the explicit
940 _X_HIDDEN GLubyte
*__glXFlushRenderBuffer(__GLXcontext
*ctx
, GLubyte
*pc
)
942 Display
* const dpy
= ctx
->currentDpy
;
944 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
948 const GLint size
= pc
- ctx
->buf
;
950 if ( (dpy
!= NULL
) && (size
> 0) ) {
952 xcb_glx_render(c
, ctx
->currentContextTag
, size
,
953 (const uint8_t *)ctx
->buf
);
955 /* Send the entire buffer as an X request */
957 GetReq(GLXRender
,req
);
958 req
->reqType
= ctx
->majorOpcode
;
959 req
->glxCode
= X_GLXRender
;
960 req
->contextTag
= ctx
->currentContextTag
;
961 req
->length
+= (size
+ 3) >> 2;
962 _XSend(dpy
, (char *)ctx
->buf
, size
);
968 /* Reset pointer and return it */
975 * Send a portion of a GLXRenderLarge command to the server. The advantage of
976 * this function over \c __glXSendLargeCommand is that callers can use the
977 * data buffer in the GLX context and may be able to avoid allocating an
978 * extra buffer. The disadvantage is the clients will have to do more
979 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
981 * \sa __glXSendLargeCommand
983 * \param gc GLX context
984 * \param requestNumber Which part of the whole command is this? The first
986 * \param totalRequests How many requests will there be?
987 * \param data Command data.
988 * \param dataLen Size, in bytes, of the command data.
990 _X_HIDDEN
void __glXSendLargeChunk(__GLXcontext
*gc
, GLint requestNumber
,
992 const GLvoid
* data
, GLint dataLen
)
994 Display
*dpy
= gc
->currentDpy
;
996 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
997 xcb_glx_render_large(c
, gc
->currentContextTag
, requestNumber
, totalRequests
, dataLen
, data
);
999 xGLXRenderLargeReq
*req
;
1001 if ( requestNumber
== 1 ) {
1005 GetReq(GLXRenderLarge
,req
);
1006 req
->reqType
= gc
->majorOpcode
;
1007 req
->glxCode
= X_GLXRenderLarge
;
1008 req
->contextTag
= gc
->currentContextTag
;
1009 req
->length
+= (dataLen
+ 3) >> 2;
1010 req
->requestNumber
= requestNumber
;
1011 req
->requestTotal
= totalRequests
;
1012 req
->dataBytes
= dataLen
;
1013 Data(dpy
, data
, dataLen
);
1015 if ( requestNumber
== totalRequests
) {
1019 #endif /* USE_XCB */
1024 * Send a command that is too large for the GLXRender protocol request.
1026 * Send a large command, one that is too large for some reason to
1027 * send using the GLXRender protocol request. One reason to send
1028 * a large command is to avoid copying the data.
1030 * \param ctx GLX context
1031 * \param header Header data.
1032 * \param headerLen Size, in bytes, of the header data. It is assumed that
1033 * the header data will always be small enough to fit in
1034 * a single X protocol packet.
1035 * \param data Command data.
1036 * \param dataLen Size, in bytes, of the command data.
1038 _X_HIDDEN
void __glXSendLargeCommand(__GLXcontext
*ctx
,
1039 const GLvoid
*header
, GLint headerLen
,
1040 const GLvoid
*data
, GLint dataLen
)
1043 GLint totalRequests
, requestNumber
;
1046 ** Calculate the maximum amount of data can be stuffed into a single
1047 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1048 ** packet size minus sz_xGLXRenderReq.
1050 maxSize
= (ctx
->bufSize
+ sz_xGLXRenderReq
) - sz_xGLXRenderLargeReq
;
1051 totalRequests
= 1 + (dataLen
/ maxSize
);
1052 if (dataLen
% maxSize
) totalRequests
++;
1055 ** Send all of the command, except the large array, as one request.
1057 assert( headerLen
<= maxSize
);
1058 __glXSendLargeChunk(ctx
, 1, totalRequests
, header
, headerLen
);
1061 ** Send enough requests until the whole array is sent.
1063 for ( requestNumber
= 2 ; requestNumber
<= (totalRequests
- 1) ; requestNumber
++ ) {
1064 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, maxSize
);
1065 data
= (const GLvoid
*) (((const GLubyte
*) data
) + maxSize
);
1067 assert( dataLen
> 0 );
1070 assert( dataLen
<= maxSize
);
1071 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, dataLen
);
1074 /************************************************************************/
1076 PUBLIC GLXContext
glXGetCurrentContext(void)
1078 GLXContext cx
= __glXGetCurrentContext();
1080 if (cx
== &dummyContext
) {
1087 PUBLIC GLXDrawable
glXGetCurrentDrawable(void)
1089 GLXContext gc
= __glXGetCurrentContext();
1090 return gc
->currentDrawable
;
1094 /************************************************************************/
1096 static Bool
SendMakeCurrentRequest( Display
*dpy
, CARD8 opcode
,
1097 GLXContextID gc
, GLXContextTag old_gc
, GLXDrawable draw
, GLXDrawable read
,
1098 xGLXMakeCurrentReply
* reply
);
1101 * Sends a GLX protocol message to the specified display to make the context
1102 * and the drawables current.
1104 * \param dpy Display to send the message to.
1105 * \param opcode Major opcode value for the display.
1106 * \param gc_id Context tag for the context to be made current.
1107 * \param draw Drawable ID for the "draw" drawable.
1108 * \param read Drawable ID for the "read" drawable.
1109 * \param reply Space to store the X-server's reply.
1112 * This function assumes that \c dpy is locked with \c LockDisplay on entry.
1114 static Bool
SendMakeCurrentRequest(Display
*dpy
, CARD8 opcode
,
1115 GLXContextID gc_id
, GLXContextTag gc_tag
,
1116 GLXDrawable draw
, GLXDrawable read
,
1117 xGLXMakeCurrentReply
*reply
)
1125 xGLXMakeCurrentReq
*req
;
1127 GetReq(GLXMakeCurrent
,req
);
1128 req
->reqType
= opcode
;
1129 req
->glxCode
= X_GLXMakeCurrent
;
1130 req
->drawable
= draw
;
1131 req
->context
= gc_id
;
1132 req
->oldContextTag
= gc_tag
;
1135 __GLXdisplayPrivate
*priv
= __glXInitialize(dpy
);
1137 /* If the server can support the GLX 1.3 version, we should
1138 * perfer that. Not only that, some servers support GLX 1.3 but
1139 * not the SGI extension.
1142 if ((priv
->majorVersion
> 1) || (priv
->minorVersion
>= 3)) {
1143 xGLXMakeContextCurrentReq
*req
;
1145 GetReq(GLXMakeContextCurrent
,req
);
1146 req
->reqType
= opcode
;
1147 req
->glxCode
= X_GLXMakeContextCurrent
;
1148 req
->drawable
= draw
;
1149 req
->readdrawable
= read
;
1150 req
->context
= gc_id
;
1151 req
->oldContextTag
= gc_tag
;
1154 xGLXVendorPrivateWithReplyReq
*vpreq
;
1155 xGLXMakeCurrentReadSGIReq
*req
;
1157 GetReqExtra(GLXVendorPrivateWithReply
,
1158 sz_xGLXMakeCurrentReadSGIReq
-sz_xGLXVendorPrivateWithReplyReq
,vpreq
);
1159 req
= (xGLXMakeCurrentReadSGIReq
*)vpreq
;
1160 req
->reqType
= opcode
;
1161 req
->glxCode
= X_GLXVendorPrivateWithReply
;
1162 req
->vendorCode
= X_GLXvop_MakeCurrentReadSGI
;
1163 req
->drawable
= draw
;
1164 req
->readable
= read
;
1165 req
->context
= gc_id
;
1166 req
->oldContextTag
= gc_tag
;
1170 ret
= _XReply(dpy
, (xReply
*) reply
, 0, False
);
1179 #ifdef GLX_DIRECT_RENDERING
1180 static __GLXDRIdrawable
*
1181 FetchDRIDrawable(Display
*dpy
, GLXDrawable drawable
, GLXContext gc
)
1183 __GLXdisplayPrivate
* const priv
= __glXInitialize(dpy
);
1184 __GLXDRIdrawable
*pdraw
;
1185 __GLXscreenConfigs
*psc
;
1187 if (priv
== NULL
|| priv
->driDisplay
== NULL
)
1190 psc
= &priv
->screenConfigs
[gc
->screen
];
1191 if (__glxHashLookup(psc
->drawHash
, drawable
, (void *) &pdraw
) == 0)
1194 return psc
->driScreen
->createDrawable(psc
, drawable
, gc
);
1196 #endif /* GLX_DIRECT_RENDERING */
1200 * Make a particular context current.
1202 * \note This is in this file so that it can access dummyContext.
1204 static Bool
MakeContextCurrent(Display
*dpy
, GLXDrawable draw
,
1205 GLXDrawable read
, GLXContext gc
)
1207 xGLXMakeCurrentReply reply
;
1208 const GLXContext oldGC
= __glXGetCurrentContext();
1209 const CARD8 opcode
= __glXSetupForCommand(dpy
);
1210 const CARD8 oldOpcode
= ((gc
== oldGC
) || (oldGC
== &dummyContext
))
1211 ? opcode
: __glXSetupForCommand(oldGC
->currentDpy
);
1212 Bool bindReturnValue
;
1215 if (!opcode
|| !oldOpcode
) {
1219 /* Make sure that the new context has a nonzero ID. In the request,
1220 * a zero context ID is used only to mean that we bind to no current
1223 if ((gc
!= NULL
) && (gc
->xid
== None
)) {
1227 _glapi_check_multithread();
1229 #ifdef GLX_DIRECT_RENDERING
1230 /* Bind the direct rendering context to the drawable */
1231 if (gc
&& gc
->driContext
) {
1232 __GLXDRIdrawable
*pdraw
= FetchDRIDrawable(dpy
, draw
, gc
);
1233 __GLXDRIdrawable
*pread
= FetchDRIDrawable(dpy
, read
, gc
);
1236 (gc
->driContext
->bindContext
) (gc
->driContext
, pdraw
, pread
);
1240 /* Send a glXMakeCurrent request to bind the new context. */
1242 SendMakeCurrentRequest(dpy
, opcode
, gc
? gc
->xid
: None
,
1243 ((dpy
!= oldGC
->currentDpy
) || oldGC
->driContext
)
1244 ? None
: oldGC
->currentContextTag
,
1245 draw
, read
, &reply
);
1249 if (!bindReturnValue
) {
1253 if ((dpy
!= oldGC
->currentDpy
|| (gc
&& gc
->driContext
)) &&
1254 !oldGC
->driContext
&& oldGC
!= &dummyContext
) {
1255 xGLXMakeCurrentReply dummy_reply
;
1257 /* We are either switching from one dpy to another and have to
1258 * send a request to the previous dpy to unbind the previous
1259 * context, or we are switching away from a indirect context to
1260 * a direct context and have to send a request to the dpy to
1261 * unbind the previous context.
1263 (void) SendMakeCurrentRequest(oldGC
->currentDpy
, oldOpcode
, None
,
1264 oldGC
->currentContextTag
, None
, None
,
1267 #ifdef GLX_DIRECT_RENDERING
1268 else if (oldGC
->driContext
) {
1269 oldGC
->driContext
->unbindContext(oldGC
->driContext
);
1274 /* Update our notion of what is current */
1277 /* Even though the contexts are the same the drawable might have
1278 * changed. Note that gc cannot be the dummy, and that oldGC
1279 * cannot be NULL, therefore if they are the same, gc is not
1280 * NULL and not the dummy.
1282 gc
->currentDrawable
= draw
;
1283 gc
->currentReadable
= read
;
1285 if (oldGC
!= &dummyContext
) {
1286 /* Old current context is no longer current to anybody */
1287 oldGC
->currentDpy
= 0;
1288 oldGC
->currentDrawable
= None
;
1289 oldGC
->currentReadable
= None
;
1290 oldGC
->currentContextTag
= 0;
1292 if (oldGC
->xid
== None
) {
1293 /* We are switching away from a context that was
1294 * previously destroyed, so we need to free the memory
1295 * for the old handle.
1297 #ifdef GLX_DIRECT_RENDERING
1298 /* Destroy the old direct rendering context */
1299 if (oldGC
->driContext
) {
1300 oldGC
->driContext
->destroyContext(oldGC
->driContext
,
1303 oldGC
->driContext
= NULL
;
1306 __glXFreeContext(oldGC
);
1310 __glXSetCurrentContext(gc
);
1312 gc
->currentDpy
= dpy
;
1313 gc
->currentDrawable
= draw
;
1314 gc
->currentReadable
= read
;
1316 if (!gc
->driContext
) {
1318 IndirectAPI
= __glXNewIndirectAPI();
1319 _glapi_set_dispatch(IndirectAPI
);
1321 #ifdef GLX_USE_APPLEGL
1323 extern void XAppleDRIUseIndirectDispatch(void);
1324 XAppleDRIUseIndirectDispatch();
1328 __GLXattribute
*state
=
1329 (__GLXattribute
*)(gc
->client_state_private
);
1331 gc
->currentContextTag
= reply
.contextTag
;
1332 if (state
->array_state
== NULL
) {
1333 (void) glGetString(GL_EXTENSIONS
);
1334 (void) glGetString(GL_VERSION
);
1335 __glXInitVertexArrayState(gc
);
1339 gc
->currentContextTag
= -1;
1342 __glXSetCurrentContext(&dummyContext
);
1343 #ifdef GLX_DIRECT_RENDERING
1344 _glapi_set_dispatch(NULL
); /* no-op functions */
1353 PUBLIC Bool
glXMakeCurrent(Display
*dpy
, GLXDrawable draw
, GLXContext gc
)
1355 return MakeContextCurrent( dpy
, draw
, draw
, gc
);
1358 PUBLIC
GLX_ALIAS(Bool
, glXMakeCurrentReadSGI
,
1359 (Display
*dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
1360 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
1362 PUBLIC
GLX_ALIAS(Bool
, glXMakeContextCurrent
,
1363 (Display
*dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
1364 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
1368 _X_HIDDEN
void __glXDumpDrawBuffer(__GLXcontext
*ctx
)
1370 GLubyte
*p
= ctx
->buf
;
1371 GLubyte
*end
= ctx
->pc
;
1372 GLushort opcode
, length
;
1376 opcode
= *((GLushort
*) p
);
1377 length
= *((GLushort
*) (p
+ 2));
1378 printf("%2x: %5d: ", opcode
, length
);
1381 while (length
> 0) {
1382 printf("%08x ", *((unsigned *) p
));
1391 #ifdef USE_SPARC_ASM
1393 * Used only when we are sparc, using sparc assembler.
1398 _glx_mesa_init_sparc_glapi_relocs(void)
1400 unsigned int *insn_ptr
, *end_ptr
;
1401 unsigned long disp_addr
;
1403 insn_ptr
= &_mesa_sparc_glapi_begin
;
1404 end_ptr
= &_mesa_sparc_glapi_end
;
1405 disp_addr
= (unsigned long) &_glapi_Dispatch
;
1408 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1409 * seem to be a better way to do this:
1411 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1413 * This code is patching in the ADDRESS of the pointer to the
1414 * dispatch table. Hence, it must be called exactly once, because
1415 * that address is not going to change.
1417 * What it points to can change, but Mesa (and hence, we) assume
1418 * that there is only one pointer.
1421 while (insn_ptr
< end_ptr
) {
1422 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1424 This code patches for 64-bit addresses. This had better
1425 not happen for Sparc/Linux, no matter what architecture we
1426 are building for. So, don't do this.
1428 The 'defined(__linux_64__)' is used here as a placeholder for
1429 when we do do 64-bit usermode on sparc linux.
1431 insn_ptr
[0] |= (disp_addr
>> (32 + 10));
1432 insn_ptr
[1] |= ((disp_addr
& 0xffffffff) >> 10);
1433 __glapi_sparc_icache_flush(&insn_ptr
[0]);
1434 insn_ptr
[2] |= ((disp_addr
>> 32) & ((1 << 10) - 1));
1435 insn_ptr
[3] |= (disp_addr
& ((1 << 10) - 1));
1436 __glapi_sparc_icache_flush(&insn_ptr
[2]);
1439 insn_ptr
[0] |= (disp_addr
>> 10);
1440 insn_ptr
[1] |= (disp_addr
& ((1 << 10) - 1));
1441 __glapi_sparc_icache_flush(&insn_ptr
[0]);
1446 #endif /* sparc ASM in use */