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/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs index a7697b7a..063bd8a6 100644 --- a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_D2D_IMAGE_FLAGS.cs @@ -46,5 +46,5 @@ internal enum WIN2D_GET_D2D_IMAGE_FLAGS : uint /// /// Ignored. /// - WIN2D_GET_D2D_IMAGE_FLAGS_FORCE_DWORD = 0xffffffff + WIN2D_GET_D2D_IMAGE_FLAGS_FORCE_DWORD = 0xFFFFFFFF } \ No newline at end of file diff --git a/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs new file mode 100644 index 00000000..9ce3c2e4 --- /dev/null +++ b/src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs @@ -0,0 +1,30 @@ +using System; + +namespace ABI.Microsoft.Graphics.Canvas; + +/// +/// Options for fine-tuning the behavior of . +/// +[Flags] +internal enum WIN2D_GET_DEVICE_ASSOCIATION_TYPE : uint +{ + /// + /// No device info is available, callers will handle the returned device with their own logic. + /// + WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED = 0, + + /// + /// The returned device is the one the image is currently realized on, if any. + /// + WIN2D_GET_DEVICE_ASSOCIATION_TYPE_REALIZATION_DEVICE = 1, + + /// + /// The returned device is the one that created the image resource, and cannot change. + /// + WIN2D_GET_DEVICE_ASSOCIATION_TYPE_CREATION_DEVICE = 2, + + /// + /// Ignored. + /// + WIN2D_GET_DEVICE_ASSOCIATION_TYPE_FORCE_DWORD = 0xFFFFFFFF +} \ No newline at end of file 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/ComputeSharp.D2D1.Uwp.csproj b/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj index 252b25b4..311b0712 100644 --- a/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj +++ b/src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj @@ -35,7 +35,7 @@ - + diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 33d14648..a482d837 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,8 +21,18 @@ 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) { + // Validate all input parameters + if (device is null || type is null) + { + return E.E_POINTER; + } + + // Set parameters to default values + *device = null; + *type = WIN2D_GET_DEVICE_ASSOCIATION_TYPE_UNSPECIFIED; + using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal @@ -40,15 +52,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; } /// @@ -60,6 +82,15 @@ unsafe partial class PixelShaderEffect float* realizeDpi, ID2D1Image** ppImage) { + // The device and resulting image pointers cannot be null + if (device is null || ppImage is null) + { + return E.E_POINTER; + } + + // Set the resulting image to null + *ppImage = null; + using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal @@ -124,8 +155,6 @@ unsafe partial class PixelShaderEffect { if (!Realize(flags, targetDpi, deviceContext)) { - *ppImage = null; - return E.E_FAIL; } } @@ -146,12 +175,6 @@ unsafe partial class PixelShaderEffect return S.S_OK; } } - catch (Exception e) - { - *ppImage = null; - - return e.HResult; - } finally { // Ensure that reset this flag so that this method can be called again 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) {