diff Framework/Viewport/SdlViewport.h @ 1216:5147277850cf broker

better abstraction for IViewport
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 05 Dec 2019 16:36:40 +0100
parents 9efa66d8d3f8
children 0ca50d275b9a
line wrap: on
line diff
--- a/Framework/Viewport/SdlViewport.h	Wed Dec 04 20:12:15 2019 +0100
+++ b/Framework/Viewport/SdlViewport.h	Thu Dec 05 16:36:40 2019 +0100
@@ -48,43 +48,24 @@
   class SdlViewport : public IViewport
   {
   private:
-    uint32_t  refreshEvent_;
+    boost::mutex                           mutex_;
+    uint32_t                               refreshEvent_;
+    boost::shared_ptr<ViewportController>  controller_;
+    std::auto_ptr<ICompositor>             compositor_;
+
+    void SendRefreshEvent();
 
   protected:
-    void SendRefreshEvent();
-    
-  public:
-    SdlViewport();
-
-    bool IsRefreshEvent(const SDL_Event& event) const
-    {
-      return (event.type == refreshEvent_);
-    }
-
-    virtual void UpdateSize(unsigned int width,
-                            unsigned int height) = 0;
-
-    virtual void ToggleMaximize() = 0;
-  };
-
-
-  class SdlOpenGLViewport : public SdlViewport
-  {
-  private:
-    boost::mutex                     mutex_;
-    SdlOpenGLContext                 context_;
-    std::auto_ptr<OpenGLCompositor>  compositor_;
-
     class SdlLock : public ILock
     {
     private:
-      SdlOpenGLViewport&         that_;
-      boost::mutex::scoped_lock  lock_;
-      
+      SdlViewport&                that_;
+      boost::mutex::scoped_lock   lock_;
+
     public:
-      SdlLock(SdlOpenGLViewport& viewport) :
-        that_(viewport),
-        lock_(viewport.mutex_)
+      SdlLock(SdlViewport& that) :
+      that_(that),
+      lock_(that.mutex_)
       {
       }
 
@@ -93,73 +74,78 @@
         return true;
       }
 
-      virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
+      virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE;
+      
+      virtual ViewportController& GetController() ORTHANC_OVERRIDE
       {
-        return *that_.compositor_;
+        return *that_.controller_;
+      }
+
+      virtual void Invalidate() ORTHANC_OVERRIDE
+      {
+        that_.SendRefreshEvent();
       }
     };
-    
+
+    void ClearCompositor()
+    {
+      compositor_.reset();
+    }
+
+    void AcquireCompositor(ICompositor* compositor /* takes ownership */);
+
   public:
-    SdlOpenGLViewport(const char* title,
-                      unsigned int width,
-                      unsigned int height,
-                      bool allowDpiScaling = true);
+    SdlViewport();
 
-    virtual void Invalidate() ORTHANC_OVERRIDE;
-
-    virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE;
+    bool IsRefreshEvent(const SDL_Event& event) const
+    {
+      return (event.type == refreshEvent_);
+    }
 
     virtual ILock* Lock() ORTHANC_OVERRIDE
     {
       return new SdlLock(*this);
     }
 
-    virtual void UpdateSize(unsigned int width, unsigned int height) ORTHANC_OVERRIDE
-    {
-      // nothing to do in OpenGL, the OpenGLCompositor::UpdateSize will be called automatically
-    }
+    virtual void UpdateSize(unsigned int width,
+                            unsigned int height) = 0;
+
+    virtual void ToggleMaximize() = 0;
+
+    // Must be invoked from the main SDL thread
+    virtual void Paint() = 0;
+  };
+
+
+  class SdlOpenGLViewport : public SdlViewport
+  {
+  private:
+    SdlOpenGLContext  context_;
 
-    virtual void ToggleMaximize() ORTHANC_OVERRIDE
-    {
-      boost::mutex::scoped_lock lock(mutex_);
-      context_.ToggleMaximize();
-    }
+  public:
+    SdlOpenGLViewport(const char* title,
+                      unsigned int width,
+                      unsigned int height,
+                      bool allowDpiScaling = true);
+
+    virtual ~SdlOpenGLViewport();
+
+    virtual void Paint() ORTHANC_OVERRIDE;
+
+    virtual void UpdateSize(unsigned int width, 
+                            unsigned int height) ORTHANC_OVERRIDE;
+
+    virtual void ToggleMaximize() ORTHANC_OVERRIDE;
   };
 
 
   class SdlCairoViewport : public SdlViewport
   {
   private:
-    class SdlLock : public ILock
-    {
-    private:
-      SdlCairoViewport&          that_;
-      boost::mutex::scoped_lock  lock_;
-      
-    public:
-      SdlLock(SdlCairoViewport& viewport) :
-        that_(viewport),
-        lock_(viewport.mutex_)
-      {
-      }
+    SdlWindow     window_;
+    SDL_Surface*  sdlSurface_;
 
-      virtual bool HasCompositor() const ORTHANC_OVERRIDE
-      {
-        return true;
-      }
-
-      virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
-      {
-        return that_.compositor_;
-      }
-    };
- 
-    boost::mutex      mutex_;
-    SdlWindow         window_;
-    CairoCompositor   compositor_;
-    SDL_Surface*      sdlSurface_;
-
-    void CreateSdlSurfaceFromCompositor();
+    void CreateSdlSurfaceFromCompositor(CairoCompositor& compositor);
 
   public:
     SdlCairoViewport(const char* title,
@@ -167,24 +153,13 @@
                      unsigned int height,
                      bool allowDpiScaling = true);
 
-    ~SdlCairoViewport();
-
-    virtual void Invalidate() ORTHANC_OVERRIDE;
+    virtual ~SdlCairoViewport();
 
-    virtual void Paint(const Scene2D& scene) ORTHANC_OVERRIDE;
-
-    virtual ILock* Lock() ORTHANC_OVERRIDE
-    {
-      return new SdlLock(*this);
-    }
+    virtual void Paint() ORTHANC_OVERRIDE;
 
     virtual void UpdateSize(unsigned int width,
                             unsigned int height) ORTHANC_OVERRIDE;
 
-    virtual void ToggleMaximize() ORTHANC_OVERRIDE
-    {
-      boost::mutex::scoped_lock lock(mutex_);
-      window_.ToggleMaximize();
-    }
+    virtual void ToggleMaximize() ORTHANC_OVERRIDE;
   };
 }