Skip to content

Commit

Permalink
fix native api loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
danwalmsley committed Jun 30, 2023
1 parent b0a51cc commit b580d3f
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.InteropServices;
using Avalonia.Compatibility;
using Avalonia.Platform.Interop;
using SkiaSharp;
using BindingFlags = System.Reflection.BindingFlags;
Expand All @@ -24,19 +25,23 @@ public SkiaMetalApi()
{
// Make sure that skia is loaded
GC.KeepAlive(new SKPaint());

var loader = AvaloniaLocator.Current.GetRequiredService<IDynamicLibraryLoader>();
#if NET6_0_OR_GREATER
var dll = NativeLibrary.Load("libSkiaSharp", typeof(SKPaint).Assembly, null);
#else
var dll = loader.LoadLibrary("libSkiaSharp");
#endif
_gr_direct_context_make_metal_with_options = (delegate* unmanaged[Stdcall] <IntPtr, IntPtr, IntPtr, IntPtr>)
loader.GetProcAddress(dll, "gr_direct_context_make_metal_with_options", false);
_gr_backendrendertarget_new_metal =
(delegate* unmanaged[Stdcall]<int, int, int, GRMtlTextureInfoNative*, IntPtr>)
loader.GetProcAddress(dll, "gr_backendrendertarget_new_metal", false);


var dll = NativeLibraryEx.Load("libSkiaSharp");

IntPtr address;

if (NativeLibraryEx.TryGetExport(dll, "gr_direct_context_make_metal_with_options", out address))
{
_gr_direct_context_make_metal_with_options =
(delegate* unmanaged[Stdcall] <IntPtr, IntPtr, IntPtr, IntPtr>)address;
}

if(NativeLibraryEx.TryGetExport(dll, "gr_backendrendertarget_new_metal", out address))
{
_gr_backendrendertarget_new_metal =
(delegate* unmanaged[Stdcall]<int, int, int, GRMtlTextureInfoNative*, IntPtr>)address;
}

_contextCtor = typeof(GRContext).GetConstructor(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
new[] { typeof(IntPtr), typeof(bool) }, null) ?? throw new MissingMemberException("GRContext.ctor(IntPtr,bool)");
Expand Down

0 comments on commit b580d3f

Please sign in to comment.