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)); hotkeys.MoveCursorToTheEndOfLineWithSelection.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers | hotkeys.SelectionModifiers));
AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(hotkeys); AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(hotkeys);
if (_options.UseGpu) foreach (var mode in _options.RenderingMode)
{ {
if (_options.UseMetal) if (mode == AvaloniaNativeRenderingMode.OpenGl)
{ {
try try
{ {
var metal = new MetalPlatformGraphics(_factory); _platformGraphics = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay());
metal.CreateContext().Dispose(); break;
_platformGraphics = metal;
} }
catch catch (Exception)
{ {
// Ignored // ignored
} }
} }
#pragma warning disable CS0618
if (_platformGraphics == null) else if (mode == AvaloniaNativeRenderingMode.Metal)
#pragma warning restore CS0618
{ {
try try
{ {
_platformGraphics = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay()); var metal = new MetalPlatformGraphics(_factory);
metal.CreateContext().Dispose();
_platformGraphics = metal;
} }
catch (Exception) catch
{ {
// ignored // Ignored
} }
} }
else if (mode == AvaloniaNativeRenderingMode.Software)
if(_platformGraphics != null) break;
AvaloniaLocator.CurrentMutable
.Bind<IPlatformGraphics>().ToConstant(_platformGraphics);
} }
if (_platformGraphics != null)
AvaloniaLocator.CurrentMutable
.Bind<IPlatformGraphics>().ToConstant(_platformGraphics);
Compositor = new Compositor(_platformGraphics, true); 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> /// <summary>
/// OSX backend options. /// OSX backend options.
/// </summary> /// </summary>
public class AvaloniaNativePlatformOptions public class AvaloniaNativePlatformOptions
{ {
/// <summary> /// <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> /// </summary>
public bool UseGpu { get; set; } = true; /// <remarks>
/// If application should work on as wide range of devices as possible,
public bool UseMetal { get; set; } /// 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> /// <summary>
/// Embeds popups to the window when set to true. The default value is false. /// Embeds popups to the window when set to true. The default value is false.
/// </summary> /// </summary>

Loading…
Cancel
Save