2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../stdafx.h"
26 #pragma warning (disable : 4244)
28 #include "../executor.h"
32 #include <SFML/Graphics.hpp>
34 #include <boost/foreach.hpp>
35 #include <boost/optional.hpp>
36 #include <boost/circular_buffer.hpp>
37 #include <boost/range/algorithm_ext/erase.hpp>
39 #include <tbb/concurrent_unordered_map.h>
40 #include <tbb/atomic.h>
41 #include <tbb/spin_mutex.h>
47 namespace caspar { namespace diagnostics {
49 int color(float r, float g, float b, float a)
52 code |= static_cast<int>(r*255.0f+0.5f) << 24;
53 code |= static_cast<int>(g*255.0f+0.5f) << 16;
54 code |= static_cast<int>(b*255.0f+0.5f) << 8;
55 code |= static_cast<int>(a*255.0f+0.5f) << 0;
59 std::tuple<float, float, float, float> color(int code)
61 float r = static_cast<float>((code >> 24) & 255)/255.0f;
62 float g = static_cast<float>((code >> 16) & 255)/255.0f;
63 float b = static_cast<float>((code >> 8) & 255)/255.0f;
64 float a = static_cast<float>((code >> 0) & 255)/255.0f;
65 return std::make_tuple(r, g, b, a);
68 struct drawable : public sf::Drawable
71 virtual void render(sf::RenderTarget& target) = 0;
72 virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}
75 class context : public drawable
77 std::unique_ptr<sf::RenderWindow> window_;
79 std::list<std::weak_ptr<drawable>> drawables_;
84 static void register_drawable(const std::shared_ptr<drawable>& drawable)
89 get_instance().executor_.begin_invoke([=]
91 get_instance().do_register_drawable(drawable);
92 }, task_priority::high_priority);
95 static void show(bool value)
97 get_instance().executor_.begin_invoke([=]
99 get_instance().do_show(value);
100 }, task_priority::high_priority);
104 context() : executor_(L"diagnostics")
106 executor_.begin_invoke([=]
108 SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);
112 void do_show(bool value)
118 window_.reset(new sf::RenderWindow(sf::VideoMode(750, 750), "CasparCG Diagnostics"));
119 window_->SetPosition(0, 0);
120 window_->SetActive();
122 glEnable(GL_LINE_SMOOTH);
123 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
124 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
138 while(window_->GetEvent(e))
140 if(e.Type == sf::Event::Closed)
146 glClear(GL_COLOR_BUFFER_BIT);
147 window_->Draw(*this);
149 boost::this_thread::sleep(boost::posix_time::milliseconds(10));
150 executor_.begin_invoke([this]{tick();});
153 void render(sf::RenderTarget& target)
155 auto count = std::max<size_t>(8, drawables_.size());
156 float target_dy = 1.0f/static_cast<float>(count);
160 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)
162 auto drawable = it->lock();
165 drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));
166 float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);
167 drawable->SetPosition(0.0f, target_y);
168 target.Draw(*drawable);
172 it = drawables_.erase(it);
176 void do_register_drawable(const std::shared_ptr<drawable>& drawable)
178 drawables_.push_back(drawable);
179 auto it = drawables_.begin();
180 while(it != drawables_.end())
185 it = drawables_.erase(it);
189 static context& get_instance()
196 class line : public drawable
198 boost::circular_buffer<std::pair<float, bool>> line_data_;
200 tbb::atomic<float> tick_data_;
201 tbb::atomic<bool> tick_tag_;
202 tbb::atomic<int> color_;
204 line(size_t res = 1200)
211 line_data_.push_back(std::make_pair(-1.0f, false));
214 void set_value(float value)
224 void set_color(int color)
234 void render(sf::RenderTarget&)
236 float dx = 1.0f/static_cast<float>(line_data_.capacity());
237 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;
239 line_data_.push_back(std::make_pair(tick_data_, tick_tag_));
242 glBegin(GL_LINE_STRIP);
243 auto c = color(color_);
244 glColor4f(std::get<0>(c), std::get<1>(c), std::get<2>(c), 0.8f);
245 for(size_t n = 0; n < line_data_.size(); ++n)
246 if(line_data_[n].first > -0.5)
247 glVertex3d(x+n*dx, std::max(0.05, std::min(0.95, (1.0f-line_data_[n].first)*0.8 + 0.1f)), 0.0);
250 glEnable(GL_LINE_STIPPLE);
251 glLineStipple(3, 0xAAAA);
252 for(size_t n = 0; n < line_data_.size(); ++n)
254 if(line_data_[n].second)
256 glBegin(GL_LINE_STRIP);
257 glVertex3f(x+n*dx, 0.0f, 0.0f);
258 glVertex3f(x+n*dx, 1.0f, 0.0f);
262 glDisable(GL_LINE_STIPPLE);
266 struct graph::impl : public drawable
268 tbb::concurrent_unordered_map<std::string, diagnostics::line> lines_;
270 tbb::spin_mutex mutex_;
277 void set_text(const std::wstring& value)
282 text_ = std::move(temp);
286 void set_value(const std::string& name, double value)
288 lines_[name].set_value(value);
291 void set_tag(const std::string& name)
293 lines_[name].set_tag();
296 void set_color(const std::string& name, int color)
298 lines_[name].set_color(color);
302 void render(sf::RenderTarget& target)
304 const size_t text_size = 15;
305 const size_t text_margin = 2;
306 const size_t text_offset = (text_size+text_margin*2)*2;
308 std::wstring text_str;
310 tbb::spin_mutex::scoped_lock lock(mutex_);
314 sf::String text(text_str.c_str(), sf::Font::GetDefaultFont(), text_size);
315 text.SetStyle(sf::String::Italic);
316 text.Move(text_margin, text_margin);
319 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);
321 float x_offset = text_margin;
322 for(auto it = lines_.begin(); it != lines_.end(); ++it)
324 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);
325 line_text.SetPosition(x_offset, text_margin+text_offset/2);
326 auto c = it->second.get_color();
327 line_text.SetColor(sf::Color((c >> 24) & 255, (c >> 16) & 255, (c >> 8) & 255, (c >> 0) & 255));
328 target.Draw(line_text);
329 x_offset = line_text.GetRect().Right + text_margin*2;
332 glDisable(GL_TEXTURE_2D);
336 glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
337 glVertex2f(1.0f, 0.99f);
338 glVertex2f(0.0f, 0.99f);
339 glVertex2f(0.0f, 0.01f);
340 glVertex2f(1.0f, 0.01f);
344 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);
345 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);
347 glEnable(GL_LINE_STIPPLE);
348 glLineStipple(3, 0xAAAA);
349 glColor4f(1.0f, 1.0f, 1.9f, 0.5f);
350 glBegin(GL_LINE_STRIP);
351 glVertex3f(0.0f, (1.0f-0.5f) * 0.8f + 0.1f, 0.0f);
352 glVertex3f(1.0f, (1.0f-0.5f) * 0.8f + 0.1f, 0.0f);
354 glBegin(GL_LINE_STRIP);
355 glVertex3f(0.0f, (1.0f-0.0f) * 0.8f + 0.1f, 0.0f);
356 glVertex3f(1.0f, (1.0f-0.0f) * 0.8f + 0.1f, 0.0f);
358 glBegin(GL_LINE_STRIP);
359 glVertex3f(0.0f, (1.0f-1.0f) * 0.8f + 0.1f, 0.0f);
360 glVertex3f(1.0f, (1.0f-1.0f) * 0.8f + 0.1f, 0.0f);
362 glDisable(GL_LINE_STIPPLE);
364 //target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
365 //target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));
367 for(auto it = lines_.begin(); it != lines_.end(); ++it)
368 target.Draw(it->second);
374 impl& operator=(impl&);
377 graph::graph() : impl_(new impl())
381 void graph::set_text(const std::wstring& value){impl_->set_text(value);}
382 void graph::set_value(const std::string& name, double value){impl_->set_value(name, value);}
383 void graph::set_color(const std::string& name, int color){impl_->set_color(name, color);}
384 void graph::set_tag(const std::string& name){impl_->set_tag(name);}
386 void register_graph(const spl::shared_ptr<graph>& graph)
388 context::register_drawable(graph->impl_);
391 void show_graphs(bool value)
393 context::show(value);
401 // std::wstring name_;
402 // boost::circular_buffer<data> ticks_;
404 // impl(const std::wstring& name)
408 // void set_value(float value)
410 // ticks_.push_back();
411 // ticks_.back().value = value;
414 // void set_value(float value)
422 //line::line(const std::wstring& name) : impl_(new impl(name)){}
423 //std::wstring line::print() const {return impl_->name_;}
424 //void line::set_value(float value){impl_->set_value(value);}
425 //void line::set_value(float value){impl_->set_value(value);}
426 //boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}
430 // std::map<std::wstring, line> lines_;
434 // impl(const std::wstring& name)
435 // : printer_([=]{return name;}){}
437 // impl(const printer& parent_printer)
438 // : printer_(parent_printer){}
440 // void set_value(const std::wstring& name, float value)
442 // auto it = lines_.find(name);
443 // if(it == lines_.end())
444 // it = lines_.insert(std::make_pair(name, line(name))).first;
446 // it->second.set_value(value);
449 // void set_value(const std::wstring& name, float value)
451 // auto it = lines_.find(name);
452 // if(it == lines_.end())
453 // it = lines_.insert(std::make_pair(name, line(name))).first;
455 // it->second.set_value(value);
458 // void set_color(const std::wstring& name, color color)
463 // std::map<std::wstring, line>& get_lines()
468 // color get_color() const
473 // std::wstring print() const
475 // return printer_ ? printer_() : L"graph";
479 //graph::graph(const std::wstring& name) : impl_(new impl(name)){}
480 //graph::graph(const printer& parent_printer) : impl_(new impl(parent_printer)){}
481 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
482 //void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}
483 //void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}
484 //color graph::get_color() const {return impl_->get_color();}
485 //std::wstring graph::print() const {return impl_->print();}
487 //spl::shared_ptr<graph> graph::clone() const
489 // spl::shared_ptr<graph> clone(new graph(std::wstring(L"")));
490 // clone->impl_->printer_ = impl_->printer_;
491 // clone->impl_->lines_ = impl_->lines_;
492 // clone->impl_->color_ = impl_->color_;
495 //std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}
497 //std::vector<spl::shared_ptr<graph>> g_graphs;
499 //spl::shared_ptr<graph> create_graph(const std::string& name)
501 // g_graphs.push_back(spl::make_shared<graph>(name));
502 // return g_graphs.back();
505 //spl::shared_ptr<graph> create_graph(const printer& parent_printer)
507 // g_graphs.push_back(spl::make_shared<graph>(parent_printer));
508 // return g_graphs.back();
511 //static std::vector<spl::shared_ptr<graph>> get_all_graphs()
513 // std::vector<spl::shared_ptr<graph>> graphs;
514 // BOOST_FOREACH(auto& graph, g_graphs)
515 // graphs.push_back(graph->clone());