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