1 #include "../stdafx.h"
\r
5 #pragma warning (disable : 4244)
\r
7 #include "../concurrency/executor.h"
\r
8 #include "../utility/timer.h"
\r
11 #include <SFML/Graphics.hpp>
\r
13 #include <boost/foreach.hpp>
\r
14 #include <boost/optional.hpp>
\r
15 #include <boost/circular_buffer.hpp>
\r
16 #include <boost/range/algorithm_ext/erase.hpp>
\r
22 namespace caspar { namespace diagnostics {
\r
24 struct drawable : public sf::Drawable
\r
26 virtual ~drawable(){}
\r
27 virtual void render(sf::RenderTarget& target) = 0;
\r
28 virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}
\r
31 class context : public drawable
\r
34 sf::RenderWindow window_;
\r
36 std::list<std::shared_ptr<drawable>> drawables_;
\r
41 template<typename Func>
\r
42 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
44 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
47 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
54 get_instance().do_register_drawable(drawable);
\r
59 context() : executor_(L"diagnostics")
\r
62 executor_.begin_invoke([this]
\r
64 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
65 window_.SetPosition(0, 0);
\r
66 window_.SetActive();
\r
68 glEnable(GL_LINE_SMOOTH);
\r
69 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
70 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
78 while(window_.GetEvent(e)){}
\r
79 glClear(GL_COLOR_BUFFER_BIT);
\r
80 window_.Draw(*this);
\r
82 timer_.tick(1.0/50.0);
\r
83 executor_.begin_invoke([this]{tick();});
\r
86 void render(sf::RenderTarget& target)
\r
88 auto count = std::max<size_t>(8, drawables_.size());
\r
89 float target_dy = 1.0f/static_cast<float>(count);
\r
91 float last_y = 0.0f;
\r
93 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
95 auto& drawable = *it;
\r
96 if(!drawable.unique())
\r
98 drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));
\r
99 float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);
\r
100 drawable->SetPosition(0.0f, target_y);
\r
101 target.Draw(*drawable);
\r
105 it = drawables_.erase(it);
\r
109 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
\r
111 drawables_.push_back(drawable);
\r
114 static context& get_instance()
\r
116 static context impl;
\r
121 class guide : public drawable
\r
126 guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
130 guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
134 void set_color(color c) {c_ = c;}
\r
136 void render(sf::RenderTarget&)
\r
138 glEnable(GL_LINE_STIPPLE);
\r
139 glLineStipple(3, 0xAAAA);
\r
140 glBegin(GL_LINE_STRIP);
\r
141 glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);
\r
142 glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
143 glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
145 glDisable(GL_LINE_STIPPLE);
\r
149 class line : public drawable
\r
151 boost::optional<diagnostics::guide> guide_;
\r
152 boost::circular_buffer<std::pair<float, bool>> line_data_;
\r
154 std::vector<float> tick_data_;
\r
158 line(size_t res = 600)
\r
161 , c_(1.0f, 1.0f, 1.0f)
\r
163 line_data_.push_back(std::make_pair(-1.0f, false));
\r
166 void update(float value)
\r
168 tick_data_.push_back(value);
\r
171 void set(float value)
\r
173 tick_data_.clear();
\r
174 tick_data_.push_back(value);
\r
182 void guide(const guide& guide)
\r
185 guide_->set_color(c_);
\r
188 void set_color(color c)
\r
192 guide_->set_color(c_);
\r
195 color get_color() const { return c_; }
\r
197 void render(sf::RenderTarget& target)
\r
199 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
200 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
202 if(!tick_data_.empty())
\r
204 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
205 line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));
\r
206 tick_data_.clear();
\r
208 else if(!line_data_.empty())
\r
210 line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));
\r
215 target.Draw(*guide_);
\r
217 glBegin(GL_LINE_STRIP);
\r
218 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
219 for(size_t n = 0; n < line_data_.size(); ++n)
\r
220 if(line_data_[n].first > -0.5)
\r
221 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
224 glEnable(GL_LINE_STIPPLE);
\r
225 glLineStipple(3, 0xAAAA);
\r
226 for(size_t n = 0; n < line_data_.size(); ++n)
\r
228 if(line_data_[n].second)
\r
230 glBegin(GL_LINE_STRIP);
\r
231 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);
\r
232 glVertex3f(x+n*dx, 0.0f, 0.0f);
\r
233 glVertex3f(x+n*dx, 1.0f, 0.0f);
\r
237 glDisable(GL_LINE_STIPPLE);
\r
241 struct graph::implementation : public drawable
\r
243 std::map<std::string, diagnostics::line> lines_;
\r
244 const printer parent_printer_;
\r
249 implementation(const std::string& name)
\r
253 implementation(const printer& parent_printer)
\r
254 : parent_printer_(parent_printer)
\r
255 , name_(parent_printer_ ? narrow(parent_printer_()) : "")
\r
258 void update(const std::string& name, float value)
\r
260 context::begin_invoke([=]
\r
262 lines_[name].update(value);
\r
266 void set(const std::string& name, float value)
\r
268 context::begin_invoke([=]
\r
270 lines_[name].set(value);
\r
274 void tag(const std::string& name)
\r
276 context::begin_invoke([=]
\r
278 lines_[name].tag();
\r
282 void set_color(const std::string& name, color c)
\r
284 context::begin_invoke([=]
\r
286 lines_[name].set_color(c);
\r
290 void guide(const std::string& name, float value)
\r
292 context::begin_invoke([=]
\r
294 lines_[name].guide(diagnostics::guide(value));
\r
299 void render(sf::RenderTarget& target)
\r
301 if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.
\r
304 if(parent_printer_)
\r
305 name_ = narrow(parent_printer_());
\r
307 const size_t text_size = 15;
\r
308 const size_t text_margin = 2;
\r
309 const size_t text_offset = (text_size+text_margin*2)*2;
\r
311 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
312 text.SetStyle(sf::String::Italic);
\r
313 text.Move(text_margin, text_margin);
\r
316 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
318 float x_offset = text_margin;
\r
319 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
321 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
322 line_text.SetPosition(x_offset, text_margin+text_offset/2);
\r
323 auto c = it->second.get_color();
\r
324 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
325 target.Draw(line_text);
\r
326 x_offset = line_text.GetRect().Right + text_margin*2;
\r
329 glDisable(GL_TEXTURE_2D);
\r
333 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
334 glVertex2f(1.0f, 0.99f);
\r
335 glVertex2f(0.0f, 0.99f);
\r
336 glVertex2f(0.0f, 0.01f);
\r
337 glVertex2f(1.0f, 0.01f);
\r
341 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
342 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
344 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
345 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
347 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
348 target.Draw(it->second);
\r
353 implementation(implementation&);
\r
354 implementation& operator=(implementation&);
\r
357 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)
\r
360 context::register_drawable(impl_);
\r
363 graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)
\r
366 context::register_drawable(impl_);
\r
369 void graph::update_value(const std::string& name, float value){if(impl_)impl_->update(name, value);}
\r
370 void graph::set_value(const std::string& name, float value){if(impl_)impl_->set(name, value);}
\r
371 void graph::set_color(const std::string& name, color c){if(impl_)impl_->set_color(name, c);}
\r
372 void graph::add_tag(const std::string& name){if(impl_)impl_->tag(name);}
\r
373 void graph::add_guide(const std::string& name, float value){if(impl_)impl_->guide(name, value);}
\r
375 safe_ptr<graph> create_graph(const std::string& name)
\r
377 return safe_ptr<graph>(new graph(name));
\r
379 safe_ptr<graph> create_graph(const printer& parent_printer)
\r
381 return safe_ptr<graph>(new graph(parent_printer));
\r
388 //struct line::implementation
\r
390 // std::wstring name_;
\r
391 // boost::circular_buffer<data> ticks_;
\r
393 // implementation(const std::wstring& name)
\r
395 // , ticks_(1024){}
\r
397 // void update_value(float value)
\r
399 // ticks_.push_back();
\r
400 // ticks_.back().value = value;
\r
403 // void set_value(float value)
\r
406 // update_value(value);
\r
411 //line::line(const std::wstring& name) : impl_(new implementation(name)){}
\r
412 //std::wstring line::print() const {return impl_->name_;}
\r
413 //void line::update_value(float value){impl_->update_value(value);}
\r
414 //void line::set_value(float value){impl_->set_value(value);}
\r
415 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}
\r
417 //struct graph::implementation
\r
419 // std::map<std::wstring, line> lines_;
\r
421 // printer printer_;
\r
423 // implementation(const std::wstring& name)
\r
424 // : printer_([=]{return name;}){}
\r
426 // implementation(const printer& parent_printer)
\r
427 // : printer_(parent_printer){}
\r
429 // void update_value(const std::wstring& name, float value)
\r
431 // auto it = lines_.find(name);
\r
432 // if(it == lines_.end())
\r
433 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
435 // it->second.update_value(value);
\r
438 // void set_value(const std::wstring& name, float value)
\r
440 // auto it = lines_.find(name);
\r
441 // if(it == lines_.end())
\r
442 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
444 // it->second.set_value(value);
\r
447 // void set_color(const std::wstring& name, color color)
\r
452 // std::map<std::wstring, line>& get_lines()
\r
457 // color get_color() const
\r
462 // std::wstring print() const
\r
464 // return printer_ ? printer_() : L"graph";
\r
468 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}
\r
469 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}
\r
470 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}
\r
471 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
\r
472 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}
\r
473 //color graph::get_color() const {return impl_->get_color();}
\r
474 //std::wstring graph::print() const {return impl_->print();}
\r
476 //safe_ptr<graph> graph::clone() const
\r
478 // safe_ptr<graph> clone(new graph(std::wstring(L"")));
\r
479 // clone->impl_->printer_ = impl_->printer_;
\r
480 // clone->impl_->lines_ = impl_->lines_;
\r
481 // clone->impl_->color_ = impl_->color_;
\r
484 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}
\r
486 //std::vector<safe_ptr<graph>> g_graphs;
\r
488 //safe_ptr<graph> create_graph(const std::string& name)
\r
490 // g_graphs.push_back(make_safe<graph>(name));
\r
491 // return g_graphs.back();
\r
494 //safe_ptr<graph> create_graph(const printer& parent_printer)
\r
496 // g_graphs.push_back(make_safe<graph>(parent_printer));
\r
497 // return g_graphs.back();
\r
500 //static std::vector<safe_ptr<graph>> get_all_graphs()
\r
502 // std::vector<safe_ptr<graph>> graphs;
\r
503 // BOOST_FOREACH(auto& graph, g_graphs)
\r
504 // graphs.push_back(graph->clone());
\r