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