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