summaryrefslogtreecommitdiff
path: root/libs/waveview/waveview/wave_view_private.h
blob: b1e5ace844926e2b712a88d2d8428b3b47ef185c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
    Copyright (C) 2017 Tim Mayberry

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef _WAVEVIEW_WAVE_VIEW_PRIVATE_H_
#define _WAVEVIEW_WAVE_VIEW_PRIVATE_H_

#include <deque>

#include "waveview/wave_view.h"

namespace ARDOUR {
	class AudioRegion;
}

namespace ArdourWaveView {

struct WaveViewProperties
{
public: // ctors
	WaveViewProperties (boost::shared_ptr<ARDOUR::AudioRegion> region);

	// WaveViewProperties (WaveViewProperties const& other) = default;

	// WaveViewProperties& operator=(WaveViewProperties const& other) = default;

public: // member variables

	framepos_t            region_start;
	framepos_t            region_end;
	uint16_t              channel;
	double                height;
	double                samples_per_pixel;
	double                amplitude;
	double                amplitude_above_axis;
	Gtkmm2ext::Color      fill_color;
	Gtkmm2ext::Color      outline_color;
	Gtkmm2ext::Color      zero_color;
	Gtkmm2ext::Color      clip_color;
	bool                  show_zero;
	bool                  logscaled;
	WaveView::Shape       shape;
	double                gradient_depth;
	double                start_shift;

private: // member variables

	framepos_t            sample_start;
	framepos_t            sample_end;

public: // methods

	bool is_valid () const
	{
		return (sample_end != 0 && samples_per_pixel != 0);
	}

	void set_width_samples (ARDOUR::framecnt_t const width_samples)
	{
		assert (is_valid());
		assert (width_samples != 0);
		ARDOUR::framecnt_t half_width = width_samples / 2;
		framepos_t new_sample_start = std::max (region_start, get_center_sample () - half_width);
		framepos_t new_sample_end = std::min (get_center_sample () + half_width, region_end);
		assert (new_sample_start <= new_sample_end);
		sample_start = new_sample_start;
		sample_end = new_sample_end;
	}

	uint64_t get_width_pixels () const
	{
		return (uint64_t)std::max (1LL, llrint (ceil (get_length_samples () / samples_per_pixel)));
	}


	void set_sample_offsets (framepos_t const start, framepos_t const end)
	{
		assert (start <= end);

		// sample_start and sample_end are bounded by the region limits.
		if (start < region_start) {
			sample_start = region_start;
		} else if (start > region_end) {
			sample_start = region_end;
		} else {
			sample_start = start;
		}

		if (end > region_end) {
			sample_end = region_end;
		} else if (end < region_start) {
			sample_end = region_start;
		} else {
			sample_end = end;
		}

		assert (sample_start <= sample_end);
	}

	framepos_t get_sample_start () const
	{
		return sample_start;
	}

	framepos_t get_sample_end () const
	{
		return sample_end;
	}

	void set_sample_positions_from_pixel_offsets (double start_pixel, double end_pixel)
	{
		assert (start_pixel <= end_pixel);
		/**
		 * It is possible for the new sample positions to be past the region_end,
		 * so we have to do bounds checking/adjustment for this in set_sample_offsets.
		 */
		framepos_t new_sample_start = region_start + (start_pixel * samples_per_pixel);
		framepos_t new_sample_end = region_start + (end_pixel * samples_per_pixel);
		set_sample_offsets (new_sample_start, new_sample_end);
	}

	ARDOUR::framecnt_t get_length_samples () const
	{
		assert (sample_start <= sample_end);
		return sample_end - sample_start;
	}

	framepos_t get_center_sample ()
	{
		return sample_start + (get_length_samples() / 2);
	}

	bool is_equivalent (WaveViewProperties const& other)
	{
		return (samples_per_pixel == other.samples_per_pixel &&
		        contains (other.sample_start, other.sample_end) && channel == other.channel &&
		        height == other.height && amplitude == other.amplitude &&
		        amplitude_above_axis == other.amplitude_above_axis && fill_color == other.fill_color &&
		        outline_color == other.outline_color && zero_color == other.zero_color &&
		        clip_color == other.clip_color && show_zero == other.show_zero &&
		        logscaled == other.logscaled && shape == other.shape &&
		        gradient_depth == other.gradient_depth);
		// region_start && start_shift??
	}

	bool contains (framepos_t start, framepos_t end)
	{
		return (sample_start <= start && end <= sample_end);
	}
};

struct WaveViewImage {
public: // ctors
	WaveViewImage (boost::shared_ptr<const ARDOUR::AudioRegion> const& region_ptr,
	               WaveViewProperties const& properties);

	~WaveViewImage ();

public: // member variables
	boost::weak_ptr<const ARDOUR::AudioRegion> region;
	WaveViewProperties props;
	Cairo::RefPtr<Cairo::ImageSurface> cairo_image;
	uint64_t timestamp;

public: // methods
	bool finished() { return static_cast<bool>(cairo_image); }

	bool
	contains_image_with_properties (WaveViewProperties const& other_props)
	{
		return cairo_image && props.is_equivalent (other_props);
	}

	bool is_valid () {
		return props.is_valid ();
	}

	size_t size_in_bytes ()
	{
		// 4 = bytes per FORMAT_ARGB32 pixel
		return props.height * props.get_width_pixels() * 4;
	}
};

struct WaveViewDrawRequest
{
public:
	WaveViewDrawRequest ();
	~WaveViewDrawRequest ();

	bool stopped() const { return (bool) g_atomic_int_get (const_cast<gint*>(&stop)); }
	void cancel() { g_atomic_int_set (&stop, 1); }
	bool finished() { return image->finished(); }

	boost::shared_ptr<WaveViewImage> image;

	bool is_valid () {
		return (image && image->is_valid());
	}

private:
	gint stop; /* intended for atomic access */
};

class WaveViewCache;

class WaveViewCacheGroup
{
public:
	WaveViewCacheGroup (WaveViewCache& parent_cache);

	~WaveViewCacheGroup ();

public:

	// @return image with matching properties or null
	boost::shared_ptr<WaveViewImage> lookup_image (WaveViewProperties const&);

	void add_image (boost::shared_ptr<WaveViewImage>);

	bool full () const { return _cached_images.size() > max_size(); }

	static uint32_t max_size () { return 16; }

	void clear_cache ();

private:

	/**
	 * At time of writing we don't strictly need a reference to the parent cache
	 * as there is only a single global cache but if the image cache ever becomes
	 * a per canvas cache then a using a reference is handy.
	 */
	WaveViewCache& _parent_cache;

	typedef std::list<boost::shared_ptr<WaveViewImage> > ImageCache;
	ImageCache _cached_images;
};

class WaveViewCache
{
public:
	static WaveViewCache* get_instance ();

	uint64_t image_cache_threshold () const { return _image_cache_threshold; }
	void set_image_cache_threshold (uint64_t);

	void clear_cache ();

	boost::shared_ptr<WaveViewCacheGroup> get_cache_group (boost::shared_ptr<ARDOUR::AudioSource>);

	void reset_cache_group (boost::shared_ptr<WaveViewCacheGroup>&);

private:
	WaveViewCache();
	~WaveViewCache();

private:
	typedef std::map<boost::shared_ptr<ARDOUR::AudioSource>, boost::shared_ptr<WaveViewCacheGroup> >
	    CacheGroups;

	CacheGroups cache_group_map;

	uint64_t image_cache_size;
	uint64_t _image_cache_threshold;

private:
	friend class WaveViewCacheGroup;

	void increase_size (uint64_t bytes);
	void decrease_size (uint64_t bytes);

	bool full () { return image_cache_size > _image_cache_threshold; }
};

class WaveViewDrawRequestQueue
{
public:

	void enqueue (boost::shared_ptr<WaveViewDrawRequest>&);

	// @return valid request or null if non-blocking or no request is available
	boost::shared_ptr<WaveViewDrawRequest> dequeue (bool block);

	void wake_up ();

private:

	mutable Glib::Threads::Mutex _queue_mutex;
	Glib::Threads::Cond _cond;

	typedef std::deque<boost::shared_ptr<WaveViewDrawRequest> > DrawRequestQueueType;
	DrawRequestQueueType _queue;
};

class WaveViewDrawingThread
{
public:
	WaveViewDrawingThread ();
	~WaveViewDrawingThread ();

private:
	void start ();
	void quit ();
	void run ();

private:
	Glib::Threads::Thread* _thread;
	gint _quit;
};

class WaveViewThreads {
private:
	WaveViewThreads ();
	~WaveViewThreads ();

public:
	static void initialize ();
	static void deinitialize ();

	static bool enabled () { return (instance); }

	static void enqueue_draw_request (boost::shared_ptr<WaveViewDrawRequest>&);

private:
	friend class WaveViewDrawingThread;

	static void wake_up ();

	// will block until a request is available
	static boost::shared_ptr<WaveViewDrawRequest> dequeue_draw_request ();

	void start_threads ();
	void stop_threads ();

private:
	static uint32_t init_count;
	static WaveViewThreads* instance;

	// TODO use std::unique_ptr when possible
	typedef std::vector<boost::shared_ptr<WaveViewDrawingThread> > WaveViewThreadList;

	WaveViewThreadList _threads;
	WaveViewDrawRequestQueue _request_queue;
};


} /* namespace */

#endif