diff --git a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs
index c7afd40d..f5fc2b5e 100644
--- a/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs
+++ b/src/ComputeSharp.D2D1.UI/Helpers/RcwMarshaller.cs
@@ -36,6 +36,24 @@ internal static class RcwMarshaller
#endif
}
+ ///
+ /// Retrieves the underlying native object for an input RCW.
+ ///
+ /// The type of managed object to unwrap.
+ /// The input RCW instance to unwrap.
+ /// A pointer to the resulting native object to retrieve.
+ public static unsafe void GetNativeObject<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] T>(T managedObject, IUnknown** nativeObject)
+ where T : class
+ {
+#if WINDOWS_UWP
+ // On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic
+ *nativeObject = (IUnknown*)Marshal.GetIUnknownForObject(managedObject);
+#else
+ // On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs
+ *nativeObject = (IUnknown*)MarshalInspectable.FromManaged(managedObject);
+#endif
+ }
+
///
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
///
@@ -50,15 +68,8 @@ internal static class RcwMarshaller
{
using ComPtr unknownObject = default;
-#if WINDOWS_UWP
- // On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic
- unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject));
-#else
- // On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs
- unknownObject.Attach((IUnknown*)MarshalInspectable.FromManaged(managedObject));
-#endif
+ GetNativeObject(managedObject, unknownObject.GetAddressOf());
- // QueryInterface to the specific interface we need
return unknownObject.CopyTo(nativeObject);
}
}
diff --git a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
index efc9b107..77c1b4a7 100644
--- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
+++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
@@ -1,4 +1,5 @@
using System;
+using System.Diagnostics.CodeAnalysis;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.Windows;
@@ -19,7 +20,7 @@ using WinRT = TerraFX.Interop.WinRT.WinRT;
///
/// This type provides an entry point to use the Win2D activation factory.
///
-internal static class ResourceManager
+internal static unsafe class ResourceManager
{
///
/// Gets or creates an instance for a native resource.
@@ -29,10 +30,66 @@ internal static class ResourceManager
/// The realization DPIs for
/// The resulting wrapper instance.
/// Thrown if the managed wrapper could not be retrieved.
- public static unsafe IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
+ public static IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
{
using ComPtr canvasFactoryNative = default;
+ GetActivationFactory(canvasFactoryNative.GetAddressOf());
+
+ using ComPtr wrapperInspectable = default;
+
+ // Get or create a WinRT wrapper for the resource
+ canvasFactoryNative.Get()->GetOrCreate(
+ device: device,
+ resource: resource,
+ dpi: dpi,
+ wrapper: wrapperInspectable.GetAddressOf()).Assert();
+
+ // Get the runtime-provided RCW for the resulting WinRT wrapper
+ return RcwMarshaller.GetOrCreateManagedObject((IUnknown*)wrapperInspectable.Get());
+ }
+
+ ///
+ /// Registers a managed wrapper for a given native resource.
+ ///
+ /// The input native resource to register a wrapper for.
+ /// The wrapper to register for .
+ public static void RegisterWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] T>(IUnknown* resource, T wrapper)
+ where T : class
+ {
+ using ComPtr canvasFactoryNative = default;
+
+ GetActivationFactory(canvasFactoryNative.GetAddressOf());
+
+ using ComPtr wrapperInspectable = default;
+
+ // Unwrap the input wrapper and get an IInspectable* object
+ RcwMarshaller.GetNativeObject(wrapper, wrapperInspectable.GetAddressOf()).Assert();
+
+ // Register this pair of native resource and inspectable wrapper
+ canvasFactoryNative.Get()->RegisterWrapper(resource, wrapperInspectable.Get()).Assert();
+ }
+
+ ///
+ /// Unregisters a native resource from Win2D's internal cache.
+ ///
+ /// The input native resource to unregister a wrapper for.
+ public static void UnregisterWrapper(IUnknown* resource)
+ {
+ using ComPtr canvasFactoryNative = default;
+
+ GetActivationFactory(canvasFactoryNative.GetAddressOf());
+
+ // Unregister the previously registered native resource
+ canvasFactoryNative.Get()->UnregisterWrapper(resource).Assert();
+ }
+
+ ///
+ /// Gets the activation factory.
+ ///
+ /// A pointer to the resulting activation factory.
+ private static void GetActivationFactory(ICanvasFactoryNative** factoryNative)
+ {
const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
fixed (char* pActivatableClassId = activatableClassId)
@@ -53,19 +110,7 @@ internal static class ResourceManager
WinRT.RoGetActivationFactory(
activatableClassId: hStringActivatableClass,
iid: &canvasFactoryNativeId,
- factory: canvasFactoryNative.GetVoidAddressOf()).Assert();
+ factory: (void**)factoryNative).Assert();
}
-
- using ComPtr wrapperInspectable = default;
-
- // Get or create a WinRT wrapper for the resource
- canvasFactoryNative.Get()->GetOrCreate(
- device: device,
- resource: resource,
- dpi: dpi,
- wrapper: wrapperInspectable.GetAddressOf()).Assert();
-
- // Get the runtime-provided RCW for the resulting WinRT wrapper
- return RcwMarshaller.GetOrCreateManagedObject((IUnknown*)wrapperInspectable.Get());
}
}
diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
index 5f376fdf..9e9c1ffa 100644
--- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasImageInterop.cs
@@ -243,6 +243,10 @@ unsafe partial class PixelShaderEffect
}
}
+ // Unregister the wrapper for the effect. In case someone is still holding a reference
+ // to it, the effect is now "orphaned" and without a registered inspectable wrapper.
+ ResourceManager.UnregisterWrapper((IUnknown*)this.d2D1Effect.Get());
+
// Finally release the effect as well
this.d2D1Effect.Dispose();
}
@@ -340,6 +344,9 @@ unsafe partial class PixelShaderEffect
}
}
+ // Register the new realized effect
+ ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
+
return true;
}