Temporarily memoize IPlugView::canResize()

This makes VST3 plugin resizing more responsive, because this function
would otherwise be constantly running in lockstep from the GUI thread.
This commit is contained in:
Robbert van der Helm
2021-05-05 19:44:44 +02:00
parent 2c33048137
commit 9424c36993
5 changed files with 59 additions and 6 deletions
@@ -244,8 +244,30 @@ Vst3PlugViewProxyImpl::setFrame(Steinberg::IPlugFrame* frame) {
}
tresult PLUGIN_API Vst3PlugViewProxyImpl::canResize() {
return send_mutually_recursive_message(
YaPlugView::CanResize{.owner_instance_id = owner_instance_id()});
const auto request =
YaPlugView::CanResize{.owner_instance_id = owner_instance_id()};
{
std::lock_guard lock(can_resize_cache_mutex);
if (const tresult* result = can_resize_cache.get_and_keep_alive(5)) {
const bool log_response = bridge.logger.log_request(true, request);
if (log_response) {
bridge.logger.log_response(
true, YaPlugView::CanResize::Response(*result));
}
return *result;
}
}
const UniversalTResult result = send_mutually_recursive_message(request);
{
std::lock_guard lock(can_resize_cache_mutex);
can_resize_cache.set(result, 5);
}
return result;
}
tresult PLUGIN_API
@@ -301,4 +301,20 @@ class Vst3PlugViewProxyImpl : public Vst3PlugViewProxy {
* This will be an `std::nullopt` if the hostdoes not support `IRunLoop`.
*/
std::optional<RunLoopTasks> run_loop_tasks;
// Caches
/**
* During resizing the host will likely constantly ask the plugin if it can
* be freely resized. Even if it is technically possible, I'm not aware of
* any plugins that change from not being able arbitrarily resizeable to
* being able to be resized like this. The reason why we might want to cache
* `IPlugView::canResize()` is because this function has to be run on the
* GUI thread, just like `IPlugView::onSize()` and
* `IPlugView::checkSizeConstraint`. Everything running in lockstep makes
* resizing a lot laggier than they would have to be, so as a compromise
* we'll remember this value for the duration of the resize.
*/
TimedValueCache<tresult> can_resize_cache;
std::mutex can_resize_cache_mutex;
};