changeset 1045:6a9300ecfa13

tests for context loss are now restricted to WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Oct 2019 17:17:56 +0200
parents b60d70e8b55c
children 513a4053c54a
files Applications/Sdl/SdlOpenGLContext.cpp Applications/Sdl/SdlOpenGLContext.h Framework/OpenGL/IOpenGLContext.h Framework/OpenGL/IOpenGLContextLossMonitor.h Framework/OpenGL/OpenGLIncludes.h Framework/OpenGL/OpenGLProgram.cpp Framework/OpenGL/OpenGLTexture.cpp Framework/OpenGL/WebAssemblyOpenGLContext.h Framework/Viewport/IViewport.h Framework/Viewport/SdlViewport.cpp Framework/Viewport/SdlViewport.h Framework/Viewport/ViewportBase.h Framework/Viewport/WebAssemblyViewport.cpp Framework/Viewport/WebAssemblyViewport.h
diffstat 14 files changed, 56 insertions(+), 214 deletions(-) [+]
line wrap: on
line diff
--- 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_);
-
-    // <DEBUG STUFF>
-    // 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_);
-    // }
-    // </DEBUG STUFF>
-
     if (SDL_GL_MakeCurrent(window_.GetObject(), context_) != 0)
     {
       const char* errText = SDL_GetError();
--- 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_;
-    }
   };
 }
 
--- 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;
     };
   }
 }
-
--- /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 <http://www.gnu.org/licenses/>.
+ **/
+
+#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;
+    };
+  }
+}
--- 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
--- 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_);
--- 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
--- 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 <Core/Enumerations.h>
 
@@ -47,7 +48,9 @@
 {
   namespace OpenGL
   {
-    class WebAssemblyOpenGLContext : public OpenGL::IOpenGLContext
+    class WebAssemblyOpenGLContext :
+      public OpenGL::IOpenGLContext,
+      public OpenGL::IOpenGLContextLossMonitor
     {
     private:
       class PImpl;
--- 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
--- 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()
   {
-    // <DEBUG CODE USED FOR CONTEXT LOSS RESTORING>
-    // 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;
-    // </DEBUG CODE USED FOR CONTEXT LOSS RESTORING>
-
-    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();
-
-      // <DEBUG CODE USED FOR CONTEXT LOSS RESTORING>
-      // in case this was externally triggered...
-      //Debug_Context_ClearKillFlag(GetCanvasIdentifier());
-      // </DEBUG CODE USED FOR CONTEXT LOSS RESTORING>
-    }
-    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_)
--- 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,
--- 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<Scene2D>  scene_;
-  protected:
-    virtual void RestoreCompositor() = 0;
-    virtual void DisableCompositor() = 0;
   };
 }
--- 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
--- 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_;
     }
   };
-
 }