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