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
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>(10, 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
246 implementation(const std::string& name)
\r
249 void update(const std::string& name, float value)
\r
251 context::begin_invoke([=]
\r
253 lines_[name].update(value);
\r
257 void set(const std::string& name, float value)
\r
259 context::begin_invoke([=]
\r
261 lines_[name].set(value);
\r
265 void tag(const std::string& name)
\r
267 context::begin_invoke([=]
\r
269 lines_[name].tag();
\r
273 void set_color(const std::string& name, color c)
\r
275 context::begin_invoke([=]
\r
277 lines_[name].set_color(c);
\r
281 void guide(const std::string& name, float value)
\r
283 context::begin_invoke([=]
\r
285 lines_[name].guide(diagnostics::guide(value));
\r
290 void render(sf::RenderTarget& target)
\r
292 const size_t text_size = 15;
\r
293 const size_t text_margin = 2;
\r
294 const size_t text_offset = text_size+text_margin*2;
\r
296 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
297 text.SetStyle(sf::String::Italic);
\r
298 text.Move(text_margin, text_margin);
\r
301 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
303 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;
\r
304 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
306 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
307 line_text.SetPosition(x_offset, text_margin);
\r
308 auto c = it->second.get_color();
\r
309 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
310 target.Draw(line_text);
\r
311 x_offset = line_text.GetRect().Right + text_margin*2;
\r
314 glDisable(GL_TEXTURE_2D);
\r
318 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
319 glVertex2f(1.0f, 0.99f);
\r
320 glVertex2f(0.0f, 0.99f);
\r
321 glVertex2f(0.0f, 0.01f);
\r
322 glVertex2f(1.0f, 0.01f);
\r
326 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
327 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
329 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
330 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
332 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
333 target.Draw(it->second);
\r
338 implementation(implementation&);
\r
339 implementation& operator=(implementation&);
\r
342 graph::graph(const std::string& name) : impl_(env::properties().get("configuration.diagnostics.graphs", true) ? new implementation(name) : nullptr)
\r
345 context::register_drawable(impl_);
\r
348 void graph::update(const std::string& name, float value){if(impl_)impl_->update(name, value);}
\r
349 void graph::set(const std::string& name, float value){if(impl_)impl_->set(name, value);}
\r
350 void graph::tag(const std::string& name){if(impl_)impl_->tag(name);}
\r
351 void graph::guide(const std::string& name, float value){if(impl_)impl_->guide(name, value);}
\r
352 void graph::set_color(const std::string& name, color c){if(impl_)impl_->set_color(name, c);}
\r
354 safe_ptr<graph> create_graph(const std::string& name)
\r
356 return safe_ptr<graph>(new graph(name));
\r