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>
43 #include "glxclient.h"
44 #include <X11/extensions/Xext.h>
45 #include <X11/extensions/extutil.h>
46 #ifdef GLX_USE_APPLEGL
47 #include "apple/apple_glx.h"
48 #include "apple/apple_visual.h"
50 #include "glxextensions.h"
52 #include "util/debug.h"
53 #include "dri_common.h"
55 #include <X11/Xlib-xcb.h>
61 void __glXDumpDrawBuffer(struct glx_context
* ctx
);
65 ** You can set this cell to 1 to force the gl drawing stuff to be
66 ** one command per packet
68 _X_HIDDEN
int __glXDebug
= 0;
70 /* Extension required boiler plate */
72 static const char __glXExtensionName
[] = GLX_EXTENSION_NAME
;
73 static struct glx_display
*glx_displays
;
75 static /* const */ char *error_list
[] = {
81 "GLXBadCurrentWindow",
82 "GLXBadRenderRequest",
84 "GLXUnsupportedPrivateRequest",
87 "GLXBadCurrentDrawable",
92 #ifdef GLX_USE_APPLEGL
93 static char *__glXErrorString(Display
*dpy
, int code
, XExtCodes
*codes
,
98 XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
99 __GLX_NUMBER_ERRORS
, error_list
)
102 * GLX events are a bit funky. We don't stuff the X event code into
103 * our user exposed (via XNextEvent) structure. Instead we use the GLX
104 * private event code namespace (and hope it doesn't conflict). Clients
105 * have to know that bit 15 in the event type field means they're getting
106 * a GLX event, and then handle the various sub-event types there, rather
107 * than simply checking the event code and handling it directly.
111 __glXWireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
113 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
118 switch ((wire
->u
.u
.type
& 0x7f) - glx_dpy
->codes
->first_event
) {
119 case GLX_PbufferClobber
:
121 GLXPbufferClobberEvent
*aevent
= (GLXPbufferClobberEvent
*)event
;
122 xGLXPbufferClobberEvent
*awire
= (xGLXPbufferClobberEvent
*)wire
;
123 aevent
->event_type
= awire
->type
;
124 aevent
->serial
= awire
->sequenceNumber
;
125 aevent
->event_type
= awire
->event_type
;
126 aevent
->draw_type
= awire
->draw_type
;
127 aevent
->drawable
= awire
->drawable
;
128 aevent
->buffer_mask
= awire
->buffer_mask
;
129 aevent
->aux_buffer
= awire
->aux_buffer
;
130 aevent
->x
= awire
->x
;
131 aevent
->y
= awire
->y
;
132 aevent
->width
= awire
->width
;
133 aevent
->height
= awire
->height
;
134 aevent
->count
= awire
->count
;
137 case GLX_BufferSwapComplete
:
139 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
140 xGLXBufferSwapComplete2
*awire
= (xGLXBufferSwapComplete2
*)wire
;
141 struct glx_drawable
*glxDraw
= GetGLXDrawable(dpy
, awire
->drawable
);
146 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
147 aevent
->send_event
= (awire
->type
& 0x80) != 0;
148 aevent
->display
= dpy
;
149 aevent
->event_type
= awire
->event_type
;
150 aevent
->drawable
= glxDraw
->xDrawable
;
151 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
152 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
154 /* Handle 32-Bit wire sbc wraparound in both directions to cope with out
155 * of sequence 64-Bit sbc's
157 if ((int64_t) awire
->sbc
< ((int64_t) glxDraw
->lastEventSbc
- 0x40000000))
158 glxDraw
->eventSbcWrap
+= 0x100000000;
159 if ((int64_t) awire
->sbc
> ((int64_t) glxDraw
->lastEventSbc
+ 0x40000000))
160 glxDraw
->eventSbcWrap
-= 0x100000000;
161 glxDraw
->lastEventSbc
= awire
->sbc
;
162 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
166 /* client doesn't support server event */
173 /* We don't actually support this. It doesn't make sense for clients to
174 * send each other GLX events.
177 __glXEventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
179 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
184 switch (event
->type
) {
189 case GLX_EXCHANGE_COMPLETE_INTEL
:
191 case GLX_COPY_COMPLETE_INTEL
:
193 case GLX_FLIP_COMPLETE_INTEL
:
196 /* client doesn't support server event */
203 /************************************************************************/
205 ** Free the per screen configs data as well as the array of
206 ** __glXScreenConfigs.
209 FreeScreenConfigs(struct glx_display
* priv
)
211 struct glx_screen
*psc
;
214 /* Free screen configuration information */
215 screens
= ScreenCount(priv
->dpy
);
216 for (i
= 0; i
< screens
; i
++) {
217 psc
= priv
->screens
[i
];
220 glx_screen_cleanup(psc
);
222 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
223 if (psc
->driScreen
) {
224 psc
->driScreen
->destroyScreen(psc
);
232 free((char *) priv
->screens
);
233 priv
->screens
= NULL
;
237 glx_display_free(struct glx_display
*priv
)
239 struct glx_context
*gc
;
241 gc
= __glXGetCurrentContext();
242 if (priv
->dpy
== gc
->currentDpy
) {
243 gc
->vtable
->destroy(gc
);
244 __glXSetCurrentContextNull();
247 FreeScreenConfigs(priv
);
248 free((char *) priv
->serverGLXvendor
);
249 free((char *) priv
->serverGLXversion
);
251 __glxHashDestroy(priv
->glXDrawHash
);
253 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
254 __glxHashDestroy(priv
->drawHash
);
256 /* Free the direct rendering per display data */
257 if (priv
->driswDisplay
)
258 (*priv
->driswDisplay
->destroyDisplay
) (priv
->driswDisplay
);
259 priv
->driswDisplay
= NULL
;
261 #if defined (GLX_USE_DRM)
262 if (priv
->driDisplay
)
263 (*priv
->driDisplay
->destroyDisplay
) (priv
->driDisplay
);
264 priv
->driDisplay
= NULL
;
266 if (priv
->dri2Display
)
267 (*priv
->dri2Display
->destroyDisplay
) (priv
->dri2Display
);
268 priv
->dri2Display
= NULL
;
270 if (priv
->dri3Display
)
271 (*priv
->dri3Display
->destroyDisplay
) (priv
->dri3Display
);
272 priv
->dri3Display
= NULL
;
273 #endif /* GLX_USE_DRM */
275 #if defined(GLX_USE_WINDOWSGL)
276 if (priv
->windowsdriDisplay
)
277 (*priv
->windowsdriDisplay
->destroyDisplay
) (priv
->windowsdriDisplay
);
278 priv
->windowsdriDisplay
= NULL
;
279 #endif /* GLX_USE_WINDOWSGL */
281 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
287 __glXCloseDisplay(Display
* dpy
, XExtCodes
* codes
)
289 struct glx_display
*priv
, **prev
;
291 _XLockMutex(_Xglobal_lock
);
292 prev
= &glx_displays
;
293 for (priv
= glx_displays
; priv
; prev
= &priv
->next
, priv
= priv
->next
) {
294 if (priv
->dpy
== dpy
) {
299 _XUnlockMutex(_Xglobal_lock
);
302 glx_display_free(priv
);
308 ** Query the version of the GLX extension. This procedure works even if
309 ** the client extension is not completely set up.
312 QueryVersion(Display
* dpy
, int opcode
, int *major
, int *minor
)
314 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
315 xcb_glx_query_version_reply_t
*reply
= xcb_glx_query_version_reply(c
,
316 xcb_glx_query_version
325 if (reply
->major_version
!= GLX_MAJOR_VERSION
) {
329 *major
= reply
->major_version
;
330 *minor
= min(reply
->minor_version
, GLX_MINOR_VERSION
);
336 * We don't want to enable this GLX_OML_swap_method in glxext.h,
337 * because we can't support it. The X server writes it out though,
338 * so we should handle it somehow, to avoid false warnings.
341 IGNORE_GLX_SWAP_METHOD_OML
= 0x8060
346 convert_from_x_visual_type(int visualType
)
348 static const int glx_visual_types
[] = {
349 [StaticGray
] = GLX_STATIC_GRAY
,
350 [GrayScale
] = GLX_GRAY_SCALE
,
351 [StaticColor
] = GLX_STATIC_COLOR
,
352 [PseudoColor
] = GLX_PSEUDO_COLOR
,
353 [TrueColor
] = GLX_TRUE_COLOR
,
354 [DirectColor
] = GLX_DIRECT_COLOR
,
357 if (visualType
< ARRAY_SIZE(glx_visual_types
))
358 return glx_visual_types
[visualType
];
364 * getVisualConfigs uses the !tagged_only path.
365 * getFBConfigs uses the tagged_only path.
368 __glXInitializeVisualConfigFromTags(struct glx_config
* config
, int count
,
369 const INT32
* bp
, Bool tagged_only
,
370 Bool fbconfig_style_tags
)
375 /* Copy in the first set of properties */
376 config
->visualID
= *bp
++;
378 config
->visualType
= convert_from_x_visual_type(*bp
++);
380 config
->renderType
= *bp
++ ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
382 config
->redBits
= *bp
++;
383 config
->greenBits
= *bp
++;
384 config
->blueBits
= *bp
++;
385 config
->alphaBits
= *bp
++;
386 config
->accumRedBits
= *bp
++;
387 config
->accumGreenBits
= *bp
++;
388 config
->accumBlueBits
= *bp
++;
389 config
->accumAlphaBits
= *bp
++;
391 config
->doubleBufferMode
= *bp
++;
392 config
->stereoMode
= *bp
++;
394 config
->rgbBits
= *bp
++;
395 config
->depthBits
= *bp
++;
396 config
->stencilBits
= *bp
++;
397 config
->numAuxBuffers
= *bp
++;
398 config
->level
= *bp
++;
400 #ifdef GLX_USE_APPLEGL
401 /* AppleSGLX supports pixmap and pbuffers with all config. */
402 config
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
403 /* Unfortunately this can create an ABI compatibility problem. */
406 count
-= __GLX_MIN_CONFIG_PROPS
;
411 ** Additional properties may be in a list at the end
412 ** of the reply. They are in pairs of property type
413 ** and property value.
416 #define FETCH_OR_SET(tag) \
417 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
419 for (i
= 0; i
< count
; i
+= 2) {
420 long int tag
= *bp
++;
424 if (fbconfig_style_tags
)
425 config
->renderType
= *bp
++ ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
427 config
->renderType
= GLX_RGBA_BIT
;
429 case GLX_BUFFER_SIZE
:
430 config
->rgbBits
= *bp
++;
433 config
->level
= *bp
++;
435 case GLX_DOUBLEBUFFER
:
436 FETCH_OR_SET(doubleBufferMode
);
439 FETCH_OR_SET(stereoMode
);
441 case GLX_AUX_BUFFERS
:
442 config
->numAuxBuffers
= *bp
++;
445 config
->redBits
= *bp
++;
448 config
->greenBits
= *bp
++;
451 config
->blueBits
= *bp
++;
454 config
->alphaBits
= *bp
++;
457 config
->depthBits
= *bp
++;
459 case GLX_STENCIL_SIZE
:
460 config
->stencilBits
= *bp
++;
462 case GLX_ACCUM_RED_SIZE
:
463 config
->accumRedBits
= *bp
++;
465 case GLX_ACCUM_GREEN_SIZE
:
466 config
->accumGreenBits
= *bp
++;
468 case GLX_ACCUM_BLUE_SIZE
:
469 config
->accumBlueBits
= *bp
++;
471 case GLX_ACCUM_ALPHA_SIZE
:
472 config
->accumAlphaBits
= *bp
++;
474 case GLX_VISUAL_CAVEAT_EXT
:
475 config
->visualRating
= *bp
++;
477 case GLX_X_VISUAL_TYPE
:
478 config
->visualType
= *bp
++;
480 case GLX_TRANSPARENT_TYPE
:
481 config
->transparentPixel
= *bp
++;
483 case GLX_TRANSPARENT_INDEX_VALUE
:
484 config
->transparentIndex
= *bp
++;
486 case GLX_TRANSPARENT_RED_VALUE
:
487 config
->transparentRed
= *bp
++;
489 case GLX_TRANSPARENT_GREEN_VALUE
:
490 config
->transparentGreen
= *bp
++;
492 case GLX_TRANSPARENT_BLUE_VALUE
:
493 config
->transparentBlue
= *bp
++;
495 case GLX_TRANSPARENT_ALPHA_VALUE
:
496 config
->transparentAlpha
= *bp
++;
499 config
->visualID
= *bp
++;
501 case GLX_DRAWABLE_TYPE
:
502 config
->drawableType
= *bp
++;
503 #ifdef GLX_USE_APPLEGL
504 /* AppleSGLX supports pixmap and pbuffers with all config. */
505 config
->drawableType
|= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
508 case GLX_RENDER_TYPE
: /* fbconfig render type bits */
509 config
->renderType
= *bp
++;
511 case GLX_X_RENDERABLE
:
512 config
->xRenderable
= *bp
++;
514 case GLX_FBCONFIG_ID
:
515 config
->fbconfigID
= *bp
++;
517 case GLX_MAX_PBUFFER_WIDTH
:
518 config
->maxPbufferWidth
= *bp
++;
520 case GLX_MAX_PBUFFER_HEIGHT
:
521 config
->maxPbufferHeight
= *bp
++;
523 case GLX_MAX_PBUFFER_PIXELS
:
524 config
->maxPbufferPixels
= *bp
++;
526 #ifndef GLX_USE_APPLEGL
527 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
528 config
->optimalPbufferWidth
= *bp
++;
530 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
531 config
->optimalPbufferHeight
= *bp
++;
533 case GLX_VISUAL_SELECT_GROUP_SGIX
:
534 config
->visualSelectGroup
= *bp
++;
536 case GLX_SWAP_METHOD_OML
:
537 if (*bp
== GLX_SWAP_UNDEFINED_OML
||
538 *bp
== GLX_SWAP_COPY_OML
||
539 *bp
== GLX_SWAP_EXCHANGE_OML
) {
540 config
->swapMethod
= *bp
++;
542 /* X servers with old HW drivers may return any value here, so
543 * assume GLX_SWAP_METHOD_UNDEFINED.
545 config
->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
550 case GLX_SAMPLE_BUFFERS_SGIS
:
551 config
->sampleBuffers
= *bp
++;
553 case GLX_SAMPLES_SGIS
:
554 config
->samples
= *bp
++;
556 #ifdef GLX_USE_APPLEGL
557 case IGNORE_GLX_SWAP_METHOD_OML
:
558 /* We ignore this tag. See the comment above this function. */
562 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
563 config
->bindToTextureRgb
= *bp
++;
565 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
566 config
->bindToTextureRgba
= *bp
++;
568 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
569 config
->bindToMipmapTexture
= *bp
++;
571 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
572 config
->bindToTextureTargets
= *bp
++;
574 case GLX_Y_INVERTED_EXT
:
575 config
->yInverted
= *bp
++;
578 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT
:
579 config
->sRGBCapable
= *bp
++;
583 if (fbconfig_style_tags
)
590 if(env_var_as_boolean("LIBGL_DIAGNOSTIC", false)) {
591 long int tagvalue
= *bp
++;
592 fprintf(stderr
, "WARNING: unknown GLX tag from server: "
593 "tag 0x%lx value 0x%lx\n", tag
, tagvalue
);
595 /* Ignore the unrecognized tag's value */
602 /* The GLX_ARB_fbconfig_float spec says:
604 * "Note that floating point rendering is only supported for
605 * GLXPbuffer drawables."
607 if (config
->renderType
&
608 (GLX_RGBA_FLOAT_BIT_ARB
|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
))
609 config
->drawableType
&= GLX_PBUFFER_BIT
;
612 static struct glx_config
*
613 createConfigsFromProperties(Display
* dpy
, int nvisuals
, int nprops
,
614 int screen
, GLboolean tagged_only
)
616 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
618 struct glx_config
*modes
, *m
;
624 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
626 /* Check number of properties */
627 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
630 /* Allocate memory for our config structure */
631 modes
= glx_config_create_list(nvisuals
);
635 prop_size
= nprops
* __GLX_SIZE_INT32
;
636 if (prop_size
<= sizeof(buf
))
639 props
= malloc(prop_size
);
641 /* Read each config structure and convert it into our format */
643 for (i
= 0; i
< nvisuals
; i
++) {
644 _XRead(dpy
, (char *) props
, prop_size
);
645 #ifdef GLX_USE_APPLEGL
646 /* Older X servers don't send this so we default it here. */
647 m
->drawableType
= GLX_WINDOW_BIT
;
650 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
651 * set the proper bits here.
652 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
654 m
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
656 /* Older X servers don't send this so we default it here. */
657 m
->sRGBCapable
= GL_FALSE
;
658 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
659 tagged_only
, GL_TRUE
);
671 getVisualConfigs(struct glx_screen
*psc
,
672 struct glx_display
*priv
, int screen
)
674 xGLXGetVisualConfigsReq
*req
;
675 xGLXGetVisualConfigsReply reply
;
676 Display
*dpy
= priv
->dpy
;
681 GetReq(GLXGetVisualConfigs
, req
);
682 req
->reqType
= priv
->majorOpcode
;
683 req
->glxCode
= X_GLXGetVisualConfigs
;
684 req
->screen
= screen
;
686 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
689 psc
->visuals
= createConfigsFromProperties(dpy
,
696 return psc
->visuals
!= NULL
;
700 getFBConfigs(struct glx_screen
*psc
, struct glx_display
*priv
, int screen
)
702 xGLXGetFBConfigsReq
*fb_req
;
703 xGLXGetFBConfigsSGIXReq
*sgi_req
;
704 xGLXVendorPrivateWithReplyReq
*vpreq
;
705 xGLXGetFBConfigsReply reply
;
706 Display
*dpy
= priv
->dpy
;
709 __glXQueryServerString(dpy
, priv
->majorOpcode
, screen
, GLX_EXTENSIONS
);
711 if (psc
->serverGLXexts
== NULL
) {
718 if (priv
->majorVersion
> 1 ||
719 (priv
->majorVersion
== 1 && priv
->minorVersion
>= 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
= calloc(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
= !env_var_as_boolean("LIBGL_ALWAYS_INDIRECT", false);
907 glx_accel
= !env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);
909 dpyPriv
->drawHash
= __glxHashCreate();
911 /* Set the logger before the *CreateDisplay functions. */
912 loader_set_logger(dri_message
);
915 ** Initialize the direct rendering per display data and functions.
916 ** Note: This _must_ be done before calling any other DRI routines
917 ** (e.g., those called in AllocAndFetchScreenConfigs).
919 #if defined(GLX_USE_DRM)
920 if (glx_direct
&& glx_accel
) {
921 #if defined(HAVE_DRI3)
922 if (!env_var_as_boolean("LIBGL_DRI3_DISABLE", false))
923 dpyPriv
->dri3Display
= dri3_create_display(dpy
);
924 #endif /* HAVE_DRI3 */
925 dpyPriv
->dri2Display
= dri2CreateDisplay(dpy
);
926 dpyPriv
->driDisplay
= driCreateDisplay(dpy
);
928 #endif /* GLX_USE_DRM */
930 dpyPriv
->driswDisplay
= driswCreateDisplay(dpy
);
931 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
933 #ifdef GLX_USE_APPLEGL
934 if (!applegl_create_display(dpyPriv
)) {
940 #ifdef GLX_USE_WINDOWSGL
941 if (glx_direct
&& glx_accel
)
942 dpyPriv
->windowsdriDisplay
= driwindowsCreateDisplay(dpy
);
945 if (!AllocAndFetchScreenConfigs(dpy
, dpyPriv
)) {
950 __glX_send_client_info(dpyPriv
);
952 /* Grab the lock again and add the dispay private, unless somebody
953 * beat us to initializing on this display in the meantime. */
954 _XLockMutex(_Xglobal_lock
);
956 for (d
= glx_displays
; d
; d
= d
->next
) {
958 _XUnlockMutex(_Xglobal_lock
);
959 glx_display_free(dpyPriv
);
964 dpyPriv
->next
= glx_displays
;
965 glx_displays
= dpyPriv
;
967 _XUnlockMutex(_Xglobal_lock
);
973 ** Setup for sending a GLX command on dpy. Make sure the extension is
974 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
977 __glXSetupForCommand(Display
* dpy
)
979 struct glx_context
*gc
;
980 struct glx_display
*priv
;
982 /* If this thread has a current context, flush its rendering commands */
983 gc
= __glXGetCurrentContext();
984 if (gc
->currentDpy
) {
985 /* Flush rendering buffer of the current context, if any */
986 (void) __glXFlushRenderBuffer(gc
, gc
->pc
);
988 if (gc
->currentDpy
== dpy
) {
989 /* Use opcode from gc because its right */
990 return gc
->majorOpcode
;
994 ** Have to get info about argument dpy because it might be to
995 ** a different server
1000 /* Forced to lookup extension via the slow initialize route */
1001 priv
= __glXInitialize(dpy
);
1005 return priv
->majorOpcode
;
1009 * Flush the drawing command transport buffer.
1011 * \param ctx Context whose transport buffer is to be flushed.
1012 * \param pc Pointer to first unused buffer location.
1015 * Modify this function to use \c ctx->pc instead of the explicit
1019 __glXFlushRenderBuffer(struct glx_context
* ctx
, GLubyte
* pc
)
1021 Display
*const dpy
= ctx
->currentDpy
;
1022 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1023 const GLint size
= pc
- ctx
->buf
;
1025 if ((dpy
!= NULL
) && (size
> 0)) {
1026 xcb_glx_render(c
, ctx
->currentContextTag
, size
,
1027 (const uint8_t *) ctx
->buf
);
1030 /* Reset pointer and return it */
1037 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1038 * this function over \c __glXSendLargeCommand is that callers can use the
1039 * data buffer in the GLX context and may be able to avoid allocating an
1040 * extra buffer. The disadvantage is the clients will have to do more
1041 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1043 * \sa __glXSendLargeCommand
1045 * \param gc GLX context
1046 * \param requestNumber Which part of the whole command is this? The first
1048 * \param totalRequests How many requests will there be?
1049 * \param data Command data.
1050 * \param dataLen Size, in bytes, of the command data.
1053 __glXSendLargeChunk(struct glx_context
* gc
, GLint requestNumber
,
1054 GLint totalRequests
, const GLvoid
* data
, GLint dataLen
)
1056 Display
*dpy
= gc
->currentDpy
;
1057 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1058 xcb_glx_render_large(c
, gc
->currentContextTag
, requestNumber
,
1059 totalRequests
, dataLen
, data
);
1064 * Send a command that is too large for the GLXRender protocol request.
1066 * Send a large command, one that is too large for some reason to
1067 * send using the GLXRender protocol request. One reason to send
1068 * a large command is to avoid copying the data.
1070 * \param ctx GLX context
1071 * \param header Header data.
1072 * \param headerLen Size, in bytes, of the header data. It is assumed that
1073 * the header data will always be small enough to fit in
1074 * a single X protocol packet.
1075 * \param data Command data.
1076 * \param dataLen Size, in bytes, of the command data.
1079 __glXSendLargeCommand(struct glx_context
* ctx
,
1080 const GLvoid
* header
, GLint headerLen
,
1081 const GLvoid
* data
, GLint dataLen
)
1084 GLint totalRequests
, requestNumber
;
1087 ** Calculate the maximum amount of data can be stuffed into a single
1088 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1089 ** packet size minus sz_xGLXRenderReq.
1091 maxSize
= (ctx
->bufSize
+ sz_xGLXRenderReq
) - sz_xGLXRenderLargeReq
;
1092 totalRequests
= 1 + (dataLen
/ maxSize
);
1093 if (dataLen
% maxSize
)
1097 ** Send all of the command, except the large array, as one request.
1099 assert(headerLen
<= maxSize
);
1100 __glXSendLargeChunk(ctx
, 1, totalRequests
, header
, headerLen
);
1103 ** Send enough requests until the whole array is sent.
1105 for (requestNumber
= 2; requestNumber
<= (totalRequests
- 1);
1107 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, maxSize
);
1108 data
= (const GLvoid
*) (((const GLubyte
*) data
) + maxSize
);
1110 assert(dataLen
> 0);
1113 assert(dataLen
<= maxSize
);
1114 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, dataLen
);
1117 /************************************************************************/
1121 __glXDumpDrawBuffer(struct glx_context
* ctx
)
1123 GLubyte
*p
= ctx
->buf
;
1124 GLubyte
*end
= ctx
->pc
;
1125 GLushort opcode
, length
;
1129 opcode
= *((GLushort
*) p
);
1130 length
= *((GLushort
*) (p
+ 2));
1131 printf("%2x: %5d: ", opcode
, length
);
1134 while (length
> 0) {
1135 printf("%08x ", *((unsigned *) p
));