glx: fix 630 times -Wlto-type-mismatch when building with LTO enabled
[mesa.git] / src / glx / glxext.c
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
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:
11 *
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.
16 *
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
23 * SOFTWARE.
24 *
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.
29 */
30
31 /**
32 * \file glxext.c
33 * GLX protocol interface boot-strap code.
34 *
35 * Direct rendering support added by Precision Insight, Inc.
36 *
37 * \author Kevin E. Martin <kevin@precisioninsight.com>
38 */
39
40 #include <assert.h>
41 #include <stdbool.h>
42
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"
49 #endif
50 #include "glxextensions.h"
51
52 #include "util/debug.h"
53 #include "dri_common.h"
54
55 #include <X11/Xlib-xcb.h>
56 #include <xcb/xcb.h>
57 #include <xcb/glx.h>
58
59
60 #ifdef DEBUG
61 void __glXDumpDrawBuffer(struct glx_context * ctx);
62 #endif
63
64 /*
65 ** You can set this cell to 1 to force the gl drawing stuff to be
66 ** one command per packet
67 */
68 _X_HIDDEN int __glXDebug = 0;
69
70 /* Extension required boiler plate */
71
72 static const char __glXExtensionName[] = GLX_EXTENSION_NAME;
73 static struct glx_display *glx_displays;
74
75 static /* const */ char *error_list[] = {
76 "GLXBadContext",
77 "GLXBadContextState",
78 "GLXBadDrawable",
79 "GLXBadPixmap",
80 "GLXBadContextTag",
81 "GLXBadCurrentWindow",
82 "GLXBadRenderRequest",
83 "GLXBadLargeRequest",
84 "GLXUnsupportedPrivateRequest",
85 "GLXBadFBConfig",
86 "GLXBadPbuffer",
87 "GLXBadCurrentDrawable",
88 "GLXBadWindow",
89 "GLXBadProfileARB",
90 };
91
92 #ifdef GLX_USE_APPLEGL
93 static char *__glXErrorString(Display *dpy, int code, XExtCodes *codes,
94 char *buf, int n);
95 #endif
96
97 static
98 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
99 __GLX_NUMBER_ERRORS, error_list)
100
101 /*
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.
108 */
109
110 static Bool
111 __glXWireToEvent(Display *dpy, XEvent *event, xEvent *wire)
112 {
113 struct glx_display *glx_dpy = __glXInitialize(dpy);
114
115 if (glx_dpy == NULL)
116 return False;
117
118 switch ((wire->u.u.type & 0x7f) - glx_dpy->codes->first_event) {
119 case GLX_PbufferClobber:
120 {
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;
135 return True;
136 }
137 case GLX_BufferSwapComplete:
138 {
139 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
140 xGLXBufferSwapComplete2 *awire = (xGLXBufferSwapComplete2 *)wire;
141 struct glx_drawable *glxDraw = GetGLXDrawable(dpy, awire->drawable);
142
143 if (!glxDraw)
144 return False;
145
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;
153
154 /* Handle 32-Bit wire sbc wraparound in both directions to cope with out
155 * of sequence 64-Bit sbc's
156 */
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;
163 return True;
164 }
165 default:
166 /* client doesn't support server event */
167 break;
168 }
169
170 return False;
171 }
172
173 /* We don't actually support this. It doesn't make sense for clients to
174 * send each other GLX events.
175 */
176 static Status
177 __glXEventToWire(Display *dpy, XEvent *event, xEvent *wire)
178 {
179 struct glx_display *glx_dpy = __glXInitialize(dpy);
180
181 if (glx_dpy == NULL)
182 return False;
183
184 switch (event->type) {
185 case GLX_DAMAGED:
186 break;
187 case GLX_SAVED:
188 break;
189 case GLX_EXCHANGE_COMPLETE_INTEL:
190 break;
191 case GLX_COPY_COMPLETE_INTEL:
192 break;
193 case GLX_FLIP_COMPLETE_INTEL:
194 break;
195 default:
196 /* client doesn't support server event */
197 break;
198 }
199
200 return Success;
201 }
202
203 /************************************************************************/
204 /*
205 ** Free the per screen configs data as well as the array of
206 ** __glXScreenConfigs.
207 */
208 static void
209 FreeScreenConfigs(struct glx_display * priv)
210 {
211 struct glx_screen *psc;
212 GLint i, screens;
213
214 /* Free screen configuration information */
215 screens = ScreenCount(priv->dpy);
216 for (i = 0; i < screens; i++) {
217 psc = priv->screens[i];
218 if (!psc)
219 continue;
220 glx_screen_cleanup(psc);
221
222 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
223 if (psc->driScreen) {
224 psc->driScreen->destroyScreen(psc);
225 } else {
226 free(psc);
227 }
228 #else
229 free(psc);
230 #endif
231 }
232 free((char *) priv->screens);
233 priv->screens = NULL;
234 }
235
236 static void
237 glx_display_free(struct glx_display *priv)
238 {
239 struct glx_context *gc;
240
241 gc = __glXGetCurrentContext();
242 if (priv->dpy == gc->currentDpy) {
243 gc->vtable->destroy(gc);
244 __glXSetCurrentContextNull();
245 }
246
247 FreeScreenConfigs(priv);
248 free((char *) priv->serverGLXvendor);
249 free((char *) priv->serverGLXversion);
250
251 __glxHashDestroy(priv->glXDrawHash);
252
253 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
254 __glxHashDestroy(priv->drawHash);
255
256 /* Free the direct rendering per display data */
257 if (priv->driswDisplay)
258 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
259 priv->driswDisplay = NULL;
260
261 #if defined (GLX_USE_DRM)
262 if (priv->driDisplay)
263 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
264 priv->driDisplay = NULL;
265
266 if (priv->dri2Display)
267 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
268 priv->dri2Display = NULL;
269
270 if (priv->dri3Display)
271 (*priv->dri3Display->destroyDisplay) (priv->dri3Display);
272 priv->dri3Display = NULL;
273 #endif /* GLX_USE_DRM */
274
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 */
280
281 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
282
283 free((char *) priv);
284 }
285
286 static int
287 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
288 {
289 struct glx_display *priv, **prev;
290
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) {
295 *prev = priv->next;
296 break;
297 }
298 }
299 _XUnlockMutex(_Xglobal_lock);
300
301 if (priv != NULL)
302 glx_display_free(priv);
303
304 return 1;
305 }
306
307 /*
308 ** Query the version of the GLX extension. This procedure works even if
309 ** the client extension is not completely set up.
310 */
311 static Bool
312 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
313 {
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
317 (c,
318 GLX_MAJOR_VERSION,
319 GLX_MINOR_VERSION),
320 NULL);
321
322 if (!reply)
323 return GL_FALSE;
324
325 if (reply->major_version != GLX_MAJOR_VERSION) {
326 free(reply);
327 return GL_FALSE;
328 }
329 *major = reply->major_version;
330 *minor = min(reply->minor_version, GLX_MINOR_VERSION);
331 free(reply);
332 return GL_TRUE;
333 }
334
335 /*
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.
339 */
340 enum {
341 IGNORE_GLX_SWAP_METHOD_OML = 0x8060
342 };
343
344
345 static GLint
346 convert_from_x_visual_type(int visualType)
347 {
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,
355 };
356
357 if (visualType < ARRAY_SIZE(glx_visual_types))
358 return glx_visual_types[visualType];
359
360 return GLX_NONE;
361 }
362
363 /*
364 * getVisualConfigs uses the !tagged_only path.
365 * getFBConfigs uses the tagged_only path.
366 */
367 _X_HIDDEN void
368 __glXInitializeVisualConfigFromTags(struct glx_config * config, int count,
369 const INT32 * bp, Bool tagged_only,
370 Bool fbconfig_style_tags)
371 {
372 int i;
373
374 if (!tagged_only) {
375 /* Copy in the first set of properties */
376 config->visualID = *bp++;
377
378 config->visualType = convert_from_x_visual_type(*bp++);
379
380 config->renderType = *bp++ ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
381
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++;
390
391 config->doubleBufferMode = *bp++;
392 config->stereoMode = *bp++;
393
394 config->rgbBits = *bp++;
395 config->depthBits = *bp++;
396 config->stencilBits = *bp++;
397 config->numAuxBuffers = *bp++;
398 config->level = *bp++;
399
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. */
404 count -= 18;
405 #else
406 count -= __GLX_MIN_CONFIG_PROPS;
407 #endif
408 }
409
410 /*
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.
414 */
415
416 #define FETCH_OR_SET(tag) \
417 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
418
419 for (i = 0; i < count; i += 2) {
420 long int tag = *bp++;
421
422 switch (tag) {
423 case GLX_RGBA:
424 if (fbconfig_style_tags)
425 config->renderType = *bp++ ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
426 else
427 config->renderType = GLX_RGBA_BIT;
428 break;
429 case GLX_BUFFER_SIZE:
430 config->rgbBits = *bp++;
431 break;
432 case GLX_LEVEL:
433 config->level = *bp++;
434 break;
435 case GLX_DOUBLEBUFFER:
436 FETCH_OR_SET(doubleBufferMode);
437 break;
438 case GLX_STEREO:
439 FETCH_OR_SET(stereoMode);
440 break;
441 case GLX_AUX_BUFFERS:
442 config->numAuxBuffers = *bp++;
443 break;
444 case GLX_RED_SIZE:
445 config->redBits = *bp++;
446 break;
447 case GLX_GREEN_SIZE:
448 config->greenBits = *bp++;
449 break;
450 case GLX_BLUE_SIZE:
451 config->blueBits = *bp++;
452 break;
453 case GLX_ALPHA_SIZE:
454 config->alphaBits = *bp++;
455 break;
456 case GLX_DEPTH_SIZE:
457 config->depthBits = *bp++;
458 break;
459 case GLX_STENCIL_SIZE:
460 config->stencilBits = *bp++;
461 break;
462 case GLX_ACCUM_RED_SIZE:
463 config->accumRedBits = *bp++;
464 break;
465 case GLX_ACCUM_GREEN_SIZE:
466 config->accumGreenBits = *bp++;
467 break;
468 case GLX_ACCUM_BLUE_SIZE:
469 config->accumBlueBits = *bp++;
470 break;
471 case GLX_ACCUM_ALPHA_SIZE:
472 config->accumAlphaBits = *bp++;
473 break;
474 case GLX_VISUAL_CAVEAT_EXT:
475 config->visualRating = *bp++;
476 break;
477 case GLX_X_VISUAL_TYPE:
478 config->visualType = *bp++;
479 break;
480 case GLX_TRANSPARENT_TYPE:
481 config->transparentPixel = *bp++;
482 break;
483 case GLX_TRANSPARENT_INDEX_VALUE:
484 config->transparentIndex = *bp++;
485 break;
486 case GLX_TRANSPARENT_RED_VALUE:
487 config->transparentRed = *bp++;
488 break;
489 case GLX_TRANSPARENT_GREEN_VALUE:
490 config->transparentGreen = *bp++;
491 break;
492 case GLX_TRANSPARENT_BLUE_VALUE:
493 config->transparentBlue = *bp++;
494 break;
495 case GLX_TRANSPARENT_ALPHA_VALUE:
496 config->transparentAlpha = *bp++;
497 break;
498 case GLX_VISUAL_ID:
499 config->visualID = *bp++;
500 break;
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;
506 #endif
507 break;
508 case GLX_RENDER_TYPE: /* fbconfig render type bits */
509 config->renderType = *bp++;
510 break;
511 case GLX_X_RENDERABLE:
512 config->xRenderable = *bp++;
513 break;
514 case GLX_FBCONFIG_ID:
515 config->fbconfigID = *bp++;
516 break;
517 case GLX_MAX_PBUFFER_WIDTH:
518 config->maxPbufferWidth = *bp++;
519 break;
520 case GLX_MAX_PBUFFER_HEIGHT:
521 config->maxPbufferHeight = *bp++;
522 break;
523 case GLX_MAX_PBUFFER_PIXELS:
524 config->maxPbufferPixels = *bp++;
525 break;
526 #ifndef GLX_USE_APPLEGL
527 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
528 config->optimalPbufferWidth = *bp++;
529 break;
530 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
531 config->optimalPbufferHeight = *bp++;
532 break;
533 case GLX_VISUAL_SELECT_GROUP_SGIX:
534 config->visualSelectGroup = *bp++;
535 break;
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++;
541 } else {
542 /* X servers with old HW drivers may return any value here, so
543 * assume GLX_SWAP_METHOD_UNDEFINED.
544 */
545 config->swapMethod = GLX_SWAP_UNDEFINED_OML;
546 bp++;
547 }
548 break;
549 #endif
550 case GLX_SAMPLE_BUFFERS_SGIS:
551 config->sampleBuffers = *bp++;
552 break;
553 case GLX_SAMPLES_SGIS:
554 config->samples = *bp++;
555 break;
556 #ifdef GLX_USE_APPLEGL
557 case IGNORE_GLX_SWAP_METHOD_OML:
558 /* We ignore this tag. See the comment above this function. */
559 ++bp;
560 break;
561 #else
562 case GLX_BIND_TO_TEXTURE_RGB_EXT:
563 config->bindToTextureRgb = *bp++;
564 break;
565 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
566 config->bindToTextureRgba = *bp++;
567 break;
568 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
569 config->bindToMipmapTexture = *bp++;
570 break;
571 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
572 config->bindToTextureTargets = *bp++;
573 break;
574 case GLX_Y_INVERTED_EXT:
575 config->yInverted = *bp++;
576 break;
577 #endif
578 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:
579 config->sRGBCapable = *bp++;
580 break;
581
582 case GLX_USE_GL:
583 if (fbconfig_style_tags)
584 bp++;
585 break;
586 case None:
587 i = count;
588 break;
589 default:
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);
594 } else {
595 /* Ignore the unrecognized tag's value */
596 bp++;
597 }
598 break;
599 }
600 }
601
602 /* The GLX_ARB_fbconfig_float spec says:
603 *
604 * "Note that floating point rendering is only supported for
605 * GLXPbuffer drawables."
606 */
607 if (config->renderType &
608 (GLX_RGBA_FLOAT_BIT_ARB|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT))
609 config->drawableType &= GLX_PBUFFER_BIT;
610 }
611
612 static struct glx_config *
613 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
614 int screen, GLboolean tagged_only)
615 {
616 INT32 buf[__GLX_TOTAL_CONFIG], *props;
617 unsigned prop_size;
618 struct glx_config *modes, *m;
619 int i;
620
621 if (nprops == 0)
622 return NULL;
623
624 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
625
626 /* Check number of properties */
627 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
628 return NULL;
629
630 /* Allocate memory for our config structure */
631 modes = glx_config_create_list(nvisuals);
632 if (!modes)
633 return NULL;
634
635 prop_size = nprops * __GLX_SIZE_INT32;
636 if (prop_size <= sizeof(buf))
637 props = buf;
638 else
639 props = malloc(prop_size);
640
641 /* Read each config structure and convert it into our format */
642 m = modes;
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;
648 #else
649 /*
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.
653 */
654 m->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
655 #endif
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);
660 m->screen = screen;
661 m = m->next;
662 }
663
664 if (props != buf)
665 free(props);
666
667 return modes;
668 }
669
670 static GLboolean
671 getVisualConfigs(struct glx_screen *psc,
672 struct glx_display *priv, int screen)
673 {
674 xGLXGetVisualConfigsReq *req;
675 xGLXGetVisualConfigsReply reply;
676 Display *dpy = priv->dpy;
677
678 LockDisplay(dpy);
679
680 psc->visuals = NULL;
681 GetReq(GLXGetVisualConfigs, req);
682 req->reqType = priv->majorOpcode;
683 req->glxCode = X_GLXGetVisualConfigs;
684 req->screen = screen;
685
686 if (!_XReply(dpy, (xReply *) & reply, 0, False))
687 goto out;
688
689 psc->visuals = createConfigsFromProperties(dpy,
690 reply.numVisuals,
691 reply.numProps,
692 screen, GL_FALSE);
693
694 out:
695 UnlockDisplay(dpy);
696 return psc->visuals != NULL;
697 }
698
699 static GLboolean
700 getFBConfigs(struct glx_screen *psc, struct glx_display *priv, int screen)
701 {
702 xGLXGetFBConfigsReq *fb_req;
703 xGLXGetFBConfigsSGIXReq *sgi_req;
704 xGLXVendorPrivateWithReplyReq *vpreq;
705 xGLXGetFBConfigsReply reply;
706 Display *dpy = priv->dpy;
707
708 psc->serverGLXexts =
709 __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
710
711 if (psc->serverGLXexts == NULL) {
712 return GL_FALSE;
713 }
714
715 LockDisplay(dpy);
716
717 psc->configs = 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;
724 }
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;
734 }
735 else
736 goto out;
737
738 if (!_XReply(dpy, (xReply *) & reply, 0, False))
739 goto out;
740
741 psc->configs = createConfigsFromProperties(dpy,
742 reply.numFBConfigs,
743 reply.numAttribs * 2,
744 screen, GL_TRUE);
745
746 out:
747 UnlockDisplay(dpy);
748 return psc->configs != NULL;
749 }
750
751 _X_HIDDEN Bool
752 glx_screen_init(struct glx_screen *psc,
753 int screen, struct glx_display * priv)
754 {
755 /* Initialize per screen dynamic client GLX extensions */
756 psc->ext_list_first_time = GL_TRUE;
757 psc->scr = screen;
758 psc->dpy = priv->dpy;
759 psc->display = priv;
760
761 if (!getVisualConfigs(psc, priv, screen))
762 return GL_FALSE;
763
764 if (!getFBConfigs(psc, priv, screen))
765 return GL_FALSE;
766
767 return GL_TRUE;
768 }
769
770 _X_HIDDEN void
771 glx_screen_cleanup(struct glx_screen *psc)
772 {
773 if (psc->configs) {
774 glx_config_destroy_list(psc->configs);
775 free(psc->effectiveGLXexts);
776 psc->configs = NULL; /* NOTE: just for paranoia */
777 }
778 if (psc->visuals) {
779 glx_config_destroy_list(psc->visuals);
780 psc->visuals = NULL; /* NOTE: just for paranoia */
781 }
782 free((char *) psc->serverGLXexts);
783 }
784
785 /*
786 ** Allocate the memory for the per screen configs for each screen.
787 ** If that works then fetch the per screen configs data.
788 */
789 static Bool
790 AllocAndFetchScreenConfigs(Display * dpy, struct glx_display * priv)
791 {
792 struct glx_screen *psc;
793 GLint i, screens;
794
795 /*
796 ** First allocate memory for the array of per screen configs.
797 */
798 screens = ScreenCount(dpy);
799 priv->screens = calloc(screens, sizeof *priv->screens);
800 if (!priv->screens)
801 return GL_FALSE;
802
803 priv->serverGLXversion =
804 __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
805 if (priv->serverGLXversion == NULL) {
806 FreeScreenConfigs(priv);
807 return GL_FALSE;
808 }
809
810 for (i = 0; i < screens; i++, psc++) {
811 psc = NULL;
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 */
823
824 #ifdef GLX_USE_WINDOWSGL
825 if (psc == NULL && priv->windowsdriDisplay)
826 psc = (*priv->windowsdriDisplay->createScreen) (i, priv);
827 #endif
828
829 if (psc == NULL && priv->driswDisplay)
830 psc = (*priv->driswDisplay->createScreen) (i, priv);
831 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
832
833 #if defined(GLX_USE_APPLEGL)
834 if (psc == NULL)
835 psc = applegl_create_screen(i, priv);
836 #else
837 if (psc == NULL)
838 psc = indirect_create_screen(i, priv);
839 #endif
840 priv->screens[i] = psc;
841 }
842 SyncHandle();
843 return GL_TRUE;
844 }
845
846 /*
847 ** Initialize the client side extension code.
848 */
849 _X_HIDDEN struct glx_display *
850 __glXInitialize(Display * dpy)
851 {
852 struct glx_display *dpyPriv, *d;
853 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
854 Bool glx_direct, glx_accel;
855 #endif
856 int i;
857
858 _XLockMutex(_Xglobal_lock);
859
860 for (dpyPriv = glx_displays; dpyPriv; dpyPriv = dpyPriv->next) {
861 if (dpyPriv->dpy == dpy) {
862 _XUnlockMutex(_Xglobal_lock);
863 return dpyPriv;
864 }
865 }
866
867 /* Drop the lock while we create the display private. */
868 _XUnlockMutex(_Xglobal_lock);
869
870 dpyPriv = calloc(1, sizeof *dpyPriv);
871 if (!dpyPriv)
872 return NULL;
873
874 dpyPriv->codes = XInitExtension(dpy, __glXExtensionName);
875 if (!dpyPriv->codes) {
876 free(dpyPriv);
877 return NULL;
878 }
879
880 dpyPriv->dpy = dpy;
881 dpyPriv->majorOpcode = dpyPriv->codes->major_opcode;
882 dpyPriv->serverGLXvendor = 0x0;
883 dpyPriv->serverGLXversion = 0x0;
884
885 /* See if the versions are compatible. This GLX implementation does not
886 * work with servers that only support GLX 1.0.
887 */
888 if (!QueryVersion(dpy, dpyPriv->majorOpcode,
889 &dpyPriv->majorVersion, &dpyPriv->minorVersion)
890 || (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion < 1)) {
891 free(dpyPriv);
892 return NULL;
893 }
894
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);
898 }
899
900 XESetCloseDisplay(dpy, dpyPriv->codes->extension, __glXCloseDisplay);
901 XESetErrorString (dpy, dpyPriv->codes->extension, __glXErrorString);
902
903 dpyPriv->glXDrawHash = __glxHashCreate();
904
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);
908
909 dpyPriv->drawHash = __glxHashCreate();
910
911 /* Set the logger before the *CreateDisplay functions. */
912 loader_set_logger(dri_message);
913
914 /*
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).
918 */
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);
927 }
928 #endif /* GLX_USE_DRM */
929 if (glx_direct)
930 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
931 #endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
932
933 #ifdef GLX_USE_APPLEGL
934 if (!applegl_create_display(dpyPriv)) {
935 free(dpyPriv);
936 return NULL;
937 }
938 #endif
939
940 #ifdef GLX_USE_WINDOWSGL
941 if (glx_direct && glx_accel)
942 dpyPriv->windowsdriDisplay = driwindowsCreateDisplay(dpy);
943 #endif
944
945 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
946 free(dpyPriv);
947 return NULL;
948 }
949
950 __glX_send_client_info(dpyPriv);
951
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);
955
956 for (d = glx_displays; d; d = d->next) {
957 if (d->dpy == dpy) {
958 _XUnlockMutex(_Xglobal_lock);
959 glx_display_free(dpyPriv);
960 return d;
961 }
962 }
963
964 dpyPriv->next = glx_displays;
965 glx_displays = dpyPriv;
966
967 _XUnlockMutex(_Xglobal_lock);
968
969 return dpyPriv;
970 }
971
972 /*
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.
975 */
976 _X_HIDDEN CARD8
977 __glXSetupForCommand(Display * dpy)
978 {
979 struct glx_context *gc;
980 struct glx_display *priv;
981
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);
987
988 if (gc->currentDpy == dpy) {
989 /* Use opcode from gc because its right */
990 return gc->majorOpcode;
991 }
992 else {
993 /*
994 ** Have to get info about argument dpy because it might be to
995 ** a different server
996 */
997 }
998 }
999
1000 /* Forced to lookup extension via the slow initialize route */
1001 priv = __glXInitialize(dpy);
1002 if (!priv) {
1003 return 0;
1004 }
1005 return priv->majorOpcode;
1006 }
1007
1008 /**
1009 * Flush the drawing command transport buffer.
1010 *
1011 * \param ctx Context whose transport buffer is to be flushed.
1012 * \param pc Pointer to first unused buffer location.
1013 *
1014 * \todo
1015 * Modify this function to use \c ctx->pc instead of the explicit
1016 * \c pc parameter.
1017 */
1018 _X_HIDDEN GLubyte *
1019 __glXFlushRenderBuffer(struct glx_context * ctx, GLubyte * pc)
1020 {
1021 Display *const dpy = ctx->currentDpy;
1022 xcb_connection_t *c = XGetXCBConnection(dpy);
1023 const GLint size = pc - ctx->buf;
1024
1025 if ((dpy != NULL) && (size > 0)) {
1026 xcb_glx_render(c, ctx->currentContextTag, size,
1027 (const uint8_t *) ctx->buf);
1028 }
1029
1030 /* Reset pointer and return it */
1031 ctx->pc = ctx->buf;
1032 return ctx->pc;
1033 }
1034
1035
1036 /**
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.).
1042 *
1043 * \sa __glXSendLargeCommand
1044 *
1045 * \param gc GLX context
1046 * \param requestNumber Which part of the whole command is this? The first
1047 * request is 1.
1048 * \param totalRequests How many requests will there be?
1049 * \param data Command data.
1050 * \param dataLen Size, in bytes, of the command data.
1051 */
1052 _X_HIDDEN void
1053 __glXSendLargeChunk(struct glx_context * gc, GLint requestNumber,
1054 GLint totalRequests, const GLvoid * data, GLint dataLen)
1055 {
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);
1060 }
1061
1062
1063 /**
1064 * Send a command that is too large for the GLXRender protocol request.
1065 *
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.
1069 *
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.
1077 */
1078 _X_HIDDEN void
1079 __glXSendLargeCommand(struct glx_context * ctx,
1080 const GLvoid * header, GLint headerLen,
1081 const GLvoid * data, GLint dataLen)
1082 {
1083 GLint maxSize;
1084 GLint totalRequests, requestNumber;
1085
1086 /*
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.
1090 */
1091 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1092 totalRequests = 1 + (dataLen / maxSize);
1093 if (dataLen % maxSize)
1094 totalRequests++;
1095
1096 /*
1097 ** Send all of the command, except the large array, as one request.
1098 */
1099 assert(headerLen <= maxSize);
1100 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1101
1102 /*
1103 ** Send enough requests until the whole array is sent.
1104 */
1105 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
1106 requestNumber++) {
1107 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1108 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1109 dataLen -= maxSize;
1110 assert(dataLen > 0);
1111 }
1112
1113 assert(dataLen <= maxSize);
1114 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1115 }
1116
1117 /************************************************************************/
1118
1119 #ifdef DEBUG
1120 _X_HIDDEN void
1121 __glXDumpDrawBuffer(struct glx_context * ctx)
1122 {
1123 GLubyte *p = ctx->buf;
1124 GLubyte *end = ctx->pc;
1125 GLushort opcode, length;
1126
1127 while (p < end) {
1128 /* Fetch opcode */
1129 opcode = *((GLushort *) p);
1130 length = *((GLushort *) (p + 2));
1131 printf("%2x: %5d: ", opcode, length);
1132 length -= 4;
1133 p += 4;
1134 while (length > 0) {
1135 printf("%08x ", *((unsigned *) p));
1136 p += 4;
1137 length -= 4;
1138 }
1139 printf("\n");
1140 }
1141 }
1142 #endif