X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Fwave_view.cc;h=f4e56cb1d53f6fb2af75ca26a69276fdd9293efc;hb=bace0e85dcc1d6d06ded3d6174fbdc803d180744;hp=390fb5deab163d73c075d3bb8c60bc4b6568e654;hpb=ac1f7e0b9fe67fd2c0f848349396524f23e9f316;p=ardour.git diff --git a/libs/canvas/wave_view.cc b/libs/canvas/wave_view.cc index 390fb5deab..f4e56cb1d5 100644 --- a/libs/canvas/wave_view.cc +++ b/libs/canvas/wave_view.cc @@ -21,9 +21,14 @@ #include #include +#include + #include "gtkmm2ext/utils.h" +#include "gtkmm2ext/gui_thread.h" +#include "pbd/base_ui.h" #include "pbd/compose.h" +#include "pbd/convert.h" #include "pbd/signals.h" #include "pbd/stacktrace.h" @@ -31,11 +36,16 @@ #include "ardour/dB.h" #include "ardour/lmath.h" #include "ardour/audioregion.h" +#include "ardour/audiosource.h" +#include "ardour/session.h" -#include "canvas/wave_view.h" -#include "canvas/utils.h" #include "canvas/canvas.h" #include "canvas/colors.h" +#include "canvas/debug.h" +#include "canvas/utils.h" +#include "canvas/wave_view.h" + +#include "evoral/Range.hpp" #include @@ -43,17 +53,22 @@ using namespace std; using namespace ARDOUR; +using namespace PBD; using namespace ArdourCanvas; -#define CACHE_HIGH_WATER (2) - -std::map , std::vector > WaveView::_image_cache; double WaveView::_global_gradient_depth = 0.6; bool WaveView::_global_logscaled = false; WaveView::Shape WaveView::_global_shape = WaveView::Normal; bool WaveView::_global_show_waveform_clipping = true; double WaveView::_clip_level = 0.98853; +WaveViewCache* WaveView::images = 0; +gint WaveView::drawing_thread_should_quit = 0; +Glib::Threads::Mutex WaveView::request_queue_lock; +Glib::Threads::Cond WaveView::request_cond; +Glib::Threads::Thread* WaveView::_drawing_thread = 0; +WaveView::DrawingRequestQueue WaveView::request_queue; + PBD::Signal0 WaveView::VisualPropertiesChanged; PBD::Signal0 WaveView::ClipLevelChanged; @@ -73,16 +88,21 @@ WaveView::WaveView (Canvas* c, boost::shared_ptr region) , _logscaled_independent (false) , _gradient_depth_independent (false) , _amplitude_above_axis (1.0) - , _region_amplitude (_region->scale_amplitude ()) + , _region_amplitude (region->scale_amplitude ()) , _start_shift (0.0) , _region_start (region->start()) + , get_image_in_thread (false) + , always_get_image_in_thread (false) + , rendered (false) { - _region->DropReferences.connect (_source_invalidated_connection, MISSING_INVALIDATOR, - boost::bind (&ArdourCanvas::WaveView::invalidate_source, - this, boost::weak_ptr(_region->audio_source())), gui_context()); + 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)); + + ImageReady.connect (image_ready_connection, invalidator (*this), boost::bind (&WaveView::image_ready, this), gui_context()); } WaveView::WaveView (Item* parent, boost::shared_ptr region) @@ -101,23 +121,47 @@ WaveView::WaveView (Item* parent, boost::shared_ptr region) , _logscaled_independent (false) , _gradient_depth_independent (false) , _amplitude_above_axis (1.0) - , _region_amplitude (_region->scale_amplitude ()) + , _region_amplitude (region->scale_amplitude ()) + , _start_shift (0.0) , _region_start (region->start()) + , get_image_in_thread (false) + , always_get_image_in_thread (false) + , rendered (false) { - _region->DropReferences.connect (_source_invalidated_connection, MISSING_INVALIDATOR, - boost::bind (&ArdourCanvas::WaveView::invalidate_source, - this, boost::weak_ptr(_region->audio_source())), gui_context()); + 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)); + + ImageReady.connect (image_ready_connection, invalidator (*this), boost::bind (&WaveView::image_ready, this), gui_context()); } WaveView::~WaveView () { - _source_invalidated_connection.disconnect(); invalidate_image_cache (); } +string +WaveView::debug_name() const +{ + return _region->name() + string (":") + PBD::to_string (_channel+1, std::dec); +} + +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::set_always_get_image_in_thread (bool yn) +{ + always_get_image_in_thread = yn; +} + void WaveView::handle_visual_property_change () { @@ -189,12 +233,6 @@ WaveView::set_samples_per_pixel (double samples_per_pixel) } } -static inline double -window_to_image (double wave_origin, double image_start) -{ - return image_start - wave_origin; -} - static inline float _log_meter (float power, double lower_db, double upper_db, double non_linearity) { @@ -217,167 +255,74 @@ WaveView::set_clip_level (double dB) } } -void -WaveView::invalidate_source (boost::weak_ptr src) -{ - if (boost::shared_ptr source = src.lock()) { - - std::map , std::vector >::iterator i; - for (i = _image_cache.begin (); i != _image_cache.end (); ++i) { - if (i->first == source) { - for (uint32_t n = 0; n < i->second.size (); ++n) { - i->second[n].image.clear (); - } - i->second.clear (); - _image_cache.erase (i->first); - } - } - } -} - void WaveView::invalidate_image_cache () { - vector deletion_list; - vector caches; - - /* The source may have disappeared.*/ - - if (_region->n_channels() == 0) { - return; - } - - if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) { - caches = _image_cache.find (_region->audio_source ())->second; - } else { - return; - } - - for (uint32_t i = 0; i < caches.size (); ++i) { - - if (_channel != caches[i].channel - || _height != caches[i].height - || _region_amplitude != caches[i].amplitude - || _fill_color != caches[i].fill_color) { - - continue; - } - - deletion_list.push_back (i); - } - - while (deletion_list.size() > 0) { - caches[deletion_list.back ()].image.clear (); - caches.erase (caches.begin() + deletion_list.back()); - deletion_list.pop_back(); - } - - if (caches.size () == 0) { - _image_cache.erase(_region->audio_source ()); - } else { - _image_cache[_region->audio_source ()] = caches; - } + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 invalidates image cache and cancels current request\n", this)); + cancel_my_render_request (); + _current_image.reset (); } void -WaveView::consolidate_image_cache () const +WaveView::compute_tips (PeakData const & peak, WaveView::LineTips& tips) const { - list deletion_list; - vector caches; - uint32_t other_entries = 0; + const double effective_height = _height; - if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) { - caches = _image_cache.find (_region->audio_source ())->second; - } - - for (uint32_t i = 0; i < caches.size (); ++i) { - - if (_channel != caches[i].channel - || _height != caches[i].height - || _region_amplitude != caches[i].amplitude - || _fill_color != caches[i].fill_color) { + /* remember: canvas (and cairo) coordinate space puts the origin at the upper left. + + So, a sample value of 1.0 (0dbFS) will be computed as: - other_entries++; - continue; - } + (1.0 - 1.0) * 0.5 * effective_height - framepos_t segment_start = caches[i].start; - framepos_t segment_end = caches[i].end; + which evaluates to 0, or the top of the image. - for (uint32_t j = i; j < caches.size (); ++j) { + A sample value of -1.0 will be computed as - if (i == j || _channel != caches[j].channel - || _height != caches[i].height - || _region_amplitude != caches[i].amplitude - || _fill_color != caches[i].fill_color) { + (1.0 + 1.0) * 0.5 * effective height - continue; - } + which evaluates to effective height, or the bottom of the image. + */ - if (caches[j].start >= segment_start && caches[j].end <= segment_end) { + const double pmax = (1.0 - peak.max) * 0.5 * effective_height; + const double pmin = (1.0 - peak.min) * 0.5 * effective_height; - deletion_list.push_back (j); - } - } - } - - deletion_list.sort (); - deletion_list.unique (); + /* remember that the bottom of the image (pmin) has larger y-coordinates + than the top (pmax). + */ - while (deletion_list.size() > 0) { - caches[deletion_list.back ()].image.clear (); - caches.erase (caches.begin() + deletion_list.back ()); - deletion_list.pop_back(); - } + double spread = (pmin - pmax) * 0.5; - /* We don't care if this channel/height/amplitude has anything in the cache - just drop the Last Added entries - until we reach a size where there is a maximum of CACHE_HIGH_WATER + other entries. - */ + /* find the nearest pixel to the nominal center. */ + const double center = round (pmin - spread); - while (caches.size() > CACHE_HIGH_WATER + other_entries) { - caches.front ().image.clear (); - caches.erase(caches.begin ()); - } + if (spread < 1.0) { + /* minimum distance between line ends is 1 pixel, and we want it "centered" on a pixel, + as per cairo single-pixel line issues. - if (caches.size () == 0) { - _image_cache.erase (_region->audio_source ()); + NOTE: the caller will not draw a line between these two points if the spread is + less than 2 pixels. So only the tips.top value matters, which is where we will + draw a single pixel as part of the outline. + */ + tips.top = center; + tips.bot = center + 1.0; } else { - _image_cache[_region->audio_source ()] = caches; + /* round spread above and below center to an integer number of pixels */ + spread = round (spread); + /* top and bottom are located equally either side of the center */ + tips.top = center - spread; + tips.bot = center + spread; } + + tips.top = min (effective_height, max (0.0, tips.top)); + tips.bot = min (effective_height, max (0.0, tips.bot)); } + Coord -WaveView::y_extent (double s, bool /*round_to_lower_edge*/) const +WaveView::y_extent (double s) const { - /* it is important that this returns an integral value, so that we - * can ensure correct single pixel behaviour. - * - * we need (_height - max(wave_line_width)) - * wave_line_width == 1 IFF top==bottom (1 sample per pixel or flat line) - * wave_line_width == 2 otherwise - * then round away from the zero line, towards peak - */ - if (_shape == Rectified) { - // we only ever have 1 point and align to the bottom (not center) - return floor ((1.0 - s) * (_height - 2.0)); - } else { - /* currently canvas rectangle is off-by-one and we - * cannot draw a pixel at 0 (-.5 .. +.5) without it being - * clipped. A value 1.0 (ideally one point at y=0) ends - * up a pixel down. and a value of -1.0 (ideally y = _height-1) - * currently is on the bottom separator line :( - * So to make the complete waveform appear centered in - * a region, we translate by +1.5 (instead of -.5) - * and scale to height - 2.5 (if we scale to height - 2.0 - * then the bottom most pixel may bleed into the selection rect - * by 0.5 px) - * - */ - Coord pos; - pos = floor ((1.0 - s) * .5 * (_height - 2.5)); - - return min (_height - 2.5, (max (0.0, pos))); - } + assert (_shape == Rectified); + return floor ((1.0 - s) * _height); } void @@ -412,16 +357,6 @@ WaveView::draw_absent_image (Cairo::RefPtr& image, PeakData context->fill (); } -struct LineTips { - double top; - double bot; - double spread; - bool clip_max; - bool clip_min; - - LineTips() : top (0.0), bot (0.0), clip_max (false), clip_min (false) {} -}; - struct ImageSet { Cairo::RefPtr wave; Cairo::RefPtr outline; @@ -433,7 +368,7 @@ struct ImageSet { }; void -WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peaks, int n_peaks) const +WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peaks, int n_peaks, boost::shared_ptr req) const { ImageSet images; @@ -464,7 +399,7 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak has been scaled by scale_amplitude() already. */ - const double clip_level = _clip_level * _region_amplitude; + const double clip_level = _clip_level * req->amplitude; if (_shape == WaveView::Rectified) { @@ -478,8 +413,8 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak tips[i].bot = height() - 1.0; const double p = alt_log_meter (fast_coefficient_to_dB (max (fabs (_peaks[i].max), fabs (_peaks[i].min)))); - tips[i].top = y_extent (p, false); - tips[i].spread = p * (_height - 1.0); + tips[i].top = y_extent (p); + tips[i].spread = p * _height; if (_peaks[i].max >= clip_level) { tips[i].clip_max = true; @@ -495,8 +430,8 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak tips[i].bot = height() - 1.0; const double p = max(fabs (_peaks[i].max), fabs (_peaks[i].min)); - tips[i].top = y_extent (p, false); - tips[i].spread = p * (_height - 2.0); + tips[i].top = y_extent (p); + tips[i].spread = p * _height; if (p >= clip_level) { tips[i].clip_max = true; } @@ -508,34 +443,34 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak if (_logscaled) { for (int i = 0; i < n_peaks; ++i) { - double top = _peaks[i].max; - double bot = _peaks[i].min; + PeakData p; + p.max = _peaks[i].max; + p.min = _peaks[i].min; if (_peaks[i].max >= clip_level) { - tips[i].clip_max = true; + tips[i].clip_max = true; } if (-(_peaks[i].min) >= clip_level) { tips[i].clip_min = true; } - if (top > 0.0) { - top = alt_log_meter (fast_coefficient_to_dB (top)); - } else if (top < 0.0) { - top =-alt_log_meter (fast_coefficient_to_dB (-top)); + if (p.max > 0.0) { + p.max = alt_log_meter (fast_coefficient_to_dB (p.max)); + } else if (p.max < 0.0) { + p.max =-alt_log_meter (fast_coefficient_to_dB (-p.max)); } else { - top = 0.0; + p.max = 0.0; } - if (bot > 0.0) { - bot = alt_log_meter (fast_coefficient_to_dB (bot)); - } else if (bot < 0.0) { - bot = -alt_log_meter (fast_coefficient_to_dB (-bot)); + if (p.min > 0.0) { + p.min = alt_log_meter (fast_coefficient_to_dB (p.min)); + } else if (p.min < 0.0) { + p.min = -alt_log_meter (fast_coefficient_to_dB (-p.min)); } else { - bot = 0.0; + p.min = 0.0; } - - tips[i].top = y_extent (top, false); - tips[i].bot = y_extent (bot, true); + + compute_tips (p, tips[i]); tips[i].spread = tips[i].bot - tips[i].top; } @@ -548,13 +483,17 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak tips[i].clip_min = true; } - tips[i].top = y_extent (_peaks[i].max, false); - tips[i].bot = y_extent (_peaks[i].min, true); + compute_tips (_peaks[i], tips[i]); tips[i].spread = tips[i].bot - tips[i].top; } } } + + if (req->should_stop()) { + return; + } + Color alpha_one = rgba_to_color (0, 0, 0, 1.0); set_source_rgba (wave_context, alpha_one); @@ -565,16 +504,16 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak /* ensure single-pixel lines */ wave_context->set_line_width (1.0); - wave_context->translate (0.5, +1.5); + wave_context->translate (0.5, 0.5); outline_context->set_line_width (1.0); - outline_context->translate (0.5, +1.5); + outline_context->translate (0.5, 0.5); clip_context->set_line_width (1.0); - clip_context->translate (0.5, +1.5); + clip_context->translate (0.5, 0.5); zero_context->set_line_width (1.0); - zero_context->translate (0.5, +1.5); + zero_context->translate (0.5, 0.5); /* the height of the clip-indicator should be at most 7 pixels, * or 5% of the height of the waveview item. @@ -619,15 +558,12 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak } /* clip indicator */ - bool clipped = false; + if (_global_show_waveform_clipping && (tips[i].clip_max || tips[i].clip_min)) { clip_context->move_to (i, tips[i].top); /* clip-indicating upper terminal line */ clip_context->rel_line_to (0, min (clip_height, ceil(tips[i].spread + .5))); - clipped = true; - } - - if (!clipped) { + } else { outline_context->move_to (i, tips[i].top); /* normal upper terminal dot */ outline_context->rel_line_to (0, -1.0); @@ -639,7 +575,7 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak outline_context->stroke (); } else { - const double height_2 = (_height - 2.5) * .5; + const int height_zero = floor( _height * .5); for (int i = 0; i < n_peaks; ++i) { @@ -649,6 +585,7 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak wave_context->move_to (i, tips[i].top); wave_context->line_to (i, tips[i].bot); } + /* draw square waves and other discontiguous points clearly */ if (i > 0) { if (tips[i-1].top + 2 < tips[i].top) { @@ -664,10 +601,11 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak } } - /* zero line */ + /* 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); } @@ -688,13 +626,17 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak clipped = true; } - if (!clipped) { - outline_context->move_to (i, tips[i].bot + 1.0); - /* normal lower terminal dot */ + 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); - outline_context->move_to (i, tips[i].top - 1.0); - /* normal upper terminal dot */ + outline_context->move_to (i, tips[i].top); + /* normal upper terminal dot, line moves down */ outline_context->rel_line_to (0, 1.0); } } else { @@ -708,10 +650,15 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak } 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); } } @@ -723,6 +670,10 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak zero_context->stroke (); } + if (req->should_stop()) { + return; + } + Cairo::RefPtr context = Cairo::Context::create (image); /* Here we set a source colour and use the various components as a mask. */ @@ -763,6 +714,10 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak set_source_rgba (context, _fill_color); } + if (req->should_stop()) { + return; + } + context->mask (images.wave, 0, 0); context->fill (); @@ -777,94 +732,334 @@ WaveView::draw_image (Cairo::RefPtr& image, PeakData* _peak set_source_rgba (context, _zero_color); context->mask (images.zero, 0, 0); context->fill (); - } -void -WaveView::get_image (Cairo::RefPtr& image, framepos_t start, framepos_t end, double& image_offset) const +boost::shared_ptr +WaveView::cache_request_result (boost::shared_ptr req) const { - vector caches; + + boost::shared_ptr ret (new WaveViewCache::Entry (req->channel, + req->height, + req->amplitude, + req->fill_color, + req->samples_per_pixel, + req->start, + req->end, + req->image)); + images->add (_region->audio_source (_channel), ret); + + /* consolidate cache first (removes fully-contained + * duplicate images) + */ + + images->consolidate_image_cache (_region->audio_source (_channel), + req->channel, req->height, req->amplitude, + req->fill_color, req->samples_per_pixel); - if (_image_cache.find (_region->audio_source ()) != _image_cache.end ()) { + return ret; +} - caches = _image_cache.find (_region->audio_source ())->second; - } +boost::shared_ptr +WaveView::get_image (framepos_t start, framepos_t end, bool& full_image) const +{ + boost::shared_ptr ret; - /* Find a suitable ImageSurface. + full_image = true; + + /* this is called from a ::render() call, when we need an image to + draw with. */ - for (uint32_t i = 0; i < caches.size (); ++i) { - if (_channel != caches[i].channel - || _height != caches[i].height - || _region_amplitude != caches[i].amplitude - || _fill_color != caches[i].fill_color) { + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 needs image from %2 .. %3\n", name, start, end)); + + + { + Glib::Threads::Mutex::Lock lmq (request_queue_lock); - continue; + /* if there's a draw request outstanding, check to see if we + * 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) { + + /* put the image into the cache so that other + * WaveViews can use it if it is useful + */ + + if (current_request->start <= start && current_request->end >= end) { + + ret.reset (new WaveViewCache::Entry (current_request->channel, + current_request->height, + current_request->amplitude, + current_request->fill_color, + current_request->samples_per_pixel, + current_request->start, + current_request->end, + current_request->image)); + + cache_request_result (current_request); + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: got image from completed request, spans %2..%3\n", + name, current_request->start, current_request->end)); + } + + /* drop our handle on the current request */ + current_request.reset (); } + } - framepos_t segment_start = caches[i].start; - framepos_t segment_end = caches[i].end; + if (!ret) { - if (end <= segment_end && start >= segment_start) { - image_offset = (segment_start - _region_start) / _samples_per_pixel; - image = caches[i].image; + /* no current image draw request, so look in the cache */ + + ret = get_image_from_cache (start, end, full_image); + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1: lookup from cache gave %2 (full %3)\n", + name, ret, full_image)); - return; + } + + + + 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 req (new WaveViewThreadRequest); + + req->type = WaveViewThreadRequest::Draw; + req->start = start; + req->end = end; + 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->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) */ + + generate_image (req, false); + + /* cache the result */ + + ret = cache_request_result (req); + + /* reset this so that future missing images are + * generated in a a worker thread. + */ + + get_image_in_thread = false; + + } else { + queue_get_image (_region, start, end); } } - consolidate_image_cache (); + if (ret) { + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 got an image from %2 .. %3 (full ? %4)\n", name, ret->start, ret->end, full_image)); + } else { + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 no useful image available\n", name)); + } - /* sample position is canonical here, and we want to generate - * an image that spans about twice the canvas width - */ + return ret; +} - const framepos_t center = start + ((end - start) / 2); - const framecnt_t canvas_samples = _canvas->visible_area().width() * _samples_per_pixel; /* one canvas width */ +boost::shared_ptr +WaveView::get_image_from_cache (framepos_t start, framepos_t end, bool& full) const +{ + if (!images) { + return boost::shared_ptr(); + } - /* we can request data from anywhere in the Source, between 0 and its length - */ + return images->lookup_image (_region->audio_source (_channel), start, end, _channel, + _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; +} - framepos_t sample_start = max ((framepos_t) 0, (center - canvas_samples)); - framepos_t sample_end = min (center + canvas_samples, _region->source_length (0)); +void +WaveView::queue_get_image (boost::shared_ptr region, framepos_t start, framepos_t end) const +{ + boost::shared_ptr req (new WaveViewThreadRequest); + + req->type = WaveViewThreadRequest::Draw; + req->start = start; + req->end = end; + 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->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 + be long lived) for any current request. + */ + current_request->cancel (); + } - const int n_peaks = llrintf ((sample_end - sample_start)/ (double) _samples_per_pixel); + start_drawing_thread (); - boost::scoped_array peaks (new PeakData[n_peaks]); + /* swap requests (protected by lock) */ - framecnt_t peaks_read; - peaks_read = _region->read_peaks (peaks.get(), n_peaks, - sample_start, sample_end - sample_start, - _channel, - _samples_per_pixel); + { + Glib::Threads::Mutex::Lock lm (request_queue_lock); + current_request = req; - image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, _height); + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 now has current request %2\n", this, req)); - if (peaks_read > 0) { - draw_image (image, peaks.get(), n_peaks); - } else { - draw_absent_image (image, peaks.get(), n_peaks); + 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 (); + } } +} - _image_cache[_region->audio_source ()].push_back (CacheEntry (_channel, _height, _region_amplitude, _fill_color, sample_start, sample_end, image)); - - image_offset = (sample_start - _region->start()) / _samples_per_pixel; +void +WaveView::generate_image (boost::shared_ptr req, bool in_render_thread) const +{ + if (!req->should_stop()) { - //cerr << "_image_cache size is : " << _image_cache.size() << " entries for this audiosource : " << _image_cache.find (_region->audio_source ())->second.size() << endl; + /* sample position is canonical here, and we want to generate + * 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; + + /* we can request data from anywhere in the Source, between 0 and its length + */ + + framepos_t sample_start = max (_region_start, (center - image_samples)); + framepos_t sample_end = min (center + image_samples, region_end()); + const int n_peaks = llrintf ((sample_end - sample_start)/ (req->samples_per_pixel)); + + boost::scoped_array peaks (new PeakData[n_peaks]); + + /* Note that Region::read_peaks() takes a start position based on an + offset into the Region's **SOURCE**, rather than an offset into + the Region itself. + */ + + framecnt_t peaks_read = _region->read_peaks (peaks.get(), n_peaks, + sample_start, sample_end - sample_start, + req->channel, + req->samples_per_pixel); + + req->image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, n_peaks, req->height); + /* make sure we record the sample positions that were actually used */ + req->start = sample_start; + req->end = sample_end; + + 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; + peaks[i].min *= _amplitude_above_axis; + } + } + draw_image (req->image, peaks.get(), n_peaks, req); + } else { + draw_absent_image (req->image, peaks.get(), n_peaks); + } + } + + 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(this)->ImageReady (); /* emit signal */ + } + return; } +/** Given a waveform that starts at window x-coordinate @param wave_origin + * and the first pixel that we will actually draw @param draw_start, return + * the offset into an image of the entire waveform that we will need to use. + * + * Note: most of our cached images are NOT of the entire waveform, this is just + * computationally useful when determining which the sample range span for + * the image we need. + */ +static inline double +window_to_image (double wave_origin, double image_start) +{ + return image_start - wave_origin; +} + void WaveView::render (Rect const & area, Cairo::RefPtr context) const { assert (_samples_per_pixel != 0); - + if (!_region) { return; } - Rect self = item_to_window (Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height)); + 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. + * + * the area we've been aked to render may overlap with area covered + * by the region in any of the normal ways: + * + * - it may begin and end within the area covered by the region + * - it may start before and end after the area covered by region + * - it may start before and end within the area covered by the region + * - it may start within and end after the area covered by the region + * - it may be precisely coincident with the area covered by region. + * + * So let's start by determining the area covered by the region, in + * window coordinates. It begins at zero (in item coordinates for this + * waveview, and extends to region_length() / _samples_per_pixel. + */ + + Rect self = item_to_window (Rect (0.0, 0.0, region_length() / _samples_per_pixel, _height)); + + // cerr << name << " RENDER " << area << " self = " << self << endl; + + /* Now lets get the intersection with the area we've been asked to draw */ + boost::optional d = self.intersection (area); if (!d) { @@ -873,6 +1068,10 @@ WaveView::render (Rect const & area, Cairo::RefPtr context) cons Rect draw = d.get(); + /* "draw" is now a rectangle that defines the rectangle we need to + * update/render the waveview into, in window coordinate space. + */ + /* window coordinates - pixels where x=0 is the left edge of the canvas * window. We round down in case we were asked to * draw "between" pixels at the start and/or end. @@ -881,32 +1080,86 @@ WaveView::render (Rect const & area, Cairo::RefPtr context) cons double draw_start = floor (draw.x0); const double draw_end = floor (draw.x1); - // cerr << "Need to draw " << draw_start << " .. " << draw_end << endl; - + // cerr << "Need to draw " << draw_start << " .. " << draw_end << " vs. " << area << " and self = " << self << endl; + /* image coordnates: pixels where x=0 is the start of this waveview, * wherever it may be positioned. thus image_start=N means "an image - * that beings N pixels after the start of region that this waveview is + * that begins N pixels after the start of region that this waveview is * representing. */ const framepos_t image_start = window_to_image (self.x0, draw_start); const framepos_t image_end = window_to_image (self.x0, draw_end); - + // cerr << "Image/WV space: " << image_start << " .. " << image_end << endl; - - /* sample coordinates - note, these are not subject to rounding error */ + + /* sample coordinates - note, these are not subject to rounding error + * + * "sample_start = N" means "the first sample we need to represent is N + * samples after the first sample of the region" + */ + framepos_t sample_start = _region_start + (image_start * _samples_per_pixel); framepos_t sample_end = _region_start + (image_end * _samples_per_pixel); - // cerr << "Sample space: " << sample_start << " .. " << sample_end << endl; + // cerr << "Sample space: " << sample_start << " .. " << sample_end << " @ " << _samples_per_pixel << " rs = " << _region_start << endl; + + /* sample_start and sample_end are bounded by the region + * limits. sample_start, because of the was just computed, must already + * be greater than or equal to the _region_start value. + */ + + sample_end = min (region_end(), sample_end); + + // cerr << debug_name() << " will need image spanning " << sample_start << " .. " << sample_end << " region spans " << _region_start << " .. " << region_end() << endl; - Cairo::RefPtr image; - double image_offset = 0; + double image_origin_in_self_coordinates; + boost::shared_ptr image_to_draw; + + if (_current_image) { - get_image (image, sample_start, sample_end, image_offset); + /* check it covers the right sample range */ + + if (_current_image->start > sample_start || _current_image->end < sample_end) { + /* doesn't cover the area we need ... reset */ + _current_image.reset (); + } else { + /* timestamp our continuing use of this image/cache entry */ + images->use (_region->audio_source (_channel), _current_image); + image_to_draw = _current_image; + } + } - // cerr << "Offset into image to place at zero: " << image_offset << endl; + if (!image_to_draw) { + /* look it up */ + + bool full_image; + image_to_draw = get_image (sample_start, sample_end, full_image); + + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 image to draw = %2 (full? %3)\n", name, image_to_draw, full_image)); + + if (!image_to_draw) { + /* image not currently available. A redraw will be scheduled + when it is ready. + */ + return; + } + + if (full_image) { + /* found an image that covers our entire sample range, + * so keep a reference to it. + */ + _current_image = image_to_draw; + } + } + + /* compute the first pixel of the image that should be used when we + * render the specified range. + */ + + image_origin_in_self_coordinates = (image_to_draw->start - _region_start) / _samples_per_pixel; + if (_start_shift && (sample_start == _region_start) && (self.x0 == draw.x0)) { /* we are going to draw the first pixel for this region, but we may not want this to overlap a border around the @@ -914,32 +1167,72 @@ WaveView::render (Rect const & area, Cairo::RefPtr context) cons */ //cerr << name.substr (23) << " ss = " << sample_start << " rs = " << _region_start << " sf = " << _start_shift << " ds = " << draw_start << " self = " << self << " draw = " << draw << endl; //draw_start += _start_shift; - //image_offset += _start_shift; + //image_origin_in_self_coordinates += _start_shift; } - context->rectangle (draw_start, draw.y0, draw_end - draw_start, draw.height()); + /* the image may only be a best-effort ... it may not span the entire + * range requested, though it is guaranteed to cover the start. So + * determine how many pixels we can actually draw. + */ + + double draw_width; + + if (image_to_draw != _current_image) { + + /* the image is guaranteed to start at or before + * draw_start. But if it starts before draw_start, that reduces + * the maximum available width we can render with. + * + * so .. clamp the draw width to the smaller of what we need to + * draw or the available width of the image. + */ + + draw_width = min ((double) image_to_draw->image->get_width() - (draw_start - image_to_draw->start), + (draw_end - draw_start)); + + + DEBUG_TRACE (DEBUG::WaveView, string_compose ("%1 draw just %2 of %3 (iwidth %4 off %5 img @ %6 rs @ %7)\n", name, draw_width, (draw_end - draw_start), + image_to_draw->image->get_width(), image_origin_in_self_coordinates, + image_to_draw->start, _region_start)); + } else { + draw_width = draw_end - draw_start; + DEBUG_TRACE (DEBUG::WaveView, string_compose ("use current image, span entire render width %1..%2\n", draw_start, draw_end)); + } + + context->rectangle (draw_start, draw.y0, draw_width, draw.height()); /* round image origin position to an exact pixel in device space to * avoid blurring */ - double x = self.x0 + image_offset; + double x = self.x0 + image_origin_in_self_coordinates; double y = self.y0; context->user_to_device (x, y); x = round (x); y = round (y); context->device_to_user (x, y); - context->set_source (image, x, y); + /* the coordinates specify where in "user coordinates" (i.e. what we + * generally call "canvas coordinates" in this code) the image origin + * will appear. So specifying (10,10) will put the upper left corner of + * the image at (10,10) in user space. + */ + + context->set_source (image_to_draw->image, x, y); context->fill (); + /* image obtained, some of it painted to display: we are rendered. + Future calls to get_image_in_thread are now meaningful. + */ + + rendered = true; } void WaveView::compute_bounding_box () const { if (_region) { - _bounding_box = Rect (0.0, 0.0, _region->length() / _samples_per_pixel, _height); + _bounding_box = Rect (0.0, 0.0, region_length() / _samples_per_pixel, _height); } else { _bounding_box = boost::optional (); } @@ -955,7 +1248,8 @@ WaveView::set_height (Distance height) invalidate_image_cache (); _height = height; - + get_image_in_thread = true; + _bounding_box_dirty = true; end_change (); } @@ -992,6 +1286,7 @@ WaveView::gain_changed () begin_visual_change (); invalidate_image_cache (); _region_amplitude = _region->scale_amplitude (); + get_image_in_thread = true; end_visual_change (); } @@ -1042,10 +1337,11 @@ WaveView::set_shape (Shape s) void WaveView::set_amplitude_above_axis (double a) { - if (_amplitude_above_axis != a) { + if (fabs (_amplitude_above_axis - a) > 0.01) { begin_visual_change (); invalidate_image_cache (); _amplitude_above_axis = a; + get_image_in_thread = true; end_visual_change (); } } @@ -1068,6 +1364,18 @@ WaveView::set_global_logscaled (bool yn) } } +framecnt_t +WaveView::region_length() const +{ + return _region->length() - (_region_start - _region->start()); +} + +framepos_t +WaveView::region_end() const +{ + return _region_start + region_length(); +} + void WaveView::set_region_start (frameoffset_t start) { @@ -1130,4 +1438,399 @@ WaveView::set_start_shift (double pixels) _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. + */ + + if (current_request) { + current_request->cancel (); + } + + Glib::Threads::Mutex::Lock lm (request_queue_lock); + + /* 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 () +{ + if (_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); + } + + /* 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 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. + */ + + request_queue_lock.unlock (); /* 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 */ + } + + request_queue_lock.lock (); + + 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::lookup_image (boost::shared_ptr 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 (); + } + + CacheLine& caches = x->second; + boost::shared_ptr 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 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; + } + + return boost::shared_ptr (); +} + +void +WaveViewCache::consolidate_image_cache (boost::shared_ptr src, + int channel, + Coord height, + float amplitude, + Color fill_color, + double samples_per_pixel) +{ + list deletion_list; + uint32_t other_entries = 0; + ImageCache::iterator x; + + /* MUST BE CALLED FROM (SINGLE) GUI THREAD */ + + if ((x = cache_map.find (src)) == cache_map.end ()) { + return; + } + + CacheLine& caches = x->second; + + for (CacheLine::iterator c1 = caches.begin(); c1 != caches.end(); ) { + + CacheLine::iterator nxt = c1; + ++nxt; + + boost::shared_ptr e1 (*c1); + + if (channel != e1->channel + || height != e1->height + || amplitude != e1->amplitude + || samples_per_pixel != e1->samples_per_pixel + || fill_color != e1->fill_color) { + + /* doesn't match current properties, ignore and move on + * to the next one. + */ + + other_entries++; + c1 = nxt; + continue; + } + + /* c1 now points to a cached image entry that matches current + * properties. Check all subsequent cached imaged entries to + * see if there are others that also match but represent + * subsets of the range covered by this one. + */ + + for (CacheLine::iterator c2 = c1; c2 != caches.end(); ) { + + CacheLine::iterator nxt2 = c2; + ++nxt2; + + boost::shared_ptr e2 (*c2); + + if (e1 == e2 || channel != e2->channel + || height != e2->height + || amplitude != e2->amplitude + || samples_per_pixel != e2->samples_per_pixel + || fill_color != e2->fill_color) { + + /* properties do not match, ignore for the + * purposes of consolidation. + */ + c2 = nxt2; + continue; + } + + if (e2->start >= e1->start && e2->end <= e1->end) { + /* c2 is fully contained by c1, so delete it */ + caches.erase (c2); + + /* and re-start the whole iteration */ + nxt = caches.begin (); + break; + } + + c2 = nxt2; + } + + c1 = nxt; + } +} + +void +WaveViewCache::use (boost::shared_ptr src, boost::shared_ptr ce) +{ + ce->timestamp = g_get_monotonic_time (); +} + +void +WaveViewCache::add (boost::shared_ptr src, boost::shared_ptr ce) +{ + /* MUST BE CALLED FROM (SINGLE) GUI THREAD */ + Cairo::RefPtr img (ce->image); + + image_cache_size += img->get_height() * img->get_width () * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */ + + if (cache_full()) { + cache_flush (); + } + + ce->timestamp = g_get_monotonic_time (); + + cache_map[src].push_back (ce); +} + +uint64_t +WaveViewCache::compute_image_cache_size() +{ + uint64_t total = 0; + for (ImageCache::iterator s = cache_map.begin(); s != cache_map.end(); ++s) { + CacheLine& per_source_cache (s->second); + for (CacheLine::iterator c = per_source_cache.begin(); c != per_source_cache.end(); ++c) { + Cairo::RefPtr img ((*c)->image); + total += img->get_height() * img->get_width() * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */ + } + } + return total; +} + +bool +WaveViewCache::cache_full() +{ + return image_cache_size > _image_cache_threshold; +} + +void +WaveViewCache::cache_flush () +{ + /* 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()) { + + ListEntry& le (cache_list.front()); + + ImageCache::iterator x; + + if ((x = cache_map.find (le.first)) != cache_map.end ()) { + + CacheLine& cl = x->second; + + 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); + + if (cl.empty()) { + /* remove cache line from main cache: no more entries */ + cache_map.erase (x); + } + + break; + } + } + + Cairo::RefPtr img (le.second->image); + uint64_t size = img->get_height() * img->get_width() * 4; /* 4 = bytes per FORMAT_ARGB32 pixel */ + + if (image_cache_size > size) { + image_cache_size -= size; + } 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 + * the actual cache_mao + */ + cache_list.erase (cache_list.begin()); + } +} + +void +WaveViewCache::set_image_cache_threshold (uint64_t sz) +{ + DEBUG_TRACE (DEBUG::WaveView, string_compose ("new image cache size \n", sz)); + _image_cache_threshold = sz; + cache_flush (); +}