Fix FP8/16 port-names
[ardour.git] / libs / waveview / waveview / wave_view_private.h
1 /*
2     Copyright (C) 2017 Tim Mayberry
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
21 #define _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
22
23 #include <deque>
24
25 #include "waveview/wave_view.h"
26
27 namespace ARDOUR {
28         class AudioRegion;
29 }
30
31 namespace ArdourWaveView {
32
33 struct WaveViewProperties
34 {
35 public: // ctors
36         WaveViewProperties (boost::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
44         samplepos_t            region_start;
45         samplepos_t            region_end;
46         uint16_t              channel;
47         double                height;
48         double                samples_per_pixel;
49         double                amplitude;
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;
55         bool                  show_zero;
56         bool                  logscaled;
57         WaveView::Shape       shape;
58         double                gradient_depth;
59         double                start_shift;
60
61 private: // member variables
62
63         samplepos_t            sample_start;
64         samplepos_t            sample_end;
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
91         void set_sample_offsets (samplepos_t const start, samplepos_t const end)
92         {
93                 assert (start <= end);
94
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;
100                 } else {
101                         sample_start = start;
102                 }
103
104                 if (end > region_end) {
105                         sample_end = region_end;
106                 } else if (end < region_start) {
107                         sample_end = region_start;
108                 } else {
109                         sample_end = end;
110                 }
111
112                 assert (sample_start <= sample_end);
113         }
114
115         samplepos_t get_sample_start () const
116         {
117                 return sample_start;
118         }
119
120         samplepos_t get_sample_end () const
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);
128                 /**
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.
131                  */
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
137         ARDOUR::samplecnt_t get_length_samples () const
138         {
139                 assert (sample_start <= sample_end);
140                 return sample_end - sample_start;
141         }
142
143         samplepos_t get_center_sample ()
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 &&
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??
159         }
160
161         bool contains (samplepos_t start, samplepos_t end)
162         {
163                 return (sample_start <= start && end <= sample_end);
164         }
165 };
166
167 struct WaveViewImage {
168 public: // ctors
169         WaveViewImage (boost::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
170                        WaveViewProperties const& properties);
171
172         ~WaveViewImage ();
173
174 public: // member variables
175         boost::weak_ptr<const ARDOUR::AudioRegion> region;
176         WaveViewProperties props;
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
184         contains_image_with_properties (WaveViewProperties const& other_props)
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
200 struct WaveViewDrawRequest
201 {
202 public:
203         WaveViewDrawRequest ();
204         ~WaveViewDrawRequest ();
205
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(); }
209
210         boost::shared_ptr<WaveViewImage> image;
211
212         bool is_valid () {
213                 return (image && image->is_valid());
214         }
215
216 private:
217         gint stop; /* intended for atomic access */
218 };
219
220 class WaveViewCache;
221
222 class WaveViewCacheGroup
223 {
224 public:
225         WaveViewCacheGroup (WaveViewCache& parent_cache);
226
227         ~WaveViewCacheGroup ();
228
229 public:
230
231         // @return image with matching properties or null
232         boost::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);
233
234         void add_image (boost::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
244         /**
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.
248          */
249         WaveViewCache& _parent_cache;
250
251         typedef std::list<boost::shared_ptr<WaveViewImage> > ImageCache;
252         ImageCache _cached_images;
253 };
254
255 class WaveViewCache
256 {
257 public:
258         static WaveViewCache* get_instance ();
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         boost::shared_ptr<WaveViewCacheGroup> get_cache_group (boost::shared_ptr<ARDOUR::AudioSource>);
266
267         void reset_cache_group (boost::shared_ptr<WaveViewCacheGroup>&);
268
269 private:
270         WaveViewCache();
271         ~WaveViewCache();
272
273 private:
274         typedef std::map<boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<WaveViewCacheGroup> >
275             CacheGroups;
276
277         CacheGroups cache_group_map;
278
279         uint64_t image_cache_size;
280         uint64_t _image_cache_threshold;
281
282 private:
283         friend class WaveViewCacheGroup;
284
285         void increase_size (uint64_t bytes);
286         void decrease_size (uint64_t bytes);
287
288         bool full () { return image_cache_size > _image_cache_threshold; }
289 };
290
291 class WaveViewDrawRequestQueue
292 {
293 public:
294
295         void enqueue (boost::shared_ptr<WaveViewDrawRequest>&);
296
297         // @return valid request or null if non-blocking or no request is available
298         boost::shared_ptr<WaveViewDrawRequest> dequeue (bool block);
299
300         void wake_up ();
301
302 private:
303
304         mutable Glib::Threads::Mutex _queue_mutex;
305         Glib::Threads::Cond _cond;
306
307         typedef std::deque<boost::shared_ptr<WaveViewDrawRequest> > DrawRequestQueueType;
308         DrawRequestQueueType _queue;
309 };
310
311 class WaveViewDrawingThread
312 {
313 public:
314         WaveViewDrawingThread ();
315         ~WaveViewDrawingThread ();
316
317 private:
318         void start ();
319         void quit ();
320         void run ();
321
322 private:
323         Glib::Threads::Thread* _thread;
324         gint _quit;
325 };
326
327 class WaveViewThreads {
328 private:
329         WaveViewThreads ();
330         ~WaveViewThreads ();
331
332 public:
333         static void initialize ();
334         static void deinitialize ();
335
336         static bool enabled () { return (instance); }
337
338         static void enqueue_draw_request (boost::shared_ptr<WaveViewDrawRequest>&);
339
340 private:
341         friend class WaveViewDrawingThread;
342
343         static void wake_up ();
344
345         // will block until a request is available
346         static boost::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();
347
348         void start_threads ();
349         void stop_threads ();
350
351 private:
352         static uint32_t init_count;
353         static WaveViewThreads* instance;
354
355         // TODO use std::unique_ptr when possible
356         typedef std::vector<boost::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;
357
358         WaveViewThreadList _threads;
359         WaveViewDrawRequestQueue _request_queue;
360 };
361
362
363 } /* namespace */
364
365 #endif