changeset 1239:ce3052f28f2e toa2019122001

Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 20 Dec 2019 15:44:20 +0100
parents e257b91fae2c
children 8f6c2f9d0fe2
files Framework/Viewport/WebAssemblyViewport.cpp Framework/Viewport/WebAssemblyViewport.h
diffstat 2 files changed, 81 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Viewport/WebAssemblyViewport.cpp	Thu Dec 12 14:30:33 2019 +0100
+++ b/Framework/Viewport/WebAssemblyViewport.cpp	Fri Dec 20 15:44:20 2019 +0100
@@ -27,18 +27,26 @@
 
 namespace OrthancStone
 {
-  WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) :
-    WebAssemblyViewport(canvas),
-    context_(canvas)
+  WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) 
+    : WebAssemblyViewport(canvas)
+    , context_(canvas)
+    , cssWidth_(0)    // will be set in Refresh()
+    , cssHeight_(0)   // ditto
+    , pixelWidth_(0)  // ditto
+    , pixelHeight_(0) // ditto
   {
     compositor_.reset(new OpenGLCompositor(context_, GetScene()));
     RegisterContextCallbacks();
   }
 
   WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas,
-    boost::shared_ptr<Scene2D>& scene) :
-    WebAssemblyViewport(canvas, scene),
-    context_(canvas)
+    boost::shared_ptr<Scene2D>& scene) 
+    : WebAssemblyViewport(canvas, scene)
+    , context_(canvas)
+    , cssWidth_(0)    // will be set in Refresh()
+    , cssHeight_(0)   // ditto
+    , pixelWidth_(0)  // ditto
+    , pixelHeight_(0) // ditto
   {
     compositor_.reset(new OpenGLCompositor(context_, GetScene()));
     RegisterContextCallbacks();
@@ -101,10 +109,65 @@
     }
   }
 
+  void WebAssemblyOpenGLViewport::UpdateSizeIfNeeded()
+  {
+    bool needsRefresh = false;
+    std::string canvasId = GetCanvasIdentifier();
+
+    {
+      double cssWidth = 0;
+      double cssHeight = 0;
+      EMSCRIPTEN_RESULT res = EMSCRIPTEN_RESULT_SUCCESS;
+      res =
+        emscripten_get_element_css_size(canvasId.c_str(), &cssWidth, &cssHeight);
+
+      if (res == EMSCRIPTEN_RESULT_SUCCESS)
+      {
+        if ((cssWidth != cssWidth_) || (cssHeight != cssHeight_))
+        {
+          cssWidth_ = cssWidth;
+          cssHeight_ = cssHeight;
+          needsRefresh = true;
+        }
+      }
+    }
+
+    {
+      int pixelWidth = 0;
+      int pixelHeight = 0;
+      EMSCRIPTEN_RESULT res = EMSCRIPTEN_RESULT_SUCCESS;
+      res =
+        emscripten_get_canvas_element_size(canvasId.c_str(), &pixelWidth, &pixelHeight);
+
+      if (res == EMSCRIPTEN_RESULT_SUCCESS)
+      {
+        if ((pixelWidth != pixelWidth_) || (pixelHeight != pixelHeight_))
+        {
+          pixelWidth_ = pixelWidth;
+          pixelHeight_ = pixelHeight;
+          needsRefresh = true;
+        }
+      }
+    }
+
+    if (needsRefresh)
+      UpdateSize();
+  }
+
   void WebAssemblyOpenGLViewport::Refresh()
   {
     try
     {
+      // first, we check if the canvas size (both css size in css pixels and
+      // backing store) have changed. if so, we call UpdateSize to deal with
+      // it
+
+      LOG(INFO) << "updating cairo viewport size";
+
+      // maybe the canvas size has changed and we need to update the 
+      // canvas backing store size
+      UpdateSizeIfNeeded();
+           
       if (HasCompositor())
       {
         GetCompositor().Refresh();
--- a/Framework/Viewport/WebAssemblyViewport.h	Thu Dec 12 14:30:33 2019 +0100
+++ b/Framework/Viewport/WebAssemblyViewport.h	Fri Dec 20 15:44:20 2019 +0100
@@ -34,15 +34,15 @@
     std::string  canvasIdentifier_;
 
   public:
-    WebAssemblyViewport(const std::string& canvasIdentifier) :
-      canvasIdentifier_(canvasIdentifier)
+    WebAssemblyViewport(const std::string& canvasIdentifier) 
+      : canvasIdentifier_(canvasIdentifier)
     {
     }
 
     WebAssemblyViewport(const std::string& canvasIdentifier,
-                        boost::shared_ptr<Scene2D>& scene) :
-      ViewportBase(scene),
-      canvasIdentifier_(canvasIdentifier)
+                        boost::shared_ptr<Scene2D>& scene) 
+      : ViewportBase(scene)
+      , canvasIdentifier_(canvasIdentifier)
     {
     }
 
@@ -58,6 +58,13 @@
   private:
     OpenGL::WebAssemblyOpenGLContext  context_;
     std::auto_ptr<OpenGLCompositor>   compositor_;
+    double                            cssWidth_;
+    double                            cssHeight_;
+    int                               pixelWidth_;
+    int                               pixelHeight_;
+
+  private:
+    void UpdateSizeIfNeeded();
 
   public:
     WebAssemblyOpenGLViewport(const std::string& canvas);