]> 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 #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                 context::begin_invoke([=]\r
259                 {\r
260                         lines_[name].update(value);\r
261                 });\r
262         }\r
263 \r
264         void set(const std::string& name, float value)\r
265         {\r
266                 context::begin_invoke([=]\r
267                 {\r
268                         lines_[name].set(value);\r
269                 });\r
270         }\r
271 \r
272         void tag(const std::string& name)\r
273         {\r
274                 context::begin_invoke([=]\r
275                 {\r
276                         lines_[name].tag();\r
277                 });\r
278         }\r
279 \r
280         void set_color(const std::string& name, color c)\r
281         {\r
282                 context::begin_invoke([=]\r
283                 {\r
284                         lines_[name].set_color(c);\r
285                 });\r
286         }\r
287         \r
288         void guide(const std::string& name, float value)\r
289         {\r
290                 context::begin_invoke([=]\r
291                 {\r
292                         lines_[name].guide(diagnostics::guide(value));\r
293                 });\r
294         }\r
295         \r
296 private:\r
297         void render(sf::RenderTarget& target)\r
298         {\r
299                 if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.\r
300                 {\r
301                         counter_ = 0;\r
302                         if(parent_printer_)\r
303                                 name_ = narrow(parent_printer_());\r
304                 }\r
305                 const size_t text_size = 15;\r
306                 const size_t text_margin = 2;\r
307                 const size_t text_offset = (text_size+text_margin*2)*2;\r
308 \r
309                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
310                 text.SetStyle(sf::String::Italic);\r
311                 text.Move(text_margin, text_margin);\r
312                 \r
313                 glPushMatrix();\r
314                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
315                         target.Draw(text);\r
316                         float x_offset = text_margin;\r
317                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
318                         {                                               \r
319                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
320                                 line_text.SetPosition(x_offset, text_margin+text_offset/2);\r
321                                 auto c = it->second.get_color();\r
322                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
323                                 target.Draw(line_text);\r
324                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
325                         }\r
326 \r
327                         glDisable(GL_TEXTURE_2D);\r
328                 glPopMatrix();\r
329 \r
330                 glBegin(GL_QUADS);\r
331                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
332                         glVertex2f(1.0f, 0.99f);\r
333                         glVertex2f(0.0f, 0.99f);\r
334                         glVertex2f(0.0f, 0.01f);        \r
335                         glVertex2f(1.0f, 0.01f);        \r
336                 glEnd();\r
337 \r
338                 glPushMatrix();\r
339                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
340                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
341                 \r
342                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
343                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
344 \r
345                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
346                                 target.Draw(it->second);\r
347                 \r
348                 glPopMatrix();\r
349         }\r
350 \r
351         implementation(implementation&);\r
352         implementation& operator=(implementation&);\r
353 };\r
354         \r
355 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)\r
356 {\r
357         if(impl_)\r
358                 context::register_drawable(impl_);\r
359 }\r
360 \r
361 graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)\r
362 {\r
363         if(impl_)\r
364                 context::register_drawable(impl_);\r
365 }\r
366 \r
367 void graph::update_value(const std::string& name, float value){if(impl_)impl_->update(name, value);}\r
368 void graph::set_value(const std::string& name, float value){if(impl_)impl_->set(name, value);}\r
369 void graph::set_color(const std::string& name, color c){if(impl_)impl_->set_color(name, c);}\r
370 void graph::add_tag(const std::string& name){if(impl_)impl_->tag(name);}\r
371 void graph::add_guide(const std::string& name, float value){if(impl_)impl_->guide(name, value);}\r
372 \r
373 safe_ptr<graph> create_graph(const std::string& name)\r
374 {\r
375         return safe_ptr<graph>(new graph(name));\r
376 }\r
377 safe_ptr<graph> create_graph(const printer& parent_printer)\r
378 {\r
379         return safe_ptr<graph>(new graph(parent_printer));\r
380 }\r
381 \r
382 \r
383 //namespace v2\r
384 //{     \r
385 //      \r
386 //struct line::implementation\r
387 //{\r
388 //      std::wstring name_;\r
389 //      boost::circular_buffer<data> ticks_;\r
390 //\r
391 //      implementation(const std::wstring& name) \r
392 //              : name_(name)\r
393 //              , ticks_(1024){}\r
394 //      \r
395 //      void update_value(float value)\r
396 //      {\r
397 //              ticks_.push_back();\r
398 //              ticks_.back().value = value;\r
399 //      }\r
400 //\r
401 //      void set_value(float value)\r
402 //      {\r
403 //              ticks_.clear();\r
404 //              update_value(value);\r
405 //      }\r
406 //};\r
407 //\r
408 //line::line(){}\r
409 //line::line(const std::wstring& name) : impl_(new implementation(name)){}\r
410 //std::wstring line::print() const {return impl_->name_;}\r
411 //void line::update_value(float value){impl_->update_value(value);}\r
412 //void line::set_value(float value){impl_->set_value(value);}\r
413 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}\r
414 //\r
415 //struct graph::implementation\r
416 //{\r
417 //      std::map<std::wstring, line> lines_;\r
418 //      color                                            color_;\r
419 //      printer                                          printer_;\r
420 //\r
421 //      implementation(const std::wstring& name) \r
422 //              : printer_([=]{return name;}){}\r
423 //\r
424 //      implementation(const printer& parent_printer) \r
425 //              : printer_(parent_printer){}\r
426 //      \r
427 //      void update_value(const std::wstring& name, float value)\r
428 //      {\r
429 //              auto it = lines_.find(name);\r
430 //              if(it == lines_.end())\r
431 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
432 //\r
433 //              it->second.update_value(value);\r
434 //      }\r
435 //\r
436 //      void set_value(const std::wstring& name, float value)\r
437 //      {\r
438 //              auto it = lines_.find(name);\r
439 //              if(it == lines_.end())\r
440 //                      it = lines_.insert(std::make_pair(name, line(name))).first;\r
441 //\r
442 //              it->second.set_value(value);\r
443 //      }\r
444 //      \r
445 //      void set_color(const std::wstring& name, color color)\r
446 //      {\r
447 //              color_ = color;\r
448 //      }\r
449 //\r
450 //      std::map<std::wstring, line>& get_lines()\r
451 //      {\r
452 //              return lines_;\r
453 //      }\r
454 //      \r
455 //      color get_color() const\r
456 //      {\r
457 //              return color_;\r
458 //      }\r
459 //\r
460 //      std::wstring print() const\r
461 //      {\r
462 //              return printer_ ? printer_() : L"graph";\r
463 //      }\r
464 //};\r
465 //      \r
466 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}\r
467 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}\r
468 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}\r
469 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}\r
470 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}\r
471 //color graph::get_color() const {return impl_->get_color();}\r
472 //std::wstring graph::print() const {return impl_->print();}\r
473 //\r
474 //safe_ptr<graph> graph::clone() const \r
475 //{\r
476 //      safe_ptr<graph> clone(new graph(std::wstring(L"")));\r
477 //      clone->impl_->printer_ = impl_->printer_;\r
478 //      clone->impl_->lines_ = impl_->lines_;\r
479 //      clone->impl_->color_ = impl_->color_;   \r
480 //}\r
481 //\r
482 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}\r
483 //\r
484 //std::vector<safe_ptr<graph>> g_graphs;\r
485 //\r
486 //safe_ptr<graph> create_graph(const std::string& name)\r
487 //{\r
488 //      g_graphs.push_back(make_safe<graph>(name));\r
489 //      return g_graphs.back();\r
490 //}\r
491 //\r
492 //safe_ptr<graph> create_graph(const printer& parent_printer)\r
493 //{\r
494 //      g_graphs.push_back(make_safe<graph>(parent_printer));\r
495 //      return g_graphs.back();\r
496 //}\r
497 //\r
498 //static std::vector<safe_ptr<graph>> get_all_graphs()\r
499 //{\r
500 //      std::vector<safe_ptr<graph>> graphs;\r
501 //      BOOST_FOREACH(auto& graph, g_graphs)\r
502 //              graphs.push_back(graph->clone());\r
503 //\r
504 //      return graphs;\r
505 //}\r
506 //\r
507 //}\r
508 \r
509 }}