changeset 46:766d31dc5716 wasm

removing threads for wasm
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Apr 2017 14:33:06 +0200
parents ecd96e563929
children 25befef48c35
files Framework/Viewport/IViewport.h Framework/Viewport/WidgetViewport.cpp Framework/Viewport/WidgetViewport.h Framework/Widgets/EmptyWidget.cpp Framework/Widgets/EmptyWidget.h Framework/Widgets/IWidget.h Framework/Widgets/LayeredSceneWidget.cpp Framework/Widgets/LayeredSceneWidget.h Framework/Widgets/LayoutWidget.cpp Framework/Widgets/LayoutWidget.h Framework/Widgets/TestCairoWidget.cpp Framework/Widgets/TestCairoWidget.h Framework/Widgets/TestWorldSceneWidget.cpp Framework/Widgets/TestWorldSceneWidget.h Framework/Widgets/WidgetBase.cpp Framework/Widgets/WidgetBase.h Framework/Widgets/WorldSceneWidget.cpp Framework/Widgets/WorldSceneWidget.h
diffstat 18 files changed, 165 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Viewport/IViewport.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Viewport/IViewport.h	Wed Apr 19 14:33:06 2017 +0200
@@ -94,5 +94,9 @@
 
     virtual void KeyPressed(char key,
                             KeyboardModifiers modifiers) = 0;
+
+    virtual bool HasUpdateContent() = 0;
+
+    virtual void UpdateContent() = 0;
   };
 }
--- a/Framework/Viewport/WidgetViewport.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Viewport/WidgetViewport.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -178,18 +178,23 @@
     {
       return false;
     }
+    
+    Orthanc::ImageAccessor background = background_.GetAccessor();
 
-    if (backgroundChanged_)
+    if (backgroundChanged_ &&
+        !centralWidget_->Render(background))
     {
-      Orthanc::ImageAccessor accessor = background_.GetAccessor();
-      if (!centralWidget_->Render(accessor))
-      {
-        return false;
-      }
+      return false;
     }
 
-    Orthanc::ImageProcessing::Copy(surface, background_.GetAccessor());
+    if (background.GetWidth() != surface.GetWidth() ||
+        background.GetHeight() != surface.GetHeight())
+    {
+      return false;
+    }
 
+    Orthanc::ImageProcessing::Convert(surface, background);
+    
     if (mouseTracker_.get() != NULL)
     {
       mouseTracker_->Render(surface);
@@ -284,6 +289,14 @@
   {
     boost::mutex::scoped_lock lock(mutex_);
     isMouseOver_ = false;
+
+    if (started_ &&
+        mouseTracker_.get() != NULL)
+    {
+      mouseTracker_->MouseUp();
+      mouseTracker_.reset(NULL);
+    }
+
     observers_.NotifyChange(this);
   }
 
@@ -319,4 +332,30 @@
       centralWidget_->KeyPressed(key, modifiers);
     }
   }
+
+
+  bool WidgetViewport::HasUpdateContent()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    if (centralWidget_.get() != NULL)
+    {
+      return centralWidget_->HasUpdateContent();
+    }
+    else
+    {
+      return false;
+    }
+  }
+   
+
+  void WidgetViewport::UpdateContent()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    if (centralWidget_.get() != NULL)
+    {
+      centralWidget_->UpdateContent();
+    }
+  }
 }
--- a/Framework/Viewport/WidgetViewport.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Viewport/WidgetViewport.h	Wed Apr 19 14:33:06 2017 +0200
@@ -114,5 +114,9 @@
 
     virtual void KeyPressed(char key,
                             KeyboardModifiers modifiers);
+
+    virtual bool HasUpdateContent();
+
+    virtual void UpdateContent();
   };
 }
--- a/Framework/Widgets/EmptyWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/EmptyWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -43,4 +43,10 @@
     Orthanc::ImageProcessing::Set(surface, red_, green_, blue_, 255);
     return true;
   }
+
+  
+  void EmptyWidget::UpdateContent()
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+  }
 }
--- a/Framework/Widgets/EmptyWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/EmptyWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -114,5 +114,12 @@
                             KeyboardModifiers modifiers)
     {
     }
+
+    virtual bool HasUpdateContent() const
+    {
+      return false;
+    }
+
+    virtual void UpdateContent();
   };
 }
--- a/Framework/Widgets/IWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/IWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -85,5 +85,9 @@
 
     virtual void KeyPressed(char key,
                             KeyboardModifiers modifiers) = 0;
+
+    virtual bool HasUpdateContent() const = 0;
+
+    virtual void UpdateContent() = 0;
   };
 }
--- a/Framework/Widgets/LayeredSceneWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/LayeredSceneWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -369,7 +369,7 @@
   }
 
 
-  void LayeredSceneWidget::UpdateStep()
+  void LayeredSceneWidget::UpdateContent()
   {
     size_t layer = 0;
     bool isLast = true;
--- a/Framework/Widgets/LayeredSceneWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/LayeredSceneWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -72,13 +72,6 @@
     Observers                     observers_;
 
   protected:
-    virtual bool HasUpdateThread() const
-    {
-      return true;
-    }
-
-    virtual void UpdateStep();
-
     virtual bool RenderScene(CairoContext& context,
                              const ViewportGeometry& view);
 
@@ -132,5 +125,12 @@
     {
       observers_.Unregister(observer);
     }
+
+    virtual bool HasUpdateContent() const
+    {
+      return true;
+    }
+
+    virtual void UpdateContent();
   };
 }
--- a/Framework/Widgets/LayoutWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/LayoutWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -264,12 +264,6 @@
   }
 
 
-  void LayoutWidget::UpdateStep()
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
-  }
-
-
   LayoutWidget::LayoutWidget() :
     isHorizontal_(true),
     started_(false),
--- a/Framework/Widgets/LayoutWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/LayoutWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -60,15 +60,6 @@
 
     void ComputeChildrenExtents();
 
-  protected:
-    virtual bool HasUpdateThread() const 
-    {
-      return false;
-    }
-
-    virtual void UpdateStep();
-
-
   public:
     LayoutWidget();
 
--- a/Framework/Widgets/TestCairoWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/TestCairoWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -33,8 +33,6 @@
 
 #include "TestCairoWidget.h"
 
-#include "../../Resources/Orthanc/Core/SystemToolbox.h"
-
 #include <stdio.h>
 
 
@@ -42,7 +40,7 @@
 {
   namespace Samples
   {
-    void TestCairoWidget::UpdateStep() 
+    void TestCairoWidget::UpdateContent() 
     {
       value_ -= 0.01f;
       if (value_ < 0)
@@ -51,8 +49,6 @@
       }
 
       NotifyChange();
-
-      Orthanc::SystemToolbox::USleep(25000);
     }
 
 
--- a/Framework/Widgets/TestCairoWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/TestCairoWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -47,13 +47,6 @@
       float         value_;
       bool          animate_;
 
-      virtual bool HasUpdateThread() const
-      {
-        return animate_;
-      }
-
-      virtual void UpdateStep();
-
     protected:
       virtual bool RenderCairo(CairoContext& context);
 
@@ -79,6 +72,13 @@
     
       virtual void KeyPressed(char key,
                               KeyboardModifiers modifiers);
+
+      virtual bool HasUpdateContent() const
+      {
+        return animate_;
+      }
+      
+      virtual void UpdateContent();
     };
   }
 }
--- a/Framework/Widgets/TestWorldSceneWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/TestWorldSceneWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -106,7 +106,7 @@
 
 
     bool TestWorldSceneWidget::RenderScene(CairoContext& context,
-                                           const ViewportGeometry& view)
+                                           const ViewportGeometry& view) 
     {
       cairo_t* cr = context.GetObject();
 
@@ -114,7 +114,8 @@
       cairo_set_source_rgb(cr, 0, 0, 0);
       cairo_paint(cr);
 
-      cairo_set_source_rgb(cr, 0, 1, 0);
+      float color = static_cast<float>(count_ % 16) / 15.0f;
+      cairo_set_source_rgb(cr, 0, 1.0f - color, color);
       cairo_rectangle(cr, -10, -.5, 20, 1);
       cairo_fill(cr);
 
@@ -122,8 +123,10 @@
     }
 
 
-    TestWorldSceneWidget::TestWorldSceneWidget() :
-      interactor_(new Interactor)
+    TestWorldSceneWidget::TestWorldSceneWidget(bool animate) :
+      interactor_(new Interactor),
+      animate_(animate),
+      count_(0)
     {
       SetInteractor(*interactor_);
     }
@@ -139,5 +142,19 @@
       y1 = -.5;
       y2 = .5;
     }
+
+
+    void TestWorldSceneWidget::UpdateContent()
+    {
+      if (animate_)
+      {
+        count_++;
+        NotifyChange();
+      }
+      else
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+      }
+    }
   }
 }
--- a/Framework/Widgets/TestWorldSceneWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/TestWorldSceneWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -45,23 +45,27 @@
       class Interactor;
 
       std::auto_ptr<Interactor>   interactor_;
+      bool                        animate_;
+      unsigned int                count_;
 
     protected:
-      virtual SliceGeometry GetSlice()
-      {
-        return SliceGeometry();
-      }
-
       virtual bool RenderScene(CairoContext& context,
                                const ViewportGeometry& view);
 
     public:
-      TestWorldSceneWidget();
+      TestWorldSceneWidget(bool animate);
 
       virtual void GetSceneExtent(double& x1,
                                   double& y1,
                                   double& x2,
                                   double& y2);
+
+      virtual bool HasUpdateContent() const
+      {
+        return animate_;
+      }
+
+      virtual void UpdateContent();
     };
   }
 }
--- a/Framework/Widgets/WidgetBase.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/WidgetBase.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -92,15 +92,6 @@
   }
 
 
-  void WidgetBase::WorkerThread(WidgetBase* that)
-  {
-    while (that->started_)
-    {
-      that->UpdateStep();
-    }
-  }
-
-
   WidgetBase::WidgetBase() :
     statusBar_(NULL),
     started_(false),
@@ -133,12 +124,22 @@
 
   void WidgetBase::Register(IChangeObserver& observer)
   {
+    if (started_)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+
     observers_.Register(observer);
   }
 
 
   void WidgetBase::Unregister(IChangeObserver& observer)
   {
+    if (started_)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+
     observers_.Unregister(observer);
   }
 
@@ -147,39 +148,35 @@
   {
     if (started_)
     {
-      LOG(ERROR) << "Cannot Start() twice";
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
     }
-
-    started_ = true;
-
-    if (HasUpdateThread())
+    else
     {
-      thread_ = boost::thread(WorkerThread, this);
+      started_ = true;
     }
   }
 
+  
+  void WidgetBase::Stop()
+  {
+    if (started_)
+    {
+      started_ = false;
+    }
+    else
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+  }
 
-  void WidgetBase::Stop()
+  
+  bool WidgetBase::Render(Orthanc::ImageAccessor& surface)
   {
     if (!started_)
     {
-      LOG(ERROR) << "Cannot Stop() if Start() has not been invoked";
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
     }
-
-    started_ = false;
-
-    if (HasUpdateThread() &&
-        thread_.joinable())
-    {
-      thread_.join();
-    }
-  }
-
-
-  bool WidgetBase::Render(Orthanc::ImageAccessor& surface)
-  {
+    
 #if 0
     ClearBackgroundOrthanc(surface);
 #else
@@ -188,4 +185,10 @@
 
     return true;
   }
+
+  
+  void WidgetBase::UpdateContent()
+  {
+    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+  }
 }
--- a/Framework/Widgets/WidgetBase.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/WidgetBase.h	Wed Apr 19 14:33:06 2017 +0200
@@ -38,8 +38,6 @@
 #include "../Viewport/CairoContext.h"
 #include "../Toolbox/ObserversRegistry.h"
 
-#include <boost/thread.hpp>
-
 namespace OrthancStone
 {
   class WidgetBase : public IWidget
@@ -48,7 +46,6 @@
     IStatusBar*                  statusBar_;
     ObserversRegistry<IWidget>   observers_;
     bool                         started_;
-    boost::thread                thread_;
     bool                         backgroundCleared_;
     uint8_t                      backgroundColor_[3];
 
@@ -63,25 +60,19 @@
 
     void UpdateStatusBar(const std::string& message);
 
-    static void WorkerThread(WidgetBase* that);
-
     IStatusBar* GetStatusBar() const
     {
       return statusBar_;
     }
 
-    virtual bool HasUpdateThread() const = 0;
-
-    virtual void UpdateStep() = 0;
-
-  public:
-    WidgetBase();
-
     bool IsStarted() const
     {
       return started_;
     }
 
+  public:
+    WidgetBase();
+
     void SetBackgroundCleared(bool clear)
     {
       backgroundCleared_ = clear;
@@ -119,5 +110,12 @@
     virtual void Stop();
 
     virtual bool Render(Orthanc::ImageAccessor& surface);
+
+    virtual bool HasUpdateContent() const
+    {
+      return false;
+    }
+
+    virtual void UpdateContent();
   };
 }
--- a/Framework/Widgets/WorldSceneWidget.cpp	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/WorldSceneWidget.cpp	Wed Apr 19 14:33:06 2017 +0200
@@ -248,12 +248,6 @@
   };
 
 
-  void WorldSceneWidget::UpdateStep()
-  {
-    throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
-  }
-
-
   bool WorldSceneWidget::RenderCairo(CairoContext& context)
   {
     ViewportGeometry view;
--- a/Framework/Widgets/WorldSceneWidget.h	Wed Apr 19 10:28:08 2017 +0200
+++ b/Framework/Widgets/WorldSceneWidget.h	Wed Apr 19 14:33:06 2017 +0200
@@ -78,13 +78,6 @@
     virtual bool RenderScene(CairoContext& context,
                              const ViewportGeometry& view) = 0;
 
-    virtual bool HasUpdateThread() const
-    {
-      return false;
-    }
-
-    virtual void UpdateStep();
-
     virtual bool RenderCairo(CairoContext& context);
 
     virtual void RenderMouseOverCairo(CairoContext& context,
@@ -112,7 +105,10 @@
       observers_.Unregister(observer);
     }
 
-    virtual SliceGeometry GetSlice() = 0;
+    virtual SliceGeometry GetSlice()
+    {
+      return SliceGeometry();
+    }
 
     virtual void GetSceneExtent(double& x1,
                                 double& y1,