summaryrefslogtreecommitdiff
path: root/test/egl_and_glx_different_pointers.c
blob: 009b82b35cfa8ecb840cf218feb1f94586a4b4e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright © 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/**
 * @file egl_and_glx_different_pointers.c
 *
 * Tests that epoxy correctly handles an EGL and GLX implementation
 * that return different function pointers between the two.
 *
 * This is the case for EGL and GLX on nvidia binary drivers
 * currently, but is also the case if someone has nvidia binary GLX
 * installed but still has Mesa (software) EGL installed.  This seems
 * common enough that we should make sure things work.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <err.h>
#include <dlfcn.h>
#include "epoxy/gl.h"
#include "epoxy/egl.h"
#include "epoxy/glx.h"

#include "egl_common.h"
#include "glx_common.h"
#include "dlwrap.h"

#define GLX_FAKED_VENDOR_STRING "libepoxy override GLX"
#define EGL_FAKED_VENDOR_STRING "libepoxy override EGL"

#define GL_CREATESHADER_VALUE 1234
#define GLES2_CREATESHADER_VALUE 5678

const char *override_GLES2_glGetString(GLenum e);
const char *override_GL_glGetString(GLenum e);
GLuint override_GLES2_glCreateShader(GLenum e);
GLuint override_GL_glCreateShader(GLenum e);

const char *
override_GL_glGetString(GLenum e)
{
    if (e == GL_VENDOR)
        return GLX_FAKED_VENDOR_STRING;

    return DEFER_TO_GL("libGL.so.1", override_GL_glGetString,
                       "glGetString", (e));
}

const char *
override_GLES2_glGetString(GLenum e)
{
    if (e == GL_VENDOR)
        return EGL_FAKED_VENDOR_STRING;

    return DEFER_TO_GL("libGLESv2.so.2", override_GLES2_glGetString,
                       "glGetString", (e));
}

GLuint
override_GL_glCreateShader(GLenum type)
{
    EPOXY_UNUSED(type);
    return GL_CREATESHADER_VALUE;
}

GLuint
override_GLES2_glCreateShader(GLenum type)
{
    EPOXY_UNUSED(type);
    return GLES2_CREATESHADER_VALUE;
}

#ifdef USE_GLX
static bool
make_glx_current_and_test(Display *dpy, GLXContext ctx, Drawable draw)
{
    const char *string;
    GLuint shader;
    bool pass = true;

    glXMakeCurrent(dpy, draw, ctx);

    if (!epoxy_is_desktop_gl()) {
        fprintf(stderr, "Claimed to be ES\n");
        pass = false;
    }

    string = (const char *)glGetString(GL_VENDOR);
    printf("GLX vendor: %s\n", string);

    shader = glCreateShader(GL_FRAGMENT_SHADER);
    if (shader != GL_CREATESHADER_VALUE) {
        fprintf(stderr, "glCreateShader() returned %d instead of %d\n",
                shader, GL_CREATESHADER_VALUE);
        pass = false;
    }

    pass = pass && !strcmp(string, GLX_FAKED_VENDOR_STRING);

    return pass;
}

static void
init_glx(Display **out_dpy, GLXContext *out_ctx, Drawable *out_draw)
{
    Display *dpy = get_display_or_skip();
    make_glx_context_current_or_skip(dpy);

    *out_dpy = dpy;
    *out_ctx = glXGetCurrentContext();
    *out_draw= glXGetCurrentDrawable();
}
#endif /* USE_GLX */

#ifdef USE_EGL
static bool
make_egl_current_and_test(EGLDisplay dpy, EGLContext ctx)
{
    const char *string;
    GLuint shader;
    bool pass = true;

    eglMakeCurrent(dpy, NULL, NULL, ctx);

    if (epoxy_is_desktop_gl()) {
        fprintf(stderr, "Claimed to be desktop\n");
        pass = false;
    }

    if (epoxy_gl_version() < 20) {
        fprintf(stderr, "Claimed to be GL version %d\n",
                epoxy_gl_version());
        pass = false;
    }

    shader = glCreateShader(GL_FRAGMENT_SHADER);
    if (shader != GLES2_CREATESHADER_VALUE) {
        fprintf(stderr, "glCreateShader() returned %d instead of %d\n",
                shader, GLES2_CREATESHADER_VALUE);
        pass = false;
    }

    string = (const char *)glGetString(GL_VENDOR);
    printf("EGL vendor: %s\n", string);

    pass = pass && !strcmp(string, EGL_FAKED_VENDOR_STRING);

    return pass;
}

static void
init_egl(EGLDisplay *out_dpy, EGLContext *out_ctx)
{
    EGLDisplay dpy = get_egl_display_or_skip();
    static const EGLint config_attribs[] = {
	EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
	EGL_RED_SIZE, 1,
	EGL_GREEN_SIZE, 1,
	EGL_BLUE_SIZE, 1,
	EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
	EGL_NONE
    };
    static const EGLint context_attribs[] = {
        EGL_CONTEXT_CLIENT_VERSION, 2,
        EGL_NONE
    };
    EGLContext ctx;
    EGLConfig cfg;
    EGLint count;

    if (!epoxy_has_egl_extension(dpy, "EGL_KHR_surfaceless_context"))
        errx(77, "Test requires EGL_KHR_surfaceless_context");

    eglBindAPI(EGL_OPENGL_ES_API);

    if (!eglChooseConfig(dpy, config_attribs, &cfg, 1, &count))
        errx(77, "Couldn't get an EGLConfig\n");

    ctx = eglCreateContext(dpy, cfg, NULL, context_attribs);
    if (!ctx)
        errx(77, "Couldn't create a GLES2 context\n");

    *out_dpy = dpy;
    *out_ctx = ctx;
}
#endif /* USE_EGL */

int main(void)
{
    bool pass = true;
#ifdef USE_EGL
    EGLDisplay egl_dpy;
    EGLContext egl_ctx;
#endif
#ifdef USE_GLX
    Display *glx_dpy;
    GLXContext glx_ctx;
    Drawable glx_draw;
#endif

    /* Force epoxy to have loaded both EGL and GLX libs already -- we
     * can't assume anything about symbol resolution based on having
     * EGL or GLX loaded.
     */
    (void)glXGetCurrentContext();
    (void)eglGetCurrentContext();

#ifdef USE_GLX
    init_glx(&glx_dpy, &glx_ctx, &glx_draw);
    pass = make_glx_current_and_test(glx_dpy, glx_ctx, glx_draw) && pass;
#endif
#ifdef USE_EGL
    init_egl(&egl_dpy, &egl_ctx);
    pass = make_egl_current_and_test(egl_dpy, egl_ctx) && pass;
#endif

#if defined(USE_GLX) && defined(USE_EGL)
    pass = make_glx_current_and_test(glx_dpy, glx_ctx, glx_draw) && pass;
    pass = make_egl_current_and_test(egl_dpy, egl_ctx) && pass;
#endif

    return pass != true;
}