Skip to content

Commit

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

Contributes towards #15684.
  • Loading branch information
rolfbjarne committed Feb 28, 2024
1 parent 69bb4ac commit 90992e4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
42 changes: 20 additions & 22 deletions src/CoreGraphics/CGContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,11 @@ public void ReplacePathWithStrokedPath ()


[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGContextIsPathEmpty (/* CGContextRef */ IntPtr context);
extern static byte CGContextIsPathEmpty (/* CGContextRef */ IntPtr context);

public bool IsPathEmpty ()
{
return CGContextIsPathEmpty (Handle);
return CGContextIsPathEmpty (Handle) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand All @@ -365,12 +364,11 @@ public CGRect GetPathBoundingBox ()
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGContextPathContainsPoint (/* CGContextRef */ IntPtr context, CGPoint point, CGPathDrawingMode mode);
extern static byte CGContextPathContainsPoint (/* CGContextRef */ IntPtr context, CGPoint point, CGPathDrawingMode mode);

public bool PathContainsPoint (CGPoint point, CGPathDrawingMode mode)
{
return CGContextPathContainsPoint (Handle, point, mode);
return CGContextPathContainsPoint (Handle, point, mode) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -1244,26 +1242,26 @@ public void Synchronize ()
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetShouldAntialias (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldAntialias);
extern static void CGContextSetShouldAntialias (/* CGContextRef */ IntPtr context, byte shouldAntialias);

public void SetShouldAntialias (bool shouldAntialias)
{
CGContextSetShouldAntialias (Handle, shouldAntialias);
CGContextSetShouldAntialias (Handle, shouldAntialias.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetAllowsAntialiasing (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool allowsAntialiasing);
extern static void CGContextSetAllowsAntialiasing (/* CGContextRef */ IntPtr context, byte allowsAntialiasing);
public void SetAllowsAntialiasing (bool allowsAntialiasing)
{
CGContextSetAllowsAntialiasing (Handle, allowsAntialiasing);
CGContextSetAllowsAntialiasing (Handle, allowsAntialiasing.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetShouldSmoothFonts (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldSmoothFonts);
extern static void CGContextSetShouldSmoothFonts (/* CGContextRef */ IntPtr context, byte shouldSmoothFonts);

public void SetShouldSmoothFonts (bool shouldSmoothFonts)
{
CGContextSetShouldSmoothFonts (Handle, shouldSmoothFonts);
CGContextSetShouldSmoothFonts (Handle, shouldSmoothFonts.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -1353,43 +1351,43 @@ public CGPath CopyPath ()
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetAllowsFontSmoothing (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldSubpixelPositionFonts);
extern static void CGContextSetAllowsFontSmoothing (/* CGContextRef */ IntPtr context, byte shouldSubpixelPositionFonts);

public void SetAllowsFontSmoothing (bool allows)
{
CGContextSetAllowsFontSmoothing (Handle, allows);
CGContextSetAllowsFontSmoothing (Handle, allows.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetAllowsFontSubpixelPositioning (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool allowsFontSubpixelPositioning);
extern static void CGContextSetAllowsFontSubpixelPositioning (/* CGContextRef */ IntPtr context, byte allowsFontSubpixelPositioning);

public void SetAllowsSubpixelPositioning (bool allows)
{
CGContextSetAllowsFontSubpixelPositioning (Handle, allows);
CGContextSetAllowsFontSubpixelPositioning (Handle, allows.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetAllowsFontSubpixelQuantization (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldSubpixelQuantizeFonts);
extern static void CGContextSetAllowsFontSubpixelQuantization (/* CGContextRef */ IntPtr context, byte shouldSubpixelQuantizeFonts);

public void SetAllowsFontSubpixelQuantization (bool allows)
{
CGContextSetAllowsFontSubpixelQuantization (Handle, allows);
CGContextSetAllowsFontSubpixelQuantization (Handle, allows.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetShouldSubpixelPositionFonts (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldSubpixelPositionFonts);
extern static void CGContextSetShouldSubpixelPositionFonts (/* CGContextRef */ IntPtr context, byte shouldSubpixelPositionFonts);

public void SetShouldSubpixelPositionFonts (bool shouldSubpixelPositionFonts)
{
CGContextSetShouldSubpixelPositionFonts (Handle, shouldSubpixelPositionFonts);
CGContextSetShouldSubpixelPositionFonts (Handle, shouldSubpixelPositionFonts.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGContextSetShouldSubpixelQuantizeFonts (/* CGContextRef */ IntPtr context, [MarshalAs (UnmanagedType.I1)] bool shouldSubpixelQuantizeFonts);
extern static void CGContextSetShouldSubpixelQuantizeFonts (/* CGContextRef */ IntPtr context, byte shouldSubpixelQuantizeFonts);

public void ShouldSubpixelQuantizeFonts (bool shouldSubpixelQuantizeFonts)
{
CGContextSetShouldSubpixelQuantizeFonts (Handle, shouldSubpixelQuantizeFonts);
CGContextSetShouldSubpixelQuantizeFonts (Handle, shouldSubpixelQuantizeFonts.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
8 changes: 8 additions & 0 deletions src/ObjCRuntime/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ObjCRuntime {
static class Extensions {
public static byte AsByte (this bool value)
{
return value ? (byte) 1 : (byte) 0;
}
}
}
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,7 @@ SHARED_CORE_SOURCES = \
ObjCRuntime/Constants.cs \
ObjCRuntime/DisposableObject.cs \
ObjCRuntime/ErrorConstants.cs \
ObjCRuntime/Extensions.cs \
ObjCRuntime/INativeObject.cs \
ObjCRuntime/LinkWithAttribute.cs \
ObjCRuntime/Messaging.cs \
Expand Down
10 changes: 0 additions & 10 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLSetSessionOption(System.IntPtr,Security.SslSessionOption,System.Boolean)",
"Security.SslStatus Security.SslContext::SSLWrite(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreGraphics.CGColor::CGColorEqualToColor(System.IntPtr,System.IntPtr)",
"System.Boolean CoreGraphics.CGContext::CGContextIsPathEmpty(System.IntPtr)",
"System.Boolean CoreGraphics.CGContext::CGContextPathContainsPoint(System.IntPtr,CoreGraphics.CGPoint,CoreGraphics.CGPathDrawingMode)",
"System.Boolean CoreGraphics.CGDisplay::CGDisplayIsCaptured(System.UInt32)",
"System.Boolean CoreGraphics.CGEvent::CGEventTapIsEnabled(System.IntPtr)",
"System.Boolean CoreGraphics.CGEvent::PreflightListenEventAccess()",
Expand Down Expand Up @@ -449,14 +447,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.CGContext::CGContextSetAllowsAntialiasing(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsFontSmoothing(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsFontSubpixelPositioning(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsFontSubpixelQuantization(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetShouldAntialias(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetShouldSmoothFonts(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetShouldSubpixelPositionFonts(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetShouldSubpixelQuantizeFonts(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGEvent::CGEventKeyboardGetUnicodeString(System.IntPtr,System.UIntPtr,System.UIntPtr&,System.Char*)",
"System.Void CoreGraphics.CGEvent::CGEventTapEnable(System.IntPtr,System.Boolean)",
"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)",
Expand Down

8 comments on commit 90992e4

@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.