2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../stdafx.h"
\r
24 #pragma warning (disable : 4244)
\r
26 #include "../concurrency/executor.h"
\r
29 #include <SFML/Graphics.hpp>
\r
31 #include <boost/foreach.hpp>
\r
32 #include <boost/optional.hpp>
\r
33 #include <boost/circular_buffer.hpp>
\r
34 #include <boost/range/algorithm_ext/erase.hpp>
\r
40 namespace caspar { namespace diagnostics {
\r
42 struct drawable : public sf::Drawable
\r
44 virtual ~drawable(){}
\r
45 virtual void render(sf::RenderTarget& target) = 0;
\r
46 virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}
\r
49 class context : public drawable
\r
51 std::unique_ptr<sf::RenderWindow> window_;
\r
53 std::list<std::shared_ptr<drawable>> drawables_;
\r
58 template<typename Func>
\r
59 static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
\r
61 return get_instance().executor_.begin_invoke(std::forward<Func>(func));
\r
64 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
71 get_instance().do_register_drawable(drawable);
\r
75 static void show(bool value)
\r
79 get_instance().do_show(value);
\r
84 context() : executor_(L"diagnostics")
\r
88 void do_show(bool value)
\r
94 SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);
\r
95 window_.reset(new sf::RenderWindow(sf::VideoMode(600, 1000), "CasparCG Diagnostics"));
\r
96 window_->SetPosition(0, 0);
\r
97 window_->SetActive();
\r
99 glEnable(GL_LINE_SMOOTH);
\r
100 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
101 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
115 while(window_->GetEvent(e)){}
\r
116 glClear(GL_COLOR_BUFFER_BIT);
\r
117 window_->Draw(*this);
\r
118 window_->Display();
\r
119 boost::this_thread::sleep(boost::posix_time::milliseconds(20));
\r
120 executor_.begin_invoke([this]{tick();});
\r
123 void render(sf::RenderTarget& target)
\r
125 auto count = std::max<size_t>(8, drawables_.size());
\r
126 float target_dy = 1.0f/static_cast<float>(count);
\r
128 float last_y = 0.0f;
\r
130 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
132 auto& drawable = *it;
\r
133 if(!drawable.unique())
\r
135 drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));
\r
136 float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);
\r
137 drawable->SetPosition(0.0f, target_y);
\r
138 target.Draw(*drawable);
\r
142 it = drawables_.erase(it);
\r
146 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
\r
148 if(std::find(drawables_.begin(), drawables_.end(), drawable) == drawables_.end())
\r
149 drawables_.push_back(drawable);
\r
152 static context& get_instance()
\r
154 static context impl;
\r
159 class guide : public drawable
\r
164 guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
168 guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f))
\r
172 void set_color(color c) {c_ = c;}
\r
174 void render(sf::RenderTarget&)
\r
176 glEnable(GL_LINE_STIPPLE);
\r
177 glLineStipple(3, 0xAAAA);
\r
178 glBegin(GL_LINE_STRIP);
\r
179 glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);
\r
180 glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
181 glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);
\r
183 glDisable(GL_LINE_STIPPLE);
\r
187 class line : public drawable
\r
189 boost::optional<diagnostics::guide> guide_;
\r
190 boost::circular_buffer<std::pair<double, bool>> line_data_;
\r
192 boost::circular_buffer<double> tick_data_;
\r
196 line(size_t res = 600)
\r
200 , c_(1.0f, 1.0f, 1.0f)
\r
202 line_data_.push_back(std::make_pair(-1.0f, false));
\r
205 void update(double value)
\r
207 tick_data_.push_back(value);
\r
210 void set(double value)
\r
212 tick_data_.clear();
\r
213 tick_data_.push_back(value);
\r
221 void guide(const guide& guide)
\r
224 guide_->set_color(c_);
\r
227 void set_color(color c)
\r
231 guide_->set_color(c_);
\r
234 color get_color() const { return c_; }
\r
236 void render(sf::RenderTarget& target)
\r
238 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
239 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
241 if(!tick_data_.empty())
\r
243 float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();
\r
244 line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));
\r
245 tick_data_.clear();
\r
247 else if(!line_data_.empty())
\r
249 line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));
\r
254 target.Draw(*guide_);
\r
256 glBegin(GL_LINE_STRIP);
\r
257 glColor4f(c_.red, c_.green, c_.blue, 0.8f);
\r
258 for(size_t n = 0; n < line_data_.size(); ++n)
\r
259 if(line_data_[n].first > -0.5)
\r
260 glVertex3d(x+n*dx, std::max(0.05, std::min(0.95, (1.0f-line_data_[n].first)*0.8 + 0.1f)), 0.0);
\r
263 glEnable(GL_LINE_STIPPLE);
\r
264 glLineStipple(3, 0xAAAA);
\r
265 for(size_t n = 0; n < line_data_.size(); ++n)
\r
267 if(line_data_[n].second)
\r
269 glBegin(GL_LINE_STRIP);
\r
270 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);
\r
271 glVertex3f(x+n*dx, 0.0f, 0.0f);
\r
272 glVertex3f(x+n*dx, 1.0f, 0.0f);
\r
276 glDisable(GL_LINE_STIPPLE);
\r
280 struct graph::implementation : public drawable
\r
282 std::map<std::string, diagnostics::line> lines_;
\r
286 implementation(const std::string& name)
\r
290 void set_text(const std::string& value)
\r
295 void update(const std::string& name, double value)
\r
297 lines_[name].update(value);
\r
300 void set(const std::string& name, double value)
\r
302 lines_[name].set(value);
\r
305 void tag(const std::string& name)
\r
307 lines_[name].tag();
\r
310 void set_color(const std::string& name, color c)
\r
312 lines_[name].set_color(c);
\r
315 void guide(const std::string& name, double value)
\r
317 lines_[name].guide(diagnostics::guide(value));
\r
321 void render(sf::RenderTarget& target)
\r
323 const size_t text_size = 15;
\r
324 const size_t text_margin = 2;
\r
325 const size_t text_offset = (text_size+text_margin*2)*2;
\r
327 sf::String text(text_.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
328 text.SetStyle(sf::String::Italic);
\r
329 text.Move(text_margin, text_margin);
\r
332 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
334 float x_offset = text_margin;
\r
335 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
337 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
338 line_text.SetPosition(x_offset, text_margin+text_offset/2);
\r
339 auto c = it->second.get_color();
\r
340 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));
\r
341 target.Draw(line_text);
\r
342 x_offset = line_text.GetRect().Right + text_margin*2;
\r
345 glDisable(GL_TEXTURE_2D);
\r
349 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
350 glVertex2f(1.0f, 0.99f);
\r
351 glVertex2f(0.0f, 0.99f);
\r
352 glVertex2f(0.0f, 0.01f);
\r
353 glVertex2f(1.0f, 0.01f);
\r
357 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
358 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
360 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
361 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
363 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
364 target.Draw(it->second);
\r
369 implementation(implementation&);
\r
370 implementation& operator=(implementation&);
\r
373 graph::graph() : impl_(new implementation(""))
\r
378 void graph::set_text(const std::string& value)
\r
381 context::begin_invoke([=]
\r
383 p->set_text(value);
\r
387 void graph::set_text(const std::wstring& value)
\r
389 set_text(narrow(value));
\r
392 void graph::update_value(const std::string& name, double value)
\r
395 context::begin_invoke([=]
\r
397 p->update(name, value);
\r
400 void graph::set_value(const std::string& name, double value)
\r
403 context::begin_invoke([=]
\r
405 p->set(name, value);
\r
408 void graph::set_color(const std::string& name, color c)
\r
411 context::begin_invoke([=]
\r
413 p->set_color(name, c);
\r
416 void graph::add_tag(const std::string& name)
\r
419 context::begin_invoke([=]
\r
424 void graph::add_guide(const std::string& name, double value)
\r
427 context::begin_invoke([=]
\r
429 p->guide(name, value);
\r
433 void register_graph(const safe_ptr<graph>& graph)
\r
435 context::register_drawable(graph->impl_);
\r
438 void show_graphs(bool value)
\r
440 context::show(value);
\r
446 //struct line::implementation
\r
448 // std::wstring name_;
\r
449 // boost::circular_buffer<data> ticks_;
\r
451 // implementation(const std::wstring& name)
\r
453 // , ticks_(1024){}
\r
455 // void update_value(float value)
\r
457 // ticks_.push_back();
\r
458 // ticks_.back().value = value;
\r
461 // void set_value(float value)
\r
464 // update_value(value);
\r
469 //line::line(const std::wstring& name) : impl_(new implementation(name)){}
\r
470 //std::wstring line::print() const {return impl_->name_;}
\r
471 //void line::update_value(float value){impl_->update_value(value);}
\r
472 //void line::set_value(float value){impl_->set_value(value);}
\r
473 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}
\r
475 //struct graph::implementation
\r
477 // std::map<std::wstring, line> lines_;
\r
479 // printer printer_;
\r
481 // implementation(const std::wstring& name)
\r
482 // : printer_([=]{return name;}){}
\r
484 // implementation(const printer& parent_printer)
\r
485 // : printer_(parent_printer){}
\r
487 // void update_value(const std::wstring& name, float value)
\r
489 // auto it = lines_.find(name);
\r
490 // if(it == lines_.end())
\r
491 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
493 // it->second.update_value(value);
\r
496 // void set_value(const std::wstring& name, float value)
\r
498 // auto it = lines_.find(name);
\r
499 // if(it == lines_.end())
\r
500 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
502 // it->second.set_value(value);
\r
505 // void set_color(const std::wstring& name, color color)
\r
510 // std::map<std::wstring, line>& get_lines()
\r
515 // color get_color() const
\r
520 // std::wstring print() const
\r
522 // return printer_ ? printer_() : L"graph";
\r
526 //graph::graph(const std::wstring& name) : impl_(new implementation(name)){}
\r
527 //graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}
\r
528 //void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}
\r
529 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
\r
530 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}
\r
531 //color graph::get_color() const {return impl_->get_color();}
\r
532 //std::wstring graph::print() const {return impl_->print();}
\r
534 //safe_ptr<graph> graph::clone() const
\r
536 // safe_ptr<graph> clone(new graph(std::wstring(L"")));
\r
537 // clone->impl_->printer_ = impl_->printer_;
\r
538 // clone->impl_->lines_ = impl_->lines_;
\r
539 // clone->impl_->color_ = impl_->color_;
\r
542 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}
\r
544 //std::vector<safe_ptr<graph>> g_graphs;
\r
546 //safe_ptr<graph> create_graph(const std::string& name)
\r
548 // g_graphs.push_back(make_safe<graph>(name));
\r
549 // return g_graphs.back();
\r
552 //safe_ptr<graph> create_graph(const printer& parent_printer)
\r
554 // g_graphs.push_back(make_safe<graph>(parent_printer));
\r
555 // return g_graphs.back();
\r
558 //static std::vector<safe_ptr<graph>> get_all_graphs()
\r
560 // std::vector<safe_ptr<graph>> graphs;
\r
561 // BOOST_FOREACH(auto& graph, g_graphs)
\r
562 // graphs.push_back(graph->clone());
\r