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