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