Less code duplication.
glretrace_wgl.cpp
glretrace_egl.cpp
glretrace_main.cpp
+ glretrace_ws.cpp
glstate.cpp
glstate_images.cpp
glstate_params.cpp
extern bool insideGlBeginEnd;
-extern glws::Profile defaultProfile;
-extern glws::Visual *visual[glws::PROFILE_MAX];
-extern glws::Drawable *drawable;
-extern glws::Context *context;
+
+
+extern glws::Drawable *currentDrawable;
+extern glws::Context *currentContext;
+
+glws::Drawable *
+createDrawable(glws::Profile profile);
+
+glws::Drawable *
+createDrawable(void);
+
+glws::Context *
+createContext(glws::Context *shareContext, glws::Profile profile);
+
+glws::Context *
+createContext(glws::Context *shareContext = 0);
+
+bool
+makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context);
+
void
checkGlError(trace::Call &call);
print ' glretrace::insideGlBeginEnd = false;'
if function.name.startswith('gl') and not function.name.startswith('glX'):
- print r' if (retrace::debug && !glretrace::context) {'
+ print r' if (retrace::debug && !glretrace::currentContext) {'
print r' retrace::warning(call) << "no current context\n";'
print r' }'
DrawableMap::const_iterator it;
it = drawable_map.find(drawable_id);
if (it == drawable_map.end()) {
- return (drawable_map[drawable_id] = glws::createDrawable(visual[glretrace::defaultProfile]));
+ return (drawable_map[drawable_id] = glretrace::createDrawable());
}
return it->second;
it = context_map.find(ctx);
if (it == context_map.end()) {
glws::Context *context;
- context_map[ctx] = context = glws::createContext(visual[glretrace::defaultProfile], sharedContext, glretrace::defaultProfile, retrace::debug);
+ context_map[ctx] = context = glretrace::createContext(sharedContext);
if (!sharedContext) {
sharedContext = context;
}
glws::Drawable *new_drawable = getDrawable(ctx);
glws::Context *new_context = getContext(ctx);
- bool result = glws::makeCurrent(new_drawable, new_context);
-
- if (new_drawable && new_context && result) {
- drawable = new_drawable;
- context = new_context;
- } else {
- drawable = NULL;
- context = NULL;
- }
+ glretrace::makeCurrent(call, new_drawable, new_context);
}
static void retrace_CGLFlushDrawable(trace::Call &call) {
- if (drawable && context) {
+ if (currentDrawable && currentContext) {
if (retrace::doubleBuffer) {
- drawable->swapBuffers();
+ currentDrawable->swapBuffers();
} else {
glFlush();
}
profile = last_profile;
}
- glws::Visual *visual = glretrace::visual[profile];
-
- glws::Drawable *drawable = glws::createDrawable(visual);
+ glws::Drawable *drawable = glretrace::createDrawable(profile);
drawable_map[orig_surface] = drawable;
}
it = drawable_map.find(orig_surface);
if (it != drawable_map.end()) {
- if (it->second != drawable) {
+ if (it->second != currentDrawable) {
// TODO: reference count
delete it->second;
}
}
- glws::Context *context = glws::createContext(glretrace::visual[profile], share_context, profile, retrace::debug);
+ glws::Context *context = glretrace::createContext(share_context, profile);
if (!context) {
const char *name;
switch (profile) {
glws::Drawable *new_drawable = getDrawable(call.arg(1).toUIntPtr());
glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
- if (new_drawable == drawable && new_context == context) {
- return;
- }
-
- if (drawable && context) {
- glFlush();
- if (!retrace::doubleBuffer) {
- frame_complete(call);
- }
- }
-
- bool result = glws::makeCurrent(new_drawable, new_context);
-
- if (new_drawable && new_context && result) {
- drawable = new_drawable;
- context = new_context;
- } else {
- drawable = NULL;
- context = NULL;
- }
+ glretrace::makeCurrent(call, new_drawable, new_context);
}
static void retrace_eglSwapBuffers(trace::Call &call) {
frame_complete(call);
- if (retrace::doubleBuffer && drawable) {
- drawable->swapBuffers();
+ if (retrace::doubleBuffer && currentDrawable) {
+ currentDrawable->swapBuffers();
} else {
glFlush();
}
DrawableMap::const_iterator it;
it = drawable_map.find(drawable_id);
if (it == drawable_map.end()) {
- return (drawable_map[drawable_id] = glws::createDrawable(visual[glretrace::defaultProfile]));
+ return (drawable_map[drawable_id] = glretrace::createDrawable());
}
return it->second;
ContextMap::const_iterator it;
it = context_map.find(context_ptr);
if (it == context_map.end()) {
- return (context_map[context_ptr] = glws::createContext(visual[glretrace::defaultProfile], NULL, glretrace::defaultProfile, retrace::debug));
+ return (context_map[context_ptr] = glretrace::createContext());
}
return it->second;
unsigned long long orig_context = call.ret->toUIntPtr();
glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
- glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+ glws::Context *context = glretrace::createContext(share_context);
context_map[orig_context] = context;
}
unsigned long long orig_context = call.ret->toUIntPtr();
glws::Context *share_context = getContext(call.arg(2).toUIntPtr());
- glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+ glws::Context *context = glretrace::createContext(share_context);
context_map[orig_context] = context;
}
glws::Drawable *new_drawable = getDrawable(call.arg(1).toUInt());
glws::Context *new_context = getContext(call.arg(2).toUIntPtr());
- if (new_drawable == drawable && new_context == context) {
- return;
- }
-
- if (drawable && context) {
- glFlush();
- if (!retrace::doubleBuffer) {
- frame_complete(call);
- }
- }
-
- bool result = glws::makeCurrent(new_drawable, new_context);
-
- if (new_drawable && new_context && result) {
- drawable = new_drawable;
- context = new_context;
- } else {
- drawable = NULL;
- context = NULL;
- }
+ glretrace::makeCurrent(call, new_drawable, new_context);
}
static void retrace_glXSwapBuffers(trace::Call &call) {
frame_complete(call);
if (retrace::doubleBuffer) {
- drawable->swapBuffers();
+ currentDrawable->swapBuffers();
} else {
glFlush();
}
unsigned long long orig_context = call.ret->toUIntPtr();
glws::Context *share_context = getContext(call.arg(3).toUIntPtr());
- glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+ glws::Context *context = glretrace::createContext(share_context);
context_map[orig_context] = context;
}
glws::Drawable *new_drawable = getDrawable(call.arg(1).toUInt());
glws::Context *new_context = getContext(call.arg(3).toUIntPtr());
- if (new_drawable == drawable && new_context == context) {
- return;
- }
-
- if (drawable && context) {
- glFlush();
- if (!retrace::doubleBuffer) {
- frame_complete(call);
- }
- }
-
- bool result = glws::makeCurrent(new_drawable, new_context);
-
- if (new_drawable && new_context && result) {
- drawable = new_drawable;
- context = new_context;
- } else {
- drawable = NULL;
- context = NULL;
- }
+ glretrace::makeCurrent(call, new_drawable, new_context);
}
const retrace::Entry glretrace::glx_callbacks[] = {
namespace glretrace {
bool insideGlBeginEnd = false;
-glws::Profile defaultProfile = glws::PROFILE_COMPAT;
-glws::Visual *visual[glws::PROFILE_MAX];
-glws::Drawable *drawable = NULL;
-glws::Context *context = NULL;
+
void
checkGlError(trace::Call &call) {
os << "\n";
}
-/**
- * Grow the current drawble.
- *
- * We need to infer the drawable size from GL calls because the drawable sizes
- * are specified by OS specific calls which we do not trace.
- */
-void
-updateDrawable(int width, int height) {
- if (!drawable) {
- return;
- }
-
- if (drawable->visible &&
- width <= drawable->width &&
- height <= drawable->height) {
- return;
- }
-
- // Ignore zero area viewports
- if (width == 0 || height == 0) {
- return;
- }
-
- // Check for bound framebuffer last, as this may have a performance impact.
- GLint draw_framebuffer = 0;
- glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
- if (draw_framebuffer != 0) {
- return;
- }
-
- drawable->resize(width, height);
- drawable->show();
-
- glScissor(0, 0, width, height);
-}
-
void frame_complete(trace::Call &call) {
retrace::frameComplete(call);
- if (!drawable) {
+ if (!currentDrawable) {
return;
}
- if (!drawable->visible) {
+ if (!currentDrawable->visible) {
retrace::warning(call) << "could not infer drawable size (glViewport never called)\n";
}
}
void
retrace::setUp(void) {
- if (retrace::coreProfile) {
- glretrace::defaultProfile = glws::PROFILE_CORE;
- }
-
glws::init();
-
- glretrace::visual[glws::PROFILE_COMPAT] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_COMPAT);
- glretrace::visual[glws::PROFILE_CORE] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_CORE);
- glretrace::visual[glws::PROFILE_ES1] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES1);
- glretrace::visual[glws::PROFILE_ES2] = glws::createVisual(retrace::doubleBuffer, glws::PROFILE_ES2);
}
image::Image *
retrace::getSnapshot(void) {
- if (!glretrace::drawable) {
+ if (!glretrace::currentDrawable) {
return NULL;
}
retrace::dumpState(std::ostream &os)
{
if (glretrace::insideGlBeginEnd ||
- !glretrace::drawable ||
- !glretrace::context) {
+ !glretrace::currentDrawable ||
+ !glretrace::currentContext) {
return false;
}
void
retrace::cleanUp(void) {
- for (int n = 0; n < glws::PROFILE_MAX; n++) {
- delete glretrace::visual[n];
- }
-
glws::cleanup();
}
DrawableMap::const_iterator it;
it = drawable_map.find(hdc);
if (it == drawable_map.end()) {
- return (drawable_map[hdc] = glws::createDrawable(visual[glretrace::defaultProfile]));
+ return (drawable_map[hdc] = glretrace::createDrawable());
}
return it->second;
static void retrace_wglCreateContext(trace::Call &call) {
unsigned long long orig_context = call.ret->toUIntPtr();
- glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], NULL, glretrace::defaultProfile, retrace::debug);
+ glws::Context *context = glretrace::createContext();
context_map[orig_context] = context;
}
}
static void retrace_wglMakeCurrent(trace::Call &call) {
- if (drawable && context) {
- glFlush();
- if (!retrace::doubleBuffer) {
- frame_complete(call);
- }
- }
-
glws::Drawable *new_drawable = getDrawable(call.arg(0).toUIntPtr());
glws::Context *new_context = context_map[call.arg(1).toUIntPtr()];
- bool result = glws::makeCurrent(new_drawable, new_context);
-
- if (new_drawable && new_context && result) {
- drawable = new_drawable;
- context = new_context;
- } else {
- drawable = NULL;
- context = NULL;
- }
+ glretrace::makeCurrent(call, new_drawable, new_context);
}
static void retrace_wglCopyContext(trace::Call &call) {
static void retrace_wglSwapBuffers(trace::Call &call) {
frame_complete(call);
if (retrace::doubleBuffer) {
- drawable->swapBuffers();
+ currentDrawable->swapBuffers();
} else {
glFlush();
}
glws::Context *share_context = context_map[hglrc1];
glws::Context *old_context = context_map[hglrc2];
- glws::Context *new_context =
- glws::createContext(old_context->visual, share_context, glretrace::defaultProfile, retrace::debug);
+ glws::Context *new_context = glretrace::createContext(share_context);
if (new_context) {
- if (context == old_context) {
- glws::makeCurrent(drawable, new_context);
+ if (currentContext == old_context) {
+ glretrace::makeCurrent(call, currentDrawable, new_context);
}
context_map[hglrc2] = new_context;
int iHeight = call.arg(3).toUInt();
unsigned long long orig_pbuffer = call.ret->toUIntPtr();
- glws::Drawable *drawable = glws::createDrawable(glretrace::visual[glretrace::defaultProfile]);
+ glws::Drawable *drawable = glretrace::createDrawable();
drawable->resize(iWidth, iHeight);
drawable->show();
share_context = context_map[call.arg(1).toUIntPtr()];
}
- glws::Context *context = glws::createContext(glretrace::visual[glretrace::defaultProfile], share_context, glretrace::defaultProfile, retrace::debug);
+ glws::Context *context = glretrace::createContext(share_context);
context_map[orig_context] = context;
}
--- /dev/null
+/**************************************************************************
+ *
+ * Copyright 2011-2012 Jose Fonseca
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+/**
+ * Window system helpers for glretrace.
+ */
+
+
+#include <string.h>
+
+#include "retrace.hpp"
+#include "glproc.hpp"
+#include "glstate.hpp"
+#include "glretrace.hpp"
+
+
+namespace glretrace {
+
+
+glws::Drawable *currentDrawable = NULL;
+glws::Context *currentContext = NULL;
+
+
+static glws::Visual *
+visuals[glws::PROFILE_MAX];
+
+
+inline glws::Visual *
+getVisual(glws::Profile profile) {
+ glws::Visual * & visual = visuals[profile];
+ if (!visual) {
+ visual = glws::createVisual(retrace::doubleBuffer, profile);
+ }
+ return visual;
+}
+
+
+inline glws::Profile
+getDefaultProfile(void)
+{
+ if (retrace::coreProfile) {
+ return glws::PROFILE_CORE;
+ } else {
+ return glws::PROFILE_COMPAT;
+ }
+}
+
+
+glws::Drawable *
+createDrawable(glws::Profile profile) {
+ glws::Drawable *draw = glws::createDrawable(getVisual(profile));
+ if (!draw) {
+ std::cerr << "error: failed to create OpenGL drawable\n";
+ exit(1);
+ return NULL;
+ }
+
+ return draw;
+}
+
+
+glws::Drawable *
+createDrawable(void) {
+ return glretrace::createDrawable(getDefaultProfile());
+}
+
+
+glws::Context *
+createContext(glws::Context *shareContext, glws::Profile profile) {
+ glws::Context *ctx = glws::createContext(getVisual(profile), shareContext, profile, retrace::debug);
+ if (!ctx) {
+ std::cerr << "error: failed to create OpenGL context\n";
+ exit(1);
+ return NULL;
+ }
+
+ return ctx;
+}
+
+
+glws::Context *
+createContext(glws::Context *shareContext) {
+ return createContext(shareContext, getDefaultProfile());
+}
+
+
+bool
+makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context)
+{
+ if (drawable == currentDrawable && context == currentContext) {
+ return true;
+ }
+
+ if (currentDrawable && currentContext) {
+ glFlush();
+ if (!retrace::doubleBuffer) {
+ frame_complete(call);
+ }
+ }
+
+ bool success = glws::makeCurrent(drawable, context);
+
+ if (!success) {
+ std::cerr << "error: failed to make current OpenGL context and drawable\n";
+ exit(1);
+ return false;
+ }
+
+ if (drawable && context) {
+ currentDrawable = drawable;
+ currentContext = context;
+ } else {
+ currentDrawable = NULL;
+ currentContext = NULL;
+ }
+
+ return true;
+}
+
+
+
+
+/**
+ * Grow the current drawble.
+ *
+ * We need to infer the drawable size from GL calls because the drawable sizes
+ * are specified by OS specific calls which we do not trace.
+ */
+void
+updateDrawable(int width, int height) {
+ if (!currentDrawable) {
+ return;
+ }
+
+ if (!currentDrawable->visible &&
+ width <= currentDrawable->width &&
+ height <= currentDrawable->height) {
+ return;
+ }
+
+ // Ignore zero area viewports
+ if (width == 0 || height == 0) {
+ return;
+ }
+
+ // Check for bound framebuffer last, as this may have a performance impact.
+ GLint draw_framebuffer = 0;
+ glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &draw_framebuffer);
+ if (draw_framebuffer != 0) {
+ return;
+ }
+
+ currentDrawable->resize(width, height);
+ currentDrawable->show();
+
+ glScissor(0, 0, width, height);
+}
+
+
+} /* namespace glretrace */