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

[CoreGraphics] Make P/Invokes in CGImage have blittable signatures. (#20229) #20237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
81 changes: 41 additions & 40 deletions src/CoreGraphics/CGPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

using CoreFoundation;
Expand Down Expand Up @@ -97,10 +98,10 @@ public CGPath ()
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static /* CGMutablePathRef */ IntPtr CGPathCreateMutableCopyByTransformingPath (/* CGPathRef */ IntPtr path, /* const CGAffineTransform* */ ref CGAffineTransform transform);
unsafe extern static /* CGMutablePathRef */ IntPtr CGPathCreateMutableCopyByTransformingPath (/* CGPathRef */ IntPtr path, /* const CGAffineTransform* */ CGAffineTransform* transform);

public CGPath (CGPath reference, CGAffineTransform transform)
: base (CGPathCreateMutableCopyByTransformingPath (Runtime.ThrowOnNull (reference, nameof (reference)).Handle, ref transform), true)
public unsafe CGPath (CGPath reference, CGAffineTransform transform)
: base (CGPathCreateMutableCopyByTransformingPath (Runtime.ThrowOnNull (reference, nameof (reference)).Handle, &transform), true)
{
}

Expand Down Expand Up @@ -142,8 +143,7 @@ protected internal override void Release ()
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPathEqualToPath (/* CGPathRef */ IntPtr path1, /* CGPathRef */ IntPtr path2);
extern static byte CGPathEqualToPath (/* CGPathRef */ IntPtr path1, /* CGPathRef */ IntPtr path2);

public static bool operator == (CGPath? path1, CGPath? path2)
{
Expand Down Expand Up @@ -173,7 +173,7 @@ public override bool Equals (object? o)
if (other is null)
return false;

return CGPathEqualToPath (this.Handle, other.Handle);
return CGPathEqualToPath (this.Handle, other.Handle) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -363,16 +363,16 @@ public unsafe void AddEllipseInRect (CGRect rect)
}

[DllImport (Constants.CoreGraphicsLibrary)]
unsafe extern static void CGPathAddArc (/* CGMutablePathRef */ IntPtr path, CGAffineTransform* m, /* CGFloat */ nfloat x, /* CGFloat */ nfloat y, /* CGFloat */ nfloat radius, /* CGFloat */ nfloat startAngle, /* CGFloat */ nfloat endAngle, [MarshalAs (UnmanagedType.I1)] bool clockwise);
unsafe extern static void CGPathAddArc (/* CGMutablePathRef */ IntPtr path, CGAffineTransform* m, /* CGFloat */ nfloat x, /* CGFloat */ nfloat y, /* CGFloat */ nfloat radius, /* CGFloat */ nfloat startAngle, /* CGFloat */ nfloat endAngle, byte clockwise);

public unsafe void AddArc (CGAffineTransform m, nfloat x, nfloat y, nfloat radius, nfloat startAngle, nfloat endAngle, bool clockwise)
{
CGPathAddArc (Handle, &m, x, y, radius, startAngle, endAngle, clockwise);
CGPathAddArc (Handle, &m, x, y, radius, startAngle, endAngle, clockwise.AsByte ());
}

public unsafe void AddArc (nfloat x, nfloat y, nfloat radius, nfloat startAngle, nfloat endAngle, bool clockwise)
{
CGPathAddArc (Handle, null, x, y, radius, startAngle, endAngle, clockwise);
CGPathAddArc (Handle, null, x, y, radius, startAngle, endAngle, clockwise.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -419,22 +419,23 @@ public unsafe void AddPath (CGPath path2)
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPathIsEmpty (/* CGPathRef */ IntPtr path);
extern static byte CGPathIsEmpty (/* CGPathRef */ IntPtr path);

public bool IsEmpty {
get {
return CGPathIsEmpty (Handle);
return CGPathIsEmpty (Handle) != 0;
}
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGPathIsRect (/* CGPathRef */ IntPtr path, out CGRect rect);
unsafe extern static byte CGPathIsRect (/* CGPathRef */ IntPtr path, CGRect* rect);

public bool IsRect (out CGRect rect)
{
return CGPathIsRect (Handle, out rect);
unsafe {
rect = default;
return CGPathIsRect (Handle, (CGRect*) Unsafe.AsPointer<CGRect> (ref rect)) != 0;
}
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -465,17 +466,16 @@ public CGRect PathBoundingBox {
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
unsafe extern static bool CGPathContainsPoint (IntPtr path, CGAffineTransform* m, CGPoint point, [MarshalAs (UnmanagedType.I1)] bool eoFill);
unsafe extern static byte CGPathContainsPoint (IntPtr path, CGAffineTransform* m, CGPoint point, byte eoFill);

public unsafe bool ContainsPoint (CGAffineTransform m, CGPoint point, bool eoFill)
{
return CGPathContainsPoint (Handle, &m, point, eoFill);
return CGPathContainsPoint (Handle, &m, point, eoFill.AsByte ()) != 0;
}

public unsafe bool ContainsPoint (CGPoint point, bool eoFill)
{
return CGPathContainsPoint (Handle, null, point, eoFill);
return CGPathContainsPoint (Handle, null, point, eoFill.AsByte ()) != 0;
}

public delegate void ApplierFunction (CGPathElement element);
Expand Down Expand Up @@ -554,11 +554,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByNormalizing (IntPtr path, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyByNormalizing (IntPtr path, byte evenOddFillRule);

public CGPath? CreateByNormalizing (bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByNormalizing (Handle, evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByNormalizing (Handle, evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -570,11 +570,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByUnioningPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyByUnioningPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateByUnioningPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByUnioningPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByUnioningPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -586,11 +586,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByIntersectingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyByIntersectingPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateByIntersectingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -602,11 +602,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyBySubtractingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyBySubtractingPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateBySubtractingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -618,11 +618,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyBySymmetricDifferenceOfPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyBySymmetricDifferenceOfPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateBySymmetricDifferenceOfPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyBySymmetricDifferenceOfPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyBySymmetricDifferenceOfPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -634,11 +634,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyOfLineBySubtractingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyOfLineBySubtractingPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateLineBySubtractingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyOfLineBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyOfLineBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -650,11 +650,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyOfLineByIntersectingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern IntPtr CGPathCreateCopyOfLineByIntersectingPath (IntPtr path, IntPtr maskPath, byte evenOddFillRule);

public CGPath? CreateLineByIntersectingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyOfLineByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
return Runtime.GetINativeObject<CGPath> (CGPathCreateCopyOfLineByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()), owns: true);
}

#if NET
Expand All @@ -666,11 +666,11 @@ public void Apply (ApplierFunction func)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern unsafe /* CFArrayRef __nullable */ IntPtr CGPathCreateSeparateComponents (IntPtr path, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern unsafe /* CFArrayRef __nullable */ IntPtr CGPathCreateSeparateComponents (IntPtr path, byte evenOddFillRule);

public CGPath [] GetSeparateComponents (bool evenOddFillRule)
{
var cfArrayRef = CGPathCreateSeparateComponents (Handle, evenOddFillRule);
var cfArrayRef = CGPathCreateSeparateComponents (Handle, evenOddFillRule.AsByte ());
if (cfArrayRef == IntPtr.Zero)
return Array.Empty<CGPath> ();
return NSArray.ArrayFromHandle<CGPath> (cfArrayRef);
Expand Down Expand Up @@ -701,12 +701,11 @@ public CGPath [] GetSeparateComponents (bool evenOddFillRule)
[Mac (13, 0), iOS (16, 0), TV (16, 0), MacCatalyst (16, 0), Watch (9, 0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CGPathIntersectsPath (IntPtr path1, IntPtr path2, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);
static extern byte CGPathIntersectsPath (IntPtr path1, IntPtr path2, byte evenOddFillRule);

public bool DoesIntersect (CGPath? maskPath, bool evenOddFillRule)
{
return CGPathIntersectsPath (Handle, maskPath.GetHandle (), evenOddFillRule);
return CGPathIntersectsPath (Handle, maskPath.GetHandle (), evenOddFillRule.AsByte ()) != 0;
}

static CGPath MakeMutable (IntPtr source, bool owns)
Expand Down Expand Up @@ -769,11 +768,13 @@ public unsafe CGPath CopyByStrokingPath (nfloat lineWidth, CGLineCap lineCap, CG
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static IntPtr CGPathCreateCopyByTransformingPath (/* CGPathRef */ IntPtr path, ref CGAffineTransform transform);
unsafe extern static IntPtr CGPathCreateCopyByTransformingPath (/* CGPathRef */ IntPtr path, CGAffineTransform* transform);

public CGPath CopyByTransformingPath (CGAffineTransform transform)
{
return MakeMutable (CGPathCreateCopyByTransformingPath (Handle, ref transform), true);
unsafe {
return MakeMutable (CGPathCreateCopyByTransformingPath (Handle, &transform), true);
}
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
16 changes: 0 additions & 16 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,6 @@ public partial class BlittablePInvokes {
"System.Boolean CoreGraphics.CGDisplay::CGDisplayIsCaptured(System.UInt32)",
"System.Boolean CoreGraphics.CGEventSource::GetButtonState(CoreGraphics.CGEventSourceStateID,CoreGraphics.CGMouseButton)",
"System.Boolean CoreGraphics.CGEventSource::GetKeyState(CoreGraphics.CGEventSourceStateID,System.UInt16)",
"System.Boolean CoreGraphics.CGPath::CGPathContainsPoint(System.IntPtr,CoreGraphics.CGAffineTransform*,CoreGraphics.CGPoint,System.Boolean)",
"System.Boolean CoreGraphics.CGPath::CGPathEqualToPath(System.IntPtr,System.IntPtr)",
"System.Boolean CoreGraphics.CGPath::CGPathIntersectsPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.Boolean CoreGraphics.CGPath::CGPathIsEmpty(System.IntPtr)",
"System.Boolean CoreGraphics.CGPath::CGPathIsRect(System.IntPtr,CoreGraphics.CGRect&)",
"System.Boolean CoreGraphics.CGPDFArray::CGPDFArrayApplyBlock(System.IntPtr,ObjCRuntime.BlockLiteral*,System.IntPtr)",
"System.Boolean CoreGraphics.CGPDFArray::CGPDFArrayGetArray(System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGPDFArray::CGPDFArrayGetBoolean(System.IntPtr,System.IntPtr,System.Boolean&)",
Expand Down Expand Up @@ -372,16 +367,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.Int32 SystemConfiguration.NetworkReachability::SCNetworkReachabilityGetFlags(System.IntPtr,SystemConfiguration.NetworkReachabilityFlags&)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyByIntersectingPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyByNormalizing(System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyBySubtractingPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyBySymmetricDifferenceOfPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyByTransformingPath(System.IntPtr,CoreGraphics.CGAffineTransform&)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyByUnioningPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyOfLineByIntersectingPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateCopyOfLineBySubtractingPath(System.IntPtr,System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateMutableCopyByTransformingPath(System.IntPtr,CoreGraphics.CGAffineTransform&)",
"System.IntPtr CoreGraphics.CGPath::CGPathCreateSeparateComponents(System.IntPtr,System.Boolean)",
"System.IntPtr CoreGraphics.CGPattern::CGPatternCreate(System.IntPtr,CoreGraphics.CGRect,CoreGraphics.CGAffineTransform,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,CoreGraphics.CGPatternTiling,System.Boolean,CoreGraphics.CGPatternCallbacks&)",
"System.IntPtr CoreGraphics.CGPDFStream::CGPDFStreamCopyData(System.IntPtr,CoreGraphics.CGPDFDataFormat&)",
"System.IntPtr CoreGraphics.CGShading::CGShadingCreateAxial(System.IntPtr,CoreGraphics.CGPoint,CoreGraphics.CGPoint,System.IntPtr,System.Boolean,System.Boolean)",
Expand Down Expand Up @@ -433,7 +418,6 @@ public partial class BlittablePInvokes {
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,System.IntPtr)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.Void CoreGraphics.CGPath::CGPathAddArc(System.IntPtr,CoreGraphics.CGAffineTransform*,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,System.Runtime.InteropServices.NFloat,System.Boolean)",
"System.Void CoreGraphics.CGPDFDocument::CGPDFDocumentGetVersion(System.IntPtr,System.Int32&,System.Int32&)",
"System.Void CoreGraphics.CGRectExtensions::CGRectDivide(CoreGraphics.CGRect,CoreGraphics.CGRect&,CoreGraphics.CGRect&,System.Runtime.InteropServices.NFloat,CoreGraphics.CGRectEdge)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontDescriptors(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
Expand Down
Loading