comparison OrthancStone/Sources/Viewport/SdlViewport.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Viewport/SdlViewport.cpp@30deba7bc8e2
children b4ccd4963d37
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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 #include "SdlViewport.h"
22
23 #include <OrthancException.h>
24
25 #include <boost/make_shared.hpp>
26
27 namespace OrthancStone
28 {
29 ICompositor& SdlViewport::SdlLock::GetCompositor()
30 {
31 if (that_.compositor_.get() == NULL)
32 {
33 // The derived class should have called "AcquireCompositor()"
34 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
35 }
36 else
37 {
38 return *that_.compositor_;
39 }
40 }
41
42
43 void SdlViewport::AcquireCompositor(ICompositor* compositor /* takes ownership */)
44 {
45 if (compositor == NULL)
46 {
47 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
48 }
49
50 compositor_.reset(compositor);
51 }
52
53 SdlViewport::SdlViewport()
54 {
55 refreshEvent_ = SDL_RegisterEvents(1);
56
57 if (refreshEvent_ == static_cast<uint32_t>(-1))
58 {
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
60 }
61 }
62
63 void SdlViewport::PostConstructor()
64 {
65 controller_ = boost::make_shared<ViewportController>(shared_from_this());
66 }
67
68 void SdlViewport::SendRefreshEvent()
69 {
70 SDL_Event event;
71 SDL_memset(&event, 0, sizeof(event));
72 event.type = refreshEvent_;
73 SDL_PushEvent(&event); // This function is thread-safe, and can be called from other threads safely.
74 }
75
76
77 SdlOpenGLViewport::SdlOpenGLViewport(const std::string& title,
78 unsigned int width,
79 unsigned int height,
80 bool allowDpiScaling) :
81 context_(title.c_str(), width, height, allowDpiScaling)
82 {
83 AcquireCompositor(new OpenGLCompositor(context_)); // (*)
84 }
85
86 boost::shared_ptr<SdlOpenGLViewport> SdlOpenGLViewport::Create(
87 const std::string& title,
88 unsigned int width,
89 unsigned int height,
90 bool allowDpiScaling)
91 {
92 boost::shared_ptr<SdlOpenGLViewport> that =
93 boost::shared_ptr<SdlOpenGLViewport>(new SdlOpenGLViewport(title, width, height, allowDpiScaling));
94 that->SdlViewport::PostConstructor();
95 return that;
96 }
97
98 uint32_t SdlOpenGLViewport::GetSdlWindowId()
99 {
100 SdlWindow& sdlWindowWrapper = context_.GetWindow();
101 SDL_Window* sdlWindow = sdlWindowWrapper.GetObject();
102 Uint32 sdlWindowId = SDL_GetWindowID(sdlWindow);
103 return sdlWindowId;
104 }
105
106 SdlOpenGLViewport::~SdlOpenGLViewport()
107 {
108 // Make sure that the "OpenGLCompositor" is destroyed BEFORE the
109 // "OpenGLContext" it references (*)
110 ClearCompositor();
111 }
112
113
114 void SdlOpenGLViewport::Paint()
115 {
116 SdlLock lock(*this);
117 lock.GetCompositor().Refresh(lock.GetController().GetScene());
118 }
119
120
121 void SdlOpenGLViewport::UpdateSize(unsigned int width, unsigned int height)
122 {
123 // nothing to do in OpenGL, the OpenGLCompositor::UpdateSize will be called automatically
124 SdlLock lock(*this);
125 lock.Invalidate();
126 }
127
128
129 void SdlOpenGLViewport::ToggleMaximize()
130 {
131 // No need to call "Invalidate()" here, as "UpdateSize()" will
132 // be invoked after event "SDL_WINDOWEVENT_SIZE_CHANGED"
133 SdlLock lock(*this);
134 context_.ToggleMaximize();
135 }
136
137
138
139 SdlCairoViewport::SdlCairoViewport(const char* title,
140 unsigned int width,
141 unsigned int height,
142 bool allowDpiScaling) :
143 window_(title, width, height, false /* enable OpenGL */, allowDpiScaling),
144 sdlSurface_(NULL)
145 {
146 AcquireCompositor(new CairoCompositor(width, height));
147 }
148
149 SdlCairoViewport::~SdlCairoViewport()
150 {
151 if (sdlSurface_)
152 {
153 SDL_FreeSurface(sdlSurface_);
154 }
155 }
156
157 uint32_t SdlCairoViewport::GetSdlWindowId()
158 {
159 SDL_Window* sdlWindow = window_.GetObject();
160 Uint32 sdlWindowId = SDL_GetWindowID(sdlWindow);
161 return sdlWindowId;
162 }
163
164 void SdlCairoViewport::Paint()
165 {
166 SdlLock lock(*this);
167
168 lock.GetCompositor().Refresh(lock.GetController().GetScene());
169 CreateSdlSurfaceFromCompositor(dynamic_cast<CairoCompositor&>(lock.GetCompositor()));
170
171 if (sdlSurface_ != NULL)
172 {
173 window_.Render(sdlSurface_);
174 }
175 }
176
177
178 void SdlCairoViewport::UpdateSize(unsigned int width,
179 unsigned int height)
180 {
181 SdlLock lock(*this);
182 dynamic_cast<CairoCompositor&>(lock.GetCompositor()).UpdateSize(width, height);
183 lock.Invalidate();
184 }
185
186
187 void SdlCairoViewport::ToggleMaximize()
188 {
189 // No need to call "Invalidate()" here, as "UpdateSize()" will
190 // be invoked after event "SDL_WINDOWEVENT_SIZE_CHANGED"
191 SdlLock lock(*this);
192 window_.ToggleMaximize();
193 }
194
195
196 // Assumes that the mutex is locked
197 void SdlCairoViewport::CreateSdlSurfaceFromCompositor(CairoCompositor& compositor)
198 {
199 static const uint32_t rmask = 0x00ff0000;
200 static const uint32_t gmask = 0x0000ff00;
201 static const uint32_t bmask = 0x000000ff;
202
203 const unsigned int width = compositor.GetCanvas().GetWidth();
204 const unsigned int height = compositor.GetCanvas().GetHeight();
205
206 if (sdlSurface_ != NULL)
207 {
208 if (sdlSurface_->pixels == compositor.GetCanvas().GetBuffer() &&
209 sdlSurface_->w == static_cast<int>(width) &&
210 sdlSurface_->h == static_cast<int>(height) &&
211 sdlSurface_->pitch == static_cast<int>(compositor.GetCanvas().GetPitch()))
212 {
213 // The image from the compositor has not changed, no need to update the surface
214 return;
215 }
216 else
217 {
218 SDL_FreeSurface(sdlSurface_);
219 }
220 }
221
222 sdlSurface_ = SDL_CreateRGBSurfaceFrom((void*)(compositor.GetCanvas().GetBuffer()), width, height, 32,
223 compositor.GetCanvas().GetPitch(), rmask, gmask, bmask, 0);
224 if (!sdlSurface_)
225 {
226 LOG(ERROR) << "Cannot create a SDL surface from a Cairo surface";
227 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
228 }
229 }
230 }