]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
2.0.0.2: Added audio support to decklink producer.
[casparcg] / common / diagnostics / graph.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "graph.h"\r
4 \r
5 #pragma warning (disable : 4244)\r
6 \r
7 #include "../concurrency/executor.h"\r
8 #include "../utility/timer.h"\r
9 \r
10 #include <SFML/Graphics.hpp>\r
11 \r
12 #include <boost/foreach.hpp>\r
13 #include <boost/optional.hpp>\r
14 #include <boost/circular_buffer.hpp>\r
15 #include <boost/range/algorithm_ext/erase.hpp>\r
16 \r
17 #include <numeric>\r
18 #include <map>\r
19 #include <array>\r
20 \r
21 namespace caspar { namespace diagnostics {\r
22                 \r
23 struct drawable : public sf::Drawable\r
24 {\r
25         virtual ~drawable(){}\r
26         virtual void render(sf::RenderTarget& target) = 0;\r
27         virtual void Render(sf::RenderTarget& target) const { const_cast<drawable*>(this)->render(target);}\r
28 };\r
29 \r
30 class context : public drawable\r
31 {       \r
32         timer timer_;\r
33         sf::RenderWindow window_;\r
34         \r
35         std::list<std::shared_ptr<drawable>> drawables_;\r
36                 \r
37         executor executor_;\r
38 public:                                 \r
39 \r
40         template<typename Func>\r
41         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
42         {       \r
43                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
44         }\r
45 \r
46         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
47         {\r
48                 begin_invoke([=]\r
49                 {\r
50                         get_instance().do_register_drawable(drawable);\r
51                 });\r
52         }\r
53                         \r
54 private:\r
55         context()\r
56         {\r
57                 executor_.start();\r
58                 executor_.begin_invoke([this]\r
59                 {\r
60                         window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
61                         window_.SetPosition(0, 0);\r
62                         window_.SetActive();\r
63                         glEnable(GL_BLEND);\r
64                         glEnable(GL_LINE_SMOOTH);\r
65                         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
66                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
67                         tick();\r
68                 });\r
69         }\r
70 \r
71         void tick()\r
72         {\r
73                 sf::Event e;\r
74                 while(window_.GetEvent(e)){}            \r
75                 glClear(GL_COLOR_BUFFER_BIT);\r
76                 window_.Draw(*this);\r
77                 window_.Display();\r
78                 timer_.tick(1.0/50.0);\r
79                 executor_.begin_invoke([this]{tick();});\r
80         }\r
81 \r
82         void render(sf::RenderTarget& target)\r
83         {\r
84                 auto count = std::max<size_t>(10, drawables_.size());\r
85                 float target_dy = 1.0f/static_cast<float>(count);\r
86 \r
87                 float last_y = 0.0f;\r
88                 int n = 0;\r
89                 for(auto it = drawables_.begin(); it != drawables_.end(); ++n)\r
90                 {\r
91                         auto& drawable = *it;\r
92                         if(!drawable.unique())\r
93                         {\r
94                                 drawable->SetScale(static_cast<float>(window_.GetWidth()), static_cast<float>(target_dy*window_.GetHeight()));\r
95                                 float target_y = std::max(last_y, static_cast<float>(n * window_.GetHeight())*target_dy);\r
96                                 drawable->SetPosition(0.0f, target_y);                  \r
97                                 target.Draw(*drawable);                         \r
98                                 ++it;           \r
99                         }\r
100                         else    \r
101                                 it = drawables_.erase(it);                      \r
102                 }               \r
103         }       \r
104         \r
105         void do_register_drawable(const std::shared_ptr<drawable>& drawable)\r
106         {\r
107                 drawables_.push_back(drawable);\r
108         }\r
109         \r
110         static context& get_instance()\r
111         {\r
112                 static context impl;\r
113                 return impl;\r
114         }\r
115 };\r
116 \r
117 class guide : public drawable\r
118 {\r
119         float value_;\r
120         color c_;\r
121 public:\r
122         guide(color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
123                 : value_(0.0f)\r
124                 , c_(c){}\r
125 \r
126         guide(float value, color c = color(1.0f, 1.0f, 1.0f, 0.6f)) \r
127                 : value_(value)\r
128                 , c_(c){}\r
129                         \r
130         void set_color(color c) {c_ = c;}\r
131 \r
132         void render(sf::RenderTarget&)\r
133         {               \r
134                 glEnable(GL_LINE_STIPPLE);\r
135                 glLineStipple(3, 0xAAAA);\r
136                 glBegin(GL_LINE_STRIP); \r
137                         glColor4f(c_.red, c_.green, c_.blue+0.2f, c_.alpha);            \r
138                         glVertex3f(0.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);            \r
139                         glVertex3f(1.0f, (1.0f-value_) * 0.8f + 0.1f, 0.0f);    \r
140                 glEnd();\r
141                 glDisable(GL_LINE_STIPPLE);\r
142         }\r
143 };\r
144 \r
145 class line : public drawable\r
146 {\r
147         boost::optional<diagnostics::guide> guide_;\r
148         boost::circular_buffer<std::pair<float, bool>> line_data_;\r
149 \r
150         std::vector<float>              tick_data_;\r
151         bool                                    tick_tag_;\r
152         color c_;\r
153 public:\r
154         line(size_t res = 600)\r
155                 : line_data_(res)\r
156                 , tick_tag_(false)\r
157                 , c_(1.0f, 1.0f, 1.0f)\r
158         {\r
159                 line_data_.push_back(std::make_pair(-1.0f, false));\r
160         }\r
161         \r
162         void update(float value)\r
163         {\r
164                 tick_data_.push_back(value);\r
165         }\r
166 \r
167         void set(float value)\r
168         {\r
169                 tick_data_.clear();\r
170                 tick_data_.push_back(value);\r
171         }\r
172         \r
173         void tag()\r
174         {\r
175                 tick_tag_ = true;\r
176         }\r
177 \r
178         void guide(const guide& guide)\r
179         {\r
180                 guide_ = guide;\r
181                 guide_->set_color(c_);\r
182         }\r
183         \r
184         void set_color(color c)\r
185         {\r
186                 c_ = c;\r
187                 if(guide_)\r
188                         guide_->set_color(c_);\r
189         }\r
190 \r
191         color get_color() const { return c_; }\r
192         \r
193         void render(sf::RenderTarget& target)\r
194         {\r
195                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
196                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
197 \r
198                 if(!tick_data_.empty())\r
199                 {\r
200                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
201                         line_data_.push_back(std::make_pair(static_cast<float>(sum)/static_cast<float>(tick_data_.size()), tick_tag_));\r
202                         tick_data_.clear();\r
203                 }\r
204                 else if(!line_data_.empty())\r
205                 {\r
206                         line_data_.push_back(std::make_pair(line_data_.back().first, tick_tag_));\r
207                 }\r
208                 tick_tag_ = false;\r
209 \r
210                 if(guide_)\r
211                         target.Draw(*guide_);\r
212                 \r
213                 glBegin(GL_LINE_STRIP);\r
214                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);             \r
215                 for(size_t n = 0; n < line_data_.size(); ++n)           \r
216                         if(line_data_[n].first > -0.5)\r
217                                 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
218                 glEnd();\r
219                                 \r
220                 glEnable(GL_LINE_STIPPLE);\r
221                 glLineStipple(3, 0xAAAA);\r
222                 for(size_t n = 0; n < line_data_.size(); ++n)   \r
223                 {\r
224                         if(line_data_[n].second)\r
225                         {\r
226                                 glBegin(GL_LINE_STRIP);\r
227                                 glColor4f(c_.red, c_.green, c_.blue, c_.alpha);                                 \r
228                                         glVertex3f(x+n*dx, 0.0f, 0.0f);                         \r
229                                         glVertex3f(x+n*dx, 1.0f, 0.0f);         \r
230                                 glEnd();\r
231                         }\r
232                 }\r
233                 glDisable(GL_LINE_STIPPLE);\r
234         }\r
235 };\r
236 \r
237 struct graph::implementation : public drawable\r
238 {\r
239         std::map<std::string, diagnostics::line> lines_;\r
240         std::string name_;\r
241 \r
242         implementation(const std::string& name) \r
243                 : name_(name){}\r
244 \r
245         void update(const std::string& name, float value)\r
246         {\r
247                 context::begin_invoke([=]\r
248                 {\r
249                         lines_[name].update(value);\r
250                 });\r
251         }\r
252 \r
253         void set(const std::string& name, float value)\r
254         {\r
255                 context::begin_invoke([=]\r
256                 {\r
257                         lines_[name].set(value);\r
258                 });\r
259         }\r
260 \r
261         void tag(const std::string& name)\r
262         {\r
263                 context::begin_invoke([=]\r
264                 {\r
265                         lines_[name].tag();\r
266                 });\r
267         }\r
268 \r
269         void set_color(const std::string& name, color c)\r
270         {\r
271                 context::begin_invoke([=]\r
272                 {\r
273                         lines_[name].set_color(c);\r
274                 });\r
275         }\r
276         \r
277         void guide(const std::string& name, float value)\r
278         {\r
279                 context::begin_invoke([=]\r
280                 {\r
281                         lines_[name].guide(diagnostics::guide(value));\r
282                 });\r
283         }\r
284 \r
285 private:\r
286         void render(sf::RenderTarget& target)\r
287         {\r
288                 const size_t text_size = 15;\r
289                 const size_t text_margin = 2;\r
290                 const size_t text_offset = text_size+text_margin*2;\r
291 \r
292                 sf::String text(name_.c_str(), sf::Font::GetDefaultFont(), text_size);\r
293                 text.SetStyle(sf::String::Italic);\r
294                 text.Move(text_margin, text_margin);\r
295                 \r
296                 glPushMatrix();\r
297                         glScaled(1.0f/GetScale().x, 1.0f/GetScale().y, 1.0f);\r
298                         target.Draw(text);\r
299                         float x_offset = text.GetPosition().x + text.GetRect().Right + text_margin*4;\r
300                         for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
301                         {                                               \r
302                                 sf::String line_text(it->first, sf::Font::GetDefaultFont(), text_size);\r
303                                 line_text.SetPosition(x_offset, text_margin);\r
304                                 auto c = it->second.get_color();\r
305                                 line_text.SetColor(sf::Color(c.red*255.0f, c.green*255.0f, c.blue*255.0f, c.alpha*255.0f));\r
306                                 target.Draw(line_text);\r
307                                 x_offset = line_text.GetRect().Right + text_margin*2;\r
308                         }\r
309 \r
310                         glDisable(GL_TEXTURE_2D);\r
311                 glPopMatrix();\r
312 \r
313                 glBegin(GL_QUADS);\r
314                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
315                         glVertex2f(1.0f, 0.99f);\r
316                         glVertex2f(0.0f, 0.99f);\r
317                         glVertex2f(0.0f, 0.01f);        \r
318                         glVertex2f(1.0f, 0.01f);        \r
319                 glEnd();\r
320 \r
321                 glPushMatrix();\r
322                         glTranslated(0.0f, text_offset/GetScale().y, 1.0f);\r
323                         glScaled(1.0f, 1.0-text_offset/GetScale().y, 1.0f);\r
324                 \r
325                         target.Draw(diagnostics::guide(1.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
326                         target.Draw(diagnostics::guide(0.0f, color(1.0f, 1.0f, 1.0f, 0.6f)));\r
327 \r
328                         for(auto it = lines_.begin(); it != lines_.end(); ++it)         \r
329                                 target.Draw(it->second);\r
330                 \r
331                 glPopMatrix();\r
332         }\r
333 \r
334         implementation(implementation&);\r
335         implementation& operator=(implementation&);\r
336 };\r
337         \r
338 graph::graph(const std::string& name) : impl_(new implementation(name))\r
339 {\r
340         context::register_drawable(impl_);\r
341 }\r
342 \r
343 void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
344 void graph::set(const std::string& name, float value){impl_->set(name, value);}\r
345 void graph::tag(const std::string& name){impl_->tag(name);}\r
346 void graph::guide(const std::string& name, float value){impl_->guide(name, value);}\r
347 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
348 \r
349 safe_ptr<graph> create_graph(const std::string& name)\r
350 {\r
351         return safe_ptr<graph>(new graph(name));\r
352 }\r
353 \r
354 }}