Skip to content

Commit

Permalink
Use Unsafe.As for better perf (#2780)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Mar 4, 2024
1 parent ca295d0 commit 7ebc1f0
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 119 deletions.
4 changes: 4 additions & 0 deletions binding/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>

<ItemGroup Condition=" ('$(TargetFramework)' == 'netstandard2.1') and (!$(MSBuildProjectName.Contains('.NativeAssets.'))) ">
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.3" />
</ItemGroup>

<PropertyGroup Condition="'$(PackagingGroup)' == 'SkiaSharp'">
<PackageDescription Condition="'$(PackageDescription)' == ''">SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library.
It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.</PackageDescription>
Expand Down
65 changes: 29 additions & 36 deletions binding/SkiaSharp/SKMatrix44.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Numerics;
using System.Runtime.CompilerServices;

namespace SkiaSharp
{
Expand Down Expand Up @@ -348,46 +349,46 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>

public SKMatrix Matrix =>
new SKMatrix (
m00, m01, m03,
m10, m11, m13,
m30, m31, m33);
m00, m10, m30,
m01, m11, m31,
m03, m13, m33);

public float this[int row, int column] {
get => column switch {
0 => row switch {
get => row switch {
0 => column switch {
0 => m00,
1 => m01,
2 => m02,
3 => m03,
_ => throw new ArgumentOutOfRangeException (nameof (row))
_ => throw new ArgumentOutOfRangeException (nameof (column))
},
1 => row switch {
1 => column switch {
0 => m10,
1 => m11,
2 => m12,
3 => m13,
_ => throw new ArgumentOutOfRangeException (nameof (row))
_ => throw new ArgumentOutOfRangeException (nameof (column))
},
2 => row switch {
2 => column switch {
0 => m20,
1 => m21,
2 => m22,
3 => m23,
_ => throw new ArgumentOutOfRangeException (nameof (row))
_ => throw new ArgumentOutOfRangeException (nameof (column))
},
3 => row switch {
3 => column switch {
0 => m30,
1 => m31,
2 => m32,
3 => m33,
_ => throw new ArgumentOutOfRangeException (nameof (row))
_ => throw new ArgumentOutOfRangeException (nameof (column))
},
_ => throw new ArgumentOutOfRangeException (nameof (column))
_ => throw new ArgumentOutOfRangeException (nameof (row))
};
set {
switch (column) {
switch (row) {
case 0:
switch (row) {
switch (column) {
case 0:
m00 = value;
break;
Expand All @@ -401,11 +402,11 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>
m03 = value;
break;
default:
throw new ArgumentOutOfRangeException (nameof (row));
throw new ArgumentOutOfRangeException (nameof (column));
};
break;
case 1:
switch (row) {
switch (column) {
case 0:
m10 = value;
break;
Expand All @@ -419,11 +420,11 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>
m13 = value;
break;
default:
throw new ArgumentOutOfRangeException (nameof (row));
throw new ArgumentOutOfRangeException (nameof (column));
};
break;
case 2:
switch (row) {
switch (column) {
case 0:
m20 = value;
break;
Expand All @@ -437,11 +438,11 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>
m23 = value;
break;
default:
throw new ArgumentOutOfRangeException (nameof (row));
throw new ArgumentOutOfRangeException (nameof (column));
};
break;
case 3:
switch (row) {
switch (column) {
case 0:
m30 = value;
break;
Expand All @@ -455,11 +456,11 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>
m33 = value;
break;
default:
throw new ArgumentOutOfRangeException (nameof (row));
throw new ArgumentOutOfRangeException (nameof (column));
};
break;
default:
throw new ArgumentOutOfRangeException (nameof (column));
throw new ArgumentOutOfRangeException (nameof (row));
};
}
}
Expand All @@ -468,23 +469,15 @@ public static SKMatrix44 Multiply (SKMatrix44 value1, float value2) =>

public static implicit operator SKMatrix44 (SKMatrix matrix) =>
new SKMatrix44 (
matrix.ScaleX, matrix.SkewX, 0, matrix.TransX,
matrix.SkewY, matrix.ScaleY, 0, matrix.TransY,
matrix.ScaleX, matrix.SkewY, 0, matrix.Persp0,
matrix.SkewX, matrix.ScaleY, 0, matrix.Persp1,
0, 0, 1, 0,
matrix.Persp0, matrix.Persp1, 0, matrix.Persp2);
matrix.TransX, matrix.TransY, 0, matrix.Persp2);

public static implicit operator Matrix4x4 (SKMatrix44 matrix) =>
new Matrix4x4 (
matrix.m00, matrix.m10, matrix.m20, matrix.m30,
matrix.m01, matrix.m11, matrix.m21, matrix.m31,
matrix.m02, matrix.m12, matrix.m22, matrix.m32,
matrix.m03, matrix.m13, matrix.m23, matrix.m33);
Unsafe.As<SKMatrix44, Matrix4x4> (ref matrix);

public static implicit operator SKMatrix44 (Matrix4x4 matrix) =>
new SKMatrix44 (
matrix.M11, matrix.M21, matrix.M31, matrix.M41,
matrix.M12, matrix.M22, matrix.M32, matrix.M42,
matrix.M13, matrix.M23, matrix.M33, matrix.M43,
matrix.M14, matrix.M24, matrix.M34, matrix.M44);
Unsafe.As<Matrix4x4, SKMatrix44> (ref matrix);
}
}
2 changes: 1 addition & 1 deletion externals/skia
Loading

0 comments on commit 7ebc1f0

Please sign in to comment.