Mercurial > hg > orthanc-stone
comparison Framework/Viewport/WebGLViewportsRegistry.cpp @ 1232:a28861abf888 broker
viewports for WebAssembly
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 17:46:33 +0100 |
parents | |
children | e71ee3e88448 |
comparison
equal
deleted
inserted
replaced
1229:b9f2a111c5b9 | 1232:a28861abf888 |
---|---|
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-2019 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 <Core/OrthancException.h> | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 void WebGLViewportsRegistry::LaunchTimer() | |
29 { | |
30 emscripten_set_timeout(OnTimeoutCallback, 1000.0 * static_cast<double>(timeoutSeconds_), this); | |
31 } | |
32 | |
33 | |
34 void WebGLViewportsRegistry::OnTimeout() | |
35 { | |
36 for (Viewports::iterator it = viewports_.begin(); it != viewports_.end(); ++it) | |
37 { | |
38 if (it->second == NULL || | |
39 it->second->IsContextLost()) | |
40 { | |
41 LOG(INFO) << "WebGL context lost for canvas: " << it->first; | |
42 | |
43 // Try and duplicate the HTML5 canvas in the DOM | |
44 EM_ASM({ | |
45 var canvas = document.getElementById(UTF8ToString($0)); | |
46 if (canvas) { | |
47 var parent = canvas.parentElement; | |
48 if (parent) { | |
49 var cloned = canvas.cloneNode(true /* deep copy */); | |
50 parent.insertBefore(cloned, canvas); | |
51 parent.removeChild(canvas); | |
52 } | |
53 } | |
54 }, | |
55 it->first.c_str() // $0 = ID of the canvas | |
56 ); | |
57 | |
58 // At this point, the old canvas is removed from the DOM and | |
59 // replaced by a fresh one with the same ID: Recreate the | |
60 // WebGL context on the new canvas | |
61 std::auto_ptr<WebGLViewport> viewport; | |
62 | |
63 { | |
64 std::auto_ptr<IViewport::ILock> lock(it->second->Lock()); | |
65 viewport.reset(new WebGLViewport(it->first, lock->GetController().GetScene())); | |
66 } | |
67 | |
68 // Replace the old WebGL viewport by the new one | |
69 delete it->second; | |
70 it->second = viewport.release(); | |
71 | |
72 // Tag the fresh canvas as needing a repaint | |
73 { | |
74 std::auto_ptr<IViewport::ILock> lock(it->second->Lock()); | |
75 lock->Invalidate(); | |
76 } | |
77 } | |
78 } | |
79 | |
80 LaunchTimer(); | |
81 } | |
82 | |
83 | |
84 void WebGLViewportsRegistry::OnTimeoutCallback(void *userData) | |
85 { | |
86 WebGLViewportsRegistry& that = *reinterpret_cast<WebGLViewportsRegistry*>(userData); | |
87 that.OnTimeout(); | |
88 } | |
89 | |
90 | |
91 WebGLViewportsRegistry::WebGLViewportsRegistry(unsigned int timeoutSeconds) : | |
92 timeoutSeconds_(timeoutSeconds) | |
93 { | |
94 if (timeoutSeconds <= 0) | |
95 { | |
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
97 } | |
98 | |
99 LaunchTimer(); | |
100 } | |
101 | |
102 | |
103 WebGLViewportsRegistry::~WebGLViewportsRegistry() | |
104 { | |
105 for (Viewports::iterator it = viewports_.begin(); it != viewports_.end(); ++it) | |
106 { | |
107 if (it->second != NULL) | |
108 { | |
109 delete it->second; | |
110 } | |
111 } | |
112 } | |
113 | |
114 | |
115 void WebGLViewportsRegistry::Add(const std::string& canvasId) | |
116 { | |
117 if (viewports_.find(canvasId) != viewports_.end()) | |
118 { | |
119 LOG(ERROR) << "Canvas was already registered: " << canvasId; | |
120 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
121 } | |
122 else | |
123 { | |
124 viewports_[canvasId] = new WebGLViewport(canvasId); | |
125 } | |
126 } | |
127 | |
128 | |
129 void WebGLViewportsRegistry::Remove(const std::string& canvasId) | |
130 { | |
131 Viewports::iterator found = viewports_.find(canvasId); | |
132 | |
133 if (found == viewports_.end()) | |
134 { | |
135 LOG(ERROR) << "Cannot remove unregistered canvas: " << canvasId; | |
136 } | |
137 else | |
138 { | |
139 if (found->second != NULL) | |
140 { | |
141 delete found->second; | |
142 } | |
143 | |
144 viewports_.erase(found); | |
145 } | |
146 } | |
147 | |
148 | |
149 WebGLViewportsRegistry::Accessor::Accessor(WebGLViewportsRegistry& that, | |
150 const std::string& canvasId) : | |
151 that_(that) | |
152 { | |
153 Viewports::iterator viewport = that.viewports_.find(canvasId); | |
154 if (viewport != that.viewports_.end() && | |
155 viewport->second != NULL) | |
156 { | |
157 lock_.reset(viewport->second->Lock()); | |
158 } | |
159 } | |
160 | |
161 | |
162 IViewport::ILock& WebGLViewportsRegistry::Accessor::GetViewport() const | |
163 { | |
164 if (IsValid()) | |
165 { | |
166 return *lock_; | |
167 } | |
168 else | |
169 { | |
170 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
171 } | |
172 } | |
173 } |