From 199d6678209308df971dc87d11fd03bc35618a11 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Sun, 5 Feb 2023 11:21:29 +0100 Subject: [PATCH] Lazy initialized Win32 pointer info buffers --- src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs | 5 +++++ src/Windows/Avalonia.Win32/WindowImpl.cs | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index d169ccfc1e..e8d2d8f0c5 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -793,6 +793,7 @@ namespace Avalonia.Win32 if (info.pointerType == PointerInputType.PT_TOUCH) { + s_historyTouchInfos ??= new POINTER_TOUCH_INFO[MaxPointerHistorySize]; if (GetPointerTouchInfoHistory(info.pointerId, ref historyCount, s_historyTouchInfos)) { for (int i = historyCount - 1; i >= 1; i--) @@ -804,6 +805,7 @@ namespace Avalonia.Win32 } else if (info.pointerType == PointerInputType.PT_PEN) { + s_historyPenInfos ??= new POINTER_PEN_INFO[MaxPointerHistorySize]; if (GetPointerPenInfoHistory(info.pointerId, ref historyCount, s_historyPenInfos)) { for (int i = historyCount - 1; i >= 1; i--) @@ -815,6 +817,7 @@ namespace Avalonia.Win32 } else { + s_historyInfos ??= new POINTER_INFO[MaxPointerHistorySize]; // Currently Windows does not return history info for mouse input, but we handle it just for case. if (GetPointerInfoHistory(info.pointerId, ref historyCount, s_historyInfos)) { @@ -837,6 +840,8 @@ namespace Avalonia.Win32 // To understand some of this code, please check MS docs: // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getmousemovepointsex#remarks + s_mouseHistoryInfos ??= new MOUSEMOVEPOINT[64]; + fixed (MOUSEMOVEPOINT* movePoints = s_mouseHistoryInfos) { var movePointCopy = movePoint; diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 39593331c1..419cb57e87 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -100,10 +100,10 @@ namespace Avalonia.Win32 private const int MaxPointerHistorySize = 512; private static readonly PooledList s_intermediatePointsPooledList = new(); - private static readonly POINTER_TOUCH_INFO[] s_historyTouchInfos = new POINTER_TOUCH_INFO[MaxPointerHistorySize]; - private static readonly POINTER_PEN_INFO[] s_historyPenInfos = new POINTER_PEN_INFO[MaxPointerHistorySize]; - private static readonly POINTER_INFO[] s_historyInfos = new POINTER_INFO[MaxPointerHistorySize]; - private static readonly MOUSEMOVEPOINT[] s_mouseHistoryInfos = new MOUSEMOVEPOINT[64]; + private static POINTER_TOUCH_INFO[]? s_historyTouchInfos; + private static POINTER_PEN_INFO[]? s_historyPenInfos; + private static POINTER_INFO[]? s_historyInfos; + private static MOUSEMOVEPOINT[]? s_mouseHistoryInfos; public WindowImpl() {