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
36 std::map<size_t, sf::Font> fonts_;
\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
51 get_instance().drawables_.push_back(drawable);
\r
60 while(window_.GetEvent(e)){}
\r
61 glClear(GL_COLOR_BUFFER_BIT);
\r
62 window_.Draw(*this);
\r
64 timer_.tick(1.0/50.0);
\r
65 executor_.begin_invoke([this]{tick();});
\r
68 void render(sf::RenderTarget& target)
\r
70 auto count = std::max<size_t>(5, drawables_.size());
\r
73 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
75 auto& drawable = *it;
\r
76 if(!drawable.unique())
\r
78 drawable->SetScale(window_.GetWidth(), window_.GetHeight()/count);
\r
79 drawable->SetPosition(0.0f, n* window_.GetHeight()/count);
\r
80 target.Draw(*drawable);
\r
84 it = drawables_.erase(it);
\r
88 static context& get_instance()
\r
90 static context impl;
\r
97 executor_.begin_invoke([this]
\r
99 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
100 window_.SetPosition(0, 0);
\r
101 window_.SetActive();
\r
102 glEnable(GL_BLEND);
\r
103 glEnable(GL_LINE_SMOOTH);
\r
104 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
105 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
111 class guide : public drawable
\r
116 guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
120 guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
124 void set_color(color c) {c_ = c;}
\r
126 void render(sf::RenderTarget&)
\r
128 glEnable(GL_LINE_STIPPLE);
\r
129 glLineStipple(3, 0xAAAA);
\r
130 glBegin(GL_LINE_STRIP);
\r
131 glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);
\r
132 glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
133 glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
135 glDisable(GL_LINE_STIPPLE);
\r
139 class line : public drawable
\r
141 boost::optional<diagnostics::guide> guide_;
\r
142 boost::circular_buffer<float> line_data_;
\r
143 std::vector<float> tick_data_;
\r
146 line(size_t res = 600)
\r
148 , c_(1.0f, 1.0f, 1.0f){}
\r
150 void update(float value)
\r
152 tick_data_.push_back(value);
\r
155 void guide(const guide& guide)
\r
158 guide_->set_color(c_);
\r
161 void set_color(color c)
\r
165 guide_->set_color(c_);
\r
168 color get_color() const { return c_; }
\r
170 void render(sf::RenderTarget& target)
\r
172 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
173 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
175 if(!tick_data_.empty())
\r
177 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
178 line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));
\r
179 tick_data_.clear();
\r
181 else if(!line_data_.empty())
\r
183 line_data_.push_back(line_data_.back());
\r
187 target.Draw(*guide_);
\r
189 glBegin(GL_LINE_STRIP);
\r
190 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
191 for(size_t n = 0; n < line_data_.size(); ++n)
\r
192 glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, (1.0f-line_data_[n])*0.8f + 0.1f)), 0.0f);
\r
197 struct graph::implementation : public drawable
\r
199 std::map<std::string, diagnostics::line> lines_;
\r
202 implementation(const std::string& name) : name_(name){}
\r
204 void update(const std::string& name, float value)
\r
206 context::begin_invoke([=]
\r
208 lines_[name].update(value);
\r
212 void set_color(const std::string& name, color c)
\r
214 context::begin_invoke([=]
\r
216 lines_[name].set_color(c);
\r
220 void guide(const std::string& name, float value)
\r
222 context::begin_invoke([=]
\r
224 lines_[name].guide(diagnostics::guide(value));
\r
229 void render(sf::RenderTarget& target)
\r
231 const size_t text_size = 15;
\r
232 const size_t text_margin = 2;
\r
233 const size_t text_offset = text_size+text_margin*2;
\r
235 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
236 text.SetStyle(sf::String::Italic);
\r
237 text.Move(text_margin, text_margin);
\r
240 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
242 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;
\r
243 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
245 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
246 line_text.SetPosition(x_offset, text_margin);
\r
247 auto c = it->second.get_color();
\r
248 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
249 target.Draw(line_text);
\r
250 x_offset = line_text.GetRect().Right + text_margin*2;
\r
253 glDisable(GL_TEXTURE_2D);
\r
257 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
258 glVertex2f(1.0f, 0.99f);
\r
259 glVertex2f(0.0f, 0.99f);
\r
260 glVertex2f(0.0f, 0.01f);
\r
261 glVertex2f(1.0f, 0.01f);
\r
265 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
266 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
268 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
269 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
271 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
272 target.Draw(it->second);
\r
278 implementation(implementation&);
\r
279 implementation& operator=(implementation&);
\r
282 graph::graph(const std::string& name) : impl_(new implementation(name))
\r
284 context::register_drawable(impl_);
\r
286 void graph::update(const std::string& name, float value){impl_->update(name, value);}
\r
287 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}
\r
288 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}
\r
290 safe_ptr<graph> create_graph(const std::string& name)
\r
292 return safe_ptr<graph>(new graph(name));
\r