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/Window.hpp>
\r
12 #include <boost/foreach.hpp>
\r
13 #include <boost/circular_buffer.hpp>
\r
14 #include <boost/range/algorithm_ext/erase.hpp>
\r
20 namespace caspar { namespace diagnostics {
\r
24 virtual ~drawable(){}
\r
25 virtual void draw() = 0;
\r
33 std::list<std::weak_ptr<drawable>> drawables_;
\r
38 template<typename Func>
\r
39 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
41 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
44 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
48 get_instance().drawables_.push_back(drawable);
\r
57 while(window_.GetEvent(e)){}
\r
58 glClear(GL_COLOR_BUFFER_BIT);
\r
61 timer_.tick(1.0/50.0);
\r
62 executor_.begin_invoke([this]{tick();});
\r
68 glTranslated(-1.0f, -1.0f, 0.0f);
\r
69 glScaled(2.0f, 2.0f, 1.0f);
\r
71 float dy = 1.0/static_cast<float>(std::max<int>(5, drawables_.size()));
\r
73 glTranslated(0.0f, (1.0-dy), 0.0f);
\r
74 for(auto it = drawables_.begin(); it != drawables_.end();)
\r
76 auto drawable = it->lock();
\r
80 glScaled(1.0f, dy, 1.0f);
\r
83 glTranslated(0.0f, -dy, 0.0f);
\r
87 it = drawables_.erase(it);
\r
91 static context& get_instance()
\r
93 static context impl;
\r
100 executor_.begin_invoke([this]
\r
102 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
103 window_.SetPosition(0, 0);
\r
104 window_.SetActive();
\r
105 glEnable(GL_BLEND);
\r
106 glEnable(GL_LINE_SMOOTH);
\r
107 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
108 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
116 boost::circular_buffer<float> line_data_;
\r
117 std::vector<float> tick_data_;
\r
120 line(size_t res = 600)
\r
122 , c_(1.0f, 1.0f, 1.0f){}
\r
124 void update(float value)
\r
126 tick_data_.push_back(value);
\r
129 void set_color(color c){c_ = c;}
\r
133 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
134 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
136 if(!tick_data_.empty())
\r
138 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
139 line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));
\r
140 tick_data_.clear();
\r
142 else if(!line_data_.empty())
\r
144 line_data_.push_back(line_data_.back());
\r
147 glBegin(GL_LINE_STRIP);
\r
148 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
149 for(size_t n = 0; n < line_data_.size(); ++n)
\r
150 glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, line_data_[n]*0.8f + 0.1f)), 0.0f);
\r
160 guide() : value_(0.0f){}
\r
162 guide(float value, color c)
\r
168 glEnable(GL_LINE_STIPPLE);
\r
169 glLineStipple(3, 0xAAAA);
\r
170 glBegin(GL_LINE_STRIP);
\r
171 glColor4f(c_.red, c_.green, c_.blue, 1.0f);
\r
172 glVertex3f(0.0f, value_ * 0.8f + 0.1f, 0.0f);
\r
173 glVertex3f(1.0f, value_ * 0.8f + 0.1f, 0.0f);
\r
175 glDisable(GL_LINE_STIPPLE);
\r
179 struct graph::implementation : public drawable
\r
181 std::map<std::string, diagnostics::line> lines_;
\r
182 std::map<std::string, diagnostics::guide> guides_;
\r
184 implementation(const std::string&)
\r
186 guides_["max"] = diagnostics::guide(1.0f, color(0.4f, 0.4f, 0.4f));
\r
187 guides_["min"] = diagnostics::guide(0.0f, color(0.4f, 0.4f, 0.4f));
\r
190 void update(const std::string& name, float value)
\r
192 context::begin_invoke([=]
\r
194 lines_[name].update(value);
\r
198 void set_color(const std::string& name, color c)
\r
200 context::begin_invoke([=]
\r
202 lines_[name].set_color(c);
\r
206 void add_guide(const std::string& name, float value, color c)
\r
208 context::begin_invoke([=]
\r
210 guides_[name] = diagnostics::guide(value, c);
\r
218 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
219 glVertex2f(1.0f, 0.99f);
\r
220 glVertex2f(0.0f, 0.99f);
\r
221 glVertex2f(0.0f, 0.01f);
\r
222 glVertex2f(1.0f, 0.01f);
\r
225 for(auto it = guides_.begin(); it != guides_.end(); ++it)
\r
228 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
232 implementation(implementation&);
\r
233 implementation& operator=(implementation&);
\r
236 graph::graph(const std::string& name) : impl_(new implementation(name))
\r
238 context::register_drawable(impl_);
\r
240 void graph::update(const std::string& name, float value){impl_->update(name, value);}
\r
241 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}
\r
242 void graph::add_guide(const std::string& name, float value, color c){impl_->add_guide(name, value, c);}
\r
244 safe_ptr<graph> create_graph(const std::string& name)
\r
246 return safe_ptr<graph>(new graph(name));
\r