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 (&stop); }
60 void cancel() { g_atomic_int_set (&stop, 1); }
67 double samples_per_pixel;
69 double region_amplitude;
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);
130 void add (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
131 void use (boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<Entry>);
133 void consolidate_image_cache (boost::shared_ptr<ARDOUR::AudioSource>,
138 double samples_per_pixel);
140 boost::shared_ptr<Entry> lookup_image (boost::shared_ptr<ARDOUR::AudioSource>,
141 framepos_t start, framepos_t end,
146 double samples_per_pixel);
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.)
154 typedef std::vector<boost::shared_ptr<Entry> > CacheLine;
155 /* Indexed, non-sortable structure used to lookup images associated
156 * with a particular AudioSource
158 typedef std::map <boost::shared_ptr<ARDOUR::AudioSource>,CacheLine> ImageCache;
159 ImageCache cache_map;
161 /* Linear, sortable structure used when we need to do a timestamp-based
162 * flush of entries from the cache.
164 typedef std::pair<boost::shared_ptr<ARDOUR::AudioSource>,boost::shared_ptr<Entry> > ListEntry;
165 typedef std::vector<ListEntry> CacheList;
166 CacheList cache_list;
168 struct SortByTimestamp {
169 bool operator() (const WaveViewCache::ListEntry& a, const WaveViewCache::ListEntry& b) {
170 return a.second->timestamp < b.second->timestamp;
173 friend struct SortByTimestamp;
175 uint64_t image_cache_size;
176 uint64_t _image_cache_threshold;
178 uint64_t compute_image_cache_size ();
183 class LIBCANVAS_API WaveView : public Item, public sigc::trackable
192 std::string debug_name() const;
194 /* final ImageSurface rendered with colours */
196 Cairo::RefPtr<Cairo::ImageSurface> _image;
197 PBD::Signal0<void> ImageReady;
199 /* Displays a single channel of waveform data for the given Region.
201 x = 0 in the waveview corresponds to the first waveform datum taken
202 from region->start() samples into the source data.
204 x = N in the waveview corresponds to the (N * spp)'th sample
205 measured from region->start() into the source data.
207 when drawing, we will map the zeroth-pixel of the waveview
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).
217 WaveView (Canvas *, boost::shared_ptr<ARDOUR::AudioRegion>);
218 WaveView (Item*, boost::shared_ptr<ARDOUR::AudioRegion>);
221 void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const;
222 void compute_bounding_box () const;
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);
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.
236 void set_start_shift (double pixels);
238 void set_fill_color (Color);
239 void set_outline_color (Color);
241 void region_resized ();
242 void gain_changed ();
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);
253 /* currently missing because we don't need them (yet):
254 set_shape_independent();
255 set_logscaled_independent()
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);
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; }
267 void set_amplitude_above_axis (double v);
268 double amplitude_above_axis () const { return _amplitude_above_axis; }
270 static void set_clip_level (double dB);
271 static PBD::Signal0<void> ClipLevelChanged;
273 static void start_drawing_thread ();
274 static void stop_drawing_thread ();
276 #ifdef CANVAS_COMPATIBILITY
277 void*& property_gain_src () {
280 void*& property_gain_function () {
289 friend class ::WaveViewTest;
290 friend class WaveViewThreadClient;
292 void invalidate_image_cache ();
294 boost::shared_ptr<ARDOUR::AudioRegion> _region;
296 double _samples_per_pixel;
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;
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.
314 ARDOUR::frameoffset_t _region_start;
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.
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.
325 ARDOUR::framepos_t region_end() const;
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().
331 mutable bool get_image_in_thread;
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.
338 mutable bool rendered;
340 PBD::ScopedConnectionList invalidation_connection;
341 PBD::ScopedConnection image_ready_connection;
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;
349 static PBD::Signal0<void> VisualPropertiesChanged;
351 void handle_visual_property_change ();
352 void handle_clip_level_change ();
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;
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;
361 void cancel_my_render_request () const;
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;
369 mutable boost::shared_ptr<WaveViewCache::Entry> _current_image;
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;
375 static WaveViewCache* images;
377 static void drawing_thread ();
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;