From ff23551c0128065cb980e64b08e41a0753931399 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Tue, 2 Oct 2018 15:10:17 +0300 Subject: [PATCH] Introduced GTK platform options with environment based configuration --- src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs | 44 +++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs index 9fae22b0a9..e2e8a34676 100644 --- a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs +++ b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs @@ -24,8 +24,33 @@ namespace Avalonia.Gtk3 internal static string DisplayClassName; public static bool UseDeferredRendering = true; private static bool s_gtkInitialized; - public static void Initialize() + + static bool EnvOption(string option, bool def, bool? specified) { + bool? Parse(string env) + { + var v = Environment.GetEnvironmentVariable("AVALONIA_GTK3_" + env); + if (v == null) + return null; + if (v.ToLowerInvariant() == "false" || v == "0") + return false; + return true; + } + + var overridden = Parse(option + "_OVERRIDE"); + if (overridden.HasValue) + return overridden.Value; + if (specified.HasValue) + return specified.Value; + var envValue = Parse(option); + return envValue ?? def; + } + + public static void Initialize(Gtk3PlatformOptions options) + { + Resolver.Custom = options.CustomResolver; + UseDeferredRendering = EnvOption("USE_DEFERRED_RENDERING", true, options.UseDeferredRendering); + var useGpu = EnvOption("USE_GPU", false, options.UseGpuAcceleration); if (!s_gtkInitialized) { try @@ -57,7 +82,8 @@ namespace Avalonia.Gtk3 .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new DefaultRenderTimer(60)) .Bind().ToConstant(new PlatformIconLoader()); - EglGlPlatformFeature.TryInitialize(); + if (useGpu) + EglGlPlatformFeature.TryInitialize(); } public IWindowImpl CreateWindow() => new WindowImpl(); @@ -118,18 +144,24 @@ namespace Avalonia.Gtk3 public bool CurrentThreadIsLoopThread => s_tlsMarker; } + + public class Gtk3PlatformOptions + { + public bool? UseDeferredRendering { get; set; } + public bool? UseGpuAcceleration { get; set; } + public ICustomGtk3NativeLibraryResolver CustomResolver { get; set; } + } } namespace Avalonia { public static class Gtk3AppBuilderExtensions { - public static T UseGtk3(this AppBuilderBase builder, bool deferredRendering = true, ICustomGtk3NativeLibraryResolver resolver = null) + public static T UseGtk3(this AppBuilderBase builder, Gtk3PlatformOptions options = null) where T : AppBuilderBase, new() { - Resolver.Custom = resolver; - Gtk3Platform.UseDeferredRendering = deferredRendering; - return builder.UseWindowingSubsystem(Gtk3Platform.Initialize, "GTK3"); + return builder.UseWindowingSubsystem(() => Gtk3Platform.Initialize(options ?? new Gtk3PlatformOptions()), + "GTK3"); } } }