]> git.cworth.org Git - fips/blob - test/egl-opengl-dlopen-dlsym.c
803d10d15fdd5fa37f5f10ae5d79d79f6b1c3671
[fips] / test / egl-opengl-dlopen-dlsym.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 OpenGL context
25  *      2. By using dlopen to dynamically load libGL.so and libEGL.so
26  *      3. By using dlsym to lookup OpenGL functions
27  */
28
29 #define GL_GLEXT_PROTOTYPES
30 #include <GL/gl.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 EGLBoolean (*my_eglInitialize)(EGLDisplay, EGLint *, EGLint *);
46 EGLBoolean (*my_eglMakeCurrent)(EGLDisplay, EGLSurface, EGLSurface, EGLContext);
47 EGLBoolean (*my_eglSwapBuffers)(EGLDisplay, EGLSurface);
48 void (*my_glClear)(GLbitfield);
49 void (*my_glClearColor)(GLclampf, GLclampf, GLclampf, GLclampf);
50 void (*my_glLoadIdentity)(void);
51 void (*my_glMatrixMode)(GLenum);
52 void (*my_glOrtho)(GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
53 void (*my_glViewport)(GLint, GLint, GLsizei, GLsizei);
54
55 #define COMMON_USE_EGL
56 #define COMMON_GL_PREFIX my_
57 #define COMMON_EGL_PREFIX my_
58 #include "common.c"
59
60 static void
61 resolve_symbols (void)
62 {
63         void *gl_handle;
64         void *egl_handle;
65         char *error;
66
67         gl_handle = dlopen ("libGL.so", RTLD_LAZY);
68         if (gl_handle == NULL) {
69                 fprintf (stderr, "Error: Failed to open libGL.so\n");
70                 exit (1);
71         }
72
73         egl_handle = dlopen ("libEGL.so", RTLD_LAZY);
74         if (egl_handle == NULL) {
75                 fprintf (stderr, "Error: Failed to open libEGL.so\n");
76                 exit (1);
77         }
78
79         /* Clear errors. */
80         dlerror();
81
82         my_eglBindAPI = dlsym (egl_handle, "eglBindAPI");
83         error = dlerror ();
84         if (error) {
85                 fprintf (stderr, "Failed to dlsym eglBindAPI: %s\n", error);
86                 exit (1);
87         }
88
89         my_eglChooseConfig = dlsym (egl_handle, "eglChooseConfig");
90         error = dlerror ();
91         if (error) {
92                 fprintf (stderr, "Failed to dlsym eglChooseConfig: %s\n", error);
93                 exit (1);
94         }
95
96         my_eglCreateContext = dlsym (egl_handle, "eglCreateContext");
97         error = dlerror ();
98         if (error) {
99                 fprintf (stderr, "Failed to dlsym eglCreateContext: %s\n", error);
100                 exit (1);
101         }
102
103         my_eglCreateWindowSurface = dlsym (egl_handle, "eglCreateWindowSurface");
104         error = dlerror ();
105         if (error) {
106                 fprintf (stderr, "Failed to dlsym eglCreateWindowSurface: %s\n", error);
107                 exit (1);
108         }
109
110         my_eglGetConfigAttrib = dlsym (egl_handle, "eglGetConfigAttrib");
111         error = dlerror ();
112         if (error) {
113                 fprintf (stderr, "Failed to dlsym eglGetConfigAttrib: %s\n", error);
114                 exit (1);
115         }
116
117         my_eglGetDisplay = dlsym (egl_handle, "eglGetDisplay");
118         error = dlerror ();
119         if (error) {
120                 fprintf (stderr, "Failed to dlsym eglGetDisplay: %s\n", error);
121                 exit (1);
122         }
123
124         my_eglInitialize = dlsym (egl_handle, "eglInitialize");
125         error = dlerror ();
126         if (error) {
127                 fprintf (stderr, "Failed to dlsym eglInitialize: %s\n", error);
128                 exit (1);
129         }
130
131         my_eglMakeCurrent = dlsym (egl_handle, "eglMakeCurrent");
132         error = dlerror ();
133         if (error) {
134                 fprintf (stderr, "Failed to dlsym eglMakeCurrent: %s\n", error);
135                 exit (1);
136         }
137
138         my_eglSwapBuffers = dlsym (egl_handle, "eglSwapBuffers");
139         error = dlerror ();
140         if (error) {
141                 fprintf (stderr, "Failed to dlsym eglSwapBuffers: %s\n", error);
142                 exit (1);
143         }
144
145         my_glClear = dlsym (gl_handle, "glClear");
146         error = dlerror ();
147         if (error) {
148                 fprintf (stderr, "Failed to dlsym glClear: %s\n", error);
149                 exit (1);
150         }
151
152         my_glClearColor = dlsym (gl_handle, "glClearColor");
153         error = dlerror ();
154         if (error) {
155                 fprintf (stderr, "Failed to dlsym glClearColor: %s\n", error);
156                 exit (1);
157         }
158
159         my_glLoadIdentity = dlsym (gl_handle, "glLoadIdentity");
160         error = dlerror ();
161         if (error) {
162                 fprintf (stderr, "Failed to dlsym glLoadIdentity: %s\n", error);
163                 exit (1);
164         }
165
166         my_glMatrixMode = dlsym (gl_handle, "glMatrixMode");
167         error = dlerror ();
168         if (error) {
169                 fprintf (stderr, "Failed to dlsym glMatrixMode: %s\n", error);
170                 exit (1);
171         }
172
173         my_glOrtho = dlsym (gl_handle, "glOrtho");
174         error = dlerror ();
175         if (error) {
176                 fprintf (stderr, "Failed to dlsym glOrtho: %s\n", error);
177                 exit (1);
178         }
179
180         my_glViewport = dlsym (gl_handle, "glViewport");
181         error = dlerror ();
182         if (error) {
183                 fprintf (stderr, "Failed to dlsym glViewport: %s\n", error);
184                 exit (1);
185         }
186 }
187
188 int
189 main (void)
190 {
191         Display *dpy;
192         Window window;
193         EGLDisplay egl_dpy;
194         EGLContext ctx;
195         EGLConfig config;
196         EGLSurface surface;
197         XVisualInfo *visual_info;
198
199         resolve_symbols ();
200
201         dpy = util_x11_init_display ();
202
203         common_create_egl_context (dpy, EGL_OPENGL_API, &egl_dpy,
204                                    &ctx, &config, &visual_info);
205
206         window = util_x11_init_window (dpy, visual_info);
207
208         surface = my_eglCreateWindowSurface (egl_dpy, config, window, NULL);
209
210         common_make_current (egl_dpy, ctx, surface);
211
212         common_handle_events (dpy, egl_dpy, surface);
213
214         util_x11_fini_window (dpy, window);
215
216         util_x11_fini_display (dpy);
217
218         return 0;
219 }