drop in threaded waveview changes via the two affected files.
[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 (&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     region_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         
130         void add (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
131         void use (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
132         
133         void consolidate_image_cache (boost::shared_ptr<ARDOUR::AudioSource>,
134                                       int channel,
135                                       Coord height,
136                                       float amplitude,
137                                       Color fill_color,
138                                       double samples_per_pixel);
139
140         boost::shared_ptr<Entry> lookup_image (boost::shared_ptr<ARDOUR::AudioSource>,
141                                                framepos_t start, framepos_t end,
142                                                int _channel,
143                                                Coord height,
144                                                float amplitude,
145                                                Color fill_color,
146                                                double samples_per_pixel);
147
148   private:
149         /* an unsorted, unindexd collection of cache entries associated with
150            a particular AudioSource. All cache Entries in the collection
151            share the AudioSource in common, but represent different parameter
152            settings (e.g. height, color, samples per pixel etc.)
153         */
154         typedef std::vector<boost::shared_ptr<Entry> > CacheLine;
155         /* Indexed, non-sortable structure used to lookup images associated
156          * with a particular AudioSource
157          */
158         typedef std::map <boost::shared_ptr<ARDOUR::AudioSource>,CacheLine> ImageCache;
159         ImageCache cache_map;
160
161         /* Linear, sortable structure used when we need to do a timestamp-based
162          * flush of entries from the cache.
163          */
164         typedef std::pair<boost::shared_ptr<ARDOUR::AudioSource>,boost::shared_ptr<Entry> > ListEntry;
165         typedef std::vector<ListEntry> CacheList;
166         CacheList cache_list;
167  
168         struct SortByTimestamp {
169                 bool operator() (const WaveViewCache::ListEntry& a, const WaveViewCache::ListEntry& b) {
170                         return a.second->timestamp < b.second->timestamp;
171                 }
172         };
173         friend struct SortByTimestamp;
174         
175         uint64_t image_cache_size;
176         uint64_t _image_cache_threshold;
177
178         uint64_t compute_image_cache_size ();
179         void cache_flush ();
180         bool cache_full ();
181 };
182
183 class LIBCANVAS_API WaveView : public Item, public sigc::trackable
184 {
185 public:
186
187         enum Shape { 
188                 Normal,
189                 Rectified
190         };
191
192         std::string debug_name() const;
193
194         /* final ImageSurface rendered with colours */
195
196         Cairo::RefPtr<Cairo::ImageSurface> _image;
197         PBD::Signal0<void> ImageReady;
198         
199         /* Displays a single channel of waveform data for the given Region.
200
201            x = 0 in the waveview corresponds to the first waveform datum taken
202            from region->start() samples into the source data.
203            
204            x = N in the waveview corresponds to the (N * spp)'th sample 
205            measured from region->start() into the source data.
206            
207            when drawing, we will map the zeroth-pixel of the waveview
208            into a window. 
209            
210            The waveview itself contains a set of pre-rendered Cairo::ImageSurfaces
211            that cache sections of the display. This is filled on-demand and
212            never cleared until something explicitly marks the cache invalid
213            (such as a change in samples_per_pixel, the log scaling, rectified or
214            other view parameters).
215         */
216
217         WaveView (Canvas *, boost::shared_ptr<ARDOUR::AudioRegion>);
218         WaveView (Item*, boost::shared_ptr<ARDOUR::AudioRegion>);
219        ~WaveView ();
220
221         void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
222         void compute_bounding_box () const;
223     
224         void set_samples_per_pixel (double);
225         void set_height (Distance);
226         void set_channel (int);
227         void set_region_start (ARDOUR::frameoffset_t);
228
229         /** Change the first position drawn by @param pixels.
230          * @param pixels must be positive. This is used by
231          * AudioRegionViews in Ardour to avoid drawing the
232          * first pixel of a waveform, and exists in case
233          * there are uses for WaveView where we do not
234          * want this behaviour.
235          */
236         void set_start_shift (double pixels);
237         
238         void set_fill_color (Color);
239         void set_outline_color (Color);
240         
241         void region_resized ();
242         void gain_changed ();
243
244         void set_show_zero_line (bool);
245         bool show_zero_line() const { return _show_zero; }
246         void set_zero_color (Color);
247         void set_clip_color (Color);
248         void set_logscaled (bool);
249         void set_gradient_depth (double);
250         double gradient_depth() const { return _gradient_depth; }
251         void set_shape (Shape);
252
253         /* currently missing because we don't need them (yet):
254            set_shape_independent();
255            set_logscaled_independent()
256         */
257
258         static void set_global_gradient_depth (double);
259         static void set_global_logscaled (bool);
260         static void set_global_shape (Shape);
261         static void set_global_show_waveform_clipping (bool);
262     
263         static double  global_gradient_depth()  { return _global_gradient_depth; }
264         static bool    global_logscaled()  { return _global_logscaled; }
265         static Shape   global_shape()  { return _global_shape; }
266
267         void set_amplitude_above_axis (double v);
268         double amplitude_above_axis () const { return _amplitude_above_axis; }
269
270         static void set_clip_level (double dB);
271         static PBD::Signal0<void> ClipLevelChanged;
272
273         static void start_drawing_thread ();
274         static void stop_drawing_thread ();
275
276 #ifdef CANVAS_COMPATIBILITY     
277         void*& property_gain_src () {
278                 return _foo_void;
279         }
280         void*& property_gain_function () {
281                 return _foo_void;
282         }
283   private:
284         void* _foo_void;
285
286 #endif
287
288   private:
289         friend class ::WaveViewTest;
290         friend class WaveViewThreadClient;
291
292         void invalidate_image_cache ();
293
294         boost::shared_ptr<ARDOUR::AudioRegion> _region;
295         int    _channel;
296         double _samples_per_pixel;
297         Coord  _height;
298         bool   _show_zero;
299         Color  _zero_color;
300         Color  _clip_color;
301         bool   _logscaled;
302         Shape  _shape;
303         double _gradient_depth;
304         bool   _shape_independent;
305         bool   _logscaled_independent;
306         bool   _gradient_depth_independent;
307         double _amplitude_above_axis;
308         float  _region_amplitude;
309         double _start_shift;
310         
311         /** The `start' value to use for the region; we can't use the region's
312          *  value as the crossfade editor needs to alter it.
313          */
314         ARDOUR::frameoffset_t _region_start;
315
316         /** Under almost conditions, this is going to return _region->length(),
317          * but if _region_start has been reset, then we need
318          * to use this modified computation.
319          */
320         ARDOUR::framecnt_t region_length() const;
321         /** Under almost conditions, this is going to return _region->start() +
322          * _region->length(), but if _region_start has been reset, then we need
323          * to use this modified computation.
324          */
325         ARDOUR::framepos_t region_end() const;
326
327         /** If true, calls to get_image() will render a missing wave image
328            in the calling thread. Generally set to false, but true after a
329            call to set_height().
330         */
331         mutable bool get_image_in_thread;
332
333         /** Set to true by render(). Used so that we know if the wave view
334          * has actually been displayed on screen. ::set_height() when this
335          * is true does not use get_image_in_thread, because it implies
336          * that the height is being set BEFORE the waveview is drawn.
337          */
338         mutable bool rendered;
339         
340         PBD::ScopedConnectionList invalidation_connection;
341         PBD::ScopedConnection     image_ready_connection;
342
343         static double _global_gradient_depth;
344         static bool   _global_logscaled;
345         static Shape  _global_shape;
346         static bool   _global_show_waveform_clipping;
347         static double _clip_level;
348
349         static PBD::Signal0<void> VisualPropertiesChanged;
350
351         void handle_visual_property_change ();
352         void handle_clip_level_change ();
353
354         boost::shared_ptr<WaveViewCache::Entry> get_image (framepos_t start, framepos_t end) const;
355         boost::shared_ptr<WaveViewCache::Entry> get_image_from_cache (framepos_t start, framepos_t end) const;
356         
357         ArdourCanvas::Coord y_extent (double, bool) const;
358         void draw_image (Cairo::RefPtr<Cairo::ImageSurface>&, ARDOUR::PeakData*, int n_peaks, boost::shared_ptr<WaveViewThreadRequest>) const;
359         void draw_absent_image (Cairo::RefPtr<Cairo::ImageSurface>&, ARDOUR::PeakData*, int) const;
360         
361         void cancel_my_render_request () const;
362
363         void queue_get_image (boost::shared_ptr<const ARDOUR::Region> region, framepos_t start, framepos_t end) const;
364         void generate_image (boost::shared_ptr<WaveViewThreadRequest>, bool in_render_thread) const;
365         boost::shared_ptr<WaveViewCache::Entry> cache_request_result (boost::shared_ptr<WaveViewThreadRequest> req) const;
366         
367         void image_ready ();
368         
369         mutable boost::shared_ptr<WaveViewCache::Entry> _current_image;
370         
371         mutable boost::shared_ptr<WaveViewThreadRequest> current_request;
372         void send_request (boost::shared_ptr<WaveViewThreadRequest>) const;
373         bool idle_send_request (boost::shared_ptr<WaveViewThreadRequest>) const;
374         
375         static WaveViewCache* images;
376
377         static void drawing_thread ();
378
379         static gint drawing_thread_should_quit;
380         static Glib::Threads::Mutex request_queue_lock;
381         static Glib::Threads::Cond request_cond;
382         static Glib::Threads::Thread* _drawing_thread;
383         typedef std::set<WaveView const *> DrawingRequestQueue;
384         static DrawingRequestQueue request_queue;
385 };
386
387 }