2 * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "gtkmm2ext/utils.h"
21 #include "ardour/session.h"
23 #include "plugin_dspload_ui.h"
28 using namespace Gtkmm2ext;
31 PluginLoadStatsGui::PluginLoadStatsGui (boost::shared_ptr<ARDOUR::PluginInsert> insert)
33 , _lbl_min ("", ALIGN_RIGHT, ALIGN_CENTER)
34 , _lbl_max ("", ALIGN_RIGHT, ALIGN_CENTER)
35 , _lbl_avg ("", ALIGN_RIGHT, ALIGN_CENTER)
36 , _lbl_dev ("", ALIGN_RIGHT, ALIGN_CENTER)
37 , _reset_button (_("Reset"))
40 _reset_button.set_name ("generic button");
41 _reset_button.signal_clicked.connect (sigc::mem_fun (*this, &PluginLoadStatsGui::clear_stats));
42 _darea.signal_expose_event ().connect (sigc::mem_fun (*this, &PluginLoadStatsGui::draw_bar));
43 set_size_request_to_display_given_text (_lbl_dev, string_compose (_("%1 [ms]"), 99.123), 0, 0);
44 _darea.set_size_request (360, 32); // TODO max (320, 360 * UIConfiguration::instance().get_ui_scale ())
46 attach (*manage (new Gtk::Label (_("Minimum"), ALIGN_RIGHT, ALIGN_CENTER)),
47 0, 1, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 0);
48 attach (*manage (new Gtk::Label (_("Maximum"), ALIGN_RIGHT, ALIGN_CENTER)),
49 0, 1, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 0);
50 attach (*manage (new Gtk::Label (_("Average"), ALIGN_RIGHT, ALIGN_CENTER)),
51 0, 1, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 0);
52 attach (*manage (new Gtk::Label (_("Std.Dev"), ALIGN_RIGHT, ALIGN_CENTER)),
53 0, 1, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 0);
55 attach (_lbl_min, 1, 2, 0, 1, Gtk::FILL, Gtk::SHRINK, 2, 0);
56 attach (_lbl_max, 1, 2, 1, 2, Gtk::FILL, Gtk::SHRINK, 2, 0);
57 attach (_lbl_avg, 1, 2, 2, 3, Gtk::FILL, Gtk::SHRINK, 2, 0);
58 attach (_lbl_dev, 1, 2, 3, 4, Gtk::FILL, Gtk::SHRINK, 2, 0);
60 attach (*manage (new Gtk::VSeparator ()),
61 2, 3, 0, 4, Gtk::FILL, Gtk::FILL, 4, 0);
63 attach (_darea, 3, 4, 0, 4, Gtk::FILL|Gtk::EXPAND, Gtk::FILL, 4, 4);
65 attach (_reset_button, 4, 5, 2, 4, Gtk::FILL, Gtk::SHRINK);
69 PluginLoadStatsGui::start_updating () {
71 update_cpu_label_connection = Timers::second_connect (sigc::mem_fun(*this, &PluginLoadStatsGui::update_cpu_label));
75 PluginLoadStatsGui::stop_updating () {
77 update_cpu_label_connection.disconnect ();
82 PluginLoadStatsGui::update_cpu_label()
84 if (_insert->get_stats (_min, _max, _avg, _dev)) {
86 _lbl_min.set_text (string_compose (_("%1 [ms]"), rint (_min / 10.) / 100.));
87 _lbl_max.set_text (string_compose (_("%1 [ms]"), rint (_max / 10.) / 100.));
88 _lbl_avg.set_text (string_compose (_("%1 [ms]"), rint (_avg) / 1000.));
89 _lbl_dev.set_text (string_compose (_("%1 [ms]"), rint (_dev) / 1000.));
90 _lbl_dev.set_text (string_compose (_("%1 [ms]"), rint (_dev) / 1000.));
93 _lbl_min.set_text ("-");
94 _lbl_max.set_text ("-");
95 _lbl_avg.set_text ("-");
96 _lbl_dev.set_text ("-");
102 PluginLoadStatsGui::draw_bar (GdkEventExpose* ev)
104 Gtk::Allocation a = _darea.get_allocation ();
105 const int width = a.get_width ();
106 const int height = a.get_height ();
107 cairo_t* cr = gdk_cairo_create (_darea.get_window ()->gobj ());
108 cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
111 Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
112 Gdk::Color const fg = get_style ()->get_fg (STATE_NORMAL);
114 cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
115 cairo_rectangle (cr, 0, 0, width, height);
118 int border = (height / 7) | 1;
123 int y1 = (height - 3 * border) & ~1;
125 const int w = x1 - x0;
126 const int h = y1 - y0;
127 const double cycle_ms = 1000. * _insert->session().get_block_size() / (double)_insert->session().nominal_sample_rate();
129 const double base_mult = std::max (1.0, cycle_ms / 2.0);
130 const double log_base = log1p (base_mult);
133 # define DEFLECT(T) ( (T) * w * 8. / (9. * cycle_ms) )
135 # define DEFLECT(T) ( log1p((T) * base_mult / cycle_ms) * w * 8. / (9 * log_base) )
139 rounded_rectangle (cr, x0, y0, w, h, 7);
141 cairo_set_source_rgba (cr, .0, .0, .0, 1);
142 cairo_set_line_width (cr, 1);
143 cairo_stroke_preserve (cr);
146 cairo_pattern_t *pat = cairo_pattern_create_linear (x0, 0.0, w, 0.0);
147 cairo_pattern_add_color_stop_rgba (pat, 0, 0, 1, 0, .7);
148 cairo_pattern_add_color_stop_rgba (pat, 6. / 9., 0, 1, 0, .7);
149 cairo_pattern_add_color_stop_rgba (pat, 6.5 / 9., .8, .8, 0, .7);
150 cairo_pattern_add_color_stop_rgba (pat, 7.5 / 9., .8, .8, 0, .7);
151 cairo_pattern_add_color_stop_rgba (pat, 8. / 9., 1, 0, 0, .7);
152 cairo_set_source (cr, pat);
153 cairo_pattern_destroy (pat);
154 cairo_fill_preserve (cr);
156 cairo_set_source_rgba (cr, .4, .3, .1, .5);
157 cairo_fill_preserve (cr);
164 double xmin = DEFLECT(_min / 1000.);
165 double xmax = DEFLECT(_max / 1000.);
167 rounded_rectangle (cr, x0 + xmin, y0, xmax - xmin, h, 7);
168 cairo_set_source_rgba (cr, .8, .8, .9, .5);
174 Glib::RefPtr<Pango::Layout> layout;
175 layout = Pango::Layout::create (get_pango_context ());
177 cairo_set_line_width (cr, 1);
179 for (int i = 1; i < 9; ++i) {
180 int text_width, text_height;
182 double v = cycle_ms * i / 8.;
184 double v = (exp (i * log_base / 8) - 1) * cycle_ms / base_mult;
186 double decimal = v > 10 ? 10 : 100;
187 layout->set_text (string_compose ("%1", rint (decimal * v) / decimal));
188 layout->get_pixel_size (text_width, text_height);
190 const int dx = w * i / 9.; // == DEFLECT (v)
192 cairo_move_to (cr, x0 + dx - .5, y0);
193 cairo_line_to (cr, x0 + dx - .5, y1);
194 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
197 cairo_move_to (cr, x0 + dx - .5 * text_width, y1 + 1);
198 cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
199 pango_cairo_show_layout (cr, layout->gobj ());
203 int text_width, text_height;
204 layout->set_text ("0");
205 cairo_move_to (cr, x0 + 1, y1 + 1);
206 cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
207 pango_cairo_show_layout (cr, layout->gobj ());
209 layout->set_text (_("[ms]"));
210 layout->get_pixel_size (text_width, text_height);
211 cairo_move_to (cr, x0 + w - text_width - 1, y1 + 1);
212 pango_cairo_show_layout (cr, layout->gobj ());
216 double xavg = round (DEFLECT(_avg / 1000.));
217 double xd0 = DEFLECT((_avg - _dev) / 1000.);
218 double xd1 = DEFLECT((_avg + _dev) / 1000.);
220 cairo_move_to (cr, x0 + xavg - .5, y0 - 1);
221 cairo_rel_line_to (cr, -5, -5);
222 cairo_rel_line_to (cr, 10, 0);
223 cairo_close_path (cr);
224 cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
229 rounded_rectangle (cr, x0, y0, w, h, 7);
232 const double dashes[] = { 1, 2 };
233 cairo_set_dash (cr, dashes, 2, 0);
234 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
235 cairo_set_line_width (cr, 1);
236 cairo_move_to (cr, x0 + xavg - .5, y0 - .5);
237 cairo_line_to (cr, x0 + xavg - .5, y1 + .5);
238 cairo_set_source_rgba (cr, .0, .0, .0, 1.);
240 cairo_set_dash (cr, 0, 0, 0);
243 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
244 const double ym = .5 + floor ((double)(y0 + h / 2));
245 const int h4 = h / 4;
247 cairo_set_line_width (cr, 1);
248 cairo_set_source_rgba (cr, 0, 0, 0, 1.);
249 cairo_move_to (cr, floor (x0 + xd0), ym);
250 cairo_line_to (cr, ceil (x0 + xd1), ym);
253 cairo_move_to (cr, floor (x0 + xd0) - .5, ym - h4);
254 cairo_line_to (cr, floor (x0 + xd0) - .5, ym + h4);
256 cairo_move_to (cr, ceil (x0 + xd1) - .5, ym - h4);
257 cairo_line_to (cr, ceil (x0 + xd1) - .5, ym + h4);