comparison Framework/OpenGL/WebAssemblyOpenGLContext.cpp @ 1351:1b8e37770d78 broker

ID vs CSS selector distinction in API and field names.
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 15 Apr 2020 12:57:36 +0200
parents 1b24f6b06408
children 30deba7bc8e2
comparison
equal deleted inserted replaced
1350:c53a4667f895 1351:1b8e37770d78
35 namespace OpenGL 35 namespace OpenGL
36 { 36 {
37 class WebAssemblyOpenGLContext::PImpl 37 class WebAssemblyOpenGLContext::PImpl
38 { 38 {
39 private: 39 private:
40 std::string canvas_; 40 std::string canvasSelector_;
41 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context_; 41 EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context_;
42 unsigned int canvasWidth_; 42 unsigned int canvasWidth_;
43 unsigned int canvasHeight_; 43 unsigned int canvasHeight_;
44 bool isContextLost_; 44 bool isContextLost_;
45 45
46 public: 46 public:
47 PImpl(const std::string& canvas) 47 PImpl(const std::string& canvasSelector)
48 : canvas_(canvas) 48 : canvasSelector_(canvasSelector)
49 , isContextLost_(false) 49 , isContextLost_(false)
50 { 50 {
51 // Context configuration 51 // Context configuration
52 EmscriptenWebGLContextAttributes attr; 52 EmscriptenWebGLContextAttributes attr;
53 emscripten_webgl_init_context_attributes(&attr); 53 emscripten_webgl_init_context_attributes(&attr);
54 54
55 context_ = emscripten_webgl_create_context(canvas.c_str(), &attr); 55 context_ = emscripten_webgl_create_context(canvasSelector.c_str(), &attr);
56 if (context_ == 0) 56 if (context_ == 0)
57 { 57 {
58 std::string message("Cannot create an OpenGL context for canvas: "); 58 std::string message("Cannot create an OpenGL context for the element with the following CSS selector: \"");
59 message += canvas; 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.";
60 LOG(ERROR) << message; 61 LOG(ERROR) << message;
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, message); 62 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, message);
62 } 63 }
63 64
64 UpdateSize(); 65 UpdateSize();
111 { 112 {
112 LOG(ERROR) << "Unknown exception in WebAssemblyOpenGLContext::~PImpl"; 113 LOG(ERROR) << "Unknown exception in WebAssemblyOpenGLContext::~PImpl";
113 } 114 }
114 } 115 }
115 116
116 const std::string& GetCanvasIdentifier() const 117 const std::string& GetCanvasSelector() const
117 { 118 {
118 return canvas_; 119 return canvasSelector_;
119 } 120 }
120 121
121 void MakeCurrent() 122 void MakeCurrent()
122 { 123 {
123 if (IsContextLost()) 124 if (IsContextLost())
126 throw StoneException(ErrorCode_WebGLContextLost); 127 throw StoneException(ErrorCode_WebGLContextLost);
127 } 128 }
128 129
129 if (emscripten_is_webgl_context_lost(context_)) 130 if (emscripten_is_webgl_context_lost(context_))
130 { 131 {
131 LOG(ERROR) << "OpenGL context has been lost for canvas: " << canvas_; 132 LOG(ERROR) << "OpenGL context has been lost for canvas selector: " << canvasSelector_;
132 SetLostContext(); 133 SetLostContext();
133 throw StoneException(ErrorCode_WebGLContextLost); 134 throw StoneException(ErrorCode_WebGLContextLost);
134 } 135 }
135 136
136 if (emscripten_webgl_make_context_current(context_) != EMSCRIPTEN_RESULT_SUCCESS) 137 if (emscripten_webgl_make_context_current(context_) != EMSCRIPTEN_RESULT_SUCCESS)
164 } 165 }
165 166
166 void UpdateSize() 167 void UpdateSize()
167 { 168 {
168 double w, h; 169 double w, h;
169 emscripten_get_element_css_size(canvas_.c_str(), &w, &h); 170 emscripten_get_element_css_size(canvasSelector_.c_str(), &w, &h);
170 171
171 /** 172 /**
172 * Emscripten has the function emscripten_get_element_css_size() 173 * Emscripten has the function emscripten_get_element_css_size()
173 * to query the width and height of a named HTML element. I'm 174 * to query the width and height of a named HTML element. I'm
174 * calling this first to get the initial size of the canvas DOM 175 * calling this first to get the initial size of the canvas DOM
188 { 189 {
189 canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w)); 190 canvasWidth_ = static_cast<unsigned int>(boost::math::iround(w));
190 canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h)); 191 canvasHeight_ = static_cast<unsigned int>(boost::math::iround(h));
191 } 192 }
192 193
193 emscripten_set_canvas_element_size(canvas_.c_str(), canvasWidth_, canvasHeight_); 194 emscripten_set_canvas_element_size(canvasSelector_.c_str(), canvasWidth_, canvasHeight_);
194 } 195 }
195 }; 196 };
196 197
197 198
198 WebAssemblyOpenGLContext::WebAssemblyOpenGLContext(const std::string& canvas) : 199 WebAssemblyOpenGLContext::WebAssemblyOpenGLContext(const std::string& canvasSelector) :
199 pimpl_(new PImpl(canvas)) 200 pimpl_(new PImpl(canvasSelector))
200 { 201 {
201 } 202 }
202 203
203 bool WebAssemblyOpenGLContext::IsContextLost() 204 bool WebAssemblyOpenGLContext::IsContextLost()
204 { 205 {
243 { 244 {
244 assert(pimpl_.get() != NULL); 245 assert(pimpl_.get() != NULL);
245 pimpl_->UpdateSize(); 246 pimpl_->UpdateSize();
246 } 247 }
247 248
248 const std::string& WebAssemblyOpenGLContext::GetCanvasIdentifier() const 249 const std::string& WebAssemblyOpenGLContext::GetCanvasSelector() const
249 { 250 {
250 assert(pimpl_.get() != NULL); 251 assert(pimpl_.get() != NULL);
251 return pimpl_->GetCanvasIdentifier(); 252 return pimpl_->GetCanvasSelector();
252 } 253 }
253 } 254 }
254 } 255 }