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