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