changeset 237:b4642964c355 am

SimpleViewer demo running both with SDL and Wasm
author am@osimis.io
date Wed, 20 Jun 2018 09:03:48 +0200
parents f73d722d98c8
children 126c9c0c9333
files .hgignore Applications/Samples/SampleApplicationBase.h Applications/Samples/SampleApplicationContext.cpp Applications/Samples/SampleApplicationContext.h Applications/Samples/SampleMainSdl.cpp Applications/Samples/SimpleViewerApplication.h Platforms/Wasm/CMakeLists.txt Platforms/Wasm/WasmWebService.h Platforms/Wasm/build-wasm.sh Platforms/Wasm/build-web.sh Platforms/Wasm/stone-framework-loader.ts
diffstat 11 files changed, 134 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Jun 19 16:06:32 2018 +0200
+++ b/.hgignore	Wed Jun 20 09:03:48 2018 +0200
@@ -1,2 +1,6 @@
 Platforms/Generic/CMakeLists.txt.user
 Platforms/Generic/ThirdPartyDownloads/
+Platforms/Wasm/CMakeLists.txt.user
+Platforms/Wasm/build/
+Pltaforms/Wasm/build-web/
+Platforms/Wasm/ThirdPartyDownloads/
--- a/Applications/Samples/SampleApplicationBase.h	Tue Jun 19 16:06:32 2018 +0200
+++ b/Applications/Samples/SampleApplicationBase.h	Wed Jun 20 09:03:48 2018 +0200
@@ -21,7 +21,11 @@
 
 #pragma once
 
+#if ORTHANC_ENABLE_SDL==1
 #include "../../Applications/Sdl/BasicSdlApplication.h"
+#else
+#include "../../Applications/Wasm/BasicWasmApplication.h"
+#endif
 #include "../../Framework/Viewport/WidgetViewport.h"
 #include "SampleApplicationContext.h"
 
@@ -30,12 +34,12 @@
   namespace Samples
   {
 
-#ifdef ORTHANC_ENABLE_SDL
+#if ORTHANC_ENABLE_SDL==1
     class SampleSdlApplicationBase : public BasicSdlApplication {
     protected:
       std::unique_ptr<SampleApplicationContext> context_;
     public:
-      BasicApplicationContext& CreateApplicationContext(Orthanc::WebServiceParameters& orthanc, WidgetViewport* centralViewport) {
+      virtual BasicApplicationContext& CreateApplicationContext(Orthanc::WebServiceParameters& orthanc, WidgetViewport* centralViewport) {
         context_.reset(new SampleApplicationContext(orthanc, centralViewport));
 
         return *context_;
@@ -44,6 +48,18 @@
 
     typedef SampleSdlApplicationBase SampleApplicationBase_;
 #else
+    class SampleWasmApplicationBase : public BasicWasmApplication {
+    protected:
+      std::unique_ptr<SampleApplicationContext> context_;
+    public:
+      virtual BasicApplicationContext& CreateApplicationContext(IWebService& orthancWebService, std::shared_ptr<WidgetViewport> centralViewport) {
+        context_.reset(new SampleApplicationContext(orthancWebService));
+        return *context_;
+      }
+
+    };
+
+    typedef SampleWasmApplicationBase SampleApplicationBase_;
 
 #endif
 
--- a/Applications/Samples/SampleApplicationContext.cpp	Tue Jun 19 16:06:32 2018 +0200
+++ b/Applications/Samples/SampleApplicationContext.cpp	Wed Jun 20 09:03:48 2018 +0200
@@ -23,12 +23,6 @@
 
 namespace OrthancStone
 {
-  SampleApplicationContext::SampleApplicationContext(Orthanc::WebServiceParameters& orthanc, WidgetViewport* centralViewport) :
-    BasicApplicationContext_(orthanc, centralViewport)
-  {
-  }
-
-
   SampleApplicationContext::~SampleApplicationContext()
   {
     for (Interactors::iterator it = interactors_.begin(); it != interactors_.end(); ++it)
--- a/Applications/Samples/SampleApplicationContext.h	Tue Jun 19 16:06:32 2018 +0200
+++ b/Applications/Samples/SampleApplicationContext.h	Wed Jun 20 09:03:48 2018 +0200
@@ -42,12 +42,10 @@
 {
 
 #if ORTHANC_ENABLE_SDL==1
-  typedef BasicSdlApplicationContext BasicApplicationContext_;
+  class SampleApplicationContext : public BasicSdlApplicationContext
 #else
-  typedef BasicWasmApplicationContext BasicApplicationContext_;
+  class SampleApplicationContext : public BasicWasmApplicationContext
 #endif
-
-  class SampleApplicationContext : public BasicApplicationContext_
   {
   private:
     typedef std::list<ISlicedVolume*>          SlicedVolumes;  // this is actually used by the samples and shall be moved to a SampleApplicationContext
@@ -60,7 +58,15 @@
 
   public:
 
-    SampleApplicationContext(Orthanc::WebServiceParameters& orthanc, WidgetViewport* centralViewport);
+#if ORTHANC_ENABLE_SDL==1
+    SampleApplicationContext(Orthanc::WebServiceParameters& orthanc, WidgetViewport* centralViewport)
+    : BasicSdlApplicationContext(orthanc, centralViewport) {
+    }
+#else
+    SampleApplicationContext(IWebService& webService)
+    : BasicWasmApplicationContext(webService) {
+    }
+#endif
 
     virtual ~SampleApplicationContext();
 
--- a/Applications/Samples/SampleMainSdl.cpp	Tue Jun 19 16:06:32 2018 +0200
+++ b/Applications/Samples/SampleMainSdl.cpp	Wed Jun 20 09:03:48 2018 +0200
@@ -18,45 +18,7 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  **/
 
-
-// The macro "ORTHANC_STONE_SAMPLE" must be set by the CMake script
-
-#if ORTHANC_STONE_SAMPLE == 1
-#include "EmptyApplication.h"
-typedef OrthancStone::Samples::EmptyApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 2
-#include "TestPatternApplication.h"
-typedef OrthancStone::Samples::TestPatternApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 3
-#include "SingleFrameApplication.h"
-typedef OrthancStone::Samples::SingleFrameApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 4
-#include "SingleVolumeApplication.h"
-typedef OrthancStone::Samples::SingleVolumeApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 5
-#include "BasicPetCtFusionApplication.h"
-typedef OrthancStone::Samples::BasicPetCtFusionApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 6
-#include "SynchronizedSeriesApplication.h"
-typedef OrthancStone::Samples::SynchronizedSeriesApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 7
-#include "LayoutPetCtFusionApplication.h"
-typedef OrthancStone::Samples::LayoutPetCtFusionApplication Application;
-
-#elif ORTHANC_STONE_SAMPLE == 8
-#include "SimpleViewerApplication.h"
-typedef OrthancStone::Samples::SimpleViewerApplication Application;
-
-#else
-#error Please set the ORTHANC_STONE_SAMPLE macro
-#endif
-
+#include "SampleList.h"
 
 int main(int argc, char* argv[]) 
 {
--- a/Applications/Samples/SimpleViewerApplication.h	Tue Jun 19 16:06:32 2018 +0200
+++ b/Applications/Samples/SimpleViewerApplication.h	Wed Jun 20 09:03:48 2018 +0200
@@ -209,19 +209,26 @@
       std::vector<LayerWidget*>       thumbnails_;
       std::vector<std::string>        instances_;
       unsigned int                    currentInstanceIndex_;
+      OrthancStone::WidgetViewport*                wasmViewport1_;
+      OrthancStone::WidgetViewport*                wasmViewport2_;
 
       OrthancFrameLayerSource*        source_;
       unsigned int                    slice_;
       
     public:
-      SimpleViewerApplication() :
+      SimpleViewerApplication(OrthancStone::WidgetViewport* wasmViewport1, OrthancStone::WidgetViewport* wasmViewport2) :
         mainLayout_(NULL),
         currentInstanceIndex_(0),
         source_(NULL),
-        slice_(0)
+        slice_(0),
+        wasmViewport1_(wasmViewport1),
+        wasmViewport2_(wasmViewport2)
       {
       }
       
+      virtual void Finalize() {}
+      virtual IWidget* GetCentralWidget() {return mainLayout_;}
+
       virtual void DeclareStartupOptions(boost::program_options::options_description& options)
       {
         boost::program_options::options_description generic("Sample options");
@@ -295,7 +302,13 @@
 
         mainLayout_->SetTransmitMouseOver(true);
         mainViewport_->SetInteractor(context_->AddInteractor(new Interactor(*this)));
+#if ORTHANC_ENABLE_SDL == 1
         context_->SetCentralWidget(mainLayout_);
+#else
+  wasmViewport1_->SetCentralWidget(thumbnailsLayout_);
+  wasmViewport2_->SetCentralWidget(mainViewport_);
+
+#endif
       }
     };
   }
--- a/Platforms/Wasm/CMakeLists.txt	Tue Jun 19 16:06:32 2018 +0200
+++ b/Platforms/Wasm/CMakeLists.txt	Wed Jun 20 09:03:48 2018 +0200
@@ -9,14 +9,15 @@
 #####################################################################
 
 set(WASM_FLAGS "-s WASM=1")
+set(WASM_MODULE_NAME "StoneFrameworkModule" CACHE STRING "Name of the WebAssembly module")
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WASM_FLAGS}")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${WASM_FLAGS}")
-set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --js-library ${CMAKE_SOURCE_DIR}/library.js")
+set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --js-library ${STONE_SOURCES_DIR}/Applications/Samples/samples-library.js --js-library ${STONE_SOURCES_DIR}/Platforms/Wasm/WasmWebService.js --js-library ${STONE_SOURCES_DIR}/Platforms/Wasm/default-library.js  -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]'")
 
 # Handling of memory
 #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_MEMORY_GROWTH=1")  # Resize
 #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s TOTAL_MEMORY=536870912")  # 512MB
-set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=536870912")  # 512MB + resize
+set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s EXPORT_NAME='\"${WASM_MODULE_NAME}\"' -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=536870912 -s TOTAL_STACK=128000000")  # 512MB + resize
 #set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=1073741824")  # 1GB + resize
 
 # To debug exceptions
@@ -38,12 +39,46 @@
 
 
 
+# Regenerate a dummy "WasmWebService.c" file each time the "WasmWebService.js" file
+# is modified, so as to force a new execution of the linking
+add_custom_command(
+    OUTPUT "${AUTOGENERATED_DIR}/WasmWebService.c"
+    COMMAND ${CMAKE_COMMAND} -E touch "${AUTOGENERATED_DIR}/WasmWebService.c" ""
+    DEPENDS "${ORTHANC_STONE_ROOT}/Platforms/WebAssembly/WasmWebService.js")
 
+add_custom_command(
+    OUTPUT "${AUTOGENERATED_DIR}/default-library.c"
+    COMMAND ${CMAKE_COMMAND} -E touch "${AUTOGENERATED_DIR}/default-library.c" ""
+    DEPENDS "${ORTHANC_STONE_ROOT}/Platforms/WebAssembly/default-library.js")
+
+
+#####################################################################
+## Build all the sample applications
+#####################################################################
+
+include_directories(${ORTHANC_STONE_ROOT})
 
 
-# Regenerate a dummy "library.c" file each time the "library.js" file
-# is modified, so as to force a new execution of the linking
-add_custom_command(
-    OUTPUT "${AUTOGENERATED_DIR}/library.c"
-    COMMAND ${CMAKE_COMMAND} -E touch "${AUTOGENERATED_DIR}/library.c" ""
-    DEPENDS "${CMAKE_SOURCE_DIR}/library.js")
+macro(BuildSample Target Header Sample)
+  add_executable(${Target}
+    ${ORTHANC_STONE_ROOT}/Platforms/Wasm/Defaults.cpp
+    ${ORTHANC_STONE_ROOT}/Platforms/Wasm/WasmWebService.cpp
+
+    ${ORTHANC_STONE_ROOT}/Applications/Samples/SampleMainWasm.cpp
+    ${ORTHANC_STONE_ROOT}/Applications/Samples/SampleApplicationContext.cpp
+    ${ORTHANC_STONE_ROOT}/Applications/Samples/SampleInteractor.h
+    ${ORTHANC_STONE_ROOT}/Applications/Samples/SampleApplicationBase.h
+    ${ORTHANC_STONE_ROOT}/Applications/Samples/${Header}
+    )
+  set_target_properties(${Target} PROPERTIES COMPILE_DEFINITIONS ORTHANC_STONE_SAMPLE=${Sample})
+  target_link_libraries(${Target} OrthancStone)
+endmacro()
+
+#BuildSample(OrthancStoneEmpty EmptyApplication.h 1)
+#BuildSample(OrthancStoneTestPattern TestPatternApplication.h 2)
+#BuildSample(OrthancStoneSingleFrame SingleFrameApplication.h 3)
+#BuildSample(OrthancStoneSingleVolume SingleVolumeApplication.h 4)
+#BuildSample(OrthancStoneBasicPetCtFusion 5)
+#BuildSample(OrthancStoneSynchronizedSeries 6)
+#BuildSample(OrthancStoneLayoutPetCtFusion 7)
+BuildSample(OrthancStoneSimpleViewer SimpleViewerApplication.h 8)
--- a/Platforms/Wasm/WasmWebService.h	Tue Jun 19 16:06:32 2018 +0200
+++ b/Platforms/Wasm/WasmWebService.h	Wed Jun 20 09:03:48 2018 +0200
@@ -11,7 +11,7 @@
 
     // Private constructor => Singleton design pattern
     WasmWebService() :
-      base_("../../")
+      base_("../../")   // TODO: this should be configurable from the JS code !
     {
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platforms/Wasm/build-wasm.sh	Wed Jun 20 09:03:48 2018 +0200
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+currentDir=$(pwd)
+wasmRootDir=$(pwd)
+
+mkdir -p $wasmRootDir/build
+cd $wasmRootDir/build
+
+source ~/Downloads/emsdk/emsdk_env.sh
+cmake -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake .. -DCMAKE_BUILD_TYPE=Release -DSTONE_SOURCES_DIR=$currentDir/../../../orthanc-stone -DORTHANC_FRAMEWORK_SOURCE=path -DORTHANC_FRAMEWORK_ROOT=$currentDir/../../../orthanc -DALLOW_DOWNLOADS=ON -DSTATIC_BUILD=OFF
+make -j 5
+
+echo "-- building the web application -- "
+cd $currentDir
+#./build-web.sh
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platforms/Wasm/build-web.sh	Wed Jun 20 09:03:48 2018 +0200
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# this script currently assumes that the wasm code has been built on its side and is availabie in Wasm/build/
+
+currentDir=$(pwd)
+wasmRootDir=$(pwd)
+samplesRootDir=$(pwd)/../../Applications/Samples/
+
+outputDir=$wasmRootDir/build-web/
+mkdir -p $outputDir
+
+cp $samplesRootDir/Web/index.html $outputDir
+cp $samplesRootDir/Web/samples-styles.css $outputDir
+
+cp $samplesRootDir/Web/simple-viewer.html $outputDir
+tsc --allowJs --project $samplesRootDir/Web/tsconfig-simple-viewer.json
+cp $currentDir/build/OrthancStoneSimpleViewer.js  $outputDir
+cp $currentDir/build/OrthancStoneSimpleViewer.wasm  $outputDir
+
+
+# cat ../wasm/build/wasm-app.js $currentDir/../../../orthanc-stone/Platforms/WebAssembly/defaults.js > $outputDir/app.js
+
+cd $currentDir
+
--- a/Platforms/Wasm/stone-framework-loader.ts	Tue Jun 19 16:06:32 2018 +0200
+++ b/Platforms/Wasm/stone-framework-loader.ts	Wed Jun 20 09:03:48 2018 +0200
@@ -82,7 +82,8 @@
         // Dynamic loading of the JavaScript wrapper around WebAssembly
         var script = document.createElement('script');
         script.type = 'application/javascript';
-        script.src = "orthanc-stone.js"; // ASSETS_FOLDER + '/' + WASM_FILENAME + '.js';
+        //script.src = "orthanc-stone.js"; // ASSETS_FOLDER + '/' + WASM_FILENAME + '.js';
+        script.src = "OrthancStoneSimpleViewer.js"; // ASSETS_FOLDER + '/' + WASM_FILENAME + '.js';
         script.async = true;
         document.head.appendChild(script);
       }