improve caching of metric and tick patters
[ardour.git] / gtk2_ardour / meter_patterns.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3     Author: Robin Gareus
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gtkmm2ext/cairo_widget.h>
21 #include <gtkmm2ext/gtk_ui.h>
22 #include <gtkmm2ext/utils.h>
23 #include <gtkmm2ext/rgb_macros.h>
24
25 #include <ardour/rc_configuration.h>
26 #include "ardour_ui.h"
27 #include "utils.h"
28 #include "logmeter.h"
29 #include "meter_patterns.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using namespace Gtk;
36 using namespace Gtkmm2ext;
37 using namespace std;
38
39
40 static const int max_pattern_metric_size = 1026;
41
42 sigc::signal<void> ResetAllPeakDisplays;
43 sigc::signal<void,ARDOUR::Route*> ResetRoutePeakDisplays;
44 sigc::signal<void,ARDOUR::RouteGroup*> ResetGroupPeakDisplays;
45 sigc::signal<void> RedrawMetrics;
46
47 sigc::signal<void, int, ARDOUR::RouteGroup*, ARDOUR::MeterType> SetMeterTypeMulti;
48
49 cairo_pattern_t*
50 meter_render_ticks (Gtk::Widget& w, vector<ARDOUR::DataType> types)
51 {
52         Glib::RefPtr<Gdk::Window> win (w.get_window());
53
54         bool background;
55         gint width, height;
56         win->get_size (width, height);
57         background = types.size() == 0
58                 || w.get_name().substr(w.get_name().length() - 4) == "Left"
59                 || w.get_name().substr(w.get_name().length() - 5) == "Right";
60
61         cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
62         cairo_t* cr = cairo_create (surface);
63
64         cairo_move_to (cr, 0, 0);
65         cairo_rectangle (cr, 0, 0, width, height);
66         {
67                 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
68                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
69         }
70         cairo_fill (cr);
71
72         height = min(max_pattern_metric_size, height);
73         uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
74
75         for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
76
77                 Gdk::Color c;
78                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
79
80                 if (types.size() > 1) {
81                         /* we're overlaying more than 1 set of marks, so use different colours */
82                         switch (*i) {
83                         case DataType::AUDIO:
84                                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
85                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
86                                 break;
87                         case DataType::MIDI:
88                                 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
89                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
90                                 break;
91                         }
92                 } else {
93                         c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
94                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
95                 }
96
97                 std::map<int,float> points;
98
99                 switch (*i) {
100                 case DataType::AUDIO:
101                         points.insert (std::pair<int,float>(-60, 0.5));
102                         points.insert (std::pair<int,float>(-50, 0.5));
103                         points.insert (std::pair<int,float>(-40, 0.5));
104                         points.insert (std::pair<int,float>(-30, 0.5));
105                         if (Config->get_meter_line_up_level() == MeteringLineUp24) {
106                                 points.insert (std::pair<int,float>(-24, 0.5));
107                         } else {
108                                 points.insert (std::pair<int,float>(-25, 0.5));
109                         }
110                         points.insert (std::pair<int,float>(-20, 1.0));
111
112                         points.insert (std::pair<int,float>(-19, 0.5));
113                         points.insert (std::pair<int,float>(-18, 1.0));
114                         points.insert (std::pair<int,float>(-17, 0.5));
115                         points.insert (std::pair<int,float>(-16, 0.5));
116                         points.insert (std::pair<int,float>(-15, 1.0));
117
118                         points.insert (std::pair<int,float>(-14, 0.5));
119                         points.insert (std::pair<int,float>(-13, 0.5));
120                         points.insert (std::pair<int,float>(-12, 0.5));
121                         points.insert (std::pair<int,float>(-11, 0.5));
122                         points.insert (std::pair<int,float>(-10, 1.0));
123
124                         points.insert (std::pair<int,float>( -9, 0.5));
125                         points.insert (std::pair<int,float>( -8, 0.5));
126                         points.insert (std::pair<int,float>( -7, 0.5));
127                         points.insert (std::pair<int,float>( -6, 0.5));
128                         points.insert (std::pair<int,float>( -5, 1.0));
129                         points.insert (std::pair<int,float>( -4, 0.5));
130                         points.insert (std::pair<int,float>( -3, 0.5));
131                         points.insert (std::pair<int,float>( -2, 0.5));
132                         points.insert (std::pair<int,float>( -1, 0.5));
133
134                         points.insert (std::pair<int,float>(  0, 1.0));
135                         points.insert (std::pair<int,float>(  1, 0.5));
136                         points.insert (std::pair<int,float>(  2, 0.5));
137                         points.insert (std::pair<int,float>(  3, 0.5));
138                         points.insert (std::pair<int,float>(  4, 0.5));
139                         points.insert (std::pair<int,float>(  5, 0.5));
140                         break;
141
142                 case DataType::MIDI:
143                         points.insert (std::pair<int,float>(  0, 1.0));
144                         points.insert (std::pair<int,float>( 16, 0.5));
145                         points.insert (std::pair<int,float>( 32, 0.5));
146                         points.insert (std::pair<int,float>( 48, 0.5));
147                         points.insert (std::pair<int,float>( 64, 1.0));
148                         points.insert (std::pair<int,float>( 80, 0.5));
149                         points.insert (std::pair<int,float>( 96, 0.5));
150                         points.insert (std::pair<int,float>(100, 1.0));
151                         points.insert (std::pair<int,float>(112, 0.5));
152                         points.insert (std::pair<int,float>(127, 1.0));
153                         break;
154                 }
155
156                 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
157                         cairo_set_line_width (cr, (j->second));
158
159                         float fraction = 0;
160                         gint pos;
161
162                         switch (*i) {
163                         case DataType::AUDIO:
164                                 if (j->first >= 0 || j->first == -9) {
165                                         cairo_set_source_rgb (cr,
166                                                         UINT_RGBA_R_FLT(peakcolor),
167                                                         UINT_RGBA_G_FLT(peakcolor),
168                                                         UINT_RGBA_B_FLT(peakcolor));
169                                 } else {
170                                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
171                                 }
172                                 fraction = log_meter (j->first);
173                                 pos = height - (gint) floor (height * fraction);
174                                 cairo_move_to(cr, 0, pos + .5);
175                                 cairo_line_to(cr, 3, pos + .5);
176                                 cairo_stroke (cr);
177                                 break;
178                         case DataType::MIDI:
179                                 fraction = (j->first) / 127.0;
180                                 pos = 1 + height - (gint) floor (height * fraction);
181                                 pos = min (pos, height);
182                                 cairo_arc(cr, 1.5, pos + .5, 1.0, 0, 2 * M_PI);
183                                 cairo_fill(cr);
184                                 break;
185                         }
186                 }
187         }
188
189         cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
190
191         cairo_destroy (cr);
192         cairo_surface_destroy (surface);
193
194         return pattern;
195 }
196
197
198 cairo_pattern_t*
199 meter_render_metrics (Gtk::Widget& w, vector<DataType> types)
200 {
201         Glib::RefPtr<Gdk::Window> win (w.get_window());
202
203         bool tickleft;
204         bool background;
205         gint width, height;
206         win->get_size (width, height);
207
208         tickleft = w.get_name().substr(w.get_name().length() - 4) == "Left";
209         background = types.size() == 0 || tickleft || w.get_name().substr(w.get_name().length() - 5) == "Right";
210
211         cairo_surface_t* surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
212         cairo_t* cr = cairo_create (surface);
213         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(w.get_pango_context());
214
215         Pango::AttrList audio_font_attributes;
216         Pango::AttrList midi_font_attributes;
217         Pango::AttrList unit_font_attributes;
218
219         Pango::AttrFontDesc* font_attr;
220         Pango::FontDescription font;
221
222         font = Pango::FontDescription (""); // use defaults
223         //font = get_font_for_style("gain-fader");
224         //font = w.get_style()->get_font();
225
226         font.set_weight (Pango::WEIGHT_NORMAL);
227         font.set_size (9.0 * PANGO_SCALE);
228         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
229         audio_font_attributes.change (*font_attr);
230         delete font_attr;
231
232         font.set_weight (Pango::WEIGHT_ULTRALIGHT);
233         font.set_stretch (Pango::STRETCH_ULTRA_CONDENSED);
234         font.set_size (7.5 * PANGO_SCALE);
235         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
236         midi_font_attributes.change (*font_attr);
237         delete font_attr;
238
239         font.set_size (7.0 * PANGO_SCALE);
240         font_attr = new Pango::AttrFontDesc (Pango::Attribute::create_attr_font_desc (font));
241         unit_font_attributes.change (*font_attr);
242         delete font_attr;
243
244         cairo_move_to (cr, 0, 0);
245         cairo_rectangle (cr, 0, 0, width, height);
246         {
247                 Gdk::Color c = w.get_style()->get_bg (background ? Gtk::STATE_ACTIVE : Gtk::STATE_NORMAL);
248                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
249         }
250         cairo_fill (cr);
251
252         height = min(max_pattern_metric_size, height);
253         uint32_t peakcolor = ARDOUR_UI::config()->color_by_name ("meterbridge peaklabel");
254
255         for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
256
257                 Gdk::Color c;
258
259                 if (types.size() > 1) {
260                         /* we're overlaying more than 1 set of marks, so use different colours */
261                         Gdk::Color c;
262                         switch (*i) {
263                         case DataType::AUDIO:
264                                 c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
265                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
266                                 break;
267                         case DataType::MIDI:
268                                 c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
269                                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
270                                 break;
271                         }
272                 } else {
273                         c = w.get_style()->get_fg (Gtk::STATE_NORMAL);
274                         cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
275                 }
276
277                 std::map<int,float> points;
278
279                 switch (*i) {
280                 case DataType::AUDIO:
281                         layout->set_attributes (audio_font_attributes);
282                         points.insert (std::pair<int,float>(-50, 0.5));
283                         points.insert (std::pair<int,float>(-40, 0.5));
284                         points.insert (std::pair<int,float>(-30, 0.5));
285                         points.insert (std::pair<int,float>(-20, 1.0));
286                         if (types.size() == 1) {
287                                 if (Config->get_meter_line_up_level() == MeteringLineUp24) {
288                                         points.insert (std::pair<int,float>(-24, 0.5));
289                                 } else {
290                                         points.insert (std::pair<int,float>(-25, 0.5));
291                                 }
292                                 points.insert (std::pair<int,float>(-15, 1.0));
293                         }
294                         points.insert (std::pair<int,float>(-18, 1.0));
295                         points.insert (std::pair<int,float>(-10, 1.0));
296                         points.insert (std::pair<int,float>( -5, 1.0));
297                         points.insert (std::pair<int,float>( -3, 0.5));
298                         points.insert (std::pair<int,float>(  0, 1.0));
299                         points.insert (std::pair<int,float>(  3, 0.5));
300                         break;
301
302                 case DataType::MIDI:
303                         layout->set_attributes (midi_font_attributes);
304                         points.insert (std::pair<int,float>(  0, 1.0));
305                         if (types.size() == 1) {
306                                 points.insert (std::pair<int,float>( 16, 0.5));
307                                 points.insert (std::pair<int,float>( 32, 0.5));
308                                 points.insert (std::pair<int,float>( 48, 0.5));
309                                 points.insert (std::pair<int,float>( 64, 1.0));
310                                 points.insert (std::pair<int,float>( 80, 0.5));
311                                 points.insert (std::pair<int,float>( 96, 0.5));
312                                 points.insert (std::pair<int,float>(100, 0.5));
313                                 points.insert (std::pair<int,float>(112, 0.5));
314                         } else {
315                                 /* labels that don't overlay with dB */
316                                 points.insert (std::pair<int,float>( 24, 0.5));
317                                 points.insert (std::pair<int,float>( 48, 0.5));
318                                 points.insert (std::pair<int,float>( 72, 0.5));
319                         }
320                         points.insert (std::pair<int,float>(127, 1.0));
321                         break;
322                 }
323
324                 char buf[32];
325                 gint pos;
326
327                 for (std::map<int,float>::const_iterator j = points.begin(); j != points.end(); ++j) {
328
329                         float fraction = 0;
330                         switch (*i) {
331                         case DataType::AUDIO:
332                                 cairo_set_line_width (cr, (j->second));
333                                 if (j->first >= 0) {
334                                         cairo_set_source_rgb (cr,
335                                                         UINT_RGBA_R_FLT(peakcolor),
336                                                         UINT_RGBA_G_FLT(peakcolor),
337                                                         UINT_RGBA_B_FLT(peakcolor));
338                                 }
339                                 fraction = log_meter (j->first);
340                                 snprintf (buf, sizeof (buf), "%+2d", j->first);
341                                 pos = height - (gint) floor (height * fraction);
342                                 if (tickleft) {
343                                         cairo_move_to(cr, width-2.5, pos + .5);
344                                         cairo_line_to(cr, width, pos + .5);
345                                 } else {
346                                         cairo_move_to(cr, 0, pos + .5);
347                                         cairo_line_to(cr, 2.5, pos + .5);
348                                 }
349                                 cairo_stroke (cr);
350                                 break;
351                         case DataType::MIDI:
352                                 cairo_set_line_width (cr, 1.0);
353                                 fraction = (j->first) / 127.0;
354                                 snprintf (buf, sizeof (buf), "%3d", j->first);
355                                 pos = 1 + height - (gint) rintf (height * fraction);
356                                 pos = min (pos, height);
357                                 if (tickleft) {
358                                         cairo_arc(cr, width - 2.0, pos + .5, 1.0, 0, 2 * M_PI);
359                                 } else {
360                                         cairo_arc(cr, 3, pos + .5, 1.0, 0, 2 * M_PI);
361                                 }
362                                 cairo_fill(cr);
363                                 break;
364                         }
365
366                         layout->set_text(buf);
367
368                         /* we want logical extents, not ink extents here */
369
370                         int tw, th;
371                         layout->get_pixel_size(tw, th);
372
373                         int p = pos - (th / 2);
374                         p = min (p, height - th);
375                         p = max (p, 0);
376
377                         cairo_move_to (cr, width-4-tw, p);
378                         pango_cairo_show_layout (cr, layout->gobj());
379                 }
380         }
381
382         if (types.size() == 1) {
383                 int tw, th;
384                 layout->set_attributes (unit_font_attributes);
385                 switch (types.at(0)) {
386                         case DataType::AUDIO:
387                                 layout->set_text("dBFS");
388                                 layout->get_pixel_size(tw, th);
389                                 break;
390                         case DataType::MIDI:
391                                 layout->set_text("vel");
392                                 layout->get_pixel_size(tw, th);
393                                 break;
394                 }
395                 Gdk::Color c = w.get_style()->get_fg (Gtk::STATE_ACTIVE);
396                 cairo_set_source_rgb (cr, c.get_red_p(), c.get_green_p(), c.get_blue_p());
397                 cairo_move_to (cr, 2, height - th - 1.5);
398                 pango_cairo_show_layout (cr, layout->gobj());
399         }
400
401         cairo_pattern_t* pattern = cairo_pattern_create_for_surface (surface);
402
403         cairo_destroy (cr);
404         cairo_surface_destroy (surface);
405
406         return pattern;
407 }
408
409
410 typedef std::map<std::string,cairo_pattern_t*> TickPatterns;
411 static  TickPatterns ticks_patterns;
412
413 gint meter_expose_ticks (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mta)
414 {
415         Glib::RefPtr<Gdk::Window> win (mta->get_window());
416         cairo_t* cr;
417
418         cr = gdk_cairo_create (win->gobj());
419
420         /* clip to expose area */
421
422         gdk_cairo_rectangle (cr, &ev->area);
423         cairo_clip (cr);
424
425         cairo_pattern_t* pattern;
426         TickPatterns::iterator i = ticks_patterns.find (mta->get_name());
427
428         if (i == ticks_patterns.end()) {
429                 pattern = meter_render_ticks (*mta, types);
430                 ticks_patterns[mta->get_name()] = pattern;
431         } else {
432                 pattern = i->second;
433         }
434
435         cairo_move_to (cr, 0, 0);
436         cairo_set_source (cr, pattern);
437
438         gint width, height;
439         win->get_size (width, height);
440
441         cairo_rectangle (cr, 0, 0, width, height);
442         cairo_fill (cr);
443
444         cairo_destroy (cr);
445
446         return true;
447 }
448
449 typedef std::map<std::string,cairo_pattern_t*> MetricPatterns;
450 static  MetricPatterns metric_patterns;
451
452 gint meter_expose_metrics (GdkEventExpose *ev, std::vector<ARDOUR::DataType> types, Gtk::DrawingArea *mma)
453 {
454         Glib::RefPtr<Gdk::Window> win (mma->get_window());
455         cairo_t* cr;
456
457         cr = gdk_cairo_create (win->gobj());
458
459         /* clip to expose area */
460
461         gdk_cairo_rectangle (cr, &ev->area);
462         cairo_clip (cr);
463
464         cairo_pattern_t* pattern;
465         MetricPatterns::iterator i = metric_patterns.find (mma->get_name());
466
467         if (i == metric_patterns.end()) {
468                 pattern = meter_render_metrics (*mma, types);
469                 metric_patterns[mma->get_name()] = pattern;
470         } else {
471                 pattern = i->second;
472         }
473
474         cairo_move_to (cr, 0, 0);
475         cairo_set_source (cr, pattern);
476
477         gint width, height;
478         win->get_size (width, height);
479
480         cairo_rectangle (cr, 0, 0, width, height);
481         cairo_fill (cr);
482
483         cairo_destroy (cr);
484
485         return true;
486 }
487
488 void meter_clear_pattern_cache(int which) {
489         MetricPatterns::iterator i = metric_patterns.begin();
490         TickPatterns::iterator j = ticks_patterns.begin();
491
492         while (i != metric_patterns.end()) {
493                 int m = 4;
494                 std::string n = i->first;
495                 if (n.substr(n.length() - 4) == "Left")  { m = 1; }
496                 if (n.substr(n.length() - 5) == "Right") { m = 2; }
497                 if (which & m) {
498                         cairo_pattern_destroy(i->second);
499                         metric_patterns.erase(i++);
500                 } else {
501                         ++i;
502                 }
503         }
504
505         while (j != ticks_patterns.end()) {
506                 int m = 4;
507                 std::string n = j->first;
508                 if (n.substr(n.length() - 4) == "Left")  { m = 1; }
509                 if (n.substr(n.length() - 5) == "Right") { m = 2; }
510                 if (which & m) {
511                         cairo_pattern_destroy(j->second);
512                         ticks_patterns.erase(j++);
513                 } else {
514                         ++j;
515                 }
516         }
517         RedrawMetrics();
518 }