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.
21 #include <boost/shared_ptr.hpp>
22 #include <boost/shared_array.hpp>
23 #include <boost/scoped_array.hpp>
25 #include "pbd/properties.h"
27 #include "ardour/types.h"
29 #include <glibmm/refptr.h>
31 #include "canvas/visibility.h"
32 #include "canvas/item.h"
33 #include "canvas/fill.h"
34 #include "canvas/outline.h"
46 namespace ArdourCanvas {
48 struct LIBCANVAS_API WaveViewThreadRequest
57 WaveViewThreadRequest () : stop (0) {}
59 bool should_stop () const { return (bool) g_atomic_int_get (const_cast<gint*>(&stop)); }
60 void cancel() { g_atomic_int_set (&stop, 1); }
67 double samples_per_pixel;
71 boost::weak_ptr<const ARDOUR::Region> region;
73 /* resulting image, after request has been satisfied */
75 Cairo::RefPtr<Cairo::ImageSurface> image;
78 gint stop; /* intended for atomic access */
81 class LIBCANVAS_API WaveView;
83 class LIBCANVAS_API WaveViewCache
91 /* these properties define the cache entry as unique.
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).
104 double samples_per_pixel;
108 /* the actual image referred to by the cache entry */
110 Cairo::RefPtr<Cairo::ImageSurface> image;
112 /* last time the cache entry was used */
115 Entry (int chan, Coord hght, float amp, Color fcl, double spp, framepos_t strt, framepos_t ed,
116 Cairo::RefPtr<Cairo::ImageSurface> img)
121 , samples_per_pixel (spp)
127 uint64_t image_cache_threshold () const { return _image_cache_threshold; }
128 void set_image_cache_threshold (uint64_t);
131 void add (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
132 void use (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
134 void consolidate_image_cache (boost::shared_ptr<ARDOUR::AudioSource>,
139 double samples_per_pixel);
141 boost::shared_ptr<Entry> lookup_image (boost::shared_ptr<ARDOUR::AudioSource>,
142 framepos_t start, framepos_t end,
147 double samples_per_pixel,
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.)
156 typedef std::vector<boost::shared_ptr<Entry> > CacheLine;
157 /* Indexed, non-sortable structure used to lookup images associated
158 * with a particular AudioSource
160 typedef std::map <boost::shared_ptr<ARDOUR::AudioSource>,CacheLine> ImageCache;
161 ImageCache cache_map;
163 /* Linear, sortable structure used when we need to do a timestamp-based
164 * flush of entries from the cache.
166 typedef std::pair<boost::shared_ptr<ARDOUR::AudioSource>,boost::shared_ptr<Entry> > ListEntry;
167 typedef std::vector<ListEntry> CacheList;
169 struct SortByTimestamp {
170 bool operator() (const WaveViewCache::ListEntry& a, const WaveViewCache::ListEntry& b) {
171 return a.second->timestamp < b.second->timestamp;
174 friend struct SortByTimestamp;
176 uint64_t image_cache_size;
177 uint64_t _image_cache_threshold;
179 uint64_t compute_image_cache_size ();
184 class LIBCANVAS_API WaveView : public Item, public sigc::trackable
193 std::string debug_name() const;
195 /* final ImageSurface rendered with colours */
197 Cairo::RefPtr<Cairo::ImageSurface> _image;
198 PBD::Signal0<void> ImageReady;
200 /* Displays a single channel of waveform data for the given Region.
202 x = 0 in the waveview corresponds to the first waveform datum taken
203 from region->start() samples into the source data.
205 x = N in the waveview corresponds to the (N * spp)'th sample
206 measured from region->start() into the source data.
208 when drawing, we will map the zeroth-pixel of the waveview
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).
218 WaveView (Canvas *, boost::shared_ptr<ARDOUR::AudioRegion>);
219 WaveView (Item*, boost::shared_ptr<ARDOUR::AudioRegion>);
222 void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
223 void compute_bounding_box () const;
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);
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.
237 void set_start_shift (double pixels);
239 void set_fill_color (Color);
240 void set_outline_color (Color);
242 void region_resized ();
243 void gain_changed ();
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);
254 void set_always_get_image_in_thread (bool yn);
256 /* currently missing because we don't need them (yet):
257 set_shape_independent();
258 set_logscaled_independent()
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);
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; }
270 void set_amplitude_above_axis (double v);
271 double amplitude_above_axis () const { return _amplitude_above_axis; }
273 static void set_clip_level (double dB);
274 static PBD::Signal0<void> ClipLevelChanged;
276 static void start_drawing_thread ();
277 static void stop_drawing_thread ();
279 static void set_image_cache_size (uint64_t);
281 #ifdef CANVAS_COMPATIBILITY
282 void*& property_gain_src () {
285 void*& property_gain_function () {
294 friend class ::WaveViewTest;
295 friend class WaveViewThreadClient;
297 void invalidate_image_cache ();
299 boost::shared_ptr<ARDOUR::AudioRegion> _region;
301 double _samples_per_pixel;
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;
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.
319 ARDOUR::frameoffset_t _region_start;
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.
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.
330 ARDOUR::framepos_t region_end() const;
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().
336 mutable bool get_image_in_thread;
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)
342 bool always_get_image_in_thread;
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.
349 mutable bool rendered;
351 PBD::ScopedConnectionList invalidation_connection;
352 PBD::ScopedConnection image_ready_connection;
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;
360 static PBD::Signal0<void> VisualPropertiesChanged;
362 void handle_visual_property_change ();
363 void handle_clip_level_change ();
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;
375 LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {}
378 ArdourCanvas::Coord y_extent (double) const;
379 void compute_tips (ARDOUR::PeakData const & peak, LineTips& tips) const;
381 ARDOUR::framecnt_t desired_image_width () const;
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;
386 void cancel_my_render_request () const;
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;
394 mutable boost::shared_ptr<WaveViewCache::Entry> _current_image;
396 mutable boost::shared_ptr<WaveViewThreadRequest> current_request;
398 static WaveViewCache* images;
400 static void drawing_thread ();
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;