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"
54 #include <X11/Xlib-xcb.h>
60 void __glXDumpDrawBuffer(struct glx_context
* ctx
);
64 ** You can set this cell to 1 to force the gl drawing stuff to be
65 ** one command per packet
67 _X_HIDDEN
int __glXDebug
= 0;
69 /* Extension required boiler plate */
71 static const char __glXExtensionName
[] = GLX_EXTENSION_NAME
;
72 static struct glx_display
*glx_displays
;
74 static /* const */ char *error_list
[] = {
80 "GLXBadCurrentWindow",
81 "GLXBadRenderRequest",
83 "GLXUnsupportedPrivateRequest",
86 "GLXBadCurrentDrawable",
91 #ifdef GLX_USE_APPLEGL
92 static char *__glXErrorString(Display
*dpy
, int code
, XExtCodes
*codes
,
97 XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
98 __GLX_NUMBER_ERRORS
, error_list
)
101 * GLX events are a bit funky. We don't stuff the X event code into
102 * our user exposed (via XNextEvent) structure. Instead we use the GLX
103 * private event code namespace (and hope it doesn't conflict). Clients
104 * have to know that bit 15 in the event type field means they're getting
105 * a GLX event, and then handle the various sub-event types there, rather
106 * than simply checking the event code and handling it directly.
110 __glXWireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
112 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
117 switch ((wire
->u
.u
.type
& 0x7f) - glx_dpy
->codes
->first_event
) {
118 case GLX_PbufferClobber
:
120 GLXPbufferClobberEvent
*aevent
= (GLXPbufferClobberEvent
*)event
;
121 xGLXPbufferClobberEvent
*awire
= (xGLXPbufferClobberEvent
*)wire
;
122 aevent
->event_type
= awire
->type
;
123 aevent
->serial
= awire
->sequenceNumber
;
124 aevent
->event_type
= awire
->event_type
;
125 aevent
->draw_type
= awire
->draw_type
;
126 aevent
->drawable
= awire
->drawable
;
127 aevent
->buffer_mask
= awire
->buffer_mask
;
128 aevent
->aux_buffer
= awire
->aux_buffer
;
129 aevent
->x
= awire
->x
;
130 aevent
->y
= awire
->y
;
131 aevent
->width
= awire
->width
;
132 aevent
->height
= awire
->height
;
133 aevent
->count
= awire
->count
;
136 case GLX_BufferSwapComplete
:
138 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
139 xGLXBufferSwapComplete2
*awire
= (xGLXBufferSwapComplete2
*)wire
;
140 struct glx_drawable
*glxDraw
= GetGLXDrawable(dpy
, awire
->drawable
);
145 aevent
->serial
= _XSetLastRequestRead(dpy
, (xGenericReply
*) wire
);
146 aevent
->send_event
= (awire
->type
& 0x80) != 0;
147 aevent
->display
= dpy
;
148 aevent
->event_type
= awire
->event_type
;
149 aevent
->drawable
= glxDraw
->xDrawable
;
150 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
151 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
153 /* Handle 32-Bit wire sbc wraparound in both directions to cope with out
154 * of sequence 64-Bit sbc's
156 if ((int64_t) awire
->sbc
< ((int64_t) glxDraw
->lastEventSbc
- 0x40000000))
157 glxDraw
->eventSbcWrap
+= 0x100000000;
158 if ((int64_t) awire
->sbc
> ((int64_t) glxDraw
->lastEventSbc
+ 0x40000000))
159 glxDraw
->eventSbcWrap
-= 0x100000000;
160 glxDraw
->lastEventSbc
= awire
->sbc
;
161 aevent
->sbc
= awire
->sbc
+ glxDraw
->eventSbcWrap
;
165 /* client doesn't support server event */
172 /* We don't actually support this. It doesn't make sense for clients to
173 * send each other GLX events.
176 __glXEventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
178 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
183 switch (event
->type
) {
188 case GLX_EXCHANGE_COMPLETE_INTEL
:
190 case GLX_COPY_COMPLETE_INTEL
:
192 case GLX_FLIP_COMPLETE_INTEL
:
195 /* client doesn't support server event */
202 /************************************************************************/
204 ** Free the per screen configs data as well as the array of
205 ** __glXScreenConfigs.
208 FreeScreenConfigs(struct glx_display
* priv
)
210 struct glx_screen
*psc
;
213 /* Free screen configuration information */
214 screens
= ScreenCount(priv
->dpy
);
215 for (i
= 0; i
< screens
; i
++) {
216 psc
= priv
->screens
[i
];
217 glx_screen_cleanup(psc
);
219 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
220 if (psc
->driScreen
) {
221 psc
->driScreen
->destroyScreen(psc
);
229 free((char *) priv
->screens
);
230 priv
->screens
= NULL
;
234 glx_display_free(struct glx_display
*priv
)
236 struct glx_context
*gc
;
238 gc
= __glXGetCurrentContext();
239 if (priv
->dpy
== gc
->currentDpy
) {
240 gc
->vtable
->destroy(gc
);
241 __glXSetCurrentContextNull();
244 FreeScreenConfigs(priv
);
245 free((char *) priv
->serverGLXvendor
);
246 free((char *) priv
->serverGLXversion
);
248 __glxHashDestroy(priv
->glXDrawHash
);
250 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
251 __glxHashDestroy(priv
->drawHash
);
253 /* Free the direct rendering per display data */
254 if (priv
->driswDisplay
)
255 (*priv
->driswDisplay
->destroyDisplay
) (priv
->driswDisplay
);
256 priv
->driswDisplay
= NULL
;
258 #if defined (GLX_USE_DRM)
259 if (priv
->driDisplay
)
260 (*priv
->driDisplay
->destroyDisplay
) (priv
->driDisplay
);
261 priv
->driDisplay
= NULL
;
263 if (priv
->dri2Display
)
264 (*priv
->dri2Display
->destroyDisplay
) (priv
->dri2Display
);
265 priv
->dri2Display
= NULL
;
267 if (priv
->dri3Display
)
268 (*priv
->dri3Display
->destroyDisplay
) (priv
->dri3Display
);
269 priv
->dri3Display
= NULL
;
270 #endif /* GLX_USE_DRM */
272 #if defined(GLX_USE_WINDOWSGL)
273 if (priv
->windowsdriDisplay
)
274 (*priv
->windowsdriDisplay
->destroyDisplay
) (priv
->windowsdriDisplay
);
275 priv
->windowsdriDisplay
= NULL
;
276 #endif /* GLX_USE_WINDOWSGL */
278 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
284 __glXCloseDisplay(Display
* dpy
, XExtCodes
* codes
)
286 struct glx_display
*priv
, **prev
;
288 _XLockMutex(_Xglobal_lock
);
289 prev
= &glx_displays
;
290 for (priv
= glx_displays
; priv
; prev
= &priv
->next
, priv
= priv
->next
) {
291 if (priv
->dpy
== dpy
) {
296 _XUnlockMutex(_Xglobal_lock
);
299 glx_display_free(priv
);
305 ** Query the version of the GLX extension. This procedure works even if
306 ** the client extension is not completely set up.
309 QueryVersion(Display
* dpy
, int opcode
, int *major
, int *minor
)
311 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
312 xcb_glx_query_version_reply_t
*reply
= xcb_glx_query_version_reply(c
,
313 xcb_glx_query_version
322 if (reply
->major_version
!= GLX_MAJOR_VERSION
) {
326 *major
= reply
->major_version
;
327 *minor
= min(reply
->minor_version
, GLX_MINOR_VERSION
);
333 * We don't want to enable this GLX_OML_swap_method in glxext.h,
334 * because we can't support it. The X server writes it out though,
335 * so we should handle it somehow, to avoid false warnings.
338 IGNORE_GLX_SWAP_METHOD_OML
= 0x8060
343 convert_from_x_visual_type(int visualType
)
345 static const int glx_visual_types
[] = {
346 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
347 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
348 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
351 if (visualType
< ARRAY_SIZE(glx_visual_types
))
352 return glx_visual_types
[visualType
];
358 * getVisualConfigs uses the !tagged_only path.
359 * getFBConfigs uses the tagged_only path.
362 __glXInitializeVisualConfigFromTags(struct glx_config
* config
, int count
,
363 const INT32
* bp
, Bool tagged_only
,
364 Bool fbconfig_style_tags
)
367 GLint renderType
= 0;
370 /* Copy in the first set of properties */
371 config
->visualID
= *bp
++;
373 config
->visualType
= convert_from_x_visual_type(*bp
++);
375 config
->rgbMode
= *bp
++;
377 config
->redBits
= *bp
++;
378 config
->greenBits
= *bp
++;
379 config
->blueBits
= *bp
++;
380 config
->alphaBits
= *bp
++;
381 config
->accumRedBits
= *bp
++;
382 config
->accumGreenBits
= *bp
++;
383 config
->accumBlueBits
= *bp
++;
384 config
->accumAlphaBits
= *bp
++;
386 config
->doubleBufferMode
= *bp
++;
387 config
->stereoMode
= *bp
++;
389 config
->rgbBits
= *bp
++;
390 config
->depthBits
= *bp
++;
391 config
->stencilBits
= *bp
++;
392 config
->numAuxBuffers
= *bp
++;
393 config
->level
= *bp
++;
395 #ifdef GLX_USE_APPLEGL
396 /* AppleSGLX supports pixmap and pbuffers with all config. */
397 config
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
398 /* Unfortunately this can create an ABI compatibility problem. */
401 count
-= __GLX_MIN_CONFIG_PROPS
;
405 config
->sRGBCapable
= GL_FALSE
;
408 ** Additional properties may be in a list at the end
409 ** of the reply. They are in pairs of property type
410 ** and property value.
413 #define FETCH_OR_SET(tag) \
414 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
416 for (i
= 0; i
< count
; i
+= 2) {
417 long int tag
= *bp
++;
421 FETCH_OR_SET(rgbMode
);
423 case GLX_BUFFER_SIZE
:
424 config
->rgbBits
= *bp
++;
427 config
->level
= *bp
++;
429 case GLX_DOUBLEBUFFER
:
430 FETCH_OR_SET(doubleBufferMode
);
433 FETCH_OR_SET(stereoMode
);
435 case GLX_AUX_BUFFERS
:
436 config
->numAuxBuffers
= *bp
++;
439 config
->redBits
= *bp
++;
442 config
->greenBits
= *bp
++;
445 config
->blueBits
= *bp
++;
448 config
->alphaBits
= *bp
++;
451 config
->depthBits
= *bp
++;
453 case GLX_STENCIL_SIZE
:
454 config
->stencilBits
= *bp
++;
456 case GLX_ACCUM_RED_SIZE
:
457 config
->accumRedBits
= *bp
++;
459 case GLX_ACCUM_GREEN_SIZE
:
460 config
->accumGreenBits
= *bp
++;
462 case GLX_ACCUM_BLUE_SIZE
:
463 config
->accumBlueBits
= *bp
++;
465 case GLX_ACCUM_ALPHA_SIZE
:
466 config
->accumAlphaBits
= *bp
++;
468 case GLX_VISUAL_CAVEAT_EXT
:
469 config
->visualRating
= *bp
++;
471 case GLX_X_VISUAL_TYPE
:
472 config
->visualType
= *bp
++;
474 case GLX_TRANSPARENT_TYPE
:
475 config
->transparentPixel
= *bp
++;
477 case GLX_TRANSPARENT_INDEX_VALUE
:
478 config
->transparentIndex
= *bp
++;
480 case GLX_TRANSPARENT_RED_VALUE
:
481 config
->transparentRed
= *bp
++;
483 case GLX_TRANSPARENT_GREEN_VALUE
:
484 config
->transparentGreen
= *bp
++;
486 case GLX_TRANSPARENT_BLUE_VALUE
:
487 config
->transparentBlue
= *bp
++;
489 case GLX_TRANSPARENT_ALPHA_VALUE
:
490 config
->transparentAlpha
= *bp
++;
493 config
->visualID
= *bp
++;
495 case GLX_DRAWABLE_TYPE
:
496 config
->drawableType
= *bp
++;
497 #ifdef GLX_USE_APPLEGL
498 /* AppleSGLX supports pixmap and pbuffers with all config. */
499 config
->drawableType
|= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
502 case GLX_RENDER_TYPE
: /* fbconfig render type bits */
505 case GLX_X_RENDERABLE
:
506 config
->xRenderable
= *bp
++;
508 case GLX_FBCONFIG_ID
:
509 config
->fbconfigID
= *bp
++;
511 case GLX_MAX_PBUFFER_WIDTH
:
512 config
->maxPbufferWidth
= *bp
++;
514 case GLX_MAX_PBUFFER_HEIGHT
:
515 config
->maxPbufferHeight
= *bp
++;
517 case GLX_MAX_PBUFFER_PIXELS
:
518 config
->maxPbufferPixels
= *bp
++;
520 #ifndef GLX_USE_APPLEGL
521 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
522 config
->optimalPbufferWidth
= *bp
++;
524 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
525 config
->optimalPbufferHeight
= *bp
++;
527 case GLX_VISUAL_SELECT_GROUP_SGIX
:
528 config
->visualSelectGroup
= *bp
++;
530 case GLX_SWAP_METHOD_OML
:
531 if (*bp
== GLX_SWAP_UNDEFINED_OML
||
532 *bp
== GLX_SWAP_COPY_OML
||
533 *bp
== GLX_SWAP_EXCHANGE_OML
) {
534 config
->swapMethod
= *bp
++;
536 /* X servers with old HW drivers may return any value here, so
537 * assume GLX_SWAP_METHOD_UNDEFINED.
539 config
->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
544 case GLX_SAMPLE_BUFFERS_SGIS
:
545 config
->sampleBuffers
= *bp
++;
547 case GLX_SAMPLES_SGIS
:
548 config
->samples
= *bp
++;
550 #ifdef GLX_USE_APPLEGL
551 case IGNORE_GLX_SWAP_METHOD_OML
:
552 /* We ignore this tag. See the comment above this function. */
556 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
557 config
->bindToTextureRgb
= *bp
++;
559 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
560 config
->bindToTextureRgba
= *bp
++;
562 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
563 config
->bindToMipmapTexture
= *bp
++;
565 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
566 config
->bindToTextureTargets
= *bp
++;
568 case GLX_Y_INVERTED_EXT
:
569 config
->yInverted
= *bp
++;
572 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT
:
573 config
->sRGBCapable
= *bp
++;
577 if (fbconfig_style_tags
)
584 if(env_var_as_boolean("LIBGL_DIAGNOSTIC", false)) {
585 long int tagvalue
= *bp
++;
586 fprintf(stderr
, "WARNING: unknown GLX tag from server: "
587 "tag 0x%lx value 0x%lx\n", tag
, tagvalue
);
589 /* Ignore the unrecognized tag's value */
596 if (renderType
!= 0 && renderType
!= GLX_DONT_CARE
) {
597 config
->renderType
= renderType
;
598 config
->floatMode
= (renderType
&
599 (GLX_RGBA_FLOAT_BIT_ARB
|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT
)) != 0;
601 /* If there wasn't GLX_RENDER_TYPE property, set it based on
602 * config->rgbMode. The only way to communicate that the config is
603 * floating-point is via GLX_RENDER_TYPE, so this cannot be a float
607 (config
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
610 /* The GLX_ARB_fbconfig_float spec says:
612 * "Note that floating point rendering is only supported for
613 * GLXPbuffer drawables."
615 if (config
->floatMode
)
616 config
->drawableType
&= GLX_PBUFFER_BIT
;
619 static struct glx_config
*
620 createConfigsFromProperties(Display
* dpy
, int nvisuals
, int nprops
,
621 int screen
, GLboolean tagged_only
)
623 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
625 struct glx_config
*modes
, *m
;
631 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
633 /* Check number of properties */
634 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
637 /* Allocate memory for our config structure */
638 modes
= glx_config_create_list(nvisuals
);
642 prop_size
= nprops
* __GLX_SIZE_INT32
;
643 if (prop_size
<= sizeof(buf
))
646 props
= malloc(prop_size
);
648 /* Read each config structure and convert it into our format */
650 for (i
= 0; i
< nvisuals
; i
++) {
651 _XRead(dpy
, (char *) props
, prop_size
);
652 #ifdef GLX_USE_APPLEGL
653 /* Older X servers don't send this so we default it here. */
654 m
->drawableType
= GLX_WINDOW_BIT
;
657 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
658 * set the proper bits here.
659 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
661 m
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
663 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
664 tagged_only
, GL_TRUE
);
676 getVisualConfigs(struct glx_screen
*psc
,
677 struct glx_display
*priv
, int screen
)
679 xGLXGetVisualConfigsReq
*req
;
680 xGLXGetVisualConfigsReply reply
;
681 Display
*dpy
= priv
->dpy
;
686 GetReq(GLXGetVisualConfigs
, req
);
687 req
->reqType
= priv
->majorOpcode
;
688 req
->glxCode
= X_GLXGetVisualConfigs
;
689 req
->screen
= screen
;
691 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
694 psc
->visuals
= createConfigsFromProperties(dpy
,
701 return psc
->visuals
!= NULL
;
705 getFBConfigs(struct glx_screen
*psc
, struct glx_display
*priv
, int screen
)
707 xGLXGetFBConfigsReq
*fb_req
;
708 xGLXGetFBConfigsSGIXReq
*sgi_req
;
709 xGLXVendorPrivateWithReplyReq
*vpreq
;
710 xGLXGetFBConfigsReply reply
;
711 Display
*dpy
= priv
->dpy
;
714 __glXQueryServerString(dpy
, priv
->majorOpcode
, screen
, GLX_EXTENSIONS
);
716 if (psc
->serverGLXexts
== NULL
) {
723 if (atof(priv
->serverGLXversion
) >= 1.3) {
724 GetReq(GLXGetFBConfigs
, fb_req
);
725 fb_req
->reqType
= priv
->majorOpcode
;
726 fb_req
->glxCode
= X_GLXGetFBConfigs
;
727 fb_req
->screen
= screen
;
729 else if (strstr(psc
->serverGLXexts
, "GLX_SGIX_fbconfig") != NULL
) {
730 GetReqExtra(GLXVendorPrivateWithReply
,
731 sz_xGLXGetFBConfigsSGIXReq
-
732 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
733 sgi_req
= (xGLXGetFBConfigsSGIXReq
*) vpreq
;
734 sgi_req
->reqType
= priv
->majorOpcode
;
735 sgi_req
->glxCode
= X_GLXVendorPrivateWithReply
;
736 sgi_req
->vendorCode
= X_GLXvop_GetFBConfigsSGIX
;
737 sgi_req
->screen
= screen
;
742 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
745 psc
->configs
= createConfigsFromProperties(dpy
,
747 reply
.numAttribs
* 2,
752 return psc
->configs
!= NULL
;
756 glx_screen_init(struct glx_screen
*psc
,
757 int screen
, struct glx_display
* priv
)
759 /* Initialize per screen dynamic client GLX extensions */
760 psc
->ext_list_first_time
= GL_TRUE
;
762 psc
->dpy
= priv
->dpy
;
765 if (!getVisualConfigs(psc
, priv
, screen
))
768 if (!getFBConfigs(psc
, priv
, screen
))
775 glx_screen_cleanup(struct glx_screen
*psc
)
778 glx_config_destroy_list(psc
->configs
);
779 free(psc
->effectiveGLXexts
);
780 psc
->configs
= NULL
; /* NOTE: just for paranoia */
783 glx_config_destroy_list(psc
->visuals
);
784 psc
->visuals
= NULL
; /* NOTE: just for paranoia */
786 free((char *) psc
->serverGLXexts
);
790 ** Allocate the memory for the per screen configs for each screen.
791 ** If that works then fetch the per screen configs data.
794 AllocAndFetchScreenConfigs(Display
* dpy
, struct glx_display
* priv
)
796 struct glx_screen
*psc
;
800 ** First allocate memory for the array of per screen configs.
802 screens
= ScreenCount(dpy
);
803 priv
->screens
= malloc(screens
* sizeof *priv
->screens
);
807 priv
->serverGLXversion
=
808 __glXQueryServerString(dpy
, priv
->majorOpcode
, 0, GLX_VERSION
);
809 if (priv
->serverGLXversion
== NULL
) {
810 FreeScreenConfigs(priv
);
814 for (i
= 0; i
< screens
; i
++, psc
++) {
816 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
817 #if defined(GLX_USE_DRM)
818 #if defined(HAVE_DRI3)
819 if (priv
->dri3Display
)
820 psc
= (*priv
->dri3Display
->createScreen
) (i
, priv
);
821 #endif /* HAVE_DRI3 */
822 if (psc
== NULL
&& priv
->dri2Display
)
823 psc
= (*priv
->dri2Display
->createScreen
) (i
, priv
);
824 if (psc
== NULL
&& priv
->driDisplay
)
825 psc
= (*priv
->driDisplay
->createScreen
) (i
, priv
);
826 #endif /* GLX_USE_DRM */
828 #ifdef GLX_USE_WINDOWSGL
829 if (psc
== NULL
&& priv
->windowsdriDisplay
)
830 psc
= (*priv
->windowsdriDisplay
->createScreen
) (i
, priv
);
833 if (psc
== NULL
&& priv
->driswDisplay
)
834 psc
= (*priv
->driswDisplay
->createScreen
) (i
, priv
);
835 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
837 #if defined(GLX_USE_APPLEGL)
839 psc
= applegl_create_screen(i
, priv
);
842 psc
= indirect_create_screen(i
, priv
);
844 priv
->screens
[i
] = psc
;
851 ** Initialize the client side extension code.
853 _X_HIDDEN
struct glx_display
*
854 __glXInitialize(Display
* dpy
)
856 struct glx_display
*dpyPriv
, *d
;
857 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
858 Bool glx_direct
, glx_accel
;
862 _XLockMutex(_Xglobal_lock
);
864 for (dpyPriv
= glx_displays
; dpyPriv
; dpyPriv
= dpyPriv
->next
) {
865 if (dpyPriv
->dpy
== dpy
) {
866 _XUnlockMutex(_Xglobal_lock
);
871 /* Drop the lock while we create the display private. */
872 _XUnlockMutex(_Xglobal_lock
);
874 dpyPriv
= calloc(1, sizeof *dpyPriv
);
878 dpyPriv
->codes
= XInitExtension(dpy
, __glXExtensionName
);
879 if (!dpyPriv
->codes
) {
885 dpyPriv
->majorOpcode
= dpyPriv
->codes
->major_opcode
;
886 dpyPriv
->serverGLXvendor
= 0x0;
887 dpyPriv
->serverGLXversion
= 0x0;
889 /* See if the versions are compatible. This GLX implementation does not
890 * work with servers that only support GLX 1.0.
892 if (!QueryVersion(dpy
, dpyPriv
->majorOpcode
,
893 &dpyPriv
->majorVersion
, &dpyPriv
->minorVersion
)
894 || (dpyPriv
->majorVersion
== 1 && dpyPriv
->minorVersion
< 1)) {
899 for (i
= 0; i
< __GLX_NUMBER_EVENTS
; i
++) {
900 XESetWireToEvent(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXWireToEvent
);
901 XESetEventToWire(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXEventToWire
);
904 XESetCloseDisplay(dpy
, dpyPriv
->codes
->extension
, __glXCloseDisplay
);
905 XESetErrorString (dpy
, dpyPriv
->codes
->extension
,__glXErrorString
);
907 dpyPriv
->glXDrawHash
= __glxHashCreate();
909 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
910 glx_direct
= !env_var_as_boolean("LIBGL_ALWAYS_INDIRECT", false);
911 glx_accel
= !env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false);
913 dpyPriv
->drawHash
= __glxHashCreate();
916 ** Initialize the direct rendering per display data and functions.
917 ** Note: This _must_ be done before calling any other DRI routines
918 ** (e.g., those called in AllocAndFetchScreenConfigs).
920 #if defined(GLX_USE_DRM)
921 if (glx_direct
&& glx_accel
) {
922 #if defined(HAVE_DRI3)
923 if (!env_var_as_boolean("LIBGL_DRI3_DISABLE", false))
924 dpyPriv
->dri3Display
= dri3_create_display(dpy
);
925 #endif /* HAVE_DRI3 */
926 dpyPriv
->dri2Display
= dri2CreateDisplay(dpy
);
927 dpyPriv
->driDisplay
= driCreateDisplay(dpy
);
929 #endif /* GLX_USE_DRM */
931 dpyPriv
->driswDisplay
= driswCreateDisplay(dpy
);
932 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
934 #ifdef GLX_USE_APPLEGL
935 if (!applegl_create_display(dpyPriv
)) {
941 #ifdef GLX_USE_WINDOWSGL
942 if (glx_direct
&& glx_accel
)
943 dpyPriv
->windowsdriDisplay
= driwindowsCreateDisplay(dpy
);
946 if (!AllocAndFetchScreenConfigs(dpy
, dpyPriv
)) {
951 __glX_send_client_info(dpyPriv
);
953 /* Grab the lock again and add the dispay private, unless somebody
954 * beat us to initializing on this display in the meantime. */
955 _XLockMutex(_Xglobal_lock
);
957 for (d
= glx_displays
; d
; d
= d
->next
) {
959 _XUnlockMutex(_Xglobal_lock
);
960 glx_display_free(dpyPriv
);
965 dpyPriv
->next
= glx_displays
;
966 glx_displays
= dpyPriv
;
968 _XUnlockMutex(_Xglobal_lock
);
974 ** Setup for sending a GLX command on dpy. Make sure the extension is
975 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
978 __glXSetupForCommand(Display
* dpy
)
980 struct glx_context
*gc
;
981 struct glx_display
*priv
;
983 /* If this thread has a current context, flush its rendering commands */
984 gc
= __glXGetCurrentContext();
985 if (gc
->currentDpy
) {
986 /* Flush rendering buffer of the current context, if any */
987 (void) __glXFlushRenderBuffer(gc
, gc
->pc
);
989 if (gc
->currentDpy
== dpy
) {
990 /* Use opcode from gc because its right */
991 return gc
->majorOpcode
;
995 ** Have to get info about argument dpy because it might be to
996 ** a different server
1001 /* Forced to lookup extension via the slow initialize route */
1002 priv
= __glXInitialize(dpy
);
1006 return priv
->majorOpcode
;
1010 * Flush the drawing command transport buffer.
1012 * \param ctx Context whose transport buffer is to be flushed.
1013 * \param pc Pointer to first unused buffer location.
1016 * Modify this function to use \c ctx->pc instead of the explicit
1020 __glXFlushRenderBuffer(struct glx_context
* ctx
, GLubyte
* pc
)
1022 Display
*const dpy
= ctx
->currentDpy
;
1023 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1024 const GLint size
= pc
- ctx
->buf
;
1026 if ((dpy
!= NULL
) && (size
> 0)) {
1027 xcb_glx_render(c
, ctx
->currentContextTag
, size
,
1028 (const uint8_t *) ctx
->buf
);
1031 /* Reset pointer and return it */
1038 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1039 * this function over \c __glXSendLargeCommand is that callers can use the
1040 * data buffer in the GLX context and may be able to avoid allocating an
1041 * extra buffer. The disadvantage is the clients will have to do more
1042 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1044 * \sa __glXSendLargeCommand
1046 * \param gc GLX context
1047 * \param requestNumber Which part of the whole command is this? The first
1049 * \param totalRequests How many requests will there be?
1050 * \param data Command data.
1051 * \param dataLen Size, in bytes, of the command data.
1054 __glXSendLargeChunk(struct glx_context
* gc
, GLint requestNumber
,
1055 GLint totalRequests
, const GLvoid
* data
, GLint dataLen
)
1057 Display
*dpy
= gc
->currentDpy
;
1058 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1059 xcb_glx_render_large(c
, gc
->currentContextTag
, requestNumber
,
1060 totalRequests
, dataLen
, data
);
1065 * Send a command that is too large for the GLXRender protocol request.
1067 * Send a large command, one that is too large for some reason to
1068 * send using the GLXRender protocol request. One reason to send
1069 * a large command is to avoid copying the data.
1071 * \param ctx GLX context
1072 * \param header Header data.
1073 * \param headerLen Size, in bytes, of the header data. It is assumed that
1074 * the header data will always be small enough to fit in
1075 * a single X protocol packet.
1076 * \param data Command data.
1077 * \param dataLen Size, in bytes, of the command data.
1080 __glXSendLargeCommand(struct glx_context
* ctx
,
1081 const GLvoid
* header
, GLint headerLen
,
1082 const GLvoid
* data
, GLint dataLen
)
1085 GLint totalRequests
, requestNumber
;
1088 ** Calculate the maximum amount of data can be stuffed into a single
1089 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1090 ** packet size minus sz_xGLXRenderReq.
1092 maxSize
= (ctx
->bufSize
+ sz_xGLXRenderReq
) - sz_xGLXRenderLargeReq
;
1093 totalRequests
= 1 + (dataLen
/ maxSize
);
1094 if (dataLen
% maxSize
)
1098 ** Send all of the command, except the large array, as one request.
1100 assert(headerLen
<= maxSize
);
1101 __glXSendLargeChunk(ctx
, 1, totalRequests
, header
, headerLen
);
1104 ** Send enough requests until the whole array is sent.
1106 for (requestNumber
= 2; requestNumber
<= (totalRequests
- 1);
1108 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, maxSize
);
1109 data
= (const GLvoid
*) (((const GLubyte
*) data
) + maxSize
);
1111 assert(dataLen
> 0);
1114 assert(dataLen
<= maxSize
);
1115 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, dataLen
);
1118 /************************************************************************/
1122 __glXDumpDrawBuffer(struct glx_context
* ctx
)
1124 GLubyte
*p
= ctx
->buf
;
1125 GLubyte
*end
= ctx
->pc
;
1126 GLushort opcode
, length
;
1130 opcode
= *((GLushort
*) p
);
1131 length
= *((GLushort
*) (p
+ 2));
1132 printf("%2x: %5d: ", opcode
, length
);
1135 while (length
> 0) {
1136 printf("%08x ", *((unsigned *) p
));