Skip to content

Commit 9cbf9c8

Browse files
maxkatz6mattleibowfilipnavara
authored
Function pointers and LibraryImport (#2917)
* Update generator to emit function pointers * Regenerate interop files * Enable USE_LIBRARY_IMPORT in HarfBuzzSharp * Enable USE_LIBRARY_IMPORT on SkiaSharp * Set DisableRuntimeMarshalling on HarfBuzzSharp * Replace remaining DllImports with LibraryImport on SkiaSharp * Set DisableRuntimeMarshalling on SkiaSharp as well * Fix missed proxy definition * Regenerate skia api with a correct submodule version * Collections literals are not supported on the CI .NET SDK * An attempt to fix Tizen build * Forgot about partial * Set UnmanagedType.LPStr on evas_gl_proc_address_get instead * Set USE_LIBRARY_IMPORT on remaining projects too * Update generator tool to generate DelegateProxy as well * Regenerate HarfBuzz and SkiaSharp with new DelegateProxy source gen * Regenerate other projects as well * Add `protected internal` to test classes too, since this project has InternalsVisibleTo configured * Disable DelegateTypesAreValid and DelegateTypesHaveAttributes tests on .NET 7+ build, see comments * Reduce warnings noise * Update binding/SkiaSharp/GRGlInterface.cs Co-authored-by: Filip Navara <[email protected]> * Add missing USE_LIBRARY_IMPORT defines * Update binding/SkiaSharp/GRGlInterface.cs * Also needs USE_LIBRARY_IMPORT --------- Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Filip Navara <[email protected]>
1 parent 6058ab0 commit 9cbf9c8

File tree

62 files changed

+7937
-536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+7937
-536
lines changed

Diff for: binding/Binding.Shared/DelegateProxies.shared.cs

-14
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@ internal static partial class DelegateProxies
2525
{
2626
// normal delegates
2727

28-
[MethodImpl (MethodImplOptions.AggressiveInlining)]
29-
public static T Create<T> (object managedDel, T nativeDel, out GCHandle gch, out IntPtr contextPtr)
30-
{
31-
if (managedDel == null) {
32-
gch = default (GCHandle);
33-
contextPtr = IntPtr.Zero;
34-
return default (T);
35-
}
36-
37-
gch = GCHandle.Alloc (managedDel);
38-
contextPtr = GCHandle.ToIntPtr (gch);
39-
return nativeDel;
40-
}
41-
4228
[MethodImpl (MethodImplOptions.AggressiveInlining)]
4329
public static void Create (object managedDel, out GCHandle gch, out IntPtr contextPtr)
4430
{

Diff for: binding/HarfBuzzSharp/Blob.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#nullable disable
22

33
using System;
4-
using System.ComponentModel;
54
using System.IO;
65

76
namespace HarfBuzzSharp
@@ -84,7 +83,8 @@ public static unsafe Blob FromStream (Stream stream)
8483

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

Diff for: binding/HarfBuzzSharp/DelegateProxies.cs

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#nullable disable
22

33
using System;
4-
using System.ComponentModel;
4+
using System.Runtime.CompilerServices;
5+
using System.Runtime.InteropServices;
6+
// ReSharper disable PartialMethodParameterNameMismatch
57

68
namespace HarfBuzzSharp
79
{
@@ -11,15 +13,7 @@ namespace HarfBuzzSharp
1113

1214
internal static unsafe partial class DelegateProxies
1315
{
14-
// references to the proxy implementations
15-
public static readonly DestroyProxyDelegate ReleaseDelegateProxy = ReleaseDelegateProxyImplementation;
16-
public static readonly DestroyProxyDelegate ReleaseDelegateProxyForMulti = ReleaseDelegateProxyImplementationForMulti;
17-
public static readonly ReferenceTableProxyDelegate GetTableDelegateProxy = GetTableDelegateProxyImplementation;
18-
19-
// internal proxy implementations
20-
21-
[MonoPInvokeCallback (typeof (DestroyProxyDelegate))]
22-
private static void ReleaseDelegateProxyImplementation (void* context)
16+
private static partial void DestroyProxyImplementation (void* context)
2317
{
2418
var del = Get<ReleaseDelegate> ((IntPtr)context, out var gch);
2519
try {
@@ -29,16 +23,7 @@ private static void ReleaseDelegateProxyImplementation (void* context)
2923
}
3024
}
3125

32-
[MonoPInvokeCallback (typeof (ReferenceTableProxyDelegate))]
33-
private static IntPtr GetTableDelegateProxyImplementation (IntPtr face, uint tag, void* context)
34-
{
35-
GetMultiUserData<GetTableDelegate, Face> ((IntPtr)context, out var getTable, out var userData, out _);
36-
var blob = getTable.Invoke (userData, tag);
37-
return blob?.Handle ?? IntPtr.Zero;
38-
}
39-
40-
[MonoPInvokeCallback (typeof (DestroyProxyDelegate))]
41-
private static void ReleaseDelegateProxyImplementationForMulti (void* context)
26+
private static partial void DestroyProxyImplementationForMulti (void* context)
4227
{
4328
var del = GetMulti<ReleaseDelegate> ((IntPtr)context, out var gch);
4429
try {
@@ -47,5 +32,12 @@ private static void ReleaseDelegateProxyImplementationForMulti (void* context)
4732
gch.Free ();
4833
}
4934
}
35+
36+
private static partial IntPtr ReferenceTableProxyImplementation (IntPtr face, uint tag, void* context)
37+
{
38+
GetMultiUserData<GetTableDelegate, Face> ((IntPtr)context, out var getTable, out var userData, out _);
39+
var blob = getTable.Invoke (userData, tag);
40+
return blob?.Handle ?? IntPtr.Zero;
41+
}
5042
}
5143
}

Diff for: binding/HarfBuzzSharp/DelegateProxies.font.cs

+13-38
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#nullable disable
2+
// ReSharper disable PartialMethodParameterNameMismatch
23

34
using System;
45

@@ -30,24 +31,9 @@ namespace HarfBuzzSharp
3031

3132
internal static unsafe partial class DelegateProxies
3233
{
33-
// references to the proxy implementations
34-
public static readonly FontGetFontExtentsProxyDelegate FontExtentsProxy = FontExtentsProxyImplementation;
35-
public static readonly FontGetNominalGlyphProxyDelegate NominalGlyphProxy = NominalGlyphProxyImplementation;
36-
public static readonly FontGetVariationGlyphProxyDelegate VariationGlyphProxy = VariationGlyphProxyImplementation;
37-
public static readonly FontGetNominalGlyphsProxyDelegate NominalGlyphsProxy = NominalGlyphsProxyImplementation;
38-
public static readonly FontGetGlyphAdvanceProxyDelegate GlyphAdvanceProxy = GlyphAdvanceProxyImplementation;
39-
public static readonly FontGetGlyphAdvancesProxyDelegate GlyphAdvancesProxy = GlyphAdvancesProxyImplementation;
40-
public static readonly FontGetGlyphOriginProxyDelegate GlyphOriginProxy = GlyphOriginProxyImplementation;
41-
public static readonly FontGetGlyphKerningProxyDelegate GlyphKerningProxy = GlyphKerningProxyImplementation;
42-
public static readonly FontGetGlyphExtentsProxyDelegate GlyphExtentsProxy = GlyphExtentsProxyImplementation;
43-
public static readonly FontGetGlyphContourPointProxyDelegate GlyphContourPointProxy = GlyphContourPointProxyImplementation;
44-
public static readonly FontGetGlyphNameProxyDelegate GlyphNameProxy = GlyphNameProxyImplementation;
45-
public static readonly FontGetGlyphFromNameProxyDelegate GlyphFromNameProxy = GlyphFromNameProxyImplementation;
46-
4734
// internal proxy implementations
4835

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

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

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

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

92-
[MonoPInvokeCallback (typeof (FontGetGlyphAdvanceProxyDelegate))]
93-
private static int GlyphAdvanceProxyImplementation (IntPtr font, void* fontData, uint glyph, void* context)
75+
private static partial int FontGetGlyphAdvanceProxyImplementation (IntPtr font, void* fontData, uint glyph, void* context)
9476
{
9577
var del = GetMulti<GlyphAdvanceDelegate> ((IntPtr)context, out _);
9678
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
9779
return del.Invoke (userData.Font, userData.FontData, glyph);
9880
}
9981

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

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

123-
[MonoPInvokeCallback (typeof (FontGetGlyphKerningProxyDelegate))]
124-
private static int GlyphKerningProxyImplementation (IntPtr font, void* fontData, uint firstGlyph, uint secondGlyph, void* context)
103+
private static partial int FontGetGlyphKerningProxyImplementation (IntPtr font, void* fontData, uint firstGlyph, uint secondGlyph, void* context)
125104
{
126105
var del = GetMulti<GlyphKerningDelegate> ((IntPtr)context, out _);
127106
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);
128107
return del.Invoke (userData.Font, userData.FontData, firstGlyph, secondGlyph);
129108
}
130109

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

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

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

169-
[MonoPInvokeCallback (typeof (FontGetGlyphFromNameProxyDelegate))]
170-
private static bool GlyphFromNameProxyImplementation (IntPtr font, void* fontData, void* name, int len, uint* glyph, void* context)
145+
private static partial bool FontGetGlyphFromNameProxyImplementation (IntPtr font, void* fontData, void* name, int len, uint* glyph, void* context)
171146
{
172147
var del = GetMulti<GlyphFromNameDelegate> ((IntPtr)context, out _);
173148
var userData = GetMultiUserData<FontUserData> ((IntPtr)fontData, out _);

Diff for: binding/HarfBuzzSharp/DelegateProxies.unicode.cs

+9-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#nullable disable
2+
// ReSharper disable PartialMethodParameterNameMismatch
23

34
using System;
5+
using System.Runtime.CompilerServices;
6+
using System.Runtime.InteropServices;
47

58
namespace HarfBuzzSharp
69
{
@@ -18,43 +21,31 @@ namespace HarfBuzzSharp
1821

1922
internal static unsafe partial class DelegateProxies
2023
{
21-
public static readonly UnicodeCombiningClassProxyDelegate CombiningClassProxy = CombiningClassProxyImplementation;
22-
public static readonly UnicodeGeneralCategoryProxyDelegate GeneralCategoryProxy = GeneralCategoryProxyImplementation;
23-
public static readonly UnicodeMirroringProxyDelegate MirroringProxy = MirroringProxyImplementation;
24-
public static readonly UnicodeScriptProxyDelegate ScriptProxy = ScriptProxyImplementation;
25-
public static readonly UnicodeComposeProxyDelegate ComposeProxy = ComposeProxyImplementation;
26-
public static readonly UnicodeDecomposeProxyDelegate DecomposeProxy = DecomposeProxyImplementation;
27-
28-
[MonoPInvokeCallback (typeof (UnicodeCombiningClassProxyDelegate))]
29-
private static int CombiningClassProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
24+
private static partial int UnicodeCombiningClassProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
3025
{
3126
GetMultiUserData<CombiningClassDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
3227
return (int)del.Invoke (functions, unicode);
3328
}
3429

35-
[MonoPInvokeCallback (typeof (UnicodeGeneralCategoryProxyDelegate))]
36-
private static int GeneralCategoryProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
30+
private static partial int UnicodeGeneralCategoryProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
3731
{
3832
GetMultiUserData<GeneralCategoryDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
3933
return (int)del.Invoke (functions, unicode);
4034
}
4135

42-
[MonoPInvokeCallback (typeof (UnicodeMirroringProxyDelegate))]
43-
private static uint MirroringProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
36+
private static partial uint UnicodeMirroringProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
4437
{
4538
GetMultiUserData<MirroringDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
4639
return del.Invoke (functions, unicode);
4740
}
4841

49-
[MonoPInvokeCallback (typeof (UnicodeScriptProxyDelegate))]
50-
private static uint ScriptProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
42+
private static partial uint UnicodeScriptProxyImplementation (IntPtr ufuncs, uint unicode, void* context)
5143
{
5244
GetMultiUserData<ScriptDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
5345
return del.Invoke (functions, unicode);
5446
}
5547

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

66-
[MonoPInvokeCallback (typeof (UnicodeDecomposeProxyDelegate))]
67-
private static bool DecomposeProxyImplementation (IntPtr ufuncs, uint ab, uint* a, uint* b, void* context)
57+
private static partial bool UnicodeDecomposeProxyImplementation (IntPtr ufuncs, uint ab, uint* a, uint* b, void* context)
6858
{
6959
GetMultiUserData<DecomposeDelegate, UnicodeFunctions> ((IntPtr)context, out var del, out var functions, out _);
7060
var result = del.Invoke (functions, ab, out var aManaged, out var bManaged);

Diff for: binding/HarfBuzzSharp/Face.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public Face (GetTableDelegate getTable, ReleaseDelegate destroy)
4141
throw new ArgumentNullException (nameof (getTable));
4242

4343
Handle = HarfBuzzApi.hb_face_create_for_tables (
44-
DelegateProxies.GetTableDelegateProxy,
44+
DelegateProxies.ReferenceTableProxy,
4545
(void*)DelegateProxies.CreateMultiUserData (getTable, destroy, this),
46-
DelegateProxies.ReleaseDelegateProxyForMulti);
46+
DelegateProxies.DestroyProxyForMulti);
4747
}
4848

4949
internal Face (IntPtr handle)

Diff for: binding/HarfBuzzSharp/Font.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void SetFontFunctions (FontFunctions fontFunctions, object fontData, Rele
5454

5555
var container = new FontUserData (this, fontData);
5656
var ctx = DelegateProxies.CreateMultiUserData (destroy, container);
57-
HarfBuzzApi.hb_font_set_funcs (Handle, fontFunctions.Handle, (void*)ctx, DelegateProxies.ReleaseDelegateProxyForMulti);
57+
HarfBuzzApi.hb_font_set_funcs (Handle, fontFunctions.Handle, (void*)ctx, DelegateProxies.DestroyProxyForMulti);
5858
}
5959

6060
public void GetScale (out int xScale, out int yScale)

0 commit comments

Comments
 (0)