Move make install logic for libGL back into src/mesa/Makefile.
[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 <stdio.h>
49 #include <X11/extensions/Xext.h>
50 #include <X11/extensions/extutil.h>
51 #include <X11/extensions/Xfixes.h>
52 #include <X11/extensions/Xdamage.h>
53 #include <assert.h>
54 #include "indirect_init.h"
55 #include "glapi.h"
56 #include "glxextensions.h"
57 #include "glcontextmodes.h"
58 #include "glheader.h"
59
60 #ifdef GLX_DIRECT_RENDERING
61 #include <inttypes.h>
62 #include <sys/mman.h>
63 #include "xf86dri.h"
64 #include "xf86drm.h"
65 #include "sarea.h"
66 #endif
67
68 #ifdef USE_XCB
69 #include <X11/Xlib-xcb.h>
70 #include <xcb/xcb.h>
71 #include <xcb/glx.h>
72 #endif
73
74
75 #ifdef DEBUG
76 void __glXDumpDrawBuffer(__GLXcontext *ctx);
77 #endif
78
79 #ifdef USE_SPARC_ASM
80 /*
81 * This is where our dispatch table's bounds are.
82 * And the static mesa_init is taken directly from
83 * Mesa's 'sparc.c' initializer.
84 *
85 * We need something like this here, because this version
86 * of openGL/glx never initializes a Mesa context, and so
87 * the address of the dispatch table pointer never gets stuffed
88 * into the dispatch jump table otherwise.
89 *
90 * It matters only on SPARC, and only if you are using assembler
91 * code instead of C-code indirect dispatch.
92 *
93 * -- FEM, 04.xii.03
94 */
95 extern unsigned int _mesa_sparc_glapi_begin;
96 extern unsigned int _mesa_sparc_glapi_end;
97 extern void __glapi_sparc_icache_flush(unsigned int *);
98 static void _glx_mesa_init_sparc_glapi_relocs(void);
99 static int _mesa_sparc_needs_init = 1;
100 #define INIT_MESA_SPARC { \
101 if(_mesa_sparc_needs_init) { \
102 _glx_mesa_init_sparc_glapi_relocs(); \
103 _mesa_sparc_needs_init = 0; \
104 } \
105 }
106 #else
107 #define INIT_MESA_SPARC
108 #endif
109
110 static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
111 GLXDrawable read, GLXContext gc);
112
113 /*
114 ** We setup some dummy structures here so that the API can be used
115 ** even if no context is current.
116 */
117
118 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
119
120 /*
121 ** Dummy context used by small commands when there is no current context.
122 ** All the
123 ** gl and glx entry points are designed to operate as nop's when using
124 ** the dummy context structure.
125 */
126 static __GLXcontext dummyContext = {
127 &dummyBuffer[0],
128 &dummyBuffer[0],
129 &dummyBuffer[0],
130 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
131 sizeof(dummyBuffer),
132 };
133
134
135 /*
136 ** All indirect rendering contexts will share the same indirect dispatch table.
137 */
138 static __GLapi *IndirectAPI = NULL;
139
140
141 /*
142 * Current context management and locking
143 */
144
145 #if defined( USE_XTHREADS )
146
147 /* thread safe */
148 static GLboolean TSDinitialized = GL_FALSE;
149 static xthread_key_t ContextTSD;
150
151 _X_HIDDEN __GLXcontext *__glXGetCurrentContext(void)
152 {
153 if (!TSDinitialized) {
154 xthread_key_create(&ContextTSD, NULL);
155 TSDinitialized = GL_TRUE;
156 return &dummyContext;
157 }
158 else {
159 void *p;
160 xthread_get_specific(ContextTSD, &p);
161 if (!p)
162 return &dummyContext;
163 else
164 return (__GLXcontext *) p;
165 }
166 }
167
168 _X_HIDDEN void __glXSetCurrentContext(__GLXcontext *c)
169 {
170 if (!TSDinitialized) {
171 xthread_key_create(&ContextTSD, NULL);
172 TSDinitialized = GL_TRUE;
173 }
174 xthread_set_specific(ContextTSD, c);
175 }
176
177
178 /* Used by the __glXLock() and __glXUnlock() macros */
179 _X_HIDDEN xmutex_rec __glXmutex;
180
181 #elif defined( PTHREADS )
182
183 _X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
184
185 # if defined( GLX_USE_TLS )
186
187 /**
188 * Per-thread GLX context pointer.
189 *
190 * \c __glXSetCurrentContext is written is such a way that this pointer can
191 * \b never be \c NULL. This is important! Because of this
192 * \c __glXGetCurrentContext can be implemented as trivial macro.
193 */
194 __thread void * __glX_tls_Context __attribute__((tls_model("initial-exec")))
195 = &dummyContext;
196
197 _X_HIDDEN void __glXSetCurrentContext( __GLXcontext * c )
198 {
199 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
200 }
201
202 # else
203
204 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
205
206 /**
207 * Per-thread data key.
208 *
209 * Once \c init_thread_data has been called, the per-thread data key will
210 * take a value of \c NULL. As each new thread is created the default
211 * value, in that thread, will be \c NULL.
212 */
213 static pthread_key_t ContextTSD;
214
215 /**
216 * Initialize the per-thread data key.
217 *
218 * This function is called \b exactly once per-process (not per-thread!) to
219 * initialize the per-thread data key. This is ideally done using the
220 * \c pthread_once mechanism.
221 */
222 static void init_thread_data( void )
223 {
224 if ( pthread_key_create( & ContextTSD, NULL ) != 0 ) {
225 perror( "pthread_key_create" );
226 exit( -1 );
227 }
228 }
229
230 _X_HIDDEN void __glXSetCurrentContext( __GLXcontext * c )
231 {
232 pthread_once( & once_control, init_thread_data );
233 pthread_setspecific( ContextTSD, c );
234 }
235
236 _X_HIDDEN __GLXcontext * __glXGetCurrentContext( void )
237 {
238 void * v;
239
240 pthread_once( & once_control, init_thread_data );
241
242 v = pthread_getspecific( ContextTSD );
243 return (v == NULL) ? & dummyContext : (__GLXcontext *) v;
244 }
245
246 # endif /* defined( GLX_USE_TLS ) */
247
248 #elif defined( THREADS )
249
250 #error Unknown threading method specified.
251
252 #else
253
254 /* not thread safe */
255 _X_HIDDEN __GLXcontext *__glXcurrentContext = &dummyContext;
256
257 #endif
258
259
260 /*
261 ** You can set this cell to 1 to force the gl drawing stuff to be
262 ** one command per packet
263 */
264 _X_HIDDEN int __glXDebug = 0;
265
266 /* Extension required boiler plate */
267
268 static char *__glXExtensionName = GLX_EXTENSION_NAME;
269 XExtensionInfo *__glXExtensionInfo = NULL;
270
271 static /* const */ char *error_list[] = {
272 "GLXBadContext",
273 "GLXBadContextState",
274 "GLXBadDrawable",
275 "GLXBadPixmap",
276 "GLXBadContextTag",
277 "GLXBadCurrentWindow",
278 "GLXBadRenderRequest",
279 "GLXBadLargeRequest",
280 "GLXUnsupportedPrivateRequest",
281 "GLXBadFBConfig",
282 "GLXBadPbuffer",
283 "GLXBadCurrentDrawable",
284 "GLXBadWindow",
285 };
286
287 static int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
288 {
289 GLXContext gc;
290
291 gc = __glXGetCurrentContext();
292 if (dpy == gc->currentDpy) {
293 __glXSetCurrentContext(&dummyContext);
294 #ifdef GLX_DIRECT_RENDERING
295 _glapi_set_dispatch(NULL); /* no-op functions */
296 #endif
297 __glXFreeContext(gc);
298 }
299
300 return XextRemoveDisplay(__glXExtensionInfo, dpy);
301 }
302
303
304 static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
305 __GLX_NUMBER_ERRORS, error_list)
306
307 static /* const */ XExtensionHooks __glXExtensionHooks = {
308 NULL, /* create_gc */
309 NULL, /* copy_gc */
310 NULL, /* flush_gc */
311 NULL, /* free_gc */
312 NULL, /* create_font */
313 NULL, /* free_font */
314 __glXCloseDisplay, /* close_display */
315 NULL, /* wire_to_event */
316 NULL, /* event_to_wire */
317 NULL, /* error */
318 __glXErrorString, /* error_string */
319 };
320
321 static
322 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
323 __glXExtensionName, &__glXExtensionHooks,
324 __GLX_NUMBER_EVENTS, NULL)
325
326 /************************************************************************/
327
328 /*
329 ** Free the per screen configs data as well as the array of
330 ** __glXScreenConfigs.
331 */
332 static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
333 {
334 __GLXscreenConfigs *psc;
335 GLint i, screens;
336
337 /* Free screen configuration information */
338 psc = priv->screenConfigs;
339 screens = ScreenCount(priv->dpy);
340 for (i = 0; i < screens; i++, psc++) {
341 if (psc->configs) {
342 _gl_context_modes_destroy( psc->configs );
343 if(psc->effectiveGLXexts)
344 Xfree(psc->effectiveGLXexts);
345
346 psc->configs = NULL; /* NOTE: just for paranoia */
347 }
348 Xfree((char*) psc->serverGLXexts);
349
350 #ifdef GLX_DIRECT_RENDERING
351 psc->driScreen->destroyScreen(psc);
352 #endif
353 }
354 XFree((char*) priv->screenConfigs);
355 }
356
357 /*
358 ** Release the private memory referred to in a display private
359 ** structure. The caller will free the extension structure.
360 */
361 static int __glXFreeDisplayPrivate(XExtData *extension)
362 {
363 __GLXdisplayPrivate *priv;
364
365 priv = (__GLXdisplayPrivate*) extension->private_data;
366 FreeScreenConfigs(priv);
367 if(priv->serverGLXvendor) {
368 Xfree((char*)priv->serverGLXvendor);
369 priv->serverGLXvendor = 0x0; /* to protect against double free's */
370 }
371 if(priv->serverGLXversion) {
372 Xfree((char*)priv->serverGLXversion);
373 priv->serverGLXversion = 0x0; /* to protect against double free's */
374 }
375
376 #ifdef GLX_DIRECT_RENDERING
377 /* Free the direct rendering per display data */
378 if (priv->driDisplay)
379 (*priv->driDisplay->destroyDisplay)(priv->driDisplay);
380 priv->driDisplay = NULL;
381 #endif
382
383 Xfree((char*) priv);
384 return 0;
385 }
386
387 /************************************************************************/
388
389 /*
390 ** Query the version of the GLX extension. This procedure works even if
391 ** the client extension is not completely set up.
392 */
393 static Bool QueryVersion(Display *dpy, int opcode, int *major, int *minor)
394 {
395 xGLXQueryVersionReq *req;
396 xGLXQueryVersionReply reply;
397
398 /* Send the glXQueryVersion request */
399 LockDisplay(dpy);
400 GetReq(GLXQueryVersion,req);
401 req->reqType = opcode;
402 req->glxCode = X_GLXQueryVersion;
403 req->majorVersion = GLX_MAJOR_VERSION;
404 req->minorVersion = GLX_MINOR_VERSION;
405 _XReply(dpy, (xReply*) &reply, 0, False);
406 UnlockDisplay(dpy);
407 SyncHandle();
408
409 if (reply.majorVersion != GLX_MAJOR_VERSION) {
410 /*
411 ** The server does not support the same major release as this
412 ** client.
413 */
414 return GL_FALSE;
415 }
416 *major = reply.majorVersion;
417 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
418 return GL_TRUE;
419 }
420
421
422 _X_HIDDEN void
423 __glXInitializeVisualConfigFromTags( __GLcontextModes *config, int count,
424 const INT32 *bp, Bool tagged_only,
425 Bool fbconfig_style_tags )
426 {
427 int i;
428
429 if (!tagged_only) {
430 /* Copy in the first set of properties */
431 config->visualID = *bp++;
432
433 config->visualType = _gl_convert_from_x_visual_type( *bp++ );
434
435 config->rgbMode = *bp++;
436
437 config->redBits = *bp++;
438 config->greenBits = *bp++;
439 config->blueBits = *bp++;
440 config->alphaBits = *bp++;
441 config->accumRedBits = *bp++;
442 config->accumGreenBits = *bp++;
443 config->accumBlueBits = *bp++;
444 config->accumAlphaBits = *bp++;
445
446 config->doubleBufferMode = *bp++;
447 config->stereoMode = *bp++;
448
449 config->rgbBits = *bp++;
450 config->depthBits = *bp++;
451 config->stencilBits = *bp++;
452 config->numAuxBuffers = *bp++;
453 config->level = *bp++;
454
455 count -= __GLX_MIN_CONFIG_PROPS;
456 }
457
458 /*
459 ** Additional properties may be in a list at the end
460 ** of the reply. They are in pairs of property type
461 ** and property value.
462 */
463
464 #define FETCH_OR_SET(tag) \
465 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
466
467 for (i = 0; i < count; i += 2 ) {
468 switch(*bp++) {
469 case GLX_RGBA:
470 FETCH_OR_SET( rgbMode );
471 break;
472 case GLX_BUFFER_SIZE:
473 config->rgbBits = *bp++;
474 break;
475 case GLX_LEVEL:
476 config->level = *bp++;
477 break;
478 case GLX_DOUBLEBUFFER:
479 FETCH_OR_SET( doubleBufferMode );
480 break;
481 case GLX_STEREO:
482 FETCH_OR_SET( stereoMode );
483 break;
484 case GLX_AUX_BUFFERS:
485 config->numAuxBuffers = *bp++;
486 break;
487 case GLX_RED_SIZE:
488 config->redBits = *bp++;
489 break;
490 case GLX_GREEN_SIZE:
491 config->greenBits = *bp++;
492 break;
493 case GLX_BLUE_SIZE:
494 config->blueBits = *bp++;
495 break;
496 case GLX_ALPHA_SIZE:
497 config->alphaBits = *bp++;
498 break;
499 case GLX_DEPTH_SIZE:
500 config->depthBits = *bp++;
501 break;
502 case GLX_STENCIL_SIZE:
503 config->stencilBits = *bp++;
504 break;
505 case GLX_ACCUM_RED_SIZE:
506 config->accumRedBits = *bp++;
507 break;
508 case GLX_ACCUM_GREEN_SIZE:
509 config->accumGreenBits = *bp++;
510 break;
511 case GLX_ACCUM_BLUE_SIZE:
512 config->accumBlueBits = *bp++;
513 break;
514 case GLX_ACCUM_ALPHA_SIZE:
515 config->accumAlphaBits = *bp++;
516 break;
517 case GLX_VISUAL_CAVEAT_EXT:
518 config->visualRating = *bp++;
519 break;
520 case GLX_X_VISUAL_TYPE:
521 config->visualType = *bp++;
522 break;
523 case GLX_TRANSPARENT_TYPE:
524 config->transparentPixel = *bp++;
525 break;
526 case GLX_TRANSPARENT_INDEX_VALUE:
527 config->transparentIndex = *bp++;
528 break;
529 case GLX_TRANSPARENT_RED_VALUE:
530 config->transparentRed = *bp++;
531 break;
532 case GLX_TRANSPARENT_GREEN_VALUE:
533 config->transparentGreen = *bp++;
534 break;
535 case GLX_TRANSPARENT_BLUE_VALUE:
536 config->transparentBlue = *bp++;
537 break;
538 case GLX_TRANSPARENT_ALPHA_VALUE:
539 config->transparentAlpha = *bp++;
540 break;
541 case GLX_VISUAL_ID:
542 config->visualID = *bp++;
543 break;
544 case GLX_DRAWABLE_TYPE:
545 config->drawableType = *bp++;
546 break;
547 case GLX_RENDER_TYPE:
548 config->renderType = *bp++;
549 break;
550 case GLX_X_RENDERABLE:
551 config->xRenderable = *bp++;
552 break;
553 case GLX_FBCONFIG_ID:
554 config->fbconfigID = *bp++;
555 break;
556 case GLX_MAX_PBUFFER_WIDTH:
557 config->maxPbufferWidth = *bp++;
558 break;
559 case GLX_MAX_PBUFFER_HEIGHT:
560 config->maxPbufferHeight = *bp++;
561 break;
562 case GLX_MAX_PBUFFER_PIXELS:
563 config->maxPbufferPixels = *bp++;
564 break;
565 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
566 config->optimalPbufferWidth = *bp++;
567 break;
568 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
569 config->optimalPbufferHeight = *bp++;
570 break;
571 case GLX_VISUAL_SELECT_GROUP_SGIX:
572 config->visualSelectGroup = *bp++;
573 break;
574 case GLX_SWAP_METHOD_OML:
575 config->swapMethod = *bp++;
576 break;
577 case GLX_SAMPLE_BUFFERS_SGIS:
578 config->sampleBuffers = *bp++;
579 break;
580 case GLX_SAMPLES_SGIS:
581 config->samples = *bp++;
582 break;
583 case GLX_BIND_TO_TEXTURE_RGB_EXT:
584 config->bindToTextureRgb = *bp++;
585 break;
586 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
587 config->bindToTextureRgba = *bp++;
588 break;
589 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
590 config->bindToMipmapTexture = *bp++;
591 break;
592 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
593 config->bindToTextureTargets = *bp++;
594 break;
595 case GLX_Y_INVERTED_EXT:
596 config->yInverted = *bp++;
597 break;
598 case None:
599 i = count;
600 break;
601 default:
602 break;
603 }
604 }
605
606 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
607
608 config->haveAccumBuffer = ((config->accumRedBits +
609 config->accumGreenBits +
610 config->accumBlueBits +
611 config->accumAlphaBits) > 0);
612 config->haveDepthBuffer = (config->depthBits > 0);
613 config->haveStencilBuffer = (config->stencilBits > 0);
614 }
615
616 static __GLcontextModes *
617 createConfigsFromProperties(Display *dpy, int nvisuals, int nprops,
618 int screen, GLboolean tagged_only)
619 {
620 INT32 buf[__GLX_TOTAL_CONFIG], *props;
621 unsigned prop_size;
622 __GLcontextModes *modes, *m;
623 int i;
624
625 if (nprops == 0)
626 return NULL;
627
628 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
629
630 /* Check number of properties */
631 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
632 return NULL;
633
634 /* Allocate memory for our config structure */
635 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
636 if (!modes)
637 return NULL;
638
639 prop_size = nprops * __GLX_SIZE_INT32;
640 if (prop_size <= sizeof(buf))
641 props = buf;
642 else
643 props = Xmalloc(prop_size);
644
645 /* Read each config structure and convert it into our format */
646 m = modes;
647 for (i = 0; i < nvisuals; i++) {
648 _XRead(dpy, (char *)props, prop_size);
649 /* Older X servers don't send this so we default it here. */
650 m->drawableType = GLX_WINDOW_BIT;
651 __glXInitializeVisualConfigFromTags(m, nprops, props,
652 tagged_only, GL_TRUE);
653 m->screen = screen;
654 m = m->next;
655 }
656
657 if (props != buf)
658 Xfree(props);
659
660 return modes;
661 }
662
663 static GLboolean
664 getVisualConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
665 {
666 xGLXGetVisualConfigsReq *req;
667 __GLXscreenConfigs *psc;
668 xGLXGetVisualConfigsReply reply;
669
670 LockDisplay(dpy);
671
672 psc = priv->screenConfigs + screen;
673 psc->visuals = NULL;
674 GetReq(GLXGetVisualConfigs, req);
675 req->reqType = priv->majorOpcode;
676 req->glxCode = X_GLXGetVisualConfigs;
677 req->screen = screen;
678
679 if (!_XReply(dpy, (xReply*) &reply, 0, False))
680 goto out;
681
682 psc->visuals = createConfigsFromProperties(dpy,
683 reply.numVisuals,
684 reply.numProps,
685 screen, GL_FALSE);
686
687 out:
688 UnlockDisplay(dpy);
689 return psc->visuals != NULL;
690 }
691
692 static GLboolean
693 getFBConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
694 {
695 xGLXGetFBConfigsReq *fb_req;
696 xGLXGetFBConfigsSGIXReq *sgi_req;
697 xGLXVendorPrivateWithReplyReq *vpreq;
698 xGLXGetFBConfigsReply reply;
699 __GLXscreenConfigs *psc;
700
701 psc = priv->screenConfigs + screen;
702 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
703 X_GLXQueryServerString,
704 screen, GLX_EXTENSIONS);
705
706 LockDisplay(dpy);
707
708 psc->configs = NULL;
709 if (atof(priv->serverGLXversion) >= 1.3) {
710 GetReq(GLXGetFBConfigs, fb_req);
711 fb_req->reqType = priv->majorOpcode;
712 fb_req->glxCode = X_GLXGetFBConfigs;
713 fb_req->screen = screen;
714 } else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
715 GetReqExtra(GLXVendorPrivateWithReply,
716 sz_xGLXGetFBConfigsSGIXReq +
717 sz_xGLXVendorPrivateWithReplyReq, vpreq);
718 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
719 sgi_req->reqType = priv->majorOpcode;
720 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
721 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
722 sgi_req->screen = screen;
723 } else
724 goto out;
725
726 if (!_XReply(dpy, (xReply*) &reply, 0, False))
727 goto out;
728
729 psc->configs = createConfigsFromProperties(dpy,
730 reply.numFBConfigs,
731 reply.numAttribs * 2,
732 screen, GL_TRUE);
733
734 out:
735 UnlockDisplay(dpy);
736 return psc->configs != NULL;
737 }
738
739 /*
740 ** Allocate the memory for the per screen configs for each screen.
741 ** If that works then fetch the per screen configs data.
742 */
743 static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
744 {
745 __GLXscreenConfigs *psc;
746 GLint i, screens;
747
748 /*
749 ** First allocate memory for the array of per screen configs.
750 */
751 screens = ScreenCount(dpy);
752 psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
753 if (!psc) {
754 return GL_FALSE;
755 }
756 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
757 priv->screenConfigs = psc;
758
759 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
760 X_GLXQueryServerString,
761 0, GLX_VERSION);
762 if ( priv->serverGLXversion == NULL ) {
763 FreeScreenConfigs(priv);
764 return GL_FALSE;
765 }
766
767 for (i = 0; i < screens; i++, psc++) {
768 getVisualConfigs(dpy, priv, i);
769 getFBConfigs(dpy, priv, i);
770
771 psc->scr = i;
772 psc->dpy = dpy;
773 #ifdef GLX_DIRECT_RENDERING
774 if (priv->driDisplay)
775 psc->driScreen = (*priv->driDisplay->createScreen)(psc, i, priv);
776 #endif
777 }
778 SyncHandle();
779 return GL_TRUE;
780 }
781
782 /*
783 ** Initialize the client side extension code.
784 */
785 _X_HIDDEN __GLXdisplayPrivate *__glXInitialize(Display* dpy)
786 {
787 XExtDisplayInfo *info = __glXFindDisplay(dpy);
788 XExtData **privList, *private, *found;
789 __GLXdisplayPrivate *dpyPriv;
790 XEDataObject dataObj;
791 int major, minor;
792
793 #if defined(USE_XTHREADS)
794 {
795 static int firstCall = 1;
796 if (firstCall) {
797 /* initialize the GLX mutexes */
798 xmutex_init(&__glXmutex);
799 firstCall = 0;
800 }
801 }
802 #endif
803
804 INIT_MESA_SPARC
805 /* The one and only long long lock */
806 __glXLock();
807
808 if (!XextHasExtension(info)) {
809 /* No GLX extension supported by this server. Oh well. */
810 __glXUnlock();
811 XMissingExtension(dpy, __glXExtensionName);
812 return 0;
813 }
814
815 /* See if a display private already exists. If so, return it */
816 dataObj.display = dpy;
817 privList = XEHeadOfExtensionList(dataObj);
818 found = XFindOnExtensionList(privList, info->codes->extension);
819 if (found) {
820 __glXUnlock();
821 return (__GLXdisplayPrivate *) found->private_data;
822 }
823
824 /* See if the versions are compatible */
825 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
826 /* The client and server do not agree on versions. Punt. */
827 __glXUnlock();
828 return 0;
829 }
830
831 /*
832 ** Allocate memory for all the pieces needed for this buffer.
833 */
834 private = (XExtData *) Xmalloc(sizeof(XExtData));
835 if (!private) {
836 __glXUnlock();
837 return 0;
838 }
839 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
840 if (!dpyPriv) {
841 __glXUnlock();
842 Xfree((char*) private);
843 return 0;
844 }
845
846 /*
847 ** Init the display private and then read in the screen config
848 ** structures from the server.
849 */
850 dpyPriv->majorOpcode = info->codes->major_opcode;
851 dpyPriv->majorVersion = major;
852 dpyPriv->minorVersion = minor;
853 dpyPriv->dpy = dpy;
854
855 dpyPriv->serverGLXvendor = 0x0;
856 dpyPriv->serverGLXversion = 0x0;
857
858 #ifdef GLX_DIRECT_RENDERING
859 /*
860 ** Initialize the direct rendering per display data and functions.
861 ** Note: This _must_ be done before calling any other DRI routines
862 ** (e.g., those called in AllocAndFetchScreenConfigs).
863 */
864 if (getenv("LIBGL_ALWAYS_INDIRECT") == NULL) {
865 dpyPriv->driDisplay = driCreateDisplay(dpy);
866 }
867 #endif
868
869 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
870 __glXUnlock();
871 Xfree((char*) dpyPriv);
872 Xfree((char*) private);
873 return 0;
874 }
875
876 /*
877 ** Fill in the private structure. This is the actual structure that
878 ** hangs off of the Display structure. Our private structure is
879 ** referred to by this structure. Got that?
880 */
881 private->number = info->codes->extension;
882 private->next = 0;
883 private->free_private = __glXFreeDisplayPrivate;
884 private->private_data = (char *) dpyPriv;
885 XAddToExtensionList(privList, private);
886
887 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
888 __glXClientInfo(dpy, dpyPriv->majorOpcode);
889 }
890 __glXUnlock();
891
892 return dpyPriv;
893 }
894
895 /*
896 ** Setup for sending a GLX command on dpy. Make sure the extension is
897 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
898 */
899 _X_HIDDEN CARD8 __glXSetupForCommand(Display *dpy)
900 {
901 GLXContext gc;
902 __GLXdisplayPrivate *priv;
903
904 /* If this thread has a current context, flush its rendering commands */
905 gc = __glXGetCurrentContext();
906 if (gc->currentDpy) {
907 /* Flush rendering buffer of the current context, if any */
908 (void) __glXFlushRenderBuffer(gc, gc->pc);
909
910 if (gc->currentDpy == dpy) {
911 /* Use opcode from gc because its right */
912 INIT_MESA_SPARC
913 return gc->majorOpcode;
914 } else {
915 /*
916 ** Have to get info about argument dpy because it might be to
917 ** a different server
918 */
919 }
920 }
921
922 /* Forced to lookup extension via the slow initialize route */
923 priv = __glXInitialize(dpy);
924 if (!priv) {
925 return 0;
926 }
927 return priv->majorOpcode;
928 }
929
930 /**
931 * Flush the drawing command transport buffer.
932 *
933 * \param ctx Context whose transport buffer is to be flushed.
934 * \param pc Pointer to first unused buffer location.
935 *
936 * \todo
937 * Modify this function to use \c ctx->pc instead of the explicit
938 * \c pc parameter.
939 */
940 _X_HIDDEN GLubyte *__glXFlushRenderBuffer(__GLXcontext *ctx, GLubyte *pc)
941 {
942 Display * const dpy = ctx->currentDpy;
943 #ifdef USE_XCB
944 xcb_connection_t *c = XGetXCBConnection(dpy);
945 #else
946 xGLXRenderReq *req;
947 #endif /* USE_XCB */
948 const GLint size = pc - ctx->buf;
949
950 if ( (dpy != NULL) && (size > 0) ) {
951 #ifdef USE_XCB
952 xcb_glx_render(c, ctx->currentContextTag, size,
953 (const uint8_t *)ctx->buf);
954 #else
955 /* Send the entire buffer as an X request */
956 LockDisplay(dpy);
957 GetReq(GLXRender,req);
958 req->reqType = ctx->majorOpcode;
959 req->glxCode = X_GLXRender;
960 req->contextTag = ctx->currentContextTag;
961 req->length += (size + 3) >> 2;
962 _XSend(dpy, (char *)ctx->buf, size);
963 UnlockDisplay(dpy);
964 SyncHandle();
965 #endif
966 }
967
968 /* Reset pointer and return it */
969 ctx->pc = ctx->buf;
970 return ctx->pc;
971 }
972
973
974 /**
975 * Send a portion of a GLXRenderLarge command to the server. The advantage of
976 * this function over \c __glXSendLargeCommand is that callers can use the
977 * data buffer in the GLX context and may be able to avoid allocating an
978 * extra buffer. The disadvantage is the clients will have to do more
979 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
980 *
981 * \sa __glXSendLargeCommand
982 *
983 * \param gc GLX context
984 * \param requestNumber Which part of the whole command is this? The first
985 * request is 1.
986 * \param totalRequests How many requests will there be?
987 * \param data Command data.
988 * \param dataLen Size, in bytes, of the command data.
989 */
990 _X_HIDDEN void __glXSendLargeChunk(__GLXcontext *gc, GLint requestNumber,
991 GLint totalRequests,
992 const GLvoid * data, GLint dataLen)
993 {
994 Display *dpy = gc->currentDpy;
995 #ifdef USE_XCB
996 xcb_connection_t *c = XGetXCBConnection(dpy);
997 xcb_glx_render_large(c, gc->currentContextTag, requestNumber, totalRequests, dataLen, data);
998 #else
999 xGLXRenderLargeReq *req;
1000
1001 if ( requestNumber == 1 ) {
1002 LockDisplay(dpy);
1003 }
1004
1005 GetReq(GLXRenderLarge,req);
1006 req->reqType = gc->majorOpcode;
1007 req->glxCode = X_GLXRenderLarge;
1008 req->contextTag = gc->currentContextTag;
1009 req->length += (dataLen + 3) >> 2;
1010 req->requestNumber = requestNumber;
1011 req->requestTotal = totalRequests;
1012 req->dataBytes = dataLen;
1013 Data(dpy, data, dataLen);
1014
1015 if ( requestNumber == totalRequests ) {
1016 UnlockDisplay(dpy);
1017 SyncHandle();
1018 }
1019 #endif /* USE_XCB */
1020 }
1021
1022
1023 /**
1024 * Send a command that is too large for the GLXRender protocol request.
1025 *
1026 * Send a large command, one that is too large for some reason to
1027 * send using the GLXRender protocol request. One reason to send
1028 * a large command is to avoid copying the data.
1029 *
1030 * \param ctx GLX context
1031 * \param header Header data.
1032 * \param headerLen Size, in bytes, of the header data. It is assumed that
1033 * the header data will always be small enough to fit in
1034 * a single X protocol packet.
1035 * \param data Command data.
1036 * \param dataLen Size, in bytes, of the command data.
1037 */
1038 _X_HIDDEN void __glXSendLargeCommand(__GLXcontext *ctx,
1039 const GLvoid *header, GLint headerLen,
1040 const GLvoid *data, GLint dataLen)
1041 {
1042 GLint maxSize;
1043 GLint totalRequests, requestNumber;
1044
1045 /*
1046 ** Calculate the maximum amount of data can be stuffed into a single
1047 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1048 ** packet size minus sz_xGLXRenderReq.
1049 */
1050 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1051 totalRequests = 1 + (dataLen / maxSize);
1052 if (dataLen % maxSize) totalRequests++;
1053
1054 /*
1055 ** Send all of the command, except the large array, as one request.
1056 */
1057 assert( headerLen <= maxSize );
1058 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1059
1060 /*
1061 ** Send enough requests until the whole array is sent.
1062 */
1063 for ( requestNumber = 2 ; requestNumber <= (totalRequests - 1) ; requestNumber++ ) {
1064 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1065 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1066 dataLen -= maxSize;
1067 assert( dataLen > 0 );
1068 }
1069
1070 assert( dataLen <= maxSize );
1071 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1072 }
1073
1074 /************************************************************************/
1075
1076 PUBLIC GLXContext glXGetCurrentContext(void)
1077 {
1078 GLXContext cx = __glXGetCurrentContext();
1079
1080 if (cx == &dummyContext) {
1081 return NULL;
1082 } else {
1083 return cx;
1084 }
1085 }
1086
1087 PUBLIC GLXDrawable glXGetCurrentDrawable(void)
1088 {
1089 GLXContext gc = __glXGetCurrentContext();
1090 return gc->currentDrawable;
1091 }
1092
1093
1094 /************************************************************************/
1095
1096 static Bool SendMakeCurrentRequest( Display *dpy, CARD8 opcode,
1097 GLXContextID gc, GLXContextTag old_gc, GLXDrawable draw, GLXDrawable read,
1098 xGLXMakeCurrentReply * reply );
1099
1100 /**
1101 * Sends a GLX protocol message to the specified display to make the context
1102 * and the drawables current.
1103 *
1104 * \param dpy Display to send the message to.
1105 * \param opcode Major opcode value for the display.
1106 * \param gc_id Context tag for the context to be made current.
1107 * \param draw Drawable ID for the "draw" drawable.
1108 * \param read Drawable ID for the "read" drawable.
1109 * \param reply Space to store the X-server's reply.
1110 *
1111 * \warning
1112 * This function assumes that \c dpy is locked with \c LockDisplay on entry.
1113 */
1114 static Bool SendMakeCurrentRequest(Display *dpy, CARD8 opcode,
1115 GLXContextID gc_id, GLXContextTag gc_tag,
1116 GLXDrawable draw, GLXDrawable read,
1117 xGLXMakeCurrentReply *reply)
1118 {
1119 Bool ret;
1120
1121
1122 LockDisplay(dpy);
1123
1124 if (draw == read) {
1125 xGLXMakeCurrentReq *req;
1126
1127 GetReq(GLXMakeCurrent,req);
1128 req->reqType = opcode;
1129 req->glxCode = X_GLXMakeCurrent;
1130 req->drawable = draw;
1131 req->context = gc_id;
1132 req->oldContextTag = gc_tag;
1133 }
1134 else {
1135 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1136
1137 /* If the server can support the GLX 1.3 version, we should
1138 * perfer that. Not only that, some servers support GLX 1.3 but
1139 * not the SGI extension.
1140 */
1141
1142 if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
1143 xGLXMakeContextCurrentReq *req;
1144
1145 GetReq(GLXMakeContextCurrent,req);
1146 req->reqType = opcode;
1147 req->glxCode = X_GLXMakeContextCurrent;
1148 req->drawable = draw;
1149 req->readdrawable = read;
1150 req->context = gc_id;
1151 req->oldContextTag = gc_tag;
1152 }
1153 else {
1154 xGLXVendorPrivateWithReplyReq *vpreq;
1155 xGLXMakeCurrentReadSGIReq *req;
1156
1157 GetReqExtra(GLXVendorPrivateWithReply,
1158 sz_xGLXMakeCurrentReadSGIReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
1159 req = (xGLXMakeCurrentReadSGIReq *)vpreq;
1160 req->reqType = opcode;
1161 req->glxCode = X_GLXVendorPrivateWithReply;
1162 req->vendorCode = X_GLXvop_MakeCurrentReadSGI;
1163 req->drawable = draw;
1164 req->readable = read;
1165 req->context = gc_id;
1166 req->oldContextTag = gc_tag;
1167 }
1168 }
1169
1170 ret = _XReply(dpy, (xReply*) reply, 0, False);
1171
1172 UnlockDisplay(dpy);
1173 SyncHandle();
1174
1175 return ret;
1176 }
1177
1178
1179 #ifdef GLX_DIRECT_RENDERING
1180 static __GLXDRIdrawable *
1181 FetchDRIDrawable(Display *dpy, GLXDrawable drawable, GLXContext gc)
1182 {
1183 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
1184 __GLXDRIdrawable *pdraw;
1185 __GLXscreenConfigs *psc;
1186
1187 if (priv == NULL || priv->driDisplay == NULL)
1188 return NULL;
1189
1190 psc = &priv->screenConfigs[gc->screen];
1191 if (__glxHashLookup(psc->drawHash, drawable, (void *) &pdraw) == 0)
1192 return pdraw;
1193
1194 return psc->driScreen->createDrawable(psc, drawable, gc);
1195 }
1196 #endif /* GLX_DIRECT_RENDERING */
1197
1198
1199 /**
1200 * Make a particular context current.
1201 *
1202 * \note This is in this file so that it can access dummyContext.
1203 */
1204 static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
1205 GLXDrawable read, GLXContext gc)
1206 {
1207 xGLXMakeCurrentReply reply;
1208 const GLXContext oldGC = __glXGetCurrentContext();
1209 const CARD8 opcode = __glXSetupForCommand(dpy);
1210 const CARD8 oldOpcode = ((gc == oldGC) || (oldGC == &dummyContext))
1211 ? opcode : __glXSetupForCommand(oldGC->currentDpy);
1212 Bool bindReturnValue;
1213
1214
1215 if (!opcode || !oldOpcode) {
1216 return GL_FALSE;
1217 }
1218
1219 /* Make sure that the new context has a nonzero ID. In the request,
1220 * a zero context ID is used only to mean that we bind to no current
1221 * context.
1222 */
1223 if ((gc != NULL) && (gc->xid == None)) {
1224 return GL_FALSE;
1225 }
1226
1227 _glapi_check_multithread();
1228
1229 #ifdef GLX_DIRECT_RENDERING
1230 /* Bind the direct rendering context to the drawable */
1231 if (gc && gc->driContext) {
1232 __GLXDRIdrawable *pdraw = FetchDRIDrawable(dpy, draw, gc);
1233 __GLXDRIdrawable *pread = FetchDRIDrawable(dpy, read, gc);
1234
1235 bindReturnValue =
1236 (gc->driContext->bindContext) (gc->driContext, pdraw, pread);
1237 } else
1238 #endif
1239 {
1240 /* Send a glXMakeCurrent request to bind the new context. */
1241 bindReturnValue =
1242 SendMakeCurrentRequest(dpy, opcode, gc ? gc->xid : None,
1243 ((dpy != oldGC->currentDpy) || oldGC->driContext)
1244 ? None : oldGC->currentContextTag,
1245 draw, read, &reply);
1246 }
1247
1248
1249 if (!bindReturnValue) {
1250 return False;
1251 }
1252
1253 if ((dpy != oldGC->currentDpy || (gc && gc->driContext)) &&
1254 !oldGC->driContext && oldGC != &dummyContext) {
1255 xGLXMakeCurrentReply dummy_reply;
1256
1257 /* We are either switching from one dpy to another and have to
1258 * send a request to the previous dpy to unbind the previous
1259 * context, or we are switching away from a indirect context to
1260 * a direct context and have to send a request to the dpy to
1261 * unbind the previous context.
1262 */
1263 (void) SendMakeCurrentRequest(oldGC->currentDpy, oldOpcode, None,
1264 oldGC->currentContextTag, None, None,
1265 & dummy_reply);
1266 }
1267 #ifdef GLX_DIRECT_RENDERING
1268 else if (oldGC->driContext) {
1269 oldGC->driContext->unbindContext(oldGC->driContext);
1270 }
1271 #endif
1272
1273
1274 /* Update our notion of what is current */
1275 __glXLock();
1276 if (gc == oldGC) {
1277 /* Even though the contexts are the same the drawable might have
1278 * changed. Note that gc cannot be the dummy, and that oldGC
1279 * cannot be NULL, therefore if they are the same, gc is not
1280 * NULL and not the dummy.
1281 */
1282 gc->currentDrawable = draw;
1283 gc->currentReadable = read;
1284 } else {
1285 if (oldGC != &dummyContext) {
1286 /* Old current context is no longer current to anybody */
1287 oldGC->currentDpy = 0;
1288 oldGC->currentDrawable = None;
1289 oldGC->currentReadable = None;
1290 oldGC->currentContextTag = 0;
1291
1292 if (oldGC->xid == None) {
1293 /* We are switching away from a context that was
1294 * previously destroyed, so we need to free the memory
1295 * for the old handle.
1296 */
1297 #ifdef GLX_DIRECT_RENDERING
1298 /* Destroy the old direct rendering context */
1299 if (oldGC->driContext) {
1300 oldGC->driContext->destroyContext(oldGC->driContext,
1301 oldGC->psc,
1302 oldGC->createDpy);
1303 oldGC->driContext = NULL;
1304 }
1305 #endif
1306 __glXFreeContext(oldGC);
1307 }
1308 }
1309 if (gc) {
1310 __glXSetCurrentContext(gc);
1311
1312 gc->currentDpy = dpy;
1313 gc->currentDrawable = draw;
1314 gc->currentReadable = read;
1315
1316 if (!gc->driContext) {
1317 if (!IndirectAPI)
1318 IndirectAPI = __glXNewIndirectAPI();
1319 _glapi_set_dispatch(IndirectAPI);
1320
1321 #ifdef GLX_USE_APPLEGL
1322 do {
1323 extern void XAppleDRIUseIndirectDispatch(void);
1324 XAppleDRIUseIndirectDispatch();
1325 } while (0);
1326 #endif
1327
1328 __GLXattribute *state =
1329 (__GLXattribute *)(gc->client_state_private);
1330
1331 gc->currentContextTag = reply.contextTag;
1332 if (state->array_state == NULL) {
1333 (void) glGetString(GL_EXTENSIONS);
1334 (void) glGetString(GL_VERSION);
1335 __glXInitVertexArrayState(gc);
1336 }
1337 }
1338 else {
1339 gc->currentContextTag = -1;
1340 }
1341 } else {
1342 __glXSetCurrentContext(&dummyContext);
1343 #ifdef GLX_DIRECT_RENDERING
1344 _glapi_set_dispatch(NULL); /* no-op functions */
1345 #endif
1346 }
1347 }
1348 __glXUnlock();
1349 return GL_TRUE;
1350 }
1351
1352
1353 PUBLIC Bool glXMakeCurrent(Display *dpy, GLXDrawable draw, GLXContext gc)
1354 {
1355 return MakeContextCurrent( dpy, draw, draw, gc );
1356 }
1357
1358 PUBLIC GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
1359 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1360 (dpy, d, r, ctx), MakeContextCurrent)
1361
1362 PUBLIC GLX_ALIAS(Bool, glXMakeContextCurrent,
1363 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1364 (dpy, d, r, ctx), MakeContextCurrent)
1365
1366
1367 #ifdef DEBUG
1368 _X_HIDDEN void __glXDumpDrawBuffer(__GLXcontext *ctx)
1369 {
1370 GLubyte *p = ctx->buf;
1371 GLubyte *end = ctx->pc;
1372 GLushort opcode, length;
1373
1374 while (p < end) {
1375 /* Fetch opcode */
1376 opcode = *((GLushort*) p);
1377 length = *((GLushort*) (p + 2));
1378 printf("%2x: %5d: ", opcode, length);
1379 length -= 4;
1380 p += 4;
1381 while (length > 0) {
1382 printf("%08x ", *((unsigned *) p));
1383 p += 4;
1384 length -= 4;
1385 }
1386 printf("\n");
1387 }
1388 }
1389 #endif
1390
1391 #ifdef USE_SPARC_ASM
1392 /*
1393 * Used only when we are sparc, using sparc assembler.
1394 *
1395 */
1396
1397 static void
1398 _glx_mesa_init_sparc_glapi_relocs(void)
1399 {
1400 unsigned int *insn_ptr, *end_ptr;
1401 unsigned long disp_addr;
1402
1403 insn_ptr = &_mesa_sparc_glapi_begin;
1404 end_ptr = &_mesa_sparc_glapi_end;
1405 disp_addr = (unsigned long) &_glapi_Dispatch;
1406
1407 /*
1408 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1409 * seem to be a better way to do this:
1410 *
1411 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1412 *
1413 * This code is patching in the ADDRESS of the pointer to the
1414 * dispatch table. Hence, it must be called exactly once, because
1415 * that address is not going to change.
1416 *
1417 * What it points to can change, but Mesa (and hence, we) assume
1418 * that there is only one pointer.
1419 *
1420 */
1421 while (insn_ptr < end_ptr) {
1422 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1423 /*
1424 This code patches for 64-bit addresses. This had better
1425 not happen for Sparc/Linux, no matter what architecture we
1426 are building for. So, don't do this.
1427
1428 The 'defined(__linux_64__)' is used here as a placeholder for
1429 when we do do 64-bit usermode on sparc linux.
1430 */
1431 insn_ptr[0] |= (disp_addr >> (32 + 10));
1432 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1433 __glapi_sparc_icache_flush(&insn_ptr[0]);
1434 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1435 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1436 __glapi_sparc_icache_flush(&insn_ptr[2]);
1437 insn_ptr += 11;
1438 #else
1439 insn_ptr[0] |= (disp_addr >> 10);
1440 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1441 __glapi_sparc_icache_flush(&insn_ptr[0]);
1442 insn_ptr += 5;
1443 #endif
1444 }
1445 }
1446 #endif /* sparc ASM in use */
1447