]> git.cworth.org Git - fips/blob - fips-dispatch.c
c5f767cee7f2112e34c768d829d892013292dc81
[fips] / fips-dispatch.c
1 /* Copyright © 2012, 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 #include "fips.h"
23
24 #include "fips-dispatch.h"
25
26 #define GL_GLEXT_PROTOTYPES
27 #include <GL/gl.h>
28 #include <GL/glx.h>
29
30 #include <EGL/egl.h>
31
32 static bool fips_initialized;
33 static fips_api_t fips_api;
34
35 void
36 fips_dispatch_init (fips_api_t api)
37 {
38         fips_api = api;
39
40         fips_initialized = true;
41 }
42
43 static void
44 check_initialized (void)
45 {
46         if (fips_initialized)
47                 return;
48
49         fprintf (stderr,
50                  "Internal error: fips_dispatch_init must be called before\n"
51                  "any OpenGL function supported via fips_dispatch.\n");
52         exit (1);
53 }
54
55 static void
56 unsupported (const char *name)
57 {
58         fprintf (stderr, "Error: fips failed to find support for %s\n", name);
59
60         exit (1);
61 }
62
63 static void *
64 lookup (const char *name)
65 {
66         if (fips_api == FIPS_API_GLX)
67                 return glXGetProcAddressARB ((const GLubyte *)name);
68         else
69                 return eglGetProcAddress (name);
70 }
71
72 static void
73 resolve_glGenQueries (void)
74 {
75         fips_dispatch_glGenQueries = lookup ("glGenQueries");
76
77         if (! fips_dispatch_glGenQueries)
78                 fips_dispatch_glGenQueries = lookup ("glGenQueriesARB");
79
80         if (! fips_dispatch_glGenQueries)
81                 unsupported ("GenQueries");
82 }
83
84 static void
85 stub_glGenQueries (GLsizei n, GLuint *ids)
86 {
87         check_initialized ();
88         resolve_glGenQueries ();
89         fips_dispatch_glGenQueries (n, ids);
90 }
91
92 PFNGLGENQUERIESPROC fips_dispatch_glGenQueries = stub_glGenQueries;
93
94 static void
95 resolve_glDeleteQueries (void)
96 {
97         fips_dispatch_glDeleteQueries = lookup ("glDeleteQueries");
98
99         if (! fips_dispatch_glDeleteQueries)
100                 fips_dispatch_glDeleteQueries = lookup ("glDeleteQueriesARB");
101
102         if (! fips_dispatch_glDeleteQueries)
103                 unsupported ("DeleteQueries");
104 }
105
106 static void
107 stub_glDeleteQueries (GLsizei n, const GLuint * ids)
108 {
109         check_initialized ();
110         resolve_glDeleteQueries ();
111         fips_dispatch_glDeleteQueries (n, ids);
112 }
113
114 PFNGLDELETEQUERIESPROC fips_dispatch_glDeleteQueries = stub_glDeleteQueries;
115
116 static void
117 resolve_glBeginQuery (void)
118 {
119         fips_dispatch_glBeginQuery = lookup ("glBeginQuery");
120
121         if (! fips_dispatch_glBeginQuery)
122                 fips_dispatch_glBeginQuery = lookup ("glBeginQueryARB");
123
124         if (! fips_dispatch_glBeginQuery)
125                 unsupported ("BeginQuery");
126 }
127
128 static void
129 stub_glBeginQuery (GLenum target, GLuint id)
130 {
131         check_initialized ();
132         resolve_glBeginQuery ();
133         fips_dispatch_glBeginQuery (target, id);
134 }
135
136 PFNGLBEGINQUERYPROC fips_dispatch_glBeginQuery = stub_glBeginQuery;
137
138 static void
139 resolve_glEndQuery (void)
140 {
141         fips_dispatch_glEndQuery = lookup ("glEndQuery");
142
143         if (! fips_dispatch_glEndQuery)
144                 fips_dispatch_glEndQuery = lookup ("glEndQueryARB");
145
146         if (! fips_dispatch_glEndQuery)
147                 unsupported ("EndQuery");
148 }
149
150 static void
151 stub_glEndQuery (GLenum target)
152 {
153         check_initialized ();
154         resolve_glEndQuery ();
155         fips_dispatch_glEndQuery (target);
156 }
157
158 PFNGLENDQUERYPROC fips_dispatch_glEndQuery = stub_glEndQuery;
159
160 static void
161 resolve_glGetQueryObjectuiv (void)
162 {
163         fips_dispatch_glGetQueryObjectuiv = (PFNGLGETQUERYOBJECTUIVARBPROC) lookup("glGetQueryObjectuivARB");
164
165         if (! fips_dispatch_glGetQueryObjectuiv)
166                 unsupported ("GetQueryObjectuiv");
167 }
168
169 static void
170 stub_glGetQueryObjectuiv (GLuint id, GLenum pname, GLuint * params)
171 {
172         check_initialized ();
173         resolve_glGetQueryObjectuiv ();
174         fips_dispatch_glGetQueryObjectuiv (id, pname, params);
175 }
176
177 PFNGLGETQUERYOBJECTUIVPROC fips_dispatch_glGetQueryObjectuiv = stub_glGetQueryObjectuiv;