1/*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <err.h>
28#include "epoxy/gl.h"
29#include "epoxy/glx.h"
30#include <X11/Xlib.h>
31
32#include "glx_common.h"
33
34static Display *dpy;
35
36static bool
37test_has_extensions(void)
38{
39    int num_extensions;
40
41    glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
42
43    for (int i = 0; i < num_extensions; i++) {
44        char *ext = (char *)glGetStringi(GL_EXTENSIONS, i);
45
46        if (!epoxy_has_gl_extension(ext)) {
47            fprintf(stderr, "GL implementation reported support for %s, "
48                    "but epoxy didn't\n", ext);
49            return false;
50        }
51    }
52
53    if (epoxy_has_gl_extension("GL_ARB_ham_sandwich")) {
54        fputs("epoxy implementation reported support for "
55              "GL_ARB_ham_sandwich, but it shouldn't\n",
56              stderr);
57        return false;
58    }
59
60    return true;
61}
62
63static bool
64test_gl_version(void)
65{
66    int gl_version, epoxy_version;
67    int major, minor;
68
69    glGetIntegerv(GL_MAJOR_VERSION, &major);
70    glGetIntegerv(GL_MINOR_VERSION, &minor);
71    gl_version = major * 10 + minor;
72
73    if (gl_version < 32) {
74        fprintf(stderr,
75                "Implementation reported GL version %d, should be at least 32\n",
76                gl_version);
77        return false;
78    }
79
80    epoxy_version = epoxy_gl_version();
81    if (epoxy_version != gl_version) {
82        fprintf(stderr,
83                "Epoxy reported GL version %d, should be %d\n",
84                epoxy_version, gl_version);
85        return false;
86    }
87
88    return true;
89}
90
91static bool
92test_glx_version(void)
93{
94    int version = epoxy_glx_version(dpy, 0);
95    const char *version_string;
96    int ret;
97    int server_major, server_minor;
98    int client_major, client_minor;
99    int server, client, expected;
100
101    if (version < 13) {
102        fprintf(stderr,
103                "Reported GLX version %d, should be at least 13 "
104                "according to Linux GL ABI\n",
105                version);
106        return false;
107    }
108
109    version_string = glXQueryServerString(dpy, 0, GLX_VERSION);
110    ret = sscanf(version_string, "%d.%d", &server_major, &server_minor);
111    assert(ret == 2);
112    server = server_major * 10 + server_minor;
113
114    version_string = glXGetClientString(dpy, GLX_VERSION);
115    ret = sscanf(version_string, "%d.%d", &client_major, &client_minor);
116    assert(ret == 2);
117    client = client_major * 10 + client_minor;
118
119    if (client < server)
120        expected = client;
121    else
122        expected = server;
123
124    if (version != expected) {
125        fprintf(stderr,
126                "Reported GLX version %d, should be %d (%s)\n",
127                version, expected, version_string);
128        return false;
129    }
130
131    return true;
132}
133
134static int
135error_handler(Display *d, XErrorEvent *ev)
136{
137    return 0;
138}
139
140int
141main(int argc, char **argv)
142{
143    bool pass = true;
144    XVisualInfo *visinfo;
145    Window win;
146    GLXFBConfig config;
147    static const int attribs[] = {
148        GLX_CONTEXT_PROFILE_MASK_ARB,
149        GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
150        GLX_CONTEXT_MAJOR_VERSION_ARB,
151        3,
152        GLX_CONTEXT_MINOR_VERSION_ARB,
153        2,
154        None
155    };
156    GLXContext ctx;
157    int (*old_handler)(Display *, XErrorEvent *);
158
159    dpy = get_display_or_skip();
160
161    if (!epoxy_has_glx_extension(dpy, 0, "GLX_ARB_create_context_profile"))
162        errx(77, "Test requires GLX_ARB_create_context_profile");
163
164    visinfo = get_glx_visual(dpy);
165    win = get_glx_window(dpy, visinfo, false);
166    config = get_fbconfig_for_visinfo(dpy, visinfo);
167
168    old_handler = XSetErrorHandler(error_handler);
169    ctx = glXCreateContextAttribsARB(dpy, config, NULL, True, attribs);
170    if (ctx == None)
171        errx(77, "glXCreateContext failed");
172    XSetErrorHandler(old_handler);
173
174    glXMakeCurrent(dpy, win, ctx);
175
176    pass = test_gl_version() && pass;
177    pass = test_glx_version() && pass;
178    pass = test_has_extensions() && pass;
179
180    return pass != true;
181}
182