Merge remote branch 'origin/gallium-0.2' into gallium-0.2
[mesa.git] / src / glx / x11 / glxext.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
4 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice including the dates of first publication and
14 * either this permission notice or a reference to
15 * http://oss.sgi.com/projects/FreeB/
16 * shall be included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Except as contained in this notice, the name of Silicon Graphics, Inc.
27 * shall not be used in advertising or otherwise to promote the sale, use or
28 * other dealings in this Software without prior written authorization from
29 * Silicon Graphics, Inc.
30 */
31
32 /**
33 * \file glxext.c
34 * GLX protocol interface boot-strap code.
35 *
36 * Direct rendering support added by Precision Insight, Inc.
37 *
38 * \author Kevin E. Martin <kevin@precisioninsight.com>
39 */
40
41 #include <assert.h>
42 #include "glxclient.h"
43 #include <X11/extensions/Xext.h>
44 #include <X11/extensions/extutil.h>
45 #include "glapi.h"
46 #include "glxextensions.h"
47 #include "glcontextmodes.h"
48
49 #ifdef USE_XCB
50 #include <X11/Xlib-xcb.h>
51 #include <xcb/xcb.h>
52 #include <xcb/glx.h>
53 #endif
54
55
56 #ifdef DEBUG
57 void __glXDumpDrawBuffer(__GLXcontext * ctx);
58 #endif
59
60 #ifdef USE_SPARC_ASM
61 static void _glx_mesa_init_sparc_glapi_relocs(void);
62 static int _mesa_sparc_needs_init = 1;
63 #define INIT_MESA_SPARC do { \
64 if (_mesa_sparc_needs_init) { \
65 _glx_mesa_init_sparc_glapi_relocs(); \
66 _mesa_sparc_needs_init = 0; \
67 } \
68 } while(0)
69 #else
70 #define INIT_MESA_SPARC do { } while(0)
71 #endif
72
73 /*
74 ** You can set this cell to 1 to force the gl drawing stuff to be
75 ** one command per packet
76 */
77 _X_HIDDEN int __glXDebug = 0;
78
79 /* Extension required boiler plate */
80
81 static char *__glXExtensionName = GLX_EXTENSION_NAME;
82 XExtensionInfo *__glXExtensionInfo = NULL;
83
84 static /* const */ char *error_list[] = {
85 "GLXBadContext",
86 "GLXBadContextState",
87 "GLXBadDrawable",
88 "GLXBadPixmap",
89 "GLXBadContextTag",
90 "GLXBadCurrentWindow",
91 "GLXBadRenderRequest",
92 "GLXBadLargeRequest",
93 "GLXUnsupportedPrivateRequest",
94 "GLXBadFBConfig",
95 "GLXBadPbuffer",
96 "GLXBadCurrentDrawable",
97 "GLXBadWindow",
98 };
99
100 static int
101 __glXCloseDisplay(Display * dpy, XExtCodes * codes)
102 {
103 GLXContext gc;
104
105 gc = __glXGetCurrentContext();
106 if (dpy == gc->currentDpy) {
107 __glXSetCurrentContextNull();
108 __glXFreeContext(gc);
109 }
110
111 return XextRemoveDisplay(__glXExtensionInfo, dpy);
112 }
113
114
115 static
116 XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
117 __GLX_NUMBER_ERRORS, error_list)
118
119 static /* const */ XExtensionHooks __glXExtensionHooks = {
120 NULL, /* create_gc */
121 NULL, /* copy_gc */
122 NULL, /* flush_gc */
123 NULL, /* free_gc */
124 NULL, /* create_font */
125 NULL, /* free_font */
126 __glXCloseDisplay, /* close_display */
127 NULL, /* wire_to_event */
128 NULL, /* event_to_wire */
129 NULL, /* error */
130 __glXErrorString, /* error_string */
131 };
132
133 static
134 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
135 __glXExtensionName, &__glXExtensionHooks,
136 __GLX_NUMBER_EVENTS, NULL)
137
138 /************************************************************************/
139 /*
140 ** Free the per screen configs data as well as the array of
141 ** __glXScreenConfigs.
142 */
143 static void FreeScreenConfigs(__GLXdisplayPrivate * priv)
144 {
145 __GLXscreenConfigs *psc;
146 GLint i, screens;
147
148 /* Free screen configuration information */
149 psc = priv->screenConfigs;
150 screens = ScreenCount(priv->dpy);
151 for (i = 0; i < screens; i++, psc++) {
152 if (psc->configs) {
153 _gl_context_modes_destroy(psc->configs);
154 if (psc->effectiveGLXexts)
155 Xfree(psc->effectiveGLXexts);
156 psc->configs = NULL; /* NOTE: just for paranoia */
157 }
158 if (psc->visuals) {
159 _gl_context_modes_destroy(psc->visuals);
160 psc->visuals = NULL; /* NOTE: just for paranoia */
161 }
162 Xfree((char *) psc->serverGLXexts);
163
164 #ifdef GLX_DIRECT_RENDERING
165 if (psc->driScreen) {
166 psc->driScreen->destroyScreen(psc);
167 __glxHashDestroy(psc->drawHash);
168 XFree(psc->driScreen);
169 psc->driScreen = NULL;
170 }
171 #endif
172 }
173 XFree((char *) priv->screenConfigs);
174 priv->screenConfigs = NULL;
175 }
176
177 /*
178 ** Release the private memory referred to in a display private
179 ** structure. The caller will free the extension structure.
180 */
181 static int
182 __glXFreeDisplayPrivate(XExtData * extension)
183 {
184 __GLXdisplayPrivate *priv;
185
186 priv = (__GLXdisplayPrivate *) extension->private_data;
187 FreeScreenConfigs(priv);
188 if (priv->serverGLXvendor) {
189 Xfree((char *) priv->serverGLXvendor);
190 priv->serverGLXvendor = 0x0; /* to protect against double free's */
191 }
192 if (priv->serverGLXversion) {
193 Xfree((char *) priv->serverGLXversion);
194 priv->serverGLXversion = 0x0; /* to protect against double free's */
195 }
196
197 #ifdef GLX_DIRECT_RENDERING
198 /* Free the direct rendering per display data */
199 if (priv->driswDisplay)
200 (*priv->driswDisplay->destroyDisplay) (priv->driswDisplay);
201 priv->driswDisplay = NULL;
202
203 if (priv->driDisplay)
204 (*priv->driDisplay->destroyDisplay) (priv->driDisplay);
205 priv->driDisplay = NULL;
206
207 if (priv->dri2Display)
208 (*priv->dri2Display->destroyDisplay) (priv->dri2Display);
209 priv->dri2Display = NULL;
210 #endif
211
212 Xfree((char *) priv);
213 return 0;
214 }
215
216 /************************************************************************/
217
218 /*
219 ** Query the version of the GLX extension. This procedure works even if
220 ** the client extension is not completely set up.
221 */
222 static Bool
223 QueryVersion(Display * dpy, int opcode, int *major, int *minor)
224 {
225 xGLXQueryVersionReq *req;
226 xGLXQueryVersionReply reply;
227
228 /* Send the glXQueryVersion request */
229 LockDisplay(dpy);
230 GetReq(GLXQueryVersion, req);
231 req->reqType = opcode;
232 req->glxCode = X_GLXQueryVersion;
233 req->majorVersion = GLX_MAJOR_VERSION;
234 req->minorVersion = GLX_MINOR_VERSION;
235 _XReply(dpy, (xReply *) & reply, 0, False);
236 UnlockDisplay(dpy);
237 SyncHandle();
238
239 if (reply.majorVersion != GLX_MAJOR_VERSION) {
240 /*
241 ** The server does not support the same major release as this
242 ** client.
243 */
244 return GL_FALSE;
245 }
246 *major = reply.majorVersion;
247 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
248 return GL_TRUE;
249 }
250
251
252 _X_HIDDEN void
253 __glXInitializeVisualConfigFromTags(__GLcontextModes * config, int count,
254 const INT32 * bp, Bool tagged_only,
255 Bool fbconfig_style_tags)
256 {
257 int i;
258
259 if (!tagged_only) {
260 /* Copy in the first set of properties */
261 config->visualID = *bp++;
262
263 config->visualType = _gl_convert_from_x_visual_type(*bp++);
264
265 config->rgbMode = *bp++;
266
267 config->redBits = *bp++;
268 config->greenBits = *bp++;
269 config->blueBits = *bp++;
270 config->alphaBits = *bp++;
271 config->accumRedBits = *bp++;
272 config->accumGreenBits = *bp++;
273 config->accumBlueBits = *bp++;
274 config->accumAlphaBits = *bp++;
275
276 config->doubleBufferMode = *bp++;
277 config->stereoMode = *bp++;
278
279 config->rgbBits = *bp++;
280 config->depthBits = *bp++;
281 config->stencilBits = *bp++;
282 config->numAuxBuffers = *bp++;
283 config->level = *bp++;
284
285 count -= __GLX_MIN_CONFIG_PROPS;
286 }
287
288 /*
289 ** Additional properties may be in a list at the end
290 ** of the reply. They are in pairs of property type
291 ** and property value.
292 */
293
294 #define FETCH_OR_SET(tag) \
295 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
296
297 for (i = 0; i < count; i += 2) {
298 switch (*bp++) {
299 case GLX_RGBA:
300 FETCH_OR_SET(rgbMode);
301 break;
302 case GLX_BUFFER_SIZE:
303 config->rgbBits = *bp++;
304 break;
305 case GLX_LEVEL:
306 config->level = *bp++;
307 break;
308 case GLX_DOUBLEBUFFER:
309 FETCH_OR_SET(doubleBufferMode);
310 break;
311 case GLX_STEREO:
312 FETCH_OR_SET(stereoMode);
313 break;
314 case GLX_AUX_BUFFERS:
315 config->numAuxBuffers = *bp++;
316 break;
317 case GLX_RED_SIZE:
318 config->redBits = *bp++;
319 break;
320 case GLX_GREEN_SIZE:
321 config->greenBits = *bp++;
322 break;
323 case GLX_BLUE_SIZE:
324 config->blueBits = *bp++;
325 break;
326 case GLX_ALPHA_SIZE:
327 config->alphaBits = *bp++;
328 break;
329 case GLX_DEPTH_SIZE:
330 config->depthBits = *bp++;
331 break;
332 case GLX_STENCIL_SIZE:
333 config->stencilBits = *bp++;
334 break;
335 case GLX_ACCUM_RED_SIZE:
336 config->accumRedBits = *bp++;
337 break;
338 case GLX_ACCUM_GREEN_SIZE:
339 config->accumGreenBits = *bp++;
340 break;
341 case GLX_ACCUM_BLUE_SIZE:
342 config->accumBlueBits = *bp++;
343 break;
344 case GLX_ACCUM_ALPHA_SIZE:
345 config->accumAlphaBits = *bp++;
346 break;
347 case GLX_VISUAL_CAVEAT_EXT:
348 config->visualRating = *bp++;
349 break;
350 case GLX_X_VISUAL_TYPE:
351 config->visualType = *bp++;
352 break;
353 case GLX_TRANSPARENT_TYPE:
354 config->transparentPixel = *bp++;
355 break;
356 case GLX_TRANSPARENT_INDEX_VALUE:
357 config->transparentIndex = *bp++;
358 break;
359 case GLX_TRANSPARENT_RED_VALUE:
360 config->transparentRed = *bp++;
361 break;
362 case GLX_TRANSPARENT_GREEN_VALUE:
363 config->transparentGreen = *bp++;
364 break;
365 case GLX_TRANSPARENT_BLUE_VALUE:
366 config->transparentBlue = *bp++;
367 break;
368 case GLX_TRANSPARENT_ALPHA_VALUE:
369 config->transparentAlpha = *bp++;
370 break;
371 case GLX_VISUAL_ID:
372 config->visualID = *bp++;
373 break;
374 case GLX_DRAWABLE_TYPE:
375 config->drawableType = *bp++;
376 break;
377 case GLX_RENDER_TYPE:
378 config->renderType = *bp++;
379 break;
380 case GLX_X_RENDERABLE:
381 config->xRenderable = *bp++;
382 break;
383 case GLX_FBCONFIG_ID:
384 config->fbconfigID = *bp++;
385 break;
386 case GLX_MAX_PBUFFER_WIDTH:
387 config->maxPbufferWidth = *bp++;
388 break;
389 case GLX_MAX_PBUFFER_HEIGHT:
390 config->maxPbufferHeight = *bp++;
391 break;
392 case GLX_MAX_PBUFFER_PIXELS:
393 config->maxPbufferPixels = *bp++;
394 break;
395 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
396 config->optimalPbufferWidth = *bp++;
397 break;
398 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
399 config->optimalPbufferHeight = *bp++;
400 break;
401 case GLX_VISUAL_SELECT_GROUP_SGIX:
402 config->visualSelectGroup = *bp++;
403 break;
404 case GLX_SWAP_METHOD_OML:
405 config->swapMethod = *bp++;
406 break;
407 case GLX_SAMPLE_BUFFERS_SGIS:
408 config->sampleBuffers = *bp++;
409 break;
410 case GLX_SAMPLES_SGIS:
411 config->samples = *bp++;
412 break;
413 case GLX_BIND_TO_TEXTURE_RGB_EXT:
414 config->bindToTextureRgb = *bp++;
415 break;
416 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
417 config->bindToTextureRgba = *bp++;
418 break;
419 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
420 config->bindToMipmapTexture = *bp++;
421 break;
422 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
423 config->bindToTextureTargets = *bp++;
424 break;
425 case GLX_Y_INVERTED_EXT:
426 config->yInverted = *bp++;
427 break;
428 case None:
429 i = count;
430 break;
431 default:
432 break;
433 }
434 }
435
436 config->renderType =
437 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
438
439 config->haveAccumBuffer = ((config->accumRedBits +
440 config->accumGreenBits +
441 config->accumBlueBits +
442 config->accumAlphaBits) > 0);
443 config->haveDepthBuffer = (config->depthBits > 0);
444 config->haveStencilBuffer = (config->stencilBits > 0);
445 }
446
447 static __GLcontextModes *
448 createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
449 int screen, GLboolean tagged_only)
450 {
451 INT32 buf[__GLX_TOTAL_CONFIG], *props;
452 unsigned prop_size;
453 __GLcontextModes *modes, *m;
454 int i;
455
456 if (nprops == 0)
457 return NULL;
458
459 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
460
461 /* Check number of properties */
462 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
463 return NULL;
464
465 /* Allocate memory for our config structure */
466 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
467 if (!modes)
468 return NULL;
469
470 prop_size = nprops * __GLX_SIZE_INT32;
471 if (prop_size <= sizeof(buf))
472 props = buf;
473 else
474 props = Xmalloc(prop_size);
475
476 /* Read each config structure and convert it into our format */
477 m = modes;
478 for (i = 0; i < nvisuals; i++) {
479 _XRead(dpy, (char *) props, prop_size);
480 /* Older X servers don't send this so we default it here. */
481 m->drawableType = GLX_WINDOW_BIT;
482 __glXInitializeVisualConfigFromTags(m, nprops, props,
483 tagged_only, GL_TRUE);
484 m->screen = screen;
485 m = m->next;
486 }
487
488 if (props != buf)
489 Xfree(props);
490
491 return modes;
492 }
493
494 static GLboolean
495 getVisualConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
496 {
497 xGLXGetVisualConfigsReq *req;
498 __GLXscreenConfigs *psc;
499 xGLXGetVisualConfigsReply reply;
500
501 LockDisplay(dpy);
502
503 psc = priv->screenConfigs + screen;
504 psc->visuals = NULL;
505 GetReq(GLXGetVisualConfigs, req);
506 req->reqType = priv->majorOpcode;
507 req->glxCode = X_GLXGetVisualConfigs;
508 req->screen = screen;
509
510 if (!_XReply(dpy, (xReply *) & reply, 0, False))
511 goto out;
512
513 psc->visuals = createConfigsFromProperties(dpy,
514 reply.numVisuals,
515 reply.numProps,
516 screen, GL_FALSE);
517
518 out:
519 UnlockDisplay(dpy);
520 return psc->visuals != NULL;
521 }
522
523 static GLboolean
524 getFBConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
525 {
526 xGLXGetFBConfigsReq *fb_req;
527 xGLXGetFBConfigsSGIXReq *sgi_req;
528 xGLXVendorPrivateWithReplyReq *vpreq;
529 xGLXGetFBConfigsReply reply;
530 __GLXscreenConfigs *psc;
531
532 psc = priv->screenConfigs + screen;
533 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
534 X_GLXQueryServerString,
535 screen, GLX_EXTENSIONS);
536
537 LockDisplay(dpy);
538
539 psc->configs = NULL;
540 if (atof(priv->serverGLXversion) >= 1.3) {
541 GetReq(GLXGetFBConfigs, fb_req);
542 fb_req->reqType = priv->majorOpcode;
543 fb_req->glxCode = X_GLXGetFBConfigs;
544 fb_req->screen = screen;
545 }
546 else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
547 GetReqExtra(GLXVendorPrivateWithReply,
548 sz_xGLXGetFBConfigsSGIXReq +
549 sz_xGLXVendorPrivateWithReplyReq, vpreq);
550 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
551 sgi_req->reqType = priv->majorOpcode;
552 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
553 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
554 sgi_req->screen = screen;
555 }
556 else
557 goto out;
558
559 if (!_XReply(dpy, (xReply *) & reply, 0, False))
560 goto out;
561
562 psc->configs = createConfigsFromProperties(dpy,
563 reply.numFBConfigs,
564 reply.numAttribs * 2,
565 screen, GL_TRUE);
566
567 out:
568 UnlockDisplay(dpy);
569 return psc->configs != NULL;
570 }
571
572 /*
573 ** Allocate the memory for the per screen configs for each screen.
574 ** If that works then fetch the per screen configs data.
575 */
576 static Bool
577 AllocAndFetchScreenConfigs(Display * dpy, __GLXdisplayPrivate * priv)
578 {
579 __GLXscreenConfigs *psc;
580 GLint i, screens;
581
582 /*
583 ** First allocate memory for the array of per screen configs.
584 */
585 screens = ScreenCount(dpy);
586 psc = (__GLXscreenConfigs *) Xmalloc(screens * sizeof(__GLXscreenConfigs));
587 if (!psc) {
588 return GL_FALSE;
589 }
590 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
591 priv->screenConfigs = psc;
592
593 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
594 X_GLXQueryServerString,
595 0, GLX_VERSION);
596 if (priv->serverGLXversion == NULL) {
597 FreeScreenConfigs(priv);
598 return GL_FALSE;
599 }
600
601 for (i = 0; i < screens; i++, psc++) {
602 getVisualConfigs(dpy, priv, i);
603 getFBConfigs(dpy, priv, i);
604
605 #ifdef GLX_DIRECT_RENDERING
606 psc->scr = i;
607 psc->dpy = dpy;
608 psc->drawHash = __glxHashCreate();
609 if (psc->drawHash == NULL)
610 continue;
611
612 if (priv->dri2Display)
613 psc->driScreen = (*priv->dri2Display->createScreen) (psc, i, priv);
614
615 if (psc->driScreen == NULL && priv->driDisplay)
616 psc->driScreen = (*priv->driDisplay->createScreen) (psc, i, priv);
617
618 if (psc->driScreen == NULL && priv->driswDisplay)
619 psc->driScreen = (*priv->driswDisplay->createScreen) (psc, i, priv);
620
621 if (psc->driScreen == NULL) {
622 __glxHashDestroy(psc->drawHash);
623 psc->drawHash = NULL;
624 }
625 #endif
626 }
627 SyncHandle();
628 return GL_TRUE;
629 }
630
631 /*
632 ** Initialize the client side extension code.
633 */
634 _X_HIDDEN __GLXdisplayPrivate *
635 __glXInitialize(Display * dpy)
636 {
637 XExtDisplayInfo *info = __glXFindDisplay(dpy);
638 XExtData **privList, *private, *found;
639 __GLXdisplayPrivate *dpyPriv;
640 XEDataObject dataObj;
641 int major, minor;
642 #ifdef GLX_DIRECT_RENDERING
643 Bool glx_direct, glx_accel;
644 #endif
645
646 #if defined(USE_XTHREADS)
647 {
648 static int firstCall = 1;
649 if (firstCall) {
650 /* initialize the GLX mutexes */
651 xmutex_init(&__glXmutex);
652 firstCall = 0;
653 }
654 }
655 #endif
656
657 INIT_MESA_SPARC;
658 /* The one and only long long lock */
659 __glXLock();
660
661 if (!XextHasExtension(info)) {
662 /* No GLX extension supported by this server. Oh well. */
663 __glXUnlock();
664 XMissingExtension(dpy, __glXExtensionName);
665 return 0;
666 }
667
668 /* See if a display private already exists. If so, return it */
669 dataObj.display = dpy;
670 privList = XEHeadOfExtensionList(dataObj);
671 found = XFindOnExtensionList(privList, info->codes->extension);
672 if (found) {
673 __glXUnlock();
674 return (__GLXdisplayPrivate *) found->private_data;
675 }
676
677 /* See if the versions are compatible */
678 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
679 /* The client and server do not agree on versions. Punt. */
680 __glXUnlock();
681 return 0;
682 }
683
684 /*
685 ** Allocate memory for all the pieces needed for this buffer.
686 */
687 private = (XExtData *) Xmalloc(sizeof(XExtData));
688 if (!private) {
689 __glXUnlock();
690 return 0;
691 }
692 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
693 if (!dpyPriv) {
694 __glXUnlock();
695 Xfree((char *) private);
696 return 0;
697 }
698
699 /*
700 ** Init the display private and then read in the screen config
701 ** structures from the server.
702 */
703 dpyPriv->majorOpcode = info->codes->major_opcode;
704 dpyPriv->majorVersion = major;
705 dpyPriv->minorVersion = minor;
706 dpyPriv->dpy = dpy;
707
708 dpyPriv->serverGLXvendor = 0x0;
709 dpyPriv->serverGLXversion = 0x0;
710
711 #ifdef GLX_DIRECT_RENDERING
712 glx_direct = (getenv("LIBGL_ALWAYS_INDIRECT") == NULL);
713 glx_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
714
715 /*
716 ** Initialize the direct rendering per display data and functions.
717 ** Note: This _must_ be done before calling any other DRI routines
718 ** (e.g., those called in AllocAndFetchScreenConfigs).
719 */
720 if (glx_direct && glx_accel) {
721 dpyPriv->dri2Display = dri2CreateDisplay(dpy);
722 dpyPriv->driDisplay = driCreateDisplay(dpy);
723 }
724 if (glx_direct)
725 dpyPriv->driswDisplay = driswCreateDisplay(dpy);
726 #endif
727
728 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
729 __glXUnlock();
730 Xfree((char *) dpyPriv);
731 Xfree((char *) private);
732 return 0;
733 }
734
735 /*
736 ** Fill in the private structure. This is the actual structure that
737 ** hangs off of the Display structure. Our private structure is
738 ** referred to by this structure. Got that?
739 */
740 private->number = info->codes->extension;
741 private->next = 0;
742 private->free_private = __glXFreeDisplayPrivate;
743 private->private_data = (char *) dpyPriv;
744 XAddToExtensionList(privList, private);
745
746 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
747 __glXClientInfo(dpy, dpyPriv->majorOpcode);
748 }
749 __glXUnlock();
750
751 return dpyPriv;
752 }
753
754 /*
755 ** Setup for sending a GLX command on dpy. Make sure the extension is
756 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
757 */
758 _X_HIDDEN CARD8
759 __glXSetupForCommand(Display * dpy)
760 {
761 GLXContext gc;
762 __GLXdisplayPrivate *priv;
763
764 /* If this thread has a current context, flush its rendering commands */
765 gc = __glXGetCurrentContext();
766 if (gc->currentDpy) {
767 /* Flush rendering buffer of the current context, if any */
768 (void) __glXFlushRenderBuffer(gc, gc->pc);
769
770 if (gc->currentDpy == dpy) {
771 /* Use opcode from gc because its right */
772 INIT_MESA_SPARC;
773 return gc->majorOpcode;
774 }
775 else {
776 /*
777 ** Have to get info about argument dpy because it might be to
778 ** a different server
779 */
780 }
781 }
782
783 /* Forced to lookup extension via the slow initialize route */
784 priv = __glXInitialize(dpy);
785 if (!priv) {
786 return 0;
787 }
788 return priv->majorOpcode;
789 }
790
791 /**
792 * Flush the drawing command transport buffer.
793 *
794 * \param ctx Context whose transport buffer is to be flushed.
795 * \param pc Pointer to first unused buffer location.
796 *
797 * \todo
798 * Modify this function to use \c ctx->pc instead of the explicit
799 * \c pc parameter.
800 */
801 _X_HIDDEN GLubyte *
802 __glXFlushRenderBuffer(__GLXcontext * ctx, GLubyte * pc)
803 {
804 Display *const dpy = ctx->currentDpy;
805 #ifdef USE_XCB
806 xcb_connection_t *c = XGetXCBConnection(dpy);
807 #else
808 xGLXRenderReq *req;
809 #endif /* USE_XCB */
810 const GLint size = pc - ctx->buf;
811
812 if ((dpy != NULL) && (size > 0)) {
813 #ifdef USE_XCB
814 xcb_glx_render(c, ctx->currentContextTag, size,
815 (const uint8_t *) ctx->buf);
816 #else
817 /* Send the entire buffer as an X request */
818 LockDisplay(dpy);
819 GetReq(GLXRender, req);
820 req->reqType = ctx->majorOpcode;
821 req->glxCode = X_GLXRender;
822 req->contextTag = ctx->currentContextTag;
823 req->length += (size + 3) >> 2;
824 _XSend(dpy, (char *) ctx->buf, size);
825 UnlockDisplay(dpy);
826 SyncHandle();
827 #endif
828 }
829
830 /* Reset pointer and return it */
831 ctx->pc = ctx->buf;
832 return ctx->pc;
833 }
834
835
836 /**
837 * Send a portion of a GLXRenderLarge command to the server. The advantage of
838 * this function over \c __glXSendLargeCommand is that callers can use the
839 * data buffer in the GLX context and may be able to avoid allocating an
840 * extra buffer. The disadvantage is the clients will have to do more
841 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
842 *
843 * \sa __glXSendLargeCommand
844 *
845 * \param gc GLX context
846 * \param requestNumber Which part of the whole command is this? The first
847 * request is 1.
848 * \param totalRequests How many requests will there be?
849 * \param data Command data.
850 * \param dataLen Size, in bytes, of the command data.
851 */
852 _X_HIDDEN void
853 __glXSendLargeChunk(__GLXcontext * gc, GLint requestNumber,
854 GLint totalRequests, const GLvoid * data, GLint dataLen)
855 {
856 Display *dpy = gc->currentDpy;
857 #ifdef USE_XCB
858 xcb_connection_t *c = XGetXCBConnection(dpy);
859 xcb_glx_render_large(c, gc->currentContextTag, requestNumber,
860 totalRequests, dataLen, data);
861 #else
862 xGLXRenderLargeReq *req;
863
864 if (requestNumber == 1) {
865 LockDisplay(dpy);
866 }
867
868 GetReq(GLXRenderLarge, req);
869 req->reqType = gc->majorOpcode;
870 req->glxCode = X_GLXRenderLarge;
871 req->contextTag = gc->currentContextTag;
872 req->length += (dataLen + 3) >> 2;
873 req->requestNumber = requestNumber;
874 req->requestTotal = totalRequests;
875 req->dataBytes = dataLen;
876 Data(dpy, data, dataLen);
877
878 if (requestNumber == totalRequests) {
879 UnlockDisplay(dpy);
880 SyncHandle();
881 }
882 #endif /* USE_XCB */
883 }
884
885
886 /**
887 * Send a command that is too large for the GLXRender protocol request.
888 *
889 * Send a large command, one that is too large for some reason to
890 * send using the GLXRender protocol request. One reason to send
891 * a large command is to avoid copying the data.
892 *
893 * \param ctx GLX context
894 * \param header Header data.
895 * \param headerLen Size, in bytes, of the header data. It is assumed that
896 * the header data will always be small enough to fit in
897 * a single X protocol packet.
898 * \param data Command data.
899 * \param dataLen Size, in bytes, of the command data.
900 */
901 _X_HIDDEN void
902 __glXSendLargeCommand(__GLXcontext * ctx,
903 const GLvoid * header, GLint headerLen,
904 const GLvoid * data, GLint dataLen)
905 {
906 GLint maxSize;
907 GLint totalRequests, requestNumber;
908
909 /*
910 ** Calculate the maximum amount of data can be stuffed into a single
911 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
912 ** packet size minus sz_xGLXRenderReq.
913 */
914 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
915 totalRequests = 1 + (dataLen / maxSize);
916 if (dataLen % maxSize)
917 totalRequests++;
918
919 /*
920 ** Send all of the command, except the large array, as one request.
921 */
922 assert(headerLen <= maxSize);
923 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
924
925 /*
926 ** Send enough requests until the whole array is sent.
927 */
928 for (requestNumber = 2; requestNumber <= (totalRequests - 1);
929 requestNumber++) {
930 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
931 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
932 dataLen -= maxSize;
933 assert(dataLen > 0);
934 }
935
936 assert(dataLen <= maxSize);
937 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
938 }
939
940 /************************************************************************/
941
942 #ifdef DEBUG
943 _X_HIDDEN void
944 __glXDumpDrawBuffer(__GLXcontext * ctx)
945 {
946 GLubyte *p = ctx->buf;
947 GLubyte *end = ctx->pc;
948 GLushort opcode, length;
949
950 while (p < end) {
951 /* Fetch opcode */
952 opcode = *((GLushort *) p);
953 length = *((GLushort *) (p + 2));
954 printf("%2x: %5d: ", opcode, length);
955 length -= 4;
956 p += 4;
957 while (length > 0) {
958 printf("%08x ", *((unsigned *) p));
959 p += 4;
960 length -= 4;
961 }
962 printf("\n");
963 }
964 }
965 #endif
966
967 #ifdef USE_SPARC_ASM
968 /*
969 * This is where our dispatch table's bounds are.
970 * And the static mesa_init is taken directly from
971 * Mesa's 'sparc.c' initializer.
972 *
973 * We need something like this here, because this version
974 * of openGL/glx never initializes a Mesa context, and so
975 * the address of the dispatch table pointer never gets stuffed
976 * into the dispatch jump table otherwise.
977 *
978 * It matters only on SPARC, and only if you are using assembler
979 * code instead of C-code indirect dispatch.
980 *
981 * -- FEM, 04.xii.03
982 */
983 extern unsigned int _mesa_sparc_glapi_begin;
984 extern unsigned int _mesa_sparc_glapi_end;
985 extern void __glapi_sparc_icache_flush(unsigned int *);
986
987 static void
988 _glx_mesa_init_sparc_glapi_relocs(void)
989 {
990 unsigned int *insn_ptr, *end_ptr;
991 unsigned long disp_addr;
992
993 insn_ptr = &_mesa_sparc_glapi_begin;
994 end_ptr = &_mesa_sparc_glapi_end;
995 disp_addr = (unsigned long) &_glapi_Dispatch;
996
997 /*
998 * Verbatim from Mesa sparc.c. It's needed because there doesn't
999 * seem to be a better way to do this:
1000 *
1001 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1002 *
1003 * This code is patching in the ADDRESS of the pointer to the
1004 * dispatch table. Hence, it must be called exactly once, because
1005 * that address is not going to change.
1006 *
1007 * What it points to can change, but Mesa (and hence, we) assume
1008 * that there is only one pointer.
1009 *
1010 */
1011 while (insn_ptr < end_ptr) {
1012 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1013 /*
1014 This code patches for 64-bit addresses. This had better
1015 not happen for Sparc/Linux, no matter what architecture we
1016 are building for. So, don't do this.
1017
1018 The 'defined(__linux_64__)' is used here as a placeholder for
1019 when we do do 64-bit usermode on sparc linux.
1020 */
1021 insn_ptr[0] |= (disp_addr >> (32 + 10));
1022 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1023 __glapi_sparc_icache_flush(&insn_ptr[0]);
1024 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1025 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1026 __glapi_sparc_icache_flush(&insn_ptr[2]);
1027 insn_ptr += 11;
1028 #else
1029 insn_ptr[0] |= (disp_addr >> 10);
1030 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1031 __glapi_sparc_icache_flush(&insn_ptr[0]);
1032 insn_ptr += 5;
1033 #endif
1034 }
1035 }
1036 #endif /* sparc ASM in use */