#include "ardour/lmath.h"
#include "ardour/audioregion.h"
#include "ardour/audiosource.h"
+#include "ardour/session.h"
#include "canvas/canvas.h"
#include "canvas/colors.h"
#include "canvas/utils.h"
#include "canvas/wave_view.h"
+#include "evoral/Range.hpp"
+
#include <gdkmm/general.h>
#include "gtkmm2ext/gui_thread.h"
WaveViewCache* WaveView::images = 0;
gint WaveView::drawing_thread_should_quit = 0;
Glib::Threads::Mutex WaveView::request_queue_lock;
+Glib::Threads::Mutex WaveView::current_image_lock;
Glib::Threads::Cond WaveView::request_cond;
Glib::Threads::Thread* WaveView::_drawing_thread = 0;
WaveView::DrawingRequestQueue WaveView::request_queue;
, _amplitude_above_axis (1.0)
, _region_amplitude (region->scale_amplitude ())
, _start_shift (0.0)
- , idle_queued (false)
, _region_start (region->start())
, get_image_in_thread (false)
, always_get_image_in_thread (false)
, rendered (false)
{
+ if (!images) {
+ images = new WaveViewCache;
+ }
+
VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
, _amplitude_above_axis (1.0)
, _region_amplitude (region->scale_amplitude ())
, _start_shift (0.0)
- , idle_queued (false)
, _region_start (region->start())
, get_image_in_thread (false)
, always_get_image_in_thread (false)
, rendered (false)
{
+ if (!images) {
+ images = new WaveViewCache;
+ }
+
VisualPropertiesChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_visual_property_change, this));
ClipLevelChanged.connect_same_thread (invalidation_connection, boost::bind (&WaveView::handle_clip_level_change, this));
WaveView::~WaveView ()
{
invalidate_image_cache ();
+ if (images ) {
+ images->clear_cache ();
+ }
}
string
void
WaveView::image_ready ()
{
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("queue draw for %1 at %2 (vis = %3 CR %4)\n", this, g_get_monotonic_time(), visible(), current_request));
redraw ();
}
void
WaveView::invalidate_image_cache ()
{
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 invalidates image cache and cancels current request\n", this));
cancel_my_render_request ();
+ Glib::Threads::Mutex::Lock lci (current_image_lock);
_current_image.reset ();
}
outline_context->stroke ();
} else {
- const double height_2 = _height * .5;
+ const int height_zero = floor( _height * .5);
for (int i = 0; i < n_peaks; ++i) {
}
}
- /* zero line, show only if there is enough spread */
+ /* zero line, show only if there is enough spread
+ or the waveform line does not cross zero line */
- if (tips[i].spread >= 5.0 && show_zero_line()) {
- zero_context->move_to (i, floor(height_2));
+ if (show_zero_line() && ((tips[i].spread >= 5.0) || (tips[i].top > height_zero ) || (tips[i].bot < height_zero)) ) {
+ zero_context->move_to (i, height_zero);
zero_context->rel_line_to (1.0, 0);
}
clipped = true;
}
- if (!clipped) {
+ if (!clipped && tips[i].spread > 2.0) {
+ /* only draw the outline if the spread
+ implies 3 or more pixels (so that we see 1
+ white pixel in the middle).
+ */
outline_context->move_to (i, tips[i].bot);
/* normal lower terminal dot; line moves up */
outline_context->rel_line_to (0, -1.0);
}
if (!clipped) {
+ /* special case where only 1 pixel of
+ * the waveform line is drawn (and
+ * nothing else).
+ *
+ * we draw a 1px "line", pretending
+ * that the span is 1.0 (whether it is
+ * zero or 1.0)
+ */
wave_context->move_to (i, tips[i].top);
- /* special case where outline only is drawn.
- * we draw a 1px "line", pretending that the span is 1.0
- */
wave_context->rel_line_to (0, 1.0);
}
}
boost::shared_ptr<WaveViewCache::Entry>
WaveView::cache_request_result (boost::shared_ptr<WaveViewThreadRequest> req) const
{
+
boost::shared_ptr<WaveViewCache::Entry> ret (new WaveViewCache::Entry (req->channel,
req->height,
req->amplitude,
req->start,
req->end,
req->image));
- if (!images) {
- images = new WaveViewCache;
- }
-
images->add (_region->audio_source (_channel), ret);
/* consolidate cache first (removes fully-contained
* have an image there. if so, use it (and put it in the cache
* while we're here.
*/
+
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 CR %2 stop? %3 image %4\n", this, current_request,
+ (current_request ? current_request->should_stop() : false),
+ (current_request ? current_request->image : 0)));
if (current_request && !current_request->should_stop() && current_request->image) {
if (!ret || !full_image) {
if ((rendered && get_image_in_thread) || always_get_image_in_thread) {
-
+
DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: generating image in caller thread\n", name));
boost::shared_ptr<WaveViewThreadRequest> req (new WaveViewThreadRequest);
req->samples_per_pixel = _samples_per_pixel;
req->region = _region; /* weak ptr, to avoid storing a reference in the request queue */
req->channel = _channel;
- req->width = _canvas->visible_area().width();
req->height = _height;
req->fill_color = _fill_color;
req->amplitude = _region_amplitude * _amplitude_above_axis;
+ req->width = desired_image_width ();
/* draw image in this (the GUI thread) */
_height, _region_amplitude * _amplitude_above_axis, _fill_color, _samples_per_pixel, full);
}
+framecnt_t
+WaveView::desired_image_width () const
+{
+ /* compute how wide the image should be, in samples.
+ *
+ * We want at least 1 canvas width's worth, but if that
+ * represents less than 1/10th of a second, use 1/10th of
+ * a second instead.
+ */
+
+ framecnt_t canvas_width_samples = _canvas->visible_area().width() * _samples_per_pixel;
+ const framecnt_t one_tenth_of_second = _region->session().frame_rate() / 10;
+
+ if (canvas_width_samples > one_tenth_of_second) {
+ return canvas_width_samples;
+ }
+
+ return one_tenth_of_second;
+}
+
void
WaveView::queue_get_image (boost::shared_ptr<const ARDOUR::Region> region, framepos_t start, framepos_t end) const
{
req->samples_per_pixel = _samples_per_pixel;
req->region = _region; /* weak ptr, to avoid storing a reference in the request queue */
req->channel = _channel;
- req->width = _canvas->visible_area().width();
req->height = _height;
req->fill_color = _fill_color;
req->amplitude = _region_amplitude * _amplitude_above_axis;
+ req->width = desired_image_width ();
if (current_request) {
/* this will stop rendering in progress (which might otherwise
{
Glib::Threads::Mutex::Lock lm (request_queue_lock);
current_request = req;
- }
-
- /* this is always called from the GUI thread (there is only one), and
- * the same thread runs the idle callback chain. thus we do not need
- * any locks to protect idle_queued - it is only ever set or read in
- * the unitary GUI thread.
- */
-
- if (!idle_queued) {
- Glib::signal_idle().connect (sigc::mem_fun (*this, &WaveView::idle_send_request));
- idle_queued = true;
- }
-}
-bool
-WaveView::idle_send_request () const
-{
- Glib::Threads::Mutex::Lock lm (request_queue_lock);
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 now has current request %2\n", this, req));
- request_queue.insert (this);
- request_cond.signal (); /* wake thread - must be done while holding lock */
- idle_queued = false;
-
- return false; /* do not call from idle again */
+ if (request_queue.insert (this).second) {
+ /* this waveview was not already in the request queue, make sure we wake
+ the rendering thread in case it is asleep.
+ */
+ request_cond.signal ();
+ }
+ }
}
-
void
WaveView::generate_image (boost::shared_ptr<WaveViewThreadRequest> req, bool in_render_thread) const
{
if (!req->should_stop()) {
/* sample position is canonical here, and we want to generate
- * an image that spans about twice the canvas width
+ * an image that spans about 3x the canvas width. We get to that
+ * width by using an image sample count of the screen width added
+ * on each side of the desired image center.
*/
const framepos_t center = req->start + ((req->end - req->start) / 2);
- const framecnt_t image_samples = req->width * req->samples_per_pixel; /* one canvas width */
+ const framecnt_t image_samples = req->width;
/* we can request data from anywhere in the Source, between 0 and its length
*/
if (peaks_read > 0) {
+ /* region amplitude will have been used to generate the
+ * peak values already, but not the visual-only
+ * amplitude_above_axis. So apply that here before
+ * rendering.
+ */
+
if (_amplitude_above_axis != 1.0) {
for (framecnt_t i = 0; i < n_peaks; ++i) {
peaks[i].max *= _amplitude_above_axis;
} else {
draw_absent_image (req->image, peaks.get(), n_peaks);
}
+ } else {
+ cerr << "Request stopped before image generation\n";
}
if (in_render_thread && !req->should_stop()) {
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("done with request for %1 at %2 CR %3 req %4 range %5 .. %6\n", this, g_get_monotonic_time(), current_request, req, req->start, req->end));
const_cast<WaveView*>(this)->ImageReady (); /* emit signal */
}
+
+ cerr << "Generate image done, image = " << req->image << endl;
return;
}
return;
}
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("render %1 at %2\n", this, g_get_monotonic_time()));
+
/* a WaveView is intimately connected to an AudioRegion. It will
* display the waveform within the region, anywhere from the start of
* the region to its end.
double image_origin_in_self_coordinates;
boost::shared_ptr<WaveViewCache::Entry> image_to_draw;
+ Glib::Threads::Mutex::Lock lci (current_image_lock);
if (_current_image) {
/* check it covers the right sample range */
double draw_width;
if (image_to_draw != _current_image) {
+ lci.release ();
/* the image is guaranteed to start at or before
* draw_start. But if it starts before draw_start, that reduces
{
if (_global_shape != s) {
_global_shape = s;
+ if (images) {
+ images->clear_cache ();
+ }
VisualPropertiesChanged (); /* EMIT SIGNAL */
}
}
{
if (_global_logscaled != yn) {
_global_logscaled = yn;
+ if (images) {
+ images->clear_cache ();
+ }
VisualPropertiesChanged (); /* EMIT SIGNAL */
}
}
request_queue.erase (this);
current_request.reset ();
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 now has no request %2\n", this));
+
+}
+
+void
+WaveView::set_image_cache_size (uint64_t sz)
+{
+ if (!images) {
+ images = new WaveViewCache;
+ }
+
+ images->set_image_cache_threshold (sz);
}
/*-------------------------------------------------*/
request_cond.wait (request_queue_lock);
}
+ if (request_queue.empty()) {
+ assert (g_atomic_int_get (&drawing_thread_should_quit));
+ continue; // or break;
+ }
+
/* remove the request from the queue (remember: the "request"
* is just a pointer to a WaveView object)
*/
requestor = *(request_queue.begin());
request_queue.erase (request_queue.begin());
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("start request for %1 at %2\n", requestor, g_get_monotonic_time()));
+
boost::shared_ptr<WaveViewThreadRequest> req = requestor->current_request;
if (!req) {
continue;
}
- if (end <= e->end && start >= e->start) {
- /* found an image that covers the range we need */
+ switch (Evoral::coverage (start, end, e->start, e->end)) {
+ case Evoral::OverlapExternal: /* required range is inside image range */
DEBUG_TRACE (DEBUG::WaveView, string_compose ("found image spanning %1..%2 covers %3..%4\n",
e->start, e->end, start, end));
use (src, e);
- full_coverage = true;
- return e;
- }
-
- if (start >= e->start) {
- /* found an image that covers the start, but not the
- * end. See if it is longer than any other similar
- * partial image that we've found so far.
- */
-
- if ((e->end - e->start) > max_coverage) {
- best_partial = e;
- max_coverage = e->end - e->start;
- }
+ full_coverage = true;
+ return e;
+
+ case Evoral::OverlapStart: /* required range start is covered by image range */
+ if ((e->end - start) > max_coverage) {
+ best_partial = e;
+ max_coverage = e->end - start;
+ }
+ break;
+
+ case Evoral::OverlapNone:
+ case Evoral::OverlapEnd:
+ case Evoral::OverlapInternal:
+ break;
}
}
ce->timestamp = g_get_monotonic_time ();
cache_map[src].push_back (ce);
- cache_list.push_back (make_pair (src, ce));
}
uint64_t
void
WaveViewCache::cache_flush ()
{
- SortByTimestamp sorter;
+ /* Build a sortable list of all cache entries */
+
+ CacheList cache_list;
+ for (ImageCache::const_iterator cm = cache_map.begin(); cm != cache_map.end(); ++cm) {
+ for (CacheLine::const_iterator cl = cm->second.begin(); cl != cm->second.end(); ++cl) {
+ cache_list.push_back (make_pair (cm->first, *cl));
+ }
+ }
+
/* sort list in LRU order */
-
+ SortByTimestamp sorter;
sort (cache_list.begin(), cache_list.end(), sorter);
while (image_cache_size > _image_cache_threshold && !cache_map.empty() && !cache_list.empty()) {
for (CacheLine::iterator c = cl.begin(); c != cl.end(); ++c) {
if (*c == le.second) {
+
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("Removing cache line entry for %1\n", x->first->name()));
/* Remove this entry from this cache line */
cl.erase (c);
} else {
image_cache_size = 0;
}
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("cache shrunk to %1\n", image_cache_size));
}
/* Remove from the linear list, even if we didn't find it in
}
}
+void
+WaveViewCache::clear_cache ()
+{
+ DEBUG_TRACE (DEBUG::WaveView, "clear cache\n");
+ const uint64_t image_cache_threshold = _image_cache_threshold;
+ _image_cache_threshold = 0;
+ cache_flush ();
+ _image_cache_threshold = image_cache_threshold;
+}
+
void
WaveViewCache::set_image_cache_threshold (uint64_t sz)
{
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("new image cache size %1\n", sz));
_image_cache_threshold = sz;
cache_flush ();
}