OSDN Git Service

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