comparison Platforms/WebAssembly/Defaults.cpp @ 227:c8f11437a6fd am

getting ready for multiple viewports
author am@osimis.io
date Thu, 14 Jun 2018 13:28:40 +0200
parents d30a10d574ec
children 210c1ce8e1a6
comparison
equal deleted inserted replaced
226:1fa4c65c7e1b 227:c8f11437a6fd
15 static std::shared_ptr<OrthancStone::WidgetViewport> viewport_; 15 static std::shared_ptr<OrthancStone::WidgetViewport> viewport_;
16 static OrthancStone::ChangeObserver changeObserver_; 16 static OrthancStone::ChangeObserver changeObserver_;
17 static OrthancStone::StatusBar statusBar_; 17 static OrthancStone::StatusBar statusBar_;
18 18
19 19
20 static std::list<std::shared_ptr<OrthancStone::WidgetViewport>> viewports_;
21
20 #ifdef __cplusplus 22 #ifdef __cplusplus
21 extern "C" { 23 extern "C" {
22 #endif 24 #endif
23 25
24 using namespace OrthancStone; 26 using namespace OrthancStone;
25 27
26 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication() { 28 // when WASM needs a C++ viewport
29 ViewportHandle EMSCRIPTEN_KEEPALIVE CreateCppViewport() {
30
31 std::shared_ptr<OrthancStone::WidgetViewport> viewport(new OrthancStone::WidgetViewport);
32 printf("viewport %x\n", viewport.get());
33
34 viewports_.push_back(viewport);
35
36 printf("There are now %d viewports in C++\n", viewports_.size());
37
38 viewport->SetStatusBar(statusBar_);
39 viewport->Register(changeObserver_);
40
41 // TODO: remove once we really want to handle multiple viewports
42 viewport_ = viewport;
43 return viewport.get();
44 }
45
46 // when WASM does not need a viewport anymore, it should release it
47 void EMSCRIPTEN_KEEPALIVE ReleaseCppViewport(ViewportHandle viewport) {
48 viewports_.remove_if([viewport](const std::shared_ptr<OrthancStone::WidgetViewport>& v) { return v.get() == viewport;});
49
50 printf("There are now %d viewports in C++\n", viewports_.size());
51 }
52
53
54 void EMSCRIPTEN_KEEPALIVE CreateWasmApplication(ViewportHandle viewport) {
27 55
28 printf("CreateWasmApplication\n"); 56 printf("CreateWasmApplication\n");
29 viewport_.reset(new OrthancStone::WidgetViewport);
30 viewport_->SetStatusBar(statusBar_);
31 viewport_->Register(changeObserver_);
32
33 57
34 application.reset(CreateUserApplication()); 58 application.reset(CreateUserApplication());
35 59
36 boost::program_options::options_description options; 60 boost::program_options::options_description options;
37 application->DeclareStartupOptions(options); 61 application->DeclareStartupOptions(options);
120 { 144 {
121 viewport_->SetSize(width, height); 145 viewport_->SetSize(width, height);
122 } 146 }
123 } 147 }
124 148
125 int EMSCRIPTEN_KEEPALIVE ViewportRender(unsigned int width, 149 int EMSCRIPTEN_KEEPALIVE ViewportRender(OrthancStone::WidgetViewport* viewport,
150 unsigned int width,
126 unsigned int height, 151 unsigned int height,
127 uint8_t* data) 152 uint8_t* data)
128 { 153 {
129 changeObserver_.Reset(); 154 changeObserver_.Reset();
130 155
136 } 161 }
137 162
138 Orthanc::ImageAccessor surface; 163 Orthanc::ImageAccessor surface;
139 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data); 164 surface.AssignWritable(Orthanc::PixelFormat_BGRA32, width, height, 4 * width, data);
140 165
141 if (viewport_.get() != NULL) 166 viewport->Render(surface);
142 {
143 viewport_->Render(surface);
144 }
145 167
146 // Convert from BGRA32 memory layout (only color mode supported by 168 // Convert from BGRA32 memory layout (only color mode supported by
147 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as 169 // Cairo, which corresponds to CAIRO_FORMAT_ARGB32) to RGBA32 (as
148 // expected by HTML5 canvas). This simply amounts to swapping the 170 // expected by HTML5 canvas). This simply amounts to swapping the
149 // B and R channels. 171 // B and R channels.