From ad400fbd3bf3266f3fdc5a118fe03c3299b4eac1 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 10 Dec 2022 20:23:10 +0100 Subject: [PATCH] Fix DPI compensation path when no device context is available --- .../Extensions/ICanvasDeviceExtensions.cs | 21 +++++++++++++++++++ ...derEffect{T}.Properties.Interop.Sources.cs | 14 ++++++++++--- .../um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs | 16 ++++++++++++++ .../DirectX/um/d2d1_2/ID2D1Device1.cs | 7 +++++++ .../TerraFX.Interop.Windows.D2D1.projitems | 1 + 5 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs diff --git a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs index d73d7534..94857be8 100644 --- a/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs +++ b/src/ComputeSharp.D2D1.Uwp/Extensions/ICanvasDeviceExtensions.cs @@ -35,4 +35,25 @@ internal static unsafe class ICanvasDeviceExtensions return hresult; } + + /// + /// Creates a new from an input . + /// + /// The input object. + /// The resulting object. + /// The for the operation. + public static HRESULT CreateD2DDeviceContext(this ref ICanvasDevice canvasDevice, ID2D1DeviceContext** d2D1DeviceContext) + { + using ComPtr d2D1Device1 = default; + + // Get the underlying ID2D1Device1 object + canvasDevice.GetD2DDevice(d2D1Device1.GetAddressOf()).Assert(); + + // Create the new device context + HRESULT hresult = d2D1Device1.Get()->CreateDeviceContext( + options: D2D1_DEVICE_CONTEXT_OPTIONS.D2D1_DEVICE_CONTEXT_OPTIONS_NONE, + deviceContext: d2D1DeviceContext); + + return hresult; + } } \ No newline at end of file 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 3d44e0f3..c4ef1365 100644 --- a/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs +++ b/src/ComputeSharp.D2D1.Uwp/PixelShaderEffect{T}.Properties.Interop.Sources.cs @@ -280,14 +280,22 @@ unsafe partial class PixelShaderEffect // 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 + using ComPtr d2D1DeviceContextEffective = default; + + // We need to create a DPI compensation effect, so a device context must be available. + // If there is no input device context, just create a new one from the realization device. if (d2D1DeviceContext is null) { - Marshal.ThrowExceptionForHR(E.E_POINTER); + this.canvasDevice.Get()->CreateD2DDeviceContext(d2D1DeviceContextEffective.GetAddressOf()).Assert(); + } + else + { + // Otherwise, just use the input device context + *&d2D1DeviceContextEffective = new ComPtr(d2D1DeviceContext); } // Create the DPI compensation effect - d2D1DeviceContext->CreateEffect( + d2D1DeviceContextEffective.Get()->CreateEffect( effectId: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_D2D1DpiCompensation)), effect: d2D1EffectDpiCompensation.GetAddressOf()).Assert(); diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs new file mode 100644 index 00000000..bd22e599 --- /dev/null +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_1/D2D1_DEVICE_CONTEXT_OPTIONS.cs @@ -0,0 +1,16 @@ +// Copyright © Tanner Gooding and Contributors. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +// Ported from um/d2d1_1.h in the Windows SDK for Windows 10.0.22621.0 +// Original source is Copyright © Microsoft. All rights reserved. + +using System; + +namespace TerraFX.Interop.DirectX; + +[Flags] +internal enum D2D1_DEVICE_CONTEXT_OPTIONS : uint +{ + D2D1_DEVICE_CONTEXT_OPTIONS_NONE = 0, + D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS = 1, + D2D1_DEVICE_CONTEXT_OPTIONS_FORCE_DWORD = 0xffffffff, +} \ No newline at end of file diff --git a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs index 193e8b8c..1a374296 100644 --- a/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs +++ b/src/TerraFX.Interop.Windows.D2D1/DirectX/um/d2d1_2/ID2D1Device1.cs @@ -49,4 +49,11 @@ internal unsafe partial struct ID2D1Device1 { ((delegate* unmanaged[Stdcall])(lpVtbl[3]))((ID2D1Device1*)Unsafe.AsPointer(ref this), factory); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + [VtblIndex(4)] + public HRESULT CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS options, ID2D1DeviceContext** deviceContext) + { + return ((delegate* unmanaged[Stdcall])(lpVtbl[4]))((ID2D1Device1*)Unsafe.AsPointer(ref this), options, deviceContext); + } } \ 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 cfa82c7b..51cb9906 100644 --- a/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems +++ b/src/TerraFX.Interop.Windows.D2D1/TerraFX.Interop.Windows.D2D1.projitems @@ -35,6 +35,7 @@ +