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