+ ClipLevelChanged ();
+ }
+}
+
+void
+WaveView::set_start_shift (double pixels)
+{
+ if (pixels < 0) {
+ return;
+ }
+
+ begin_visual_change ();
+ _start_shift = pixels;
+ end_visual_change ();
+}
+
+void
+WaveView::cancel_my_render_request () const
+{
+ if (!images) {
+ return;
+ }
+
+ /* try to stop any current rendering of the request, or prevent it from
+ * ever starting up.
+ */
+
+ Glib::Threads::Mutex::Lock lm (request_queue_lock);
+
+ if (current_request) {
+ current_request->cancel ();
+ }
+
+ /* now remove it from the queue and reset our request pointer so that
+ have no outstanding request (that we know about)
+ */
+
+ 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);
+}
+
+/*-------------------------------------------------*/
+
+void
+WaveView::start_drawing_thread ()
+{
+ if (!_drawing_thread) {
+ _drawing_thread = Glib::Threads::Thread::create (sigc::ptr_fun (WaveView::drawing_thread));
+ }
+}
+
+void
+WaveView::stop_drawing_thread ()
+{
+ while (_drawing_thread) {
+ Glib::Threads::Mutex::Lock lm (request_queue_lock);
+ g_atomic_int_set (&drawing_thread_should_quit, 1);
+ request_cond.signal ();
+ }
+}
+
+void
+WaveView::drawing_thread ()
+{
+ using namespace Glib::Threads;
+
+ WaveView const * requestor;
+ Mutex::Lock lm (request_queue_lock);
+ bool run = true;
+
+ while (run) {
+
+ /* remember that we hold the lock at this point, no matter what */
+
+ if (g_atomic_int_get (&drawing_thread_should_quit)) {
+ break;
+ }
+
+ if (request_queue.empty()) {
+ request_cond.wait (request_queue_lock);
+ }
+
+ if (request_queue.empty()) {
+ continue;
+ }
+
+ /* 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;
+ }
+
+ /* Generate an image. Unlock the request queue lock
+ * while we do this, so that other things can happen
+ * as we do rendering.
+ */
+
+ lm.release (); /* some RAII would be good here */
+
+ try {
+ requestor->generate_image (req, true);
+ } catch (...) {
+ req->image.clear(); /* just in case it was set before the exception, whatever it was */
+ }
+
+ lm.acquire ();
+
+ req.reset (); /* drop/delete request as appropriate */
+ }
+
+ /* thread is vanishing */
+ _drawing_thread = 0;
+}
+
+/*-------------------------------------------------*/
+
+WaveViewCache::WaveViewCache ()
+ : image_cache_size (0)
+ , _image_cache_threshold (100 * 1048576) /* bytes */
+{
+}
+
+WaveViewCache::~WaveViewCache ()
+{
+}
+
+
+boost::shared_ptr<WaveViewCache::Entry>
+WaveViewCache::lookup_image (boost::shared_ptr<ARDOUR::AudioSource> src,
+ framepos_t start, framepos_t end,
+ int channel,
+ Coord height,
+ float amplitude,
+ Color fill_color,
+ double samples_per_pixel,
+ bool& full_coverage)
+{
+ ImageCache::iterator x;
+
+ if ((x = cache_map.find (src)) == cache_map.end ()) {
+ /* nothing in the cache for this audio source at all */
+ return boost::shared_ptr<WaveViewCache::Entry> ();
+ }
+
+ CacheLine& caches = x->second;
+ boost::shared_ptr<Entry> best_partial;
+ framecnt_t max_coverage = 0;
+
+ /* Find a suitable ImageSurface, if it exists.
+ */
+
+ for (CacheLine::iterator c = caches.begin(); c != caches.end(); ++c) {
+
+ boost::shared_ptr<Entry> e (*c);
+
+ if (channel != e->channel
+ || height != e->height
+ || amplitude != e->amplitude
+ || samples_per_pixel != e->samples_per_pixel
+ || fill_color != e->fill_color) {
+ continue;
+ }
+
+ 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;
+
+ 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;
+ }
+ }
+
+ if (best_partial) {
+ DEBUG_TRACE (DEBUG::WaveView, string_compose ("found PARTIAL image spanning %1..%2 partially covers %3..%4\n",
+ best_partial->start, best_partial->end, start, end));
+ use (src, best_partial);
+ full_coverage = false;
+ return best_partial;