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 #include <X11/extensions/dri2proto.h>
45 #ifdef GLX_USE_APPLEGL
46 #include "apple_glx.h"
47 #include "apple_visual.h"
49 #include "glxextensions.h"
52 #include <X11/Xlib-xcb.h>
59 void __glXDumpDrawBuffer(struct glx_context
* ctx
);
63 ** You can set this cell to 1 to force the gl drawing stuff to be
64 ** one command per packet
66 _X_HIDDEN
int __glXDebug
= 0;
68 /* Extension required boiler plate */
70 static const char __glXExtensionName
[] = GLX_EXTENSION_NAME
;
71 static struct glx_display
*glx_displays
;
73 static /* const */ char *error_list
[] = {
79 "GLXBadCurrentWindow",
80 "GLXBadRenderRequest",
82 "GLXUnsupportedPrivateRequest",
85 "GLXBadCurrentDrawable",
89 #ifdef GLX_USE_APPLEGL
90 static char *__glXErrorString(Display
*dpy
, int code
, XExtCodes
*codes
,
95 XEXT_GENERATE_ERROR_STRING(__glXErrorString
, __glXExtensionName
,
96 __GLX_NUMBER_ERRORS
, error_list
)
99 * GLX events are a bit funky. We don't stuff the X event code into
100 * our user exposed (via XNextEvent) structure. Instead we use the GLX
101 * private event code namespace (and hope it doesn't conflict). Clients
102 * have to know that bit 15 in the event type field means they're getting
103 * a GLX event, and then handle the various sub-event types there, rather
104 * than simply checking the event code and handling it directly.
108 __glXWireToEvent(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
110 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
115 switch ((wire
->u
.u
.type
& 0x7f) - glx_dpy
->codes
->first_event
) {
116 case GLX_PbufferClobber
:
118 GLXPbufferClobberEvent
*aevent
= (GLXPbufferClobberEvent
*)event
;
119 xGLXPbufferClobberEvent
*awire
= (xGLXPbufferClobberEvent
*)wire
;
120 aevent
->event_type
= awire
->type
;
121 aevent
->serial
= awire
->sequenceNumber
;
122 aevent
->event_type
= awire
->event_type
;
123 aevent
->draw_type
= awire
->draw_type
;
124 aevent
->drawable
= awire
->drawable
;
125 aevent
->buffer_mask
= awire
->buffer_mask
;
126 aevent
->aux_buffer
= awire
->aux_buffer
;
127 aevent
->x
= awire
->x
;
128 aevent
->y
= awire
->y
;
129 aevent
->width
= awire
->width
;
130 aevent
->height
= awire
->height
;
131 aevent
->count
= awire
->count
;
134 /* No easy symbol to test for this, as GLX_BufferSwapComplete is
135 * defined in the local glx.h header, but the
136 * xGLXBufferSwapComplete typedef is only available in new versions
137 * of the external glxproto.h header, which doesn't have any
138 * testable versioning define.
140 * I'll use the related DRI2 define, in the hope that we won't
141 * receive these events unless we know how to ask for them:
143 #ifdef X_DRI2SwapBuffers
144 case GLX_BufferSwapComplete
:
146 GLXBufferSwapComplete
*aevent
= (GLXBufferSwapComplete
*)event
;
147 xGLXBufferSwapComplete
*awire
= (xGLXBufferSwapComplete
*)wire
;
148 aevent
->event_type
= awire
->event_type
;
149 aevent
->drawable
= awire
->drawable
;
150 aevent
->ust
= ((CARD64
)awire
->ust_hi
<< 32) | awire
->ust_lo
;
151 aevent
->msc
= ((CARD64
)awire
->msc_hi
<< 32) | awire
->msc_lo
;
152 aevent
->sbc
= ((CARD64
)awire
->sbc_hi
<< 32) | awire
->sbc_lo
;
157 /* client doesn't support server event */
164 /* We don't actually support this. It doesn't make sense for clients to
165 * send each other GLX events.
168 __glXEventToWire(Display
*dpy
, XEvent
*event
, xEvent
*wire
)
170 struct glx_display
*glx_dpy
= __glXInitialize(dpy
);
175 switch (event
->type
) {
180 case GLX_EXCHANGE_COMPLETE_INTEL
:
182 case GLX_COPY_COMPLETE_INTEL
:
184 case GLX_FLIP_COMPLETE_INTEL
:
187 /* client doesn't support server event */
194 /************************************************************************/
196 ** Free the per screen configs data as well as the array of
197 ** __glXScreenConfigs.
200 FreeScreenConfigs(struct glx_display
* priv
)
202 struct glx_screen
*psc
;
205 /* Free screen configuration information */
206 screens
= ScreenCount(priv
->dpy
);
207 for (i
= 0; i
< screens
; i
++) {
208 psc
= priv
->screens
[i
];
210 glx_config_destroy_list(psc
->configs
);
211 if (psc
->effectiveGLXexts
)
212 Xfree(psc
->effectiveGLXexts
);
213 psc
->configs
= NULL
; /* NOTE: just for paranoia */
216 glx_config_destroy_list(psc
->visuals
);
217 psc
->visuals
= NULL
; /* NOTE: just for paranoia */
219 Xfree((char *) psc
->serverGLXexts
);
221 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
222 if (psc
->driScreen
) {
223 psc
->driScreen
->destroyScreen(psc
);
231 XFree((char *) priv
->screens
);
232 priv
->screens
= NULL
;
236 glx_display_free(struct glx_display
*priv
)
238 struct glx_context
*gc
;
240 gc
= __glXGetCurrentContext();
241 if (priv
->dpy
== gc
->currentDpy
) {
242 gc
->vtable
->destroy(gc
);
243 __glXSetCurrentContextNull();
246 FreeScreenConfigs(priv
);
247 if (priv
->serverGLXvendor
)
248 Xfree((char *) priv
->serverGLXvendor
);
249 if (priv
->serverGLXversion
)
250 Xfree((char *) priv
->serverGLXversion
);
252 __glxHashDestroy(priv
->drawHash
);
254 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
255 /* Free the direct rendering per display data */
256 if (priv
->driswDisplay
)
257 (*priv
->driswDisplay
->destroyDisplay
) (priv
->driswDisplay
);
258 priv
->driswDisplay
= NULL
;
260 if (priv
->driDisplay
)
261 (*priv
->driDisplay
->destroyDisplay
) (priv
->driDisplay
);
262 priv
->driDisplay
= NULL
;
264 if (priv
->dri2Display
)
265 (*priv
->dri2Display
->destroyDisplay
) (priv
->dri2Display
);
266 priv
->dri2Display
= NULL
;
269 Xfree((char *) priv
);
273 __glXCloseDisplay(Display
* dpy
, XExtCodes
* codes
)
275 struct glx_display
*priv
, **prev
;
277 _XLockMutex(_Xglobal_lock
);
278 prev
= &glx_displays
;
279 for (priv
= glx_displays
; priv
; prev
= &priv
->next
, priv
= priv
->next
) {
280 if (priv
->dpy
== dpy
) {
281 (*prev
) = priv
->next
;
285 _XUnlockMutex(_Xglobal_lock
);
287 glx_display_free(priv
);
293 ** Query the version of the GLX extension. This procedure works even if
294 ** the client extension is not completely set up.
297 QueryVersion(Display
* dpy
, int opcode
, int *major
, int *minor
)
300 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
301 xcb_glx_query_version_reply_t
*reply
= xcb_glx_query_version_reply(c
,
302 xcb_glx_query_version
308 if (reply
->major_version
!= GLX_MAJOR_VERSION
) {
312 *major
= reply
->major_version
;
313 *minor
= min(reply
->minor_version
, GLX_MINOR_VERSION
);
317 xGLXQueryVersionReq
*req
;
318 xGLXQueryVersionReply reply
;
320 /* Send the glXQueryVersion request */
322 GetReq(GLXQueryVersion
, req
);
323 req
->reqType
= opcode
;
324 req
->glxCode
= X_GLXQueryVersion
;
325 req
->majorVersion
= GLX_MAJOR_VERSION
;
326 req
->minorVersion
= GLX_MINOR_VERSION
;
327 _XReply(dpy
, (xReply
*) & reply
, 0, False
);
331 if (reply
.majorVersion
!= GLX_MAJOR_VERSION
) {
333 ** The server does not support the same major release as this
338 *major
= reply
.majorVersion
;
339 *minor
= min(reply
.minorVersion
, GLX_MINOR_VERSION
);
345 * We don't want to enable this GLX_OML_swap_method in glxext.h,
346 * because we can't support it. The X server writes it out though,
347 * so we should handle it somehow, to avoid false warnings.
350 IGNORE_GLX_SWAP_METHOD_OML
= 0x8060
355 convert_from_x_visual_type(int visualType
)
357 static const int glx_visual_types
[] = {
358 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
359 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
360 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
363 if (visualType
< ARRAY_SIZE(glx_visual_types
))
364 return glx_visual_types
[visualType
];
370 * getVisualConfigs uses the !tagged_only path.
371 * getFBConfigs uses the tagged_only path.
374 __glXInitializeVisualConfigFromTags(struct glx_config
* config
, int count
,
375 const INT32
* bp
, Bool tagged_only
,
376 Bool fbconfig_style_tags
)
381 /* Copy in the first set of properties */
382 config
->visualID
= *bp
++;
384 config
->visualType
= convert_from_x_visual_type(*bp
++);
386 config
->rgbMode
= *bp
++;
388 config
->redBits
= *bp
++;
389 config
->greenBits
= *bp
++;
390 config
->blueBits
= *bp
++;
391 config
->alphaBits
= *bp
++;
392 config
->accumRedBits
= *bp
++;
393 config
->accumGreenBits
= *bp
++;
394 config
->accumBlueBits
= *bp
++;
395 config
->accumAlphaBits
= *bp
++;
397 config
->doubleBufferMode
= *bp
++;
398 config
->stereoMode
= *bp
++;
400 config
->rgbBits
= *bp
++;
401 config
->depthBits
= *bp
++;
402 config
->stencilBits
= *bp
++;
403 config
->numAuxBuffers
= *bp
++;
404 config
->level
= *bp
++;
406 #ifdef GLX_USE_APPLEGL
407 /* AppleSGLX supports pixmap and pbuffers with all config. */
408 config
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
409 /* Unfortunately this can create an ABI compatibility problem. */
412 count
-= __GLX_MIN_CONFIG_PROPS
;
417 ** Additional properties may be in a list at the end
418 ** of the reply. They are in pairs of property type
419 ** and property value.
422 #define FETCH_OR_SET(tag) \
423 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
425 for (i
= 0; i
< count
; i
+= 2) {
426 long int tag
= *bp
++;
430 FETCH_OR_SET(rgbMode
);
432 case GLX_BUFFER_SIZE
:
433 config
->rgbBits
= *bp
++;
436 config
->level
= *bp
++;
438 case GLX_DOUBLEBUFFER
:
439 FETCH_OR_SET(doubleBufferMode
);
442 FETCH_OR_SET(stereoMode
);
444 case GLX_AUX_BUFFERS
:
445 config
->numAuxBuffers
= *bp
++;
448 config
->redBits
= *bp
++;
451 config
->greenBits
= *bp
++;
454 config
->blueBits
= *bp
++;
457 config
->alphaBits
= *bp
++;
460 config
->depthBits
= *bp
++;
462 case GLX_STENCIL_SIZE
:
463 config
->stencilBits
= *bp
++;
465 case GLX_ACCUM_RED_SIZE
:
466 config
->accumRedBits
= *bp
++;
468 case GLX_ACCUM_GREEN_SIZE
:
469 config
->accumGreenBits
= *bp
++;
471 case GLX_ACCUM_BLUE_SIZE
:
472 config
->accumBlueBits
= *bp
++;
474 case GLX_ACCUM_ALPHA_SIZE
:
475 config
->accumAlphaBits
= *bp
++;
477 case GLX_VISUAL_CAVEAT_EXT
:
478 config
->visualRating
= *bp
++;
480 case GLX_X_VISUAL_TYPE
:
481 config
->visualType
= *bp
++;
483 case GLX_TRANSPARENT_TYPE
:
484 config
->transparentPixel
= *bp
++;
486 case GLX_TRANSPARENT_INDEX_VALUE
:
487 config
->transparentIndex
= *bp
++;
489 case GLX_TRANSPARENT_RED_VALUE
:
490 config
->transparentRed
= *bp
++;
492 case GLX_TRANSPARENT_GREEN_VALUE
:
493 config
->transparentGreen
= *bp
++;
495 case GLX_TRANSPARENT_BLUE_VALUE
:
496 config
->transparentBlue
= *bp
++;
498 case GLX_TRANSPARENT_ALPHA_VALUE
:
499 config
->transparentAlpha
= *bp
++;
502 config
->visualID
= *bp
++;
504 case GLX_DRAWABLE_TYPE
:
505 config
->drawableType
= *bp
++;
506 #ifdef GLX_USE_APPLEGL
507 /* AppleSGLX supports pixmap and pbuffers with all config. */
508 config
->drawableType
|= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
511 case GLX_RENDER_TYPE
:
512 config
->renderType
= *bp
++;
514 case GLX_X_RENDERABLE
:
515 config
->xRenderable
= *bp
++;
517 case GLX_FBCONFIG_ID
:
518 config
->fbconfigID
= *bp
++;
520 case GLX_MAX_PBUFFER_WIDTH
:
521 config
->maxPbufferWidth
= *bp
++;
523 case GLX_MAX_PBUFFER_HEIGHT
:
524 config
->maxPbufferHeight
= *bp
++;
526 case GLX_MAX_PBUFFER_PIXELS
:
527 config
->maxPbufferPixels
= *bp
++;
529 #ifndef GLX_USE_APPLEGL
530 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
531 config
->optimalPbufferWidth
= *bp
++;
533 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
534 config
->optimalPbufferHeight
= *bp
++;
536 case GLX_VISUAL_SELECT_GROUP_SGIX
:
537 config
->visualSelectGroup
= *bp
++;
539 case GLX_SWAP_METHOD_OML
:
540 config
->swapMethod
= *bp
++;
543 case GLX_SAMPLE_BUFFERS_SGIS
:
544 config
->sampleBuffers
= *bp
++;
546 case GLX_SAMPLES_SGIS
:
547 config
->samples
= *bp
++;
549 #ifdef GLX_USE_APPLEGL
550 case IGNORE_GLX_SWAP_METHOD_OML
:
551 /* We ignore this tag. See the comment above this function. */
555 case GLX_BIND_TO_TEXTURE_RGB_EXT
:
556 config
->bindToTextureRgb
= *bp
++;
558 case GLX_BIND_TO_TEXTURE_RGBA_EXT
:
559 config
->bindToTextureRgba
= *bp
++;
561 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT
:
562 config
->bindToMipmapTexture
= *bp
++;
564 case GLX_BIND_TO_TEXTURE_TARGETS_EXT
:
565 config
->bindToTextureTargets
= *bp
++;
567 case GLX_Y_INVERTED_EXT
:
568 config
->yInverted
= *bp
++;
572 if (fbconfig_style_tags
)
579 if(getenv("LIBGL_DIAGNOSTIC")) {
580 long int tagvalue
= *bp
++;
581 fprintf(stderr
, "WARNING: unknown GLX tag from server: "
582 "tag 0x%lx value 0x%lx\n", tag
, tagvalue
);
584 /* Ignore the unrecognized tag's value */
592 (config
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
595 static struct glx_config
*
596 createConfigsFromProperties(Display
* dpy
, int nvisuals
, int nprops
,
597 int screen
, GLboolean tagged_only
)
599 INT32 buf
[__GLX_TOTAL_CONFIG
], *props
;
601 struct glx_config
*modes
, *m
;
607 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
609 /* Check number of properties */
610 if (nprops
< __GLX_MIN_CONFIG_PROPS
|| nprops
> __GLX_MAX_CONFIG_PROPS
)
613 /* Allocate memory for our config structure */
614 modes
= glx_config_create_list(nvisuals
);
618 prop_size
= nprops
* __GLX_SIZE_INT32
;
619 if (prop_size
<= sizeof(buf
))
622 props
= Xmalloc(prop_size
);
624 /* Read each config structure and convert it into our format */
626 for (i
= 0; i
< nvisuals
; i
++) {
627 _XRead(dpy
, (char *) props
, prop_size
);
628 #ifdef GLX_USE_APPLEGL
629 /* Older X servers don't send this so we default it here. */
630 m
->drawableType
= GLX_WINDOW_BIT
;
633 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
634 * set the proper bits here.
635 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
637 m
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
639 __glXInitializeVisualConfigFromTags(m
, nprops
, props
,
640 tagged_only
, GL_TRUE
);
652 getVisualConfigs(struct glx_screen
*psc
,
653 struct glx_display
*priv
, int screen
)
655 xGLXGetVisualConfigsReq
*req
;
656 xGLXGetVisualConfigsReply reply
;
657 Display
*dpy
= priv
->dpy
;
662 GetReq(GLXGetVisualConfigs
, req
);
663 req
->reqType
= priv
->majorOpcode
;
664 req
->glxCode
= X_GLXGetVisualConfigs
;
665 req
->screen
= screen
;
667 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
670 psc
->visuals
= createConfigsFromProperties(dpy
,
677 return psc
->visuals
!= NULL
;
681 getFBConfigs(struct glx_screen
*psc
, struct glx_display
*priv
, int screen
)
683 xGLXGetFBConfigsReq
*fb_req
;
684 xGLXGetFBConfigsSGIXReq
*sgi_req
;
685 xGLXVendorPrivateWithReplyReq
*vpreq
;
686 xGLXGetFBConfigsReply reply
;
687 Display
*dpy
= priv
->dpy
;
690 __glXQueryServerString(dpy
, priv
->majorOpcode
, screen
, GLX_EXTENSIONS
);
695 if (atof(priv
->serverGLXversion
) >= 1.3) {
696 GetReq(GLXGetFBConfigs
, fb_req
);
697 fb_req
->reqType
= priv
->majorOpcode
;
698 fb_req
->glxCode
= X_GLXGetFBConfigs
;
699 fb_req
->screen
= screen
;
701 else if (strstr(psc
->serverGLXexts
, "GLX_SGIX_fbconfig") != NULL
) {
702 GetReqExtra(GLXVendorPrivateWithReply
,
703 sz_xGLXGetFBConfigsSGIXReq
+
704 sz_xGLXVendorPrivateWithReplyReq
, vpreq
);
705 sgi_req
= (xGLXGetFBConfigsSGIXReq
*) vpreq
;
706 sgi_req
->reqType
= priv
->majorOpcode
;
707 sgi_req
->glxCode
= X_GLXVendorPrivateWithReply
;
708 sgi_req
->vendorCode
= X_GLXvop_GetFBConfigsSGIX
;
709 sgi_req
->screen
= screen
;
714 if (!_XReply(dpy
, (xReply
*) & reply
, 0, False
))
717 psc
->configs
= createConfigsFromProperties(dpy
,
719 reply
.numAttribs
* 2,
724 return psc
->configs
!= NULL
;
728 glx_screen_init(struct glx_screen
*psc
,
729 int screen
, struct glx_display
* priv
)
731 /* Initialize per screen dynamic client GLX extensions */
732 psc
->ext_list_first_time
= GL_TRUE
;
734 psc
->dpy
= priv
->dpy
;
737 getVisualConfigs(psc
, priv
, screen
);
738 getFBConfigs(psc
, priv
, screen
);
744 ** Allocate the memory for the per screen configs for each screen.
745 ** If that works then fetch the per screen configs data.
748 AllocAndFetchScreenConfigs(Display
* dpy
, struct glx_display
* priv
)
750 struct glx_screen
*psc
;
754 ** First allocate memory for the array of per screen configs.
756 screens
= ScreenCount(dpy
);
757 priv
->screens
= Xmalloc(screens
* sizeof *priv
->screens
);
761 priv
->serverGLXversion
=
762 __glXQueryServerString(dpy
, priv
->majorOpcode
, 0, GLX_VERSION
);
763 if (priv
->serverGLXversion
== NULL
) {
764 FreeScreenConfigs(priv
);
768 for (i
= 0; i
< screens
; i
++, psc
++) {
770 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
771 if (priv
->dri2Display
)
772 psc
= (*priv
->dri2Display
->createScreen
) (i
, priv
);
773 if (psc
== NULL
&& priv
->driDisplay
)
774 psc
= (*priv
->driDisplay
->createScreen
) (i
, priv
);
775 if (psc
== NULL
&& priv
->driswDisplay
)
776 psc
= (*priv
->driswDisplay
->createScreen
) (i
, priv
);
778 #if defined(GLX_USE_APPLEGL)
779 if (psc
== NULL
&& priv
->appleglDisplay
)
780 psc
= (*priv
->appleglDisplay
->createScreen
) (i
, priv
);
783 psc
= indirect_create_screen(i
, priv
);
784 priv
->screens
[i
] = psc
;
791 ** Initialize the client side extension code.
793 _X_HIDDEN
struct glx_display
*
794 __glXInitialize(Display
* dpy
)
796 struct glx_display
*dpyPriv
, *d
;
797 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
798 Bool glx_direct
, glx_accel
;
802 _XLockMutex(_Xglobal_lock
);
804 for (dpyPriv
= glx_displays
; dpyPriv
; dpyPriv
= dpyPriv
->next
) {
805 if (dpyPriv
->dpy
== dpy
) {
806 _XUnlockMutex(_Xglobal_lock
);
811 /* Drop the lock while we create the display private. */
812 _XUnlockMutex(_Xglobal_lock
);
814 dpyPriv
= Xcalloc(1, sizeof *dpyPriv
);
818 dpyPriv
->codes
= XInitExtension(dpy
, __glXExtensionName
);
819 if (!dpyPriv
->codes
) {
821 _XUnlockMutex(_Xglobal_lock
);
826 dpyPriv
->majorOpcode
= dpyPriv
->codes
->major_opcode
;
827 dpyPriv
->serverGLXvendor
= 0x0;
828 dpyPriv
->serverGLXversion
= 0x0;
830 /* See if the versions are compatible */
831 if (!QueryVersion(dpy
, dpyPriv
->majorOpcode
,
832 &dpyPriv
->majorVersion
, &dpyPriv
->minorVersion
)) {
834 _XUnlockMutex(_Xglobal_lock
);
838 for (i
= 0; i
< __GLX_NUMBER_EVENTS
; i
++) {
839 XESetWireToEvent(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXWireToEvent
);
840 XESetEventToWire(dpy
, dpyPriv
->codes
->first_event
+ i
, __glXEventToWire
);
843 XESetCloseDisplay(dpy
, dpyPriv
->codes
->extension
, __glXCloseDisplay
);
844 XESetErrorString (dpy
, dpyPriv
->codes
->extension
,__glXErrorString
);
846 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
847 glx_direct
= (getenv("LIBGL_ALWAYS_INDIRECT") == NULL
);
848 glx_accel
= (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL
);
850 dpyPriv
->drawHash
= __glxHashCreate();
853 ** Initialize the direct rendering per display data and functions.
854 ** Note: This _must_ be done before calling any other DRI routines
855 ** (e.g., those called in AllocAndFetchScreenConfigs).
857 if (glx_direct
&& glx_accel
) {
858 dpyPriv
->dri2Display
= dri2CreateDisplay(dpy
);
859 dpyPriv
->driDisplay
= driCreateDisplay(dpy
);
862 dpyPriv
->driswDisplay
= driswCreateDisplay(dpy
);
865 #ifdef GLX_USE_APPLEGL
866 if (!applegl_create_display(dpyPriv
)) {
871 if (!AllocAndFetchScreenConfigs(dpy
, dpyPriv
)) {
876 if (dpyPriv
->majorVersion
== 1 && dpyPriv
->minorVersion
>= 1)
877 __glXClientInfo(dpy
, dpyPriv
->majorOpcode
);
879 /* Grab the lock again and add the dispay private, unless somebody
880 * beat us to initializing on this display in the meantime. */
881 _XLockMutex(_Xglobal_lock
);
883 for (d
= glx_displays
; d
; d
= d
->next
) {
885 _XUnlockMutex(_Xglobal_lock
);
886 glx_display_free(dpyPriv
);
891 dpyPriv
->next
= glx_displays
;
892 glx_displays
= dpyPriv
;
894 _XUnlockMutex(_Xglobal_lock
);
900 ** Setup for sending a GLX command on dpy. Make sure the extension is
901 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
904 __glXSetupForCommand(Display
* dpy
)
906 struct glx_context
*gc
;
907 struct glx_display
*priv
;
909 /* If this thread has a current context, flush its rendering commands */
910 gc
= __glXGetCurrentContext();
911 if (gc
->currentDpy
) {
912 /* Flush rendering buffer of the current context, if any */
913 (void) __glXFlushRenderBuffer(gc
, gc
->pc
);
915 if (gc
->currentDpy
== dpy
) {
916 /* Use opcode from gc because its right */
917 return gc
->majorOpcode
;
921 ** Have to get info about argument dpy because it might be to
922 ** a different server
927 /* Forced to lookup extension via the slow initialize route */
928 priv
= __glXInitialize(dpy
);
932 return priv
->majorOpcode
;
936 * Flush the drawing command transport buffer.
938 * \param ctx Context whose transport buffer is to be flushed.
939 * \param pc Pointer to first unused buffer location.
942 * Modify this function to use \c ctx->pc instead of the explicit
946 __glXFlushRenderBuffer(struct glx_context
* ctx
, GLubyte
* pc
)
948 Display
*const dpy
= ctx
->currentDpy
;
950 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
954 const GLint size
= pc
- ctx
->buf
;
956 if ((dpy
!= NULL
) && (size
> 0)) {
958 xcb_glx_render(c
, ctx
->currentContextTag
, size
,
959 (const uint8_t *) ctx
->buf
);
961 /* Send the entire buffer as an X request */
963 GetReq(GLXRender
, req
);
964 req
->reqType
= ctx
->majorOpcode
;
965 req
->glxCode
= X_GLXRender
;
966 req
->contextTag
= ctx
->currentContextTag
;
967 req
->length
+= (size
+ 3) >> 2;
968 _XSend(dpy
, (char *) ctx
->buf
, size
);
974 /* Reset pointer and return it */
981 * Send a portion of a GLXRenderLarge command to the server. The advantage of
982 * this function over \c __glXSendLargeCommand is that callers can use the
983 * data buffer in the GLX context and may be able to avoid allocating an
984 * extra buffer. The disadvantage is the clients will have to do more
985 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
987 * \sa __glXSendLargeCommand
989 * \param gc GLX context
990 * \param requestNumber Which part of the whole command is this? The first
992 * \param totalRequests How many requests will there be?
993 * \param data Command data.
994 * \param dataLen Size, in bytes, of the command data.
997 __glXSendLargeChunk(struct glx_context
* gc
, GLint requestNumber
,
998 GLint totalRequests
, const GLvoid
* data
, GLint dataLen
)
1000 Display
*dpy
= gc
->currentDpy
;
1002 xcb_connection_t
*c
= XGetXCBConnection(dpy
);
1003 xcb_glx_render_large(c
, gc
->currentContextTag
, requestNumber
,
1004 totalRequests
, dataLen
, data
);
1006 xGLXRenderLargeReq
*req
;
1008 if (requestNumber
== 1) {
1012 GetReq(GLXRenderLarge
, req
);
1013 req
->reqType
= gc
->majorOpcode
;
1014 req
->glxCode
= X_GLXRenderLarge
;
1015 req
->contextTag
= gc
->currentContextTag
;
1016 req
->length
+= (dataLen
+ 3) >> 2;
1017 req
->requestNumber
= requestNumber
;
1018 req
->requestTotal
= totalRequests
;
1019 req
->dataBytes
= dataLen
;
1020 Data(dpy
, data
, dataLen
);
1022 if (requestNumber
== totalRequests
) {
1026 #endif /* USE_XCB */
1031 * Send a command that is too large for the GLXRender protocol request.
1033 * Send a large command, one that is too large for some reason to
1034 * send using the GLXRender protocol request. One reason to send
1035 * a large command is to avoid copying the data.
1037 * \param ctx GLX context
1038 * \param header Header data.
1039 * \param headerLen Size, in bytes, of the header data. It is assumed that
1040 * the header data will always be small enough to fit in
1041 * a single X protocol packet.
1042 * \param data Command data.
1043 * \param dataLen Size, in bytes, of the command data.
1046 __glXSendLargeCommand(struct glx_context
* ctx
,
1047 const GLvoid
* header
, GLint headerLen
,
1048 const GLvoid
* data
, GLint dataLen
)
1051 GLint totalRequests
, requestNumber
;
1054 ** Calculate the maximum amount of data can be stuffed into a single
1055 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1056 ** packet size minus sz_xGLXRenderReq.
1058 maxSize
= (ctx
->bufSize
+ sz_xGLXRenderReq
) - sz_xGLXRenderLargeReq
;
1059 totalRequests
= 1 + (dataLen
/ maxSize
);
1060 if (dataLen
% maxSize
)
1064 ** Send all of the command, except the large array, as one request.
1066 assert(headerLen
<= maxSize
);
1067 __glXSendLargeChunk(ctx
, 1, totalRequests
, header
, headerLen
);
1070 ** Send enough requests until the whole array is sent.
1072 for (requestNumber
= 2; requestNumber
<= (totalRequests
- 1);
1074 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, maxSize
);
1075 data
= (const GLvoid
*) (((const GLubyte
*) data
) + maxSize
);
1077 assert(dataLen
> 0);
1080 assert(dataLen
<= maxSize
);
1081 __glXSendLargeChunk(ctx
, requestNumber
, totalRequests
, data
, dataLen
);
1084 /************************************************************************/
1088 __glXDumpDrawBuffer(struct glx_context
* ctx
)
1090 GLubyte
*p
= ctx
->buf
;
1091 GLubyte
*end
= ctx
->pc
;
1092 GLushort opcode
, length
;
1096 opcode
= *((GLushort
*) p
);
1097 length
= *((GLushort
*) (p
+ 2));
1098 printf("%2x: %5d: ", opcode
, length
);
1101 while (length
> 0) {
1102 printf("%08x ", *((unsigned *) p
));