#include <string.h>
+#include "os_thread.hpp"
#include "retrace.hpp"
#include "glproc.hpp"
#include "glstate.hpp"
namespace glretrace {
-glws::Drawable *currentDrawable = NULL;
-glws::Context *currentContext = NULL;
-
-
static glws::Visual *
visuals[glws::PROFILE_MAX];
glws::Visual * & visual = visuals[profile];
if (!visual) {
visual = glws::createVisual(retrace::doubleBuffer, profile);
+ if (!visual) {
+ std::cerr << "error: failed to create OpenGL visual\n";
+ exit(1);
+ }
}
return visual;
}
}
-glws::Drawable *
-createDrawable(glws::Profile profile) {
- glws::Drawable *draw = glws::createDrawable(getVisual(profile));
+static glws::Drawable *
+createDrawableHelper(glws::Profile profile, int width = 32, int height = 32, bool pbuffer = false) {
+ glws::Visual *visual = getVisual(profile);
+ glws::Drawable *draw = glws::createDrawable(visual, width, height, pbuffer);
if (!draw) {
std::cerr << "error: failed to create OpenGL drawable\n";
exit(1);
- return NULL;
}
return draw;
}
+glws::Drawable *
+createDrawable(glws::Profile profile) {
+ return createDrawableHelper(profile);
+}
+
+
glws::Drawable *
createDrawable(void) {
- return glretrace::createDrawable(getDefaultProfile());
+ return createDrawable(getDefaultProfile());
+}
+
+
+glws::Drawable *
+createPbuffer(int width, int height) {
+ return createDrawableHelper(getDefaultProfile(), width, height, true);
}
-glws::Context *
-createContext(glws::Context *shareContext, glws::Profile profile) {
- glws::Context *ctx = glws::createContext(getVisual(profile), shareContext, profile, retrace::debug);
+Context *
+createContext(Context *shareContext, glws::Profile profile) {
+ glws::Visual *visual = getVisual(profile);
+ glws::Context *shareWsContext = shareContext ? shareContext->wsContext : NULL;
+ glws::Context *ctx = glws::createContext(visual, shareWsContext, profile, retrace::debug);
if (!ctx) {
std::cerr << "error: failed to create OpenGL context\n";
exit(1);
return NULL;
}
- return ctx;
+ return new Context(ctx);
}
-glws::Context *
-createContext(glws::Context *shareContext) {
+Context *
+createContext(Context *shareContext) {
return createContext(shareContext, getDefaultProfile());
}
+Context::~Context()
+{
+ //assert(this != getCurrentContext());
+ if (this != getCurrentContext()) {
+ delete wsContext;
+ }
+}
+
+
+static thread_specific Context *
+currentContextPtr;
+
+
bool
-makeCurrent(trace::Call &call, glws::Drawable *drawable, glws::Context *context)
+makeCurrent(trace::Call &call, glws::Drawable *drawable, Context *context)
{
+ Context *currentContext = currentContextPtr;
+ glws::Drawable *currentDrawable = currentContext ? currentContext->drawable : NULL;
+
if (drawable == currentDrawable && context == currentContext) {
return true;
}
- if (currentDrawable && currentContext) {
+ if (currentContext) {
glFlush();
if (!retrace::doubleBuffer) {
frame_complete(call);
}
}
- bool success = glws::makeCurrent(drawable, context);
+ flushQueries();
+
+ bool success = glws::makeCurrent(drawable, context ? context->wsContext : NULL);
if (!success) {
std::cerr << "error: failed to make current OpenGL context and drawable\n";
return false;
}
+ currentContextPtr = context;
+
if (drawable && context) {
- currentDrawable = drawable;
- currentContext = context;
- } else {
- currentDrawable = NULL;
- currentContext = NULL;
+ context->drawable = drawable;
+
+ if (!context->used) {
+ initContext();
+ context->used = true;
+ }
}
return true;
}
+Context *
+getCurrentContext(void) {
+ return currentContextPtr;
+}
/**
*/
void
updateDrawable(int width, int height) {
- if (!currentDrawable) {
+ Context *currentContext = getCurrentContext();
+ if (!currentContext) {
+ return;
+ }
+
+ glws::Drawable *currentDrawable = currentContext->drawable;
+ assert(currentDrawable);
+
+ if (currentDrawable->pbuffer) {
return;
}
- if (!currentDrawable->visible &&
+ if (currentDrawable->visible &&
width <= currentDrawable->width &&
height <= currentDrawable->height) {
return;
}
+int
+parseAttrib(const trace::Value *attribs, int param, int default_ = 0) {
+ const trace::Array *attribs_ = dynamic_cast<const trace::Array *>(attribs);
+
+ if (attribs_) {
+ for (size_t i = 0; i + 1 < attribs_->values.size(); i += 2) {
+ int param_i = attribs_->values[i]->toSInt();
+ if (param_i == 0) {
+ break;
+ }
+
+ if (param_i == param) {
+ int value = attribs_->values[i + 1]->toSInt();
+ return value;
+ }
+ }
+ }
+
+ return default_;
+}
+
+
} /* namespace glretrace */