diff Framework/Viewport/WebGLViewportsRegistry.cpp @ 1331:ab81ee8fce1f broker

- Viewport is not passed and stored as a shared_ptr instead of raw reference. - ViewportController can now be injected with an undo stack (not a ctor param anymore, as a preparation for the move of the undo stack to an interactor) - Added (temp) flag to disable emscripten events registration in the WebAssemblyViewport (because legacy client code deals with them directly) - Added emscripten_clear_timeout in ~WebGLViewportsRegistry - Removed GenericToolbox::HoldingRef whose responsibility is better served with proper callback un-registration.
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 30 Mar 2020 14:23:46 +0200
parents c38c89684d83
children 30deba7bc8e2
line wrap: on
line diff
--- a/Framework/Viewport/WebGLViewportsRegistry.cpp	Mon Mar 30 08:47:30 2020 +0200
+++ b/Framework/Viewport/WebGLViewportsRegistry.cpp	Mon Mar 30 14:23:46 2020 +0200
@@ -21,6 +21,8 @@
 
 #include "WebGLViewportsRegistry.h"
 
+#include "../Toolbox/GenericToolbox.h"
+
 #include <Core/OrthancException.h>
 
 #include <boost/make_shared.hpp>
@@ -29,13 +31,17 @@
 {
   void WebGLViewportsRegistry::LaunchTimer()
   {
-    emscripten_set_timeout(OnTimeoutCallback, timeoutMS_, this);
+    timeOutID_ = emscripten_set_timeout(
+      OnTimeoutCallback, 
+      timeoutMS_, 
+      reinterpret_cast<void*>(this));
   }
-
   
   void WebGLViewportsRegistry::OnTimeout()
   {
-    for (Viewports::iterator it = viewports_.begin(); it != viewports_.end(); ++it)
+    for (Viewports::iterator it = viewports_.begin();
+         it != viewports_.end(); 
+         ++it)
     {
       if (it->second == NULL ||
           it->second->IsContextLost())
@@ -61,10 +67,20 @@
         // replaced by a fresh one with the same ID: Recreate the
         // WebGL context on the new canvas
         boost::shared_ptr<WebGLViewport> viewport;
-          
+
+        // we need to steal the properties from the old viewport
+        // and set them to the new viewport
         {
           std::unique_ptr<IViewport::ILock> lock(it->second->Lock());
-          viewport = boost::make_shared<WebGLViewport>(it->first, lock->GetController().GetScene());
+
+          // TODO: remove ViewportController
+          Scene2D* scene = lock->GetController().ReleaseScene();
+          viewport = WebGLViewport::Create(it->first);
+
+          {
+            std::unique_ptr<IViewport::ILock> newLock(viewport->Lock());
+            newLock->GetController().AcquireScene(scene);
+          }
         }
 
         // Replace the old WebGL viewport by the new one
@@ -81,16 +97,17 @@
     LaunchTimer();
   }
 
-    
   void WebGLViewportsRegistry::OnTimeoutCallback(void *userData)
   {
-    WebGLViewportsRegistry& that = *reinterpret_cast<WebGLViewportsRegistry*>(userData);
-    that.OnTimeout();
+    // This object dies with the process or tab. 
+    WebGLViewportsRegistry* that = 
+      reinterpret_cast<WebGLViewportsRegistry*>(userData);
+    that->OnTimeout();
   }
 
-    
   WebGLViewportsRegistry::WebGLViewportsRegistry(double timeoutMS) :
-    timeoutMS_(timeoutMS)
+    timeoutMS_(timeoutMS),
+    timeOutID_(0)
   {
     if (timeoutMS <= 0)
     {
@@ -100,8 +117,14 @@
     LaunchTimer();
   }
 
+  WebGLViewportsRegistry::~WebGLViewportsRegistry()
+  {
+    emscripten_clear_timeout(timeOutID_);
+    Clear();
+  }
 
-  boost::shared_ptr<WebGLViewport> WebGLViewportsRegistry::Add(const std::string& canvasId)
+  boost::shared_ptr<WebGLViewport> WebGLViewportsRegistry::Add(
+    const std::string& canvasId)
   {
     if (viewports_.find(canvasId) != viewports_.end())
     {
@@ -110,13 +133,13 @@
     }
     else
     {
-      boost::shared_ptr<WebGLViewport> viewport(new WebGLViewport(canvasId));
+      boost::shared_ptr<WebGLViewport> viewport = 
+        WebGLViewport::Create(canvasId);
       viewports_[canvasId] = viewport;
       return viewport;
     }
   }
-
-    
+      
   void WebGLViewportsRegistry::Remove(const std::string& canvasId)
   {
     Viewports::iterator found = viewports_.find(canvasId);
@@ -130,14 +153,12 @@
       viewports_.erase(found);
     }
   }
-
-    
+  
   void WebGLViewportsRegistry::Clear()
   {
     viewports_.clear();
   }
 
-
   WebGLViewportsRegistry::Accessor::Accessor(WebGLViewportsRegistry& that,
                                              const std::string& canvasId) :
     that_(that)
@@ -150,7 +171,6 @@
     }
   }
 
-
   IViewport::ILock& WebGLViewportsRegistry::Accessor::GetViewport() const
   {
     if (IsValid())