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