Browse Source
* Add missing AvsCurrentTargetFramework on Skia project * Implement SkiaCompat using UnsafeAccessor and cached functon pointer * Do not support SkiaSharp 3.0 on older targest, avoiding reflection completely * Make SkiaCompat static initialization more consistent * Remove function pointers completely --------- Co-authored-by: Nikita Tsukanov <keks9n@gmail.com>pull/14856/head
committed by
GitHub
9 changed files with 150 additions and 10 deletions
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia; |
|||
|
|||
internal static partial class SkiaCompat |
|||
{ |
|||
public static void SetMatrix(SKCanvas canvas, in SKMatrix matrix) |
|||
{ |
|||
if (s_isSkiaSharp3) |
|||
{ |
|||
#if NET8_0_OR_GREATER
|
|||
NewCanvasSetMatrix(canvas, matrix); |
|||
#else
|
|||
throw UnsupportedException(); |
|||
#endif
|
|||
} |
|||
else |
|||
{ |
|||
LegacyCall(canvas, matrix); |
|||
|
|||
static void LegacyCall(SKCanvas canvas, in SKMatrix matrix) |
|||
{ |
|||
canvas.SetMatrix(matrix); |
|||
} |
|||
} |
|||
} |
|||
|
|||
#if NET8_0_OR_GREATER
|
|||
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "SetMatrix")] |
|||
private static extern void NewCanvasSetMatrix(SKCanvas canvas, in SKMatrix matrix); |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia; |
|||
|
|||
internal static partial class SkiaCompat |
|||
{ |
|||
public static SKImageFilter CreateBlur(float sigmaX, float sigmaY) |
|||
{ |
|||
if (s_isSkiaSharp3) |
|||
{ |
|||
#if NET8_0_OR_GREATER
|
|||
return NewSKImageFilterCreateBlur(null, sigmaX, sigmaY); |
|||
#else
|
|||
throw UnsupportedException(); |
|||
#endif
|
|||
} |
|||
else |
|||
{ |
|||
return LegacyBlurCall(sigmaX, sigmaY); |
|||
|
|||
static SKImageFilter LegacyBlurCall(float sigmaX, float sigmaY) => |
|||
SKImageFilter.CreateBlur(sigmaX, sigmaY); |
|||
} |
|||
} |
|||
|
|||
public static SKImageFilter CreateDropShadow(float dropOffsetX, float dropOffsetY, float sigma, float f, |
|||
SKColor color) |
|||
{ |
|||
if (s_isSkiaSharp3) |
|||
{ |
|||
#if NET8_0_OR_GREATER
|
|||
return NewSKImageFilterCreateDropShadow(null!, dropOffsetX, dropOffsetY, sigma, f, color); |
|||
#else
|
|||
throw UnsupportedException(); |
|||
#endif
|
|||
} |
|||
else |
|||
{ |
|||
return LegacyDropShadowCall(dropOffsetX, dropOffsetY, sigma, f, color); |
|||
|
|||
static SKImageFilter LegacyDropShadowCall(float dropOffsetX, float dropOffsetY, float sigma, float f, SKColor color) => |
|||
SKImageFilter.CreateDropShadow(dropOffsetX, dropOffsetY, sigma, f, color); |
|||
} |
|||
} |
|||
|
|||
#if NET8_0_OR_GREATER
|
|||
// See https://github.com/dotnet/runtime/issues/90081 why we need `SKImageFilter _`
|
|||
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "CreateBlur")] |
|||
private static extern SKImageFilter NewSKImageFilterCreateBlur(SKImageFilter? _, float sigmaX, float sigmaY); |
|||
|
|||
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod, Name = "CreateDropShadow")] |
|||
private static extern SKImageFilter NewSKImageFilterCreateDropShadow(SKImageFilter? _, float dropOffsetX, |
|||
float dropOffsetY, float sigma, float f, SKColor color); |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia; |
|||
|
|||
internal static partial class SkiaCompat |
|||
{ |
|||
public static void Transform(SKPath path, in SKMatrix matrix) |
|||
{ |
|||
if (s_isSkiaSharp3) |
|||
{ |
|||
#if NET8_0_OR_GREATER
|
|||
NewPathTransform(path, matrix); |
|||
#else
|
|||
throw UnsupportedException(); |
|||
#endif
|
|||
} |
|||
else |
|||
{ |
|||
LegacyCall(path, matrix); |
|||
|
|||
static void LegacyCall(SKPath path, in SKMatrix matrix) => |
|||
path.Transform(matrix); |
|||
} |
|||
} |
|||
|
|||
#if NET8_0_OR_GREATER
|
|||
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "Transform")] |
|||
private static extern void NewPathTransform(SKPath path, in SKMatrix matrix); |
|||
#endif
|
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
using System; |
|||
using SkiaSharp; |
|||
|
|||
namespace Avalonia.Skia; |
|||
|
|||
internal static partial class SkiaCompat |
|||
{ |
|||
private static readonly bool s_isSkiaSharp3 = typeof(SKPaint).Assembly.GetName().Version?.Major == 3; |
|||
|
|||
#if !NET8_0_OR_GREATER
|
|||
private static Exception UnsupportedException() |
|||
{ |
|||
return new InvalidOperationException("Avalonia doesn't support SkiaSharp 3.0 on .NET 7 and older. Please upgrade to .NET 8."); |
|||
} |
|||
#endif
|
|||
} |
|||
Loading…
Reference in new issue