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