]> git.sesse.net Git - casparcg/blob - common/diagnostics/graph.cpp
2.0.0.2: Added performance graphs.
[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(double dy, double y) = 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                 float dy = 1.0/static_cast<float>(std::max<int>(5, drawables_.size()));\r
50                 float y = 1.0-dy;\r
51                 for(auto it = drawables_.begin(); it != drawables_.end();)\r
52                 {\r
53                         auto drawable = it->lock();\r
54                         if(drawable)\r
55                         {\r
56                                 drawable->draw(dy, y);          \r
57                                 y -= dy;// + 0.01;\r
58                                 ++it;\r
59                         }\r
60                         else                    \r
61                                 it = drawables_.erase(it);                      \r
62                 }\r
63         }       \r
64         static context& get_instance()\r
65         {\r
66                 static context impl;\r
67                 return impl;\r
68         }\r
69 \r
70         context()\r
71         {\r
72                 executor_.start();\r
73                 executor_.begin_invoke([this]\r
74                 {\r
75                         window_.Create(sf::VideoMode(600, 1000), "CasparCG Diagnostics");\r
76                         window_.SetPosition(0, 0);\r
77                         window_.SetActive();\r
78                         glEnable(GL_BLEND);\r
79                         glEnable(GL_LINE_SMOOTH);\r
80                         glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);\r
81                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);\r
82                         tick();\r
83                 });\r
84         }\r
85 \r
86 public: \r
87                                 \r
88         template<typename Func>\r
89         static auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept\r
90         {       \r
91                 return get_instance().executor_.begin_invoke(std::forward<Func>(func)); \r
92         }\r
93 \r
94         static void register_drawable(const std::shared_ptr<drawable>& drawable)\r
95         {\r
96                 begin_invoke([=]\r
97                 {\r
98                         get_instance().drawables_.push_back(drawable);\r
99                 });\r
100         }\r
101 };\r
102 \r
103 class line\r
104 {\r
105         boost::circular_buffer<float> line_data_;\r
106         std::vector<float> tick_data_;\r
107         std::array<float, 3> color_;\r
108 public:\r
109         line(size_t res = 600)\r
110                 : line_data_(res)\r
111         {\r
112                 color(1.0f, 1.0f, 1.0f);\r
113         }\r
114         \r
115         void update(float value)\r
116         {\r
117                 tick_data_.push_back(value);\r
118         }\r
119         \r
120         void color(float r, float g, float b)\r
121         {\r
122                 color_[0] = r; color_[1] = g; color_[2] = b;\r
123         }\r
124         \r
125         void draw(double dy, double y)\r
126         {\r
127                 float dx = 1.0f/static_cast<float>(line_data_.capacity());\r
128                 float x = static_cast<float>(line_data_.capacity()-line_data_.size())*dx;\r
129 \r
130                 if(!tick_data_.empty())\r
131                 {\r
132                         float sum = std::accumulate(tick_data_.begin(), tick_data_.end(), 0.0) + std::numeric_limits<float>::min();\r
133                         line_data_.push_back(static_cast<float>(sum)/static_cast<float>(tick_data_.size()));\r
134                         tick_data_.clear();\r
135                 }\r
136                 else if(!line_data_.empty())\r
137                 {\r
138                         line_data_.push_back(line_data_.back());\r
139                 }\r
140                 \r
141                 glBegin(GL_LINE_STRIP);\r
142                 glColor4f(color_[0], color_[1], color_[2], 1.0f);                       \r
143                 for(size_t n = 0; n < line_data_.size(); ++n)                           \r
144                         glVertex3f((x+n*dx)*2.0f-1.0f, (y + dy * std::max(0.05f, std::min(0.95f, line_data_[n]*0.8f + 0.1f))) * 2.0f - 1.0f, 0.0f);             \r
145                 glEnd();\r
146         }\r
147 };\r
148         \r
149 class guide\r
150 {\r
151         std::array<float, 3> color_;\r
152         float value_;\r
153 public:\r
154         guide() : value_(0.0f)\r
155         {\r
156                 color_[0] = color_[1] = color_[2] = 0.0f;\r
157         }\r
158 \r
159         guide(float value, float r, float g, float b) : value_(value)\r
160         {\r
161                 color_[0] = r; color_[1] = g; color_[2] = b;\r
162         }\r
163                         \r
164         void draw(double dy, double y)\r
165         {               \r
166                 glEnable(GL_LINE_STIPPLE);\r
167                 glLineStipple(3, 0xAAAA);\r
168                 glBegin(GL_LINE_STRIP);\r
169                 glColor4f(color_[0], color_[1], color_[2], 1.0f);                               \r
170                         glVertex3f(0.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 0.0f);              \r
171                         glVertex3f(1.0f*2.0f-1.0f, (y + dy * (value_ * 0.8f + 0.1f)) * 2.0f - 1.0f, 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, 0.4f, 0.4f, 0.4f);\r
185                 guides_["min"] = diagnostics::guide(0.0f, 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 color(const std::string& name, float r, float g, float b)\r
197         {\r
198                 context::begin_invoke([=]\r
199                 {\r
200                         lines_[name].color(r, g, b);\r
201                 });\r
202         }\r
203         \r
204         void line(const std::string& name, float value, float r, float g, float b)\r
205         {\r
206                 context::begin_invoke([=]\r
207                 {\r
208                         guides_[name] = diagnostics::guide(value, r, g, b);\r
209                 });\r
210         }\r
211 \r
212 private:\r
213         void draw(double dy, double y)\r
214         {\r
215                 glBegin(GL_QUADS);\r
216                         glColor4f(1.0f, 1.0f, 1.0f, 0.2f);      \r
217                         glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);\r
218                         glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.99 )*2.0f-1.0f);\r
219                         glVertex2f(1.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);    \r
220                         glVertex2f(0.0f*2.0f-1.0f, (y + dy*0.01)*2.0f-1.0f);    \r
221                 glEnd();\r
222                 \r
223                 for(auto it = guides_.begin(); it != guides_.end(); ++it)\r
224                         it->second.draw(dy, y);\r
225 \r
226                 for(auto it = lines_.begin(); it != lines_.end(); ++it)\r
227                         it->second.draw(dy, y);\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::color(const std::string& name, float r, float g, float b){impl_->color(name, r, g, b);}\r
240 void graph::line(const std::string& name, float value, float r, float g, float b){impl_->line(name, value, r, g, b);}\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 }}