]> git.cworth.org Git - apitrace/blob - glcaps.cpp
apitrace: Replace tracedump program with new "apitrace dump" command
[apitrace] / glcaps.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 /*
28  * Manipulation of GL extensions.
29  *
30  * So far we insert GREMEDY extensions, but in the future we could also clamp
31  * the GL extensions to core GL versions here.
32  */
33
34
35 #include <assert.h>
36 #include <string.h>
37 #include <stdlib.h>
38
39 #include <string>
40 #include <map>
41
42 #include "glproc.hpp"
43 #include "gltrace.hpp"
44
45
46 namespace gltrace {
47
48
49 typedef std::map<std::string, const char *> ExtensionsMap;
50
51 // Cache of the translated extensions strings
52 static ExtensionsMap extensionsMap;
53
54
55 // Additional extensions to be advertised
56 static const char *extra_extensions[] = {
57     "GL_GREMEDY_string_marker",
58     "GL_GREMEDY_frame_terminator",
59 };
60 #define NUM_EXTRA_EXTENSIONS (sizeof(extra_extensions)/sizeof(extra_extensions[0]))
61
62
63 /**
64  * Translate the GL extensions string, adding new extensions.
65  */
66 static const char *
67 overrideExtensionsString(const char *extensions)
68 {
69     size_t i;
70
71     ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
72     if (it != extensionsMap.end()) {
73         return it->second;
74     }
75
76     size_t extensions_len = strlen(extensions);
77
78     size_t extra_extensions_len = 0;
79     for (i = 0; i < NUM_EXTRA_EXTENSIONS; ++i) {
80         const char * extra_extension = extra_extensions[i];
81         size_t extra_extension_len = strlen(extra_extension);
82         extra_extensions_len += extra_extension_len + 1;
83     }
84
85     // We use malloc memory instead of a std::string because we need to ensure
86     // that extensions strings will not move in memory as the extensionsMap is
87     // updated.
88     size_t new_extensions_len = extensions_len + 1 + extra_extensions_len + 1;
89     char *new_extensions = (char *)malloc(new_extensions_len);
90     if (!new_extensions) {
91         return extensions;
92     }
93
94     if (extensions_len) {
95         memcpy(new_extensions, extensions, extensions_len);
96
97         // Add space separator if necessary
98         if (new_extensions[extensions_len - 1] != ' ') {
99             new_extensions[extensions_len++] = ' ';
100         }
101     }
102
103     for (i = 0; i < NUM_EXTRA_EXTENSIONS; ++i) {
104         const char * extra_extension = extra_extensions[i];
105         size_t extra_extension_len = strlen(extra_extension);
106         memcpy(new_extensions + extensions_len, extra_extension, extra_extension_len);
107         extensions_len += extra_extension_len;
108         new_extensions[extensions_len++] = ' ';
109     }
110     new_extensions[extensions_len++] = '\0';
111     assert(extensions_len <= new_extensions_len);
112
113     extensionsMap[extensions] = new_extensions;
114
115     return new_extensions;
116 }
117
118
119 const GLubyte *
120 __glGetString_override(GLenum name)
121 {
122     const GLubyte *result = __glGetString(name);
123
124     if (result) {
125         switch (name) {
126         case GL_EXTENSIONS:
127             result = (const GLubyte *)overrideExtensionsString((const char *)result);
128             break;
129         default:
130             break;
131         }
132     }
133
134     return result;
135 }
136
137
138 void
139 __glGetIntegerv_override(GLenum pname, GLint *params)
140 {
141     __glGetIntegerv(pname, params);
142
143     if (params) {
144         switch (pname) {
145         case GL_NUM_EXTENSIONS:
146             *params += NUM_EXTRA_EXTENSIONS;
147             break;
148         default:
149             break;
150         }
151     }
152 }
153
154
155 const GLubyte *
156 __glGetStringi_override(GLenum name, GLuint index)
157 {
158     switch (name) {
159     case GL_EXTENSIONS:
160         {
161             GLint num_extensions = 0;
162             __glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
163             if ((GLuint)num_extensions <= index && index < (GLuint)num_extensions + NUM_EXTRA_EXTENSIONS) {
164                 return (const GLubyte *)extra_extensions[index - (GLuint)num_extensions];
165             }
166         }
167         break;
168     default:
169         break;
170     }
171
172     return __glGetStringi(name, index);
173 }
174
175
176 } /* namespace gltrace */
177