From 0675feb54b3b105d512329c5c391fd32f90a8a1d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 16 Feb 2022 20:43:53 +0100 Subject: [PATCH] Keep a reference to WrapperView in WrapperInner --- src/wrapper/vst3/inner.rs | 9 ++++++++- src/wrapper/vst3/util.rs | 16 ++++++++-------- src/wrapper/vst3/view.rs | 6 +++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/wrapper/vst3/inner.rs b/src/wrapper/vst3/inner.rs index 9582b479..cae4ac20 100644 --- a/src/wrapper/vst3/inner.rs +++ b/src/wrapper/vst3/inner.rs @@ -8,7 +8,8 @@ use vst3_sys::base::{kInvalidArgument, kResultOk, tresult}; use vst3_sys::vst::IComponentHandler; use super::context::WrapperProcessContext; -use super::util::{VstPtr, BYPASS_PARAM_HASH, BYPASS_PARAM_ID}; +use super::util::{ObjectPtr, VstPtr, BYPASS_PARAM_HASH, BYPASS_PARAM_ID}; +use super::view::WrapperView; use crate::buffer::Buffer; use crate::context::{EventLoop, GuiContext, MainThreadExecutor, OsEventLoop}; use crate::param::internals::ParamPtr; @@ -30,6 +31,10 @@ pub(crate) struct WrapperInner { /// `IEditController::set_component_handler`. pub component_handler: RwLock>>, + /// Our own [IPlugView] instance. This is set while the editor is actually visible (which is + /// different form the lifetimei of [super::WrapperView] itself). + pub plug_view: RwLock>>>, + /// A realtime-safe task queue so the plugin can schedule tasks that need to be run later on the /// GUI thread. /// @@ -106,6 +111,8 @@ impl WrapperInner

{ component_handler: RwLock::new(None), + plug_view: RwLock::new(None), + event_loop: RwLock::new(MaybeUninit::uninit()), is_processing: AtomicBool::new(false), diff --git a/src/wrapper/vst3/util.rs b/src/wrapper/vst3/util.rs index f70b4c8c..29ec56e8 100644 --- a/src/wrapper/vst3/util.rs +++ b/src/wrapper/vst3/util.rs @@ -64,6 +64,14 @@ impl From> for VstPtr } } +impl From<&T> for ObjectPtr { + /// Create a smart pointer for an existing reference counted object. + fn from(obj: &T) -> Self { + unsafe { obj.add_ref() }; + Self { ptr: obj } + } +} + impl Drop for ObjectPtr { fn drop(&mut self) { unsafe { (*self).release() }; @@ -77,11 +85,3 @@ unsafe impl Sync for VstPtr {} unsafe impl Send for ObjectPtr {} unsafe impl Sync for ObjectPtr {} - -impl ObjectPtr { - /// Create a smart pointer for an existing reference counted object. - pub fn new(obj: &T) -> Self { - unsafe { obj.add_ref() }; - Self { ptr: obj } - } -} diff --git a/src/wrapper/vst3/view.rs b/src/wrapper/vst3/view.rs index 27d3c19f..1f63a649 100644 --- a/src/wrapper/vst3/view.rs +++ b/src/wrapper/vst3/view.rs @@ -10,7 +10,7 @@ use vst3_sys::gui::{IPlugFrame, IPlugView}; use vst3_sys::VST3; use super::inner::WrapperInner; -use super::util::VstPtr; +use super::util::{ObjectPtr, VstPtr}; use crate::plugin::{Editor, Plugin}; use crate::ParentWindowHandle; @@ -117,6 +117,8 @@ impl IPlugView for WrapperView

{ self.editor .spawn(ParentWindowHandle { handle }, self.inner.clone()), ); + *self.inner.plug_view.write() = Some(ObjectPtr::from(self)); + kResultOk } else { kResultFalse @@ -126,7 +128,9 @@ impl IPlugView for WrapperView

{ unsafe fn removed(&self) -> tresult { let mut editor_handle = self.editor_handle.write(); if editor_handle.is_some() { + *self.inner.plug_view.write() = None; *editor_handle = None; + kResultOk } else { kResultFalse