blob: 2f16b63c172febde9021a7ad1f7e3b26f41d981c [file] [log] [blame]
José Fonseca1b3d3752011-07-15 10:15:19 +01001/**************************************************************************
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
José Fonsecaa08d2752011-08-25 13:26:43 +010027/*
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
José Fonseca02873842011-10-27 13:23:17 +010035#include <assert.h>
José Fonseca1b3d3752011-07-15 10:15:19 +010036#include <string.h>
37#include <stdlib.h>
38
39#include <string>
40#include <map>
41
José Fonsecaa08d2752011-08-25 13:26:43 +010042#include "glproc.hpp"
José Fonseca1b3d3752011-07-15 10:15:19 +010043#include "gltrace.hpp"
44
45
46namespace gltrace {
47
48
49typedef std::map<std::string, const char *> ExtensionsMap;
50
51// Cache of the translated extensions strings
52static ExtensionsMap extensionsMap;
53
54
55// Additional extensions to be advertised
José Fonsecaa08d2752011-08-25 13:26:43 +010056static 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]))
José Fonseca1b3d3752011-07-15 10:15:19 +010061
62
63/**
64 * Translate the GL extensions string, adding new extensions.
65 */
José Fonsecaa08d2752011-08-25 13:26:43 +010066static const char *
67overrideExtensionsString(const char *extensions)
José Fonseca1b3d3752011-07-15 10:15:19 +010068{
José Fonseca7525e6f2011-09-28 09:04:56 +010069 size_t i;
José Fonsecaa08d2752011-08-25 13:26:43 +010070
José Fonseca1b3d3752011-07-15 10:15:19 +010071 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
José Fonsecaa08d2752011-08-25 13:26:43 +010078 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
José Fonseca02873842011-10-27 13:23:17 +010085 // 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);
José Fonseca1b3d3752011-07-15 10:15:19 +010090 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
José Fonsecaa08d2752011-08-25 13:26:43 +0100103 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 }
José Fonseca02873842011-10-27 13:23:17 +0100110 new_extensions[extensions_len++] = '\0';
111 assert(extensions_len <= new_extensions_len);
José Fonseca1b3d3752011-07-15 10:15:19 +0100112
113 extensionsMap[extensions] = new_extensions;
114
115 return new_extensions;
116}
117
118
José Fonsecaa08d2752011-08-25 13:26:43 +0100119const 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
138void
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
155const 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);
José Fonseca7525e6f2011-09-28 09:04:56 +0100163 if ((GLuint)num_extensions <= index && index < (GLuint)num_extensions + NUM_EXTRA_EXTENSIONS) {
164 return (const GLubyte *)extra_extensions[index - (GLuint)num_extensions];
José Fonsecaa08d2752011-08-25 13:26:43 +0100165 }
166 }
167 break;
168 default:
169 break;
170 }
171
172 return __glGetStringi(name, index);
173}
174
175
José Fonseca1b3d3752011-07-15 10:15:19 +0100176} /* namespace gltrace */
177