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