2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include <cairomm/cairomm.h>
24 #include <glibmm/threads.h>
26 #include "gtkmm2ext/utils.h"
27 #include "gtkmm2ext/gui_thread.h"
29 #include "pbd/base_ui.h"
30 #include "pbd/compose.h"
31 #include "pbd/convert.h"
32 #include "pbd/signals.h"
33 #include "pbd/stacktrace.h"
35 #include "ardour/types.h"
36 #include "ardour/dB.h"
37 #include "ardour/lmath.h"
38 #include "ardour/audioregion.h"
39 #include "ardour/audiosource.h"
40 #include "ardour/session.h"
42 #include "canvas/canvas.h"
43 #include "canvas/colors.h"
44 #include "canvas/debug.h"
45 #include "canvas/utils.h"
46 #include "canvas/wave_view.h"
48 #include "evoral/Range.hpp"
50 #include <gdkmm/general.h>
52 #include "gtkmm2ext/gui_thread.h"
55 using namespace ARDOUR;
57 using namespace ArdourCanvas;
59 double WaveView::_global_gradient_depth = 0.6;
60 bool WaveView::_global_logscaled = false;
61 WaveView::Shape WaveView::_global_shape = WaveView::Normal;
62 bool WaveView::_global_show_waveform_clipping = true;
63 double WaveView::_clip_level = 0.98853;
65 WaveViewCache* WaveView::images = 0;
66 gint WaveView::drawing_thread_should_quit = 0;
67 Glib::Threads::Mutex WaveView::request_queue_lock;
68 Glib::Threads::Mutex WaveView::current_image_lock;
69 Glib::Threads::Cond WaveView::request_cond;
70 Glib::Threads::Thread* WaveView::_drawing_thread = 0;
71 WaveView::DrawingRequestQueue WaveView::request_queue;
73 PBD::Signal0<void> WaveView::VisualPropertiesChanged;
74 PBD::Signal0<void> WaveView::ClipLevelChanged;
76 #undef ENABLE_THREADED_WAVEFORM_RENDERING
78 WaveView::WaveView (Canvas* c, boost::shared_ptr<ARDOUR::AudioRegion> region)
82 , _samples_per_pixel (0)
85 , _zero_color (0xff0000ff)
86 , _clip_color (0xff0000ff)
87 , _logscaled (_global_logscaled)
88 , _shape (_global_shape)
89 , _gradient_depth (_global_gradient_depth)
90 , _shape_independent (false)
91 , _logscaled_independent (false)
92 , _gradient_depth_independent (false)
93 , _amplitude_above_axis (1.0)
94 , _region_amplitude (region->scale_amplitude ())
96 , _region_start (region->start())
97 , get_image_in_thread (false)
98 , always_get_image_in_thread (false)
102 images = new WaveViewCache;
105 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
106 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
108 ImageReady.connect (image_ready_connection, invalidator (*this), boost::bind (&WaveView::image_ready, this), gui_context());
111 WaveView::WaveView (Item* parent, boost::shared_ptr<ARDOUR::AudioRegion> region)
115 , _samples_per_pixel (0)
118 , _zero_color (0xff0000ff)
119 , _clip_color (0xff0000ff)
120 , _logscaled (_global_logscaled)
121 , _shape (_global_shape)
122 , _gradient_depth (_global_gradient_depth)
123 , _shape_independent (false)
124 , _logscaled_independent (false)
125 , _gradient_depth_independent (false)
126 , _amplitude_above_axis (1.0)
127 , _region_amplitude (region->scale_amplitude ())
129 , _region_start (region->start())
130 , get_image_in_thread (false)
131 , always_get_image_in_thread (false)
135 images = new WaveViewCache;
138 VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
139 ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
141 ImageReady.connect (image_ready_connection, invalidator (*this), boost::bind (&WaveView::image_ready, this), gui_context());
144 WaveView::~WaveView ()
146 invalidate_image_cache ();
148 images->clear_cache ();
153 WaveView::debug_name() const
155 return _region->name() + string (":") + PBD::to_string (_channel+1, std::dec);
159 WaveView::image_ready ()
161 DEBUG_TRACE (DEBUG::WaveView, string_compose ("queue draw for %1 at %2 (vis = %3 CR %4)\n", this, g_get_monotonic_time(), visible(), current_request));
166 WaveView::set_always_get_image_in_thread (bool yn)
168 always_get_image_in_thread = yn;
172 WaveView::handle_visual_property_change ()
174 bool changed = false;
176 if (!_shape_independent && (_shape != global_shape())) {
177 _shape = global_shape();
181 if (!_logscaled_independent && (_logscaled != global_logscaled())) {
182 _logscaled = global_logscaled();
186 if (!_gradient_depth_independent && (_gradient_depth != global_gradient_depth())) {
187 _gradient_depth = global_gradient_depth();
192 begin_visual_change ();
193 invalidate_image_cache ();
194 end_visual_change ();
199 WaveView::handle_clip_level_change ()
201 begin_visual_change ();
202 invalidate_image_cache ();
203 end_visual_change ();
207 WaveView::set_fill_color (Color c)
209 if (c != _fill_color) {
210 begin_visual_change ();
211 invalidate_image_cache ();
212 Fill::set_fill_color (c);
213 end_visual_change ();
218 WaveView::set_outline_color (Color c)
220 if (c != _outline_color) {
221 begin_visual_change ();
222 invalidate_image_cache ();
223 Outline::set_outline_color (c);
224 end_visual_change ();
229 WaveView::set_samples_per_pixel (double samples_per_pixel)
231 if (samples_per_pixel != _samples_per_pixel) {
234 invalidate_image_cache ();
235 _samples_per_pixel = samples_per_pixel;
236 _bounding_box_dirty = true;
243 _log_meter (float power, double lower_db, double upper_db, double non_linearity)
245 return (power < lower_db ? 0.0 : pow((power-lower_db)/(upper_db-lower_db), non_linearity));
249 alt_log_meter (float power)
251 return _log_meter (power, -192.0, 0.0, 8.0);
255 WaveView::set_clip_level (double dB)
257 const double clip_level = dB_to_coefficient (dB);
258 if (clip_level != _clip_level) {
259 _clip_level = clip_level;
265 WaveView::invalidate_image_cache ()
267 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 invalidates image cache and cancels current request\n", this));
268 cancel_my_render_request ();
269 Glib::Threads::Mutex::Lock lci (current_image_lock);
270 _current_image.reset ();
274 WaveView::compute_tips (PeakData const & peak, WaveView::LineTips& tips) const
276 const double effective_height = _height;
278 /* remember: canvas (and cairo) coordinate space puts the origin at the upper left.
280 So, a sample value of 1.0 (0dbFS) will be computed as:
282 (1.0 - 1.0) * 0.5 * effective_height
284 which evaluates to 0, or the top of the image.
286 A sample value of -1.0 will be computed as
288 (1.0 + 1.0) * 0.5 * effective height
290 which evaluates to effective height, or the bottom of the image.
293 const double pmax = (1.0 - peak.max) * 0.5 * effective_height;
294 const double pmin = (1.0 - peak.min) * 0.5 * effective_height;
296 /* remember that the bottom of the image (pmin) has larger y-coordinates
300 double spread = (pmin - pmax) * 0.5;
302 /* find the nearest pixel to the nominal center. */
303 const double center = round (pmin - spread);
306 /* minimum distance between line ends is 1 pixel, and we want it "centered" on a pixel,
307 as per cairo single-pixel line issues.
309 NOTE: the caller will not draw a line between these two points if the spread is
310 less than 2 pixels. So only the tips.top value matters, which is where we will
311 draw a single pixel as part of the outline.
314 tips.bot = center + 1.0;
316 /* round spread above and below center to an integer number of pixels */
317 spread = round (spread);
318 /* top and bottom are located equally either side of the center */
319 tips.top = center - spread;
320 tips.bot = center + spread;
323 tips.top = min (effective_height, max (0.0, tips.top));
324 tips.bot = min (effective_height, max (0.0, tips.bot));
329 WaveView::y_extent (double s) const
331 assert (_shape == Rectified);
332 return floor ((1.0 - s) * _height);
336 WaveView::draw_absent_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks) const
338 Cairo::RefPtr<Cairo::ImageSurface> stripe = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
340 Cairo::RefPtr<Cairo::Context> stripe_context = Cairo::Context::create (stripe);
341 stripe_context->set_antialias (Cairo::ANTIALIAS_NONE);
343 uint32_t stripe_separation = 150;
344 double start = - floor (_height / stripe_separation) * stripe_separation;
347 while (start < n_peaks) {
349 stripe_context->move_to (start, 0);
350 stripe_x = start + _height;
351 stripe_context->line_to (stripe_x, _height);
352 start += stripe_separation;
355 stripe_context->set_source_rgba (1.0, 1.0, 1.0, 1.0);
356 stripe_context->set_line_cap (Cairo::LINE_CAP_SQUARE);
357 stripe_context->set_line_width(50);
358 stripe_context->stroke();
360 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
362 context->set_source_rgba (1.0, 1.0, 0.0, 0.3);
363 context->mask (stripe, 0, 0);
368 Cairo::RefPtr<Cairo::ImageSurface> wave;
369 Cairo::RefPtr<Cairo::ImageSurface> outline;
370 Cairo::RefPtr<Cairo::ImageSurface> clip;
371 Cairo::RefPtr<Cairo::ImageSurface> zero;
374 wave (0), outline (0), clip (0), zero (0) {}
378 WaveView::draw_image (Cairo::RefPtr<Cairo::ImageSurface>& image, PeakData* _peaks, int n_peaks, boost::shared_ptr<WaveViewThreadRequest> req) const
383 images.wave = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
384 images.outline = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
385 images.clip = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
386 images.zero = Cairo::ImageSurface::create (Cairo::FORMAT_A8, n_peaks, _height);
388 Cairo::RefPtr<Cairo::Context> wave_context = Cairo::Context::create (images.wave);
389 Cairo::RefPtr<Cairo::Context> outline_context = Cairo::Context::create (images.outline);
390 Cairo::RefPtr<Cairo::Context> clip_context = Cairo::Context::create (images.clip);
391 Cairo::RefPtr<Cairo::Context> zero_context = Cairo::Context::create (images.zero);
392 wave_context->set_antialias (Cairo::ANTIALIAS_NONE);
393 outline_context->set_antialias (Cairo::ANTIALIAS_NONE);
394 clip_context->set_antialias (Cairo::ANTIALIAS_NONE);
395 zero_context->set_antialias (Cairo::ANTIALIAS_NONE);
397 boost::scoped_array<LineTips> tips (new LineTips[n_peaks]);
399 /* Clip level nominally set to -0.9dBFS to account for inter-sample
400 interpolation possibly clipping (value may be too low).
402 We adjust by the region's own gain (but note: not by any gain
403 automation or its gain envelope) so that clip indicators are closer
404 to providing data about on-disk data. This multiplication is
405 needed because the data we get from AudioRegion::read_peaks()
406 has been scaled by scale_amplitude() already.
409 const double clip_level = _clip_level * _region_amplitude;
411 if (_shape == WaveView::Rectified) {
413 /* each peak is a line from the bottom of the waveview
414 * to a point determined by max (_peaks[i].max,
419 for (int i = 0; i < n_peaks; ++i) {
421 tips[i].bot = height() - 1.0;
422 const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min))));
423 tips[i].top = y_extent (p);
424 tips[i].spread = p * _height;
426 if (_peaks[i].max >= clip_level) {
427 tips[i].clip_max = true;
430 if (-(_peaks[i].min) >= clip_level) {
431 tips[i].clip_min = true;
436 for (int i = 0; i < n_peaks; ++i) {
438 tips[i].bot = height() - 1.0;
439 const double p = max(fabs (_peaks[i].max), fabs (_peaks[i].min));
440 tips[i].top = y_extent (p);
441 tips[i].spread = p * _height;
442 if (p >= clip_level) {
443 tips[i].clip_max = true;
452 for (int i = 0; i < n_peaks; ++i) {
454 p.max = _peaks[i].max;
455 p.min = _peaks[i].min;
457 if (_peaks[i].max >= clip_level) {
458 tips[i].clip_max = true;
460 if (-(_peaks[i].min) >= clip_level) {
461 tips[i].clip_min = true;
465 p.max = alt_log_meter (fast_coefficient_to_dB (p.max));
466 } else if (p.max < 0.0) {
467 p.max =-alt_log_meter (fast_coefficient_to_dB (-p.max));
473 p.min = alt_log_meter (fast_coefficient_to_dB (p.min));
474 } else if (p.min < 0.0) {
475 p.min = -alt_log_meter (fast_coefficient_to_dB (-p.min));
480 compute_tips (p, tips[i]);
481 tips[i].spread = tips[i].bot - tips[i].top;
485 for (int i = 0; i < n_peaks; ++i) {
486 if (_peaks[i].max >= clip_level) {
487 tips[i].clip_max = true;
489 if (-(_peaks[i].min) >= clip_level) {
490 tips[i].clip_min = true;
493 compute_tips (_peaks[i], tips[i]);
494 tips[i].spread = tips[i].bot - tips[i].top;
500 if (req->should_stop()) {
504 Color alpha_one = rgba_to_color (0, 0, 0, 1.0);
506 set_source_rgba (wave_context, alpha_one);
507 set_source_rgba (outline_context, alpha_one);
508 set_source_rgba (clip_context, alpha_one);
509 set_source_rgba (zero_context, alpha_one);
511 /* ensure single-pixel lines */
513 wave_context->set_line_width (1.0);
514 wave_context->translate (0.5, 0.5);
516 outline_context->set_line_width (1.0);
517 outline_context->translate (0.5, 0.5);
519 clip_context->set_line_width (1.0);
520 clip_context->translate (0.5, 0.5);
522 zero_context->set_line_width (1.0);
523 zero_context->translate (0.5, 0.5);
525 /* the height of the clip-indicator should be at most 7 pixels,
526 * or 5% of the height of the waveview item.
529 const double clip_height = min (7.0, ceil (_height * 0.05));
531 /* There are 3 possible components to draw at each x-axis position: the
532 waveform "line", the zero line and an outline/clip indicator. We
533 have to decide which of the 3 to draw at each position, pixel by
534 pixel. This makes the rendering less efficient but it is the only
535 way I can see to do this correctly.
537 To avoid constant source swapping and stroking, we draw the components separately
538 onto four alpha only image surfaces for use as a mask.
540 With only 1 pixel of spread between the top and bottom of the line,
541 we just draw the upper outline/clip indicator.
543 With 2 pixels of spread, we draw the upper and lower outline clip
546 With 3 pixels of spread we draw the upper and lower outline/clip
547 indicators and at least 1 pixel of the waveform line.
549 With 5 pixels of spread, we draw all components.
551 We can do rectified as two separate passes because we have a much
552 easier decision regarding whether to draw the waveform line. We
553 always draw the clip/outline indicators.
556 if (_shape == WaveView::Rectified) {
558 for (int i = 0; i < n_peaks; ++i) {
562 if (tips[i].spread >= 1.0) {
563 wave_context->move_to (i, tips[i].top);
564 wave_context->line_to (i, tips[i].bot);
569 if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) {
570 clip_context->move_to (i, tips[i].top);
571 /* clip-indicating upper terminal line */
572 clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5)));
574 outline_context->move_to (i, tips[i].top);
575 /* normal upper terminal dot */
576 outline_context->rel_line_to (0, -1.0);
580 wave_context->stroke ();
581 clip_context->stroke ();
582 outline_context->stroke ();
585 const int height_zero = floor( _height * .5);
587 for (int i = 0; i < n_peaks; ++i) {
591 if (tips[i].spread >= 2.0) {
592 wave_context->move_to (i, tips[i].top);
593 wave_context->line_to (i, tips[i].bot);
596 /* draw square waves and other discontiguous points clearly */
598 if (tips[i-1].top + 2 < tips[i].top) {
599 wave_context->move_to (i-1, tips[i-1].top);
600 wave_context->line_to (i-1, (tips[i].bot + tips[i-1].top)/2);
601 wave_context->move_to (i, (tips[i].bot + tips[i-1].top)/2);
602 wave_context->line_to (i, tips[i].top);
603 } else if (tips[i-1].bot > tips[i].bot + 2) {
604 wave_context->move_to (i-1, tips[i-1].bot);
605 wave_context->line_to (i-1, (tips[i].top + tips[i-1].bot)/2);
606 wave_context->move_to (i, (tips[i].top + tips[i-1].bot)/2);
607 wave_context->line_to (i, tips[i].bot);
611 /* zero line, show only if there is enough spread
612 or the waveform line does not cross zero line */
614 if (show_zero_line() && ((tips[i].spread >= 5.0) || (tips[i].top > height_zero ) || (tips[i].bot < height_zero)) ) {
615 zero_context->move_to (i, height_zero);
616 zero_context->rel_line_to (1.0, 0);
619 if (tips[i].spread > 1.0) {
620 bool clipped = false;
621 /* outline/clip indicators */
622 if (_global_show_waveform_clipping && tips[i].clip_max) {
623 clip_context->move_to (i, tips[i].top);
624 /* clip-indicating upper terminal line */
625 clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + 0.5)));
629 if (_global_show_waveform_clipping && tips[i].clip_min) {
630 clip_context->move_to (i, tips[i].bot);
631 /* clip-indicating lower terminal line */
632 clip_context->rel_line_to (0, - min (clip_height, ceil(tips[i].spread + 0.5)));
636 if (!clipped && tips[i].spread > 2.0) {
637 /* only draw the outline if the spread
638 implies 3 or more pixels (so that we see 1
639 white pixel in the middle).
641 outline_context->move_to (i, tips[i].bot);
642 /* normal lower terminal dot; line moves up */
643 outline_context->rel_line_to (0, -1.0);
645 outline_context->move_to (i, tips[i].top);
646 /* normal upper terminal dot, line moves down */
647 outline_context->rel_line_to (0, 1.0);
650 bool clipped = false;
651 /* outline/clip indicator */
652 if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) {
653 clip_context->move_to (i, tips[i].top);
654 /* clip-indicating upper / lower terminal line */
655 clip_context->rel_line_to (0, 1.0);
660 /* special case where only 1 pixel of
661 * the waveform line is drawn (and
664 * we draw a 1px "line", pretending
665 * that the span is 1.0 (whether it is
668 wave_context->move_to (i, tips[i].top);
669 wave_context->rel_line_to (0, 1.0);
674 wave_context->stroke ();
675 outline_context->stroke ();
676 clip_context->stroke ();
677 zero_context->stroke ();
680 if (req->should_stop()) {
684 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create (image);
686 /* Here we set a source colour and use the various components as a mask. */
688 if (gradient_depth() != 0.0) {
690 Cairo::RefPtr<Cairo::LinearGradient> gradient (Cairo::LinearGradient::create (0, 0, 0, _height));
696 if (_shape == Rectified) {
706 color_to_rgba (_fill_color, r, g, b, a);
707 gradient->add_color_stop_rgba (stops[1], r, g, b, a);
708 /* generate a new color for the middle of the gradient */
710 color_to_hsv (_fill_color, h, s, v);
711 /* change v towards white */
712 v *= 1.0 - gradient_depth();
713 Color center = hsva_to_color (h, s, v, a);
714 color_to_rgba (center, r, g, b, a);
716 gradient->add_color_stop_rgba (stops[0], r, g, b, a);
717 gradient->add_color_stop_rgba (stops[2], r, g, b, a);
719 context->set_source (gradient);
721 set_source_rgba (context, _fill_color);
724 if (req->should_stop()) {
728 context->mask (images.wave, 0, 0);
731 set_source_rgba (context, _outline_color);
732 context->mask (images.outline, 0, 0);
735 set_source_rgba (context, _clip_color);
736 context->mask (images.clip, 0, 0);
739 set_source_rgba (context, _zero_color);
740 context->mask (images.zero, 0, 0);
744 boost::shared_ptr<WaveViewCache::Entry>
745 WaveView::cache_request_result (boost::shared_ptr<WaveViewThreadRequest> req) const
748 cerr << "asked to cache null image!!!\n";
749 return boost::shared_ptr<WaveViewCache::Entry> ();
752 boost::shared_ptr<WaveViewCache::Entry> ret (new WaveViewCache::Entry (req->channel,
756 req->samples_per_pixel,
760 images->add (_region->audio_source (_channel), ret);
762 /* consolidate cache first (removes fully-contained
766 images->consolidate_image_cache (_region->audio_source (_channel),
767 req->channel, req->height, req->amplitude,
768 req->fill_color, req->samples_per_pixel);
773 boost::shared_ptr<WaveViewCache::Entry>
774 WaveView::get_image (framepos_t start, framepos_t end, bool& full_image) const
776 boost::shared_ptr<WaveViewCache::Entry> ret;
780 /* this is called from a ::render() call, when we need an image to
784 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 needs image from %2 .. %3\n", name, start, end));
788 Glib::Threads::Mutex::Lock lmq (request_queue_lock);
790 /* if there's a draw request outstanding, check to see if we
791 * have an image there. if so, use it (and put it in the cache
795 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 CR %2 stop? %3 image %4\n", this, current_request,
796 (current_request ? current_request->should_stop() : false),
797 (current_request ? current_request->image : 0)));
799 if (current_request && !current_request->should_stop() && current_request->image) {
801 /* put the image into the cache so that other
802 * WaveViews can use it if it is useful
805 if (current_request->start <= start && current_request->end >= end) {
807 ret.reset (new WaveViewCache::Entry (current_request->channel,
808 current_request->height,
809 current_request->amplitude,
810 current_request->fill_color,
811 current_request->samples_per_pixel,
812 current_request->start,
813 current_request->end,
814 current_request->image));
816 cache_request_result (current_request);
817 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: got image from completed request, spans %2..%3\n",
818 name, current_request->start, current_request->end));
821 /* drop our handle on the current request */
822 current_request.reset ();
828 /* no current image draw request, so look in the cache */
830 ret = get_image_from_cache (start, end, full_image);
831 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: lookup from cache gave %2 (full %3)\n",
832 name, ret, full_image));
838 if (!ret || !full_image) {
840 #ifndef ENABLE_THREADED_WAVEFORM_RENDERING
843 if ((rendered && get_image_in_thread) || always_get_image_in_thread)
847 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: generating image in caller thread\n", name));
849 boost::shared_ptr<WaveViewThreadRequest> req (new WaveViewThreadRequest);
851 req->type = WaveViewThreadRequest::Draw;
854 req->samples_per_pixel = _samples_per_pixel;
855 req->region = _region; /* weak ptr, to avoid storing a reference in the request queue */
856 req->channel = _channel;
857 req->height = _height;
858 req->fill_color = _fill_color;
859 req->amplitude = _region_amplitude * _amplitude_above_axis;
860 req->width = desired_image_width ();
862 /* draw image in this (the GUI thread) */
864 generate_image (req, false);
866 /* cache the result */
868 ret = cache_request_result (req);
870 /* reset this so that future missing images are
871 * generated in a a worker thread.
874 get_image_in_thread = false;
877 queue_get_image (_region, start, end);
882 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 got an image from %2 .. %3 (full ? %4)\n", name, ret->start, ret->end, full_image));
884 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 no useful image available\n", name));
890 boost::shared_ptr<WaveViewCache::Entry>
891 WaveView::get_image_from_cache (framepos_t start, framepos_t end, bool& full) const
894 return boost::shared_ptr<WaveViewCache::Entry>();
897 return images->lookup_image (_region->audio_source (_channel), start, end, _channel,
898 _height, _region_amplitude * _amplitude_above_axis, _fill_color, _samples_per_pixel, full);
902 WaveView::desired_image_width () const
904 /* compute how wide the image should be, in samples.
906 * We want at least 1 canvas width's worth, but if that
907 * represents less than 1/10th of a second, use 1/10th of
911 framecnt_t canvas_width_samples = _canvas->visible_area().width() * _samples_per_pixel;
912 const framecnt_t one_tenth_of_second = _region->session().frame_rate() / 10;
914 if (canvas_width_samples > one_tenth_of_second) {
915 return canvas_width_samples;
918 return one_tenth_of_second;
922 WaveView::queue_get_image (boost::shared_ptr<const ARDOUR::Region> region, framepos_t start, framepos_t end) const
924 boost::shared_ptr<WaveViewThreadRequest> req (new WaveViewThreadRequest);
926 req->type = WaveViewThreadRequest::Draw;
929 req->samples_per_pixel = _samples_per_pixel;
930 req->region = _region; /* weak ptr, to avoid storing a reference in the request queue */
931 req->channel = _channel;
932 req->height = _height;
933 req->fill_color = _fill_color;
934 req->amplitude = _region_amplitude * _amplitude_above_axis;
935 req->width = desired_image_width ();
937 if (current_request) {
938 /* this will stop rendering in progress (which might otherwise
939 be long lived) for any current request.
941 current_request->cancel ();
944 start_drawing_thread ();
946 /* swap requests (protected by lock) */
949 Glib::Threads::Mutex::Lock lm (request_queue_lock);
950 current_request = req;
952 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 now has current request %2\n", this, req));
954 if (request_queue.insert (this).second) {
955 /* this waveview was not already in the request queue, make sure we wake
956 the rendering thread in case it is asleep.
958 request_cond.signal ();
964 WaveView::generate_image (boost::shared_ptr<WaveViewThreadRequest> req, bool in_render_thread) const
966 if (!req->should_stop()) {
968 /* sample position is canonical here, and we want to generate
969 * an image that spans about 3x the canvas width. We get to that
970 * width by using an image sample count of the screen width added
971 * on each side of the desired image center.
974 const framepos_t center = req->start + ((req->end - req->start) / 2);
975 const framecnt_t image_samples = req->width;
977 /* we can request data from anywhere in the Source, between 0 and its length
980 framepos_t sample_start = max (_region_start, (center - image_samples));
981 framepos_t sample_end = min (center + image_samples, region_end());
982 const int n_peaks = llrintf ((sample_end - sample_start)/ (req->samples_per_pixel));
984 boost::scoped_array<ARDOUR::PeakData> peaks (new PeakData[n_peaks]);
986 /* Note that Region::read_peaks() takes a start position based on an
987 offset into the Region's **SOURCE**, rather than an offset into
991 framecnt_t peaks_read = _region->read_peaks (peaks.get(), n_peaks,
992 sample_start, sample_end - sample_start,
994 req->samples_per_pixel);
996 req->image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, req->height);
997 /* make sure we record the sample positions that were actually used */
998 req->start = sample_start;
999 req->end = sample_end;
1001 if (peaks_read > 0) {
1003 /* region amplitude will have been used to generate the
1004 * peak values already, but not the visual-only
1005 * amplitude_above_axis. So apply that here before
1009 if (_amplitude_above_axis != 1.0) {
1010 for (framecnt_t i = 0; i < n_peaks; ++i) {
1011 peaks[i].max *= _amplitude_above_axis;
1012 peaks[i].min *= _amplitude_above_axis;
1016 draw_image (req->image, peaks.get(), n_peaks, req);
1018 draw_absent_image (req->image, peaks.get(), n_peaks);
1021 cerr << "Request stopped before image generation\n";
1024 if (in_render_thread && !req->should_stop()) {
1025 DEBUG_TRACE (DEBUG::WaveView, string_compose ("done with request for %1 at %2 CR %3 req %4 range %5 .. %6\n", this, g_get_monotonic_time(), current_request, req, req->start, req->end));
1026 const_cast<WaveView*>(this)->ImageReady (); /* emit signal */
1032 /** Given a waveform that starts at window x-coordinate @param wave_origin
1033 * and the first pixel that we will actually draw @param draw_start, return
1034 * the offset into an image of the entire waveform that we will need to use.
1036 * Note: most of our cached images are NOT of the entire waveform, this is just
1037 * computationally useful when determining which the sample range span for
1038 * the image we need.
1040 static inline double
1041 window_to_image (double wave_origin, double image_start)
1043 return image_start - wave_origin;
1047 WaveView::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
1049 assert (_samples_per_pixel != 0);
1055 DEBUG_TRACE (DEBUG::WaveView, string_compose ("render %1 at %2\n", this, g_get_monotonic_time()));
1057 /* a WaveView is intimately connected to an AudioRegion. It will
1058 * display the waveform within the region, anywhere from the start of
1059 * the region to its end.
1061 * the area we've been aked to render may overlap with area covered
1062 * by the region in any of the normal ways:
1064 * - it may begin and end within the area covered by the region
1065 * - it may start before and end after the area covered by region
1066 * - it may start before and end within the area covered by the region
1067 * - it may start within and end after the area covered by the region
1068 * - it may be precisely coincident with the area covered by region.
1070 * So let's start by determining the area covered by the region, in
1071 * window coordinates. It begins at zero (in item coordinates for this
1072 * waveview, and extends to region_length() / _samples_per_pixel.
1075 Rect self = item_to_window (Rect (0.0, 0.0, region_length() / _samples_per_pixel, _height));
1077 // cerr << name << " RENDER " << area << " self = " << self << endl;
1079 /* Now lets get the intersection with the area we've been asked to draw */
1081 boost::optional<Rect> d = self.intersection (area);
1087 Rect draw = d.get();
1089 /* "draw" is now a rectangle that defines the rectangle we need to
1090 * update/render the waveview into, in window coordinate space.
1093 /* window coordinates - pixels where x=0 is the left edge of the canvas
1094 * window. We round down in case we were asked to
1095 * draw "between" pixels at the start and/or end.
1098 double draw_start = floor (draw.x0);
1099 const double draw_end = floor (draw.x1);
1101 // cerr << "Need to draw " << draw_start << " .. " << draw_end << " vs. " << area << " and self = " << self << endl;
1103 /* image coordnates: pixels where x=0 is the start of this waveview,
1104 * wherever it may be positioned. thus image_start=N means "an image
1105 * that begins N pixels after the start of region that this waveview is
1109 const framepos_t image_start = window_to_image (self.x0, draw_start);
1110 const framepos_t image_end = window_to_image (self.x0, draw_end);
1112 // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl;
1114 /* sample coordinates - note, these are not subject to rounding error
1116 * "sample_start = N" means "the first sample we need to represent is N
1117 * samples after the first sample of the region"
1120 framepos_t sample_start = _region_start + (image_start * _samples_per_pixel);
1121 framepos_t sample_end = _region_start + (image_end * _samples_per_pixel);
1123 // cerr << "Sample space: " << sample_start << " .. " << sample_end << " @ " << _samples_per_pixel << " rs = " << _region_start << endl;
1125 /* sample_start and sample_end are bounded by the region
1126 * limits. sample_start, because of the was just computed, must already
1127 * be greater than or equal to the _region_start value.
1130 sample_end = min (region_end(), sample_end);
1132 // cerr << debug_name() << " will need image spanning " << sample_start << " .. " << sample_end << " region spans " << _region_start << " .. " << region_end() << endl;
1134 double image_origin_in_self_coordinates;
1135 boost::shared_ptr<WaveViewCache::Entry> image_to_draw;
1137 Glib::Threads::Mutex::Lock lci (current_image_lock);
1138 if (_current_image) {
1140 /* check it covers the right sample range */
1142 if (_current_image->start > sample_start || _current_image->end < sample_end) {
1143 /* doesn't cover the area we need ... reset */
1144 _current_image.reset ();
1146 /* timestamp our continuing use of this image/cache entry */
1147 images->use (_region->audio_source (_channel), _current_image);
1148 image_to_draw = _current_image;
1152 if (!image_to_draw) {
1157 image_to_draw = get_image (sample_start, sample_end, full_image);
1159 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 image to draw = %2 (full? %3)\n", name, image_to_draw, full_image));
1161 if (!image_to_draw) {
1162 /* image not currently available. A redraw will be scheduled
1169 /* found an image that covers our entire sample range,
1170 * so keep a reference to it.
1172 _current_image = image_to_draw;
1176 /* compute the first pixel of the image that should be used when we
1177 * render the specified range.
1180 image_origin_in_self_coordinates = (image_to_draw->start - _region_start) / _samples_per_pixel;
1182 if (_start_shift && (sample_start == _region_start) && (self.x0 == draw.x0)) {
1183 /* we are going to draw the first pixel for this region, but
1184 we may not want this to overlap a border around the
1185 waveform. If so, _start_shift will be set.
1187 //cerr << name.substr (23) << " ss = " << sample_start << " rs = " << _region_start << " sf = " << _start_shift << " ds = " << draw_start << " self = " << self << " draw = " << draw << endl;
1188 //draw_start += _start_shift;
1189 //image_origin_in_self_coordinates += _start_shift;
1192 /* the image may only be a best-effort ... it may not span the entire
1193 * range requested, though it is guaranteed to cover the start. So
1194 * determine how many pixels we can actually draw.
1199 if (image_to_draw != _current_image) {
1202 /* the image is guaranteed to start at or before
1203 * draw_start. But if it starts before draw_start, that reduces
1204 * the maximum available width we can render with.
1206 * so .. clamp the draw width to the smaller of what we need to
1207 * draw or the available width of the image.
1210 draw_width = min ((double) image_to_draw->image->get_width() - (draw_start - image_to_draw->start),
1211 (draw_end - draw_start));
1214 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 draw just %2 of %3 (iwidth %4 off %5 img @ %6 rs @ %7)\n", name, draw_width, (draw_end - draw_start),
1215 image_to_draw->image->get_width(), image_origin_in_self_coordinates,
1216 image_to_draw->start, _region_start));
1218 draw_width = draw_end - draw_start;
1219 DEBUG_TRACE (DEBUG::WaveView, string_compose ("use current image, span entire render width %1..%2\n", draw_start, draw_end));
1222 context->rectangle (draw_start, draw.y0, draw_width, draw.height());
1224 /* round image origin position to an exact pixel in device space to
1228 double x = self.x0 + image_origin_in_self_coordinates;
1230 context->user_to_device (x, y);
1233 context->device_to_user (x, y);
1235 /* the coordinates specify where in "user coordinates" (i.e. what we
1236 * generally call "canvas coordinates" in this code) the image origin
1237 * will appear. So specifying (10,10) will put the upper left corner of
1238 * the image at (10,10) in user space.
1241 context->set_source (image_to_draw->image, x, y);
1244 /* image obtained, some of it painted to display: we are rendered.
1245 Future calls to get_image_in_thread are now meaningful.
1252 WaveView::compute_bounding_box () const
1255 _bounding_box = Rect (0.0, 0.0, region_length() / _samples_per_pixel, _height);
1257 _bounding_box = boost::optional<Rect> ();
1260 _bounding_box_dirty = false;
1264 WaveView::set_height (Distance height)
1266 if (height != _height) {
1269 invalidate_image_cache ();
1271 get_image_in_thread = true;
1273 _bounding_box_dirty = true;
1279 WaveView::set_channel (int channel)
1281 if (channel != _channel) {
1284 invalidate_image_cache ();
1287 _bounding_box_dirty = true;
1293 WaveView::set_logscaled (bool yn)
1295 if (_logscaled != yn) {
1296 begin_visual_change ();
1297 invalidate_image_cache ();
1299 end_visual_change ();
1304 WaveView::gain_changed ()
1306 begin_visual_change ();
1307 invalidate_image_cache ();
1308 _region_amplitude = _region->scale_amplitude ();
1309 get_image_in_thread = true;
1310 end_visual_change ();
1314 WaveView::set_zero_color (Color c)
1316 if (_zero_color != c) {
1317 begin_visual_change ();
1318 invalidate_image_cache ();
1320 end_visual_change ();
1325 WaveView::set_clip_color (Color c)
1327 if (_clip_color != c) {
1328 begin_visual_change ();
1329 invalidate_image_cache ();
1331 end_visual_change ();
1336 WaveView::set_show_zero_line (bool yn)
1338 if (_show_zero != yn) {
1339 begin_visual_change ();
1340 invalidate_image_cache ();
1342 end_visual_change ();
1347 WaveView::set_shape (Shape s)
1350 begin_visual_change ();
1351 invalidate_image_cache ();
1353 end_visual_change ();
1358 WaveView::set_amplitude_above_axis (double a)
1360 if (fabs (_amplitude_above_axis - a) > 0.01) {
1361 begin_visual_change ();
1362 invalidate_image_cache ();
1363 _amplitude_above_axis = a;
1364 get_image_in_thread = true;
1365 end_visual_change ();
1370 WaveView::set_global_shape (Shape s)
1372 if (_global_shape != s) {
1375 images->clear_cache ();
1377 VisualPropertiesChanged (); /* EMIT SIGNAL */
1382 WaveView::set_global_logscaled (bool yn)
1384 if (_global_logscaled != yn) {
1385 _global_logscaled = yn;
1387 images->clear_cache ();
1389 VisualPropertiesChanged (); /* EMIT SIGNAL */
1394 WaveView::region_length() const
1396 return _region->length() - (_region_start - _region->start());
1400 WaveView::region_end() const
1402 return _region_start + region_length();
1406 WaveView::set_region_start (frameoffset_t start)
1412 if (_region_start == start) {
1417 _region_start = start;
1418 _bounding_box_dirty = true;
1423 WaveView::region_resized ()
1425 /* Called when the region start or end (thus length) has changed.
1433 _region_start = _region->start();
1434 _bounding_box_dirty = true;
1439 WaveView::set_global_gradient_depth (double depth)
1441 if (_global_gradient_depth != depth) {
1442 _global_gradient_depth = depth;
1443 VisualPropertiesChanged (); /* EMIT SIGNAL */
1448 WaveView::set_global_show_waveform_clipping (bool yn)
1450 if (_global_show_waveform_clipping != yn) {
1451 _global_show_waveform_clipping = yn;
1452 ClipLevelChanged ();
1457 WaveView::set_start_shift (double pixels)
1463 begin_visual_change ();
1464 _start_shift = pixels;
1465 end_visual_change ();
1469 WaveView::cancel_my_render_request () const
1475 /* try to stop any current rendering of the request, or prevent it from
1479 if (current_request) {
1480 current_request->cancel ();
1483 Glib::Threads::Mutex::Lock lm (request_queue_lock);
1485 /* now remove it from the queue and reset our request pointer so that
1486 have no outstanding request (that we know about)
1489 request_queue.erase (this);
1490 current_request.reset ();
1491 DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 now has no request %2\n", this));
1496 WaveView::set_image_cache_size (uint64_t sz)
1499 images = new WaveViewCache;
1502 images->set_image_cache_threshold (sz);
1505 /*-------------------------------------------------*/
1508 WaveView::start_drawing_thread ()
1510 if (!_drawing_thread) {
1511 _drawing_thread = Glib::Threads::Thread::create (sigc::ptr_fun (WaveView::drawing_thread));
1516 WaveView::stop_drawing_thread ()
1518 while (_drawing_thread) {
1519 Glib::Threads::Mutex::Lock lm (request_queue_lock);
1520 g_atomic_int_set (&drawing_thread_should_quit, 1);
1521 request_cond.signal ();
1526 WaveView::drawing_thread ()
1528 using namespace Glib::Threads;
1530 WaveView const * requestor;
1531 Mutex::Lock lm (request_queue_lock);
1536 /* remember that we hold the lock at this point, no matter what */
1538 if (g_atomic_int_get (&drawing_thread_should_quit)) {
1542 if (request_queue.empty()) {
1543 request_cond.wait (request_queue_lock);
1546 if (request_queue.empty()) {
1550 /* remove the request from the queue (remember: the "request"
1551 * is just a pointer to a WaveView object)
1554 requestor = *(request_queue.begin());
1555 request_queue.erase (request_queue.begin());
1557 DEBUG_TRACE (DEBUG::WaveView, string_compose ("start request for %1 at %2\n", requestor, g_get_monotonic_time()));
1559 boost::shared_ptr<WaveViewThreadRequest> req = requestor->current_request;
1565 /* Generate an image. Unlock the request queue lock
1566 * while we do this, so that other things can happen
1567 * as we do rendering.
1570 request_queue_lock.unlock (); /* some RAII would be good here */
1573 requestor->generate_image (req, true);
1575 req->image.clear(); /* just in case it was set before the exception, whatever it was */
1578 request_queue_lock.lock ();
1580 req.reset (); /* drop/delete request as appropriate */
1583 /* thread is vanishing */
1584 _drawing_thread = 0;
1587 /*-------------------------------------------------*/
1589 WaveViewCache::WaveViewCache ()
1590 : image_cache_size (0)
1591 , _image_cache_threshold (100 * 1048576) /* bytes */
1595 WaveViewCache::~WaveViewCache ()
1600 boost::shared_ptr<WaveViewCache::Entry>
1601 WaveViewCache::lookup_image (boost::shared_ptr<ARDOUR::AudioSource> src,
1602 framepos_t start, framepos_t end,
1607 double samples_per_pixel,
1608 bool& full_coverage)
1610 ImageCache::iterator x;
1612 if ((x = cache_map.find (src)) == cache_map.end ()) {
1613 /* nothing in the cache for this audio source at all */
1614 return boost::shared_ptr<WaveViewCache::Entry> ();
1617 CacheLine& caches = x->second;
1618 boost::shared_ptr<Entry> best_partial;
1619 framecnt_t max_coverage = 0;
1621 /* Find a suitable ImageSurface, if it exists.
1624 for (CacheLine::iterator c = caches.begin(); c != caches.end(); ++c) {
1626 boost::shared_ptr<Entry> e (*c);
1628 if (channel != e->channel
1629 || height != e->height
1630 || amplitude != e->amplitude
1631 || samples_per_pixel != e->samples_per_pixel
1632 || fill_color != e->fill_color) {
1636 switch (Evoral::coverage (start, end, e->start, e->end)) {
1637 case Evoral::OverlapExternal: /* required range is inside image range */
1638 DEBUG_TRACE (DEBUG::WaveView, string_compose ("found image spanning %1..%2 covers %3..%4\n",
1639 e->start, e->end, start, end));
1641 full_coverage = true;
1644 case Evoral::OverlapStart: /* required range start is covered by image range */
1645 if ((e->end - start) > max_coverage) {
1647 max_coverage = e->end - start;
1651 case Evoral::OverlapNone:
1652 case Evoral::OverlapEnd:
1653 case Evoral::OverlapInternal:
1659 DEBUG_TRACE (DEBUG::WaveView, string_compose ("found PARTIAL image spanning %1..%2 partially covers %3..%4\n",
1660 best_partial->start, best_partial->end, start, end));
1661 use (src, best_partial);
1662 full_coverage = false;
1663 return best_partial;
1666 return boost::shared_ptr<Entry> ();
1670 WaveViewCache::consolidate_image_cache (boost::shared_ptr<ARDOUR::AudioSource> src,
1675 double samples_per_pixel)
1677 list <uint32_t> deletion_list;
1678 uint32_t other_entries = 0;
1679 ImageCache::iterator x;
1681 /* MUST BE CALLED FROM (SINGLE) GUI THREAD */
1683 if ((x = cache_map.find (src)) == cache_map.end ()) {
1687 CacheLine& caches = x->second;
1689 for (CacheLine::iterator c1 = caches.begin(); c1 != caches.end(); ) {
1691 CacheLine::iterator nxt = c1;
1694 boost::shared_ptr<Entry> e1 (*c1);
1696 if (channel != e1->channel
1697 || height != e1->height
1698 || amplitude != e1->amplitude
1699 || samples_per_pixel != e1->samples_per_pixel
1700 || fill_color != e1->fill_color) {
1702 /* doesn't match current properties, ignore and move on
1711 /* c1 now points to a cached image entry that matches current
1712 * properties. Check all subsequent cached imaged entries to
1713 * see if there are others that also match but represent
1714 * subsets of the range covered by this one.
1717 for (CacheLine::iterator c2 = c1; c2 != caches.end(); ) {
1719 CacheLine::iterator nxt2 = c2;
1722 boost::shared_ptr<Entry> e2 (*c2);
1724 if (e1 == e2 || channel != e2->channel
1725 || height != e2->height
1726 || amplitude != e2->amplitude
1727 || samples_per_pixel != e2->samples_per_pixel
1728 || fill_color != e2->fill_color) {
1730 /* properties do not match, ignore for the
1731 * purposes of consolidation.
1737 if (e2->start >= e1->start && e2->end <= e1->end) {
1738 /* c2 is fully contained by c1, so delete it */
1741 /* and re-start the whole iteration */
1742 nxt = caches.begin ();
1754 WaveViewCache::use (boost::shared_ptr<ARDOUR::AudioSource> src, boost::shared_ptr<Entry> ce)
1756 ce->timestamp = g_get_monotonic_time ();
1760 WaveViewCache::add (boost::shared_ptr<ARDOUR::AudioSource> src, boost::shared_ptr<Entry> ce)
1762 /* MUST BE CALLED FROM (SINGLE) GUI THREAD */
1764 Cairo::RefPtr<Cairo::ImageSurface> img (ce->image);
1766 image_cache_size += img->get_height() * img->get_width () * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */
1772 ce->timestamp = g_get_monotonic_time ();
1774 cache_map[src].push_back (ce);
1778 WaveViewCache::compute_image_cache_size()
1781 for (ImageCache::iterator s = cache_map.begin(); s != cache_map.end(); ++s) {
1782 CacheLine& per_source_cache (s->second);
1783 for (CacheLine::iterator c = per_source_cache.begin(); c != per_source_cache.end(); ++c) {
1784 Cairo::RefPtr<Cairo::ImageSurface> img ((*c)->image);
1785 total += img->get_height() * img->get_width() * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */
1792 WaveViewCache::cache_full()
1794 return image_cache_size > _image_cache_threshold;
1798 WaveViewCache::cache_flush ()
1800 /* Build a sortable list of all cache entries */
1802 CacheList cache_list;
1804 for (ImageCache::const_iterator cm = cache_map.begin(); cm != cache_map.end(); ++cm) {
1805 for (CacheLine::const_iterator cl = cm->second.begin(); cl != cm->second.end(); ++cl) {
1806 cache_list.push_back (make_pair (cm->first, *cl));
1810 /* sort list in LRU order */
1811 SortByTimestamp sorter;
1812 sort (cache_list.begin(), cache_list.end(), sorter);
1814 while (image_cache_size > _image_cache_threshold && !cache_map.empty() && !cache_list.empty()) {
1816 ListEntry& le (cache_list.front());
1818 ImageCache::iterator x;
1820 if ((x = cache_map.find (le.first)) != cache_map.end ()) {
1822 CacheLine& cl = x->second;
1824 for (CacheLine::iterator c = cl.begin(); c != cl.end(); ++c) {
1826 if (*c == le.second) {
1828 DEBUG_TRACE (DEBUG::WaveView, string_compose ("Removing cache line entry for %1\n", x->first->name()));
1830 /* Remove this entry from this cache line */
1834 /* remove cache line from main cache: no more entries */
1835 cache_map.erase (x);
1842 Cairo::RefPtr<Cairo::ImageSurface> img (le.second->image);
1843 uint64_t size = img->get_height() * img->get_width() * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */
1845 if (image_cache_size > size) {
1846 image_cache_size -= size;
1848 image_cache_size = 0;
1850 DEBUG_TRACE (DEBUG::WaveView, string_compose ("cache shrunk to %1\n", image_cache_size));
1853 /* Remove from the linear list, even if we didn't find it in
1854 * the actual cache_mao
1856 cache_list.erase (cache_list.begin());
1861 WaveViewCache::clear_cache ()
1863 DEBUG_TRACE (DEBUG::WaveView, "clear cache\n");
1864 const uint64_t image_cache_threshold = _image_cache_threshold;
1865 _image_cache_threshold = 0;
1867 _image_cache_threshold = image_cache_threshold;
1871 WaveViewCache::set_image_cache_threshold (uint64_t sz)
1873 DEBUG_TRACE (DEBUG::WaveView, string_compose ("new image cache size %1\n", sz));
1874 _image_cache_threshold = sz;