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/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(double dy, double y) = 0;
\r
32 sf::RenderWindow window_;
\r
34 std::list<std::weak_ptr<drawable>> drawables_;
\r
39 while(window_.GetEvent(e)){}
\r
43 timer_.tick(1.0/50.0);
\r
44 executor_.begin_invoke([this]{tick();});
\r
49 float dy = 1.0/static_cast<float>(std::max<int>(5, drawables_.size()));
\r
51 for(auto it = drawables_.begin(); it != drawables_.end();)
\r
53 auto drawable = it->lock();
\r
56 drawable->draw(dy, y);
\r
61 it = drawables_.erase(it);
\r
64 static context& get_instance()
\r
66 static context impl;
\r
73 executor_.begin_invoke([this]
\r
75 window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");
\r
76 window_.SetPosition(0, 0);
\r
77 window_.SetActive();
\r
79 glEnable(GL_LINE_SMOOTH);
\r
80 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
81 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
88 template<typename Func>
\r
89 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
91 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
94 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
98 get_instance().drawables_.push_back(drawable);
\r
105 boost::circular_buffer<float> line_data_;
\r
106 std::vector<float> tick_data_;
\r
107 std::array<float, 3> color_;
\r
109 line(size_t res = 600)
\r
112 color(1.0f, 1.0f, 1.0f);
\r
115 void update(float value)
\r
117 tick_data_.push_back(value);
\r
120 void color(float r, float g, float b)
\r
122 color_[0] = r; color_[1] = g; color_[2] = b;
\r
125 void draw(double dy, double y)
\r
127 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
128 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
130 if(!tick_data_.empty())
\r
132 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
133 line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));
\r
134 tick_data_.clear();
\r
136 else if(!line_data_.empty())
\r
138 line_data_.push_back(line_data_.back());
\r
141 glBegin(GL_LINE_STRIP);
\r
142 glColor4f(color_[0], color_[1], color_[2], 1.0f);
\r
143 for(size_t n = 0; n < line_data_.size(); ++n)
\r
144 glVertex3f((x+n*dx)*2.0f-1.0f, (y + dy * std::max(0.05f, std::min(0.95f, line_data_[n]*0.8f + 0.1f))) * 2.0f - 1.0f, 0.0f);
\r
151 std::array<float, 3> color_;
\r
154 guide() : value_(0.0f)
\r
156 color_[0] = color_[1] = color_[2] = 0.0f;
\r
159 guide(float value, float r, float g, float b) : value_(value)
\r
161 color_[0] = r; color_[1] = g; color_[2] = b;
\r
164 void draw(double dy, double y)
\r
166 glEnable(GL_LINE_STIPPLE);
\r
167 glLineStipple(3, 0xAAAA);
\r
168 glBegin(GL_LINE_STRIP);
\r
169 glColor4f(color_[0], color_[1], color_[2], 1.0f);
\r
170 glVertex3f(0.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 0.0f);
\r
171 glVertex3f(1.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 0.0f);
\r
173 glDisable(GL_LINE_STIPPLE);
\r
177 struct graph::implementation : public drawable
\r
179 std::map<std::string, diagnostics::line> lines_;
\r
180 std::map<std::string, diagnostics::guide> guides_;
\r
182 implementation(const std::string&)
\r
184 guides_["max"] = diagnostics::guide(1.0f, 0.4f, 0.4f, 0.4f);
\r
185 guides_["min"] = diagnostics::guide(0.0f, 0.4f, 0.4f, 0.4f);
\r
188 void update(const std::string& name, float value)
\r
190 context::begin_invoke([=]
\r
192 lines_[name].update(value);
\r
196 void color(const std::string& name, float r, float g, float b)
\r
198 context::begin_invoke([=]
\r
200 lines_[name].color(r, g, b);
\r
204 void line(const std::string& name, float value, float r, float g, float b)
\r
206 context::begin_invoke([=]
\r
208 guides_[name] = diagnostics::guide(value, r, g, b);
\r
213 void draw(double dy, double y)
\r
216 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
217 glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);
\r
218 glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);
\r
219 glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);
\r
220 glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);
\r
223 for(auto it = guides_.begin(); it != guides_.end(); ++it)
\r
224 it->second.draw(dy, y);
\r
226 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
227 it->second.draw(dy, y);
\r
230 implementation(implementation&);
\r
231 implementation& operator=(implementation&);
\r
234 graph::graph(const std::string& name) : impl_(new implementation(name))
\r
236 context::register_drawable(impl_);
\r
238 void graph::update(const std::string& name, float value){impl_->update(name, value);}
\r
239 void graph::color(const std::string& name, float r, float g, float b){impl_->color(name, r, g, b);}
\r
240 void graph::line(const std::string& name, float value, float r, float g, float b){impl_->line(name, value, r, g, b);}
\r
242 safe_ptr<graph> create_graph(const std::string& name)
\r
244 return safe_ptr<graph>(new graph(name));
\r