Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Merge pull request #1203 from b-editor/feat/migrate-skia3" #1208

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<PackageVersion Include="MonoMac.NetStandard" Version="0.0.4" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="NAudio.Wasapi" Version="2.2.1" />
<PackageVersion Include="HarfBuzzSharp" Version="8.3.0.1" />
<PackageVersion Include="HarfBuzzSharp.NativeAssets.Linux" Version="8.3.0.1" />
<PackageVersion Include="HarfBuzzSharp" Version="7.3.0.3" />
<PackageVersion Include="HarfBuzzSharp.NativeAssets.Linux" Version="7.3.0.3" />
<PackageVersion Include="ILGPU" Version="1.5.1" />
<PackageVersion Include="Kokuban" Version="0.2.0" />
<PackageVersion Include="Kurukuru" Version="1.4.2" />
Expand Down Expand Up @@ -72,9 +72,9 @@
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.OpenTelemetry" Version="1.2.0" />
<PackageVersion Include="Sharprompt" Version="2.4.5" />
<PackageVersion Include="SkiaSharp" Version="3.116.1" />
<PackageVersion Include="SkiaSharp.HarfBuzz" Version="3.116.1" />
<PackageVersion Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />
<PackageVersion Include="SkiaSharp" Version="2.88.9" />
<PackageVersion Include="SkiaSharp.HarfBuzz" Version="2.88.9" />
<PackageVersion Include="SkiaSharp.NativeAssets.Linux" Version="2.88.9" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="System.Interactive" Version="6.0.1" />
<PackageVersion Include="System.Interactive.Async" Version="6.0.1" />
Expand All @@ -84,4 +84,4 @@
<PackageVersion Include="Vortice.MediaFoundation" Version="3.6.2" />
<PackageVersion Include="Vortice.XAudio2" Version="3.6.2" />
</ItemGroup>
</Project>
</Project>
21 changes: 12 additions & 9 deletions src/Beutl.Engine/Graphics/BrushConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -291,16 +294,16 @@ 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;
}
}
finally
{
renderTarget?.Dispose();
skImage?.Dispose();
skbitmap?.Dispose();
intermediate?.Dispose();
}
}
Expand Down
14 changes: 2 additions & 12 deletions src/Beutl.Engine/Graphics/FilterEffects/FilterEffectContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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));
}

Expand Down
22 changes: 0 additions & 22 deletions src/Beutl.Engine/Graphics/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bgra8888> => SKColorType.Bgra8888,
Bitmap<Bgra4444> => SKColorType.Argb4444,
Bitmap<Grayscale8> => 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<Bgra8888> typed = self.Convert<Bgra8888>();
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));
Expand Down
13 changes: 0 additions & 13 deletions src/Beutl.Engine/Graphics/SkiaSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Beutl.Graphics;

internal static class SkiaSharpExtensions
{
[Obsolete("Use ToSKSamplingOptions")]
public static SKFilterQuality ToSKFilterQuality(this BitmapInterpolationMode interpolationMode)
{
return interpolationMode switch
Expand All @@ -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);
Expand Down
25 changes: 12 additions & 13 deletions src/Beutl.Engine/Media/TextFormatting/FormattedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ushort> glyphs = run.Glyphs;
Span<SKPoint> positions = run.Positions;
Span<ushort> glyphs = run.GetGlyphSpan();
Span<SKPoint> positions = run.GetPositionSpan();
for (int i = 0; i < result.Codepoints.Length; i++)
{
glyphs[i] = (ushort)result.Codepoints[i];
Expand All @@ -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++)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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<ushort> glyphs = run.Glyphs;
Span<SKPoint> positions = run.Positions;
Span<ushort> glyphs = run.GetGlyphSpan();
Span<SKPoint> positions = run.GetPositionSpan();
CollectionsMarshal.SetCount(_pathList, result.Codepoints.Length);
Span<SKPathGeometry> pathList = CollectionsMarshal.AsSpan(_pathList);
for (int i = 0; i < result.Codepoints.Length; i++)
Expand Down Expand Up @@ -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)
{
Expand Down
Loading