Mercurial > hg > orthanc-stone
comparison Framework/Applications/Sdl/SdlBuffering.cpp @ 0:351ab0da0150
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Oct 2016 15:34:11 +0200 |
parents | |
children | ff1e935768e7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:351ab0da0150 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "SdlBuffering.h" | |
34 | |
35 #if ORTHANC_ENABLE_SDL == 1 | |
36 | |
37 #include "../../Orthanc/Core/Logging.h" | |
38 #include "../../Orthanc/Core/OrthancException.h" | |
39 | |
40 namespace OrthancStone | |
41 { | |
42 SdlBuffering::SdlBuffering() : | |
43 sdlSurface_(NULL), | |
44 pendingFrame_(false) | |
45 { | |
46 } | |
47 | |
48 | |
49 SdlBuffering::~SdlBuffering() | |
50 { | |
51 if (sdlSurface_) | |
52 { | |
53 SDL_FreeSurface(sdlSurface_); | |
54 } | |
55 } | |
56 | |
57 | |
58 void SdlBuffering::SetSize(unsigned int width, | |
59 unsigned int height, | |
60 IViewport& viewport) | |
61 { | |
62 boost::mutex::scoped_lock lock(mutex_); | |
63 | |
64 viewport.SetSize(width, height); | |
65 | |
66 if (offscreenSurface_.get() == NULL || | |
67 offscreenSurface_->GetWidth() != width || | |
68 offscreenSurface_->GetHeight() != height) | |
69 { | |
70 offscreenSurface_.reset(new CairoSurface(width, height)); | |
71 } | |
72 | |
73 if (onscreenSurface_.get() == NULL || | |
74 onscreenSurface_->GetWidth() != width || | |
75 onscreenSurface_->GetHeight() != height) | |
76 { | |
77 onscreenSurface_.reset(new CairoSurface(width, height)); | |
78 | |
79 // TODO Big endian? | |
80 static const uint32_t rmask = 0x00ff0000; | |
81 static const uint32_t gmask = 0x0000ff00; | |
82 static const uint32_t bmask = 0x000000ff; | |
83 | |
84 if (sdlSurface_) | |
85 { | |
86 SDL_FreeSurface(sdlSurface_); | |
87 } | |
88 | |
89 sdlSurface_ = SDL_CreateRGBSurfaceFrom(onscreenSurface_->GetBuffer(), width, height, 32, | |
90 onscreenSurface_->GetPitch(), rmask, gmask, bmask, 0); | |
91 if (!sdlSurface_) | |
92 { | |
93 LOG(ERROR) << "Cannot create a SDL surface from a Cairo surface"; | |
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
95 } | |
96 } | |
97 | |
98 pendingFrame_ = false; | |
99 } | |
100 | |
101 | |
102 bool SdlBuffering::RenderOffscreen(IViewport& viewport) | |
103 { | |
104 boost::mutex::scoped_lock lock(mutex_); | |
105 | |
106 if (offscreenSurface_.get() == NULL) | |
107 { | |
108 return false; | |
109 } | |
110 | |
111 Orthanc::ImageAccessor target = offscreenSurface_->GetAccessor(); | |
112 | |
113 if (viewport.Render(target) && | |
114 !pendingFrame_) | |
115 { | |
116 pendingFrame_ = true; | |
117 return true; | |
118 } | |
119 else | |
120 { | |
121 return false; | |
122 } | |
123 } | |
124 | |
125 | |
126 void SdlBuffering::SwapToScreen(SdlWindow& window) | |
127 { | |
128 if (!pendingFrame_ || | |
129 offscreenSurface_.get() == NULL || | |
130 onscreenSurface_.get() == NULL) | |
131 { | |
132 return; | |
133 } | |
134 | |
135 { | |
136 boost::mutex::scoped_lock lock(mutex_); | |
137 onscreenSurface_->Copy(*offscreenSurface_); | |
138 } | |
139 | |
140 window.Render(sdlSurface_); | |
141 pendingFrame_ = false; | |
142 } | |
143 } | |
144 | |
145 #endif |