2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
33 * GLX protocol interface boot-strap code.
35 * Direct rendering support added by Precision Insight, Inc.
37 * \author Kevin E. Martin <kevin@precisioninsight.com>
41 #include "glxclient.h"
42 #include <X11/extensions/Xext.h>
43 #include <X11/extensions/extutil.h>
44 #ifdef GLX_USE_APPLEGL
45 #include "apple/apple_glx.h"
46 #include "apple/apple_visual.h"
48 #include "glxextensions.h"
50 #include <X11/Xlib-xcb.h>
56 void __glXDumpDrawBuffer(struct glx_context
* ctx
);
60 ** You can set this cell to 1 to force the gl drawing stuff to be
61 ** one command per packet
63 _X_HIDDEN
int __glXDebug
= 0;
65 /* Extension required boiler plate */
67 static const char __glXExtensionName
[] = GLX_EXTENSION_NAME
;
68 static struct glx_display
*glx_displays
;
70 static /* const */ char *error_list
[] = {
76 "GLXBadCurrentWindow",
77 "GLXBadRenderRequest",
79 "GLXUnsupportedPrivateRequest",
82 "GLXBadCurrentDrawable",
87 #ifdef GLX_USE_APPLEGL
88 static char *__glXErrorString(Display
*dpy
, int code
, XExtCodes
*codes
,
93 XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
94 __GLX_NUMBER_ERRORS
, error_list
)
97 * GLX events are a bit funky. We don't stuff the X event code into
98 * our user exposed (via XNextEvent) structure. Instead we use the GLX
99 * private event code namespace (and hope it doesn't conflict). Clients
100 * have to know that bit 15 in the event type field means they're getting
101 * a GLX event, and then handle the various sub-event types there, rather
102 * than simply checking the event code and handling it directly.
106 __glXWireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
108 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
113 switch ((wire
->u
.u
.type
& 0x7f) - glx_dpy
->codes
->first_event
) {
114 case GLX_PbufferClobber
:
116 GLXPbufferClobberEvent
*aevent
= (GLXPbufferClobberEvent
*)event
;
117 xGLXPbufferClobberEvent
*awire
= (xGLXPbufferClobberEvent
*)wire
;
118 aevent
->event_type
= awire
->type
;
119 aevent
->serial
= awire
->sequenceNumber
;
120 aevent
->event_type
= awire
->event_type
;
121 aevent
->draw_type
= awire
->draw_type
;
122 aevent
->drawable
= awire
->drawable
;
123 aevent
->buffer_mask
= awire
->buffer_mask
;
124 aevent
->aux_buffer
= awire
->aux_buffer
;
125 aevent
->x
= awire
->x
;
126 aevent
->y
= awire
->y
;
127 aevent
->width
= awire
->width
;
128 aevent
->height
= awire
->height
;
129 aevent
->count
= awire
->count
;
132 case GLX_BufferSwapComplete
:
134 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
135 xGLXBufferSwapComplete2
*awire
= (xGLXBufferSwapComplete2
*)wire
;
136 struct glx_drawable
*glxDraw
= GetGLXDrawable(dpy
, awire
->drawable
);
141 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
142 aevent
->send_event
= (awire
->type
& 0x80) != 0;
143 aevent
->display
= dpy
;
144 aevent
->event_type
= awire
->event_type
;
145 aevent
->drawable
= glxDraw
->xDrawable
;
146 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
147 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
149 /* Handle 32-Bit wire sbc wraparound in both directions to cope with out
150 * of sequence 64-Bit sbc's
152 if ((int64_t) awire
->sbc
< ((int64_t) glxDraw
->lastEventSbc
- 0x40000000))
153 glxDraw
->eventSbcWrap
+= 0x100000000;
154 if ((int64_t) awire
->sbc
> ((int64_t) glxDraw
->lastEventSbc
+ 0x40000000))
155 glxDraw
->eventSbcWrap
-= 0x100000000;
156 glxDraw
->lastEventSbc
= awire
->sbc
;
157 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
161 /* client doesn't support server event */
168 /* We don't actually support this. It doesn't make sense for clients to
169 * send each other GLX events.
172 __glXEventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
174 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
179 switch (event
->type
) {
184 case GLX_EXCHANGE_COMPLETE_INTEL
:
186 case GLX_COPY_COMPLETE_INTEL
:
188 case GLX_FLIP_COMPLETE_INTEL
:
191 /* client doesn't support server event */
198 /************************************************************************/
200 ** Free the per screen configs data as well as the array of
201 ** __glXScreenConfigs.
204 FreeScreenConfigs(struct glx_display
* priv
)
206 struct glx_screen
*psc
;
209 /* Free screen configuration information */
210 screens
= ScreenCount(priv
->dpy
);
211 for (i
= 0; i
< screens
; i
++) {
212 psc
= priv
->screens
[i
];
213 glx_screen_cleanup(psc
);
215 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
216 if (psc
->driScreen
) {
217 psc
->driScreen
->destroyScreen(psc
);
225 free((char *) priv
->screens
);
226 priv
->screens
= NULL
;
230 glx_display_free(struct glx_display
*priv
)
232 struct glx_context
*gc
;
234 gc
= __glXGetCurrentContext();
235 if (priv
->dpy
== gc
->currentDpy
) {
236 gc
->vtable
->destroy(gc
);
237 __glXSetCurrentContextNull();
240 FreeScreenConfigs(priv
);
241 free((char *) priv
->serverGLXvendor
);
242 free((char *) priv
->serverGLXversion
);
244 __glxHashDestroy(priv
->glXDrawHash
);
246 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
247 __glxHashDestroy(priv
->drawHash
);
249 /* Free the direct rendering per display data */
250 if (priv
->driswDisplay
)
251 (*priv
->driswDisplay
->destroyDisplay
) (priv
->driswDisplay
);
252 priv
->driswDisplay
= NULL
;
254 #if defined (GLX_USE_DRM)
255 if (priv
->driDisplay
)
256 (*priv
->driDisplay
->destroyDisplay
) (priv
->driDisplay
);
257 priv
->driDisplay
= NULL
;
259 if (priv
->dri2Display
)
260 (*priv
->dri2Display
->destroyDisplay
) (priv
->dri2Display
);
261 priv
->dri2Display
= NULL
;
263 if (priv
->dri3Display
)
264 (*priv
->dri3Display
->destroyDisplay
) (priv
->dri3Display
);
265 priv
->dri3Display
= NULL
;
266 #endif /* GLX_USE_DRM */
268 #if defined(GLX_USE_WINDOWSGL)
269 if (priv
->windowsdriDisplay
)
270 (*priv
->windowsdriDisplay
->destroyDisplay
) (priv
->windowsdriDisplay
);
271 priv
->windowsdriDisplay
= NULL
;
272 #endif /* GLX_USE_WINDOWSGL */
274 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
280 __glXCloseDisplay(Display
* dpy
, XExtCodes
* codes
)
282 struct glx_display
*priv
, **prev
;
284 _XLockMutex(_Xglobal_lock
);
285 prev
= &glx_displays
;
286 for (priv
= glx_displays
; priv
; prev
= &priv
->next
, priv
= priv
->next
) {
287 if (priv
->dpy
== dpy
) {
292 _XUnlockMutex(_Xglobal_lock
);
295 glx_display_free(priv
);
301 ** Query the version of the GLX extension. This procedure works even if
302 ** the client extension is not completely set up.
305 QueryVersion(Display
* dpy
, int opcode
, int *major
, int *minor
)
307 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
308 xcb_glx_query_version_reply_t
*reply
= xcb_glx_query_version_reply(c
,
309 xcb_glx_query_version
318 if (reply
->major_version
!= GLX_MAJOR_VERSION
) {
322 *major
= reply
->major_version
;
323 *minor
= min(reply
->minor_version
, GLX_MINOR_VERSION
);
329 * We don't want to enable this GLX_OML_swap_method in glxext.h,
330 * because we can't support it. The X server writes it out though,
331 * so we should handle it somehow, to avoid false warnings.
334 IGNORE_GLX_SWAP_METHOD_OML
= 0x8060
339 convert_from_x_visual_type(int visualType
)
341 static const int glx_visual_types
[] = {
342 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
343 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
344 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
347 if (visualType
< ARRAY_SIZE(glx_visual_types
))
348 return glx_visual_types
[visualType
];
354 * getVisualConfigs uses the !tagged_only path.
355 * getFBConfigs uses the tagged_only path.
358 __glXInitializeVisualConfigFromTags(struct glx_config
* config
, int count
,
359 const INT32
* bp
, Bool tagged_only
,
360 Bool fbconfig_style_tags
)
363 GLint renderType
= 0;
366 /* Copy in the first set of properties */
367 config
->visualID
= *bp
++;
369 config
->visualType
= convert_from_x_visual_type(*bp
++);
371 config
->rgbMode
= *bp
++;
373 config
->redBits
= *bp
++;
374 config
->greenBits
= *bp
++;
375 config
->blueBits
= *bp
++;
376 config
->alphaBits
= *bp
++;
377 config
->accumRedBits
= *bp
++;
378 config
->accumGreenBits
= *bp
++;
379 config
->accumBlueBits
= *bp
++;
380 config
->accumAlphaBits
= *bp
++;
382 config
->doubleBufferMode
= *bp
++;
383 config
->stereoMode
= *bp
++;
385 config
->rgbBits
= *bp
++;
386 config
->depthBits
= *bp
++;
387 config
->stencilBits
= *bp
++;
388 config
->numAuxBuffers
= *bp
++;
389 config
->level
= *bp
++;
391 #ifdef GLX_USE_APPLEGL
392 /* AppleSGLX supports pixmap and pbuffers with all config. */
393 config
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
394 /* Unfortunately this can create an ABI compatibility problem. */
397 count
-= __GLX_MIN_CONFIG_PROPS
;
401 config
->sRGBCapable
= GL_FALSE
;
404 ** Additional properties may be in a list at the end
405 ** of the reply. They are in pairs of property type
406 ** and property value.
409 #define FETCH_OR_SET(tag) \
410 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
412 for (i
= 0; i
< count
; i
+= 2) {
413 long int tag
= *bp
++;
417 FETCH_OR_SET(rgbMode
);
419 case GLX_BUFFER_SIZE
:
420 config
->rgbBits
= *bp
++;
423 config
->level
= *bp
++;
425 case GLX_DOUBLEBUFFER
:
426 FETCH_OR_SET(doubleBufferMode
);
429 FETCH_OR_SET(stereoMode
);
431 case GLX_AUX_BUFFERS
:
432 config
->numAuxBuffers
= *bp
++;
435 config
->redBits
= *bp
++;
438 config
->greenBits
= *bp
++;
441 config
->blueBits
= *bp
++;
444 config
->alphaBits
= *bp
++;
447 config
->depthBits
= *bp
++;
449 case GLX_STENCIL_SIZE
:
450 config
->stencilBits
= *bp
++;
452 case GLX_ACCUM_RED_SIZE
:
453 config
->accumRedBits
= *bp
++;
455 case GLX_ACCUM_GREEN_SIZE
:
456 config
->accumGreenBits
= *bp
++;
458 case GLX_ACCUM_BLUE_SIZE
:
459 config
->accumBlueBits
= *bp
++;
461 case GLX_ACCUM_ALPHA_SIZE
:
462 config
->accumAlphaBits
= *bp
++;
464 case GLX_VISUAL_CAVEAT_EXT
:
465 config
->visualRating
= *bp
++;
467 case GLX_X_VISUAL_TYPE
:
468 config
->visualType
= *bp
++;
470 case GLX_TRANSPARENT_TYPE
:
471 config
->transparentPixel
= *bp
++;
473 case GLX_TRANSPARENT_INDEX_VALUE
:
474 config
->transparentIndex
= *bp
++;
476 case GLX_TRANSPARENT_RED_VALUE
:
477 config
->transparentRed
= *bp
++;
479 case GLX_TRANSPARENT_GREEN_VALUE
:
480 config
->transparentGreen
= *bp
++;
482 case GLX_TRANSPARENT_BLUE_VALUE
:
483 config
->transparentBlue
= *bp
++;
485 case GLX_TRANSPARENT_ALPHA_VALUE
:
486 config
->transparentAlpha
= *bp
++;
489 config
->visualID
= *bp
++;
491 case GLX_DRAWABLE_TYPE
:
492 config
->drawableType
= *bp
++;
493 #ifdef GLX_USE_APPLEGL
494 /* AppleSGLX supports pixmap and pbuffers with all config. */
495 config
->drawableType
|= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
498 case GLX_RENDER_TYPE
: /* fbconfig render type bits */
501 case GLX_X_RENDERABLE
:
502 config
->xRenderable
= *bp
++;
504 case GLX_FBCONFIG_ID
:
505 config
->fbconfigID
= *bp
++;
507 case GLX_MAX_PBUFFER_WIDTH
:
508 config
->maxPbufferWidth
= *bp
++;
510 case GLX_MAX_PBUFFER_HEIGHT
:
511 config
->maxPbufferHeight
= *bp
++;
513 case GLX_MAX_PBUFFER_PIXELS
:
514 config
->maxPbufferPixels
= *bp
++;
516 #ifndef GLX_USE_APPLEGL
517 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
518 config
->optimalPbufferWidth
= *bp
++;
520 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
521 config
->optimalPbufferHeight
= *bp
++;
523 case GLX_VISUAL_SELECT_GROUP_SGIX
:
524 config
->visualSelectGroup
= *bp
++;
526 case GLX_SWAP_METHOD_OML
:
527 if (*bp
== GLX_SWAP_UNDEFINED_OML
||
528 *bp
== GLX_SWAP_COPY_OML
||
529 *bp
== GLX_SWAP_EXCHANGE_OML
) {
530 config
->swapMethod
= *bp
++;
532 /* X servers with old HW drivers may return any value here, so
533 * assume GLX_SWAP_METHOD_UNDEFINED.
535 config
->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
540 case GLX_SAMPLE_BUFFERS_SGIS
:
541 config
->sampleBuffers
= *bp
++;
543 case GLX_SAMPLES_SGIS
:
544 config
->samples
= *bp
++;
546 #ifdef GLX_USE_APPLEGL
547 case IGNORE_GLX_SWAP_METHOD_OML
:
548 /* We ignore this tag. See the comment above this function. */
552 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
553 config
->bindToTextureRgb
= *bp
++;
555 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
556 config
->bindToTextureRgba
= *bp
++;
558 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
559 config
->bindToMipmapTexture
= *bp
++;
561 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
562 config
->bindToTextureTargets
= *bp
++;
564 case GLX_Y_INVERTED_EXT
:
565 config
->yInverted
= *bp
++;
568 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT
:
569 config
->sRGBCapable
= *bp
++;
573 if (fbconfig_style_tags
)
580 if(getenv("LIBGL_DIAGNOSTIC")) {
581 long int tagvalue
= *bp
++;
582 fprintf(stderr
, "WARNING: unknown GLX tag from server: "
583 "tag 0x%lx value 0x%lx\n", tag
, tagvalue
);
585 /* Ignore the unrecognized tag's value */
592 if (renderType
!= 0 && renderType
!= GLX_DONT_CARE
) {
593 config
->renderType
= renderType
;
594 config
->floatMode
= (renderType
&
595 (GLX_RGBA_FLOAT_BIT_ARB
|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
)) != 0;
597 /* If there wasn't GLX_RENDER_TYPE property, set it based on
598 * config->rgbMode. The only way to communicate that the config is
599 * floating-point is via GLX_RENDER_TYPE, so this cannot be a float
603 (config
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
606 /* The GLX_ARB_fbconfig_float spec says:
608 * "Note that floating point rendering is only supported for
609 * GLXPbuffer drawables."
611 if (config
->floatMode
)
612 config
->drawableType
&= GLX_PBUFFER_BIT
;
615 static struct glx_config
*
616 createConfigsFromProperties(Display
* dpy
, int nvisuals
, int nprops
,
617 int screen
, GLboolean tagged_only
)
619 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
621 struct glx_config
*modes
, *m
;
627 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
629 /* Check number of properties */
630 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
633 /* Allocate memory for our config structure */
634 modes
= glx_config_create_list(nvisuals
);
638 prop_size
= nprops
* __GLX_SIZE_INT32
;
639 if (prop_size
<= sizeof(buf
))
642 props
= malloc(prop_size
);
644 /* Read each config structure and convert it into our format */
646 for (i
= 0; i
< nvisuals
; i
++) {
647 _XRead(dpy
, (char *) props
, prop_size
);
648 #ifdef GLX_USE_APPLEGL
649 /* Older X servers don't send this so we default it here. */
650 m
->drawableType
= GLX_WINDOW_BIT
;
653 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
654 * set the proper bits here.
655 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
657 m
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
659 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
660 tagged_only
, GL_TRUE
);
672 getVisualConfigs(struct glx_screen
*psc
,
673 struct glx_display
*priv
, int screen
)
675 xGLXGetVisualConfigsReq
*req
;
676 xGLXGetVisualConfigsReply reply
;
677 Display
*dpy
= priv
->dpy
;
682 GetReq(GLXGetVisualConfigs
, req
);
683 req
->reqType
= priv
->majorOpcode
;
684 req
->glxCode
= X_GLXGetVisualConfigs
;
685 req
->screen
= screen
;
687 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
690 psc
->visuals
= createConfigsFromProperties(dpy
,
697 return psc
->visuals
!= NULL
;
701 getFBConfigs(struct glx_screen
*psc
, struct glx_display
*priv
, int screen
)
703 xGLXGetFBConfigsReq
*fb_req
;
704 xGLXGetFBConfigsSGIXReq
*sgi_req
;
705 xGLXVendorPrivateWithReplyReq
*vpreq
;
706 xGLXGetFBConfigsReply reply
;
707 Display
*dpy
= priv
->dpy
;
710 __glXQueryServerString(dpy
, priv
->majorOpcode
, screen
, GLX_EXTENSIONS
);
712 if (psc
->serverGLXexts
== NULL
) {
719 if (atof(priv
->serverGLXversion
) >= 1.3) {
720 GetReq(GLXGetFBConfigs
, fb_req
);
721 fb_req
->reqType
= priv
->majorOpcode
;
722 fb_req
->glxCode
= X_GLXGetFBConfigs
;
723 fb_req
->screen
= screen
;
725 else if (strstr(psc
->serverGLXexts
, "GLX_SGIX_fbconfig") != NULL
) {
726 GetReqExtra(GLXVendorPrivateWithReply
,
727 sz_xGLXGetFBConfigsSGIXReq
-
728 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
729 sgi_req
= (xGLXGetFBConfigsSGIXReq
*) vpreq
;
730 sgi_req
->reqType
= priv
->majorOpcode
;
731 sgi_req
->glxCode
= X_GLXVendorPrivateWithReply
;
732 sgi_req
->vendorCode
= X_GLXvop_GetFBConfigsSGIX
;
733 sgi_req
->screen
= screen
;
738 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
741 psc
->configs
= createConfigsFromProperties(dpy
,
743 reply
.numAttribs
* 2,
748 return psc
->configs
!= NULL
;
752 glx_screen_init(struct glx_screen
*psc
,
753 int screen
, struct glx_display
* priv
)
755 /* Initialize per screen dynamic client GLX extensions */
756 psc
->ext_list_first_time
= GL_TRUE
;
758 psc
->dpy
= priv
->dpy
;
761 if (!getVisualConfigs(psc
, priv
, screen
))
764 if (!getFBConfigs(psc
, priv
, screen
))
771 glx_screen_cleanup(struct glx_screen
*psc
)
774 glx_config_destroy_list(psc
->configs
);
775 free(psc
->effectiveGLXexts
);
776 psc
->configs
= NULL
; /* NOTE: just for paranoia */
779 glx_config_destroy_list(psc
->visuals
);
780 psc
->visuals
= NULL
; /* NOTE: just for paranoia */
782 free((char *) psc
->serverGLXexts
);
786 ** Allocate the memory for the per screen configs for each screen.
787 ** If that works then fetch the per screen configs data.
790 AllocAndFetchScreenConfigs(Display
* dpy
, struct glx_display
* priv
)
792 struct glx_screen
*psc
;
796 ** First allocate memory for the array of per screen configs.
798 screens
= ScreenCount(dpy
);
799 priv
->screens
= malloc(screens
* sizeof *priv
->screens
);
803 priv
->serverGLXversion
=
804 __glXQueryServerString(dpy
, priv
->majorOpcode
, 0, GLX_VERSION
);
805 if (priv
->serverGLXversion
== NULL
) {
806 FreeScreenConfigs(priv
);
810 for (i
= 0; i
< screens
; i
++, psc
++) {
812 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
813 #if defined(GLX_USE_DRM)
814 #if defined(HAVE_DRI3)
815 if (priv
->dri3Display
)
816 psc
= (*priv
->dri3Display
->createScreen
) (i
, priv
);
817 #endif /* HAVE_DRI3 */
818 if (psc
== NULL
&& priv
->dri2Display
)
819 psc
= (*priv
->dri2Display
->createScreen
) (i
, priv
);
820 if (psc
== NULL
&& priv
->driDisplay
)
821 psc
= (*priv
->driDisplay
->createScreen
) (i
, priv
);
822 #endif /* GLX_USE_DRM */
824 #ifdef GLX_USE_WINDOWSGL
825 if (psc
== NULL
&& priv
->windowsdriDisplay
)
826 psc
= (*priv
->windowsdriDisplay
->createScreen
) (i
, priv
);
829 if (psc
== NULL
&& priv
->driswDisplay
)
830 psc
= (*priv
->driswDisplay
->createScreen
) (i
, priv
);
831 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
833 #if defined(GLX_USE_APPLEGL)
835 psc
= applegl_create_screen(i
, priv
);
838 psc
= indirect_create_screen(i
, priv
);
840 priv
->screens
[i
] = psc
;
847 ** Initialize the client side extension code.
849 _X_HIDDEN
struct glx_display
*
850 __glXInitialize(Display
* dpy
)
852 struct glx_display
*dpyPriv
, *d
;
853 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
854 Bool glx_direct
, glx_accel
;
858 _XLockMutex(_Xglobal_lock
);
860 for (dpyPriv
= glx_displays
; dpyPriv
; dpyPriv
= dpyPriv
->next
) {
861 if (dpyPriv
->dpy
== dpy
) {
862 _XUnlockMutex(_Xglobal_lock
);
867 /* Drop the lock while we create the display private. */
868 _XUnlockMutex(_Xglobal_lock
);
870 dpyPriv
= calloc(1, sizeof *dpyPriv
);
874 dpyPriv
->codes
= XInitExtension(dpy
, __glXExtensionName
);
875 if (!dpyPriv
->codes
) {
881 dpyPriv
->majorOpcode
= dpyPriv
->codes
->major_opcode
;
882 dpyPriv
->serverGLXvendor
= 0x0;
883 dpyPriv
->serverGLXversion
= 0x0;
885 /* See if the versions are compatible. This GLX implementation does not
886 * work with servers that only support GLX 1.0.
888 if (!QueryVersion(dpy
, dpyPriv
->majorOpcode
,
889 &dpyPriv
->majorVersion
, &dpyPriv
->minorVersion
)
890 || (dpyPriv
->majorVersion
== 1 && dpyPriv
->minorVersion
< 1)) {
895 for (i
= 0; i
< __GLX_NUMBER_EVENTS
; i
++) {
896 XESetWireToEvent(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXWireToEvent
);
897 XESetEventToWire(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXEventToWire
);
900 XESetCloseDisplay(dpy
, dpyPriv
->codes
->extension
, __glXCloseDisplay
);
901 XESetErrorString (dpy
, dpyPriv
->codes
->extension
,__glXErrorString
);
903 dpyPriv
->glXDrawHash
= __glxHashCreate();
905 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
906 glx_direct
= (getenv("LIBGL_ALWAYS_INDIRECT") == NULL
);
907 glx_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
909 dpyPriv
->drawHash
= __glxHashCreate();
912 ** Initialize the direct rendering per display data and functions.
913 ** Note: This _must_ be done before calling any other DRI routines
914 ** (e.g., those called in AllocAndFetchScreenConfigs).
916 #if defined(GLX_USE_DRM)
917 if (glx_direct
&& glx_accel
) {
918 #if defined(HAVE_DRI3)
919 if (!getenv("LIBGL_DRI3_DISABLE"))
920 dpyPriv
->dri3Display
= dri3_create_display(dpy
);
921 #endif /* HAVE_DRI3 */
922 dpyPriv
->dri2Display
= dri2CreateDisplay(dpy
);
923 dpyPriv
->driDisplay
= driCreateDisplay(dpy
);
925 #endif /* GLX_USE_DRM */
927 dpyPriv
->driswDisplay
= driswCreateDisplay(dpy
);
928 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
930 #ifdef GLX_USE_APPLEGL
931 if (!applegl_create_display(dpyPriv
)) {
937 #ifdef GLX_USE_WINDOWSGL
938 if (glx_direct
&& glx_accel
)
939 dpyPriv
->windowsdriDisplay
= driwindowsCreateDisplay(dpy
);
942 if (!AllocAndFetchScreenConfigs(dpy
, dpyPriv
)) {
947 __glX_send_client_info(dpyPriv
);
949 /* Grab the lock again and add the dispay private, unless somebody
950 * beat us to initializing on this display in the meantime. */
951 _XLockMutex(_Xglobal_lock
);
953 for (d
= glx_displays
; d
; d
= d
->next
) {
955 _XUnlockMutex(_Xglobal_lock
);
956 glx_display_free(dpyPriv
);
961 dpyPriv
->next
= glx_displays
;
962 glx_displays
= dpyPriv
;
964 _XUnlockMutex(_Xglobal_lock
);
970 ** Setup for sending a GLX command on dpy. Make sure the extension is
971 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
974 __glXSetupForCommand(Display
* dpy
)
976 struct glx_context
*gc
;
977 struct glx_display
*priv
;
979 /* If this thread has a current context, flush its rendering commands */
980 gc
= __glXGetCurrentContext();
981 if (gc
->currentDpy
) {
982 /* Flush rendering buffer of the current context, if any */
983 (void) __glXFlushRenderBuffer(gc
, gc
->pc
);
985 if (gc
->currentDpy
== dpy
) {
986 /* Use opcode from gc because its right */
987 return gc
->majorOpcode
;
991 ** Have to get info about argument dpy because it might be to
992 ** a different server
997 /* Forced to lookup extension via the slow initialize route */
998 priv
= __glXInitialize(dpy
);
1002 return priv
->majorOpcode
;
1006 * Flush the drawing command transport buffer.
1008 * \param ctx Context whose transport buffer is to be flushed.
1009 * \param pc Pointer to first unused buffer location.
1012 * Modify this function to use \c ctx->pc instead of the explicit
1016 __glXFlushRenderBuffer(struct glx_context
* ctx
, GLubyte
* pc
)
1018 Display
*const dpy
= ctx
->currentDpy
;
1019 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1020 const GLint size
= pc
- ctx
->buf
;
1022 if ((dpy
!= NULL
) && (size
> 0)) {
1023 xcb_glx_render(c
, ctx
->currentContextTag
, size
,
1024 (const uint8_t *) ctx
->buf
);
1027 /* Reset pointer and return it */
1034 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1035 * this function over \c __glXSendLargeCommand is that callers can use the
1036 * data buffer in the GLX context and may be able to avoid allocating an
1037 * extra buffer. The disadvantage is the clients will have to do more
1038 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1040 * \sa __glXSendLargeCommand
1042 * \param gc GLX context
1043 * \param requestNumber Which part of the whole command is this? The first
1045 * \param totalRequests How many requests will there be?
1046 * \param data Command data.
1047 * \param dataLen Size, in bytes, of the command data.
1050 __glXSendLargeChunk(struct glx_context
* gc
, GLint requestNumber
,
1051 GLint totalRequests
, const GLvoid
* data
, GLint dataLen
)
1053 Display
*dpy
= gc
->currentDpy
;
1054 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1055 xcb_glx_render_large(c
, gc
->currentContextTag
, requestNumber
,
1056 totalRequests
, dataLen
, data
);
1061 * Send a command that is too large for the GLXRender protocol request.
1063 * Send a large command, one that is too large for some reason to
1064 * send using the GLXRender protocol request. One reason to send
1065 * a large command is to avoid copying the data.
1067 * \param ctx GLX context
1068 * \param header Header data.
1069 * \param headerLen Size, in bytes, of the header data. It is assumed that
1070 * the header data will always be small enough to fit in
1071 * a single X protocol packet.
1072 * \param data Command data.
1073 * \param dataLen Size, in bytes, of the command data.
1076 __glXSendLargeCommand(struct glx_context
* ctx
,
1077 const GLvoid
* header
, GLint headerLen
,
1078 const GLvoid
* data
, GLint dataLen
)
1081 GLint totalRequests
, requestNumber
;
1084 ** Calculate the maximum amount of data can be stuffed into a single
1085 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1086 ** packet size minus sz_xGLXRenderReq.
1088 maxSize
= (ctx
->bufSize
+ sz_xGLXRenderReq
) - sz_xGLXRenderLargeReq
;
1089 totalRequests
= 1 + (dataLen
/ maxSize
);
1090 if (dataLen
% maxSize
)
1094 ** Send all of the command, except the large array, as one request.
1096 assert(headerLen
<= maxSize
);
1097 __glXSendLargeChunk(ctx
, 1, totalRequests
, header
, headerLen
);
1100 ** Send enough requests until the whole array is sent.
1102 for (requestNumber
= 2; requestNumber
<= (totalRequests
- 1);
1104 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, maxSize
);
1105 data
= (const GLvoid
*) (((const GLubyte
*) data
) + maxSize
);
1107 assert(dataLen
> 0);
1110 assert(dataLen
<= maxSize
);
1111 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, dataLen
);
1114 /************************************************************************/
1118 __glXDumpDrawBuffer(struct glx_context
* ctx
)
1120 GLubyte
*p
= ctx
->buf
;
1121 GLubyte
*end
= ctx
->pc
;
1122 GLushort opcode
, length
;
1126 opcode
= *((GLushort
*) p
);
1127 length
= *((GLushort
*) (p
+ 2));
1128 printf("%2x: %5d: ", opcode
, length
);
1131 while (length
> 0) {
1132 printf("%08x ", *((unsigned *) p
));