From 65f4e5dd74028a327ad31189c67cb9e7174bc9e8 Mon Sep 17 00:00:00 2001 From: Yuto Terada Date: Thu, 12 Dec 2024 16:39:22 +0900 Subject: [PATCH] Revert "Merge pull request #1203 from b-editor/feat/migrate-skia3" This reverts commit 21f9da9d726850339ca5f5792e163794e83fea4c, reversing changes made to 53922c4dfc4126a3cc699a278c2804bf447ed788. --- Directory.Packages.props | 12 ++++----- src/Beutl.Engine/Graphics/BrushConstructor.cs | 21 +++++++++------- .../FilterEffects/FilterEffectContext.cs | 14 ++--------- src/Beutl.Engine/Graphics/Image.cs | 22 ---------------- .../Graphics/SkiaSharpExtensions.cs | 13 ---------- .../Media/TextFormatting/FormattedText.cs | 25 +++++++++---------- 6 files changed, 32 insertions(+), 75 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 25b283216..f2a02fee6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -32,8 +32,8 @@ - - + + @@ -72,9 +72,9 @@ - - - + + + @@ -84,4 +84,4 @@ - \ No newline at end of file + diff --git a/src/Beutl.Engine/Graphics/BrushConstructor.cs b/src/Beutl.Engine/Graphics/BrushConstructor.cs index a1b4756dc..27da4933c 100644 --- a/src/Beutl.Engine/Graphics/BrushConstructor.cs +++ b/src/Beutl.Engine/Graphics/BrushConstructor.cs @@ -18,6 +18,9 @@ public void ConfigurePaint(SKPaint paint) float opacity = (Brush?.Opacity ?? 0) / 100f; paint.IsAntialias = true; paint.BlendMode = (SKBlendMode)BlendMode; + paint.HintingLevel = SKPaintHinting.Full; + paint.LcdRenderText = true; + paint.SubpixelText = true; paint.Color = new SKColor(255, 255, 255, (byte)(255 * opacity)); @@ -191,7 +194,7 @@ private void ConfigureGradientBrush(SKPaint paint, IGradientBrush gradientBrush) private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) { RenderTarget? renderTarget = null; - SKImage? skImage = null; + SKBitmap? skbitmap = null; PixelSize pixelSize; if (tileBrush is RenderSceneBrush sceneBrush) @@ -225,7 +228,7 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) { using (bitmap) { - skImage = bitmap.Value.ToSKImage(copy: true); + skbitmap = bitmap.Value.ToSKBitmap(); pixelSize = new(bitmap.Value.Width, bitmap.Value.Height); } } @@ -234,7 +237,7 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) throw new InvalidOperationException($"'{tileBrush.GetType().Name}' not supported."); } - if (renderTarget == null && skImage == null) + if (renderTarget == null && skbitmap == null) return; RenderTarget? intermediate = null; @@ -250,7 +253,7 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) SKCanvas canvas = intermediate.Value.Canvas; using var ipaint = new SKPaint(); { - var options = tileBrush.BitmapInterpolationMode.ToSKSamplingOptions(); + ipaint.FilterQuality = tileBrush.BitmapInterpolationMode.ToSKFilterQuality(); canvas.Clear(); canvas.Save(); @@ -259,8 +262,8 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) if (renderTarget != null) canvas.DrawSurface(renderTarget.Value, default, ipaint); - else if (skImage != null) - canvas.DrawImage(skImage, (SKPoint)default, options, ipaint); + else if (skbitmap != null) + canvas.DrawBitmap(skbitmap, (SKPoint)default, ipaint); canvas.Restore(); } @@ -291,8 +294,8 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) tileTransform = tileTransform.PreConcat(transform.ToSKMatrix()); } - using (SKImage snapshot = intermediate.Value.Snapshot()) - using (SKShader shader = snapshot.ToShader(tileX, tileY, tileTransform)) + using (SKImage skimage = intermediate.Value.Snapshot()) + using (SKShader shader = skimage.ToShader(tileX, tileY, tileTransform)) { paint.Shader = shader; } @@ -300,7 +303,7 @@ private void ConfigureTileBrush(SKPaint paint, ITileBrush tileBrush) finally { renderTarget?.Dispose(); - skImage?.Dispose(); + skbitmap?.Dispose(); intermediate?.Dispose(); } } diff --git a/src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs b/src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs index 1b1d71fe4..9f9648627 100644 --- a/src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs +++ b/src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs @@ -193,17 +193,7 @@ public void DisplacementMap( AppendSkiaFilter( data: (xChannelSelector, yChannelSelector, scale, child), factory: static (t, input, activator) - => - { - var displacement = activator.Activate(t.child); - if (displacement != null) - { - return SKImageFilter.CreateDisplacementMapEffect(t.xChannelSelector, t.yChannelSelector, t.scale, - displacement, input); - } - - return input; - }, + => SKImageFilter.CreateDisplacementMapEffect(t.xChannelSelector, t.yChannelSelector, t.scale, activator.Activate(t.child), input), transformBounds: static (data, bounds) => bounds.Inflate(data.scale / 2)); } @@ -308,7 +298,7 @@ public void Transform(Matrix matrix, BitmapInterpolationMode bitmapInterpolation { AppendSkiaFilter( (matrix, bitmapInterpolationMode), - (data, input, _) => SKImageFilter.CreateMatrix(data.matrix.ToSKMatrix(), data.bitmapInterpolationMode.ToSKSamplingOptions(), input), + (data, input, _) => SKImageFilter.CreateMatrix(data.matrix.ToSKMatrix(), data.bitmapInterpolationMode.ToSKFilterQuality(), input), (data, rect) => rect.TransformToAABB(data.matrix)); } diff --git a/src/Beutl.Engine/Graphics/Image.cs b/src/Beutl.Engine/Graphics/Image.cs index 92ca2cdae..7ff4835bf 100644 --- a/src/Beutl.Engine/Graphics/Image.cs +++ b/src/Beutl.Engine/Graphics/Image.cs @@ -146,28 +146,6 @@ public static SKBitmap ToSKBitmap(this IBitmap self) } } - public static SKImage ToSKImage(this IBitmap self, bool copy = false) - { - SKColorType? type = self switch - { - Bitmap => SKColorType.Bgra8888, - Bitmap => SKColorType.Argb4444, - Bitmap => SKColorType.Alpha8, - _ => null - }; - if (type.HasValue) - { - return copy - ? SKImage.FromPixelCopy(new(self.Width, self.Height, type.Value), self.Data) - : SKImage.FromPixels(new(self.Width, self.Height, type.Value), self.Data); - } - else - { - using Bitmap typed = self.Convert(); - return SKImage.FromPixelCopy(new(self.Width, self.Height, SKColorType.Bgra8888), typed.Data); - } - } - public static SKBitmap ToSKBitmap(this Mat self) { var result = new SKBitmap(new(self.Width, self.Height, SKColorType.Bgra8888)); diff --git a/src/Beutl.Engine/Graphics/SkiaSharpExtensions.cs b/src/Beutl.Engine/Graphics/SkiaSharpExtensions.cs index c44a8ec52..3b579d1f1 100644 --- a/src/Beutl.Engine/Graphics/SkiaSharpExtensions.cs +++ b/src/Beutl.Engine/Graphics/SkiaSharpExtensions.cs @@ -6,7 +6,6 @@ namespace Beutl.Graphics; internal static class SkiaSharpExtensions { - [Obsolete("Use ToSKSamplingOptions")] public static SKFilterQuality ToSKFilterQuality(this BitmapInterpolationMode interpolationMode) { return interpolationMode switch @@ -19,18 +18,6 @@ public static SKFilterQuality ToSKFilterQuality(this BitmapInterpolationMode int }; } - public static SKSamplingOptions ToSKSamplingOptions(this BitmapInterpolationMode interpolationMode) - { - return interpolationMode switch - { - BitmapInterpolationMode.Default => new SKSamplingOptions(SKFilterMode.Nearest, SKMipmapMode.None), - BitmapInterpolationMode.LowQuality => new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.None), - BitmapInterpolationMode.MediumQuality => new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear), - BitmapInterpolationMode.HighQuality => new SKSamplingOptions(SKCubicResampler.Mitchell), - _ => throw new ArgumentOutOfRangeException(nameof(interpolationMode), interpolationMode, null) - }; - } - public static SKPoint ToSKPoint(this Point p) { return new SKPoint(p.X, p.Y); diff --git a/src/Beutl.Engine/Media/TextFormatting/FormattedText.cs b/src/Beutl.Engine/Media/TextFormatting/FormattedText.cs index 676b33650..51f180d22 100644 --- a/src/Beutl.Engine/Media/TextFormatting/FormattedText.cs +++ b/src/Beutl.Engine/Media/TextFormatting/FormattedText.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using Beutl.Graphics; using Beutl.Graphics.Rendering; +using Beutl.Media.Immutable; using Beutl.Reactive; using SkiaSharp; using SkiaSharp.HarfBuzz; @@ -127,15 +128,16 @@ internal Point AddToSKPath(SKPath path, Point point) buffer.AddUtf16(Text.AsSpan()); buffer.GuessSegmentProperties(); - SKShaper.Result result = shaper.Shape(buffer, font); + using SKPaint paint = new() { TextSize = Size, Typeface = font.Typeface }; + SKShaper.Result result = shaper.Shape(buffer, paint); // create the text blob using var builder = new SKTextBlobBuilder(); SKPositionedRunBuffer run = builder.AllocatePositionedRun(font, result.Codepoints.Length); // copy the glyphs - Span glyphs = run.Glyphs; - Span positions = run.Positions; + Span glyphs = run.GetGlyphSpan(); + Span positions = run.GetPositionSpan(); for (int i = 0; i < result.Codepoints.Length; i++) { glyphs[i] = (ushort)result.Codepoints[i]; @@ -145,7 +147,7 @@ internal Point AddToSKPath(SKPath path, Point point) } // build - using SKTextBlob? textBlob = builder.Build(); + using SKTextBlob textBlob = builder.Build(); for (int i = 0; i < glyphs.Length; i++) { @@ -185,11 +187,7 @@ internal SKFont ToSKFont() { Edging = SKFontEdging.Antialias, Subpixel = true, - Hinting = SKFontHinting.Full, - Embolden = true - //paint.HintingLevel = SKPaintHinting.Full; - //paint.LcdRenderText = true; - //paint.SubpixelText = true; + Hinting = SKFontHinting.Full }; return font; @@ -210,15 +208,16 @@ private void Measure() buffer.AddUtf16(Text.AsSpan()); buffer.GuessSegmentProperties(); - SKShaper.Result result = shaper.Shape(buffer, font); + using SKPaint paint = new() { TextSize = Size, Typeface = font.Typeface, }; + SKShaper.Result result = shaper.Shape(buffer, paint); // create the text blob using var builder = new SKTextBlobBuilder(); SKPositionedRunBuffer run = builder.AllocatePositionedRun(font, result.Codepoints.Length); var fillPath = new SKPath(); - Span glyphs = run.Glyphs; - Span positions = run.Positions; + Span glyphs = run.GetGlyphSpan(); + Span positions = run.GetPositionSpan(); CollectionsMarshal.SetCount(_pathList, result.Codepoints.Length); Span pathList = CollectionsMarshal.AsSpan(_pathList); for (int i = 0; i < result.Codepoints.Length; i++) @@ -251,7 +250,7 @@ private void Measure() // 空白で開始または、終了した場合 var bounds = new Rect(0, 0, (glyphs.Length - 1) * Spacing + result.Width, fillPath.TightBounds.Height); Rect actualBounds = fillPath.TightBounds.ToGraphicsRect(); - SKTextBlob? textBlob = builder.Build(); + SKTextBlob textBlob = builder.Build(); if (result.Codepoints.Length > 0) {