Skip to content

Commit d050f20

Browse files
committed
Improve performance avoid the GC stress.
1 parent 95c2ad8 commit d050f20

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Avalonia.X11/Screens/X11Screens.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ public Screen ScreenFromPoint(PixelPoint point)
8484
return ScreenHelper.ScreenFromPoint(point, AllScreens);
8585
}
8686

87+
internal PixelRect? GetScreenBoundsFromPoint(PixelPoint point)
88+
{
89+
// Why not ScreenFromPoint? Because the AllScreens property will create some Screen instances which will cause the stress of GC. This method will be called in every touch event, so we need to avoid the GC stress.
90+
foreach (var screen in _impl.Screens)
91+
{
92+
if (screen.Bounds.ContainsExclusive(point))
93+
{
94+
return screen.Bounds;
95+
}
96+
}
97+
98+
return null;
99+
}
100+
87101
public Screen ScreenFromRect(PixelRect rect)
88102
{
89103
return ScreenHelper.ScreenFromRect(rect, AllScreens);

src/Avalonia.X11/XI2Manager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,11 @@ private void OnDeviceEvent(IXI2Client client, ParsedDeviceEvent ev)
275275
PixelRect screenBounds = default;
276276
if (ev.Valuators.TryGetValue(touchMajorXIValuatorClassInfo.Number, out var touchMajorValue))
277277
{
278-
var screen = _platform.X11Screens.ScreenFromPoint(new PixelPoint((int)ev.RootPosition.X, (int)ev.RootPosition.Y));
279-
Debug.Assert(screen != null);
280-
if (screen != null)
278+
var screenBoundsFromPoint = _platform.X11Screens.GetScreenBoundsFromPoint(new PixelPoint((int)ev.RootPosition.X, (int)ev.RootPosition.Y));
279+
Debug.Assert(screenBoundsFromPoint != null);
280+
if (screenBoundsFromPoint != null)
281281
{
282-
screenBounds = screen.Bounds;
282+
screenBounds = screenBoundsFromPoint.Value;
283283

284284
// As https://www.kernel.org/doc/html/latest/input/multi-touch-protocol.html says, using `screenBounds.Width` is not accurate enough.
285285
touchMajor = (touchMajorValue - touchMajorXIValuatorClassInfo.Min) /

0 commit comments

Comments
 (0)