2f3261801a6f70ce42001dd90f3635d4b47d0a75
[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 #include <assert.h>
75
76 #ifdef DEBUG
77 void __glXDumpDrawBuffer(__GLXcontext *ctx);
78 #endif
79
80 #ifdef USE_SPARC_ASM
81 /*
82 * This is where our dispatch table's bounds are.
83 * And the static mesa_init is taken directly from
84 * Mesa's 'sparc.c' initializer.
85 *
86 * We need something like this here, because this version
87 * of openGL/glx never initializes a Mesa context, and so
88 * the address of the dispatch table pointer never gets stuffed
89 * into the dispatch jump table otherwise.
90 *
91 * It matters only on SPARC, and only if you are using assembler
92 * code instead of C-code indirect dispatch.
93 *
94 * -- FEM, 04.xii.03
95 */
96 extern unsigned int _mesa_sparc_glapi_begin;
97 extern unsigned int _mesa_sparc_glapi_end;
98 extern void __glapi_sparc_icache_flush(unsigned int *);
99 static void _glx_mesa_init_sparc_glapi_relocs(void);
100 static int _mesa_sparc_needs_init = 1;
101 #define INIT_MESA_SPARC { \
102 if(_mesa_sparc_needs_init) { \
103 _glx_mesa_init_sparc_glapi_relocs(); \
104 _mesa_sparc_needs_init = 0; \
105 } \
106 }
107 #else
108 #define INIT_MESA_SPARC
109 #endif
110
111 static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
112 GLXDrawable read, GLXContext gc);
113
114 /*
115 ** We setup some dummy structures here so that the API can be used
116 ** even if no context is current.
117 */
118
119 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
120
121 /*
122 ** Dummy context used by small commands when there is no current context.
123 ** All the
124 ** gl and glx entry points are designed to operate as nop's when using
125 ** the dummy context structure.
126 */
127 static __GLXcontext dummyContext = {
128 &dummyBuffer[0],
129 &dummyBuffer[0],
130 &dummyBuffer[0],
131 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
132 sizeof(dummyBuffer),
133 };
134
135
136 /*
137 ** All indirect rendering contexts will share the same indirect dispatch table.
138 */
139 static __GLapi *IndirectAPI = NULL;
140
141
142 /*
143 * Current context management and locking
144 */
145
146 #if defined( USE_XTHREADS )
147
148 /* thread safe */
149 static GLboolean TSDinitialized = GL_FALSE;
150 static xthread_key_t ContextTSD;
151
152 __GLXcontext *__glXGetCurrentContext(void)
153 {
154 if (!TSDinitialized) {
155 xthread_key_create(&ContextTSD, NULL);
156 TSDinitialized = GL_TRUE;
157 return &dummyContext;
158 }
159 else {
160 void *p;
161 xthread_get_specific(ContextTSD, &p);
162 if (!p)
163 return &dummyContext;
164 else
165 return (__GLXcontext *) p;
166 }
167 }
168
169 void __glXSetCurrentContext(__GLXcontext *c)
170 {
171 if (!TSDinitialized) {
172 xthread_key_create(&ContextTSD, NULL);
173 TSDinitialized = GL_TRUE;
174 }
175 xthread_set_specific(ContextTSD, c);
176 }
177
178
179 /* Used by the __glXLock() and __glXUnlock() macros */
180 xmutex_rec __glXmutex;
181
182 #elif defined( PTHREADS )
183
184 pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
185
186 # if defined( GLX_USE_TLS )
187
188 /**
189 * Per-thread GLX context pointer.
190 *
191 * \c __glXSetCurrentContext is written is such a way that this pointer can
192 * \b never be \c NULL. This is important! Because of this
193 * \c __glXGetCurrentContext can be implemented as trivial macro.
194 */
195 __thread void * __glX_tls_Context __attribute__((tls_model("initial-exec")))
196 = &dummyContext;
197
198 void __glXSetCurrentContext( __GLXcontext * c )
199 {
200 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
201 }
202
203 # else
204
205 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
206
207 /**
208 * Per-thread data key.
209 *
210 * Once \c init_thread_data has been called, the per-thread data key will
211 * take a value of \c NULL. As each new thread is created the default
212 * value, in that thread, will be \c NULL.
213 */
214 static pthread_key_t ContextTSD;
215
216 /**
217 * Initialize the per-thread data key.
218 *
219 * This function is called \b exactly once per-process (not per-thread!) to
220 * initialize the per-thread data key. This is ideally done using the
221 * \c pthread_once mechanism.
222 */
223 static void init_thread_data( void )
224 {
225 if ( pthread_key_create( & ContextTSD, NULL ) != 0 ) {
226 perror( "pthread_key_create" );
227 exit( -1 );
228 }
229 }
230
231 void __glXSetCurrentContext( __GLXcontext * c )
232 {
233 pthread_once( & once_control, init_thread_data );
234 pthread_setspecific( ContextTSD, c );
235 }
236
237 __GLXcontext * __glXGetCurrentContext( void )
238 {
239 void * v;
240
241 pthread_once( & once_control, init_thread_data );
242
243 v = pthread_getspecific( ContextTSD );
244 return (v == NULL) ? & dummyContext : (__GLXcontext *) v;
245 }
246
247 # endif /* defined( GLX_USE_TLS ) */
248
249 #elif defined( THREADS )
250
251 #error Unknown threading method specified.
252
253 #else
254
255 /* not thread safe */
256 __GLXcontext *__glXcurrentContext = &dummyContext;
257
258 #endif
259
260
261 /*
262 ** You can set this cell to 1 to force the gl drawing stuff to be
263 ** one command per packet
264 */
265 int __glXDebug = 0;
266
267 /* Extension required boiler plate */
268
269 static char *__glXExtensionName = GLX_EXTENSION_NAME;
270 XExtensionInfo *__glXExtensionInfo = NULL;
271
272 static /* const */ char *error_list[] = {
273 "GLXBadContext",
274 "GLXBadContextState",
275 "GLXBadDrawable",
276 "GLXBadPixmap",
277 "GLXBadContextTag",
278 "GLXBadCurrentWindow",
279 "GLXBadRenderRequest",
280 "GLXBadLargeRequest",
281 "GLXUnsupportedPrivateRequest",
282 "GLXBadFBConfig",
283 "GLXBadPbuffer",
284 "GLXBadCurrentDrawable",
285 "GLXBadWindow",
286 };
287
288 static int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
289 {
290 GLXContext gc;
291
292 gc = __glXGetCurrentContext();
293 if (dpy == gc->currentDpy) {
294 __glXSetCurrentContext(&dummyContext);
295 #ifdef GLX_DIRECT_RENDERING
296 _glapi_set_dispatch(NULL); /* no-op functions */
297 #endif
298 __glXFreeContext(gc);
299 }
300
301 return XextRemoveDisplay(__glXExtensionInfo, dpy);
302 }
303
304
305 static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
306 __GLX_NUMBER_ERRORS, error_list)
307
308 static /* const */ XExtensionHooks __glXExtensionHooks = {
309 NULL, /* create_gc */
310 NULL, /* copy_gc */
311 NULL, /* flush_gc */
312 NULL, /* free_gc */
313 NULL, /* create_font */
314 NULL, /* free_font */
315 __glXCloseDisplay, /* close_display */
316 NULL, /* wire_to_event */
317 NULL, /* event_to_wire */
318 NULL, /* error */
319 __glXErrorString, /* error_string */
320 };
321
322 static
323 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
324 __glXExtensionName, &__glXExtensionHooks,
325 __GLX_NUMBER_EVENTS, NULL)
326
327 /************************************************************************/
328
329 /*
330 ** Free the per screen configs data as well as the array of
331 ** __glXScreenConfigs.
332 */
333 static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
334 {
335 __GLXscreenConfigs *psc;
336 GLint i, screens;
337
338 /* Free screen configuration information */
339 psc = priv->screenConfigs;
340 screens = ScreenCount(priv->dpy);
341 for (i = 0; i < screens; i++, psc++) {
342 if (psc->configs) {
343 _gl_context_modes_destroy( psc->configs );
344 if(psc->effectiveGLXexts)
345 Xfree(psc->effectiveGLXexts);
346
347 psc->configs = NULL; /* NOTE: just for paranoia */
348 }
349 Xfree((char*) psc->serverGLXexts);
350
351 #ifdef GLX_DIRECT_RENDERING
352 psc->driScreen->destroyScreen(psc);
353 #endif
354 }
355 XFree((char*) priv->screenConfigs);
356 }
357
358 /*
359 ** Release the private memory referred to in a display private
360 ** structure. The caller will free the extension structure.
361 */
362 static int __glXFreeDisplayPrivate(XExtData *extension)
363 {
364 __GLXdisplayPrivate *priv;
365
366 priv = (__GLXdisplayPrivate*) extension->private_data;
367 FreeScreenConfigs(priv);
368 if(priv->serverGLXvendor) {
369 Xfree((char*)priv->serverGLXvendor);
370 priv->serverGLXvendor = 0x0; /* to protect against double free's */
371 }
372 if(priv->serverGLXversion) {
373 Xfree((char*)priv->serverGLXversion);
374 priv->serverGLXversion = 0x0; /* to protect against double free's */
375 }
376
377 #ifdef GLX_DIRECT_RENDERING
378 /* Free the direct rendering per display data */
379 if (priv->driDisplay)
380 (*priv->driDisplay->destroyDisplay)(priv->driDisplay);
381 priv->driDisplay = NULL;
382 #endif
383
384 Xfree((char*) priv);
385 return 0;
386 }
387
388 /************************************************************************/
389
390 /*
391 ** Query the version of the GLX extension. This procedure works even if
392 ** the client extension is not completely set up.
393 */
394 static Bool QueryVersion(Display *dpy, int opcode, int *major, int *minor)
395 {
396 xGLXQueryVersionReq *req;
397 xGLXQueryVersionReply reply;
398
399 /* Send the glXQueryVersion request */
400 LockDisplay(dpy);
401 GetReq(GLXQueryVersion,req);
402 req->reqType = opcode;
403 req->glxCode = X_GLXQueryVersion;
404 req->majorVersion = GLX_MAJOR_VERSION;
405 req->minorVersion = GLX_MINOR_VERSION;
406 _XReply(dpy, (xReply*) &reply, 0, False);
407 UnlockDisplay(dpy);
408 SyncHandle();
409
410 if (reply.majorVersion != GLX_MAJOR_VERSION) {
411 /*
412 ** The server does not support the same major release as this
413 ** client.
414 */
415 return GL_FALSE;
416 }
417 *major = reply.majorVersion;
418 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
419 return GL_TRUE;
420 }
421
422
423 void
424 __glXInitializeVisualConfigFromTags( __GLcontextModes *config, int count,
425 const INT32 *bp, Bool tagged_only,
426 Bool fbconfig_style_tags )
427 {
428 int i;
429
430 if (!tagged_only) {
431 /* Copy in the first set of properties */
432 config->visualID = *bp++;
433
434 config->visualType = _gl_convert_from_x_visual_type( *bp++ );
435
436 config->rgbMode = *bp++;
437
438 config->redBits = *bp++;
439 config->greenBits = *bp++;
440 config->blueBits = *bp++;
441 config->alphaBits = *bp++;
442 config->accumRedBits = *bp++;
443 config->accumGreenBits = *bp++;
444 config->accumBlueBits = *bp++;
445 config->accumAlphaBits = *bp++;
446
447 config->doubleBufferMode = *bp++;
448 config->stereoMode = *bp++;
449
450 config->rgbBits = *bp++;
451 config->depthBits = *bp++;
452 config->stencilBits = *bp++;
453 config->numAuxBuffers = *bp++;
454 config->level = *bp++;
455
456 count -= __GLX_MIN_CONFIG_PROPS;
457 }
458
459 /*
460 ** Additional properties may be in a list at the end
461 ** of the reply. They are in pairs of property type
462 ** and property value.
463 */
464
465 #define FETCH_OR_SET(tag) \
466 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
467
468 for (i = 0; i < count; i += 2 ) {
469 switch(*bp++) {
470 case GLX_RGBA:
471 FETCH_OR_SET( rgbMode );
472 break;
473 case GLX_BUFFER_SIZE:
474 config->rgbBits = *bp++;
475 break;
476 case GLX_LEVEL:
477 config->level = *bp++;
478 break;
479 case GLX_DOUBLEBUFFER:
480 FETCH_OR_SET( doubleBufferMode );
481 break;
482 case GLX_STEREO:
483 FETCH_OR_SET( stereoMode );
484 break;
485 case GLX_AUX_BUFFERS:
486 config->numAuxBuffers = *bp++;
487 break;
488 case GLX_RED_SIZE:
489 config->redBits = *bp++;
490 break;
491 case GLX_GREEN_SIZE:
492 config->greenBits = *bp++;
493 break;
494 case GLX_BLUE_SIZE:
495 config->blueBits = *bp++;
496 break;
497 case GLX_ALPHA_SIZE:
498 config->alphaBits = *bp++;
499 break;
500 case GLX_DEPTH_SIZE:
501 config->depthBits = *bp++;
502 break;
503 case GLX_STENCIL_SIZE:
504 config->stencilBits = *bp++;
505 break;
506 case GLX_ACCUM_RED_SIZE:
507 config->accumRedBits = *bp++;
508 break;
509 case GLX_ACCUM_GREEN_SIZE:
510 config->accumGreenBits = *bp++;
511 break;
512 case GLX_ACCUM_BLUE_SIZE:
513 config->accumBlueBits = *bp++;
514 break;
515 case GLX_ACCUM_ALPHA_SIZE:
516 config->accumAlphaBits = *bp++;
517 break;
518 case GLX_VISUAL_CAVEAT_EXT:
519 config->visualRating = *bp++;
520 break;
521 case GLX_X_VISUAL_TYPE:
522 config->visualType = *bp++;
523 break;
524 case GLX_TRANSPARENT_TYPE:
525 config->transparentPixel = *bp++;
526 break;
527 case GLX_TRANSPARENT_INDEX_VALUE:
528 config->transparentIndex = *bp++;
529 break;
530 case GLX_TRANSPARENT_RED_VALUE:
531 config->transparentRed = *bp++;
532 break;
533 case GLX_TRANSPARENT_GREEN_VALUE:
534 config->transparentGreen = *bp++;
535 break;
536 case GLX_TRANSPARENT_BLUE_VALUE:
537 config->transparentBlue = *bp++;
538 break;
539 case GLX_TRANSPARENT_ALPHA_VALUE:
540 config->transparentAlpha = *bp++;
541 break;
542 case GLX_VISUAL_ID:
543 config->visualID = *bp++;
544 break;
545 case GLX_DRAWABLE_TYPE:
546 config->drawableType = *bp++;
547 break;
548 case GLX_RENDER_TYPE:
549 config->renderType = *bp++;
550 break;
551 case GLX_X_RENDERABLE:
552 config->xRenderable = *bp++;
553 break;
554 case GLX_FBCONFIG_ID:
555 config->fbconfigID = *bp++;
556 break;
557 case GLX_MAX_PBUFFER_WIDTH:
558 config->maxPbufferWidth = *bp++;
559 break;
560 case GLX_MAX_PBUFFER_HEIGHT:
561 config->maxPbufferHeight = *bp++;
562 break;
563 case GLX_MAX_PBUFFER_PIXELS:
564 config->maxPbufferPixels = *bp++;
565 break;
566 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
567 config->optimalPbufferWidth = *bp++;
568 break;
569 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
570 config->optimalPbufferHeight = *bp++;
571 break;
572 case GLX_VISUAL_SELECT_GROUP_SGIX:
573 config->visualSelectGroup = *bp++;
574 break;
575 case GLX_SWAP_METHOD_OML:
576 config->swapMethod = *bp++;
577 break;
578 case GLX_SAMPLE_BUFFERS_SGIS:
579 config->sampleBuffers = *bp++;
580 break;
581 case GLX_SAMPLES_SGIS:
582 config->samples = *bp++;
583 break;
584 case GLX_BIND_TO_TEXTURE_RGB_EXT:
585 config->bindToTextureRgb = *bp++;
586 break;
587 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
588 config->bindToTextureRgba = *bp++;
589 break;
590 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
591 config->bindToMipmapTexture = *bp++;
592 break;
593 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
594 config->bindToTextureTargets = *bp++;
595 break;
596 case GLX_Y_INVERTED_EXT:
597 config->yInverted = *bp++;
598 break;
599 case None:
600 i = count;
601 break;
602 default:
603 break;
604 }
605 }
606
607 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
608
609 config->haveAccumBuffer = ((config->accumRedBits +
610 config->accumGreenBits +
611 config->accumBlueBits +
612 config->accumAlphaBits) > 0);
613 config->haveDepthBuffer = (config->depthBits > 0);
614 config->haveStencilBuffer = (config->stencilBits > 0);
615 }
616
617 static __GLcontextModes *
618 createConfigsFromProperties(Display *dpy, int nvisuals, int nprops,
619 int screen, GLboolean tagged_only)
620 {
621 INT32 buf[__GLX_TOTAL_CONFIG], *props;
622 unsigned prop_size;
623 __GLcontextModes *modes, *m;
624 int i;
625
626 if (nprops == 0)
627 return NULL;
628
629 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
630
631 /* Check number of properties */
632 if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
633 return NULL;
634
635 /* Allocate memory for our config structure */
636 modes = _gl_context_modes_create(nvisuals, sizeof(__GLcontextModes));
637 if (!modes)
638 return NULL;
639
640 prop_size = nprops * __GLX_SIZE_INT32;
641 if (prop_size <= sizeof(buf))
642 props = buf;
643 else
644 props = Xmalloc(prop_size);
645
646 /* Read each config structure and convert it into our format */
647 m = modes;
648 for (i = 0; i < nvisuals; i++) {
649 _XRead(dpy, (char *)props, prop_size);
650 /* Older X servers don't send this so we default it here. */
651 m->drawableType = GLX_WINDOW_BIT;
652 __glXInitializeVisualConfigFromTags(m, nprops, props,
653 tagged_only, GL_TRUE);
654 m->screen = screen;
655 m = m->next;
656 }
657
658 if (props != buf)
659 Xfree(props);
660
661 return modes;
662 }
663
664 static GLboolean
665 getVisualConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
666 {
667 xGLXGetVisualConfigsReq *req;
668 __GLXscreenConfigs *psc;
669 xGLXGetVisualConfigsReply reply;
670
671 LockDisplay(dpy);
672
673 psc = priv->screenConfigs + screen;
674 psc->visuals = NULL;
675 GetReq(GLXGetVisualConfigs, req);
676 req->reqType = priv->majorOpcode;
677 req->glxCode = X_GLXGetVisualConfigs;
678 req->screen = screen;
679
680 if (!_XReply(dpy, (xReply*) &reply, 0, False))
681 goto out;
682
683 psc->visuals = createConfigsFromProperties(dpy,
684 reply.numVisuals,
685 reply.numProps,
686 screen, GL_FALSE);
687
688 out:
689 UnlockDisplay(dpy);
690 return psc->visuals != NULL;
691 }
692
693 static GLboolean
694 getFBConfigs(Display *dpy, __GLXdisplayPrivate *priv, int screen)
695 {
696 xGLXGetFBConfigsReq *fb_req;
697 xGLXGetFBConfigsSGIXReq *sgi_req;
698 xGLXVendorPrivateWithReplyReq *vpreq;
699 xGLXGetFBConfigsReply reply;
700 __GLXscreenConfigs *psc;
701
702 psc = priv->screenConfigs + screen;
703 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
704 X_GLXQueryServerString,
705 screen, GLX_EXTENSIONS);
706
707 LockDisplay(dpy);
708
709 psc->configs = NULL;
710 if (atof(priv->serverGLXversion) >= 1.3) {
711 GetReq(GLXGetFBConfigs, fb_req);
712 fb_req->reqType = priv->majorOpcode;
713 fb_req->glxCode = X_GLXGetFBConfigs;
714 fb_req->screen = screen;
715 } else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
716 GetReqExtra(GLXVendorPrivateWithReply,
717 sz_xGLXGetFBConfigsSGIXReq +
718 sz_xGLXVendorPrivateWithReplyReq, vpreq);
719 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
720 sgi_req->reqType = priv->majorOpcode;
721 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
722 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
723 sgi_req->screen = screen;
724 } else
725 goto out;
726
727 if (!_XReply(dpy, (xReply*) &reply, 0, False))
728 goto out;
729
730 psc->configs = createConfigsFromProperties(dpy,
731 reply.numFBConfigs,
732 reply.numAttribs * 2,
733 screen, GL_TRUE);
734
735 out:
736 UnlockDisplay(dpy);
737 return psc->configs != NULL;
738 }
739
740 /*
741 ** Allocate the memory for the per screen configs for each screen.
742 ** If that works then fetch the per screen configs data.
743 */
744 static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
745 {
746 __GLXscreenConfigs *psc;
747 GLint i, screens;
748
749 /*
750 ** First allocate memory for the array of per screen configs.
751 */
752 screens = ScreenCount(dpy);
753 psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
754 if (!psc) {
755 return GL_FALSE;
756 }
757 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
758 priv->screenConfigs = psc;
759
760 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
761 X_GLXQueryServerString,
762 0, GLX_VERSION);
763 if ( priv->serverGLXversion == NULL ) {
764 FreeScreenConfigs(priv);
765 return GL_FALSE;
766 }
767
768 for (i = 0; i < screens; i++, psc++) {
769 getVisualConfigs(dpy, priv, i);
770 getFBConfigs(dpy, priv, i);
771
772 psc->scr = i;
773 psc->dpy = dpy;
774 #ifdef GLX_DIRECT_RENDERING
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 __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 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 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 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 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 __DRIdrawable *
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->driDrawable;
1193
1194 pdraw = psc->driScreen->createDrawable(psc, drawable, gc);
1195
1196 return &pdraw->driDrawable;
1197 }
1198
1199 static Bool BindContextWrapper( Display *dpy, GLXContext gc,
1200 GLXDrawable draw, GLXDrawable read )
1201 {
1202 __DRIdrawable *pdraw = FetchDRIDrawable(dpy, draw, gc);
1203 __DRIdrawable *pread = FetchDRIDrawable(dpy, read, gc);
1204
1205 return (*gc->driContext.bindContext)(&gc->driContext, pdraw, pread);
1206 }
1207
1208
1209 static Bool UnbindContextWrapper( GLXContext gc )
1210 {
1211 return (*gc->driContext.unbindContext)(&gc->driContext);
1212 }
1213 #endif /* GLX_DIRECT_RENDERING */
1214
1215
1216 /**
1217 * Make a particular context current.
1218 *
1219 * \note This is in this file so that it can access dummyContext.
1220 */
1221 USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
1222 GLXDrawable read, GLXContext gc)
1223 {
1224 xGLXMakeCurrentReply reply;
1225 const GLXContext oldGC = __glXGetCurrentContext();
1226 const CARD8 opcode = __glXSetupForCommand(dpy);
1227 const CARD8 oldOpcode = ((gc == oldGC) || (oldGC == &dummyContext))
1228 ? opcode : __glXSetupForCommand(oldGC->currentDpy);
1229 Bool bindReturnValue;
1230
1231
1232 if (!opcode || !oldOpcode) {
1233 return GL_FALSE;
1234 }
1235
1236 /* Make sure that the new context has a nonzero ID. In the request,
1237 * a zero context ID is used only to mean that we bind to no current
1238 * context.
1239 */
1240 if ((gc != NULL) && (gc->xid == None)) {
1241 return GL_FALSE;
1242 }
1243
1244 #ifndef GLX_DIRECT_RENDERING
1245 if (gc && gc->isDirect) {
1246 return GL_FALSE;
1247 }
1248 #endif
1249
1250 _glapi_check_multithread();
1251
1252 #ifdef GLX_DIRECT_RENDERING
1253 /* Bind the direct rendering context to the drawable */
1254 if (gc && gc->isDirect) {
1255 bindReturnValue = (gc->driContext.private)
1256 ? BindContextWrapper(dpy, gc, draw, read)
1257 : False;
1258 } else
1259 #endif
1260 {
1261 /* Send a glXMakeCurrent request to bind the new context. */
1262 bindReturnValue =
1263 SendMakeCurrentRequest(dpy, opcode, gc ? gc->xid : None,
1264 ((dpy != oldGC->currentDpy) || oldGC->isDirect)
1265 ? None : oldGC->currentContextTag,
1266 draw, read, &reply);
1267 }
1268
1269
1270 if (!bindReturnValue) {
1271 return False;
1272 }
1273
1274 if ((dpy != oldGC->currentDpy || (gc && gc->isDirect)) &&
1275 !oldGC->isDirect && oldGC != &dummyContext) {
1276 xGLXMakeCurrentReply dummy_reply;
1277
1278 /* We are either switching from one dpy to another and have to
1279 * send a request to the previous dpy to unbind the previous
1280 * context, or we are switching away from a indirect context to
1281 * a direct context and have to send a request to the dpy to
1282 * unbind the previous context.
1283 */
1284 (void) SendMakeCurrentRequest(oldGC->currentDpy, oldOpcode, None,
1285 oldGC->currentContextTag, None, None,
1286 & dummy_reply);
1287 }
1288 #ifdef GLX_DIRECT_RENDERING
1289 else if (oldGC->isDirect && oldGC->driContext.private) {
1290 (void) UnbindContextWrapper(oldGC);
1291 }
1292 #endif
1293
1294
1295 /* Update our notion of what is current */
1296 __glXLock();
1297 if (gc == oldGC) {
1298 /* Even though the contexts are the same the drawable might have
1299 * changed. Note that gc cannot be the dummy, and that oldGC
1300 * cannot be NULL, therefore if they are the same, gc is not
1301 * NULL and not the dummy.
1302 */
1303 gc->currentDrawable = draw;
1304 gc->currentReadable = read;
1305 } else {
1306 if (oldGC != &dummyContext) {
1307 /* Old current context is no longer current to anybody */
1308 oldGC->currentDpy = 0;
1309 oldGC->currentDrawable = None;
1310 oldGC->currentReadable = None;
1311 oldGC->currentContextTag = 0;
1312
1313 if (oldGC->xid == None) {
1314 /* We are switching away from a context that was
1315 * previously destroyed, so we need to free the memory
1316 * for the old handle.
1317 */
1318 #ifdef GLX_DIRECT_RENDERING
1319 /* Destroy the old direct rendering context */
1320 if (oldGC->isDirect) {
1321 if (oldGC->driContext.private) {
1322 (*oldGC->driContext.destroyContext)
1323 (&oldGC->driContext);
1324 XF86DRIDestroyContext(oldGC->createDpy,
1325 oldGC->psc->scr,
1326 gc->hwContextID);
1327 oldGC->driContext.private = NULL;
1328 }
1329 }
1330 #endif
1331 __glXFreeContext(oldGC);
1332 }
1333 }
1334 if (gc) {
1335 __glXSetCurrentContext(gc);
1336
1337 gc->currentDpy = dpy;
1338 gc->currentDrawable = draw;
1339 gc->currentReadable = read;
1340
1341 if (!gc->isDirect) {
1342 if (!IndirectAPI)
1343 IndirectAPI = __glXNewIndirectAPI();
1344 _glapi_set_dispatch(IndirectAPI);
1345
1346 #ifdef GLX_USE_APPLEGL
1347 do {
1348 extern void XAppleDRIUseIndirectDispatch(void);
1349 XAppleDRIUseIndirectDispatch();
1350 } while (0);
1351 #endif
1352
1353 __GLXattribute *state =
1354 (__GLXattribute *)(gc->client_state_private);
1355
1356 gc->currentContextTag = reply.contextTag;
1357 if (state->array_state == NULL) {
1358 (void) glGetString(GL_EXTENSIONS);
1359 (void) glGetString(GL_VERSION);
1360 __glXInitVertexArrayState(gc);
1361 }
1362 }
1363 else {
1364 gc->currentContextTag = -1;
1365 }
1366 } else {
1367 __glXSetCurrentContext(&dummyContext);
1368 #ifdef GLX_DIRECT_RENDERING
1369 _glapi_set_dispatch(NULL); /* no-op functions */
1370 #endif
1371 }
1372 }
1373 __glXUnlock();
1374 return GL_TRUE;
1375 }
1376
1377
1378 PUBLIC Bool glXMakeCurrent(Display *dpy, GLXDrawable draw, GLXContext gc)
1379 {
1380 return MakeContextCurrent( dpy, draw, draw, gc );
1381 }
1382
1383 PUBLIC GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
1384 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1385 (dpy, d, r, ctx), MakeContextCurrent)
1386
1387 PUBLIC GLX_ALIAS(Bool, glXMakeContextCurrent,
1388 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1389 (dpy, d, r, ctx), MakeContextCurrent)
1390
1391
1392 #ifdef DEBUG
1393 void __glXDumpDrawBuffer(__GLXcontext *ctx)
1394 {
1395 GLubyte *p = ctx->buf;
1396 GLubyte *end = ctx->pc;
1397 GLushort opcode, length;
1398
1399 while (p < end) {
1400 /* Fetch opcode */
1401 opcode = *((GLushort*) p);
1402 length = *((GLushort*) (p + 2));
1403 printf("%2x: %5d: ", opcode, length);
1404 length -= 4;
1405 p += 4;
1406 while (length > 0) {
1407 printf("%08x ", *((unsigned *) p));
1408 p += 4;
1409 length -= 4;
1410 }
1411 printf("\n");
1412 }
1413 }
1414 #endif
1415
1416 #ifdef USE_SPARC_ASM
1417 /*
1418 * Used only when we are sparc, using sparc assembler.
1419 *
1420 */
1421
1422 static void
1423 _glx_mesa_init_sparc_glapi_relocs(void)
1424 {
1425 unsigned int *insn_ptr, *end_ptr;
1426 unsigned long disp_addr;
1427
1428 insn_ptr = &_mesa_sparc_glapi_begin;
1429 end_ptr = &_mesa_sparc_glapi_end;
1430 disp_addr = (unsigned long) &_glapi_Dispatch;
1431
1432 /*
1433 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1434 * seem to be a better way to do this:
1435 *
1436 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1437 *
1438 * This code is patching in the ADDRESS of the pointer to the
1439 * dispatch table. Hence, it must be called exactly once, because
1440 * that address is not going to change.
1441 *
1442 * What it points to can change, but Mesa (and hence, we) assume
1443 * that there is only one pointer.
1444 *
1445 */
1446 while (insn_ptr < end_ptr) {
1447 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1448 /*
1449 This code patches for 64-bit addresses. This had better
1450 not happen for Sparc/Linux, no matter what architecture we
1451 are building for. So, don't do this.
1452
1453 The 'defined(__linux_64__)' is used here as a placeholder for
1454 when we do do 64-bit usermode on sparc linux.
1455 */
1456 insn_ptr[0] |= (disp_addr >> (32 + 10));
1457 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1458 __glapi_sparc_icache_flush(&insn_ptr[0]);
1459 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1460 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1461 __glapi_sparc_icache_flush(&insn_ptr[2]);
1462 insn_ptr += 11;
1463 #else
1464 insn_ptr[0] |= (disp_addr >> 10);
1465 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1466 __glapi_sparc_icache_flush(&insn_ptr[0]);
1467 insn_ptr += 5;
1468 #endif
1469 }
1470 }
1471 #endif /* sparc ASM in use */
1472