]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/profilergraph.cpp
Fix hash implementation for SerializedBlockCache
[dragonfireclient.git] / src / gui / profilergraph.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "porting.h"
22 #include "profilergraph.h"
23 #include "util/string.h"
24
25 void ProfilerGraph::put(const Profiler::GraphValues &values)
26 {
27         m_log.emplace_back(values);
28
29         while (m_log.size() > m_log_max_size)
30                 m_log.erase(m_log.begin());
31 }
32
33 void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
34                 gui::IGUIFont *font) const
35 {
36         // Do *not* use UNORDERED_MAP here as the order needs
37         // to be the same for each call to prevent flickering
38         std::map<std::string, Meta> m_meta;
39
40         for (const Piece &piece : m_log) {
41                 for (const auto &i : piece.values) {
42                         const std::string &id = i.first;
43                         const float &value = i.second;
44                         std::map<std::string, Meta>::iterator j = m_meta.find(id);
45
46                         if (j == m_meta.end()) {
47                                 m_meta[id] = Meta(value);
48                                 continue;
49                         }
50
51                         if (value < j->second.min)
52                                 j->second.min = value;
53
54                         if (value > j->second.max)
55                                 j->second.max = value;
56                 }
57         }
58
59         // Assign colors
60         static const video::SColor usable_colors[] = {video::SColor(255, 255, 100, 100),
61                         video::SColor(255, 90, 225, 90),
62                         video::SColor(255, 100, 100, 255),
63                         video::SColor(255, 255, 150, 50),
64                         video::SColor(255, 220, 220, 100)};
65         static const u32 usable_colors_count =
66                         sizeof(usable_colors) / sizeof(*usable_colors);
67         u32 next_color_i = 0;
68
69         for (auto &i : m_meta) {
70                 Meta &meta = i.second;
71                 video::SColor color(255, 200, 200, 200);
72
73                 if (next_color_i < usable_colors_count)
74                         color = usable_colors[next_color_i++];
75
76                 meta.color = color;
77         }
78
79         s32 graphh = 50;
80         s32 textx = x_left + m_log_max_size + 15;
81         s32 textx2 = textx + 200 - 15;
82         s32 meta_i = 0;
83
84         for (const auto &p : m_meta) {
85                 const std::string &id = p.first;
86                 const Meta &meta = p.second;
87                 s32 x = x_left;
88                 s32 y = y_bottom - meta_i * 50;
89                 float show_min = meta.min;
90                 float show_max = meta.max;
91
92                 if (show_min >= -0.0001 && show_max >= -0.0001) {
93                         if (show_min <= show_max * 0.5)
94                                 show_min = 0;
95                 }
96
97                 const s32 texth = 15;
98                 char buf[10];
99                 if (floorf(show_max) == show_max)
100                         porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_max);
101                 else
102                         porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
103                 font->draw(utf8_to_wide(buf).c_str(),
104                                 core::rect<s32>(textx, y - graphh, textx2,
105                                                 y - graphh + texth),
106                                 meta.color);
107
108                 if (floorf(show_min) == show_min)
109                         porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_min);
110                 else
111                         porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
112                 font->draw(utf8_to_wide(buf).c_str(),
113                                 core::rect<s32>(textx, y - texth, textx2, y), meta.color);
114
115                 font->draw(utf8_to_wide(id).c_str(),
116                                 core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
117                                                 y - graphh / 2 + texth / 2),
118                                 meta.color);
119
120                 s32 graph1y = y;
121                 s32 graph1h = graphh;
122                 bool relativegraph = (show_min != 0 && show_min != show_max);
123                 float lastscaledvalue = 0.0;
124                 bool lastscaledvalue_exists = false;
125
126                 for (const Piece &piece : m_log) {
127                         float value = 0;
128                         bool value_exists = false;
129                         Profiler::GraphValues::const_iterator k = piece.values.find(id);
130
131                         if (k != piece.values.end()) {
132                                 value = k->second;
133                                 value_exists = true;
134                         }
135
136                         if (!value_exists) {
137                                 x++;
138                                 lastscaledvalue_exists = false;
139                                 continue;
140                         }
141
142                         float scaledvalue = 1.0;
143
144                         if (show_max != show_min)
145                                 scaledvalue = (value - show_min) / (show_max - show_min);
146
147                         if (scaledvalue == 1.0 && value == 0) {
148                                 x++;
149                                 lastscaledvalue_exists = false;
150                                 continue;
151                         }
152
153                         if (relativegraph) {
154                                 if (lastscaledvalue_exists) {
155                                         s32 ivalue1 = lastscaledvalue * graph1h;
156                                         s32 ivalue2 = scaledvalue * graph1h;
157                                         driver->draw2DLine(
158                                                         v2s32(x - 1, graph1y - ivalue1),
159                                                         v2s32(x, graph1y - ivalue2),
160                                                         meta.color);
161                                 }
162
163                                 lastscaledvalue = scaledvalue;
164                                 lastscaledvalue_exists = true;
165                         } else {
166                                 s32 ivalue = scaledvalue * graph1h;
167                                 driver->draw2DLine(v2s32(x, graph1y),
168                                                 v2s32(x, graph1y - ivalue), meta.color);
169                         }
170
171                         x++;
172                 }
173
174                 meta_i++;
175         }
176 }