diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
index 4de25009..a4bb8cb9 100644
--- a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/ICanvasImageInterop.cs
@@ -22,13 +22,14 @@ internal unsafe struct ICanvasImageInterop
///
/// Gets the device that the effect is currently realized on, if any.
///
- /// The resulting device, if available ((as a marshalled )).
+ /// The resulting device, if available (as a marshalled ).
+ /// The value describing the returned instance.
/// The for the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(3)]
- public HRESULT GetDevice(ICanvasDevice** device)
+ public HRESULT GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
- return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device);
+ return ((delegate* unmanaged[Stdcall])this.lpVtbl[3])((ICanvasImageInterop*)Unsafe.AsPointer(ref this), device, type);
}
///
@@ -88,7 +89,7 @@ internal unsafe struct ICanvasImageInterop
///
[PreserveSig]
[return: NativeTypeName("HRESULT")]
- int GetDevice(ICanvasDevice** device);
+ int GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type);
///
[PreserveSig]
diff --git a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
index a0d1552d..694bf1a0 100644
--- a/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/CanvasEffect.Interop.cs
@@ -41,13 +41,13 @@ unsafe partial class CanvasEffect
}
///
- unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device)
+ unsafe int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
using ComPtr canvasImageInterop = default;
GetCanvasImageInterop(canvasImageInterop.GetAddressOf());
- return canvasImageInterop.Get()->GetDevice(device);
+ return canvasImageInterop.Get()->GetDevice(device, type);
}
///
diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
index 33d14648..a4896926 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs
@@ -12,6 +12,8 @@ using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
+using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_DEVICE_ASSOCIATION_TYPE;
+using Win32 = TerraFX.Interop.Windows.Windows;
namespace ComputeSharp.D2D1.Uwp;
@@ -19,7 +21,7 @@ namespace ComputeSharp.D2D1.Uwp;
unsafe partial class PixelShaderEffect
{
///
- int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device)
+ int ICanvasImageInterop.Interface.GetDevice(ICanvasDevice** device, WIN2D_GET_DEVICE_ASSOCIATION_TYPE* type)
{
using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken);
@@ -40,15 +42,25 @@ unsafe partial class PixelShaderEffect
return E.E_NOT_VALID_STATE;
}
+ HRESULT hresult;
+
try
{
// Copy the device over to the target
- return this.canvasDevice.CopyTo(device);
+ hresult = this.canvasDevice.CopyTo(device);
}
finally
{
Monitor.Exit(this.lockObject);
}
+
+ // Set the association type if the copy was successful
+ if (Win32.SUCCEEDED(hresult))
+ {
+ *type = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_REALIZATION_DEVICE;
+ }
+
+ return hresult;
}
///
diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs
index c4ef1365..2f524503 100644
--- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs
+++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs
@@ -11,6 +11,7 @@ using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
using Windows.Graphics.Effects;
using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_D2D_IMAGE_FLAGS;
+using static ABI.Microsoft.Graphics.Canvas.WIN2D_GET_DEVICE_ASSOCIATION_TYPE;
using static TerraFX.Interop.DirectX.D2D1_BORDER_MODE;
using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_INTERPOLATION_MODE;
using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_PROP;
@@ -158,12 +159,30 @@ unsafe partial class PixelShaderEffect
using ComPtr sourceCanvasDevice = default;
- // Get the canvas device the source is realized on, if any
- canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf()).Assert();
+ WIN2D_GET_DEVICE_ASSOCIATION_TYPE sourceCanvasDeviceType = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED;
- // If a device was retrieved, it must be the same as the input one. If the source has no device,
- // that is fine, as it just means that its underlying D2D image has not been realized yet.
- if (sourceCanvasDevice.Get() is not null && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get()))
+ // Get the canvas device the source is realized on, if any, and its association type
+ canvasImageInterop.Get()->GetDevice(sourceCanvasDevice.GetAddressOf(), &sourceCanvasDeviceType).Assert();
+
+ // If the specified source is bound to a creation device, validate that this matches the one we are realized on.
+ // A "creation device" is a canvas device that is used to create the native resources in a given canvas image
+ // implementation, such that the canvas device and the canvas image are bound for the entire lifetime of the
+ // canvas image itself. This applies to eg. CanvasBitmap, which directly wraps some native memory allocated on
+ // a specific device. In these cases, if the associated device doesn't match the one this effect is currently
+ // realized on, that is an error and the source is invalid.
+ //
+ // Otherwise, the device returned here will depend on the specific implementation of this external effect. That is,
+ // it could be null for an unrealized effect, it could be null for a lazily initialized device-bound resource, or it
+ // could also be an already instantiated resource tied to a specific device.
+ //
+ // The only case where the device has to be an exact match is if the device is an owning device for the canvas image.
+ // In all other cases (including when the device is null), we can proceed to resolve the effects graph normally. This
+ // will allow sources to either just work in case the device matches, or unrealize and re-realize themselves on the
+ // new device if they were already realized on a different device (so that things would just work fine if someone was
+ // trying to draw a reused effect after a device lost event, for instance). In the niche scenario where a given source
+ // was some lazily-initialized instance that doesn't allow changing device after that, then the call to Get2D2Image
+ // on that specific source will just fail during the recursive traversal, but that couldn't be checked from here anyway.
+ if (sourceCanvasDeviceType == WIN2D_GET_DEVICE_ASSOCIATION_TYPE_CREATION_DEVICE && !this.canvasDevice.Get()->IsSameInstance(sourceCanvasDevice.Get()))
{
if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_UNREALIZE_ON_FAILURE) == WIN2D_GET_D2D_IMAGE_FLAGS_NONE)
{