remove stray 'foo' line
[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 <assert.h>
52 #include "indirect_init.h"
53 #include "glapi.h"
54 #include "glxextensions.h"
55 #include "glcontextmodes.h"
56 #include "glheader.h"
57
58 #ifdef GLX_DIRECT_RENDERING
59 #include <inttypes.h>
60 #include <sys/mman.h>
61 #include "xf86dri.h"
62 #include "sarea.h"
63 #include "dri_glx.h"
64 #endif
65
66 #ifdef USE_XCB
67 #include <X11/xcl.h>
68 #include <X11/XCB/xcb.h>
69 #include <X11/XCB/glx.h>
70 #endif
71
72 #include <assert.h>
73
74 #ifdef DEBUG
75 void __glXDumpDrawBuffer(__GLXcontext *ctx);
76 #endif
77
78 #ifdef USE_SPARC_ASM
79 /*
80 * This is where our dispatch table's bounds are.
81 * And the static mesa_init is taken directly from
82 * Mesa's 'sparc.c' initializer.
83 *
84 * We need something like this here, because this version
85 * of openGL/glx never initializes a Mesa context, and so
86 * the address of the dispatch table pointer never gets stuffed
87 * into the dispatch jump table otherwise.
88 *
89 * It matters only on SPARC, and only if you are using assembler
90 * code instead of C-code indirect dispatch.
91 *
92 * -- FEM, 04.xii.03
93 */
94 extern unsigned int _mesa_sparc_glapi_begin;
95 extern unsigned int _mesa_sparc_glapi_end;
96 extern void __glapi_sparc_icache_flush(unsigned int *);
97 static void _glx_mesa_init_sparc_glapi_relocs(void);
98 static int _mesa_sparc_needs_init = 1;
99 #define INIT_MESA_SPARC { \
100 if(_mesa_sparc_needs_init) { \
101 _glx_mesa_init_sparc_glapi_relocs(); \
102 _mesa_sparc_needs_init = 0; \
103 } \
104 }
105 #else
106 #define INIT_MESA_SPARC
107 #endif
108
109 #ifdef GLX_DIRECT_RENDERING
110 static __DRIscreen *__glXFindDRIScreen(__DRInativeDisplay *dpy, int scrn);
111 #endif /* GLX_DIRECT_RENDERING */
112
113 static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
114 GLXDrawable read, GLXContext gc);
115
116 /*
117 ** We setup some dummy structures here so that the API can be used
118 ** even if no context is current.
119 */
120
121 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
122
123 /*
124 ** Dummy context used by small commands when there is no current context.
125 ** All the
126 ** gl and glx entry points are designed to operate as nop's when using
127 ** the dummy context structure.
128 */
129 static __GLXcontext dummyContext = {
130 &dummyBuffer[0],
131 &dummyBuffer[0],
132 &dummyBuffer[0],
133 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
134 sizeof(dummyBuffer),
135 };
136
137
138 /*
139 ** All indirect rendering contexts will share the same indirect dispatch table.
140 */
141 static __GLapi *IndirectAPI = NULL;
142
143
144 /*
145 * Current context management and locking
146 */
147
148 #if defined( USE_XTHREADS )
149
150 /* thread safe */
151 static GLboolean TSDinitialized = GL_FALSE;
152 static xthread_key_t ContextTSD;
153
154 __GLXcontext *__glXGetCurrentContext(void)
155 {
156 if (!TSDinitialized) {
157 xthread_key_create(&ContextTSD, NULL);
158 TSDinitialized = GL_TRUE;
159 return &dummyContext;
160 }
161 else {
162 void *p;
163 xthread_get_specific(ContextTSD, &p);
164 if (!p)
165 return &dummyContext;
166 else
167 return (__GLXcontext *) p;
168 }
169 }
170
171 void __glXSetCurrentContext(__GLXcontext *c)
172 {
173 if (!TSDinitialized) {
174 xthread_key_create(&ContextTSD, NULL);
175 TSDinitialized = GL_TRUE;
176 }
177 xthread_set_specific(ContextTSD, c);
178 }
179
180
181 /* Used by the __glXLock() and __glXUnlock() macros */
182 xmutex_rec __glXmutex;
183
184 #elif defined( PTHREADS )
185
186 pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
187
188 # if defined( GLX_USE_TLS )
189
190 /**
191 * Per-thread GLX context pointer.
192 *
193 * \c __glXSetCurrentContext is written is such a way that this pointer can
194 * \b never be \c NULL. This is important! Because of this
195 * \c __glXGetCurrentContext can be implemented as trivial macro.
196 */
197 __thread void * __glX_tls_Context __attribute__((tls_model("initial-exec")))
198 = &dummyContext;
199
200 void __glXSetCurrentContext( __GLXcontext * c )
201 {
202 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
203 }
204
205 # else
206
207 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
208
209 /**
210 * Per-thread data key.
211 *
212 * Once \c init_thread_data has been called, the per-thread data key will
213 * take a value of \c NULL. As each new thread is created the default
214 * value, in that thread, will be \c NULL.
215 */
216 static pthread_key_t ContextTSD;
217
218 /**
219 * Initialize the per-thread data key.
220 *
221 * This function is called \b exactly once per-process (not per-thread!) to
222 * initialize the per-thread data key. This is ideally done using the
223 * \c pthread_once mechanism.
224 */
225 static void init_thread_data( void )
226 {
227 if ( pthread_key_create( & ContextTSD, NULL ) != 0 ) {
228 perror( "pthread_key_create" );
229 exit( -1 );
230 }
231 }
232
233 void __glXSetCurrentContext( __GLXcontext * c )
234 {
235 pthread_once( & once_control, init_thread_data );
236 pthread_setspecific( ContextTSD, c );
237 }
238
239 __GLXcontext * __glXGetCurrentContext( void )
240 {
241 void * v;
242
243 pthread_once( & once_control, init_thread_data );
244
245 v = pthread_getspecific( ContextTSD );
246 return (v == NULL) ? & dummyContext : (__GLXcontext *) v;
247 }
248
249 # endif /* defined( GLX_USE_TLS ) */
250
251 #elif defined( THREADS )
252
253 #error Unknown threading method specified.
254
255 #else
256
257 /* not thread safe */
258 __GLXcontext *__glXcurrentContext = &dummyContext;
259
260 #endif
261
262
263 /*
264 ** You can set this cell to 1 to force the gl drawing stuff to be
265 ** one command per packet
266 */
267 int __glXDebug = 0;
268
269 /*
270 ** forward prototype declarations
271 */
272 int __glXCloseDisplay(Display *dpy, XExtCodes *codes);
273
274
275 /************************************************************************/
276
277 /* Extension required boiler plate */
278
279 static char *__glXExtensionName = GLX_EXTENSION_NAME;
280 XExtensionInfo *__glXExtensionInfo = NULL;
281
282 static /* const */ char *error_list[] = {
283 "GLXBadContext",
284 "GLXBadContextState",
285 "GLXBadDrawable",
286 "GLXBadPixmap",
287 "GLXBadContextTag",
288 "GLXBadCurrentWindow",
289 "GLXBadRenderRequest",
290 "GLXBadLargeRequest",
291 "GLXUnsupportedPrivateRequest",
292 };
293
294 int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
295 {
296 GLXContext gc;
297
298 gc = __glXGetCurrentContext();
299 if (dpy == gc->currentDpy) {
300 __glXSetCurrentContext(&dummyContext);
301 #ifdef GLX_DIRECT_RENDERING
302 _glapi_set_dispatch(NULL); /* no-op functions */
303 #endif
304 __glXFreeContext(gc);
305 }
306
307 return XextRemoveDisplay(__glXExtensionInfo, dpy);
308 }
309
310
311 static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
312 __GLX_NUMBER_ERRORS, error_list)
313
314 static /* const */ XExtensionHooks __glXExtensionHooks = {
315 NULL, /* create_gc */
316 NULL, /* copy_gc */
317 NULL, /* flush_gc */
318 NULL, /* free_gc */
319 NULL, /* create_font */
320 NULL, /* free_font */
321 __glXCloseDisplay, /* close_display */
322 NULL, /* wire_to_event */
323 NULL, /* event_to_wire */
324 NULL, /* error */
325 __glXErrorString, /* error_string */
326 };
327
328 static
329 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
330 __glXExtensionName, &__glXExtensionHooks,
331 __GLX_NUMBER_EVENTS, NULL)
332
333 /************************************************************************/
334
335 /*
336 ** Free the per screen configs data as well as the array of
337 ** __glXScreenConfigs.
338 */
339 static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
340 {
341 __GLXscreenConfigs *psc;
342 GLint i, screens;
343
344 /* Free screen configuration information */
345 psc = priv->screenConfigs;
346 screens = ScreenCount(priv->dpy);
347 for (i = 0; i < screens; i++, psc++) {
348 if (psc->configs) {
349 _gl_context_modes_destroy( psc->configs );
350 if(psc->effectiveGLXexts)
351 Xfree(psc->effectiveGLXexts);
352
353 psc->configs = NULL; /* NOTE: just for paranoia */
354 }
355
356 #ifdef GLX_DIRECT_RENDERING
357 /* Free the direct rendering per screen data */
358 if (psc->driScreen.private)
359 (*psc->driScreen.destroyScreen)(priv->dpy, i,
360 psc->driScreen.private);
361 psc->driScreen.private = NULL;
362 #endif
363 }
364 XFree((char*) priv->screenConfigs);
365 }
366
367 /*
368 ** Release the private memory referred to in a display private
369 ** structure. The caller will free the extension structure.
370 */
371 static int __glXFreeDisplayPrivate(XExtData *extension)
372 {
373 __GLXdisplayPrivate *priv;
374
375 priv = (__GLXdisplayPrivate*) extension->private_data;
376 FreeScreenConfigs(priv);
377 if(priv->serverGLXvendor) {
378 Xfree((char*)priv->serverGLXvendor);
379 priv->serverGLXvendor = 0x0; /* to protect against double free's */
380 }
381 if(priv->serverGLXversion) {
382 Xfree((char*)priv->serverGLXversion);
383 priv->serverGLXversion = 0x0; /* to protect against double free's */
384 }
385
386 #if 0 /* GLX_DIRECT_RENDERING */
387 /* Free the direct rendering per display data */
388 if (priv->driDisplay.private)
389 (*priv->driDisplay.destroyDisplay)(priv->dpy,
390 priv->driDisplay.private);
391 priv->driDisplay.private = NULL;
392 #endif
393
394 Xfree((char*) priv);
395 return 0;
396 }
397
398 /************************************************************************/
399
400 /*
401 ** Query the version of the GLX extension. This procedure works even if
402 ** the client extension is not completely set up.
403 */
404 static Bool QueryVersion(Display *dpy, int opcode, int *major, int *minor)
405 {
406 xGLXQueryVersionReq *req;
407 xGLXQueryVersionReply reply;
408
409 /* Send the glXQueryVersion request */
410 LockDisplay(dpy);
411 GetReq(GLXQueryVersion,req);
412 req->reqType = opcode;
413 req->glxCode = X_GLXQueryVersion;
414 req->majorVersion = GLX_MAJOR_VERSION;
415 req->minorVersion = GLX_MINOR_VERSION;
416 _XReply(dpy, (xReply*) &reply, 0, False);
417 UnlockDisplay(dpy);
418 SyncHandle();
419
420 if (reply.majorVersion != GLX_MAJOR_VERSION) {
421 /*
422 ** The server does not support the same major release as this
423 ** client.
424 */
425 return GL_FALSE;
426 }
427 *major = reply.majorVersion;
428 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
429 return GL_TRUE;
430 }
431
432
433 void
434 __glXInitializeVisualConfigFromTags( __GLcontextModes *config, int count,
435 const INT32 *bp, Bool tagged_only,
436 Bool fbconfig_style_tags )
437 {
438 int i;
439
440 if (!tagged_only) {
441 /* Copy in the first set of properties */
442 config->visualID = *bp++;
443
444 config->visualType = _gl_convert_from_x_visual_type( *bp++ );
445
446 config->rgbMode = *bp++;
447
448 config->redBits = *bp++;
449 config->greenBits = *bp++;
450 config->blueBits = *bp++;
451 config->alphaBits = *bp++;
452 config->accumRedBits = *bp++;
453 config->accumGreenBits = *bp++;
454 config->accumBlueBits = *bp++;
455 config->accumAlphaBits = *bp++;
456
457 config->doubleBufferMode = *bp++;
458 config->stereoMode = *bp++;
459
460 config->rgbBits = *bp++;
461 config->depthBits = *bp++;
462 config->stencilBits = *bp++;
463 config->numAuxBuffers = *bp++;
464 config->level = *bp++;
465
466 count -= __GLX_MIN_CONFIG_PROPS;
467 }
468
469 /*
470 ** Additional properties may be in a list at the end
471 ** of the reply. They are in pairs of property type
472 ** and property value.
473 */
474
475 #define FETCH_OR_SET(tag) \
476 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
477
478 for (i = 0; i < count; i += 2 ) {
479 switch(*bp++) {
480 case GLX_RGBA:
481 FETCH_OR_SET( rgbMode );
482 break;
483 case GLX_BUFFER_SIZE:
484 config->rgbBits = *bp++;
485 break;
486 case GLX_LEVEL:
487 config->level = *bp++;
488 break;
489 case GLX_DOUBLEBUFFER:
490 FETCH_OR_SET( doubleBufferMode );
491 break;
492 case GLX_STEREO:
493 FETCH_OR_SET( stereoMode );
494 break;
495 case GLX_AUX_BUFFERS:
496 config->numAuxBuffers = *bp++;
497 break;
498 case GLX_RED_SIZE:
499 config->redBits = *bp++;
500 break;
501 case GLX_GREEN_SIZE:
502 config->greenBits = *bp++;
503 break;
504 case GLX_BLUE_SIZE:
505 config->blueBits = *bp++;
506 break;
507 case GLX_ALPHA_SIZE:
508 config->alphaBits = *bp++;
509 break;
510 case GLX_DEPTH_SIZE:
511 config->depthBits = *bp++;
512 break;
513 case GLX_STENCIL_SIZE:
514 config->stencilBits = *bp++;
515 break;
516 case GLX_ACCUM_RED_SIZE:
517 config->accumRedBits = *bp++;
518 break;
519 case GLX_ACCUM_GREEN_SIZE:
520 config->accumGreenBits = *bp++;
521 break;
522 case GLX_ACCUM_BLUE_SIZE:
523 config->accumBlueBits = *bp++;
524 break;
525 case GLX_ACCUM_ALPHA_SIZE:
526 config->accumAlphaBits = *bp++;
527 break;
528 case GLX_VISUAL_CAVEAT_EXT:
529 config->visualRating = *bp++;
530 break;
531 case GLX_X_VISUAL_TYPE:
532 config->visualType = *bp++;
533 break;
534 case GLX_TRANSPARENT_TYPE:
535 config->transparentPixel = *bp++;
536 break;
537 case GLX_TRANSPARENT_INDEX_VALUE:
538 config->transparentIndex = *bp++;
539 break;
540 case GLX_TRANSPARENT_RED_VALUE:
541 config->transparentRed = *bp++;
542 break;
543 case GLX_TRANSPARENT_GREEN_VALUE:
544 config->transparentGreen = *bp++;
545 break;
546 case GLX_TRANSPARENT_BLUE_VALUE:
547 config->transparentBlue = *bp++;
548 break;
549 case GLX_TRANSPARENT_ALPHA_VALUE:
550 config->transparentAlpha = *bp++;
551 break;
552 case GLX_VISUAL_ID:
553 config->visualID = *bp++;
554 break;
555 case GLX_DRAWABLE_TYPE:
556 config->drawableType = *bp++;
557 break;
558 case GLX_RENDER_TYPE:
559 config->renderType = *bp++;
560 break;
561 case GLX_X_RENDERABLE:
562 config->xRenderable = *bp++;
563 break;
564 case GLX_FBCONFIG_ID:
565 config->fbconfigID = *bp++;
566 break;
567 case GLX_MAX_PBUFFER_WIDTH:
568 config->maxPbufferWidth = *bp++;
569 break;
570 case GLX_MAX_PBUFFER_HEIGHT:
571 config->maxPbufferHeight = *bp++;
572 break;
573 case GLX_MAX_PBUFFER_PIXELS:
574 config->maxPbufferPixels = *bp++;
575 break;
576 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
577 config->optimalPbufferWidth = *bp++;
578 break;
579 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
580 config->optimalPbufferHeight = *bp++;
581 break;
582 case GLX_VISUAL_SELECT_GROUP_SGIX:
583 config->visualSelectGroup = *bp++;
584 break;
585 case GLX_SWAP_METHOD_OML:
586 config->swapMethod = *bp++;
587 break;
588 case GLX_SAMPLE_BUFFERS_SGIS:
589 config->sampleBuffers = *bp++;
590 break;
591 case GLX_SAMPLES_SGIS:
592 config->samples = *bp++;
593 break;
594 case None:
595 i = count;
596 break;
597 default:
598 break;
599 }
600 }
601
602 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
603
604 config->haveAccumBuffer = ((config->accumRedBits +
605 config->accumGreenBits +
606 config->accumBlueBits +
607 config->accumAlphaBits) > 0);
608 config->haveDepthBuffer = (config->depthBits > 0);
609 config->haveStencilBuffer = (config->stencilBits > 0);
610 }
611
612
613 #ifdef GLX_DIRECT_RENDERING
614 static unsigned
615 filter_modes( __GLcontextModes ** server_modes,
616 const __GLcontextModes * driver_modes )
617 {
618 __GLcontextModes * m;
619 __GLcontextModes ** prev_next;
620 const __GLcontextModes * check;
621 unsigned modes_count = 0;
622
623 if ( driver_modes == NULL ) {
624 fprintf(stderr, "libGL warning: 3D driver returned no fbconfigs.\n");
625 return 0;
626 }
627
628 /* For each mode in server_modes, check to see if a matching mode exists
629 * in driver_modes. If not, then the mode is not available.
630 */
631
632 prev_next = server_modes;
633 for ( m = *prev_next ; m != NULL ; m = *prev_next ) {
634 GLboolean do_delete = GL_TRUE;
635
636 for ( check = driver_modes ; check != NULL ; check = check->next ) {
637 if ( _gl_context_modes_are_same( m, check ) ) {
638 do_delete = GL_FALSE;
639 break;
640 }
641 }
642
643 /* The 3D has to support all the modes that match the GLX visuals
644 * sent from the X server.
645 */
646 if ( do_delete && (m->visualID != 0) ) {
647 do_delete = GL_FALSE;
648
649 fprintf(stderr, "libGL warning: 3D driver claims to not support "
650 "visual 0x%02x\n", m->visualID);
651 }
652
653 if ( do_delete ) {
654 *prev_next = m->next;
655
656 m->next = NULL;
657 _gl_context_modes_destroy( m );
658 }
659 else {
660 modes_count++;
661 prev_next = & m->next;
662 }
663 }
664
665 return modes_count;
666 }
667
668
669 /**
670 * Implement \c __DRIinterfaceMethods::getProcAddress.
671 */
672 static __DRIfuncPtr get_proc_address( const char * proc_name )
673 {
674 if (strcmp( proc_name, "glxEnableExtension" ) == 0) {
675 return (__DRIfuncPtr) __glXScrEnableExtension;
676 }
677
678 return NULL;
679 }
680
681
682 /**
683 * Table of functions exported by the loader to the driver.
684 */
685 static const __DRIinterfaceMethods interface_methods = {
686 get_proc_address,
687
688 _gl_context_modes_create,
689 _gl_context_modes_destroy,
690
691 __glXFindDRIScreen,
692 __glXWindowExists,
693
694 XF86DRICreateContextWithConfig,
695 XF86DRIDestroyContext,
696
697 XF86DRICreateDrawable,
698 XF86DRIDestroyDrawable,
699 XF86DRIGetDrawableInfo,
700
701 __glXGetUST,
702 glXGetMscRateOML,
703 };
704
705
706 /**
707 * Perform the required libGL-side initialization and call the client-side
708 * driver's \c __driCreateNewScreen function.
709 *
710 * \param dpy Display pointer.
711 * \param scrn Screen number on the display.
712 * \param psc DRI screen information.
713 * \param driDpy DRI display information.
714 * \param createNewScreen Pointer to the client-side driver's
715 * \c __driCreateNewScreen function.
716 * \returns A pointer to the \c __DRIscreenPrivate structure returned by
717 * the client-side driver on success, or \c NULL on failure.
718 *
719 * \todo This function needs to be modified to remove context-modes from the
720 * list stored in the \c __GLXscreenConfigsRec to match the list
721 * returned by the client-side driver.
722 */
723 static void *
724 CallCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
725 __DRIdisplay * driDpy,
726 PFNCREATENEWSCREENFUNC createNewScreen)
727 {
728 __DRIscreenPrivate *psp = NULL;
729 #ifndef GLX_USE_APPLEGL
730 drm_handle_t hSAREA;
731 drmAddress pSAREA = MAP_FAILED;
732 char *BusID;
733 __DRIversion ddx_version;
734 __DRIversion dri_version;
735 __DRIversion drm_version;
736 __DRIframebuffer framebuffer;
737 int fd = -1;
738 int status;
739 const char * err_msg;
740 const char * err_extra;
741 int api_ver = __glXGetInternalVersion();
742
743
744 dri_version.major = driDpy->private->driMajor;
745 dri_version.minor = driDpy->private->driMinor;
746 dri_version.patch = driDpy->private->driPatch;
747
748
749 err_msg = "XF86DRIOpenConnection";
750 err_extra = NULL;
751
752 framebuffer.base = MAP_FAILED;
753 framebuffer.dev_priv = NULL;
754
755 if (XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
756 fd = drmOpen(NULL,BusID);
757 Xfree(BusID); /* No longer needed */
758
759 err_msg = "open DRM";
760 err_extra = strerror( -fd );
761
762 if (fd >= 0) {
763 drm_magic_t magic;
764
765 err_msg = "drmGetMagic";
766 err_extra = NULL;
767
768 if (!drmGetMagic(fd, &magic)) {
769 drmVersionPtr version = drmGetVersion(fd);
770 if (version) {
771 drm_version.major = version->version_major;
772 drm_version.minor = version->version_minor;
773 drm_version.patch = version->version_patchlevel;
774 drmFreeVersion(version);
775 }
776 else {
777 drm_version.major = -1;
778 drm_version.minor = -1;
779 drm_version.patch = -1;
780 }
781
782 err_msg = "XF86DRIAuthConnection";
783 if (XF86DRIAuthConnection(dpy, scrn, magic)) {
784 char *driverName;
785
786 /*
787 * Get device name (like "tdfx") and the ddx version
788 * numbers. We'll check the version in each DRI driver's
789 * "createNewScreen" function.
790 */
791 err_msg = "XF86DRIGetClientDriverName";
792 if (XF86DRIGetClientDriverName(dpy, scrn,
793 &ddx_version.major,
794 &ddx_version.minor,
795 &ddx_version.patch,
796 &driverName)) {
797 drm_handle_t hFB;
798 int junk;
799
800 /* No longer needed. */
801 Xfree( driverName );
802
803
804 /*
805 * Get device-specific info. pDevPriv will point to a struct
806 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
807 * that has information about the screen size, depth, pitch,
808 * ancilliary buffers, DRM mmap handles, etc.
809 */
810 err_msg = "XF86DRIGetDeviceInfo";
811 if (XF86DRIGetDeviceInfo(dpy, scrn,
812 &hFB,
813 &junk,
814 &framebuffer.size,
815 &framebuffer.stride,
816 &framebuffer.dev_priv_size,
817 &framebuffer.dev_priv)) {
818 framebuffer.width = DisplayWidth(dpy, scrn);
819 framebuffer.height = DisplayHeight(dpy, scrn);
820
821 /*
822 * Map the framebuffer region.
823 */
824 status = drmMap(fd, hFB, framebuffer.size,
825 (drmAddressPtr)&framebuffer.base);
826
827 err_msg = "drmMap of framebuffer";
828 err_extra = strerror( -status );
829
830 if ( status == 0 ) {
831 /*
832 * Map the SAREA region. Further mmap regions
833 * may be setup in each DRI driver's
834 * "createNewScreen" function.
835 */
836 status = drmMap(fd, hSAREA, SAREA_MAX,
837 &pSAREA);
838
839 err_msg = "drmMap of sarea";
840 err_extra = strerror( -status );
841
842 if ( status == 0 ) {
843 __GLcontextModes * driver_modes = NULL;
844 __GLXscreenConfigs *configs = psc->screenConfigs;
845
846 err_msg = "InitDriver";
847 err_extra = NULL;
848 psp = (*createNewScreen)(dpy, scrn,
849 psc,
850 configs->configs,
851 & ddx_version,
852 & dri_version,
853 & drm_version,
854 & framebuffer,
855 pSAREA,
856 fd,
857 api_ver,
858 & interface_methods,
859 & driver_modes );
860
861 filter_modes( & configs->configs,
862 driver_modes );
863 _gl_context_modes_destroy( driver_modes );
864 }
865 }
866 }
867 }
868 }
869 }
870 }
871 }
872
873 if ( psp == NULL ) {
874 if ( pSAREA != MAP_FAILED ) {
875 (void)drmUnmap(pSAREA, SAREA_MAX);
876 }
877
878 if ( framebuffer.base != MAP_FAILED ) {
879 (void)drmUnmap((drmAddress)framebuffer.base, framebuffer.size);
880 }
881
882 if ( framebuffer.dev_priv != NULL ) {
883 Xfree(framebuffer.dev_priv);
884 }
885
886 if ( fd >= 0 ) {
887 (void)drmClose(fd);
888 }
889
890 (void)XF86DRICloseConnection(dpy, scrn);
891
892 if ( err_extra != NULL ) {
893 fprintf(stderr, "libGL error: %s failed (%s)\n", err_msg,
894 err_extra);
895 }
896 else {
897 fprintf(stderr, "libGL error: %s failed\n", err_msg );
898 }
899
900 fprintf(stderr, "libGL error: reverting to (slow) indirect rendering\n");
901 }
902 #endif /* !GLX_USE_APPLEGL */
903
904 return psp;
905 }
906 #endif /* GLX_DIRECT_RENDERING */
907
908
909 /*
910 ** Allocate the memory for the per screen configs for each screen.
911 ** If that works then fetch the per screen configs data.
912 */
913 static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
914 {
915 xGLXGetVisualConfigsReq *req;
916 xGLXGetFBConfigsReq *fb_req;
917 xGLXVendorPrivateWithReplyReq *vpreq;
918 xGLXGetFBConfigsSGIXReq *sgi_req;
919 xGLXGetVisualConfigsReply reply;
920 __GLXscreenConfigs *psc;
921 __GLcontextModes *config;
922 GLint i, j, nprops, screens;
923 INT32 buf[__GLX_TOTAL_CONFIG], *props;
924 unsigned supported_request = 0;
925 unsigned prop_size;
926
927 /*
928 ** First allocate memory for the array of per screen configs.
929 */
930 screens = ScreenCount(dpy);
931 psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
932 if (!psc) {
933 return GL_FALSE;
934 }
935 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
936 priv->screenConfigs = psc;
937
938 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
939 X_GLXQueryServerString,
940 0, GLX_VERSION);
941 if ( priv->serverGLXversion == NULL ) {
942 FreeScreenConfigs(priv);
943 return GL_FALSE;
944 }
945
946 if ( atof( priv->serverGLXversion ) >= 1.3 ) {
947 supported_request = 1;
948 }
949
950 /*
951 ** Now fetch each screens configs structures. If a screen supports
952 ** GL (by returning a numVisuals > 0) then allocate memory for our
953 ** config structure and then fill it in.
954 */
955 for (i = 0; i < screens; i++, psc++) {
956 if ( supported_request != 1 ) {
957 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
958 X_GLXQueryServerString,
959 i, GLX_EXTENSIONS);
960 if ( strstr( psc->serverGLXexts, "GLX_SGIX_fbconfig" ) != NULL ) {
961 supported_request = 2;
962 }
963 else {
964 supported_request = 3;
965 }
966 }
967
968
969 LockDisplay(dpy);
970 switch( supported_request ) {
971 case 1:
972 GetReq(GLXGetFBConfigs,fb_req);
973 fb_req->reqType = priv->majorOpcode;
974 fb_req->glxCode = X_GLXGetFBConfigs;
975 fb_req->screen = i;
976 break;
977
978 case 2:
979 GetReqExtra(GLXVendorPrivateWithReply,
980 sz_xGLXGetFBConfigsSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
981 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
982 sgi_req->reqType = priv->majorOpcode;
983 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
984 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
985 sgi_req->screen = i;
986 break;
987
988 case 3:
989 GetReq(GLXGetVisualConfigs,req);
990 req->reqType = priv->majorOpcode;
991 req->glxCode = X_GLXGetVisualConfigs;
992 req->screen = i;
993 break;
994 }
995
996 if (!_XReply(dpy, (xReply*) &reply, 0, False)) {
997 /* Something is busted. Punt. */
998 UnlockDisplay(dpy);
999 FreeScreenConfigs(priv);
1000 return GL_FALSE;
1001 }
1002
1003 UnlockDisplay(dpy);
1004 if (!reply.numVisuals) {
1005 /* This screen does not support GL rendering */
1006 UnlockDisplay(dpy);
1007 continue;
1008 }
1009
1010 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for
1011 * FIXME: FBconfigs?
1012 */
1013 /* Check number of properties */
1014 nprops = reply.numProps;
1015 if ((nprops < __GLX_MIN_CONFIG_PROPS) ||
1016 (nprops > __GLX_MAX_CONFIG_PROPS)) {
1017 /* Huh? Not in protocol defined limits. Punt */
1018 UnlockDisplay(dpy);
1019 SyncHandle();
1020 FreeScreenConfigs(priv);
1021 return GL_FALSE;
1022 }
1023
1024 /* Allocate memory for our config structure */
1025 psc->configs = _gl_context_modes_create(reply.numVisuals,
1026 sizeof(__GLcontextModes));
1027 if (!psc->configs) {
1028 UnlockDisplay(dpy);
1029 SyncHandle();
1030 FreeScreenConfigs(priv);
1031 return GL_FALSE;
1032 }
1033
1034 /* Allocate memory for the properties, if needed */
1035 if ( supported_request != 3 ) {
1036 nprops *= 2;
1037 }
1038
1039 prop_size = nprops * __GLX_SIZE_INT32;
1040
1041 if (prop_size <= sizeof(buf)) {
1042 props = buf;
1043 } else {
1044 props = (INT32 *) Xmalloc(prop_size);
1045 }
1046
1047 /* Read each config structure and convert it into our format */
1048 config = psc->configs;
1049 for (j = 0; j < reply.numVisuals; j++) {
1050 assert( config != NULL );
1051 _XRead(dpy, (char *)props, prop_size);
1052
1053 if ( supported_request != 3 ) {
1054 config->rgbMode = GL_TRUE;
1055 config->drawableType = GLX_WINDOW_BIT;
1056 }
1057 else {
1058 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
1059 }
1060
1061 __glXInitializeVisualConfigFromTags( config, nprops, props,
1062 (supported_request != 3),
1063 GL_TRUE );
1064 if ( config->fbconfigID == GLX_DONT_CARE ) {
1065 config->fbconfigID = config->visualID;
1066 }
1067 config->screen = i;
1068 config = config->next;
1069 }
1070 if (props != buf) {
1071 Xfree((char *)props);
1072 }
1073 UnlockDisplay(dpy);
1074
1075 #ifdef GLX_DIRECT_RENDERING
1076 /* Initialize per screen dynamic client GLX extensions */
1077 psc->ext_list_first_time = GL_TRUE;
1078 /* Initialize the direct rendering per screen data and functions */
1079 if (priv->driDisplay.private != NULL) {
1080 /* FIXME: Should it be some sort of an error if createNewScreen[i]
1081 * FIXME: is NULL?
1082 */
1083 if (priv->driDisplay.createNewScreen &&
1084 priv->driDisplay.createNewScreen[i]) {
1085
1086 psc->driScreen.screenConfigs = (void *)psc;
1087 psc->driScreen.private =
1088 CallCreateNewScreen(dpy, i, & psc->driScreen,
1089 & priv->driDisplay,
1090 priv->driDisplay.createNewScreen[i] );
1091 }
1092 }
1093 #endif
1094 }
1095 SyncHandle();
1096 return GL_TRUE;
1097 }
1098
1099 /*
1100 ** Initialize the client side extension code.
1101 */
1102 __GLXdisplayPrivate *__glXInitialize(Display* dpy)
1103 {
1104 XExtDisplayInfo *info = __glXFindDisplay(dpy);
1105 XExtData **privList, *private, *found;
1106 __GLXdisplayPrivate *dpyPriv;
1107 XEDataObject dataObj;
1108 int major, minor;
1109
1110 #if defined(USE_XTHREADS)
1111 {
1112 static int firstCall = 1;
1113 if (firstCall) {
1114 /* initialize the GLX mutexes */
1115 xmutex_init(&__glXmutex);
1116 firstCall = 0;
1117 }
1118 }
1119 #endif
1120
1121 INIT_MESA_SPARC
1122 /* The one and only long long lock */
1123 __glXLock();
1124
1125 if (!XextHasExtension(info)) {
1126 /* No GLX extension supported by this server. Oh well. */
1127 __glXUnlock();
1128 XMissingExtension(dpy, __glXExtensionName);
1129 return 0;
1130 }
1131
1132 /* See if a display private already exists. If so, return it */
1133 dataObj.display = dpy;
1134 privList = XEHeadOfExtensionList(dataObj);
1135 found = XFindOnExtensionList(privList, info->codes->extension);
1136 if (found) {
1137 __glXUnlock();
1138 return (__GLXdisplayPrivate *) found->private_data;
1139 }
1140
1141 /* See if the versions are compatible */
1142 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
1143 /* The client and server do not agree on versions. Punt. */
1144 __glXUnlock();
1145 return 0;
1146 }
1147
1148 /*
1149 ** Allocate memory for all the pieces needed for this buffer.
1150 */
1151 private = (XExtData *) Xmalloc(sizeof(XExtData));
1152 if (!private) {
1153 __glXUnlock();
1154 return 0;
1155 }
1156 dpyPriv = (__GLXdisplayPrivate *) Xmalloc(sizeof(__GLXdisplayPrivate));
1157 if (!dpyPriv) {
1158 __glXUnlock();
1159 Xfree((char*) private);
1160 return 0;
1161 }
1162
1163 /*
1164 ** Init the display private and then read in the screen config
1165 ** structures from the server.
1166 */
1167 dpyPriv->majorOpcode = info->codes->major_opcode;
1168 dpyPriv->majorVersion = major;
1169 dpyPriv->minorVersion = minor;
1170 dpyPriv->dpy = dpy;
1171
1172 dpyPriv->serverGLXvendor = 0x0;
1173 dpyPriv->serverGLXversion = 0x0;
1174
1175 #ifdef GLX_DIRECT_RENDERING
1176 /*
1177 ** Initialize the direct rendering per display data and functions.
1178 ** Note: This _must_ be done before calling any other DRI routines
1179 ** (e.g., those called in AllocAndFetchScreenConfigs).
1180 */
1181 if (getenv("LIBGL_ALWAYS_INDIRECT")) {
1182 /* Assinging zero here assures we'll never go direct */
1183 dpyPriv->driDisplay.private = 0;
1184 dpyPriv->driDisplay.destroyDisplay = 0;
1185 }
1186 else {
1187 dpyPriv->driDisplay.private =
1188 driCreateDisplay(dpy, &dpyPriv->driDisplay);
1189 }
1190 #endif
1191
1192 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
1193 __glXUnlock();
1194 Xfree((char*) dpyPriv);
1195 Xfree((char*) private);
1196 return 0;
1197 }
1198
1199 /*
1200 ** Fill in the private structure. This is the actual structure that
1201 ** hangs off of the Display structure. Our private structure is
1202 ** referred to by this structure. Got that?
1203 */
1204 private->number = info->codes->extension;
1205 private->next = 0;
1206 private->free_private = __glXFreeDisplayPrivate;
1207 private->private_data = (char *) dpyPriv;
1208 XAddToExtensionList(privList, private);
1209
1210 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
1211 __glXClientInfo(dpy, dpyPriv->majorOpcode);
1212 }
1213 __glXUnlock();
1214
1215 return dpyPriv;
1216 }
1217
1218 /*
1219 ** Setup for sending a GLX command on dpy. Make sure the extension is
1220 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
1221 */
1222 CARD8 __glXSetupForCommand(Display *dpy)
1223 {
1224 GLXContext gc;
1225 __GLXdisplayPrivate *priv;
1226
1227 /* If this thread has a current context, flush its rendering commands */
1228 gc = __glXGetCurrentContext();
1229 if (gc->currentDpy) {
1230 /* Flush rendering buffer of the current context, if any */
1231 (void) __glXFlushRenderBuffer(gc, gc->pc);
1232
1233 if (gc->currentDpy == dpy) {
1234 /* Use opcode from gc because its right */
1235 INIT_MESA_SPARC
1236 return gc->majorOpcode;
1237 } else {
1238 /*
1239 ** Have to get info about argument dpy because it might be to
1240 ** a different server
1241 */
1242 }
1243 }
1244
1245 /* Forced to lookup extension via the slow initialize route */
1246 priv = __glXInitialize(dpy);
1247 if (!priv) {
1248 return 0;
1249 }
1250 return priv->majorOpcode;
1251 }
1252
1253 /**
1254 * Flush the drawing command transport buffer.
1255 *
1256 * \param ctx Context whose transport buffer is to be flushed.
1257 * \param pc Pointer to first unused buffer location.
1258 *
1259 * \todo
1260 * Modify this function to use \c ctx->pc instead of the explicit
1261 * \c pc parameter.
1262 */
1263 GLubyte *__glXFlushRenderBuffer(__GLXcontext *ctx, GLubyte *pc)
1264 {
1265 Display * const dpy = ctx->currentDpy;
1266 #ifdef USE_XCB
1267 XCBConnection *c = XCBConnectionOfDisplay(dpy);
1268 #else
1269 xGLXRenderReq *req;
1270 #endif /* USE_XCB */
1271 const GLint size = pc - ctx->buf;
1272
1273 if ( (dpy != NULL) && (size > 0) ) {
1274 #ifdef USE_XCB
1275 XCBGlxRender(c, ctx->currentContextTag, size, (char *)ctx->buf);
1276 #else
1277 /* Send the entire buffer as an X request */
1278 LockDisplay(dpy);
1279 GetReq(GLXRender,req);
1280 req->reqType = ctx->majorOpcode;
1281 req->glxCode = X_GLXRender;
1282 req->contextTag = ctx->currentContextTag;
1283 req->length += (size + 3) >> 2;
1284 _XSend(dpy, (char *)ctx->buf, size);
1285 UnlockDisplay(dpy);
1286 SyncHandle();
1287 #endif
1288 }
1289
1290 /* Reset pointer and return it */
1291 ctx->pc = ctx->buf;
1292 return ctx->pc;
1293 }
1294
1295
1296 /**
1297 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1298 * this function over \c __glXSendLargeCommand is that callers can use the
1299 * data buffer in the GLX context and may be able to avoid allocating an
1300 * extra buffer. The disadvantage is the clients will have to do more
1301 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1302 *
1303 * \sa __glXSendLargeCommand
1304 *
1305 * \param gc GLX context
1306 * \param requestNumber Which part of the whole command is this? The first
1307 * request is 1.
1308 * \param totalRequests How many requests will there be?
1309 * \param data Command data.
1310 * \param dataLen Size, in bytes, of the command data.
1311 */
1312 void __glXSendLargeChunk(__GLXcontext *gc, GLint requestNumber,
1313 GLint totalRequests,
1314 const GLvoid * data, GLint dataLen)
1315 {
1316 Display *dpy = gc->currentDpy;
1317 #ifdef USE_XCB
1318 XCBConnection *c = XCBConnectionOfDisplay(dpy);
1319 XCBGlxRenderLarge(c, gc->currentContextTag, requestNumber, totalRequests, dataLen, data);
1320 #else
1321 xGLXRenderLargeReq *req;
1322
1323 if ( requestNumber == 1 ) {
1324 LockDisplay(dpy);
1325 }
1326
1327 GetReq(GLXRenderLarge,req);
1328 req->reqType = gc->majorOpcode;
1329 req->glxCode = X_GLXRenderLarge;
1330 req->contextTag = gc->currentContextTag;
1331 req->length += (dataLen + 3) >> 2;
1332 req->requestNumber = requestNumber;
1333 req->requestTotal = totalRequests;
1334 req->dataBytes = dataLen;
1335 Data(dpy, data, dataLen);
1336
1337 if ( requestNumber == totalRequests ) {
1338 UnlockDisplay(dpy);
1339 SyncHandle();
1340 }
1341 #endif /* USE_XCB */
1342 }
1343
1344
1345 /**
1346 * Send a command that is too large for the GLXRender protocol request.
1347 *
1348 * Send a large command, one that is too large for some reason to
1349 * send using the GLXRender protocol request. One reason to send
1350 * a large command is to avoid copying the data.
1351 *
1352 * \param ctx GLX context
1353 * \param header Header data.
1354 * \param headerLen Size, in bytes, of the header data. It is assumed that
1355 * the header data will always be small enough to fit in
1356 * a single X protocol packet.
1357 * \param data Command data.
1358 * \param dataLen Size, in bytes, of the command data.
1359 */
1360 void __glXSendLargeCommand(__GLXcontext *ctx,
1361 const GLvoid *header, GLint headerLen,
1362 const GLvoid *data, GLint dataLen)
1363 {
1364 GLint maxSize;
1365 GLint totalRequests, requestNumber;
1366
1367 /*
1368 ** Calculate the maximum amount of data can be stuffed into a single
1369 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1370 ** packet size minus sz_xGLXRenderReq.
1371 */
1372 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1373 totalRequests = 1 + (dataLen / maxSize);
1374 if (dataLen % maxSize) totalRequests++;
1375
1376 /*
1377 ** Send all of the command, except the large array, as one request.
1378 */
1379 assert( headerLen <= maxSize );
1380 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1381
1382 /*
1383 ** Send enough requests until the whole array is sent.
1384 */
1385 for ( requestNumber = 2 ; requestNumber <= (totalRequests - 1) ; requestNumber++ ) {
1386 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1387 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1388 dataLen -= maxSize;
1389 assert( dataLen > 0 );
1390 }
1391
1392 assert( dataLen <= maxSize );
1393 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1394 }
1395
1396 /************************************************************************/
1397
1398 GLXContext glXGetCurrentContext(void)
1399 {
1400 GLXContext cx = __glXGetCurrentContext();
1401
1402 if (cx == &dummyContext) {
1403 return NULL;
1404 } else {
1405 return cx;
1406 }
1407 }
1408
1409 GLXDrawable glXGetCurrentDrawable(void)
1410 {
1411 GLXContext gc = __glXGetCurrentContext();
1412 return gc->currentDrawable;
1413 }
1414
1415
1416 /************************************************************************/
1417
1418 #ifdef GLX_DIRECT_RENDERING
1419 /* Return the DRI per screen structure */
1420 __DRIscreen *__glXFindDRIScreen(__DRInativeDisplay *dpy, int scrn)
1421 {
1422 __DRIscreen *pDRIScreen = NULL;
1423 XExtDisplayInfo *info = __glXFindDisplay(dpy);
1424 XExtData **privList, *found;
1425 __GLXdisplayPrivate *dpyPriv;
1426 XEDataObject dataObj;
1427
1428 __glXLock();
1429 dataObj.display = dpy;
1430 privList = XEHeadOfExtensionList(dataObj);
1431 found = XFindOnExtensionList(privList, info->codes->extension);
1432 __glXUnlock();
1433
1434 if (found) {
1435 dpyPriv = (__GLXdisplayPrivate *)found->private_data;
1436 pDRIScreen = &dpyPriv->screenConfigs[scrn].driScreen;
1437 }
1438
1439 return pDRIScreen;
1440 }
1441 #endif
1442
1443 /************************************************************************/
1444
1445 static Bool SendMakeCurrentRequest( Display *dpy, CARD8 opcode,
1446 GLXContextID gc, GLXContextTag old_gc, GLXDrawable draw, GLXDrawable read,
1447 xGLXMakeCurrentReply * reply );
1448
1449 /**
1450 * Sends a GLX protocol message to the specified display to make the context
1451 * and the drawables current.
1452 *
1453 * \param dpy Display to send the message to.
1454 * \param opcode Major opcode value for the display.
1455 * \param gc_id Context tag for the context to be made current.
1456 * \param draw Drawable ID for the "draw" drawable.
1457 * \param read Drawable ID for the "read" drawable.
1458 * \param reply Space to store the X-server's reply.
1459 *
1460 * \warning
1461 * This function assumes that \c dpy is locked with \c LockDisplay on entry.
1462 */
1463 static Bool SendMakeCurrentRequest( Display *dpy, CARD8 opcode,
1464 GLXContextID gc_id, GLXContextTag gc_tag,
1465 GLXDrawable draw, GLXDrawable read,
1466 xGLXMakeCurrentReply * reply )
1467 {
1468 if ( draw == read ) {
1469 xGLXMakeCurrentReq *req;
1470
1471 GetReq(GLXMakeCurrent,req);
1472 req->reqType = opcode;
1473 req->glxCode = X_GLXMakeCurrent;
1474 req->drawable = draw;
1475 req->context = gc_id;
1476 req->oldContextTag = gc_tag;
1477 }
1478 else {
1479 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1480
1481 /* If the server can support the GLX 1.3 version, we should
1482 * perfer that. Not only that, some servers support GLX 1.3 but
1483 * not the SGI extension.
1484 */
1485
1486 if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
1487 xGLXMakeContextCurrentReq *req;
1488
1489 GetReq(GLXMakeContextCurrent,req);
1490 req->reqType = opcode;
1491 req->glxCode = X_GLXMakeContextCurrent;
1492 req->drawable = draw;
1493 req->readdrawable = read;
1494 req->context = gc_id;
1495 req->oldContextTag = gc_tag;
1496 }
1497 else {
1498 xGLXVendorPrivateWithReplyReq *vpreq;
1499 xGLXMakeCurrentReadSGIReq *req;
1500
1501 GetReqExtra(GLXVendorPrivateWithReply,
1502 sz_xGLXMakeCurrentReadSGIReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
1503 req = (xGLXMakeCurrentReadSGIReq *)vpreq;
1504 req->reqType = opcode;
1505 req->glxCode = X_GLXVendorPrivateWithReply;
1506 req->vendorCode = X_GLXvop_MakeCurrentReadSGI;
1507 req->drawable = draw;
1508 req->readable = read;
1509 req->context = gc_id;
1510 req->oldContextTag = gc_tag;
1511 }
1512 }
1513
1514 return _XReply(dpy, (xReply*) reply, 0, False);
1515 }
1516
1517
1518 #ifdef GLX_DIRECT_RENDERING
1519 static Bool BindContextWrapper( Display *dpy, GLXContext gc,
1520 GLXDrawable draw, GLXDrawable read )
1521 {
1522 return (*gc->driContext.bindContext)(dpy, gc->screen, draw, read,
1523 & gc->driContext);
1524 }
1525
1526
1527 static Bool UnbindContextWrapper( GLXContext gc )
1528 {
1529 return (*gc->driContext.unbindContext)(gc->currentDpy, gc->screen,
1530 gc->currentDrawable,
1531 gc->currentReadable,
1532 & gc->driContext );
1533 }
1534 #endif /* GLX_DIRECT_RENDERING */
1535
1536
1537 /*
1538 ** Make a particular context current.
1539 ** NOTE: this is in this file so that it can access dummyContext.
1540 */
1541 USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
1542 GLXDrawable read, GLXContext gc)
1543 {
1544 xGLXMakeCurrentReply reply;
1545 GLXContext oldGC;
1546 CARD8 opcode, oldOpcode;
1547 Bool sentRequestToOldDpy = False;
1548 Bool bindReturnValue = True;
1549
1550 opcode = __glXSetupForCommand(dpy);
1551 if (!opcode) {
1552 return GL_FALSE;
1553 }
1554
1555 /*
1556 ** Make sure that the new context has a nonzero ID. In the request,
1557 ** a zero context ID is used only to mean that we bind to no current
1558 ** context.
1559 */
1560 if ((gc != NULL) && (gc->xid == None)) {
1561 return GL_FALSE;
1562 }
1563
1564 oldGC = __glXGetCurrentContext();
1565 oldOpcode = (gc == oldGC) ? opcode : __glXSetupForCommand(dpy);
1566 if (!oldOpcode) {
1567 return GL_FALSE;
1568 }
1569
1570 if ((dpy != oldGC->currentDpy || (gc && gc->isDirect)) &&
1571 !oldGC->isDirect && oldGC != &dummyContext) {
1572 /*
1573 ** We are either switching from one dpy to another and have to
1574 ** send a request to the previous dpy to unbind the previous
1575 ** context, or we are switching away from a indirect context to
1576 ** a direct context and have to send a request to the dpy to
1577 ** unbind the previous context.
1578 */
1579 sentRequestToOldDpy = True;
1580 LockDisplay(oldGC->currentDpy);
1581 if ( ! SendMakeCurrentRequest( oldGC->currentDpy, oldOpcode, None,
1582 oldGC->currentContextTag, None, None,
1583 &reply ) ) {
1584 /* The make current failed. Just return GL_FALSE. */
1585 UnlockDisplay(oldGC->currentDpy);
1586 SyncHandle();
1587 return GL_FALSE;
1588 }
1589
1590 oldGC->currentContextTag = 0;
1591 }
1592
1593 _glapi_check_multithread();
1594
1595 #ifdef GLX_DIRECT_RENDERING
1596 /* Unbind the old direct rendering context */
1597 if (oldGC->isDirect) {
1598 if (oldGC->driContext.private) {
1599 if (! UnbindContextWrapper( oldGC )) {
1600 /* The make current failed. Just return GL_FALSE. */
1601 return GL_FALSE;
1602 }
1603 }
1604 oldGC->currentContextTag = 0;
1605 }
1606
1607 /* Bind the direct rendering context to the drawable */
1608 if (gc && gc->isDirect) {
1609 if (gc->driContext.private) {
1610 bindReturnValue = BindContextWrapper( dpy, gc, draw, read );
1611 }
1612 } else {
1613 #endif
1614 /* Send a glXMakeCurrent request to bind the new context. */
1615 LockDisplay(dpy);
1616
1617 bindReturnValue = SendMakeCurrentRequest( dpy, opcode,
1618 gc ? gc->xid : None,
1619 oldGC->currentContextTag,
1620 draw, read, &reply );
1621 UnlockDisplay(dpy);
1622 #ifdef GLX_DIRECT_RENDERING
1623 }
1624 #endif
1625
1626
1627 if (!bindReturnValue) {
1628 /* The make current failed. */
1629 if (gc && !gc->isDirect) {
1630 SyncHandle();
1631 }
1632
1633 #ifdef GLX_DIRECT_RENDERING
1634 /* If the old context was direct rendering, then re-bind to it. */
1635 if (oldGC->isDirect) {
1636 if (oldGC->driContext.private) {
1637 if (! BindContextWrapper( oldGC->currentDpy, oldGC,
1638 oldGC->currentDrawable,
1639 oldGC->currentReadable )) {
1640 /*
1641 ** The request failed; this cannot happen with the
1642 ** current API. If in the future the API is
1643 ** extended to allow context sharing between
1644 ** clients, then this may fail (because another
1645 ** client may have grabbed the context); in that
1646 ** case, we cannot undo the previous request, and
1647 ** cannot adhere to the "no-op" behavior.
1648 */
1649 }
1650 }
1651 } else
1652 #endif
1653 /*
1654 ** If we had just sent a request to a previous dpy, we have to
1655 ** undo that request (because if a command fails, it should act
1656 ** like a no-op) by making current to the previous context and
1657 ** drawable.
1658 */
1659 if (sentRequestToOldDpy) {
1660 if ( !SendMakeCurrentRequest( oldGC->currentDpy, oldOpcode,
1661 oldGC->xid, 0,
1662 oldGC->currentDrawable,
1663 oldGC->currentReadable, &reply ) ) {
1664 UnlockDisplay(oldGC->currentDpy);
1665 SyncHandle();
1666 /*
1667 ** The request failed; this cannot happen with the
1668 ** current API. If in the future the API is extended to
1669 ** allow context sharing between clients, then this may
1670 ** fail (because another client may have grabbed the
1671 ** context); in that case, we cannot undo the previous
1672 ** request, and cannot adhere to the "no-op" behavior.
1673 */
1674 }
1675 else {
1676 UnlockDisplay(oldGC->currentDpy);
1677 }
1678 oldGC->currentContextTag = reply.contextTag;
1679 }
1680 return GL_FALSE;
1681 }
1682
1683 /* Update our notion of what is current */
1684 __glXLock();
1685 if (gc == oldGC) {
1686 /*
1687 ** Even though the contexts are the same the drawable might have
1688 ** changed. Note that gc cannot be the dummy, and that oldGC
1689 ** cannot be NULL, therefore if they are the same, gc is not
1690 ** NULL and not the dummy.
1691 */
1692 gc->currentDrawable = draw;
1693 gc->currentReadable = read;
1694 } else {
1695 if (oldGC != &dummyContext) {
1696 /* Old current context is no longer current to anybody */
1697 oldGC->currentDpy = 0;
1698 oldGC->currentDrawable = None;
1699 oldGC->currentReadable = None;
1700 oldGC->currentContextTag = 0;
1701
1702 if (oldGC->xid == None) {
1703 /*
1704 ** We are switching away from a context that was
1705 ** previously destroyed, so we need to free the memory
1706 ** for the old handle.
1707 */
1708 #ifdef GLX_DIRECT_RENDERING
1709 /* Destroy the old direct rendering context */
1710 if (oldGC->isDirect) {
1711 if (oldGC->driContext.private) {
1712 (*oldGC->driContext.destroyContext)
1713 (dpy, oldGC->screen, oldGC->driContext.private);
1714 oldGC->driContext.private = NULL;
1715 }
1716 }
1717 #endif
1718 __glXFreeContext(oldGC);
1719 }
1720 }
1721 if (gc) {
1722 __glXSetCurrentContext(gc);
1723 #ifdef GLX_DIRECT_RENDERING
1724 if (!gc->isDirect) {
1725 if (!IndirectAPI)
1726 IndirectAPI = __glXNewIndirectAPI();
1727 _glapi_set_dispatch(IndirectAPI);
1728 # ifdef GLX_USE_APPLEGL
1729 do {
1730 extern void XAppleDRIUseIndirectDispatch(void);
1731 XAppleDRIUseIndirectDispatch();
1732 } while (0);
1733 # endif
1734 }
1735 #else
1736 /* if not direct rendering, always need indirect dispatch */
1737 if (!IndirectAPI)
1738 IndirectAPI = __glXNewIndirectAPI();
1739 _glapi_set_dispatch(IndirectAPI);
1740 #endif
1741 gc->currentDpy = dpy;
1742 gc->currentDrawable = draw;
1743 gc->currentReadable = read;
1744
1745 if ( ! gc->isDirect ) {
1746 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1747
1748 gc->currentContextTag = reply.contextTag;
1749 if ( state->array_state == NULL ) {
1750 (void) glGetString( GL_EXTENSIONS );
1751 (void) glGetString( GL_VERSION );
1752 __glXInitVertexArrayState(gc);
1753 }
1754 }
1755 else {
1756 gc->currentContextTag = -1;
1757 }
1758 } else {
1759 __glXSetCurrentContext(&dummyContext);
1760 #ifdef GLX_DIRECT_RENDERING
1761 _glapi_set_dispatch(NULL); /* no-op functions */
1762 #endif
1763 }
1764 }
1765 __glXUnlock();
1766 return GL_TRUE;
1767 }
1768
1769
1770 PUBLIC Bool glXMakeCurrent(Display *dpy, GLXDrawable draw, GLXContext gc)
1771 {
1772 return MakeContextCurrent( dpy, draw, draw, gc );
1773 }
1774
1775 PUBLIC GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
1776 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1777 (dpy, d, r, ctx), MakeContextCurrent)
1778
1779 PUBLIC GLX_ALIAS(Bool, glXMakeContextCurrent,
1780 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1781 (dpy, d, r, ctx), MakeContextCurrent)
1782
1783
1784 #ifdef DEBUG
1785 void __glXDumpDrawBuffer(__GLXcontext *ctx)
1786 {
1787 GLubyte *p = ctx->buf;
1788 GLubyte *end = ctx->pc;
1789 GLushort opcode, length;
1790
1791 while (p < end) {
1792 /* Fetch opcode */
1793 opcode = *((GLushort*) p);
1794 length = *((GLushort*) (p + 2));
1795 printf("%2x: %5d: ", opcode, length);
1796 length -= 4;
1797 p += 4;
1798 while (length > 0) {
1799 printf("%08x ", *((unsigned *) p));
1800 p += 4;
1801 length -= 4;
1802 }
1803 printf("\n");
1804 }
1805 }
1806 #endif
1807
1808 #ifdef USE_SPARC_ASM
1809 /*
1810 * Used only when we are sparc, using sparc assembler.
1811 *
1812 */
1813
1814 static void
1815 _glx_mesa_init_sparc_glapi_relocs(void)
1816 {
1817 unsigned int *insn_ptr, *end_ptr;
1818 unsigned long disp_addr;
1819
1820 insn_ptr = &_mesa_sparc_glapi_begin;
1821 end_ptr = &_mesa_sparc_glapi_end;
1822 disp_addr = (unsigned long) &_glapi_Dispatch;
1823
1824 /*
1825 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1826 * seem to be a better way to do this:
1827 *
1828 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1829 *
1830 * This code is patching in the ADDRESS of the pointer to the
1831 * dispatch table. Hence, it must be called exactly once, because
1832 * that address is not going to change.
1833 *
1834 * What it points to can change, but Mesa (and hence, we) assume
1835 * that there is only one pointer.
1836 *
1837 */
1838 while (insn_ptr < end_ptr) {
1839 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1840 /*
1841 This code patches for 64-bit addresses. This had better
1842 not happen for Sparc/Linux, no matter what architecture we
1843 are building for. So, don't do this.
1844
1845 The 'defined(__linux_64__)' is used here as a placeholder for
1846 when we do do 64-bit usermode on sparc linux.
1847 */
1848 insn_ptr[0] |= (disp_addr >> (32 + 10));
1849 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1850 __glapi_sparc_icache_flush(&insn_ptr[0]);
1851 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1852 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1853 __glapi_sparc_icache_flush(&insn_ptr[2]);
1854 insn_ptr += 11;
1855 #else
1856 insn_ptr[0] |= (disp_addr >> 10);
1857 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1858 __glapi_sparc_icache_flush(&insn_ptr[0]);
1859 insn_ptr += 5;
1860 #endif
1861 }
1862 }
1863 #endif /* sparc ASM in use */
1864