# HG changeset patch # User Sebastien Jodogne # Date 1570634276 -7200 # Node ID 6a9300ecfa130f926c274d6319a1ac53ebe6af5c # Parent b60d70e8b55cab4df438da501009ea3090c01895 tests for context loss are now restricted to WebAssembly diff -r b60d70e8b55c -r 6a9300ecfa13 Applications/Sdl/SdlOpenGLContext.cpp --- a/Applications/Sdl/SdlOpenGLContext.cpp Wed Oct 09 15:58:39 2019 +0200 +++ b/Applications/Sdl/SdlOpenGLContext.cpp Wed Oct 09 17:17:56 2019 +0200 @@ -42,7 +42,6 @@ bool allowDpiScaling) : window_(title, width, height, true /* enable OpenGL */, allowDpiScaling) , context_(NULL) - , contextLost_(false) { context_ = SDL_GL_CreateContext(window_.GetObject()); @@ -85,43 +84,8 @@ SDL_GL_DeleteContext(context_); } - - bool SdlOpenGLContext::IsContextLost() - { - return contextLost_; - } - - void SdlOpenGLContext::SetLostContext() - { - contextLost_ = true; - } - - void SdlOpenGLContext::RestoreLostContext() - { - contextLost_ = false; - } - - // extern bool Debug_MustContextBeKilled(std::string title); - // extern void Debug_Context_ClearKillFlag(std::string title); - void SdlOpenGLContext::MakeCurrent() { - if (IsContextLost()) - throw OpenGLContextLostException(context_); - - // - // This is used for context loss simulation - // SDL_Window* internalWindow = GetWindow().GetObject(); - // std::string title(SDL_GetWindowTitle(internalWindow)); - - // if (Debug_MustContextBeKilled(title)) - // { - // Debug_Context_ClearKillFlag(title); - // SetLostContext(); - // throw OpenGLContextLostException(context_); - // } - // - if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0) { const char* errText = SDL_GetError(); diff -r b60d70e8b55c -r 6a9300ecfa13 Applications/Sdl/SdlOpenGLContext.h --- a/Applications/Sdl/SdlOpenGLContext.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Applications/Sdl/SdlOpenGLContext.h Wed Oct 09 17:17:56 2019 +0200 @@ -35,7 +35,6 @@ private: SdlWindow window_; SDL_GLContext context_; - bool contextLost_; public: SdlOpenGLContext(const char* title, @@ -50,10 +49,11 @@ return window_; } - virtual bool IsContextLost() ORTHANC_OVERRIDE; - - virtual void SetLostContext() ORTHANC_OVERRIDE; - virtual void RestoreLostContext() ORTHANC_OVERRIDE; + virtual bool IsContextLost() ORTHANC_OVERRIDE + { + // On desktop applications, an OpenGL context should never be lost + return false; + } virtual void MakeCurrent() ORTHANC_OVERRIDE; @@ -62,11 +62,6 @@ virtual unsigned int GetCanvasWidth() const ORTHANC_OVERRIDE; virtual unsigned int GetCanvasHeight() const ORTHANC_OVERRIDE; - - virtual void* DebugGetInternalContext() const ORTHANC_OVERRIDE - { - return context_; - } }; } diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/IOpenGLContext.h --- a/Framework/OpenGL/IOpenGLContext.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/OpenGL/IOpenGLContext.h Wed Oct 09 17:17:56 2019 +0200 @@ -43,13 +43,6 @@ virtual unsigned int GetCanvasWidth() const = 0; virtual unsigned int GetCanvasHeight() const = 0; - - virtual void* DebugGetInternalContext() const = 0; - - // This is for manual context loss (debug purposes) - virtual void SetLostContext() = 0; - virtual void RestoreLostContext() = 0; }; } } - diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/IOpenGLContextLossMonitor.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/OpenGL/IOpenGLContextLossMonitor.h Wed Oct 09 17:17:56 2019 +0200 @@ -0,0 +1,41 @@ +/** + * Stone of Orthanc + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2019 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + **/ + +#pragma once + +namespace OrthancStone +{ + namespace OpenGL + { + class IOpenGLContextLossMonitor : public boost::noncopyable + { + public: + virtual ~IOpenGLContextLossMonitor() + { + } + + virtual void* DebugGetInternalContext() const = 0; + + // This is for manual context loss (debug purposes) + virtual void SetLostContext() = 0; + virtual void RestoreLostContext() = 0; + }; + } +} diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/OpenGLIncludes.h --- a/Framework/OpenGL/OpenGLIncludes.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/OpenGL/OpenGLIncludes.h Wed Oct 09 17:17:56 2019 +0200 @@ -58,7 +58,6 @@ # define ORTHANC_OPENGL_CHECK(name) # define ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT(msg) -# define ORTHANC_CHECK_CURRENT_CONTEXT(context) # else @@ -80,17 +79,6 @@ LOG(TRACE) << msg << " | Current OpenGL context is " << std::hex << ctx; \ } else (void)0 -# define ORTHANC_CHECK_CURRENT_CONTEXT(context) \ -if(true) \ -{ \ - SDL_GLContext actualCtx = SDL_GL_GetCurrentContext(); \ - void* expectedCtx = context.DebugGetInternalContext(); \ - if(expectedCtx != actualCtx) \ - { \ - LOG(ERROR) << "Expected context was " << std::hex << expectedCtx << " while actual context is " << std::hex << actualCtx; \ - } \ -} else (void)0 - # endif #endif diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/OpenGLProgram.cpp --- a/Framework/OpenGL/OpenGLProgram.cpp Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/OpenGL/OpenGLProgram.cpp Wed Oct 09 17:17:56 2019 +0200 @@ -49,7 +49,6 @@ { if (!context_.IsContextLost()) { - ORTHANC_CHECK_CURRENT_CONTEXT(context_); ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteProgram"); assert(program_ != 0); glDeleteProgram(program_); diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/OpenGLTexture.cpp --- a/Framework/OpenGL/OpenGLTexture.cpp Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/OpenGL/OpenGLTexture.cpp Wed Oct 09 17:17:56 2019 +0200 @@ -35,8 +35,6 @@ { if (!context_.IsContextLost()) { - // context is made current externally. Let's check this! - ORTHANC_CHECK_CURRENT_CONTEXT(context_); // Generate a texture object glGenTextures(1, &texture_); if (texture_ == 0) @@ -53,8 +51,6 @@ { if (!context_.IsContextLost()) { - // context is made current externally. Let's check this! - ORTHANC_CHECK_CURRENT_CONTEXT(context_); assert(texture_ != 0); ORTHANC_OPENGL_TRACE_CURRENT_CONTEXT("About to call glDeleteTextures"); glDeleteTextures(1, &texture_); @@ -84,8 +80,6 @@ void OpenGLTexture::Load(const Orthanc::ImageAccessor& image, bool isLinearInterpolation) { - // context is made current externally. Let's check this! - ORTHANC_CHECK_CURRENT_CONTEXT(context_); if (!context_.IsContextLost()) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Disable byte-alignment restriction diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/OpenGL/WebAssemblyOpenGLContext.h --- a/Framework/OpenGL/WebAssemblyOpenGLContext.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/OpenGL/WebAssemblyOpenGLContext.h Wed Oct 09 17:17:56 2019 +0200 @@ -38,6 +38,7 @@ #endif #include "IOpenGLContext.h" +#include "IOpenGLContextLossMonitor.h" #include @@ -47,7 +48,9 @@ { namespace OpenGL { - class WebAssemblyOpenGLContext : public OpenGL::IOpenGLContext + class WebAssemblyOpenGLContext : + public OpenGL::IOpenGLContext, + public OpenGL::IOpenGLContextLossMonitor { private: class PImpl; diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/IViewport.h --- a/Framework/Viewport/IViewport.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/IViewport.h Wed Oct 09 17:17:56 2019 +0200 @@ -46,14 +46,11 @@ virtual unsigned int GetCanvasHeight() const = 0; + // TODO - Is this needed at this level (e.g. for SDL)? virtual const std::string& GetCanvasIdentifier() const = 0; virtual ScenePoint2D GetPixelCenterCoordinates(int x, int y) const = 0; - virtual bool IsContextLost() = 0; - - virtual void* DebugGetInternalContext() const = 0; - #if ORTHANC_ENABLE_LOCALE == 1 virtual void SetFont(size_t index, Orthanc::EmbeddedResources::FileResourceId resource, @@ -62,6 +59,7 @@ #endif protected: + // TODO - Replace this by "ICompositor& GetCompositor()" virtual ICompositor* GetCompositor() = 0; virtual const ICompositor* GetCompositor() const diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/SdlViewport.cpp --- a/Framework/Viewport/SdlViewport.cpp Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/SdlViewport.cpp Wed Oct 09 17:17:56 2019 +0200 @@ -47,107 +47,15 @@ compositor_.reset(new OpenGLCompositor(context_, GetScene())); } - - void* SdlOpenGLViewport::DebugGetInternalContext() const - { - return context_.DebugGetInternalContext(); - } - - bool SdlOpenGLViewport::IsContextLost() { - return context_.IsContextLost(); - } - - bool SdlOpenGLViewport::OpenGLContextLost() - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); - } - - bool SdlOpenGLViewport::OpenGLContextRestored() - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); - } - - void SdlOpenGLViewport::DisableCompositor() - { - compositor_.reset(NULL); - } - - void SdlOpenGLViewport::RestoreCompositor() - { - // the context must have been restored! - ORTHANC_ASSERT(!context_.IsContextLost()); - - if (compositor_.get() == NULL) - { - compositor_.reset(new OpenGLCompositor(context_, GetScene())); - } - else - { - std::string windowTitle(SDL_GetWindowTitle(GetWindow().GetObject())); - LOG(WARNING) << "RestoreCompositor() called for \"" << windowTitle << "\" while it was NOT lost! Nothing done."; - } - } - - // extern bool Debug_MustContextBeRestored(std::string title); - // extern void Debug_Context_ClearRestoreFlag(std::string title); - // extern void Debug_Context_ClearKillFlag(std::string title); - - bool Debug_SdlOpenGLViewport_Refresh_BP = false; - void SdlOpenGLViewport::Refresh() { - // - // try to restore the context if requested - // Debug_Context_ClearRestoreFlag - // Debug_SdlOpenGLViewport_Refresh_BP = true; - // try - // { - // if (Debug_MustContextBeRestored(GetCanvasIdentifier())) - // { - // // to prevent a bug where the context is both labelled as "to be lost" and "to be restored" - // // (occurs when one is hammering away at the keyboard like there's no tomorrow) - // Debug_Context_ClearKillFlag(GetCanvasIdentifier()); - // // this is called manually for loss/restore simulation - // context_.RestoreLostContext(); - // RestoreCompositor(); - // Debug_Context_ClearRestoreFlag(GetCanvasIdentifier()); - // } - // } - // catch (const OpenGLContextLostException& e) - // { - // LOG(ERROR) << "OpenGLContextLostException in SdlOpenGLViewport::Refresh() part 1"; - // } - // Debug_SdlOpenGLViewport_Refresh_BP = false; - // - - try + if (GetCompositor()) { - // the compositor COULD be dead! - if (GetCompositor()) - GetCompositor()->Refresh(); - } - catch (const OpenGLContextLostException& e) - { - // we need to wait for the "context restored" callback - LOG(WARNING) << "Context " << std::hex << e.context_ << " is lost! Compositor will be disabled."; - DisableCompositor(); - - // - // in case this was externally triggered... - //Debug_Context_ClearKillFlag(GetCanvasIdentifier()); - // - } - catch (...) - { - // something else nasty happened - throw; + GetCompositor()->Refresh(); } } - - - SdlCairoViewport::SdlCairoViewport(const char* title, unsigned int width, unsigned int height, @@ -159,16 +67,6 @@ UpdateSdlSurfaceSize(width, height); } - void SdlCairoViewport::DisableCompositor() - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); - } - - void SdlCairoViewport::RestoreCompositor() - { - throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); - } - SdlCairoViewport::~SdlCairoViewport() { if (sdlSurface_) diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/SdlViewport.h --- a/Framework/Viewport/SdlViewport.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/SdlViewport.h Wed Oct 09 17:17:56 2019 +0200 @@ -87,12 +87,6 @@ return context_.GetWindow(); } - virtual void* DebugGetInternalContext() const ORTHANC_OVERRIDE; - - virtual bool IsContextLost() ORTHANC_OVERRIDE; - bool OpenGLContextLost(); - bool OpenGLContextRestored(); - virtual void UpdateSize(unsigned int width, unsigned int height) ORTHANC_OVERRIDE { // nothing to do in OpenGL, the OpenGLCompositor::UpdateSize will be called automatically @@ -100,9 +94,6 @@ virtual void Refresh() ORTHANC_OVERRIDE; protected: - virtual void DisableCompositor() ORTHANC_OVERRIDE; - virtual void RestoreCompositor() ORTHANC_OVERRIDE; - virtual ICompositor* GetCompositor() ORTHANC_OVERRIDE { return compositor_.get(); @@ -136,9 +127,6 @@ return window_; } - virtual void DisableCompositor() ORTHANC_OVERRIDE; - virtual void RestoreCompositor() ORTHANC_OVERRIDE; - virtual void Refresh() ORTHANC_OVERRIDE; virtual void UpdateSize(unsigned int width, diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/ViewportBase.h --- a/Framework/Viewport/ViewportBase.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/ViewportBase.h Wed Oct 09 17:17:56 2019 +0200 @@ -75,8 +75,5 @@ private: std::string identifier_; boost::shared_ptr scene_; - protected: - virtual void RestoreCompositor() = 0; - virtual void DisableCompositor() = 0; }; } diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/WebAssemblyViewport.cpp --- a/Framework/Viewport/WebAssemblyViewport.cpp Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/WebAssemblyViewport.cpp Wed Oct 09 17:17:56 2019 +0200 @@ -122,11 +122,6 @@ throw; } } - - bool WebAssemblyOpenGLViewport::IsContextLost() - { - return context_.IsContextLost(); - } void WebAssemblyOpenGLViewport::RestoreCompositor() { @@ -163,11 +158,6 @@ return false; } - void* WebAssemblyOpenGLViewport::DebugGetInternalContext() const - { - return context_.DebugGetInternalContext(); - } - void WebAssemblyOpenGLViewport::RegisterContextCallbacks() { #if 0 diff -r b60d70e8b55c -r 6a9300ecfa13 Framework/Viewport/WebAssemblyViewport.h --- a/Framework/Viewport/WebAssemblyViewport.h Wed Oct 09 15:58:39 2019 +0200 +++ b/Framework/Viewport/WebAssemblyViewport.h Wed Oct 09 17:17:56 2019 +0200 @@ -65,8 +65,6 @@ virtual void Refresh() ORTHANC_OVERRIDE; - virtual bool IsContextLost() ORTHANC_OVERRIDE; - // this does NOT return whether the context is lost! This is called to // tell Stone that the context has been lost bool OpenGLContextLost(); @@ -74,12 +72,9 @@ // This should be called to indicate that the context has been lost bool OpenGLContextRestored(); - - virtual void* DebugGetInternalContext() const ORTHANC_OVERRIDE; - private: - virtual void DisableCompositor() ORTHANC_OVERRIDE; - virtual void RestoreCompositor() ORTHANC_OVERRIDE; + void DisableCompositor(); + void RestoreCompositor(); void RegisterContextCallbacks(); }; @@ -105,5 +100,4 @@ return &compositor_; } }; - }