]> git.cworth.org Git - fips/blob - test/glx-dlopen-gpaa.c
1189c3c00d96bcf7e9948686b6b4dc78ea5e1b43
[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 glXGetProcAddressARB 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-x11.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 #define HANDLE_EVENTS_GL_PREFIX my_
53 #include "handle-events.c"
54
55 static void
56 resolve_symbols (void)
57 {
58         void *gl_handle;
59         char *error;
60
61         gl_handle = dlopen ("libGL.so", RTLD_LAZY);
62         if (gl_handle == NULL) {
63                 fprintf (stderr, "Error: Failed to open libGL.so\n");
64                 exit (1);
65         }
66
67         /* Clear errors. */
68         dlerror();
69
70         my_glXGetProcAddressARB = dlsym (gl_handle, "glXGetProcAddressARB");
71         error = dlerror ();
72         if (error) {
73                 fprintf (stderr, "Failed to dlsym glXGetProcAddressARB: %s\n", error);
74                 exit (1);
75         }
76
77         my_glClear = my_glXGetProcAddressARB ("glClear");
78         if (my_glClear == NULL) {
79                 fprintf (stderr, "Failed to glXGetProcAddressARB glClear\n");
80                 exit (1);
81         }
82
83         my_glClearColor = my_glXGetProcAddressARB ("glClearColor");
84         if (my_glClearColor == NULL) {
85                 fprintf (stderr, "Failed to glXGetProcAddressARB glClearColor\n");
86                 exit (1);
87         }
88
89         my_glLoadIdentity = my_glXGetProcAddressARB ("glLoadIdentity");
90         if (my_glLoadIdentity == NULL) {
91                 fprintf (stderr, "Failed to glXGetProcAddressARB glLoadIdentity\n");
92                 exit (1);
93         }
94
95         my_glMatrixMode = my_glXGetProcAddressARB ("glMatrixMode");
96         if (my_glMatrixMode == NULL) {
97                 fprintf (stderr, "Failed to glXGetProcAddressARB glMatrixMode\n");
98                 exit (1);
99         }
100
101         my_glOrtho = my_glXGetProcAddressARB ("glOrtho");
102         if (my_glOrtho == NULL) {
103                 fprintf (stderr, "Failed to glXGetProcAddressARB glOrtho\n");
104                 exit (1);
105         }
106
107         my_glViewport = my_glXGetProcAddressARB ("glViewport");
108         if (my_glViewport == NULL) {
109                 fprintf (stderr, "Failed to glXGetProcAddressARB glViewport\n");
110                 exit (1);
111         }
112
113         my_glXChooseVisual = my_glXGetProcAddressARB ("glXChooseVisual");
114         if (my_glXChooseVisual == NULL) {
115                 fprintf (stderr, "Failed to glXGetProcAddressARB glXChooseVisual\n");
116                 exit (1);
117         }
118
119         my_glXCreateContext = my_glXGetProcAddressARB ("glXCreateContext");
120         if (my_glXCreateContext == NULL) {
121                 fprintf (stderr, "Failed to glXGetProcAddressARB glXCreateContext\n");
122                 exit (1);
123         }
124
125         my_glXDestroyContext = my_glXGetProcAddressARB ("glXDestroyContext");
126         if (my_glXDestroyContext == NULL) {
127                 fprintf (stderr, "Failed to glXGetProcAddressARB glXDestroyContext\n");
128                 exit (1);
129         }
130
131         my_glXMakeCurrent = my_glXGetProcAddressARB ("glXMakeCurrent");
132         if (my_glXMakeCurrent == NULL) {
133                 fprintf (stderr, "Failed to glXGetProcAddressARB glXMakeCurrent\n");
134                 exit (1);
135         }
136
137         my_glXSwapBuffers = my_glXGetProcAddressARB ("glXSwapBuffers");
138         if (my_glXSwapBuffers == NULL) {
139                 fprintf (stderr, "Failed to glXGetProcAddressARB glXSwapBuffers\n");
140                 exit (1);
141         }
142 }
143
144 int
145 main (void)
146 {
147         Display *dpy;
148         Window window;
149
150         util_x11_init_display (&dpy);
151
152         util_x11_init_window (dpy, &window);
153
154         resolve_symbols ();
155
156         handle_events (dpy, window);
157
158         util_x11_fini_window (dpy, window);
159
160         util_x11_fini_display (dpy);
161
162         return 0;
163 }