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