2 Copyright (C) 2017 Tim Mayberry
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
21 #define _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
25 #include "waveview/wave_view.h"
31 namespace ArdourWaveView {
33 struct WaveViewProperties
36 WaveViewProperties (boost::shared_ptr<ARDOUR::AudioRegion> region);
38 // WaveViewProperties (WaveViewProperties const& other) = default;
40 // WaveViewProperties& operator=(WaveViewProperties const& other) = default;
42 public: // member variables
44 samplepos_t region_start;
45 samplepos_t region_end;
48 double samples_per_pixel;
50 double amplitude_above_axis;
51 Gtkmm2ext::Color fill_color;
52 Gtkmm2ext::Color outline_color;
53 Gtkmm2ext::Color zero_color;
54 Gtkmm2ext::Color clip_color;
57 WaveView::Shape shape;
58 double gradient_depth;
61 private: // member variables
63 samplepos_t sample_start;
64 samplepos_t sample_end;
68 bool is_valid () const
70 return (sample_end != 0 && samples_per_pixel != 0);
73 void set_width_samples (ARDOUR::samplecnt_t const width_samples)
76 assert (width_samples != 0);
77 ARDOUR::samplecnt_t half_width = width_samples / 2;
78 samplepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width);
79 samplepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end);
80 assert (new_sample_start <= new_sample_end);
81 sample_start = new_sample_start;
82 sample_end = new_sample_end;
85 uint64_t get_width_pixels () const
87 return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel)));
91 void set_sample_offsets (samplepos_t const start, samplepos_t const end)
93 assert (start <= end);
95 // sample_start and sample_end are bounded by the region limits.
96 if (start < region_start) {
97 sample_start = region_start;
98 } else if (start > region_end) {
99 sample_start = region_end;
101 sample_start = start;
104 if (end > region_end) {
105 sample_end = region_end;
106 } else if (end < region_start) {
107 sample_end = region_start;
112 assert (sample_start <= sample_end);
115 samplepos_t get_sample_start () const
120 samplepos_t get_sample_end () const
125 void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel)
127 assert (start_pixel <= end_pixel);
129 * It is possible for the new sample positions to be past the region_end,
130 * so we have to do bounds checking/adjustment for this in set_sample_offsets.
132 samplepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel);
133 samplepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel);
134 set_sample_offsets (new_sample_start, new_sample_end);
137 ARDOUR::samplecnt_t get_length_samples () const
139 assert (sample_start <= sample_end);
140 return sample_end - sample_start;
143 samplepos_t get_center_sample ()
145 return sample_start + (get_length_samples() / 2);
148 bool is_equivalent (WaveViewProperties const& other)
150 return (samples_per_pixel == other.samples_per_pixel &&
151 contains (other.sample_start, other.sample_end) && channel == other.channel &&
152 height == other.height && amplitude == other.amplitude &&
153 amplitude_above_axis == other.amplitude_above_axis && fill_color == other.fill_color &&
154 outline_color == other.outline_color && zero_color == other.zero_color &&
155 clip_color == other.clip_color && show_zero == other.show_zero &&
156 logscaled == other.logscaled && shape == other.shape &&
157 gradient_depth == other.gradient_depth);
158 // region_start && start_shift??
161 bool contains (samplepos_t start, samplepos_t end)
163 return (sample_start <= start && end <= sample_end);
167 struct WaveViewImage {
169 WaveViewImage (boost::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
170 WaveViewProperties const& properties);
174 public: // member variables
175 boost::weak_ptr<const ARDOUR::AudioRegion> region;
176 WaveViewProperties props;
177 Cairo::RefPtr<Cairo::ImageSurface> cairo_image;
181 bool finished() { return static_cast<bool>(cairo_image); }
184 contains_image_with_properties (WaveViewProperties const& other_props)
186 return cairo_image && props.is_equivalent (other_props);
190 return props.is_valid ();
193 size_t size_in_bytes ()
195 // 4 = bytes per FORMAT_ARGB32 pixel
196 return props.height * props.get_width_pixels() * 4;
200 struct WaveViewDrawRequest
203 WaveViewDrawRequest ();
204 ~WaveViewDrawRequest ();
206 bool stopped() const { return (bool) g_atomic_int_get (const_cast<gint*>(&stop)); }
207 void cancel() { g_atomic_int_set (&stop, 1); }
208 bool finished() { return image->finished(); }
210 boost::shared_ptr<WaveViewImage> image;
213 return (image && image->is_valid());
217 gint stop; /* intended for atomic access */
222 class WaveViewCacheGroup
225 WaveViewCacheGroup (WaveViewCache& parent_cache);
227 ~WaveViewCacheGroup ();
231 // @return image with matching properties or null
232 boost::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);
234 void add_image (boost::shared_ptr<WaveViewImage>);
236 bool full () const { return _cached_images.size() > max_size(); }
238 static uint32_t max_size () { return 16; }
245 * At time of writing we don't strictly need a reference to the parent cache
246 * as there is only a single global cache but if the image cache ever becomes
247 * a per canvas cache then a using a reference is handy.
249 WaveViewCache& _parent_cache;
251 typedef std::list<boost::shared_ptr<WaveViewImage> > ImageCache;
252 ImageCache _cached_images;
258 static WaveViewCache* get_instance ();
260 uint64_t image_cache_threshold () const { return _image_cache_threshold; }
261 void set_image_cache_threshold (uint64_t);
265 boost::shared_ptr<WaveViewCacheGroup> get_cache_group (boost::shared_ptr<ARDOUR::AudioSource>);
267 void reset_cache_group (boost::shared_ptr<WaveViewCacheGroup>&);
274 typedef std::map<boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<WaveViewCacheGroup> >
277 CacheGroups cache_group_map;
279 uint64_t image_cache_size;
280 uint64_t _image_cache_threshold;
283 friend class WaveViewCacheGroup;
285 void increase_size (uint64_t bytes);
286 void decrease_size (uint64_t bytes);
288 bool full () { return image_cache_size > _image_cache_threshold; }
291 class WaveViewDrawRequestQueue
295 void enqueue (boost::shared_ptr<WaveViewDrawRequest>&);
297 // @return valid request or null if non-blocking or no request is available
298 boost::shared_ptr<WaveViewDrawRequest> dequeue (bool block);
304 mutable Glib::Threads::Mutex _queue_mutex;
305 Glib::Threads::Cond _cond;
307 typedef std::deque<boost::shared_ptr<WaveViewDrawRequest> > DrawRequestQueueType;
308 DrawRequestQueueType _queue;
311 class WaveViewDrawingThread
314 WaveViewDrawingThread ();
315 ~WaveViewDrawingThread ();
323 Glib::Threads::Thread* _thread;
327 class WaveViewThreads {
333 static void initialize ();
334 static void deinitialize ();
336 static bool enabled () { return (instance); }
338 static void enqueue_draw_request (boost::shared_ptr<WaveViewDrawRequest>&);
341 friend class WaveViewDrawingThread;
343 static void wake_up ();
345 // will block until a request is available
346 static boost::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();
348 void start_threads ();
349 void stop_threads ();
352 static uint32_t init_count;
353 static WaveViewThreads* instance;
355 // TODO use std::unique_ptr when possible
356 typedef std::vector<boost::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;
358 WaveViewThreadList _threads;
359 WaveViewDrawRequestQueue _request_queue;