# HG changeset patch # User Benjamin Golinvaux # Date 1576853060 -3600 # Node ID ce3052f28f2e6e7b29cfad4efae4e3881e70b056 # Parent e257b91fae2c9d469371e5264e95b643d8dacb7a Added a lazy size update system in WebAssemblyOpenGLViewport::Refresh diff -r e257b91fae2c -r ce3052f28f2e Framework/Viewport/WebAssemblyViewport.cpp --- 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& scene) : - WebAssemblyViewport(canvas, scene), - context_(canvas) + boost::shared_ptr& 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(); diff -r e257b91fae2c -r ce3052f28f2e Framework/Viewport/WebAssemblyViewport.h --- 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& scene) : - ViewportBase(scene), - canvasIdentifier_(canvasIdentifier) + boost::shared_ptr& scene) + : ViewportBase(scene) + , canvasIdentifier_(canvasIdentifier) { } @@ -58,6 +58,13 @@ private: OpenGL::WebAssemblyOpenGLContext context_; std::auto_ptr compositor_; + double cssWidth_; + double cssHeight_; + int pixelWidth_; + int pixelHeight_; + + private: + void UpdateSizeIfNeeded(); public: WebAssemblyOpenGLViewport(const std::string& canvas);