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