Move 'ComputeSharp.D2D1.WinUI' files to shared project

このコミットが含まれているのは:
Sergio Pedri committed 2024-08-13 18:02:58 -07:00
1 親 c106c0fd50
コミット a44b585927
46ファイル変更
+49 -6

検索条件に一致するファイルはありません

+60
ファイルの表示
@@ -0,0 +1,60 @@
using System;
using ComputeSharp.Win32;
using WinRT;
using IInspectable = ComputeSharp.Win32.IInspectable;
namespace ComputeSharp.D2D1.WinUI.Helpers;
/// <summary>
/// A helper type to handle marshalling of RCW instances.
/// </summary>
internal static unsafe class RcwMarshaller
{
/// <summary>
/// Gets or creates a managed object of a specified type for an input native object.
/// </summary>
/// <typeparam name="T">The interface type to retrieve an instance of.</typeparam>
/// <param name="nativeObject">A pointer to the native object to get a managed wrapper for.</param>
/// <returns>The resulting managed object wrapping <paramref name="nativeObject"/>.</returns>
/// <remarks>This method should only be called with <typeparamref name="T"/> being a projected interface type.</remarks>
public static T GetOrCreateManagedInterface<T>(IUnknown* nativeObject)
where T : class
{
return MarshalInterface<T>.FromAbi((IntPtr)nativeObject);
}
/// <summary>
/// Retrieves the underlying native object for an input RCW.
/// </summary>
/// <typeparam name="T">The type of managed object to unwrap.</typeparam>
/// <param name="managedObject">The input RCW instance to unwrap.</param>
/// <param name="nativeObject">A pointer to the resulting native object to retrieve.</param>
/// <remarks>This method should only be called with <typeparamref name="T"/> being a concrete projected type.</remarks>
public static void GetNativeObject<T>(T managedObject, IInspectable** nativeObject)
where T : class
{
*nativeObject = (IInspectable*)MarshalInspectable<T>.FromManaged(managedObject);
}
/// <summary>
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
/// </summary>
/// <typeparam name="TFrom">The type of managed object to unwrap.</typeparam>
/// <typeparam name="TTo">The interface type to retrieve an instance of.</typeparam>
/// <param name="managedObject">The input RCW instance to unwrap.</param>
/// <param name="nativeObject">A pointer to the resulting native object to retrieve.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
/// <remarks>This method should only be called with <typeparamref name="TFrom"/> being a projected interface type.</remarks>
public static HRESULT GetNativeInterface<TFrom, TTo>(TFrom managedObject, TTo** nativeObject)
where TFrom : class
where TTo : unmanaged, IComObject
{
using ComPtr<IUnknown> unknownObject = default;
// Here we only want to get an IUnknown* pointer for a given interface, and then we'll do
// QueryInterface ourselves to get the target COM type. So MarshalInterface<TFrom> is fine.
unknownObject.Attach((IUnknown*)MarshalInterface<TFrom>.FromManaged(managedObject));
return unknownObject.CopyTo(nativeObject);
}
}
+119
ファイルの表示
@@ -0,0 +1,119 @@
using System;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.Win32;
using Windows.Graphics.Effects;
using WinRT;
namespace ComputeSharp.D2D1.WinUI.Helpers;
using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice;
using ICanvasEffectFactoryNative = Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative;
using IInspectable = Win32.IInspectable;
/// <summary>
/// A helper type to replicate Win2D's <c>ResourceManager</c> type.
/// </summary>
/// <remarks>
/// This type provides an entry point to use the Win2D <see cref="ICanvasFactoryNative"/> activation factory.
/// </remarks>
internal static unsafe class ResourceManager
{
/// <summary>
/// Gets or creates an <see cref="IGraphicsEffectSource"/> instance for a native resource.
/// </summary>
/// <param name="device">The input canvas device (as a marshalled <see cref="CanvasDevice"/>).</param>
/// <param name="resource">The input native resource to create a wrapper for.</param>
/// <param name="dpi">The realization DPIs for <paramref name="resource"/></param>
/// <returns>The resulting <see cref="IGraphicsEffectSource"/> wrapper instance.</returns>
/// <exception cref="Exception">Thrown if the managed wrapper could not be retrieved.</exception>
public static IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
{
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
using ComPtr<IInspectable> 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<IGraphicsEffectSource>((IUnknown*)wrapperInspectable.Get());
}
/// <summary>
/// Registers a managed wrapper for a given native resource.
/// </summary>
/// <param name="resource">The input native resource to register a wrapper for.</param>
/// <param name="wrapper">The wrapper to register for <paramref name="resource"/>.</param>
public static void RegisterWrapper<T>(IUnknown* resource, T wrapper)
where T : class
{
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
using ComPtr<IInspectable> 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();
}
/// <summary>
/// Unregisters a native resource from Win2D's internal cache.
/// </summary>
/// <param name="resource">The input native resource to unregister a wrapper for.</param>
public static void UnregisterWrapper(IUnknown* resource)
{
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
// Unregister the previously registered native resource
canvasFactoryNative.Get()->UnregisterWrapper(resource).Assert();
}
/// <summary>
/// Registers a factory of wrappers for custom effect with a given effect id.
/// </summary>
/// <param name="effectId">The id of the effects to register a factory for.</param>
/// <param name="factory">The input factory to create wrappers of native effects.</param>
public static void RegisterEffectFactory(Guid effectId, ICanvasEffectFactoryNative factory)
{
using ComPtr<ICanvasFactoryNative> canvasFactoryNative = default;
GetActivationFactory(canvasFactoryNative.GetAddressOf());
using ComPtr<ABI.Microsoft.Graphics.Canvas.ICanvasEffectFactoryNative> 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();
}
/// <summary>
/// Gets the <see cref="ICanvasFactoryNative"/> activation factory.
/// </summary>
/// <param name="factoryNative">A pointer to the resulting activation factory.</param>
private static void GetActivationFactory(ICanvasFactoryNative** factoryNative)
{
const string ActivatableClassId = "Microsoft.Graphics.Canvas.CanvasDevice";
// 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 ActivationFactory type, 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.
using IObjectReference canvasDeviceActivationFactory = ActivationFactory.Get(ActivatableClassId, *ICanvasFactoryNative.IID);
*factoryNative = (ICanvasFactoryNative*)canvasDeviceActivationFactory.GetRef();
}
}