Skip to content
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
6 changes: 6 additions & 0 deletions api/Avalonia.Skia.nupkg.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Avalonia.Skia.SkiaSharpExtensions.ToSKFilterQuality(Avalonia.Media.Imaging.BitmapInterpolationMode)</Target>
<Left>baseline/netstandard2.0/Avalonia.Skia.dll</Left>
<Right>target/netstandard2.0/Avalonia.Skia.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Skia.ISkiaGpuWithPlatformGraphicsContext.TryGetGrContext</Target>
Expand Down
7 changes: 1 addition & 6 deletions build/SkiaSharp.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition="'$(AvsIncludeSkiaSharp3)' != 'true'">
<PackageReference Include="SkiaSharp" Version="2.88.9" />
<PackageReference Condition="'$(IncludeLinuxSkia)' == 'true'" Include="SkiaSharp.NativeAssets.Linux" Version="2.88.9" />
<PackageReference Condition="'$(IncludeWasmSkia)' == 'true'" Include="SkiaSharp.NativeAssets.WebAssembly" Version="2.88.9" />
</ItemGroup>
<ItemGroup Condition="'$(AvsIncludeSkiaSharp3)' == 'true'">
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="3.119.0" />
<PackageReference Condition="'$(IncludeLinuxSkia)' == 'true'" Include="SkiaSharp.NativeAssets.Linux" Version="3.119.0" />
<PackageReference Condition="'$(IncludeWasmSkia)' == 'true'" Include="SkiaSharp.NativeAssets.WebAssembly" Version="3.119.0" />
Expand Down
2 changes: 2 additions & 0 deletions src/Skia/Avalonia.Skia/Avalonia.Skia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<IncludeLinuxSkia>true</IncludeLinuxSkia>
<IncludeWasmSkia>true</IncludeWasmSkia>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- No obsolete code usage -->
<WarningsAsErrors>$(WarningsAsErrors);CS0618</WarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\NoiseAsset_256X256_PNG.png" />
Expand Down
13 changes: 8 additions & 5 deletions src/Skia/Avalonia.Skia/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ public void DrawBitmap(IBitmapImpl source, double opacity, Rect sourceRect, Rect
var d = destRect.ToSKRect();

var paint = SKPaintCache.Shared.Get();
var samplingOptions = RenderOptions.BitmapInterpolationMode.ToSKSamplingOptions();

paint.Color = new SKColor(255, 255, 255, (byte)(255 * opacity * _currentOpacity));
paint.FilterQuality = RenderOptions.BitmapInterpolationMode.ToSKFilterQuality();
paint.BlendMode = RenderOptions.BitmapBlendingMode.ToSKBlendMode();

drawableImage.Draw(this, s, d, paint);
drawableImage.Draw(this, s, d, samplingOptions, paint);
SKPaintCache.Shared.ReturnReset(paint);
}

Expand Down Expand Up @@ -844,7 +844,9 @@ public void PopOpacityMask()
/// <inheritdoc />
public Matrix Transform
{
get { return _currentTransform ??= Canvas.TotalMatrix.ToAvaloniaMatrix(); }
// There is a Canvas.TotalMatrix (non 4x4 overload), but internally it still uses 4x4 matrix.
// We want to avoid SKMatrix4x4 -> SKMatrix -> Matrix conversion by directly going SKMatrix4x4 -> Matrix.
get { return _currentTransform ??= Canvas.TotalMatrix44.ToAvaloniaMatrix(); }
set
{
CheckLease();
Expand All @@ -860,7 +862,9 @@ public Matrix Transform
transform *= _postTransform.Value;
}

Canvas.SetMatrix(transform.ToSKMatrix());
// Canvas.SetMatrix internally uses 4x4 matrix, even with SKMatrix(3x3) overload.
// We want to avoid Matrix -> SKMatrix -> SKMatrix4x4 conversion by directly going Matrix -> SKMatrix4x4.
Canvas.SetMatrix(transform.ToSKMatrix44());
}
}

Expand Down Expand Up @@ -1257,7 +1261,6 @@ private void ConfigureSceneBrushContentWithPicture(ref PaintWrapper paintWrapper
using(var shader = tile.ToShader(tileX, tileY, shaderTransform.ToSKMatrix(),
new SKRect(0, 0, tile.CullRect.Width, tile.CullRect.Height)))
{
paintWrapper.Paint.FilterQuality = SKFilterQuality.None;
paintWrapper.Paint.Shader = shader;
}
}
Expand Down
108 changes: 0 additions & 108 deletions src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalApi.cs

This file was deleted.

15 changes: 8 additions & 7 deletions src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalGpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ namespace Avalonia.Skia.Metal;

internal class SkiaMetalGpu : ISkiaGpu, ISkiaGpuWithPlatformGraphicsContext
{
private SkiaMetalApi _api = new();
private GRContext? _context;
private readonly IMetalDevice _device;

public SkiaMetalGpu(IMetalDevice device, long? maxResourceBytes)
{
_context = _api.CreateContext(device.Device, device.CommandQueue,
new GRContextOptions() { AvoidStencilBuffers = true });
_context = GRContext.CreateMetal(
new GRMtlBackendContext { DeviceHandle = device.Device, QueueHandle = device.CommandQueue, },
new GRContextOptions { AvoidStencilBuffers = true })
?? throw new InvalidOperationException("Unable to create GRContext from Metal device.");
_device = device;
if (maxResourceBytes.HasValue)
_context.SetResourceCacheLimit(maxResourceBytes.Value);
Expand All @@ -35,7 +36,7 @@ public void Dispose()
public IPlatformGraphicsContext? PlatformGraphicsContext => _device;

public IScopedResource<GRContext> TryGetGrContext() =>
ScopedResource<GRContext>.Create(_context ?? throw new ObjectDisposedException(nameof(SkiaMetalApi)),
ScopedResource<GRContext>.Create(_context ?? throw new ObjectDisposedException(nameof(SkiaMetalGpu)),
EnsureCurrent().Dispose);

public ISkiaGpuRenderTarget? TryCreateRenderTarget(IEnumerable<object> surfaces)
Expand Down Expand Up @@ -72,13 +73,13 @@ public void Dispose()
public ISkiaGpuRenderSession BeginRenderingSession()
{
var session = (_target ?? throw new ObjectDisposedException(nameof(SkiaMetalRenderTarget))).BeginRendering();
var backendTarget = _gpu._api.CreateBackendRenderTarget(session.Size.Width, session.Size.Height,
1, session.Texture);
var backendTarget = new GRBackendRenderTarget(session.Size.Width, session.Size.Height,
new GRMtlTextureInfo(session.Texture));

var surface = SKSurface.Create(_gpu._context!, backendTarget,
session.IsYFlipped ? GRSurfaceOrigin.BottomLeft : GRSurfaceOrigin.TopLeft,
SKColorType.Bgra8888);

return new SkiaMetalRenderSession(_gpu, surface, session);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public IBitmapImpl SnapshotWithSemaphores(IPlatformRenderInterfaceImportedSemaph
Size = info.MemorySize
}
};
using var renderTarget = new GRBackendRenderTarget(_properties.Width, _properties.Height, 1, imageInfo);
using var renderTarget = new GRBackendRenderTarget(_properties.Width, _properties.Height, imageInfo);
using var surface = SKSurface.Create(_gpu.GrContext, renderTarget,
_properties.TopLeftOrigin ? GRSurfaceOrigin.TopLeft : GRSurfaceOrigin.BottomLeft,
_properties.Format == PlatformGraphicsExternalImageFormat.R8G8B8A8UNorm
Expand Down Expand Up @@ -121,4 +121,4 @@ public CompositionGpuImportedImageSynchronizationCapabilities

public IReadOnlyList<string> SupportedImageHandleTypes => _feature.SupportedImageHandleTypes;
public IReadOnlyList<string> SupportedSemaphoreTypes => _feature.SupportedSemaphoreTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ISkiaGpuRenderSession BeginRenderingSession()
Size = sessionImageInfo.MemorySize
}
};
using var renderTarget = new GRBackendRenderTarget(size.Width, size.Height, 1, imageInfo);
using var renderTarget = new GRBackendRenderTarget(size.Width, size.Height, imageInfo);
var surface = SKSurface.Create(_gpu.GrContext, renderTarget,
session.IsYFlipped ? GRSurfaceOrigin.TopLeft : GRSurfaceOrigin.BottomLeft,
session.IsRgba ? SKColorType.Rgba8888 : SKColorType.Bgra8888, SKColorSpace.CreateSrgb());
Expand Down
3 changes: 2 additions & 1 deletion src/Skia/Avalonia.Skia/IDrawableBitmapImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal interface IDrawableBitmapImpl : IBitmapImpl
/// <param name="context">Drawing context.</param>
/// <param name="sourceRect">Source rect.</param>
/// <param name="destRect">Destination rect.</param>
/// <param name="samplingOptions">Interpolation sampling options.</param>
/// <param name="paint">Paint to use.</param>
void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKPaint paint);
void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKSamplingOptions samplingOptions, SKPaint paint);
}
}
10 changes: 5 additions & 5 deletions src/Skia/Avalonia.Skia/ImmutableBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ImmutableBitmap(ImmutableBitmap src, PixelSize destinationSize, BitmapInt
{
SKImageInfo info = new SKImageInfo(destinationSize.Width, destinationSize.Height, SKColorType.Bgra8888);
_bitmap = new SKBitmap(info);
src._image.ScalePixels(_bitmap.PeekPixels(), interpolationMode.ToSKFilterQuality());
src._image.ScalePixels(_bitmap.PeekPixels(), interpolationMode.ToSKSamplingOptions());
_bitmap.SetImmutable();
_image = SKImage.FromBitmap(_bitmap);

Expand Down Expand Up @@ -95,11 +95,11 @@ public ImmutableBitmap(Stream stream, int decodeSize, bool horizontal, BitmapInt

if (_bitmap.Width != desired.Width || _bitmap.Height != desired.Height)
{
var scaledBmp = _bitmap.Resize(desired, interpolationMode.ToSKFilterQuality());
var scaledBmp = _bitmap.Resize(desired, interpolationMode.ToSKSamplingOptions());
_bitmap.Dispose();
_bitmap = scaledBmp;
}

_bitmap.SetImmutable();

_image = SKImage.FromBitmap(_bitmap);
Expand Down Expand Up @@ -171,9 +171,9 @@ public void Save(Stream stream, int? quality = null)
}

/// <inheritdoc />
public void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKPaint paint)
public void Draw(DrawingContextImpl context, SKRect sourceRect, SKRect destRect, SKSamplingOptions samplingOptions, SKPaint paint)
{
context.Canvas.DrawImage(_image, sourceRect, destRect, paint);
context.Canvas.DrawImage(_image, sourceRect, destRect, samplingOptions, paint);
}

public PixelFormat? Format => _bitmap?.ColorType.ToAvalonia();
Expand Down
56 changes: 42 additions & 14 deletions src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ namespace Avalonia.Skia
{
public static class SkiaSharpExtensions
{
public static SKFilterQuality ToSKFilterQuality(this BitmapInterpolationMode interpolationMode)
public static SKSamplingOptions ToSKSamplingOptions(this BitmapInterpolationMode interpolationMode)
{
switch (interpolationMode)
return interpolationMode switch
{
case BitmapInterpolationMode.Unspecified:
case BitmapInterpolationMode.LowQuality:
return SKFilterQuality.Low;
case BitmapInterpolationMode.MediumQuality:
return SKFilterQuality.Medium;
case BitmapInterpolationMode.HighQuality:
return SKFilterQuality.High;
case BitmapInterpolationMode.None:
return SKFilterQuality.None;
default:
throw new ArgumentOutOfRangeException(nameof(interpolationMode), interpolationMode, null);
}
BitmapInterpolationMode.None =>
new SKSamplingOptions(SKFilterMode.Nearest, SKMipmapMode.None),
BitmapInterpolationMode.Unspecified or 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)
};
Comment on lines +12 to +25

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public static SKBlendMode ToSKBlendMode(this BitmapBlendingMode blendingMode)
Expand Down Expand Up @@ -146,11 +144,41 @@ public static SKMatrix ToSKMatrix(this Matrix m)
return sm;
}

public static SKMatrix44 ToSKMatrix44(this Matrix m)
{
var sm = new SKMatrix44
{
M00 = (float)m.M11,
M01 = (float)m.M12,
M02 = 0,
M03 = (float)m.M13,
M10 = (float)m.M21,
M11 = (float)m.M22,
M12 = 0,
M13 = (float)m.M23,
M20 = 0,
M21 = 0,
M22 = 1,
M23 = 0,
M30 = (float)m.M31,
M31 = (float)m.M32,
M32 = 0,
M33 = (float)m.M33
};

return sm;
}

internal static Matrix ToAvaloniaMatrix(this SKMatrix m) => new(
m.ScaleX, m.SkewY, m.Persp0,
m.SkewX, m.ScaleY, m.Persp1,
m.TransX, m.TransY, m.Persp2);

internal static Matrix ToAvaloniaMatrix(this SKMatrix44 m) => new(
m.M00, m.M01, m.M03,
m.M10, m.M11, m.M13,
m.M30, m.M31, m.M33);

public static SKColor ToSKColor(this Color c)
{
return new SKColor(c.R, c.G, c.B, c.A);
Expand Down
Loading
Loading