comparison Framework/Viewport/WebGLViewportsRegistry.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 c38c89684d83
children 30deba7bc8e2
comparison
equal deleted inserted replaced
1329:8d3e669f01a2 1331:ab81ee8fce1f
19 **/ 19 **/
20 20
21 21
22 #include "WebGLViewportsRegistry.h" 22 #include "WebGLViewportsRegistry.h"
23 23
24 #include "../Toolbox/GenericToolbox.h"
25
24 #include <Core/OrthancException.h> 26 #include <Core/OrthancException.h>
25 27
26 #include <boost/make_shared.hpp> 28 #include <boost/make_shared.hpp>
27 29
28 namespace OrthancStone 30 namespace OrthancStone
29 { 31 {
30 void WebGLViewportsRegistry::LaunchTimer() 32 void WebGLViewportsRegistry::LaunchTimer()
31 { 33 {
32 emscripten_set_timeout(OnTimeoutCallback, timeoutMS_, this); 34 timeOutID_ = emscripten_set_timeout(
35 OnTimeoutCallback,
36 timeoutMS_,
37 reinterpret_cast<void*>(this));
33 } 38 }
34
35 39
36 void WebGLViewportsRegistry::OnTimeout() 40 void WebGLViewportsRegistry::OnTimeout()
37 { 41 {
38 for (Viewports::iterator it = viewports_.begin(); it != viewports_.end(); ++it) 42 for (Viewports::iterator it = viewports_.begin();
43 it != viewports_.end();
44 ++it)
39 { 45 {
40 if (it->second == NULL || 46 if (it->second == NULL ||
41 it->second->IsContextLost()) 47 it->second->IsContextLost())
42 { 48 {
43 LOG(INFO) << "WebGL context lost for canvas: " << it->first; 49 LOG(INFO) << "WebGL context lost for canvas: " << it->first;
59 65
60 // At this point, the old canvas is removed from the DOM and 66 // At this point, the old canvas is removed from the DOM and
61 // replaced by a fresh one with the same ID: Recreate the 67 // replaced by a fresh one with the same ID: Recreate the
62 // WebGL context on the new canvas 68 // WebGL context on the new canvas
63 boost::shared_ptr<WebGLViewport> viewport; 69 boost::shared_ptr<WebGLViewport> viewport;
64 70
71 // we need to steal the properties from the old viewport
72 // and set them to the new viewport
65 { 73 {
66 std::unique_ptr<IViewport::ILock> lock(it->second->Lock()); 74 std::unique_ptr<IViewport::ILock> lock(it->second->Lock());
67 viewport = boost::make_shared<WebGLViewport>(it->first, lock->GetController().GetScene()); 75
76 // TODO: remove ViewportController
77 Scene2D* scene = lock->GetController().ReleaseScene();
78 viewport = WebGLViewport::Create(it->first);
79
80 {
81 std::unique_ptr<IViewport::ILock> newLock(viewport->Lock());
82 newLock->GetController().AcquireScene(scene);
83 }
68 } 84 }
69 85
70 // Replace the old WebGL viewport by the new one 86 // Replace the old WebGL viewport by the new one
71 it->second = viewport; 87 it->second = viewport;
72 88
79 } 95 }
80 96
81 LaunchTimer(); 97 LaunchTimer();
82 } 98 }
83 99
84
85 void WebGLViewportsRegistry::OnTimeoutCallback(void *userData) 100 void WebGLViewportsRegistry::OnTimeoutCallback(void *userData)
86 { 101 {
87 WebGLViewportsRegistry& that = *reinterpret_cast<WebGLViewportsRegistry*>(userData); 102 // This object dies with the process or tab.
88 that.OnTimeout(); 103 WebGLViewportsRegistry* that =
104 reinterpret_cast<WebGLViewportsRegistry*>(userData);
105 that->OnTimeout();
89 } 106 }
90 107
91
92 WebGLViewportsRegistry::WebGLViewportsRegistry(double timeoutMS) : 108 WebGLViewportsRegistry::WebGLViewportsRegistry(double timeoutMS) :
93 timeoutMS_(timeoutMS) 109 timeoutMS_(timeoutMS),
110 timeOutID_(0)
94 { 111 {
95 if (timeoutMS <= 0) 112 if (timeoutMS <= 0)
96 { 113 {
97 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); 114 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
98 } 115 }
99 116
100 LaunchTimer(); 117 LaunchTimer();
101 } 118 }
102 119
120 WebGLViewportsRegistry::~WebGLViewportsRegistry()
121 {
122 emscripten_clear_timeout(timeOutID_);
123 Clear();
124 }
103 125
104 boost::shared_ptr<WebGLViewport> WebGLViewportsRegistry::Add(const std::string& canvasId) 126 boost::shared_ptr<WebGLViewport> WebGLViewportsRegistry::Add(
127 const std::string& canvasId)
105 { 128 {
106 if (viewports_.find(canvasId) != viewports_.end()) 129 if (viewports_.find(canvasId) != viewports_.end())
107 { 130 {
108 LOG(ERROR) << "Canvas was already registered: " << canvasId; 131 LOG(ERROR) << "Canvas was already registered: " << canvasId;
109 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 132 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
110 } 133 }
111 else 134 else
112 { 135 {
113 boost::shared_ptr<WebGLViewport> viewport(new WebGLViewport(canvasId)); 136 boost::shared_ptr<WebGLViewport> viewport =
137 WebGLViewport::Create(canvasId);
114 viewports_[canvasId] = viewport; 138 viewports_[canvasId] = viewport;
115 return viewport; 139 return viewport;
116 } 140 }
117 } 141 }
118 142
119
120 void WebGLViewportsRegistry::Remove(const std::string& canvasId) 143 void WebGLViewportsRegistry::Remove(const std::string& canvasId)
121 { 144 {
122 Viewports::iterator found = viewports_.find(canvasId); 145 Viewports::iterator found = viewports_.find(canvasId);
123 146
124 if (found == viewports_.end()) 147 if (found == viewports_.end())
128 else 151 else
129 { 152 {
130 viewports_.erase(found); 153 viewports_.erase(found);
131 } 154 }
132 } 155 }
133 156
134
135 void WebGLViewportsRegistry::Clear() 157 void WebGLViewportsRegistry::Clear()
136 { 158 {
137 viewports_.clear(); 159 viewports_.clear();
138 } 160 }
139
140 161
141 WebGLViewportsRegistry::Accessor::Accessor(WebGLViewportsRegistry& that, 162 WebGLViewportsRegistry::Accessor::Accessor(WebGLViewportsRegistry& that,
142 const std::string& canvasId) : 163 const std::string& canvasId) :
143 that_(that) 164 that_(that)
144 { 165 {
148 { 169 {
149 lock_.reset(viewport->second->Lock()); 170 lock_.reset(viewport->second->Lock());
150 } 171 }
151 } 172 }
152 173
153
154 IViewport::ILock& WebGLViewportsRegistry::Accessor::GetViewport() const 174 IViewport::ILock& WebGLViewportsRegistry::Accessor::GetViewport() const
155 { 175 {
156 if (IsValid()) 176 if (IsValid())
157 { 177 {
158 return *lock_; 178 return *lock_;