From e3dd353da3dc95d3d934872c1541fce010be9548 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Thu, 4 May 2023 20:29:38 +0600 Subject: [PATCH 1/2] Use SKBitmap-backed bitmap for RenderTargetBitmap --- .../Avalonia.Skia/PlatformRenderInterface.cs | 11 +------ .../Avalonia.Skia/RenderTargetBitmapImpl.cs | 31 +++++++++++++++++++ src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs | 2 +- 3 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 src/Skia/Avalonia.Skia/RenderTargetBitmapImpl.cs diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index a9a79ff0c5..767be3149d 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -191,16 +191,7 @@ namespace Avalonia.Skia throw new ArgumentException("Height can't be less than 1", nameof(size)); } - var createInfo = new SurfaceRenderTarget.CreateInfo - { - Width = size.Width, - Height = size.Height, - Dpi = dpi, - DisableTextLcdRendering = false, - DisableManualFbo = true, - }; - - return new SurfaceRenderTarget(createInfo); + return new RenderTargetBitmapImpl(size, dpi); } /// diff --git a/src/Skia/Avalonia.Skia/RenderTargetBitmapImpl.cs b/src/Skia/Avalonia.Skia/RenderTargetBitmapImpl.cs new file mode 100644 index 0000000000..e20755b4e2 --- /dev/null +++ b/src/Skia/Avalonia.Skia/RenderTargetBitmapImpl.cs @@ -0,0 +1,31 @@ +using System.IO; +using Avalonia.Controls.Platform.Surfaces; +using Avalonia.Media.Imaging; +using Avalonia.Platform; +using SkiaSharp; + +namespace Avalonia.Skia; + +internal class RenderTargetBitmapImpl : WriteableBitmapImpl, + IRenderTargetBitmapImpl, + IFramebufferPlatformSurface +{ + private readonly FramebufferRenderTarget _renderTarget; + + public RenderTargetBitmapImpl(PixelSize size, Vector dpi) : base(size, dpi, + SKImageInfo.PlatformColorType == SKColorType.Rgba8888 ? PixelFormats.Rgba8888 : PixelFormat.Bgra8888, + AlphaFormat.Premul) + { + _renderTarget = new FramebufferRenderTarget(this); + } + + public IDrawingContextImpl CreateDrawingContext() => _renderTarget.CreateDrawingContext(); + + public bool IsCorrupted => false; + + public override void Dispose() + { + _renderTarget.Dispose(); + base.Dispose(); + } +} \ No newline at end of file diff --git a/src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs b/src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs index 8ea7434c23..38df7b2933 100644 --- a/src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs +++ b/src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs @@ -131,7 +131,7 @@ namespace Avalonia.Skia } /// - public void Dispose() + public virtual void Dispose() { _bitmap.Dispose(); } From 9ab08885ed620b82bcea67e1fef095b3a83c5338 Mon Sep 17 00:00:00 2001 From: Nikita Tsukanov Date: Thu, 4 May 2023 20:29:53 +0600 Subject: [PATCH 2/2] Fixed RTB Dispose --- src/Avalonia.Base/Media/Imaging/RenderTargetBitmap.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Media/Imaging/RenderTargetBitmap.cs b/src/Avalonia.Base/Media/Imaging/RenderTargetBitmap.cs index e77dd9d1ab..4921e9b756 100644 --- a/src/Avalonia.Base/Media/Imaging/RenderTargetBitmap.cs +++ b/src/Avalonia.Base/Media/Imaging/RenderTargetBitmap.cs @@ -9,7 +9,7 @@ namespace Avalonia.Media.Imaging /// /// A bitmap that holds the rendering of a . /// - public class RenderTargetBitmap : Bitmap, IDisposable + public class RenderTargetBitmap : Bitmap { /// /// Initializes a new instance of the class. @@ -68,5 +68,11 @@ namespace Avalonia.Media.Imaging platform.Clear(Colors.Transparent); return new PlatformDrawingContext(platform); } + + public override void Dispose() + { + PlatformImpl.Dispose(); + base.Dispose(); + } } }