]> git.cworth.org Git - fips/blob - test/egl-glesv2-dlopen-gpa.c
test: Add 4 tests using EGL and OpenGLESv2
[fips] / test / egl-glesv2-dlopen-gpa.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 /* Perform some simple drawing via OpenGL as follows:
23  *
24  *      1. Using EGL to construct an OpenGLESv2 context
25  *      2. By using dlopen to dynamically load libGLESv2.so
26  *      3. By using eglGetProcAddress to lookup OpenGL functions
27  */
28
29 #define GL_GLEXT_PROTOTYPES
30 #include <GLES2/gl2.h>
31 #include <EGL/egl.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36
37 #include "util-x11.h"
38
39 EGLBoolean (*my_eglBindAPI)(EGLenum);
40 EGLBoolean (*my_eglChooseConfig)(EGLDisplay, EGLint const *, EGLConfig *, EGLint, EGLint *);
41 EGLContext (*my_eglCreateContext)(EGLDisplay, EGLConfig, EGLContext, EGLint const *);
42 EGLSurface (*my_eglCreateWindowSurface)(EGLDisplay, EGLConfig, NativeWindowType, EGLint const *);
43 EGLBoolean (*my_eglGetConfigAttrib)(EGLDisplay, EGLConfig, EGLint, EGLint *);
44 EGLDisplay (*my_eglGetDisplay)(NativeDisplayType);
45 void * (*my_eglGetProcAddress)(char const *);
46 EGLBoolean (*my_eglInitialize)(EGLDisplay, EGLint *, EGLint *);
47 EGLBoolean (*my_eglMakeCurrent)(EGLDisplay, EGLSurface, EGLSurface, EGLContext);
48 EGLBoolean (*my_eglSwapBuffers)(EGLDisplay, EGLSurface);
49
50 /* The OpenGL header files give typedefs for the types of all
51  * functions provided by etensisons. Here, though, we're using
52  * glxGetProcAddress to lookup core functions, so we provide our own
53  * typedefs. */
54 typedef void (*FIPS_GLCLEAR_FN)(GLbitfield);
55 FIPS_GLCLEAR_FN my_glClear;
56
57 typedef void (*FIPS_GLCLEARCOLOR_FN)(GLclampf, GLclampf, GLclampf, GLclampf);
58 FIPS_GLCLEARCOLOR_FN my_glClearColor;
59
60 typedef void (*FIPS_GLVIEWPORT_FN)(GLint, GLint, GLsizei, GLsizei);
61 FIPS_GLVIEWPORT_FN my_glViewport;
62
63 #define COMMON_USE_EGL
64 #define COMMON_GL_PREFIX my_
65 #define COMMON_EGL_PREFIX my_
66 #include "common.c"
67
68 static void
69 resolve_symbols (void)
70 {
71         void *egl_handle, *glesv2_handle;
72         char *error;
73
74         egl_handle = dlopen ("libEGL.so", RTLD_LAZY);
75         if (egl_handle == NULL) {
76                 fprintf (stderr, "Error: Failed to open libEGL.so\n");
77                 exit (1);
78         }
79
80         glesv2_handle = dlopen ("libGLESv2.so", RTLD_LAZY);
81         if (glesv2_handle == NULL) {
82                 fprintf (stderr, "Error: Failed to open libGLESv2.so\n");
83                 exit (1);
84         }
85
86         /* Clear errors. */
87         dlerror();
88
89         my_eglBindAPI = dlsym (egl_handle, "eglBindAPI");
90         error = dlerror ();
91         if (error) {
92                 fprintf (stderr, "Failed to dlsym eglBindAPI: %s\n", error);
93                 exit (1);
94         }
95
96         my_eglChooseConfig = dlsym (egl_handle, "eglChooseConfig");
97         error = dlerror ();
98         if (error) {
99                 fprintf (stderr, "Failed to dlsym eglChooseConfig: %s\n", error);
100                 exit (1);
101         }
102
103         my_eglCreateContext = dlsym (egl_handle, "eglCreateContext");
104         error = dlerror ();
105         if (error) {
106                 fprintf (stderr, "Failed to dlsym eglCreateContext: %s\n", error);
107                 exit (1);
108         }
109
110         my_eglCreateWindowSurface = dlsym (egl_handle, "eglCreateWindowSurface");
111         error = dlerror ();
112         if (error) {
113                 fprintf (stderr, "Failed to dlsym eglCreateWindowSurface: %s\n", error);
114                 exit (1);
115         }
116
117         my_eglGetConfigAttrib = dlsym (egl_handle, "eglGetConfigAttrib");
118         error = dlerror ();
119         if (error) {
120                 fprintf (stderr, "Failed to dlsym eglGetConfigAttrib: %s\n", error);
121                 exit (1);
122         }
123
124         my_eglGetDisplay = dlsym (egl_handle, "eglGetDisplay");
125         error = dlerror ();
126         if (error) {
127                 fprintf (stderr, "Failed to dlsym eglGetDisplay: %s\n", error);
128                 exit (1);
129         }
130
131         my_eglGetProcAddress = dlsym (egl_handle, "eglGetProcAddress");
132         error = dlerror ();
133         if (error) {
134                 fprintf (stderr, "Failed to dlsym eglGetProcAddress: %s\n", error);
135                 exit (1);
136         }
137
138         my_eglInitialize = dlsym (egl_handle, "eglInitialize");
139         error = dlerror ();
140         if (error) {
141                 fprintf (stderr, "Failed to dlsym eglInitialize: %s\n", error);
142                 exit (1);
143         }
144
145         my_eglMakeCurrent = dlsym (egl_handle, "eglMakeCurrent");
146         error = dlerror ();
147         if (error) {
148                 fprintf (stderr, "Failed to dlsym eglMakeCurrent: %s\n", error);
149                 exit (1);
150         }
151
152         my_eglSwapBuffers = dlsym (egl_handle, "eglSwapBuffers");
153         error = dlerror ();
154         if (error) {
155                 fprintf (stderr, "Failed to dlsym eglSwapBuffers: %s\n", error);
156                 exit (1);
157         }
158
159         my_glClear = (FIPS_GLCLEAR_FN) my_eglGetProcAddress ("glClear");
160         if (my_glClear == NULL) {
161                 fprintf (stderr, "Failed to eglGetProcAddress glClear\n");
162                 exit (1);
163         }
164
165         my_glClearColor = (FIPS_GLCLEARCOLOR_FN) my_eglGetProcAddress ("glClearColor");
166         if (my_glClearColor == NULL) {
167                 fprintf (stderr, "Failed to eglGetProcAddress glClearColor\n");
168                 exit (1);
169         }
170
171         my_glViewport = (FIPS_GLVIEWPORT_FN) my_eglGetProcAddress ("glViewport");
172         if (my_glViewport == NULL) {
173                 fprintf (stderr, "Failed to eglGetProcAddress glViewport\n");
174                 exit (1);
175         }
176 }
177
178 int
179 main (void)
180 {
181         Display *dpy;
182         Window window;
183         EGLDisplay egl_dpy;
184         EGLContext ctx;
185         EGLConfig config;
186         EGLSurface surface;
187         XVisualInfo *visual_info;
188
189         resolve_symbols ();
190
191         dpy = util_x11_init_display ();
192
193         common_create_egl_context (dpy, EGL_OPENGL_ES_API, &egl_dpy,
194                                    &ctx, &config, &visual_info);
195
196         window = util_x11_init_window (dpy, visual_info);
197
198         surface = my_eglCreateWindowSurface (egl_dpy, config, window, NULL);
199
200         common_make_current (egl_dpy, ctx, surface);
201
202         common_handle_events (dpy, egl_dpy, surface);
203
204         util_x11_fini_window (dpy, window);
205
206         util_x11_fini_display (dpy);
207
208         return 0;
209 }