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