1 #include "../stdafx.h"
\r
5 #pragma warning (disable : 4244)
\r
7 #include "../concurrency/executor.h"
\r
10 #include <SFML/Graphics.hpp>
\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
21 namespace caspar { namespace diagnostics {
\r
23 struct drawable : public sf::Drawable
\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
30 class context : public drawable
\r
32 sf::RenderWindow window_;
\r
34 std::list<std::shared_ptr<drawable>> drawables_;
\r
39 template<typename Func>
\r
40 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
42 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
45 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
52 get_instance().do_register_drawable(drawable);
\r
57 context() : executor_(L"diagnostics")
\r
60 executor_.begin_invoke([this]
\r
62 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
63 window_.SetPosition(0, 0);
\r
64 window_.SetActive();
\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
76 while(window_.GetEvent(e)){}
\r
77 glClear(GL_COLOR_BUFFER_BIT);
\r
78 window_.Draw(*this);
\r
80 boost::this_thread::sleep(boost::posix_time::milliseconds(20));
\r
81 executor_.begin_invoke([this]{tick();});
\r
84 void render(sf::RenderTarget& target)
\r
86 auto count = std::max<size_t>(8, drawables_.size());
\r
87 float target_dy = 1.0f/static_cast<float>(count);
\r
89 float last_y = 0.0f;
\r
91 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
93 auto& drawable = *it;
\r
94 if(!drawable.unique())
\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
103 it = drawables_.erase(it);
\r
107 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
\r
109 drawables_.push_back(drawable);
\r
112 static context& get_instance()
\r
114 static context impl;
\r
119 class guide : public drawable
\r
124 guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
128 guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
132 void set_color(color c) {c_ = c;}
\r
134 void render(sf::RenderTarget&)
\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
143 glDisable(GL_LINE_STIPPLE);
\r
147 class line : public drawable
\r
149 boost::optional<diagnostics::guide> guide_;
\r
150 boost::circular_buffer<std::pair<float, bool>> line_data_;
\r
152 std::vector<float> tick_data_;
\r
156 line(size_t res = 600)
\r
159 , c_(1.0f, 1.0f, 1.0f)
\r
161 line_data_.push_back(std::make_pair(-1.0f, false));
\r
164 void update(float value)
\r
166 tick_data_.push_back(value);
\r
169 void set(float value)
\r
171 tick_data_.clear();
\r
172 tick_data_.push_back(value);
\r
180 void guide(const guide& guide)
\r
183 guide_->set_color(c_);
\r
186 void set_color(color c)
\r
190 guide_->set_color(c_);
\r
193 color get_color() const { return c_; }
\r
195 void render(sf::RenderTarget& target)
\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
200 if(!tick_data_.empty())
\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
206 else if(!line_data_.empty())
\r
208 line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));
\r
213 target.Draw(*guide_);
\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
222 glEnable(GL_LINE_STIPPLE);
\r
223 glLineStipple(3, 0xAAAA);
\r
224 for(size_t n = 0; n < line_data_.size(); ++n)
\r
226 if(line_data_[n].second)
\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
235 glDisable(GL_LINE_STIPPLE);
\r
239 struct graph::implementation : public drawable
\r
241 std::map<std::string, diagnostics::line> lines_;
\r
242 const printer parent_printer_;
\r
247 implementation(const std::string& name)
\r
251 implementation(const printer& parent_printer)
\r
252 : parent_printer_(parent_printer)
\r
253 , name_(parent_printer_ ? narrow(parent_printer_()) : "")
\r
256 void update(const std::string& name, float value)
\r
258 context::begin_invoke([=]
\r
260 lines_[name].update(value);
\r
264 void set(const std::string& name, float value)
\r
266 context::begin_invoke([=]
\r
268 lines_[name].set(value);
\r
272 void tag(const std::string& name)
\r
274 context::begin_invoke([=]
\r
276 lines_[name].tag();
\r
280 void set_color(const std::string& name, color c)
\r
282 context::begin_invoke([=]
\r
284 lines_[name].set_color(c);
\r
288 void guide(const std::string& name, float value)
\r
290 context::begin_invoke([=]
\r
292 lines_[name].guide(diagnostics::guide(value));
\r
297 void render(sf::RenderTarget& target)
\r
299 if(counter_++ > 25) // Don't update name too often since print can be implemented with locks.
\r
302 if(parent_printer_)
\r
303 name_ = narrow(parent_printer_());
\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
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
314 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
316 float x_offset = text_margin;
\r
317 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\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
327 glDisable(GL_TEXTURE_2D);
\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
339 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
340 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\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
345 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
346 target.Draw(it->second);
\r
351 implementation(implementation&);
\r
352 implementation& operator=(implementation&);
\r
355 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)
\r
358 context::register_drawable(impl_);
\r
361 graph::graph(const printer& parent_printer) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(parent_printer) : nullptr)
\r
364 context::register_drawable(impl_);
\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
373 safe_ptr<graph> create_graph(const std::string& name)
\r
375 return safe_ptr<graph>(new graph(name));
\r
377 safe_ptr<graph> create_graph(const printer& parent_printer)
\r
379 return safe_ptr<graph>(new graph(parent_printer));
\r
386 //struct line::implementation
\r
388 // std::wstring name_;
\r
389 // boost::circular_buffer<data> ticks_;
\r
391 // implementation(const std::wstring& name)
\r
393 // , ticks_(1024){}
\r
395 // void update_value(float value)
\r
397 // ticks_.push_back();
\r
398 // ticks_.back().value = value;
\r
401 // void set_value(float value)
\r
404 // update_value(value);
\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
415 //struct graph::implementation
\r
417 // std::map<std::wstring, line> lines_;
\r
419 // printer printer_;
\r
421 // implementation(const std::wstring& name)
\r
422 // : printer_([=]{return name;}){}
\r
424 // implementation(const printer& parent_printer)
\r
425 // : printer_(parent_printer){}
\r
427 // void update_value(const std::wstring& name, float value)
\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
433 // it->second.update_value(value);
\r
436 // void set_value(const std::wstring& name, float value)
\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
442 // it->second.set_value(value);
\r
445 // void set_color(const std::wstring& name, color color)
\r
450 // std::map<std::wstring, line>& get_lines()
\r
455 // color get_color() const
\r
460 // std::wstring print() const
\r
462 // return printer_ ? printer_() : L"graph";
\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
474 //safe_ptr<graph> graph::clone() const
\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
482 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}
\r
484 //std::vector<safe_ptr<graph>> g_graphs;
\r
486 //safe_ptr<graph> create_graph(const std::string& name)
\r
488 // g_graphs.push_back(make_safe<graph>(name));
\r
489 // return g_graphs.back();
\r
492 //safe_ptr<graph> create_graph(const printer& parent_printer)
\r
494 // g_graphs.push_back(make_safe<graph>(parent_printer));
\r
495 // return g_graphs.back();
\r
498 //static std::vector<safe_ptr<graph>> get_all_graphs()
\r
500 // std::vector<safe_ptr<graph>> graphs;
\r
501 // BOOST_FOREACH(auto& graph, g_graphs)
\r
502 // graphs.push_back(graph->clone());
\r