Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function pointers and LibraryImport #2917

Merged
merged 30 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9313a30
Update generator to emit function pointers
maxkatz6 Jul 3, 2024
1977a7c
Regenerate interop files
maxkatz6 Jul 3, 2024
a35eaa4
Enable USE_LIBRARY_IMPORT in HarfBuzzSharp
maxkatz6 Jul 3, 2024
2b9c477
Enable USE_LIBRARY_IMPORT on SkiaSharp
maxkatz6 Jul 3, 2024
2af4504
Set DisableRuntimeMarshalling on HarfBuzzSharp
maxkatz6 Jul 3, 2024
d7223e9
Replace remaining DllImports with LibraryImport on SkiaSharp
maxkatz6 Jul 3, 2024
c83cf08
Set DisableRuntimeMarshalling on SkiaSharp as well
maxkatz6 Jul 3, 2024
fdd2782
Fix missed proxy definition
maxkatz6 Jul 3, 2024
f2736d0
Regenerate skia api with a correct submodule version
maxkatz6 Jul 3, 2024
ffb5057
Collections literals are not supported on the CI .NET SDK
maxkatz6 Jul 3, 2024
9c7c6b0
An attempt to fix Tizen build
maxkatz6 Jul 3, 2024
d5035b3
Forgot about partial
maxkatz6 Jul 3, 2024
38ee2ca
Set UnmanagedType.LPStr on evas_gl_proc_address_get instead
maxkatz6 Jul 3, 2024
a054db9
Set USE_LIBRARY_IMPORT on remaining projects too
maxkatz6 Jul 3, 2024
78c3880
Merge branch 'main' into function-pointers-and-library-import
maxkatz6 Jul 4, 2024
112af64
Merge branch 'main' into function-pointers-and-library-import
maxkatz6 Jul 17, 2024
26b4961
Update generator tool to generate DelegateProxy as well
maxkatz6 Jul 19, 2024
c13089b
Regenerate HarfBuzz and SkiaSharp with new DelegateProxy source gen
maxkatz6 Jul 19, 2024
1810c0a
Regenerate other projects as well
maxkatz6 Jul 19, 2024
b2d998b
Merge branch 'main' into function-pointers-and-library-import
maxkatz6 Jul 19, 2024
ffd76e2
Merge branch 'main' into function-pointers-and-library-import
maxkatz6 Jul 26, 2024
ac124fe
Add `protected internal` to test classes too, since this project has …
maxkatz6 Aug 15, 2024
544a3f7
Disable DelegateTypesAreValid and DelegateTypesHaveAttributes tests o…
maxkatz6 Aug 15, 2024
53e445e
Reduce warnings noise
maxkatz6 Aug 15, 2024
7c13e28
Merge branch 'main' into function-pointers-and-library-import
mattleibow Aug 17, 2024
65b6a54
Merge branch 'main' into function-pointers-and-library-import
mattleibow Aug 20, 2024
ca37c82
Update binding/SkiaSharp/GRGlInterface.cs
maxkatz6 Aug 20, 2024
03a3b40
Add missing USE_LIBRARY_IMPORT defines
maxkatz6 Aug 20, 2024
b36ad1a
Update binding/SkiaSharp/GRGlInterface.cs
mattleibow Aug 21, 2024
f62498a
Also needs USE_LIBRARY_IMPORT
mattleibow Aug 21, 2024
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
14 changes: 0 additions & 14 deletions binding/Binding.Shared/DelegateProxies.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,6 @@ internal static partial class DelegateProxies
{
// normal delegates

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static T Create<T> (object managedDel, T nativeDel, out GCHandle gch, out IntPtr contextPtr)
{
if (managedDel == null) {
gch = default (GCHandle);
contextPtr = IntPtr.Zero;
return default (T);
}

gch = GCHandle.Alloc (managedDel);
contextPtr = GCHandle.ToIntPtr (gch);
return nativeDel;
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static void Create (object managedDel, out GCHandle gch, out IntPtr contextPtr)
{
Expand Down
4 changes: 2 additions & 2 deletions binding/HarfBuzzSharp/Blob.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable disable

using System;
using System.ComponentModel;
using System.IO;

namespace HarfBuzzSharp
Expand Down Expand Up @@ -84,7 +83,8 @@ public static unsafe Blob FromStream (Stream stream)

private static IntPtr Create (IntPtr data, int length, MemoryMode mode, ReleaseDelegate releaseProc)
{
var proxy = DelegateProxies.Create (releaseProc, DelegateProxies.ReleaseDelegateProxy, out _, out var ctx);
DelegateProxies.Create (releaseProc, out _, out var ctx);
var proxy = releaseProc != null ? DelegateProxies.DestroyProxy : null;
return HarfBuzzApi.hb_blob_create ((void*)data, (uint)length, mode, (void*)ctx, proxy);
}

Expand Down
32 changes: 12 additions & 20 deletions binding/HarfBuzzSharp/DelegateProxies.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#nullable disable

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// ReSharper disable PartialMethodParameterNameMismatch

namespace HarfBuzzSharp
{
Expand All @@ -11,15 +13,7 @@ namespace HarfBuzzSharp

internal static unsafe partial class DelegateProxies
{
// references to the proxy implementations
public static readonly DestroyProxyDelegate ReleaseDelegateProxy = ReleaseDelegateProxyImplementation;
public static readonly DestroyProxyDelegate ReleaseDelegateProxyForMulti = ReleaseDelegateProxyImplementationForMulti;
public static readonly ReferenceTableProxyDelegate GetTableDelegateProxy = GetTableDelegateProxyImplementation;

// internal proxy implementations

[MonoPInvokeCallback (typeof (DestroyProxyDelegate))]
private static void ReleaseDelegateProxyImplementation (void* context)
private static partial void DestroyProxyImplementation (void* context)
{
var del = Get<ReleaseDelegate> ((IntPtr)context, out var gch);
try {
Expand All @@ -29,16 +23,7 @@ private static void ReleaseDelegateProxyImplementation (void* context)
}
}

[MonoPInvokeCallback (typeof (ReferenceTableProxyDelegate))]
private static IntPtr GetTableDelegateProxyImplementation (IntPtr face, uint tag, void* context)
{
GetMultiUserData<GetTableDelegate, Face> ((IntPtr)context, out var getTable, out var userData, out _);
var blob = getTable.Invoke (userData, tag);
return blob?.Handle ?? IntPtr.Zero;
}

[MonoPInvokeCallback (typeof (DestroyProxyDelegate))]
private static void ReleaseDelegateProxyImplementationForMulti (void* context)
private static partial void DestroyProxyImplementationForMulti (void* context)
{
var del = GetMulti<ReleaseDelegate> ((IntPtr)context, out var gch);
try {
Expand All @@ -47,5 +32,12 @@ private static void ReleaseDelegateProxyImplementationForMulti (void* context)
gch.Free ();
}
}

private static partial IntPtr ReferenceTableProxyImplementation (IntPtr face, uint tag, void* context)
{
GetMultiUserData<GetTableDelegate, Face> ((IntPtr)context, out var getTable, out var userData, out _);
var blob = getTable.Invoke (userData, tag);
return blob?.Handle ?? IntPtr.Zero;
}
}
}
51 changes: 13 additions & 38 deletions binding/HarfBuzzSharp/DelegateProxies.font.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable disable
// ReSharper disable PartialMethodParameterNameMismatch

using System;

Expand Down Expand Up @@ -30,24 +31,9 @@ namespace HarfBuzzSharp

internal static unsafe partial class DelegateProxies
{
// references to the proxy implementations
public static readonly FontGetFontExtentsProxyDelegate FontExtentsProxy = FontExtentsProxyImplementation;
public static readonly FontGetNominalGlyphProxyDelegate NominalGlyphProxy = NominalGlyphProxyImplementation;
public static readonly FontGetVariationGlyphProxyDelegate VariationGlyphProxy = VariationGlyphProxyImplementation;
public static readonly FontGetNominalGlyphsProxyDelegate NominalGlyphsProxy = NominalGlyphsProxyImplementation;
public static readonly FontGetGlyphAdvanceProxyDelegate GlyphAdvanceProxy = GlyphAdvanceProxyImplementation;
public static readonly FontGetGlyphAdvancesProxyDelegate GlyphAdvancesProxy = GlyphAdvancesProxyImplementation;
public static readonly FontGetGlyphOriginProxyDelegate GlyphOriginProxy = GlyphOriginProxyImplementation;
public static readonly FontGetGlyphKerningProxyDelegate GlyphKerningProxy = GlyphKerningProxyImplementation;
public static readonly FontGetGlyphExtentsProxyDelegate GlyphExtentsProxy = GlyphExtentsProxyImplementation;
public static readonly FontGetGlyphContourPointProxyDelegate GlyphContourPointProxy = GlyphContourPointProxyImplementation;
public static readonly FontGetGlyphNameProxyDelegate GlyphNameProxy = GlyphNameProxyImplementation;
public static readonly FontGetGlyphFromNameProxyDelegate GlyphFromNameProxy = GlyphFromNameProxyImplementation;

// internal proxy implementations

[MonoPInvokeCallback (typeof (FontGetFontExtentsProxyDelegate))]
private static bool FontExtentsProxyImplementation (IntPtr font, void* fontData, FontExtents* extents, void* context)
private static partial bool FontGetFontExtentsProxyImplementation (IntPtr font, void* fontData, FontExtents* extents, void* context)
{
var del = GetMulti<FontExtentsDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -57,8 +43,7 @@ private static bool FontExtentsProxyImplementation (IntPtr font, void* fontData,
return result;
}

[MonoPInvokeCallback (typeof (FontGetNominalGlyphProxyDelegate))]
private static bool NominalGlyphProxyImplementation (IntPtr font, void* fontData, uint unicode, uint* glyph, void* context)
private static partial bool FontGetNominalGlyphProxyImplementation (IntPtr font, void* fontData, uint unicode, uint* glyph, void* context)
{
var del = GetMulti<NominalGlyphDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -68,8 +53,7 @@ private static bool NominalGlyphProxyImplementation (IntPtr font, void* fontData
return result;
}

[MonoPInvokeCallback (typeof (FontGetNominalGlyphsProxyDelegate))]
private static uint NominalGlyphsProxyImplementation (IntPtr font, void* fontData, uint count, uint* firstUnicode, uint unicodeStride, uint* firstGlyph, uint glyphStride, void* context)
private static partial uint FontGetNominalGlyphsProxyImplementation (IntPtr font, void* fontData, uint count, uint* firstUnicode, uint unicodeStride, uint* firstGlyph, uint glyphStride, void* context)
{
var del = GetMulti<NominalGlyphsDelegate> ((IntPtr)context, out _);
var unicodes = new ReadOnlySpan<uint> (firstUnicode, (int)count);
Expand All @@ -78,8 +62,7 @@ private static uint NominalGlyphsProxyImplementation (IntPtr font, void* fontDat
return del.Invoke (userData.Font, userData.FontData, count, unicodes, glyphs);
}

[MonoPInvokeCallback (typeof (FontGetVariationGlyphProxyDelegate))]
private static bool VariationGlyphProxyImplementation (IntPtr font, void* fontData, uint unicode, uint variationSelector, uint* glyph, void* context)
private static partial bool FontGetVariationGlyphProxyImplementation (IntPtr font, void* fontData, uint unicode, uint variationSelector, uint* glyph, void* context)
{
var del = GetMulti<VariationGlyphDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -89,16 +72,14 @@ private static bool VariationGlyphProxyImplementation (IntPtr font, void* fontDa
return result;
}

[MonoPInvokeCallback (typeof (FontGetGlyphAdvanceProxyDelegate))]
private static int GlyphAdvanceProxyImplementation (IntPtr font, void* fontData, uint glyph, void* context)
private static partial int FontGetGlyphAdvanceProxyImplementation (IntPtr font, void* fontData, uint glyph, void* context)
{
var del = GetMulti<GlyphAdvanceDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
return del.Invoke (userData.Font, userData.FontData, glyph);
}

[MonoPInvokeCallback (typeof (FontGetGlyphAdvancesProxyDelegate))]
private static void GlyphAdvancesProxyImplementation (IntPtr font, void* fontData, uint count, uint* firstGlyph, uint glyphStride, int* firstAdvance, uint advanceStride, void* context)
private static partial void FontGetGlyphAdvancesProxyImplementation (IntPtr font, void* fontData, uint count, uint* firstGlyph, uint glyphStride, int* firstAdvance, uint advanceStride, void* context)
{
var del = GetMulti<GlyphAdvancesDelegate> ((IntPtr)context, out _);
var glyphs = new ReadOnlySpan<uint> (firstGlyph, (int)count);
Expand All @@ -107,8 +88,7 @@ private static void GlyphAdvancesProxyImplementation (IntPtr font, void* fontDat
del.Invoke (userData.Font, userData.FontData, count, glyphs, advances);
}

[MonoPInvokeCallback (typeof (FontGetGlyphOriginProxyDelegate))]
private static bool GlyphOriginProxyImplementation (IntPtr font, void* fontData, uint glyph, int* x, int* y, void* context)
private static partial bool FontGetGlyphOriginProxyImplementation (IntPtr font, void* fontData, uint glyph, int* x, int* y, void* context)
{
var del = GetMulti<GlyphOriginDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -120,16 +100,14 @@ private static bool GlyphOriginProxyImplementation (IntPtr font, void* fontData,
return result;
}

[MonoPInvokeCallback (typeof (FontGetGlyphKerningProxyDelegate))]
private static int GlyphKerningProxyImplementation (IntPtr font, void* fontData, uint firstGlyph, uint secondGlyph, void* context)
private static partial int FontGetGlyphKerningProxyImplementation (IntPtr font, void* fontData, uint firstGlyph, uint secondGlyph, void* context)
{
var del = GetMulti<GlyphKerningDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
return del.Invoke (userData.Font, userData.FontData, firstGlyph, secondGlyph);
}

[MonoPInvokeCallback (typeof (FontGetGlyphExtentsProxyDelegate))]
private static bool GlyphExtentsProxyImplementation (IntPtr font, void* fontData, uint glyph, GlyphExtents* extents, void* context)
private static partial bool FontGetGlyphExtentsProxyImplementation (IntPtr font, void* fontData, uint glyph, GlyphExtents* extents, void* context)
{
var del = GetMulti<GlyphExtentsDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -139,8 +117,7 @@ private static bool GlyphExtentsProxyImplementation (IntPtr font, void* fontData
return result;
}

[MonoPInvokeCallback (typeof (FontGetGlyphContourPointProxyDelegate))]
private static bool GlyphContourPointProxyImplementation (IntPtr font, void* fontData, uint glyph, uint pointIndex, int* x, int* y, void* context)
private static partial bool FontGetGlyphContourPointProxyImplementation (IntPtr font, void* fontData, uint glyph, uint pointIndex, int* x, int* y, void* context)
{
var del = GetMulti<GlyphContourPointDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -152,8 +129,7 @@ private static bool GlyphContourPointProxyImplementation (IntPtr font, void* fon
return result;
}

[MonoPInvokeCallback (typeof (FontGetGlyphNameProxyDelegate))]
private static bool GlyphNameProxyImplementation (IntPtr font, void* fontData, uint glyph, void* nameBuffer, uint size, void* context)
private static partial bool FontGetGlyphNameProxyImplementation (IntPtr font, void* fontData, uint glyph, void* nameBuffer, uint size, void* context)
{
var del = GetMulti<GlyphNameDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand All @@ -166,8 +142,7 @@ private static bool GlyphNameProxyImplementation (IntPtr font, void* fontData, u
return result;
}

[MonoPInvokeCallback (typeof (FontGetGlyphFromNameProxyDelegate))]
private static bool GlyphFromNameProxyImplementation (IntPtr font, void* fontData, void* name, int len, uint* glyph, void* context)
private static partial bool FontGetGlyphFromNameProxyImplementation (IntPtr font, void* fontData, void* name, int len, uint* glyph, void* context)
{
var del = GetMulti<GlyphFromNameDelegate> ((IntPtr)context, out _);
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
Expand Down
28 changes: 9 additions & 19 deletions binding/HarfBuzzSharp/DelegateProxies.unicode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable disable
// ReSharper disable PartialMethodParameterNameMismatch

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace HarfBuzzSharp
{
Expand All @@ -18,43 +21,31 @@ namespace HarfBuzzSharp

internal static unsafe partial class DelegateProxies
{
public static readonly UnicodeCombiningClassProxyDelegate CombiningClassProxy = CombiningClassProxyImplementation;
public static readonly UnicodeGeneralCategoryProxyDelegate GeneralCategoryProxy = GeneralCategoryProxyImplementation;
public static readonly UnicodeMirroringProxyDelegate MirroringProxy = MirroringProxyImplementation;
public static readonly UnicodeScriptProxyDelegate ScriptProxy = ScriptProxyImplementation;
public static readonly UnicodeComposeProxyDelegate ComposeProxy = ComposeProxyImplementation;
public static readonly UnicodeDecomposeProxyDelegate DecomposeProxy = DecomposeProxyImplementation;

[MonoPInvokeCallback (typeof (UnicodeCombiningClassProxyDelegate))]
private static int CombiningClassProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
private static partial int UnicodeCombiningClassProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
{
GetMultiUserData<CombiningClassDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
return (int)del.Invoke (functions, unicode);
}

[MonoPInvokeCallback (typeof (UnicodeGeneralCategoryProxyDelegate))]
private static int GeneralCategoryProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
private static partial int UnicodeGeneralCategoryProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
{
GetMultiUserData<GeneralCategoryDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
return (int)del.Invoke (functions, unicode);
}

[MonoPInvokeCallback (typeof (UnicodeMirroringProxyDelegate))]
private static uint MirroringProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
private static partial uint UnicodeMirroringProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
{
GetMultiUserData<MirroringDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
return del.Invoke (functions, unicode);
}

[MonoPInvokeCallback (typeof (UnicodeScriptProxyDelegate))]
private static uint ScriptProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
private static partial uint UnicodeScriptProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
{
GetMultiUserData<ScriptDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
return del.Invoke (functions, unicode);
}

[MonoPInvokeCallback (typeof (UnicodeComposeProxyDelegate))]
private static bool ComposeProxyImplementation (IntPtr ufuncs, uint a, uint b, uint* ab, void* context)
private static partial bool UnicodeComposeProxyImplementation (IntPtr ufuncs, uint a, uint b, uint* ab, void* context)
{
GetMultiUserData<ComposeDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
var result = del.Invoke (functions, a, b, out var abManaged);
Expand All @@ -63,8 +54,7 @@ private static bool ComposeProxyImplementation (IntPtr ufuncs, uint a, uint b, u
return result;
}

[MonoPInvokeCallback (typeof (UnicodeDecomposeProxyDelegate))]
private static bool DecomposeProxyImplementation (IntPtr ufuncs, uint ab, uint* a, uint* b, void* context)
private static partial bool UnicodeDecomposeProxyImplementation (IntPtr ufuncs, uint ab, uint* a, uint* b, void* context)
{
GetMultiUserData<DecomposeDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
var result = del.Invoke (functions, ab, out var aManaged, out var bManaged);
Expand Down
4 changes: 2 additions & 2 deletions binding/HarfBuzzSharp/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public Face (GetTableDelegate getTable, ReleaseDelegate destroy)
throw new ArgumentNullException (nameof (getTable));

Handle = HarfBuzzApi.hb_face_create_for_tables (
DelegateProxies.GetTableDelegateProxy,
DelegateProxies.ReferenceTableProxy,
(void*)DelegateProxies.CreateMultiUserData (getTable, destroy, this),
DelegateProxies.ReleaseDelegateProxyForMulti);
DelegateProxies.DestroyProxyForMulti);
}

internal Face (IntPtr handle)
Expand Down
2 changes: 1 addition & 1 deletion binding/HarfBuzzSharp/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void SetFontFunctions (FontFunctions fontFunctions, object fontData, Rele

var container = new FontUserData (this, fontData);
var ctx = DelegateProxies.CreateMultiUserData (destroy, container);
HarfBuzzApi.hb_font_set_funcs (Handle, fontFunctions.Handle, (void*)ctx, DelegateProxies.ReleaseDelegateProxyForMulti);
HarfBuzzApi.hb_font_set_funcs (Handle, fontFunctions.Handle, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
}

public void GetScale (out int xScale, out int yScale)
Expand Down
Loading