1
0
Fork 0

Keep a reference to WrapperView in WrapperInner

This commit is contained in:
Robbert van der Helm 2022-02-16 20:43:53 +01:00
parent 8ddad4e893
commit 0675feb54b
3 changed files with 21 additions and 10 deletions

View file

@ -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<P: Plugin> {
/// `IEditController::set_component_handler`.
pub component_handler: RwLock<Option<VstPtr<dyn IComponentHandler>>>,
/// 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<Option<ObjectPtr<WrapperView<P>>>>,
/// 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<P: Plugin> WrapperInner<P> {
component_handler: RwLock::new(None),
plug_view: RwLock::new(None),
event_loop: RwLock::new(MaybeUninit::uninit()),
is_processing: AtomicBool::new(false),

View file

@ -64,6 +64,14 @@ impl<T: vst3_sys::ComInterface + ?Sized> From<vst3_sys::VstPtr<T>> for VstPtr<T>
}
}
impl<T: IUnknown> From<&T> for ObjectPtr<T> {
/// Create a smart pointer for an existing reference counted object.
fn from(obj: &T) -> Self {
unsafe { obj.add_ref() };
Self { ptr: obj }
}
}
impl<T: IUnknown> Drop for ObjectPtr<T> {
fn drop(&mut self) {
unsafe { (*self).release() };
@ -77,11 +85,3 @@ unsafe impl<T: ComInterface + ?Sized> Sync for VstPtr<T> {}
unsafe impl<T: IUnknown> Send for ObjectPtr<T> {}
unsafe impl<T: IUnknown> Sync for ObjectPtr<T> {}
impl<T: IUnknown> ObjectPtr<T> {
/// Create a smart pointer for an existing reference counted object.
pub fn new(obj: &T) -> Self {
unsafe { obj.add_ref() };
Self { ptr: obj }
}
}

View file

@ -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<P: Plugin> IPlugView for WrapperView<P> {
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<P: Plugin> IPlugView for WrapperView<P> {
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