3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
22 #include "profilergraph.h"
23 #include "util/string.h"
25 void ProfilerGraph::put(const Profiler::GraphValues &values)
27 m_log.emplace_back(values);
29 while (m_log.size() > m_log_max_size)
30 m_log.erase(m_log.begin());
33 void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
34 gui::IGUIFont *font) const
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;
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);
46 if (j == m_meta.end()) {
47 m_meta[id] = Meta(value);
51 if (value < j->second.min)
52 j->second.min = value;
54 if (value > j->second.max)
55 j->second.max = value;
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);
69 for (auto &i : m_meta) {
70 Meta &meta = i.second;
71 video::SColor color(255, 200, 200, 200);
73 if (next_color_i < usable_colors_count)
74 color = usable_colors[next_color_i++];
80 s32 textx = x_left + m_log_max_size + 15;
81 s32 textx2 = textx + 200 - 15;
84 for (const auto &p : m_meta) {
85 const std::string &id = p.first;
86 const Meta &meta = p.second;
88 s32 y = y_bottom - meta_i * 50;
89 float show_min = meta.min;
90 float show_max = meta.max;
92 if (show_min >= -0.0001 && show_max >= -0.0001) {
93 if (show_min <= show_max * 0.5)
99 porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
100 font->draw(utf8_to_wide(buf).c_str(),
101 core::rect<s32>(textx, y - graphh, textx2,
104 porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
105 font->draw(utf8_to_wide(buf).c_str(),
106 core::rect<s32>(textx, y - texth, textx2, y), meta.color);
107 font->draw(utf8_to_wide(id).c_str(),
108 core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
109 y - graphh / 2 + texth / 2),
112 s32 graph1h = graphh;
113 bool relativegraph = (show_min != 0 && show_min != show_max);
114 float lastscaledvalue = 0.0;
115 bool lastscaledvalue_exists = false;
117 for (const Piece &piece : m_log) {
119 bool value_exists = false;
120 Profiler::GraphValues::const_iterator k = piece.values.find(id);
122 if (k != piece.values.end()) {
129 lastscaledvalue_exists = false;
133 float scaledvalue = 1.0;
135 if (show_max != show_min)
136 scaledvalue = (value - show_min) / (show_max - show_min);
138 if (scaledvalue == 1.0 && value == 0) {
140 lastscaledvalue_exists = false;
145 if (lastscaledvalue_exists) {
146 s32 ivalue1 = lastscaledvalue * graph1h;
147 s32 ivalue2 = scaledvalue * graph1h;
149 v2s32(x - 1, graph1y - ivalue1),
150 v2s32(x, graph1y - ivalue2),
154 lastscaledvalue = scaledvalue;
155 lastscaledvalue_exists = true;
157 s32 ivalue = scaledvalue * graph1h;
158 driver->draw2DLine(v2s32(x, graph1y),
159 v2s32(x, graph1y - ivalue), meta.color);