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