From 67e02bda4ac442871130d7d1abd8cc1ad8ada68a Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Thu, 23 Jun 2022 13:05:10 -0400 Subject: [PATCH 1/2] perf(macOS): Cache DisplayInformation properties --- .../Display/DisplayInformation.macOS.cs | 79 +++++++++++++------ 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/src/Uno.UWP/Graphics/Display/DisplayInformation.macOS.cs b/src/Uno.UWP/Graphics/Display/DisplayInformation.macOS.cs index 1fd0509066d6..dfbbcdc0987d 100644 --- a/src/Uno.UWP/Graphics/Display/DisplayInformation.macOS.cs +++ b/src/Uno.UWP/Graphics/Display/DisplayInformation.macOS.cs @@ -14,17 +14,8 @@ public sealed partial class DisplayInformation public DisplayOrientations CurrentOrientation { - get - { - if (NSScreen.MainScreen.Frame.Width > NSScreen.MainScreen.Frame.Height) - { - return DisplayOrientations.Landscape; - } - else - { - return DisplayOrientations.Portrait; - } - } + get; + private set; } /// @@ -36,33 +27,69 @@ public DisplayOrientations CurrentOrientation public uint ScreenHeightInRawPixels { - get - { - var screenSize = NSScreen.MainScreen.Frame.Size; - var scale = NSScreen.MainScreen.BackingScaleFactor; - return (uint)(screenSize.Height * scale); - } + get; + private set; } public uint ScreenWidthInRawPixels { - get - { - var screenSize = NSScreen.MainScreen.Frame.Size; - var scale = NSScreen.MainScreen.BackingScaleFactor; - return (uint)(screenSize.Width * scale); - } + get; + private set; } - public double RawPixelsPerViewPixel => NSScreen.MainScreen.BackingScaleFactor; + public double RawPixelsPerViewPixel + { + get; + private set; + } /// /// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP. /// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale /// - public float LogicalDpi => (float)(NSScreen.MainScreen.BackingScaleFactor * BaseDpi); + public float LogicalDpi + { + get; + private set; + } + + public ResolutionScale ResolutionScale + { + get; + private set; + } + + private void Update() + { + var screen = NSScreen.MainScreen; + var rect = screen.ConvertRectToBacking(screen.Frame); + + ScreenHeightInRawPixels = (uint)rect.Height; + ScreenWidthInRawPixels = (uint)rect.Width; + RawPixelsPerViewPixel = screen.BackingScaleFactor; + + CurrentOrientation = ScreenWidthInRawPixels > ScreenHeightInRawPixels + ? DisplayOrientations.Landscape : DisplayOrientations.Portrait; - public ResolutionScale ResolutionScale => (ResolutionScale)(int)(NSScreen.MainScreen.BackingScaleFactor * 100.0); + LogicalDpi = (float)(RawPixelsPerViewPixel * BaseDpi); + + ResolutionScale = (ResolutionScale)(int)(RawPixelsPerViewPixel * 100.0); + } + + partial void Initialize() + { + NSNotificationCenter + .DefaultCenter + .AddObserver( + NSWindow.DidChangeScreenNotification, + n => + { + Update(); + } + ); + // we are notified on changes but we're already on a screen so let's initialize + Update(); + } partial void StartOrientationChanged() => ObserveDisplayMetricsChanges(); From a5919a81789d1d53aeff84a588ac91f93375eff2 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Thu, 23 Jun 2022 14:17:29 -0400 Subject: [PATCH 2/2] perf(iOS): Cache DisplayInformation properties --- .../Display/DisplayInformation.iOS.cs | 73 +++++++++++++------ 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs b/src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs index 24a5ecff407e..32ee48ad6815 100644 --- a/src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs +++ b/src/Uno.UWP/Graphics/Display/DisplayInformation.iOS.cs @@ -1,5 +1,4 @@ -#if __IOS__ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -36,37 +35,37 @@ public sealed partial class DisplayInformation public uint ScreenHeightInRawPixels { - get - { - var screenSize = UIScreen.MainScreen.Bounds.Size; - var scale = UIScreen.MainScreen.NativeScale; - return (uint)(screenSize.Height * scale); - } + get; + private set; } public uint ScreenWidthInRawPixels { - get - { - var screenSize = UIScreen.MainScreen.Bounds.Size; - var scale = UIScreen.MainScreen.NativeScale; - return (uint)(screenSize.Width * scale); - } + get; + private set; } - public double RawPixelsPerViewPixel => UIScreen.MainScreen.NativeScale; + public double RawPixelsPerViewPixel + { + get; + private set; + } + /// + /// Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP. + /// https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale + /// public float LogicalDpi { - get - { - // Scale of 1 is considered @1x, which is the equivalent of 96.0 or 100% for UWP. - // https://developer.apple.com/documentation/uikit/uiscreen/1617836-scale - return (float)(UIScreen.MainScreen.Scale * BaseDpi); - } + get; + private set; } - public ResolutionScale ResolutionScale => (ResolutionScale)(int)(UIScreen.MainScreen.Scale * 100.0); + public ResolutionScale ResolutionScale + { + get; + private set; + } /// /// Sets the NativeOrientation property @@ -106,6 +105,35 @@ private DisplayOrientations GetCurrentOrientation() } } + private void Update() + { + var screen = UIScreen.MainScreen; + var bounds = screen.Bounds; + + RawPixelsPerViewPixel = screen.NativeScale; + ScreenHeightInRawPixels = (uint)(bounds.Height * RawPixelsPerViewPixel); + ScreenWidthInRawPixels = (uint)(bounds.Width * RawPixelsPerViewPixel); + + LogicalDpi = (float)(RawPixelsPerViewPixel * BaseDpi); + + ResolutionScale = (ResolutionScale)(int)(RawPixelsPerViewPixel * 100.0); + } + + partial void Initialize() + { + NSNotificationCenter + .DefaultCenter + .AddObserver( + UIScreen.ModeDidChangeNotification, + n => + { + Update(); + } + ); + // we are notified on changes but we're already on a screen so let's initialize + Update(); + } + partial void StartOrientationChanged() => ObserveDisplayMetricsChanges(); partial void StopOrientationChanged() => UnobserveDisplayMetricsChanges(); @@ -170,4 +198,3 @@ static partial void SetOrientationPartial(DisplayOrientations orientations) } } } -#endif