Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / glx / x11 / glxext.c
1
2 /*
3 ** License Applicability. Except to the extent portions of this file are
4 ** made subject to an alternative license as permitted in the SGI Free
5 ** Software License B, Version 1.1 (the "License"), the contents of this
6 ** file are subject only to the provisions of the License. You may not use
7 ** this file except in compliance with the License. You may obtain a copy
8 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
9 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
10 **
11 ** http://oss.sgi.com/projects/FreeB
12 **
13 ** Note that, as provided in the License, the Software is distributed on an
14 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
15 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
16 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
17 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
18 **
19 ** Original Code. The Original Code is: OpenGL Sample Implementation,
20 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
21 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
22 ** Copyright in any portions created by third parties is as indicated
23 ** elsewhere herein. All Rights Reserved.
24 **
25 ** Additional Notice Provisions: The application programming interfaces
26 ** established by SGI in conjunction with the Original Code are The
27 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
28 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
29 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
30 ** Window System(R) (Version 1.3), released October 19, 1998. This software
31 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
32 ** published by SGI, but has not been independently verified as being
33 ** compliant with the OpenGL(R) version 1.2.1 Specification.
34 **
35 */
36
37 /**
38 * \file glxext.c
39 * GLX protocol interface boot-strap code.
40 *
41 * Direct rendering support added by Precision Insight, Inc.
42 *
43 * \author Kevin E. Martin <kevin@precisioninsight.com>
44 */
45
46 #include "glxclient.h"
47 #include <stdio.h>
48 #include <X11/extensions/Xext.h>
49 #include <X11/extensions/extutil.h>
50 #include <X11/extensions/Xfixes.h>
51 #include <X11/extensions/Xdamage.h>
52 #include <assert.h>
53 #include "indirect_init.h"
54 #include "glapi.h"
55 #include "glxextensions.h"
56 #include "glcontextmodes.h"
57 #include "glheader.h"
58
59 #ifdef GLX_DIRECT_RENDERING
60 #include <inttypes.h>
61 #include <sys/mman.h>
62 #include "xf86dri.h"
63 #include "sarea.h"
64 #include "dri_glx.h"
65 #endif
66
67 #ifdef USE_XCB
68 #include <X11/Xlib-xcb.h>
69 #include <xcb/xcb.h>
70 #include <xcb/glx.h>
71 #endif
72
73 #include <assert.h>
74
75 #ifdef DEBUG
76 void __glXDumpDrawBuffer(__GLXcontext *ctx);
77 #endif
78
79 #ifdef USE_SPARC_ASM
80 /*
81 * This is where our dispatch table's bounds are.
82 * And the static mesa_init is taken directly from
83 * Mesa's 'sparc.c' initializer.
84 *
85 * We need something like this here, because this version
86 * of openGL/glx never initializes a Mesa context, and so
87 * the address of the dispatch table pointer never gets stuffed
88 * into the dispatch jump table otherwise.
89 *
90 * It matters only on SPARC, and only if you are using assembler
91 * code instead of C-code indirect dispatch.
92 *
93 * -- FEM, 04.xii.03
94 */
95 extern unsigned int _mesa_sparc_glapi_begin;
96 extern unsigned int _mesa_sparc_glapi_end;
97 extern void __glapi_sparc_icache_flush(unsigned int *);
98 static void _glx_mesa_init_sparc_glapi_relocs(void);
99 static int _mesa_sparc_needs_init = 1;
100 #define INIT_MESA_SPARC { \
101 if(_mesa_sparc_needs_init) { \
102 _glx_mesa_init_sparc_glapi_relocs(); \
103 _mesa_sparc_needs_init = 0; \
104 } \
105 }
106 #else
107 #define INIT_MESA_SPARC
108 #endif
109
110 #ifdef GLX_DIRECT_RENDERING
111 static __DRIscreen *__glXFindDRIScreen(__DRInativeDisplay *dpy, int scrn);
112 #endif /* GLX_DIRECT_RENDERING */
113
114 static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
115 GLXDrawable read, GLXContext gc);
116
117 /*
118 ** We setup some dummy structures here so that the API can be used
119 ** even if no context is current.
120 */
121
122 static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
123
124 /*
125 ** Dummy context used by small commands when there is no current context.
126 ** All the
127 ** gl and glx entry points are designed to operate as nop's when using
128 ** the dummy context structure.
129 */
130 static __GLXcontext dummyContext = {
131 &dummyBuffer[0],
132 &dummyBuffer[0],
133 &dummyBuffer[0],
134 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
135 sizeof(dummyBuffer),
136 };
137
138
139 /*
140 ** All indirect rendering contexts will share the same indirect dispatch table.
141 */
142 static __GLapi *IndirectAPI = NULL;
143
144
145 /*
146 * Current context management and locking
147 */
148
149 #if defined( USE_XTHREADS )
150
151 /* thread safe */
152 static GLboolean TSDinitialized = GL_FALSE;
153 static xthread_key_t ContextTSD;
154
155 __GLXcontext *__glXGetCurrentContext(void)
156 {
157 if (!TSDinitialized) {
158 xthread_key_create(&ContextTSD, NULL);
159 TSDinitialized = GL_TRUE;
160 return &dummyContext;
161 }
162 else {
163 void *p;
164 xthread_get_specific(ContextTSD, &p);
165 if (!p)
166 return &dummyContext;
167 else
168 return (__GLXcontext *) p;
169 }
170 }
171
172 void __glXSetCurrentContext(__GLXcontext *c)
173 {
174 if (!TSDinitialized) {
175 xthread_key_create(&ContextTSD, NULL);
176 TSDinitialized = GL_TRUE;
177 }
178 xthread_set_specific(ContextTSD, c);
179 }
180
181
182 /* Used by the __glXLock() and __glXUnlock() macros */
183 xmutex_rec __glXmutex;
184
185 #elif defined( PTHREADS )
186
187 pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
188
189 # if defined( GLX_USE_TLS )
190
191 /**
192 * Per-thread GLX context pointer.
193 *
194 * \c __glXSetCurrentContext is written is such a way that this pointer can
195 * \b never be \c NULL. This is important! Because of this
196 * \c __glXGetCurrentContext can be implemented as trivial macro.
197 */
198 __thread void * __glX_tls_Context __attribute__((tls_model("initial-exec")))
199 = &dummyContext;
200
201 void __glXSetCurrentContext( __GLXcontext * c )
202 {
203 __glX_tls_Context = (c != NULL) ? c : &dummyContext;
204 }
205
206 # else
207
208 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
209
210 /**
211 * Per-thread data key.
212 *
213 * Once \c init_thread_data has been called, the per-thread data key will
214 * take a value of \c NULL. As each new thread is created the default
215 * value, in that thread, will be \c NULL.
216 */
217 static pthread_key_t ContextTSD;
218
219 /**
220 * Initialize the per-thread data key.
221 *
222 * This function is called \b exactly once per-process (not per-thread!) to
223 * initialize the per-thread data key. This is ideally done using the
224 * \c pthread_once mechanism.
225 */
226 static void init_thread_data( void )
227 {
228 if ( pthread_key_create( & ContextTSD, NULL ) != 0 ) {
229 perror( "pthread_key_create" );
230 exit( -1 );
231 }
232 }
233
234 void __glXSetCurrentContext( __GLXcontext * c )
235 {
236 pthread_once( & once_control, init_thread_data );
237 pthread_setspecific( ContextTSD, c );
238 }
239
240 __GLXcontext * __glXGetCurrentContext( void )
241 {
242 void * v;
243
244 pthread_once( & once_control, init_thread_data );
245
246 v = pthread_getspecific( ContextTSD );
247 return (v == NULL) ? & dummyContext : (__GLXcontext *) v;
248 }
249
250 # endif /* defined( GLX_USE_TLS ) */
251
252 #elif defined( THREADS )
253
254 #error Unknown threading method specified.
255
256 #else
257
258 /* not thread safe */
259 __GLXcontext *__glXcurrentContext = &dummyContext;
260
261 #endif
262
263
264 /*
265 ** You can set this cell to 1 to force the gl drawing stuff to be
266 ** one command per packet
267 */
268 int __glXDebug = 0;
269
270 /*
271 ** forward prototype declarations
272 */
273 int __glXCloseDisplay(Display *dpy, XExtCodes *codes);
274
275
276 /************************************************************************/
277
278 /* Extension required boiler plate */
279
280 static char *__glXExtensionName = GLX_EXTENSION_NAME;
281 XExtensionInfo *__glXExtensionInfo = NULL;
282
283 static /* const */ char *error_list[] = {
284 "GLXBadContext",
285 "GLXBadContextState",
286 "GLXBadDrawable",
287 "GLXBadPixmap",
288 "GLXBadContextTag",
289 "GLXBadCurrentWindow",
290 "GLXBadRenderRequest",
291 "GLXBadLargeRequest",
292 "GLXUnsupportedPrivateRequest",
293 "GLXBadFBConfig",
294 "GLXBadPbuffer",
295 "GLXBadCurrentDrawable",
296 "GLXBadWindow",
297 };
298
299 int __glXCloseDisplay(Display *dpy, XExtCodes *codes)
300 {
301 GLXContext gc;
302
303 gc = __glXGetCurrentContext();
304 if (dpy == gc->currentDpy) {
305 __glXSetCurrentContext(&dummyContext);
306 #ifdef GLX_DIRECT_RENDERING
307 _glapi_set_dispatch(NULL); /* no-op functions */
308 #endif
309 __glXFreeContext(gc);
310 }
311
312 return XextRemoveDisplay(__glXExtensionInfo, dpy);
313 }
314
315
316 static XEXT_GENERATE_ERROR_STRING(__glXErrorString, __glXExtensionName,
317 __GLX_NUMBER_ERRORS, error_list)
318
319 static /* const */ XExtensionHooks __glXExtensionHooks = {
320 NULL, /* create_gc */
321 NULL, /* copy_gc */
322 NULL, /* flush_gc */
323 NULL, /* free_gc */
324 NULL, /* create_font */
325 NULL, /* free_font */
326 __glXCloseDisplay, /* close_display */
327 NULL, /* wire_to_event */
328 NULL, /* event_to_wire */
329 NULL, /* error */
330 __glXErrorString, /* error_string */
331 };
332
333 static
334 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
335 __glXExtensionName, &__glXExtensionHooks,
336 __GLX_NUMBER_EVENTS, NULL)
337
338 /************************************************************************/
339
340 /*
341 ** Free the per screen configs data as well as the array of
342 ** __glXScreenConfigs.
343 */
344 static void FreeScreenConfigs(__GLXdisplayPrivate *priv)
345 {
346 __GLXscreenConfigs *psc;
347 GLint i, screens;
348
349 /* Free screen configuration information */
350 psc = priv->screenConfigs;
351 screens = ScreenCount(priv->dpy);
352 for (i = 0; i < screens; i++, psc++) {
353 if (psc->configs) {
354 _gl_context_modes_destroy( psc->configs );
355 if(psc->effectiveGLXexts)
356 Xfree(psc->effectiveGLXexts);
357
358 psc->configs = NULL; /* NOTE: just for paranoia */
359 }
360 Xfree((char*) psc->serverGLXexts);
361
362 #ifdef GLX_DIRECT_RENDERING
363 /* Free the direct rendering per screen data */
364 if (psc->driScreen.private)
365 (*psc->driScreen.destroyScreen)(priv->dpy, i,
366 psc->driScreen.private);
367 psc->driScreen.private = NULL;
368 #endif
369 }
370 XFree((char*) priv->screenConfigs);
371 }
372
373 /*
374 ** Release the private memory referred to in a display private
375 ** structure. The caller will free the extension structure.
376 */
377 static int __glXFreeDisplayPrivate(XExtData *extension)
378 {
379 __GLXdisplayPrivate *priv;
380
381 priv = (__GLXdisplayPrivate*) extension->private_data;
382 FreeScreenConfigs(priv);
383 if(priv->serverGLXvendor) {
384 Xfree((char*)priv->serverGLXvendor);
385 priv->serverGLXvendor = 0x0; /* to protect against double free's */
386 }
387 if(priv->serverGLXversion) {
388 Xfree((char*)priv->serverGLXversion);
389 priv->serverGLXversion = 0x0; /* to protect against double free's */
390 }
391
392 #ifdef GLX_DIRECT_RENDERING
393 /* Free the direct rendering per display data */
394 if (priv->driDisplay.private)
395 (*priv->driDisplay.destroyDisplay)(priv->dpy,
396 priv->driDisplay.private);
397 priv->driDisplay.private = NULL;
398 if (priv->driDisplay.createNewScreen) {
399 Xfree(priv->driDisplay.createNewScreen); /* free array of ptrs */
400 priv->driDisplay.createNewScreen = NULL;
401 }
402 #endif
403
404 Xfree((char*) priv);
405 return 0;
406 }
407
408 /************************************************************************/
409
410 /*
411 ** Query the version of the GLX extension. This procedure works even if
412 ** the client extension is not completely set up.
413 */
414 static Bool QueryVersion(Display *dpy, int opcode, int *major, int *minor)
415 {
416 xGLXQueryVersionReq *req;
417 xGLXQueryVersionReply reply;
418
419 /* Send the glXQueryVersion request */
420 LockDisplay(dpy);
421 GetReq(GLXQueryVersion,req);
422 req->reqType = opcode;
423 req->glxCode = X_GLXQueryVersion;
424 req->majorVersion = GLX_MAJOR_VERSION;
425 req->minorVersion = GLX_MINOR_VERSION;
426 _XReply(dpy, (xReply*) &reply, 0, False);
427 UnlockDisplay(dpy);
428 SyncHandle();
429
430 if (reply.majorVersion != GLX_MAJOR_VERSION) {
431 /*
432 ** The server does not support the same major release as this
433 ** client.
434 */
435 return GL_FALSE;
436 }
437 *major = reply.majorVersion;
438 *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
439 return GL_TRUE;
440 }
441
442
443 void
444 __glXInitializeVisualConfigFromTags( __GLcontextModes *config, int count,
445 const INT32 *bp, Bool tagged_only,
446 Bool fbconfig_style_tags )
447 {
448 int i;
449
450 if (!tagged_only) {
451 /* Copy in the first set of properties */
452 config->visualID = *bp++;
453
454 config->visualType = _gl_convert_from_x_visual_type( *bp++ );
455
456 config->rgbMode = *bp++;
457
458 config->redBits = *bp++;
459 config->greenBits = *bp++;
460 config->blueBits = *bp++;
461 config->alphaBits = *bp++;
462 config->accumRedBits = *bp++;
463 config->accumGreenBits = *bp++;
464 config->accumBlueBits = *bp++;
465 config->accumAlphaBits = *bp++;
466
467 config->doubleBufferMode = *bp++;
468 config->stereoMode = *bp++;
469
470 config->rgbBits = *bp++;
471 config->depthBits = *bp++;
472 config->stencilBits = *bp++;
473 config->numAuxBuffers = *bp++;
474 config->level = *bp++;
475
476 count -= __GLX_MIN_CONFIG_PROPS;
477 }
478
479 /*
480 ** Additional properties may be in a list at the end
481 ** of the reply. They are in pairs of property type
482 ** and property value.
483 */
484
485 #define FETCH_OR_SET(tag) \
486 config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
487
488 for (i = 0; i < count; i += 2 ) {
489 switch(*bp++) {
490 case GLX_RGBA:
491 FETCH_OR_SET( rgbMode );
492 break;
493 case GLX_BUFFER_SIZE:
494 config->rgbBits = *bp++;
495 break;
496 case GLX_LEVEL:
497 config->level = *bp++;
498 break;
499 case GLX_DOUBLEBUFFER:
500 FETCH_OR_SET( doubleBufferMode );
501 break;
502 case GLX_STEREO:
503 FETCH_OR_SET( stereoMode );
504 break;
505 case GLX_AUX_BUFFERS:
506 config->numAuxBuffers = *bp++;
507 break;
508 case GLX_RED_SIZE:
509 config->redBits = *bp++;
510 break;
511 case GLX_GREEN_SIZE:
512 config->greenBits = *bp++;
513 break;
514 case GLX_BLUE_SIZE:
515 config->blueBits = *bp++;
516 break;
517 case GLX_ALPHA_SIZE:
518 config->alphaBits = *bp++;
519 break;
520 case GLX_DEPTH_SIZE:
521 config->depthBits = *bp++;
522 break;
523 case GLX_STENCIL_SIZE:
524 config->stencilBits = *bp++;
525 break;
526 case GLX_ACCUM_RED_SIZE:
527 config->accumRedBits = *bp++;
528 break;
529 case GLX_ACCUM_GREEN_SIZE:
530 config->accumGreenBits = *bp++;
531 break;
532 case GLX_ACCUM_BLUE_SIZE:
533 config->accumBlueBits = *bp++;
534 break;
535 case GLX_ACCUM_ALPHA_SIZE:
536 config->accumAlphaBits = *bp++;
537 break;
538 case GLX_VISUAL_CAVEAT_EXT:
539 config->visualRating = *bp++;
540 break;
541 case GLX_X_VISUAL_TYPE:
542 config->visualType = *bp++;
543 break;
544 case GLX_TRANSPARENT_TYPE:
545 config->transparentPixel = *bp++;
546 break;
547 case GLX_TRANSPARENT_INDEX_VALUE:
548 config->transparentIndex = *bp++;
549 break;
550 case GLX_TRANSPARENT_RED_VALUE:
551 config->transparentRed = *bp++;
552 break;
553 case GLX_TRANSPARENT_GREEN_VALUE:
554 config->transparentGreen = *bp++;
555 break;
556 case GLX_TRANSPARENT_BLUE_VALUE:
557 config->transparentBlue = *bp++;
558 break;
559 case GLX_TRANSPARENT_ALPHA_VALUE:
560 config->transparentAlpha = *bp++;
561 break;
562 case GLX_VISUAL_ID:
563 config->visualID = *bp++;
564 break;
565 case GLX_DRAWABLE_TYPE:
566 config->drawableType = *bp++;
567 break;
568 case GLX_RENDER_TYPE:
569 config->renderType = *bp++;
570 break;
571 case GLX_X_RENDERABLE:
572 config->xRenderable = *bp++;
573 break;
574 case GLX_FBCONFIG_ID:
575 config->fbconfigID = *bp++;
576 break;
577 case GLX_MAX_PBUFFER_WIDTH:
578 config->maxPbufferWidth = *bp++;
579 break;
580 case GLX_MAX_PBUFFER_HEIGHT:
581 config->maxPbufferHeight = *bp++;
582 break;
583 case GLX_MAX_PBUFFER_PIXELS:
584 config->maxPbufferPixels = *bp++;
585 break;
586 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
587 config->optimalPbufferWidth = *bp++;
588 break;
589 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
590 config->optimalPbufferHeight = *bp++;
591 break;
592 case GLX_VISUAL_SELECT_GROUP_SGIX:
593 config->visualSelectGroup = *bp++;
594 break;
595 case GLX_SWAP_METHOD_OML:
596 config->swapMethod = *bp++;
597 break;
598 case GLX_SAMPLE_BUFFERS_SGIS:
599 config->sampleBuffers = *bp++;
600 break;
601 case GLX_SAMPLES_SGIS:
602 config->samples = *bp++;
603 break;
604 case GLX_BIND_TO_TEXTURE_RGB_EXT:
605 config->bindToTextureRgb = *bp++;
606 break;
607 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
608 config->bindToTextureRgba = *bp++;
609 break;
610 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
611 config->bindToMipmapTexture = *bp++;
612 break;
613 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
614 config->bindToTextureTargets = *bp++;
615 break;
616 case GLX_Y_INVERTED_EXT:
617 config->yInverted = *bp++;
618 break;
619 case None:
620 i = count;
621 break;
622 default:
623 break;
624 }
625 }
626
627 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
628
629 config->haveAccumBuffer = ((config->accumRedBits +
630 config->accumGreenBits +
631 config->accumBlueBits +
632 config->accumAlphaBits) > 0);
633 config->haveDepthBuffer = (config->depthBits > 0);
634 config->haveStencilBuffer = (config->stencilBits > 0);
635 }
636
637
638 #ifdef GLX_DIRECT_RENDERING
639 static unsigned
640 filter_modes( __GLcontextModes ** server_modes,
641 const __GLcontextModes * driver_modes )
642 {
643 __GLcontextModes * m;
644 __GLcontextModes ** prev_next;
645 const __GLcontextModes * check;
646 unsigned modes_count = 0;
647
648 if ( driver_modes == NULL ) {
649 fprintf(stderr, "libGL warning: 3D driver returned no fbconfigs.\n");
650 return 0;
651 }
652
653 /* For each mode in server_modes, check to see if a matching mode exists
654 * in driver_modes. If not, then the mode is not available.
655 */
656
657 prev_next = server_modes;
658 for ( m = *prev_next ; m != NULL ; m = *prev_next ) {
659 GLboolean do_delete = GL_TRUE;
660
661 for ( check = driver_modes ; check != NULL ; check = check->next ) {
662 if ( _gl_context_modes_are_same( m, check ) ) {
663 do_delete = GL_FALSE;
664 break;
665 }
666 }
667
668 /* The 3D has to support all the modes that match the GLX visuals
669 * sent from the X server.
670 */
671 if ( do_delete && (m->visualID != 0) ) {
672 do_delete = GL_FALSE;
673
674 if (getenv("LIBGL_DEBUG")) {
675 fprintf(stderr, "libGL warning: 3D driver claims to not support "
676 "visual 0x%02x\n", m->visualID);
677 }
678 }
679
680 if ( do_delete ) {
681 *prev_next = m->next;
682
683 m->next = NULL;
684 _gl_context_modes_destroy( m );
685 }
686 else {
687 modes_count++;
688 prev_next = & m->next;
689 }
690 }
691
692 return modes_count;
693 }
694
695
696 /**
697 * Implement \c __DRIinterfaceMethods::getProcAddress.
698 */
699 static __DRIfuncPtr get_proc_address( const char * proc_name )
700 {
701 if (strcmp( proc_name, "glxEnableExtension" ) == 0) {
702 return (__DRIfuncPtr) __glXScrEnableExtension;
703 }
704
705 return NULL;
706 }
707
708 #ifdef XDAMAGE_1_1_INTERFACE
709 static GLboolean has_damage_post(__DRInativeDisplay *dpy)
710 {
711 static GLboolean inited = GL_FALSE;
712 static GLboolean has_damage;
713
714 if (!inited) {
715 int major, minor;
716
717 if (XDamageQueryVersion(dpy, &major, &minor) &&
718 major == 1 && minor >= 1)
719 {
720 has_damage = GL_TRUE;
721 } else {
722 has_damage = GL_FALSE;
723 }
724 inited = GL_TRUE;
725 }
726
727 return has_damage;
728 }
729 #endif /* XDAMAGE_1_1_INTERFACE */
730
731 static void __glXReportDamage(__DRInativeDisplay *dpy, int screen,
732 __DRIid drawable,
733 int x, int y,
734 drm_clip_rect_t *rects, int num_rects,
735 GLboolean front_buffer)
736 {
737 #ifdef XDAMAGE_1_1_INTERFACE
738 XRectangle *xrects;
739 XserverRegion region;
740 int i;
741 int x_off, y_off;
742
743 if (!has_damage_post(dpy))
744 return;
745
746 if (front_buffer) {
747 x_off = x;
748 y_off = y;
749 drawable = RootWindow(dpy, screen);
750 } else{
751 x_off = 0;
752 y_off = 0;
753 }
754
755 xrects = malloc(sizeof(XRectangle) * num_rects);
756 if (xrects == NULL)
757 return;
758
759 for (i = 0; i < num_rects; i++) {
760 xrects[i].x = rects[i].x1 + x_off;
761 xrects[i].y = rects[i].y1 + y_off;
762 xrects[i].width = rects[i].x2 - rects[i].x1;
763 xrects[i].height = rects[i].y2 - rects[i].y1;
764 }
765 region = XFixesCreateRegion(dpy, xrects, num_rects);
766 free(xrects);
767 XDamageAdd(dpy, drawable, region);
768 XFixesDestroyRegion(dpy, region);
769 #endif
770 }
771
772 /**
773 * Table of functions exported by the loader to the driver.
774 */
775 static const __DRIinterfaceMethods interface_methods = {
776 get_proc_address,
777
778 _gl_context_modes_create,
779 _gl_context_modes_destroy,
780
781 __glXFindDRIScreen,
782 __glXWindowExists,
783
784 XF86DRICreateContextWithConfig,
785 XF86DRIDestroyContext,
786
787 XF86DRICreateDrawable,
788 XF86DRIDestroyDrawable,
789 XF86DRIGetDrawableInfo,
790
791 __glXGetUST,
792 __glXGetMscRateOML,
793
794 __glXReportDamage,
795 };
796
797
798
799 /**
800 * Perform the required libGL-side initialization and call the client-side
801 * driver's \c __driCreateNewScreen function.
802 *
803 * \param dpy Display pointer.
804 * \param scrn Screen number on the display.
805 * \param psc DRI screen information.
806 * \param driDpy DRI display information.
807 * \param createNewScreen Pointer to the client-side driver's
808 * \c __driCreateNewScreen function.
809 * \returns A pointer to the \c __DRIscreenPrivate structure returned by
810 * the client-side driver on success, or \c NULL on failure.
811 *
812 * \todo This function needs to be modified to remove context-modes from the
813 * list stored in the \c __GLXscreenConfigsRec to match the list
814 * returned by the client-side driver.
815 */
816 static void *
817 CallCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
818 __DRIdisplay * driDpy,
819 PFNCREATENEWSCREENFUNC createNewScreen)
820 {
821 __DRIscreenPrivate *psp = NULL;
822 #ifndef GLX_USE_APPLEGL
823 drm_handle_t hSAREA;
824 drmAddress pSAREA = MAP_FAILED;
825 char *BusID;
826 __DRIversion ddx_version;
827 __DRIversion dri_version;
828 __DRIversion drm_version;
829 __DRIframebuffer framebuffer;
830 int fd = -1;
831 int status;
832 const char * err_msg;
833 const char * err_extra;
834 int api_ver = __glXGetInternalVersion();
835
836
837 dri_version.major = driDpy->private->driMajor;
838 dri_version.minor = driDpy->private->driMinor;
839 dri_version.patch = driDpy->private->driPatch;
840
841
842 err_msg = "XF86DRIOpenConnection";
843 err_extra = NULL;
844
845 framebuffer.base = MAP_FAILED;
846 framebuffer.dev_priv = NULL;
847
848 if (XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
849 int newlyopened;
850 fd = drmOpenOnce(NULL,BusID, &newlyopened);
851 Xfree(BusID); /* No longer needed */
852
853 err_msg = "open DRM";
854 err_extra = strerror( -fd );
855
856 if (fd >= 0) {
857 drm_magic_t magic;
858
859 err_msg = "drmGetMagic";
860 err_extra = NULL;
861
862 if (!drmGetMagic(fd, &magic)) {
863 drmVersionPtr version = drmGetVersion(fd);
864 if (version) {
865 drm_version.major = version->version_major;
866 drm_version.minor = version->version_minor;
867 drm_version.patch = version->version_patchlevel;
868 drmFreeVersion(version);
869 }
870 else {
871 drm_version.major = -1;
872 drm_version.minor = -1;
873 drm_version.patch = -1;
874 }
875
876 err_msg = "XF86DRIAuthConnection";
877 if (!newlyopened || XF86DRIAuthConnection(dpy, scrn, magic)) {
878 char *driverName;
879
880 /*
881 * Get device name (like "tdfx") and the ddx version
882 * numbers. We'll check the version in each DRI driver's
883 * "createNewScreen" function.
884 */
885 err_msg = "XF86DRIGetClientDriverName";
886 if (XF86DRIGetClientDriverName(dpy, scrn,
887 &ddx_version.major,
888 &ddx_version.minor,
889 &ddx_version.patch,
890 &driverName)) {
891 drm_handle_t hFB;
892 int junk;
893
894 /* No longer needed. */
895 Xfree( driverName );
896
897
898 /*
899 * Get device-specific info. pDevPriv will point to a struct
900 * (such as DRIRADEONRec in xfree86/driver/ati/radeon_dri.h)
901 * that has information about the screen size, depth, pitch,
902 * ancilliary buffers, DRM mmap handles, etc.
903 */
904 err_msg = "XF86DRIGetDeviceInfo";
905 if (XF86DRIGetDeviceInfo(dpy, scrn,
906 &hFB,
907 &junk,
908 &framebuffer.size,
909 &framebuffer.stride,
910 &framebuffer.dev_priv_size,
911 &framebuffer.dev_priv)) {
912 framebuffer.width = DisplayWidth(dpy, scrn);
913 framebuffer.height = DisplayHeight(dpy, scrn);
914
915 /*
916 * Map the framebuffer region.
917 */
918 status = drmMap(fd, hFB, framebuffer.size,
919 (drmAddressPtr)&framebuffer.base);
920
921 err_msg = "drmMap of framebuffer";
922 err_extra = strerror( -status );
923
924 if ( status == 0 ) {
925 /*
926 * Map the SAREA region. Further mmap regions
927 * may be setup in each DRI driver's
928 * "createNewScreen" function.
929 */
930 status = drmMap(fd, hSAREA, SAREA_MAX,
931 &pSAREA);
932
933 err_msg = "drmMap of sarea";
934 err_extra = strerror( -status );
935
936 if ( status == 0 ) {
937 __GLcontextModes * driver_modes = NULL;
938 __GLXscreenConfigs *configs = psc->screenConfigs;
939
940 err_msg = "InitDriver";
941 err_extra = NULL;
942 psp = (*createNewScreen)(dpy, scrn,
943 psc,
944 configs->configs,
945 & ddx_version,
946 & dri_version,
947 & drm_version,
948 & framebuffer,
949 pSAREA,
950 fd,
951 api_ver,
952 & interface_methods,
953 & driver_modes );
954
955 filter_modes( & configs->configs,
956 driver_modes );
957 _gl_context_modes_destroy( driver_modes );
958 }
959 }
960 }
961 }
962 }
963 }
964 }
965 }
966
967 if ( psp == NULL ) {
968 if ( pSAREA != MAP_FAILED ) {
969 (void)drmUnmap(pSAREA, SAREA_MAX);
970 }
971
972 if ( framebuffer.base != MAP_FAILED ) {
973 (void)drmUnmap((drmAddress)framebuffer.base, framebuffer.size);
974 }
975
976 if ( framebuffer.dev_priv != NULL ) {
977 Xfree(framebuffer.dev_priv);
978 }
979
980 if ( fd >= 0 ) {
981 (void)drmCloseOnce(fd);
982 }
983
984 (void)XF86DRICloseConnection(dpy, scrn);
985
986 if ( err_extra != NULL ) {
987 fprintf(stderr, "libGL error: %s failed (%s)\n", err_msg,
988 err_extra);
989 }
990 else {
991 fprintf(stderr, "libGL error: %s failed\n", err_msg );
992 }
993
994 fprintf(stderr, "libGL error: reverting to (slow) indirect rendering\n");
995 }
996 #endif /* !GLX_USE_APPLEGL */
997
998 return psp;
999 }
1000 #endif /* GLX_DIRECT_RENDERING */
1001
1002
1003 /*
1004 ** Allocate the memory for the per screen configs for each screen.
1005 ** If that works then fetch the per screen configs data.
1006 */
1007 static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
1008 {
1009 xGLXGetVisualConfigsReq *req;
1010 xGLXGetFBConfigsReq *fb_req;
1011 xGLXVendorPrivateWithReplyReq *vpreq;
1012 xGLXGetFBConfigsSGIXReq *sgi_req;
1013 xGLXGetVisualConfigsReply reply;
1014 __GLXscreenConfigs *psc;
1015 __GLcontextModes *config;
1016 GLint i, j, nprops, screens;
1017 INT32 buf[__GLX_TOTAL_CONFIG], *props;
1018 unsigned supported_request = 0;
1019 unsigned prop_size;
1020
1021 /*
1022 ** First allocate memory for the array of per screen configs.
1023 */
1024 screens = ScreenCount(dpy);
1025 psc = (__GLXscreenConfigs*) Xmalloc(screens * sizeof(__GLXscreenConfigs));
1026 if (!psc) {
1027 return GL_FALSE;
1028 }
1029 memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
1030 priv->screenConfigs = psc;
1031
1032 priv->serverGLXversion = __glXGetStringFromServer(dpy, priv->majorOpcode,
1033 X_GLXQueryServerString,
1034 0, GLX_VERSION);
1035 if ( priv->serverGLXversion == NULL ) {
1036 FreeScreenConfigs(priv);
1037 return GL_FALSE;
1038 }
1039
1040 if ( atof( priv->serverGLXversion ) >= 1.3 ) {
1041 supported_request = 1;
1042 }
1043
1044 /*
1045 ** Now fetch each screens configs structures. If a screen supports
1046 ** GL (by returning a numVisuals > 0) then allocate memory for our
1047 ** config structure and then fill it in.
1048 */
1049 for (i = 0; i < screens; i++, psc++) {
1050 if ( supported_request != 1 ) {
1051 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
1052 X_GLXQueryServerString,
1053 i, GLX_EXTENSIONS);
1054 if ( strstr( psc->serverGLXexts, "GLX_SGIX_fbconfig" ) != NULL ) {
1055 supported_request = 2;
1056 }
1057 else {
1058 supported_request = 3;
1059 }
1060 }
1061
1062
1063 LockDisplay(dpy);
1064 switch( supported_request ) {
1065 case 1:
1066 GetReq(GLXGetFBConfigs,fb_req);
1067 fb_req->reqType = priv->majorOpcode;
1068 fb_req->glxCode = X_GLXGetFBConfigs;
1069 fb_req->screen = i;
1070 break;
1071
1072 case 2:
1073 GetReqExtra(GLXVendorPrivateWithReply,
1074 sz_xGLXGetFBConfigsSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
1075 sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
1076 sgi_req->reqType = priv->majorOpcode;
1077 sgi_req->glxCode = X_GLXVendorPrivateWithReply;
1078 sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
1079 sgi_req->screen = i;
1080 break;
1081
1082 case 3:
1083 GetReq(GLXGetVisualConfigs,req);
1084 req->reqType = priv->majorOpcode;
1085 req->glxCode = X_GLXGetVisualConfigs;
1086 req->screen = i;
1087 break;
1088 }
1089
1090 if (!_XReply(dpy, (xReply*) &reply, 0, False)) {
1091 /* Something is busted. Punt. */
1092 UnlockDisplay(dpy);
1093 SyncHandle();
1094 FreeScreenConfigs(priv);
1095 return GL_FALSE;
1096 }
1097
1098 if (!reply.numVisuals) {
1099 /* This screen does not support GL rendering */
1100 UnlockDisplay(dpy);
1101 continue;
1102 }
1103
1104 /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for
1105 * FIXME: FBconfigs?
1106 */
1107 /* Check number of properties */
1108 nprops = reply.numProps;
1109 if ((nprops < __GLX_MIN_CONFIG_PROPS) ||
1110 (nprops > __GLX_MAX_CONFIG_PROPS)) {
1111 /* Huh? Not in protocol defined limits. Punt */
1112 UnlockDisplay(dpy);
1113 SyncHandle();
1114 FreeScreenConfigs(priv);
1115 return GL_FALSE;
1116 }
1117
1118 /* Allocate memory for our config structure */
1119 psc->configs = _gl_context_modes_create(reply.numVisuals,
1120 sizeof(__GLcontextModes));
1121 if (!psc->configs) {
1122 UnlockDisplay(dpy);
1123 SyncHandle();
1124 FreeScreenConfigs(priv);
1125 return GL_FALSE;
1126 }
1127
1128 /* Allocate memory for the properties, if needed */
1129 if ( supported_request != 3 ) {
1130 nprops *= 2;
1131 }
1132
1133 prop_size = nprops * __GLX_SIZE_INT32;
1134
1135 if (prop_size <= sizeof(buf)) {
1136 props = buf;
1137 } else {
1138 props = (INT32 *) Xmalloc(prop_size);
1139 }
1140
1141 /* Read each config structure and convert it into our format */
1142 config = psc->configs;
1143 for (j = 0; j < reply.numVisuals; j++) {
1144 assert( config != NULL );
1145 _XRead(dpy, (char *)props, prop_size);
1146
1147 if ( supported_request != 3 ) {
1148 config->rgbMode = GL_TRUE;
1149 config->drawableType = GLX_WINDOW_BIT;
1150 }
1151 else {
1152 config->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
1153 }
1154
1155 __glXInitializeVisualConfigFromTags( config, nprops, props,
1156 (supported_request != 3),
1157 GL_TRUE );
1158 if ( config->fbconfigID == GLX_DONT_CARE ) {
1159 config->fbconfigID = config->visualID;
1160 }
1161 config->screen = i;
1162 config = config->next;
1163 }
1164 if (props != buf) {
1165 Xfree((char *)props);
1166 }
1167 UnlockDisplay(dpy);
1168
1169 #ifdef GLX_DIRECT_RENDERING
1170 /* Initialize per screen dynamic client GLX extensions */
1171 psc->ext_list_first_time = GL_TRUE;
1172 /* Initialize the direct rendering per screen data and functions */
1173 if (priv->driDisplay.private != NULL) {
1174 /* FIXME: Should it be some sort of an error if createNewScreen[i]
1175 * FIXME: is NULL?
1176 */
1177 if (priv->driDisplay.createNewScreen &&
1178 priv->driDisplay.createNewScreen[i]) {
1179
1180 psc->driScreen.screenConfigs = (void *)psc;
1181 psc->driScreen.private =
1182 CallCreateNewScreen(dpy, i, & psc->driScreen,
1183 & priv->driDisplay,
1184 priv->driDisplay.createNewScreen[i] );
1185 }
1186 }
1187 #endif
1188 }
1189 SyncHandle();
1190 return GL_TRUE;
1191 }
1192
1193 /*
1194 ** Initialize the client side extension code.
1195 */
1196 __GLXdisplayPrivate *__glXInitialize(Display* dpy)
1197 {
1198 XExtDisplayInfo *info = __glXFindDisplay(dpy);
1199 XExtData **privList, *private, *found;
1200 __GLXdisplayPrivate *dpyPriv;
1201 XEDataObject dataObj;
1202 int major, minor;
1203
1204 #if defined(USE_XTHREADS)
1205 {
1206 static int firstCall = 1;
1207 if (firstCall) {
1208 /* initialize the GLX mutexes */
1209 xmutex_init(&__glXmutex);
1210 firstCall = 0;
1211 }
1212 }
1213 #endif
1214
1215 INIT_MESA_SPARC
1216 /* The one and only long long lock */
1217 __glXLock();
1218
1219 if (!XextHasExtension(info)) {
1220 /* No GLX extension supported by this server. Oh well. */
1221 __glXUnlock();
1222 XMissingExtension(dpy, __glXExtensionName);
1223 return 0;
1224 }
1225
1226 /* See if a display private already exists. If so, return it */
1227 dataObj.display = dpy;
1228 privList = XEHeadOfExtensionList(dataObj);
1229 found = XFindOnExtensionList(privList, info->codes->extension);
1230 if (found) {
1231 __glXUnlock();
1232 return (__GLXdisplayPrivate *) found->private_data;
1233 }
1234
1235 /* See if the versions are compatible */
1236 if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor)) {
1237 /* The client and server do not agree on versions. Punt. */
1238 __glXUnlock();
1239 return 0;
1240 }
1241
1242 /*
1243 ** Allocate memory for all the pieces needed for this buffer.
1244 */
1245 private = (XExtData *) Xmalloc(sizeof(XExtData));
1246 if (!private) {
1247 __glXUnlock();
1248 return 0;
1249 }
1250 dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
1251 if (!dpyPriv) {
1252 __glXUnlock();
1253 Xfree((char*) private);
1254 return 0;
1255 }
1256
1257 /*
1258 ** Init the display private and then read in the screen config
1259 ** structures from the server.
1260 */
1261 dpyPriv->majorOpcode = info->codes->major_opcode;
1262 dpyPriv->majorVersion = major;
1263 dpyPriv->minorVersion = minor;
1264 dpyPriv->dpy = dpy;
1265
1266 dpyPriv->serverGLXvendor = 0x0;
1267 dpyPriv->serverGLXversion = 0x0;
1268
1269 #ifdef GLX_DIRECT_RENDERING
1270 /*
1271 ** Initialize the direct rendering per display data and functions.
1272 ** Note: This _must_ be done before calling any other DRI routines
1273 ** (e.g., those called in AllocAndFetchScreenConfigs).
1274 */
1275 if (getenv("LIBGL_ALWAYS_INDIRECT") == NULL) {
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 PUBLIC 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 PUBLIC 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