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