Ardour  9.2-79-gba93f2fe52
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;
61 
62 private: // member variables
63 
66 
67 public: // methods
68 
69  bool is_valid () const
70  {
71  return (sample_end != 0 && samples_per_pixel != 0);
72  }
73 
74  void set_width_samples (ARDOUR::samplecnt_t const width_samples)
75  {
76  assert (is_valid());
77  assert (width_samples != 0);
78  ARDOUR::samplecnt_t half_width = width_samples / 2;
79  samplepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width);
80  samplepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end);
81  assert (new_sample_start <= new_sample_end);
82  sample_start = new_sample_start;
83  sample_end = new_sample_end;
84  }
85 
86  uint64_t get_width_pixels () const
87  {
88  return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel)));
89  }
90 
91 
93  {
94  assert (start <= end);
95 
96  // sample_start and sample_end are bounded by the region limits.
97  if (start < region_start) {
99  } else if (start > region_end) {
101  } else {
103  }
104 
105  if (end > region_end) {
107  } else if (end < region_start) {
109  } else {
110  sample_end = end;
111  }
112 
113  assert (sample_start <= sample_end);
114  }
115 
117  {
118  return sample_start;
119  }
120 
122  {
123  return sample_end;
124  }
125 
126  void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel)
127  {
128  assert (start_pixel <= end_pixel);
133  samplepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel);
134  samplepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel);
135  set_sample_offsets (new_sample_start, new_sample_end);
136  }
137 
139  {
140  assert (sample_start <= sample_end);
141  return sample_end - sample_start;
142  }
143 
145  {
146  return sample_start + (get_length_samples() / 2);
147  }
148 
149  bool is_equivalent (WaveViewProperties const& other)
150  {
151  return (samples_per_pixel == other.samples_per_pixel &&
152  contains (other.sample_start, other.sample_end) && channel == other.channel &&
153  height == other.height && amplitude == other.amplitude &&
155  outline_color == other.outline_color && zero_color == other.zero_color &&
156  clip_color == other.clip_color && show_zero == other.show_zero &&
157  logscaled == other.logscaled && shape == other.shape &&
158  gradient_depth == other.gradient_depth);
159  // region_start && start_shift??
160  }
161 
163  {
164  return (sample_start <= start && end <= sample_end);
165  }
166 };
167 
169 public: // ctors
170  WaveViewImage (std::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
171  WaveViewProperties const& properties);
172 
174 
175 public: // member variables
176  std::weak_ptr<const ARDOUR::AudioRegion> region;
178  Cairo::RefPtr<Cairo::ImageSurface> cairo_image;
179  uint64_t timestamp;
180 
181 public: // methods
182  bool finished() { return static_cast<bool>(cairo_image); }
183 
184  bool
186  {
187  return cairo_image && props.is_equivalent (other_props);
188  }
189 
190  bool is_valid () {
191  return props.is_valid ();
192  }
193 
194  size_t size_in_bytes ()
195  {
196  // 4 = bytes per FORMAT_ARGB32 pixel
197  return props.height * props.get_width_pixels() * 4;
198  }
199 };
200 
202 {
203 public:
206 
207  bool stopped() const { return (bool) _stop.load (); }
208  void cancel() { _stop.store (1); }
209  bool finished() { return image->finished(); }
210 
211  std::shared_ptr<WaveViewImage> image;
212 
213  bool is_valid () {
214  return (image && image->is_valid());
215  }
216 
217 private:
218  std::atomic<int> _stop; /* intended for atomic access */
219 };
220 
221 class WaveViewCache;
222 
224 {
225 public:
227 
229 
230 public:
231 
232  // @return image with matching properties or null
233  std::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);
234 
235  void add_image (std::shared_ptr<WaveViewImage>);
236 
237  bool full () const { return _cached_images.size() > max_size(); }
238 
239  static uint32_t max_size () { return 16; }
240 
241  void clear_cache ();
242 
243 private:
244 
251 
252  typedef std::list<std::shared_ptr<WaveViewImage> > ImageCache;
254 };
255 
257 {
258 public:
260 
261  uint64_t image_cache_threshold () const { return _image_cache_threshold; }
262  void set_image_cache_threshold (uint64_t);
263 
264  void clear_cache ();
265 
266  std::shared_ptr<WaveViewCacheGroup> get_cache_group (std::shared_ptr<ARDOUR::AudioSource>);
267 
268  void reset_cache_group (std::shared_ptr<WaveViewCacheGroup>&);
269 
270 private:
273 
274 private:
275  typedef std::map<std::shared_ptr<ARDOUR::AudioSource>, std::shared_ptr<WaveViewCacheGroup> >
277 
279 
282 
283 private:
284  friend class WaveViewCacheGroup;
285 
286  void increase_size (uint64_t bytes);
287  void decrease_size (uint64_t bytes);
288 
290 };
291 
293 {
294 public:
297 
298 private:
299  void start ();
300  void run ();
301 
302 private:
304 };
305 
307 private:
310 
311 public:
312  static void initialize ();
313  static void deinitialize ();
314 
315  static bool enabled () { return (instance); }
316 
317  static void enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
318 
319 private:
320  friend class WaveViewDrawingThread;
321 
322  // will block until a request is available
323  static std::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();
324  static void thread_proc ();
325 
326  std::shared_ptr<WaveViewDrawRequest> _dequeue_draw_request ();
327  void _enqueue_draw_request (std::shared_ptr<WaveViewDrawRequest>&);
328  void _thread_proc ();
329 
330  void start_threads ();
331  void stop_threads ();
332 
333 private:
334  static uint32_t init_count;
336 
337  // TODO use std::unique_ptr when possible
338  typedef std::vector<std::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;
339 
340  bool _quit;
342 
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< 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