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