diff Framework/Viewport/WebAssemblyViewport.cpp @ 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 3c9529edf5fd
children a4bb8c2dd211 2d8ab34c8c91
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();