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