bf033b7b32c11efe742f9f53fc103d9e9c805e2d
[ardour.git] / gtk2_ardour / dsp_load_indicator.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  *
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.
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
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.
17  */
18
19 #include "gtkmm2ext/utils.h"
20 #include "widgets/tooltips.h"
21
22 #include "ardour_ui.h"
23 #include "dsp_load_indicator.h"
24 #include "ui_config.h"
25
26 #include "pbd/i18n.h"
27
28 #define PADDING 3
29
30 DspLoadIndicator::DspLoadIndicator ()
31         : _dsp_load (0)
32         , _xrun_count (0)
33 {
34         _layout = Pango::Layout::create (get_pango_context ());
35         _layout->set_text ("99.9%");
36 }
37
38 DspLoadIndicator::~DspLoadIndicator ()
39 {
40 }
41
42 void
43 DspLoadIndicator::on_size_request (Gtk::Requisition* req)
44 {
45         req->width = req->height = 0;
46         CairoWidget::on_size_request (req);
47
48         int w, h;
49         _layout->get_pixel_size (w, h);
50
51         req->width = std::max (req->width, std::max (12, h + PADDING));
52         req->height = std::max (req->height, 20 /*std::max (20, w + PADDING) */);
53 }
54
55 void
56 DspLoadIndicator::set_xrun_count (const unsigned int xruns)
57 {
58         if (xruns == _xrun_count) {
59                 return;
60         }
61         _xrun_count = xruns;
62         queue_draw ();
63         update_tooltip ();
64 }
65
66 void
67 DspLoadIndicator::set_dsp_load (const double load)
68 {
69         if (load == _dsp_load) {
70                 return;
71         }
72         _dsp_load = load;
73
74         char buf[64];
75         snprintf (buf, sizeof (buf), "%.1f%%", _dsp_load);
76         _layout->set_text (buf);
77
78         queue_draw ();
79         update_tooltip ();
80 }
81
82 void
83 DspLoadIndicator::update_tooltip ()
84 {
85         char buf[64];
86         if (_xrun_count == UINT_MAX) {
87                 snprintf (buf, sizeof (buf), _("DSP: %.1f%% X: ?"), _dsp_load);
88         } else if (_xrun_count > 9999) {
89                 snprintf (buf, sizeof (buf), _("DSP: %.1f%% X: >10k"), _dsp_load);
90         } else {
91                 snprintf (buf, sizeof (buf), _("DSP: %.1f%% X: %u"), _dsp_load, _xrun_count);
92         }
93         ArdourWidgets::set_tooltip (*this, buf);
94 }
95
96 void
97 DspLoadIndicator::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
98 {
99         cairo_t* cr = ctx->cobj ();
100         Gtkmm2ext::Color base = UIConfiguration::instance ().color ("ruler base");
101         Gtkmm2ext::Color text = UIConfiguration::instance ().color ("ruler text");
102
103         const int width = get_width ();
104         const int height = get_height ();
105
106         Gtkmm2ext::rounded_rectangle (cr, 0, 0, width, height, PADDING + 1);
107         Gtkmm2ext::set_source_rgba (cr, base);
108         cairo_fill (cr);
109
110         if (_xrun_count > 0) {
111                 Gtkmm2ext::rounded_rectangle (cr, 1, 1, width - 2, height - 2, PADDING + 1);
112                 cairo_set_source_rgba (cr, 0.5, 0, 0, 1.0);
113                 cairo_fill (cr);
114         }
115
116         Gtkmm2ext::rounded_rectangle (cr, PADDING, PADDING, width - PADDING - PADDING, height - PADDING - PADDING, PADDING + 1);
117         cairo_clip (cr);
118
119         int bh = (height - PADDING - PADDING) * _dsp_load / 100.f;
120         cairo_rectangle (cr, PADDING, height - PADDING - bh, width - PADDING, bh);
121
122         if (_dsp_load > 90) {
123                 cairo_set_source_rgba (cr, .9, 0, 0, 1.0);
124         } else if (_dsp_load > 80) {
125                 cairo_set_source_rgba (cr, .7, .6, 0, 1.0);
126         } else {
127                 cairo_set_source_rgba (cr, 0, .5, 0, 1.0);
128         }
129         cairo_fill (cr);
130
131         int w, h;
132         _layout->get_pixel_size (w, h);
133
134         cairo_save (cr);
135         cairo_new_path (cr);
136         cairo_translate (cr, width * .5, height * .5);
137         cairo_rotate (cr, M_PI * -.5);
138
139         cairo_move_to (cr, w * -.5, h * -.5);
140         pango_cairo_update_layout (cr, _layout->gobj());
141         Gtkmm2ext::set_source_rgb_a (cr, base, 0.5);
142         pango_cairo_layout_path (cr, _layout->gobj());
143         cairo_set_line_width (cr, 1.5);
144         cairo_stroke (cr);
145
146         cairo_move_to (cr, w * -.5, h * -.5);
147         pango_cairo_update_layout (cr, _layout->gobj());
148         Gtkmm2ext::set_source_rgba (cr, text);
149         pango_cairo_show_layout (cr, _layout->gobj());
150
151         cairo_restore (cr);
152 }
153
154 bool
155 DspLoadIndicator::on_button_release_event (GdkEventButton *ev)
156 {
157         ARDOUR::Session* s = ARDOUR_UI::instance ()->the_session ();
158         if (s) {
159                 s->reset_xrun_count ();
160         }
161         return true;
162 }