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