]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
50f0455601e9a144d46f79690cffee765bbd9683
[casparcg] / common / diagnostics / graph.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "graph.h"\r
25 \r
26 #pragma warning (disable : 4244)\r
27 \r
28 #include "../concurrency/executor.h"\r
29 #include "../env.h"\r
30 \r
31 #include <SFML/Graphics.hpp>\r
32 \r
33 #include <boost/foreach.hpp>\r
34 #include <boost/optional.hpp>\r
35 #include <boost/circular_buffer.hpp>\r
36 #include <boost/range/algorithm_ext/erase.hpp>\r
37 \r
38 #include <numeric>\r
39 #include <map>\r
40 #include <array>\r
41 \r
42 namespace caspar { namespace diagnostics {\r
43                 \r
44 struct drawable : public sf::Drawable\r
45 {\r
46         virtual ~drawable(){}\r
47         virtual void render(sf::RenderTarget& target) = 0;\r
48         virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}\r
49 };\r
50 \r
51 class context : public drawable\r
52 {       \r
53         std::unique_ptr<sf::RenderWindow> window_;\r
54         \r
55         std::list<std::shared_ptr<drawable>> drawables_;\r
56                 \r
57         executor executor_;\r
58 public:                                 \r
59 \r
60         template<typename Func>\r
61         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
62         {       \r
63                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
64         }\r
65 \r
66         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
67         {\r
68                 if(!drawable)\r
69                         return;\r
70 \r
71                 begin_invoke([=]\r
72                 {\r
73                         get_instance().do_register_drawable(drawable);\r
74                 });\r
75         }\r
76 \r
77         static void show(bool value)\r
78         {\r
79                 begin_invoke([=]\r
80                 {       \r
81                         get_instance().do_show(value);\r
82                 });\r
83         }\r
84                                 \r
85 private:\r
86         context() : executor_(L"diagnostics")\r
87         {\r
88                 executor_.set_priority_class(below_normal_priority_class);\r
89         }\r
90 \r
91         void do_show(bool value)\r
92         {\r
93                 if(value)\r
94                 {\r
95                         if(!window_)\r
96                         {\r
97                                 window_.reset(new sf::RenderWindow(sf::VideoMode(600, 1000), "CasparCG Diagnostics"));\r
98                                 window_->SetPosition(0, 0);\r
99                                 window_->SetActive();\r
100                                 glEnable(GL_BLEND);\r
101                                 glEnable(GL_LINE_SMOOTH);\r
102                                 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
103                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
104                                 tick();\r
105                         }\r
106                 }\r
107                 else\r
108                         window_.reset();\r
109         }\r
110 \r
111         void tick()\r
112         {\r
113                 if(!window_)\r
114                         return;\r
115 \r
116                 sf::Event e;\r
117                 while(window_->GetEvent(e))\r
118                 {\r
119                         if(e.Type == sf::Event::Closed)\r
120                         {\r
121                                 window_.reset();\r
122                                 return;\r
123                         }\r
124                 }               \r
125                 glClear(GL_COLOR_BUFFER_BIT);\r
126                 window_->Draw(*this);\r
127                 window_->Display();\r
128                 boost::this_thread::sleep(boost::posix_time::milliseconds(20));\r
129                 executor_.begin_invoke([this]{tick();});\r
130         }\r
131 \r
132         void render(sf::RenderTarget& target)\r
133         {\r
134                 auto count = std::max<size_t>(8, drawables_.size());\r
135                 float target_dy = 1.0f/static_cast<float>(count);\r
136 \r
137                 float last_y = 0.0f;\r
138                 int n = 0;\r
139                 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)\r
140                 {\r
141                         auto& drawable = *it;\r
142                         if(!drawable.unique())\r
143                         {\r
144                                 drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));\r
145                                 float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);\r
146                                 drawable->SetPosition(0.0f, target_y);                  \r
147                                 target.Draw(*drawable);                         \r
148                                 ++it;           \r
149                         }\r
150                         else    \r
151                                 it = drawables_.erase(it);                      \r
152                 }               \r
153         }       \r
154         \r
155         void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
156         {\r
157                 if(std::find(drawables_.begin(), drawables_.end(), drawable) == drawables_.end())\r
158                         drawables_.push_back(drawable);\r
159         }\r
160         \r
161         static context& get_instance()\r
162         {\r
163                 static context impl;\r
164                 return impl;\r
165         }\r
166 };\r
167 \r
168 class guide : public drawable\r
169 {\r
170         float value_;\r
171         color c_;\r
172 public:\r
173         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
174                 : value_(0.0f)\r
175                 , c_(c){}\r
176 \r
177         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
178                 : value_(value)\r
179                 , c_(c){}\r
180                         \r
181         void set_color(color c) {c_ = c;}\r
182 \r
183         void render(sf::RenderTarget&)\r
184         {               \r
185                 glEnable(GL_LINE_STIPPLE);\r
186                 glLineStipple(3, 0xAAAA);\r
187                 glBegin(GL_LINE_STRIP); \r
188                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
189                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
190                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
191                 glEnd();\r
192                 glDisable(GL_LINE_STIPPLE);\r
193         }\r
194 };\r
195 \r
196 class line : public drawable\r
197 {\r
198         boost::optional<diagnostics::guide> guide_;\r
199         boost::circular_buffer<std::pair<double, bool>> line_data_;\r
200 \r
201         boost::circular_buffer<double>  tick_data_;\r
202         bool                                                    tick_tag_;\r
203         color c_;\r
204 public:\r
205         line(size_t res = 600)\r
206                 : line_data_(res)\r
207                 , tick_data_(50)\r
208                 , tick_tag_(false)\r
209                 , c_(1.0f, 1.0f, 1.0f)\r
210         {\r
211                 line_data_.push_back(std::make_pair(-1.0f, false));\r
212         }\r
213         \r
214         void update(double value)\r
215         {\r
216                 tick_data_.push_back(value);\r
217         }\r
218 \r
219         void set(double value)\r
220         {\r
221                 tick_data_.clear();\r
222                 tick_data_.push_back(value);\r
223         }\r
224         \r
225         void tag()\r
226         {\r
227                 tick_tag_ = true;\r
228         }\r
229 \r
230         void guide(const guide& guide)\r
231         {\r
232                 guide_ = guide;\r
233                 guide_->set_color(c_);\r
234         }\r
235         \r
236         void set_color(color c)\r
237         {\r
238                 c_ = c;\r
239                 if(guide_)\r
240                         guide_->set_color(c_);\r
241         }\r
242 \r
243         color get_color() const { return c_; }\r
244         \r
245         void render(sf::RenderTarget& target)\r
246         {\r
247                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
248                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
249 \r
250                 if(!tick_data_.empty())\r
251                 {\r
252                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
253                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
254                         tick_data_.clear();\r
255                 }\r
256                 else if(!line_data_.empty())\r
257                 {\r
258                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
259                 }\r
260                 tick_tag_ = false;\r
261 \r
262                 if(guide_)\r
263                         target.Draw(*guide_);\r
264                 \r
265                 glBegin(GL_LINE_STRIP);\r
266                 glColor4f(c_.red, c_.green, c_.blue, 0.8f);             \r
267                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
268                         if(line_data_[n].first > -0.5)\r
269                                 glVertex3d(x+n*dx, std::max(0.05, std::min(0.95, (1.0f-line_data_[n].first)*0.8 + 0.1f)), 0.0);         \r
270                 glEnd();\r
271                                 \r
272                 glEnable(GL_LINE_STIPPLE);\r
273                 glLineStipple(3, 0xAAAA);\r
274                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
275                 {\r
276                         if(line_data_[n].second)\r
277                         {\r
278                                 glBegin(GL_LINE_STRIP);\r
279                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
280                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
281                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
282                                 glEnd();\r
283                         }\r
284                 }\r
285                 glDisable(GL_LINE_STIPPLE);\r
286         }\r
287 };\r
288 \r
289 struct graph::implementation : public drawable\r
290 {\r
291         std::map<std::string, diagnostics::line> lines_;\r
292         std::string name_;\r
293         std::string text_;\r
294         \r
295         implementation(const std::string& name) \r
296                 : name_(name)\r
297                 , text_(name_){}\r
298         \r
299         void set_text(const std::string& value)\r
300         {\r
301                 text_ = value;\r
302         }\r
303 \r
304         void update(const std::string& name, double value)\r
305         {\r
306                 lines_[name].update(value);\r
307         }\r
308 \r
309         void set(const std::string& name, double value)\r
310         {\r
311                 lines_[name].set(value);\r
312         }\r
313 \r
314         void tag(const std::string& name)\r
315         {\r
316                 lines_[name].tag();\r
317         }\r
318 \r
319         void set_color(const std::string& name, color c)\r
320         {\r
321                 lines_[name].set_color(c);\r
322         }\r
323         \r
324         void guide(const std::string& name, double value)\r
325         {\r
326                 lines_[name].guide(diagnostics::guide(value));  \r
327         }\r
328         \r
329 private:\r
330         void render(sf::RenderTarget& target)\r
331         {\r
332                 const size_t text_size = 15;\r
333                 const size_t text_margin = 2;\r
334                 const size_t text_offset = (text_size+text_margin*2)*2;\r
335 \r
336                 sf::String text(text_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
337                 text.SetStyle(sf::String::Italic);\r
338                 text.Move(text_margin, text_margin);\r
339                 \r
340                 glPushMatrix();\r
341                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
342                         target.Draw(text);\r
343                         float x_offset = text_margin;\r
344                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
345                         {                                               \r
346                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
347                                 line_text.SetPosition(x_offset, text_margin+text_offset/2);\r
348                                 auto c = it->second.get_color();\r
349                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
350                                 target.Draw(line_text);\r
351                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
352                         }\r
353 \r
354                         glDisable(GL_TEXTURE_2D);\r
355                 glPopMatrix();\r
356 \r
357                 glBegin(GL_QUADS);\r
358                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
359                         glVertex2f(1.0f, 0.99f);\r
360                         glVertex2f(0.0f, 0.99f);\r
361                         glVertex2f(0.0f, 0.01f);        \r
362                         glVertex2f(1.0f, 0.01f);        \r
363                 glEnd();\r
364 \r
365                 glPushMatrix();\r
366                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
367                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
368                 \r
369                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
370                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
371 \r
372                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
373                                 target.Draw(it->second);\r
374                 \r
375                 glPopMatrix();\r
376         }\r
377 \r
378         implementation(implementation&);\r
379         implementation& operator=(implementation&);\r
380 };\r
381         \r
382 graph::graph() : impl_(new implementation(""))\r
383 {\r
384 \r
385 }\r
386 \r
387 void graph::set_text(const std::string& value)\r
388 {\r
389         auto p = impl_;\r
390         context::begin_invoke([=]\r
391         {       \r
392                 p->set_text(value);\r
393         });\r
394 }\r
395 \r
396 void graph::set_text(const std::wstring& value)\r
397 {\r
398         set_text(narrow(value));\r
399 }\r
400 \r
401 void graph::update_value(const std::string& name, double value)\r
402 {\r
403         auto p = impl_;\r
404         context::begin_invoke([=]\r
405         {       \r
406                 p->update(name, value);\r
407         });\r
408 }\r
409 void graph::set_value(const std::string& name, double value)\r
410 {       \r
411         auto p = impl_;\r
412         context::begin_invoke([=]\r
413         {       \r
414                 p->set(name, value);\r
415         });     \r
416 }\r
417 void graph::set_color(const std::string& name, color c)\r
418 {               \r
419         auto p = impl_;\r
420         context::begin_invoke([=]\r
421         {       \r
422                 p->set_color(name, c);\r
423         });\r
424 }\r
425 void graph::add_tag(const std::string& name)\r
426 {               \r
427         auto p = impl_;\r
428         context::begin_invoke([=]\r
429         {       \r
430                 p->tag(name);\r
431         });\r
432 }\r
433 void graph::add_guide(const std::string& name, double value)\r
434 {       \r
435         auto p = impl_;\r
436         context::begin_invoke([=]\r
437         {       \r
438                 p->guide(name, value);\r
439         });\r
440 }\r
441 \r
442 void register_graph(const safe_ptr<graph>& graph)\r
443 {\r
444         context::register_drawable(graph->impl_);\r
445 }\r
446 \r
447 void show_graphs(bool value)\r
448 {\r
449         context::show(value);\r
450 }\r
451 \r
452 //namespace v2\r
453 //{     \r
454 //      \r
455 //struct line::implementation\r
456 //{\r
457 //      std::wstring name_;\r
458 //      boost::circular_buffer<data> ticks_;\r
459 //\r
460 //      implementation(const std::wstring& name) \r
461 //              : name_(name)\r
462 //              , ticks_(1024){}\r
463 //      \r
464 //      void update_value(float value)\r
465 //      {\r
466 //              ticks_.push_back();\r
467 //              ticks_.back().value = value;\r
468 //      }\r
469 //\r
470 //      void set_value(float value)\r
471 //      {\r
472 //              ticks_.clear();\r
473 //              update_value(value);\r
474 //      }\r
475 //};\r
476 //\r
477 //line::line(){}\r
478 //line::line(const std::wstring& name) : impl_(new implementation(name)){}\r
479 //std::wstring line::print() const {return impl_->name_;}\r
480 //void line::update_value(float value){impl_->update_value(value);}\r
481 //void line::set_value(float value){impl_->set_value(value);}\r
482 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}\r
483 //\r
484 //struct graph::implementation\r
485 //{\r
486 //      std::map<std::wstring, line> lines_;\r
487 //      color                                            color_;\r
488 //      printer                                          printer_;\r
489 //\r
490 //      implementation(const std::wstring& name) \r
491 //              : printer_([=]{return name;}){}\r
492 //\r
493 //      implementation(const printer& parent_printer) \r
494 //              : printer_(parent_printer){}\r
495 //      \r
496 //      void update_value(const std::wstring& name, float value)\r
497 //      {\r
498 //              auto it = lines_.find(name);\r
499 //              if(it == lines_.end())\r
500 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
501 //\r
502 //              it->second.update_value(value);\r
503 //      }\r
504 //\r
505 //      void set_value(const std::wstring& name, float value)\r
506 //      {\r
507 //              auto it = lines_.find(name);\r
508 //              if(it == lines_.end())\r
509 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
510 //\r
511 //              it->second.set_value(value);\r
512 //      }\r
513 //      \r
514 //      void set_color(const std::wstring& name, color color)\r
515 //      {\r
516 //              color_ = color;\r
517 //      }\r
518 //\r
519 //      std::map<std::wstring, line>& get_lines()\r
520 //      {\r
521 //              return lines_;\r
522 //      }\r
523 //      \r
524 //      color get_color() const\r
525 //      {\r
526 //              return color_;\r
527 //      }\r
528 //\r
529 //      std::wstring print() const\r
530 //      {\r
531 //              return printer_ ? printer_() : L"graph";\r
532 //      }\r
533 //};\r
534 //      \r
535 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}\r
536 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}\r
537 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}\r
538 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}\r
539 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}\r
540 //color graph::get_color() const {return impl_->get_color();}\r
541 //std::wstring graph::print() const {return impl_->print();}\r
542 //\r
543 //safe_ptr<graph> graph::clone() const \r
544 //{\r
545 //      safe_ptr<graph> clone(new graph(std::wstring(L"")));\r
546 //      clone->impl_->printer_ = impl_->printer_;\r
547 //      clone->impl_->lines_ = impl_->lines_;\r
548 //      clone->impl_->color_ = impl_->color_;   \r
549 //}\r
550 //\r
551 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}\r
552 //\r
553 //std::vector<safe_ptr<graph>> g_graphs;\r
554 //\r
555 //safe_ptr<graph> create_graph(const std::string& name)\r
556 //{\r
557 //      g_graphs.push_back(make_safe<graph>(name));\r
558 //      return g_graphs.back();\r
559 //}\r
560 //\r
561 //safe_ptr<graph> create_graph(const printer& parent_printer)\r
562 //{\r
563 //      g_graphs.push_back(make_safe<graph>(parent_printer));\r
564 //      return g_graphs.back();\r
565 //}\r
566 //\r
567 //static std::vector<safe_ptr<graph>> get_all_graphs()\r
568 //{\r
569 //      std::vector<safe_ptr<graph>> graphs;\r
570 //      BOOST_FOREACH(auto& graph, g_graphs)\r
571 //              graphs.push_back(graph->clone());\r
572 //\r
573 //      return graphs;\r
574 //}\r
575 //\r
576 //}\r
577 \r
578 }}