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