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