Port CanvasEffect::ApplyDpiCompensation logic

このコミットが含まれているのは:
Sergio Pedri committed 2022-12-22 10:56:03 -08:00
1 親 941488a647
コミット d8bdd89617
8ファイル変更
+226 -3

検索条件に一致するファイルはありません

+7 -2
ファイルの表示
@@ -365,9 +365,14 @@ unsafe partial class PixelShaderEffect<T>
}
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)
+135 -1
ファイルの表示
@@ -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<T>
// 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;
}
/// <summary>
/// Applies a DPI compensation effect onto a realized image, if needed.
/// </summary>
/// <param name="index">The index of the source being processed.</param>
/// <param name="d2D1Image">A reference to the realized source (this might be replaced if DPI compensation is added).</param>
/// <param name="inputDpi">The input DPU for <paramref name="d2D1Image"/>.</param>
/// <param name="flags">The <see cref="WIN2D_GET_D2D_IMAGE_FLAGS"/> flags for <paramref name="d2D1Image"/>.</param>
/// <param name="targetDpi">The target DPI for the realized image, if present.</param>
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> object to use, if present.</param>
/// <returns>Whether the DPI configuration has changed.</returns>
private bool ApplyDpiCompensation(
int index,
ref ComPtr<ID2D1Image> 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<ID2D1Effect> 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<ID2D1Effect>(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;
}
}
+24
ファイルの表示
@@ -34,4 +34,28 @@ internal static partial class CLSID
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
[NativeTypeName("const GUID")]
public static ref readonly Guid CLSID_D2D1DpiCompensation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ReadOnlySpan<byte> data = new byte[] {
0xC7, 0xC5, 0x26, 0x6C,
0xE0, 0x34,
0xFC, 0x46,
0x9C,
0xFD,
0xE5,
0x82,
0x37,
0x06,
0xE2,
0x28
};
return ref Unsafe.As<byte, Guid>(ref MemoryMarshal.GetReference(data));
}
}
}
+13
ファイルの表示
@@ -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,
}
@@ -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,
}
@@ -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,
}
+12
ファイルの表示
@@ -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;
}
+4
ファイルの表示
@@ -27,6 +27,9 @@
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformGraph.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effectauthor\ID2D1TransformNode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\CLSID.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_BORDER_MODE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_DPICOMPENSATION_INTERPOLATION_MODE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1effects\D2D1_DPICOMPENSATION_PROP.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\D2D1_EXTEND_MODE.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\ID2D1Factory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d2d1\ID2D1Image.cs" />
@@ -53,6 +56,7 @@
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\DirectX\um\d3dcommon\D3D_TESSELLATOR_PARTITIONING.cs" Link="DirectX\um\d3dcommon\D3D_TESSELLATOR_PARTITIONING.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" Link="DirectX\um\d3dcommon\D3D_TESSELLATOR_DOMAIN.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\d3d11shader\ID3D11ShaderReflection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DirectX\um\dcommon\D2D_VECTOR_2F.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Windows\shared\uuids\Windows.Manual.cs" />
<Compile Include="$(MSBuildThisFileDirectory)WinRT\um\windows.graphics.directx.direct3d11.interop\IDirect3DDxgiInterfaceAccess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\TerraFX.Interop.Windows\ComPtr`1.cs" Link="ComPtr`1.cs" />