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