Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Add new property DisplayInfo.RefreshRate (#1505)
Browse files Browse the repository at this point in the history
* DisplayInfo: add a property 'RefreshRate' (#1443)
* Samples: show RefreshRate on DeviceInfoPage (#1443)
* Add documentation for DisplayInfo.RefreshRate (#1443)
* Add the logic to read the macos refresh rate
  Based on xamarin-macios issue: xamarin/xamarin-macios#9958
  Copied code from: https://gist.github.com/chamons/1eae64a2e5e10cbaf18b140e92ec4ca3
* Try to guess the Hz, but fall back to 60
  This is not the most correct way, but is probably good enough. If there are any better ways in future, we can just add them in or replace this logic.

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
janusw and mattleibow committed Jan 25, 2021
1 parent db10113 commit 90a40a3
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 7 deletions.
1 change: 1 addition & 0 deletions Samples/Samples/View/DeviceInfoPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Label Text="{Binding ScreenMetrics.Density, StringFormat='Density: {0}'}" />
<Label Text="{Binding ScreenMetrics.Orientation, StringFormat='Orientation: {0}'}" />
<Label Text="{Binding ScreenMetrics.Rotation, StringFormat='Rotation: {0}'}" />
<Label Text="{Binding ScreenMetrics.RefreshRate, StringFormat='Refresh Rate: {0}'}" />
</StackLayout>
</ScrollView>
</StackLayout>
Expand Down
7 changes: 4 additions & 3 deletions Xamarin.Essentials/DeviceDisplay/DeviceDisplay.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ static bool PlatformKeepScreenOn
static DisplayInfo GetMainDisplayInfo()
{
using var displayMetrics = new DisplayMetrics();
using var display = GetDefaultDisplay();
var display = GetDefaultDisplay();
display?.GetRealMetrics(displayMetrics);

return new DisplayInfo(
width: displayMetrics?.WidthPixels ?? 0,
height: displayMetrics?.HeightPixels ?? 0,
density: displayMetrics?.Density ?? 0,
orientation: CalculateOrientation(),
rotation: CalculateRotation());
rotation: CalculateRotation(),
rate: display?.RefreshRate ?? 0);
}

static void StartScreenMetricsListeners()
Expand All @@ -66,7 +67,7 @@ static void OnScreenMetricsChanged()

static DisplayRotation CalculateRotation()
{
using var display = GetDefaultDisplay();
var display = GetDefaultDisplay();

return display?.Rotation switch
{
Expand Down
3 changes: 2 additions & 1 deletion Xamarin.Essentials/DeviceDisplay/DeviceDisplay.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ static DisplayInfo GetMainDisplayInfo()
height: bounds.Height * scale,
density: scale,
orientation: CalculateOrientation(),
rotation: CalculateRotation());
rotation: CalculateRotation(),
rate: UIScreen.MainScreen.MaximumFramesPerSecond);
}

static void StartScreenMetricsListeners()
Expand Down
13 changes: 12 additions & 1 deletion Xamarin.Essentials/DeviceDisplay/DeviceDisplay.macos.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AppKit;
using CoreVideo;
using Foundation;

namespace Xamarin.Essentials
Expand Down Expand Up @@ -38,12 +39,22 @@ static DisplayInfo GetMainDisplayInfo()
var frame = mainScreen.Frame;
var scale = mainScreen.BackingScaleFactor;

var mainDisplayId = CoreGraphicsInterop.MainDisplayId;

// try determine the refresh rate, but fall back to 60Hz
var refreshRate = CoreGraphicsInterop.GetRefreshRate(mainDisplayId);
if (refreshRate == 0)
refreshRate = CVDisplayLinkInterop.GetRefreshRate(mainDisplayId);
if (refreshRate == 0)
refreshRate = 60.0;

return new DisplayInfo(
width: frame.Width,
height: frame.Height,
density: scale,
orientation: DisplayOrientation.Portrait,
rotation: DisplayRotation.Rotation0);
rotation: DisplayRotation.Rotation0,
rate: (float)refreshRate);
}

static void StartScreenMetricsListeners()
Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Essentials/DeviceDisplay/DeviceDisplay.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static bool KeepScreenOn
public static DisplayInfo MainDisplayInfo => GetMainDisplayInfo();

static void SetCurrent(DisplayInfo metrics) =>
currentMetrics = new DisplayInfo(metrics.Width, metrics.Height, metrics.Density, metrics.Orientation, metrics.Rotation);
currentMetrics = new DisplayInfo(metrics.Width, metrics.Height, metrics.Density, metrics.Orientation, metrics.Rotation, metrics.RefreshRate);

public static event EventHandler<DisplayInfoChangedEventArgs> MainDisplayInfoChanged
{
Expand Down
7 changes: 6 additions & 1 deletion Xamarin.Essentials/DeviceDisplay/DeviceDisplay.uwp.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Windows.Graphics.Display;
using Windows.Graphics.Display.Core;
using Windows.System.Display;

namespace Xamarin.Essentials
Expand Down Expand Up @@ -54,12 +55,16 @@ static DisplayInfo GetMainDisplayInfo(DisplayInformation di = null)
var w = di.ScreenWidthInRawPixels;
var h = di.ScreenHeightInRawPixels;

var hdi = HdmiDisplayInformation.GetForCurrentView();
var hdm = hdi?.GetCurrentDisplayMode();

return new DisplayInfo(
width: perpendicular ? h : w,
height: perpendicular ? w : h,
density: di.LogicalDpi / 96.0,
orientation: CalculateOrientation(di),
rotation: rotation);
rotation: rotation,
rate: (float)(hdm?.RefreshRate ?? 0));
}

static void StartScreenMetricsListeners()
Expand Down
52 changes: 52 additions & 0 deletions Xamarin.Essentials/Platform/Platform.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using AppKit;
using CoreFoundation;
using CoreVideo;
using Foundation;
using ObjCRuntime;

Expand All @@ -20,6 +21,57 @@ internal static NSWindow GetCurrentWindow(bool throwIfNull = true)
}
}

internal static class CoreGraphicsInterop
{
public static uint MainDisplayId => CGMainDisplayID();

[DllImport(Constants.CoreGraphicsLibrary)]
static extern IntPtr CGDisplayCopyDisplayMode(uint display);

[DllImport(Constants.CoreGraphicsLibrary)]
static extern void CGDisplayModeRelease(IntPtr mode);

[DllImport(Constants.CoreGraphicsLibrary)]
static extern double CGDisplayModeGetRefreshRate(IntPtr mode);

[DllImport(Constants.CoreGraphicsLibrary)]
static extern uint CGMainDisplayID();

public static double GetRefreshRate(uint display)
{
var mode = CGDisplayCopyDisplayMode(display);
if (mode == IntPtr.Zero)
return 0.0;

var refreshRate = CGDisplayModeGetRefreshRate(mode);

CGDisplayModeRelease(mode);

return refreshRate;
}
}

internal static class CVDisplayLinkInterop
{
[DllImport(Constants.CoreGraphicsLibrary)]
static extern int CVDisplayLinkCreateWithCGDisplay(uint display, out IntPtr handle);

public static double GetRefreshRate(uint display)
{
var result = CVDisplayLinkCreateWithCGDisplay(display, out var handle);
if (result != 0 || handle == IntPtr.Zero)
return 0.0;

using var dl = new CVDisplayLink(handle);

var period = dl.NominalOutputVideoRefreshPeriod;
if (((CVTimeFlags)period.Flags).HasFlag(CVTimeFlags.IsIndefinite) || period.TimeValue == 0)
return 0.0;

return period.TimeScale / (double)period.TimeValue;
}
}

internal static class IOKit
{
const string IOKitLibrary = "/System/Library/Frameworks/IOKit.framework/IOKit";
Expand Down
13 changes: 13 additions & 0 deletions Xamarin.Essentials/Types/DisplayInfo.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ public DisplayInfo(double width, double height, double density, DisplayOrientati
Density = density;
Orientation = orientation;
Rotation = rotation;
RefreshRate = 0;
}

public DisplayInfo(double width, double height, double density, DisplayOrientation orientation, DisplayRotation rotation, float rate)
{
Width = width;
Height = height;
Density = density;
Orientation = orientation;
Rotation = rotation;
RefreshRate = rate;
}

public double Width { get; }
Expand All @@ -24,6 +35,8 @@ public DisplayInfo(double width, double height, double density, DisplayOrientati

public DisplayRotation Rotation { get; }

public float RefreshRate { get; }

public static bool operator ==(DisplayInfo left, DisplayInfo right) =>
left.Equals(right);

Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -322,6 +323,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -310,6 +311,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-macos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -309,6 +310,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-tvos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -309,6 +310,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -311,6 +312,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-watchos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -309,6 +310,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
2 changes: 2 additions & 0 deletions docs/en/FrameworksIndex/xamarin-essentials.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
</Type>
<Type Name="Xamarin.Essentials.DisplayInfo" Id="T:Xamarin.Essentials.DisplayInfo">
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(System.Object)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.Equals(Xamarin.Essentials.DisplayInfo)" />
<Member Id="M:Xamarin.Essentials.DisplayInfo.GetHashCode" />
Expand All @@ -308,6 +309,7 @@
<Member Id="P:Xamarin.Essentials.DisplayInfo.Density" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Height" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Orientation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Rotation" />
<Member Id="P:Xamarin.Essentials.DisplayInfo.Width" />
</Type>
Expand Down
48 changes: 48 additions & 0 deletions docs/en/Xamarin.Essentials/DisplayInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public DisplayInfo (double width, double height, double density, Xamarin.Essentials.DisplayOrientation orientation, Xamarin.Essentials.DisplayRotation rotation, float rate);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(float64 width, float64 height, float64 density, valuetype Xamarin.Essentials.DisplayOrientation orientation, valuetype Xamarin.Essentials.DisplayRotation rotation, float32 rate) cil managed" />
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.DisplayInfo.#ctor(System.Double,System.Double,System.Double,Xamarin.Essentials.DisplayOrientation,Xamarin.Essentials.DisplayRotation,System.Single)" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="width" Type="System.Double" />
<Parameter Name="height" Type="System.Double" />
<Parameter Name="density" Type="System.Double" />
<Parameter Name="orientation" Type="Xamarin.Essentials.DisplayOrientation" />
<Parameter Name="rotation" Type="Xamarin.Essentials.DisplayRotation" />
<Parameter Name="rate" Type="System.Single" />
</Parameters>
<Docs>
<param name="width">The width of the display</param>
<param name="height">The height of the display</param>
<param name="density">The screen density</param>
<param name="orientation">The current orientation</param>
<param name="rotation">The rotation of the device.</param>
<param name="rate">The refresh rate of the display.</param>
<summary>Main constructor for Display Information</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="Density">
<MemberSignature Language="C#" Value="public double Density { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance float64 Density" />
Expand Down Expand Up @@ -222,6 +250,26 @@
<remarks></remarks>
</Docs>
</Member>
<Member MemberName="RefreshRate">
<MemberSignature Language="C#" Value="public float RefreshRate { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance float32 RefreshRate" />
<MemberSignature Language="DocId" Value="P:Xamarin.Essentials.DisplayInfo.RefreshRate" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Single</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets the refresh rate of the display.</summary>
<value>The refresh rate in Hz.</value>
<remarks>
<para></para>
</remarks>
</Docs>
</Member>
<Member MemberName="Rotation">
<MemberSignature Language="C#" Value="public Xamarin.Essentials.DisplayRotation Rotation { get; }" />
<MemberSignature Language="ILAsm" Value=".property instance valuetype Xamarin.Essentials.DisplayRotation Rotation" />
Expand Down

0 comments on commit 90a40a3

Please sign in to comment.