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.
21 #include "profilergraph.h"
22 #include "util/string.h"
24 void ProfilerGraph::put(const Profiler::GraphValues &values)
26 m_log.emplace_back(values);
28 while (m_log.size() > m_log_max_size)
29 m_log.erase(m_log.begin());
32 void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
33 gui::IGUIFont *font) const
35 // Do *not* use UNORDERED_MAP here as the order needs
36 // to be the same for each call to prevent flickering
37 std::map<std::string, Meta> m_meta;
39 for (const Piece &piece : m_log) {
40 for (const auto &i : piece.values) {
41 const std::string &id = i.first;
42 const float &value = i.second;
43 std::map<std::string, Meta>::iterator j = m_meta.find(id);
45 if (j == m_meta.end()) {
46 m_meta[id] = Meta(value);
50 if (value < j->second.min)
51 j->second.min = value;
53 if (value > j->second.max)
54 j->second.max = value;
59 static const video::SColor usable_colors[] = {video::SColor(255, 255, 100, 100),
60 video::SColor(255, 90, 225, 90),
61 video::SColor(255, 100, 100, 255),
62 video::SColor(255, 255, 150, 50),
63 video::SColor(255, 220, 220, 100)};
64 static const u32 usable_colors_count =
65 sizeof(usable_colors) / sizeof(*usable_colors);
68 for (auto &i : m_meta) {
69 Meta &meta = i.second;
70 video::SColor color(255, 200, 200, 200);
72 if (next_color_i < usable_colors_count)
73 color = usable_colors[next_color_i++];
79 s32 textx = x_left + m_log_max_size + 15;
80 s32 textx2 = textx + 200 - 15;
83 for (const auto &p : m_meta) {
84 const std::string &id = p.first;
85 const Meta &meta = p.second;
87 s32 y = y_bottom - meta_i * 50;
88 float show_min = meta.min;
89 float show_max = meta.max;
91 if (show_min >= -0.0001 && show_max >= -0.0001) {
92 if (show_min <= show_max * 0.5)
98 snprintf(buf, 10, "%.3g", show_max);
99 font->draw(utf8_to_wide(buf).c_str(),
100 core::rect<s32>(textx, y - graphh, textx2,
103 snprintf(buf, 10, "%.3g", show_min);
104 font->draw(utf8_to_wide(buf).c_str(),
105 core::rect<s32>(textx, y - texth, textx2, y), meta.color);
106 font->draw(utf8_to_wide(id).c_str(),
107 core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
108 y - graphh / 2 + texth / 2),
111 s32 graph1h = graphh;
112 bool relativegraph = (show_min != 0 && show_min != show_max);
113 float lastscaledvalue = 0.0;
114 bool lastscaledvalue_exists = false;
116 for (const Piece &piece : m_log) {
118 bool value_exists = false;
119 Profiler::GraphValues::const_iterator k = piece.values.find(id);
121 if (k != piece.values.end()) {
128 lastscaledvalue_exists = false;
132 float scaledvalue = 1.0;
134 if (show_max != show_min)
135 scaledvalue = (value - show_min) / (show_max - show_min);
137 if (scaledvalue == 1.0 && value == 0) {
139 lastscaledvalue_exists = false;
144 if (lastscaledvalue_exists) {
145 s32 ivalue1 = lastscaledvalue * graph1h;
146 s32 ivalue2 = scaledvalue * graph1h;
148 v2s32(x - 1, graph1y - ivalue1),
149 v2s32(x, graph1y - ivalue2),
153 lastscaledvalue = scaledvalue;
154 lastscaledvalue_exists = true;
156 s32 ivalue = scaledvalue * graph1h;
157 driver->draw2DLine(v2s32(x, graph1y),
158 v2s32(x, graph1y - ivalue), meta.color);