Dont add fbconfigs to array that we didn't allocate for.
[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 __DRIcontext *shared = (shareList != NULL)
399 ? &shareList->driContext : 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 if ( modes->fbconfigID != GLX_DONT_CARE ) {
1682 config[i] = modes;
1683 i++;
1684 }
1685 }
1686 }
1687 }
1688 return (GLXFBConfig *) config;
1689 }
1690
1691
1692 PUBLIC int glXGetFBConfigAttrib(Display *dpy, GLXFBConfig config,
1693 int attribute, int *value)
1694 {
1695 __GLcontextModes * const modes = ValidateGLXFBConfig( dpy, config );
1696
1697 return (modes != NULL)
1698 ? _gl_get_context_mode_data( modes, attribute, value )
1699 : GLXBadFBConfig;
1700 }
1701
1702
1703 PUBLIC XVisualInfo *glXGetVisualFromFBConfig(Display *dpy, GLXFBConfig config)
1704 {
1705 XVisualInfo visualTemplate;
1706 __GLcontextModes * fbconfig = (__GLcontextModes *) config;
1707 int count;
1708
1709 /*
1710 ** Get a list of all visuals, return if list is empty
1711 */
1712 visualTemplate.visualid = fbconfig->visualID;
1713 return XGetVisualInfo(dpy,VisualIDMask,&visualTemplate,&count);
1714 }
1715
1716
1717 /*
1718 ** GLX_SGI_swap_control
1719 */
1720 static int __glXSwapIntervalSGI(int interval)
1721 {
1722 xGLXVendorPrivateReq *req;
1723 GLXContext gc = __glXGetCurrentContext();
1724 Display * dpy;
1725 CARD32 * interval_ptr;
1726 CARD8 opcode;
1727
1728 if ( gc == NULL ) {
1729 return GLX_BAD_CONTEXT;
1730 }
1731
1732 if ( interval <= 0 ) {
1733 return GLX_BAD_VALUE;
1734 }
1735
1736 #ifdef __DRI_SWAP_CONTROL
1737 if ( gc->isDirect ) {
1738 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1739 gc->screen );
1740 __DRIdrawable * const pdraw = GetDRIDrawable( gc->currentDpy,
1741 gc->currentDrawable,
1742 NULL );
1743 if (psc->swapControl != NULL && pdraw != NULL) {
1744 psc->swapControl->setSwapInterval(pdraw, interval);
1745 return 0;
1746 }
1747 else {
1748 return GLX_BAD_CONTEXT;
1749 }
1750 }
1751 #endif
1752 dpy = gc->currentDpy;
1753 opcode = __glXSetupForCommand(dpy);
1754 if (!opcode) {
1755 return 0;
1756 }
1757
1758 /* Send the glXSwapIntervalSGI request */
1759 LockDisplay(dpy);
1760 GetReqExtra(GLXVendorPrivate,sizeof(CARD32),req);
1761 req->reqType = opcode;
1762 req->glxCode = X_GLXVendorPrivate;
1763 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1764 req->contextTag = gc->currentContextTag;
1765
1766 interval_ptr = (CARD32 *) (req + 1);
1767 *interval_ptr = interval;
1768
1769 UnlockDisplay(dpy);
1770 SyncHandle();
1771 XFlush(dpy);
1772
1773 return 0;
1774 }
1775
1776
1777 /*
1778 ** GLX_MESA_swap_control
1779 */
1780 static int __glXSwapIntervalMESA(unsigned int interval)
1781 {
1782 #ifdef __DRI_SWAP_CONTROL
1783 GLXContext gc = __glXGetCurrentContext();
1784
1785 if ( interval < 0 ) {
1786 return GLX_BAD_VALUE;
1787 }
1788
1789 if ( (gc != NULL) && gc->isDirect ) {
1790 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1791 gc->screen );
1792
1793 if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
1794 __DRIdrawable * const pdraw =
1795 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1796 if (psc->swapControl != NULL && pdraw != NULL) {
1797 psc->swapControl->setSwapInterval(pdraw, interval);
1798 return 0;
1799 }
1800 }
1801 }
1802 #else
1803 (void) interval;
1804 #endif
1805
1806 return GLX_BAD_CONTEXT;
1807 }
1808
1809
1810 static int __glXGetSwapIntervalMESA(void)
1811 {
1812 #ifdef __DRI_SWAP_CONTROL
1813 GLXContext gc = __glXGetCurrentContext();
1814
1815 if ( (gc != NULL) && gc->isDirect ) {
1816 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1817 gc->screen );
1818
1819 if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
1820 __DRIdrawable * const pdraw =
1821 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1822 if (psc->swapControl != NULL && pdraw != NULL) {
1823 return psc->swapControl->getSwapInterval(pdraw);
1824 }
1825 }
1826 }
1827 #endif
1828
1829 return 0;
1830 }
1831
1832
1833 /*
1834 ** GLX_MESA_swap_frame_usage
1835 */
1836
1837 static GLint __glXBeginFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
1838 {
1839 int status = GLX_BAD_CONTEXT;
1840 #ifdef __DRI_FRAME_TRACKING
1841 int screen;
1842 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1843 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1844
1845 if (pdraw != NULL && psc->frameTracking != NULL)
1846 status = psc->frameTracking->frameTracking(pdraw, GL_TRUE);
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 __DRI_FRAME_TRACKING
1859 int screen;
1860 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1861 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1862
1863 if (pdraw != NULL && psc->frameTracking != NULL)
1864 status = psc->frameTracking->frameTracking(pdraw, GL_FALSE);
1865 #else
1866 (void) dpy;
1867 (void) drawable;
1868 #endif
1869 return status;
1870 }
1871
1872
1873 static GLint __glXGetFrameUsageMESA(Display *dpy, GLXDrawable drawable,
1874 GLfloat *usage)
1875 {
1876 int status = GLX_BAD_CONTEXT;
1877 #ifdef __DRI_FRAME_TRACKING
1878 int screen;
1879 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1880 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1881
1882 if (pdraw != NULL && psc->frameTracking != NULL) {
1883 int64_t sbc, missedFrames;
1884 float lastMissedUsage;
1885
1886 status = psc->frameTracking->queryFrameTracking(pdraw, &sbc,
1887 &missedFrames,
1888 &lastMissedUsage,
1889 usage);
1890 }
1891 #else
1892 (void) dpy;
1893 (void) drawable;
1894 (void) usage;
1895 #endif
1896 return status;
1897 }
1898
1899
1900 static GLint __glXQueryFrameTrackingMESA(Display *dpy, GLXDrawable drawable,
1901 int64_t *sbc, int64_t *missedFrames,
1902 GLfloat *lastMissedUsage)
1903 {
1904 int status = GLX_BAD_CONTEXT;
1905 #ifdef __DRI_FRAME_TRACKING
1906 int screen;
1907 __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
1908 __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
1909
1910 if (pdraw != NULL && psc->frameTracking != NULL) {
1911 float usage;
1912
1913 status = psc->frameTracking->queryFrameTracking(pdraw, sbc, missedFrames,
1914 lastMissedUsage, &usage);
1915 }
1916 #else
1917 (void) dpy;
1918 (void) drawable;
1919 (void) sbc;
1920 (void) missedFrames;
1921 (void) lastMissedUsage;
1922 #endif
1923 return status;
1924 }
1925
1926
1927 /*
1928 ** GLX_SGI_video_sync
1929 */
1930 static int __glXGetVideoSyncSGI(unsigned int *count)
1931 {
1932 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1933 * FIXME: there should be a GLX encoding for this call. I can find no
1934 * FIXME: documentation for the GLX encoding.
1935 */
1936 #ifdef __DRI_MEDIA_STREAM_COUNTER
1937 GLXContext gc = __glXGetCurrentContext();
1938
1939
1940 if ( (gc != NULL) && gc->isDirect ) {
1941 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1942 gc->screen );
1943 if (psc->msc != NULL && psc->driScreen.private != NULL) {
1944 int ret;
1945 int64_t temp;
1946
1947 ret = psc->msc->getMSC(&psc->driScreen, &temp);
1948 *count = (unsigned) temp;
1949 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
1950 }
1951 }
1952 #else
1953 (void) count;
1954 #endif
1955 return GLX_BAD_CONTEXT;
1956 }
1957
1958 static int __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1959 {
1960 #ifdef __DRI_MEDIA_STREAM_COUNTER
1961 GLXContext gc = __glXGetCurrentContext();
1962
1963 if ( divisor <= 0 || remainder < 0 )
1964 return GLX_BAD_VALUE;
1965
1966 if ( (gc != NULL) && gc->isDirect ) {
1967 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
1968 gc->screen );
1969 if (psc->msc != NULL && psc->driScreen.private ) {
1970 __DRIdrawable * const pdraw =
1971 GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
1972 if (pdraw != NULL) {
1973 int ret;
1974 int64_t msc;
1975 int64_t sbc;
1976
1977 ret = (*psc->msc->waitForMSC)(pdraw, 0,
1978 divisor, remainder, &msc, &sbc);
1979 *count = (unsigned) msc;
1980 return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
1981 }
1982 }
1983 }
1984 #else
1985 (void) count;
1986 #endif
1987 return GLX_BAD_CONTEXT;
1988 }
1989
1990
1991 /*
1992 ** GLX_SGIX_fbconfig
1993 ** Many of these functions are aliased to GLX 1.3 entry points in the
1994 ** GLX_functions table.
1995 */
1996
1997 PUBLIC GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
1998 (Display *dpy, GLXFBConfigSGIX config, int attribute, int *value),
1999 (dpy, config, attribute, value),
2000 glXGetFBConfigAttrib)
2001
2002 PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
2003 (Display *dpy, int screen, int *attrib_list, int *nelements),
2004 (dpy, screen, attrib_list, nelements),
2005 glXChooseFBConfig)
2006
2007 PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
2008 (Display * dpy, GLXFBConfigSGIX config),
2009 (dpy, config),
2010 glXGetVisualFromFBConfig)
2011
2012 PUBLIC GLXPixmap glXCreateGLXPixmapWithConfigSGIX(Display *dpy,
2013 GLXFBConfigSGIX config, Pixmap pixmap)
2014 {
2015 xGLXVendorPrivateWithReplyReq *vpreq;
2016 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
2017 GLXPixmap xid = None;
2018 CARD8 opcode;
2019 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2020 __GLXscreenConfigs * psc;
2021
2022
2023 if ( (dpy == NULL) || (config == NULL) ) {
2024 return None;
2025 }
2026
2027 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2028 if ( (psc != NULL)
2029 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2030 opcode = __glXSetupForCommand(dpy);
2031 if (!opcode) {
2032 return None;
2033 }
2034
2035 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
2036 LockDisplay(dpy);
2037 GetReqExtra(GLXVendorPrivateWithReply,
2038 sz_xGLXCreateGLXPixmapWithConfigSGIXReq-sz_xGLXVendorPrivateWithReplyReq,vpreq);
2039 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *)vpreq;
2040 req->reqType = opcode;
2041 req->glxCode = X_GLXVendorPrivateWithReply;
2042 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
2043 req->screen = fbconfig->screen;
2044 req->fbconfig = fbconfig->fbconfigID;
2045 req->pixmap = pixmap;
2046 req->glxpixmap = xid = XAllocID(dpy);
2047 UnlockDisplay(dpy);
2048 SyncHandle();
2049 }
2050
2051 return xid;
2052 }
2053
2054 PUBLIC GLXContext glXCreateContextWithConfigSGIX(Display *dpy,
2055 GLXFBConfigSGIX config, int renderType,
2056 GLXContext shareList, Bool allowDirect)
2057 {
2058 GLXContext gc = NULL;
2059 const __GLcontextModes * const fbconfig = (__GLcontextModes *) config;
2060 __GLXscreenConfigs * psc;
2061
2062
2063 if ( (dpy == NULL) || (config == NULL) ) {
2064 return None;
2065 }
2066
2067 psc = GetGLXScreenConfigs( dpy, fbconfig->screen );
2068 if ( (psc != NULL)
2069 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit ) ) {
2070 gc = CreateContext( dpy, NULL, (__GLcontextModes *) config, shareList,
2071 allowDirect, None, False, renderType );
2072 }
2073
2074 return gc;
2075 }
2076
2077
2078 PUBLIC GLXFBConfigSGIX glXGetFBConfigFromVisualSGIX(Display *dpy,
2079 XVisualInfo *vis)
2080 {
2081 __GLXdisplayPrivate *priv;
2082 __GLXscreenConfigs *psc;
2083
2084 if ( (GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc ) != Success)
2085 && __glXExtensionBitIsEnabled( psc, SGIX_fbconfig_bit )
2086 && (psc->configs->fbconfigID != GLX_DONT_CARE) ) {
2087 return (GLXFBConfigSGIX) _gl_context_modes_find_visual( psc->configs,
2088 vis->visualid );
2089 }
2090
2091 return NULL;
2092 }
2093
2094
2095 /*
2096 ** GLX_SGIX_swap_group
2097 */
2098 static void __glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable,
2099 GLXDrawable member)
2100 {
2101 (void) dpy;
2102 (void) drawable;
2103 (void) member;
2104 }
2105
2106
2107 /*
2108 ** GLX_SGIX_swap_barrier
2109 */
2110 static void __glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable,
2111 int barrier)
2112 {
2113 (void) dpy;
2114 (void) drawable;
2115 (void) barrier;
2116 }
2117
2118 static Bool __glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2119 {
2120 (void) dpy;
2121 (void) screen;
2122 (void) max;
2123 return False;
2124 }
2125
2126
2127 /*
2128 ** GLX_OML_sync_control
2129 */
2130 static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
2131 int64_t *ust, int64_t *msc, int64_t *sbc)
2132 {
2133 #if defined(__DRI_SWAP_BUFFER_COUNTER) && defined(__DRI_MEDIA_STREAM_COUNTER)
2134 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2135
2136 if ( priv != NULL ) {
2137 int i;
2138 __DRIdrawable * const pdraw = GetDRIDrawable( dpy, drawable, & i );
2139 __GLXscreenConfigs * const psc = &priv->screenConfigs[i];
2140
2141 assert( (pdraw == NULL) || (i != -1) );
2142 return ( (pdraw && psc->sbc && psc->msc)
2143 && ((*psc->msc->getMSC)(&psc->driScreen, msc) == 0)
2144 && ((*psc->sbc->getSBC)(pdraw, sbc) == 0)
2145 && (__glXGetUST(ust) == 0) );
2146 }
2147 #else
2148 (void) dpy;
2149 (void) drawable;
2150 (void) ust;
2151 (void) msc;
2152 (void) sbc;
2153 #endif
2154 return False;
2155 }
2156
2157
2158 /**
2159 * Determine the refresh rate of the specified drawable and display.
2160 *
2161 * \param dpy Display whose refresh rate is to be determined.
2162 * \param drawable Drawable whose refresh rate is to be determined.
2163 * \param numerator Numerator of the refresh rate.
2164 * \param demoninator Denominator of the refresh rate.
2165 * \return If the refresh rate for the specified display and drawable could
2166 * be calculated, True is returned. Otherwise False is returned.
2167 *
2168 * \note This function is implemented entirely client-side. A lot of other
2169 * functionality is required to export GLX_OML_sync_control, so on
2170 * XFree86 this function can be called for direct-rendering contexts
2171 * when GLX_OML_sync_control appears in the client extension string.
2172 */
2173
2174 GLboolean __glXGetMscRateOML(__DRIdrawable *draw,
2175 int32_t * numerator, int32_t * denominator)
2176 {
2177 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2178 __GLXdrawable *glxDraw =
2179 containerOf(draw, __GLXdrawable, driDrawable);
2180 __GLXscreenConfigs *psc = glxDraw->psc;
2181 Display *dpy = psc->dpy;
2182 __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
2183
2184
2185 if ( priv != NULL ) {
2186 XF86VidModeModeLine mode_line;
2187 int dot_clock;
2188 int i;
2189
2190
2191 if (XF86VidModeQueryVersion( dpy, & i, & i ) &&
2192 XF86VidModeGetModeLine(dpy, psc->scr, &dot_clock, &mode_line) ) {
2193 unsigned n = dot_clock * 1000;
2194 unsigned d = mode_line.vtotal * mode_line.htotal;
2195
2196 # define V_INTERLACE 0x010
2197 # define V_DBLSCAN 0x020
2198
2199 if ( (mode_line.flags & V_INTERLACE) ) {
2200 n *= 2;
2201 }
2202 else if ( (mode_line.flags & V_DBLSCAN) ) {
2203 d *= 2;
2204 }
2205
2206 /* The OML_sync_control spec requires that if the refresh rate is a
2207 * whole number, that the returned numerator be equal to the refresh
2208 * rate and the denominator be 1.
2209 */
2210
2211 if ( (n % d) == 0 ) {
2212 n /= d;
2213 d = 1;
2214 }
2215 else {
2216 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2217
2218
2219 /* This is a poor man's way to reduce a fraction. It's far from
2220 * perfect, but it will work well enough for this situation.
2221 */
2222
2223 for ( i = 0 ; f[i] != 0 ; i++ ) {
2224 while ( ((n % f[i]) == 0) && ((d % f[i]) == 0) ) {
2225 d /= f[i];
2226 n /= f[i];
2227 }
2228 }
2229 }
2230
2231 *numerator = n;
2232 *denominator = d;
2233
2234 return True;
2235 }
2236 }
2237 #else
2238 (void) draw;
2239 (void) numerator;
2240 (void) denominator;
2241 #endif
2242 return False;
2243 }
2244
2245
2246 static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
2247 int64_t target_msc, int64_t divisor,
2248 int64_t remainder)
2249 {
2250 #ifdef __DRI_SWAP_BUFFER_COUNTER
2251 int screen;
2252 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2253 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2254
2255 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2256 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2257 * of -1 if the function failed because of errors detected in the input
2258 * parameters"
2259 */
2260 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2261 return -1;
2262 if ( divisor > 0 && remainder >= divisor )
2263 return -1;
2264
2265 if (pdraw != NULL && psc->counters != NULL)
2266 return (*psc->sbc->swapBuffersMSC)(pdraw, target_msc,
2267 divisor, remainder);
2268
2269 #else
2270 (void) dpy;
2271 (void) drawable;
2272 (void) target_msc;
2273 (void) divisor;
2274 (void) remainder;
2275 #endif
2276 return 0;
2277 }
2278
2279
2280 static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2281 int64_t target_msc, int64_t divisor,
2282 int64_t remainder, int64_t *ust,
2283 int64_t *msc, int64_t *sbc)
2284 {
2285 #ifdef __DRI_MEDIA_STREAM_COUNTER
2286 int screen;
2287 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2288 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2289 int ret;
2290
2291 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2292 * error", but the return type in the spec is Bool.
2293 */
2294 if ( divisor < 0 || remainder < 0 || target_msc < 0 )
2295 return False;
2296 if ( divisor > 0 && remainder >= divisor )
2297 return False;
2298
2299 if (pdraw != NULL && psc->msc != NULL) {
2300 ret = (*psc->msc->waitForMSC)(pdraw, target_msc,
2301 divisor, remainder, msc, sbc);
2302
2303 /* __glXGetUST returns zero on success and non-zero on failure.
2304 * This function returns True on success and False on failure.
2305 */
2306 return ( (ret == 0) && (__glXGetUST( ust ) == 0) );
2307 }
2308 #else
2309 (void) dpy;
2310 (void) drawable;
2311 (void) target_msc;
2312 (void) divisor;
2313 (void) remainder;
2314 (void) ust;
2315 (void) msc;
2316 (void) sbc;
2317 #endif
2318 return False;
2319 }
2320
2321
2322 static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2323 int64_t target_sbc, int64_t *ust,
2324 int64_t *msc, int64_t *sbc )
2325 {
2326 #ifdef __DRI_SWAP_BUFFER_COUNTER
2327 int screen;
2328 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2329 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2330 int ret;
2331
2332 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2333 * error", but the return type in the spec is Bool.
2334 */
2335 if ( target_sbc < 0 )
2336 return False;
2337
2338 if (pdraw != NULL && psc->sbc != NULL) {
2339 ret = (*psc->sbc->waitForSBC)(pdraw, target_sbc, msc, sbc);
2340
2341 /* __glXGetUST returns zero on success and non-zero on failure.
2342 * This function returns True on success and False on failure.
2343 */
2344 return( (ret == 0) && (__glXGetUST( ust ) == 0) );
2345 }
2346 #else
2347 (void) dpy;
2348 (void) drawable;
2349 (void) target_sbc;
2350 (void) ust;
2351 (void) msc;
2352 (void) sbc;
2353 #endif
2354 return False;
2355 }
2356
2357
2358 /**
2359 * GLX_MESA_allocate_memory
2360 */
2361 /*@{*/
2362
2363 PUBLIC void *glXAllocateMemoryMESA(Display *dpy, int scrn,
2364 size_t size, float readFreq,
2365 float writeFreq, float priority)
2366 {
2367 #ifdef __DRI_ALLOCATE
2368 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2369
2370 if (psc && psc->allocate)
2371 return (*psc->allocate->allocateMemory)( &psc->driScreen, size,
2372 readFreq, writeFreq,
2373 priority );
2374
2375 #else
2376 (void) dpy;
2377 (void) scrn;
2378 (void) size;
2379 (void) readFreq;
2380 (void) writeFreq;
2381 (void) priority;
2382 #endif /* GLX_DIRECT_RENDERING */
2383
2384 return NULL;
2385 }
2386
2387
2388 PUBLIC void glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
2389 {
2390 #ifdef __DRI_ALLOCATE
2391 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2392
2393 if (psc && psc->allocate)
2394 (*psc->allocate->freeMemory)( &psc->driScreen, pointer );
2395
2396 #else
2397 (void) dpy;
2398 (void) scrn;
2399 (void) pointer;
2400 #endif /* GLX_DIRECT_RENDERING */
2401 }
2402
2403
2404 PUBLIC GLuint glXGetMemoryOffsetMESA( Display *dpy, int scrn,
2405 const void *pointer )
2406 {
2407 #ifdef __DRI_ALLOCATE
2408 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
2409
2410 if (psc && psc->allocate)
2411 return (*psc->allocate->memoryOffset)( &psc->driScreen, pointer );
2412
2413 #else
2414 (void) dpy;
2415 (void) scrn;
2416 (void) pointer;
2417 #endif /* GLX_DIRECT_RENDERING */
2418
2419 return ~0L;
2420 }
2421 /*@}*/
2422
2423
2424 /**
2425 * Mesa extension stubs. These will help reduce portability problems.
2426 */
2427 /*@{*/
2428
2429 /**
2430 * Release all buffers associated with the specified GLX drawable.
2431 *
2432 * \todo
2433 * This function was intended for stand-alone Mesa. The issue there is that
2434 * the library doesn't get any notification when a window is closed. In
2435 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2436 * supported, there are 3 different functions to destroy a drawable. It
2437 * should be possible to create GLX protocol (or have it determine which
2438 * protocol to use based on the type of the drawable) to have one function
2439 * do the work of 3. For the direct-rendering case, this function could
2440 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2441 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2442 * would need to be used. This really should be done as part of the new DRI
2443 * interface work.
2444 *
2445 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2446 * __driGarbageCollectDrawables
2447 * glXDestroyGLXPixmap
2448 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2449 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2450 */
2451 static Bool __glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2452 {
2453 (void) dpy;
2454 (void) d;
2455 return False;
2456 }
2457
2458
2459 PUBLIC GLXPixmap glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visual,
2460 Pixmap pixmap, Colormap cmap )
2461 {
2462 (void) dpy;
2463 (void) visual;
2464 (void) pixmap;
2465 (void) cmap;
2466 return 0;
2467 }
2468 /*@}*/
2469
2470
2471 /**
2472 * GLX_MESA_copy_sub_buffer
2473 */
2474 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2475 static void __glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
2476 int x, int y, int width, int height)
2477 {
2478 xGLXVendorPrivateReq *req;
2479 GLXContext gc;
2480 GLXContextTag tag;
2481 CARD32 *drawable_ptr;
2482 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2483 CARD8 opcode;
2484
2485 #ifdef __DRI_COPY_SUB_BUFFER
2486 int screen;
2487 __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
2488 if ( pdraw != NULL ) {
2489 __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
2490 if (psc->copySubBuffer != NULL) {
2491 (*psc->copySubBuffer->copySubBuffer)(pdraw, x, y, width, height);
2492 }
2493
2494 return;
2495 }
2496 #endif
2497
2498 opcode = __glXSetupForCommand(dpy);
2499 if (!opcode)
2500 return;
2501
2502 /*
2503 ** The calling thread may or may not have a current context. If it
2504 ** does, send the context tag so the server can do a flush.
2505 */
2506 gc = __glXGetCurrentContext();
2507 if ((gc != NULL) && (dpy == gc->currentDpy) &&
2508 ((drawable == gc->currentDrawable) ||
2509 (drawable == gc->currentReadable)) ) {
2510 tag = gc->currentContextTag;
2511 } else {
2512 tag = 0;
2513 }
2514
2515 LockDisplay(dpy);
2516 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4,req);
2517 req->reqType = opcode;
2518 req->glxCode = X_GLXVendorPrivate;
2519 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2520 req->contextTag = tag;
2521
2522 drawable_ptr = (CARD32 *) (req + 1);
2523 x_ptr = (INT32 *) (drawable_ptr + 1);
2524 y_ptr = (INT32 *) (drawable_ptr + 2);
2525 w_ptr = (INT32 *) (drawable_ptr + 3);
2526 h_ptr = (INT32 *) (drawable_ptr + 4);
2527
2528 *drawable_ptr = drawable;
2529 *x_ptr = x;
2530 *y_ptr = y;
2531 *w_ptr = width;
2532 *h_ptr = height;
2533
2534 UnlockDisplay(dpy);
2535 SyncHandle();
2536 }
2537
2538
2539 /**
2540 * GLX_EXT_texture_from_pixmap
2541 */
2542 /*@{*/
2543 static void __glXBindTexImageEXT(Display *dpy,
2544 GLXDrawable drawable,
2545 int buffer,
2546 const int *attrib_list)
2547 {
2548 xGLXVendorPrivateReq *req;
2549 GLXContext gc = __glXGetCurrentContext();
2550 CARD32 *drawable_ptr;
2551 INT32 *buffer_ptr;
2552 CARD32 *num_attrib_ptr;
2553 CARD32 *attrib_ptr;
2554 CARD8 opcode;
2555 unsigned int i;
2556
2557 if (gc == NULL)
2558 return;
2559
2560 i = 0;
2561 if (attrib_list) {
2562 while (attrib_list[i * 2] != None)
2563 i++;
2564 }
2565
2566 #ifdef GLX_DIRECT_RENDERING
2567 if (gc->isDirect)
2568 return;
2569 #endif
2570
2571 opcode = __glXSetupForCommand(dpy);
2572 if (!opcode)
2573 return;
2574
2575 LockDisplay(dpy);
2576 GetReqExtra(GLXVendorPrivate, 12 + 8 * i,req);
2577 req->reqType = opcode;
2578 req->glxCode = X_GLXVendorPrivate;
2579 req->vendorCode = X_GLXvop_BindTexImageEXT;
2580 req->contextTag = gc->currentContextTag;
2581
2582 drawable_ptr = (CARD32 *) (req + 1);
2583 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2584 num_attrib_ptr = (CARD32 *) (buffer_ptr + 1);
2585 attrib_ptr = (CARD32 *) (num_attrib_ptr + 1);
2586
2587 *drawable_ptr = drawable;
2588 *buffer_ptr = buffer;
2589 *num_attrib_ptr = (CARD32) i;
2590
2591 i = 0;
2592 if (attrib_list) {
2593 while (attrib_list[i * 2] != None)
2594 {
2595 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 0];
2596 *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 1];
2597 i++;
2598 }
2599 }
2600
2601 UnlockDisplay(dpy);
2602 SyncHandle();
2603 }
2604
2605 static void __glXReleaseTexImageEXT(Display *dpy,
2606 GLXDrawable drawable,
2607 int buffer)
2608 {
2609 xGLXVendorPrivateReq *req;
2610 GLXContext gc = __glXGetCurrentContext();
2611 CARD32 *drawable_ptr;
2612 INT32 *buffer_ptr;
2613 CARD8 opcode;
2614
2615 if (gc == NULL)
2616 return;
2617
2618 #ifdef GLX_DIRECT_RENDERING
2619 if (gc->isDirect)
2620 return;
2621 #endif
2622
2623 opcode = __glXSetupForCommand(dpy);
2624 if (!opcode)
2625 return;
2626
2627 LockDisplay(dpy);
2628 GetReqExtra(GLXVendorPrivate, sizeof(CARD32)+sizeof(INT32),req);
2629 req->reqType = opcode;
2630 req->glxCode = X_GLXVendorPrivate;
2631 req->vendorCode = X_GLXvop_ReleaseTexImageEXT;
2632 req->contextTag = gc->currentContextTag;
2633
2634 drawable_ptr = (CARD32 *) (req + 1);
2635 buffer_ptr = (INT32 *) (drawable_ptr + 1);
2636
2637 *drawable_ptr = drawable;
2638 *buffer_ptr = buffer;
2639
2640 UnlockDisplay(dpy);
2641 SyncHandle();
2642 }
2643 /*@}*/
2644
2645 /**
2646 * \c strdup is actually not a standard ANSI C or POSIX routine.
2647 * Irix will not define it if ANSI mode is in effect.
2648 *
2649 * \sa strdup
2650 */
2651 char *
2652 __glXstrdup(const char *str)
2653 {
2654 char *copy;
2655 copy = (char *) Xmalloc(strlen(str) + 1);
2656 if (!copy)
2657 return NULL;
2658 strcpy(copy, str);
2659 return copy;
2660 }
2661
2662 /*
2663 ** glXGetProcAddress support
2664 */
2665
2666 struct name_address_pair {
2667 const char *Name;
2668 GLvoid *Address;
2669 };
2670
2671 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2672 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2673
2674 static const struct name_address_pair GLX_functions[] = {
2675 /*** GLX_VERSION_1_0 ***/
2676 GLX_FUNCTION( glXChooseVisual ),
2677 GLX_FUNCTION( glXCopyContext ),
2678 GLX_FUNCTION( glXCreateContext ),
2679 GLX_FUNCTION( glXCreateGLXPixmap ),
2680 GLX_FUNCTION( glXDestroyContext ),
2681 GLX_FUNCTION( glXDestroyGLXPixmap ),
2682 GLX_FUNCTION( glXGetConfig ),
2683 GLX_FUNCTION( glXGetCurrentContext ),
2684 GLX_FUNCTION( glXGetCurrentDrawable ),
2685 GLX_FUNCTION( glXIsDirect ),
2686 GLX_FUNCTION( glXMakeCurrent ),
2687 GLX_FUNCTION( glXQueryExtension ),
2688 GLX_FUNCTION( glXQueryVersion ),
2689 GLX_FUNCTION( glXSwapBuffers ),
2690 GLX_FUNCTION( glXUseXFont ),
2691 GLX_FUNCTION( glXWaitGL ),
2692 GLX_FUNCTION( glXWaitX ),
2693
2694 /*** GLX_VERSION_1_1 ***/
2695 GLX_FUNCTION( glXGetClientString ),
2696 GLX_FUNCTION( glXQueryExtensionsString ),
2697 GLX_FUNCTION( glXQueryServerString ),
2698
2699 /*** GLX_VERSION_1_2 ***/
2700 GLX_FUNCTION( glXGetCurrentDisplay ),
2701
2702 /*** GLX_VERSION_1_3 ***/
2703 GLX_FUNCTION( glXChooseFBConfig ),
2704 GLX_FUNCTION( glXCreateNewContext ),
2705 GLX_FUNCTION( glXCreatePbuffer ),
2706 GLX_FUNCTION( glXCreatePixmap ),
2707 GLX_FUNCTION( glXCreateWindow ),
2708 GLX_FUNCTION( glXDestroyPbuffer ),
2709 GLX_FUNCTION( glXDestroyPixmap ),
2710 GLX_FUNCTION( glXDestroyWindow ),
2711 GLX_FUNCTION( glXGetCurrentReadDrawable ),
2712 GLX_FUNCTION( glXGetFBConfigAttrib ),
2713 GLX_FUNCTION( glXGetFBConfigs ),
2714 GLX_FUNCTION( glXGetSelectedEvent ),
2715 GLX_FUNCTION( glXGetVisualFromFBConfig ),
2716 GLX_FUNCTION( glXMakeContextCurrent ),
2717 GLX_FUNCTION( glXQueryContext ),
2718 GLX_FUNCTION( glXQueryDrawable ),
2719 GLX_FUNCTION( glXSelectEvent ),
2720
2721 /*** GLX_SGI_swap_control ***/
2722 GLX_FUNCTION2( glXSwapIntervalSGI, __glXSwapIntervalSGI ),
2723
2724 /*** GLX_SGI_video_sync ***/
2725 GLX_FUNCTION2( glXGetVideoSyncSGI, __glXGetVideoSyncSGI ),
2726 GLX_FUNCTION2( glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI ),
2727
2728 /*** GLX_SGI_make_current_read ***/
2729 GLX_FUNCTION2( glXMakeCurrentReadSGI, glXMakeContextCurrent ),
2730 GLX_FUNCTION2( glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable ),
2731
2732 /*** GLX_EXT_import_context ***/
2733 GLX_FUNCTION( glXFreeContextEXT ),
2734 GLX_FUNCTION( glXGetContextIDEXT ),
2735 GLX_FUNCTION2( glXGetCurrentDisplayEXT, glXGetCurrentDisplay ),
2736 GLX_FUNCTION( glXImportContextEXT ),
2737 GLX_FUNCTION2( glXQueryContextInfoEXT, glXQueryContext ),
2738
2739 /*** GLX_SGIX_fbconfig ***/
2740 GLX_FUNCTION2( glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib ),
2741 GLX_FUNCTION2( glXChooseFBConfigSGIX, glXChooseFBConfig ),
2742 GLX_FUNCTION( glXCreateGLXPixmapWithConfigSGIX ),
2743 GLX_FUNCTION( glXCreateContextWithConfigSGIX ),
2744 GLX_FUNCTION2( glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig ),
2745 GLX_FUNCTION( glXGetFBConfigFromVisualSGIX ),
2746
2747 /*** GLX_SGIX_pbuffer ***/
2748 GLX_FUNCTION( glXCreateGLXPbufferSGIX ),
2749 GLX_FUNCTION( glXDestroyGLXPbufferSGIX ),
2750 GLX_FUNCTION( glXQueryGLXPbufferSGIX ),
2751 GLX_FUNCTION( glXSelectEventSGIX ),
2752 GLX_FUNCTION( glXGetSelectedEventSGIX ),
2753
2754 /*** GLX_SGIX_swap_group ***/
2755 GLX_FUNCTION2( glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX ),
2756
2757 /*** GLX_SGIX_swap_barrier ***/
2758 GLX_FUNCTION2( glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX ),
2759 GLX_FUNCTION2( glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX ),
2760
2761 /*** GLX_MESA_allocate_memory ***/
2762 GLX_FUNCTION( glXAllocateMemoryMESA ),
2763 GLX_FUNCTION( glXFreeMemoryMESA ),
2764 GLX_FUNCTION( glXGetMemoryOffsetMESA ),
2765
2766 /*** GLX_MESA_copy_sub_buffer ***/
2767 GLX_FUNCTION2( glXCopySubBufferMESA, __glXCopySubBufferMESA ),
2768
2769 /*** GLX_MESA_pixmap_colormap ***/
2770 GLX_FUNCTION( glXCreateGLXPixmapMESA ),
2771
2772 /*** GLX_MESA_release_buffers ***/
2773 GLX_FUNCTION2( glXReleaseBuffersMESA, __glXReleaseBuffersMESA ),
2774
2775 /*** GLX_MESA_swap_control ***/
2776 GLX_FUNCTION2( glXSwapIntervalMESA, __glXSwapIntervalMESA ),
2777 GLX_FUNCTION2( glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA ),
2778
2779 /*** GLX_MESA_swap_frame_usage ***/
2780 GLX_FUNCTION2( glXBeginFrameTrackingMESA, __glXBeginFrameTrackingMESA ),
2781 GLX_FUNCTION2( glXEndFrameTrackingMESA, __glXEndFrameTrackingMESA ),
2782 GLX_FUNCTION2( glXGetFrameUsageMESA, __glXGetFrameUsageMESA ),
2783 GLX_FUNCTION2( glXQueryFrameTrackingMESA, __glXQueryFrameTrackingMESA ),
2784
2785 /*** GLX_ARB_get_proc_address ***/
2786 GLX_FUNCTION( glXGetProcAddressARB ),
2787
2788 /*** GLX 1.4 ***/
2789 GLX_FUNCTION2( glXGetProcAddress, glXGetProcAddressARB ),
2790
2791 /*** GLX_OML_sync_control ***/
2792 GLX_FUNCTION2( glXWaitForSbcOML, __glXWaitForSbcOML ),
2793 GLX_FUNCTION2( glXWaitForMscOML, __glXWaitForMscOML ),
2794 GLX_FUNCTION2( glXSwapBuffersMscOML, __glXSwapBuffersMscOML ),
2795 GLX_FUNCTION2( glXGetMscRateOML, __glXGetMscRateOML ),
2796 GLX_FUNCTION2( glXGetSyncValuesOML, __glXGetSyncValuesOML ),
2797
2798 /*** GLX_EXT_texture_from_pixmap ***/
2799 GLX_FUNCTION2( glXBindTexImageEXT, __glXBindTexImageEXT ),
2800 GLX_FUNCTION2( glXReleaseTexImageEXT, __glXReleaseTexImageEXT ),
2801
2802 #ifdef GLX_DIRECT_RENDERING
2803 /*** DRI configuration ***/
2804 GLX_FUNCTION( glXGetScreenDriver ),
2805 GLX_FUNCTION( glXGetDriverConfig ),
2806 #endif
2807
2808 { NULL, NULL } /* end of list */
2809 };
2810
2811
2812 static const GLvoid *
2813 get_glx_proc_address(const char *funcName)
2814 {
2815 GLuint i;
2816
2817 /* try static functions */
2818 for (i = 0; GLX_functions[i].Name; i++) {
2819 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2820 return GLX_functions[i].Address;
2821 }
2822
2823 return NULL;
2824 }
2825
2826
2827 /**
2828 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2829 * \c glXGetProcAddress.
2830 *
2831 * \param procName Name of a GL or GLX function.
2832 * \returns A pointer to the named function
2833 *
2834 * \sa glXGetProcAddress
2835 */
2836 PUBLIC void (*glXGetProcAddressARB(const GLubyte *procName))( void )
2837 {
2838 typedef void (*gl_function)( void );
2839 gl_function f;
2840
2841
2842 /* Search the table of GLX and internal functions first. If that
2843 * fails and the supplied name could be a valid core GL name, try
2844 * searching the core GL function table. This check is done to prevent
2845 * DRI based drivers from searching the core GL function table for
2846 * internal API functions.
2847 */
2848
2849 f = (gl_function) get_glx_proc_address((const char *) procName);
2850 if ( (f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2851 && (procName[2] != 'X') ) {
2852 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2853 }
2854
2855 return f;
2856 }
2857
2858 /**
2859 * Get the address of a named GL function. This is the GLX 1.4 name for
2860 * \c glXGetProcAddressARB.
2861 *
2862 * \param procName Name of a GL or GLX function.
2863 * \returns A pointer to the named function
2864 *
2865 * \sa glXGetProcAddressARB
2866 */
2867 PUBLIC void (*glXGetProcAddress(const GLubyte *procName))( void )
2868 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
2869 __attribute__ ((alias ("glXGetProcAddressARB")));
2870 #else
2871 {
2872 return glXGetProcAddressARB(procName);
2873 }
2874 #endif /* __GNUC__ */
2875
2876
2877 #ifdef GLX_DIRECT_RENDERING
2878 /**
2879 * Retrieves the verion of the internal libGL API in YYYYMMDD format. This
2880 * might be used by the DRI drivers to determine how new libGL is at runtime.
2881 * Drivers should not call this function directly. They should instead use
2882 * \c glXGetProcAddress to obtain a pointer to the function.
2883 *
2884 * \returns An 8-digit decimal number representing the internal libGL API in
2885 * YYYYMMDD format.
2886 *
2887 * \sa glXGetProcAddress, PFNGLXGETINTERNALVERSIONPROC
2888 *
2889 * \since Internal API version 20021121.
2890 */
2891 int __glXGetInternalVersion(void)
2892 {
2893 /* History:
2894 * 20021121 - Initial version
2895 * 20021128 - Added __glXWindowExists() function
2896 * 20021207 - Added support for dynamic GLX extensions,
2897 * GLX_SGI_swap_control, GLX_SGI_video_sync,
2898 * GLX_OML_sync_control, and GLX_MESA_swap_control.
2899 * Never officially released. Do NOT test against
2900 * this version. Use 20030317 instead.
2901 * 20030317 - Added support GLX_SGIX_fbconfig,
2902 * GLX_MESA_swap_frame_usage, GLX_OML_swap_method,
2903 * GLX_{ARB,SGIS}_multisample, and
2904 * GLX_SGIX_visual_select_group.
2905 * 20030606 - Added support for GLX_SGI_make_current_read.
2906 * 20030813 - Made support for dynamic extensions multi-head aware.
2907 * 20030818 - Added support for GLX_MESA_allocate_memory in place of the
2908 * deprecated GLX_NV_vertex_array_range & GLX_MESA_agp_offset
2909 * interfaces.
2910 * 20031201 - Added support for the first round of DRI interface changes.
2911 * Do NOT test against this version! It has binary
2912 * compatibility bugs, use 20040317 instead.
2913 * 20040317 - Added the 'mode' field to __DRIcontextRec.
2914 * 20040415 - Added support for bindContext3 and unbindContext3.
2915 * 20040602 - Add __glXGetDrawableInfo. I though that was there
2916 * months ago. :(
2917 * 20050727 - Gut all the old interfaces. This breaks compatability with
2918 * any DRI driver built to any previous version.
2919 * 20060314 - Added support for GLX_MESA_copy_sub_buffer.
2920 * 20070105 - Added support for damage reporting.
2921 */
2922 return 20070105;
2923 }
2924
2925
2926 /**
2927 * Get the unadjusted system time (UST). Currently, the UST is measured in
2928 * microseconds since Epoc. The actual resolution of the UST may vary from
2929 * system to system, and the units may vary from release to release.
2930 * Drivers should not call this function directly. They should instead use
2931 * \c glXGetProcAddress to obtain a pointer to the function.
2932 *
2933 * \param ust Location to store the 64-bit UST
2934 * \returns Zero on success or a negative errno value on failure.
2935 *
2936 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2937 *
2938 * \since Internal API version 20030317.
2939 */
2940 int __glXGetUST( int64_t * ust )
2941 {
2942 struct timeval tv;
2943
2944 if ( ust == NULL ) {
2945 return -EFAULT;
2946 }
2947
2948 if ( gettimeofday( & tv, NULL ) == 0 ) {
2949 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2950 return 0;
2951 } else {
2952 return -errno;
2953 }
2954 }
2955 #endif /* GLX_DIRECT_RENDERING */