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