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>
77 void __glXDumpDrawBuffer(__GLXcontext
*ctx
);
82 * This is where our dispatch table's bounds are.
83 * And the static mesa_init is taken directly from
84 * Mesa's 'sparc.c' initializer.
86 * We need something like this here, because this version
87 * of openGL/glx never initializes a Mesa context, and so
88 * the address of the dispatch table pointer never gets stuffed
89 * into the dispatch jump table otherwise.
91 * It matters only on SPARC, and only if you are using assembler
92 * code instead of C-code indirect dispatch.
96 extern unsigned int _mesa_sparc_glapi_begin
;
97 extern unsigned int _mesa_sparc_glapi_end
;
98 extern void __glapi_sparc_icache_flush(unsigned int *);
99 static void _glx_mesa_init_sparc_glapi_relocs(void);
100 static int _mesa_sparc_needs_init
= 1;
101 #define INIT_MESA_SPARC { \
102 if(_mesa_sparc_needs_init) { \
103 _glx_mesa_init_sparc_glapi_relocs(); \
104 _mesa_sparc_needs_init = 0; \
108 #define INIT_MESA_SPARC
111 static Bool
MakeContextCurrent(Display
*dpy
, GLXDrawable draw
,
112 GLXDrawable read
, GLXContext gc
);
115 ** We setup some dummy structures here so that the API can be used
116 ** even if no context is current.
119 static GLubyte dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
];
122 ** Dummy context used by small commands when there is no current context.
124 ** gl and glx entry points are designed to operate as nop's when using
125 ** the dummy context structure.
127 static __GLXcontext dummyContext
= {
131 &dummyBuffer
[__GLX_BUFFER_LIMIT_SIZE
],
137 ** All indirect rendering contexts will share the same indirect dispatch table.
139 static __GLapi
*IndirectAPI
= NULL
;
143 * Current context management and locking
146 #if defined( USE_XTHREADS )
149 static GLboolean TSDinitialized
= GL_FALSE
;
150 static xthread_key_t ContextTSD
;
152 __GLXcontext
*__glXGetCurrentContext(void)
154 if (!TSDinitialized
) {
155 xthread_key_create(&ContextTSD
, NULL
);
156 TSDinitialized
= GL_TRUE
;
157 return &dummyContext
;
161 xthread_get_specific(ContextTSD
, &p
);
163 return &dummyContext
;
165 return (__GLXcontext
*) p
;
169 void __glXSetCurrentContext(__GLXcontext
*c
)
171 if (!TSDinitialized
) {
172 xthread_key_create(&ContextTSD
, NULL
);
173 TSDinitialized
= GL_TRUE
;
175 xthread_set_specific(ContextTSD
, c
);
179 /* Used by the __glXLock() and __glXUnlock() macros */
180 xmutex_rec __glXmutex
;
182 #elif defined( PTHREADS )
184 pthread_mutex_t __glXmutex
= PTHREAD_MUTEX_INITIALIZER
;
186 # if defined( GLX_USE_TLS )
189 * Per-thread GLX context pointer.
191 * \c __glXSetCurrentContext is written is such a way that this pointer can
192 * \b never be \c NULL. This is important! Because of this
193 * \c __glXGetCurrentContext can be implemented as trivial macro.
195 __thread
void * __glX_tls_Context
__attribute__((tls_model("initial-exec")))
198 void __glXSetCurrentContext( __GLXcontext
* c
)
200 __glX_tls_Context
= (c
!= NULL
) ? c
: &dummyContext
;
205 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
208 * Per-thread data key.
210 * Once \c init_thread_data has been called, the per-thread data key will
211 * take a value of \c NULL. As each new thread is created the default
212 * value, in that thread, will be \c NULL.
214 static pthread_key_t ContextTSD
;
217 * Initialize the per-thread data key.
219 * This function is called \b exactly once per-process (not per-thread!) to
220 * initialize the per-thread data key. This is ideally done using the
221 * \c pthread_once mechanism.
223 static void init_thread_data( void )
225 if ( pthread_key_create( & ContextTSD
, NULL
) != 0 ) {
226 perror( "pthread_key_create" );
231 void __glXSetCurrentContext( __GLXcontext
* c
)
233 pthread_once( & once_control
, init_thread_data
);
234 pthread_setspecific( ContextTSD
, c
);
237 __GLXcontext
* __glXGetCurrentContext( void )
241 pthread_once( & once_control
, init_thread_data
);
243 v
= pthread_getspecific( ContextTSD
);
244 return (v
== NULL
) ? & dummyContext
: (__GLXcontext
*) v
;
247 # endif /* defined( GLX_USE_TLS ) */
249 #elif defined( THREADS )
251 #error Unknown threading method specified.
255 /* not thread safe */
256 __GLXcontext
*__glXcurrentContext
= &dummyContext
;
262 ** You can set this cell to 1 to force the gl drawing stuff to be
263 ** one command per packet
267 /* Extension required boiler plate */
269 static char *__glXExtensionName
= GLX_EXTENSION_NAME
;
270 XExtensionInfo
*__glXExtensionInfo
= NULL
;
272 static /* const */ char *error_list
[] = {
274 "GLXBadContextState",
278 "GLXBadCurrentWindow",
279 "GLXBadRenderRequest",
280 "GLXBadLargeRequest",
281 "GLXUnsupportedPrivateRequest",
284 "GLXBadCurrentDrawable",
288 static int __glXCloseDisplay(Display
*dpy
, XExtCodes
*codes
)
292 gc
= __glXGetCurrentContext();
293 if (dpy
== gc
->currentDpy
) {
294 __glXSetCurrentContext(&dummyContext
);
295 #ifdef GLX_DIRECT_RENDERING
296 _glapi_set_dispatch(NULL
); /* no-op functions */
298 __glXFreeContext(gc
);
301 return XextRemoveDisplay(__glXExtensionInfo
, dpy
);
305 static XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
306 __GLX_NUMBER_ERRORS
, error_list
)
308 static /* const */ XExtensionHooks __glXExtensionHooks
= {
309 NULL
, /* create_gc */
313 NULL
, /* create_font */
314 NULL
, /* free_font */
315 __glXCloseDisplay
, /* close_display */
316 NULL
, /* wire_to_event */
317 NULL
, /* event_to_wire */
319 __glXErrorString
, /* error_string */
323 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay
, __glXExtensionInfo
,
324 __glXExtensionName
, &__glXExtensionHooks
,
325 __GLX_NUMBER_EVENTS
, NULL
)
327 /************************************************************************/
330 ** Free the per screen configs data as well as the array of
331 ** __glXScreenConfigs.
333 static void FreeScreenConfigs(__GLXdisplayPrivate
*priv
)
335 __GLXscreenConfigs
*psc
;
338 /* Free screen configuration information */
339 psc
= priv
->screenConfigs
;
340 screens
= ScreenCount(priv
->dpy
);
341 for (i
= 0; i
< screens
; i
++, psc
++) {
343 _gl_context_modes_destroy( psc
->configs
);
344 if(psc
->effectiveGLXexts
)
345 Xfree(psc
->effectiveGLXexts
);
347 psc
->configs
= NULL
; /* NOTE: just for paranoia */
349 Xfree((char*) psc
->serverGLXexts
);
351 #ifdef GLX_DIRECT_RENDERING
352 psc
->driScreen
->destroyScreen(psc
);
355 XFree((char*) priv
->screenConfigs
);
359 ** Release the private memory referred to in a display private
360 ** structure. The caller will free the extension structure.
362 static int __glXFreeDisplayPrivate(XExtData
*extension
)
364 __GLXdisplayPrivate
*priv
;
366 priv
= (__GLXdisplayPrivate
*) extension
->private_data
;
367 FreeScreenConfigs(priv
);
368 if(priv
->serverGLXvendor
) {
369 Xfree((char*)priv
->serverGLXvendor
);
370 priv
->serverGLXvendor
= 0x0; /* to protect against double free's */
372 if(priv
->serverGLXversion
) {
373 Xfree((char*)priv
->serverGLXversion
);
374 priv
->serverGLXversion
= 0x0; /* to protect against double free's */
377 #ifdef GLX_DIRECT_RENDERING
378 /* Free the direct rendering per display data */
379 if (priv
->driDisplay
)
380 (*priv
->driDisplay
->destroyDisplay
)(priv
->driDisplay
);
381 priv
->driDisplay
= NULL
;
388 /************************************************************************/
391 ** Query the version of the GLX extension. This procedure works even if
392 ** the client extension is not completely set up.
394 static Bool
QueryVersion(Display
*dpy
, int opcode
, int *major
, int *minor
)
396 xGLXQueryVersionReq
*req
;
397 xGLXQueryVersionReply reply
;
399 /* Send the glXQueryVersion request */
401 GetReq(GLXQueryVersion
,req
);
402 req
->reqType
= opcode
;
403 req
->glxCode
= X_GLXQueryVersion
;
404 req
->majorVersion
= GLX_MAJOR_VERSION
;
405 req
->minorVersion
= GLX_MINOR_VERSION
;
406 _XReply(dpy
, (xReply
*) &reply
, 0, False
);
410 if (reply
.majorVersion
!= GLX_MAJOR_VERSION
) {
412 ** The server does not support the same major release as this
417 *major
= reply
.majorVersion
;
418 *minor
= min(reply
.minorVersion
, GLX_MINOR_VERSION
);
424 __glXInitializeVisualConfigFromTags( __GLcontextModes
*config
, int count
,
425 const INT32
*bp
, Bool tagged_only
,
426 Bool fbconfig_style_tags
)
431 /* Copy in the first set of properties */
432 config
->visualID
= *bp
++;
434 config
->visualType
= _gl_convert_from_x_visual_type( *bp
++ );
436 config
->rgbMode
= *bp
++;
438 config
->redBits
= *bp
++;
439 config
->greenBits
= *bp
++;
440 config
->blueBits
= *bp
++;
441 config
->alphaBits
= *bp
++;
442 config
->accumRedBits
= *bp
++;
443 config
->accumGreenBits
= *bp
++;
444 config
->accumBlueBits
= *bp
++;
445 config
->accumAlphaBits
= *bp
++;
447 config
->doubleBufferMode
= *bp
++;
448 config
->stereoMode
= *bp
++;
450 config
->rgbBits
= *bp
++;
451 config
->depthBits
= *bp
++;
452 config
->stencilBits
= *bp
++;
453 config
->numAuxBuffers
= *bp
++;
454 config
->level
= *bp
++;
456 count
-= __GLX_MIN_CONFIG_PROPS
;
460 ** Additional properties may be in a list at the end
461 ** of the reply. They are in pairs of property type
462 ** and property value.
465 #define FETCH_OR_SET(tag) \
466 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
468 for (i
= 0; i
< count
; i
+= 2 ) {
471 FETCH_OR_SET( rgbMode
);
473 case GLX_BUFFER_SIZE
:
474 config
->rgbBits
= *bp
++;
477 config
->level
= *bp
++;
479 case GLX_DOUBLEBUFFER
:
480 FETCH_OR_SET( doubleBufferMode
);
483 FETCH_OR_SET( stereoMode
);
485 case GLX_AUX_BUFFERS
:
486 config
->numAuxBuffers
= *bp
++;
489 config
->redBits
= *bp
++;
492 config
->greenBits
= *bp
++;
495 config
->blueBits
= *bp
++;
498 config
->alphaBits
= *bp
++;
501 config
->depthBits
= *bp
++;
503 case GLX_STENCIL_SIZE
:
504 config
->stencilBits
= *bp
++;
506 case GLX_ACCUM_RED_SIZE
:
507 config
->accumRedBits
= *bp
++;
509 case GLX_ACCUM_GREEN_SIZE
:
510 config
->accumGreenBits
= *bp
++;
512 case GLX_ACCUM_BLUE_SIZE
:
513 config
->accumBlueBits
= *bp
++;
515 case GLX_ACCUM_ALPHA_SIZE
:
516 config
->accumAlphaBits
= *bp
++;
518 case GLX_VISUAL_CAVEAT_EXT
:
519 config
->visualRating
= *bp
++;
521 case GLX_X_VISUAL_TYPE
:
522 config
->visualType
= *bp
++;
524 case GLX_TRANSPARENT_TYPE
:
525 config
->transparentPixel
= *bp
++;
527 case GLX_TRANSPARENT_INDEX_VALUE
:
528 config
->transparentIndex
= *bp
++;
530 case GLX_TRANSPARENT_RED_VALUE
:
531 config
->transparentRed
= *bp
++;
533 case GLX_TRANSPARENT_GREEN_VALUE
:
534 config
->transparentGreen
= *bp
++;
536 case GLX_TRANSPARENT_BLUE_VALUE
:
537 config
->transparentBlue
= *bp
++;
539 case GLX_TRANSPARENT_ALPHA_VALUE
:
540 config
->transparentAlpha
= *bp
++;
543 config
->visualID
= *bp
++;
545 case GLX_DRAWABLE_TYPE
:
546 config
->drawableType
= *bp
++;
548 case GLX_RENDER_TYPE
:
549 config
->renderType
= *bp
++;
551 case GLX_X_RENDERABLE
:
552 config
->xRenderable
= *bp
++;
554 case GLX_FBCONFIG_ID
:
555 config
->fbconfigID
= *bp
++;
557 case GLX_MAX_PBUFFER_WIDTH
:
558 config
->maxPbufferWidth
= *bp
++;
560 case GLX_MAX_PBUFFER_HEIGHT
:
561 config
->maxPbufferHeight
= *bp
++;
563 case GLX_MAX_PBUFFER_PIXELS
:
564 config
->maxPbufferPixels
= *bp
++;
566 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
567 config
->optimalPbufferWidth
= *bp
++;
569 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
570 config
->optimalPbufferHeight
= *bp
++;
572 case GLX_VISUAL_SELECT_GROUP_SGIX
:
573 config
->visualSelectGroup
= *bp
++;
575 case GLX_SWAP_METHOD_OML
:
576 config
->swapMethod
= *bp
++;
578 case GLX_SAMPLE_BUFFERS_SGIS
:
579 config
->sampleBuffers
= *bp
++;
581 case GLX_SAMPLES_SGIS
:
582 config
->samples
= *bp
++;
584 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
585 config
->bindToTextureRgb
= *bp
++;
587 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
588 config
->bindToTextureRgba
= *bp
++;
590 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
591 config
->bindToMipmapTexture
= *bp
++;
593 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
594 config
->bindToTextureTargets
= *bp
++;
596 case GLX_Y_INVERTED_EXT
:
597 config
->yInverted
= *bp
++;
607 config
->renderType
= (config
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
609 config
->haveAccumBuffer
= ((config
->accumRedBits
+
610 config
->accumGreenBits
+
611 config
->accumBlueBits
+
612 config
->accumAlphaBits
) > 0);
613 config
->haveDepthBuffer
= (config
->depthBits
> 0);
614 config
->haveStencilBuffer
= (config
->stencilBits
> 0);
617 static __GLcontextModes
*
618 createConfigsFromProperties(Display
*dpy
, int nvisuals
, int nprops
,
619 int screen
, GLboolean tagged_only
)
621 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
623 __GLcontextModes
*modes
, *m
;
629 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
631 /* Check number of properties */
632 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
635 /* Allocate memory for our config structure */
636 modes
= _gl_context_modes_create(nvisuals
, sizeof(__GLcontextModes
));
640 prop_size
= nprops
* __GLX_SIZE_INT32
;
641 if (prop_size
<= sizeof(buf
))
644 props
= Xmalloc(prop_size
);
646 /* Read each config structure and convert it into our format */
648 for (i
= 0; i
< nvisuals
; i
++) {
649 _XRead(dpy
, (char *)props
, prop_size
);
650 /* Older X servers don't send this so we default it here. */
651 m
->drawableType
= GLX_WINDOW_BIT
;
652 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
653 tagged_only
, GL_TRUE
);
665 getVisualConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
, int screen
)
667 xGLXGetVisualConfigsReq
*req
;
668 __GLXscreenConfigs
*psc
;
669 xGLXGetVisualConfigsReply reply
;
673 psc
= priv
->screenConfigs
+ screen
;
675 GetReq(GLXGetVisualConfigs
, req
);
676 req
->reqType
= priv
->majorOpcode
;
677 req
->glxCode
= X_GLXGetVisualConfigs
;
678 req
->screen
= screen
;
680 if (!_XReply(dpy
, (xReply
*) &reply
, 0, False
))
683 psc
->visuals
= createConfigsFromProperties(dpy
,
690 return psc
->visuals
!= NULL
;
694 getFBConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
, int screen
)
696 xGLXGetFBConfigsReq
*fb_req
;
697 xGLXGetFBConfigsSGIXReq
*sgi_req
;
698 xGLXVendorPrivateWithReplyReq
*vpreq
;
699 xGLXGetFBConfigsReply reply
;
700 __GLXscreenConfigs
*psc
;
702 psc
= priv
->screenConfigs
+ screen
;
703 psc
->serverGLXexts
= __glXGetStringFromServer(dpy
, priv
->majorOpcode
,
704 X_GLXQueryServerString
,
705 screen
, GLX_EXTENSIONS
);
710 if (atof(priv
->serverGLXversion
) >= 1.3) {
711 GetReq(GLXGetFBConfigs
, fb_req
);
712 fb_req
->reqType
= priv
->majorOpcode
;
713 fb_req
->glxCode
= X_GLXGetFBConfigs
;
714 fb_req
->screen
= screen
;
715 } else if (strstr(psc
->serverGLXexts
, "GLX_SGIX_fbconfig") != NULL
) {
716 GetReqExtra(GLXVendorPrivateWithReply
,
717 sz_xGLXGetFBConfigsSGIXReq
+
718 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
719 sgi_req
= (xGLXGetFBConfigsSGIXReq
*) vpreq
;
720 sgi_req
->reqType
= priv
->majorOpcode
;
721 sgi_req
->glxCode
= X_GLXVendorPrivateWithReply
;
722 sgi_req
->vendorCode
= X_GLXvop_GetFBConfigsSGIX
;
723 sgi_req
->screen
= screen
;
727 if (!_XReply(dpy
, (xReply
*) &reply
, 0, False
))
730 psc
->configs
= createConfigsFromProperties(dpy
,
732 reply
.numAttribs
* 2,
737 return psc
->configs
!= NULL
;
741 ** Allocate the memory for the per screen configs for each screen.
742 ** If that works then fetch the per screen configs data.
744 static Bool
AllocAndFetchScreenConfigs(Display
*dpy
, __GLXdisplayPrivate
*priv
)
746 __GLXscreenConfigs
*psc
;
750 ** First allocate memory for the array of per screen configs.
752 screens
= ScreenCount(dpy
);
753 psc
= (__GLXscreenConfigs
*) Xmalloc(screens
* sizeof(__GLXscreenConfigs
));
757 memset(psc
, 0, screens
* sizeof(__GLXscreenConfigs
));
758 priv
->screenConfigs
= psc
;
760 priv
->serverGLXversion
= __glXGetStringFromServer(dpy
, priv
->majorOpcode
,
761 X_GLXQueryServerString
,
763 if ( priv
->serverGLXversion
== NULL
) {
764 FreeScreenConfigs(priv
);
768 for (i
= 0; i
< screens
; i
++, psc
++) {
769 getVisualConfigs(dpy
, priv
, i
);
770 getFBConfigs(dpy
, priv
, i
);
774 #ifdef GLX_DIRECT_RENDERING
775 psc
->driScreen
= (*priv
->driDisplay
->createScreen
)(psc
, i
, priv
);
783 ** Initialize the client side extension code.
785 __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 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 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 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 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 __DRIdrawable
*
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)
1192 return &pdraw
->driDrawable
;
1194 pdraw
= psc
->driScreen
->createDrawable(psc
, drawable
, gc
);
1196 return &pdraw
->driDrawable
;
1199 static Bool
BindContextWrapper( Display
*dpy
, GLXContext gc
,
1200 GLXDrawable draw
, GLXDrawable read
)
1202 __DRIdrawable
*pdraw
= FetchDRIDrawable(dpy
, draw
, gc
);
1203 __DRIdrawable
*pread
= FetchDRIDrawable(dpy
, read
, gc
);
1205 return (*gc
->driContext
.bindContext
)(&gc
->driContext
, pdraw
, pread
);
1209 static Bool
UnbindContextWrapper( GLXContext gc
)
1211 return (*gc
->driContext
.unbindContext
)(&gc
->driContext
);
1213 #endif /* GLX_DIRECT_RENDERING */
1217 * Make a particular context current.
1219 * \note This is in this file so that it can access dummyContext.
1221 USED
static Bool
MakeContextCurrent(Display
*dpy
, GLXDrawable draw
,
1222 GLXDrawable read
, GLXContext gc
)
1224 xGLXMakeCurrentReply reply
;
1225 const GLXContext oldGC
= __glXGetCurrentContext();
1226 const CARD8 opcode
= __glXSetupForCommand(dpy
);
1227 const CARD8 oldOpcode
= ((gc
== oldGC
) || (oldGC
== &dummyContext
))
1228 ? opcode
: __glXSetupForCommand(oldGC
->currentDpy
);
1229 Bool bindReturnValue
;
1232 if (!opcode
|| !oldOpcode
) {
1236 /* Make sure that the new context has a nonzero ID. In the request,
1237 * a zero context ID is used only to mean that we bind to no current
1240 if ((gc
!= NULL
) && (gc
->xid
== None
)) {
1244 #ifndef GLX_DIRECT_RENDERING
1245 if (gc
&& gc
->isDirect
) {
1250 _glapi_check_multithread();
1252 #ifdef GLX_DIRECT_RENDERING
1253 /* Bind the direct rendering context to the drawable */
1254 if (gc
&& gc
->isDirect
) {
1255 bindReturnValue
= (gc
->driContext
.private)
1256 ? BindContextWrapper(dpy
, gc
, draw
, read
)
1261 /* Send a glXMakeCurrent request to bind the new context. */
1263 SendMakeCurrentRequest(dpy
, opcode
, gc
? gc
->xid
: None
,
1264 ((dpy
!= oldGC
->currentDpy
) || oldGC
->isDirect
)
1265 ? None
: oldGC
->currentContextTag
,
1266 draw
, read
, &reply
);
1270 if (!bindReturnValue
) {
1274 if ((dpy
!= oldGC
->currentDpy
|| (gc
&& gc
->isDirect
)) &&
1275 !oldGC
->isDirect
&& oldGC
!= &dummyContext
) {
1276 xGLXMakeCurrentReply dummy_reply
;
1278 /* We are either switching from one dpy to another and have to
1279 * send a request to the previous dpy to unbind the previous
1280 * context, or we are switching away from a indirect context to
1281 * a direct context and have to send a request to the dpy to
1282 * unbind the previous context.
1284 (void) SendMakeCurrentRequest(oldGC
->currentDpy
, oldOpcode
, None
,
1285 oldGC
->currentContextTag
, None
, None
,
1288 #ifdef GLX_DIRECT_RENDERING
1289 else if (oldGC
->isDirect
&& oldGC
->driContext
.private) {
1290 (void) UnbindContextWrapper(oldGC
);
1295 /* Update our notion of what is current */
1298 /* Even though the contexts are the same the drawable might have
1299 * changed. Note that gc cannot be the dummy, and that oldGC
1300 * cannot be NULL, therefore if they are the same, gc is not
1301 * NULL and not the dummy.
1303 gc
->currentDrawable
= draw
;
1304 gc
->currentReadable
= read
;
1306 if (oldGC
!= &dummyContext
) {
1307 /* Old current context is no longer current to anybody */
1308 oldGC
->currentDpy
= 0;
1309 oldGC
->currentDrawable
= None
;
1310 oldGC
->currentReadable
= None
;
1311 oldGC
->currentContextTag
= 0;
1313 if (oldGC
->xid
== None
) {
1314 /* We are switching away from a context that was
1315 * previously destroyed, so we need to free the memory
1316 * for the old handle.
1318 #ifdef GLX_DIRECT_RENDERING
1319 /* Destroy the old direct rendering context */
1320 if (oldGC
->isDirect
) {
1321 if (oldGC
->driContext
.private) {
1322 (*oldGC
->driContext
.destroyContext
)
1323 (&oldGC
->driContext
);
1324 XF86DRIDestroyContext(oldGC
->createDpy
,
1327 oldGC
->driContext
.private = NULL
;
1331 __glXFreeContext(oldGC
);
1335 __glXSetCurrentContext(gc
);
1337 gc
->currentDpy
= dpy
;
1338 gc
->currentDrawable
= draw
;
1339 gc
->currentReadable
= read
;
1341 if (!gc
->isDirect
) {
1343 IndirectAPI
= __glXNewIndirectAPI();
1344 _glapi_set_dispatch(IndirectAPI
);
1346 #ifdef GLX_USE_APPLEGL
1348 extern void XAppleDRIUseIndirectDispatch(void);
1349 XAppleDRIUseIndirectDispatch();
1353 __GLXattribute
*state
=
1354 (__GLXattribute
*)(gc
->client_state_private
);
1356 gc
->currentContextTag
= reply
.contextTag
;
1357 if (state
->array_state
== NULL
) {
1358 (void) glGetString(GL_EXTENSIONS
);
1359 (void) glGetString(GL_VERSION
);
1360 __glXInitVertexArrayState(gc
);
1364 gc
->currentContextTag
= -1;
1367 __glXSetCurrentContext(&dummyContext
);
1368 #ifdef GLX_DIRECT_RENDERING
1369 _glapi_set_dispatch(NULL
); /* no-op functions */
1378 PUBLIC Bool
glXMakeCurrent(Display
*dpy
, GLXDrawable draw
, GLXContext gc
)
1380 return MakeContextCurrent( dpy
, draw
, draw
, gc
);
1383 PUBLIC
GLX_ALIAS(Bool
, glXMakeCurrentReadSGI
,
1384 (Display
*dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
1385 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
1387 PUBLIC
GLX_ALIAS(Bool
, glXMakeContextCurrent
,
1388 (Display
*dpy
, GLXDrawable d
, GLXDrawable r
, GLXContext ctx
),
1389 (dpy
, d
, r
, ctx
), MakeContextCurrent
)
1393 void __glXDumpDrawBuffer(__GLXcontext
*ctx
)
1395 GLubyte
*p
= ctx
->buf
;
1396 GLubyte
*end
= ctx
->pc
;
1397 GLushort opcode
, length
;
1401 opcode
= *((GLushort
*) p
);
1402 length
= *((GLushort
*) (p
+ 2));
1403 printf("%2x: %5d: ", opcode
, length
);
1406 while (length
> 0) {
1407 printf("%08x ", *((unsigned *) p
));
1416 #ifdef USE_SPARC_ASM
1418 * Used only when we are sparc, using sparc assembler.
1423 _glx_mesa_init_sparc_glapi_relocs(void)
1425 unsigned int *insn_ptr
, *end_ptr
;
1426 unsigned long disp_addr
;
1428 insn_ptr
= &_mesa_sparc_glapi_begin
;
1429 end_ptr
= &_mesa_sparc_glapi_end
;
1430 disp_addr
= (unsigned long) &_glapi_Dispatch
;
1433 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1434 * seem to be a better way to do this:
1436 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1438 * This code is patching in the ADDRESS of the pointer to the
1439 * dispatch table. Hence, it must be called exactly once, because
1440 * that address is not going to change.
1442 * What it points to can change, but Mesa (and hence, we) assume
1443 * that there is only one pointer.
1446 while (insn_ptr
< end_ptr
) {
1447 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1449 This code patches for 64-bit addresses. This had better
1450 not happen for Sparc/Linux, no matter what architecture we
1451 are building for. So, don't do this.
1453 The 'defined(__linux_64__)' is used here as a placeholder for
1454 when we do do 64-bit usermode on sparc linux.
1456 insn_ptr
[0] |= (disp_addr
>> (32 + 10));
1457 insn_ptr
[1] |= ((disp_addr
& 0xffffffff) >> 10);
1458 __glapi_sparc_icache_flush(&insn_ptr
[0]);
1459 insn_ptr
[2] |= ((disp_addr
>> 32) & ((1 << 10) - 1));
1460 insn_ptr
[3] |= (disp_addr
& ((1 << 10) - 1));
1461 __glapi_sparc_icache_flush(&insn_ptr
[2]);
1464 insn_ptr
[0] |= (disp_addr
>> 10);
1465 insn_ptr
[1] |= (disp_addr
& ((1 << 10) - 1));
1466 __glapi_sparc_icache_flush(&insn_ptr
[0]);
1471 #endif /* sparc ASM in use */