Browse Source

Use rendering mode

pull/11914/head
Nikita Tsukanov 3 years ago
parent
commit
e44c5a869a
  1. 39
      src/Avalonia.Native/AvaloniaNativePlatform.cs
  2. 36
      src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs

39
src/Avalonia.Native/AvaloniaNativePlatform.cs

@ -123,40 +123,45 @@ namespace Avalonia.Native
hotkeys.MoveCursorToTheEndOfLineWithSelection.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers | hotkeys.SelectionModifiers));
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(hotkeys);
if (_options.UseGpu)
foreach (var mode in _options.RenderingMode)
{
if (_options.UseMetal)
if (mode == AvaloniaNativeRenderingMode.OpenGl)
{
try
{
var metal = new MetalPlatformGraphics(_factory);
metal.CreateContext().Dispose();
_platformGraphics = metal;
_platformGraphics = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay());
break;
}
catch
catch (Exception)
{
// Ignored
// ignored
}
}
if (_platformGraphics == null)
#pragma warning disable CS0618
else if (mode == AvaloniaNativeRenderingMode.Metal)
#pragma warning restore CS0618
{
try
{
_platformGraphics = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay());
var metal = new MetalPlatformGraphics(_factory);
metal.CreateContext().Dispose();
_platformGraphics = metal;
}
catch (Exception)
catch
{
// ignored
// Ignored
}
}
if(_platformGraphics != null)
AvaloniaLocator.CurrentMutable
.Bind<IPlatformGraphics>().ToConstant(_platformGraphics);
else if (mode == AvaloniaNativeRenderingMode.Software)
break;
}
if (_platformGraphics != null)
AvaloniaLocator.CurrentMutable
.Bind<IPlatformGraphics>().ToConstant(_platformGraphics);
Compositor = new Compositor(_platformGraphics, true);
}

36
src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs

@ -26,18 +26,44 @@ namespace Avalonia
}
}
public enum AvaloniaNativeRenderingMode
{
/// <summary>
/// Avalonia would try to use native OpenGL with GPU rendering.
/// </summary>
OpenGl = 1,
/// <summary>
/// Avalonia is rendered into a framebuffer.
/// </summary>
Software = 2,
/// <summary>
/// Avalonia would try to use Metal with GPU rendering.
/// </summary>
[Obsolete("Experimental, unstable, not for production usage")]
Metal = 3
}
/// <summary>
/// OSX backend options.
/// </summary>
public class AvaloniaNativePlatformOptions
{
/// <summary>
/// Determines whether to use GPU for rendering in your project. The default value is true.
/// Gets or sets Avalonia rendering modes with fallbacks.
/// The first element in the array has the highest priority.
/// The default value is: <see cref="AvaloniaNativeRenderingMode.OpenGl"/>, <see cref="AvaloniaNativeRenderingMode.Software"/>.
/// </summary>
public bool UseGpu { get; set; } = true;
public bool UseMetal { get; set; }
/// <remarks>
/// If application should work on as wide range of devices as possible,
/// at least add <see cref="AvaloniaNativeRenderingMode.Software"/> as a fallback value.
/// </remarks>
/// <exception cref="System.InvalidOperationException">Thrown if no values were matched.</exception>
public IReadOnlyList<AvaloniaNativeRenderingMode> RenderingMode { get; set; } = new[]
{
AvaloniaNativeRenderingMode.OpenGl,
AvaloniaNativeRenderingMode.Software
};
/// <summary>
/// Embeds popups to the window when set to true. The default value is false.
/// </summary>

Loading…
Cancel
Save