]> git.cworth.org Git - apitrace/blob - glcaps.cpp
Use Cocoa on Mac OS X.
[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 <string.h>
36 #include <stdlib.h>
37
38 #include <string>
39 #include <map>
40
41 #include "glproc.hpp"
42 #include "gltrace.hpp"
43
44
45 namespace gltrace {
46
47
48 typedef std::map<std::string, const char *> ExtensionsMap;
49
50 // Cache of the translated extensions strings
51 static ExtensionsMap extensionsMap;
52
53
54 // Additional extensions to be advertised
55 static const char *extra_extensions[] = {
56     "GL_GREMEDY_string_marker",
57     "GL_GREMEDY_frame_terminator",
58 };
59 #define NUM_EXTRA_EXTENSIONS (sizeof(extra_extensions)/sizeof(extra_extensions[0]))
60
61
62 /**
63  * Translate the GL extensions string, adding new extensions.
64  */
65 static const char *
66 overrideExtensionsString(const char *extensions)
67 {
68     size_t i;
69
70     ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
71     if (it != extensionsMap.end()) {
72         return it->second;
73     }
74
75     size_t extensions_len = strlen(extensions);
76
77     size_t extra_extensions_len = 0;
78     for (i = 0; i < NUM_EXTRA_EXTENSIONS; ++i) {
79         const char * extra_extension = extra_extensions[i];
80         size_t extra_extension_len = strlen(extra_extension);
81         extra_extensions_len += extra_extension_len + 1;
82     }
83
84     char *new_extensions = (char *)malloc(extensions_len + 1 + extra_extensions_len);
85     if (!new_extensions) {
86         return extensions;
87     }
88
89     if (extensions_len) {
90         memcpy(new_extensions, extensions, extensions_len);
91
92         // Add space separator if necessary
93         if (new_extensions[extensions_len - 1] != ' ') {
94             new_extensions[extensions_len++] = ' ';
95         }
96     }
97
98     for (i = 0; i < NUM_EXTRA_EXTENSIONS; ++i) {
99         const char * extra_extension = extra_extensions[i];
100         size_t extra_extension_len = strlen(extra_extension);
101         memcpy(new_extensions + extensions_len, extra_extension, extra_extension_len);
102         extensions_len += extra_extension_len;
103         new_extensions[extensions_len++] = ' ';
104     }
105     new_extensions[extensions_len] = '\0';
106
107     extensionsMap[extensions] = new_extensions;
108
109     return new_extensions;
110 }
111
112
113 const GLubyte *
114 __glGetString_override(GLenum name)
115 {
116     const GLubyte *result = __glGetString(name);
117
118     if (result) {
119         switch (name) {
120         case GL_EXTENSIONS:
121             result = (const GLubyte *)overrideExtensionsString((const char *)result);
122             break;
123         default:
124             break;
125         }
126     }
127
128     return result;
129 }
130
131
132 void
133 __glGetIntegerv_override(GLenum pname, GLint *params)
134 {
135     __glGetIntegerv(pname, params);
136
137     if (params) {
138         switch (pname) {
139         case GL_NUM_EXTENSIONS:
140             *params += NUM_EXTRA_EXTENSIONS;
141             break;
142         default:
143             break;
144         }
145     }
146 }
147
148
149 const GLubyte *
150 __glGetStringi_override(GLenum name, GLuint index)
151 {
152     switch (name) {
153     case GL_EXTENSIONS:
154         {
155             GLint num_extensions = 0;
156             __glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
157             if ((GLuint)num_extensions <= index && index < (GLuint)num_extensions + NUM_EXTRA_EXTENSIONS) {
158                 return (const GLubyte *)extra_extensions[index - (GLuint)num_extensions];
159             }
160         }
161         break;
162     default:
163         break;
164     }
165
166     return __glGetStringi(name, index);
167 }
168
169
170 } /* namespace gltrace */
171