Implement register/unregister wrapper support
このコミットが含まれているのは:
1 親
9e1677e06f
コミット
c51e39c8bf
3ファイル変更
+86
-23
検索条件に一致するファイルはありません
@@ -36,6 +36,24 @@ internal static class RcwMarshaller
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <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>
|
||||
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<T>.FromManaged(managedObject);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
|
||||
/// </summary>
|
||||
@@ -50,15 +68,8 @@ internal static class RcwMarshaller
|
||||
{
|
||||
using ComPtr<IUnknown> 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<TFrom>.FromManaged(managedObject));
|
||||
#endif
|
||||
GetNativeObject(managedObject, unknownObject.GetAddressOf());
|
||||
|
||||
// QueryInterface to the specific interface we need
|
||||
return unknownObject.CopyTo(nativeObject);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
/// <remarks>
|
||||
/// This type provides an entry point to use the Win2D <see cref="ICanvasFactoryNative"/> activation factory.
|
||||
/// </remarks>
|
||||
internal static class ResourceManager
|
||||
internal static unsafe class ResourceManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or creates an <see cref="IGraphicsEffectSource"/> instance for a native resource.
|
||||
@@ -29,10 +30,66 @@ internal static class ResourceManager
|
||||
/// <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 unsafe IGraphicsEffectSource GetOrCreate(ICanvasDevice* device, IUnknown* resource, float dpi)
|
||||
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.GetOrCreateManagedObject<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<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] 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()).Assert();
|
||||
|
||||
// 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>
|
||||
/// 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";
|
||||
|
||||
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<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.GetOrCreateManagedObject<IGraphicsEffectSource>((IUnknown*)wrapperInspectable.Get());
|
||||
}
|
||||
}
|
||||
@@ -243,6 +243,10 @@ unsafe partial class PixelShaderEffect<T>
|
||||
}
|
||||
}
|
||||
|
||||
// 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<T>
|
||||
}
|
||||
}
|
||||
|
||||
// Register the new realized effect
|
||||
ResourceManager.RegisterWrapper((IUnknown*)this.d2D1Effect.Get(), this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする