Mercurial > hg > orthanc-stone
comparison Applications/Platforms/Sdl/SdlViewport.h @ 1591:5887a4f8594b
moving platform-specific files out of the "OrthancStone" folder
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 23 Oct 2020 13:15:03 +0200 |
parents | OrthancStone/Sources/Viewport/SdlViewport.h@92fca2b3ba3d |
children | 9ac2a65d4172 |
comparison
equal
deleted
inserted
replaced
1590:7b963bccafef | 1591:5887a4f8594b |
---|---|
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 #pragma once | |
22 | |
23 #if !defined(ORTHANC_ENABLE_SDL) | |
24 # error Macro ORTHANC_ENABLE_SDL must be defined | |
25 #endif | |
26 | |
27 #if ORTHANC_ENABLE_SDL != 1 | |
28 # error SDL must be enabled to use this file | |
29 #endif | |
30 | |
31 #if !defined(ORTHANC_ENABLE_OPENGL) | |
32 # error The macro ORTHANC_ENABLE_OPENGL must be defined | |
33 #endif | |
34 | |
35 #if ORTHANC_ENABLE_OPENGL != 1 | |
36 # error Support for OpenGL is disabled | |
37 #endif | |
38 | |
39 #include "SdlOpenGLContext.h" | |
40 #include "../../../OrthancStone/Sources/Scene2D/OpenGLCompositor.h" | |
41 #include "../../../OrthancStone/Sources/Scene2D/CairoCompositor.h" | |
42 #include "../../../OrthancStone/Sources/Viewport/IViewport.h" | |
43 | |
44 #include <SDL_events.h> | |
45 | |
46 // TODO: required for UndoStack injection | |
47 // I don't like it either :) | |
48 #include <boost/weak_ptr.hpp> | |
49 | |
50 #include <boost/thread/recursive_mutex.hpp> | |
51 #include <boost/enable_shared_from_this.hpp> | |
52 | |
53 namespace OrthancStone | |
54 { | |
55 class UndoStack; | |
56 | |
57 class SdlViewport : public IViewport, | |
58 public boost::enable_shared_from_this<SdlViewport> | |
59 { | |
60 private: | |
61 boost::recursive_mutex mutex_; | |
62 uint32_t refreshEvent_; | |
63 boost::shared_ptr<ViewportController> controller_; | |
64 std::unique_ptr<ICompositor> compositor_; | |
65 | |
66 void SendRefreshEvent(); | |
67 | |
68 protected: | |
69 class SdlLock : public ILock | |
70 { | |
71 private: | |
72 SdlViewport& that_; | |
73 boost::recursive_mutex::scoped_lock lock_; | |
74 | |
75 public: | |
76 explicit SdlLock(SdlViewport& that) : | |
77 that_(that), | |
78 lock_(that.mutex_) | |
79 { | |
80 } | |
81 | |
82 virtual bool HasCompositor() const ORTHANC_OVERRIDE | |
83 { | |
84 return true; | |
85 } | |
86 | |
87 virtual ICompositor& GetCompositor() ORTHANC_OVERRIDE; | |
88 | |
89 virtual ViewportController& GetController() ORTHANC_OVERRIDE | |
90 { | |
91 return *that_.controller_; | |
92 } | |
93 | |
94 virtual void Invalidate() ORTHANC_OVERRIDE | |
95 { | |
96 that_.SendRefreshEvent(); | |
97 } | |
98 | |
99 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE | |
100 { | |
101 that_.RefreshCanvasSize(); | |
102 } | |
103 }; | |
104 | |
105 void ClearCompositor() | |
106 { | |
107 compositor_.reset(); | |
108 } | |
109 | |
110 void AcquireCompositor(ICompositor* compositor /* takes ownership */); | |
111 | |
112 virtual void RefreshCanvasSize() = 0; | |
113 | |
114 protected: | |
115 SdlViewport(); | |
116 | |
117 void PostConstructor(); | |
118 | |
119 public: | |
120 bool IsRefreshEvent(const SDL_Event& event) const | |
121 { | |
122 return (event.type == refreshEvent_); | |
123 } | |
124 | |
125 virtual ILock* Lock() ORTHANC_OVERRIDE | |
126 { | |
127 return new SdlLock(*this); | |
128 } | |
129 | |
130 virtual uint32_t GetSdlWindowId() = 0; | |
131 | |
132 void UpdateSize(unsigned int width, | |
133 unsigned int height); | |
134 | |
135 virtual void ToggleMaximize() = 0; | |
136 | |
137 // Must be invoked from the main SDL thread | |
138 virtual void Paint() = 0; | |
139 }; | |
140 | |
141 | |
142 class SdlOpenGLViewport : public SdlViewport | |
143 { | |
144 private: | |
145 SdlOpenGLContext context_; | |
146 | |
147 SdlOpenGLViewport(const std::string& title, | |
148 unsigned int width, | |
149 unsigned int height, | |
150 bool allowDpiScaling = true); | |
151 | |
152 protected: | |
153 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE; | |
154 | |
155 public: | |
156 static boost::shared_ptr<SdlOpenGLViewport> Create(const std::string&, | |
157 unsigned int width, | |
158 unsigned int height, | |
159 bool allowDpiScaling = true); | |
160 | |
161 | |
162 virtual ~SdlOpenGLViewport(); | |
163 | |
164 virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; | |
165 | |
166 virtual void Paint() ORTHANC_OVERRIDE; | |
167 | |
168 virtual void ToggleMaximize() ORTHANC_OVERRIDE; | |
169 }; | |
170 | |
171 | |
172 class SdlCairoViewport : public SdlViewport | |
173 { | |
174 private: | |
175 SdlWindow window_; | |
176 SDL_Surface* sdlSurface_; | |
177 | |
178 void CreateSdlSurfaceFromCompositor(const CairoCompositor& compositor); | |
179 | |
180 SdlCairoViewport(const char* title, | |
181 unsigned int width, | |
182 unsigned int height, | |
183 bool allowDpiScaling = true); | |
184 | |
185 protected: | |
186 virtual void RefreshCanvasSize() ORTHANC_OVERRIDE; | |
187 | |
188 public: | |
189 static boost::shared_ptr<SdlCairoViewport> Create(const char* title, | |
190 unsigned int width, | |
191 unsigned int height, | |
192 bool allowDpiScaling = true); | |
193 | |
194 | |
195 virtual ~SdlCairoViewport(); | |
196 | |
197 virtual uint32_t GetSdlWindowId() ORTHANC_OVERRIDE; | |
198 | |
199 virtual void Paint() ORTHANC_OVERRIDE; | |
200 | |
201 virtual void ToggleMaximize() ORTHANC_OVERRIDE; | |
202 }; | |
203 } |