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