2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../stdafx.h"
\r
26 #pragma warning (disable : 4244)
\r
28 #include "../concurrency/executor.h"
\r
29 #include "../concurrency/lock.h"
\r
32 #include <SFML/Graphics.hpp>
\r
34 #include <boost/foreach.hpp>
\r
35 #include <boost/optional.hpp>
\r
36 #include <boost/circular_buffer.hpp>
\r
37 #include <boost/range/algorithm_ext/erase.hpp>
\r
39 #include <tbb/concurrent_unordered_map.h>
\r
40 #include <tbb/atomic.h>
\r
41 #include <tbb/spin_mutex.h>
\r
47 namespace caspar { namespace diagnostics {
\r
49 int color(float r, float g, float b, float a)
\r
52 code |= static_cast<int>(r*255.0f+0.5f) << 24;
\r
53 code |= static_cast<int>(g*255.0f+0.5f) << 16;
\r
54 code |= static_cast<int>(b*255.0f+0.5f) << 8;
\r
55 code |= static_cast<int>(a*255.0f+0.5f) << 0;
\r
59 std::tuple<float, float, float, float> color(int code)
\r
61 float r = static_cast<float>((code >> 24) & 255)/255.0f;
\r
62 float g = static_cast<float>((code >> 16) & 255)/255.0f;
\r
63 float b = static_cast<float>((code >> 8) & 255)/255.0f;
\r
64 float a = static_cast<float>((code >> 0) & 255)/255.0f;
\r
65 return std::make_tuple(r, g, b, a);
\r
68 struct drawable : public sf::Drawable
\r
70 virtual ~drawable(){}
\r
71 virtual void render(sf::RenderTarget& target) = 0;
\r
72 virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}
\r
75 class context : public drawable
\r
77 std::unique_ptr<sf::RenderWindow> window_;
\r
79 std::list<std::weak_ptr<drawable>> drawables_;
\r
84 static void register_drawable(const std::shared_ptr<drawable>& drawable)
\r
89 get_instance().executor_.begin_invoke([=]
\r
91 get_instance().do_register_drawable(drawable);
\r
95 static void show(bool value)
\r
97 get_instance().executor_.begin_invoke([=]
\r
99 get_instance().do_show(value);
\r
104 context() : executor_(L"diagnostics")
\r
106 executor_.set_priority_class(below_normal_priority_class);
\r
109 void do_show(bool value)
\r
115 window_.reset(new sf::RenderWindow(sf::VideoMode(750, 750), "CasparCG Diagnostics"));
\r
116 window_->SetPosition(0, 0);
\r
117 window_->SetActive();
\r
118 glEnable(GL_BLEND);
\r
119 glEnable(GL_LINE_SMOOTH);
\r
120 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
\r
121 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
\r
135 while(window_->GetEvent(e))
\r
137 if(e.Type == sf::Event::Closed)
\r
146 glClear(GL_COLOR_BUFFER_BIT);
\r
147 window_->Draw(*this);
\r
148 window_->Display();
\r
149 boost::this_thread::sleep(boost::posix_time::milliseconds(10));
\r
153 CASPAR_LOG_CURRENT_EXCEPTION();
\r
155 << L"Closing diag window due to error during rendering";
\r
160 executor_.begin_invoke([this]{tick();});
\r
163 void render(sf::RenderTarget& target)
\r
165 auto count = std::max<size_t>(8, drawables_.size());
\r
166 float target_dy = 1.0f/static_cast<float>(count);
\r
168 float last_y = 0.0f;
\r
170 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
\r
172 auto drawable = it->lock();
\r
175 drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));
\r
176 float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);
\r
177 drawable->SetPosition(0.0f, target_y);
\r
178 target.Draw(*drawable);
\r
182 it = drawables_.erase(it);
\r
186 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
\r
188 drawables_.push_back(drawable);
\r
189 auto it = drawables_.begin();
\r
190 while(it != drawables_.end())
\r
195 it = drawables_.erase(it);
\r
199 static context& get_instance()
\r
201 static context impl;
\r
206 class line : public drawable
\r
208 boost::circular_buffer<std::pair<float, bool>> line_data_;
\r
210 tbb::atomic<float> tick_data_;
\r
211 tbb::atomic<bool> tick_tag_;
\r
212 tbb::atomic<int> color_;
\r
214 line(size_t res = 1200)
\r
217 tick_data_ = -1.0f;
\r
218 color_ = 0xFFFFFFFF;
\r
221 line_data_.push_back(std::make_pair(-1.0f, false));
\r
224 void set_value(float value)
\r
226 tick_data_ = value;
\r
234 void set_color(int color)
\r
244 void render(sf::RenderTarget& target)
\r
246 float dx = 1.0f/static_cast<float>(line_data_.capacity());
\r
247 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
\r
249 line_data_.push_back(std::make_pair(tick_data_, tick_tag_));
\r
252 glBegin(GL_LINE_STRIP);
\r
253 auto c = color(color_);
\r
254 glColor4f(std::get<0>(c), std::get<1>(c), std::get<2>(c), 0.8f);
\r
255 for(size_t n = 0; n < line_data_.size(); ++n)
\r
256 if(line_data_[n].first > -0.5)
\r
257 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
260 glEnable(GL_LINE_STIPPLE);
\r
261 glLineStipple(3, 0xAAAA);
\r
262 for(size_t n = 0; n < line_data_.size(); ++n)
\r
264 if(line_data_[n].second)
\r
266 glBegin(GL_LINE_STRIP);
\r
267 glVertex3f(x+n*dx, 0.0f, 0.0f);
\r
268 glVertex3f(x+n*dx, 1.0f, 0.0f);
\r
272 glDisable(GL_LINE_STIPPLE);
\r
276 struct graph::impl : public drawable
\r
278 tbb::concurrent_unordered_map<std::string, diagnostics::line> lines_;
\r
280 tbb::spin_mutex mutex_;
\r
281 std::wstring text_;
\r
288 void set_text(const std::wstring& value)
\r
293 text_ = std::move(temp);
\r
297 void set_value(const std::string& name, double value)
\r
299 lines_[name].set_value(value);
\r
302 void set_tag(const std::string& name)
\r
304 lines_[name].set_tag();
\r
307 void set_color(const std::string& name, int color)
\r
309 lines_[name].set_color(color);
\r
314 lock(mutex_, [this]
\r
316 auto_reset_ = true;
\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 std::wstring text_str;
\r
330 tbb::spin_mutex::scoped_lock lock(mutex_);
\r
332 auto_reset = auto_reset_;
\r
335 sf::String text(text_str.c_str(), sf::Font::GetDefaultFont(), text_size);
\r
336 text.SetStyle(sf::String::Italic);
\r
337 text.Move(text_margin, text_margin);
\r
340 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
\r
342 float x_offset = text_margin;
\r
343 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
345 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
\r
346 line_text.SetPosition(x_offset, text_margin+text_offset/2);
\r
347 auto c = it->second.get_color();
\r
348 line_text.SetColor(sf::Color((c >> 24) & 255, (c >> 16) & 255, (c >> 8) & 255, (c >> 0) & 255));
\r
349 target.Draw(line_text);
\r
350 x_offset = line_text.GetRect().Right + text_margin*2;
\r
353 glDisable(GL_TEXTURE_2D);
\r
357 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
\r
358 glVertex2f(1.0f, 0.99f);
\r
359 glVertex2f(0.0f, 0.99f);
\r
360 glVertex2f(0.0f, 0.01f);
\r
361 glVertex2f(1.0f, 0.01f);
\r
365 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
\r
366 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
\r
368 glEnable(GL_LINE_STIPPLE);
\r
369 glLineStipple(3, 0xAAAA);
\r
370 glColor4f(1.0f, 1.0f, 1.9f, 0.5f);
\r
371 glBegin(GL_LINE_STRIP);
\r
372 glVertex3f(0.0f, (1.0f-0.5f) * 0.8f + 0.1f, 0.0f);
\r
373 glVertex3f(1.0f, (1.0f-0.5f) * 0.8f + 0.1f, 0.0f);
\r
375 glBegin(GL_LINE_STRIP);
\r
376 glVertex3f(0.0f, (1.0f-0.0f) * 0.8f + 0.1f, 0.0f);
\r
377 glVertex3f(1.0f, (1.0f-0.0f) * 0.8f + 0.1f, 0.0f);
\r
379 glBegin(GL_LINE_STRIP);
\r
380 glVertex3f(0.0f, (1.0f-1.0f) * 0.8f + 0.1f, 0.0f);
\r
381 glVertex3f(1.0f, (1.0f-1.0f) * 0.8f + 0.1f, 0.0f);
\r
383 glDisable(GL_LINE_STIPPLE);
\r
385 //target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
386 //target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
\r
388 for(auto it = lines_.begin(); it != lines_.end(); ++it)
\r
390 target.Draw(it->second);
\r
393 it->second.set_value(0.0f);
\r
400 impl& operator=(impl&);
\r
403 graph::graph() : impl_(new impl())
\r
407 void graph::set_text(const std::wstring& value){impl_->set_text(value);}
\r
408 void graph::set_value(const std::string& name, double value){impl_->set_value(name, value);}
\r
409 void graph::set_color(const std::string& name, int color){impl_->set_color(name, color);}
\r
410 void graph::set_tag(const std::string& name){impl_->set_tag(name);}
\r
411 void graph::auto_reset(){impl_->auto_reset();}
\r
413 void register_graph(const safe_ptr<graph>& graph)
\r
415 context::register_drawable(graph->impl_);
\r
418 void show_graphs(bool value)
\r
420 context::show(value);
\r
426 //struct line::impl
\r
428 // std::wstring name_;
\r
429 // boost::circular_buffer<data> ticks_;
\r
431 // impl(const std::wstring& name)
\r
433 // , ticks_(1024){}
\r
435 // void set_value(float value)
\r
437 // ticks_.push_back();
\r
438 // ticks_.back().value = value;
\r
441 // void set_value(float value)
\r
444 // set_value(value);
\r
449 //line::line(const std::wstring& name) : impl_(new impl(name)){}
\r
450 //std::wstring line::print() const {return impl_->name_;}
\r
451 //void line::set_value(float value){impl_->set_value(value);}
\r
452 //void line::set_value(float value){impl_->set_value(value);}
\r
453 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}
\r
455 //struct graph::impl
\r
457 // std::map<std::wstring, line> lines_;
\r
459 // printer printer_;
\r
461 // impl(const std::wstring& name)
\r
462 // : printer_([=]{return name;}){}
\r
464 // impl(const printer& parent_printer)
\r
465 // : printer_(parent_printer){}
\r
467 // void set_value(const std::wstring& name, float value)
\r
469 // auto it = lines_.find(name);
\r
470 // if(it == lines_.end())
\r
471 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
473 // it->second.set_value(value);
\r
476 // void set_value(const std::wstring& name, float value)
\r
478 // auto it = lines_.find(name);
\r
479 // if(it == lines_.end())
\r
480 // it = lines_.insert(std::make_pair(name, line(name))).first;
\r
482 // it->second.set_value(value);
\r
485 // void set_color(const std::wstring& name, color color)
\r
490 // std::map<std::wstring, line>& get_lines()
\r
495 // color get_color() const
\r
500 // std::wstring print() const
\r
502 // return printer_ ? printer_() : L"graph";
\r
506 //graph::graph(const std::wstring& name) : impl_(new impl(name)){}
\r
507 //graph::graph(const printer& parent_printer) : impl_(new impl(parent_printer)){}
\r
508 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
\r
509 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
\r
510 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}
\r
511 //color graph::get_color() const {return impl_->get_color();}
\r
512 //std::wstring graph::print() const {return impl_->print();}
\r
514 //safe_ptr<graph> graph::clone() const
\r
516 // safe_ptr<graph> clone(new graph(std::wstring(L"")));
\r
517 // clone->impl_->printer_ = impl_->printer_;
\r
518 // clone->impl_->lines_ = impl_->lines_;
\r
519 // clone->impl_->color_ = impl_->color_;
\r
522 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}
\r
524 //std::vector<safe_ptr<graph>> g_graphs;
\r
526 //safe_ptr<graph> create_graph(const std::string& name)
\r
528 // g_graphs.push_back(make_safe<graph>(name));
\r
529 // return g_graphs.back();
\r
532 //safe_ptr<graph> create_graph(const printer& parent_printer)
\r
534 // g_graphs.push_back(make_safe<graph>(parent_printer));
\r
535 // return g_graphs.back();
\r
538 //static std::vector<safe_ptr<graph>> get_all_graphs()
\r
540 // std::vector<safe_ptr<graph>> graphs;
\r
541 // BOOST_FOREACH(auto& graph, g_graphs)
\r
542 // graphs.push_back(graph->clone());
\r