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