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