From c790217085985d1da287cfa94640723a139c2a65 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 12 Jan 2023 01:03:25 +0100 Subject: [PATCH 1/5] Add WIN2D_GET_DEVICE_ASSOCIATION_TYPE binding type --- .../WIN2D_GET_D2D_IMAGE_FLAGS.cs | 2 +- .../WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/ComputeSharp.D2D1.Uwp/ABI.Microsoft.Graphics.Canvas/WIN2D_GET_DEVICE_ASSOCIATION_TYPE.cs 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 From 278062743c693084833f36ad2f028103a5f9a292 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 12 Jan 2023 01:28:40 +0100 Subject: [PATCH 2/5] Implement WIN2D_GET_DEVICE_ASSOCIATION_TYPE support --- .../ICanvasImageInterop.cs | 9 +++--- .../CanvasEffect.Interop.cs | 4 +-- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 16 ++++++++-- ...derEffect{T}.Properties.Interop.Sources.cs | 29 +++++++++++++++---- 4 files changed, 45 insertions(+), 13 deletions(-) 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) { From fb16181de316bea63c281cefb680007395f7073a Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 12 Jan 2023 01:29:00 +0100 Subject: [PATCH 3/5] Add parameter validation in ICanvasImageInterop APIs --- .../PixelShaderEffect{T}.ICanvasImageInterop.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index a4896926..94924f72 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -23,6 +23,12 @@ unsafe partial class PixelShaderEffect /// 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; + } + using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal @@ -72,6 +78,12 @@ unsafe partial class PixelShaderEffect float* realizeDpi, ID2D1Image** ppImage) { + // No input pointer can be null + if (device is null || ppImage is null) + { + return E.E_POINTER; + } + using ReferenceTracker.Lease _0 = GetReferenceTracker().TryGetLease(out bool leaseTaken); // Check for disposal From ca35f249f7cd7b1cd96428d8e631afd8157f9a33 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 12 Jan 2023 21:15:51 +0100 Subject: [PATCH 4/5] Set parameters to null in public COM APIs --- .../PixelShaderEffect{T}.ICanvasImageInterop.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 94924f72..a482d837 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -29,6 +29,10 @@ unsafe partial class PixelShaderEffect 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 @@ -78,12 +82,15 @@ unsafe partial class PixelShaderEffect float* realizeDpi, ID2D1Image** ppImage) { - // No input pointer can be null + // 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 @@ -148,8 +155,6 @@ unsafe partial class PixelShaderEffect { if (!Realize(flags, targetDpi, deviceContext)) { - *ppImage = null; - return E.E_FAIL; } } @@ -170,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 From b197d5b8576eddecbfa35975508fdd4a39bc9613 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Feb 2023 00:07:57 +0100 Subject: [PATCH 5/5] Bump Win2D reference to 1.27.0-preview2 --- src/ComputeSharp.D2D1.Uwp/ComputeSharp.D2D1.Uwp.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 @@ - +