]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
2.0.0.2: Further diagnostics improvements.
[casparcg] / common / diagnostics / graph.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "graph.h"\r
4 #include "context.h"\r
5 \r
6 #pragma warning (disable : 4244)\r
7 \r
8 #include "../concurrency/executor.h"\r
9 #include "../utility/timer.h"\r
10 \r
11 #include <SFML/Graphics.hpp>\r
12 \r
13 #include <boost/foreach.hpp>\r
14 #include <boost/optional.hpp>\r
15 #include <boost/circular_buffer.hpp>\r
16 #include <boost/range/algorithm_ext/erase.hpp>\r
17 \r
18 #include <numeric>\r
19 #include <map>\r
20 #include <array>\r
21 \r
22 namespace caspar { namespace diagnostics {\r
23 \r
24 class guide : public drawable\r
25 {\r
26         float value_;\r
27         color c_;\r
28 public:\r
29         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
30                 : value_(0.0f)\r
31                 , c_(c){}\r
32 \r
33         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
34                 : value_(value)\r
35                 , c_(c){}\r
36                         \r
37         void set_color(color c) {c_ = c;}\r
38 \r
39         void render(sf::RenderTarget&)\r
40         {               \r
41                 glEnable(GL_LINE_STIPPLE);\r
42                 glLineStipple(3, 0xAAAA);\r
43                 glBegin(GL_LINE_STRIP); \r
44                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
45                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
46                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
47                 glEnd();\r
48                 glDisable(GL_LINE_STIPPLE);\r
49         }\r
50 };\r
51 \r
52 class line : public drawable\r
53 {\r
54         boost::optional<diagnostics::guide> guide_;\r
55         boost::circular_buffer<std::pair<float, bool>> line_data_;\r
56 \r
57         std::vector<float>              tick_data_;\r
58         bool                                    tick_tag_;\r
59         color c_;\r
60 public:\r
61         line(size_t res = 600)\r
62                 : line_data_(res)\r
63                 , tick_tag_(false)\r
64                 , c_(1.0f, 1.0f, 1.0f)\r
65         {\r
66                 line_data_.push_back(std::make_pair(-1.0f, false));\r
67         }\r
68         \r
69         void update(float value)\r
70         {\r
71                 tick_data_.push_back(value);\r
72         }\r
73         \r
74         void tag()\r
75         {\r
76                 tick_tag_ = true;\r
77         }\r
78 \r
79         void guide(const guide& guide)\r
80         {\r
81                 guide_ = guide;\r
82                 guide_->set_color(c_);\r
83         }\r
84         \r
85         void set_color(color c)\r
86         {\r
87                 c_ = c;\r
88                 if(guide_)\r
89                         guide_->set_color(c_);\r
90         }\r
91 \r
92         color get_color() const { return c_; }\r
93         \r
94         void render(sf::RenderTarget& target)\r
95         {\r
96                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
97                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
98 \r
99                 if(!tick_data_.empty())\r
100                 {\r
101                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
102                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
103                         tick_data_.clear();\r
104                 }\r
105                 else if(!line_data_.empty())\r
106                 {\r
107                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
108                 }\r
109                 tick_tag_ = false;\r
110 \r
111                 if(guide_)\r
112                         target.Draw(*guide_);\r
113                 \r
114                 glBegin(GL_LINE_STRIP);\r
115                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);             \r
116                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
117                         if(line_data_[n].first > -0.5)\r
118                                 glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, (1.0f-line_data_[n].first)*0.8f + 0.1f)), 0.0f);             \r
119                 glEnd();\r
120                                 \r
121                 glEnable(GL_LINE_STIPPLE);\r
122                 glLineStipple(3, 0xAAAA);\r
123                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
124                 {\r
125                         if(line_data_[n].second)\r
126                         {\r
127                                 glBegin(GL_LINE_STRIP);\r
128                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
129                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
130                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
131                                 glEnd();\r
132                         }\r
133                 }\r
134                 glDisable(GL_LINE_STIPPLE);\r
135         }\r
136 };\r
137 \r
138 struct graph::implementation : public drawable\r
139 {\r
140         std::map<std::string, diagnostics::line> lines_;\r
141         std::string name_;\r
142 \r
143         implementation(const std::string& name) : name_(name){}\r
144 \r
145         void update(const std::string& name, float value)\r
146         {\r
147                 context::begin_invoke([=]\r
148                 {\r
149                         lines_[name].update(value);\r
150                 });\r
151         }\r
152 \r
153         void tag(const std::string& name)\r
154         {\r
155                 context::begin_invoke([=]\r
156                 {\r
157                         lines_[name].tag();\r
158                 });\r
159         }\r
160 \r
161         void set_color(const std::string& name, color c)\r
162         {\r
163                 context::begin_invoke([=]\r
164                 {\r
165                         lines_[name].set_color(c);\r
166                 });\r
167         }\r
168         \r
169         void guide(const std::string& name, float value)\r
170         {\r
171                 context::begin_invoke([=]\r
172                 {\r
173                         lines_[name].guide(diagnostics::guide(value));\r
174                 });\r
175         }\r
176 \r
177 private:\r
178         void render(sf::RenderTarget& target)\r
179         {\r
180                 const size_t text_size = 15;\r
181                 const size_t text_margin = 2;\r
182                 const size_t text_offset = text_size+text_margin*2;\r
183 \r
184                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
185                 text.SetStyle(sf::String::Italic);\r
186                 text.Move(text_margin, text_margin);\r
187                 \r
188                 glPushMatrix();\r
189                 glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
190                 target.Draw(text);\r
191                 float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;\r
192                 for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
193                 {                                               \r
194                         sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
195                         line_text.SetPosition(x_offset, text_margin);\r
196                         auto c = it->second.get_color();\r
197                         line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
198                         target.Draw(line_text);\r
199                         x_offset = line_text.GetRect().Right + text_margin*2;\r
200                 }\r
201 \r
202                 glDisable(GL_TEXTURE_2D);\r
203                 glPopMatrix();\r
204 \r
205                 glBegin(GL_QUADS);\r
206                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
207                         glVertex2f(1.0f, 0.99f);\r
208                         glVertex2f(0.0f, 0.99f);\r
209                         glVertex2f(0.0f, 0.01f);        \r
210                         glVertex2f(1.0f, 0.01f);        \r
211                 glEnd();\r
212 \r
213                 glPushMatrix();\r
214                 glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
215                 glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
216                 \r
217                 target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
218                 target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
219 \r
220                 for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
221                         target.Draw(it->second);\r
222                 \r
223                 glPopMatrix();\r
224 \r
225         }\r
226 \r
227         implementation(implementation&);\r
228         implementation& operator=(implementation&);\r
229 };\r
230         \r
231 graph::graph(const std::string& name) : impl_(new implementation(name))\r
232 {\r
233         context::register_drawable(impl_);\r
234 }\r
235 \r
236 void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
237 void graph::tag(const std::string& name){impl_->tag(name);}\r
238 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}\r
239 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
240 \r
241 safe_ptr<graph> create_graph(const std::string& name)\r
242 {\r
243         return safe_ptr<graph>(new graph(name));\r
244 }\r
245 \r
246 }}