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