Skip to content

Commit

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

Contributes towards #15684.
  • Loading branch information
rolfbjarne authored Mar 15, 2024
1 parent c343732 commit 87953ed
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 49 deletions.
5 changes: 2 additions & 3 deletions src/CoreGraphics/CGColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ public CGColor (CGColor source, nfloat alpha)
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CGColorEqualToColor (/* CGColorRef */ IntPtr color1, /* CGColorRef */ IntPtr color2);
extern static byte CGColorEqualToColor (/* CGColorRef */ IntPtr color1, /* CGColorRef */ IntPtr color2);

public static bool operator == (CGColor color1, CGColor color2)
{
Expand Down Expand Up @@ -242,7 +241,7 @@ public override bool Equals (object? o)
if (other is null)
return false;

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

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
5 changes: 2 additions & 3 deletions src/CoreGraphics/CGDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ public static int GetGammaTableCapacity (int display)
[Deprecated (PlatformName.MacOSX, 10, 9)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CGDisplayIsCaptured (uint display);
static extern byte CGDisplayIsCaptured (uint display);

public static bool IsCaptured (int display)
{
return CGDisplayIsCaptured ((uint)display);
return CGDisplayIsCaptured ((uint)display) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
16 changes: 10 additions & 6 deletions src/CoreGraphics/CGEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ public double PixelsPerLine {
}
}

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary, EntryPoint="CGEventSourceButtonState")]
[return: MarshalAs (UnmanagedType.I1)]
public extern static bool GetButtonState (CGEventSourceStateID stateID, CGMouseButton button);
[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary)]
extern static byte CGEventSourceButtonState (CGEventSourceStateID stateID, CGMouseButton button);

public static bool GetButtonState (CGEventSourceStateID stateID, CGMouseButton button)
=> CGEventSourceButtonState (stateID, button) != 0;

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary)]
extern static byte CGEventSourceKeyState (CGEventSourceStateID stateID, ushort keycode);

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary, EntryPoint="CGEventSourceKeyState")]
[return: MarshalAs (UnmanagedType.I1)]
public extern static bool GetKeyState (CGEventSourceStateID stateID, ushort keycode);
public static bool GetKeyState (CGEventSourceStateID stateID, ushort keycode)
=> CGEventSourceKeyState (stateID, keycode) != 0;

[DllImport (Constants.ApplicationServicesCoreGraphicsLibrary, EntryPoint="CGEventSourceFlagsState")]
public extern static CGEventFlags GetFlagsState (CGEventSourceStateID stateID);
Expand Down
21 changes: 13 additions & 8 deletions src/CoreGraphics/CGGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#nullable enable

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

Expand Down Expand Up @@ -112,21 +113,19 @@ public static CGRect Standardize (this CGRect self)
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CGRectIsNull (CGRect rect);
static extern byte CGRectIsNull (CGRect rect);

public static bool IsNull (this CGRect self)
{
return CGRectIsNull (self);
return CGRectIsNull (self) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CGRectIsInfinite (CGRect rect);
static extern byte CGRectIsInfinite (CGRect rect);

public static bool IsInfinite (this CGRect self)
{
return CGRectIsInfinite (self);
return CGRectIsInfinite (self) != 0;
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down Expand Up @@ -154,11 +153,17 @@ public static CGRect UnionWith (this CGRect self, CGRect other)
}

[DllImport (Constants.CoreGraphicsLibrary)]
static extern void CGRectDivide (CGRect rect, out CGRect slice, out CGRect remainder, /* GCFloat */ nfloat amount, CGRectEdge edge);
unsafe static extern void CGRectDivide (CGRect rect, CGRect* slice, CGRect* remainder, /* GCFloat */ nfloat amount, CGRectEdge edge);

#if !COREBUILD
public static void Divide (this CGRect self, nfloat amount, CGRectEdge edge, out CGRect slice, out CGRect remainder)
{
CGRectDivide (self, out slice, out remainder, amount, edge);
slice = default;
remainder = default;
unsafe {
CGRectDivide (self, (CGRect*) Unsafe.AsPointer<CGRect> (ref slice), (CGRect*) Unsafe.AsPointer<CGRect> (ref remainder), amount, edge);
}
}
#endif
}
}
4 changes: 2 additions & 2 deletions src/CoreGraphics/CGPDFPage-2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ public int RotationAngle {
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static CGAffineTransform CGPDFPageGetDrawingTransform (/* CGPDFPageRef */ IntPtr page, CGPDFBox box, CGRect rect, int rotate, [MarshalAs (UnmanagedType.I1)] bool preserveAspectRatio);
extern static CGAffineTransform CGPDFPageGetDrawingTransform (/* CGPDFPageRef */ IntPtr page, CGPDFBox box, CGRect rect, int rotate, byte preserveAspectRatio);

public CGAffineTransform GetDrawingTransform (CGPDFBox box, CGRect rect, int rotate, bool preserveAspectRatio)
{
return CGPDFPageGetDrawingTransform (Handle, box, rect, rotate, preserveAspectRatio);
return CGPDFPageGetDrawingTransform (Handle, box, rect, rotate, preserveAspectRatio.AsByte ());
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
10 changes: 7 additions & 3 deletions src/CoreGraphics/CGPDFStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Foundation;
Expand Down Expand Up @@ -76,12 +77,15 @@ public CGPDFDictionary Dictionary {
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static /* CFDataRef */ IntPtr CGPDFStreamCopyData (/* CGPDFStreamRef */ IntPtr stream, /* CGPDFDataFormat* */ out CGPDFDataFormat format);
unsafe extern static /* CFDataRef */ IntPtr CGPDFStreamCopyData (/* CGPDFStreamRef */ IntPtr stream, /* CGPDFDataFormat* */ CGPDFDataFormat* format);

public NSData? GetData (out CGPDFDataFormat format)
{
IntPtr obj = CGPDFStreamCopyData (Handle, out format);
return Runtime.GetNSObject<NSData> (obj, true);
format = default;
unsafe {
IntPtr obj = CGPDFStreamCopyData (Handle, (CGPDFDataFormat*) Unsafe.AsPointer<CGPDFDataFormat> (ref format));
return Runtime.GetNSObject<NSData> (obj, true);
}
}
}
}
21 changes: 13 additions & 8 deletions src/CoreGraphics/CGPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ struct CGPatternCallbacks {
internal unsafe delegate* unmanaged<IntPtr, IntPtr, void> draw;
internal unsafe delegate* unmanaged<IntPtr, void> release;
#else
internal DrawPatternCallback draw;
internal ReleaseInfoCallback release;
internal IntPtr draw;
internal IntPtr release;
#endif
}

Expand Down Expand Up @@ -95,9 +95,9 @@ internal CGPattern (NativeHandle handle, bool owns)
public delegate void DrawPattern (CGContext ctx);

[DllImport (Constants.CoreGraphicsLibrary)]
extern static IntPtr CGPatternCreate (/* void* */ IntPtr info, CGRect bounds, CGAffineTransform matrix,
/* CGFloat */ nfloat xStep, /* CGFloat */ nfloat yStep, CGPatternTiling tiling, [MarshalAs (UnmanagedType.I1)] bool isColored,
/* const CGPatternCallbacks* */ ref CGPatternCallbacks callbacks);
unsafe extern static IntPtr CGPatternCreate (/* void* */ IntPtr info, CGRect bounds, CGAffineTransform matrix,
/* CGFloat */ nfloat xStep, /* CGFloat */ nfloat yStep, CGPatternTiling tiling, byte isColored,
/* const CGPatternCallbacks* */ CGPatternCallbacks* callbacks);

#if NET
static CGPatternCallbacks callbacks;
Expand All @@ -112,10 +112,12 @@ static CGPattern () {
}
}
#else
static DrawPatternCallback drawCallbackDelegate = DrawCallback;
static ReleaseInfoCallback releaseCallbackDelegate = ReleaseCallback;
static CGPatternCallbacks callbacks = new CGPatternCallbacks () {
version = 0,
draw = DrawCallback,
release = ReleaseCallback,
draw = Marshal.GetFunctionPointerForDelegate (drawCallbackDelegate),
release = Marshal.GetFunctionPointerForDelegate (releaseCallbackDelegate),
};
#endif
GCHandle gch;
Expand All @@ -126,7 +128,10 @@ public CGPattern (CGRect bounds, CGAffineTransform matrix, nfloat xStep, nfloat
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (drawPattern));

gch = GCHandle.Alloc (drawPattern);
Handle = CGPatternCreate (GCHandle.ToIntPtr (gch), bounds, matrix, xStep, yStep, tiling, isColored, ref callbacks);
unsafe {
fixed (CGPatternCallbacks* callbacksptr = &callbacks)
Handle = CGPatternCreate (GCHandle.ToIntPtr (gch), bounds, matrix, xStep, yStep, tiling, isColored.AsByte (), callbacksptr);
}
}

#if NET
Expand Down
8 changes: 4 additions & 4 deletions src/CoreGraphics/CGShading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected internal override void Release ()

[DllImport (Constants.CoreGraphicsLibrary)]
extern static /* CGShadingRef */ IntPtr CGShadingCreateAxial (/* CGColorSpaceRef */ IntPtr space,
CGPoint start, CGPoint end, /* CGFunctionRef */ IntPtr functionHandle, [MarshalAs (UnmanagedType.I1)] bool extendStart, [MarshalAs (UnmanagedType.I1)] bool extendEnd);
CGPoint start, CGPoint end, /* CGFunctionRef */ IntPtr functionHandle, byte extendStart, byte extendEnd);

public static CGShading CreateAxial (CGColorSpace colorspace, CGPoint start, CGPoint end, CGFunction function, bool extendStart, bool extendEnd)
{
Expand All @@ -86,13 +86,13 @@ public static CGShading CreateAxial (CGColorSpace colorspace, CGPoint start, CGP
if (function is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (function));

return new CGShading (CGShadingCreateAxial (colorspace.GetCheckedHandle (), start, end, function.GetCheckedHandle (), extendStart, extendEnd), true);
return new CGShading (CGShadingCreateAxial (colorspace.GetCheckedHandle (), start, end, function.GetCheckedHandle (), extendStart.AsByte (), extendEnd.AsByte ()), true);
}

[DllImport (Constants.CoreGraphicsLibrary)]
extern static /* CGShadingRef */ IntPtr CGShadingCreateRadial (/* CGColorSpaceRef */ IntPtr space,
CGPoint start, /* CGFloat */ nfloat startRadius, CGPoint end, /* CGFloat */ nfloat endRadius,
/* CGFunctionRef */ IntPtr function, [MarshalAs (UnmanagedType.I1)] bool extendStart, [MarshalAs (UnmanagedType.I1)] bool extendEnd);
/* CGFunctionRef */ IntPtr function, byte extendStart, byte extendEnd);

public static CGShading CreateRadial (CGColorSpace colorspace, CGPoint start, nfloat startRadius, CGPoint end, nfloat endRadius,
CGFunction function, bool extendStart, bool extendEnd)
Expand All @@ -103,7 +103,7 @@ public static CGShading CreateRadial (CGColorSpace colorspace, CGPoint start, nf
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (function));

return new CGShading (CGShadingCreateRadial (colorspace.GetCheckedHandle (), start, startRadius, end, endRadius,
function.GetCheckedHandle (), extendStart, extendEnd), true);
function.GetCheckedHandle (), extendStart.AsByte (), extendEnd.AsByte ()), true);
}

[DllImport (Constants.CoreGraphicsLibrary)]
Expand Down
12 changes: 0 additions & 12 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public partial class BlittablePInvokes {
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSend(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"CoreGraphics.CGAffineTransform CoreGraphics.CGPDFPage::CGPDFPageGetDrawingTransform(System.IntPtr,CoreGraphics.CGPDFBox,CoreGraphics.CGRect,System.Int32,System.Boolean)",
"CoreGraphics.CGRect CoreMedia.CMFormatDescription::CMVideoFormatDescriptionGetCleanAperture(System.IntPtr,System.Boolean)",
"CoreGraphics.CGSize CoreMedia.CMFormatDescription::CMVideoFormatDescriptionGetPresentationDimensions(System.IntPtr,System.Boolean,System.Boolean)",
"CoreGraphics.CGSize CoreText.CTFramesetter::CTFramesetterSuggestFrameSizeWithConstraints(System.IntPtr,Foundation.NSRange,System.IntPtr,CoreGraphics.CGSize,Foundation.NSRange&)",
Expand Down Expand Up @@ -192,12 +191,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLRead(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"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.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.CGRectExtensions::CGRectIsInfinite(CoreGraphics.CGRect)",
"System.Boolean CoreGraphics.CGRectExtensions::CGRectIsNull(CoreGraphics.CGRect)",
"System.Boolean CoreMedia.CMBlockBuffer::CMBlockBufferIsEmpty(System.IntPtr)",
"System.Boolean CoreMedia.CMBlockBuffer::CMBlockBufferIsRangeContiguous(System.IntPtr,System.UIntPtr,System.UIntPtr)",
"System.Boolean CoreMedia.CMClock::CMClockMightDrift(System.IntPtr,System.IntPtr)",
Expand Down Expand Up @@ -328,10 +321,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.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)",
"System.IntPtr CoreGraphics.CGShading::CGShadingCreateRadial(System.IntPtr,CoreGraphics.CGPoint,System.Runtime.InteropServices.NFloat,CoreGraphics.CGPoint,System.Runtime.InteropServices.NFloat,System.IntPtr,System.Boolean,System.Boolean)",
"System.IntPtr CoreMedia.CMAttachmentBearer::CMGetAttachment(System.IntPtr,System.IntPtr,CoreMedia.CMAttachmentMode&)",
"System.IntPtr CoreMedia.CMFormatDescription::CMAudioFormatDescriptionGetChannelLayout(System.IntPtr,System.IntPtr&)",
"System.IntPtr CoreMedia.CMFormatDescription::CMAudioFormatDescriptionGetFormatList(System.IntPtr,System.IntPtr&)",
Expand Down Expand Up @@ -379,7 +368,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.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*)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontsWithAssetNames(System.IntPtr,System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
"System.Void CoreText.CTFontManager::CTFontManagerRegisterFontURLs(System.IntPtr,CoreText.CTFontManagerScope,System.Boolean,ObjCRuntime.BlockLiteral*)",
Expand Down

8 comments on commit 87953ed

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