]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
2.0.0.2: Fixed debug compilation.
[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 "../env.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         sf::RenderWindow window_;\r
33         \r
34         std::list<std::shared_ptr<drawable>> drawables_;\r
35                 \r
36         executor executor_;\r
37 public:                                 \r
38 \r
39         template<typename Func>\r
40         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
41         {       \r
42                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
43         }\r
44 \r
45         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
46         {\r
47                 if(!drawable)\r
48                         return;\r
49 \r
50                 begin_invoke([=]\r
51                 {\r
52                         get_instance().do_register_drawable(drawable);\r
53                 });\r
54         }\r
55                         \r
56 private:\r
57         context() : executor_(L"diagnostics")\r
58         {\r
59                 executor_.start();\r
60                 executor_.begin_invoke([this]\r
61                 {\r
62                         window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
63                         window_.SetPosition(0, 0);\r
64                         window_.SetActive();\r
65                         glEnable(GL_BLEND);\r
66                         glEnable(GL_LINE_SMOOTH);\r
67                         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
68                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
69                         tick();\r
70                 });\r
71         }\r
72 \r
73         void tick()\r
74         {\r
75                 sf::Event e;\r
76                 while(window_.GetEvent(e)){}            \r
77                 glClear(GL_COLOR_BUFFER_BIT);\r
78                 window_.Draw(*this);\r
79                 window_.Display();\r
80                 boost::this_thread::sleep(boost::posix_time::milliseconds(20));\r
81                 executor_.begin_invoke([this]{tick();});\r
82         }\r
83 \r
84         void render(sf::RenderTarget& target)\r
85         {\r
86                 auto count = std::max<size_t>(8, drawables_.size());\r
87                 float target_dy = 1.0f/static_cast<float>(count);\r
88 \r
89                 float last_y = 0.0f;\r
90                 int n = 0;\r
91                 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)\r
92                 {\r
93                         auto& drawable = *it;\r
94                         if(!drawable.unique())\r
95                         {\r
96                                 drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));\r
97                                 float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);\r
98                                 drawable->SetPosition(0.0f, target_y);                  \r
99                                 target.Draw(*drawable);                         \r
100                                 ++it;           \r
101                         }\r
102                         else    \r
103                                 it = drawables_.erase(it);                      \r
104                 }               \r
105         }       \r
106         \r
107         void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
108         {\r
109                 drawables_.push_back(drawable);\r
110         }\r
111         \r
112         static context& get_instance()\r
113         {\r
114                 static context impl;\r
115                 return impl;\r
116         }\r
117 };\r
118 \r
119 class guide : public drawable\r
120 {\r
121         float value_;\r
122         color c_;\r
123 public:\r
124         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
125                 : value_(0.0f)\r
126                 , c_(c){}\r
127 \r
128         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
129                 : value_(value)\r
130                 , c_(c){}\r
131                         \r
132         void set_color(color c) {c_ = c;}\r
133 \r
134         void render(sf::RenderTarget&)\r
135         {               \r
136                 glEnable(GL_LINE_STIPPLE);\r
137                 glLineStipple(3, 0xAAAA);\r
138                 glBegin(GL_LINE_STRIP); \r
139                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
140                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
141                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
142                 glEnd();\r
143                 glDisable(GL_LINE_STIPPLE);\r
144         }\r
145 };\r
146 \r
147 class line : public drawable\r
148 {\r
149         boost::optional<diagnostics::guide> guide_;\r
150         boost::circular_buffer<std::pair<float, bool>> line_data_;\r
151 \r
152         std::vector<float>              tick_data_;\r
153         bool                                    tick_tag_;\r
154         color c_;\r
155 public:\r
156         line(size_t res = 600)\r
157                 : line_data_(res)\r
158                 , tick_tag_(false)\r
159                 , c_(1.0f, 1.0f, 1.0f)\r
160         {\r
161                 line_data_.push_back(std::make_pair(-1.0f, false));\r
162         }\r
163         \r
164         void update(float value)\r
165         {\r
166                 tick_data_.push_back(value);\r
167         }\r
168 \r
169         void set(float value)\r
170         {\r
171                 tick_data_.clear();\r
172                 tick_data_.push_back(value);\r
173         }\r
174         \r
175         void tag()\r
176         {\r
177                 tick_tag_ = true;\r
178         }\r
179 \r
180         void guide(const guide& guide)\r
181         {\r
182                 guide_ = guide;\r
183                 guide_->set_color(c_);\r
184         }\r
185         \r
186         void set_color(color c)\r
187         {\r
188                 c_ = c;\r
189                 if(guide_)\r
190                         guide_->set_color(c_);\r
191         }\r
192 \r
193         color get_color() const { return c_; }\r
194         \r
195         void render(sf::RenderTarget& target)\r
196         {\r
197                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
198                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
199 \r
200                 if(!tick_data_.empty())\r
201                 {\r
202                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
203                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
204                         tick_data_.clear();\r
205                 }\r
206                 else if(!line_data_.empty())\r
207                 {\r
208                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
209                 }\r
210                 tick_tag_ = false;\r
211 \r
212                 if(guide_)\r
213                         target.Draw(*guide_);\r
214                 \r
215                 glBegin(GL_LINE_STRIP);\r
216                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);             \r
217                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
218                         if(line_data_[n].first > -0.5)\r
219                                 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
220                 glEnd();\r
221                                 \r
222                 glEnable(GL_LINE_STIPPLE);\r
223                 glLineStipple(3, 0xAAAA);\r
224                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
225                 {\r
226                         if(line_data_[n].second)\r
227                         {\r
228                                 glBegin(GL_LINE_STRIP);\r
229                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
230                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
231                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
232                                 glEnd();\r
233                         }\r
234                 }\r
235                 glDisable(GL_LINE_STIPPLE);\r
236         }\r
237 };\r
238 \r
239 struct graph::implementation : public drawable\r
240 {\r
241         std::map<std::string, diagnostics::line> lines_;\r
242         const printer parent_printer_;\r
243         std::string name_;\r
244 \r
245         int counter_;\r
246 \r
247         implementation(const std::string& name) \r
248                 : name_(name)\r
249                 , counter_(0){}\r
250 \r
251         implementation(const printer& parent_printer) \r
252                 : parent_printer_(parent_printer)\r
253                 , name_(parent_printer_ ? narrow(parent_printer_()) : "")\r
254                 , counter_(0){}\r
255 \r
256         void update(const std::string& name, float value)\r
257         {\r
258                 lines_[name].update(value);\r
259         }\r
260 \r
261         void set(const std::string& name, float value)\r
262         {\r
263                 lines_[name].set(value);\r
264         }\r
265 \r
266         void tag(const std::string& name)\r
267         {\r
268                 lines_[name].tag();\r
269         }\r
270 \r
271         void set_color(const std::string& name, color c)\r
272         {\r
273                 lines_[name].set_color(c);\r
274         }\r
275         \r
276         void guide(const std::string& name, float value)\r
277         {\r
278                 lines_[name].guide(diagnostics::guide(value));  \r
279         }\r
280         \r
281 private:\r
282         void render(sf::RenderTarget& target)\r
283         {\r
284                 if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.\r
285                 {\r
286                         counter_ = 0;\r
287                         if(parent_printer_)\r
288                                 name_ = narrow(parent_printer_());\r
289                 }\r
290                 const size_t text_size = 15;\r
291                 const size_t text_margin = 2;\r
292                 const size_t text_offset = (text_size+text_margin*2)*2;\r
293 \r
294                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
295                 text.SetStyle(sf::String::Italic);\r
296                 text.Move(text_margin, text_margin);\r
297                 \r
298                 glPushMatrix();\r
299                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
300                         target.Draw(text);\r
301                         float x_offset = text_margin;\r
302                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
303                         {                                               \r
304                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
305                                 line_text.SetPosition(x_offset, text_margin+text_offset/2);\r
306                                 auto c = it->second.get_color();\r
307                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
308                                 target.Draw(line_text);\r
309                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
310                         }\r
311 \r
312                         glDisable(GL_TEXTURE_2D);\r
313                 glPopMatrix();\r
314 \r
315                 glBegin(GL_QUADS);\r
316                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
317                         glVertex2f(1.0f, 0.99f);\r
318                         glVertex2f(0.0f, 0.99f);\r
319                         glVertex2f(0.0f, 0.01f);        \r
320                         glVertex2f(1.0f, 0.01f);        \r
321                 glEnd();\r
322 \r
323                 glPushMatrix();\r
324                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
325                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
326                 \r
327                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
328                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
329 \r
330                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
331                                 target.Draw(it->second);\r
332                 \r
333                 glPopMatrix();\r
334         }\r
335 \r
336         implementation(implementation&);\r
337         implementation& operator=(implementation&);\r
338 };\r
339         \r
340 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)\r
341 {\r
342         if(impl_)\r
343                 context::register_drawable(impl_);\r
344 }\r
345 \r
346 graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)\r
347 {\r
348         if(impl_)\r
349                 context::register_drawable(impl_);\r
350 }\r
351 \r
352 void graph::update_value(const std::string& name, float value)\r
353 {\r
354         if(impl_)\r
355         {       \r
356                 auto p = impl_;\r
357                 context::begin_invoke([=]\r
358                 {       \r
359                         p->update(name, value);\r
360                 });\r
361         }\r
362 }\r
363 void graph::set_value(const std::string& name, float value)\r
364 {\r
365         if(impl_)\r
366         {               \r
367                 auto p = impl_;\r
368                 context::begin_invoke([=]\r
369                 {       \r
370                         p->set(name, value);\r
371                 });\r
372         }\r
373 }\r
374 void graph::set_color(const std::string& name, color c)\r
375 {       \r
376         if(impl_)\r
377         {               \r
378                 auto p = impl_;\r
379                 context::begin_invoke([=]\r
380                 {       \r
381                         p->set_color(name, c);\r
382                 });\r
383         }\r
384 }\r
385 void graph::add_tag(const std::string& name)\r
386 {       \r
387         if(impl_)\r
388         {               \r
389                 auto p = impl_;\r
390                 context::begin_invoke([=]\r
391                 {       \r
392                         p->tag(name);\r
393                 });\r
394         }\r
395 }\r
396 void graph::add_guide(const std::string& name, float value)\r
397 {       \r
398         if(impl_)\r
399         {               \r
400                 auto p = impl_;\r
401                 context::begin_invoke([=]\r
402                 {       \r
403                         p->guide(name, value);\r
404                 });\r
405         }\r
406 }\r
407 \r
408 safe_ptr<graph> create_graph(const std::string& name)\r
409 {\r
410         return safe_ptr<graph>(new graph(name));\r
411 }\r
412 safe_ptr<graph> create_graph(const printer& parent_printer)\r
413 {\r
414         return safe_ptr<graph>(new graph(parent_printer));\r
415 }\r
416 \r
417 \r
418 //namespace v2\r
419 //{     \r
420 //      \r
421 //struct line::implementation\r
422 //{\r
423 //      std::wstring name_;\r
424 //      boost::circular_buffer<data> ticks_;\r
425 //\r
426 //      implementation(const std::wstring& name) \r
427 //              : name_(name)\r
428 //              , ticks_(1024){}\r
429 //      \r
430 //      void update_value(float value)\r
431 //      {\r
432 //              ticks_.push_back();\r
433 //              ticks_.back().value = value;\r
434 //      }\r
435 //\r
436 //      void set_value(float value)\r
437 //      {\r
438 //              ticks_.clear();\r
439 //              update_value(value);\r
440 //      }\r
441 //};\r
442 //\r
443 //line::line(){}\r
444 //line::line(const std::wstring& name) : impl_(new implementation(name)){}\r
445 //std::wstring line::print() const {return impl_->name_;}\r
446 //void line::update_value(float value){impl_->update_value(value);}\r
447 //void line::set_value(float value){impl_->set_value(value);}\r
448 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}\r
449 //\r
450 //struct graph::implementation\r
451 //{\r
452 //      std::map<std::wstring, line> lines_;\r
453 //      color                                            color_;\r
454 //      printer                                          printer_;\r
455 //\r
456 //      implementation(const std::wstring& name) \r
457 //              : printer_([=]{return name;}){}\r
458 //\r
459 //      implementation(const printer& parent_printer) \r
460 //              : printer_(parent_printer){}\r
461 //      \r
462 //      void update_value(const std::wstring& name, float value)\r
463 //      {\r
464 //              auto it = lines_.find(name);\r
465 //              if(it == lines_.end())\r
466 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
467 //\r
468 //              it->second.update_value(value);\r
469 //      }\r
470 //\r
471 //      void set_value(const std::wstring& name, float value)\r
472 //      {\r
473 //              auto it = lines_.find(name);\r
474 //              if(it == lines_.end())\r
475 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
476 //\r
477 //              it->second.set_value(value);\r
478 //      }\r
479 //      \r
480 //      void set_color(const std::wstring& name, color color)\r
481 //      {\r
482 //              color_ = color;\r
483 //      }\r
484 //\r
485 //      std::map<std::wstring, line>& get_lines()\r
486 //      {\r
487 //              return lines_;\r
488 //      }\r
489 //      \r
490 //      color get_color() const\r
491 //      {\r
492 //              return color_;\r
493 //      }\r
494 //\r
495 //      std::wstring print() const\r
496 //      {\r
497 //              return printer_ ? printer_() : L"graph";\r
498 //      }\r
499 //};\r
500 //      \r
501 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}\r
502 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}\r
503 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}\r
504 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}\r
505 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}\r
506 //color graph::get_color() const {return impl_->get_color();}\r
507 //std::wstring graph::print() const {return impl_->print();}\r
508 //\r
509 //safe_ptr<graph> graph::clone() const \r
510 //{\r
511 //      safe_ptr<graph> clone(new graph(std::wstring(L"")));\r
512 //      clone->impl_->printer_ = impl_->printer_;\r
513 //      clone->impl_->lines_ = impl_->lines_;\r
514 //      clone->impl_->color_ = impl_->color_;   \r
515 //}\r
516 //\r
517 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}\r
518 //\r
519 //std::vector<safe_ptr<graph>> g_graphs;\r
520 //\r
521 //safe_ptr<graph> create_graph(const std::string& name)\r
522 //{\r
523 //      g_graphs.push_back(make_safe<graph>(name));\r
524 //      return g_graphs.back();\r
525 //}\r
526 //\r
527 //safe_ptr<graph> create_graph(const printer& parent_printer)\r
528 //{\r
529 //      g_graphs.push_back(make_safe<graph>(parent_printer));\r
530 //      return g_graphs.back();\r
531 //}\r
532 //\r
533 //static std::vector<safe_ptr<graph>> get_all_graphs()\r
534 //{\r
535 //      std::vector<safe_ptr<graph>> graphs;\r
536 //      BOOST_FOREACH(auto& graph, g_graphs)\r
537 //              graphs.push_back(graph->clone());\r
538 //\r
539 //      return graphs;\r
540 //}\r
541 //\r
542 //}\r
543 \r
544 }}