blob: 9326b5a06e9d303fc3839655ee24b69b0a00e4e8 [file] [log] [blame]
Eric Anholtdbf940d2014-01-30 17:52:40 -08001/*
2 * Copyright © 2014 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/**
25 * @file egl_without_glx.c
26 *
Eric Anholtf15c1692014-03-17 09:12:18 -070027 * Tries to test operation of the library on a GL stack with EGL and
28 * GLES but no GLX or desktop GL (such as Arm's Mali GLES3 drivers).
29 * This test is varied by the GLES_VERSION defined at compile time to
30 * test either a GLES1-only or a GLES2-only system.
Eric Anholtdbf940d2014-01-30 17:52:40 -080031 */
32
33#define _GNU_SOURCE
34
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <assert.h>
39#include <err.h>
40#include <dlfcn.h>
41#include "epoxy/gl.h"
42#include "epoxy/egl.h"
43
44#include "egl_common.h"
45
46/**
47 * Wraps the system dlopen(), which libepoxy will end up calling when
48 * it tries to dlopen() the API libraries, and errors out the
49 * libraries we're trying to simulate not being installed on the
50 * system.
51 */
52void *
53dlopen(const char *filename, int flag)
54{
55 void * (*dlopen_unwrapped)(const char *filename, int flag);
56
57 if (!strcmp(filename, "libGL.so.1"))
58 return NULL;
59#if GLES_VERSION == 2
60 if (!strcmp(filename, "libGLESv1_CM.so.1"))
61 return NULL;
62#else
63 if (!strcmp(filename, "libGLESv2.so.2"))
64 return NULL;
65#endif
66
67 dlopen_unwrapped = dlsym(RTLD_NEXT, "dlopen");
68 assert(dlopen_unwrapped);
69
70 return dlopen_unwrapped(filename, flag);
71}
72
Eric Anholtf15c1692014-03-17 09:12:18 -070073
74static EGLenum last_api;
75static EGLenum extra_error = EGL_SUCCESS;
76
77/**
78 * Override of the real libEGL's eglBindAPI to simulate the target
79 * system's eglBindAPI.
80 */
81static EGLBoolean
82override_eglBindAPI(EGLenum api)
83{
84 void *egl = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL);
85 EGLBoolean (*real_eglBindAPI)(EGLenum api) = dlsym(egl, "eglBindAPI");
86
87 last_api = api;
88
89 if (api == EGL_OPENGL_API) {
90 extra_error = EGL_BAD_PARAMETER;
91 return EGL_FALSE;
92 }
93
94 assert(real_eglBindAPI);
95 return real_eglBindAPI(api);
96}
97
98/**
99 * Override of the real libEGL's eglGetError() to feed back the error
100 * that might have been generated by override_eglBindAPI().
101 */
102static EGLint
103override_eglGetError(void)
104{
105 void *egl = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL);
106 EGLint (*real_eglGetError)(void) = dlsym(egl, "eglGetError");
107
108 if (extra_error != EGL_SUCCESS) {
109 EGLenum error = extra_error;
110 extra_error = EGL_SUCCESS;
111 return error;
112 }
113
114 assert(real_eglGetError);
115 return real_eglGetError();
116}
117
Eric Anholtdbf940d2014-01-30 17:52:40 -0800118int
119main(int argc, char **argv)
120{
121 bool pass = true;
122 EGLDisplay *dpy = get_egl_display_or_skip();
123 EGLint context_attribs[] = {
124 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
125 EGL_NONE
126 };
127 EGLConfig cfg;
128 EGLint config_attribs[] = {
129 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
130 EGL_RED_SIZE, 1,
131 EGL_GREEN_SIZE, 1,
132 EGL_BLUE_SIZE, 1,
133 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
134 EGL_NONE
135 };
136 EGLint count;
137 EGLContext ctx;
138 const unsigned char *string;
139
Eric Anholtf15c1692014-03-17 09:12:18 -0700140 epoxy_eglBindAPI = override_eglBindAPI;
141 epoxy_eglGetError = override_eglGetError;
142
Eric Anholtdbf940d2014-01-30 17:52:40 -0800143 if (!epoxy_has_egl_extension(dpy, "EGL_KHR_surfaceless_context"))
144 errx(77, "Test requires EGL_KHR_surfaceless_context");
145
146 eglBindAPI(EGL_OPENGL_ES_API);
147
148 if (!eglChooseConfig(dpy, config_attribs, &cfg, 1, &count))
149 errx(77, "Couldn't get an EGLConfig\n");
150
151 ctx = eglCreateContext(dpy, cfg, NULL, context_attribs);
152 if (!ctx)
153 errx(77, "Couldn't create a GLES%d context\n", GLES_VERSION);
154
155 eglMakeCurrent(dpy, NULL, NULL, ctx);
156
157 string = glGetString(GL_VERSION);
158 printf("GL_VERSION: %s\n", string);
159
Eric Anholtf15c1692014-03-17 09:12:18 -0700160 assert(eglGetError() == EGL_SUCCESS);
161
Eric Anholtdbf940d2014-01-30 17:52:40 -0800162 return pass != true;
163}