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