Skip to content

Commit

Permalink
Merge pull request #18468 from ramezgerges/x11_rendertimer_interval
Browse files Browse the repository at this point in the history
fix: X11's render timer interval was set to 1 frame per 16 ticks, not 16 ms
  • Loading branch information
jeromelaban authored Oct 16, 2024
2 parents cca656d + 6475d1a commit c89139e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 12 additions & 1 deletion src/Uno.UI.Runtime.Skia.X11/Builder/X11HostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@ internal partial class X11HostBuilder : IPlatformHostBuilder
[GeneratedRegex(@"^(?:(?<hostname>[\w\.-]+))?:(?<displaynumber>\d+)(?:\.(?<screennumber>\d+))?$")]
private static partial Regex DisplayRegex();

private int _renderFrameRate = 60;

public X11HostBuilder()
{
}

/// <summary>
/// Sets the FPS that the application should try to achieve.
/// </summary>
public X11HostBuilder RenderFrameRate(int renderFrameRate)
{
_renderFrameRate = renderFrameRate;
return this;
}

public bool IsSupported
=> OperatingSystem.IsLinux() &&
Environment.GetEnvironmentVariable("DISPLAY") is { } displayString &&
DisplayRegex().Match(displayString).Success;

public SkiaHost Create(Func<Microsoft.UI.Xaml.Application> appBuilder)
=> new X11ApplicationHost(appBuilder);
=> new X11ApplicationHost(appBuilder, _renderFrameRate);
}
11 changes: 9 additions & 2 deletions src/Uno.UI.Runtime.Skia.X11/X11ApplicationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Uno.WinUI.Runtime.Skia.X11;
public partial class X11ApplicationHost : SkiaHost, ISkiaApplicationHost, IDisposable
{
[ThreadStatic] private static bool _isDispatcherThread;
[ThreadStatic] private static int _renderFrameRate;
private readonly EventLoop _eventLoop;

private readonly Func<Application> _appBuilder;
Expand Down Expand Up @@ -70,18 +71,24 @@ static X11ApplicationHost()
ApiExtensibility.Register<XamlRoot>(typeof(Uno.Graphics.INativeOpenGLWrapper), xamlRoot => new X11NativeOpenGLWrapper(xamlRoot));
}

public X11ApplicationHost(Func<Application> appBuilder)
public X11ApplicationHost(Func<Application> appBuilder, int renderFrameRate = 60)
{
_appBuilder = appBuilder;

_eventLoop = new EventLoop();
_eventLoop.Schedule(() => { Thread.CurrentThread.Name = "Uno Event Loop"; }, UI.Dispatching.NativeDispatcherPriority.Normal);

_eventLoop.Schedule(() => _isDispatcherThread = true, UI.Dispatching.NativeDispatcherPriority.Normal);
_eventLoop.Schedule(() =>
{
_isDispatcherThread = true;
_renderFrameRate = renderFrameRate;
}, UI.Dispatching.NativeDispatcherPriority.Normal);
CoreDispatcher.DispatchOverride = _eventLoop.Schedule;
CoreDispatcher.HasThreadAccessOverride = () => _isDispatcherThread;
}

internal static int RenderFrameRate => _renderFrameRate;

[LibraryImport("libc", StringMarshallingCustomType = typeof(AnsiStringMarshaller))]
private static partial void setlocale(int type, string s);

Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI.Runtime.Skia.X11/X11XamlRootHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public X11XamlRootHost(X11WindowWrapper wrapper, Window winUIWindow, XamlRoot xa
_configureCallback = configureCallback;

_renderTimer = new DispatcherTimer();
_renderTimer.Interval = new TimeSpan(1000 / 16);
_renderTimer.Interval = TimeSpan.FromSeconds(1.0 / X11ApplicationHost.RenderFrameRate); // we're on the UI thread
_renderTimer.Tick += (sender, o) =>
{
if (Interlocked.Exchange(ref _needsConfigureCallback, 0) == 1)
Expand All @@ -110,8 +110,8 @@ public X11XamlRootHost(X11WindowWrapper wrapper, Window winUIWindow, XamlRoot xa
if (_renderDirty)
{
_renderer?.Render();
_renderDirty = false;
_renderer?.Render();
}
};
_renderTimer.Start();
Expand Down

0 comments on commit c89139e

Please sign in to comment.