glx: Add missing null check in __glXCloseDisplay
[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 "glxclient.h"
42 #include <X11/extensions/Xext.h>
43 #include <X11/extensions/extutil.h>
44 #ifdef GLX_USE_APPLEGL
45 #include "apple_glx.h"
46 #include "apple_visual.h"
47 #endif
48 #include "glxextensions.h"
49
50 #include <X11/Xlib-xcb.h>
51 #include <xcb/xcb.h>
52 #include <xcb/glx.h>
53
54
55 #ifdef DEBUG
56 void __glXDumpDrawBuffer(struct glx_context * ctx);
57 #endif
58
59 /*
60 ** You can set this cell to 1 to force the gl drawing stuff to be
61 ** one command per packet
62 */
63 _X_HIDDEN int __glXDebug = 0;
64
65 /* Extension required boiler plate */
66
67 static const char __glXExtensionName[] = GLX_EXTENSION_NAME;
68 static struct glx_display *glx_displays;
69
70 static /* const */ char *error_list[] = {
71 "GLXBadContext",
72 "GLXBadContextState",
73 "GLXBadDrawable",
74 "GLXBadPixmap",
75 "GLXBadContextTag",
76 "GLXBadCurrentWindow",
77 "GLXBadRenderRequest",
78 "GLXBadLargeRequest",
79 "GLXUnsupportedPrivateRequest",
80 "GLXBadFBConfig",
81 "GLXBadPbuffer",
82 "GLXBadCurrentDrawable",
83 "GLXBadWindow",
84 "GLXBadProfileARB",
85 };
86
87 #ifdef GLX_USE_APPLEGL
88 static char *__glXErrorString(Display *dpy, int code, XExtCodes *codes,
89 char *buf, int n);
90 #endif
91
92 static
93 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
94 __GLX_NUMBER_ERRORS, error_list)
95
96 /*
97 * GLX events are a bit funky. We don't stuff the X event code into
98 * our user exposed (via XNextEvent) structure. Instead we use the GLX
99 * private event code namespace (and hope it doesn't conflict). Clients
100 * have to know that bit 15 in the event type field means they're getting
101 * a GLX event, and then handle the various sub-event types there, rather
102 * than simply checking the event code and handling it directly.
103 */
104
105 static Bool
106 __glXWireToEvent(Display *dpy, XEvent *event, xEvent *wire)
107 {
108 struct glx_display *glx_dpy = __glXInitialize(dpy);
109
110 if (glx_dpy == NULL)
111 return False;
112
113 switch ((wire->u.u.type & 0x7f) - glx_dpy->codes->first_event) {
114 case GLX_PbufferClobber:
115 {
116 GLXPbufferClobberEvent *aevent = (GLXPbufferClobberEvent *)event;
117 xGLXPbufferClobberEvent *awire = (xGLXPbufferClobberEvent *)wire;
118 aevent->event_type = awire->type;
119 aevent->serial = awire->sequenceNumber;
120 aevent->event_type = awire->event_type;
121 aevent->draw_type = awire->draw_type;
122 aevent->drawable = awire->drawable;
123 aevent->buffer_mask = awire->buffer_mask;
124 aevent->aux_buffer = awire->aux_buffer;
125 aevent->x = awire->x;
126 aevent->y = awire->y;
127 aevent->width = awire->width;
128 aevent->height = awire->height;
129 aevent->count = awire->count;
130 return True;
131 }
132 case GLX_BufferSwapComplete:
133 {
134 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
135 xGLXBufferSwapComplete2 *awire = (xGLXBufferSwapComplete2 *)wire;
136 struct glx_drawable *glxDraw = GetGLXDrawable(dpy, awire->drawable);
137 aevent->event_type = awire->event_type;
138 aevent->drawable = awire->drawable;
139 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
140 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
141
142 if (!glxDraw)
143 return False;
144
145 if (awire->sbc < glxDraw->lastEventSbc)
146 glxDraw->eventSbcWrap += 0x100000000;
147 glxDraw->lastEventSbc = awire->sbc;
148 aevent->sbc = awire->sbc + glxDraw->eventSbcWrap;
149 return True;
150 }
151 default:
152 /* client doesn't support server event */
153 break;
154 }
155
156 return False;
157 }
158
159 /* We don't actually support this. It doesn't make sense for clients to
160 * send each other GLX events.
161 */
162 static Status
163 __glXEventToWire(Display *dpy, XEvent *event, xEvent *wire)
164 {
165 struct glx_display *glx_dpy = __glXInitialize(dpy);
166
167 if (glx_dpy == NULL)
168 return False;
169
170 switch (event->type) {
171 case GLX_DAMAGED:
172 break;
173 case GLX_SAVED:
174 break;
175 case GLX_EXCHANGE_COMPLETE_INTEL:
176 break;
177 case GLX_COPY_COMPLETE_INTEL:
178 break;
179 case GLX_FLIP_COMPLETE_INTEL:
180 break;
181 default:
182 /* client doesn't support server event */
183 break;
184 }
185
186 return Success;
187 }
188
189 /************************************************************************/
190 /*
191 ** Free the per screen configs data as well as the array of
192 ** __glXScreenConfigs.
193 */
194 static void
195 FreeScreenConfigs(struct glx_display * priv)
196 {
197 struct glx_screen *psc;
198 GLint i, screens;
199
200 /* Free screen configuration information */
201 screens = ScreenCount(priv->dpy);
202 for (i = 0; i < screens; i++) {
203 psc = priv->screens[i];
204 glx_screen_cleanup(psc);
205
206 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
207 if (psc->driScreen) {
208 psc->driScreen->destroyScreen(psc);
209 } else {
210 free(psc);
211 }
212 #else
213 free(psc);
214 #endif
215 }
216 free((char *) priv->screens);
217 priv->screens = NULL;
218 }
219
220 static void
221 glx_display_free(struct glx_display *priv)
222 {
223 struct glx_context *gc;
224
225 gc = __glXGetCurrentContext();
226 if (priv->dpy == gc->currentDpy) {
227 gc->vtable->destroy(gc);
228 __glXSetCurrentContextNull();
229 }
230
231 FreeScreenConfigs(priv);
232 free((char *) priv->serverGLXvendor);
233 free((char *) priv->serverGLXversion);
234
235 __glxHashDestroy(priv->glXDrawHash);
236
237 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
238 __glxHashDestroy(priv->drawHash);
239
240 /* Free the direct rendering per display data */
241 if (priv->driswDisplay)
242 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
243 priv->driswDisplay = NULL;
244
245 if (priv->driDisplay)
246 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
247 priv->driDisplay = NULL;
248
249 if (priv->dri2Display)
250 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
251 priv->dri2Display = NULL;
252 #endif
253
254 free((char *) priv);
255 }
256
257 static int
258 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
259 {
260 struct glx_display *priv, **prev;
261
262 _XLockMutex(_Xglobal_lock);
263 prev = &glx_displays;
264 for (priv = glx_displays; priv; prev = &priv->next, priv = priv->next) {
265 if (priv->dpy == dpy) {
266 *prev = priv->next;
267 break;
268 }
269 }
270 _XUnlockMutex(_Xglobal_lock);
271
272 if (priv != NULL)
273 glx_display_free(priv);
274
275 return 1;
276 }
277
278 /*
279 ** Query the version of the GLX extension. This procedure works even if
280 ** the client extension is not completely set up.
281 */
282 static Bool
283 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
284 {
285 xcb_connection_t *c = XGetXCBConnection(dpy);
286 xcb_glx_query_version_reply_t *reply = xcb_glx_query_version_reply(c,
287 xcb_glx_query_version
288 (c,
289 GLX_MAJOR_VERSION,
290 GLX_MINOR_VERSION),
291 NULL);
292
293 if (!reply)
294 return GL_FALSE;
295
296 if (reply->major_version != GLX_MAJOR_VERSION) {
297 free(reply);
298 return GL_FALSE;
299 }
300 *major = reply->major_version;
301 *minor = min(reply->minor_version, GLX_MINOR_VERSION);
302 free(reply);
303 return GL_TRUE;
304 }
305
306 /*
307 * We don't want to enable this GLX_OML_swap_method in glxext.h,
308 * because we can't support it. The X server writes it out though,
309 * so we should handle it somehow, to avoid false warnings.
310 */
311 enum {
312 IGNORE_GLX_SWAP_METHOD_OML = 0x8060
313 };
314
315
316 static GLint
317 convert_from_x_visual_type(int visualType)
318 {
319 static const int glx_visual_types[] = {
320 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
321 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
322 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
323 };
324
325 if (visualType < ARRAY_SIZE(glx_visual_types))
326 return glx_visual_types[visualType];
327
328 return GLX_NONE;
329 }
330
331 /*
332 * getVisualConfigs uses the !tagged_only path.
333 * getFBConfigs uses the tagged_only path.
334 */
335 _X_HIDDEN void
336 __glXInitializeVisualConfigFromTags(struct glx_config * config, int count,
337 const INT32 * bp, Bool tagged_only,
338 Bool fbconfig_style_tags)
339 {
340 int i;
341 GLint renderType = 0;
342
343 if (!tagged_only) {
344 /* Copy in the first set of properties */
345 config->visualID = *bp++;
346
347 config->visualType = convert_from_x_visual_type(*bp++);
348
349 config->rgbMode = *bp++;
350
351 config->redBits = *bp++;
352 config->greenBits = *bp++;
353 config->blueBits = *bp++;
354 config->alphaBits = *bp++;
355 config->accumRedBits = *bp++;
356 config->accumGreenBits = *bp++;
357 config->accumBlueBits = *bp++;
358 config->accumAlphaBits = *bp++;
359
360 config->doubleBufferMode = *bp++;
361 config->stereoMode = *bp++;
362
363 config->rgbBits = *bp++;
364 config->depthBits = *bp++;
365 config->stencilBits = *bp++;
366 config->numAuxBuffers = *bp++;
367 config->level = *bp++;
368
369 #ifdef GLX_USE_APPLEGL
370 /* AppleSGLX supports pixmap and pbuffers with all config. */
371 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
372 /* Unfortunately this can create an ABI compatibility problem. */
373 count -= 18;
374 #else
375 count -= __GLX_MIN_CONFIG_PROPS;
376 #endif
377 }
378
379 config->sRGBCapable = GL_FALSE;
380
381 /*
382 ** Additional properties may be in a list at the end
383 ** of the reply. They are in pairs of property type
384 ** and property value.
385 */
386
387 #define FETCH_OR_SET(tag) \
388 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
389
390 for (i = 0; i < count; i += 2) {
391 long int tag = *bp++;
392
393 switch (tag) {
394 case GLX_RGBA:
395 FETCH_OR_SET(rgbMode);
396 break;
397 case GLX_BUFFER_SIZE:
398 config->rgbBits = *bp++;
399 break;
400 case GLX_LEVEL:
401 config->level = *bp++;
402 break;
403 case GLX_DOUBLEBUFFER:
404 FETCH_OR_SET(doubleBufferMode);
405 break;
406 case GLX_STEREO:
407 FETCH_OR_SET(stereoMode);
408 break;
409 case GLX_AUX_BUFFERS:
410 config->numAuxBuffers = *bp++;
411 break;
412 case GLX_RED_SIZE:
413 config->redBits = *bp++;
414 break;
415 case GLX_GREEN_SIZE:
416 config->greenBits = *bp++;
417 break;
418 case GLX_BLUE_SIZE:
419 config->blueBits = *bp++;
420 break;
421 case GLX_ALPHA_SIZE:
422 config->alphaBits = *bp++;
423 break;
424 case GLX_DEPTH_SIZE:
425 config->depthBits = *bp++;
426 break;
427 case GLX_STENCIL_SIZE:
428 config->stencilBits = *bp++;
429 break;
430 case GLX_ACCUM_RED_SIZE:
431 config->accumRedBits = *bp++;
432 break;
433 case GLX_ACCUM_GREEN_SIZE:
434 config->accumGreenBits = *bp++;
435 break;
436 case GLX_ACCUM_BLUE_SIZE:
437 config->accumBlueBits = *bp++;
438 break;
439 case GLX_ACCUM_ALPHA_SIZE:
440 config->accumAlphaBits = *bp++;
441 break;
442 case GLX_VISUAL_CAVEAT_EXT:
443 config->visualRating = *bp++;
444 break;
445 case GLX_X_VISUAL_TYPE:
446 config->visualType = *bp++;
447 break;
448 case GLX_TRANSPARENT_TYPE:
449 config->transparentPixel = *bp++;
450 break;
451 case GLX_TRANSPARENT_INDEX_VALUE:
452 config->transparentIndex = *bp++;
453 break;
454 case GLX_TRANSPARENT_RED_VALUE:
455 config->transparentRed = *bp++;
456 break;
457 case GLX_TRANSPARENT_GREEN_VALUE:
458 config->transparentGreen = *bp++;
459 break;
460 case GLX_TRANSPARENT_BLUE_VALUE:
461 config->transparentBlue = *bp++;
462 break;
463 case GLX_TRANSPARENT_ALPHA_VALUE:
464 config->transparentAlpha = *bp++;
465 break;
466 case GLX_VISUAL_ID:
467 config->visualID = *bp++;
468 break;
469 case GLX_DRAWABLE_TYPE:
470 config->drawableType = *bp++;
471 #ifdef GLX_USE_APPLEGL
472 /* AppleSGLX supports pixmap and pbuffers with all config. */
473 config->drawableType |= GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
474 #endif
475 break;
476 case GLX_RENDER_TYPE: /* fbconfig render type bits */
477 renderType = *bp++;
478 break;
479 case GLX_X_RENDERABLE:
480 config->xRenderable = *bp++;
481 break;
482 case GLX_FBCONFIG_ID:
483 config->fbconfigID = *bp++;
484 break;
485 case GLX_MAX_PBUFFER_WIDTH:
486 config->maxPbufferWidth = *bp++;
487 break;
488 case GLX_MAX_PBUFFER_HEIGHT:
489 config->maxPbufferHeight = *bp++;
490 break;
491 case GLX_MAX_PBUFFER_PIXELS:
492 config->maxPbufferPixels = *bp++;
493 break;
494 #ifndef GLX_USE_APPLEGL
495 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
496 config->optimalPbufferWidth = *bp++;
497 break;
498 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
499 config->optimalPbufferHeight = *bp++;
500 break;
501 case GLX_VISUAL_SELECT_GROUP_SGIX:
502 config->visualSelectGroup = *bp++;
503 break;
504 case GLX_SWAP_METHOD_OML:
505 config->swapMethod = *bp++;
506 break;
507 #endif
508 case GLX_SAMPLE_BUFFERS_SGIS:
509 config->sampleBuffers = *bp++;
510 break;
511 case GLX_SAMPLES_SGIS:
512 config->samples = *bp++;
513 break;
514 #ifdef GLX_USE_APPLEGL
515 case IGNORE_GLX_SWAP_METHOD_OML:
516 /* We ignore this tag. See the comment above this function. */
517 ++bp;
518 break;
519 #else
520 case GLX_BIND_TO_TEXTURE_RGB_EXT:
521 config->bindToTextureRgb = *bp++;
522 break;
523 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
524 config->bindToTextureRgba = *bp++;
525 break;
526 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
527 config->bindToMipmapTexture = *bp++;
528 break;
529 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
530 config->bindToTextureTargets = *bp++;
531 break;
532 case GLX_Y_INVERTED_EXT:
533 config->yInverted = *bp++;
534 break;
535 #endif
536 case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:
537 config->sRGBCapable = *bp++;
538 break;
539
540 case GLX_USE_GL:
541 if (fbconfig_style_tags)
542 bp++;
543 break;
544 case None:
545 i = count;
546 break;
547 default:
548 if(getenv("LIBGL_DIAGNOSTIC")) {
549 long int tagvalue = *bp++;
550 fprintf(stderr, "WARNING: unknown GLX tag from server: "
551 "tag 0x%lx value 0x%lx\n", tag, tagvalue);
552 } else {
553 /* Ignore the unrecognized tag's value */
554 bp++;
555 }
556 break;
557 }
558 }
559
560 if (renderType != 0 && renderType != GLX_DONT_CARE) {
561 config->renderType = renderType;
562 config->floatMode = (renderType &
563 (GLX_RGBA_FLOAT_BIT_ARB|GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT)) != 0;
564 } else {
565 /* If there wasn't GLX_RENDER_TYPE property, set it based on
566 * config->rgbMode. The only way to communicate that the config is
567 * floating-point is via GLX_RENDER_TYPE, so this cannot be a float
568 * config.
569 */
570 config->renderType =
571 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
572 }
573
574 /* The GLX_ARB_fbconfig_float spec says:
575 *
576 * "Note that floating point rendering is only supported for
577 * GLXPbuffer drawables."
578 */
579 if (config->floatMode)
580 config->drawableType &= ~(GLX_WINDOW_BIT|GLX_PIXMAP_BIT);
581 }
582
583 static struct glx_config *
584 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
585 int screen, GLboolean tagged_only)
586 {
587 INT32 buf[__GLX_TOTAL_CONFIG], *props;
588 unsigned prop_size;
589 struct glx_config *modes, *m;
590 int i;
591
592 if (nprops == 0)
593 return NULL;
594
595 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
596
597 /* Check number of properties */
598 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
599 return NULL;
600
601 /* Allocate memory for our config structure */
602 modes = glx_config_create_list(nvisuals);
603 if (!modes)
604 return NULL;
605
606 prop_size = nprops * __GLX_SIZE_INT32;
607 if (prop_size <= sizeof(buf))
608 props = buf;
609 else
610 props = malloc(prop_size);
611
612 /* Read each config structure and convert it into our format */
613 m = modes;
614 for (i = 0; i < nvisuals; i++) {
615 _XRead(dpy, (char *) props, prop_size);
616 #ifdef GLX_USE_APPLEGL
617 /* Older X servers don't send this so we default it here. */
618 m->drawableType = GLX_WINDOW_BIT;
619 #else
620 /*
621 * The XQuartz 2.3.2.1 X server doesn't set this properly, so
622 * set the proper bits here.
623 * AppleSGLX supports windows, pixmaps, and pbuffers with all config.
624 */
625 m->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
626 #endif
627 __glXInitializeVisualConfigFromTags(m, nprops, props,
628 tagged_only, GL_TRUE);
629 m->screen = screen;
630 m = m->next;
631 }
632
633 if (props != buf)
634 free(props);
635
636 return modes;
637 }
638
639 static GLboolean
640 getVisualConfigs(struct glx_screen *psc,
641 struct glx_display *priv, int screen)
642 {
643 xGLXGetVisualConfigsReq *req;
644 xGLXGetVisualConfigsReply reply;
645 Display *dpy = priv->dpy;
646
647 LockDisplay(dpy);
648
649 psc->visuals = NULL;
650 GetReq(GLXGetVisualConfigs, req);
651 req->reqType = priv->majorOpcode;
652 req->glxCode = X_GLXGetVisualConfigs;
653 req->screen = screen;
654
655 if (!_XReply(dpy, (xReply *) & reply, 0, False))
656 goto out;
657
658 psc->visuals = createConfigsFromProperties(dpy,
659 reply.numVisuals,
660 reply.numProps,
661 screen, GL_FALSE);
662
663 out:
664 UnlockDisplay(dpy);
665 return psc->visuals != NULL;
666 }
667
668 static GLboolean
669 getFBConfigs(struct glx_screen *psc, struct glx_display *priv, int screen)
670 {
671 xGLXGetFBConfigsReq *fb_req;
672 xGLXGetFBConfigsSGIXReq *sgi_req;
673 xGLXVendorPrivateWithReplyReq *vpreq;
674 xGLXGetFBConfigsReply reply;
675 Display *dpy = priv->dpy;
676
677 psc->serverGLXexts =
678 __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
679
680 LockDisplay(dpy);
681
682 psc->configs = NULL;
683 if (atof(priv->serverGLXversion) >= 1.3) {
684 GetReq(GLXGetFBConfigs, fb_req);
685 fb_req->reqType = priv->majorOpcode;
686 fb_req->glxCode = X_GLXGetFBConfigs;
687 fb_req->screen = screen;
688 }
689 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
690 GetReqExtra(GLXVendorPrivateWithReply,
691 sz_xGLXGetFBConfigsSGIXReq -
692 sz_xGLXVendorPrivateWithReplyReq, vpreq);
693 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
694 sgi_req->reqType = priv->majorOpcode;
695 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
696 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
697 sgi_req->screen = screen;
698 }
699 else
700 goto out;
701
702 if (!_XReply(dpy, (xReply *) & reply, 0, False))
703 goto out;
704
705 psc->configs = createConfigsFromProperties(dpy,
706 reply.numFBConfigs,
707 reply.numAttribs * 2,
708 screen, GL_TRUE);
709
710 out:
711 UnlockDisplay(dpy);
712 return psc->configs != NULL;
713 }
714
715 _X_HIDDEN Bool
716 glx_screen_init(struct glx_screen *psc,
717 int screen, struct glx_display * priv)
718 {
719 /* Initialize per screen dynamic client GLX extensions */
720 psc->ext_list_first_time = GL_TRUE;
721 psc->scr = screen;
722 psc->dpy = priv->dpy;
723 psc->display = priv;
724
725 getVisualConfigs(psc, priv, screen);
726 getFBConfigs(psc, priv, screen);
727
728 return GL_TRUE;
729 }
730
731 _X_HIDDEN void
732 glx_screen_cleanup(struct glx_screen *psc)
733 {
734 if (psc->configs) {
735 glx_config_destroy_list(psc->configs);
736 free(psc->effectiveGLXexts);
737 psc->configs = NULL; /* NOTE: just for paranoia */
738 }
739 if (psc->visuals) {
740 glx_config_destroy_list(psc->visuals);
741 psc->visuals = NULL; /* NOTE: just for paranoia */
742 }
743 free((char *) psc->serverGLXexts);
744 }
745
746 /*
747 ** Allocate the memory for the per screen configs for each screen.
748 ** If that works then fetch the per screen configs data.
749 */
750 static Bool
751 AllocAndFetchScreenConfigs(Display * dpy, struct glx_display * priv)
752 {
753 struct glx_screen *psc;
754 GLint i, screens;
755
756 /*
757 ** First allocate memory for the array of per screen configs.
758 */
759 screens = ScreenCount(dpy);
760 priv->screens = malloc(screens * sizeof *priv->screens);
761 if (!priv->screens)
762 return GL_FALSE;
763
764 priv->serverGLXversion =
765 __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
766 if (priv->serverGLXversion == NULL) {
767 FreeScreenConfigs(priv);
768 return GL_FALSE;
769 }
770
771 for (i = 0; i < screens; i++, psc++) {
772 psc = NULL;
773 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
774 #if defined(HAVE_DRI3)
775 if (priv->dri3Display)
776 psc = (*priv->dri3Display->createScreen) (i, priv);
777 #endif
778 if (psc == NULL && priv->dri2Display)
779 psc = (*priv->dri2Display->createScreen) (i, priv);
780 if (psc == NULL && priv->driDisplay)
781 psc = (*priv->driDisplay->createScreen) (i, priv);
782 if (psc == NULL && priv->driswDisplay)
783 psc = (*priv->driswDisplay->createScreen) (i, priv);
784 #endif
785 #if defined(GLX_USE_APPLEGL)
786 if (psc == NULL)
787 psc = applegl_create_screen(i, priv);
788 #else
789 if (psc == NULL)
790 psc = indirect_create_screen(i, priv);
791 #endif
792 priv->screens[i] = psc;
793 }
794 SyncHandle();
795 return GL_TRUE;
796 }
797
798 /*
799 ** Initialize the client side extension code.
800 */
801 _X_HIDDEN struct glx_display *
802 __glXInitialize(Display * dpy)
803 {
804 struct glx_display *dpyPriv, *d;
805 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
806 Bool glx_direct, glx_accel;
807 #endif
808 int i;
809
810 _XLockMutex(_Xglobal_lock);
811
812 for (dpyPriv = glx_displays; dpyPriv; dpyPriv = dpyPriv->next) {
813 if (dpyPriv->dpy == dpy) {
814 _XUnlockMutex(_Xglobal_lock);
815 return dpyPriv;
816 }
817 }
818
819 /* Drop the lock while we create the display private. */
820 _XUnlockMutex(_Xglobal_lock);
821
822 dpyPriv = calloc(1, sizeof *dpyPriv);
823 if (!dpyPriv)
824 return NULL;
825
826 dpyPriv->codes = XInitExtension(dpy, __glXExtensionName);
827 if (!dpyPriv->codes) {
828 free(dpyPriv);
829 _XUnlockMutex(_Xglobal_lock);
830 return NULL;
831 }
832
833 dpyPriv->dpy = dpy;
834 dpyPriv->majorOpcode = dpyPriv->codes->major_opcode;
835 dpyPriv->serverGLXvendor = 0x0;
836 dpyPriv->serverGLXversion = 0x0;
837
838 /* See if the versions are compatible. This GLX implementation does not
839 * work with servers that only support GLX 1.0.
840 */
841 if (!QueryVersion(dpy, dpyPriv->majorOpcode,
842 &dpyPriv->majorVersion, &dpyPriv->minorVersion)
843 || (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion < 1)) {
844 free(dpyPriv);
845 _XUnlockMutex(_Xglobal_lock);
846 return NULL;
847 }
848
849 for (i = 0; i < __GLX_NUMBER_EVENTS; i++) {
850 XESetWireToEvent(dpy, dpyPriv->codes->first_event + i, __glXWireToEvent);
851 XESetEventToWire(dpy, dpyPriv->codes->first_event + i, __glXEventToWire);
852 }
853
854 XESetCloseDisplay(dpy, dpyPriv->codes->extension, __glXCloseDisplay);
855 XESetErrorString (dpy, dpyPriv->codes->extension,__glXErrorString);
856
857 dpyPriv->glXDrawHash = __glxHashCreate();
858
859 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
860 glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
861 glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
862
863 dpyPriv->drawHash = __glxHashCreate();
864
865 /*
866 ** Initialize the direct rendering per display data and functions.
867 ** Note: This _must_ be done before calling any other DRI routines
868 ** (e.g., those called in AllocAndFetchScreenConfigs).
869 */
870 if (glx_direct && glx_accel) {
871 #if defined(HAVE_DRI3)
872 if (!getenv("LIBGL_DRI3_DISABLE"))
873 dpyPriv->dri3Display = dri3_create_display(dpy);
874 #endif
875 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
876 dpyPriv->driDisplay = driCreateDisplay(dpy);
877 }
878 if (glx_direct)
879 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
880 #endif
881
882 #ifdef GLX_USE_APPLEGL
883 if (!applegl_create_display(dpyPriv)) {
884 free(dpyPriv);
885 return NULL;
886 }
887 #endif
888 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
889 free(dpyPriv);
890 return NULL;
891 }
892
893 __glX_send_client_info(dpyPriv);
894
895 /* Grab the lock again and add the dispay private, unless somebody
896 * beat us to initializing on this display in the meantime. */
897 _XLockMutex(_Xglobal_lock);
898
899 for (d = glx_displays; d; d = d->next) {
900 if (d->dpy == dpy) {
901 _XUnlockMutex(_Xglobal_lock);
902 glx_display_free(dpyPriv);
903 return d;
904 }
905 }
906
907 dpyPriv->next = glx_displays;
908 glx_displays = dpyPriv;
909
910 _XUnlockMutex(_Xglobal_lock);
911
912 return dpyPriv;
913 }
914
915 /*
916 ** Setup for sending a GLX command on dpy. Make sure the extension is
917 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
918 */
919 _X_HIDDEN CARD8
920 __glXSetupForCommand(Display * dpy)
921 {
922 struct glx_context *gc;
923 struct glx_display *priv;
924
925 /* If this thread has a current context, flush its rendering commands */
926 gc = __glXGetCurrentContext();
927 if (gc->currentDpy) {
928 /* Flush rendering buffer of the current context, if any */
929 (void) __glXFlushRenderBuffer(gc, gc->pc);
930
931 if (gc->currentDpy == dpy) {
932 /* Use opcode from gc because its right */
933 return gc->majorOpcode;
934 }
935 else {
936 /*
937 ** Have to get info about argument dpy because it might be to
938 ** a different server
939 */
940 }
941 }
942
943 /* Forced to lookup extension via the slow initialize route */
944 priv = __glXInitialize(dpy);
945 if (!priv) {
946 return 0;
947 }
948 return priv->majorOpcode;
949 }
950
951 /**
952 * Flush the drawing command transport buffer.
953 *
954 * \param ctx Context whose transport buffer is to be flushed.
955 * \param pc Pointer to first unused buffer location.
956 *
957 * \todo
958 * Modify this function to use \c ctx->pc instead of the explicit
959 * \c pc parameter.
960 */
961 _X_HIDDEN GLubyte *
962 __glXFlushRenderBuffer(struct glx_context * ctx, GLubyte * pc)
963 {
964 Display *const dpy = ctx->currentDpy;
965 xcb_connection_t *c = XGetXCBConnection(dpy);
966 const GLint size = pc - ctx->buf;
967
968 if ((dpy != NULL) && (size > 0)) {
969 xcb_glx_render(c, ctx->currentContextTag, size,
970 (const uint8_t *) ctx->buf);
971 }
972
973 /* Reset pointer and return it */
974 ctx->pc = ctx->buf;
975 return ctx->pc;
976 }
977
978
979 /**
980 * Send a portion of a GLXRenderLarge command to the server. The advantage of
981 * this function over \c __glXSendLargeCommand is that callers can use the
982 * data buffer in the GLX context and may be able to avoid allocating an
983 * extra buffer. The disadvantage is the clients will have to do more
984 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
985 *
986 * \sa __glXSendLargeCommand
987 *
988 * \param gc GLX context
989 * \param requestNumber Which part of the whole command is this? The first
990 * request is 1.
991 * \param totalRequests How many requests will there be?
992 * \param data Command data.
993 * \param dataLen Size, in bytes, of the command data.
994 */
995 _X_HIDDEN void
996 __glXSendLargeChunk(struct glx_context * gc, GLint requestNumber,
997 GLint totalRequests, const GLvoid * data, GLint dataLen)
998 {
999 Display *dpy = gc->currentDpy;
1000 xcb_connection_t *c = XGetXCBConnection(dpy);
1001 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
1002 totalRequests, dataLen, data);
1003 }
1004
1005
1006 /**
1007 * Send a command that is too large for the GLXRender protocol request.
1008 *
1009 * Send a large command, one that is too large for some reason to
1010 * send using the GLXRender protocol request. One reason to send
1011 * a large command is to avoid copying the data.
1012 *
1013 * \param ctx GLX context
1014 * \param header Header data.
1015 * \param headerLen Size, in bytes, of the header data. It is assumed that
1016 * the header data will always be small enough to fit in
1017 * a single X protocol packet.
1018 * \param data Command data.
1019 * \param dataLen Size, in bytes, of the command data.
1020 */
1021 _X_HIDDEN void
1022 __glXSendLargeCommand(struct glx_context * ctx,
1023 const GLvoid * header, GLint headerLen,
1024 const GLvoid * data, GLint dataLen)
1025 {
1026 GLint maxSize;
1027 GLint totalRequests, requestNumber;
1028
1029 /*
1030 ** Calculate the maximum amount of data can be stuffed into a single
1031 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1032 ** packet size minus sz_xGLXRenderReq.
1033 */
1034 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1035 totalRequests = 1 + (dataLen / maxSize);
1036 if (dataLen % maxSize)
1037 totalRequests++;
1038
1039 /*
1040 ** Send all of the command, except the large array, as one request.
1041 */
1042 assert(headerLen <= maxSize);
1043 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1044
1045 /*
1046 ** Send enough requests until the whole array is sent.
1047 */
1048 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
1049 requestNumber++) {
1050 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1051 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1052 dataLen -= maxSize;
1053 assert(dataLen > 0);
1054 }
1055
1056 assert(dataLen <= maxSize);
1057 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1058 }
1059
1060 /************************************************************************/
1061
1062 #ifdef DEBUG
1063 _X_HIDDEN void
1064 __glXDumpDrawBuffer(struct glx_context * ctx)
1065 {
1066 GLubyte *p = ctx->buf;
1067 GLubyte *end = ctx->pc;
1068 GLushort opcode, length;
1069
1070 while (p < end) {
1071 /* Fetch opcode */
1072 opcode = *((GLushort *) p);
1073 length = *((GLushort *) (p + 2));
1074 printf("%2x: %5d: ", opcode, length);
1075 length -= 4;
1076 p += 4;
1077 while (length > 0) {
1078 printf("%08x ", *((unsigned *) p));
1079 p += 4;
1080 length -= 4;
1081 }
1082 printf("\n");
1083 }
1084 }
1085 #endif