# HG changeset patch # User Alain Mazy # Date 1563439140 -7200 # Node ID a6c12fe88bcb9eed59c58859affbabb8a5c85355 # Parent 7a7e4e1f558f06a1fd265781b65db3c86781530e wip: WebAssemblyCairoViewport: still need to implement blit to canvas diff -r 7a7e4e1f558f -r a6c12fe88bcb Framework/Viewport/WebAssemblyViewport.cpp --- 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 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& scene, unsigned int width, unsigned int height) : + boost::shared_ptr& 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(boost::math::iround(w)); + canvasHeight = static_cast(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(); + } + } diff -r 7a7e4e1f558f -r a6c12fe88bcb Framework/Viewport/WebAssemblyViewport.h --- 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& scene, - unsigned int width, - unsigned int height); + boost::shared_ptr& 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_;