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 CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice;
using IInspectable = TerraFX.Interop.WinRT.IInspectable;
#endif
///
/// A helper type to replicate Win2D's ResourceManager type.
///
///
/// This type provides an entry point to use the Win2D activation factory.
///
internal static unsafe class ResourceManager
{
///
/// Gets or creates an instance for a native resource.
///
/// The input canvas device (as a marshalled ).
/// The input native resource to create a wrapper for.
/// The realization DPIs for
/// The resulting wrapper instance.
/// Thrown if the managed wrapper could not be retrieved.
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.GetOrCreateManagedInterface((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());
// 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();
}
///
/// Registers a factory of wrappers for custom effect with a given effect id.
///
/// The id of the effects to register a factory for.
/// The input factory to create wrappers of native effects.
public static void RegisterEffectFactory(Guid effectId, ICanvasEffectFactoryNative.Interface factory)
{
using ComPtr canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
using ComPtr canvasEffectFactoryNative = default;
RcwMarshaller.GetNativeInterface(factory, canvasEffectFactoryNative.GetAddressOf()).Assert();
// Register the factory for the current effect from the activation factory
canvasFactoryNative.Get()->RegisterEffectFactory(&effectId, canvasEffectFactoryNative.Get()).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;
HSTRING hStringActivatableClass;
// Create a fast-pass HSTRING for "Microsoft.Graphics.Canvas.CanvasDevice"
WinRT.WindowsCreateStringReference(
sourceString: (ushort*)pActivatableClassId,
length: (uint)activatableClassId.Length,
hstringHeader: &hStringHeaderActivatableClassId,
@string: &hStringActivatableClass).Assert();
Guid canvasFactoryNativeId = typeof(ICanvasFactoryNative).GUID;
// Get the activation factory for CanvasDevice
WinRT.RoGetActivationFactory(
activatableClassId: hStringActivatableClass,
iid: &canvasFactoryNativeId,
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();
*factoryNative = (ICanvasFactoryNative*)MarshalInterface.FromManaged(canvasDeviceActivationFactory);
#endif
}
}