comparison OrthancStone/Sources/Viewport/WebGLViewportsRegistry.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Viewport/WebGLViewportsRegistry.cpp@30deba7bc8e2
children 314b6dc507d9
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "WebGLViewportsRegistry.h"
23
24 #include "../Toolbox/GenericToolbox.h"
25
26 #include <OrthancException.h>
27
28 #include <boost/make_shared.hpp>
29
30 namespace OrthancStone
31 {
32 void WebGLViewportsRegistry::LaunchTimer()
33 {
34 timeOutID_ = emscripten_set_timeout(
35 OnTimeoutCallback,
36 timeoutMS_,
37 reinterpret_cast<void*>(this));
38 }
39
40 void WebGLViewportsRegistry::OnTimeout()
41 {
42 for (Viewports::iterator it = viewports_.begin();
43 it != viewports_.end();
44 ++it)
45 {
46 if (it->second == NULL ||
47 it->second->IsContextLost())
48 {
49 LOG(INFO) << "WebGL context lost for canvas: " << it->first;
50
51 // Try and duplicate the HTML5 canvas in the DOM
52 EM_ASM({
53 var canvas = document.getElementById(UTF8ToString($0));
54 if (canvas) {
55 var parent = canvas.parentElement;
56 if (parent) {
57 var cloned = canvas.cloneNode(true /* deep copy */);
58 parent.insertBefore(cloned, canvas);
59 parent.removeChild(canvas);
60 }
61 }
62 },
63 it->first.c_str() // $0 = ID of the canvas
64 );
65
66 // At this point, the old canvas is removed from the DOM and
67 // replaced by a fresh one with the same ID: Recreate the
68 // WebGL context on the new canvas
69 boost::shared_ptr<WebGLViewport> viewport;
70
71 // we need to steal the properties from the old viewport
72 // and set them to the new viewport
73 {
74 std::unique_ptr<IViewport::ILock> lock(it->second->Lock());
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 }
84 }
85
86 // Replace the old WebGL viewport by the new one
87 it->second = viewport;
88
89 // Tag the fresh canvas as needing a repaint
90 {
91 std::unique_ptr<IViewport::ILock> lock(it->second->Lock());
92 lock->Invalidate();
93 }
94 }
95 }
96
97 LaunchTimer();
98 }
99
100 void WebGLViewportsRegistry::OnTimeoutCallback(void *userData)
101 {
102 // This object dies with the process or tab.
103 WebGLViewportsRegistry* that =
104 reinterpret_cast<WebGLViewportsRegistry*>(userData);
105 that->OnTimeout();
106 }
107
108 WebGLViewportsRegistry::WebGLViewportsRegistry(double timeoutMS) :
109 timeoutMS_(timeoutMS),
110 timeOutID_(0)
111 {
112 if (timeoutMS <= 0)
113 {
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
115 }
116
117 LaunchTimer();
118 }
119
120 WebGLViewportsRegistry::~WebGLViewportsRegistry()
121 {
122 emscripten_clear_timeout(timeOutID_);
123 Clear();
124 }
125
126 boost::shared_ptr<WebGLViewport> WebGLViewportsRegistry::Add(
127 const std::string& canvasId)
128 {
129 if (viewports_.find(canvasId) != viewports_.end())
130 {
131 LOG(ERROR) << "Canvas was already registered: " << canvasId;
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
133 }
134 else
135 {
136 boost::shared_ptr<WebGLViewport> viewport =
137 WebGLViewport::Create(canvasId);
138 viewports_[canvasId] = viewport;
139 return viewport;
140 }
141 }
142
143 void WebGLViewportsRegistry::Remove(const std::string& canvasId)
144 {
145 Viewports::iterator found = viewports_.find(canvasId);
146
147 if (found == viewports_.end())
148 {
149 LOG(ERROR) << "Cannot remove unregistered canvas: " << canvasId;
150 }
151 else
152 {
153 viewports_.erase(found);
154 }
155 }
156
157 void WebGLViewportsRegistry::Clear()
158 {
159 viewports_.clear();
160 }
161
162 WebGLViewportsRegistry::Accessor::Accessor(WebGLViewportsRegistry& that,
163 const std::string& canvasId) :
164 that_(that)
165 {
166 Viewports::iterator viewport = that.viewports_.find(canvasId);
167 if (viewport != that.viewports_.end() &&
168 viewport->second != NULL)
169 {
170 lock_.reset(viewport->second->Lock());
171 }
172 }
173
174 IViewport::ILock& WebGLViewportsRegistry::Accessor::GetViewport() const
175 {
176 if (IsValid())
177 {
178 return *lock_;
179 }
180 else
181 {
182 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
183 }
184 }
185 }