1 #include "../stdafx.h"
\r
5 #pragma warning (disable : 4244)
\r
7 #include "../concurrency/executor.h"
\r
8 #include "../utility/timer.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
33 sf::RenderWindow window_;
\r
35 std::list<std::shared_ptr<drawable>> drawables_;
\r
40 template<typename Func>
\r
41 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
43 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
46 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
50 get_instance().do_register_drawable(drawable);
\r
58 executor_.begin_invoke([this]
\r
60 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
61 window_.SetPosition(0, 0);
\r
62 window_.SetActive();
\r
64 glEnable(GL_LINE_SMOOTH);
\r
65 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
66 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
74 while(window_.GetEvent(e)){}
\r
75 glClear(GL_COLOR_BUFFER_BIT);
\r
76 window_.Draw(*this);
\r
78 timer_.tick(1.0/50.0);
\r
79 executor_.begin_invoke([this]{tick();});
\r
82 void render(sf::RenderTarget& target)
\r
84 auto count = std::max<size_t>(10, drawables_.size());
\r
85 float target_dy = 1.0f/static_cast<float>(count);
\r
87 float last_y = 0.0f;
\r
89 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
91 auto& drawable = *it;
\r
92 if(!drawable.unique())
\r
94 drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));
\r
95 float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);
\r
96 drawable->SetPosition(0.0f, target_y);
\r
97 target.Draw(*drawable);
\r
101 it = drawables_.erase(it);
\r
105 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
\r
107 drawables_.push_back(drawable);
\r
110 static context& get_instance()
\r
112 static context impl;
\r
117 class guide : public drawable
\r
122 guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
126 guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
130 void set_color(color c) {c_ = c;}
\r
132 void render(sf::RenderTarget&)
\r
134 glEnable(GL_LINE_STIPPLE);
\r
135 glLineStipple(3, 0xAAAA);
\r
136 glBegin(GL_LINE_STRIP);
\r
137 glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);
\r
138 glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
139 glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
141 glDisable(GL_LINE_STIPPLE);
\r
145 class line : public drawable
\r
147 boost::optional<diagnostics::guide> guide_;
\r
148 boost::circular_buffer<std::pair<float, bool>> line_data_;
\r
150 std::vector<float> tick_data_;
\r
154 line(size_t res = 600)
\r
157 , c_(1.0f, 1.0f, 1.0f)
\r
159 line_data_.push_back(std::make_pair(-1.0f, false));
\r
162 void update(float value)
\r
164 tick_data_.push_back(value);
\r
167 void set(float value)
\r
169 tick_data_.clear();
\r
170 tick_data_.push_back(value);
\r
178 void guide(const guide& guide)
\r
181 guide_->set_color(c_);
\r
184 void set_color(color c)
\r
188 guide_->set_color(c_);
\r
191 color get_color() const { return c_; }
\r
193 void render(sf::RenderTarget& target)
\r
195 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
196 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
198 if(!tick_data_.empty())
\r
200 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
201 line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));
\r
202 tick_data_.clear();
\r
204 else if(!line_data_.empty())
\r
206 line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));
\r
211 target.Draw(*guide_);
\r
213 glBegin(GL_LINE_STRIP);
\r
214 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
215 for(size_t n = 0; n < line_data_.size(); ++n)
\r
216 if(line_data_[n].first > -0.5)
\r
217 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 glEnable(GL_LINE_STIPPLE);
\r
221 glLineStipple(3, 0xAAAA);
\r
222 for(size_t n = 0; n < line_data_.size(); ++n)
\r
224 if(line_data_[n].second)
\r
226 glBegin(GL_LINE_STRIP);
\r
227 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);
\r
228 glVertex3f(x+n*dx, 0.0f, 0.0f);
\r
229 glVertex3f(x+n*dx, 1.0f, 0.0f);
\r
233 glDisable(GL_LINE_STIPPLE);
\r
237 struct graph::implementation : public drawable
\r
239 std::map<std::string, diagnostics::line> lines_;
\r
242 implementation(const std::string& name)
\r
245 void update(const std::string& name, float value)
\r
247 context::begin_invoke([=]
\r
249 lines_[name].update(value);
\r
253 void set(const std::string& name, float value)
\r
255 context::begin_invoke([=]
\r
257 lines_[name].set(value);
\r
261 void tag(const std::string& name)
\r
263 context::begin_invoke([=]
\r
265 lines_[name].tag();
\r
269 void set_color(const std::string& name, color c)
\r
271 context::begin_invoke([=]
\r
273 lines_[name].set_color(c);
\r
277 void guide(const std::string& name, float value)
\r
279 context::begin_invoke([=]
\r
281 lines_[name].guide(diagnostics::guide(value));
\r
286 void render(sf::RenderTarget& target)
\r
288 const size_t text_size = 15;
\r
289 const size_t text_margin = 2;
\r
290 const size_t text_offset = text_size+text_margin*2;
\r
292 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
293 text.SetStyle(sf::String::Italic);
\r
294 text.Move(text_margin, text_margin);
\r
297 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
299 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;
\r
300 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
302 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
303 line_text.SetPosition(x_offset, text_margin);
\r
304 auto c = it->second.get_color();
\r
305 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
306 target.Draw(line_text);
\r
307 x_offset = line_text.GetRect().Right + text_margin*2;
\r
310 glDisable(GL_TEXTURE_2D);
\r
314 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
315 glVertex2f(1.0f, 0.99f);
\r
316 glVertex2f(0.0f, 0.99f);
\r
317 glVertex2f(0.0f, 0.01f);
\r
318 glVertex2f(1.0f, 0.01f);
\r
322 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
323 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
325 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
326 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
328 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
329 target.Draw(it->second);
\r
334 implementation(implementation&);
\r
335 implementation& operator=(implementation&);
\r
338 graph::graph(const std::string& name) : impl_(new implementation(name))
\r
340 context::register_drawable(impl_);
\r
343 void graph::update(const std::string& name, float value){impl_->update(name, value);}
\r
344 void graph::set(const std::string& name, float value){impl_->set(name, value);}
\r
345 void graph::tag(const std::string& name){impl_->tag(name);}
\r
346 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}
\r
347 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}
\r
349 safe_ptr<graph> create_graph(const std::string& name)
\r
351 return safe_ptr<graph>(new graph(name));
\r