1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
28 * Manipulation of GL extensions.
30 * So far we insert GREMEDY extensions, but in the future we could also clamp
31 * the GL extensions to core GL versions here.
43 #include "gltrace.hpp"
49 typedef std::map<std::string, const char *> ExtensionsMap;
51 // Cache of the translated extensions strings
52 static ExtensionsMap extensionsMap;
55 // Additional extensions to be advertised
56 static const char *extra_extensions[] = {
57 "GL_GREMEDY_string_marker",
58 "GL_GREMEDY_frame_terminator",
60 #define NUM_EXTRA_EXTENSIONS (sizeof(extra_extensions)/sizeof(extra_extensions[0]))
64 * Translate the GL extensions string, adding new extensions.
67 overrideExtensionsString(const char *extensions)
71 ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
72 if (it != extensionsMap.end()) {
76 size_t extensions_len = strlen(extensions);
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;
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
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) {
95 memcpy(new_extensions, extensions, extensions_len);
97 // Add space separator if necessary
98 if (new_extensions[extensions_len - 1] != ' ') {
99 new_extensions[extensions_len++] = ' ';
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++] = ' ';
110 new_extensions[extensions_len++] = '\0';
111 assert(extensions_len <= new_extensions_len);
113 extensionsMap[extensions] = new_extensions;
115 return new_extensions;
120 __glGetString_override(GLenum name)
122 const GLubyte *result = __glGetString(name);
127 result = (const GLubyte *)overrideExtensionsString((const char *)result);
139 __glGetIntegerv_override(GLenum pname, GLint *params)
141 __glGetIntegerv(pname, params);
145 case GL_NUM_EXTENSIONS:
146 *params += NUM_EXTRA_EXTENSIONS;
156 __glGetStringi_override(GLenum name, GLuint index)
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];
172 return __glGetStringi(name, index);
176 } /* namespace gltrace */