diff --git a/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs b/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs index 37a6ca88..18ced739 100644 --- a/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs +++ b/src/ComputeSharp.D2D1.UI/Extensions/IUnknownExtensions.cs @@ -51,7 +51,7 @@ internal static unsafe class IUnknownExtensions /// The type of object being copied. /// The source object. /// The destination location. - /// The for the operation (always ). + /// The for the operation. public static HRESULT CopyTo(this ref T source, ref ComPtr destination) where T : unmanaged // IUnknown { @@ -65,4 +65,21 @@ internal static unsafe class IUnknownExtensions return temporary.CopyTo(ref destination); } + + /// + /// Copies the source object onto a target location. + /// + /// The type of object being copied. + /// The type of destination object being copied. + /// The source object. + /// The destination location. + /// The for the operation. + public static HRESULT CopyTo(this ref T source, U** destination) + where T : unmanaged // IUnknown + where U : unmanaged // IUnknown + { + using ComPtr temporary = new((T*)Unsafe.AsPointer(ref source)); + + return temporary.CopyTo(destination); + } } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs index c8f2b37a..d6c49f69 100644 --- a/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs +++ b/src/ComputeSharp.D2D1.UI/PixelShaderEffect{T}.ICanvasEffectFactoryNative.cs @@ -91,13 +91,46 @@ unsafe partial class PixelShaderEffect /// int ICanvasEffectFactoryNative.Interface.CreateWrapper(ICanvasDevice* device, ID2D1Effect* resource, float dpi, IInspectable** wrapper) { - PixelShaderEffect @this = new(); + 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 E.E_INVALIDARG; + } + } + + // 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();