6fda5125db720c313a525bb0b0d8955064fb276e
[mesa.git] / src / glx / x11 / glxcmds.c
1 /* $XFree86: xc/lib/GL/glx/glxcmds.c,v 1.30 2004/01/30 20:33:06 alanh Exp $ */
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 glxcmds.c
39 * Client-side GLX interface.
40 */
41
42 #include <inttypes.h>
43 #include "glxclient.h"
44 #include <X11/extensions/extutil.h>
45 #include <X11/extensions/Xext.h>
46 #include <assert.h>
47 #include <string.h>
48 #include "glapi.h"
49 #ifdef GLX_DIRECT_RENDERING
50 #include "indirect_init.h"
51 #include <X11/extensions/xf86vmode.h>
52 #include "xf86dri.h"
53 #endif
54 #include "glxextensions.h"
55 #include "glcontextmodes.h"
56 #include "glheader.h"
57 #include <sys/time.h>
58
59 static const char __glXGLXClientVendorName[] = "SGI";
60 static const char __glXGLXClientVersion[] = "1.4";
61
62
63 /****************************************************************************/
64
65 #ifdef GLX_DIRECT_RENDERING
66
67 static Bool windowExistsFlag;
68 static int windowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
69 {
70 if (xerr->error_code == BadWindow) {
71 windowExistsFlag = GL_FALSE;
72 }
73 return 0;
74 }
75
76 /**
77 * Find drawables in the local hash that have been destroyed on the
78 * server.
79 *
80 * \param dpy Display to destroy drawables for
81 * \param screen Screen number to destroy drawables for
82 */
83 static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
84 {
85 XID draw;
86 __GLXdrawable *pdraw;
87 XWindowAttributes xwa;
88 int (*oldXErrorHandler)(Display *, XErrorEvent *);
89
90 /* Set no-op error handler so Xlib doesn't bail out if the windows
91 * has alreay been destroyed on the server. */
92 XSync(dpy, GL_FALSE);
93 oldXErrorHandler = XSetErrorHandler(windowExistsErrorHandler);
94
95 if (__glxHashFirst(sc->drawHash, &draw, (void *)&pdraw) == 1) {
96 do {
97 windowExistsFlag = GL_TRUE;
98 XGetWindowAttributes(dpy, draw, &xwa); /* dummy request */
99 if (!windowExistsFlag) {
100 /* Destroy the local drawable data, if the drawable no
101 longer exists in the Xserver */
102 (*pdraw->driDrawable.destroyDrawable)(&pdraw->driDrawable);
103 XF86DRIDestroyDrawable(dpy, sc->scr, draw);
104 Xfree(pdraw);
105 }
106 } while (__glxHashNext(sc->drawHash, &draw, (void *)&pdraw) == 1);
107 }
108
109 XSetErrorHandler(oldXErrorHandler);
110 }
111
112 /**
113 * Get the __DRIdrawable for the drawable associated with a GLXContext
114 *
115 * \param dpy The display associated with \c drawable.
116 * \param drawable GLXDrawable whose __DRIdrawable part is to be retrieved.
117 * \param scrn_num If non-NULL, the drawables screen is stored there
118 * \returns A pointer to the context's __DRIdrawable on success, or NULL if
119 * the drawable is not associated with a direct-rendering context.
120 */
121 static __DRIdrawable *
122 GetDRIDrawable( Display *dpy, GLXDrawable drawable, int * const scrn_num )
123 {
124 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
125 __GLXdrawable * const pdraw;
126 const unsigned screen_count = ScreenCount(dpy);
127 unsigned i;
128 __GLXscreenConfigs *sc;
129
130 if (priv == NULL || priv->driDisplay.private == NULL)
131 return NULL;
132
133 for (i = 0; i < screen_count; i++) {
134 sc = &priv->screenConfigs[i];
135 if (__glxHashLookup(sc->drawHash, drawable, (void *) &pdraw) == 0) {
136 if (scrn_num != NULL)
137 *scrn_num = i;
138 return &pdraw->driDrawable;
139 }
140 }
141
142 return NULL;
143 }
144
145 #endif
146
147
148 /**
149 * Get the GLX per-screen data structure associated with a GLX context.
150 *
151 * \param dpy Display for which the GLX per-screen information is to be
152 * retrieved.
153 * \param scrn Screen on \c dpy for which the GLX per-screen information is
154 * to be retrieved.
155 * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
156 * specify a valid GLX screen, or NULL otherwise.
157 *
158 * \todo Should this function validate that \c scrn is within the screen
159 * number range for \c dpy?
160 */
161
162 static __GLXscreenConfigs *
163 GetGLXScreenConfigs(Display *dpy, int scrn)
164 {
165 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
166
167 return (priv->screenConfigs != NULL) ? &priv->screenConfigs[scrn] : NULL;
168 }
169
170
171 static int
172 GetGLXPrivScreenConfig( Display *dpy, int scrn, __GLXdisplayPrivate ** ppriv,
173 __GLXscreenConfigs ** ppsc )
174 {
175 /* Initialize the extension, if needed . This has the added value
176 * of initializing/allocating the display private
177 */
178
179 if ( dpy == NULL ) {
180 return GLX_NO_EXTENSION;
181 }
182
183 *ppriv = __glXInitialize(dpy);
184 if ( *ppriv == NULL ) {
185 return GLX_NO_EXTENSION;
186 }
187
188 /* Check screen number to see if its valid */
189 if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
190 return GLX_BAD_SCREEN;
191 }
192
193 /* Check to see if the GL is supported on this screen */
194 *ppsc = &((*ppriv)->screenConfigs[scrn]);
195 if ( (*ppsc)->configs == NULL ) {
196 /* No support for GL on this screen regardless of visual */
197 return GLX_BAD_VISUAL;
198 }
199
200 return Success;
201 }
202
203
204 /**
205 * Determine if a \c GLXFBConfig supplied by the application is valid.
206 *
207 * \param dpy Application supplied \c Display pointer.
208 * \param config Application supplied \c GLXFBConfig.
209 *
210 * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
211 * \c __GLcontextModes structure is returned. Otherwise, \c NULL
212 * is returned.
213 */
214 static __GLcontextModes *
215 ValidateGLXFBConfig( Display * dpy, GLXFBConfig config )
216 {
217 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
218 const unsigned num_screens = ScreenCount(dpy);
219 unsigned i;
220 const __GLcontextModes * modes;
221
222
223 if ( priv != NULL ) {
224 for ( i = 0 ; i < num_screens ; i++ ) {
225 for ( modes = priv->screenConfigs[i].configs
226 ; modes != NULL
227 ; modes = modes->next ) {
228 if ( modes == (__GLcontextModes *) config ) {
229 return (__GLcontextModes *) config;
230 }
231 }
232 }
233 }
234
235 return NULL;
236 }
237
238
239 /**
240 * \todo It should be possible to move the allocate of \c client_state_private
241 * later in the function for direct-rendering contexts. Direct-rendering
242 * contexts don't need to track client state, so they don't need that memory
243 * at all.
244 *
245 * \todo Eliminate \c __glXInitVertexArrayState. Replace it with a new
246 * function called \c __glXAllocateClientState that allocates the memory and
247 * does all the initialization (including the pixel pack / unpack).
248 */
249 static
250 GLXContext AllocateGLXContext( Display *dpy )
251 {
252 GLXContext gc;
253 int bufSize;
254 CARD8 opcode;
255 __GLXattribute *state;
256
257 if (!dpy)
258 return NULL;
259
260 opcode = __glXSetupForCommand(dpy);
261 if (!opcode) {
262 return NULL;
263 }
264
265 /* Allocate our context record */
266 gc = (GLXContext) Xmalloc(sizeof(struct __GLXcontextRec));
267 if (!gc) {
268 /* Out of memory */
269 return NULL;
270 }
271 memset(gc, 0, sizeof(struct __GLXcontextRec));
272
273 state = Xmalloc(sizeof(struct __GLXattributeRec));
274 if (state == NULL) {
275 /* Out of memory */
276 Xfree(gc);
277 return NULL;
278 }
279 gc->client_state_private = state;
280 memset(gc->client_state_private, 0, sizeof(struct __GLXattributeRec));
281 state->NoDrawArraysProtocol = (getenv("LIBGL_NO_DRAWARRAYS") != NULL);
282
283 /*
284 ** Create a temporary buffer to hold GLX rendering commands. The size
285 ** of the buffer is selected so that the maximum number of GLX rendering
286 ** commands can fit in a single X packet and still have room in the X
287 ** packet for the GLXRenderReq header.
288 */
289
290 bufSize = (XMaxRequestSize(dpy) * 4) - sz_xGLXRenderReq;
291 gc->buf = (GLubyte *) Xmalloc(bufSize);
292 if (!gc->buf) {
293 Xfree(gc->client_state_private);
294 Xfree(gc);
295 return NULL;
296 }
297 gc->bufSize = bufSize;
298
299 /* Fill in the new context */
300 gc->renderMode = GL_RENDER;
301
302 state->storePack.alignment = 4;
303 state->storeUnpack.alignment = 4;
304
305 gc->attributes.stackPointer = &gc->attributes.stack[0];
306
307 /*
308 ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
309 ** Other code uses the fastImageUnpack bit, but it is never set
310 ** to GL_TRUE.
311 */
312 gc->fastImageUnpack = GL_FALSE;
313 gc->fillImage = __glFillImage;
314 gc->isDirect = GL_FALSE;
315 gc->pc = gc->buf;
316 gc->bufEnd = gc->buf + bufSize;
317 if (__glXDebug) {
318 /*
319 ** Set limit register so that there will be one command per packet
320 */
321 gc->limit = gc->buf;
322 } else {
323 gc->limit = gc->buf + bufSize - __GLX_BUFFER_LIMIT_SIZE;
324 }
325 gc->createDpy = dpy;
326 gc->majorOpcode = opcode;
327
328 /*
329 ** Constrain the maximum drawing command size allowed to be
330 ** transfered using the X_GLXRender protocol request. First
331 ** constrain by a software limit, then constrain by the protocl
332 ** limit.
333 */
334 if (bufSize > __GLX_RENDER_CMD_SIZE_LIMIT) {
335 bufSize = __GLX_RENDER_CMD_SIZE_LIMIT;
336 }
337 if (bufSize > __GLX_MAX_RENDER_CMD_SIZE) {
338 bufSize = __GLX_MAX_RENDER_CMD_SIZE;
339 }
340 gc->maxSmallRenderCommandSize = bufSize;
341 return gc;
342 }
343
344
345 /**
346 * Create a new context. Exactly one of \c vis and \c fbconfig should be
347 * non-NULL.
348 *
349 * \param use_glx_1_3 For FBConfigs, should GLX 1.3 protocol or
350 * SGIX_fbconfig protocol be used?
351 * \param renderType For FBConfigs, what is the rendering type?
352 */
353
354 static GLXContext
355 CreateContext(Display *dpy, XVisualInfo *vis,
356 const __GLcontextModes * const fbconfig,
357 GLXContext shareList,
358 Bool allowDirect, GLXContextID contextID,
359 Bool use_glx_1_3, int renderType)
360 {
361 GLXContext gc;
362
363 if ( dpy == NULL )
364 return NULL;
365
366 gc = AllocateGLXContext(dpy);
367 if (!gc)
368 return NULL;
369
370 if (None == contextID) {
371 if ( (vis == NULL) && (fbconfig == NULL) )
372 return NULL;
373
374 #ifdef GLX_DIRECT_RENDERING
375 if (allowDirect) {
376 int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
377 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
378 const __GLcontextModes * mode;
379 drm_context_t hwContext;
380
381 /* The value of fbconfig cannot change because it is tested
382 * later in the function.
383 */
384 if ( fbconfig == NULL ) {
385 /* FIXME: Is it possible for the __GLcontextModes structure
386 * FIXME: to not be found?
387 */
388 mode = _gl_context_modes_find_visual( psc->configs,
389 vis->visualid );
390 assert( mode != NULL );
391 assert( mode->screen == screen );
392 }
393 else {
394 mode = fbconfig;
395 }
396
397 if (psc && psc->driScreen.private) {
398 void * const shared = (shareList != NULL)
399 ? shareList->driContext.private : NULL;
400
401
402 if (!XF86DRICreateContextWithConfig(dpy, psc->scr,
403 mode->fbconfigID,
404 &gc->hwContextID, &hwContext))
405 /* gah, handle this better */
406 return NULL;
407
408 gc->driContext.private =
409 (*psc->driScreen.createNewContext)( &psc->driScreen,
410 mode, renderType,
411 shared,
412 hwContext,
413 &gc->driContext );
414 if (gc->driContext.private) {
415 gc->isDirect = GL_TRUE;
416 gc->screen = mode->screen;
417 gc->psc = psc;
418 gc->vid = mode->visualID;
419 gc->fbconfigID = mode->fbconfigID;
420 gc->mode = mode;
421 }
422 else {
423 XF86DRIDestroyContext(dpy, psc->scr, gc->hwContextID);
424 }
425 }
426 }
427 #endif
428
429 LockDisplay(dpy);
430 if ( fbconfig == NULL ) {
431 xGLXCreateContextReq *req;
432
433 /* Send the glXCreateContext request */
434 GetReq(GLXCreateContext,req);
435 req->reqType = gc->majorOpcode;
436 req->glxCode = X_GLXCreateContext;
437 req->context = gc->xid = XAllocID(dpy);
438 req->visual = vis->visualid;
439 req->screen = vis->screen;
440 req->shareList = shareList ? shareList->xid : None;
441 req->isDirect = gc->isDirect;
442 }
443 else if ( use_glx_1_3 ) {
444 xGLXCreateNewContextReq *req;
445
446 /* Send the glXCreateNewContext request */
447 GetReq(GLXCreateNewContext,req);
448 req->reqType = gc->majorOpcode;
449 req->glxCode = X_GLXCreateNewContext;
450 req->context = gc->xid = XAllocID(dpy);
451 req->fbconfig = fbconfig->fbconfigID;
452 req->screen = fbconfig->screen;
453 req->renderType = renderType;
454 req->shareList = shareList ? shareList->xid : None;
455 req->isDirect = gc->isDirect;
456 }
457 else {
458 xGLXVendorPrivateWithReplyReq *vpreq;
459 xGLXCreateContextWithConfigSGIXReq *req;
460
461 /* Send the glXCreateNewContext request */
462 GetReqExtra(GLXVendorPrivateWithReply,
463 sz_xGLXCreateContextWithConfigSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
464 req = (xGLXCreateContextWithConfigSGIXReq *)vpreq;
465 req->reqType = gc->majorOpcode;
466 req->glxCode = X_GLXVendorPrivateWithReply;
467 req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
468 req->context = gc->xid = XAllocID(dpy);
469 req->fbconfig = fbconfig->fbconfigID;
470 req->screen = fbconfig->screen;
471 req->renderType = renderType;
472 req->shareList = shareList ? shareList->xid : None;
473 req->isDirect = gc->isDirect;
474 }
475
476 UnlockDisplay(dpy);
477 SyncHandle();
478 gc->imported = GL_FALSE;
479 }
480 else {
481 gc->xid = contextID;
482 gc->imported = GL_TRUE;
483 }
484
485 return gc;
486 }
487
488 PUBLIC GLXContext glXCreateContext(Display *dpy, XVisualInfo *vis,
489 GLXContext shareList, Bool allowDirect)
490 {
491 return CreateContext(dpy, vis, NULL, shareList, allowDirect, None,
492 False, 0);
493 }
494
495 void __glXFreeContext(__GLXcontext *gc)
496 {
497 if (gc->vendor) XFree((char *) gc->vendor);
498 if (gc->renderer) XFree((char *) gc->renderer);
499 if (gc->version) XFree((char *) gc->version);
500 if (gc->extensions) XFree((char *) gc->extensions);
501 __glFreeAttributeState(gc);
502 XFree((char *) gc->buf);
503 Xfree((char *) gc->client_state_private);
504 XFree((char *) gc);
505
506 }
507
508 /*
509 ** Destroy the named context
510 */
511 static void
512 DestroyContext(Display *dpy, GLXContext gc)
513 {
514 xGLXDestroyContextReq *req;
515 GLXContextID xid;
516 CARD8 opcode;
517 GLboolean imported;
518
519 opcode = __glXSetupForCommand(dpy);
520 if (!opcode || !gc) {
521 return;
522 }
523
524 __glXLock();
525 xid = gc->xid;
526 imported = gc->imported;
527 gc->xid = None;
528
529 #ifdef GLX_DIRECT_RENDERING
530 /* Destroy the direct rendering context */
531 if (gc->isDirect) {
532 if (gc->driContext.private) {
533 (*gc->driContext.destroyContext)(&gc->driContext);
534 XF86DRIDestroyContext(dpy, gc->psc->scr, gc->hwContextID);
535 gc->driContext.private = NULL;
536 }
537 GarbageCollectDRIDrawables(dpy, gc->psc);
538 }
539 #endif
540
541 if (gc->currentDpy) {
542 /* Have to free later cuz it's in use now */
543 __glXUnlock();
544 } else {
545 /* Destroy the handle if not current to anybody */
546 __glXUnlock();
547 __glXFreeContext(gc);
548 }
549
550 if (!imported) {
551 /*
552 ** This dpy also created the server side part of the context.
553 ** Send the glXDestroyContext request.
554 */
555 LockDisplay(dpy);
556 GetReq(GLXDestroyContext,req);
557 req->reqType = opcode;
558 req->glxCode = X_GLXDestroyContext;
559 req->context = xid;
560 UnlockDisplay(dpy);
561 SyncHandle();
562 }
563 }
564
565 PUBLIC void glXDestroyContext(Display *dpy, GLXContext gc)
566 {
567 DestroyContext(dpy, gc);
568 }
569
570 /*
571 ** Return the major and minor version #s for the GLX extension
572 */
573 PUBLIC Bool glXQueryVersion(Display *dpy, int *major, int *minor)
574 {
575 __GLXdisplayPrivate *priv;
576
577 /* Init the extension. This fetches the major and minor version. */
578 priv = __glXInitialize(dpy);
579 if (!priv) return GL_FALSE;
580
581 if (major) *major = priv->majorVersion;
582 if (minor) *minor = priv->minorVersion;
583 return GL_TRUE;
584 }
585
586 /*
587 ** Query the existance of the GLX extension
588 */
589 PUBLIC Bool glXQueryExtension(Display *dpy, int *errorBase, int *eventBase)
590 {
591 int major_op, erb, evb;
592 Bool rv;
593
594 rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
595 if (rv) {
596 if (errorBase) *errorBase = erb;
597 if (eventBase) *eventBase = evb;
598 }
599 return rv;
600 }
601
602 /*
603 ** Put a barrier in the token stream that forces the GL to finish its
604 ** work before X can proceed.
605 */
606 PUBLIC void glXWaitGL(void)
607 {
608 xGLXWaitGLReq *req;
609 GLXContext gc = __glXGetCurrentContext();
610 Display *dpy = gc->currentDpy;
611
612 if (!dpy) return;
613
614 /* Flush any pending commands out */
615 __glXFlushRenderBuffer(gc, gc->pc);
616
617 #ifdef GLX_DIRECT_RENDERING
618 if (gc->isDirect) {
619 /* This bit of ugliness unwraps the glFinish function */
620 #ifdef glFinish
621 #undef glFinish
622 #endif
623 glFinish();
624 return;
625 }
626 #endif
627
628 /* Send the glXWaitGL request */
629 LockDisplay(dpy);
630 GetReq(GLXWaitGL,req);
631 req->reqType = gc->majorOpcode;
632 req->glxCode = X_GLXWaitGL;
633 req->contextTag = gc->currentContextTag;
634 UnlockDisplay(dpy);
635 SyncHandle();
636 }
637
638 /*
639 ** Put a barrier in the token stream that forces X to finish its
640 ** work before GL can proceed.
641 */
642 PUBLIC void glXWaitX(void)
643 {
644 xGLXWaitXReq *req;
645 GLXContext gc = __glXGetCurrentContext();
646 Display *dpy = gc->currentDpy;
647
648 if (!dpy) return;
649
650 /* Flush any pending commands out */
651 __glXFlushRenderBuffer(gc, gc->pc);
652
653 #ifdef GLX_DIRECT_RENDERING
654 if (gc->isDirect) {
655 XSync(dpy, False);
656 return;
657 }
658 #endif
659
660 /*
661 ** Send the glXWaitX request.
662 */
663 LockDisplay(dpy);
664 GetReq(GLXWaitX,req);
665 req->reqType = gc->majorOpcode;
666 req->glxCode = X_GLXWaitX;
667 req->contextTag = gc->currentContextTag;
668 UnlockDisplay(dpy);
669 SyncHandle();
670 }
671
672 PUBLIC void glXUseXFont(Font font, int first, int count, int listBase)
673 {
674 xGLXUseXFontReq *req;
675 GLXContext gc = __glXGetCurrentContext();
676 Display *dpy = gc->currentDpy;
677
678 if (!dpy) return;
679
680 /* Flush any pending commands out */
681 (void) __glXFlushRenderBuffer(gc, gc->pc);
682
683 #ifdef GLX_DIRECT_RENDERING
684 if (gc->isDirect) {
685 DRI_glXUseXFont(font, first, count, listBase);
686 return;
687 }
688 #endif
689
690 /* Send the glXUseFont request */
691 LockDisplay(dpy);
692 GetReq(GLXUseXFont,req);
693 req->reqType = gc->majorOpcode;
694 req->glxCode = X_GLXUseXFont;
695 req->contextTag = gc->currentContextTag;
696 req->font = font;
697 req->first = first;
698 req->count = count;
699 req->listBase = listBase;
700 UnlockDisplay(dpy);
701 SyncHandle();
702 }
703
704 /************************************************************************/
705
706 /*
707 ** Copy the source context to the destination context using the
708 ** attribute "mask".
709 */
710 PUBLIC void glXCopyContext(Display *dpy, GLXContext source,
711 GLXContext dest, unsigned long mask)
712 {
713 xGLXCopyContextReq *req;
714 GLXContext gc = __glXGetCurrentContext();
715 GLXContextTag tag;
716 CARD8 opcode;
717
718 opcode = __glXSetupForCommand(dpy);
719 if (!opcode) {
720 return;
721 }
722
723 #ifdef GLX_DIRECT_RENDERING
724 if (gc->isDirect) {
725 /* NOT_DONE: This does not work yet */
726 }
727 #endif
728
729 /*
730 ** If the source is the current context, send its tag so that the context
731 ** can be flushed before the copy.
732 */
733 if (source == gc && dpy == gc->currentDpy) {
734 tag = gc->currentContextTag;
735 } else {
736 tag = 0;
737 }
738
739 /* Send the glXCopyContext request */
740 LockDisplay(dpy);
741 GetReq(GLXCopyContext,req);
742 req->reqType = opcode;
743 req->glxCode = X_GLXCopyContext;
744 req->source = source ? source->xid : None;
745 req->dest = dest ? dest->xid : None;
746 req->mask = mask;
747 req->contextTag = tag;
748 UnlockDisplay(dpy);
749 SyncHandle();
750 }
751
752
753 /**
754 * Determine if a context uses direct rendering.
755 *
756 * \param dpy Display where the context was created.
757 * \param contextID ID of the context to be tested.
758 *
759 * \returns \c GL_TRUE if the context is direct rendering or not.
760 */
761 static Bool __glXIsDirect(Display *dpy, GLXContextID contextID)
762 {
763 xGLXIsDirectReq *req;
764 xGLXIsDirectReply reply;
765 CARD8 opcode;
766
767 opcode = __glXSetupForCommand(dpy);
768 if (!opcode) {
769 return GL_FALSE;
770 }
771
772 /* Send the glXIsDirect request */
773 LockDisplay(dpy);
774 GetReq(GLXIsDirect,req);
775 req->reqType = opcode;
776 req->glxCode = X_GLXIsDirect;
777 req->context = contextID;
778 _XReply(dpy, (xReply*) &reply, 0, False);
779 UnlockDisplay(dpy);
780 SyncHandle();
781
782 return reply.isDirect;
783 }
784
785 /**
786 * \todo
787 * Shouldn't this function \b always return \c GL_FALSE when
788 * \c GLX_DIRECT_RENDERING is not defined? Do we really need to bother with
789 * the GLX protocol here at all?
790 */
791 PUBLIC Bool glXIsDirect(Display *dpy, GLXContext gc)
792 {
793 if (!gc) {
794 return GL_FALSE;
795 #ifdef GLX_DIRECT_RENDERING
796 } else if (gc->isDirect) {
797 return GL_TRUE;
798 #endif
799 }
800 return __glXIsDirect(dpy, gc->xid);
801 }
802
803 PUBLIC GLXPixmap glXCreateGLXPixmap(Display *dpy, XVisualInfo *vis,
804 Pixmap pixmap)
805 {
806 xGLXCreateGLXPixmapReq *req;
807 GLXPixmap xid;
808 CARD8 opcode;
809
810 opcode = __glXSetupForCommand(dpy);
811 if (!opcode) {
812 return None;
813 }
814
815 /* Send the glXCreateGLXPixmap request */
816 LockDisplay(dpy);
817 GetReq(GLXCreateGLXPixmap,req);
818 req->reqType = opcode;
819 req->glxCode = X_GLXCreateGLXPixmap;
820 req->screen = vis->screen;
821 req->visual = vis->visualid;
822 req->pixmap = pixmap;
823 req->glxpixmap = xid = XAllocID(dpy);
824 UnlockDisplay(dpy);
825 SyncHandle();
826 return xid;
827 }
828
829 /*
830 ** Destroy the named pixmap
831 */
832 PUBLIC void glXDestroyGLXPixmap(Display *dpy, GLXPixmap glxpixmap)
833 {
834 xGLXDestroyGLXPixmapReq *req;
835 CARD8 opcode;
836
837 opcode = __glXSetupForCommand(dpy);
838 if (!opcode) {
839 return;
840 }
841
842 /* Send the glXDestroyGLXPixmap request */
843 LockDisplay(dpy);
844 GetReq(GLXDestroyGLXPixmap,req);
845 req->reqType = opcode;
846 req->glxCode = X_GLXDestroyGLXPixmap;
847 req->glxpixmap = glxpixmap;
848 UnlockDisplay(dpy);
849 SyncHandle();
850 }
851
852 PUBLIC void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
853 {
854 xGLXSwapBuffersReq *req;
855 GLXContext gc;
856 GLXContextTag tag;
857 CARD8 opcode;
858 #ifdef GLX_DIRECT_RENDERING
859 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, NULL );
860
861 if ( pdraw != NULL ) {
862 (*pdraw->swapBuffers)(pdraw);
863 return;
864 }
865 #endif
866
867 opcode = __glXSetupForCommand(dpy);
868 if (!opcode) {
869 return;
870 }
871
872 /*
873 ** The calling thread may or may not have a current context. If it
874 ** does, send the context tag so the server can do a flush.
875 */
876 gc = __glXGetCurrentContext();
877 if ((gc != NULL) && (dpy == gc->currentDpy) &&
878 ((drawable == gc->currentDrawable) || (drawable == gc->currentReadable)) ) {
879 tag = gc->currentContextTag;
880 } else {
881 tag = 0;
882 }
883
884 /* Send the glXSwapBuffers request */
885 LockDisplay(dpy);
886 GetReq(GLXSwapBuffers,req);
887 req->reqType = opcode;
888 req->glxCode = X_GLXSwapBuffers;
889 req->drawable = drawable;
890 req->contextTag = tag;
891 UnlockDisplay(dpy);
892 SyncHandle();
893 XFlush(dpy);
894 }
895
896
897 /*
898 ** Return configuration information for the given display, screen and
899 ** visual combination.
900 */
901 PUBLIC int glXGetConfig(Display *dpy, XVisualInfo *vis, int attribute,
902 int *value_return)
903 {
904 __GLXdisplayPrivate *priv;
905 __GLXscreenConfigs *psc;
906 int status;
907
908 status = GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc );
909 if ( status == Success ) {
910 const __GLcontextModes * const modes = _gl_context_modes_find_visual(
911 psc->configs, vis->visualid );
912
913 /* Lookup attribute after first finding a match on the visual */
914 if ( modes != NULL ) {
915 return _gl_get_context_mode_data( modes, attribute, value_return );
916 }
917
918 status = GLX_BAD_VISUAL;
919 }
920
921 /*
922 ** If we can't find the config for this visual, this visual is not
923 ** supported by the OpenGL implementation on the server.
924 */
925 if ( (status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL) ) {
926 *value_return = GL_FALSE;
927 status = Success;
928 }
929
930 return status;
931 }
932
933 /************************************************************************/
934
935 static void
936 init_fbconfig_for_chooser( __GLcontextModes * config,
937 GLboolean fbconfig_style_tags )
938 {
939 memset( config, 0, sizeof( __GLcontextModes ) );
940 config->visualID = (XID) GLX_DONT_CARE;
941 config->visualType = GLX_DONT_CARE;
942
943 /* glXChooseFBConfig specifies different defaults for these two than
944 * glXChooseVisual.
945 */
946 if ( fbconfig_style_tags ) {
947 config->rgbMode = GL_TRUE;
948 config->doubleBufferMode = GLX_DONT_CARE;
949 }
950
951 config->visualRating = GLX_DONT_CARE;
952 config->transparentPixel = GLX_NONE;
953 config->transparentRed = GLX_DONT_CARE;
954 config->transparentGreen = GLX_DONT_CARE;
955 config->transparentBlue = GLX_DONT_CARE;
956 config->transparentAlpha = GLX_DONT_CARE;
957 config->transparentIndex = GLX_DONT_CARE;
958
959 config->drawableType = GLX_WINDOW_BIT;
960 config->renderType = (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
961 config->xRenderable = GLX_DONT_CARE;
962 config->fbconfigID = (GLXFBConfigID)(GLX_DONT_CARE);
963
964 config->swapMethod = GLX_DONT_CARE;
965 }
966
967 #define MATCH_DONT_CARE( param ) \
968 do { \
969 if ( (a-> param != GLX_DONT_CARE) \
970 && (a-> param != b-> param) ) { \
971 return False; \
972 } \
973 } while ( 0 )
974
975 #define MATCH_MINIMUM( param ) \
976 do { \
977 if ( (a-> param != GLX_DONT_CARE) \
978 && (a-> param > b-> param) ) { \
979 return False; \
980 } \
981 } while ( 0 )
982
983 #define MATCH_EXACT( param ) \
984 do { \
985 if ( a-> param != b-> param) { \
986 return False; \
987 } \
988 } while ( 0 )
989
990 /**
991 * Determine if two GLXFBConfigs are compatible.
992 *
993 * \param a Application specified config to test.
994 * \param b Server specified config to test against \c a.
995 */
996 static Bool
997 fbconfigs_compatible( const __GLcontextModes * const a,
998 const __GLcontextModes * const b )
999 {
1000 MATCH_DONT_CARE( doubleBufferMode );
1001 MATCH_DONT_CARE( visualType );
1002 MATCH_DONT_CARE( visualRating );
1003 MATCH_DONT_CARE( xRenderable );
1004 MATCH_DONT_CARE( fbconfigID );
1005 MATCH_DONT_CARE( swapMethod );
1006
1007 MATCH_MINIMUM( rgbBits );
1008 MATCH_MINIMUM( numAuxBuffers );
1009 MATCH_MINIMUM( redBits );
1010 MATCH_MINIMUM( greenBits );
1011 MATCH_MINIMUM( blueBits );
1012 MATCH_MINIMUM( alphaBits );
1013 MATCH_MINIMUM( depthBits );
1014 MATCH_MINIMUM( stencilBits );
1015 MATCH_MINIMUM( accumRedBits );
1016 MATCH_MINIMUM( accumGreenBits );
1017 MATCH_MINIMUM( accumBlueBits );
1018 MATCH_MINIMUM( accumAlphaBits );
1019 MATCH_MINIMUM( sampleBuffers );
1020 MATCH_MINIMUM( maxPbufferWidth );
1021 MATCH_MINIMUM( maxPbufferHeight );
1022 MATCH_MINIMUM( maxPbufferPixels );
1023 MATCH_MINIMUM( samples );
1024
1025 MATCH_DONT_CARE( stereoMode );
1026 MATCH_EXACT( level );
1027
1028 if ( ((a->drawableType & b->drawableType) == 0)
1029 || ((a->renderType & b->renderType) == 0) ) {
1030 return False;
1031 }
1032
1033
1034 /* There is a bug in a few of the XFree86 DDX drivers. They contain
1035 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
1036 * Technically speaking, it is a bug in the DDX driver, but there is
1037 * enough of an installed base to work around the problem here. In any
1038 * case, 0 is not a valid value of the transparent type, so we'll treat 0
1039 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
1040 * 0 from the server to be a match to maintain backward compatibility with
1041 * the (broken) drivers.
1042 */
1043
1044 if ( a->transparentPixel != GLX_DONT_CARE
1045 && a->transparentPixel != 0 ) {
1046 if ( a->transparentPixel == GLX_NONE ) {
1047 if ( b->transparentPixel != GLX_NONE && b->transparentPixel != 0 )
1048 return False;
1049 } else {
1050 MATCH_EXACT( transparentPixel );
1051 }
1052
1053 switch ( a->transparentPixel ) {
1054 case GLX_TRANSPARENT_RGB:
1055 MATCH_DONT_CARE( transparentRed );
1056 MATCH_DONT_CARE( transparentGreen );
1057 MATCH_DONT_CARE( transparentBlue );
1058 MATCH_DONT_CARE( transparentAlpha );
1059 break;
1060
1061 case GLX_TRANSPARENT_INDEX:
1062 MATCH_DONT_CARE( transparentIndex );
1063 break;
1064
1065 default:
1066 break;
1067 }
1068 }
1069
1070 return True;
1071 }
1072
1073
1074 /* There's some trickly language in the GLX spec about how this is supposed
1075 * to work. Basically, if a given component size is either not specified
1076 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1077 * Well, that's really hard to do with the code as-is. This behavior is
1078 * closer to correct, but still not technically right.
1079 */
1080 #define PREFER_LARGER_OR_ZERO(comp) \
1081 do { \
1082 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1083 if ( ((*a)-> comp) == 0 ) { \
1084 return -1; \
1085 } \
1086 else if ( ((*b)-> comp) == 0 ) { \
1087 return 1; \
1088 } \
1089 else { \
1090 return ((*b)-> comp) - ((*a)-> comp) ; \
1091 } \
1092 } \
1093 } while( 0 )
1094
1095 #define PREFER_LARGER(comp) \
1096 do { \
1097 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1098 return ((*b)-> comp) - ((*a)-> comp) ; \
1099 } \
1100 } while( 0 )
1101
1102 #define PREFER_SMALLER(comp) \
1103 do { \
1104 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1105 return ((*a)-> comp) - ((*b)-> comp) ; \
1106 } \
1107 } while( 0 )
1108
1109 /**
1110 * Compare two GLXFBConfigs. This function is intended to be used as the
1111 * compare function passed in to qsort.
1112 *
1113 * \returns If \c a is a "better" config, according to the specification of
1114 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1115 * better, then a number greater than zero is return. If both are
1116 * equal, zero is returned.
1117 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1118 */
1119 static int
1120 fbconfig_compare( const __GLcontextModes * const * const a,
1121 const __GLcontextModes * const * const b )
1122 {
1123 /* The order of these comparisons must NOT change. It is defined by
1124 * the GLX 1.3 spec and ARB_multisample.
1125 */
1126
1127 PREFER_SMALLER( visualSelectGroup );
1128
1129 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1130 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1131 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1132 */
1133 PREFER_SMALLER( visualRating );
1134
1135 /* This isn't quite right. It is supposed to compare the sum of the
1136 * components the user specifically set minimums for.
1137 */
1138 PREFER_LARGER_OR_ZERO( redBits );
1139 PREFER_LARGER_OR_ZERO( greenBits );
1140 PREFER_LARGER_OR_ZERO( blueBits );
1141 PREFER_LARGER_OR_ZERO( alphaBits );
1142
1143 PREFER_SMALLER( rgbBits );
1144
1145 if ( ((*a)->doubleBufferMode != (*b)->doubleBufferMode) ) {
1146 /* Prefer single-buffer.
1147 */
1148 return ( !(*a)->doubleBufferMode ) ? -1 : 1;
1149 }
1150
1151 PREFER_SMALLER( numAuxBuffers );
1152
1153 PREFER_LARGER_OR_ZERO( depthBits );
1154 PREFER_SMALLER( stencilBits );
1155
1156 /* This isn't quite right. It is supposed to compare the sum of the
1157 * components the user specifically set minimums for.
1158 */
1159 PREFER_LARGER_OR_ZERO( accumRedBits );
1160 PREFER_LARGER_OR_ZERO( accumGreenBits );
1161 PREFER_LARGER_OR_ZERO( accumBlueBits );
1162 PREFER_LARGER_OR_ZERO( accumAlphaBits );
1163
1164 PREFER_SMALLER( visualType );
1165
1166 /* None of the multisample specs say where this comparison should happen,
1167 * so I put it near the end.
1168 */
1169 PREFER_SMALLER( sampleBuffers );
1170 PREFER_SMALLER( samples );
1171
1172 /* None of the pbuffer or fbconfig specs say that this comparison needs
1173 * to happen at all, but it seems like it should.
1174 */
1175 PREFER_LARGER( maxPbufferWidth );
1176 PREFER_LARGER( maxPbufferHeight );
1177 PREFER_LARGER( maxPbufferPixels );
1178
1179 return 0;
1180 }
1181
1182
1183 /**
1184 * Selects and sorts a subset of the supplied configs based on the attributes.
1185 * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1186 * and \c glXChooseFBConfigSGIX.
1187 *
1188 * \param configs Array of pointers to possible configs. The elements of
1189 * this array that do not meet the criteria will be set to
1190 * NULL. The remaining elements will be sorted according to
1191 * the various visual / FBConfig selection rules.
1192 * \param num_configs Number of elements in the \c configs array.
1193 * \param attribList Attributes used select from \c configs. This array is
1194 * terminated by a \c None tag. The array can either take
1195 * the form expected by \c glXChooseVisual (where boolean
1196 * tags do not have a value) or by \c glXChooseFBConfig
1197 * (where every tag has a value).
1198 * \param fbconfig_style_tags Selects whether \c attribList is in
1199 * \c glXChooseVisual style or
1200 * \c glXChooseFBConfig style.
1201 * \returns The number of valid elements left in \c configs.
1202 *
1203 * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1204 */
1205 static int
1206 choose_visual( __GLcontextModes ** configs, int num_configs,
1207 const int *attribList, GLboolean fbconfig_style_tags )
1208 {
1209 __GLcontextModes test_config;
1210 int base;
1211 int i;
1212
1213 /* This is a fairly direct implementation of the selection method
1214 * described by GLX_SGIX_fbconfig. Start by culling out all the
1215 * configs that are not compatible with the selected parameter
1216 * list.
1217 */
1218
1219 init_fbconfig_for_chooser( & test_config, fbconfig_style_tags );
1220 __glXInitializeVisualConfigFromTags( & test_config, 512,
1221 (const INT32 *) attribList,
1222 GL_TRUE, fbconfig_style_tags );
1223
1224 base = 0;
1225 for ( i = 0 ; i < num_configs ; i++ ) {
1226 if ( fbconfigs_compatible( & test_config, configs[i] ) ) {
1227 configs[ base ] = configs[ i ];
1228 base++;
1229 }
1230 }
1231
1232 if ( base == 0 ) {
1233 return 0;
1234 }
1235
1236 if ( base < num_configs ) {
1237 (void) memset( & configs[ base ], 0,
1238 sizeof( void * ) * (num_configs - base) );
1239 }
1240
1241 /* After the incompatible configs are removed, the resulting
1242 * list is sorted according to the rules set out in the various
1243 * specifications.
1244 */
1245
1246 qsort( configs, base, sizeof( __GLcontextModes * ),
1247 (int (*)(const void*, const void*)) fbconfig_compare );
1248 return base;
1249 }
1250
1251
1252
1253
1254 /*
1255 ** Return the visual that best matches the template. Return None if no
1256 ** visual matches the template.
1257 */
1258 PUBLIC XVisualInfo *glXChooseVisual(Display *dpy, int screen, int *attribList)
1259 {
1260 XVisualInfo *visualList = NULL;
1261 __GLXdisplayPrivate *priv;
1262 __GLXscreenConfigs *psc;
1263 __GLcontextModes test_config;
1264 __GLcontextModes *modes;
1265 const __GLcontextModes *best_config = NULL;
1266
1267 /*
1268 ** Get a list of all visuals, return if list is empty
1269 */
1270 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1271 return None;
1272 }
1273
1274
1275 /*
1276 ** Build a template from the defaults and the attribute list
1277 ** Free visual list and return if an unexpected token is encountered
1278 */
1279 init_fbconfig_for_chooser( & test_config, GL_FALSE );
1280 __glXInitializeVisualConfigFromTags( & test_config, 512,
1281 (const INT32 *) attribList,
1282 GL_TRUE, GL_FALSE );
1283
1284 /*
1285 ** Eliminate visuals that don't meet minimum requirements
1286 ** Compute a score for those that do
1287 ** Remember which visual, if any, got the highest score
1288 */
1289 for ( modes = psc->configs ; modes != NULL ; modes = modes->next ) {
1290 if ( fbconfigs_compatible( & test_config, modes )
1291 && ((best_config == NULL)
1292 || (fbconfig_compare( (const __GLcontextModes * const * const)&modes, &best_config ) < 0)) ) {
1293 best_config = modes;
1294 }
1295 }
1296
1297 /*
1298 ** If no visual is acceptable, return None
1299 ** Otherwise, create an XVisualInfo list with just the selected X visual
1300 ** and return this.
1301 */
1302 if (best_config != NULL) {
1303 XVisualInfo visualTemplate;
1304 int i;
1305
1306 visualTemplate.screen = screen;
1307 visualTemplate.visualid = best_config->visualID;
1308 visualList = XGetVisualInfo( dpy, VisualScreenMask|VisualIDMask,
1309 &visualTemplate, &i );
1310 }
1311
1312 return visualList;
1313 }
1314
1315
1316 PUBLIC const char *glXQueryExtensionsString( Display *dpy, int screen )
1317 {
1318 __GLXscreenConfigs *psc;
1319 __GLXdisplayPrivate *priv;
1320
1321 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1322 return NULL;
1323 }
1324
1325 if (!psc->effectiveGLXexts) {
1326 if (!psc->serverGLXexts) {
1327 psc->serverGLXexts = __glXGetStringFromServer(dpy, priv->majorOpcode,
1328 X_GLXQueryServerString,
1329 screen, GLX_EXTENSIONS);
1330 }
1331
1332 __glXCalculateUsableExtensions(psc,
1333 #ifdef GLX_DIRECT_RENDERING
1334 (psc->driScreen.private != NULL),
1335 #else
1336 GL_FALSE,
1337 #endif
1338 priv->minorVersion);
1339 }
1340
1341 return psc->effectiveGLXexts;
1342 }
1343
1344 PUBLIC const char *glXGetClientString( Display *dpy, int name )
1345 {
1346 switch(name) {
1347 case GLX_VENDOR:
1348 return (__glXGLXClientVendorName);
1349 case GLX_VERSION:
1350 return (__glXGLXClientVersion);
1351 case GLX_EXTENSIONS:
1352 return (__glXGetClientExtensions());
1353 default:
1354 return NULL;
1355 }
1356 }
1357
1358 PUBLIC const char *glXQueryServerString( Display *dpy, int screen, int name )
1359 {
1360 __GLXscreenConfigs *psc;
1361 __GLXdisplayPrivate *priv;
1362 const char ** str;
1363
1364
1365 if ( GetGLXPrivScreenConfig( dpy, screen, & priv, & psc ) != Success ) {
1366 return NULL;
1367 }
1368
1369 switch(name) {
1370 case GLX_VENDOR:
1371 str = & priv->serverGLXvendor;
1372 break;
1373 case GLX_VERSION:
1374 str = & priv->serverGLXversion;
1375 break;
1376 case GLX_EXTENSIONS:
1377 str = & psc->serverGLXexts;
1378 break;
1379 default:
1380 return NULL;
1381 }
1382
1383 if ( *str == NULL ) {
1384 *str = __glXGetStringFromServer(dpy, priv->majorOpcode,
1385 X_GLXQueryServerString, screen, name);
1386 }
1387
1388 return *str;
1389 }
1390
1391 void __glXClientInfo ( Display *dpy, int opcode )
1392 {
1393 xGLXClientInfoReq *req;
1394 int size;
1395 char * ext_str = __glXGetClientGLExtensionString();
1396
1397 /* Send the glXClientInfo request */
1398 LockDisplay(dpy);
1399 GetReq(GLXClientInfo,req);
1400 req->reqType = opcode;
1401 req->glxCode = X_GLXClientInfo;
1402 req->major = GLX_MAJOR_VERSION;
1403 req->minor = GLX_MINOR_VERSION;
1404
1405 size = strlen( ext_str ) + 1;
1406 req->length += (size + 3) >> 2;
1407 req->numbytes = size;
1408 Data(dpy, ext_str, size);
1409
1410 UnlockDisplay(dpy);
1411 SyncHandle();
1412
1413 Xfree( ext_str );
1414 }
1415
1416
1417 /*
1418 ** EXT_import_context
1419 */
1420
1421 PUBLIC Display *glXGetCurrentDisplay(void)
1422 {
1423 GLXContext gc = __glXGetCurrentContext();
1424 if (NULL == gc) return NULL;
1425 return gc->currentDpy;
1426 }
1427
1428 PUBLIC GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1429 glXGetCurrentDisplay)
1430
1431 /**
1432 * Used internally by libGL to send \c xGLXQueryContextinfoExtReq requests
1433 * to the X-server.
1434 *
1435 * \param dpy Display where \c ctx was created.
1436 * \param ctx Context to query.
1437 * \returns \c Success on success. \c GLX_BAD_CONTEXT if \c ctx is invalid,
1438 * or zero if the request failed due to internal problems (i.e.,
1439 * unable to allocate temporary memory, etc.)
1440 *
1441 * \note
1442 * This function dynamically determines whether to use the EXT_import_context
1443 * version of the protocol or the GLX 1.3 version of the protocol.
1444 */
1445 static int __glXQueryContextInfo(Display *dpy, GLXContext ctx)
1446 {
1447 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1448 xGLXQueryContextReply reply;
1449 CARD8 opcode;
1450 GLuint numValues;
1451 int retval;
1452
1453 if (ctx == NULL) {
1454 return GLX_BAD_CONTEXT;
1455 }
1456 opcode = __glXSetupForCommand(dpy);
1457 if (!opcode) {
1458 return 0;
1459 }
1460
1461 /* Send the glXQueryContextInfoEXT request */
1462 LockDisplay(dpy);
1463
1464 if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
1465 xGLXQueryContextReq *req;
1466
1467 GetReq(GLXQueryContext, req);
1468
1469 req->reqType = opcode;
1470 req->glxCode = X_GLXQueryContext;
1471 req->context = (unsigned int)(ctx->xid);
1472 }
1473 else {
1474 xGLXVendorPrivateReq *vpreq;
1475 xGLXQueryContextInfoEXTReq *req;
1476
1477 GetReqExtra( GLXVendorPrivate,
1478 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1479 vpreq );
1480 req = (xGLXQueryContextInfoEXTReq *)vpreq;
1481 req->reqType = opcode;
1482 req->glxCode = X_GLXVendorPrivateWithReply;
1483 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1484 req->context = (unsigned int)(ctx->xid);
1485 }
1486
1487 _XReply(dpy, (xReply*) &reply, 0, False);
1488
1489 numValues = reply.n;
1490 if (numValues == 0)
1491 retval = Success;
1492 else if (numValues > __GLX_MAX_CONTEXT_PROPS)
1493 retval = 0;
1494 else
1495 {
1496 int *propList, *pProp;
1497 int nPropListBytes;
1498 int i;
1499
1500 nPropListBytes = numValues << 3;
1501 propList = (int *) Xmalloc(nPropListBytes);
1502 if (NULL == propList) {
1503 retval = 0;
1504 } else {
1505 _XRead(dpy, (char *)propList, nPropListBytes);
1506 pProp = propList;
1507 for (i=0; i < numValues; i++) {
1508 switch (*pProp++) {
1509 case GLX_SHARE_CONTEXT_EXT:
1510 ctx->share_xid = *pProp++;
1511 break;
1512 case GLX_VISUAL_ID_EXT:
1513 ctx->vid = *pProp++;
1514 break;
1515 case GLX_SCREEN:
1516 ctx->screen = *pProp++;
1517 break;
1518 case GLX_FBCONFIG_ID:
1519 ctx->fbconfigID = *pProp++;
1520 break;
1521 case GLX_RENDER_TYPE:
1522 ctx->renderType = *pProp++;
1523 break;
1524 default:
1525 pProp++;
1526 continue;
1527 }
1528 }
1529 Xfree((char *)propList);
1530 retval = Success;
1531 }
1532 }
1533 UnlockDisplay(dpy);
1534 SyncHandle();
1535 return retval;
1536 }
1537
1538 PUBLIC int
1539 glXQueryContext(Display *dpy, GLXContext ctx, int attribute, int *value)
1540 {
1541 int retVal;
1542
1543 /* get the information from the server if we don't have it already */
1544 if (!ctx->isDirect && (ctx->vid == None)) {
1545 retVal = __glXQueryContextInfo(dpy, ctx);
1546 if (Success != retVal) return retVal;
1547 }
1548 switch (attribute) {
1549 case GLX_SHARE_CONTEXT_EXT:
1550 *value = (int)(ctx->share_xid);
1551 break;
1552 case GLX_VISUAL_ID_EXT:
1553 *value = (int)(ctx->vid);
1554 break;
1555 case GLX_SCREEN:
1556 *value = (int)(ctx->screen);
1557 break;
1558 case GLX_FBCONFIG_ID:
1559 *value = (int)(ctx->fbconfigID);
1560 break;
1561 case GLX_RENDER_TYPE:
1562 *value = (int)(ctx->renderType);
1563 break;
1564 default:
1565 return GLX_BAD_ATTRIBUTE;
1566 }
1567 return Success;
1568 }
1569
1570 PUBLIC GLX_ALIAS( int, glXQueryContextInfoEXT,
1571 (Display *dpy, GLXContext ctx, int attribute, int *value),
1572 (dpy, ctx, attribute, value),
1573 glXQueryContext )
1574
1575 PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx)
1576 {
1577 return ctx->xid;
1578 }
1579
1580 PUBLIC GLXContext glXImportContextEXT(Display *dpy, GLXContextID contextID)
1581 {
1582 GLXContext ctx;
1583
1584 if (contextID == None) {
1585 return NULL;
1586 }
1587 if (__glXIsDirect(dpy, contextID)) {
1588 return NULL;
1589 }
1590
1591 ctx = CreateContext(dpy, NULL, NULL, NULL, False, contextID, False, 0);
1592 if (NULL != ctx) {
1593 if (Success != __glXQueryContextInfo(dpy, ctx)) {
1594 return NULL;
1595 }
1596 }
1597 return ctx;
1598 }
1599
1600 PUBLIC void glXFreeContextEXT(Display *dpy, GLXContext ctx)
1601 {
1602 DestroyContext(dpy, ctx);
1603 }
1604
1605
1606
1607 /*
1608 * GLX 1.3 functions - these are just stubs for now!
1609 */
1610
1611 PUBLIC GLXFBConfig *glXChooseFBConfig(Display *dpy, int screen,
1612 const int *attribList, int *nitems)
1613 {
1614 __GLcontextModes ** config_list;
1615 int list_size;
1616
1617
1618 config_list = (__GLcontextModes **)
1619 glXGetFBConfigs( dpy, screen, & list_size );
1620
1621 if ( (config_list != NULL) && (list_size > 0) && (attribList != NULL) ) {
1622 list_size = choose_visual( config_list, list_size, attribList,
1623 GL_TRUE );
1624 if ( list_size == 0 ) {
1625 XFree( config_list );
1626 config_list = NULL;
1627 }
1628 }
1629
1630 *nitems = list_size;
1631 return (GLXFBConfig *) config_list;
1632 }
1633
1634
1635 PUBLIC GLXContext glXCreateNewContext(Display *dpy, GLXFBConfig config,
1636 int renderType, GLXContext shareList,
1637 Bool allowDirect)
1638 {
1639 return CreateContext( dpy, NULL, (__GLcontextModes *) config, shareList,
1640 allowDirect, None, True, renderType );
1641 }
1642
1643
1644 PUBLIC GLXDrawable glXGetCurrentReadDrawable(void)
1645 {
1646 GLXContext gc = __glXGetCurrentContext();
1647 return gc->currentReadable;
1648 }
1649
1650
1651 PUBLIC GLXFBConfig *glXGetFBConfigs(Display *dpy, int screen, int *nelements)
1652 {
1653 __GLXdisplayPrivate *priv = __glXInitialize(dpy);
1654 __GLcontextModes ** config = NULL;
1655 int i;
1656
1657 if ( (priv->screenConfigs != NULL)
1658 && (screen >= 0) && (screen <= ScreenCount(dpy))
1659 && (priv->screenConfigs[screen].configs != NULL)
1660 && (priv->screenConfigs[screen].configs->fbconfigID != GLX_DONT_CARE) ) {
1661 unsigned num_configs = 0;
1662 __GLcontextModes * modes;
1663
1664
1665 for ( modes = priv->screenConfigs[screen].configs
1666 ; modes != NULL
1667 ; modes = modes->next ) {
1668 if ( modes->fbconfigID != GLX_DONT_CARE ) {
1669 num_configs++;
1670 }
1671 }
1672
1673 config = (__GLcontextModes **) Xmalloc( sizeof(__GLcontextModes *)
1674 * num_configs );
1675 if ( config != NULL ) {
1676 *nelements = num_configs;
1677 i = 0;
1678 for ( modes = priv->screenConfigs[screen].configs
1679 ; modes != NULL
1680 ; modes = modes->next ) {
1681 config[i] = modes;
1682 i++;
1683 }
1684 }
1685 }
1686 return (GLXFBConfig *) config;
1687 }
1688
1689
1690 PUBLIC int glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1691 int attribute, int *value)
1692 {
1693 __GLcontextModes * const modes = ValidateGLXFBConfig( dpy, config );
1694
1695 return (modes != NULL)
1696 ? _gl_get_context_mode_data( modes, attribute, value )
1697 : GLXBadFBConfig;
1698 }
1699
1700
1701 PUBLIC XVisualInfo *glXGetVisualFromFBConfig(Display *dpy, GLXFBConfig config)
1702 {
1703 XVisualInfo visualTemplate;
1704 __GLcontextModes * fbconfig = (__GLcontextModes *) config;
1705 int count;
1706
1707 /*
1708 ** Get a list of all visuals, return if list is empty
1709 */
1710 visualTemplate.visualid = fbconfig->visualID;
1711 return XGetVisualInfo(dpy,VisualIDMask,&visualTemplate,&count);
1712 }
1713
1714
1715 /*
1716 ** GLX_SGI_swap_control
1717 */
1718 static int __glXSwapIntervalSGI(int interval)
1719 {
1720 xGLXVendorPrivateReq *req;
1721 GLXContext gc = __glXGetCurrentContext();
1722 Display * dpy;
1723 CARD32 * interval_ptr;
1724 CARD8 opcode;
1725
1726 if ( gc == NULL ) {
1727 return GLX_BAD_CONTEXT;
1728 }
1729
1730 if ( interval <= 0 ) {
1731 return GLX_BAD_VALUE;
1732 }
1733
1734 #ifdef __DRI_SWAP_CONTROL
1735 if ( gc->isDirect ) {
1736 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1737 gc->screen );
1738 __DRIdrawable * const pdraw = GetDRIDrawable( gc->currentDpy,
1739 gc->currentDrawable,
1740 NULL );
1741 if (psc->swapControl != NULL && pdraw != NULL) {
1742 psc->swapControl->setSwapInterval(pdraw, interval);
1743 return 0;
1744 }
1745 else {
1746 return GLX_BAD_CONTEXT;
1747 }
1748 }
1749 #endif
1750 dpy = gc->currentDpy;
1751 opcode = __glXSetupForCommand(dpy);
1752 if (!opcode) {
1753 return 0;
1754 }
1755
1756 /* Send the glXSwapIntervalSGI request */
1757 LockDisplay(dpy);
1758 GetReqExtra(GLXVendorPrivate,sizeof(CARD32),req);
1759 req->reqType = opcode;
1760 req->glxCode = X_GLXVendorPrivate;
1761 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1762 req->contextTag = gc->currentContextTag;
1763
1764 interval_ptr = (CARD32 *) (req + 1);
1765 *interval_ptr = interval;
1766
1767 UnlockDisplay(dpy);
1768 SyncHandle();
1769 XFlush(dpy);
1770
1771 return 0;
1772 }
1773
1774
1775 /*
1776 ** GLX_MESA_swap_control
1777 */
1778 static int __glXSwapIntervalMESA(unsigned int interval)
1779 {
1780 #ifdef __DRI_SWAP_CONTROL
1781 GLXContext gc = __glXGetCurrentContext();
1782
1783 if ( interval < 0 ) {
1784 return GLX_BAD_VALUE;
1785 }
1786
1787 if ( (gc != NULL) && gc->isDirect ) {
1788 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1789 gc->screen );
1790
1791 if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
1792 __DRIdrawable * const pdraw =
1793 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1794 if (psc->swapControl != NULL && pdraw != NULL) {
1795 psc->swapControl->setSwapInterval(pdraw, interval);
1796 return 0;
1797 }
1798 }
1799 }
1800 #else
1801 (void) interval;
1802 #endif
1803
1804 return GLX_BAD_CONTEXT;
1805 }
1806
1807
1808 static int __glXGetSwapIntervalMESA(void)
1809 {
1810 #ifdef __DRI_SWAP_CONTROL
1811 GLXContext gc = __glXGetCurrentContext();
1812
1813 if ( (gc != NULL) && gc->isDirect ) {
1814 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1815 gc->screen );
1816
1817 if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
1818 __DRIdrawable * const pdraw =
1819 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1820 if (psc->swapControl != NULL && pdraw != NULL) {
1821 return psc->swapControl->getSwapInterval(pdraw);
1822 }
1823 }
1824 }
1825 #endif
1826
1827 return 0;
1828 }
1829
1830
1831 /*
1832 ** GLX_MESA_swap_frame_usage
1833 */
1834
1835 static GLint __glXBeginFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
1836 {
1837 int status = GLX_BAD_CONTEXT;
1838 #ifdef GLX_DIRECT_RENDERING
1839 int screen;
1840 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1841 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1842
1843 if ( (pdraw != NULL) && (pdraw->frameTracking != NULL)
1844 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1845 status = pdraw->frameTracking(pdraw, GL_TRUE);
1846 }
1847 #else
1848 (void) dpy;
1849 (void) drawable;
1850 #endif
1851 return status;
1852 }
1853
1854
1855 static GLint __glXEndFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
1856 {
1857 int status = GLX_BAD_CONTEXT;
1858 #ifdef GLX_DIRECT_RENDERING
1859 int screen;
1860 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1861 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1862
1863 if ( (pdraw != NULL) && (pdraw->frameTracking != NULL)
1864 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1865 status = pdraw->frameTracking(pdraw, GL_FALSE);
1866 }
1867 #else
1868 (void) dpy;
1869 (void) drawable;
1870 #endif
1871 return status;
1872 }
1873
1874
1875 static GLint __glXGetFrameUsageMESA(Display *dpy, GLXDrawable drawable,
1876 GLfloat *usage)
1877 {
1878 int status = GLX_BAD_CONTEXT;
1879 #ifdef GLX_DIRECT_RENDERING
1880 int screen;
1881 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1882 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1883
1884 if ( (pdraw != NULL ) && (pdraw->queryFrameTracking != NULL)
1885 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1886 int64_t sbc, missedFrames;
1887 float lastMissedUsage;
1888
1889 status = pdraw->queryFrameTracking(pdraw, &sbc, &missedFrames,
1890 &lastMissedUsage, usage);
1891 }
1892 #else
1893 (void) dpy;
1894 (void) drawable;
1895 (void) usage;
1896 #endif
1897 return status;
1898 }
1899
1900
1901 static GLint __glXQueryFrameTrackingMESA(Display *dpy, GLXDrawable drawable,
1902 int64_t *sbc, int64_t *missedFrames,
1903 GLfloat *lastMissedUsage)
1904 {
1905 int status = GLX_BAD_CONTEXT;
1906 #ifdef GLX_DIRECT_RENDERING
1907 int screen;
1908 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1909 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1910
1911 if ( (pdraw != NULL ) && (pdraw->queryFrameTracking != NULL)
1912 && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
1913 float usage;
1914
1915 status = pdraw->queryFrameTracking(pdraw, sbc, missedFrames,
1916 lastMissedUsage, &usage);
1917 }
1918 #else
1919 (void) dpy;
1920 (void) drawable;
1921 (void) sbc;
1922 (void) missedFrames;
1923 (void) lastMissedUsage;
1924 #endif
1925 return status;
1926 }
1927
1928
1929 /*
1930 ** GLX_SGI_video_sync
1931 */
1932 static int __glXGetVideoSyncSGI(unsigned int *count)
1933 {
1934 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1935 * FIXME: there should be a GLX encoding for this call. I can find no
1936 * FIXME: documentation for the GLX encoding.
1937 */
1938 #ifdef GLX_DIRECT_RENDERING
1939 GLXContext gc = __glXGetCurrentContext();
1940
1941
1942 if ( (gc != NULL) && gc->isDirect ) {
1943 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1944 gc->screen );
1945 if ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
1946 && psc->driScreen.private && psc->driScreen.getMSC) {
1947 int ret;
1948 int64_t temp;
1949
1950 ret = psc->driScreen.getMSC(&psc->driScreen, &temp);
1951 *count = (unsigned) temp;
1952 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
1953 }
1954 }
1955 #else
1956 (void) count;
1957 #endif
1958 return GLX_BAD_CONTEXT;
1959 }
1960
1961 static int __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1962 {
1963 #ifdef GLX_DIRECT_RENDERING
1964 GLXContext gc = __glXGetCurrentContext();
1965
1966 if ( divisor <= 0 || remainder < 0 )
1967 return GLX_BAD_VALUE;
1968
1969 if ( (gc != NULL) && gc->isDirect ) {
1970 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1971 gc->screen );
1972 if ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
1973 && psc->driScreen.private ) {
1974 __DRIdrawable * const pdraw =
1975 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1976 if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL) ) {
1977 int ret;
1978 int64_t msc;
1979 int64_t sbc;
1980
1981 ret = (*pdraw->waitForMSC)(pdraw, 0,
1982 divisor, remainder, &msc, &sbc);
1983 *count = (unsigned) msc;
1984 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
1985 }
1986 }
1987 }
1988 #else
1989 (void) count;
1990 #endif
1991 return GLX_BAD_CONTEXT;
1992 }
1993
1994
1995 /*
1996 ** GLX_SGIX_fbconfig
1997 ** Many of these functions are aliased to GLX 1.3 entry points in the
1998 ** GLX_functions table.
1999 */
2000
2001 PUBLIC GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
2002 (Display *dpy, GLXFBConfigSGIX config, int attribute, int *value),
2003 (dpy, config, attribute, value),
2004 glXGetFBConfigAttrib)
2005
2006 PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
2007 (Display *dpy, int screen, int *attrib_list, int *nelements),
2008 (dpy, screen, attrib_list, nelements),
2009 glXChooseFBConfig)
2010
2011 PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
2012 (Display * dpy, GLXFBConfigSGIX config),
2013 (dpy, config),
2014 glXGetVisualFromFBConfig)
2015
2016 PUBLIC GLXPixmap glXCreateGLXPixmapWithConfigSGIX(Display *dpy,
2017 GLXFBConfigSGIX config, Pixmap pixmap)
2018 {
2019 xGLXVendorPrivateWithReplyReq *vpreq;
2020 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
2021 GLXPixmap xid = None;
2022 CARD8 opcode;
2023 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2024 __GLXscreenConfigs * psc;
2025
2026
2027 if ( (dpy == NULL) || (config == NULL) ) {
2028 return None;
2029 }
2030
2031 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2032 if ( (psc != NULL)
2033 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2034 opcode = __glXSetupForCommand(dpy);
2035 if (!opcode) {
2036 return None;
2037 }
2038
2039 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
2040 LockDisplay(dpy);
2041 GetReqExtra(GLXVendorPrivateWithReply,
2042 sz_xGLXCreateGLXPixmapWithConfigSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
2043 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *)vpreq;
2044 req->reqType = opcode;
2045 req->glxCode = X_GLXVendorPrivateWithReply;
2046 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
2047 req->screen = fbconfig->screen;
2048 req->fbconfig = fbconfig->fbconfigID;
2049 req->pixmap = pixmap;
2050 req->glxpixmap = xid = XAllocID(dpy);
2051 UnlockDisplay(dpy);
2052 SyncHandle();
2053 }
2054
2055 return xid;
2056 }
2057
2058 PUBLIC GLXContext glXCreateContextWithConfigSGIX(Display *dpy,
2059 GLXFBConfigSGIX config, int renderType,
2060 GLXContext shareList, Bool allowDirect)
2061 {
2062 GLXContext gc = NULL;
2063 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2064 __GLXscreenConfigs * psc;
2065
2066
2067 if ( (dpy == NULL) || (config == NULL) ) {
2068 return None;
2069 }
2070
2071 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2072 if ( (psc != NULL)
2073 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2074 gc = CreateContext( dpy, NULL, (__GLcontextModes *) config, shareList,
2075 allowDirect, None, False, renderType );
2076 }
2077
2078 return gc;
2079 }
2080
2081
2082 PUBLIC GLXFBConfigSGIX glXGetFBConfigFromVisualSGIX(Display *dpy,
2083 XVisualInfo *vis)
2084 {
2085 __GLXdisplayPrivate *priv;
2086 __GLXscreenConfigs *psc;
2087
2088 if ( (GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc ) != Success)
2089 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit )
2090 && (psc->configs->fbconfigID != GLX_DONT_CARE) ) {
2091 return (GLXFBConfigSGIX) _gl_context_modes_find_visual( psc->configs,
2092 vis->visualid );
2093 }
2094
2095 return NULL;
2096 }
2097
2098
2099 /*
2100 ** GLX_SGIX_swap_group
2101 */
2102 static void __glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable,
2103 GLXDrawable member)
2104 {
2105 (void) dpy;
2106 (void) drawable;
2107 (void) member;
2108 }
2109
2110
2111 /*
2112 ** GLX_SGIX_swap_barrier
2113 */
2114 static void __glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable,
2115 int barrier)
2116 {
2117 (void) dpy;
2118 (void) drawable;
2119 (void) barrier;
2120 }
2121
2122 static Bool __glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2123 {
2124 (void) dpy;
2125 (void) screen;
2126 (void) max;
2127 return False;
2128 }
2129
2130
2131 /*
2132 ** GLX_OML_sync_control
2133 */
2134 static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
2135 int64_t *ust, int64_t *msc, int64_t *sbc)
2136 {
2137 #ifdef GLX_DIRECT_RENDERING
2138 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2139
2140 if ( priv != NULL ) {
2141 int i;
2142 __DRIdrawable * const pdraw = GetDRIDrawable( dpy, drawable, & i );
2143 __GLXscreenConfigs * const psc = &priv->screenConfigs[i];
2144
2145 assert( (pdraw == NULL) || (i != -1) );
2146 return ( (pdraw && pdraw->getSBC && psc->driScreen.getMSC)
2147 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )
2148 && ((*psc->driScreen.getMSC)(&psc->driScreen, msc) == 0)
2149 && ((*pdraw->getSBC)(pdraw, sbc ) == 0)
2150 && (__glXGetUST( ust ) == 0) );
2151 }
2152 #else
2153 (void) dpy;
2154 (void) drawable;
2155 (void) ust;
2156 (void) msc;
2157 (void) sbc;
2158 #endif
2159 return False;
2160 }
2161
2162
2163 /**
2164 * Determine the refresh rate of the specified drawable and display.
2165 *
2166 * \param dpy Display whose refresh rate is to be determined.
2167 * \param drawable Drawable whose refresh rate is to be determined.
2168 * \param numerator Numerator of the refresh rate.
2169 * \param demoninator Denominator of the refresh rate.
2170 * \return If the refresh rate for the specified display and drawable could
2171 * be calculated, True is returned. Otherwise False is returned.
2172 *
2173 * \note This function is implemented entirely client-side. A lot of other
2174 * functionality is required to export GLX_OML_sync_control, so on
2175 * XFree86 this function can be called for direct-rendering contexts
2176 * when GLX_OML_sync_control appears in the client extension string.
2177 */
2178
2179 GLboolean __glXGetMscRateOML(__DRIdrawable *draw,
2180 int32_t * numerator, int32_t * denominator)
2181 {
2182 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2183 __GLXdrawable *glxDraw =
2184 containerOf(draw, __GLXdrawable, driDrawable);
2185 __GLXscreenConfigs *psc = glxDraw->psc;
2186 Display *dpy = psc->dpy;
2187 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2188
2189
2190 if ( priv != NULL ) {
2191 XF86VidModeModeLine mode_line;
2192 int dot_clock;
2193 int i;
2194
2195
2196 if (XF86VidModeQueryVersion( dpy, & i, & i ) &&
2197 XF86VidModeGetModeLine(dpy, psc->scr, &dot_clock, &mode_line) ) {
2198 unsigned n = dot_clock * 1000;
2199 unsigned d = mode_line.vtotal * mode_line.htotal;
2200
2201 # define V_INTERLACE 0x010
2202 # define V_DBLSCAN 0x020
2203
2204 if ( (mode_line.flags & V_INTERLACE) ) {
2205 n *= 2;
2206 }
2207 else if ( (mode_line.flags & V_DBLSCAN) ) {
2208 d *= 2;
2209 }
2210
2211 /* The OML_sync_control spec requires that if the refresh rate is a
2212 * whole number, that the returned numerator be equal to the refresh
2213 * rate and the denominator be 1.
2214 */
2215
2216 if ( (n % d) == 0 ) {
2217 n /= d;
2218 d = 1;
2219 }
2220 else {
2221 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2222
2223
2224 /* This is a poor man's way to reduce a fraction. It's far from
2225 * perfect, but it will work well enough for this situation.
2226 */
2227
2228 for ( i = 0 ; f[i] != 0 ; i++ ) {
2229 while ( ((n % f[i]) == 0) && ((d % f[i]) == 0) ) {
2230 d /= f[i];
2231 n /= f[i];
2232 }
2233 }
2234 }
2235
2236 *numerator = n;
2237 *denominator = d;
2238
2239 return True;
2240 }
2241 }
2242 #else
2243 (void) draw;
2244 (void) numerator;
2245 (void) denominator;
2246 #endif
2247 return False;
2248 }
2249
2250
2251 static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
2252 int64_t target_msc, int64_t divisor,
2253 int64_t remainder)
2254 {
2255 #ifdef GLX_DIRECT_RENDERING
2256 int screen;
2257 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2258 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2259
2260 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2261 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2262 * of -1 if the function failed because of errors detected in the input
2263 * parameters"
2264 */
2265 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2266 return -1;
2267 if ( divisor > 0 && remainder >= divisor )
2268 return -1;
2269
2270 if ( (pdraw != NULL) && (pdraw->swapBuffersMSC != NULL)
2271 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
2272 return (*pdraw->swapBuffersMSC)(pdraw, target_msc, divisor, remainder);
2273 }
2274 #else
2275 (void) dpy;
2276 (void) drawable;
2277 (void) target_msc;
2278 (void) divisor;
2279 (void) remainder;
2280 #endif
2281 return 0;
2282 }
2283
2284
2285 static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2286 int64_t target_msc, int64_t divisor,
2287 int64_t remainder, int64_t *ust,
2288 int64_t *msc, int64_t *sbc)
2289 {
2290 #ifdef GLX_DIRECT_RENDERING
2291 int screen;
2292 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2293 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2294 int ret;
2295
2296 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2297 * error", but the return type in the spec is Bool.
2298 */
2299 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2300 return False;
2301 if ( divisor > 0 && remainder >= divisor )
2302 return False;
2303
2304 if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL)
2305 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
2306 ret = (*pdraw->waitForMSC)(pdraw, target_msc,
2307 divisor, remainder, msc, sbc);
2308
2309 /* __glXGetUST returns zero on success and non-zero on failure.
2310 * This function returns True on success and False on failure.
2311 */
2312 return ( (ret == 0) && (__glXGetUST( ust ) == 0) );
2313 }
2314 #else
2315 (void) dpy;
2316 (void) drawable;
2317 (void) target_msc;
2318 (void) divisor;
2319 (void) remainder;
2320 (void) ust;
2321 (void) msc;
2322 (void) sbc;
2323 #endif
2324 return False;
2325 }
2326
2327
2328 static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2329 int64_t target_sbc, int64_t *ust,
2330 int64_t *msc, int64_t *sbc )
2331 {
2332 #ifdef GLX_DIRECT_RENDERING
2333 int screen;
2334 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2335 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2336 int ret;
2337
2338 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2339 * error", but the return type in the spec is Bool.
2340 */
2341 if ( target_sbc < 0 )
2342 return False;
2343
2344 if ( (pdraw != NULL) && (pdraw->waitForSBC != NULL)
2345 && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )) {
2346 ret = (*pdraw->waitForSBC)(pdraw, target_sbc, msc, sbc);
2347
2348 /* __glXGetUST returns zero on success and non-zero on failure.
2349 * This function returns True on success and False on failure.
2350 */
2351 return( (ret == 0) && (__glXGetUST( ust ) == 0) );
2352 }
2353 #else
2354 (void) dpy;
2355 (void) drawable;
2356 (void) target_sbc;
2357 (void) ust;
2358 (void) msc;
2359 (void) sbc;
2360 #endif
2361 return False;
2362 }
2363
2364
2365 /**
2366 * GLX_MESA_allocate_memory
2367 */
2368 /*@{*/
2369
2370 PUBLIC void *glXAllocateMemoryMESA(Display *dpy, int scrn,
2371 size_t size, float readFreq,
2372 float writeFreq, float priority)
2373 {
2374 #ifdef GLX_DIRECT_RENDERING
2375 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2376
2377 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2378 if (psc && psc->driScreen.private && psc->driScreen.allocateMemory) {
2379 return (*psc->driScreen.allocateMemory)( &psc->driScreen, size,
2380 readFreq, writeFreq,
2381 priority );
2382 }
2383 }
2384 #else
2385 (void) dpy;
2386 (void) scrn;
2387 (void) size;
2388 (void) readFreq;
2389 (void) writeFreq;
2390 (void) priority;
2391 #endif /* GLX_DIRECT_RENDERING */
2392
2393 return NULL;
2394 }
2395
2396
2397 PUBLIC void glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
2398 {
2399 #ifdef GLX_DIRECT_RENDERING
2400 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2401
2402 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2403 if (psc && psc->driScreen.private && psc->driScreen.freeMemory) {
2404 (*psc->driScreen.freeMemory)( &psc->driScreen, pointer );
2405 }
2406 }
2407 #else
2408 (void) dpy;
2409 (void) scrn;
2410 (void) pointer;
2411 #endif /* GLX_DIRECT_RENDERING */
2412 }
2413
2414
2415 PUBLIC GLuint glXGetMemoryOffsetMESA( Display *dpy, int scrn,
2416 const void *pointer )
2417 {
2418 #ifdef GLX_DIRECT_RENDERING
2419 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2420
2421 if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
2422 if (psc && psc->driScreen.private && psc->driScreen.memoryOffset) {
2423 return (*psc->driScreen.memoryOffset)( &psc->driScreen, pointer );
2424 }
2425 }
2426 #else
2427 (void) dpy;
2428 (void) scrn;
2429 (void) pointer;
2430 #endif /* GLX_DIRECT_RENDERING */
2431
2432 return ~0L;
2433 }
2434 /*@}*/
2435
2436
2437 /**
2438 * Mesa extension stubs. These will help reduce portability problems.
2439 */
2440 /*@{*/
2441
2442 /**
2443 * Release all buffers associated with the specified GLX drawable.
2444 *
2445 * \todo
2446 * This function was intended for stand-alone Mesa. The issue there is that
2447 * the library doesn't get any notification when a window is closed. In
2448 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2449 * supported, there are 3 different functions to destroy a drawable. It
2450 * should be possible to create GLX protocol (or have it determine which
2451 * protocol to use based on the type of the drawable) to have one function
2452 * do the work of 3. For the direct-rendering case, this function could
2453 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2454 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2455 * would need to be used. This really should be done as part of the new DRI
2456 * interface work.
2457 *
2458 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2459 * __driGarbageCollectDrawables
2460 * glXDestroyGLXPixmap
2461 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2462 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2463 */
2464 static Bool __glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2465 {
2466 (void) dpy;
2467 (void) d;
2468 return False;
2469 }
2470
2471
2472 PUBLIC GLXPixmap glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visual,
2473 Pixmap pixmap, Colormap cmap )
2474 {
2475 (void) dpy;
2476 (void) visual;
2477 (void) pixmap;
2478 (void) cmap;
2479 return 0;
2480 }
2481 /*@}*/
2482
2483
2484 /**
2485 * GLX_MESA_copy_sub_buffer
2486 */
2487 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2488 static void __glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
2489 int x, int y, int width, int height)
2490 {
2491 xGLXVendorPrivateReq *req;
2492 GLXContext gc;
2493 GLXContextTag tag;
2494 CARD32 *drawable_ptr;
2495 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2496 CARD8 opcode;
2497
2498 #ifdef __DRI_COPY_SUB_BUFFER
2499 int screen;
2500 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2501 if ( pdraw != NULL ) {
2502 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2503 if (psc->copySubBuffer != NULL) {
2504 (*psc->copySubBuffer->copySubBuffer)(pdraw, x, y, width, height);
2505 }
2506
2507 return;
2508 }
2509 #endif
2510
2511 opcode = __glXSetupForCommand(dpy);
2512 if (!opcode)
2513 return;
2514
2515 /*
2516 ** The calling thread may or may not have a current context. If it
2517 ** does, send the context tag so the server can do a flush.
2518 */
2519 gc = __glXGetCurrentContext();
2520 if ((gc != NULL) && (dpy == gc->currentDpy) &&
2521 ((drawable == gc->currentDrawable) ||
2522 (drawable == gc->currentReadable)) ) {
2523 tag = gc->currentContextTag;
2524 } else {
2525 tag = 0;
2526 }
2527
2528 LockDisplay(dpy);
2529 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4,req);
2530 req->reqType = opcode;
2531 req->glxCode = X_GLXVendorPrivate;
2532 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2533 req->contextTag = tag;
2534
2535 drawable_ptr = (CARD32 *) (req + 1);
2536 x_ptr = (INT32 *) (drawable_ptr + 1);
2537 y_ptr = (INT32 *) (drawable_ptr + 2);
2538 w_ptr = (INT32 *) (drawable_ptr + 3);
2539 h_ptr = (INT32 *) (drawable_ptr + 4);
2540
2541 *drawable_ptr = drawable;
2542 *x_ptr = x;
2543 *y_ptr = y;
2544 *w_ptr = width;
2545 *h_ptr = height;
2546
2547 UnlockDisplay(dpy);
2548 SyncHandle();
2549 }
2550
2551
2552 /**
2553 * GLX_EXT_texture_from_pixmap
2554 */
2555 /*@{*/
2556 static void __glXBindTexImageEXT(Display *dpy,
2557 GLXDrawable drawable,
2558 int buffer,
2559 const int *attrib_list)
2560 {
2561 xGLXVendorPrivateReq *req;
2562 GLXContext gc = __glXGetCurrentContext();
2563 CARD32 *drawable_ptr;
2564 INT32 *buffer_ptr;
2565 CARD32 *num_attrib_ptr;
2566 CARD32 *attrib_ptr;
2567 CARD8 opcode;
2568 unsigned int i;
2569
2570 if (gc == NULL)
2571 return;
2572
2573 i = 0;
2574 if (attrib_list) {
2575 while (attrib_list[i * 2] != None)
2576 i++;
2577 }
2578
2579 #ifdef GLX_DIRECT_RENDERING
2580 if (gc->isDirect)
2581 return;
2582 #endif
2583
2584 opcode = __glXSetupForCommand(dpy);
2585 if (!opcode)
2586 return;
2587
2588 LockDisplay(dpy);
2589 GetReqExtra(GLXVendorPrivate, 12 + 8 * i,req);
2590 req->reqType = opcode;
2591 req->glxCode = X_GLXVendorPrivate;
2592 req->vendorCode = X_GLXvop_BindTexImageEXT;
2593 req->contextTag = gc->currentContextTag;
2594
2595 drawable_ptr = (CARD32 *) (req + 1);
2596 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2597 num_attrib_ptr = (CARD32 *) (buffer_ptr + 1);
2598 attrib_ptr = (CARD32 *) (num_attrib_ptr + 1);
2599
2600 *drawable_ptr = drawable;
2601 *buffer_ptr = buffer;
2602 *num_attrib_ptr = (CARD32) i;
2603
2604 i = 0;
2605 if (attrib_list) {
2606 while (attrib_list[i * 2] != None)
2607 {
2608 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 0];
2609 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 1];
2610 i++;
2611 }
2612 }
2613
2614 UnlockDisplay(dpy);
2615 SyncHandle();
2616 }
2617
2618 static void __glXReleaseTexImageEXT(Display *dpy,
2619 GLXDrawable drawable,
2620 int buffer)
2621 {
2622 xGLXVendorPrivateReq *req;
2623 GLXContext gc = __glXGetCurrentContext();
2624 CARD32 *drawable_ptr;
2625 INT32 *buffer_ptr;
2626 CARD8 opcode;
2627
2628 if (gc == NULL)
2629 return;
2630
2631 #ifdef GLX_DIRECT_RENDERING
2632 if (gc->isDirect)
2633 return;
2634 #endif
2635
2636 opcode = __glXSetupForCommand(dpy);
2637 if (!opcode)
2638 return;
2639
2640 LockDisplay(dpy);
2641 GetReqExtra(GLXVendorPrivate, sizeof(CARD32)+sizeof(INT32),req);
2642 req->reqType = opcode;
2643 req->glxCode = X_GLXVendorPrivate;
2644 req->vendorCode = X_GLXvop_ReleaseTexImageEXT;
2645 req->contextTag = gc->currentContextTag;
2646
2647 drawable_ptr = (CARD32 *) (req + 1);
2648 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2649
2650 *drawable_ptr = drawable;
2651 *buffer_ptr = buffer;
2652
2653 UnlockDisplay(dpy);
2654 SyncHandle();
2655 }
2656 /*@}*/
2657
2658 /**
2659 * \c strdup is actually not a standard ANSI C or POSIX routine.
2660 * Irix will not define it if ANSI mode is in effect.
2661 *
2662 * \sa strdup
2663 */
2664 char *
2665 __glXstrdup(const char *str)
2666 {
2667 char *copy;
2668 copy = (char *) Xmalloc(strlen(str) + 1);
2669 if (!copy)
2670 return NULL;
2671 strcpy(copy, str);
2672 return copy;
2673 }
2674
2675 /*
2676 ** glXGetProcAddress support
2677 */
2678
2679 struct name_address_pair {
2680 const char *Name;
2681 GLvoid *Address;
2682 };
2683
2684 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2685 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2686
2687 static const struct name_address_pair GLX_functions[] = {
2688 /*** GLX_VERSION_1_0 ***/
2689 GLX_FUNCTION( glXChooseVisual ),
2690 GLX_FUNCTION( glXCopyContext ),
2691 GLX_FUNCTION( glXCreateContext ),
2692 GLX_FUNCTION( glXCreateGLXPixmap ),
2693 GLX_FUNCTION( glXDestroyContext ),
2694 GLX_FUNCTION( glXDestroyGLXPixmap ),
2695 GLX_FUNCTION( glXGetConfig ),
2696 GLX_FUNCTION( glXGetCurrentContext ),
2697 GLX_FUNCTION( glXGetCurrentDrawable ),
2698 GLX_FUNCTION( glXIsDirect ),
2699 GLX_FUNCTION( glXMakeCurrent ),
2700 GLX_FUNCTION( glXQueryExtension ),
2701 GLX_FUNCTION( glXQueryVersion ),
2702 GLX_FUNCTION( glXSwapBuffers ),
2703 GLX_FUNCTION( glXUseXFont ),
2704 GLX_FUNCTION( glXWaitGL ),
2705 GLX_FUNCTION( glXWaitX ),
2706
2707 /*** GLX_VERSION_1_1 ***/
2708 GLX_FUNCTION( glXGetClientString ),
2709 GLX_FUNCTION( glXQueryExtensionsString ),
2710 GLX_FUNCTION( glXQueryServerString ),
2711
2712 /*** GLX_VERSION_1_2 ***/
2713 GLX_FUNCTION( glXGetCurrentDisplay ),
2714
2715 /*** GLX_VERSION_1_3 ***/
2716 GLX_FUNCTION( glXChooseFBConfig ),
2717 GLX_FUNCTION( glXCreateNewContext ),
2718 GLX_FUNCTION( glXCreatePbuffer ),
2719 GLX_FUNCTION( glXCreatePixmap ),
2720 GLX_FUNCTION( glXCreateWindow ),
2721 GLX_FUNCTION( glXDestroyPbuffer ),
2722 GLX_FUNCTION( glXDestroyPixmap ),
2723 GLX_FUNCTION( glXDestroyWindow ),
2724 GLX_FUNCTION( glXGetCurrentReadDrawable ),
2725 GLX_FUNCTION( glXGetFBConfigAttrib ),
2726 GLX_FUNCTION( glXGetFBConfigs ),
2727 GLX_FUNCTION( glXGetSelectedEvent ),
2728 GLX_FUNCTION( glXGetVisualFromFBConfig ),
2729 GLX_FUNCTION( glXMakeContextCurrent ),
2730 GLX_FUNCTION( glXQueryContext ),
2731 GLX_FUNCTION( glXQueryDrawable ),
2732 GLX_FUNCTION( glXSelectEvent ),
2733
2734 /*** GLX_SGI_swap_control ***/
2735 GLX_FUNCTION2( glXSwapIntervalSGI, __glXSwapIntervalSGI ),
2736
2737 /*** GLX_SGI_video_sync ***/
2738 GLX_FUNCTION2( glXGetVideoSyncSGI, __glXGetVideoSyncSGI ),
2739 GLX_FUNCTION2( glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI ),
2740
2741 /*** GLX_SGI_make_current_read ***/
2742 GLX_FUNCTION2( glXMakeCurrentReadSGI, glXMakeContextCurrent ),
2743 GLX_FUNCTION2( glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable ),
2744
2745 /*** GLX_EXT_import_context ***/
2746 GLX_FUNCTION( glXFreeContextEXT ),
2747 GLX_FUNCTION( glXGetContextIDEXT ),
2748 GLX_FUNCTION2( glXGetCurrentDisplayEXT, glXGetCurrentDisplay ),
2749 GLX_FUNCTION( glXImportContextEXT ),
2750 GLX_FUNCTION2( glXQueryContextInfoEXT, glXQueryContext ),
2751
2752 /*** GLX_SGIX_fbconfig ***/
2753 GLX_FUNCTION2( glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib ),
2754 GLX_FUNCTION2( glXChooseFBConfigSGIX, glXChooseFBConfig ),
2755 GLX_FUNCTION( glXCreateGLXPixmapWithConfigSGIX ),
2756 GLX_FUNCTION( glXCreateContextWithConfigSGIX ),
2757 GLX_FUNCTION2( glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig ),
2758 GLX_FUNCTION( glXGetFBConfigFromVisualSGIX ),
2759
2760 /*** GLX_SGIX_pbuffer ***/
2761 GLX_FUNCTION( glXCreateGLXPbufferSGIX ),
2762 GLX_FUNCTION( glXDestroyGLXPbufferSGIX ),
2763 GLX_FUNCTION( glXQueryGLXPbufferSGIX ),
2764 GLX_FUNCTION( glXSelectEventSGIX ),
2765 GLX_FUNCTION( glXGetSelectedEventSGIX ),
2766
2767 /*** GLX_SGIX_swap_group ***/
2768 GLX_FUNCTION2( glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX ),
2769
2770 /*** GLX_SGIX_swap_barrier ***/
2771 GLX_FUNCTION2( glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX ),
2772 GLX_FUNCTION2( glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX ),
2773
2774 /*** GLX_MESA_allocate_memory ***/
2775 GLX_FUNCTION( glXAllocateMemoryMESA ),
2776 GLX_FUNCTION( glXFreeMemoryMESA ),
2777 GLX_FUNCTION( glXGetMemoryOffsetMESA ),
2778
2779 /*** GLX_MESA_copy_sub_buffer ***/
2780 GLX_FUNCTION2( glXCopySubBufferMESA, __glXCopySubBufferMESA ),
2781
2782 /*** GLX_MESA_pixmap_colormap ***/
2783 GLX_FUNCTION( glXCreateGLXPixmapMESA ),
2784
2785 /*** GLX_MESA_release_buffers ***/
2786 GLX_FUNCTION2( glXReleaseBuffersMESA, __glXReleaseBuffersMESA ),
2787
2788 /*** GLX_MESA_swap_control ***/
2789 GLX_FUNCTION2( glXSwapIntervalMESA, __glXSwapIntervalMESA ),
2790 GLX_FUNCTION2( glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA ),
2791
2792 /*** GLX_MESA_swap_frame_usage ***/
2793 GLX_FUNCTION2( glXBeginFrameTrackingMESA, __glXBeginFrameTrackingMESA ),
2794 GLX_FUNCTION2( glXEndFrameTrackingMESA, __glXEndFrameTrackingMESA ),
2795 GLX_FUNCTION2( glXGetFrameUsageMESA, __glXGetFrameUsageMESA ),
2796 GLX_FUNCTION2( glXQueryFrameTrackingMESA, __glXQueryFrameTrackingMESA ),
2797
2798 /*** GLX_ARB_get_proc_address ***/
2799 GLX_FUNCTION( glXGetProcAddressARB ),
2800
2801 /*** GLX 1.4 ***/
2802 GLX_FUNCTION2( glXGetProcAddress, glXGetProcAddressARB ),
2803
2804 /*** GLX_OML_sync_control ***/
2805 GLX_FUNCTION2( glXWaitForSbcOML, __glXWaitForSbcOML ),
2806 GLX_FUNCTION2( glXWaitForMscOML, __glXWaitForMscOML ),
2807 GLX_FUNCTION2( glXSwapBuffersMscOML, __glXSwapBuffersMscOML ),
2808 GLX_FUNCTION2( glXGetMscRateOML, __glXGetMscRateOML ),
2809 GLX_FUNCTION2( glXGetSyncValuesOML, __glXGetSyncValuesOML ),
2810
2811 /*** GLX_EXT_texture_from_pixmap ***/
2812 GLX_FUNCTION2( glXBindTexImageEXT, __glXBindTexImageEXT ),
2813 GLX_FUNCTION2( glXReleaseTexImageEXT, __glXReleaseTexImageEXT ),
2814
2815 #ifdef GLX_DIRECT_RENDERING
2816 /*** DRI configuration ***/
2817 GLX_FUNCTION( glXGetScreenDriver ),
2818 GLX_FUNCTION( glXGetDriverConfig ),
2819 #endif
2820
2821 { NULL, NULL } /* end of list */
2822 };
2823
2824
2825 static const GLvoid *
2826 get_glx_proc_address(const char *funcName)
2827 {
2828 GLuint i;
2829
2830 /* try static functions */
2831 for (i = 0; GLX_functions[i].Name; i++) {
2832 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2833 return GLX_functions[i].Address;
2834 }
2835
2836 return NULL;
2837 }
2838
2839
2840 /**
2841 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2842 * \c glXGetProcAddress.
2843 *
2844 * \param procName Name of a GL or GLX function.
2845 * \returns A pointer to the named function
2846 *
2847 * \sa glXGetProcAddress
2848 */
2849 PUBLIC void (*glXGetProcAddressARB(const GLubyte *procName))( void )
2850 {
2851 typedef void (*gl_function)( void );
2852 gl_function f;
2853
2854
2855 /* Search the table of GLX and internal functions first. If that
2856 * fails and the supplied name could be a valid core GL name, try
2857 * searching the core GL function table. This check is done to prevent
2858 * DRI based drivers from searching the core GL function table for
2859 * internal API functions.
2860 */
2861
2862 f = (gl_function) get_glx_proc_address((const char *) procName);
2863 if ( (f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2864 && (procName[2] != 'X') ) {
2865 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2866 }
2867
2868 return f;
2869 }
2870
2871 /**
2872 * Get the address of a named GL function. This is the GLX 1.4 name for
2873 * \c glXGetProcAddressARB.
2874 *
2875 * \param procName Name of a GL or GLX function.
2876 * \returns A pointer to the named function
2877 *
2878 * \sa glXGetProcAddressARB
2879 */
2880 PUBLIC void (*glXGetProcAddress(const GLubyte *procName))( void )
2881 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
2882 __attribute__ ((alias ("glXGetProcAddressARB")));
2883 #else
2884 {
2885 return glXGetProcAddressARB(procName);
2886 }
2887 #endif /* __GNUC__ */
2888
2889
2890 #ifdef GLX_DIRECT_RENDERING
2891 /**
2892 * Retrieves the verion of the internal libGL API in YYYYMMDD format. This
2893 * might be used by the DRI drivers to determine how new libGL is at runtime.
2894 * Drivers should not call this function directly. They should instead use
2895 * \c glXGetProcAddress to obtain a pointer to the function.
2896 *
2897 * \returns An 8-digit decimal number representing the internal libGL API in
2898 * YYYYMMDD format.
2899 *
2900 * \sa glXGetProcAddress, PFNGLXGETINTERNALVERSIONPROC
2901 *
2902 * \since Internal API version 20021121.
2903 */
2904 int __glXGetInternalVersion(void)
2905 {
2906 /* History:
2907 * 20021121 - Initial version
2908 * 20021128 - Added __glXWindowExists() function
2909 * 20021207 - Added support for dynamic GLX extensions,
2910 * GLX_SGI_swap_control, GLX_SGI_video_sync,
2911 * GLX_OML_sync_control, and GLX_MESA_swap_control.
2912 * Never officially released. Do NOT test against
2913 * this version. Use 20030317 instead.
2914 * 20030317 - Added support GLX_SGIX_fbconfig,
2915 * GLX_MESA_swap_frame_usage, GLX_OML_swap_method,
2916 * GLX_{ARB,SGIS}_multisample, and
2917 * GLX_SGIX_visual_select_group.
2918 * 20030606 - Added support for GLX_SGI_make_current_read.
2919 * 20030813 - Made support for dynamic extensions multi-head aware.
2920 * 20030818 - Added support for GLX_MESA_allocate_memory in place of the
2921 * deprecated GLX_NV_vertex_array_range & GLX_MESA_agp_offset
2922 * interfaces.
2923 * 20031201 - Added support for the first round of DRI interface changes.
2924 * Do NOT test against this version! It has binary
2925 * compatibility bugs, use 20040317 instead.
2926 * 20040317 - Added the 'mode' field to __DRIcontextRec.
2927 * 20040415 - Added support for bindContext3 and unbindContext3.
2928 * 20040602 - Add __glXGetDrawableInfo. I though that was there
2929 * months ago. :(
2930 * 20050727 - Gut all the old interfaces. This breaks compatability with
2931 * any DRI driver built to any previous version.
2932 * 20060314 - Added support for GLX_MESA_copy_sub_buffer.
2933 * 20070105 - Added support for damage reporting.
2934 */
2935 return 20070105;
2936 }
2937
2938
2939 /**
2940 * Get the unadjusted system time (UST). Currently, the UST is measured in
2941 * microseconds since Epoc. The actual resolution of the UST may vary from
2942 * system to system, and the units may vary from release to release.
2943 * Drivers should not call this function directly. They should instead use
2944 * \c glXGetProcAddress to obtain a pointer to the function.
2945 *
2946 * \param ust Location to store the 64-bit UST
2947 * \returns Zero on success or a negative errno value on failure.
2948 *
2949 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2950 *
2951 * \since Internal API version 20030317.
2952 */
2953 int __glXGetUST( int64_t * ust )
2954 {
2955 struct timeval tv;
2956
2957 if ( ust == NULL ) {
2958 return -EFAULT;
2959 }
2960
2961 if ( gettimeofday( & tv, NULL ) == 0 ) {
2962 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2963 return 0;
2964 } else {
2965 return -errno;
2966 }
2967 }
2968 #endif /* GLX_DIRECT_RENDERING */