]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[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/circular_buffer.hpp>\r
14 #include <boost/range/algorithm_ext/erase.hpp>\r
15 \r
16 #include <numeric>\r
17 #include <map>\r
18 #include <array>\r
19 \r
20 namespace caspar { namespace diagnostics {\r
21 \r
22 struct drawable\r
23 {\r
24         virtual ~drawable(){}\r
25         virtual void draw() = 0;\r
26 };\r
27 \r
28 class context\r
29 {       \r
30         timer timer_;\r
31         executor executor_;\r
32         sf::RenderWindow window_;\r
33         \r
34         std::list<std::weak_ptr<drawable>> drawables_;\r
35                 \r
36         void tick()\r
37         {\r
38                 sf::Event e;\r
39                 while(window_.GetEvent(e)){}\r
40                 window_.Clear();\r
41                 render();\r
42                 window_.Display();\r
43                 timer_.tick(1.0/50.0);\r
44                 executor_.begin_invoke([this]{tick();});\r
45         }\r
46 \r
47         void render()\r
48         {\r
49                 glLoadIdentity();\r
50                 glTranslated(-1.0f, -1.0f, 0.0f);\r
51                 glScaled(2.0f, 2.0f, 1.0f);\r
52 \r
53                 float dy = 1.0/static_cast<float>(std::max<int>(5, drawables_.size()));\r
54 \r
55                 glTranslated(0.0f, (1.0-dy), 0.0f);\r
56                 for(auto it = drawables_.begin(); it != drawables_.end();)\r
57                 {\r
58                         auto drawable = it->lock();\r
59                         if(drawable)\r
60                         {\r
61                                 glPushMatrix();\r
62                                         glScaled(1.0f, dy, 1.0f);\r
63                                         drawable->draw();\r
64                                 glPopMatrix();\r
65                                 glTranslated(0.0f, -dy, 0.0f);\r
66                                 ++it;\r
67                         }\r
68                         else                    \r
69                                 it = drawables_.erase(it);                      \r
70                 }               \r
71         }       \r
72 \r
73         static context& get_instance()\r
74         {\r
75                 static context impl;\r
76                 return impl;\r
77         }\r
78 \r
79         context()\r
80         {\r
81                 executor_.start();\r
82                 executor_.begin_invoke([this]\r
83                 {\r
84                         window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
85                         window_.SetPosition(0, 0);\r
86                         window_.SetActive();\r
87                         glEnable(GL_BLEND);\r
88                         glEnable(GL_LINE_SMOOTH);\r
89                         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
90                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
91                         tick();\r
92                 });\r
93         }\r
94 \r
95 public: \r
96                                 \r
97         template<typename Func>\r
98         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
99         {       \r
100                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
101         }\r
102 \r
103         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
104         {\r
105                 begin_invoke([=]\r
106                 {\r
107                         get_instance().drawables_.push_back(drawable);\r
108                 });\r
109         }\r
110 };\r
111 \r
112 class line\r
113 {\r
114         boost::circular_buffer<float> line_data_;\r
115         std::vector<float> tick_data_;\r
116         color c_;\r
117 public:\r
118         line(size_t res = 600)\r
119                 : line_data_(res)\r
120                 , c_(1.0f, 1.0f, 1.0f){}\r
121         \r
122         void update(float value)\r
123         {\r
124                 tick_data_.push_back(value);\r
125         }\r
126         \r
127         void set_color(color c){c_ = c;}\r
128         \r
129         void draw()\r
130         {\r
131                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
132                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
133 \r
134                 if(!tick_data_.empty())\r
135                 {\r
136                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
137                         line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));\r
138                         tick_data_.clear();\r
139                 }\r
140                 else if(!line_data_.empty())\r
141                 {\r
142                         line_data_.push_back(line_data_.back());\r
143                 }\r
144                 \r
145                 glBegin(GL_LINE_STRIP);\r
146                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);                     \r
147                 for(size_t n = 0; n < line_data_.size(); ++n)                           \r
148                         glVertex3f(x+n*dx, std::max(0.05f, std::min(0.95f, line_data_[n]*0.8f + 0.1f)), 0.0f);          \r
149                 glEnd();\r
150         }\r
151 };\r
152         \r
153 class guide\r
154 {\r
155         color c_;\r
156         float value_;\r
157 public:\r
158         guide() : value_(0.0f){}\r
159 \r
160         guide(float value, color c) \r
161                 : value_(value)\r
162                 , c_(c){}\r
163                         \r
164         void draw()\r
165         {               \r
166                 glEnable(GL_LINE_STIPPLE);\r
167                 glLineStipple(3, 0xAAAA);\r
168                 glBegin(GL_LINE_STRIP);\r
169                 glColor4f(c_.red, c_.green, c_.blue, 1.0f);                             \r
170                         glVertex3f(0.0f, value_ * 0.8f + 0.1f, 0.0f);           \r
171                         glVertex3f(1.0f, value_ * 0.8f + 0.1f, 0.0f);   \r
172                 glEnd();\r
173                 glDisable(GL_LINE_STIPPLE);\r
174         }\r
175 };\r
176 \r
177 struct graph::implementation : public drawable\r
178 {\r
179         std::map<std::string, diagnostics::line> lines_;\r
180         std::map<std::string, diagnostics::guide> guides_;\r
181 \r
182         implementation(const std::string&)\r
183         {\r
184                 guides_["max"] = diagnostics::guide(1.0f, color(0.4f, 0.4f, 0.4f));\r
185                 guides_["min"] = diagnostics::guide(0.0f, color(0.4f, 0.4f, 0.4f));\r
186         }\r
187 \r
188         void update(const std::string& name, float value)\r
189         {\r
190                 context::begin_invoke([=]\r
191                 {\r
192                         lines_[name].update(value);\r
193                 });\r
194         }\r
195 \r
196         void set_color(const std::string& name, color c)\r
197         {\r
198                 context::begin_invoke([=]\r
199                 {\r
200                         lines_[name].set_color(c);\r
201                 });\r
202         }\r
203         \r
204         void add_guide(const std::string& name, float value, color c)\r
205         {\r
206                 context::begin_invoke([=]\r
207                 {\r
208                         guides_[name] = diagnostics::guide(value, c);\r
209                 });\r
210         }\r
211 \r
212 private:\r
213         void draw()\r
214         {\r
215                 glBegin(GL_QUADS);\r
216                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
217                         glVertex2f(1.0f, 0.99f);\r
218                         glVertex2f(0.0f, 0.99f);\r
219                         glVertex2f(0.0f, 0.01f);        \r
220                         glVertex2f(1.0f, 0.01f);        \r
221                 glEnd();\r
222                 \r
223                 for(auto it = guides_.begin(); it != guides_.end(); ++it)\r
224                         it->second.draw();\r
225 \r
226                 for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
227                         it->second.draw();\r
228         }\r
229 \r
230         implementation(implementation&);\r
231         implementation& operator=(implementation&);\r
232 };\r
233         \r
234 graph::graph(const std::string& name) : impl_(new implementation(name))\r
235 {\r
236         context::register_drawable(impl_);\r
237 }\r
238 void graph::update(const std::string& name, float value){impl_->update(name, value);}\r
239 void graph::set_color(const std::string& name, color c){impl_->set_color(name, c);}\r
240 void graph::add_guide(const std::string& name, float value, color c){impl_->add_guide(name, value, c);}\r
241 \r
242 safe_ptr<graph> create_graph(const std::string& name)\r
243 {\r
244         return safe_ptr<graph>(new graph(name));\r
245 }\r
246 \r
247 }}