comparison OrthancStone/Sources/OpenGL/WebAssemblyOpenGLContext.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/OpenGL/WebAssemblyOpenGLContext.cpp@6abd819aa534
children 85e117739eca
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 "WebAssemblyOpenGLContext.h"
23
24 #include "../StoneException.h"
25
26 #include <OrthancException.h>
27
28 #include <emscripten/html5.h>
29 #include <emscripten/em_asm.h>
30
31 #include <boost/math/special_functions/round.hpp>
32
33 namespace OrthancStone
34 {
35 namespace OpenGL
36 {
37 class WebAssemblyOpenGLContext::PImpl
38 {
39 private:
40 std::string canvasSelector_;
41 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context_;
42 unsigned int canvasWidth_;
43 unsigned int canvasHeight_;
44 bool isContextLost_;
45
46 public:
47 PImpl(const std::string& canvasSelector)
48 : canvasSelector_(canvasSelector)
49 , isContextLost_(false)
50 {
51 // Context configuration
52 EmscriptenWebGLContextAttributes attr;
53 emscripten_webgl_init_context_attributes(&attr);
54
55 context_ = emscripten_webgl_create_context(canvasSelector.c_str(), &attr);
56 if (context_ == 0)
57 {
58 std::string message("Cannot create an OpenGL context for the element with the following CSS selector: \"");
59 message += canvasSelector;
60 message += "\" Please make sure the -s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 flag has been passed to Emscripten when building.";
61 LOG(ERROR) << message;
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, message);
63 }
64
65 UpdateSize();
66 }
67
68 void* DebugGetInternalContext() const
69 {
70 return reinterpret_cast<void*>(context_);
71 }
72
73 bool IsContextLost()
74 {
75 //LOG(TRACE) << "IsContextLost() for context " << std::hex << context_ << std::dec;
76 bool apiFlag = (emscripten_is_webgl_context_lost(context_) != 0);
77 isContextLost_ = apiFlag;
78 return isContextLost_;
79 }
80
81 void SetLostContext()
82 {
83 isContextLost_ = true;
84 }
85
86 ~PImpl()
87 {
88 try
89 {
90 EMSCRIPTEN_RESULT result = emscripten_webgl_destroy_context(context_);
91 if (result != EMSCRIPTEN_RESULT_SUCCESS)
92 {
93 LOG(ERROR) << "emscripten_webgl_destroy_context returned code " << result;
94 }
95 }
96 catch (const Orthanc::OrthancException& e)
97 {
98 if (e.HasDetails())
99 {
100 LOG(ERROR) << "OrthancException in WebAssemblyOpenGLContext::~PImpl: " << e.What() << " Details: " << e.GetDetails();
101 }
102 else
103 {
104 LOG(ERROR) << "OrthancException in WebAssemblyOpenGLContext::~PImpl: " << e.What();
105 }
106 }
107 catch (const std::exception& e)
108 {
109 LOG(ERROR) << "std::exception in WebAssemblyOpenGLContext::~PImpl: " << e.what();
110 }
111 catch (...)
112 {
113 LOG(ERROR) << "Unknown exception in WebAssemblyOpenGLContext::~PImpl";
114 }
115 }
116
117 const std::string& GetCanvasSelector() const
118 {
119 return canvasSelector_;
120 }
121
122 void MakeCurrent()
123 {
124 if (IsContextLost())
125 {
126 LOG(ERROR) << "MakeCurrent() called on lost context " << context_;
127 throw StoneException(ErrorCode_WebGLContextLost);
128 }
129
130 if (emscripten_is_webgl_context_lost(context_))
131 {
132 LOG(ERROR) << "OpenGL context has been lost for canvas selector: " << canvasSelector_;
133 SetLostContext();
134 throw StoneException(ErrorCode_WebGLContextLost);
135 }
136
137 if (emscripten_webgl_make_context_current(context_) != EMSCRIPTEN_RESULT_SUCCESS)
138 {
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError,
140 "Cannot set the OpenGL context");
141 }
142 }
143
144 void SwapBuffer()
145 {
146 /**
147 * "Rendered WebGL content is implicitly presented (displayed to
148 * the user) on the canvas when the event handler that renders with
149 * WebGL returns back to the browser event loop."
150 * https://emscripten.org/docs/api_reference/html5.h.html#webgl-context
151 *
152 * Could call "emscripten_webgl_commit_frame()" if
153 * "explicitSwapControl" option were set to "true".
154 **/
155 }
156
157 unsigned int GetCanvasWidth() const
158 {
159 return canvasWidth_;
160 }
161
162 unsigned int GetCanvasHeight() const
163 {
164 return canvasHeight_;
165 }
166
167 void UpdateSize()
168 {
169 double w, h;
170 emscripten_get_element_css_size(canvasSelector_.c_str(), &w, &h);
171
172 /**
173 * Emscripten has the function emscripten_get_element_css_size()
174 * to query the width and height of a named HTML element. I'm
175 * calling this first to get the initial size of the canvas DOM
176 * element, and then call emscripten_set_canvas_size() to
177 * initialize the framebuffer size of the canvas to the same
178 * size as its DOM element.
179 * https://floooh.github.io/2017/02/22/emsc-html.html
180 **/
181
182 if (w <= 0 ||
183 h <= 0)
184 {
185 canvasWidth_ = 0;
186 canvasHeight_ = 0;
187 }
188 else
189 {
190 canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w));
191 canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h));
192 }
193
194 emscripten_set_canvas_element_size(canvasSelector_.c_str(), canvasWidth_, canvasHeight_);
195 }
196 };
197
198
199 WebAssemblyOpenGLContext::WebAssemblyOpenGLContext(const std::string& canvasSelector) :
200 pimpl_(new PImpl(canvasSelector))
201 {
202 }
203
204 bool WebAssemblyOpenGLContext::IsContextLost()
205 {
206 return pimpl_->IsContextLost();
207 }
208
209 void WebAssemblyOpenGLContext::SetLostContext()
210 {
211 pimpl_->SetLostContext();
212 }
213
214 void* WebAssemblyOpenGLContext::DebugGetInternalContext() const
215 {
216 return pimpl_->DebugGetInternalContext();
217 }
218
219 void WebAssemblyOpenGLContext::MakeCurrent()
220 {
221 assert(pimpl_.get() != NULL);
222 pimpl_->MakeCurrent();
223 }
224
225 void WebAssemblyOpenGLContext::SwapBuffer()
226 {
227 assert(pimpl_.get() != NULL);
228 pimpl_->SwapBuffer();
229 }
230
231 unsigned int WebAssemblyOpenGLContext::GetCanvasWidth() const
232 {
233 assert(pimpl_.get() != NULL);
234 return pimpl_->GetCanvasWidth();
235 }
236
237 unsigned int WebAssemblyOpenGLContext::GetCanvasHeight() const
238 {
239 assert(pimpl_.get() != NULL);
240 return pimpl_->GetCanvasHeight();
241 }
242
243 void WebAssemblyOpenGLContext::RefreshCanvasSize()
244 {
245 assert(pimpl_.get() != NULL);
246
247 try
248 {
249 pimpl_->UpdateSize();
250 }
251 catch (const StoneException& e)
252 {
253 // Ignore problems about the loss of the WebGL context (edge case)
254 if (e.GetErrorCode() == ErrorCode_WebGLContextLost)
255 {
256 return;
257 }
258 else
259 {
260 throw;
261 }
262 }
263 }
264
265 const std::string& WebAssemblyOpenGLContext::GetCanvasSelector() const
266 {
267 assert(pimpl_.get() != NULL);
268 return pimpl_->GetCanvasSelector();
269 }
270 }
271 }