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
172 void guide(const guide& guide)
\r
175 guide_->set_color(c_);
\r
178 void set_color(color c)
\r
182 guide_->set_color(c_);
\r
185 color get_color() const { return c_; }
\r
187 void render(sf::RenderTarget& target)
\r
189 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
190 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
192 if(!tick_data_.empty())
\r
194 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
195 line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));
\r
196 tick_data_.clear();
\r
198 else if(!line_data_.empty())
\r
200 line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));
\r
205 target.Draw(*guide_);
\r
207 glBegin(GL_LINE_STRIP);
\r
208 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
209 for(size_t n = 0; n < line_data_.size(); ++n)
\r
210 if(line_data_[n].first > -0.5)
\r
211 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
214 glEnable(GL_LINE_STIPPLE);
\r
215 glLineStipple(3, 0xAAAA);
\r
216 for(size_t n = 0; n < line_data_.size(); ++n)
\r
218 if(line_data_[n].second)
\r
220 glBegin(GL_LINE_STRIP);
\r
221 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);
\r
222 glVertex3f(x+n*dx, 0.0f, 0.0f);
\r
223 glVertex3f(x+n*dx, 1.0f, 0.0f);
\r
227 glDisable(GL_LINE_STIPPLE);
\r
231 struct graph::implementation : public drawable
\r
233 std::map<std::string, diagnostics::line> lines_;
\r
236 implementation(const std::string& name)
\r
239 void update(const std::string& name, float value)
\r
241 context::begin_invoke([=]
\r
243 lines_[name].update(value);
\r
247 void tag(const std::string& name)
\r
249 context::begin_invoke([=]
\r
251 lines_[name].tag();
\r
255 void set_color(const std::string& name, color c)
\r
257 context::begin_invoke([=]
\r
259 lines_[name].set_color(c);
\r
263 void guide(const std::string& name, float value)
\r
265 context::begin_invoke([=]
\r
267 lines_[name].guide(diagnostics::guide(value));
\r
272 void render(sf::RenderTarget& target)
\r
274 const size_t text_size = 15;
\r
275 const size_t text_margin = 2;
\r
276 const size_t text_offset = text_size+text_margin*2;
\r
278 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
279 text.SetStyle(sf::String::Italic);
\r
280 text.Move(text_margin, text_margin);
\r
283 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
285 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;
\r
286 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
288 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
289 line_text.SetPosition(x_offset, text_margin);
\r
290 auto c = it->second.get_color();
\r
291 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
292 target.Draw(line_text);
\r
293 x_offset = line_text.GetRect().Right + text_margin*2;
\r
296 glDisable(GL_TEXTURE_2D);
\r
300 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
301 glVertex2f(1.0f, 0.99f);
\r
302 glVertex2f(0.0f, 0.99f);
\r
303 glVertex2f(0.0f, 0.01f);
\r
304 glVertex2f(1.0f, 0.01f);
\r
308 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
309 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
311 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
312 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
314 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
315 target.Draw(it->second);
\r
320 implementation(implementation&);
\r
321 implementation& operator=(implementation&);
\r
324 graph::graph(const std::string& name) : impl_(new implementation(name))
\r
326 context::register_drawable(impl_);
\r
329 void graph::update(const std::string& name, float value){impl_->update(name, value);}
\r
330 void graph::tag(const std::string& name){impl_->tag(name);}
\r
331 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}
\r
332 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}
\r
334 safe_ptr<graph> create_graph(const std::string& name)
\r
336 return safe_ptr<graph>(new graph(name));
\r