]> git.cworth.org Git - fips/blob - glwrap.c
Rework timer queries to run continuously.
[fips] / glwrap.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 #include "dlwrap.h"
23
24 /* The prototypes for some OpenGL functions changed at one point from:
25  *
26  *      const void* *indices
27  * to:
28  *      const void* const coist *indices
29  *
30  * This makes it difficult for us to provide an implementation of
31  * these functions that is consistent with the locally-available gl.h
32  * since we don't know if the extra const will be present or not.
33  *
34  * To workaround this problem, we simply #define away const altogether
35  * before including gl.h.
36  *
37  * Kudos to Keith Packard for suggesting this kludge.
38  */
39 #define const
40
41 #define GL_GLEXT_PROTOTYPES
42 #include <GL/gl.h>
43
44 #include "fips.h"
45
46 #include "glwrap.h"
47
48 #include "metrics.h"
49
50 static void *gl_handle;
51
52 void
53 glwrap_set_gl_handle (void *handle)
54 {
55         if (gl_handle == NULL)
56                 gl_handle = handle;
57 }
58
59 void *
60 glwrap_lookup (char *name)
61 {
62         void *ret;
63
64         /* We don't call dlopen here to find the library in which to
65          * perform a dlsym lookup. That's because the application may
66          * be loading libGL.so or libGLESv2.so for its OpenGL symbols.
67          *
68          * So we instead watch for one of those filenames to go by in
69          * our dlopen wrapper, which will then call
70          * glwrap_set_gl_handle to give us the handle to use here.
71          *
72          * If the application hasn't called dlopen on a "libGL"
73          * library, then presumably the application is linked directly
74          * to an OpenGL implementation. In this case, we can use
75          * RTLD_NEXT to find the symbol.
76          *
77          * But just in case, we also let the user override that by
78          * specifying the FIPS_LIBGL environment variable to the path
79          * of the real libGL.so library that fips should dlopen here.
80          */
81         if (gl_handle == NULL) {
82                 const char *path;
83
84                 path = getenv ("FIPS_LIBGL");
85                 if (path) {
86                         gl_handle = dlopen (path, RTLD_LAZY);
87
88                         if (gl_handle == NULL) {
89                                 fprintf (stderr, "Failed to dlopen FIPS_LIBGL: "
90                                          "%s\n", path);
91                                 exit (1);
92                         }
93                 } else {
94                         gl_handle = RTLD_NEXT;
95                 }
96         }
97
98         ret = dlwrap_real_dlsym (gl_handle, name);
99
100         if (ret == NULL) {
101                 fprintf (stderr, "Error: glwrap_lookup failed to dlsym %s\n",
102                          name);
103                 exit (1);
104         }
105
106         return ret;
107 }
108
109 /* With a program change, we stop the counter, update the
110  * active program, then start the counter up again. */
111 void
112 glUseProgram (GLuint program)
113 {
114         metrics_counter_stop ();
115
116         metrics_set_current_program (program);
117
118         metrics_counter_start ();
119
120         GLWRAP_DEFER(glUseProgram, program);
121 }
122
123 void
124 glUseProgramObjectARB (GLhandleARB programObj)
125 {
126         metrics_counter_stop ();
127
128         metrics_set_current_program (programObj);
129
130         metrics_counter_start ();
131
132         GLWRAP_DEFER(glUseProgramObjectARB, programObj);
133 }