From 2f6a428f05621c8195025095f12f2ccb6f8b9c95 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 22 Mar 2024 04:48:27 +0100 Subject: [PATCH] Overhaul and optimize thread-buffer allocation Every route calls Session::ensure_buffers when configuring processors. Previously that unconditionally invoked the BufferManager, even if no change was required. This also fixes a potential issue when bouncing tracks. ::write_one_track() increases the buffersize to 8k, but only for the ChanCount required to bounce. This was never properly reset. Furthermore this is in preparation for RegionFX which may need to increase the ChanCount of Threadbuffers. --- libs/ardour/ardour/session.h | 4 ++++ libs/ardour/session.cc | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h index c94cfdbab1..859659c2f1 100644 --- a/libs/ardour/ardour/session.h +++ b/libs/ardour/ardour/session.h @@ -311,6 +311,8 @@ public: BufferSet& get_route_buffers (ChanCount count = ChanCount::ZERO, bool silence = true); BufferSet& get_mix_buffers (ChanCount count = ChanCount::ZERO); + void ensure_buffers_unlocked (ChanCount howmany); + bool have_rec_enabled_track () const; bool have_rec_disabled_track () const; @@ -1513,6 +1515,8 @@ private: void setup_engine_resampling (); void ensure_buffers (ChanCount howmany = ChanCount::ZERO); + ChanCount _required_thread_buffers; + size_t _required_thread_buffersize; void process_without_events (pframes_t); void process_with_events (pframes_t); diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 69bb709c07..b05c314dc6 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -230,6 +230,7 @@ Session::Session (AudioEngine &eng, , _writable (false) , _under_nsm_control (false) , _xrun_count (0) + , _required_thread_buffersize (0) , master_wait_end (0) , post_export_sync (false) , post_export_position (0) @@ -2407,6 +2408,7 @@ Session::set_block_size (pframes_t nframes) * here. */ current_block_size = nframes; + _required_thread_buffersize = -1; ensure_buffers (); @@ -5848,13 +5850,33 @@ Session::tempo_map_changed () set_dirty (); } +void +Session::ensure_buffers_unlocked (ChanCount howmany) +{ + if (_required_thread_buffers >= howmany) { + return; + } + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + ensure_buffers (howmany); +} + /** Ensures that all buffers (scratch, send, silent, etc) are allocated for * the given count with the current block size. + * Must be called with the process-lock held */ void Session::ensure_buffers (ChanCount howmany) { - BufferManager::ensure_buffers (howmany, bounce_processing() ? bounce_chunk_size : 0); + size_t want_size = bounce_processing() ? bounce_chunk_size : 0; + if (howmany.n_total () == 0) { + howmany = _required_thread_buffers; + } + if (_required_thread_buffers >= howmany && _required_thread_buffersize == want_size) { + return; + } + _required_thread_buffers = ChanCount::max (_required_thread_buffers, howmany); + _required_thread_buffersize = want_size; + BufferManager::ensure_buffers (_required_thread_buffers, _required_thread_buffersize); } uint32_t