diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs
new file mode 100644
index 00000000..69201dfb
--- /dev/null
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasEffectFactoryNative.cs
@@ -0,0 +1,72 @@
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using TerraFX.Interop;
+using TerraFX.Interop.DirectX;
+using TerraFX.Interop.Windows;
+using TerraFX.Interop.WinRT;
+
+#pragma warning disable CS0649, IDE1006
+
+namespace ABI.Microsoft.Graphics.Canvas;
+
+///
+/// The interop Win2D interface for factories of external effects.
+///
+[Guid("29BA1A1F-1CFE-44C3-984D-426D61B51427")]
+[NativeTypeName("class ICanvasEffectFactoryNative : IUnknown")]
+[NativeInheritance("IUnknown")]
+internal unsafe struct ICanvasEffectFactoryNative
+{
+ public void** lpVtbl;
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(0)]
+ public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[0])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this), riid, ppvObject);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(1)]
+ [return: NativeTypeName("ULONG")]
+ public uint AddRef()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[1])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this));
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(2)]
+ [return: NativeTypeName("ULONG")]
+ public uint Release()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[2])((ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this));
+ }
+
+ ///
+ /// Creates a new inspectable wrapper for an inpute D2D effect previosly registered through .
+ ///
+ /// The input canvas device.
+ /// The input native effect to create a wrapper for.
+ /// The realization DPIs for .
+ /// The resulting wrapper for .
+ /// The for the operation.
+ ///
+ /// All parameters are directly forwarded from the ones the caller passed to . The returned
+ /// wrapper should implement to be returned correctly from Win2D after this call.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(3)]
+ public HRESULT CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])(
+ (ICanvasEffectFactoryNative*)Unsafe.AsPointer(ref this),
+ device,
+ resource,
+ dpi,
+ wrapper);
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
index 0e1f8430..2fd2cf6d 100644
--- a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs
@@ -3,6 +3,13 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using TerraFX.Interop;
using TerraFX.Interop.Windows;
+#if WINDOWS_UWP
+using TerraFX.Interop.WinRT;
+#else
+using WinRT;
+using WinRT.Interop;
+using IInspectable = TerraFX.Interop.WinRT.IInspectable;
+#endif
#pragma warning disable CS0649, IDE1006
@@ -49,19 +56,119 @@ internal unsafe struct ICanvasFactoryNative
///
/// The input canvas device (as a marshalled ).
/// The input native resource to create a wrapper for.
- /// The realization DPIs for
+ /// The realization DPIs for .
/// The resulting wrapper for .
/// The for the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(6)]
- [return: NativeTypeName("ULONG")]
- public HRESULT GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi, [NativeTypeName("IInspectable**")] void** wrapper)
+ public HRESULT GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi, IInspectable** wrapper)
{
- return ((delegate* unmanaged[Stdcall])this.lpVtbl[6])(
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[6])(
(ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
device,
resource,
dpi,
wrapper);
}
+
+ ///
+ /// Registers an IInspectable wrapper for a given native D2D resource.
+ ///
+ /// The input native resource to register a wrapper for.
+ /// The wrapper to register for .
+ ///
+ /// The for the operation. In case of success, it will be if the wrapper
+ /// was not previously registered, or if was already registered.
+ ///
+ ///
+ /// The method validates at runtime that is an
+ /// and that is an . Additionally,
+ /// has to implement IWeakReferenceSource for this method to be successful.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(7)]
+ public HRESULT RegisterWrapper(IUnknown* resource, IInspectable* wrapper)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[7])(
+ (ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
+ resource,
+ wrapper);
+ }
+
+ ///
+ /// Unregisters a native D2D resource previously registered with .
+ ///
+ /// The input native resource to unregister.
+ ///
+ /// The for the operation. In case of success, it will be if the
+ /// resource was previously registered and could be correctly unregistered, otherwise.
+ ///
+ ///
+ /// The method validates at runtime that is an .
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(8)]
+ public HRESULT UnregisterWrapper(IUnknown* resource)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[8])(
+ (ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
+ resource);
+ }
+
+ ///
+ /// Registers a factory of wrappers for custom effects.
+ ///
+ /// The id of the effects to register a factory for.
+ /// The input factory to create wrappers of native effects.
+ ///
+ /// The for the operation. In case of success, it will be if the factory
+ /// was not previously registered, or if was already registered.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(9)]
+ public HRESULT RegisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId, ICanvasEffectFactoryNative* factory)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[9])(
+ (ICanvasFactoryNative*)Unsafe.AsPointer(ref this),
+ factory);
+ }
+
+ ///
+ /// Unregisters an effect factory that was previosuly registered with .
+ ///
+ /// The id of the effects to unregister the factory for.
+ ///
+ /// The for the operation. In case of success, it will be if the
+ /// factory was previously registered and could be correctly unregistered, otherwise.
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(10)]
+ public HRESULT UnregisterEffectFactory([NativeTypeName("const IID &")] Guid* effectId)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[10])(
+ (ICanvasFactoryNative*)Unsafe.AsPointer(ref this));
+ }
+
+#if !WINDOWS_UWP
+ ///
+ /// The managed interface for .
+ ///
+ [Guid("695C440D-04B3-4EDD-BFD9-63E51E9F7202")]
+ [WindowsRuntimeType]
+ [WindowsRuntimeHelperType(typeof(Interface))]
+ public interface Interface
+ {
+ ///
+ /// The vtable type for .
+ ///
+ [Guid("695C440D-04B3-4EDD-BFD9-63E51E9F7202")]
+ public readonly struct Vftbl
+ {
+ ///
+ /// Allows CsWinRT to retrieve a pointer to the projection vtable (the name is hardcoded by convention).
+ ///
+ public static readonly IntPtr AbiToProjectionVftablePtr = IUnknownVftbl.AbiToProjectionVftblPtr;
+ }
+ }
+#endif
}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/Inspectable/IInspectable.cs b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/Inspectable/IInspectable.cs
new file mode 100644
index 00000000..12fb85bc
--- /dev/null
+++ b/src/ComputeSharp.D2D1.UI/ABI.Microsoft.Graphics.Canvas/Inspectable/IInspectable.cs
@@ -0,0 +1,50 @@
+// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
+
+// Ported from winrt/inspectable.h in the Windows SDK for Windows 10.0.22621.0
+// Original source is Copyright © Microsoft. All rights reserved.
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using TerraFX.Interop.Windows;
+
+#pragma warning disable CS0649, IDE1006
+
+namespace TerraFX.Interop.WinRT;
+
+///
+/// The native WinRT interface for IInspectable objects.
+///
+[Guid("AF86E2E0-B12D-4C6A-9C5A-D7AA65101E90")]
+[NativeTypeName("struct IInspectable : IUnknown")]
+[NativeInheritance("IUnknown")]
+internal unsafe struct IInspectable
+{
+ public void** lpVtbl;
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(0)]
+ public HRESULT QueryInterface([NativeTypeName("const IID &")] Guid* riid, void** ppvObject)
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[0])((IInspectable*)Unsafe.AsPointer(ref this), riid, ppvObject);
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(1)]
+ [return: NativeTypeName("ULONG")]
+ public uint AddRef()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[1])((IInspectable*)Unsafe.AsPointer(ref this));
+ }
+
+ ///
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ [VtblIndex(2)]
+ [return: NativeTypeName("ULONG")]
+ public uint Release()
+ {
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[2])((IInspectable*)Unsafe.AsPointer(ref this));
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
index 086fc849..ebda8706 100644
--- a/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
+++ b/src/ComputeSharp.D2D1.UI/ComputeSharp.D2D1.UI.projitems
@@ -10,6 +10,7 @@
+
@@ -18,6 +19,7 @@
+
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 798354a8..2e9b9fe1 100644
--- a/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
+++ b/src/ComputeSharp.D2D1.UI/Helpers/ResourceManager.cs
@@ -1,16 +1,23 @@
using System;
+using System.Diagnostics.CodeAnalysis;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using TerraFX.Interop.Windows;
+#if WINDOWS_UWP
using TerraFX.Interop.WinRT;
+#endif
using Windows.Graphics.Effects;
+#if !WINDOWS_UWP
+using WinRT;
+#endif
#if WINDOWS_UWP
namespace ComputeSharp.D2D1.Uwp.Helpers;
#else
namespace ComputeSharp.D2D1.WinUI.Helpers;
-using WinRT = TerraFX.Interop.WinRT.WinRT;
+using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice;
+using IInspectable = TerraFX.Interop.WinRT.IInspectable;
#endif
///
@@ -19,7 +26,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,12 +36,71 @@ 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)
+ {
+#if WINDOWS_UWP
const string activatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
+ // On UWP, the WinRT types from Win2D are automatically registered for activation.
+ // This means we can simply use RoGetActivationFactory to retrieve the factory.
fixed (char* pActivatableClassId = activatableClassId)
{
HSTRING_HEADER hStringHeaderActivatableClassId;
@@ -53,19 +119,16 @@ internal static class ResourceManager
WinRT.RoGetActivationFactory(
activatableClassId: hStringActivatableClass,
iid: &canvasFactoryNativeId,
- factory: canvasFactoryNative.GetVoidAddressOf()).Assert();
+ factory: (void**)factoryNative).Assert();
}
+#else
+ // On WinUI 3, the types are not guaranteed to be registered for activation. Additionally,
+ // for concistency with other types, we just use the built-in T.As() method, which will
+ // automatically handle fallback logic to resolve types to activate if they're not registered.
+ // For instance, this will ensure the following call will work fine in unpackaged apps.
+ ICanvasFactoryNative.Interface canvasDeviceActivationFactory = CanvasDevice.As();
- using ComPtr wrapperUnknown = default;
-
- // Get or create a WinRT wrapper for the resource
- canvasFactoryNative.Get()->GetOrCreate(
- device: device,
- resource: resource,
- dpi: dpi,
- wrapper: wrapperUnknown.GetVoidAddressOf()).Assert();
-
- // Get the runtime-provided RCW for the resulting WinRT wrapper
- return RcwMarshaller.GetOrCreateManagedObject(wrapperUnknown.Get());
+ *factoryNative = (ICanvasFactoryNative*)MarshalInspectable.FromManaged(canvasDeviceActivationFactory);
+#endif
}
}
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;
}