using System;
using System.Runtime.CompilerServices;
using ABI.Microsoft.Graphics.Canvas;
using ComputeSharp.D2D1.Extensions;
using ComputeSharp.D2D1.WinUI.Extensions;
using ComputeSharp.D2D1.WinUI.Helpers;
using ComputeSharp.Win32;
namespace ComputeSharp.D2D1.WinUI;
///
unsafe partial class PixelShaderEffect
{
///
/// A manager type to handle the effect factory registration logic for .
///
private sealed class EffectFactoryManager
{
///
/// Lock object used to synchronize the registration logic.
///
private readonly object lockObject = new();
///
/// Indicates whether an effect factory for has been registered.
///
private volatile bool isEffectFactoryRegistered;
///
/// Creates a new instance.
///
private EffectFactoryManager()
{
}
///
/// Gets the shared instance.
///
public static EffectFactoryManager Instance { get; } = new();
///
/// Ensures an effect factory is registered in Win2D for the current effect type, or registers one otherwise.
///
public void EnsureEffectFactoryIsRegistered()
{
if (this.isEffectFactoryRegistered)
{
return;
}
EnsureEffectFactoryIsRegisteredWithLock();
}
///
/// Same as , but with a slower path using a lock.
///
[MethodImpl(MethodImplOptions.NoInlining)]
private void EnsureEffectFactoryIsRegisteredWithLock()
{
lock (this.lockObject)
{
if (!this.isEffectFactoryRegistered)
{
ResourceManager.RegisterEffectFactory(
effectId: T.EffectId,
factory: new EffectFactory());
this.isEffectFactoryRegistered = true;
}
}
}
}
///
/// A managed implementation of for .
///
private sealed class EffectFactory : ICanvasEffectFactoryNative.Interface
{
///
int ICanvasEffectFactoryNative.Interface.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper)
{
PixelShaderEffect @this;
using (ComPtr d2D1Device1 = default)
{
// Get the underlying ID2D1Device1 instance for the input device
device->GetD2DDevice(d2D1Device1.GetAddressOf()).Assert();
// Validate that the input underlying D2D device and resource share the same D2D factory
using (ComPtr d2D1ImageResource = default)
using (ComPtr d2D1FactoryFromDevice = default)
using (ComPtr d2D1FactoryFromResource = default)
{
resource->CopyTo(d2D1ImageResource.GetAddressOf()).Assert();
d2D1Device1.Get()->GetFactory(d2D1FactoryFromDevice.GetAddressOf());
d2D1ImageResource.Get()->GetFactory(d2D1FactoryFromResource.GetAddressOf());
// Shared logic and comments from Win2D, as this validation path is the same as that of CanvasEffect.
// There is a small chance of constructing a PixelShaderEffect object that is in an invalid state
// when using this method to create a managed wrapper indirectly from the interop APIs exposed in the
// public headers. In general, this would happen if someone did manual interop to get a managed wrapper
// for an orphaned effect previously retrieved from an existing PixelShaderEffect instance that has
// since changed. For more info and a full example, see https://github.com/microsoft/Win2D/issues/913.
// Here we're validating the D2D factories from the wrapped effect and the device to try to minimize
// the chances of this happening. We can't be 100% sure since there is no way with the existing D2D
// APIs to fully determine whether two resources are "compatible", short of just trying to draw with
// both of them to see if that fails. But, comparing factories is at least a nice additional check
// to perform, and due to the fact that Win2D creates a new D2D factory per CanvasDevice, this should
// already cover most scenarios where this might potentially happen, so that's good enough.
if (!d2D1FactoryFromDevice.Get()->IsSameInstance(d2D1FactoryFromResource.Get()))
{
*wrapper = null;
return D2DERR.D2DERR_WRONG_FACTORY;
}
}
// Only initialize the effect once we're past the validation
@this = new PixelShaderEffect();
// Initialize the effect state
device->CopyTo(ref @this.canvasDevice).Assert();
d2D1Device1.CopyTo(ref @this.d2D1RealizationDevice).Assert();
resource->CopyTo(ref @this.d2D1Effect).Assert();
}
// Register the new wrapper for the input resource. This has to be done manually in this
// case, as the effect is not being realized through the usual path from GetD2DImage.
ResourceManager.RegisterWrapper((IUnknown*)resource, @this);
// Unwrap the PixelShaderEffect and return its CCW to the caller
RcwMarshaller.GetNativeObject(@this, wrapper);
return S.S_OK;
}
}
}