glx: permit building with older protocol headers
[mesa.git] / src / glx / x11 / 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 "glxextensions.h"
45 #include "glcontextmodes.h"
46
47 #ifdef USE_XCB
48 #include <X11/Xlib-xcb.h>
49 #include <xcb/xcb.h>
50 #include <xcb/glx.h>
51 #endif
52
53
54 #ifdef DEBUG
55 void __glXDumpDrawBuffer(__GLXcontext * ctx);
56 #endif
57
58 /*
59 ** You can set this cell to 1 to force the gl drawing stuff to be
60 ** one command per packet
61 */
62 _X_HIDDEN int __glXDebug = 0;
63
64 /* Extension required boiler plate */
65
66 static char *__glXExtensionName = GLX_EXTENSION_NAME;
67 XExtensionInfo *__glXExtensionInfo = NULL;
68
69 static /* const */ char *error_list[] = {
70 "GLXBadContext",
71 "GLXBadContextState",
72 "GLXBadDrawable",
73 "GLXBadPixmap",
74 "GLXBadContextTag",
75 "GLXBadCurrentWindow",
76 "GLXBadRenderRequest",
77 "GLXBadLargeRequest",
78 "GLXUnsupportedPrivateRequest",
79 "GLXBadFBConfig",
80 "GLXBadPbuffer",
81 "GLXBadCurrentDrawable",
82 "GLXBadWindow",
83 };
84
85 static int
86 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
87 {
88 GLXContext gc;
89
90 gc = __glXGetCurrentContext();
91 if (dpy == gc->currentDpy) {
92 __glXSetCurrentContextNull();
93 __glXFreeContext(gc);
94 }
95
96 return XextRemoveDisplay(__glXExtensionInfo, dpy);
97 }
98
99
100 static
101 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
102 __GLX_NUMBER_ERRORS, error_list)
103 static Bool
104 __glXWireToEvent(Display *dpy, XEvent *event, xEvent *wire);
105 static Status
106 __glXEventToWire(Display *dpy, XEvent *event, xEvent *wire);
107
108 static /* const */ XExtensionHooks __glXExtensionHooks = {
109 NULL, /* create_gc */
110 NULL, /* copy_gc */
111 NULL, /* flush_gc */
112 NULL, /* free_gc */
113 NULL, /* create_font */
114 NULL, /* free_font */
115 __glXCloseDisplay, /* close_display */
116 __glXWireToEvent, /* wire_to_event */
117 __glXEventToWire, /* event_to_wire */
118 NULL, /* error */
119 __glXErrorString, /* error_string */
120 };
121
122 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
123 __glXExtensionName, &__glXExtensionHooks,
124 __GLX_NUMBER_EVENTS, NULL)
125
126 /*
127 * GLX events are a bit funky. We don't stuff the X event code into
128 * our user exposed (via XNextEvent) structure. Instead we use the GLX
129 * private event code namespace (and hope it doesn't conflict). Clients
130 * have to know that bit 15 in the event type field means they're getting
131 * a GLX event, and then handle the various sub-event types there, rather
132 * than simply checking the event code and handling it directly.
133 */
134
135 static Bool
136 __glXWireToEvent(Display *dpy, XEvent *event, xEvent *wire)
137 {
138 XExtDisplayInfo *info = __glXFindDisplay(dpy);
139
140 XextCheckExtension(dpy, info, __glXExtensionName, False);
141
142 switch ((wire->u.u.type & 0x7f) - info->codes->first_event) {
143 case GLX_PbufferClobber:
144 {
145 GLXPbufferClobberEvent *aevent = (GLXPbufferClobberEvent *)event;
146 xGLXPbufferClobberEvent *awire = (xGLXPbufferClobberEvent *)wire;
147 aevent->event_type = awire->type;
148 aevent->serial = awire->sequenceNumber;
149 aevent->event_type = awire->event_type;
150 aevent->draw_type = awire->draw_type;
151 aevent->drawable = awire->drawable;
152 aevent->buffer_mask = awire->buffer_mask;
153 aevent->aux_buffer = awire->aux_buffer;
154 aevent->x = awire->x;
155 aevent->y = awire->y;
156 aevent->width = awire->width;
157 aevent->height = awire->height;
158 aevent->count = awire->count;
159 return True;
160 }
161 /* No easy symbol to test for this, as GLX_BufferSwapComplete is
162 * defined in the local glx.h header, but the
163 * xGLXBufferSwapComplete typedef is only available in new versions
164 * of the external glxproto.h header, which doesn't have any
165 * testable versioning define.
166 *
167 * I'll use the related DRI2 define, in the hope that we won't
168 * receive these events unless we know how to ask for them:
169 */
170 #ifdef X_DRI2SwapBuffers
171 case GLX_BufferSwapComplete:
172 {
173 GLXBufferSwapComplete *aevent = (GLXBufferSwapComplete *)event;
174 xGLXBufferSwapComplete *awire = (xGLXBufferSwapComplete *)wire;
175 aevent->event_type = awire->event_type;
176 aevent->drawable = awire->drawable;
177 aevent->ust = ((CARD64)awire->ust_hi << 32) | awire->ust_lo;
178 aevent->msc = ((CARD64)awire->msc_hi << 32) | awire->msc_lo;
179 aevent->sbc = ((CARD64)awire->sbc_hi << 32) | awire->sbc_lo;
180 return True;
181 }
182 #endif
183 default:
184 /* client doesn't support server event */
185 break;
186 }
187
188 return False;
189 }
190
191 /* We don't actually support this. It doesn't make sense for clients to
192 * send each other GLX events.
193 */
194 static Status
195 __glXEventToWire(Display *dpy, XEvent *event, xEvent *wire)
196 {
197 XExtDisplayInfo *info = __glXFindDisplay(dpy);
198
199 XextCheckExtension(dpy, info, __glXExtensionName, False);
200
201 switch (event->type) {
202 case GLX_DAMAGED:
203 break;
204 case GLX_SAVED:
205 break;
206 case GLX_EXCHANGE_COMPLETE_INTEL:
207 break;
208 case GLX_BLIT_COMPLETE_INTEL:
209 break;
210 case GLX_FLIP_COMPLETE_INTEL:
211 break;
212 default:
213 /* client doesn't support server event */
214 break;
215 }
216
217 return Success;
218 }
219
220 /************************************************************************/
221 /*
222 ** Free the per screen configs data as well as the array of
223 ** __glXScreenConfigs.
224 */
225 static void
226 FreeScreenConfigs(__GLXdisplayPrivate * priv)
227 {
228 __GLXscreenConfigs *psc;
229 GLint i, screens;
230
231 /* Free screen configuration information */
232 psc = priv->screenConfigs;
233 screens = ScreenCount(priv->dpy);
234 for (i = 0; i < screens; i++, psc++) {
235 if (psc->configs) {
236 _gl_context_modes_destroy(psc->configs);
237 if (psc->effectiveGLXexts)
238 Xfree(psc->effectiveGLXexts);
239 psc->configs = NULL; /* NOTE: just for paranoia */
240 }
241 if (psc->visuals) {
242 _gl_context_modes_destroy(psc->visuals);
243 psc->visuals = NULL; /* NOTE: just for paranoia */
244 }
245 Xfree((char *) psc->serverGLXexts);
246
247 #ifdef GLX_DIRECT_RENDERING
248 if (psc->driver_configs) {
249 unsigned int j;
250 for (j = 0; psc->driver_configs[j]; j++)
251 free((__DRIconfig *) psc->driver_configs[j]);
252 free(psc->driver_configs);
253 psc->driver_configs = NULL;
254 }
255 if (psc->driScreen) {
256 psc->driScreen->destroyScreen(psc);
257 __glxHashDestroy(psc->drawHash);
258 XFree(psc->driScreen);
259 psc->driScreen = NULL;
260 }
261 #endif
262 }
263 XFree((char *) priv->screenConfigs);
264 priv->screenConfigs = NULL;
265 }
266
267 /*
268 ** Release the private memory referred to in a display private
269 ** structure. The caller will free the extension structure.
270 */
271 static int
272 __glXFreeDisplayPrivate(XExtData * extension)
273 {
274 __GLXdisplayPrivate *priv;
275
276 priv = (__GLXdisplayPrivate *) extension->private_data;
277 FreeScreenConfigs(priv);
278 if (priv->serverGLXvendor) {
279 Xfree((char *) priv->serverGLXvendor);
280 priv->serverGLXvendor = 0x0; /* to protect against double free's */
281 }
282 if (priv->serverGLXversion) {
283 Xfree((char *) priv->serverGLXversion);
284 priv->serverGLXversion = 0x0; /* to protect against double free's */
285 }
286
287 #ifdef GLX_DIRECT_RENDERING
288 /* Free the direct rendering per display data */
289 if (priv->driswDisplay)
290 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
291 priv->driswDisplay = NULL;
292
293 if (priv->driDisplay)
294 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
295 priv->driDisplay = NULL;
296
297 if (priv->dri2Display)
298 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
299 priv->dri2Display = NULL;
300 #endif
301
302 Xfree((char *) priv);
303 return 0;
304 }
305
306 /************************************************************************/
307
308 /*
309 ** Query the version of the GLX extension. This procedure works even if
310 ** the client extension is not completely set up.
311 */
312 static Bool
313 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
314 {
315 #ifdef USE_XCB
316 xcb_connection_t *c = XGetXCBConnection(dpy);
317 xcb_glx_query_version_reply_t *reply = xcb_glx_query_version_reply(c,
318 xcb_glx_query_version
319 (c,
320 GLX_MAJOR_VERSION,
321 GLX_MINOR_VERSION),
322 NULL);
323
324 if (reply->major_version != GLX_MAJOR_VERSION) {
325 free(reply);
326 return GL_FALSE;
327 }
328 *major = reply->major_version;
329 *minor = min(reply->minor_version, GLX_MINOR_VERSION);
330 free(reply);
331 return GL_TRUE;
332 #else
333 xGLXQueryVersionReq *req;
334 xGLXQueryVersionReply reply;
335
336 /* Send the glXQueryVersion request */
337 LockDisplay(dpy);
338 GetReq(GLXQueryVersion, req);
339 req->reqType = opcode;
340 req->glxCode = X_GLXQueryVersion;
341 req->majorVersion = GLX_MAJOR_VERSION;
342 req->minorVersion = GLX_MINOR_VERSION;
343 _XReply(dpy, (xReply *) & reply, 0, False);
344 UnlockDisplay(dpy);
345 SyncHandle();
346
347 if (reply.majorVersion != GLX_MAJOR_VERSION) {
348 /*
349 ** The server does not support the same major release as this
350 ** client.
351 */
352 return GL_FALSE;
353 }
354 *major = reply.majorVersion;
355 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
356 return GL_TRUE;
357 #endif /* USE_XCB */
358 }
359
360
361 _X_HIDDEN void
362 __glXInitializeVisualConfigFromTags(__GLcontextModes * config, int count,
363 const INT32 * bp, Bool tagged_only,
364 Bool fbconfig_style_tags)
365 {
366 int i;
367
368 if (!tagged_only) {
369 /* Copy in the first set of properties */
370 config->visualID = *bp++;
371
372 config->visualType = _gl_convert_from_x_visual_type(*bp++);
373
374 config->rgbMode = *bp++;
375
376 config->redBits = *bp++;
377 config->greenBits = *bp++;
378 config->blueBits = *bp++;
379 config->alphaBits = *bp++;
380 config->accumRedBits = *bp++;
381 config->accumGreenBits = *bp++;
382 config->accumBlueBits = *bp++;
383 config->accumAlphaBits = *bp++;
384
385 config->doubleBufferMode = *bp++;
386 config->stereoMode = *bp++;
387
388 config->rgbBits = *bp++;
389 config->depthBits = *bp++;
390 config->stencilBits = *bp++;
391 config->numAuxBuffers = *bp++;
392 config->level = *bp++;
393
394 count -= __GLX_MIN_CONFIG_PROPS;
395 }
396
397 /*
398 ** Additional properties may be in a list at the end
399 ** of the reply. They are in pairs of property type
400 ** and property value.
401 */
402
403 #define FETCH_OR_SET(tag) \
404 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
405
406 for (i = 0; i < count; i += 2) {
407 switch (*bp++) {
408 case GLX_RGBA:
409 FETCH_OR_SET(rgbMode);
410 break;
411 case GLX_BUFFER_SIZE:
412 config->rgbBits = *bp++;
413 break;
414 case GLX_LEVEL:
415 config->level = *bp++;
416 break;
417 case GLX_DOUBLEBUFFER:
418 FETCH_OR_SET(doubleBufferMode);
419 break;
420 case GLX_STEREO:
421 FETCH_OR_SET(stereoMode);
422 break;
423 case GLX_AUX_BUFFERS:
424 config->numAuxBuffers = *bp++;
425 break;
426 case GLX_RED_SIZE:
427 config->redBits = *bp++;
428 break;
429 case GLX_GREEN_SIZE:
430 config->greenBits = *bp++;
431 break;
432 case GLX_BLUE_SIZE:
433 config->blueBits = *bp++;
434 break;
435 case GLX_ALPHA_SIZE:
436 config->alphaBits = *bp++;
437 break;
438 case GLX_DEPTH_SIZE:
439 config->depthBits = *bp++;
440 break;
441 case GLX_STENCIL_SIZE:
442 config->stencilBits = *bp++;
443 break;
444 case GLX_ACCUM_RED_SIZE:
445 config->accumRedBits = *bp++;
446 break;
447 case GLX_ACCUM_GREEN_SIZE:
448 config->accumGreenBits = *bp++;
449 break;
450 case GLX_ACCUM_BLUE_SIZE:
451 config->accumBlueBits = *bp++;
452 break;
453 case GLX_ACCUM_ALPHA_SIZE:
454 config->accumAlphaBits = *bp++;
455 break;
456 case GLX_VISUAL_CAVEAT_EXT:
457 config->visualRating = *bp++;
458 break;
459 case GLX_X_VISUAL_TYPE:
460 config->visualType = *bp++;
461 break;
462 case GLX_TRANSPARENT_TYPE:
463 config->transparentPixel = *bp++;
464 break;
465 case GLX_TRANSPARENT_INDEX_VALUE:
466 config->transparentIndex = *bp++;
467 break;
468 case GLX_TRANSPARENT_RED_VALUE:
469 config->transparentRed = *bp++;
470 break;
471 case GLX_TRANSPARENT_GREEN_VALUE:
472 config->transparentGreen = *bp++;
473 break;
474 case GLX_TRANSPARENT_BLUE_VALUE:
475 config->transparentBlue = *bp++;
476 break;
477 case GLX_TRANSPARENT_ALPHA_VALUE:
478 config->transparentAlpha = *bp++;
479 break;
480 case GLX_VISUAL_ID:
481 config->visualID = *bp++;
482 break;
483 case GLX_DRAWABLE_TYPE:
484 config->drawableType = *bp++;
485 break;
486 case GLX_RENDER_TYPE:
487 config->renderType = *bp++;
488 break;
489 case GLX_X_RENDERABLE:
490 config->xRenderable = *bp++;
491 break;
492 case GLX_FBCONFIG_ID:
493 config->fbconfigID = *bp++;
494 break;
495 case GLX_MAX_PBUFFER_WIDTH:
496 config->maxPbufferWidth = *bp++;
497 break;
498 case GLX_MAX_PBUFFER_HEIGHT:
499 config->maxPbufferHeight = *bp++;
500 break;
501 case GLX_MAX_PBUFFER_PIXELS:
502 config->maxPbufferPixels = *bp++;
503 break;
504 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
505 config->optimalPbufferWidth = *bp++;
506 break;
507 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
508 config->optimalPbufferHeight = *bp++;
509 break;
510 case GLX_VISUAL_SELECT_GROUP_SGIX:
511 config->visualSelectGroup = *bp++;
512 break;
513 case GLX_SWAP_METHOD_OML:
514 config->swapMethod = *bp++;
515 break;
516 case GLX_SAMPLE_BUFFERS_SGIS:
517 config->sampleBuffers = *bp++;
518 break;
519 case GLX_SAMPLES_SGIS:
520 config->samples = *bp++;
521 break;
522 case GLX_BIND_TO_TEXTURE_RGB_EXT:
523 config->bindToTextureRgb = *bp++;
524 break;
525 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
526 config->bindToTextureRgba = *bp++;
527 break;
528 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
529 config->bindToMipmapTexture = *bp++;
530 break;
531 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
532 config->bindToTextureTargets = *bp++;
533 break;
534 case GLX_Y_INVERTED_EXT:
535 config->yInverted = *bp++;
536 break;
537 case None:
538 i = count;
539 break;
540 default:
541 break;
542 }
543 }
544
545 config->renderType =
546 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
547
548 config->haveAccumBuffer = ((config->accumRedBits +
549 config->accumGreenBits +
550 config->accumBlueBits +
551 config->accumAlphaBits) > 0);
552 config->haveDepthBuffer = (config->depthBits > 0);
553 config->haveStencilBuffer = (config->stencilBits > 0);
554 }
555
556 static __GLcontextModes *
557 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
558 int screen, GLboolean tagged_only)
559 {
560 INT32 buf[__GLX_TOTAL_CONFIG], *props;
561 unsigned prop_size;
562 __GLcontextModes *modes, *m;
563 int i;
564
565 if (nprops == 0)
566 return NULL;
567
568 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
569
570 /* Check number of properties */
571 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
572 return NULL;
573
574 /* Allocate memory for our config structure */
575 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
576 if (!modes)
577 return NULL;
578
579 prop_size = nprops * __GLX_SIZE_INT32;
580 if (prop_size <= sizeof(buf))
581 props = buf;
582 else
583 props = Xmalloc(prop_size);
584
585 /* Read each config structure and convert it into our format */
586 m = modes;
587 for (i = 0; i < nvisuals; i++) {
588 _XRead(dpy, (char *) props, prop_size);
589 /* Older X servers don't send this so we default it here. */
590 m->drawableType = GLX_WINDOW_BIT;
591 __glXInitializeVisualConfigFromTags(m, nprops, props,
592 tagged_only, GL_TRUE);
593 m->screen = screen;
594 m = m->next;
595 }
596
597 if (props != buf)
598 Xfree(props);
599
600 return modes;
601 }
602
603 static GLboolean
604 getVisualConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
605 {
606 xGLXGetVisualConfigsReq *req;
607 __GLXscreenConfigs *psc;
608 xGLXGetVisualConfigsReply reply;
609
610 LockDisplay(dpy);
611
612 psc = priv->screenConfigs + screen;
613 psc->visuals = NULL;
614 GetReq(GLXGetVisualConfigs, req);
615 req->reqType = priv->majorOpcode;
616 req->glxCode = X_GLXGetVisualConfigs;
617 req->screen = screen;
618
619 if (!_XReply(dpy, (xReply *) & reply, 0, False))
620 goto out;
621
622 psc->visuals = createConfigsFromProperties(dpy,
623 reply.numVisuals,
624 reply.numProps,
625 screen, GL_FALSE);
626
627 out:
628 UnlockDisplay(dpy);
629 return psc->visuals != NULL;
630 }
631
632 static GLboolean
633 getFBConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
634 {
635 xGLXGetFBConfigsReq *fb_req;
636 xGLXGetFBConfigsSGIXReq *sgi_req;
637 xGLXVendorPrivateWithReplyReq *vpreq;
638 xGLXGetFBConfigsReply reply;
639 __GLXscreenConfigs *psc;
640
641 psc = priv->screenConfigs + screen;
642 psc->serverGLXexts =
643 __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
644
645 LockDisplay(dpy);
646
647 psc->configs = NULL;
648 if (atof(priv->serverGLXversion) >= 1.3) {
649 GetReq(GLXGetFBConfigs, fb_req);
650 fb_req->reqType = priv->majorOpcode;
651 fb_req->glxCode = X_GLXGetFBConfigs;
652 fb_req->screen = screen;
653 }
654 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
655 GetReqExtra(GLXVendorPrivateWithReply,
656 sz_xGLXGetFBConfigsSGIXReq +
657 sz_xGLXVendorPrivateWithReplyReq, vpreq);
658 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
659 sgi_req->reqType = priv->majorOpcode;
660 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
661 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
662 sgi_req->screen = screen;
663 }
664 else
665 goto out;
666
667 if (!_XReply(dpy, (xReply *) & reply, 0, False))
668 goto out;
669
670 psc->configs = createConfigsFromProperties(dpy,
671 reply.numFBConfigs,
672 reply.numAttribs * 2,
673 screen, GL_TRUE);
674
675 out:
676 UnlockDisplay(dpy);
677 return psc->configs != NULL;
678 }
679
680 /*
681 ** Allocate the memory for the per screen configs for each screen.
682 ** If that works then fetch the per screen configs data.
683 */
684 static Bool
685 AllocAndFetchScreenConfigs(Display * dpy, __GLXdisplayPrivate * priv)
686 {
687 __GLXscreenConfigs *psc;
688 GLint i, screens;
689
690 /*
691 ** First allocate memory for the array of per screen configs.
692 */
693 screens = ScreenCount(dpy);
694 psc = (__GLXscreenConfigs *) Xmalloc(screens * sizeof(__GLXscreenConfigs));
695 if (!psc) {
696 return GL_FALSE;
697 }
698 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
699 priv->screenConfigs = psc;
700
701 priv->serverGLXversion =
702 __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
703 if (priv->serverGLXversion == NULL) {
704 FreeScreenConfigs(priv);
705 return GL_FALSE;
706 }
707
708 for (i = 0; i < screens; i++, psc++) {
709 getVisualConfigs(dpy, priv, i);
710 getFBConfigs(dpy, priv, i);
711
712 #ifdef GLX_DIRECT_RENDERING
713 psc->scr = i;
714 psc->dpy = dpy;
715 psc->drawHash = __glxHashCreate();
716 if (psc->drawHash == NULL)
717 continue;
718
719 if (priv->dri2Display)
720 psc->driScreen = (*priv->dri2Display->createScreen) (psc, i, priv);
721
722 if (psc->driScreen == NULL && priv->driDisplay)
723 psc->driScreen = (*priv->driDisplay->createScreen) (psc, i, priv);
724
725 if (psc->driScreen == NULL && priv->driswDisplay)
726 psc->driScreen = (*priv->driswDisplay->createScreen) (psc, i, priv);
727
728 if (psc->driScreen == NULL) {
729 __glxHashDestroy(psc->drawHash);
730 psc->drawHash = NULL;
731 }
732 #endif
733 }
734 SyncHandle();
735 return GL_TRUE;
736 }
737
738 /*
739 ** Initialize the client side extension code.
740 */
741 _X_HIDDEN __GLXdisplayPrivate *
742 __glXInitialize(Display * dpy)
743 {
744 XExtDisplayInfo *info = __glXFindDisplay(dpy);
745 XExtData **privList, *private, *found;
746 __GLXdisplayPrivate *dpyPriv;
747 XEDataObject dataObj;
748 int major, minor;
749 #ifdef GLX_DIRECT_RENDERING
750 Bool glx_direct, glx_accel;
751 #endif
752
753 /* The one and only long long lock */
754 __glXLock();
755
756 if (!XextHasExtension(info)) {
757 /* No GLX extension supported by this server. Oh well. */
758 __glXUnlock();
759 XMissingExtension(dpy, __glXExtensionName);
760 return 0;
761 }
762
763 /* See if a display private already exists. If so, return it */
764 dataObj.display = dpy;
765 privList = XEHeadOfExtensionList(dataObj);
766 found = XFindOnExtensionList(privList, info->codes->extension);
767 if (found) {
768 __glXUnlock();
769 return (__GLXdisplayPrivate *) found->private_data;
770 }
771
772 /* See if the versions are compatible */
773 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
774 /* The client and server do not agree on versions. Punt. */
775 __glXUnlock();
776 return 0;
777 }
778
779 /*
780 ** Allocate memory for all the pieces needed for this buffer.
781 */
782 private = (XExtData *) Xmalloc(sizeof(XExtData));
783 if (!private) {
784 __glXUnlock();
785 return 0;
786 }
787 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
788 if (!dpyPriv) {
789 __glXUnlock();
790 Xfree((char *) private);
791 return 0;
792 }
793
794 /*
795 ** Init the display private and then read in the screen config
796 ** structures from the server.
797 */
798 dpyPriv->majorOpcode = info->codes->major_opcode;
799 dpyPriv->majorVersion = major;
800 dpyPriv->minorVersion = minor;
801 dpyPriv->dpy = dpy;
802
803 dpyPriv->serverGLXvendor = 0x0;
804 dpyPriv->serverGLXversion = 0x0;
805
806 #ifdef GLX_DIRECT_RENDERING
807 glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
808 glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
809
810 /*
811 ** Initialize the direct rendering per display data and functions.
812 ** Note: This _must_ be done before calling any other DRI routines
813 ** (e.g., those called in AllocAndFetchScreenConfigs).
814 */
815 if (glx_direct && glx_accel) {
816 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
817 dpyPriv->driDisplay = driCreateDisplay(dpy);
818 }
819 if (glx_direct)
820 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
821 #endif
822
823 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
824 __glXUnlock();
825 Xfree((char *) dpyPriv);
826 Xfree((char *) private);
827 return 0;
828 }
829
830 /*
831 ** Fill in the private structure. This is the actual structure that
832 ** hangs off of the Display structure. Our private structure is
833 ** referred to by this structure. Got that?
834 */
835 private->number = info->codes->extension;
836 private->next = 0;
837 private->free_private = __glXFreeDisplayPrivate;
838 private->private_data = (char *) dpyPriv;
839 XAddToExtensionList(privList, private);
840
841 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
842 __glXClientInfo(dpy, dpyPriv->majorOpcode);
843 }
844 __glXUnlock();
845
846 return dpyPriv;
847 }
848
849 /*
850 ** Setup for sending a GLX command on dpy. Make sure the extension is
851 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
852 */
853 _X_HIDDEN CARD8
854 __glXSetupForCommand(Display * dpy)
855 {
856 GLXContext gc;
857 __GLXdisplayPrivate *priv;
858
859 /* If this thread has a current context, flush its rendering commands */
860 gc = __glXGetCurrentContext();
861 if (gc->currentDpy) {
862 /* Flush rendering buffer of the current context, if any */
863 (void) __glXFlushRenderBuffer(gc, gc->pc);
864
865 if (gc->currentDpy == dpy) {
866 /* Use opcode from gc because its right */
867 return gc->majorOpcode;
868 }
869 else {
870 /*
871 ** Have to get info about argument dpy because it might be to
872 ** a different server
873 */
874 }
875 }
876
877 /* Forced to lookup extension via the slow initialize route */
878 priv = __glXInitialize(dpy);
879 if (!priv) {
880 return 0;
881 }
882 return priv->majorOpcode;
883 }
884
885 /**
886 * Flush the drawing command transport buffer.
887 *
888 * \param ctx Context whose transport buffer is to be flushed.
889 * \param pc Pointer to first unused buffer location.
890 *
891 * \todo
892 * Modify this function to use \c ctx->pc instead of the explicit
893 * \c pc parameter.
894 */
895 _X_HIDDEN GLubyte *
896 __glXFlushRenderBuffer(__GLXcontext * ctx, GLubyte * pc)
897 {
898 Display *const dpy = ctx->currentDpy;
899 #ifdef USE_XCB
900 xcb_connection_t *c = XGetXCBConnection(dpy);
901 #else
902 xGLXRenderReq *req;
903 #endif /* USE_XCB */
904 const GLint size = pc - ctx->buf;
905
906 if ((dpy != NULL) && (size > 0)) {
907 #ifdef USE_XCB
908 xcb_glx_render(c, ctx->currentContextTag, size,
909 (const uint8_t *) ctx->buf);
910 #else
911 /* Send the entire buffer as an X request */
912 LockDisplay(dpy);
913 GetReq(GLXRender, req);
914 req->reqType = ctx->majorOpcode;
915 req->glxCode = X_GLXRender;
916 req->contextTag = ctx->currentContextTag;
917 req->length += (size + 3) >> 2;
918 _XSend(dpy, (char *) ctx->buf, size);
919 UnlockDisplay(dpy);
920 SyncHandle();
921 #endif
922 }
923
924 /* Reset pointer and return it */
925 ctx->pc = ctx->buf;
926 return ctx->pc;
927 }
928
929
930 /**
931 * Send a portion of a GLXRenderLarge command to the server. The advantage of
932 * this function over \c __glXSendLargeCommand is that callers can use the
933 * data buffer in the GLX context and may be able to avoid allocating an
934 * extra buffer. The disadvantage is the clients will have to do more
935 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
936 *
937 * \sa __glXSendLargeCommand
938 *
939 * \param gc GLX context
940 * \param requestNumber Which part of the whole command is this? The first
941 * request is 1.
942 * \param totalRequests How many requests will there be?
943 * \param data Command data.
944 * \param dataLen Size, in bytes, of the command data.
945 */
946 _X_HIDDEN void
947 __glXSendLargeChunk(__GLXcontext * gc, GLint requestNumber,
948 GLint totalRequests, const GLvoid * data, GLint dataLen)
949 {
950 Display *dpy = gc->currentDpy;
951 #ifdef USE_XCB
952 xcb_connection_t *c = XGetXCBConnection(dpy);
953 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
954 totalRequests, dataLen, data);
955 #else
956 xGLXRenderLargeReq *req;
957
958 if (requestNumber == 1) {
959 LockDisplay(dpy);
960 }
961
962 GetReq(GLXRenderLarge, req);
963 req->reqType = gc->majorOpcode;
964 req->glxCode = X_GLXRenderLarge;
965 req->contextTag = gc->currentContextTag;
966 req->length += (dataLen + 3) >> 2;
967 req->requestNumber = requestNumber;
968 req->requestTotal = totalRequests;
969 req->dataBytes = dataLen;
970 Data(dpy, data, dataLen);
971
972 if (requestNumber == totalRequests) {
973 UnlockDisplay(dpy);
974 SyncHandle();
975 }
976 #endif /* USE_XCB */
977 }
978
979
980 /**
981 * Send a command that is too large for the GLXRender protocol request.
982 *
983 * Send a large command, one that is too large for some reason to
984 * send using the GLXRender protocol request. One reason to send
985 * a large command is to avoid copying the data.
986 *
987 * \param ctx GLX context
988 * \param header Header data.
989 * \param headerLen Size, in bytes, of the header data. It is assumed that
990 * the header data will always be small enough to fit in
991 * a single X protocol packet.
992 * \param data Command data.
993 * \param dataLen Size, in bytes, of the command data.
994 */
995 _X_HIDDEN void
996 __glXSendLargeCommand(__GLXcontext * ctx,
997 const GLvoid * header, GLint headerLen,
998 const GLvoid * data, GLint dataLen)
999 {
1000 GLint maxSize;
1001 GLint totalRequests, requestNumber;
1002
1003 /*
1004 ** Calculate the maximum amount of data can be stuffed into a single
1005 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1006 ** packet size minus sz_xGLXRenderReq.
1007 */
1008 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1009 totalRequests = 1 + (dataLen / maxSize);
1010 if (dataLen % maxSize)
1011 totalRequests++;
1012
1013 /*
1014 ** Send all of the command, except the large array, as one request.
1015 */
1016 assert(headerLen <= maxSize);
1017 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1018
1019 /*
1020 ** Send enough requests until the whole array is sent.
1021 */
1022 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
1023 requestNumber++) {
1024 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1025 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1026 dataLen -= maxSize;
1027 assert(dataLen > 0);
1028 }
1029
1030 assert(dataLen <= maxSize);
1031 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1032 }
1033
1034 /************************************************************************/
1035
1036 #ifdef DEBUG
1037 _X_HIDDEN void
1038 __glXDumpDrawBuffer(__GLXcontext * ctx)
1039 {
1040 GLubyte *p = ctx->buf;
1041 GLubyte *end = ctx->pc;
1042 GLushort opcode, length;
1043
1044 while (p < end) {
1045 /* Fetch opcode */
1046 opcode = *((GLushort *) p);
1047 length = *((GLushort *) (p + 2));
1048 printf("%2x: %5d: ", opcode, length);
1049 length -= 4;
1050 p += 4;
1051 while (length > 0) {
1052 printf("%08x ", *((unsigned *) p));
1053 p += 4;
1054 length -= 4;
1055 }
1056 printf("\n");
1057 }
1058 }
1059 #endif