Ardour  8.7-15-gadf511264b
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/pthread_utils.h"
25 #include "waveview/wave_view.h"
26 
27 namespace ARDOUR {
28  class AudioRegion;
29 }
30 
31 namespace ArdourWaveView {
32 
34 {
35 public: // ctors
36  WaveViewProperties (std::shared_ptr<ARDOUR::AudioRegion> region);
37 
38  // WaveViewProperties (WaveViewProperties const& other) = default;
39 
40  // WaveViewProperties& operator=(WaveViewProperties const& other) = default;
41 
42 public: // member variables
43 
46  uint16_t channel;
47  double height;
49  double amplitude;
55  bool show_zero;
56  bool logscaled;
59  double start_shift;
60 
61 private: // member variables
62 
65 
66 public: // methods
67 
68  bool is_valid () const
69  {
70  return (sample_end != 0 && samples_per_pixel != 0);
71  }
72 
73  void set_width_samples (ARDOUR::samplecnt_t const width_samples)
74  {
75  assert (is_valid());
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;
83  }
84 
85  uint64_t get_width_pixels () const
86  {
87  return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel)));
88  }
89 
90 
92  {
93  assert (start <= end);
94 
95  // sample_start and sample_end are bounded by the region limits.
96  if (start < region_start) {
98  } else if (start > region_end) {
100  } else {
102  }
103 
104  if (end > region_end) {
106  } else if (end < region_start) {
108  } else {
109  sample_end = end;
110  }
111 
112  assert (sample_start <= sample_end);
113  }
114 
116  {
117  return sample_start;
118  }
119 
121  {
122  return sample_end;
123  }
124 
125  void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel)
126  {
127  assert (start_pixel <= end_pixel);
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);
135  }
136 
138  {
139  assert (sample_start <= sample_end);
140  return sample_end - sample_start;
141  }
142 
144  {
145  return sample_start + (get_length_samples() / 2);
146  }
147 
148  bool is_equivalent (WaveViewProperties const& other)
149  {
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 &&
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??
159  }
160 
162  {
163  return (sample_start <= start && end <= sample_end);
164  }
165 };
166 
168 public: // ctors
169  WaveViewImage (std::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
170  WaveViewProperties const& properties);
171 
173 
174 public: // member variables
175  std::weak_ptr<const ARDOUR::AudioRegion> region;
177  Cairo::RefPtr<Cairo::ImageSurface> cairo_image;
178  uint64_t timestamp;
179 
180 public: // methods
181  bool finished() { return static_cast<bool>(cairo_image); }
182 
183  bool
185  {
186  return cairo_image && props.is_equivalent (other_props);
187  }
188 
189  bool is_valid () {
190  return props.is_valid ();
191  }
192 
193  size_t size_in_bytes ()
194  {
195  // 4 = bytes per FORMAT_ARGB32 pixel
196  return props.height * props.get_width_pixels() * 4;
197  }
198 };
199 
201 {
202 public:
205 
206  bool stopped() const { return (bool) _stop.load (); }
207  void cancel() { _stop.store (1); }
208  bool finished() { return image->finished(); }
209 
210  std::shared_ptr<WaveViewImage> image;
211 
212  bool is_valid () {
213  return (image && image->is_valid());
214  }
215 
216 private:
217  std::atomic<int> _stop; /* intended for atomic access */
218 };
219 
220 class WaveViewCache;
221 
223 {
224 public:
226 
228 
229 public:
230 
231  // @return image with matching properties or null
232  std::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);
233 
234  void add_image (std::shared_ptr<WaveViewImage>);
235 
236  bool full () const { return _cached_images.size() > max_size(); }
237 
238  static uint32_t max_size () { return 16; }
239 
240  void clear_cache ();
241 
242 private:
243 
250 
251  typedef std::list<std::shared_ptr<WaveViewImage> > ImageCache;
253 };
254 
256 {
257 public:
259 
260  uint64_t image_cache_threshold () const { return _image_cache_threshold; }
261  void set_image_cache_threshold (uint64_t);
262 
263  void clear_cache ();
264 
265  std::shared_ptr<WaveViewCacheGroup> get_cache_group (std::shared_ptr<ARDOUR::AudioSource>);
266 
267  void reset_cache_group (std::shared_ptr<WaveViewCacheGroup>&);
268 
269 private:
272 
273 private:
274  typedef std::map<std::shared_ptr<ARDOUR::AudioSource>, std::shared_ptr<WaveViewCacheGroup> >
276 
278 
281 
282 private:
283  friend class WaveViewCacheGroup;
284 
285  void increase_size (uint64_t bytes);
286  void decrease_size (uint64_t bytes);
287 
289 };
290 
292 {
293 public:
296 
297 private:
298  void start ();
299  void run ();
300 
301 private:
303 };
304 
306 private:
309 
310 public:
311  static void initialize ();
312  static void deinitialize ();
313 
314  static bool enabled () { return (instance); }
315 
316  static void enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
317 
318 private:
319  friend class WaveViewDrawingThread;
320 
321  // will block until a request is available
322  static std::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();
323  static void thread_proc ();
324 
325  std::shared_ptr<WaveViewDrawRequest> _dequeue_draw_request ();
326  void _enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
327  void _thread_proc ();
328 
329  void start_threads ();
330  void stop_threads ();
331 
332 private:
333  static uint32_t init_count;
335 
336  // TODO use std::unique_ptr when possible
337  typedef std::vector<std::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;
338 
339  bool _quit;
341 
342 
343  mutable Glib::Threads::Mutex _queue_mutex;
344  Glib::Threads::Cond _cond;
345 
346  typedef std::deque<std::shared_ptr<WaveViewDrawRequest> > DrawRequestQueueType;
348 };
349 
350 
351 } /* namespace */
352 
353 #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< 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