Ardour  9.8-309-gbfb0c0c42a
wave_view_private.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Tim Mayberry <mojofunk@gmail.com>
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
20 #define _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
21 
22 #include <deque>
23 
24 #include "pbd/mutex.h"
25 #include "pbd/pthread_utils.h"
26 #include "waveview/wave_view.h"
27 
28 namespace ARDOUR {
29  class AudioRegion;
30 }
31 
32 namespace ArdourWaveView {
33 
35 {
36 public: // ctors
37  WaveViewProperties (std::shared_ptr<ARDOUR::AudioRegion> region);
38 
39  // WaveViewProperties (WaveViewProperties const& other) = default;
40 
41  // WaveViewProperties& operator=(WaveViewProperties const& other) = default;
42 
43 public: // member variables
44 
47  uint16_t channel;
48  double height;
50  double amplitude;
56  bool show_zero;
57  bool logscaled;
60  double start_shift;
64 
65 private: // member variables
66 
69 
70 public: // methods
71 
72  bool is_valid () const
73  {
74  return (sample_end != 0 && samples_per_pixel != 0);
75  }
76 
77  void set_width_samples (ARDOUR::samplecnt_t const width_samples)
78  {
79  assert (is_valid());
80  assert (width_samples != 0);
81  ARDOUR::samplecnt_t half_width = width_samples / 2;
82  samplepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width);
83  samplepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end);
84  assert (new_sample_start <= new_sample_end);
85  sample_start = new_sample_start;
86  sample_end = new_sample_end;
87  }
88 
89  uint64_t get_width_pixels () const
90  {
91  return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel)));
92  }
93 
94 
96  {
97  assert (start <= end);
98 
99  // sample_start and sample_end are bounded by the region limits.
100  if (start < region_start) {
102  } else if (start > region_end) {
104  } else {
106  }
107 
108  if (end > region_end) {
110  } else if (end < region_start) {
112  } else {
113  sample_end = end;
114  }
115 
116  assert (sample_start <= sample_end);
117  }
118 
120  {
121  return sample_start;
122  }
123 
125  {
126  return sample_end;
127  }
128 
129  void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel)
130  {
131  assert (start_pixel <= end_pixel);
136  samplepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel);
137  samplepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel);
138  set_sample_offsets (new_sample_start, new_sample_end);
139  }
140 
142  {
143  assert (sample_start <= sample_end);
144  return sample_end - sample_start;
145  }
146 
148  {
149  return sample_start + (get_length_samples() / 2);
150  }
151 
152  bool is_equivalent (WaveViewProperties const& other)
153  {
154  return (samples_per_pixel == other.samples_per_pixel &&
155  contains (other.sample_start, other.sample_end) && channel == other.channel &&
156  height == other.height && amplitude == other.amplitude &&
158  outline_color == other.outline_color && zero_color == other.zero_color &&
159  clip_color == other.clip_color && show_zero == other.show_zero &&
160  logscaled == other.logscaled && shape == other.shape &&
161  gradient_depth == other.gradient_depth &&
162  envelope_active == other.envelope_active &&
164  // region_start && start_shift??
165  }
166 
168  {
169  return (sample_start <= start && end <= sample_end);
170  }
171 };
172 
174 public: // ctors
175  WaveViewImage (std::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
176  WaveViewProperties const& properties);
177 
179 
180 public: // member variables
181  std::weak_ptr<const ARDOUR::AudioRegion> region;
183  Cairo::RefPtr<Cairo::ImageSurface> cairo_image;
184  uint64_t timestamp;
185 
186 public: // methods
187  bool finished() { return static_cast<bool>(cairo_image); }
188 
189  bool
191  {
192  return cairo_image && props.is_equivalent (other_props);
193  }
194 
195  bool is_valid () {
196  return props.is_valid ();
197  }
198 
199  size_t size_in_bytes ()
200  {
201  // 4 = bytes per FORMAT_ARGB32 pixel
202  return props.height * props.get_width_pixels() * 4;
203  }
204 };
205 
207 {
208 public:
211 
212  bool stopped() const { return (bool) _stop.load (); }
213  void cancel() { _stop.store (1); }
214  bool finished() { return image->finished(); }
215 
216  std::shared_ptr<WaveViewImage> image;
217 
218  bool is_valid () {
219  return (image && image->is_valid());
220  }
221 
222 private:
223  std::atomic<int> _stop; /* intended for atomic access */
224 };
225 
226 class WaveViewCache;
227 
229 {
230 public:
232 
234 
235 public:
236 
237  // @return image with matching properties or null
238  std::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);
239 
240  void add_image (std::shared_ptr<WaveViewImage>);
241 
242  bool full () const { return _cached_images.size() > max_size(); }
243 
244  static uint32_t max_size () { return 16; }
245 
246  void clear_cache ();
247 
248 private:
249 
256 
257  typedef std::list<std::shared_ptr<WaveViewImage> > ImageCache;
259 };
260 
262 {
263 public:
265 
266  uint64_t image_cache_threshold () const { return _image_cache_threshold; }
267  void set_image_cache_threshold (uint64_t);
268 
269  void clear_cache ();
270 
271  std::shared_ptr<WaveViewCacheGroup> get_cache_group (std::shared_ptr<ARDOUR::AudioSource>);
272 
273  void reset_cache_group (std::shared_ptr<WaveViewCacheGroup>&);
274 
275 private:
278 
279 private:
280  typedef std::map<std::shared_ptr<ARDOUR::AudioSource>, std::shared_ptr<WaveViewCacheGroup> >
282 
284 
287 
288 private:
289  friend class WaveViewCacheGroup;
290 
291  void increase_size (uint64_t bytes);
292  void decrease_size (uint64_t bytes);
293 
295 };
296 
298 {
299 public:
302 
303 private:
304  void start ();
305  void run ();
306 
307 private:
309 };
310 
312 private:
315 
316 public:
317  static void initialize ();
318  static void deinitialize ();
319 
320  static bool enabled () { return (instance); }
321 
322  static void enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
323 
324 private:
325  friend class WaveViewDrawingThread;
326 
327  // will block until a request is available
328  static std::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();
329  static void thread_proc ();
330 
331  std::shared_ptr<WaveViewDrawRequest> _dequeue_draw_request ();
332  void _enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
333  void _thread_proc ();
334 
335  void start_threads ();
336  void stop_threads ();
337 
338 private:
339  static uint32_t init_count;
341 
342  // TODO use std::unique_ptr when possible
343  typedef std::vector<std::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;
344 
345  bool _quit;
347 
350 
351  typedef std::deque<std::shared_ptr<WaveViewDrawRequest> > DrawRequestQueueType;
353 };
354 
355 
356 } /* namespace */
357 
358 #endif
WaveViewCacheGroup(WaveViewCache &parent_cache)
std::shared_ptr< WaveViewImage > lookup_image(WaveViewProperties const &)
void add_image(std::shared_ptr< WaveViewImage >)
std::list< std::shared_ptr< WaveViewImage > > ImageCache
std::map< std::shared_ptr< ARDOUR::AudioSource >, std::shared_ptr< WaveViewCacheGroup > > CacheGroups
void set_image_cache_threshold(uint64_t)
void decrease_size(uint64_t bytes)
static WaveViewCache * get_instance()
void reset_cache_group(std::shared_ptr< WaveViewCacheGroup > &)
std::shared_ptr< WaveViewCacheGroup > get_cache_group(std::shared_ptr< ARDOUR::AudioSource >)
uint64_t image_cache_threshold() const
void increase_size(uint64_t bytes)
static WaveViewThreads * instance
std::deque< std::shared_ptr< WaveViewDrawRequest > > DrawRequestQueueType
std::vector< std::shared_ptr< WaveViewDrawingThread > > WaveViewThreadList
void _enqueue_draw_request(std::shared_ptr< WaveViewDrawRequest > &)
static std::shared_ptr< WaveViewDrawRequest > dequeue_draw_request()
std::shared_ptr< WaveViewDrawRequest > _dequeue_draw_request()
static void enqueue_draw_request(std::shared_ptr< WaveViewDrawRequest > &)
PBD::PropertyDescriptor< bool > region
PBD::PropertyDescriptor< timepos_t > start
Temporal::samplecnt_t samplecnt_t
Temporal::samplepos_t samplepos_t
uint32_t Color
Definition: colors.h:33
minimum disk read bytes
std::shared_ptr< WaveViewImage > image
Cairo::RefPtr< Cairo::ImageSurface > cairo_image
WaveViewImage(std::shared_ptr< const ARDOUR::AudioRegion > const &region_ptr, WaveViewProperties const &properties)
bool contains_image_with_properties(WaveViewProperties const &other_props)
std::weak_ptr< const ARDOUR::AudioRegion > region
void set_width_samples(ARDOUR::samplecnt_t const width_samples)
WaveViewProperties(std::shared_ptr< ARDOUR::AudioRegion > region)
bool is_equivalent(WaveViewProperties const &other)
bool contains(samplepos_t start, samplepos_t end)
void set_sample_offsets(samplepos_t const start, samplepos_t const end)
void set_sample_positions_from_pixel_offsets(double start_pixel, double end_pixel)
ARDOUR::samplecnt_t get_length_samples() const