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