dispatch_glx.c revision ca86eba8
1e52adb7bSmrg/*
2e52adb7bSmrg * Copyright © 2013 Intel Corporation
3e52adb7bSmrg *
4e52adb7bSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5e52adb7bSmrg * copy of this software and associated documentation files (the "Software"),
6e52adb7bSmrg * to deal in the Software without restriction, including without limitation
7e52adb7bSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e52adb7bSmrg * and/or sell copies of the Software, and to permit persons to whom the
9e52adb7bSmrg * Software is furnished to do so, subject to the following conditions:
10e52adb7bSmrg *
11e52adb7bSmrg * The above copyright notice and this permission notice (including the next
12e52adb7bSmrg * paragraph) shall be included in all copies or substantial portions of the
13e52adb7bSmrg * Software.
14e52adb7bSmrg *
15e52adb7bSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e52adb7bSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e52adb7bSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e52adb7bSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e52adb7bSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e52adb7bSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e52adb7bSmrg * IN THE SOFTWARE.
22e52adb7bSmrg */
23e52adb7bSmrg
24e52adb7bSmrg#include <assert.h>
25e52adb7bSmrg#include <string.h>
26e52adb7bSmrg#include <stdio.h>
27e52adb7bSmrg
28e52adb7bSmrg#include "dispatch_common.h"
29e52adb7bSmrg
30e52adb7bSmrg/**
31e52adb7bSmrg * If we can determine the GLX version from the current context, then
32e52adb7bSmrg * return that, otherwise return a version that will just send us on
33e52adb7bSmrg * to dlsym() or get_proc_address().
34e52adb7bSmrg */
35e52adb7bSmrgint
36e52adb7bSmrgepoxy_conservative_glx_version(void)
37e52adb7bSmrg{
38e52adb7bSmrg    Display *dpy = glXGetCurrentDisplay();
39e52adb7bSmrg    GLXContext ctx = glXGetCurrentContext();
40e52adb7bSmrg    int screen;
41e52adb7bSmrg
42e52adb7bSmrg    if (!dpy || !ctx)
43e52adb7bSmrg        return 14;
44e52adb7bSmrg
45e52adb7bSmrg    glXQueryContext(dpy, ctx, GLX_SCREEN, &screen);
46e52adb7bSmrg
47e52adb7bSmrg    return epoxy_glx_version(dpy, screen);
48e52adb7bSmrg}
49e52adb7bSmrg
50f71742dfSmrg
51f71742dfSmrg/**
52f71742dfSmrg * @brief Returns the version of GLX we are using
53f71742dfSmrg *
54f71742dfSmrg * The version is encoded as:
55f71742dfSmrg *
56f71742dfSmrg * ```
57f71742dfSmrg *
58f71742dfSmrg *   version = major * 10 + minor
59f71742dfSmrg *
60f71742dfSmrg * ```
61f71742dfSmrg *
62f71742dfSmrg * So it can be easily used for version comparisons.
63f71742dfSmrg *
64f71742dfSmrg * @param dpy The X11 display
65f71742dfSmrg * @param screen The X11 screen
66f71742dfSmrg *
67f71742dfSmrg * @return The encoded version of GLX we are using
68f71742dfSmrg *
69f71742dfSmrg * @see epoxy_gl_version()
70f71742dfSmrg */
71f71742dfSmrgint
72e52adb7bSmrgepoxy_glx_version(Display *dpy, int screen)
73e52adb7bSmrg{
74e52adb7bSmrg    int server_major, server_minor;
75e52adb7bSmrg    int client_major, client_minor;
76e52adb7bSmrg    int server, client;
77e52adb7bSmrg    const char *version_string;
78e52adb7bSmrg    int ret;
79e52adb7bSmrg
80e52adb7bSmrg    version_string = glXQueryServerString(dpy, screen, GLX_VERSION);
81f71742dfSmrg    if (!version_string)
82f71742dfSmrg        return 0;
83f71742dfSmrg
84e52adb7bSmrg    ret = sscanf(version_string, "%d.%d", &server_major, &server_minor);
85e52adb7bSmrg    assert(ret == 2);
86e52adb7bSmrg    server = server_major * 10 + server_minor;
87e52adb7bSmrg
88e52adb7bSmrg    version_string = glXGetClientString(dpy, GLX_VERSION);
89f71742dfSmrg    if (!version_string)
90f71742dfSmrg        return 0;
91f71742dfSmrg
92e52adb7bSmrg    ret = sscanf(version_string, "%d.%d", &client_major, &client_minor);
93e52adb7bSmrg    assert(ret == 2);
94e52adb7bSmrg    client = client_major * 10 + client_minor;
95e52adb7bSmrg
96e52adb7bSmrg    if (client < server)
97e52adb7bSmrg        return client;
98e52adb7bSmrg    else
99e52adb7bSmrg        return server;
100e52adb7bSmrg}
101e52adb7bSmrg
102e52adb7bSmrg/**
103e52adb7bSmrg * If we can determine the GLX extension support from the current
104e52adb7bSmrg * context, then return that, otherwise give the answer that will just
105e52adb7bSmrg * send us on to get_proc_address().
106e52adb7bSmrg */
107e52adb7bSmrgbool
108e52adb7bSmrgepoxy_conservative_has_glx_extension(const char *ext)
109e52adb7bSmrg{
110e52adb7bSmrg    Display *dpy = glXGetCurrentDisplay();
111e52adb7bSmrg    GLXContext ctx = glXGetCurrentContext();
112e52adb7bSmrg    int screen;
113e52adb7bSmrg
114e52adb7bSmrg    if (!dpy || !ctx)
115e52adb7bSmrg        return true;
116e52adb7bSmrg
117e52adb7bSmrg    glXQueryContext(dpy, ctx, GLX_SCREEN, &screen);
118e52adb7bSmrg
119e52adb7bSmrg    return epoxy_has_glx_extension(dpy, screen, ext);
120e52adb7bSmrg}
121e52adb7bSmrg
122f71742dfSmrg/**
123f71742dfSmrg * @brief Returns true if the given GLX extension is supported in the current context.
124f71742dfSmrg *
125f71742dfSmrg * @param dpy The X11 display
126f71742dfSmrg * @param screen The X11 screen
127f71742dfSmrg * @param extension The name of the GLX extension
128f71742dfSmrg *
129f71742dfSmrg * @return `true` if the extension is available
130f71742dfSmrg *
131f71742dfSmrg * @see epoxy_has_gl_extension()
132f71742dfSmrg * @see epoxy_has_egl_extension()
133f71742dfSmrg */
134f71742dfSmrgbool
135e52adb7bSmrgepoxy_has_glx_extension(Display *dpy, int screen, const char *ext)
136f71742dfSmrg{
137e52adb7bSmrg    /* No, you can't just use glXGetClientString or
138e52adb7bSmrg     * glXGetServerString() here.  Those each tell you about one half
139e52adb7bSmrg     * of what's needed for an extension to be supported, and
140e52adb7bSmrg     * glXQueryExtensionsString() is what gives you the intersection
141e52adb7bSmrg     * of the two.
142e52adb7bSmrg     */
143e52adb7bSmrg    return epoxy_extension_in_string(glXQueryExtensionsString(dpy, screen), ext);
144e52adb7bSmrg}
145f71742dfSmrg
146f71742dfSmrg/**
147f71742dfSmrg * @brief Checks whether GLX is available.
148f71742dfSmrg *
149f71742dfSmrg * @param dpy The X11 display
150f71742dfSmrg *
151f71742dfSmrg * @return `true` if GLX is available
152f71742dfSmrg *
153f71742dfSmrg * @newin{1,4}
154f71742dfSmrg */
155f71742dfSmrgbool
156f71742dfSmrgepoxy_has_glx(Display *dpy)
157f71742dfSmrg{
158f71742dfSmrg#if !PLATFORM_HAS_GLX
159f71742dfSmrg    return false;
160f71742dfSmrg#else
161ca86eba8Smrg    if (epoxy_load_glx(false, true)) {
162ca86eba8Smrg        Bool (* pf_glXQueryExtension) (Display *, int *, int *);
163ca86eba8Smrg        int error_base, event_base;
164ca86eba8Smrg
165ca86eba8Smrg        pf_glXQueryExtension = epoxy_conservative_glx_dlsym("glXQueryExtension", false);
166ca86eba8Smrg        if (pf_glXQueryExtension && pf_glXQueryExtension(dpy, &error_base, &event_base))
167ca86eba8Smrg            return true;
168ca86eba8Smrg    }
169f71742dfSmrg
170f71742dfSmrg    return false;
171f71742dfSmrg#endif /* !PLATFORM_HAS_GLX */
172f71742dfSmrg}
173