From 475fea4d1d2ae82003cc20e13902081ce8560fd9 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Sat, 1 Aug 2020 14:04:08 +0300 Subject: [PATCH] Added SleepLoopRenderTimer because of libnvidia-glcore.so crashes --- .../Rendering/SleepLoopRenderTimer.cs | 33 +++++++++++++++++++ src/Avalonia.X11/X11Platform.cs | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs diff --git a/src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs b/src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs new file mode 100644 index 0000000000..a44891a888 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/SleepLoopRenderTimer.cs @@ -0,0 +1,33 @@ +using System; +using System.Diagnostics; +using System.Threading; + +namespace Avalonia.Rendering +{ + public class SleepLoopRenderTimer : IRenderTimer + { + public event Action Tick; + + public SleepLoopRenderTimer(int fps) + { + var timeBetweenTicks = TimeSpan.FromSeconds(1d / fps); + new Thread(() => + { + var st = Stopwatch.StartNew(); + var now = st.Elapsed; + var lastTick = now; + + while (true) + { + var timeTillNextTick = lastTick + timeBetweenTicks - now; + if (timeTillNextTick.TotalMilliseconds > 1) + Thread.Sleep(timeTillNextTick); + + + Tick?.Invoke(now); + now = st.Elapsed; + } + }) { IsBackground = true }.Start(); + } + } +} diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 9e9c4fd30e..7f3255d4da 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -47,7 +47,7 @@ namespace Avalonia.X11 AvaloniaLocator.CurrentMutable.BindToSelf(this) .Bind().ToConstant(this) .Bind().ToConstant(new X11PlatformThreading(this)) - .Bind().ToConstant(new DefaultRenderTimer(60)) + .Bind().ToConstant(new SleepLoopRenderTimer(60)) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new PlatformHotkeyConfiguration(KeyModifiers.Control)) .Bind().ToFunc(() => KeyboardDevice)