fix mistaken "do not roll" conclusion in TransportFSM::compute_should_roll()
[ardour.git] / gtk2_ardour / plugin_dspload_ui.cc
1 /*
2  * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "gtkmm2ext/utils.h"
20
21 #include "ardour/session.h"
22
23 #include "plugin_dspload_ui.h"
24 #include "timers.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace Gtkmm2ext;
29 using namespace Gtk;
30
31 PluginLoadStatsGui::PluginLoadStatsGui (boost::shared_ptr<ARDOUR::PluginInsert> insert)
32         : _insert (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"))
38         , _valid (false)
39 {
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 ())
45
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);
54
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);
59
60         attach (*manage (new Gtk::VSeparator ()),
61                         2, 3, 0, 4, Gtk::FILL, Gtk::FILL, 4, 0);
62
63         attach (_darea, 3, 4, 0, 4, Gtk::FILL|Gtk::EXPAND, Gtk::FILL, 4, 4);
64
65         attach (_reset_button, 4, 5, 2, 4, Gtk::FILL, Gtk::SHRINK);
66 }
67
68 void
69 PluginLoadStatsGui::start_updating () {
70         update_cpu_label ();
71         update_cpu_label_connection = Timers::second_connect (sigc::mem_fun(*this, &PluginLoadStatsGui::update_cpu_label));
72 }
73
74 void
75 PluginLoadStatsGui::stop_updating () {
76         _valid = false;
77         update_cpu_label_connection.disconnect ();
78 }
79
80
81 void
82 PluginLoadStatsGui::update_cpu_label()
83 {
84         if (_insert->get_stats (_min, _max, _avg, _dev)) {
85                 _valid = true;
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.));
91         } else {
92                 _valid = false;
93                 _lbl_min.set_text ("-");
94                 _lbl_max.set_text ("-");
95                 _lbl_avg.set_text ("-");
96                 _lbl_dev.set_text ("-");
97         }
98         _darea.queue_draw ();
99 }
100
101 bool
102 PluginLoadStatsGui::draw_bar (GdkEventExpose* ev)
103 {
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);
109         cairo_clip (cr);
110
111         Gdk::Color const bg = get_style ()->get_bg (STATE_NORMAL);
112         Gdk::Color const fg = get_style ()->get_fg (STATE_NORMAL);
113
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);
116         cairo_fill (cr);
117
118         int border = (height / 7) | 1;
119
120         int x0 = 2;
121         int y0 = border;
122         int x1 = width - 2;
123         int y1 = (height - 3 * border) & ~1;
124
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();
128
129         const double base_mult = std::max (1.0, cycle_ms / 2.0);
130         const double log_base = log1p (base_mult);
131
132 #if 0 // Linear
133 # define DEFLECT(T) ( (T) * w * 8. / (9. * cycle_ms) )
134 #else
135 # define DEFLECT(T) ( log1p((T) * base_mult / cycle_ms) * w * 8. / (9 * log_base) )
136 #endif
137
138         cairo_save (cr);
139         rounded_rectangle (cr, x0, y0, w, h, 7);
140
141         cairo_set_source_rgba (cr, .0, .0, .0, 1);
142         cairo_set_line_width (cr, 1);
143         cairo_stroke_preserve (cr);
144
145         /* TODO statically cache these patterns
146          * like Meter::generate_meter_background
147          */
148         if (_valid) {
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);
158                 cairo_clip (cr);
159
160                 double xmin = DEFLECT(_min / 1000.);
161                 double xmax = DEFLECT(_max / 1000.);
162
163                 rounded_rectangle (cr, x0 + xmin, y0, xmax - xmin, h, 7);
164
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);
172                 cairo_fill (cr);
173                 cairo_pattern_destroy (pat);
174
175         } else {
176                 cairo_set_source_rgba (cr, .4, .3, .1, .5);
177                 cairo_fill (cr);
178         }
179
180         cairo_restore (cr);
181
182         Glib::RefPtr<Pango::Layout> layout;
183         layout = Pango::Layout::create (get_pango_context ());
184
185         cairo_set_line_width (cr, 1);
186
187         for (int i = 1; i < 9; ++i) {
188                 int text_width, text_height;
189 #if 0 // Linear
190                 double v = cycle_ms * i / 8.;
191 #else
192                 double v = (exp (i * log_base / 8) - 1) * cycle_ms / base_mult;
193 #endif
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);
197
198                 const int dx = w * i / 9.; // == DEFLECT (v)
199
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.);
203                 cairo_stroke (cr);
204
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 ());
208         }
209
210         {
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 ());
216
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 ());
221         }
222
223         if (_valid) {
224                 double xavg = round (DEFLECT(_avg / 1000.));
225                 double xd0 = DEFLECT((_avg - _dev) / 1000.);
226                 double xd1 = DEFLECT((_avg + _dev) / 1000.);
227
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 ());
233                 cairo_fill (cr);
234
235                 cairo_save (cr);
236
237                 rounded_rectangle (cr, x0, y0, w, h, 7);
238                 cairo_clip (cr);
239
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.);
247                 cairo_stroke (cr);
248                 cairo_set_dash (cr, 0, 0, 0);
249
250                 if (xd1 - xd0 > 2) {
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;
254
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);
259                         cairo_stroke (cr);
260
261                         cairo_move_to (cr, floor (x0 + xd0) - .5, ym - h4);
262                         cairo_line_to (cr, floor (x0 + xd0) - .5, ym + h4);
263                         cairo_stroke (cr);
264                         cairo_move_to (cr, ceil (x0 + xd1) - .5, ym - h4);
265                         cairo_line_to (cr, ceil (x0 + xd1) - .5, ym + h4);
266                         cairo_stroke (cr);
267                 }
268                 cairo_restore (cr);
269         }
270 #undef DEFLECT
271
272         cairo_destroy (cr);
273         return true;
274 }
275
276
277