comparison Framework/Viewport/WebAssemblyViewport.h @ 1232:a28861abf888 broker

viewports for WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 09 Dec 2019 17:46:33 +0100
parents 391fb6d6905d
children a4bb8c2dd211
comparison
equal deleted inserted replaced
1229:b9f2a111c5b9 1232:a28861abf888
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
22 #pragma once 22 #pragma once
23 23
24 #include "../OpenGL/WebAssemblyOpenGLContext.h" 24 #if !defined(ORTHANC_ENABLE_WASM)
25 #include "../Scene2D/OpenGLCompositor.h" 25 # error Macro ORTHANC_ENABLE_WASM must be defined
26 #include "../Scene2D/CairoCompositor.h" 26 #endif
27 #include "ViewportBase.h" 27
28 #if ORTHANC_ENABLE_WASM != 1
29 # error This file can only be used if targeting WebAssembly
30 #endif
31
32 #include "IViewport.h"
33
34 #include <emscripten.h>
35 #include <emscripten/html5.h>
28 36
29 namespace OrthancStone 37 namespace OrthancStone
30 { 38 {
31 class WebAssemblyViewport : public ViewportBase 39 class WebAssemblyViewport : public IViewport
32 { 40 {
33 private: 41 private:
34 std::string canvasIdentifier_; 42 class WasmLock;
43
44 std::string shortCanvasId_;
45 std::string fullCanvasId_;
46 std::auto_ptr<ICompositor> compositor_;
47 boost::shared_ptr<ViewportController> controller_;
48 std::auto_ptr<IViewportInteractor> interactor_;
49
50 static EM_BOOL OnRequestAnimationFrame(double time, void *userData);
51
52 static EM_BOOL OnResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData);
53
54 static EM_BOOL OnMouseDown(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
55
56 static EM_BOOL OnMouseMove(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
57
58 static EM_BOOL OnMouseUp(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
59
60 protected:
61 void Invalidate();
62
63 void ClearCompositor()
64 {
65 compositor_.reset();
66 }
67
68 bool HasCompositor() const
69 {
70 return compositor_.get() != NULL;
71 }
72
73 void AcquireCompositor(ICompositor* compositor /* takes ownership */);
74
75 virtual void Paint(ICompositor& compositor,
76 ViewportController& controller) = 0;
77
78 virtual void UpdateSize(ICompositor& compositor) = 0;
35 79
36 public: 80 public:
37 WebAssemblyViewport(const std::string& canvasIdentifier) : 81 WebAssemblyViewport(const std::string& canvasId,
38 canvasIdentifier_(canvasIdentifier) 82 const Scene2D* scene);
83
84 virtual ILock* Lock() ORTHANC_OVERRIDE;
85
86 void AcquireInteractor(IViewportInteractor* interactor);
87
88 const std::string& GetShortCanvasId() const
39 { 89 {
90 return shortCanvasId_;
40 } 91 }
41 92
42 WebAssemblyViewport(const std::string& canvasIdentifier, 93 const std::string& GetFullCanvasId() const
43 boost::shared_ptr<Scene2D>& scene) :
44 ViewportBase(scene),
45 canvasIdentifier_(canvasIdentifier)
46 { 94 {
47 } 95 return fullCanvasId_;
48
49 const std::string& GetCanvasIdentifier() const
50 {
51 return canvasIdentifier_;
52 }
53 };
54
55
56 class WebAssemblyOpenGLViewport : public WebAssemblyViewport
57 {
58 private:
59 OpenGL::WebAssemblyOpenGLContext context_;
60 std::auto_ptr<OpenGLCompositor> compositor_;
61
62 public:
63 WebAssemblyOpenGLViewport(const std::string& canvas);
64
65 WebAssemblyOpenGLViewport(const std::string& canvas,
66 boost::shared_ptr<Scene2D>& scene);
67
68 // This function must be called each time the browser window is resized
69 void UpdateSize();
70
71 virtual bool HasCompositor() const ORTHANC_OVERRIDE
72 {
73 return (compositor_.get() != NULL);
74 }
75
76 bool IsContextLost()
77 {
78 return context_.IsContextLost();
79 }
80
81 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE;
82
83 virtual void Refresh() ORTHANC_OVERRIDE;
84
85 // this does NOT return whether the context is lost! This is called to
86 // tell Stone that the context has been lost
87 bool OpenGLContextLost();
88
89 // This should be called to indicate that the context has been lost
90 bool OpenGLContextRestored();
91
92 private:
93 void DisableCompositor();
94 void RestoreCompositor();
95
96 void RegisterContextCallbacks();
97 };
98
99
100 class WebAssemblyCairoViewport : public WebAssemblyViewport
101 {
102 private:
103 CairoCompositor compositor_;
104 std::string canvas_;
105
106 public:
107 WebAssemblyCairoViewport(const std::string& canvas);
108
109 WebAssemblyCairoViewport(const std::string& canvas,
110 boost::shared_ptr<Scene2D>& scene);
111
112 void UpdateSize();
113
114 virtual void Refresh() ORTHANC_OVERRIDE;
115
116 virtual bool HasCompositor() const ORTHANC_OVERRIDE
117 {
118 return true;
119 }
120
121 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE
122 {
123 return compositor_;
124 } 96 }
125 }; 97 };
126 } 98 }