]> git.cworth.org Git - fips/blob - test/glx-dlopen-dlsym.c
test: Add test using GLX with dlopen and dlsym to find symbols
[fips] / test / glx-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 GLX to construct an OpenGL context
25  *      2. By using dlopen to dynamically load libGL.so
26  *      3. By using dlsym to lookup OpenGL functions
27  */
28
29 #define GL_GLEXT_PROTOTYPES
30 #include <GL/gl.h>
31 #include <GL/glx.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36
37 #include "util.h"
38
39 void (*my_glClear) (GLbitfield);
40 void (*my_glClearColor) (GLclampf, GLclampf, GLclampf, GLclampf);
41 void (*my_glLoadIdentity) (void);
42 void (*my_glMatrixMode) (GLenum);
43 void (*my_glOrtho) (GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
44 void (*my_glViewport) (GLint, GLint, GLsizei, GLsizei);
45 XVisualInfo * (*my_glXChooseVisual) (Display *, int, int *);
46 GLXContext (*my_glXCreateContext) (Display *, XVisualInfo *, GLXContext, Bool);
47 void (*my_glXDestroyContext) (Display *, GLXContext);
48 Bool (*my_glXMakeCurrent) (Display *, GLXDrawable, GLXContext);
49 void (*my_glXSwapBuffers) (Display *, GLXDrawable);
50
51 static void
52 set_2d_projection (int width, int height)
53 {
54         my_glMatrixMode (GL_PROJECTION);
55         my_glLoadIdentity ();
56         my_glOrtho (0, width, height, 0, 0, 1);
57         my_glMatrixMode (GL_MODELVIEW);
58 }
59
60 static void
61 paint_rgb_using_clear (double r, double g, double b)
62 {
63         my_glClearColor(r, g, b, 1.0);
64         my_glClear(GL_COLOR_BUFFER_BIT);
65 }
66
67 static void
68 draw (Display *dpy, Window window, int width, int height)
69 {
70         int i;
71         int visual_attr[] = {
72                 GLX_RGBA,
73                 GLX_RED_SIZE,           8,
74                 GLX_GREEN_SIZE,         8,
75                 GLX_BLUE_SIZE,          8,
76                 GLX_ALPHA_SIZE,         8,
77                 GLX_DOUBLEBUFFER,
78                 GLX_DEPTH_SIZE,         24,
79                 GLX_STENCIL_SIZE,       8,
80                 GLX_X_VISUAL_TYPE,      GLX_DIRECT_COLOR,
81                 None
82         };
83
84         /* Window and context setup. */
85         XVisualInfo *visual_info = my_glXChooseVisual(dpy, 0, visual_attr);
86         GLXContext ctx = my_glXCreateContext(dpy, visual_info, NULL, True);
87         my_glXMakeCurrent(dpy, window, ctx);
88
89         my_glViewport(0, 0, width, height);
90
91         set_2d_projection (width, height);
92
93 /* Simply count through some colors, frame by frame. */
94 #define RGB(frame) (((frame+1)/4) % 2), (((frame+1)/2) % 2), ((frame+1) % 2)
95
96         int frame = 0;
97         for (i = 0; i < 2; i++) {
98                 /* Frame: Draw a solid (magenta) frame */
99                 paint_rgb_using_clear (RGB(frame));
100                 my_glXSwapBuffers (dpy, window);
101                 frame++;
102
103                 /* Frame: Draw a solid (yellow) frame */
104                 paint_rgb_using_clear (RGB(frame));
105                 my_glXSwapBuffers (dpy, window);
106                 frame++;
107
108                 /* Frame: Draw a solid (cyan) frame */
109                 paint_rgb_using_clear (RGB(frame));
110                 my_glXSwapBuffers (dpy, window);
111                 frame++;
112         }
113
114         /* Cleanup */
115         my_glXDestroyContext (dpy, ctx);
116 }
117
118 static void
119 handle_events(Display *dpy, Window window)
120 {
121         XEvent xev;
122         int width = 0;
123         int height = 0;
124
125         XNextEvent (dpy, &xev);
126
127         while (1) {
128                 XNextEvent (dpy, &xev);
129                 switch (xev.type) {
130                 case ConfigureNotify:
131                         width = xev.xconfigure.width;
132                         height = xev.xconfigure.height;
133                         break;
134                 case Expose:
135                         if (xev.xexpose.count == 0) {
136                                 draw (dpy, window, width, height);
137                                 return;
138                         }
139                         break;
140                 }
141         }
142 }
143
144 static void
145 resolve_symbols (void)
146 {
147         void *gl_handle;
148         char *error;
149
150         gl_handle = dlopen ("libGL.so", RTLD_LAZY);
151         if (gl_handle == NULL) {
152                 fprintf (stderr, "Error: Failed to open libGL.so\n");
153                 exit (1);
154         }
155
156         /* Clear errors. */
157         dlerror();
158
159         my_glClear = dlsym (gl_handle, "glClear");
160         error = dlerror ();
161         if (error) {
162                 fprintf (stderr, "Failed to dlsym glClear: %s\n", error);
163                 exit (1);
164         }
165
166         my_glClearColor = dlsym (gl_handle, "glClearColor");
167         error = dlerror ();
168         if (error) {
169                 fprintf (stderr, "Failed to dlsym glClearColor: %s\n", error);
170                 exit (1);
171         }
172
173         my_glLoadIdentity = dlsym (gl_handle, "glLoadIdentity");
174         error = dlerror ();
175         if (error) {
176                 fprintf (stderr, "Failed to dlsym glLoadIdentity: %s\n", error);
177                 exit (1);
178         }
179
180         my_glMatrixMode = dlsym (gl_handle, "glMatrixMode");
181         error = dlerror ();
182         if (error) {
183                 fprintf (stderr, "Failed to dlsym glMatrixMode: %s\n", error);
184                 exit (1);
185         }
186
187         my_glOrtho = dlsym (gl_handle, "glOrtho");
188         error = dlerror ();
189         if (error) {
190                 fprintf (stderr, "Failed to dlsym glOrtho: %s\n", error);
191                 exit (1);
192         }
193
194         my_glViewport = dlsym (gl_handle, "glViewport");
195         error = dlerror ();
196         if (error) {
197                 fprintf (stderr, "Failed to dlsym glViewport: %s\n", error);
198                 exit (1);
199         }
200
201         my_glXChooseVisual = dlsym (gl_handle, "glXChooseVisual");
202         error = dlerror ();
203         if (error) {
204                 fprintf (stderr, "Failed to dlsym glXChooseVisual: %s\n", error);
205                 exit (1);
206         }
207
208         my_glXCreateContext = dlsym (gl_handle, "glXCreateContext");
209         error = dlerror ();
210         if (error) {
211                 fprintf (stderr, "Failed to dlsym glXCreateContext: %s\n", error);
212                 exit (1);
213         }
214
215         my_glXDestroyContext = dlsym (gl_handle, "glXDestroyContext");
216         error = dlerror ();
217         if (error) {
218                 fprintf (stderr, "Failed to dlsym glXDestroyContext: %s\n", error);
219                 exit (1);
220         }
221
222         my_glXMakeCurrent = dlsym (gl_handle, "glXMakeCurrent");
223         error = dlerror ();
224         if (error) {
225                 fprintf (stderr, "Failed to dlsym glXMakeCurrent: %s\n", error);
226                 exit (1);
227         }
228
229         my_glXSwapBuffers = dlsym (gl_handle, "glXSwapBuffers");
230         error = dlerror ();
231         if (error) {
232                 fprintf (stderr, "Failed to dlsym glXSwapBuffers: %s\n", error);
233                 exit (1);
234         }
235 }
236
237 int
238 main (void)
239 {
240         Display *dpy;
241         Window window;
242
243         util_init_display_window (&dpy, &window);
244
245         resolve_symbols ();
246
247         handle_events (dpy, window);
248
249         util_fini_display_window (dpy, window);
250
251         return 0;
252 }