+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG.\r
+*\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+*/\r
#include "../stdafx.h"\r
\r
#include "graph.h"\r
#pragma warning (disable : 4244)\r
\r
#include "../concurrency/executor.h"\r
-#include "../utility/timer.h"\r
+#include "../env.h"\r
\r
#include <SFML/Graphics.hpp>\r
\r
#include <array>\r
\r
namespace caspar { namespace diagnostics {\r
-\r
+ \r
struct drawable : public sf::Drawable\r
{\r
virtual ~drawable(){}\r
\r
class context : public drawable\r
{ \r
- timer timer_;\r
- sf::RenderWindow window_;\r
+ std::unique_ptr<sf::RenderWindow> window_;\r
\r
std::list<std::shared_ptr<drawable>> drawables_;\r
- std::map<size_t, sf::Font> fonts_;\r
\r
executor executor_;\r
public: \r
\r
static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
{\r
+ if(!drawable)\r
+ return;\r
+\r
begin_invoke([=]\r
{\r
- get_instance().drawables_.push_back(drawable);\r
+ get_instance().do_register_drawable(drawable);\r
});\r
}\r
- \r
+\r
+ static void show(bool value)\r
+ {\r
+ begin_invoke([=]\r
+ { \r
+ get_instance().do_show(value);\r
+ });\r
+ }\r
+ \r
private:\r
+ context() : executor_(L"diagnostics")\r
+ {\r
+ }\r
+\r
+ void do_show(bool value)\r
+ {\r
+ if(value)\r
+ {\r
+ if(!window_)\r
+ {\r
+ SetThreadPriority(GetCurrentThread(), BELOW_NORMAL_PRIORITY_CLASS);\r
+ window_.reset(new sf::RenderWindow(sf::VideoMode(600, 1000), "CasparCG Diagnostics"));\r
+ window_->SetPosition(0, 0);\r
+ window_->SetActive();\r
+ glEnable(GL_BLEND);\r
+ glEnable(GL_LINE_SMOOTH);\r
+ glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
+ tick();\r
+ }\r
+ }\r
+ else\r
+ window_.reset();\r
+ }\r
\r
void tick()\r
{\r
+ if(!window_)\r
+ return;\r
+\r
sf::Event e;\r
- while(window_.GetEvent(e)){} \r
+ while(window_->GetEvent(e)){} \r
glClear(GL_COLOR_BUFFER_BIT);\r
- window_.Draw(*this);\r
- window_.Display();\r
- timer_.tick(1.0/50.0);\r
+ window_->Draw(*this);\r
+ window_->Display();\r
+ boost::this_thread::sleep(boost::posix_time::milliseconds(20));\r
executor_.begin_invoke([this]{tick();});\r
}\r
\r
void render(sf::RenderTarget& target)\r
{\r
- auto count = std::max<size_t>(5, drawables_.size());\r
+ auto count = std::max<size_t>(8, drawables_.size());\r
+ float target_dy = 1.0f/static_cast<float>(count);\r
\r
+ float last_y = 0.0f;\r
int n = 0;\r
for(auto it = drawables_.begin(); it != drawables_.end(); ++n)\r
{\r
auto& drawable = *it;\r
if(!drawable.unique())\r
{\r
- drawable->SetScale(window_.GetWidth(), window_.GetHeight()/count);\r
- drawable->SetPosition(0.0f, n* window_.GetHeight()/count);\r
+ drawable->SetScale(static_cast<float>(window_->GetWidth()), static_cast<float>(target_dy*window_->GetHeight()));\r
+ float target_y = std::max(last_y, static_cast<float>(n * window_->GetHeight())*target_dy);\r
+ drawable->SetPosition(0.0f, target_y); \r
target.Draw(*drawable); \r
++it; \r
}\r
- else \r
+ else \r
it = drawables_.erase(it); \r
} \r
} \r
\r
+ void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
+ {\r
+ if(std::find(drawables_.begin(), drawables_.end(), drawable) == drawables_.end())\r
+ drawables_.push_back(drawable);\r
+ }\r
+ \r
static context& get_instance()\r
{\r
static context impl;\r
return impl;\r
}\r
-\r
- context()\r
- {\r
- executor_.start();\r
- executor_.begin_invoke([this]\r
- {\r
- window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
- window_.SetPosition(0, 0);\r
- window_.SetActive();\r
- glEnable(GL_BLEND);\r
- glEnable(GL_LINE_SMOOTH);\r
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
- tick();\r
- });\r
- }\r
};\r
- \r
+\r
class guide : public drawable\r
{\r
float value_;\r
class line : public drawable\r
{\r
boost::optional<diagnostics::guide> guide_;\r
- boost::circular_buffer<float> line_data_;\r
- std::vector<float> tick_data_;\r
+ boost::circular_buffer<std::pair<double, bool>> line_data_;\r
+\r
+ boost::circular_buffer<double> tick_data_;\r
+ bool tick_tag_;\r
color c_;\r
public:\r
line(size_t res = 600)\r
: line_data_(res)\r
- , c_(1.0f, 1.0f, 1.0f){}\r
+ , tick_data_(50)\r
+ , tick_tag_(false)\r
+ , c_(1.0f, 1.0f, 1.0f)\r
+ {\r
+ line_data_.push_back(std::make_pair(-1.0f, false));\r
+ }\r
\r
- void update(float value)\r
+ void update(double value)\r
{\r
tick_data_.push_back(value);\r
}\r
\r
+ void set(double value)\r
+ {\r
+ tick_data_.clear();\r
+ tick_data_.push_back(value);\r
+ }\r
+ \r
+ void tag()\r
+ {\r
+ tick_tag_ = true;\r
+ }\r
+\r
void guide(const guide& guide)\r
{\r
guide_ = guide;\r
if(!tick_data_.empty())\r
{\r
float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
- line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));\r
+ line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
tick_data_.clear();\r
}\r
else if(!line_data_.empty())\r
{\r
- line_data_.push_back(line_data_.back());\r
+ line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
}\r
+ tick_tag_ = false;\r
\r
if(guide_)\r
target.Draw(*guide_);\r
\r
glBegin(GL_LINE_STRIP);\r
- glColor4f(c_.red, c_.green, c_.blue, 1.0f); \r
- for(size_t n = 0; n < line_data_.size(); ++n) \r
- glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, (1.0f-line_data_[n])*0.8f + 0.1f)), 0.0f); \r
+ glColor4f(c_.red, c_.green, c_.blue, 0.8f); \r
+ for(size_t n = 0; n < line_data_.size(); ++n) \r
+ if(line_data_[n].first > -0.5)\r
+ 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
glEnd();\r
+ \r
+ glEnable(GL_LINE_STIPPLE);\r
+ glLineStipple(3, 0xAAAA);\r
+ for(size_t n = 0; n < line_data_.size(); ++n) \r
+ {\r
+ if(line_data_[n].second)\r
+ {\r
+ glBegin(GL_LINE_STRIP);\r
+ glColor4f(c_.red, c_.green, c_.blue, c_.alpha); \r
+ glVertex3f(x+n*dx, 0.0f, 0.0f); \r
+ glVertex3f(x+n*dx, 1.0f, 0.0f); \r
+ glEnd();\r
+ }\r
+ }\r
+ glDisable(GL_LINE_STIPPLE);\r
}\r
};\r
\r
{\r
std::map<std::string, diagnostics::line> lines_;\r
std::string name_;\r
+ std::string text_;\r
+ \r
+ implementation(const std::string& name) \r
+ : name_(name)\r
+ , text_(name_){}\r
+ \r
+ void set_text(const std::string& value)\r
+ {\r
+ text_ = value;\r
+ }\r
+\r
+ void update(const std::string& name, double value)\r
+ {\r
+ lines_[name].update(value);\r
+ }\r
\r
- implementation(const std::string& name) : name_(name){}\r
+ void set(const std::string& name, double value)\r
+ {\r
+ lines_[name].set(value);\r
+ }\r
\r
- void update(const std::string& name, float value)\r
+ void tag(const std::string& name)\r
{\r
- context::begin_invoke([=]\r
- {\r
- lines_[name].update(value);\r
- });\r
+ lines_[name].tag();\r
}\r
\r
void set_color(const std::string& name, color c)\r
{\r
- context::begin_invoke([=]\r
- {\r
- lines_[name].set_color(c);\r
- });\r
+ lines_[name].set_color(c);\r
}\r
\r
- void guide(const std::string& name, float value)\r
+ void guide(const std::string& name, double value)\r
{\r
- context::begin_invoke([=]\r
- {\r
- lines_[name].guide(diagnostics::guide(value));\r
- });\r
+ lines_[name].guide(diagnostics::guide(value)); \r
}\r
-\r
+ \r
private:\r
void render(sf::RenderTarget& target)\r
{\r
const size_t text_size = 15;\r
const size_t text_margin = 2;\r
- const size_t text_offset = text_size+text_margin*2;\r
+ const size_t text_offset = (text_size+text_margin*2)*2;\r
\r
- sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
+ sf::String text(text_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
text.SetStyle(sf::String::Italic);\r
text.Move(text_margin, text_margin);\r
\r
glPushMatrix();\r
- glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
- target.Draw(text);\r
- float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;\r
- for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
- { \r
- sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
- line_text.SetPosition(x_offset, text_margin);\r
- auto c = it->second.get_color();\r
- line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
- target.Draw(line_text);\r
- x_offset = line_text.GetRect().Right + text_margin*2;\r
- }\r
+ glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
+ target.Draw(text);\r
+ float x_offset = text_margin;\r
+ for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
+ { \r
+ sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
+ line_text.SetPosition(x_offset, text_margin+text_offset/2);\r
+ auto c = it->second.get_color();\r
+ line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
+ target.Draw(line_text);\r
+ x_offset = line_text.GetRect().Right + text_margin*2;\r
+ }\r
\r
- glDisable(GL_TEXTURE_2D);\r
+ glDisable(GL_TEXTURE_2D);\r
glPopMatrix();\r
\r
glBegin(GL_QUADS);\r
glEnd();\r
\r
glPushMatrix();\r
- glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
- glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
+ glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
+ glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
\r
- target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
- target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
+ target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
+ target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
\r
- for(auto it = lines_.begin(); it != lines_.end(); ++it) \r
- target.Draw(it->second);\r
+ for(auto it = lines_.begin(); it != lines_.end(); ++it) \r
+ target.Draw(it->second);\r
\r
glPopMatrix();\r
-\r
}\r
\r
implementation(implementation&);\r
implementation& operator=(implementation&);\r
};\r
\r
-graph::graph(const std::string& name) : impl_(new implementation(name))\r
+graph::graph() : impl_(new implementation(""))\r
+{\r
+\r
+}\r
+\r
+void graph::set_text(const std::string& value)\r
+{\r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->set_text(value);\r
+ });\r
+}\r
+\r
+void graph::set_text(const std::wstring& value)\r
{\r
- context::register_drawable(impl_);\r
+ set_text(narrow(value));\r
}\r
-void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
-void graph::guide(const std::string& name, float value){impl_->guide(name, value);}\r
-void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
\r
-safe_ptr<graph> create_graph(const std::string& name)\r
+void graph::update_value(const std::string& name, double value)\r
{\r
- return safe_ptr<graph>(new graph(name));\r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->update(name, value);\r
+ });\r
}\r
+void graph::set_value(const std::string& name, double value)\r
+{ \r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->set(name, value);\r
+ }); \r
+}\r
+void graph::set_color(const std::string& name, color c)\r
+{ \r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->set_color(name, c);\r
+ });\r
+}\r
+void graph::add_tag(const std::string& name)\r
+{ \r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->tag(name);\r
+ });\r
+}\r
+void graph::add_guide(const std::string& name, double value)\r
+{ \r
+ auto p = impl_;\r
+ context::begin_invoke([=]\r
+ { \r
+ p->guide(name, value);\r
+ });\r
+}\r
+\r
+void register_graph(const safe_ptr<graph>& graph)\r
+{\r
+ context::register_drawable(graph->impl_);\r
+}\r
+\r
+void show_graphs(bool value)\r
+{\r
+ context::show(value);\r
+}\r
+\r
+//namespace v2\r
+//{ \r
+// \r
+//struct line::implementation\r
+//{\r
+// std::wstring name_;\r
+// boost::circular_buffer<data> ticks_;\r
+//\r
+// implementation(const std::wstring& name) \r
+// : name_(name)\r
+// , ticks_(1024){}\r
+// \r
+// void update_value(float value)\r
+// {\r
+// ticks_.push_back();\r
+// ticks_.back().value = value;\r
+// }\r
+//\r
+// void set_value(float value)\r
+// {\r
+// ticks_.clear();\r
+// update_value(value);\r
+// }\r
+//};\r
+//\r
+//line::line(){}\r
+//line::line(const std::wstring& name) : impl_(new implementation(name)){}\r
+//std::wstring line::print() const {return impl_->name_;}\r
+//void line::update_value(float value){impl_->update_value(value);}\r
+//void line::set_value(float value){impl_->set_value(value);}\r
+//boost::circular_buffer<data>& line::ticks() { return impl_->ticks_;}\r
+//\r
+//struct graph::implementation\r
+//{\r
+// std::map<std::wstring, line> lines_;\r
+// color color_;\r
+// printer printer_;\r
+//\r
+// implementation(const std::wstring& name) \r
+// : printer_([=]{return name;}){}\r
+//\r
+// implementation(const printer& parent_printer) \r
+// : printer_(parent_printer){}\r
+// \r
+// void update_value(const std::wstring& name, float value)\r
+// {\r
+// auto it = lines_.find(name);\r
+// if(it == lines_.end())\r
+// it = lines_.insert(std::make_pair(name, line(name))).first;\r
+//\r
+// it->second.update_value(value);\r
+// }\r
+//\r
+// void set_value(const std::wstring& name, float value)\r
+// {\r
+// auto it = lines_.find(name);\r
+// if(it == lines_.end())\r
+// it = lines_.insert(std::make_pair(name, line(name))).first;\r
+//\r
+// it->second.set_value(value);\r
+// }\r
+// \r
+// void set_color(const std::wstring& name, color color)\r
+// {\r
+// color_ = color;\r
+// }\r
+//\r
+// std::map<std::wstring, line>& get_lines()\r
+// {\r
+// return lines_;\r
+// }\r
+// \r
+// color get_color() const\r
+// {\r
+// return color_;\r
+// }\r
+//\r
+// std::wstring print() const\r
+// {\r
+// return printer_ ? printer_() : L"graph";\r
+// }\r
+//};\r
+// \r
+//graph::graph(const std::wstring& name) : impl_(new implementation(name)){}\r
+//graph::graph(const printer& parent_printer) : impl_(new implementation(parent_printer)){}\r
+//void graph::update_value(const std::wstring& name, float value){impl_->update_value(name, value);}\r
+//void graph::set_value(const std::wstring& name, float value){impl_->set_value(name, value);}\r
+//void graph::set_color(const std::wstring& name, color c){impl_->set_color(name, c);}\r
+//color graph::get_color() const {return impl_->get_color();}\r
+//std::wstring graph::print() const {return impl_->print();}\r
+//\r
+//safe_ptr<graph> graph::clone() const \r
+//{\r
+// safe_ptr<graph> clone(new graph(std::wstring(L"")));\r
+// clone->impl_->printer_ = impl_->printer_;\r
+// clone->impl_->lines_ = impl_->lines_;\r
+// clone->impl_->color_ = impl_->color_; \r
+//}\r
+//\r
+//std::map<std::wstring, line>& graph::get_lines() {impl_->get_lines();}\r
+//\r
+//std::vector<safe_ptr<graph>> g_graphs;\r
+//\r
+//safe_ptr<graph> create_graph(const std::string& name)\r
+//{\r
+// g_graphs.push_back(make_safe<graph>(name));\r
+// return g_graphs.back();\r
+//}\r
+//\r
+//safe_ptr<graph> create_graph(const printer& parent_printer)\r
+//{\r
+// g_graphs.push_back(make_safe<graph>(parent_printer));\r
+// return g_graphs.back();\r
+//}\r
+//\r
+//static std::vector<safe_ptr<graph>> get_all_graphs()\r
+//{\r
+// std::vector<safe_ptr<graph>> graphs;\r
+// BOOST_FOREACH(auto& graph, g_graphs)\r
+// graphs.push_back(graph->clone());\r
+//\r
+// return graphs;\r
+//}\r
+//\r
+//}\r
\r
}}
\ No newline at end of file