From d8bdd8961799ff4da4f339a064ab877fac649a86 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 9 Dec 2022 02:03:13 +0100 Subject: [PATCH] Port CanvasEffect::ApplyDpiCompensation logic --- ...ixelShaderEffect{T}.ICanvasImageInterop.cs | 9 +- ...derEffect{T}.Properties.Interop.Sources.cs | 136 +++++++++++++++++- .../DirectX/um/d2d1effects/CLSID.cs | 24 ++++ .../um/d2d1effects/D2D1_BORDER_MODE.cs | 13 ++ ...D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs | 17 +++ .../d2d1effects/D2D1_DPICOMPENSATION_PROP.cs | 14 ++ .../DirectX/um/dcommon/D2D_VECTOR_2F.cs | 12 ++ .../TerraFX.Interop.Windows.D2D1.projitems | 4 + 8 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_BORDER_MODE.cs create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_PROP.cs create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/dcommon/D2D_VECTOR_2F.cs diff --git a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs index 1d9a64b5..1510d1ed 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.ICanvasImageInterop.cs @@ -365,9 +365,14 @@ unsafe partial class PixelShaderEffect } bool isDifferentResource = Sources.Storage[i].UpdateResource(d2D1Image.Get()); - bool isDifferentDpi = false; - // TODO: handle DPI compensation + bool isDifferentDpi = ApplyDpiCompensation( + index: i, + d2D1Image: ref *&d2D1Image, + inputDpi: realizedDpi, + flags: flags, + targetDpi: targetDpi, + d2D1DeviceContext: deviceContext); // If the source or the DPI setting has changed, also update the D2D effect graph if (isDifferentResource || isDifferentDpi) 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 6eb2d18b..16d5dbb8 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -1,3 +1,5 @@ +using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ABI.Microsoft.Graphics.Canvas; using ComputeSharp.D2D1.Extensions; @@ -9,6 +11,10 @@ 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 TerraFX.Interop.DirectX.D2D1_BORDER_MODE; +using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_INTERPOLATION_MODE; +using static TerraFX.Interop.DirectX.D2D1_DPICOMPENSATION_PROP; +using static TerraFX.Interop.DirectX.D2D1_PROPERTY_TYPE; using Win32 = TerraFX.Interop.Windows.Windows; namespace ComputeSharp.D2D1.Uwp; @@ -201,11 +207,139 @@ unsafe partial class PixelShaderEffect // Save the managed wrapper and realized image Sources.Storage[index].SetWrapperAndResource(value, d2D1Image.Get()); - // TODO: handle DPI compensation + // Handle DPI compensation (the return is ignored here as whether DPI compensation was applied doesn't matter) + _ = ApplyDpiCompensation( + index: index, + d2D1Image: ref *&d2D1Image, + inputDpi: realizedDpi, + flags: flags, + targetDpi: targetDpi, + d2D1DeviceContext: d2D1DeviceContext); // Set the actual effect input this.d2D1Effect.Get()->SetInput(index: (uint)index, input: d2D1Image.Get()); return true; } + + /// + /// Applies a DPI compensation effect onto a realized image, if needed. + /// + /// The index of the source being processed. + /// A reference to the realized source (this might be replaced if DPI compensation is added). + /// The input DPU for . + /// The flags for . + /// The target DPI for the realized image, if present. + /// The input object to use, if present. + /// Whether the DPI configuration has changed. + private bool ApplyDpiCompensation( + int index, + ref ComPtr d2D1Image, + float inputDpi, + WIN2D_GET_D2D_IMAGE_FLAGS flags, + float targetDpi, + ID2D1DeviceContext* d2D1DeviceContext) + { + ref SourceReference sourceReference = ref Sources.Storage[index]; + + bool isDpiCompensationNeeded; + + // Detemine whether DPI compensation is needed (direct port of Win2D's CanvasEffect::ApplyDpiCompensation) + if ((flags & WIN2D_GET_D2D_IMAGE_FLAGS_MINIMAL_REALIZATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE) + { + // In minimal mode, we don't yet know the target DPI. For instance this occurs when setting an + // effect as the source of an image brush. We'll fix up later when the brush is drawn to a device + // context, at which point the real DPI is known. Since we don't yet know whether to include + // compensation, we just preserve the existing state. This avoids repeated recreation during + // set-draw-set-draw sequences. + isDpiCompensationNeeded = sourceReference.HasDpiCompensationEffect && (inputDpi != 0); + } + else + { + bool neverCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_NEVER_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE; + bool alwaysCompensate = (flags & WIN2D_GET_D2D_IMAGE_FLAGS_ALWAYS_INSERT_DPI_COMPENSATION) != WIN2D_GET_D2D_IMAGE_FLAGS_NONE; + + // We need DPI compensation if: + // - The input has a fixed DPI + // - We were not told never to include it + // - Either we were told to always include it, or the input DPI is different from the target + isDpiCompensationNeeded = (inputDpi != 0) && !neverCompensate && (alwaysCompensate || (inputDpi != targetDpi)); + + // Early out if we are already in the right state + if (isDpiCompensationNeeded == sourceReference.HasDpiCompensationEffect) + { + return false; + } + } + + // Apply the determined DPI compensation configuration + if (isDpiCompensationNeeded) + { + using ComPtr d2D1EffectDpiCompensation = default; + + // Create an ID2D1Effect for the DPI compensation, if there isn't one already + if (!sourceReference.HasDpiCompensationEffect) + { + // We need to create a DPI compensation effect, so a device context must be available + if (d2D1DeviceContext is null) + { + Marshal.ThrowExceptionForHR(E.E_POINTER); + } + + // Create the DPI compensation effect + d2D1DeviceContext->CreateEffect( + effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)), + effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert(); + + D2D1_BORDER_MODE d2D1BorderMode = D2D1_BORDER_MODE_HARD; + D2D1_DPICOMPENSATION_INTERPOLATION_MODE d2D1DpiCompensationInterpolationMode = D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR; + + // Set the border mode to hard + d2D1EffectDpiCompensation.Get()->SetValue( + index: (uint)D2D1_DPICOMPENSATION_PROP_BORDER_MODE, + type: D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1BorderMode, + dataSize: sizeof(D2D1_BORDER_MODE)).Assert(); + + // Set the interpolation mode to linear + d2D1EffectDpiCompensation.Get()->SetValue( + index: (uint)D2D1_DPICOMPENSATION_PROP_INTERPOLATION_MODE, + type: D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2D1DpiCompensationInterpolationMode, + dataSize: sizeof(D2D1_DPICOMPENSATION_INTERPOLATION_MODE)).Assert(); + + // Store the DPI effect for later use + sourceReference.SetDpiCompensationEffect(d2D1EffectDpiCompensation.Get()); + } + else + { + // Just reuse the existing DPI effect otherwise + d2D1EffectDpiCompensation.Attach(new ComPtr(sourceReference.GetDpiCompensationEffect()).Get()); + } + + // Set the input image as source for the DPI compensation effect + sourceReference.GetDpiCompensationEffect()->SetInput(index: (uint)index, input: d2D1Image.Get()); + + D2D_VECTOR_2F d2DVectorDpi; + d2DVectorDpi.x = inputDpi; + d2DVectorDpi.y = inputDpi; + + // Set the DPI setting + sourceReference.GetDpiCompensationEffect()->SetValue( + index: (uint)D2D1_DPICOMPENSATION_PROP_INPUT_DPI, + type: D2D1_PROPERTY_TYPE_UNKNOWN, + data: (byte*)&d2DVectorDpi, + dataSize: (uint)sizeof(D2D_VECTOR_2F)).Assert(); + + // Substitute the input image with the DPI compensation effect + d2D1EffectDpiCompensation.CopyTo(ref d2D1Image).Assert(); + } + else + { + // Ensure the existing effect is released, if present + sourceReference.SetDpiCompensationEffect(null); + } + + return true; + } } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/CLSID.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/CLSID.cs index dbaba616..bd310a66 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/CLSID.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/CLSID.cs @@ -34,4 +34,28 @@ internal static partial class CLSID return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); } } + + [NativeTypeName("const GUID")] + public static ref readonly Guid CLSID_D2D1DpiCompensation + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + ReadOnlySpan data = new byte[] { + 0xC7, 0xC5, 0x26, 0x6C, + 0xE0, 0x34, + 0xFC, 0x46, + 0x9C, + 0xFD, + 0xE5, + 0x82, + 0x37, + 0x06, + 0xE2, + 0x28 + }; + + return ref Unsafe.As(ref MemoryMarshal.GetReference(data)); + } + } } \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_BORDER_MODE.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_BORDER_MODE.cs new file mode 100644 index 00000000..372c5be9 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_BORDER_MODE.cs @@ -0,0 +1,13 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX; + +internal enum D2D1_BORDER_MODE : uint +{ + D2D1_BORDER_MODE_SOFT = 0, + D2D1_BORDER_MODE_HARD = 1, + D2D1_BORDER_MODE_FORCE_DWORD = 0xffffffff, +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs new file mode 100644 index 00000000..6897b0d0 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs @@ -0,0 +1,17 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX; + +internal enum D2D1_DPICOMPENSATION_INTERPOLATION_MODE : uint +{ + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_NEAREST_NEIGHBOR = 0, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_LINEAR = 1, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_CUBIC = 2, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_MULTI_SAMPLE_LINEAR = 3, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_ANISOTROPIC = 4, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_HIGH_QUALITY_CUBIC = 5, + D2D1_DPICOMPENSATION_INTERPOLATION_MODE_FORCE_DWORD = 0xffffffff, +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_PROP.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_PROP.cs new file mode 100644 index 00000000..7825110e --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1effects/D2D1_DPICOMPENSATION_PROP.cs @@ -0,0 +1,14 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1effects.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX; + +internal enum D2D1_DPICOMPENSATION_PROP : uint +{ + D2D1_DPICOMPENSATION_PROP_INTERPOLATION_MODE = 0, + D2D1_DPICOMPENSATION_PROP_BORDER_MODE = 1, + D2D1_DPICOMPENSATION_PROP_INPUT_DPI = 2, + D2D1_DPICOMPENSATION_PROP_FORCE_DWORD = 0xffffffff, +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/dcommon/D2D_VECTOR_2F.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/dcommon/D2D_VECTOR_2F.cs new file mode 100644 index 00000000..e52724d5 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/dcommon/D2D_VECTOR_2F.cs @@ -0,0 +1,12 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/dcommon.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +namespace TerraFX.Interop.DirectX; + +internal partial struct D2D_VECTOR_2F +{ + public float x; + public float y; +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems index 977bcb66..478f8ba5 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -27,6 +27,9 @@ + + + @@ -53,6 +56,7 @@ +