From 404c0c3b4b9295b28274d4a7af25aef1c0671ef3 Mon Sep 17 00:00:00 2001 From: VperuS Date: Thu, 17 Aug 2017 08:32:22 +0300 Subject: [PATCH] Fix RefreshRate return 0 on MacOS --- src/OpenTK/OpenTK.csproj | 1 + src/OpenTK/Platform/MacOS/CoreVideo.cs | 34 +++++++++++++++++++ .../MacOS/QuartzDisplayDeviceDriver.cs | 14 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/OpenTK/Platform/MacOS/CoreVideo.cs diff --git a/src/OpenTK/OpenTK.csproj b/src/OpenTK/OpenTK.csproj index bf4af5e4..ee406638 100644 --- a/src/OpenTK/OpenTK.csproj +++ b/src/OpenTK/OpenTK.csproj @@ -780,6 +780,7 @@ + diff --git a/src/OpenTK/Platform/MacOS/CoreVideo.cs b/src/OpenTK/Platform/MacOS/CoreVideo.cs new file mode 100644 index 00000000..24ec94c4 --- /dev/null +++ b/src/OpenTK/Platform/MacOS/CoreVideo.cs @@ -0,0 +1,34 @@ +// See License.txt file for copyright details + +using System; +using System.Runtime.InteropServices; + +namespace OpenTK.Platform.MacOS +{ + [StructLayout(LayoutKind.Sequential)] + internal struct CVTime + { + public Int64 timeValue; + public Int32 timeScale; + public Int32 flags; + } + + internal class CV + { + private const string LibPath = "/System/Library/Frameworks/CoreVideo.framework/Versions/Current/CoreVideo"; + + internal enum TimeFlags : Int32 + { + TimeIsIndefinite = 1 << 0 + } + + [DllImport(LibPath, EntryPoint = "CVDisplayLinkCreateWithCGDisplay")] + public extern static IntPtr DisplayLinkCreateWithCGDisplay(IntPtr currentDisplay, out IntPtr displayLink); + + [DllImport(LibPath, EntryPoint = "CVDisplayLinkGetNominalOutputVideoRefreshPeriod")] + public extern static CVTime DisplayLinkGetNominalOutputVideoRefreshPeriod(IntPtr displayLink); + + [DllImport(LibPath, EntryPoint = "CVDisplayLinkRelease")] + public extern static void DisplayLinkRelease(IntPtr displayLink); + } +} diff --git a/src/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs b/src/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs index f6e4f5b4..4d6ed792 100644 --- a/src/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs +++ b/src/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs @@ -94,6 +94,20 @@ namespace OpenTK.Platform.MacOS double freq = dict.GetNumberValue("RefreshRate"); bool current = currentMode.Ref == dict.Ref; + if (freq <= 0) + { + IntPtr displayLink; + CV.DisplayLinkCreateWithCGDisplay(currentDisplay, out displayLink); + + CVTime t = CV.DisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink); + if ((t.flags & (Int32)CV.TimeFlags.TimeIsIndefinite) != (Int32)CV.TimeFlags.TimeIsIndefinite) + { + freq = (double)t.timeScale / t.timeValue; + } + + CV.DisplayLinkRelease(displayLink); + } + //if (current) Debug.Write(" * "); //else Debug.Write(" ");