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