Mercurial > hg > orthanc-stone
comparison Framework/Viewport/WebAssemblyViewport.cpp @ 942:685c9a2d115f
Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 05 Aug 2019 12:27:27 +0200 |
parents | a6c12fe88bcb |
children | 1091b2adeb5a |
comparison
equal
deleted
inserted
replaced
939:ab90628e70d9 | 942:685c9a2d115f |
---|---|
11 * | 11 * |
12 * This program is distributed in the hope that it will be useful, but | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | 15 * Affero General Public License for more details. |
16 * | 16 * |
17 * You should have received a copy of the GNU Affero General Public License | 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/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
24 | 24 |
25 namespace OrthancStone | 25 namespace OrthancStone |
26 { | 26 { |
27 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) : | 27 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) : |
28 WebAssemblyViewport(canvas), | 28 WebAssemblyViewport(canvas), |
29 context_(canvas), | 29 context_(canvas) |
30 compositor_(context_, GetScene()) | |
31 { | 30 { |
31 compositor_.reset(new OpenGLCompositor(context_, GetScene())); | |
32 RegisterContextCallbacks(); | |
32 } | 33 } |
33 | 34 |
34 | |
35 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas, | 35 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas, |
36 boost::shared_ptr<Scene2D>& scene) : | 36 boost::shared_ptr<Scene2D>& scene) : |
37 WebAssemblyViewport(canvas, scene), | 37 WebAssemblyViewport(canvas, scene), |
38 context_(canvas), | 38 context_(canvas) |
39 compositor_(context_, GetScene()) | |
40 { | 39 { |
40 compositor_.reset(new OpenGLCompositor(context_, GetScene())); | |
41 RegisterContextCallbacks(); | |
41 } | 42 } |
42 | |
43 | 43 |
44 void WebAssemblyOpenGLViewport::UpdateSize() | 44 void WebAssemblyOpenGLViewport::UpdateSize() |
45 { | 45 { |
46 context_.UpdateSize(); // First read the size of the canvas | 46 context_.UpdateSize(); // First read the size of the canvas |
47 compositor_.Refresh(); // Then refresh the content of the canvas | 47 compositor_->Refresh(); // Then refresh the content of the canvas |
48 } | 48 } |
49 | 49 |
50 /* | |
51 typedef EM_BOOL (*em_webgl_context_callback)(int eventType, const void *reserved, void *userData); | |
52 | |
53 EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED | |
54 | |
55 EMSCRIPTEN_RESULT emscripten_set_webglcontextlost_callback( | |
56 const char *target, void *userData, EM_BOOL useCapture, em_webgl_context_callback callback) | |
57 | |
58 EMSCRIPTEN_RESULT emscripten_set_webglcontextrestored_callback( | |
59 const char *target, void *userData, EM_BOOL useCapture, em_webgl_context_callback callback) | |
60 | |
61 */ | |
62 | |
63 EM_BOOL WebAssemblyOpenGLViewport_OpenGLContextLost_callback( | |
64 int eventType, const void* reserved, void* userData) | |
65 { | |
66 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST); | |
67 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData); | |
68 return viewport->OpenGLContextLost(); | |
69 } | |
70 | |
71 EM_BOOL WebAssemblyOpenGLViewport_OpenGLContextRestored_callback( | |
72 int eventType, const void* reserved, void* userData) | |
73 { | |
74 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED); | |
75 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData); | |
76 return viewport->OpenGLContextRestored(); | |
77 } | |
78 | |
79 void WebAssemblyOpenGLViewport::RegisterContextCallbacks() | |
80 { | |
81 // TODO: what's the impact of userCapture=true ? | |
82 const char* canvasId = GetCanvasIdentifier().c_str(); | |
83 void* that = reinterpret_cast<void*>(this); | |
84 EMSCRIPTEN_RESULT status = EMSCRIPTEN_RESULT_SUCCESS; | |
85 | |
86 status = emscripten_set_webglcontextlost_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextLost_callback); | |
87 if (status != EMSCRIPTEN_RESULT_SUCCESS) | |
88 { | |
89 std::stringstream ss; | |
90 ss << "Error while calling emscripten_set_webglcontextlost_callback for: \"" << GetCanvasIdentifier() << "\""; | |
91 std::string msg = ss.str(); | |
92 LOG(ERROR) << msg; | |
93 ORTHANC_ASSERT(false, msg.c_str()); | |
94 } | |
95 status = emscripten_set_webglcontextrestored_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextRestored_callback); | |
96 if (status != EMSCRIPTEN_RESULT_SUCCESS) | |
97 { | |
98 std::stringstream ss; | |
99 ss << "Error while calling emscripten_set_webglcontextrestored_callback for: \"" << GetCanvasIdentifier() << "\""; | |
100 std::string msg = ss.str(); | |
101 LOG(ERROR) << msg; | |
102 ORTHANC_ASSERT(false, msg.c_str()); | |
103 } | |
104 } | |
105 | |
106 bool WebAssemblyOpenGLViewport::OpenGLContextLost() | |
107 { | |
108 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextLost() for canvas: " << GetCanvasIdentifier(); | |
109 return false; | |
110 } | |
111 | |
112 bool WebAssemblyOpenGLViewport::OpenGLContextRestored() | |
113 { | |
114 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextRestored() for canvas: " << GetCanvasIdentifier(); | |
115 return false; | |
116 } | |
50 | 117 |
51 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) : | 118 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) : |
52 WebAssemblyViewport(canvas), | 119 WebAssemblyViewport(canvas), |
53 canvas_(canvas), | 120 canvas_(canvas), |
54 compositor_(GetScene(), 1024, 768) | 121 compositor_(GetScene(), 1024, 768) |
55 { | 122 { |
56 } | 123 } |
57 | 124 |
58 | |
59 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas, | 125 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas, |
60 boost::shared_ptr<Scene2D>& scene) : | 126 boost::shared_ptr<Scene2D>& scene) : |
61 WebAssemblyViewport(canvas, scene), | 127 WebAssemblyViewport(canvas, scene), |
62 canvas_(canvas), | 128 canvas_(canvas), |
63 compositor_(GetScene(), 1024, 768) | 129 compositor_(GetScene(), 1024, 768) |
64 { | 130 { |
65 } | 131 } |
66 | |
67 | 132 |
68 void WebAssemblyCairoViewport::UpdateSize() | 133 void WebAssemblyCairoViewport::UpdateSize() |
69 { | 134 { |
70 LOG(INFO) << "updating cairo viewport size"; | 135 LOG(INFO) << "updating cairo viewport size"; |
71 double w, h; | 136 double w, h; |
82 **/ | 147 **/ |
83 unsigned int canvasWidth = 0; | 148 unsigned int canvasWidth = 0; |
84 unsigned int canvasHeight = 0; | 149 unsigned int canvasHeight = 0; |
85 | 150 |
86 if (w > 0 || | 151 if (w > 0 || |
87 h > 0) | 152 h > 0) |
88 { | 153 { |
89 canvasWidth = static_cast<unsigned int>(boost::math::iround(w)); | 154 canvasWidth = static_cast<unsigned int>(boost::math::iround(w)); |
90 canvasHeight = static_cast<unsigned int>(boost::math::iround(h)); | 155 canvasHeight = static_cast<unsigned int>(boost::math::iround(h)); |
91 } | 156 } |
92 | 157 |