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