diff --git a/src/Skia/Avalonia.Skia/CustomRenderTarget.cs b/src/Skia/Avalonia.Skia/CustomRenderTarget.cs new file mode 100644 index 0000000000..23a509a2a4 --- /dev/null +++ b/src/Skia/Avalonia.Skia/CustomRenderTarget.cs @@ -0,0 +1,42 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Platform; +using Avalonia.Rendering; + +namespace Avalonia.Skia +{ + /// + /// Adapts to be used within Skia rendering pipeline. + /// + internal class CustomRenderTarget : IRenderTarget + { + private readonly ICustomSkiaRenderTarget _renderTarget; + + public CustomRenderTarget(ICustomSkiaRenderTarget renderTarget) + { + _renderTarget = renderTarget; + } + + public void Dispose() + { + _renderTarget.Dispose(); + } + + public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) + { + ICustomSkiaRenderSession session = _renderTarget.BeginRendering(); + + var nfo = new DrawingContextImpl.CreateInfo + { + GrContext = session.GrContext, + Canvas = session.Canvas, + Dpi = SkiaPlatform.DefaultDpi * session.ScaleFactor, + VisualBrushRenderer = visualBrushRenderer, + DisableTextLcdRendering = true + }; + + return new DrawingContextImpl(nfo, session); + } + } +} diff --git a/src/Skia/Avalonia.Skia/ICustomSkiaGpu.cs b/src/Skia/Avalonia.Skia/ICustomSkiaGpu.cs new file mode 100644 index 0000000000..751dd3c1e7 --- /dev/null +++ b/src/Skia/Avalonia.Skia/ICustomSkiaGpu.cs @@ -0,0 +1,26 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Collections.Generic; +using SkiaSharp; + +namespace Avalonia.Skia +{ + /// + /// Custom Skia gpu instance. + /// + public interface ICustomSkiaGpu + { + /// + /// Skia GrContext used. + /// + GRContext GrContext { get; } + + /// + /// Attempts to create custom render target from given surfaces. + /// + /// Surfaces. + /// Created render target or if it fails. + ICustomSkiaRenderTarget TryCreateRenderTarget(IEnumerable surfaces); + } +} diff --git a/src/Skia/Avalonia.Skia/ICustomSkiaRenderSession.cs b/src/Skia/Avalonia.Skia/ICustomSkiaRenderSession.cs new file mode 100644 index 0000000000..6a4591921e --- /dev/null +++ b/src/Skia/Avalonia.Skia/ICustomSkiaRenderSession.cs @@ -0,0 +1,29 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using SkiaSharp; + +namespace Avalonia.Skia +{ + /// + /// Custom render session for Skia render target. + /// + public interface ICustomSkiaRenderSession : IDisposable + { + /// + /// GrContext used by this session. + /// + GRContext GrContext { get; } + + /// + /// Canvas that will be used to render. + /// + SKCanvas Canvas { get; } + + /// + /// Scaling factor. + /// + double ScaleFactor { get; } + } +} diff --git a/src/Skia/Avalonia.Skia/ICustomSkiaRenderTarget.cs b/src/Skia/Avalonia.Skia/ICustomSkiaRenderTarget.cs new file mode 100644 index 0000000000..f67b28b77b --- /dev/null +++ b/src/Skia/Avalonia.Skia/ICustomSkiaRenderTarget.cs @@ -0,0 +1,19 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; + +namespace Avalonia.Skia +{ + /// + /// Custom Skia render target. + /// + public interface ICustomSkiaRenderTarget : IDisposable + { + /// + /// Start rendering to this render target. + /// + /// + ICustomSkiaRenderSession BeginRendering(); + } +} diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index 6e13b154b4..ec162f9767 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -17,12 +17,23 @@ namespace Avalonia.Skia /// internal class PlatformRenderInterface : IPlatformRenderInterface { + private readonly ICustomSkiaGpu _customSkiaGpu; + private GRContext GrContext { get; } public IEnumerable InstalledFontNames => SKFontManager.Default.FontFamilies; - public PlatformRenderInterface() + public PlatformRenderInterface(ICustomSkiaGpu customSkiaGpu) { + if (customSkiaGpu != null) + { + _customSkiaGpu = customSkiaGpu; + + GrContext = _customSkiaGpu.GrContext; + + return; + } + var gl = AvaloniaLocator.Current.GetService(); if (gl != null) { @@ -32,12 +43,11 @@ namespace Avalonia.Skia ? GRGlInterface.AssembleGlInterface((_, proc) => display.GlInterface.GetProcAddress(proc)) : GRGlInterface.AssembleGlesInterface((_, proc) => display.GlInterface.GetProcAddress(proc))) { - GrContext = GRContext.Create(GRBackend.OpenGL, iface); } } } - + /// public IFormattedTextImpl CreateFormattedText( string text, @@ -98,13 +108,23 @@ namespace Avalonia.Skia DisableTextLcdRendering = false, GrContext = GrContext }; - + return new SurfaceRenderTarget(createInfo); } /// - public virtual IRenderTarget CreateRenderTarget(IEnumerable surfaces) + public IRenderTarget CreateRenderTarget(IEnumerable surfaces) { + if (_customSkiaGpu != null) + { + ICustomSkiaRenderTarget customRenderTarget = _customSkiaGpu.TryCreateRenderTarget(surfaces); + + if (customRenderTarget != null) + { + return new CustomRenderTarget(customRenderTarget); + } + } + foreach (var surface in surfaces) { if (surface is IGlPlatformSurface glSurface && GrContext != null) diff --git a/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs b/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs index f4412df473..102f1f92aa 100644 --- a/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs +++ b/src/Skia/Avalonia.Skia/SkiaApplicationExtensions.cs @@ -20,8 +20,9 @@ namespace Avalonia /// Configure builder. public static T UseSkia(this T builder) where T : AppBuilderBase, new() { - builder.UseRenderingSubsystem(() => SkiaPlatform.Initialize(), "Skia"); - return builder; + return builder.UseRenderingSubsystem(() => SkiaPlatform.Initialize( + AvaloniaLocator.Current.GetService() ?? new SkiaOptions()), + "Skia"); } } } diff --git a/src/Skia/Avalonia.Skia/SkiaOptions.cs b/src/Skia/Avalonia.Skia/SkiaOptions.cs new file mode 100644 index 0000000000..bac1849be8 --- /dev/null +++ b/src/Skia/Avalonia.Skia/SkiaOptions.cs @@ -0,0 +1,19 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using Avalonia.Skia; + +namespace Avalonia +{ + /// + /// Options for Skia rendering subsystem. + /// + public class SkiaOptions + { + /// + /// Custom gpu factory to use. Can be used to customize behavior of Skia renderer. + /// + public Func CustomGpuFactory { get; set; } + } +} diff --git a/src/Skia/Avalonia.Skia/SkiaPlatform.cs b/src/Skia/Avalonia.Skia/SkiaPlatform.cs index a9d69aea31..f16e967f42 100644 --- a/src/Skia/Avalonia.Skia/SkiaPlatform.cs +++ b/src/Skia/Avalonia.Skia/SkiaPlatform.cs @@ -15,8 +15,14 @@ namespace Avalonia.Skia /// public static void Initialize() { - var renderInterface = new PlatformRenderInterface(); - + Initialize(new SkiaOptions()); + } + + public static void Initialize(SkiaOptions options) + { + var customGpu = options.CustomGpuFactory?.Invoke(); + var renderInterface = new PlatformRenderInterface(customGpu); + AvaloniaLocator.CurrentMutable .Bind().ToConstant(renderInterface); }