comparison Framework/Viewport/WebAssemblyViewport.cpp @ 1331:ab81ee8fce1f broker

- Viewport is not passed and stored as a shared_ptr instead of raw reference. - ViewportController can now be injected with an undo stack (not a ctor param anymore, as a preparation for the move of the undo stack to an interactor) - Added (temp) flag to disable emscripten events registration in the WebAssemblyViewport (because legacy client code deals with them directly) - Added emscripten_clear_timeout in ~WebGLViewportsRegistry - Removed GenericToolbox::HoldingRef whose responsibility is better served with proper callback un-registration.
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 30 Mar 2020 14:23:46 +0200
parents fd616c4a5904
children df8bf351c23f
comparison
equal deleted inserted replaced
1329:8d3e669f01a2 1331:ab81ee8fce1f
104 }; 104 };
105 105
106 106
107 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData) 107 EM_BOOL WebAssemblyViewport::OnRequestAnimationFrame(double time, void *userData)
108 { 108 {
109 boost::shared_ptr<WebAssemblyViewport> that = GenericToolbox::HoldingRef<WebAssemblyViewport>::Unwrap(userData); 109 LOG(TRACE) << __func__;
110 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
110 111
111 if (that->compositor_.get() != NULL && 112 if (that->compositor_.get() != NULL &&
112 that->controller_ /* should always be true */) 113 that->controller_ /* should always be true */)
113 { 114 {
114 that->Paint(*that->compositor_, *that->controller_); 115 that->Paint(*that->compositor_, *that->controller_);
115 } 116 }
116 117
118 LOG(TRACE) << "Exiting: " << __func__;
117 return true; 119 return true;
118 } 120 }
119 121
120 EM_BOOL WebAssemblyViewport::OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) 122 EM_BOOL WebAssemblyViewport::OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData)
121 { 123 {
122 boost::shared_ptr<WebAssemblyViewport> that = GenericToolbox::HoldingRef<WebAssemblyViewport>::Unwrap(userData); 124 LOG(TRACE) << __func__;
125 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
123 126
124 if (that->compositor_.get() != NULL) 127 if (that->compositor_.get() != NULL)
125 { 128 {
126 that->UpdateSize(*that->compositor_); 129 that->UpdateSize(*that->compositor_);
127 that->Invalidate(); 130 that->Invalidate();
128 } 131 }
129 132
133 LOG(TRACE) << "Exiting: " << __func__;
130 return true; 134 return true;
131 } 135 }
132 136
133 137
134 EM_BOOL WebAssemblyViewport::OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) 138 EM_BOOL WebAssemblyViewport::OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
135 { 139 {
136 boost::shared_ptr<WebAssemblyViewport> that = GenericToolbox::HoldingRef<WebAssemblyViewport>::Unwrap(userData); 140 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
137 141
138 LOG(INFO) << "mouse down: " << that->GetFullCanvasId(); 142 LOG(TRACE) << "mouse down: " << that->GetFullCanvasId();
139 143
140 if (that->compositor_.get() != NULL && 144 if (that->compositor_.get() != NULL &&
141 that->interactor_.get() != NULL) 145 that->interactor_.get() != NULL)
142 { 146 {
143 PointerEvent pointer; 147 PointerEvent pointer;
147 that->compositor_->GetCanvasWidth(), 151 that->compositor_->GetCanvasWidth(),
148 that->compositor_->GetCanvasHeight()); 152 that->compositor_->GetCanvasHeight());
149 that->Invalidate(); 153 that->Invalidate();
150 } 154 }
151 155
156 LOG(TRACE) << "Exiting: " << __func__;
152 return true; 157 return true;
153 } 158 }
154 159
155 160
156 EM_BOOL WebAssemblyViewport::OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) 161 EM_BOOL WebAssemblyViewport::OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
157 { 162 {
158 boost::shared_ptr<WebAssemblyViewport> that = GenericToolbox::HoldingRef<WebAssemblyViewport>::Unwrap(userData); 163 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP1. userData = " << userData;
164
165 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
166 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP2";
159 167
160 if (that->compositor_.get() != NULL && 168 if (that->compositor_.get() != NULL &&
161 that->controller_->HasActiveTracker()) 169 that->controller_->HasActiveTracker())
162 { 170 {
171 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP3";
163 PointerEvent pointer; 172 PointerEvent pointer;
164 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_); 173 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
174 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP4";
165 if (that->controller_->HandleMouseMove(pointer)) 175 if (that->controller_->HandleMouseMove(pointer))
166 { 176 {
177 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP5";
167 that->Invalidate(); 178 that->Invalidate();
179 LOG(TRACE) << "WebAssemblyViewport::OnMouseMove CP6";
168 } 180 }
169 } 181 }
170 182
183 LOG(TRACE) << "Exiting: " << __func__;
171 return true; 184 return true;
172 } 185 }
173 186
174 EM_BOOL WebAssemblyViewport::OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) 187 EM_BOOL WebAssemblyViewport::OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
175 { 188 {
176 boost::shared_ptr<WebAssemblyViewport> that = GenericToolbox::HoldingRef<WebAssemblyViewport>::Unwrap(userData); 189 LOG(TRACE) << __func__;
190 WebAssemblyViewport* that = reinterpret_cast<WebAssemblyViewport*>(userData);
177 191
178 if (that->compositor_.get() != NULL) 192 if (that->compositor_.get() != NULL)
179 { 193 {
180 PointerEvent pointer; 194 PointerEvent pointer;
181 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_); 195 ConvertMouseEvent(pointer, *mouseEvent, *that->compositor_);
182 that->controller_->HandleMouseRelease(pointer); 196 that->controller_->HandleMouseRelease(pointer);
183 that->Invalidate(); 197 that->Invalidate();
184 } 198 }
185 199
200 LOG(TRACE) << "Exiting: " << __func__;
186 return true; 201 return true;
187 } 202 }
188 203
189 void WebAssemblyViewport::Invalidate() 204 void WebAssemblyViewport::Invalidate()
190 { 205 {
191 emscripten_request_animation_frame(OnRequestAnimationFrame, GenericToolbox::HoldingRef<WebAssemblyViewport>::Wrap(this)); 206 emscripten_request_animation_frame(OnRequestAnimationFrame, reinterpret_cast<void*>(this));
192 } 207 }
193 208
194 void WebAssemblyViewport::AcquireCompositor(ICompositor* compositor /* takes ownership */) 209 void WebAssemblyViewport::AcquireCompositor(ICompositor* compositor /* takes ownership */)
195 { 210 {
196 if (compositor == NULL) 211 if (compositor == NULL)
202 compositor_.reset(compositor); 217 compositor_.reset(compositor);
203 } 218 }
204 } 219 }
205 220
206 WebAssemblyViewport::WebAssemblyViewport( 221 WebAssemblyViewport::WebAssemblyViewport(
207 const std::string& canvasId, 222 const std::string& canvasId, bool enableEmscriptenEvents) :
208 const Scene2D* scene,
209 boost::weak_ptr<UndoStack> undoStackW) :
210 shortCanvasId_(canvasId), 223 shortCanvasId_(canvasId),
211 fullCanvasId_(canvasId), 224 fullCanvasId_(canvasId),
212 interactor_(new DefaultViewportInteractor) 225 interactor_(new DefaultViewportInteractor),
213 { 226 enableEmscriptenEvents_(enableEmscriptenEvents)
214 if(undoStackW.lock() != NULL) 227 {
215 { 228 }
216 controller_ = boost::make_shared<ViewportController>(*this,undoStackW); 229
217 } 230 void WebAssemblyViewport::PostConstructor()
218 else if (scene == NULL) 231 {
219 { 232 boost::shared_ptr<IViewport> viewport = shared_from_this();
220 controller_ = boost::make_shared<ViewportController>(*this); 233 controller_.reset(new ViewportController(viewport));
221 } 234
222 else 235 LOG(INFO) << "Initializing Stone viewport on HTML canvas: "
223 { 236 << shortCanvasId_;
224 controller_ = boost::make_shared<ViewportController>(*this,*scene); 237
225 } 238 if (shortCanvasId_.empty() ||
226 239 shortCanvasId_[0] == '#')
227 LOG(INFO) << "Initializing Stone viewport on HTML canvas: " << canvasId;
228
229 if (canvasId.empty() ||
230 canvasId[0] == '#')
231 { 240 {
232 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, 241 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
233 "The canvas identifier must not start with '#'"); 242 "The canvas identifier must not start with '#'");
234 } 243 }
235 244
236 // Disable right-click on the canvas (i.e. context menu) 245 // Disable right-click on the canvas (i.e. context menu)
237 EM_ASM({ 246 EM_ASM({
238 document.getElementById(UTF8ToString($0)).oncontextmenu = function(event) { 247 document.getElementById(UTF8ToString($0)).oncontextmenu =
248 function(event)
249 {
239 event.preventDefault(); 250 event.preventDefault();
240 } 251 }
241 }, 252 },
242 canvasId.c_str() // $0 253 shortCanvasId_.c_str() // $0
243 ); 254 );
244 255
245 LOG(TRACE) << "2020-03-16-16h23 About to call emscripten_set_XXXX_callback on \"" 256 if (enableEmscriptenEvents_)
246 << fullCanvasId_.c_str() << "\" from WebAssemblyViewport::WebAssemblyViewport"; 257 {
247 258 // It is not possible to monitor the resizing of individual
248 #if 1 259 // canvas, so we track the full window of the browser
249 // It is not possible to monitor the resizing of individual 260 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
250 // canvas, so we track the full window of the browser 261 reinterpret_cast<void*>(this),
251 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, this, false, OnResize); 262 false,
252 263 OnResize);
253 emscripten_set_mousedown_callback(fullCanvasId_.c_str(), this, false, OnMouseDown); 264
254 emscripten_set_mousemove_callback(fullCanvasId_.c_str(), this, false, OnMouseMove); 265 emscripten_set_mousedown_callback(fullCanvasId_.c_str(),
255 emscripten_set_mouseup_callback(fullCanvasId_.c_str(), this, false, OnMouseUp); 266 reinterpret_cast<void*>(this),
256 #endif 267 false,
257 LOG(TRACE) << "2020-03-16-16h23 DONE calling emscripten_set_XXXX_callback on \"" 268 OnMouseDown);
258 << fullCanvasId_.c_str() << "\" from WebAssemblyViewport::WebAssemblyViewport"; 269
259 } 270 emscripten_set_mousemove_callback(fullCanvasId_.c_str(),
260 271 reinterpret_cast<void*>(this),
272 false,
273 OnMouseMove);
274
275 emscripten_set_mouseup_callback(fullCanvasId_.c_str(),
276 reinterpret_cast<void*>(this),
277 false,
278 OnMouseUp);
279 }
280 }
281
282 WebAssemblyViewport::~WebAssemblyViewport()
283 {
284 if (enableEmscriptenEvents_)
285 {
286 emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW,
287 reinterpret_cast<void*>(this),
288 false,
289 NULL);
290
291 emscripten_set_mousedown_callback(fullCanvasId_.c_str(),
292 reinterpret_cast<void*>(this),
293 false,
294 OnMouseDown);
295
296 emscripten_set_mousemove_callback(fullCanvasId_.c_str(),
297 reinterpret_cast<void*>(this),
298 false,
299 OnMouseMove);
300
301 emscripten_set_mouseup_callback(fullCanvasId_.c_str(),
302 reinterpret_cast<void*>(this),
303 false,
304 OnMouseUp);
305 }
306 }
261 307
262 IViewport::ILock* WebAssemblyViewport::Lock() 308 IViewport::ILock* WebAssemblyViewport::Lock()
263 { 309 {
264 return new WasmLock(*this); 310 return new WasmLock(*this);
265 } 311 }
266
267 312
268 void WebAssemblyViewport::AcquireInteractor(IViewportInteractor* interactor) 313 void WebAssemblyViewport::AcquireInteractor(IViewportInteractor* interactor)
269 { 314 {
270 if (interactor == NULL) 315 if (interactor == NULL)
271 { 316 {