clear waveform cache when shape changes - fixes #6525
[ardour.git] / libs / canvas / canvas / wave_view.h
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
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
21 #include <boost/shared_ptr.hpp>
22 #include <boost/shared_array.hpp>
23 #include <boost/scoped_array.hpp>
24
25 #include "pbd/properties.h"
26
27 #include "ardour/types.h"
28
29 #include <glibmm/refptr.h>
30
31 #include "canvas/visibility.h"
32 #include "canvas/item.h"
33 #include "canvas/fill.h"
34 #include "canvas/outline.h"
35
36 namespace ARDOUR {
37         class AudioRegion;
38 }
39
40 namespace Gdk {
41         class Pixbuf;
42 }
43
44 class WaveViewTest;
45         
46 namespace ArdourCanvas {
47
48 struct LIBCANVAS_API WaveViewThreadRequest
49 {
50   public:
51         enum RequestType {
52                 Quit,
53                 Cancel,
54                 Draw
55         };
56         
57         WaveViewThreadRequest  () : stop (0) {}
58         
59         bool should_stop () const { return (bool) g_atomic_int_get (const_cast<gint*>(&stop)); }
60         void cancel() { g_atomic_int_set (&stop, 1); }
61         
62         RequestType type;
63         framepos_t start;
64         framepos_t end;
65         double     width;
66         double     height;
67         double     samples_per_pixel;
68         uint16_t   channel;
69         double     amplitude;
70         Color      fill_color;
71         boost::weak_ptr<const ARDOUR::Region> region;
72
73         /* resulting image, after request has been satisfied */
74         
75         Cairo::RefPtr<Cairo::ImageSurface> image;
76         
77   private:
78         gint stop; /* intended for atomic access */
79 };
80
81 class LIBCANVAS_API WaveView;
82
83 class LIBCANVAS_API WaveViewCache
84 {
85   public:
86         WaveViewCache();
87         ~WaveViewCache();
88         
89         struct Entry {
90
91                 /* these properties define the cache entry as unique.
92
93                    If an image is in use by a WaveView and any of these
94                    properties are modified on the WaveView, the image can no
95                    longer be used (or may no longer be usable for start/end
96                    parameters). It will remain in the cache until flushed for
97                    some reason (typically the cache is full).
98                 */
99
100                 int channel;
101                 Coord height;
102                 float amplitude;
103                 Color fill_color;
104                 double samples_per_pixel;
105                 framepos_t start;
106                 framepos_t end;
107
108                 /* the actual image referred to by the cache entry */
109
110                 Cairo::RefPtr<Cairo::ImageSurface> image;
111
112                 /* last time the cache entry was used */
113                 uint64_t timestamp;
114                 
115                 Entry (int chan, Coord hght, float amp, Color fcl, double spp, framepos_t strt, framepos_t ed,
116                        Cairo::RefPtr<Cairo::ImageSurface> img) 
117                         : channel (chan)
118                         , height (hght)
119                         , amplitude (amp)
120                         , fill_color (fcl)
121                         , samples_per_pixel (spp)
122                         , start (strt)
123                         , end (ed)
124                         , image (img) {}
125         };
126
127         uint64_t image_cache_threshold () const { return _image_cache_threshold; }
128         void set_image_cache_threshold (uint64_t);
129         void clear_cache ();
130         
131         void add (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
132         void use (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
133         
134         void consolidate_image_cache (boost::shared_ptr<ARDOUR::AudioSource>,
135                                       int channel,
136                                       Coord height,
137                                       float amplitude,
138                                       Color fill_color,
139                                       double samples_per_pixel);
140
141         boost::shared_ptr<Entry> lookup_image (boost::shared_ptr<ARDOUR::AudioSource>,
142                                                framepos_t start, framepos_t end,
143                                                int _channel,
144                                                Coord height,
145                                                float amplitude,
146                                                Color fill_color,
147                                                double samples_per_pixel,
148                                                bool& full_image);
149
150   private:
151         /* an unsorted, unindexd collection of cache entries associated with
152            a particular AudioSource. All cache Entries in the collection
153            share the AudioSource in common, but represent different parameter
154            settings (e.g. height, color, samples per pixel etc.)
155         */
156         typedef std::vector<boost::shared_ptr<Entry> > CacheLine;
157         /* Indexed, non-sortable structure used to lookup images associated
158          * with a particular AudioSource
159          */
160         typedef std::map <boost::shared_ptr<ARDOUR::AudioSource>,CacheLine> ImageCache;
161         ImageCache cache_map;
162
163         /* Linear, sortable structure used when we need to do a timestamp-based
164          * flush of entries from the cache.
165          */
166         typedef std::pair<boost::shared_ptr<ARDOUR::AudioSource>,boost::shared_ptr<Entry> > ListEntry;
167         typedef std::vector<ListEntry> CacheList;
168  
169         struct SortByTimestamp {
170                 bool operator() (const WaveViewCache::ListEntry& a, const WaveViewCache::ListEntry& b) {
171                         return a.second->timestamp < b.second->timestamp;
172                 }
173         };
174         friend struct SortByTimestamp;
175         
176         uint64_t image_cache_size;
177         uint64_t _image_cache_threshold;
178
179         uint64_t compute_image_cache_size ();
180         void cache_flush ();
181         bool cache_full ();
182 };
183
184 class LIBCANVAS_API WaveView : public Item, public sigc::trackable
185 {
186 public:
187
188         enum Shape { 
189                 Normal,
190                 Rectified
191         };
192
193         std::string debug_name() const;
194
195         /* final ImageSurface rendered with colours */
196
197         Cairo::RefPtr<Cairo::ImageSurface> _image;
198         PBD::Signal0<void> ImageReady;
199         
200         /* Displays a single channel of waveform data for the given Region.
201
202            x = 0 in the waveview corresponds to the first waveform datum taken
203            from region->start() samples into the source data.
204            
205            x = N in the waveview corresponds to the (N * spp)'th sample 
206            measured from region->start() into the source data.
207            
208            when drawing, we will map the zeroth-pixel of the waveview
209            into a window. 
210            
211            The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces
212            that cache sections of the display. This is filled on-demand and
213            never cleared until something explicitly marks the cache invalid
214            (such as a change in samples_per_pixel, the log scaling, rectified or
215            other view parameters).
216         */
217
218         WaveView (Canvas *, boost::shared_ptr<ARDOUR::AudioRegion>);
219         WaveView (Item*, boost::shared_ptr<ARDOUR::AudioRegion>);
220        ~WaveView ();
221
222         void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
223         void compute_bounding_box () const;
224     
225         void set_samples_per_pixel (double);
226         void set_height (Distance);
227         void set_channel (int);
228         void set_region_start (ARDOUR::frameoffset_t);
229
230         /** Change the first position drawn by @param pixels.
231          * @param pixels must be positive. This is used by
232          * AudioRegionViews in Ardour to avoid drawing the
233          * first pixel of a waveform, and exists in case
234          * there are uses for WaveView where we do not
235          * want this behaviour.
236          */
237         void set_start_shift (double pixels);
238         
239         void set_fill_color (Color);
240         void set_outline_color (Color);
241         
242         void region_resized ();
243         void gain_changed ();
244
245         void set_show_zero_line (bool);
246         bool show_zero_line() const { return _show_zero; }
247         void set_zero_color (Color);
248         void set_clip_color (Color);
249         void set_logscaled (bool);
250         void set_gradient_depth (double);
251         double gradient_depth() const { return _gradient_depth; }
252         void set_shape (Shape);
253
254         void set_always_get_image_in_thread (bool yn);
255         
256         /* currently missing because we don't need them (yet):
257            set_shape_independent();
258            set_logscaled_independent()
259         */
260
261         static void set_global_gradient_depth (double);
262         static void set_global_logscaled (bool);
263         static void set_global_shape (Shape);
264         static void set_global_show_waveform_clipping (bool);
265     
266         static double  global_gradient_depth()  { return _global_gradient_depth; }
267         static bool    global_logscaled()  { return _global_logscaled; }
268         static Shape   global_shape()  { return _global_shape; }
269
270         void set_amplitude_above_axis (double v);
271         double amplitude_above_axis () const { return _amplitude_above_axis; }
272
273         static void set_clip_level (double dB);
274         static PBD::Signal0<void> ClipLevelChanged;
275
276         static void start_drawing_thread ();
277         static void stop_drawing_thread ();
278
279         static void set_image_cache_size (uint64_t);
280         
281 #ifdef CANVAS_COMPATIBILITY     
282         void*& property_gain_src () {
283                 return _foo_void;
284         }
285         void*& property_gain_function () {
286                 return _foo_void;
287         }
288   private:
289         void* _foo_void;
290
291 #endif
292
293   private:
294         friend class ::WaveViewTest;
295         friend class WaveViewThreadClient;
296
297         void invalidate_image_cache ();
298
299         boost::shared_ptr<ARDOUR::AudioRegion> _region;
300         int    _channel;
301         double _samples_per_pixel;
302         Coord  _height;
303         bool   _show_zero;
304         Color  _zero_color;
305         Color  _clip_color;
306         bool   _logscaled;
307         Shape  _shape;
308         double _gradient_depth;
309         bool   _shape_independent;
310         bool   _logscaled_independent;
311         bool   _gradient_depth_independent;
312         double _amplitude_above_axis;
313         float  _region_amplitude;
314         double _start_shift;
315         
316         /** The `start' value to use for the region; we can't use the region's
317          *  value as the crossfade editor needs to alter it.
318          */
319         ARDOUR::frameoffset_t _region_start;
320
321         /** Under almost conditions, this is going to return _region->length(),
322          * but if _region_start has been reset, then we need
323          * to use this modified computation.
324          */
325         ARDOUR::framecnt_t region_length() const;
326         /** Under almost conditions, this is going to return _region->start() +
327          * _region->length(), but if _region_start has been reset, then we need
328          * to use this modified computation.
329          */
330         ARDOUR::framepos_t region_end() const;
331
332         /** If true, calls to get_image() will render a missing wave image
333            in the calling thread. Generally set to false, but true after a
334            call to set_height().
335         */
336         mutable bool get_image_in_thread;
337
338         /** If true, calls to get_image() will render a missing wave image
339            in the calling thread. Set true for waveviews we expect to 
340            keep updating (e.g. while recording)
341         */
342         bool always_get_image_in_thread;
343         
344         /** Set to true by render(). Used so that we know if the wave view
345          * has actually been displayed on screen. ::set_height() when this
346          * is true does not use get_image_in_thread, because it implies
347          * that the height is being set BEFORE the waveview is drawn.
348          */
349         mutable bool rendered;
350         
351         PBD::ScopedConnectionList invalidation_connection;
352         PBD::ScopedConnection     image_ready_connection;
353
354         static double _global_gradient_depth;
355         static bool   _global_logscaled;
356         static Shape  _global_shape;
357         static bool   _global_show_waveform_clipping;
358         static double _clip_level;
359
360         static PBD::Signal0<void> VisualPropertiesChanged;
361
362         void handle_visual_property_change ();
363         void handle_clip_level_change ();
364
365         boost::shared_ptr<WaveViewCache::Entry> get_image (framepos_t start, framepos_t end, bool& full_image) const;
366         boost::shared_ptr<WaveViewCache::Entry> get_image_from_cache (framepos_t start, framepos_t end, bool& full_image) const;
367         
368         struct LineTips {
369                 double top;
370                 double bot;
371                 double spread;
372                 bool clip_max;
373                 bool clip_min;
374                 
375                 LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
376         };
377
378         ArdourCanvas::Coord y_extent (double) const;
379         void compute_tips (ARDOUR::PeakData const & peak, LineTips& tips) const;
380
381         ARDOUR::framecnt_t desired_image_width () const;
382
383         void draw_image (Cairo::RefPtr<Cairo::ImageSurface>&, ARDOUR::PeakData*, int n_peaks, boost::shared_ptr<WaveViewThreadRequest>) const;
384         void draw_absent_image (Cairo::RefPtr<Cairo::ImageSurface>&, ARDOUR::PeakData*, int) const;
385         
386         void cancel_my_render_request () const;
387
388         void queue_get_image (boost::shared_ptr<const ARDOUR::Region> region, framepos_t start, framepos_t end) const;
389         void generate_image (boost::shared_ptr<WaveViewThreadRequest>, bool in_render_thread) const;
390         boost::shared_ptr<WaveViewCache::Entry> cache_request_result (boost::shared_ptr<WaveViewThreadRequest> req) const;
391         
392         void image_ready ();
393         
394         mutable boost::shared_ptr<WaveViewCache::Entry> _current_image;
395         
396         mutable boost::shared_ptr<WaveViewThreadRequest> current_request;
397         
398         static WaveViewCache* images;
399
400         static void drawing_thread ();
401
402         static gint drawing_thread_should_quit;
403         static Glib::Threads::Mutex request_queue_lock;
404         static Glib::Threads::Cond request_cond;
405         static Glib::Threads::Thread* _drawing_thread;
406         typedef std::set<WaveView const *> DrawingRequestQueue;
407         static DrawingRequestQueue request_queue;
408 };
409
410 }