changeset 910:a6c12fe88bcb

wip: WebAssemblyCairoViewport: still need to implement blit to canvas
author Alain Mazy <alain@mazy.be>
date Thu, 18 Jul 2019 10:39:00 +0200
parents 7a7e4e1f558f
children 64e5f3ff6360 2b4b6b86520a
files Framework/Viewport/WebAssemblyViewport.cpp Framework/Viewport/WebAssemblyViewport.h
diffstat 2 files changed, 49 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Viewport/WebAssemblyViewport.cpp	Thu Jul 18 09:41:10 2019 +0200
+++ b/Framework/Viewport/WebAssemblyViewport.cpp	Thu Jul 18 10:39:00 2019 +0200
@@ -20,6 +20,7 @@
 
 
 #include "WebAssemblyViewport.h"
+#include <emscripten/html5.h>
 
 namespace OrthancStone
 {
@@ -47,18 +48,56 @@
   }
 
 
-  WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas, unsigned int width, unsigned int height) :
+  WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) :
     WebAssemblyViewport(canvas),
-    compositor_(GetScene(), width, height)
+    canvas_(canvas),
+    compositor_(GetScene(), 1024, 768)
   {
   }
 
     
   WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas,
-                                                     boost::shared_ptr<Scene2D>& scene, unsigned int width, unsigned int height) :
+                                                     boost::shared_ptr<Scene2D>& scene) :
     WebAssemblyViewport(canvas, scene),
-    compositor_(GetScene(), width, height)
+    canvas_(canvas),
+    compositor_(GetScene(), 1024, 768)
   {
   }
 
+
+  void WebAssemblyCairoViewport::UpdateSize()
+  {
+    LOG(INFO) << "updating cairo viewport size";
+    double w, h;
+    emscripten_get_element_css_size(canvas_.c_str(), &w, &h);
+
+    /**
+     * Emscripten has the function emscripten_get_element_css_size()
+     * to query the width and height of a named HTML element. I'm
+     * calling this first to get the initial size of the canvas DOM
+     * element, and then call emscripten_set_canvas_size() to
+     * initialize the framebuffer size of the canvas to the same
+     * size as its DOM element.
+     * https://floooh.github.io/2017/02/22/emsc-html.html
+     **/
+    unsigned int canvasWidth = 0;
+    unsigned int canvasHeight = 0;
+
+    if (w > 0 ||
+        h > 0)
+    {
+      canvasWidth = static_cast<unsigned int>(boost::math::iround(w));
+      canvasHeight = static_cast<unsigned int>(boost::math::iround(h));
+    }
+
+    emscripten_set_canvas_element_size(canvas_.c_str(), canvasWidth, canvasHeight);
+    compositor_.UpdateSize(canvasWidth, canvasHeight);
+  }
+
+  void WebAssemblyCairoViewport::Refresh()
+  {
+    LOG(INFO) << "refreshing cairo viewport, TODO: blit to the canvans.getContext('2d')";
+    GetCompositor().Refresh();
+  }
+
 }
--- a/Framework/Viewport/WebAssemblyViewport.h	Thu Jul 18 09:41:10 2019 +0200
+++ b/Framework/Viewport/WebAssemblyViewport.h	Thu Jul 18 10:39:00 2019 +0200
@@ -68,21 +68,18 @@
   {
   private:
     CairoCompositor                  compositor_;
+    std::string                      canvas_;
 
   public:
-    WebAssemblyCairoViewport(const std::string& canvas, 
-                             unsigned int width, 
-                             unsigned int height);
+    WebAssemblyCairoViewport(const std::string& canvas);
     
     WebAssemblyCairoViewport(const std::string& canvas,
-                             boost::shared_ptr<Scene2D>& scene, 
-                             unsigned int width, 
-                             unsigned int height);
+                             boost::shared_ptr<Scene2D>& scene);
     
-    // This function must be called each time the browser window is resized
-    void UpdateSize(); // TODO: implement
+    void UpdateSize(); 
 
-    // TODO: implement Refresh
+    virtual void Refresh();
+
     virtual ICompositor& GetCompositor()
     {
       return compositor_;