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, glws::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] = glws::createDrawable(visual[glretrace::defaultProfile]));
57 static void retrace_wglCreateContext(trace::Call &call) {
58 unsigned long long orig_context = call.ret->toUIntPtr();
59 glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], NULL, glretrace::defaultProfile);
60 context_map[orig_context] = context;
63 static void retrace_wglDeleteContext(trace::Call &call) {
66 static void retrace_wglMakeCurrent(trace::Call &call) {
67 if (drawable && context) {
74 glws::Drawable *new_drawable = getDrawable(call.arg(0).toUIntPtr());
75 glws::Context *new_context = context_map[call.arg(1).toUIntPtr()];
77 bool result = glws::makeCurrent(new_drawable, new_context);
79 if (new_drawable && new_context && result) {
80 drawable = new_drawable;
81 context = new_context;
88 static void retrace_wglCopyContext(trace::Call &call) {
91 static void retrace_wglChoosePixelFormat(trace::Call &call) {
94 static void retrace_wglDescribePixelFormat(trace::Call &call) {
97 static void retrace_wglSetPixelFormat(trace::Call &call) {
100 static void retrace_wglSwapBuffers(trace::Call &call) {
101 frame_complete(call);
103 drawable->swapBuffers();
109 static void retrace_wglShareLists(trace::Call &call) {
110 unsigned long long hglrc1 = call.arg(0).toUIntPtr();
111 unsigned long long hglrc2 = call.arg(1).toUIntPtr();
113 glws::Context *share_context = context_map[hglrc1];
114 glws::Context *old_context = context_map[hglrc2];
116 glws::Context *new_context =
117 glws::createContext(old_context->visual, share_context, glretrace::defaultProfile);
119 if (context == old_context) {
120 glws::makeCurrent(drawable, new_context);
123 context_map[hglrc2] = new_context;
129 static void retrace_wglCreateLayerContext(trace::Call &call) {
130 retrace_wglCreateContext(call);
133 static void retrace_wglDescribeLayerPlane(trace::Call &call) {
136 static void retrace_wglSetLayerPaletteEntries(trace::Call &call) {
139 static void retrace_wglRealizeLayerPalette(trace::Call &call) {
142 static void retrace_wglSwapLayerBuffers(trace::Call &call) {
143 retrace_wglSwapBuffers(call);
146 static void retrace_wglUseFontBitmapsA(trace::Call &call) {
149 static void retrace_wglUseFontBitmapsW(trace::Call &call) {
152 static void retrace_wglSwapMultipleBuffers(trace::Call &call) {
155 static void retrace_wglUseFontOutlinesA(trace::Call &call) {
158 static void retrace_wglUseFontOutlinesW(trace::Call &call) {
161 static void retrace_wglCreateBufferRegionARB(trace::Call &call) {
164 static void retrace_wglDeleteBufferRegionARB(trace::Call &call) {
167 static void retrace_wglSaveBufferRegionARB(trace::Call &call) {
170 static void retrace_wglRestoreBufferRegionARB(trace::Call &call) {
173 static void retrace_wglChoosePixelFormatARB(trace::Call &call) {
176 static void retrace_wglMakeContextCurrentARB(trace::Call &call) {
179 static void retrace_wglCreatePbufferARB(trace::Call &call) {
180 int iWidth = call.arg(2).toUInt();
181 int iHeight = call.arg(3).toUInt();
183 unsigned long long orig_pbuffer = call.ret->toUIntPtr();
184 glws::Drawable *drawable = glws::createDrawable(glretrace::visual[glretrace::defaultProfile]);
186 drawable->resize(iWidth, iHeight);
189 pbuffer_map[orig_pbuffer] = drawable;
192 static void retrace_wglGetPbufferDCARB(trace::Call &call) {
193 glws::Drawable *pbuffer = pbuffer_map[call.arg(0).toUIntPtr()];
195 unsigned long long orig_hdc = call.ret->toUIntPtr();
197 drawable_map[orig_hdc] = pbuffer;
200 static void retrace_wglReleasePbufferDCARB(trace::Call &call) {
203 static void retrace_wglDestroyPbufferARB(trace::Call &call) {
206 static void retrace_wglQueryPbufferARB(trace::Call &call) {
209 static void retrace_wglBindTexImageARB(trace::Call &call) {
212 static void retrace_wglReleaseTexImageARB(trace::Call &call) {
215 static void retrace_wglSetPbufferAttribARB(trace::Call &call) {
218 static void retrace_wglCreateContextAttribsARB(trace::Call &call) {
219 unsigned long long orig_context = call.ret->toUIntPtr();
220 glws::Context *share_context = NULL;
222 if (call.arg(1).toPointer()) {
223 share_context = context_map[call.arg(1).toUIntPtr()];
226 glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile);
227 context_map[orig_context] = context;
230 static void retrace_wglMakeContextCurrentEXT(trace::Call &call) {
233 static void retrace_wglChoosePixelFormatEXT(trace::Call &call) {
236 static void retrace_wglSwapIntervalEXT(trace::Call &call) {
239 static void retrace_wglAllocateMemoryNV(trace::Call &call) {
242 static void retrace_wglFreeMemoryNV(trace::Call &call) {
245 static void retrace_glAddSwapHintRectWIN(trace::Call &call) {
248 static void retrace_wglGetProcAddress(trace::Call &call) {
251 const retrace::Entry glretrace::wgl_callbacks[] = {
252 {"glAddSwapHintRectWIN", &retrace_glAddSwapHintRectWIN},
253 {"wglAllocateMemoryNV", &retrace_wglAllocateMemoryNV},
254 {"wglBindTexImageARB", &retrace_wglBindTexImageARB},
255 {"wglChoosePixelFormat", &retrace_wglChoosePixelFormat},
256 {"wglChoosePixelFormatARB", &retrace_wglChoosePixelFormatARB},
257 {"wglChoosePixelFormatEXT", &retrace_wglChoosePixelFormatEXT},
258 {"wglCopyContext", &retrace_wglCopyContext},
259 {"wglCreateBufferRegionARB", &retrace_wglCreateBufferRegionARB},
260 {"wglCreateContext", &retrace_wglCreateContext},
261 {"wglCreateContextAttribsARB", &retrace_wglCreateContextAttribsARB},
262 {"wglCreateLayerContext", &retrace_wglCreateLayerContext},
263 {"wglCreatePbufferARB", &retrace_wglCreatePbufferARB},
264 {"wglDeleteBufferRegionARB", &retrace_wglDeleteBufferRegionARB},
265 {"wglDeleteContext", &retrace_wglDeleteContext},
266 {"wglDescribeLayerPlane", &retrace_wglDescribeLayerPlane},
267 {"wglDescribePixelFormat", &retrace_wglDescribePixelFormat},
268 {"wglDestroyPbufferARB", &retrace_wglDestroyPbufferARB},
269 {"wglFreeMemoryNV", &retrace_wglFreeMemoryNV},
270 {"wglGetCurrentContext", &retrace::ignore},
271 {"wglGetCurrentDC", &retrace::ignore},
272 {"wglGetCurrentReadDCARB", &retrace::ignore},
273 {"wglGetCurrentReadDCEXT", &retrace::ignore},
274 {"wglGetDefaultProcAddress", &retrace::ignore},
275 {"wglGetExtensionsStringARB", &retrace::ignore},
276 {"wglGetExtensionsStringEXT", &retrace::ignore},
277 {"wglGetLayerPaletteEntries", &retrace::ignore},
278 {"wglGetPbufferDCARB", &retrace_wglGetPbufferDCARB},
279 {"wglGetPixelFormat", &retrace::ignore},
280 {"wglGetPixelFormatAttribfvARB", &retrace::ignore},
281 {"wglGetPixelFormatAttribfvEXT", &retrace::ignore},
282 {"wglGetPixelFormatAttribivARB", &retrace::ignore},
283 {"wglGetPixelFormatAttribivEXT", &retrace::ignore},
284 {"wglGetProcAddress", &retrace_wglGetProcAddress},
285 {"wglGetSwapIntervalEXT", &retrace::ignore},
286 {"wglMakeContextCurrentARB", &retrace_wglMakeContextCurrentARB},
287 {"wglMakeContextCurrentEXT", &retrace_wglMakeContextCurrentEXT},
288 {"wglMakeCurrent", &retrace_wglMakeCurrent},
289 {"wglQueryPbufferARB", &retrace_wglQueryPbufferARB},
290 {"wglRealizeLayerPalette", &retrace_wglRealizeLayerPalette},
291 {"wglReleasePbufferDCARB", &retrace_wglReleasePbufferDCARB},
292 {"wglReleaseTexImageARB", &retrace_wglReleaseTexImageARB},
293 {"wglRestoreBufferRegionARB", &retrace_wglRestoreBufferRegionARB},
294 {"wglSaveBufferRegionARB", &retrace_wglSaveBufferRegionARB},
295 {"wglSetLayerPaletteEntries", &retrace_wglSetLayerPaletteEntries},
296 {"wglSetPbufferAttribARB", &retrace_wglSetPbufferAttribARB},
297 {"wglSetPixelFormat", &retrace_wglSetPixelFormat},
298 {"wglShareLists", &retrace_wglShareLists},
299 {"wglSwapBuffers", &retrace_wglSwapBuffers},
300 {"wglSwapIntervalEXT", &retrace_wglSwapIntervalEXT},
301 {"wglSwapLayerBuffers", &retrace_wglSwapLayerBuffers},
302 {"wglSwapMultipleBuffers", &retrace_wglSwapMultipleBuffers},
303 {"wglUseFontBitmapsA", &retrace_wglUseFontBitmapsA},
304 {"wglUseFontBitmapsW", &retrace_wglUseFontBitmapsW},
305 {"wglUseFontOutlinesA", &retrace_wglUseFontOutlinesA},
306 {"wglUseFontOutlinesW", &retrace_wglUseFontOutlinesW},