]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / common / diagnostics / graph.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "graph.h"\r
4 \r
5 #pragma warning (disable : 4244)\r
6 \r
7 #include "../concurrency/executor.h"\r
8 #include "../utility/timer.h"\r
9 \r
10 #include <SFML/Graphics.hpp>\r
11 \r
12 #include <boost/foreach.hpp>\r
13 #include <boost/optional.hpp>\r
14 #include <boost/circular_buffer.hpp>\r
15 #include <boost/range/algorithm_ext/erase.hpp>\r
16 \r
17 #include <numeric>\r
18 #include <map>\r
19 #include <array>\r
20 \r
21 namespace caspar { namespace diagnostics {\r
22                 \r
23 struct drawable : public sf::Drawable\r
24 {\r
25         virtual ~drawable(){}\r
26         virtual void render(sf::RenderTarget& target) = 0;\r
27         virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}\r
28 };\r
29 \r
30 class context : public drawable\r
31 {       \r
32         timer timer_;\r
33         sf::RenderWindow window_;\r
34         \r
35         std::list<std::shared_ptr<drawable>> drawables_;\r
36                 \r
37         executor executor_;\r
38 public:                                 \r
39 \r
40         template<typename Func>\r
41         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
42         {       \r
43                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
44         }\r
45 \r
46         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
47         {\r
48                 begin_invoke([=]\r
49                 {\r
50                         get_instance().do_register_drawable(drawable);\r
51                 });\r
52         }\r
53                         \r
54 private:\r
55         context()\r
56         {\r
57                 executor_.start();\r
58                 executor_.begin_invoke([this]\r
59                 {\r
60                         window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
61                         window_.SetPosition(0, 0);\r
62                         window_.SetActive();\r
63                         glEnable(GL_BLEND);\r
64                         glEnable(GL_LINE_SMOOTH);\r
65                         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
66                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
67                         tick();\r
68                 });\r
69         }\r
70 \r
71         void tick()\r
72         {\r
73                 sf::Event e;\r
74                 while(window_.GetEvent(e)){}            \r
75                 glClear(GL_COLOR_BUFFER_BIT);\r
76                 window_.Draw(*this);\r
77                 window_.Display();\r
78                 timer_.tick(1.0/50.0);\r
79                 executor_.begin_invoke([this]{tick();});\r
80         }\r
81 \r
82         void render(sf::RenderTarget& target)\r
83         {\r
84                 auto count = std::max<size_t>(10, drawables_.size());\r
85                 float target_dy = 1.0f/static_cast<float>(count);\r
86 \r
87                 float last_y = 0.0f;\r
88                 int n = 0;\r
89                 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)\r
90                 {\r
91                         auto& drawable = *it;\r
92                         if(!drawable.unique())\r
93                         {\r
94                                 drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));\r
95                                 float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);\r
96                                 drawable->SetPosition(0.0f, target_y);                  \r
97                                 target.Draw(*drawable);                         \r
98                                 ++it;           \r
99                         }\r
100                         else    \r
101                                 it = drawables_.erase(it);                      \r
102                 }               \r
103         }       \r
104         \r
105         void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
106         {\r
107                 drawables_.push_back(drawable);\r
108         }\r
109         \r
110         static context& get_instance()\r
111         {\r
112                 static context impl;\r
113                 return impl;\r
114         }\r
115 };\r
116 \r
117 class guide : public drawable\r
118 {\r
119         float value_;\r
120         color c_;\r
121 public:\r
122         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
123                 : value_(0.0f)\r
124                 , c_(c){}\r
125 \r
126         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
127                 : value_(value)\r
128                 , c_(c){}\r
129                         \r
130         void set_color(color c) {c_ = c;}\r
131 \r
132         void render(sf::RenderTarget&)\r
133         {               \r
134                 glEnable(GL_LINE_STIPPLE);\r
135                 glLineStipple(3, 0xAAAA);\r
136                 glBegin(GL_LINE_STRIP); \r
137                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
138                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
139                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
140                 glEnd();\r
141                 glDisable(GL_LINE_STIPPLE);\r
142         }\r
143 };\r
144 \r
145 class line : public drawable\r
146 {\r
147         boost::optional<diagnostics::guide> guide_;\r
148         boost::circular_buffer<std::pair<float, bool>> line_data_;\r
149 \r
150         std::vector<float>              tick_data_;\r
151         bool                                    tick_tag_;\r
152         color c_;\r
153 public:\r
154         line(size_t res = 600)\r
155                 : line_data_(res)\r
156                 , tick_tag_(false)\r
157                 , c_(1.0f, 1.0f, 1.0f)\r
158         {\r
159                 line_data_.push_back(std::make_pair(-1.0f, false));\r
160         }\r
161         \r
162         void update(float value)\r
163         {\r
164                 tick_data_.push_back(value);\r
165         }\r
166         \r
167         void tag()\r
168         {\r
169                 tick_tag_ = true;\r
170         }\r
171 \r
172         void guide(const guide& guide)\r
173         {\r
174                 guide_ = guide;\r
175                 guide_->set_color(c_);\r
176         }\r
177         \r
178         void set_color(color c)\r
179         {\r
180                 c_ = c;\r
181                 if(guide_)\r
182                         guide_->set_color(c_);\r
183         }\r
184 \r
185         color get_color() const { return c_; }\r
186         \r
187         void render(sf::RenderTarget& target)\r
188         {\r
189                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
190                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
191 \r
192                 if(!tick_data_.empty())\r
193                 {\r
194                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
195                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
196                         tick_data_.clear();\r
197                 }\r
198                 else if(!line_data_.empty())\r
199                 {\r
200                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
201                 }\r
202                 tick_tag_ = false;\r
203 \r
204                 if(guide_)\r
205                         target.Draw(*guide_);\r
206                 \r
207                 glBegin(GL_LINE_STRIP);\r
208                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);             \r
209                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
210                         if(line_data_[n].first > -0.5)\r
211                                 glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, (1.0f-line_data_[n].first)*0.8f + 0.1f)), 0.0f);             \r
212                 glEnd();\r
213                                 \r
214                 glEnable(GL_LINE_STIPPLE);\r
215                 glLineStipple(3, 0xAAAA);\r
216                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
217                 {\r
218                         if(line_data_[n].second)\r
219                         {\r
220                                 glBegin(GL_LINE_STRIP);\r
221                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
222                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
223                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
224                                 glEnd();\r
225                         }\r
226                 }\r
227                 glDisable(GL_LINE_STIPPLE);\r
228         }\r
229 };\r
230 \r
231 struct graph::implementation : public drawable\r
232 {\r
233         std::map<std::string, diagnostics::line> lines_;\r
234         std::string name_;\r
235 \r
236         implementation(const std::string& name) \r
237                 : name_(name){}\r
238 \r
239         void update(const std::string& name, float value)\r
240         {\r
241                 context::begin_invoke([=]\r
242                 {\r
243                         lines_[name].update(value);\r
244                 });\r
245         }\r
246 \r
247         void tag(const std::string& name)\r
248         {\r
249                 context::begin_invoke([=]\r
250                 {\r
251                         lines_[name].tag();\r
252                 });\r
253         }\r
254 \r
255         void set_color(const std::string& name, color c)\r
256         {\r
257                 context::begin_invoke([=]\r
258                 {\r
259                         lines_[name].set_color(c);\r
260                 });\r
261         }\r
262         \r
263         void guide(const std::string& name, float value)\r
264         {\r
265                 context::begin_invoke([=]\r
266                 {\r
267                         lines_[name].guide(diagnostics::guide(value));\r
268                 });\r
269         }\r
270 \r
271 private:\r
272         void render(sf::RenderTarget& target)\r
273         {\r
274                 const size_t text_size = 15;\r
275                 const size_t text_margin = 2;\r
276                 const size_t text_offset = text_size+text_margin*2;\r
277 \r
278                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
279                 text.SetStyle(sf::String::Italic);\r
280                 text.Move(text_margin, text_margin);\r
281                 \r
282                 glPushMatrix();\r
283                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
284                         target.Draw(text);\r
285                         float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;\r
286                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
287                         {                                               \r
288                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
289                                 line_text.SetPosition(x_offset, text_margin);\r
290                                 auto c = it->second.get_color();\r
291                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
292                                 target.Draw(line_text);\r
293                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
294                         }\r
295 \r
296                         glDisable(GL_TEXTURE_2D);\r
297                 glPopMatrix();\r
298 \r
299                 glBegin(GL_QUADS);\r
300                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
301                         glVertex2f(1.0f, 0.99f);\r
302                         glVertex2f(0.0f, 0.99f);\r
303                         glVertex2f(0.0f, 0.01f);        \r
304                         glVertex2f(1.0f, 0.01f);        \r
305                 glEnd();\r
306 \r
307                 glPushMatrix();\r
308                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
309                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
310                 \r
311                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
312                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
313 \r
314                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
315                                 target.Draw(it->second);\r
316                 \r
317                 glPopMatrix();\r
318         }\r
319 \r
320         implementation(implementation&);\r
321         implementation& operator=(implementation&);\r
322 };\r
323         \r
324 graph::graph(const std::string& name) : impl_(new implementation(name))\r
325 {\r
326         context::register_drawable(impl_);\r
327 }\r
328 \r
329 void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
330 void graph::tag(const std::string& name){impl_->tag(name);}\r
331 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}\r
332 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
333 \r
334 safe_ptr<graph> create_graph(const std::string& name)\r
335 {\r
336         return safe_ptr<graph>(new graph(name));\r
337 }\r
338 \r
339 }}