in MakeContextCurrent() use old display pointer for __glXSetupForCommand(), bug 8443
[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 <xcb/xcb.h>
69 #include <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 "GLXBadFBConfig",
293 "GLXBadPbuffer",
294 "GLXBadCurrentDrawable",
295 "GLXBadWindow",
296 };
297
298 int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
299 {
300 GLXContext gc;
301
302 gc = __glXGetCurrentContext();
303 if (dpy == gc->currentDpy) {
304 __glXSetCurrentContext(&dummyContext);
305 #ifdef GLX_DIRECT_RENDERING
306 _glapi_set_dispatch(NULL); /* no-op functions */
307 #endif
308 __glXFreeContext(gc);
309 }
310
311 return XextRemoveDisplay(__glXExtensionInfo, dpy);
312 }
313
314
315 static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
316 __GLX_NUMBER_ERRORS, error_list)
317
318 static /* const */ XExtensionHooks __glXExtensionHooks = {
319 NULL, /* create_gc */
320 NULL, /* copy_gc */
321 NULL, /* flush_gc */
322 NULL, /* free_gc */
323 NULL, /* create_font */
324 NULL, /* free_font */
325 __glXCloseDisplay, /* close_display */
326 NULL, /* wire_to_event */
327 NULL, /* event_to_wire */
328 NULL, /* error */
329 __glXErrorString, /* error_string */
330 };
331
332 static
333 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
334 __glXExtensionName, &__glXExtensionHooks,
335 __GLX_NUMBER_EVENTS, NULL)
336
337 /************************************************************************/
338
339 /*
340 ** Free the per screen configs data as well as the array of
341 ** __glXScreenConfigs.
342 */
343 static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
344 {
345 __GLXscreenConfigs *psc;
346 GLint i, screens;
347
348 /* Free screen configuration information */
349 psc = priv->screenConfigs;
350 screens = ScreenCount(priv->dpy);
351 for (i = 0; i < screens; i++, psc++) {
352 if (psc->configs) {
353 _gl_context_modes_destroy( psc->configs );
354 if(psc->effectiveGLXexts)
355 Xfree(psc->effectiveGLXexts);
356
357 psc->configs = NULL; /* NOTE: just for paranoia */
358 }
359 Xfree((char*) psc->serverGLXexts);
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 #ifdef 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 GLX_BIND_TO_TEXTURE_RGB_EXT:
600 config->bindToTextureRgb = *bp++;
601 break;
602 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
603 config->bindToTextureRgba = *bp++;
604 break;
605 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
606 config->bindToMipmapTexture = *bp++;
607 break;
608 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
609 config->bindToTextureTargets = *bp++;
610 break;
611 case GLX_Y_INVERTED_EXT:
612 config->yInverted = *bp++;
613 break;
614 case None:
615 i = count;
616 break;
617 default:
618 break;
619 }
620 }
621
622 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
623
624 config->haveAccumBuffer = ((config->accumRedBits +
625 config->accumGreenBits +
626 config->accumBlueBits +
627 config->accumAlphaBits) > 0);
628 config->haveDepthBuffer = (config->depthBits > 0);
629 config->haveStencilBuffer = (config->stencilBits > 0);
630 }
631
632
633 #ifdef GLX_DIRECT_RENDERING
634 static unsigned
635 filter_modes( __GLcontextModes ** server_modes,
636 const __GLcontextModes * driver_modes )
637 {
638 __GLcontextModes * m;
639 __GLcontextModes ** prev_next;
640 const __GLcontextModes * check;
641 unsigned modes_count = 0;
642
643 if ( driver_modes == NULL ) {
644 fprintf(stderr, "libGL warning: 3D driver returned no fbconfigs.\n");
645 return 0;
646 }
647
648 /* For each mode in server_modes, check to see if a matching mode exists
649 * in driver_modes. If not, then the mode is not available.
650 */
651
652 prev_next = server_modes;
653 for ( m = *prev_next ; m != NULL ; m = *prev_next ) {
654 GLboolean do_delete = GL_TRUE;
655
656 for ( check = driver_modes ; check != NULL ; check = check->next ) {
657 if ( _gl_context_modes_are_same( m, check ) ) {
658 do_delete = GL_FALSE;
659 break;
660 }
661 }
662
663 /* The 3D has to support all the modes that match the GLX visuals
664 * sent from the X server.
665 */
666 if ( do_delete && (m->visualID != 0) ) {
667 do_delete = GL_FALSE;
668
669 fprintf(stderr, "libGL warning: 3D driver claims to not support "
670 "visual 0x%02x\n", m->visualID);
671 }
672
673 if ( do_delete ) {
674 *prev_next = m->next;
675
676 m->next = NULL;
677 _gl_context_modes_destroy( m );
678 }
679 else {
680 modes_count++;
681 prev_next = & m->next;
682 }
683 }
684
685 return modes_count;
686 }
687
688
689 /**
690 * Implement \c __DRIinterfaceMethods::getProcAddress.
691 */
692 static __DRIfuncPtr get_proc_address( const char * proc_name )
693 {
694 if (strcmp( proc_name, "glxEnableExtension" ) == 0) {
695 return (__DRIfuncPtr) __glXScrEnableExtension;
696 }
697
698 return NULL;
699 }
700
701
702 /**
703 * Table of functions exported by the loader to the driver.
704 */
705 static const __DRIinterfaceMethods interface_methods = {
706 get_proc_address,
707
708 _gl_context_modes_create,
709 _gl_context_modes_destroy,
710
711 __glXFindDRIScreen,
712 __glXWindowExists,
713
714 XF86DRICreateContextWithConfig,
715 XF86DRIDestroyContext,
716
717 XF86DRICreateDrawable,
718 XF86DRIDestroyDrawable,
719 XF86DRIGetDrawableInfo,
720
721 __glXGetUST,
722 __glXGetMscRateOML,
723 };
724
725
726 /**
727 * Perform the required libGL-side initialization and call the client-side
728 * driver's \c __driCreateNewScreen function.
729 *
730 * \param dpy Display pointer.
731 * \param scrn Screen number on the display.
732 * \param psc DRI screen information.
733 * \param driDpy DRI display information.
734 * \param createNewScreen Pointer to the client-side driver's
735 * \c __driCreateNewScreen function.
736 * \returns A pointer to the \c __DRIscreenPrivate structure returned by
737 * the client-side driver on success, or \c NULL on failure.
738 *
739 * \todo This function needs to be modified to remove context-modes from the
740 * list stored in the \c __GLXscreenConfigsRec to match the list
741 * returned by the client-side driver.
742 */
743 static void *
744 CallCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
745 __DRIdisplay * driDpy,
746 PFNCREATENEWSCREENFUNC createNewScreen)
747 {
748 __DRIscreenPrivate *psp = NULL;
749 #ifndef GLX_USE_APPLEGL
750 drm_handle_t hSAREA;
751 drmAddress pSAREA = MAP_FAILED;
752 char *BusID;
753 __DRIversion ddx_version;
754 __DRIversion dri_version;
755 __DRIversion drm_version;
756 __DRIframebuffer framebuffer;
757 int fd = -1;
758 int status;
759 const char * err_msg;
760 const char * err_extra;
761 int api_ver = __glXGetInternalVersion();
762
763
764 dri_version.major = driDpy->private->driMajor;
765 dri_version.minor = driDpy->private->driMinor;
766 dri_version.patch = driDpy->private->driPatch;
767
768
769 err_msg = "XF86DRIOpenConnection";
770 err_extra = NULL;
771
772 framebuffer.base = MAP_FAILED;
773 framebuffer.dev_priv = NULL;
774
775 if (XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
776 fd = drmOpen(NULL,BusID);
777 Xfree(BusID); /* No longer needed */
778
779 err_msg = "open DRM";
780 err_extra = strerror( -fd );
781
782 if (fd >= 0) {
783 drm_magic_t magic;
784
785 err_msg = "drmGetMagic";
786 err_extra = NULL;
787
788 if (!drmGetMagic(fd, &magic)) {
789 drmVersionPtr version = drmGetVersion(fd);
790 if (version) {
791 drm_version.major = version->version_major;
792 drm_version.minor = version->version_minor;
793 drm_version.patch = version->version_patchlevel;
794 drmFreeVersion(version);
795 }
796 else {
797 drm_version.major = -1;
798 drm_version.minor = -1;
799 drm_version.patch = -1;
800 }
801
802 err_msg = "XF86DRIAuthConnection";
803 if (XF86DRIAuthConnection(dpy, scrn, magic)) {
804 char *driverName;
805
806 /*
807 * Get device name (like "tdfx") and the ddx version
808 * numbers. We'll check the version in each DRI driver's
809 * "createNewScreen" function.
810 */
811 err_msg = "XF86DRIGetClientDriverName";
812 if (XF86DRIGetClientDriverName(dpy, scrn,
813 &ddx_version.major,
814 &ddx_version.minor,
815 &ddx_version.patch,
816 &driverName)) {
817 drm_handle_t hFB;
818 int junk;
819
820 /* No longer needed. */
821 Xfree( driverName );
822
823
824 /*
825 * Get device-specific info. pDevPriv will point to a struct
826 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
827 * that has information about the screen size, depth, pitch,
828 * ancilliary buffers, DRM mmap handles, etc.
829 */
830 err_msg = "XF86DRIGetDeviceInfo";
831 if (XF86DRIGetDeviceInfo(dpy, scrn,
832 &hFB,
833 &junk,
834 &framebuffer.size,
835 &framebuffer.stride,
836 &framebuffer.dev_priv_size,
837 &framebuffer.dev_priv)) {
838 framebuffer.width = DisplayWidth(dpy, scrn);
839 framebuffer.height = DisplayHeight(dpy, scrn);
840
841 /*
842 * Map the framebuffer region.
843 */
844 status = drmMap(fd, hFB, framebuffer.size,
845 (drmAddressPtr)&framebuffer.base);
846
847 err_msg = "drmMap of framebuffer";
848 err_extra = strerror( -status );
849
850 if ( status == 0 ) {
851 /*
852 * Map the SAREA region. Further mmap regions
853 * may be setup in each DRI driver's
854 * "createNewScreen" function.
855 */
856 status = drmMap(fd, hSAREA, SAREA_MAX,
857 &pSAREA);
858
859 err_msg = "drmMap of sarea";
860 err_extra = strerror( -status );
861
862 if ( status == 0 ) {
863 __GLcontextModes * driver_modes = NULL;
864 __GLXscreenConfigs *configs = psc->screenConfigs;
865
866 err_msg = "InitDriver";
867 err_extra = NULL;
868 psp = (*createNewScreen)(dpy, scrn,
869 psc,
870 configs->configs,
871 & ddx_version,
872 & dri_version,
873 & drm_version,
874 & framebuffer,
875 pSAREA,
876 fd,
877 api_ver,
878 & interface_methods,
879 & driver_modes );
880
881 filter_modes( & configs->configs,
882 driver_modes );
883 _gl_context_modes_destroy( driver_modes );
884 }
885 }
886 }
887 }
888 }
889 }
890 }
891 }
892
893 if ( psp == NULL ) {
894 if ( pSAREA != MAP_FAILED ) {
895 (void)drmUnmap(pSAREA, SAREA_MAX);
896 }
897
898 if ( framebuffer.base != MAP_FAILED ) {
899 (void)drmUnmap((drmAddress)framebuffer.base, framebuffer.size);
900 }
901
902 if ( framebuffer.dev_priv != NULL ) {
903 Xfree(framebuffer.dev_priv);
904 }
905
906 if ( fd >= 0 ) {
907 (void)drmClose(fd);
908 }
909
910 (void)XF86DRICloseConnection(dpy, scrn);
911
912 if ( err_extra != NULL ) {
913 fprintf(stderr, "libGL error: %s failed (%s)\n", err_msg,
914 err_extra);
915 }
916 else {
917 fprintf(stderr, "libGL error: %s failed\n", err_msg );
918 }
919
920 fprintf(stderr, "libGL error: reverting to (slow) indirect rendering\n");
921 }
922 #endif /* !GLX_USE_APPLEGL */
923
924 return psp;
925 }
926 #endif /* GLX_DIRECT_RENDERING */
927
928
929 /*
930 ** Allocate the memory for the per screen configs for each screen.
931 ** If that works then fetch the per screen configs data.
932 */
933 static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
934 {
935 xGLXGetVisualConfigsReq *req;
936 xGLXGetFBConfigsReq *fb_req;
937 xGLXVendorPrivateWithReplyReq *vpreq;
938 xGLXGetFBConfigsSGIXReq *sgi_req;
939 xGLXGetVisualConfigsReply reply;
940 __GLXscreenConfigs *psc;
941 __GLcontextModes *config;
942 GLint i, j, nprops, screens;
943 INT32 buf[__GLX_TOTAL_CONFIG], *props;
944 unsigned supported_request = 0;
945 unsigned prop_size;
946
947 /*
948 ** First allocate memory for the array of per screen configs.
949 */
950 screens = ScreenCount(dpy);
951 psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
952 if (!psc) {
953 return GL_FALSE;
954 }
955 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
956 priv->screenConfigs = psc;
957
958 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
959 X_GLXQueryServerString,
960 0, GLX_VERSION);
961 if ( priv->serverGLXversion == NULL ) {
962 FreeScreenConfigs(priv);
963 return GL_FALSE;
964 }
965
966 if ( atof( priv->serverGLXversion ) >= 1.3 ) {
967 supported_request = 1;
968 }
969
970 /*
971 ** Now fetch each screens configs structures. If a screen supports
972 ** GL (by returning a numVisuals > 0) then allocate memory for our
973 ** config structure and then fill it in.
974 */
975 for (i = 0; i < screens; i++, psc++) {
976 if ( supported_request != 1 ) {
977 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
978 X_GLXQueryServerString,
979 i, GLX_EXTENSIONS);
980 if ( strstr( psc->serverGLXexts, "GLX_SGIX_fbconfig" ) != NULL ) {
981 supported_request = 2;
982 }
983 else {
984 supported_request = 3;
985 }
986 }
987
988
989 LockDisplay(dpy);
990 switch( supported_request ) {
991 case 1:
992 GetReq(GLXGetFBConfigs,fb_req);
993 fb_req->reqType = priv->majorOpcode;
994 fb_req->glxCode = X_GLXGetFBConfigs;
995 fb_req->screen = i;
996 break;
997
998 case 2:
999 GetReqExtra(GLXVendorPrivateWithReply,
1000 sz_xGLXGetFBConfigsSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
1001 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
1002 sgi_req->reqType = priv->majorOpcode;
1003 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
1004 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
1005 sgi_req->screen = i;
1006 break;
1007
1008 case 3:
1009 GetReq(GLXGetVisualConfigs,req);
1010 req->reqType = priv->majorOpcode;
1011 req->glxCode = X_GLXGetVisualConfigs;
1012 req->screen = i;
1013 break;
1014 }
1015
1016 if (!_XReply(dpy, (xReply*) &reply, 0, False)) {
1017 /* Something is busted. Punt. */
1018 UnlockDisplay(dpy);
1019 FreeScreenConfigs(priv);
1020 return GL_FALSE;
1021 }
1022
1023 UnlockDisplay(dpy);
1024 if (!reply.numVisuals) {
1025 /* This screen does not support GL rendering */
1026 UnlockDisplay(dpy);
1027 continue;
1028 }
1029
1030 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for
1031 * FIXME: FBconfigs?
1032 */
1033 /* Check number of properties */
1034 nprops = reply.numProps;
1035 if ((nprops < __GLX_MIN_CONFIG_PROPS) ||
1036 (nprops > __GLX_MAX_CONFIG_PROPS)) {
1037 /* Huh? Not in protocol defined limits. Punt */
1038 UnlockDisplay(dpy);
1039 SyncHandle();
1040 FreeScreenConfigs(priv);
1041 return GL_FALSE;
1042 }
1043
1044 /* Allocate memory for our config structure */
1045 psc->configs = _gl_context_modes_create(reply.numVisuals,
1046 sizeof(__GLcontextModes));
1047 if (!psc->configs) {
1048 UnlockDisplay(dpy);
1049 SyncHandle();
1050 FreeScreenConfigs(priv);
1051 return GL_FALSE;
1052 }
1053
1054 /* Allocate memory for the properties, if needed */
1055 if ( supported_request != 3 ) {
1056 nprops *= 2;
1057 }
1058
1059 prop_size = nprops * __GLX_SIZE_INT32;
1060
1061 if (prop_size <= sizeof(buf)) {
1062 props = buf;
1063 } else {
1064 props = (INT32 *) Xmalloc(prop_size);
1065 }
1066
1067 /* Read each config structure and convert it into our format */
1068 config = psc->configs;
1069 for (j = 0; j < reply.numVisuals; j++) {
1070 assert( config != NULL );
1071 _XRead(dpy, (char *)props, prop_size);
1072
1073 if ( supported_request != 3 ) {
1074 config->rgbMode = GL_TRUE;
1075 config->drawableType = GLX_WINDOW_BIT;
1076 }
1077 else {
1078 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
1079 }
1080
1081 __glXInitializeVisualConfigFromTags( config, nprops, props,
1082 (supported_request != 3),
1083 GL_TRUE );
1084 if ( config->fbconfigID == GLX_DONT_CARE ) {
1085 config->fbconfigID = config->visualID;
1086 }
1087 config->screen = i;
1088 config = config->next;
1089 }
1090 if (props != buf) {
1091 Xfree((char *)props);
1092 }
1093 UnlockDisplay(dpy);
1094
1095 #ifdef GLX_DIRECT_RENDERING
1096 /* Initialize per screen dynamic client GLX extensions */
1097 psc->ext_list_first_time = GL_TRUE;
1098 /* Initialize the direct rendering per screen data and functions */
1099 if (priv->driDisplay.private != NULL) {
1100 /* FIXME: Should it be some sort of an error if createNewScreen[i]
1101 * FIXME: is NULL?
1102 */
1103 if (priv->driDisplay.createNewScreen &&
1104 priv->driDisplay.createNewScreen[i]) {
1105
1106 psc->driScreen.screenConfigs = (void *)psc;
1107 psc->driScreen.private =
1108 CallCreateNewScreen(dpy, i, & psc->driScreen,
1109 & priv->driDisplay,
1110 priv->driDisplay.createNewScreen[i] );
1111 }
1112 }
1113 #endif
1114 }
1115 SyncHandle();
1116 return GL_TRUE;
1117 }
1118
1119 /*
1120 ** Initialize the client side extension code.
1121 */
1122 __GLXdisplayPrivate *__glXInitialize(Display* dpy)
1123 {
1124 XExtDisplayInfo *info = __glXFindDisplay(dpy);
1125 XExtData **privList, *private, *found;
1126 __GLXdisplayPrivate *dpyPriv;
1127 XEDataObject dataObj;
1128 int major, minor;
1129
1130 #if defined(USE_XTHREADS)
1131 {
1132 static int firstCall = 1;
1133 if (firstCall) {
1134 /* initialize the GLX mutexes */
1135 xmutex_init(&__glXmutex);
1136 firstCall = 0;
1137 }
1138 }
1139 #endif
1140
1141 INIT_MESA_SPARC
1142 /* The one and only long long lock */
1143 __glXLock();
1144
1145 if (!XextHasExtension(info)) {
1146 /* No GLX extension supported by this server. Oh well. */
1147 __glXUnlock();
1148 XMissingExtension(dpy, __glXExtensionName);
1149 return 0;
1150 }
1151
1152 /* See if a display private already exists. If so, return it */
1153 dataObj.display = dpy;
1154 privList = XEHeadOfExtensionList(dataObj);
1155 found = XFindOnExtensionList(privList, info->codes->extension);
1156 if (found) {
1157 __glXUnlock();
1158 return (__GLXdisplayPrivate *) found->private_data;
1159 }
1160
1161 /* See if the versions are compatible */
1162 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
1163 /* The client and server do not agree on versions. Punt. */
1164 __glXUnlock();
1165 return 0;
1166 }
1167
1168 /*
1169 ** Allocate memory for all the pieces needed for this buffer.
1170 */
1171 private = (XExtData *) Xmalloc(sizeof(XExtData));
1172 if (!private) {
1173 __glXUnlock();
1174 return 0;
1175 }
1176 dpyPriv = (__GLXdisplayPrivate *) Xmalloc(sizeof(__GLXdisplayPrivate));
1177 if (!dpyPriv) {
1178 __glXUnlock();
1179 Xfree((char*) private);
1180 return 0;
1181 }
1182
1183 /*
1184 ** Init the display private and then read in the screen config
1185 ** structures from the server.
1186 */
1187 dpyPriv->majorOpcode = info->codes->major_opcode;
1188 dpyPriv->majorVersion = major;
1189 dpyPriv->minorVersion = minor;
1190 dpyPriv->dpy = dpy;
1191
1192 dpyPriv->serverGLXvendor = 0x0;
1193 dpyPriv->serverGLXversion = 0x0;
1194
1195 #ifdef GLX_DIRECT_RENDERING
1196 /*
1197 ** Initialize the direct rendering per display data and functions.
1198 ** Note: This _must_ be done before calling any other DRI routines
1199 ** (e.g., those called in AllocAndFetchScreenConfigs).
1200 */
1201 if (getenv("LIBGL_ALWAYS_INDIRECT")) {
1202 /* Assinging zero here assures we'll never go direct */
1203 dpyPriv->driDisplay.private = 0;
1204 dpyPriv->driDisplay.destroyDisplay = 0;
1205 }
1206 else {
1207 dpyPriv->driDisplay.private =
1208 driCreateDisplay(dpy, &dpyPriv->driDisplay);
1209 }
1210 #endif
1211
1212 if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
1213 __glXUnlock();
1214 Xfree((char*) dpyPriv);
1215 Xfree((char*) private);
1216 return 0;
1217 }
1218
1219 /*
1220 ** Fill in the private structure. This is the actual structure that
1221 ** hangs off of the Display structure. Our private structure is
1222 ** referred to by this structure. Got that?
1223 */
1224 private->number = info->codes->extension;
1225 private->next = 0;
1226 private->free_private = __glXFreeDisplayPrivate;
1227 private->private_data = (char *) dpyPriv;
1228 XAddToExtensionList(privList, private);
1229
1230 if (dpyPriv->majorVersion == 1 && dpyPriv->minorVersion >= 1) {
1231 __glXClientInfo(dpy, dpyPriv->majorOpcode);
1232 }
1233 __glXUnlock();
1234
1235 return dpyPriv;
1236 }
1237
1238 /*
1239 ** Setup for sending a GLX command on dpy. Make sure the extension is
1240 ** initialized. Try to avoid calling __glXInitialize as its kinda slow.
1241 */
1242 CARD8 __glXSetupForCommand(Display *dpy)
1243 {
1244 GLXContext gc;
1245 __GLXdisplayPrivate *priv;
1246
1247 /* If this thread has a current context, flush its rendering commands */
1248 gc = __glXGetCurrentContext();
1249 if (gc->currentDpy) {
1250 /* Flush rendering buffer of the current context, if any */
1251 (void) __glXFlushRenderBuffer(gc, gc->pc);
1252
1253 if (gc->currentDpy == dpy) {
1254 /* Use opcode from gc because its right */
1255 INIT_MESA_SPARC
1256 return gc->majorOpcode;
1257 } else {
1258 /*
1259 ** Have to get info about argument dpy because it might be to
1260 ** a different server
1261 */
1262 }
1263 }
1264
1265 /* Forced to lookup extension via the slow initialize route */
1266 priv = __glXInitialize(dpy);
1267 if (!priv) {
1268 return 0;
1269 }
1270 return priv->majorOpcode;
1271 }
1272
1273 /**
1274 * Flush the drawing command transport buffer.
1275 *
1276 * \param ctx Context whose transport buffer is to be flushed.
1277 * \param pc Pointer to first unused buffer location.
1278 *
1279 * \todo
1280 * Modify this function to use \c ctx->pc instead of the explicit
1281 * \c pc parameter.
1282 */
1283 GLubyte *__glXFlushRenderBuffer(__GLXcontext *ctx, GLubyte *pc)
1284 {
1285 Display * const dpy = ctx->currentDpy;
1286 #ifdef USE_XCB
1287 xcb_connection_t *c = XGetXCBConnection(dpy);
1288 #else
1289 xGLXRenderReq *req;
1290 #endif /* USE_XCB */
1291 const GLint size = pc - ctx->buf;
1292
1293 if ( (dpy != NULL) && (size > 0) ) {
1294 #ifdef USE_XCB
1295 xcb_glx_render(c, ctx->currentContextTag, size, (char *)ctx->buf);
1296 #else
1297 /* Send the entire buffer as an X request */
1298 LockDisplay(dpy);
1299 GetReq(GLXRender,req);
1300 req->reqType = ctx->majorOpcode;
1301 req->glxCode = X_GLXRender;
1302 req->contextTag = ctx->currentContextTag;
1303 req->length += (size + 3) >> 2;
1304 _XSend(dpy, (char *)ctx->buf, size);
1305 UnlockDisplay(dpy);
1306 SyncHandle();
1307 #endif
1308 }
1309
1310 /* Reset pointer and return it */
1311 ctx->pc = ctx->buf;
1312 return ctx->pc;
1313 }
1314
1315
1316 /**
1317 * Send a portion of a GLXRenderLarge command to the server. The advantage of
1318 * this function over \c __glXSendLargeCommand is that callers can use the
1319 * data buffer in the GLX context and may be able to avoid allocating an
1320 * extra buffer. The disadvantage is the clients will have to do more
1321 * GLX protocol work (i.e., calculating \c totalRequests, etc.).
1322 *
1323 * \sa __glXSendLargeCommand
1324 *
1325 * \param gc GLX context
1326 * \param requestNumber Which part of the whole command is this? The first
1327 * request is 1.
1328 * \param totalRequests How many requests will there be?
1329 * \param data Command data.
1330 * \param dataLen Size, in bytes, of the command data.
1331 */
1332 void __glXSendLargeChunk(__GLXcontext *gc, GLint requestNumber,
1333 GLint totalRequests,
1334 const GLvoid * data, GLint dataLen)
1335 {
1336 Display *dpy = gc->currentDpy;
1337 #ifdef USE_XCB
1338 xcb_connection_t *c = XGetXCBConnection(dpy);
1339 xcb_glx_render_large(c, gc->currentContextTag, requestNumber, totalRequests, dataLen, data);
1340 #else
1341 xGLXRenderLargeReq *req;
1342
1343 if ( requestNumber == 1 ) {
1344 LockDisplay(dpy);
1345 }
1346
1347 GetReq(GLXRenderLarge,req);
1348 req->reqType = gc->majorOpcode;
1349 req->glxCode = X_GLXRenderLarge;
1350 req->contextTag = gc->currentContextTag;
1351 req->length += (dataLen + 3) >> 2;
1352 req->requestNumber = requestNumber;
1353 req->requestTotal = totalRequests;
1354 req->dataBytes = dataLen;
1355 Data(dpy, data, dataLen);
1356
1357 if ( requestNumber == totalRequests ) {
1358 UnlockDisplay(dpy);
1359 SyncHandle();
1360 }
1361 #endif /* USE_XCB */
1362 }
1363
1364
1365 /**
1366 * Send a command that is too large for the GLXRender protocol request.
1367 *
1368 * Send a large command, one that is too large for some reason to
1369 * send using the GLXRender protocol request. One reason to send
1370 * a large command is to avoid copying the data.
1371 *
1372 * \param ctx GLX context
1373 * \param header Header data.
1374 * \param headerLen Size, in bytes, of the header data. It is assumed that
1375 * the header data will always be small enough to fit in
1376 * a single X protocol packet.
1377 * \param data Command data.
1378 * \param dataLen Size, in bytes, of the command data.
1379 */
1380 void __glXSendLargeCommand(__GLXcontext *ctx,
1381 const GLvoid *header, GLint headerLen,
1382 const GLvoid *data, GLint dataLen)
1383 {
1384 GLint maxSize;
1385 GLint totalRequests, requestNumber;
1386
1387 /*
1388 ** Calculate the maximum amount of data can be stuffed into a single
1389 ** packet. sz_xGLXRenderReq is added because bufSize is the maximum
1390 ** packet size minus sz_xGLXRenderReq.
1391 */
1392 maxSize = (ctx->bufSize + sz_xGLXRenderReq) - sz_xGLXRenderLargeReq;
1393 totalRequests = 1 + (dataLen / maxSize);
1394 if (dataLen % maxSize) totalRequests++;
1395
1396 /*
1397 ** Send all of the command, except the large array, as one request.
1398 */
1399 assert( headerLen <= maxSize );
1400 __glXSendLargeChunk(ctx, 1, totalRequests, header, headerLen);
1401
1402 /*
1403 ** Send enough requests until the whole array is sent.
1404 */
1405 for ( requestNumber = 2 ; requestNumber <= (totalRequests - 1) ; requestNumber++ ) {
1406 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, maxSize);
1407 data = (const GLvoid *) (((const GLubyte *) data) + maxSize);
1408 dataLen -= maxSize;
1409 assert( dataLen > 0 );
1410 }
1411
1412 assert( dataLen <= maxSize );
1413 __glXSendLargeChunk(ctx, requestNumber, totalRequests, data, dataLen);
1414 }
1415
1416 /************************************************************************/
1417
1418 GLXContext glXGetCurrentContext(void)
1419 {
1420 GLXContext cx = __glXGetCurrentContext();
1421
1422 if (cx == &dummyContext) {
1423 return NULL;
1424 } else {
1425 return cx;
1426 }
1427 }
1428
1429 GLXDrawable glXGetCurrentDrawable(void)
1430 {
1431 GLXContext gc = __glXGetCurrentContext();
1432 return gc->currentDrawable;
1433 }
1434
1435
1436 /************************************************************************/
1437
1438 #ifdef GLX_DIRECT_RENDERING
1439 /* Return the DRI per screen structure */
1440 __DRIscreen *__glXFindDRIScreen(__DRInativeDisplay *dpy, int scrn)
1441 {
1442 __DRIscreen *pDRIScreen = NULL;
1443 XExtDisplayInfo *info = __glXFindDisplay(dpy);
1444 XExtData **privList, *found;
1445 __GLXdisplayPrivate *dpyPriv;
1446 XEDataObject dataObj;
1447
1448 __glXLock();
1449 dataObj.display = dpy;
1450 privList = XEHeadOfExtensionList(dataObj);
1451 found = XFindOnExtensionList(privList, info->codes->extension);
1452 __glXUnlock();
1453
1454 if (found) {
1455 dpyPriv = (__GLXdisplayPrivate *)found->private_data;
1456 pDRIScreen = &dpyPriv->screenConfigs[scrn].driScreen;
1457 }
1458
1459 return pDRIScreen;
1460 }
1461 #endif
1462
1463 /************************************************************************/
1464
1465 static Bool SendMakeCurrentRequest( Display *dpy, CARD8 opcode,
1466 GLXContextID gc, GLXContextTag old_gc, GLXDrawable draw, GLXDrawable read,
1467 xGLXMakeCurrentReply * reply );
1468
1469 /**
1470 * Sends a GLX protocol message to the specified display to make the context
1471 * and the drawables current.
1472 *
1473 * \param dpy Display to send the message to.
1474 * \param opcode Major opcode value for the display.
1475 * \param gc_id Context tag for the context to be made current.
1476 * \param draw Drawable ID for the "draw" drawable.
1477 * \param read Drawable ID for the "read" drawable.
1478 * \param reply Space to store the X-server's reply.
1479 *
1480 * \warning
1481 * This function assumes that \c dpy is locked with \c LockDisplay on entry.
1482 */
1483 static Bool SendMakeCurrentRequest( Display *dpy, CARD8 opcode,
1484 GLXContextID gc_id, GLXContextTag gc_tag,
1485 GLXDrawable draw, GLXDrawable read,
1486 xGLXMakeCurrentReply * reply )
1487 {
1488 if ( draw == read ) {
1489 xGLXMakeCurrentReq *req;
1490
1491 GetReq(GLXMakeCurrent,req);
1492 req->reqType = opcode;
1493 req->glxCode = X_GLXMakeCurrent;
1494 req->drawable = draw;
1495 req->context = gc_id;
1496 req->oldContextTag = gc_tag;
1497 }
1498 else {
1499 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1500
1501 /* If the server can support the GLX 1.3 version, we should
1502 * perfer that. Not only that, some servers support GLX 1.3 but
1503 * not the SGI extension.
1504 */
1505
1506 if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
1507 xGLXMakeContextCurrentReq *req;
1508
1509 GetReq(GLXMakeContextCurrent,req);
1510 req->reqType = opcode;
1511 req->glxCode = X_GLXMakeContextCurrent;
1512 req->drawable = draw;
1513 req->readdrawable = read;
1514 req->context = gc_id;
1515 req->oldContextTag = gc_tag;
1516 }
1517 else {
1518 xGLXVendorPrivateWithReplyReq *vpreq;
1519 xGLXMakeCurrentReadSGIReq *req;
1520
1521 GetReqExtra(GLXVendorPrivateWithReply,
1522 sz_xGLXMakeCurrentReadSGIReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
1523 req = (xGLXMakeCurrentReadSGIReq *)vpreq;
1524 req->reqType = opcode;
1525 req->glxCode = X_GLXVendorPrivateWithReply;
1526 req->vendorCode = X_GLXvop_MakeCurrentReadSGI;
1527 req->drawable = draw;
1528 req->readable = read;
1529 req->context = gc_id;
1530 req->oldContextTag = gc_tag;
1531 }
1532 }
1533
1534 return _XReply(dpy, (xReply*) reply, 0, False);
1535 }
1536
1537
1538 #ifdef GLX_DIRECT_RENDERING
1539 static Bool BindContextWrapper( Display *dpy, GLXContext gc,
1540 GLXDrawable draw, GLXDrawable read )
1541 {
1542 return (*gc->driContext.bindContext)(dpy, gc->screen, draw, read,
1543 & gc->driContext);
1544 }
1545
1546
1547 static Bool UnbindContextWrapper( GLXContext gc )
1548 {
1549 return (*gc->driContext.unbindContext)(gc->currentDpy, gc->screen,
1550 gc->currentDrawable,
1551 gc->currentReadable,
1552 & gc->driContext );
1553 }
1554 #endif /* GLX_DIRECT_RENDERING */
1555
1556
1557 /*
1558 ** Make a particular context current.
1559 ** NOTE: this is in this file so that it can access dummyContext.
1560 */
1561 USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
1562 GLXDrawable read, GLXContext gc)
1563 {
1564 xGLXMakeCurrentReply reply;
1565 GLXContext oldGC;
1566 CARD8 opcode, oldOpcode;
1567 Bool sentRequestToOldDpy = False;
1568 Bool bindReturnValue = True;
1569
1570 opcode = __glXSetupForCommand(dpy);
1571 if (!opcode) {
1572 return GL_FALSE;
1573 }
1574
1575 /*
1576 ** Make sure that the new context has a nonzero ID. In the request,
1577 ** a zero context ID is used only to mean that we bind to no current
1578 ** context.
1579 */
1580 if ((gc != NULL) && (gc->xid == None)) {
1581 return GL_FALSE;
1582 }
1583
1584 oldGC = __glXGetCurrentContext();
1585 oldOpcode = (gc == oldGC) ? opcode : __glXSetupForCommand(oldGC->currentDpy);
1586 if (!oldOpcode) {
1587 return GL_FALSE;
1588 }
1589
1590 if ((dpy != oldGC->currentDpy || (gc && gc->isDirect)) &&
1591 !oldGC->isDirect && oldGC != &dummyContext) {
1592 /*
1593 ** We are either switching from one dpy to another and have to
1594 ** send a request to the previous dpy to unbind the previous
1595 ** context, or we are switching away from a indirect context to
1596 ** a direct context and have to send a request to the dpy to
1597 ** unbind the previous context.
1598 */
1599 sentRequestToOldDpy = True;
1600 LockDisplay(oldGC->currentDpy);
1601 if ( ! SendMakeCurrentRequest( oldGC->currentDpy, oldOpcode, None,
1602 oldGC->currentContextTag, None, None,
1603 &reply ) ) {
1604 /* The make current failed. Just return GL_FALSE. */
1605 UnlockDisplay(oldGC->currentDpy);
1606 SyncHandle();
1607 return GL_FALSE;
1608 }
1609
1610 oldGC->currentContextTag = 0;
1611 }
1612
1613 _glapi_check_multithread();
1614
1615 #ifdef GLX_DIRECT_RENDERING
1616 /* Unbind the old direct rendering context */
1617 if (oldGC->isDirect) {
1618 if (oldGC->driContext.private) {
1619 if (! UnbindContextWrapper( oldGC )) {
1620 /* The make current failed. Just return GL_FALSE. */
1621 return GL_FALSE;
1622 }
1623 }
1624 oldGC->currentContextTag = 0;
1625 }
1626
1627 /* Bind the direct rendering context to the drawable */
1628 if (gc && gc->isDirect) {
1629 if (gc->driContext.private) {
1630 bindReturnValue = BindContextWrapper( dpy, gc, draw, read );
1631 }
1632 } else {
1633 #endif
1634 /* Send a glXMakeCurrent request to bind the new context. */
1635 LockDisplay(dpy);
1636
1637 bindReturnValue = SendMakeCurrentRequest( dpy, opcode,
1638 gc ? gc->xid : None,
1639 oldGC->currentContextTag,
1640 draw, read, &reply );
1641 UnlockDisplay(dpy);
1642 #ifdef GLX_DIRECT_RENDERING
1643 }
1644 #endif
1645
1646
1647 if (!bindReturnValue) {
1648 /* The make current failed. */
1649 if (gc && !gc->isDirect) {
1650 SyncHandle();
1651 }
1652
1653 #ifdef GLX_DIRECT_RENDERING
1654 /* If the old context was direct rendering, then re-bind to it. */
1655 if (oldGC->isDirect) {
1656 if (oldGC->driContext.private) {
1657 if (! BindContextWrapper( oldGC->currentDpy, oldGC,
1658 oldGC->currentDrawable,
1659 oldGC->currentReadable )) {
1660 /*
1661 ** The request failed; this cannot happen with the
1662 ** current API. If in the future the API is
1663 ** extended to allow context sharing between
1664 ** clients, then this may fail (because another
1665 ** client may have grabbed the context); in that
1666 ** case, we cannot undo the previous request, and
1667 ** cannot adhere to the "no-op" behavior.
1668 */
1669 }
1670 }
1671 } else
1672 #endif
1673 /*
1674 ** If we had just sent a request to a previous dpy, we have to
1675 ** undo that request (because if a command fails, it should act
1676 ** like a no-op) by making current to the previous context and
1677 ** drawable.
1678 */
1679 if (sentRequestToOldDpy) {
1680 if ( !SendMakeCurrentRequest( oldGC->currentDpy, oldOpcode,
1681 oldGC->xid, 0,
1682 oldGC->currentDrawable,
1683 oldGC->currentReadable, &reply ) ) {
1684 UnlockDisplay(oldGC->currentDpy);
1685 SyncHandle();
1686 /*
1687 ** The request failed; this cannot happen with the
1688 ** current API. If in the future the API is extended to
1689 ** allow context sharing between clients, then this may
1690 ** fail (because another client may have grabbed the
1691 ** context); in that case, we cannot undo the previous
1692 ** request, and cannot adhere to the "no-op" behavior.
1693 */
1694 }
1695 else {
1696 UnlockDisplay(oldGC->currentDpy);
1697 }
1698 oldGC->currentContextTag = reply.contextTag;
1699 }
1700 return GL_FALSE;
1701 }
1702
1703 /* Update our notion of what is current */
1704 __glXLock();
1705 if (gc == oldGC) {
1706 /*
1707 ** Even though the contexts are the same the drawable might have
1708 ** changed. Note that gc cannot be the dummy, and that oldGC
1709 ** cannot be NULL, therefore if they are the same, gc is not
1710 ** NULL and not the dummy.
1711 */
1712 gc->currentDrawable = draw;
1713 gc->currentReadable = read;
1714 } else {
1715 if (oldGC != &dummyContext) {
1716 /* Old current context is no longer current to anybody */
1717 oldGC->currentDpy = 0;
1718 oldGC->currentDrawable = None;
1719 oldGC->currentReadable = None;
1720 oldGC->currentContextTag = 0;
1721
1722 if (oldGC->xid == None) {
1723 /*
1724 ** We are switching away from a context that was
1725 ** previously destroyed, so we need to free the memory
1726 ** for the old handle.
1727 */
1728 #ifdef GLX_DIRECT_RENDERING
1729 /* Destroy the old direct rendering context */
1730 if (oldGC->isDirect) {
1731 if (oldGC->driContext.private) {
1732 (*oldGC->driContext.destroyContext)
1733 (dpy, oldGC->screen, oldGC->driContext.private);
1734 oldGC->driContext.private = NULL;
1735 }
1736 }
1737 #endif
1738 __glXFreeContext(oldGC);
1739 }
1740 }
1741 if (gc) {
1742 __glXSetCurrentContext(gc);
1743 #ifdef GLX_DIRECT_RENDERING
1744 if (!gc->isDirect) {
1745 if (!IndirectAPI)
1746 IndirectAPI = __glXNewIndirectAPI();
1747 _glapi_set_dispatch(IndirectAPI);
1748 # ifdef GLX_USE_APPLEGL
1749 do {
1750 extern void XAppleDRIUseIndirectDispatch(void);
1751 XAppleDRIUseIndirectDispatch();
1752 } while (0);
1753 # endif
1754 }
1755 #else
1756 /* if not direct rendering, always need indirect dispatch */
1757 if (!IndirectAPI)
1758 IndirectAPI = __glXNewIndirectAPI();
1759 _glapi_set_dispatch(IndirectAPI);
1760 #endif
1761 gc->currentDpy = dpy;
1762 gc->currentDrawable = draw;
1763 gc->currentReadable = read;
1764
1765 if ( ! gc->isDirect ) {
1766 __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
1767
1768 gc->currentContextTag = reply.contextTag;
1769 if ( state->array_state == NULL ) {
1770 (void) glGetString( GL_EXTENSIONS );
1771 (void) glGetString( GL_VERSION );
1772 __glXInitVertexArrayState(gc);
1773 }
1774 }
1775 else {
1776 gc->currentContextTag = -1;
1777 }
1778 } else {
1779 __glXSetCurrentContext(&dummyContext);
1780 #ifdef GLX_DIRECT_RENDERING
1781 _glapi_set_dispatch(NULL); /* no-op functions */
1782 #endif
1783 }
1784 }
1785 __glXUnlock();
1786 return GL_TRUE;
1787 }
1788
1789
1790 PUBLIC Bool glXMakeCurrent(Display *dpy, GLXDrawable draw, GLXContext gc)
1791 {
1792 return MakeContextCurrent( dpy, draw, draw, gc );
1793 }
1794
1795 PUBLIC GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
1796 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1797 (dpy, d, r, ctx), MakeContextCurrent)
1798
1799 PUBLIC GLX_ALIAS(Bool, glXMakeContextCurrent,
1800 (Display *dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
1801 (dpy, d, r, ctx), MakeContextCurrent)
1802
1803
1804 #ifdef DEBUG
1805 void __glXDumpDrawBuffer(__GLXcontext *ctx)
1806 {
1807 GLubyte *p = ctx->buf;
1808 GLubyte *end = ctx->pc;
1809 GLushort opcode, length;
1810
1811 while (p < end) {
1812 /* Fetch opcode */
1813 opcode = *((GLushort*) p);
1814 length = *((GLushort*) (p + 2));
1815 printf("%2x: %5d: ", opcode, length);
1816 length -= 4;
1817 p += 4;
1818 while (length > 0) {
1819 printf("%08x ", *((unsigned *) p));
1820 p += 4;
1821 length -= 4;
1822 }
1823 printf("\n");
1824 }
1825 }
1826 #endif
1827
1828 #ifdef USE_SPARC_ASM
1829 /*
1830 * Used only when we are sparc, using sparc assembler.
1831 *
1832 */
1833
1834 static void
1835 _glx_mesa_init_sparc_glapi_relocs(void)
1836 {
1837 unsigned int *insn_ptr, *end_ptr;
1838 unsigned long disp_addr;
1839
1840 insn_ptr = &_mesa_sparc_glapi_begin;
1841 end_ptr = &_mesa_sparc_glapi_end;
1842 disp_addr = (unsigned long) &_glapi_Dispatch;
1843
1844 /*
1845 * Verbatim from Mesa sparc.c. It's needed because there doesn't
1846 * seem to be a better way to do this:
1847 *
1848 * UNCONDITIONAL_JUMP ( (*_glapi_Dispatch) + entry_offset )
1849 *
1850 * This code is patching in the ADDRESS of the pointer to the
1851 * dispatch table. Hence, it must be called exactly once, because
1852 * that address is not going to change.
1853 *
1854 * What it points to can change, but Mesa (and hence, we) assume
1855 * that there is only one pointer.
1856 *
1857 */
1858 while (insn_ptr < end_ptr) {
1859 #if ( defined(__sparc_v9__) && ( !defined(__linux__) || defined(__linux_64__) ) )
1860 /*
1861 This code patches for 64-bit addresses. This had better
1862 not happen for Sparc/Linux, no matter what architecture we
1863 are building for. So, don't do this.
1864
1865 The 'defined(__linux_64__)' is used here as a placeholder for
1866 when we do do 64-bit usermode on sparc linux.
1867 */
1868 insn_ptr[0] |= (disp_addr >> (32 + 10));
1869 insn_ptr[1] |= ((disp_addr & 0xffffffff) >> 10);
1870 __glapi_sparc_icache_flush(&insn_ptr[0]);
1871 insn_ptr[2] |= ((disp_addr >> 32) & ((1 << 10) - 1));
1872 insn_ptr[3] |= (disp_addr & ((1 << 10) - 1));
1873 __glapi_sparc_icache_flush(&insn_ptr[2]);
1874 insn_ptr += 11;
1875 #else
1876 insn_ptr[0] |= (disp_addr >> 10);
1877 insn_ptr[1] |= (disp_addr & ((1 << 10) - 1));
1878 __glapi_sparc_icache_flush(&insn_ptr[0]);
1879 insn_ptr += 5;
1880 #endif
1881 }
1882 }
1883 #endif /* sparc ASM in use */
1884