From: José Fonseca Date: Thu, 26 Apr 2012 22:18:28 +0000 (+0100) Subject: Cleanup glretrace<->glws integration. X-Git-Url: https://git.cworth.org/git?p=apitrace;a=commitdiff_plain;h=a50b897d1287a43c9ca9a2b5032635a264d8cdfc Cleanup glretrace<->glws integration. Less code duplication. --- diff --git a/retrace/CMakeLists.txt b/retrace/CMakeLists.txt index 6b5e61a..dcf9da4 100644 --- a/retrace/CMakeLists.txt +++ b/retrace/CMakeLists.txt @@ -51,6 +51,7 @@ add_library (glretrace_common glretrace_wgl.cpp glretrace_egl.cpp glretrace_main.cpp + glretrace_ws.cpp glstate.cpp glstate_images.cpp glstate_params.cpp diff --git a/retrace/glretrace.hpp b/retrace/glretrace.hpp index 100d5a8..fea187c 100644 --- a/retrace/glretrace.hpp +++ b/retrace/glretrace.hpp @@ -34,10 +34,26 @@ namespace glretrace { 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); diff --git a/retrace/glretrace.py b/retrace/glretrace.py index c0edb20..b7659ff 100644 --- a/retrace/glretrace.py +++ b/retrace/glretrace.py @@ -250,7 +250,7 @@ class GlRetracer(Retracer): 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' }' diff --git a/retrace/glretrace_cgl.cpp b/retrace/glretrace_cgl.cpp index fccd532..0c91ba2 100644 --- a/retrace/glretrace_cgl.cpp +++ b/retrace/glretrace_cgl.cpp @@ -53,7 +53,7 @@ getDrawable(unsigned long drawable_id) { 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; @@ -70,7 +70,7 @@ getContext(unsigned long long ctx) { 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; } @@ -87,22 +87,14 @@ static void retrace_CGLSetCurrentContext(trace::Call &call) { 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(); } diff --git a/retrace/glretrace_egl.cpp b/retrace/glretrace_egl.cpp index 1f8020a..70fe354 100644 --- a/retrace/glretrace_egl.cpp +++ b/retrace/glretrace_egl.cpp @@ -102,9 +102,7 @@ static void createDrawable(unsigned long long orig_config, unsigned long long or 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; } @@ -128,7 +126,7 @@ static void retrace_eglDestroySurface(trace::Call &call) { 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; } @@ -169,7 +167,7 @@ static void retrace_eglCreateContext(trace::Call &call) { } - 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) { @@ -212,34 +210,15 @@ static void retrace_eglMakeCurrent(trace::Call &call) { 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(); } diff --git a/retrace/glretrace_glx.cpp b/retrace/glretrace_glx.cpp index 20afe51..9d1b5e5 100644 --- a/retrace/glretrace_glx.cpp +++ b/retrace/glretrace_glx.cpp @@ -47,7 +47,7 @@ getDrawable(unsigned long drawable_id) { 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; @@ -62,7 +62,7 @@ getContext(unsigned long long context_ptr) { 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; @@ -72,7 +72,7 @@ static void retrace_glXCreateContext(trace::Call &call) { 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; } @@ -80,7 +80,7 @@ static void retrace_glXCreateContextAttribsARB(trace::Call &call) { 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; } @@ -88,26 +88,7 @@ static void retrace_glXMakeCurrent(trace::Call &call) { 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); } @@ -124,7 +105,7 @@ static void retrace_glXDestroyContext(trace::Call &call) { static void retrace_glXSwapBuffers(trace::Call &call) { frame_complete(call); if (retrace::doubleBuffer) { - drawable->swapBuffers(); + currentDrawable->swapBuffers(); } else { glFlush(); } @@ -134,7 +115,7 @@ static void retrace_glXCreateNewContext(trace::Call &call) { 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; } @@ -142,26 +123,7 @@ static void retrace_glXMakeContextCurrent(trace::Call &call) { 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[] = { diff --git a/retrace/glretrace_main.cpp b/retrace/glretrace_main.cpp index 429208b..c75d368 100644 --- a/retrace/glretrace_main.cpp +++ b/retrace/glretrace_main.cpp @@ -35,10 +35,7 @@ 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) { @@ -85,51 +82,15 @@ 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"; } } @@ -140,16 +101,7 @@ void frame_complete(trace::Call &call) { 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); } @@ -166,7 +118,7 @@ retrace::addCallbacks(retrace::Retracer &retracer) image::Image * retrace::getSnapshot(void) { - if (!glretrace::drawable) { + if (!glretrace::currentDrawable) { return NULL; } @@ -178,8 +130,8 @@ bool retrace::dumpState(std::ostream &os) { if (glretrace::insideGlBeginEnd || - !glretrace::drawable || - !glretrace::context) { + !glretrace::currentDrawable || + !glretrace::currentContext) { return false; } @@ -201,9 +153,5 @@ retrace::waitForInput(void) { void retrace::cleanUp(void) { - for (int n = 0; n < glws::PROFILE_MAX; n++) { - delete glretrace::visual[n]; - } - glws::cleanup(); } diff --git a/retrace/glretrace_wgl.cpp b/retrace/glretrace_wgl.cpp index 5ede7e1..c3e1096 100644 --- a/retrace/glretrace_wgl.cpp +++ b/retrace/glretrace_wgl.cpp @@ -48,7 +48,7 @@ getDrawable(unsigned long long hdc) { 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; @@ -56,7 +56,7 @@ getDrawable(unsigned long long hdc) { 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; } @@ -64,25 +64,10 @@ static void retrace_wglDeleteContext(trace::Call &call) { } 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) { @@ -100,7 +85,7 @@ static void retrace_wglSetPixelFormat(trace::Call &call) { static void retrace_wglSwapBuffers(trace::Call &call) { frame_complete(call); if (retrace::doubleBuffer) { - drawable->swapBuffers(); + currentDrawable->swapBuffers(); } else { glFlush(); } @@ -113,11 +98,10 @@ static void retrace_wglShareLists(trace::Call &call) { 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; @@ -181,7 +165,7 @@ static void retrace_wglCreatePbufferARB(trace::Call &call) { 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(); @@ -223,7 +207,7 @@ static void retrace_wglCreateContextAttribsARB(trace::Call &call) { 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; } diff --git a/retrace/glretrace_ws.cpp b/retrace/glretrace_ws.cpp new file mode 100644 index 0000000..47b1369 --- /dev/null +++ b/retrace/glretrace_ws.cpp @@ -0,0 +1,183 @@ +/************************************************************************** + * + * 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 + +#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 */