2 * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 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);
145 /* TODO statically cache these patterns
146 * like Meter::generate_meter_background
149 cairo_pattern_t *pat = cairo_pattern_create_linear (x0, 0.0, w, 0.0);
150 cairo_pattern_add_color_stop_rgba (pat, 0, 0, 1, 0, .2);
151 cairo_pattern_add_color_stop_rgba (pat, 6. / 9., 0, 1, 0, .2);
152 cairo_pattern_add_color_stop_rgba (pat, 6.5 / 9., .8, .8, 0, .2);
153 cairo_pattern_add_color_stop_rgba (pat, 7.5 / 9., .8, .8, 0, .2);
154 cairo_pattern_add_color_stop_rgba (pat, 8. / 9., 1, 0, 0, .2);
155 cairo_set_source (cr, pat);
156 cairo_fill_preserve (cr);
157 cairo_pattern_destroy (pat);
160 double xmin = DEFLECT(_min / 1000.);
161 double xmax = DEFLECT(_max / 1000.);
163 rounded_rectangle (cr, x0 + xmin, y0, xmax - xmin, h, 7);
165 pat = cairo_pattern_create_linear (x0, 0.0, w, 0.0);
166 cairo_pattern_add_color_stop_rgba (pat, 0, 0, 1, 0, .8);
167 cairo_pattern_add_color_stop_rgba (pat, 6. / 9., 0, 1, 0, .8);
168 cairo_pattern_add_color_stop_rgba (pat, 6.5 / 9., .8, .8, 0, .8);
169 cairo_pattern_add_color_stop_rgba (pat, 7.5 / 9., .8, .8, 0, .8);
170 cairo_pattern_add_color_stop_rgba (pat, 8. / 9., 1, 0, 0, .8);
171 cairo_set_source (cr, pat);
173 cairo_pattern_destroy (pat);
176 cairo_set_source_rgba (cr, .4, .3, .1, .5);
182 Glib::RefPtr<Pango::Layout> layout;
183 layout = Pango::Layout::create (get_pango_context ());
185 cairo_set_line_width (cr, 1);
187 for (int i = 1; i < 9; ++i) {
188 int text_width, text_height;
190 double v = cycle_ms * i / 8.;
192 double v = (exp (i * log_base / 8) - 1) * cycle_ms / base_mult;
194 double decimal = v > 10 ? 10 : 100;
195 layout->set_text (string_compose ("%1", rint (decimal * v) / decimal));
196 layout->get_pixel_size (text_width, text_height);
198 const int dx = w * i / 9.; // == DEFLECT (v)
200 cairo_move_to (cr, x0 + dx - .5, y0);
201 cairo_line_to (cr, x0 + dx - .5, y1);
202 cairo_set_source_rgba (cr, 1., 1., 1., 1.);
205 cairo_move_to (cr, x0 + dx - .5 * text_width, 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 ());
211 int text_width, text_height;
212 layout->set_text ("0");
213 cairo_move_to (cr, x0 + 1, y1 + 1);
214 cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
215 pango_cairo_show_layout (cr, layout->gobj ());
217 layout->set_text (_("[ms]"));
218 layout->get_pixel_size (text_width, text_height);
219 cairo_move_to (cr, x0 + w - text_width - 1, y1 + 1);
220 pango_cairo_show_layout (cr, layout->gobj ());
224 double xavg = round (DEFLECT(_avg / 1000.));
225 double xd0 = DEFLECT((_avg - _dev) / 1000.);
226 double xd1 = DEFLECT((_avg + _dev) / 1000.);
228 cairo_move_to (cr, x0 + xavg - .5, y0 - 1);
229 cairo_rel_line_to (cr, -5, -5);
230 cairo_rel_line_to (cr, 10, 0);
231 cairo_close_path (cr);
232 cairo_set_source_rgb (cr, fg.get_red_p (), fg.get_green_p (), fg.get_blue_p ());
237 rounded_rectangle (cr, x0, y0, w, h, 7);
240 const double dashes[] = { 1, 2 };
241 cairo_set_dash (cr, dashes, 2, 0);
242 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
243 cairo_set_line_width (cr, 1);
244 cairo_move_to (cr, x0 + xavg - .5, y0 - .5);
245 cairo_line_to (cr, x0 + xavg - .5, y1 + .5);
246 cairo_set_source_rgba (cr, .0, .0, .0, 1.);
248 cairo_set_dash (cr, 0, 0, 0);
251 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
252 const double ym = .5 + floor ((double)(y0 + h / 2));
253 const int h4 = h / 4;
255 cairo_set_line_width (cr, 1);
256 cairo_set_source_rgba (cr, 0, 0, 0, 1.);
257 cairo_move_to (cr, floor (x0 + xd0), ym);
258 cairo_line_to (cr, ceil (x0 + xd1), ym);
261 cairo_move_to (cr, floor (x0 + xd0) - .5, ym - h4);
262 cairo_line_to (cr, floor (x0 + xd0) - .5, ym + h4);
264 cairo_move_to (cr, ceil (x0 + xd1) - .5, ym - h4);
265 cairo_line_to (cr, ceil (x0 + xd1) - .5, ym + h4);