Skip to content

Commit

Permalink
[CoreText] Make P/Invokes in CTRunDelegate have blittable signatures. (
Browse files Browse the repository at this point in the history
…#20461)

Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Apr 18, 2024
1 parent 9643bd1 commit 595f1ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/CoreText/CTRunDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ struct CTRunDelegateCallbacks {
[StructLayout (LayoutKind.Sequential)]
struct CTRunDelegateCallbacks {
public /* CFIndex */ nint version;
public CTRunDelegateDeallocateCallback dealloc;
public CTRunDelegateGetCallback getAscent;
public CTRunDelegateGetCallback getDescent;
public CTRunDelegateGetCallback getWidth;
public IntPtr dealloc;
public IntPtr getAscent;
public IntPtr getDescent;
public IntPtr getWidth;
}
#endif
#endregion
Expand Down Expand Up @@ -152,10 +152,10 @@ internal CTRunDelegateCallbacks GetCallbacks ()
#else
callbacks = new CTRunDelegateCallbacks () {
version = 1, // kCTRunDelegateVersion1
dealloc = Deallocate,
getAscent = GetAscent,
getDescent = GetDescent,
getWidth = GetWidth,
dealloc = Marshal.GetFunctionPointerForDelegate (DeallocateDelegate),
getAscent = Marshal.GetFunctionPointerForDelegate (GetAscentDelegate),
getDescent = Marshal.GetFunctionPointerForDelegate (GetDescentDelegate),
getWidth = Marshal.GetFunctionPointerForDelegate (GetWidthDelegate),
};
#endif
}
Expand All @@ -165,6 +165,7 @@ internal CTRunDelegateCallbacks GetCallbacks ()
#if NET
[UnmanagedCallersOnly]
#else
static CTRunDelegateDeallocateCallback DeallocateDelegate = Deallocate;
[MonoPInvokeCallback (typeof (CTRunDelegateDeallocateCallback))]
#endif
static void Deallocate (IntPtr refCon)
Expand All @@ -190,6 +191,7 @@ static void Deallocate (IntPtr refCon)
#if NET
[UnmanagedCallersOnly]
#else
static CTRunDelegateGetCallback GetAscentDelegate = GetAscent;
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetAscent (IntPtr refCon)
Expand All @@ -203,6 +205,7 @@ static nfloat GetAscent (IntPtr refCon)
#if NET
[UnmanagedCallersOnly]
#else
static CTRunDelegateGetCallback GetDescentDelegate = GetDescent;
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetDescent (IntPtr refCon)
Expand All @@ -216,6 +219,7 @@ static nfloat GetDescent (IntPtr refCon)
#if NET
[UnmanagedCallersOnly]
#else
static CTRunDelegateGetCallback GetWidthDelegate = GetWidth;
[MonoPInvokeCallback (typeof (CTRunDelegateGetCallback))]
#endif
static nfloat GetWidth (IntPtr refCon)
Expand All @@ -242,15 +246,17 @@ internal CTRunDelegate (NativeHandle handle, bool owns)

#region RunDelegate Creation
[DllImport (Constants.CoreTextLibrary)]
static extern IntPtr CTRunDelegateCreate (ref CTRunDelegateCallbacks callbacks, IntPtr refCon);
unsafe static extern IntPtr CTRunDelegateCreate (CTRunDelegateCallbacks* callbacks, IntPtr refCon);

static IntPtr Create (CTRunDelegateOperations operations)
{
if (operations is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (operations));

CTRunDelegateCallbacks callbacks = operations.GetCallbacks ();
return CTRunDelegateCreate (ref callbacks, operations.Handle);
unsafe {
return CTRunDelegateCreate (&callbacks, operations.Handle);
}
}

public CTRunDelegate (CTRunDelegateOperations operations)
Expand Down
1 change: 0 additions & 1 deletion tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.IntPtr CoreText.CTFontManager::CTFontManagerCopyRegisteredFontDescriptors(CoreText.CTFontManagerScope,System.Boolean)",
"System.IntPtr CoreText.CTRunDelegate::CTRunDelegateCreate(CoreText.CTRunDelegateCallbacks&,System.IntPtr)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferGetAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
Expand Down

8 comments on commit 595f1ec

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.