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