ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glx / glxcmds.c
blobd4aa8b5866893690710f515af70a9927ae22a585
1 /*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
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:
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.
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.
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.
31 /**
32 * \file glxcmds.c
33 * Client-side GLX interface.
36 #include "glxclient.h"
37 #include "glapi.h"
38 #include "glxextensions.h"
40 #ifdef GLX_DIRECT_RENDERING
41 #ifdef GLX_USE_APPLEGL
42 #include "apple_glx_context.h"
43 #include "apple_glx.h"
44 #include "glx_error.h"
45 #else
46 #include <sys/time.h>
47 #include <X11/extensions/xf86vmode.h>
48 #include "xf86dri.h"
49 #endif
50 #else
51 #endif
53 #if defined(USE_XCB)
54 #include <X11/Xlib-xcb.h>
55 #include <xcb/xcb.h>
56 #include <xcb/glx.h>
57 #endif
59 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
60 static const char __glXGLXClientVersion[] = "1.4";
62 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
64 static Bool windowExistsFlag;
65 static int
66 windowExistsErrorHandler(Display * dpy, XErrorEvent * xerr)
68 (void) dpy;
70 if (xerr->error_code == BadWindow) {
71 windowExistsFlag = GL_FALSE;
73 return 0;
76 /**
77 * Find drawables in the local hash that have been destroyed on the
78 * server.
80 * \param dpy Display to destroy drawables for
81 * \param screen Screen number to destroy drawables for
83 _X_HIDDEN void
84 GarbageCollectDRIDrawables(struct glx_screen * sc)
86 XID draw;
87 __GLXDRIdrawable *pdraw;
88 struct glx_display *priv = sc->display;
89 XWindowAttributes xwa;
90 int (*oldXErrorHandler) (Display *, XErrorEvent *);
92 /* Set no-op error handler so Xlib doesn't bail out if the windows
93 * has alreay been destroyed on the server. */
94 XSync(priv->dpy, GL_FALSE);
95 oldXErrorHandler = XSetErrorHandler(windowExistsErrorHandler);
97 if (__glxHashFirst(priv->drawHash, &draw, (void *) &pdraw) == 1) {
98 do {
99 windowExistsFlag = GL_TRUE;
100 XGetWindowAttributes(priv->dpy, draw, &xwa); /* dummy request */
101 if (!windowExistsFlag) {
102 /* Destroy the local drawable data, if the drawable no
103 longer exists in the Xserver */
104 (*pdraw->destroyDrawable) (pdraw);
105 __glxHashDelete(priv->drawHash, draw);
107 } while (__glxHashNext(priv->drawHash, &draw, (void *) &pdraw) == 1);
110 XSync(priv->dpy, GL_FALSE);
111 XSetErrorHandler(oldXErrorHandler);
115 * Get the __DRIdrawable for the drawable associated with a GLXContext
117 * \param dpy The display associated with \c drawable.
118 * \param drawable GLXDrawable whose __DRIdrawable part is to be retrieved.
119 * \param scrn_num If non-NULL, the drawables screen is stored there
120 * \returns A pointer to the context's __DRIdrawable on success, or NULL if
121 * the drawable is not associated with a direct-rendering context.
123 _X_HIDDEN __GLXDRIdrawable *
124 GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
126 struct glx_display *priv = __glXInitialize(dpy);
127 __GLXDRIdrawable *pdraw;
129 if (priv == NULL)
130 return NULL;
132 if (__glxHashLookup(priv->drawHash, drawable, (void *) &pdraw) == 0)
133 return pdraw;
135 return NULL;
138 #endif
142 * Get the GLX per-screen data structure associated with a GLX context.
144 * \param dpy Display for which the GLX per-screen information is to be
145 * retrieved.
146 * \param scrn Screen on \c dpy for which the GLX per-screen information is
147 * to be retrieved.
148 * \returns A pointer to the GLX per-screen data if \c dpy and \c scrn
149 * specify a valid GLX screen, or NULL otherwise.
151 * \todo Should this function validate that \c scrn is within the screen
152 * number range for \c dpy?
155 static struct glx_screen *
156 GetGLXScreenConfigs(Display * dpy, int scrn)
158 struct glx_display *const priv = __glXInitialize(dpy);
160 return (priv
161 && priv->screens !=
162 NULL) ? priv->screens[scrn] : NULL;
166 static int
167 GetGLXPrivScreenConfig(Display * dpy, int scrn, struct glx_display ** ppriv,
168 struct glx_screen ** ppsc)
170 /* Initialize the extension, if needed . This has the added value
171 * of initializing/allocating the display private
174 if (dpy == NULL) {
175 return GLX_NO_EXTENSION;
178 *ppriv = __glXInitialize(dpy);
179 if (*ppriv == NULL) {
180 return GLX_NO_EXTENSION;
183 /* Check screen number to see if its valid */
184 if ((scrn < 0) || (scrn >= ScreenCount(dpy))) {
185 return GLX_BAD_SCREEN;
188 /* Check to see if the GL is supported on this screen */
189 *ppsc = (*ppriv)->screens[scrn];
190 if ((*ppsc)->configs == NULL) {
191 /* No support for GL on this screen regardless of visual */
192 return GLX_BAD_VISUAL;
195 return Success;
200 * Determine if a \c GLXFBConfig supplied by the application is valid.
202 * \param dpy Application supplied \c Display pointer.
203 * \param config Application supplied \c GLXFBConfig.
205 * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
206 * \c struct glx_config structure is returned. Otherwise, \c NULL
207 * is returned.
209 static struct glx_config *
210 ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
212 struct glx_display *const priv = __glXInitialize(dpy);
213 int num_screens = ScreenCount(dpy);
214 unsigned i;
215 struct glx_config *config;
217 if (priv != NULL) {
218 for (i = 0; i < num_screens; i++) {
219 for (config = priv->screens[i]->configs; config != NULL;
220 config = config->next) {
221 if (config == (struct glx_config *) fbconfig) {
222 return config;
228 return NULL;
231 _X_HIDDEN Bool
232 glx_context_init(struct glx_context *gc,
233 struct glx_screen *psc, struct glx_config *config)
235 gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
236 if (!gc->majorOpcode)
237 return GL_FALSE;
239 gc->screen = psc->scr;
240 gc->psc = psc;
241 gc->config = config;
242 gc->isDirect = GL_TRUE;
243 gc->currentContextTag = -1;
245 return GL_TRUE;
250 * Create a new context. Exactly one of \c vis and \c fbconfig should be
251 * non-NULL.
253 * \param use_glx_1_3 For FBConfigs, should GLX 1.3 protocol or
254 * SGIX_fbconfig protocol be used?
255 * \param renderType For FBConfigs, what is the rendering type?
258 static GLXContext
259 CreateContext(Display * dpy, int generic_id,
260 struct glx_config *config,
261 GLXContext shareList_user,
262 Bool allowDirect,
263 unsigned code, int renderType, int screen)
265 struct glx_context *gc = NULL;
266 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
267 struct glx_context *shareList = (struct glx_context *) shareList_user;
268 if (dpy == NULL)
269 return NULL;
271 if (generic_id == None)
272 return NULL;
274 gc = NULL;
275 if (allowDirect && psc->vtable->create_context)
276 gc = psc->vtable->create_context(psc, config, shareList, renderType);
277 if (!gc)
278 gc = indirect_create_context(psc, config, shareList, renderType);
279 if (!gc)
280 return NULL;
282 LockDisplay(dpy);
283 switch (code) {
284 case X_GLXCreateContext: {
285 xGLXCreateContextReq *req;
287 /* Send the glXCreateContext request */
288 GetReq(GLXCreateContext, req);
289 req->reqType = gc->majorOpcode;
290 req->glxCode = X_GLXCreateContext;
291 req->context = gc->xid = XAllocID(dpy);
292 req->visual = generic_id;
293 req->screen = screen;
294 req->shareList = shareList ? shareList->xid : None;
295 req->isDirect = gc->isDirect;
296 break;
299 case X_GLXCreateNewContext: {
300 xGLXCreateNewContextReq *req;
302 /* Send the glXCreateNewContext request */
303 GetReq(GLXCreateNewContext, req);
304 req->reqType = gc->majorOpcode;
305 req->glxCode = X_GLXCreateNewContext;
306 req->context = gc->xid = XAllocID(dpy);
307 req->fbconfig = generic_id;
308 req->screen = screen;
309 req->renderType = renderType;
310 req->shareList = shareList ? shareList->xid : None;
311 req->isDirect = gc->isDirect;
312 break;
315 case X_GLXvop_CreateContextWithConfigSGIX: {
316 xGLXVendorPrivateWithReplyReq *vpreq;
317 xGLXCreateContextWithConfigSGIXReq *req;
319 /* Send the glXCreateNewContext request */
320 GetReqExtra(GLXVendorPrivateWithReply,
321 sz_xGLXCreateContextWithConfigSGIXReq -
322 sz_xGLXVendorPrivateWithReplyReq, vpreq);
323 req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
324 req->reqType = gc->majorOpcode;
325 req->glxCode = X_GLXVendorPrivateWithReply;
326 req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
327 req->context = gc->xid = XAllocID(dpy);
328 req->fbconfig = generic_id;
329 req->screen = screen;
330 req->renderType = renderType;
331 req->shareList = shareList ? shareList->xid : None;
332 req->isDirect = gc->isDirect;
333 break;
336 default:
337 /* What to do here? This case is the sign of an internal error. It
338 * should never be reachable.
340 break;
343 UnlockDisplay(dpy);
344 SyncHandle();
346 gc->imported = GL_FALSE;
347 gc->renderType = renderType;
349 return (GLXContext) gc;
352 _X_EXPORT GLXContext
353 glXCreateContext(Display * dpy, XVisualInfo * vis,
354 GLXContext shareList, Bool allowDirect)
356 struct glx_config *config = NULL;
357 int renderType = 0;
359 #if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
360 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, vis->screen);
362 config = glx_config_find_visual(psc->visuals, vis->visualid);
363 if (config == NULL) {
364 xError error;
366 error.errorCode = BadValue;
367 error.resourceID = vis->visualid;
368 error.sequenceNumber = dpy->request;
369 error.type = X_Error;
370 error.majorCode = __glXSetupForCommand(dpy);
371 error.minorCode = X_GLXCreateContext;
372 _XError(dpy, &error);
373 return None;
376 renderType = config->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
377 #endif
379 return CreateContext(dpy, vis->visualid, config, shareList, allowDirect,
380 X_GLXCreateContext, renderType, vis->screen);
383 _X_HIDDEN void
384 glx_send_destroy_context(Display *dpy, XID xid)
386 CARD8 opcode = __glXSetupForCommand(dpy);
387 xGLXDestroyContextReq *req;
389 LockDisplay(dpy);
390 GetReq(GLXDestroyContext, req);
391 req->reqType = opcode;
392 req->glxCode = X_GLXDestroyContext;
393 req->context = xid;
394 UnlockDisplay(dpy);
395 SyncHandle();
399 ** Destroy the named context
401 static void
402 DestroyContext(Display * dpy, GLXContext ctx)
404 struct glx_context *gc = (struct glx_context *) ctx;
406 if (!gc)
407 return;
409 __glXLock();
410 if (gc->currentDpy) {
411 /* This context is bound to some thread. According to the man page,
412 * we should not actually delete the context until it's unbound.
413 * Note that we set gc->xid = None above. In MakeContextCurrent()
414 * we check for that and delete the context there.
416 if (!gc->imported)
417 glx_send_destroy_context(dpy, gc->xid);
418 gc->xid = None;
419 __glXUnlock();
420 return;
422 __glXUnlock();
424 if (gc->vtable->destroy)
425 gc->vtable->destroy(gc);
428 _X_EXPORT void
429 glXDestroyContext(Display * dpy, GLXContext gc)
431 DestroyContext(dpy, gc);
435 ** Return the major and minor version #s for the GLX extension
437 _X_EXPORT Bool
438 glXQueryVersion(Display * dpy, int *major, int *minor)
440 struct glx_display *priv;
442 /* Init the extension. This fetches the major and minor version. */
443 priv = __glXInitialize(dpy);
444 if (!priv)
445 return GL_FALSE;
447 if (major)
448 *major = priv->majorVersion;
449 if (minor)
450 *minor = priv->minorVersion;
451 return GL_TRUE;
455 ** Query the existance of the GLX extension
457 _X_EXPORT Bool
458 glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
460 int major_op, erb, evb;
461 Bool rv;
463 rv = XQueryExtension(dpy, GLX_EXTENSION_NAME, &major_op, &evb, &erb);
464 if (rv) {
465 if (errorBase)
466 *errorBase = erb;
467 if (eventBase)
468 *eventBase = evb;
470 return rv;
474 ** Put a barrier in the token stream that forces the GL to finish its
475 ** work before X can proceed.
477 _X_EXPORT void
478 glXWaitGL(void)
480 struct glx_context *gc = __glXGetCurrentContext();
482 if (gc && gc->vtable->wait_gl)
483 gc->vtable->wait_gl(gc);
487 ** Put a barrier in the token stream that forces X to finish its
488 ** work before GL can proceed.
490 _X_EXPORT void
491 glXWaitX(void)
493 struct glx_context *gc = __glXGetCurrentContext();
495 if (gc && gc->vtable->wait_x)
496 gc->vtable->wait_x(gc);
499 _X_EXPORT void
500 glXUseXFont(Font font, int first, int count, int listBase)
502 struct glx_context *gc = __glXGetCurrentContext();
504 if (gc && gc->vtable->use_x_font)
505 gc->vtable->use_x_font(gc, font, first, count, listBase);
508 /************************************************************************/
511 ** Copy the source context to the destination context using the
512 ** attribute "mask".
514 _X_EXPORT void
515 glXCopyContext(Display * dpy, GLXContext source_user,
516 GLXContext dest_user, unsigned long mask)
518 struct glx_context *source = (struct glx_context *) source_user;
519 struct glx_context *dest = (struct glx_context *) dest_user;
520 #ifdef GLX_USE_APPLEGL
521 struct glx_context *gc = __glXGetCurrentContext();
522 int errorcode;
523 bool x11error;
525 if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
526 mask, &errorcode, &x11error)) {
527 __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
530 #else
531 xGLXCopyContextReq *req;
532 struct glx_context *gc = __glXGetCurrentContext();
533 GLXContextTag tag;
534 CARD8 opcode;
536 opcode = __glXSetupForCommand(dpy);
537 if (!opcode) {
538 return;
541 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
542 if (gc->isDirect) {
543 /* NOT_DONE: This does not work yet */
545 #endif
548 ** If the source is the current context, send its tag so that the context
549 ** can be flushed before the copy.
551 if (source == gc && dpy == gc->currentDpy) {
552 tag = gc->currentContextTag;
554 else {
555 tag = 0;
558 /* Send the glXCopyContext request */
559 LockDisplay(dpy);
560 GetReq(GLXCopyContext, req);
561 req->reqType = opcode;
562 req->glxCode = X_GLXCopyContext;
563 req->source = source ? source->xid : None;
564 req->dest = dest ? dest->xid : None;
565 req->mask = mask;
566 req->contextTag = tag;
567 UnlockDisplay(dpy);
568 SyncHandle();
569 #endif /* GLX_USE_APPLEGL */
574 * Determine if a context uses direct rendering.
576 * \param dpy Display where the context was created.
577 * \param contextID ID of the context to be tested.
579 * \returns \c GL_TRUE if the context is direct rendering or not.
581 static Bool
582 __glXIsDirect(Display * dpy, GLXContextID contextID)
584 #if !defined(USE_XCB)
585 xGLXIsDirectReq *req;
586 xGLXIsDirectReply reply;
587 #endif
588 CARD8 opcode;
590 opcode = __glXSetupForCommand(dpy);
591 if (!opcode) {
592 return GL_FALSE;
595 #ifdef USE_XCB
596 xcb_connection_t *c = XGetXCBConnection(dpy);
597 xcb_glx_is_direct_reply_t *reply = xcb_glx_is_direct_reply(c,
598 xcb_glx_is_direct
599 (c, contextID),
600 NULL);
602 const Bool is_direct = reply->is_direct ? True : False;
603 free(reply);
605 return is_direct;
606 #else
607 /* Send the glXIsDirect request */
608 LockDisplay(dpy);
609 GetReq(GLXIsDirect, req);
610 req->reqType = opcode;
611 req->glxCode = X_GLXIsDirect;
612 req->context = contextID;
613 _XReply(dpy, (xReply *) & reply, 0, False);
614 UnlockDisplay(dpy);
615 SyncHandle();
617 return reply.isDirect;
618 #endif /* USE_XCB */
622 * \todo
623 * Shouldn't this function \b always return \c GL_FALSE when
624 * \c GLX_DIRECT_RENDERING is not defined? Do we really need to bother with
625 * the GLX protocol here at all?
627 _X_EXPORT Bool
628 glXIsDirect(Display * dpy, GLXContext gc_user)
630 struct glx_context *gc = (struct glx_context *) gc_user;
632 if (!gc) {
633 return GL_FALSE;
635 else if (gc->isDirect) {
636 return GL_TRUE;
638 #ifdef GLX_USE_APPLEGL /* TODO: indirect on darwin */
639 return GL_FALSE;
640 #else
641 return __glXIsDirect(dpy, gc->xid);
642 #endif
645 _X_EXPORT GLXPixmap
646 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
648 #ifdef GLX_USE_APPLEGL
649 int screen = vis->screen;
650 struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
651 const struct glx_config *config;
653 config = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
655 if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, config))
656 return None;
658 return pixmap;
659 #else
660 xGLXCreateGLXPixmapReq *req;
661 GLXPixmap xid;
662 CARD8 opcode;
664 opcode = __glXSetupForCommand(dpy);
665 if (!opcode) {
666 return None;
669 /* Send the glXCreateGLXPixmap request */
670 LockDisplay(dpy);
671 GetReq(GLXCreateGLXPixmap, req);
672 req->reqType = opcode;
673 req->glxCode = X_GLXCreateGLXPixmap;
674 req->screen = vis->screen;
675 req->visual = vis->visualid;
676 req->pixmap = pixmap;
677 req->glxpixmap = xid = XAllocID(dpy);
678 UnlockDisplay(dpy);
679 SyncHandle();
681 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
682 do {
683 /* FIXME: Maybe delay __DRIdrawable creation until the drawable
684 * is actually bound to a context... */
686 struct glx_display *const priv = __glXInitialize(dpy);
687 __GLXDRIdrawable *pdraw;
688 struct glx_screen *psc;
689 struct glx_config *config;
691 psc = priv->screens[vis->screen];
692 if (psc->driScreen == NULL)
693 break;
694 config = glx_config_find_visual(psc->visuals, vis->visualid);
695 pdraw = psc->driScreen->createDrawable(psc, pixmap, req->glxpixmap, config);
696 if (pdraw == NULL) {
697 fprintf(stderr, "failed to create pixmap\n");
698 break;
701 if (__glxHashInsert(priv->drawHash, req->glxpixmap, pdraw)) {
702 (*pdraw->destroyDrawable) (pdraw);
703 return None; /* FIXME: Check what we're supposed to do here... */
705 } while (0);
706 #endif
708 return xid;
709 #endif
713 ** Destroy the named pixmap
715 _X_EXPORT void
716 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
718 #ifdef GLX_USE_APPLEGL
719 if(apple_glx_pixmap_destroy(dpy, glxpixmap))
720 __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
721 #else
722 xGLXDestroyGLXPixmapReq *req;
723 CARD8 opcode;
725 opcode = __glXSetupForCommand(dpy);
726 if (!opcode) {
727 return;
730 /* Send the glXDestroyGLXPixmap request */
731 LockDisplay(dpy);
732 GetReq(GLXDestroyGLXPixmap, req);
733 req->reqType = opcode;
734 req->glxCode = X_GLXDestroyGLXPixmap;
735 req->glxpixmap = glxpixmap;
736 UnlockDisplay(dpy);
737 SyncHandle();
739 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
741 struct glx_display *const priv = __glXInitialize(dpy);
742 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
744 if (pdraw != NULL) {
745 (*pdraw->destroyDrawable) (pdraw);
746 __glxHashDelete(priv->drawHash, glxpixmap);
749 #endif
750 #endif /* GLX_USE_APPLEGL */
753 _X_EXPORT void
754 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
756 #ifdef GLX_USE_APPLEGL
757 GLXContext gc = glXGetCurrentContext();
758 if(gc && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
759 apple_glx_swap_buffers(gc->driContext);
760 } else {
761 __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
763 #else
764 struct glx_context *gc;
765 GLXContextTag tag;
766 CARD8 opcode;
767 #ifdef USE_XCB
768 xcb_connection_t *c;
769 #else
770 xGLXSwapBuffersReq *req;
771 #endif
773 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
774 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
776 if (pdraw != NULL) {
777 glFlush();
778 (*pdraw->psc->driScreen->swapBuffers)(pdraw, 0, 0, 0);
779 return;
781 #endif
783 opcode = __glXSetupForCommand(dpy);
784 if (!opcode) {
785 return;
789 ** The calling thread may or may not have a current context. If it
790 ** does, send the context tag so the server can do a flush.
792 gc = __glXGetCurrentContext();
793 if ((gc != NULL) && (dpy == gc->currentDpy) &&
794 ((drawable == gc->currentDrawable)
795 || (drawable == gc->currentReadable))) {
796 tag = gc->currentContextTag;
798 else {
799 tag = 0;
802 #ifdef USE_XCB
803 c = XGetXCBConnection(dpy);
804 xcb_glx_swap_buffers(c, tag, drawable);
805 xcb_flush(c);
806 #else
807 /* Send the glXSwapBuffers request */
808 LockDisplay(dpy);
809 GetReq(GLXSwapBuffers, req);
810 req->reqType = opcode;
811 req->glxCode = X_GLXSwapBuffers;
812 req->drawable = drawable;
813 req->contextTag = tag;
814 UnlockDisplay(dpy);
815 SyncHandle();
816 XFlush(dpy);
817 #endif /* USE_XCB */
818 #endif /* GLX_USE_APPLEGL */
823 ** Return configuration information for the given display, screen and
824 ** visual combination.
826 _X_EXPORT int
827 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
828 int *value_return)
830 struct glx_display *priv;
831 struct glx_screen *psc;
832 struct glx_config *config;
833 int status;
835 status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
836 if (status == Success) {
837 config = glx_config_find_visual(psc->visuals, vis->visualid);
839 /* Lookup attribute after first finding a match on the visual */
840 if (config != NULL) {
841 return glx_config_get(config, attribute, value_return);
844 status = GLX_BAD_VISUAL;
848 ** If we can't find the config for this visual, this visual is not
849 ** supported by the OpenGL implementation on the server.
851 if ((status == GLX_BAD_VISUAL) && (attribute == GLX_USE_GL)) {
852 *value_return = GL_FALSE;
853 status = Success;
856 return status;
859 /************************************************************************/
861 static void
862 init_fbconfig_for_chooser(struct glx_config * config,
863 GLboolean fbconfig_style_tags)
865 memset(config, 0, sizeof(struct glx_config));
866 config->visualID = (XID) GLX_DONT_CARE;
867 config->visualType = GLX_DONT_CARE;
869 /* glXChooseFBConfig specifies different defaults for these two than
870 * glXChooseVisual.
872 if (fbconfig_style_tags) {
873 config->rgbMode = GL_TRUE;
874 config->doubleBufferMode = GLX_DONT_CARE;
877 config->visualRating = GLX_DONT_CARE;
878 config->transparentPixel = GLX_NONE;
879 config->transparentRed = GLX_DONT_CARE;
880 config->transparentGreen = GLX_DONT_CARE;
881 config->transparentBlue = GLX_DONT_CARE;
882 config->transparentAlpha = GLX_DONT_CARE;
883 config->transparentIndex = GLX_DONT_CARE;
885 config->drawableType = GLX_WINDOW_BIT;
886 config->renderType =
887 (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
888 config->xRenderable = GLX_DONT_CARE;
889 config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
891 config->swapMethod = GLX_DONT_CARE;
894 #define MATCH_DONT_CARE( param ) \
895 do { \
896 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
897 && (a-> param != b-> param) ) { \
898 return False; \
900 } while ( 0 )
902 #define MATCH_MINIMUM( param ) \
903 do { \
904 if ( ((int) a-> param != (int) GLX_DONT_CARE) \
905 && (a-> param > b-> param) ) { \
906 return False; \
908 } while ( 0 )
910 #define MATCH_EXACT( param ) \
911 do { \
912 if ( a-> param != b-> param) { \
913 return False; \
915 } while ( 0 )
917 /* Test that all bits from a are contained in b */
918 #define MATCH_MASK(param) \
919 do { \
920 if ((a->param & ~b->param) != 0) \
921 return False; \
922 } while (0);
925 * Determine if two GLXFBConfigs are compatible.
927 * \param a Application specified config to test.
928 * \param b Server specified config to test against \c a.
930 static Bool
931 fbconfigs_compatible(const struct glx_config * const a,
932 const struct glx_config * const b)
934 MATCH_DONT_CARE(doubleBufferMode);
935 MATCH_DONT_CARE(visualType);
936 MATCH_DONT_CARE(visualRating);
937 MATCH_DONT_CARE(xRenderable);
938 MATCH_DONT_CARE(fbconfigID);
939 MATCH_DONT_CARE(swapMethod);
941 MATCH_MINIMUM(rgbBits);
942 MATCH_MINIMUM(numAuxBuffers);
943 MATCH_MINIMUM(redBits);
944 MATCH_MINIMUM(greenBits);
945 MATCH_MINIMUM(blueBits);
946 MATCH_MINIMUM(alphaBits);
947 MATCH_MINIMUM(depthBits);
948 MATCH_MINIMUM(stencilBits);
949 MATCH_MINIMUM(accumRedBits);
950 MATCH_MINIMUM(accumGreenBits);
951 MATCH_MINIMUM(accumBlueBits);
952 MATCH_MINIMUM(accumAlphaBits);
953 MATCH_MINIMUM(sampleBuffers);
954 MATCH_MINIMUM(maxPbufferWidth);
955 MATCH_MINIMUM(maxPbufferHeight);
956 MATCH_MINIMUM(maxPbufferPixels);
957 MATCH_MINIMUM(samples);
959 MATCH_DONT_CARE(stereoMode);
960 MATCH_EXACT(level);
962 MATCH_MASK(drawableType);
963 MATCH_MASK(renderType);
965 /* There is a bug in a few of the XFree86 DDX drivers. They contain
966 * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
967 * Technically speaking, it is a bug in the DDX driver, but there is
968 * enough of an installed base to work around the problem here. In any
969 * case, 0 is not a valid value of the transparent type, so we'll treat 0
970 * from the app as GLX_DONT_CARE. We'll consider GLX_NONE from the app and
971 * 0 from the server to be a match to maintain backward compatibility with
972 * the (broken) drivers.
975 if (a->transparentPixel != (int) GLX_DONT_CARE && a->transparentPixel != 0) {
976 if (a->transparentPixel == GLX_NONE) {
977 if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
978 return False;
980 else {
981 MATCH_EXACT(transparentPixel);
984 switch (a->transparentPixel) {
985 case GLX_TRANSPARENT_RGB:
986 MATCH_DONT_CARE(transparentRed);
987 MATCH_DONT_CARE(transparentGreen);
988 MATCH_DONT_CARE(transparentBlue);
989 MATCH_DONT_CARE(transparentAlpha);
990 break;
992 case GLX_TRANSPARENT_INDEX:
993 MATCH_DONT_CARE(transparentIndex);
994 break;
996 default:
997 break;
1001 return True;
1005 /* There's some trickly language in the GLX spec about how this is supposed
1006 * to work. Basically, if a given component size is either not specified
1007 * or the requested size is zero, it is supposed to act like PERFER_SMALLER.
1008 * Well, that's really hard to do with the code as-is. This behavior is
1009 * closer to correct, but still not technically right.
1011 #define PREFER_LARGER_OR_ZERO(comp) \
1012 do { \
1013 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1014 if ( ((*a)-> comp) == 0 ) { \
1015 return -1; \
1017 else if ( ((*b)-> comp) == 0 ) { \
1018 return 1; \
1020 else { \
1021 return ((*b)-> comp) - ((*a)-> comp) ; \
1024 } while( 0 )
1026 #define PREFER_LARGER(comp) \
1027 do { \
1028 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1029 return ((*b)-> comp) - ((*a)-> comp) ; \
1031 } while( 0 )
1033 #define PREFER_SMALLER(comp) \
1034 do { \
1035 if ( ((*a)-> comp) != ((*b)-> comp) ) { \
1036 return ((*a)-> comp) - ((*b)-> comp) ; \
1038 } while( 0 )
1041 * Compare two GLXFBConfigs. This function is intended to be used as the
1042 * compare function passed in to qsort.
1044 * \returns If \c a is a "better" config, according to the specification of
1045 * SGIX_fbconfig, a number less than zero is returned. If \c b is
1046 * better, then a number greater than zero is return. If both are
1047 * equal, zero is returned.
1048 * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1050 static int
1051 fbconfig_compare(struct glx_config **a, struct glx_config **b)
1053 /* The order of these comparisons must NOT change. It is defined by
1054 * the GLX 1.3 spec and ARB_multisample.
1057 PREFER_SMALLER(visualSelectGroup);
1059 /* The sort order for the visualRating is GLX_NONE, GLX_SLOW, and
1060 * GLX_NON_CONFORMANT_CONFIG. It just so happens that this is the
1061 * numerical sort order of the enums (0x8000, 0x8001, and 0x800D).
1063 PREFER_SMALLER(visualRating);
1065 /* This isn't quite right. It is supposed to compare the sum of the
1066 * components the user specifically set minimums for.
1068 PREFER_LARGER_OR_ZERO(redBits);
1069 PREFER_LARGER_OR_ZERO(greenBits);
1070 PREFER_LARGER_OR_ZERO(blueBits);
1071 PREFER_LARGER_OR_ZERO(alphaBits);
1073 PREFER_SMALLER(rgbBits);
1075 if (((*a)->doubleBufferMode != (*b)->doubleBufferMode)) {
1076 /* Prefer single-buffer.
1078 return (!(*a)->doubleBufferMode) ? -1 : 1;
1081 PREFER_SMALLER(numAuxBuffers);
1083 PREFER_LARGER_OR_ZERO(depthBits);
1084 PREFER_SMALLER(stencilBits);
1086 /* This isn't quite right. It is supposed to compare the sum of the
1087 * components the user specifically set minimums for.
1089 PREFER_LARGER_OR_ZERO(accumRedBits);
1090 PREFER_LARGER_OR_ZERO(accumGreenBits);
1091 PREFER_LARGER_OR_ZERO(accumBlueBits);
1092 PREFER_LARGER_OR_ZERO(accumAlphaBits);
1094 PREFER_SMALLER(visualType);
1096 /* None of the multisample specs say where this comparison should happen,
1097 * so I put it near the end.
1099 PREFER_SMALLER(sampleBuffers);
1100 PREFER_SMALLER(samples);
1102 /* None of the pbuffer or fbconfig specs say that this comparison needs
1103 * to happen at all, but it seems like it should.
1105 PREFER_LARGER(maxPbufferWidth);
1106 PREFER_LARGER(maxPbufferHeight);
1107 PREFER_LARGER(maxPbufferPixels);
1109 return 0;
1114 * Selects and sorts a subset of the supplied configs based on the attributes.
1115 * This function forms to basis of \c glXChooseVisual, \c glXChooseFBConfig,
1116 * and \c glXChooseFBConfigSGIX.
1118 * \param configs Array of pointers to possible configs. The elements of
1119 * this array that do not meet the criteria will be set to
1120 * NULL. The remaining elements will be sorted according to
1121 * the various visual / FBConfig selection rules.
1122 * \param num_configs Number of elements in the \c configs array.
1123 * \param attribList Attributes used select from \c configs. This array is
1124 * terminated by a \c None tag. The array can either take
1125 * the form expected by \c glXChooseVisual (where boolean
1126 * tags do not have a value) or by \c glXChooseFBConfig
1127 * (where every tag has a value).
1128 * \param fbconfig_style_tags Selects whether \c attribList is in
1129 * \c glXChooseVisual style or
1130 * \c glXChooseFBConfig style.
1131 * \returns The number of valid elements left in \c configs.
1133 * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
1135 static int
1136 choose_visual(struct glx_config ** configs, int num_configs,
1137 const int *attribList, GLboolean fbconfig_style_tags)
1139 struct glx_config test_config;
1140 int base;
1141 int i;
1143 /* This is a fairly direct implementation of the selection method
1144 * described by GLX_SGIX_fbconfig. Start by culling out all the
1145 * configs that are not compatible with the selected parameter
1146 * list.
1149 init_fbconfig_for_chooser(&test_config, fbconfig_style_tags);
1150 __glXInitializeVisualConfigFromTags(&test_config, 512,
1151 (const INT32 *) attribList,
1152 GL_TRUE, fbconfig_style_tags);
1154 base = 0;
1155 for (i = 0; i < num_configs; i++) {
1156 if (fbconfigs_compatible(&test_config, configs[i])) {
1157 configs[base] = configs[i];
1158 base++;
1162 if (base == 0) {
1163 return 0;
1166 if (base < num_configs) {
1167 (void) memset(&configs[base], 0, sizeof(void *) * (num_configs - base));
1170 /* After the incompatible configs are removed, the resulting
1171 * list is sorted according to the rules set out in the various
1172 * specifications.
1175 qsort(configs, base, sizeof(struct glx_config *),
1176 (int (*)(const void *, const void *)) fbconfig_compare);
1177 return base;
1184 ** Return the visual that best matches the template. Return None if no
1185 ** visual matches the template.
1187 _X_EXPORT XVisualInfo *
1188 glXChooseVisual(Display * dpy, int screen, int *attribList)
1190 XVisualInfo *visualList = NULL;
1191 struct glx_display *priv;
1192 struct glx_screen *psc;
1193 struct glx_config test_config;
1194 struct glx_config *config;
1195 struct glx_config *best_config = NULL;
1198 ** Get a list of all visuals, return if list is empty
1200 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1201 return None;
1206 ** Build a template from the defaults and the attribute list
1207 ** Free visual list and return if an unexpected token is encountered
1209 init_fbconfig_for_chooser(&test_config, GL_FALSE);
1210 __glXInitializeVisualConfigFromTags(&test_config, 512,
1211 (const INT32 *) attribList,
1212 GL_TRUE, GL_FALSE);
1215 ** Eliminate visuals that don't meet minimum requirements
1216 ** Compute a score for those that do
1217 ** Remember which visual, if any, got the highest score
1218 ** If no visual is acceptable, return None
1219 ** Otherwise, create an XVisualInfo list with just the selected X visual
1220 ** and return this.
1222 for (config = psc->visuals; config != NULL; config = config->next) {
1223 if (fbconfigs_compatible(&test_config, config)
1224 && ((best_config == NULL) ||
1225 (fbconfig_compare (&config, &best_config) < 0))) {
1226 XVisualInfo visualTemplate;
1227 XVisualInfo *newList;
1228 int i;
1230 visualTemplate.screen = screen;
1231 visualTemplate.visualid = config->visualID;
1232 newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
1233 &visualTemplate, &i);
1235 if (newList) {
1236 Xfree(visualList);
1237 visualList = newList;
1238 best_config = config;
1243 #ifdef GLX_USE_APPLEGL
1244 if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
1245 printf("visualid 0x%lx\n", visualList[0].visualid);
1247 #endif
1249 return visualList;
1253 _X_EXPORT const char *
1254 glXQueryExtensionsString(Display * dpy, int screen)
1256 struct glx_screen *psc;
1257 struct glx_display *priv;
1259 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1260 return NULL;
1263 if (!psc->effectiveGLXexts) {
1264 if (!psc->serverGLXexts) {
1265 psc->serverGLXexts =
1266 __glXQueryServerString(dpy, priv->majorOpcode, screen,
1267 GLX_EXTENSIONS);
1270 __glXCalculateUsableExtensions(psc,
1271 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
1272 (psc->driScreen != NULL),
1273 #else
1274 GL_FALSE,
1275 #endif
1276 priv->minorVersion);
1279 return psc->effectiveGLXexts;
1282 _X_EXPORT const char *
1283 glXGetClientString(Display * dpy, int name)
1285 (void) dpy;
1287 switch (name) {
1288 case GLX_VENDOR:
1289 return (__glXGLXClientVendorName);
1290 case GLX_VERSION:
1291 return (__glXGLXClientVersion);
1292 case GLX_EXTENSIONS:
1293 return (__glXGetClientExtensions());
1294 default:
1295 return NULL;
1299 _X_EXPORT const char *
1300 glXQueryServerString(Display * dpy, int screen, int name)
1302 struct glx_screen *psc;
1303 struct glx_display *priv;
1304 const char **str;
1307 if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
1308 return NULL;
1311 switch (name) {
1312 case GLX_VENDOR:
1313 str = &priv->serverGLXvendor;
1314 break;
1315 case GLX_VERSION:
1316 str = &priv->serverGLXversion;
1317 break;
1318 case GLX_EXTENSIONS:
1319 str = &psc->serverGLXexts;
1320 break;
1321 default:
1322 return NULL;
1325 if (*str == NULL) {
1326 *str = __glXQueryServerString(dpy, priv->majorOpcode, screen, name);
1329 return *str;
1332 void
1333 __glXClientInfo(Display * dpy, int opcode)
1335 char *ext_str = __glXGetClientGLExtensionString();
1336 int size = strlen(ext_str) + 1;
1338 #ifdef USE_XCB
1339 xcb_connection_t *c = XGetXCBConnection(dpy);
1340 xcb_glx_client_info(c,
1341 GLX_MAJOR_VERSION, GLX_MINOR_VERSION, size, ext_str);
1342 #else
1343 xGLXClientInfoReq *req;
1345 /* Send the glXClientInfo request */
1346 LockDisplay(dpy);
1347 GetReq(GLXClientInfo, req);
1348 req->reqType = opcode;
1349 req->glxCode = X_GLXClientInfo;
1350 req->major = GLX_MAJOR_VERSION;
1351 req->minor = GLX_MINOR_VERSION;
1353 req->length += (size + 3) >> 2;
1354 req->numbytes = size;
1355 Data(dpy, ext_str, size);
1357 UnlockDisplay(dpy);
1358 SyncHandle();
1359 #endif /* USE_XCB */
1361 Xfree(ext_str);
1366 ** EXT_import_context
1369 _X_EXPORT Display *
1370 glXGetCurrentDisplay(void)
1372 struct glx_context *gc = __glXGetCurrentContext();
1373 if (NULL == gc)
1374 return NULL;
1375 return gc->currentDpy;
1378 _X_EXPORT
1379 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
1380 glXGetCurrentDisplay)
1382 #ifndef GLX_USE_APPLEGL
1383 _X_EXPORT GLXContext
1384 glXImportContextEXT(Display *dpy, GLXContextID contextID)
1386 struct glx_display *priv = __glXInitialize(dpy);
1387 struct glx_screen *psc;
1388 xGLXQueryContextReply reply;
1389 CARD8 opcode;
1390 struct glx_context *ctx;
1391 int propList[__GLX_MAX_CONTEXT_PROPS * 2], *pProp, nPropListBytes;
1392 int i, renderType;
1393 XID share;
1394 struct glx_config *mode;
1396 if (contextID == None || __glXIsDirect(dpy, contextID))
1397 return NULL;
1399 opcode = __glXSetupForCommand(dpy);
1400 if (!opcode)
1401 return 0;
1403 /* Send the glXQueryContextInfoEXT request */
1404 LockDisplay(dpy);
1406 if (priv->majorVersion > 1 || priv->minorVersion >= 3) {
1407 xGLXQueryContextReq *req;
1409 GetReq(GLXQueryContext, req);
1411 req->reqType = opcode;
1412 req->glxCode = X_GLXQueryContext;
1413 req->context = contextID;
1415 else {
1416 xGLXVendorPrivateReq *vpreq;
1417 xGLXQueryContextInfoEXTReq *req;
1419 GetReqExtra(GLXVendorPrivate,
1420 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
1421 vpreq);
1422 req = (xGLXQueryContextInfoEXTReq *) vpreq;
1423 req->reqType = opcode;
1424 req->glxCode = X_GLXVendorPrivateWithReply;
1425 req->vendorCode = X_GLXvop_QueryContextInfoEXT;
1426 req->context = contextID;
1429 _XReply(dpy, (xReply *) & reply, 0, False);
1431 if (reply.n <= __GLX_MAX_CONTEXT_PROPS)
1432 nPropListBytes = reply.n * 2 * sizeof propList[0];
1433 else
1434 nPropListBytes = 0;
1435 _XRead(dpy, (char *) propList, nPropListBytes);
1436 UnlockDisplay(dpy);
1437 SyncHandle();
1439 /* Look up screen first so we can look up visuals/fbconfigs later */
1440 psc = NULL;
1441 for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1442 if (pProp[0] == GLX_SCREEN)
1443 psc = GetGLXScreenConfigs(dpy, pProp[1]);
1444 if (psc == NULL)
1445 return NULL;
1447 share = None;
1448 mode = NULL;
1449 renderType = 0;
1450 pProp = propList;
1452 for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
1453 switch (pProp[0]) {
1454 case GLX_SHARE_CONTEXT_EXT:
1455 share = pProp[1];
1456 break;
1457 case GLX_VISUAL_ID_EXT:
1458 mode = glx_config_find_visual(psc->visuals, pProp[1]);
1459 break;
1460 case GLX_FBCONFIG_ID:
1461 mode = glx_config_find_fbconfig(psc->configs, pProp[1]);
1462 break;
1463 case GLX_RENDER_TYPE:
1464 renderType = pProp[1];
1465 break;
1468 if (mode == NULL)
1469 return NULL;
1471 ctx = indirect_create_context(psc, mode, NULL, renderType);
1472 if (ctx == NULL)
1473 return NULL;
1475 ctx->xid = contextID;
1476 ctx->imported = GL_TRUE;
1477 ctx->share_xid = share;
1479 return (GLXContext) ctx;
1482 #endif
1484 _X_EXPORT int
1485 glXQueryContext(Display * dpy, GLXContext ctx_user, int attribute, int *value)
1487 struct glx_context *ctx = (struct glx_context *) ctx_user;
1489 switch (attribute) {
1490 case GLX_SHARE_CONTEXT_EXT:
1491 *value = ctx->share_xid;
1492 break;
1493 case GLX_VISUAL_ID_EXT:
1494 *value = ctx->config ? ctx->config->visualID : None;
1495 break;
1496 case GLX_SCREEN:
1497 *value = ctx->screen;
1498 break;
1499 case GLX_FBCONFIG_ID:
1500 *value = ctx->config ? ctx->config->fbconfigID : None;
1501 break;
1502 case GLX_RENDER_TYPE:
1503 *value = ctx->renderType;
1504 break;
1505 default:
1506 return GLX_BAD_ATTRIBUTE;
1508 return Success;
1511 _X_EXPORT
1512 GLX_ALIAS(int, glXQueryContextInfoEXT,
1513 (Display * dpy, GLXContext ctx, int attribute, int *value),
1514 (dpy, ctx, attribute, value), glXQueryContext)
1516 _X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx_user)
1518 struct glx_context *ctx = (struct glx_context *) ctx_user;
1520 return ctx->xid;
1523 _X_EXPORT void
1524 glXFreeContextEXT(Display * dpy, GLXContext ctx)
1526 DestroyContext(dpy, ctx);
1530 _X_EXPORT GLXFBConfig *
1531 glXChooseFBConfig(Display * dpy, int screen,
1532 const int *attribList, int *nitems)
1534 struct glx_config **config_list;
1535 int list_size;
1538 config_list = (struct glx_config **)
1539 glXGetFBConfigs(dpy, screen, &list_size);
1541 if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
1542 list_size = choose_visual(config_list, list_size, attribList, GL_TRUE);
1543 if (list_size == 0) {
1544 XFree(config_list);
1545 config_list = NULL;
1549 *nitems = list_size;
1550 return (GLXFBConfig *) config_list;
1554 _X_EXPORT GLXContext
1555 glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
1556 int renderType, GLXContext shareList, Bool allowDirect)
1558 struct glx_config *config = (struct glx_config *) fbconfig;
1560 return CreateContext(dpy, config->fbconfigID, config, shareList,
1561 allowDirect, X_GLXCreateNewContext, renderType,
1562 config->screen);
1566 _X_EXPORT GLXDrawable
1567 glXGetCurrentReadDrawable(void)
1569 struct glx_context *gc = __glXGetCurrentContext();
1571 return gc->currentReadable;
1575 _X_EXPORT GLXFBConfig *
1576 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
1578 struct glx_display *priv = __glXInitialize(dpy);
1579 struct glx_config **config_list = NULL;
1580 struct glx_config *config;
1581 unsigned num_configs = 0;
1582 int i;
1584 *nelements = 0;
1585 if (priv && (priv->screens != NULL)
1586 && (screen >= 0) && (screen <= ScreenCount(dpy))
1587 && (priv->screens[screen]->configs != NULL)
1588 && (priv->screens[screen]->configs->fbconfigID
1589 != (int) GLX_DONT_CARE)) {
1591 for (config = priv->screens[screen]->configs; config != NULL;
1592 config = config->next) {
1593 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1594 num_configs++;
1598 config_list = Xmalloc(num_configs * sizeof *config_list);
1599 if (config_list != NULL) {
1600 *nelements = num_configs;
1601 i = 0;
1602 for (config = priv->screens[screen]->configs; config != NULL;
1603 config = config->next) {
1604 if (config->fbconfigID != (int) GLX_DONT_CARE) {
1605 config_list[i] = config;
1606 i++;
1612 return (GLXFBConfig *) config_list;
1616 _X_EXPORT int
1617 glXGetFBConfigAttrib(Display * dpy, GLXFBConfig fbconfig,
1618 int attribute, int *value)
1620 struct glx_config *config = ValidateGLXFBConfig(dpy, fbconfig);
1622 if (config == NULL)
1623 return GLXBadFBConfig;
1625 return glx_config_get(config, attribute, value);
1629 _X_EXPORT XVisualInfo *
1630 glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig fbconfig)
1632 XVisualInfo visualTemplate;
1633 struct glx_config *config = (struct glx_config *) fbconfig;
1634 int count;
1637 ** Get a list of all visuals, return if list is empty
1639 visualTemplate.visualid = config->visualID;
1640 return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
1643 #ifndef GLX_USE_APPLEGL
1645 ** GLX_SGI_swap_control
1647 static int
1648 __glXSwapIntervalSGI(int interval)
1650 xGLXVendorPrivateReq *req;
1651 struct glx_context *gc = __glXGetCurrentContext();
1652 struct glx_screen *psc;
1653 Display *dpy;
1654 CARD32 *interval_ptr;
1655 CARD8 opcode;
1657 if (gc == NULL) {
1658 return GLX_BAD_CONTEXT;
1661 if (interval <= 0) {
1662 return GLX_BAD_VALUE;
1665 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1667 #ifdef GLX_DIRECT_RENDERING
1668 if (gc->isDirect && psc->driScreen && psc->driScreen->setSwapInterval) {
1669 __GLXDRIdrawable *pdraw =
1670 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1671 psc->driScreen->setSwapInterval(pdraw, interval);
1672 return 0;
1674 #endif
1676 dpy = gc->currentDpy;
1677 opcode = __glXSetupForCommand(dpy);
1678 if (!opcode) {
1679 return 0;
1682 /* Send the glXSwapIntervalSGI request */
1683 LockDisplay(dpy);
1684 GetReqExtra(GLXVendorPrivate, sizeof(CARD32), req);
1685 req->reqType = opcode;
1686 req->glxCode = X_GLXVendorPrivate;
1687 req->vendorCode = X_GLXvop_SwapIntervalSGI;
1688 req->contextTag = gc->currentContextTag;
1690 interval_ptr = (CARD32 *) (req + 1);
1691 *interval_ptr = interval;
1693 UnlockDisplay(dpy);
1694 SyncHandle();
1695 XFlush(dpy);
1697 return 0;
1702 ** GLX_MESA_swap_control
1704 static int
1705 __glXSwapIntervalMESA(unsigned int interval)
1707 #ifdef GLX_DIRECT_RENDERING
1708 struct glx_context *gc = __glXGetCurrentContext();
1710 if (gc != NULL && gc->isDirect) {
1711 struct glx_screen *psc;
1713 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1714 if (psc->driScreen && psc->driScreen->setSwapInterval) {
1715 __GLXDRIdrawable *pdraw =
1716 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1717 return psc->driScreen->setSwapInterval(pdraw, interval);
1720 #endif
1722 return GLX_BAD_CONTEXT;
1726 static int
1727 __glXGetSwapIntervalMESA(void)
1729 #ifdef GLX_DIRECT_RENDERING
1730 struct glx_context *gc = __glXGetCurrentContext();
1732 if (gc != NULL && gc->isDirect) {
1733 struct glx_screen *psc;
1735 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1736 if (psc->driScreen && psc->driScreen->getSwapInterval) {
1737 __GLXDRIdrawable *pdraw =
1738 GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1739 return psc->driScreen->getSwapInterval(pdraw);
1742 #endif
1744 return 0;
1749 ** GLX_SGI_video_sync
1751 static int
1752 __glXGetVideoSyncSGI(unsigned int *count)
1754 int64_t ust, msc, sbc;
1755 int ret;
1756 struct glx_context *gc = __glXGetCurrentContext();
1757 struct glx_screen *psc;
1758 #ifdef GLX_DIRECT_RENDERING
1759 __GLXDRIdrawable *pdraw;
1760 #endif
1762 if (!gc)
1763 return GLX_BAD_CONTEXT;
1765 #ifdef GLX_DIRECT_RENDERING
1766 if (!gc->isDirect)
1767 return GLX_BAD_CONTEXT;
1768 #endif
1770 psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
1771 #ifdef GLX_DIRECT_RENDERING
1772 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1773 #endif
1775 /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
1776 * FIXME: there should be a GLX encoding for this call. I can find no
1777 * FIXME: documentation for the GLX encoding.
1779 #ifdef GLX_DIRECT_RENDERING
1780 if (psc->driScreen && psc->driScreen->getDrawableMSC) {
1781 ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
1782 *count = (unsigned) msc;
1783 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1785 #endif
1787 return GLX_BAD_CONTEXT;
1790 static int
1791 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
1793 struct glx_context *gc = __glXGetCurrentContext();
1794 struct glx_screen *psc;
1795 #ifdef GLX_DIRECT_RENDERING
1796 __GLXDRIdrawable *pdraw;
1797 #endif
1798 int64_t ust, msc, sbc;
1799 int ret;
1801 if (divisor <= 0 || remainder < 0)
1802 return GLX_BAD_VALUE;
1804 if (!gc)
1805 return GLX_BAD_CONTEXT;
1807 #ifdef GLX_DIRECT_RENDERING
1808 if (!gc->isDirect)
1809 return GLX_BAD_CONTEXT;
1810 #endif
1812 psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
1813 #ifdef GLX_DIRECT_RENDERING
1814 pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
1815 #endif
1817 #ifdef GLX_DIRECT_RENDERING
1818 if (psc->driScreen && psc->driScreen->waitForMSC) {
1819 ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
1820 &sbc);
1821 *count = (unsigned) msc;
1822 return (ret == True) ? 0 : GLX_BAD_CONTEXT;
1824 #endif
1826 return GLX_BAD_CONTEXT;
1829 #endif /* GLX_USE_APPLEGL */
1832 ** GLX_SGIX_fbconfig
1833 ** Many of these functions are aliased to GLX 1.3 entry points in the
1834 ** GLX_functions table.
1837 _X_EXPORT
1838 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
1839 (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
1840 (dpy, config, attribute, value), glXGetFBConfigAttrib)
1842 _X_EXPORT GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
1843 (Display * dpy, int screen, int *attrib_list,
1844 int *nelements), (dpy, screen, attrib_list, nelements),
1845 glXChooseFBConfig)
1847 _X_EXPORT GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
1848 (Display * dpy, GLXFBConfigSGIX config),
1849 (dpy, config), glXGetVisualFromFBConfig)
1851 _X_EXPORT GLXPixmap
1852 glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
1853 GLXFBConfigSGIX fbconfig,
1854 Pixmap pixmap)
1856 #ifndef GLX_USE_APPLEGL
1857 xGLXVendorPrivateWithReplyReq *vpreq;
1858 xGLXCreateGLXPixmapWithConfigSGIXReq *req;
1859 GLXPixmap xid = None;
1860 CARD8 opcode;
1861 struct glx_screen *psc;
1862 #endif
1863 struct glx_config *config = (struct glx_config *) fbconfig;
1866 if ((dpy == NULL) || (config == NULL)) {
1867 return None;
1869 #ifdef GLX_USE_APPLEGL
1870 if(apple_glx_pixmap_create(dpy, config->screen, pixmap, config))
1871 return None;
1872 return pixmap;
1873 #else
1875 psc = GetGLXScreenConfigs(dpy, config->screen);
1876 if ((psc != NULL)
1877 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1878 opcode = __glXSetupForCommand(dpy);
1879 if (!opcode) {
1880 return None;
1883 /* Send the glXCreateGLXPixmapWithConfigSGIX request */
1884 LockDisplay(dpy);
1885 GetReqExtra(GLXVendorPrivateWithReply,
1886 sz_xGLXCreateGLXPixmapWithConfigSGIXReq -
1887 sz_xGLXVendorPrivateWithReplyReq, vpreq);
1888 req = (xGLXCreateGLXPixmapWithConfigSGIXReq *) vpreq;
1889 req->reqType = opcode;
1890 req->glxCode = X_GLXVendorPrivateWithReply;
1891 req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
1892 req->screen = config->screen;
1893 req->fbconfig = config->fbconfigID;
1894 req->pixmap = pixmap;
1895 req->glxpixmap = xid = XAllocID(dpy);
1896 UnlockDisplay(dpy);
1897 SyncHandle();
1900 return xid;
1901 #endif
1904 _X_EXPORT GLXContext
1905 glXCreateContextWithConfigSGIX(Display * dpy,
1906 GLXFBConfigSGIX fbconfig, int renderType,
1907 GLXContext shareList, Bool allowDirect)
1909 GLXContext gc = NULL;
1910 struct glx_config *config = (struct glx_config *) fbconfig;
1911 struct glx_screen *psc;
1914 if ((dpy == NULL) || (config == NULL)) {
1915 return None;
1918 psc = GetGLXScreenConfigs(dpy, config->screen);
1919 if ((psc != NULL)
1920 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
1921 gc = CreateContext(dpy, config->fbconfigID, config, shareList,
1922 allowDirect,
1923 X_GLXvop_CreateContextWithConfigSGIX, renderType,
1924 config->screen);
1927 return gc;
1931 _X_EXPORT GLXFBConfigSGIX
1932 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
1934 struct glx_display *priv;
1935 struct glx_screen *psc = NULL;
1937 if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) != Success)
1938 && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
1939 && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
1940 return (GLXFBConfigSGIX) glx_config_find_visual(psc->configs,
1941 vis->visualid);
1944 return NULL;
1947 #ifndef GLX_USE_APPLEGL
1949 ** GLX_SGIX_swap_group
1951 static void
1952 __glXJoinSwapGroupSGIX(Display * dpy, GLXDrawable drawable,
1953 GLXDrawable member)
1955 (void) dpy;
1956 (void) drawable;
1957 (void) member;
1962 ** GLX_SGIX_swap_barrier
1964 static void
1965 __glXBindSwapBarrierSGIX(Display * dpy, GLXDrawable drawable, int barrier)
1967 (void) dpy;
1968 (void) drawable;
1969 (void) barrier;
1972 static Bool
1973 __glXQueryMaxSwapBarriersSGIX(Display * dpy, int screen, int *max)
1975 (void) dpy;
1976 (void) screen;
1977 (void) max;
1978 return False;
1983 ** GLX_OML_sync_control
1985 static Bool
1986 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
1987 int64_t * ust, int64_t * msc, int64_t * sbc)
1989 struct glx_display * const priv = __glXInitialize(dpy);
1990 int ret;
1991 #ifdef GLX_DIRECT_RENDERING
1992 __GLXDRIdrawable *pdraw;
1993 #endif
1994 struct glx_screen *psc;
1996 if (!priv)
1997 return False;
1999 #ifdef GLX_DIRECT_RENDERING
2000 pdraw = GetGLXDRIDrawable(dpy, drawable);
2001 psc = pdraw ? pdraw->psc : NULL;
2002 if (pdraw && psc->driScreen->getDrawableMSC) {
2003 ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
2004 return ret;
2006 #endif
2008 return False;
2011 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2012 _X_HIDDEN GLboolean
2013 __glxGetMscRate(__GLXDRIdrawable *glxDraw,
2014 int32_t * numerator, int32_t * denominator)
2016 #ifdef XF86VIDMODE
2017 struct glx_screen *psc;
2018 XF86VidModeModeLine mode_line;
2019 int dot_clock;
2020 int i;
2022 psc = glxDraw->psc;
2023 if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
2024 XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
2025 unsigned n = dot_clock * 1000;
2026 unsigned d = mode_line.vtotal * mode_line.htotal;
2028 # define V_INTERLACE 0x010
2029 # define V_DBLSCAN 0x020
2031 if (mode_line.flags & V_INTERLACE)
2032 n *= 2;
2033 else if (mode_line.flags & V_DBLSCAN)
2034 d *= 2;
2036 /* The OML_sync_control spec requires that if the refresh rate is a
2037 * whole number, that the returned numerator be equal to the refresh
2038 * rate and the denominator be 1.
2041 if (n % d == 0) {
2042 n /= d;
2043 d = 1;
2045 else {
2046 static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
2048 /* This is a poor man's way to reduce a fraction. It's far from
2049 * perfect, but it will work well enough for this situation.
2052 for (i = 0; f[i] != 0; i++) {
2053 while (n % f[i] == 0 && d % f[i] == 0) {
2054 d /= f[i];
2055 n /= f[i];
2060 *numerator = n;
2061 *denominator = d;
2063 return True;
2065 else
2066 return False;
2067 #else
2068 (void) draw;
2069 (void) numerator;
2070 (void) denominator;
2071 (void) private;
2073 return False;
2074 #endif
2076 #endif
2079 * Determine the refresh rate of the specified drawable and display.
2081 * \param dpy Display whose refresh rate is to be determined.
2082 * \param drawable Drawable whose refresh rate is to be determined.
2083 * \param numerator Numerator of the refresh rate.
2084 * \param demoninator Denominator of the refresh rate.
2085 * \return If the refresh rate for the specified display and drawable could
2086 * be calculated, True is returned. Otherwise False is returned.
2088 * \note This function is implemented entirely client-side. A lot of other
2089 * functionality is required to export GLX_OML_sync_control, so on
2090 * XFree86 this function can be called for direct-rendering contexts
2091 * when GLX_OML_sync_control appears in the client extension string.
2094 _X_HIDDEN GLboolean
2095 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
2096 int32_t * numerator, int32_t * denominator)
2098 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
2099 __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
2101 if (draw == NULL)
2102 return False;
2104 return __glxGetMscRate(draw, numerator, denominator);
2105 #else
2106 (void) dpy;
2107 (void) drawable;
2108 (void) numerator;
2109 (void) denominator;
2110 #endif
2111 return False;
2115 static int64_t
2116 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
2117 int64_t target_msc, int64_t divisor, int64_t remainder)
2119 struct glx_context *gc = __glXGetCurrentContext();
2120 #ifdef GLX_DIRECT_RENDERING
2121 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2122 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2123 #endif
2125 if (!gc) /* no GLX for this */
2126 return -1;
2128 #ifdef GLX_DIRECT_RENDERING
2129 if (!pdraw || !gc->isDirect)
2130 return -1;
2131 #endif
2133 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2134 * error", but it also says "It [glXSwapBuffersMscOML] will return a value
2135 * of -1 if the function failed because of errors detected in the input
2136 * parameters"
2138 if (divisor < 0 || remainder < 0 || target_msc < 0)
2139 return -1;
2140 if (divisor > 0 && remainder >= divisor)
2141 return -1;
2143 if (target_msc == 0 && divisor == 0 && remainder == 0)
2144 remainder = 1;
2146 #ifdef GLX_DIRECT_RENDERING
2147 if (psc->driScreen && psc->driScreen->swapBuffers)
2148 return (*psc->driScreen->swapBuffers)(pdraw, target_msc, divisor,
2149 remainder);
2150 #endif
2152 return -1;
2156 static Bool
2157 __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
2158 int64_t target_msc, int64_t divisor,
2159 int64_t remainder, int64_t * ust,
2160 int64_t * msc, int64_t * sbc)
2162 #ifdef GLX_DIRECT_RENDERING
2163 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2164 #endif
2165 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2166 int ret;
2169 /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
2170 * error", but the return type in the spec is Bool.
2172 if (divisor < 0 || remainder < 0 || target_msc < 0)
2173 return False;
2174 if (divisor > 0 && remainder >= divisor)
2175 return False;
2177 #ifdef GLX_DIRECT_RENDERING
2178 if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
2179 ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
2180 ust, msc, sbc);
2181 return ret;
2183 #endif
2185 return False;
2189 static Bool
2190 __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
2191 int64_t target_sbc, int64_t * ust,
2192 int64_t * msc, int64_t * sbc)
2194 #ifdef GLX_DIRECT_RENDERING
2195 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2196 #endif
2197 struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
2198 int ret;
2200 /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
2201 * error", but the return type in the spec is Bool.
2203 if (target_sbc < 0)
2204 return False;
2206 #ifdef GLX_DIRECT_RENDERING
2207 if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
2208 ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
2209 return ret;
2211 #endif
2213 return False;
2216 /*@}*/
2220 * Mesa extension stubs. These will help reduce portability problems.
2222 /*@{*/
2225 * Release all buffers associated with the specified GLX drawable.
2227 * \todo
2228 * This function was intended for stand-alone Mesa. The issue there is that
2229 * the library doesn't get any notification when a window is closed. In
2230 * DRI there is a similar but slightly different issue. When GLX 1.3 is
2231 * supported, there are 3 different functions to destroy a drawable. It
2232 * should be possible to create GLX protocol (or have it determine which
2233 * protocol to use based on the type of the drawable) to have one function
2234 * do the work of 3. For the direct-rendering case, this function could
2235 * just call the driver's \c __DRIdrawableRec::destroyDrawable function.
2236 * This would reduce the frequency with which \c __driGarbageCollectDrawables
2237 * would need to be used. This really should be done as part of the new DRI
2238 * interface work.
2240 * \sa http://oss.sgi.com/projects/ogl-sample/registry/MESA/release_buffers.txt
2241 * __driGarbageCollectDrawables
2242 * glXDestroyGLXPixmap
2243 * glXDestroyPbuffer glXDestroyPixmap glXDestroyWindow
2244 * glXDestroyGLXPbufferSGIX glXDestroyGLXVideoSourceSGIX
2246 static Bool
2247 __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
2249 (void) dpy;
2250 (void) d;
2251 return False;
2255 _X_EXPORT GLXPixmap
2256 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
2257 Pixmap pixmap, Colormap cmap)
2259 (void) dpy;
2260 (void) visual;
2261 (void) pixmap;
2262 (void) cmap;
2263 return 0;
2266 /*@}*/
2270 * GLX_MESA_copy_sub_buffer
2272 #define X_GLXvop_CopySubBufferMESA 5154 /* temporary */
2273 static void
2274 __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
2275 int x, int y, int width, int height)
2277 xGLXVendorPrivateReq *req;
2278 struct glx_context *gc;
2279 GLXContextTag tag;
2280 CARD32 *drawable_ptr;
2281 INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
2282 CARD8 opcode;
2284 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2285 __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
2286 if (pdraw != NULL) {
2287 struct glx_screen *psc = pdraw->psc;
2288 if (psc->driScreen->copySubBuffer != NULL) {
2289 glFlush();
2290 (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height);
2293 return;
2295 #endif
2297 opcode = __glXSetupForCommand(dpy);
2298 if (!opcode)
2299 return;
2302 ** The calling thread may or may not have a current context. If it
2303 ** does, send the context tag so the server can do a flush.
2305 gc = __glXGetCurrentContext();
2306 if ((gc != NULL) && (dpy == gc->currentDpy) &&
2307 ((drawable == gc->currentDrawable) ||
2308 (drawable == gc->currentReadable))) {
2309 tag = gc->currentContextTag;
2311 else {
2312 tag = 0;
2315 LockDisplay(dpy);
2316 GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32) * 4, req);
2317 req->reqType = opcode;
2318 req->glxCode = X_GLXVendorPrivate;
2319 req->vendorCode = X_GLXvop_CopySubBufferMESA;
2320 req->contextTag = tag;
2322 drawable_ptr = (CARD32 *) (req + 1);
2323 x_ptr = (INT32 *) (drawable_ptr + 1);
2324 y_ptr = (INT32 *) (drawable_ptr + 2);
2325 w_ptr = (INT32 *) (drawable_ptr + 3);
2326 h_ptr = (INT32 *) (drawable_ptr + 4);
2328 *drawable_ptr = drawable;
2329 *x_ptr = x;
2330 *y_ptr = y;
2331 *w_ptr = width;
2332 *h_ptr = height;
2334 UnlockDisplay(dpy);
2335 SyncHandle();
2338 /*@{*/
2339 static void
2340 __glXBindTexImageEXT(Display * dpy,
2341 GLXDrawable drawable, int buffer, const int *attrib_list)
2343 struct glx_context *gc = __glXGetCurrentContext();
2345 if (gc == NULL || gc->vtable->bind_tex_image == NULL)
2346 return;
2348 gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
2351 static void
2352 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
2354 struct glx_context *gc = __glXGetCurrentContext();
2356 if (gc == NULL || gc->vtable->release_tex_image == NULL)
2357 return;
2359 gc->vtable->release_tex_image(dpy, drawable, buffer);
2362 /*@}*/
2364 #endif /* GLX_USE_APPLEGL */
2367 * \c strdup is actually not a standard ANSI C or POSIX routine.
2368 * Irix will not define it if ANSI mode is in effect.
2370 * \sa strdup
2372 _X_HIDDEN char *
2373 __glXstrdup(const char *str)
2375 char *copy;
2376 copy = (char *) Xmalloc(strlen(str) + 1);
2377 if (!copy)
2378 return NULL;
2379 strcpy(copy, str);
2380 return copy;
2384 ** glXGetProcAddress support
2387 struct name_address_pair
2389 const char *Name;
2390 GLvoid *Address;
2393 #define GLX_FUNCTION(f) { # f, (GLvoid *) f }
2394 #define GLX_FUNCTION2(n,f) { # n, (GLvoid *) f }
2396 static const struct name_address_pair GLX_functions[] = {
2397 /*** GLX_VERSION_1_0 ***/
2398 GLX_FUNCTION(glXChooseVisual),
2399 GLX_FUNCTION(glXCopyContext),
2400 GLX_FUNCTION(glXCreateContext),
2401 GLX_FUNCTION(glXCreateGLXPixmap),
2402 GLX_FUNCTION(glXDestroyContext),
2403 GLX_FUNCTION(glXDestroyGLXPixmap),
2404 GLX_FUNCTION(glXGetConfig),
2405 GLX_FUNCTION(glXGetCurrentContext),
2406 GLX_FUNCTION(glXGetCurrentDrawable),
2407 GLX_FUNCTION(glXIsDirect),
2408 GLX_FUNCTION(glXMakeCurrent),
2409 GLX_FUNCTION(glXQueryExtension),
2410 GLX_FUNCTION(glXQueryVersion),
2411 GLX_FUNCTION(glXSwapBuffers),
2412 GLX_FUNCTION(glXUseXFont),
2413 GLX_FUNCTION(glXWaitGL),
2414 GLX_FUNCTION(glXWaitX),
2416 /*** GLX_VERSION_1_1 ***/
2417 GLX_FUNCTION(glXGetClientString),
2418 GLX_FUNCTION(glXQueryExtensionsString),
2419 GLX_FUNCTION(glXQueryServerString),
2421 /*** GLX_VERSION_1_2 ***/
2422 GLX_FUNCTION(glXGetCurrentDisplay),
2424 /*** GLX_VERSION_1_3 ***/
2425 GLX_FUNCTION(glXChooseFBConfig),
2426 GLX_FUNCTION(glXCreateNewContext),
2427 GLX_FUNCTION(glXCreatePbuffer),
2428 GLX_FUNCTION(glXCreatePixmap),
2429 GLX_FUNCTION(glXCreateWindow),
2430 GLX_FUNCTION(glXDestroyPbuffer),
2431 GLX_FUNCTION(glXDestroyPixmap),
2432 GLX_FUNCTION(glXDestroyWindow),
2433 GLX_FUNCTION(glXGetCurrentReadDrawable),
2434 GLX_FUNCTION(glXGetFBConfigAttrib),
2435 GLX_FUNCTION(glXGetFBConfigs),
2436 GLX_FUNCTION(glXGetSelectedEvent),
2437 GLX_FUNCTION(glXGetVisualFromFBConfig),
2438 GLX_FUNCTION(glXMakeContextCurrent),
2439 GLX_FUNCTION(glXQueryContext),
2440 GLX_FUNCTION(glXQueryDrawable),
2441 GLX_FUNCTION(glXSelectEvent),
2443 #ifndef GLX_USE_APPLEGL
2444 /*** GLX_SGI_swap_control ***/
2445 GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
2447 /*** GLX_SGI_video_sync ***/
2448 GLX_FUNCTION2(glXGetVideoSyncSGI, __glXGetVideoSyncSGI),
2449 GLX_FUNCTION2(glXWaitVideoSyncSGI, __glXWaitVideoSyncSGI),
2451 /*** GLX_SGI_make_current_read ***/
2452 GLX_FUNCTION2(glXMakeCurrentReadSGI, glXMakeContextCurrent),
2453 GLX_FUNCTION2(glXGetCurrentReadDrawableSGI, glXGetCurrentReadDrawable),
2455 /*** GLX_EXT_import_context ***/
2456 GLX_FUNCTION(glXFreeContextEXT),
2457 GLX_FUNCTION(glXGetContextIDEXT),
2458 GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
2459 GLX_FUNCTION(glXImportContextEXT),
2460 GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
2461 #endif
2463 /*** GLX_SGIX_fbconfig ***/
2464 GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
2465 GLX_FUNCTION2(glXChooseFBConfigSGIX, glXChooseFBConfig),
2466 GLX_FUNCTION(glXCreateGLXPixmapWithConfigSGIX),
2467 GLX_FUNCTION(glXCreateContextWithConfigSGIX),
2468 GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
2469 GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
2471 #ifndef GLX_USE_APPLEGL
2472 /*** GLX_SGIX_pbuffer ***/
2473 GLX_FUNCTION(glXCreateGLXPbufferSGIX),
2474 GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
2475 GLX_FUNCTION(glXQueryGLXPbufferSGIX),
2476 GLX_FUNCTION(glXSelectEventSGIX),
2477 GLX_FUNCTION(glXGetSelectedEventSGIX),
2479 /*** GLX_SGIX_swap_group ***/
2480 GLX_FUNCTION2(glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX),
2482 /*** GLX_SGIX_swap_barrier ***/
2483 GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
2484 GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
2486 /*** GLX_MESA_copy_sub_buffer ***/
2487 GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
2489 /*** GLX_MESA_pixmap_colormap ***/
2490 GLX_FUNCTION(glXCreateGLXPixmapMESA),
2492 /*** GLX_MESA_release_buffers ***/
2493 GLX_FUNCTION2(glXReleaseBuffersMESA, __glXReleaseBuffersMESA),
2495 /*** GLX_MESA_swap_control ***/
2496 GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
2497 GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
2498 #endif
2500 /*** GLX_ARB_get_proc_address ***/
2501 GLX_FUNCTION(glXGetProcAddressARB),
2503 /*** GLX 1.4 ***/
2504 GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
2506 #ifndef GLX_USE_APPLEGL
2507 /*** GLX_OML_sync_control ***/
2508 GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
2509 GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
2510 GLX_FUNCTION2(glXSwapBuffersMscOML, __glXSwapBuffersMscOML),
2511 GLX_FUNCTION2(glXGetMscRateOML, __glXGetMscRateOML),
2512 GLX_FUNCTION2(glXGetSyncValuesOML, __glXGetSyncValuesOML),
2514 /*** GLX_EXT_texture_from_pixmap ***/
2515 GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
2516 GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
2517 #endif
2519 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2520 /*** DRI configuration ***/
2521 GLX_FUNCTION(glXGetScreenDriver),
2522 GLX_FUNCTION(glXGetDriverConfig),
2523 #endif
2525 {NULL, NULL} /* end of list */
2528 #ifndef GLX_USE_APPLEGL
2529 static const GLvoid *
2530 get_glx_proc_address(const char *funcName)
2532 GLuint i;
2534 /* try static functions */
2535 for (i = 0; GLX_functions[i].Name; i++) {
2536 if (strcmp(GLX_functions[i].Name, funcName) == 0)
2537 return GLX_functions[i].Address;
2540 return NULL;
2542 #endif
2545 * Get the address of a named GL function. This is the pre-GLX 1.4 name for
2546 * \c glXGetProcAddress.
2548 * \param procName Name of a GL or GLX function.
2549 * \returns A pointer to the named function
2551 * \sa glXGetProcAddress
2553 _X_EXPORT void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
2555 typedef void (*gl_function) (void);
2556 gl_function f;
2559 /* Search the table of GLX and internal functions first. If that
2560 * fails and the supplied name could be a valid core GL name, try
2561 * searching the core GL function table. This check is done to prevent
2562 * DRI based drivers from searching the core GL function table for
2563 * internal API functions.
2565 #ifdef GLX_USE_APPLEGL
2566 f = (gl_function) apple_glx_get_proc_address(procName);
2567 #else
2568 f = (gl_function) get_glx_proc_address((const char *) procName);
2569 if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
2570 && (procName[2] != 'X')) {
2571 f = (gl_function) _glapi_get_proc_address((const char *) procName);
2573 #endif
2574 return f;
2578 * Get the address of a named GL function. This is the GLX 1.4 name for
2579 * \c glXGetProcAddressARB.
2581 * \param procName Name of a GL or GLX function.
2582 * \returns A pointer to the named function
2584 * \sa glXGetProcAddressARB
2586 _X_EXPORT void (*glXGetProcAddress(const GLubyte * procName)) (void)
2587 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
2588 __attribute__ ((alias("glXGetProcAddressARB")));
2589 #else
2591 return glXGetProcAddressARB(procName);
2593 #endif /* __GNUC__ */
2596 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
2598 * Get the unadjusted system time (UST). Currently, the UST is measured in
2599 * microseconds since Epoc. The actual resolution of the UST may vary from
2600 * system to system, and the units may vary from release to release.
2601 * Drivers should not call this function directly. They should instead use
2602 * \c glXGetProcAddress to obtain a pointer to the function.
2604 * \param ust Location to store the 64-bit UST
2605 * \returns Zero on success or a negative errno value on failure.
2607 * \sa glXGetProcAddress, PFNGLXGETUSTPROC
2609 * \since Internal API version 20030317.
2611 _X_HIDDEN int
2612 __glXGetUST(int64_t * ust)
2614 struct timeval tv;
2616 if (ust == NULL) {
2617 return -EFAULT;
2620 if (gettimeofday(&tv, NULL) == 0) {
2621 ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
2622 return 0;
2624 else {
2625 return -errno;
2628 #endif /* GLX_DIRECT_RENDERING */