]> 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() : executor_(L"diagnostics")\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>(8, 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         const printer parent_printer_;\r
245         std::string name_;\r
246 \r
247         int counter_;\r
248 \r
249         implementation(const std::string& name) \r
250                 : name_(name)\r
251                 , counter_(0){}\r
252 \r
253         implementation(const printer& parent_printer) \r
254                 : parent_printer_(parent_printer)\r
255                 , name_(parent_printer_ ? narrow(parent_printer_()) : "")\r
256                 , counter_(0){}\r
257 \r
258         void update(const std::string& name, float value)\r
259         {\r
260                 context::begin_invoke([=]\r
261                 {\r
262                         lines_[name].update(value);\r
263                 });\r
264         }\r
265 \r
266         void set(const std::string& name, float value)\r
267         {\r
268                 context::begin_invoke([=]\r
269                 {\r
270                         lines_[name].set(value);\r
271                 });\r
272         }\r
273 \r
274         void tag(const std::string& name)\r
275         {\r
276                 context::begin_invoke([=]\r
277                 {\r
278                         lines_[name].tag();\r
279                 });\r
280         }\r
281 \r
282         void set_color(const std::string& name, color c)\r
283         {\r
284                 context::begin_invoke([=]\r
285                 {\r
286                         lines_[name].set_color(c);\r
287                 });\r
288         }\r
289         \r
290         void guide(const std::string& name, float value)\r
291         {\r
292                 context::begin_invoke([=]\r
293                 {\r
294                         lines_[name].guide(diagnostics::guide(value));\r
295                 });\r
296         }\r
297         \r
298 private:\r
299         void render(sf::RenderTarget& target)\r
300         {\r
301                 if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.\r
302                 {\r
303                         counter_ = 0;\r
304                         if(parent_printer_)\r
305                                 name_ = narrow(parent_printer_());\r
306                 }\r
307                 const size_t text_size = 15;\r
308                 const size_t text_margin = 2;\r
309                 const size_t text_offset = (text_size+text_margin*2)*2;\r
310 \r
311                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
312                 text.SetStyle(sf::String::Italic);\r
313                 text.Move(text_margin, text_margin);\r
314                 \r
315                 glPushMatrix();\r
316                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
317                         target.Draw(text);\r
318                         float x_offset = text_margin;\r
319                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
320                         {                                               \r
321                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
322                                 line_text.SetPosition(x_offset, text_margin+text_offset/2);\r
323                                 auto c = it->second.get_color();\r
324                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
325                                 target.Draw(line_text);\r
326                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
327                         }\r
328 \r
329                         glDisable(GL_TEXTURE_2D);\r
330                 glPopMatrix();\r
331 \r
332                 glBegin(GL_QUADS);\r
333                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
334                         glVertex2f(1.0f, 0.99f);\r
335                         glVertex2f(0.0f, 0.99f);\r
336                         glVertex2f(0.0f, 0.01f);        \r
337                         glVertex2f(1.0f, 0.01f);        \r
338                 glEnd();\r
339 \r
340                 glPushMatrix();\r
341                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
342                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
343                 \r
344                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
345                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
346 \r
347                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
348                                 target.Draw(it->second);\r
349                 \r
350                 glPopMatrix();\r
351         }\r
352 \r
353         implementation(implementation&);\r
354         implementation& operator=(implementation&);\r
355 };\r
356         \r
357 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)\r
358 {\r
359         if(impl_)\r
360                 context::register_drawable(impl_);\r
361 }\r
362 \r
363 graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)\r
364 {\r
365         if(impl_)\r
366                 context::register_drawable(impl_);\r
367 }\r
368 \r
369 void graph::update_value(const std::string& name, float value){if(impl_)impl_->update(name, value);}\r
370 void graph::set_value(const std::string& name, float value){if(impl_)impl_->set(name, value);}\r
371 void graph::set_color(const std::string& name, color c){if(impl_)impl_->set_color(name, c);}\r
372 void graph::add_tag(const std::string& name){if(impl_)impl_->tag(name);}\r
373 void graph::add_guide(const std::string& name, float value){if(impl_)impl_->guide(name, value);}\r
374 \r
375 safe_ptr<graph> create_graph(const std::string& name)\r
376 {\r
377         return safe_ptr<graph>(new graph(name));\r
378 }\r
379 safe_ptr<graph> create_graph(const printer& parent_printer)\r
380 {\r
381         return safe_ptr<graph>(new graph(parent_printer));\r
382 }\r
383 \r
384 \r
385 //namespace v2\r
386 //{     \r
387 //      \r
388 //struct line::implementation\r
389 //{\r
390 //      std::wstring name_;\r
391 //      boost::circular_buffer<data> ticks_;\r
392 //\r
393 //      implementation(const std::wstring& name) \r
394 //              : name_(name)\r
395 //              , ticks_(1024){}\r
396 //      \r
397 //      void update_value(float value)\r
398 //      {\r
399 //              ticks_.push_back();\r
400 //              ticks_.back().value = value;\r
401 //      }\r
402 //\r
403 //      void set_value(float value)\r
404 //      {\r
405 //              ticks_.clear();\r
406 //              update_value(value);\r
407 //      }\r
408 //};\r
409 //\r
410 //line::line(){}\r
411 //line::line(const std::wstring& name) : impl_(new implementation(name)){}\r
412 //std::wstring line::print() const {return impl_->name_;}\r
413 //void line::update_value(float value){impl_->update_value(value);}\r
414 //void line::set_value(float value){impl_->set_value(value);}\r
415 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}\r
416 //\r
417 //struct graph::implementation\r
418 //{\r
419 //      std::map<std::wstring, line> lines_;\r
420 //      color                                            color_;\r
421 //      printer                                          printer_;\r
422 //\r
423 //      implementation(const std::wstring& name) \r
424 //              : printer_([=]{return name;}){}\r
425 //\r
426 //      implementation(const printer& parent_printer) \r
427 //              : printer_(parent_printer){}\r
428 //      \r
429 //      void update_value(const std::wstring& name, float value)\r
430 //      {\r
431 //              auto it = lines_.find(name);\r
432 //              if(it == lines_.end())\r
433 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
434 //\r
435 //              it->second.update_value(value);\r
436 //      }\r
437 //\r
438 //      void set_value(const std::wstring& name, float value)\r
439 //      {\r
440 //              auto it = lines_.find(name);\r
441 //              if(it == lines_.end())\r
442 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
443 //\r
444 //              it->second.set_value(value);\r
445 //      }\r
446 //      \r
447 //      void set_color(const std::wstring& name, color color)\r
448 //      {\r
449 //              color_ = color;\r
450 //      }\r
451 //\r
452 //      std::map<std::wstring, line>& get_lines()\r
453 //      {\r
454 //              return lines_;\r
455 //      }\r
456 //      \r
457 //      color get_color() const\r
458 //      {\r
459 //              return color_;\r
460 //      }\r
461 //\r
462 //      std::wstring print() const\r
463 //      {\r
464 //              return printer_ ? printer_() : L"graph";\r
465 //      }\r
466 //};\r
467 //      \r
468 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}\r
469 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}\r
470 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}\r
471 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}\r
472 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}\r
473 //color graph::get_color() const {return impl_->get_color();}\r
474 //std::wstring graph::print() const {return impl_->print();}\r
475 //\r
476 //safe_ptr<graph> graph::clone() const \r
477 //{\r
478 //      safe_ptr<graph> clone(new graph(std::wstring(L"")));\r
479 //      clone->impl_->printer_ = impl_->printer_;\r
480 //      clone->impl_->lines_ = impl_->lines_;\r
481 //      clone->impl_->color_ = impl_->color_;   \r
482 //}\r
483 //\r
484 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}\r
485 //\r
486 //std::vector<safe_ptr<graph>> g_graphs;\r
487 //\r
488 //safe_ptr<graph> create_graph(const std::string& name)\r
489 //{\r
490 //      g_graphs.push_back(make_safe<graph>(name));\r
491 //      return g_graphs.back();\r
492 //}\r
493 //\r
494 //safe_ptr<graph> create_graph(const printer& parent_printer)\r
495 //{\r
496 //      g_graphs.push_back(make_safe<graph>(parent_printer));\r
497 //      return g_graphs.back();\r
498 //}\r
499 //\r
500 //static std::vector<safe_ptr<graph>> get_all_graphs()\r
501 //{\r
502 //      std::vector<safe_ptr<graph>> graphs;\r
503 //      BOOST_FOREACH(auto& graph, g_graphs)\r
504 //              graphs.push_back(graph->clone());\r
505 //\r
506 //      return graphs;\r
507 //}\r
508 //\r
509 //}\r
510 \r
511 }}