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 #include "retrace.hpp"
29 #include "glretrace.hpp"
32 using namespace glretrace;
35 typedef std::map<unsigned long long, glws::Drawable *> DrawableMap;
36 typedef std::map<unsigned long long, Context *> ContextMap;
37 static DrawableMap drawable_map;
38 static DrawableMap pbuffer_map;
39 static ContextMap context_map;
42 static glws::Drawable *
43 getDrawable(unsigned long long hdc) {
48 DrawableMap::const_iterator it;
49 it = drawable_map.find(hdc);
50 if (it == drawable_map.end()) {
51 return (drawable_map[hdc] = glretrace::createDrawable());
57 static void retrace_wglCreateContext(trace::Call &call) {
58 unsigned long long orig_context = call.ret->toUIntPtr();
59 Context *context = glretrace::createContext();
60 context_map[orig_context] = context;
63 static void retrace_wglDeleteContext(trace::Call &call) {
64 unsigned long long hglrc = call.arg(0).toUIntPtr();
66 ContextMap::iterator it;
67 it = context_map.find(hglrc);
68 if (it == context_map.end()) {
74 context_map.erase(it);
77 static void retrace_wglMakeCurrent(trace::Call &call) {
78 glws::Drawable *new_drawable = getDrawable(call.arg(0).toUIntPtr());
79 Context *new_context = context_map[call.arg(1).toUIntPtr()];
81 glretrace::makeCurrent(call, new_drawable, new_context);
84 static void retrace_wglCopyContext(trace::Call &call) {
87 static void retrace_wglChoosePixelFormat(trace::Call &call) {
90 static void retrace_wglDescribePixelFormat(trace::Call &call) {
93 static void retrace_wglSetPixelFormat(trace::Call &call) {
96 static void retrace_wglSwapBuffers(trace::Call &call) {
97 glws::Drawable *drawable = getDrawable(call.arg(0).toUIntPtr());
100 if (retrace::doubleBuffer) {
102 drawable->swapBuffers();
104 glretrace::Context *currentContext = glretrace::getCurrentContext();
105 if (currentContext) {
106 currentContext->drawable->swapBuffers();
114 static void retrace_wglShareLists(trace::Call &call) {
115 unsigned long long hglrc1 = call.arg(0).toUIntPtr();
116 unsigned long long hglrc2 = call.arg(1).toUIntPtr();
118 Context *share_context = context_map[hglrc1];
119 Context *old_context = context_map[hglrc2];
121 Context *new_context = glretrace::createContext(share_context);
123 glretrace::Context *currentContext = glretrace::getCurrentContext();
124 if (currentContext == old_context) {
125 glretrace::makeCurrent(call, currentContext->drawable, new_context);
128 context_map[hglrc2] = new_context;
134 static void retrace_wglCreateLayerContext(trace::Call &call) {
135 retrace_wglCreateContext(call);
138 static void retrace_wglDescribeLayerPlane(trace::Call &call) {
141 static void retrace_wglSetLayerPaletteEntries(trace::Call &call) {
144 static void retrace_wglRealizeLayerPalette(trace::Call &call) {
147 static void retrace_wglSwapLayerBuffers(trace::Call &call) {
148 retrace_wglSwapBuffers(call);
151 static void retrace_wglUseFontBitmapsA(trace::Call &call) {
154 static void retrace_wglUseFontBitmapsW(trace::Call &call) {
157 static void retrace_wglSwapMultipleBuffers(trace::Call &call) {
160 static void retrace_wglUseFontOutlinesA(trace::Call &call) {
163 static void retrace_wglUseFontOutlinesW(trace::Call &call) {
166 static void retrace_wglCreateBufferRegionARB(trace::Call &call) {
169 static void retrace_wglDeleteBufferRegionARB(trace::Call &call) {
172 static void retrace_wglSaveBufferRegionARB(trace::Call &call) {
175 static void retrace_wglRestoreBufferRegionARB(trace::Call &call) {
178 static void retrace_wglChoosePixelFormatARB(trace::Call &call) {
181 static void retrace_wglMakeContextCurrentARB(trace::Call &call) {
184 static void retrace_wglCreatePbufferARB(trace::Call &call) {
185 int iWidth = call.arg(2).toUInt();
186 int iHeight = call.arg(3).toUInt();
188 unsigned long long orig_pbuffer = call.ret->toUIntPtr();
189 glws::Drawable *drawable = glretrace::createPbuffer(iWidth, iHeight);
191 pbuffer_map[orig_pbuffer] = drawable;
194 static void retrace_wglGetPbufferDCARB(trace::Call &call) {
195 glws::Drawable *pbuffer = pbuffer_map[call.arg(0).toUIntPtr()];
197 unsigned long long orig_hdc = call.ret->toUIntPtr();
199 drawable_map[orig_hdc] = pbuffer;
202 static void retrace_wglReleasePbufferDCARB(trace::Call &call) {
205 static void retrace_wglDestroyPbufferARB(trace::Call &call) {
208 static void retrace_wglQueryPbufferARB(trace::Call &call) {
211 static void retrace_wglBindTexImageARB(trace::Call &call) {
214 static void retrace_wglReleaseTexImageARB(trace::Call &call) {
217 static void retrace_wglSetPbufferAttribARB(trace::Call &call) {
220 static void retrace_wglCreateContextAttribsARB(trace::Call &call) {
221 unsigned long long orig_context = call.ret->toUIntPtr();
222 Context *share_context = NULL;
224 if (call.arg(1).toPointer()) {
225 share_context = context_map[call.arg(1).toUIntPtr()];
228 Context *context = glretrace::createContext(share_context);
229 context_map[orig_context] = context;
232 static void retrace_wglMakeContextCurrentEXT(trace::Call &call) {
235 static void retrace_wglChoosePixelFormatEXT(trace::Call &call) {
238 static void retrace_wglSwapIntervalEXT(trace::Call &call) {
241 static void retrace_wglAllocateMemoryNV(trace::Call &call) {
244 static void retrace_wglFreeMemoryNV(trace::Call &call) {
247 static void retrace_glAddSwapHintRectWIN(trace::Call &call) {
250 static void retrace_wglGetProcAddress(trace::Call &call) {
253 const retrace::Entry glretrace::wgl_callbacks[] = {
254 {"glAddSwapHintRectWIN", &retrace_glAddSwapHintRectWIN},
255 {"wglAllocateMemoryNV", &retrace_wglAllocateMemoryNV},
256 {"wglBindTexImageARB", &retrace_wglBindTexImageARB},
257 {"wglChoosePixelFormat", &retrace_wglChoosePixelFormat},
258 {"wglChoosePixelFormatARB", &retrace_wglChoosePixelFormatARB},
259 {"wglChoosePixelFormatEXT", &retrace_wglChoosePixelFormatEXT},
260 {"wglCopyContext", &retrace_wglCopyContext},
261 {"wglCreateBufferRegionARB", &retrace_wglCreateBufferRegionARB},
262 {"wglCreateContext", &retrace_wglCreateContext},
263 {"wglCreateContextAttribsARB", &retrace_wglCreateContextAttribsARB},
264 {"wglCreateLayerContext", &retrace_wglCreateLayerContext},
265 {"wglCreatePbufferARB", &retrace_wglCreatePbufferARB},
266 {"wglDeleteBufferRegionARB", &retrace_wglDeleteBufferRegionARB},
267 {"wglDeleteContext", &retrace_wglDeleteContext},
268 {"wglDescribeLayerPlane", &retrace_wglDescribeLayerPlane},
269 {"wglDescribePixelFormat", &retrace_wglDescribePixelFormat},
270 {"wglDestroyPbufferARB", &retrace_wglDestroyPbufferARB},
271 {"wglFreeMemoryNV", &retrace_wglFreeMemoryNV},
272 {"wglGetCurrentContext", &retrace::ignore},
273 {"wglGetCurrentDC", &retrace::ignore},
274 {"wglGetCurrentReadDCARB", &retrace::ignore},
275 {"wglGetCurrentReadDCEXT", &retrace::ignore},
276 {"wglGetDefaultProcAddress", &retrace::ignore},
277 {"wglGetExtensionsStringARB", &retrace::ignore},
278 {"wglGetExtensionsStringEXT", &retrace::ignore},
279 {"wglGetLayerPaletteEntries", &retrace::ignore},
280 {"wglGetPbufferDCARB", &retrace_wglGetPbufferDCARB},
281 {"wglGetPixelFormat", &retrace::ignore},
282 {"wglGetPixelFormatAttribfvARB", &retrace::ignore},
283 {"wglGetPixelFormatAttribfvEXT", &retrace::ignore},
284 {"wglGetPixelFormatAttribivARB", &retrace::ignore},
285 {"wglGetPixelFormatAttribivEXT", &retrace::ignore},
286 {"wglGetProcAddress", &retrace_wglGetProcAddress},
287 {"wglGetSwapIntervalEXT", &retrace::ignore},
288 {"wglMakeContextCurrentARB", &retrace_wglMakeContextCurrentARB},
289 {"wglMakeContextCurrentEXT", &retrace_wglMakeContextCurrentEXT},
290 {"wglMakeCurrent", &retrace_wglMakeCurrent},
291 {"wglQueryPbufferARB", &retrace_wglQueryPbufferARB},
292 {"wglRealizeLayerPalette", &retrace_wglRealizeLayerPalette},
293 {"wglReleasePbufferDCARB", &retrace_wglReleasePbufferDCARB},
294 {"wglReleaseTexImageARB", &retrace_wglReleaseTexImageARB},
295 {"wglRestoreBufferRegionARB", &retrace_wglRestoreBufferRegionARB},
296 {"wglSaveBufferRegionARB", &retrace_wglSaveBufferRegionARB},
297 {"wglSetLayerPaletteEntries", &retrace_wglSetLayerPaletteEntries},
298 {"wglSetPbufferAttribARB", &retrace_wglSetPbufferAttribARB},
299 {"wglSetPixelFormat", &retrace_wglSetPixelFormat},
300 {"wglShareLists", &retrace_wglShareLists},
301 {"wglSwapBuffers", &retrace_wglSwapBuffers},
302 {"wglSwapIntervalEXT", &retrace_wglSwapIntervalEXT},
303 {"wglSwapLayerBuffers", &retrace_wglSwapLayerBuffers},
304 {"wglSwapMultipleBuffers", &retrace_wglSwapMultipleBuffers},
305 {"wglUseFontBitmapsA", &retrace_wglUseFontBitmapsA},
306 {"wglUseFontBitmapsW", &retrace_wglUseFontBitmapsW},
307 {"wglUseFontOutlinesA", &retrace_wglUseFontOutlinesA},
308 {"wglUseFontOutlinesW", &retrace_wglUseFontOutlinesW},