Mercurial > hg > orthanc-stone
comparison 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 |
comparison
equal
deleted
inserted
replaced
1238:e257b91fae2c | 1239:ce3052f28f2e |
---|---|
25 | 25 |
26 #include <emscripten/html5.h> | 26 #include <emscripten/html5.h> |
27 | 27 |
28 namespace OrthancStone | 28 namespace OrthancStone |
29 { | 29 { |
30 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) : | 30 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) |
31 WebAssemblyViewport(canvas), | 31 : WebAssemblyViewport(canvas) |
32 context_(canvas) | 32 , context_(canvas) |
33 , cssWidth_(0) // will be set in Refresh() | |
34 , cssHeight_(0) // ditto | |
35 , pixelWidth_(0) // ditto | |
36 , pixelHeight_(0) // ditto | |
33 { | 37 { |
34 compositor_.reset(new OpenGLCompositor(context_, GetScene())); | 38 compositor_.reset(new OpenGLCompositor(context_, GetScene())); |
35 RegisterContextCallbacks(); | 39 RegisterContextCallbacks(); |
36 } | 40 } |
37 | 41 |
38 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas, | 42 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas, |
39 boost::shared_ptr<Scene2D>& scene) : | 43 boost::shared_ptr<Scene2D>& scene) |
40 WebAssemblyViewport(canvas, scene), | 44 : WebAssemblyViewport(canvas, scene) |
41 context_(canvas) | 45 , context_(canvas) |
46 , cssWidth_(0) // will be set in Refresh() | |
47 , cssHeight_(0) // ditto | |
48 , pixelWidth_(0) // ditto | |
49 , pixelHeight_(0) // ditto | |
42 { | 50 { |
43 compositor_.reset(new OpenGLCompositor(context_, GetScene())); | 51 compositor_.reset(new OpenGLCompositor(context_, GetScene())); |
44 RegisterContextCallbacks(); | 52 RegisterContextCallbacks(); |
45 } | 53 } |
46 | 54 |
99 { | 107 { |
100 return *compositor_; | 108 return *compositor_; |
101 } | 109 } |
102 } | 110 } |
103 | 111 |
112 void WebAssemblyOpenGLViewport::UpdateSizeIfNeeded() | |
113 { | |
114 bool needsRefresh = false; | |
115 std::string canvasId = GetCanvasIdentifier(); | |
116 | |
117 { | |
118 double cssWidth = 0; | |
119 double cssHeight = 0; | |
120 EMSCRIPTEN_RESULT res = EMSCRIPTEN_RESULT_SUCCESS; | |
121 res = | |
122 emscripten_get_element_css_size(canvasId.c_str(), &cssWidth, &cssHeight); | |
123 | |
124 if (res == EMSCRIPTEN_RESULT_SUCCESS) | |
125 { | |
126 if ((cssWidth != cssWidth_) || (cssHeight != cssHeight_)) | |
127 { | |
128 cssWidth_ = cssWidth; | |
129 cssHeight_ = cssHeight; | |
130 needsRefresh = true; | |
131 } | |
132 } | |
133 } | |
134 | |
135 { | |
136 int pixelWidth = 0; | |
137 int pixelHeight = 0; | |
138 EMSCRIPTEN_RESULT res = EMSCRIPTEN_RESULT_SUCCESS; | |
139 res = | |
140 emscripten_get_canvas_element_size(canvasId.c_str(), &pixelWidth, &pixelHeight); | |
141 | |
142 if (res == EMSCRIPTEN_RESULT_SUCCESS) | |
143 { | |
144 if ((pixelWidth != pixelWidth_) || (pixelHeight != pixelHeight_)) | |
145 { | |
146 pixelWidth_ = pixelWidth; | |
147 pixelHeight_ = pixelHeight; | |
148 needsRefresh = true; | |
149 } | |
150 } | |
151 } | |
152 | |
153 if (needsRefresh) | |
154 UpdateSize(); | |
155 } | |
156 | |
104 void WebAssemblyOpenGLViewport::Refresh() | 157 void WebAssemblyOpenGLViewport::Refresh() |
105 { | 158 { |
106 try | 159 try |
107 { | 160 { |
161 // first, we check if the canvas size (both css size in css pixels and | |
162 // backing store) have changed. if so, we call UpdateSize to deal with | |
163 // it | |
164 | |
165 LOG(INFO) << "updating cairo viewport size"; | |
166 | |
167 // maybe the canvas size has changed and we need to update the | |
168 // canvas backing store size | |
169 UpdateSizeIfNeeded(); | |
170 | |
108 if (HasCompositor()) | 171 if (HasCompositor()) |
109 { | 172 { |
110 GetCompositor().Refresh(); | 173 GetCompositor().Refresh(); |
111 } | 174 } |
112 else | 175 else |