Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Platforms/Sdl/SdlViewport.h @ 1899:917500c46fe0
moved the Platform folder from the Applications folder to the Stone library itself
author | Alain Mazy <am@osimis.io> |
---|---|
date | Sat, 29 Jan 2022 12:47:32 +0100 |
parents | |
children | 184b0aeae1af |
comparison
equal
deleted
inserted
replaced
1898:a5e54bd87b25 | 1899:917500c46fe0 |
---|---|
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-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 #pragma once | |
24 | |
25 #if !defined(ORTHANC_ENABLE_SDL) | |
26 # error Macro ORTHANC_ENABLE_SDL must be defined | |
27 #endif | |
28 | |
29 #if ORTHANC_ENABLE_SDL != 1 | |
30 # error SDL must be enabled to use this file | |
31 #endif | |
32 | |
33 #if !defined(ORTHANC_ENABLE_OPENGL) | |
34 # error The macro ORTHANC_ENABLE_OPENGL must be defined | |
35 #endif | |
36 | |
37 #include "SdlOpenGLContext.h" | |
38 #include "../../../OrthancStone/Sources/Scene2D/CairoCompositor.h" | |
39 #include "../../../OrthancStone/Sources/Viewport/IViewport.h" | |
40 | |
41 #if ORTHANC_ENABLE_OPENGL == 1 | |
42 # include "../../../OrthancStone/Sources/Scene2D/OpenGLCompositor.h" | |
43 #endif | |
44 | |
45 #include <SDL_events.h> | |
46 | |
47 // TODO: required for UndoStack injection | |
48 // I don't like it either :) | |
49 #include <boost/weak_ptr.hpp> | |
50 | |
51 #include <boost/thread/recursive_mutex.hpp> | |
52 #include <boost/enable_shared_from_this.hpp> | |
53 | |
54 namespace OrthancStone | |
55 { | |
56 class UndoStack; | |
57 | |
58 class SdlViewport : public IViewport, | |
59 public boost::enable_shared_from_this<SdlViewport> | |
60 { | |
61 private: | |
62 boost::recursive_mutex mutex_; | |
63 uint32_t refreshEvent_; | |
64 boost::shared_ptr<ViewportController> controller_; | |
65 std::unique_ptr<ICompositor> compositor_; | |
66 | |
67 void SendRefreshEvent(); | |
68 | |
69 protected: | |
70 class SdlLock : public ILock | |
71 { | |
72 private: | |
73 SdlViewport& that_; | |
74 boost::recursive_mutex::scoped_lock lock_; | |
75 | |
76 public: | |
77 explicit SdlLock(SdlViewport& that) : | |
78 that_(that), | |
79 lock_(that.mutex_) | |
80 { | |
81 } | |
82 | |
83 virtual bool HasCompositor() const ORTHANC_OVERRIDE | |
84 { | |
85 return true; | |
86 } | |
87 | |
88 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE; | |
89 | |
90 virtual ViewportController& GetController() ORTHANC_OVERRIDE | |
91 { | |
92 return *that_.controller_; | |
93 } | |
94 | |
95 virtual void Invalidate() ORTHANC_OVERRIDE | |
96 { | |
97 that_.SendRefreshEvent(); | |
98 } | |
99 | |
100 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE | |
101 { | |
102 that_.RefreshCanvasSize(); | |
103 } | |
104 }; | |
105 | |
106 void ClearCompositor() | |
107 { | |
108 compositor_.reset(); | |
109 } | |
110 | |
111 void AcquireCompositor(ICompositor* compositor /* takes ownership */); | |
112 | |
113 virtual void RefreshCanvasSize() = 0; | |
114 | |
115 protected: | |
116 SdlViewport(); | |
117 | |
118 void PostConstructor(); | |
119 | |
120 public: | |
121 bool IsRefreshEvent(const SDL_Event& event) const | |
122 { | |
123 return (event.type == refreshEvent_); | |
124 } | |
125 | |
126 virtual ILock* Lock() ORTHANC_OVERRIDE | |
127 { | |
128 return new SdlLock(*this); | |
129 } | |
130 | |
131 virtual uint32_t GetSdlWindowId() = 0; | |
132 | |
133 void UpdateSize(unsigned int width, | |
134 unsigned int height); | |
135 | |
136 virtual void ToggleMaximize() = 0; | |
137 | |
138 // Must be invoked from the main SDL thread | |
139 virtual void Paint() = 0; | |
140 }; | |
141 | |
142 | |
143 #if ORTHANC_ENABLE_OPENGL == 1 | |
144 class SdlOpenGLViewport : public SdlViewport | |
145 { | |
146 private: | |
147 SdlOpenGLContext context_; | |
148 | |
149 SdlOpenGLViewport(const std::string& title, | |
150 unsigned int width, | |
151 unsigned int height, | |
152 bool allowDpiScaling = true); | |
153 | |
154 protected: | |
155 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE; | |
156 | |
157 public: | |
158 static boost::shared_ptr<SdlOpenGLViewport> Create(const std::string& title, | |
159 unsigned int width, | |
160 unsigned int height, | |
161 bool allowDpiScaling = true); | |
162 | |
163 | |
164 virtual ~SdlOpenGLViewport(); | |
165 | |
166 virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; | |
167 | |
168 virtual void Paint() ORTHANC_OVERRIDE; | |
169 | |
170 virtual void ToggleMaximize() ORTHANC_OVERRIDE; | |
171 }; | |
172 #endif | |
173 | |
174 | |
175 class SdlCairoViewport : public SdlViewport | |
176 { | |
177 private: | |
178 SdlWindow window_; | |
179 SDL_Surface* sdlSurface_; | |
180 | |
181 void CreateSdlSurfaceFromCompositor(const CairoCompositor& compositor); | |
182 | |
183 SdlCairoViewport(const std::string& title, | |
184 unsigned int width, | |
185 unsigned int height, | |
186 bool allowDpiScaling); | |
187 | |
188 protected: | |
189 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE; | |
190 | |
191 public: | |
192 static boost::shared_ptr<SdlCairoViewport> Create(const std::string& title, | |
193 unsigned int width, | |
194 unsigned int height, | |
195 bool allowDpiScaling = true); | |
196 | |
197 virtual ~SdlCairoViewport(); | |
198 | |
199 virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; | |
200 | |
201 virtual void Paint() ORTHANC_OVERRIDE; | |
202 | |
203 virtual void ToggleMaximize() ORTHANC_OVERRIDE; | |
204 }; | |
205 } |