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