[QUESTION] What is the point of using USE_DELEGATES in SkiaApi.cs? #1607
Replies: 5 comments
-
The reason for this is mostly for working with the logic to load the native library. In the case of Android/iOS, there is only ever one libSkiaSharp to load, so we can just use the default lookup for native libraries. On Windows, there may be several libSkiaSharp libraries in memory, so we have to manually look up each function pointer. One use case was for VS. Multiple extensions can be installed, each with a different version of SkiaSharp. To avoid crashes, we have to explicitly load the functions for the specific binaries in the extension package. If we used normal p/invokes, then it would pick the one that was already loaded, and might crash. SkiaSharp also only is really supporting net462 as we needed some features that came there. If you need to support NETFX, you will have to update to 4.6.2. |
Beta Was this translation helpful? Give feedback.
-
@mattleibow Thanks for your answer. But I still have some questions:
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi @mattleibow, I want to connect to the discussion because I see some technical troubles with using SkiaSharp under platform with different architectures. The library is using PInvoke as simple and fast method to call native libraries. However, there is set of problem with it:
On my opinion, using PInvoke works correctly only for system wide libraries such as |
Beta Was this translation helpful? Give feedback.
-
That is what I am saying with the reason for the delegates AND the pinvokes. On Android/iOS/UWP, we use pinvokes because we can ever only have a single libSkiaSharp. Each architecture is placed in special location, and then the OS knows which one to load. If there are duplicates, then there will be a compile time exception about duplicate symbols. In the middle we have .NET Core which also uses the RIDs to determine which native library to load. That is why if you create a netcoreapp project now, it works across all architectures. The .NET Core runtime picks the correct architecture. If you want to support things like musl, all you have to do is just add it into the correct folder in a nuget package and let the runtime do the work: https://github.com/mono/SkiaSharp/blob/master/nuget/SkiaSharp.NativeAssets.Linux.nuspec#L50 For things like net4xx, we use delegates because there is no packaging system and we could potentially load an arbitrary number of libSkiaSharp - as is the case with several VS extensions. We have our own folder structure that the internal library loader looks at. This way, you can have all the libSkiaSharp in folders and the managed library will load: https://github.com/mono/SkiaSharp/blob/master/binding/Binding.Shared/LibraryLoader.cs#L36 With the pinvoke APIs, we can use the same API for all the architectures and OS because we make sure to use the same types in both the managed and native worlds. This means that all the things, such as the size of pointers, also change. |
Beta Was this translation helpful? Give feedback.
-
I found the code to dynamically load the native lib 'libSkiaSharp.*'.
https://github.com/mono/SkiaSharp/blob/master/binding/Binding/SkiaApi.cs
This is required for products are shipped for multiple platforms. But at the same time, dynamic loading is used only for the full .NET Framework, and in fact only starting from version 4.6.2.
SkiaSharp/binding/SkiaSharp/SkiaSharp.csproj
Lines 11 to 13 in e8869d1
SkiaSharp/binding/SkiaSharp/SkiaSharp.csproj
Line 3 in e8869d1
At the moment I have a project that is built for .NET Framework 4.6.1 and is shipped for Windows (both x86 and x64). As a result, in my case, the managed SkiaSharp.dll compiled for netstandard2.0 is referenced (with static loading of the native libSkiaSharp.dll, because USE_DELEGATES = false).
Could you please explain me what is the point of using USE_DELEGATES and why is it only applied to the full .NET Framework?
How is 'libSkiaSharp.*' supposed to load for solutions shipped for multiple platforms on non-Windows OS?
Beta Was this translation helpful? Give feedback.
All reactions