Mercurial > hg > orthanc-stone
comparison Samples/WebAssembly/RtViewer/RtViewerWasm.cpp @ 1393:27e0a00bd3e8
RtViewer SingleFrameViewer OK : wasm SDL single viewport
other viewports ongoing
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Wed, 29 Apr 2020 15:54:18 +0200 |
parents | 24bcff8ea58f |
children | 3e644f6fadd4 |
comparison
equal
deleted
inserted
replaced
1392:ffdb82850e98 | 1393:27e0a00bd3e8 |
---|---|
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 #include "RtViewer.h" | |
22 #include "SampleHelpers.h" | |
23 | |
24 // Stone of Orthanc includes | |
25 #include <Framework/Loaders/WebAssemblyLoadersContext.h> | |
26 //#include <Framework/OpenGL/WebAssemblyOpenGLContext.h> | |
27 #include <Framework/Viewport/WebGLViewport.h> | |
28 #include <Framework/StoneException.h> | |
29 #include <Framework/StoneInitialization.h> | |
30 | |
31 #include <Framework/Loaders/WebAssemblyLoadersContext.h> | |
32 | |
33 #include <Framework/StoneException.h> | |
34 #include <Framework/StoneInitialization.h> | |
35 | |
36 #include <Core/Toolbox.h> | |
37 | |
38 #include <boost/program_options.hpp> | |
39 #include <boost/shared_ptr.hpp> | |
40 // #include <boost/pointer_cast.hpp> this include might be necessary in more recent boost versions | |
41 | |
42 #include <emscripten.h> | |
43 #include <emscripten/html5.h> | |
44 | |
45 | |
46 #define DISPATCH_JAVASCRIPT_EVENT(name) \ | |
47 EM_ASM( \ | |
48 const customEvent = document.createEvent("CustomEvent"); \ | |
49 customEvent.initCustomEvent(name, false, false, undefined); \ | |
50 window.dispatchEvent(customEvent); \ | |
51 ); | |
52 | |
53 #define EXTERN_CATCH_EXCEPTIONS \ | |
54 catch (Orthanc::OrthancException& e) \ | |
55 { \ | |
56 LOG(ERROR) << "OrthancException: " << e.What(); \ | |
57 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
58 } \ | |
59 catch (OrthancStone::StoneException& e) \ | |
60 { \ | |
61 LOG(ERROR) << "StoneException: " << e.What(); \ | |
62 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
63 } \ | |
64 catch (std::exception& e) \ | |
65 { \ | |
66 LOG(ERROR) << "Runtime error: " << e.what(); \ | |
67 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
68 } \ | |
69 catch (...) \ | |
70 { \ | |
71 LOG(ERROR) << "Native exception"; \ | |
72 DISPATCH_JAVASCRIPT_EVENT("StoneException"); \ | |
73 } | |
74 | |
75 namespace OrthancStone | |
76 { | |
77 void RtViewerApp::CreateViewport() | |
78 { | |
79 viewport_ = WebGLViewport::Create("mycanvas1"); | |
80 } | |
81 | |
82 void RtViewerApp::TakeScreenshot(const std::string& target, | |
83 unsigned int canvasWidth, | |
84 unsigned int canvasHeight) | |
85 { | |
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
87 } | |
88 | |
89 | |
90 void RtViewerApp::RunWasm() | |
91 { | |
92 { | |
93 std::unique_ptr<IViewport::ILock> lock(viewport_->Lock()); | |
94 ViewportController& controller = lock->GetController(); | |
95 Scene2D& scene = controller.GetScene(); | |
96 ICompositor& compositor = lock->GetCompositor(); | |
97 | |
98 controller.FitContent(compositor.GetCanvasWidth(), compositor.GetCanvasHeight()); | |
99 | |
100 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, | |
101 FONT_SIZE_0, Orthanc::Encoding_Latin1); | |
102 compositor.SetFont(1, Orthanc::EmbeddedResources::UBUNTU_FONT, | |
103 FONT_SIZE_1, Orthanc::Encoding_Latin1); | |
104 } | |
105 | |
106 loadersContext_.reset(new OrthancStone::WebAssemblyLoadersContext(1, 4, 1)); | |
107 | |
108 // we are in WASM --> downcast to concrete type | |
109 boost::shared_ptr<WebAssemblyLoadersContext> loadersContext = | |
110 boost::dynamic_pointer_cast<WebAssemblyLoadersContext>(loadersContext_); | |
111 | |
112 if (HasArgument("orthanc")) | |
113 loadersContext->SetLocalOrthanc(GetArgument("orthanc")); | |
114 else | |
115 loadersContext->SetLocalOrthanc(".."); | |
116 | |
117 loadersContext->SetDicomCacheSize(128 * 1024 * 1024); // 128MB | |
118 | |
119 PrepareLoadersAndSlicers(); | |
120 | |
121 DefaultViewportInteractor interactor; | |
122 } | |
123 } | |
124 | |
125 extern "C" | |
126 { | |
127 boost::shared_ptr<OrthancStone::RtViewerApp> g_app; | |
128 | |
129 int main(int argc, char const *argv[]) | |
130 { | |
131 try | |
132 { | |
133 OrthancStone::StoneInitialize(); | |
134 Orthanc::Logging::Initialize(); | |
135 Orthanc::Logging::EnableTraceLevel(true); | |
136 | |
137 LOG(WARNING) << "Initializing native Stone"; | |
138 | |
139 LOG(WARNING) << "Compiled with Emscripten " << __EMSCRIPTEN_major__ | |
140 << "." << __EMSCRIPTEN_minor__ | |
141 << "." << __EMSCRIPTEN_tiny__; | |
142 | |
143 LOG(INFO) << "Endianness: " << Orthanc::EnumerationToString(Orthanc::Toolbox::DetectEndianness()); | |
144 | |
145 g_app = OrthancStone::RtViewerApp::Create(); | |
146 | |
147 DISPATCH_JAVASCRIPT_EVENT("WasmModuleInitialized"); | |
148 } | |
149 EXTERN_CATCH_EXCEPTIONS; | |
150 } | |
151 | |
152 EMSCRIPTEN_KEEPALIVE | |
153 void Initialize(const char* canvasId) | |
154 { | |
155 try | |
156 { | |
157 g_app->RunWasm(); | |
158 } | |
159 EXTERN_CATCH_EXCEPTIONS; | |
160 } | |
161 | |
162 EMSCRIPTEN_KEEPALIVE | |
163 void SetArgument(const char* key, const char* value) | |
164 { | |
165 // This is called for each GET argument (cf. "app.js") | |
166 LOG(INFO) << "Received GET argument: [" << key << "] = [" << value << "]"; | |
167 g_app->SetArgument(key, value); | |
168 } | |
169 | |
170 } |