From 7dfb9158845669c0ce66a8b0691862f4f9686b49 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 22 Dec 2023 00:36:25 +0100 Subject: [PATCH] [ImageIO] Make P/Invokes have blittable signatures. Contributes towards #15684. --- src/ImageIO/CGImageDestination.cs | 18 +++++----- src/ImageIO/CGImageSource.cs | 8 ++--- src/ImageIO/CGMutableImageMetadata.cs | 33 +++++++++---------- .../BlittablePInvokes.KnownFailures.cs | 9 ----- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/ImageIO/CGImageDestination.cs b/src/ImageIO/CGImageDestination.cs index b7d05a56a192..81efd07b309e 100644 --- a/src/ImageIO/CGImageDestination.cs +++ b/src/ImageIO/CGImageDestination.cs @@ -301,14 +301,13 @@ public void AddImage (CGImageSource source, int index, NSDictionary? properties) } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageDestinationFinalize (/* CGImageDestinationRef __nonnull */ IntPtr idst); + extern static byte CGImageDestinationFinalize (/* CGImageDestinationRef __nonnull */ IntPtr idst); public bool Close () { var success = CGImageDestinationFinalize (Handle); Dispose (); - return success; + return success != 0; } #if NET @@ -355,10 +354,9 @@ public void AddImageAndMetadata (CGImage image, CGImageMetadata meta, CGImageDes [SupportedOSPlatform ("tvos")] #endif [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageDestinationCopyImageSource (/* CGImageDestinationRef __nonnull */ IntPtr idst, + unsafe extern static byte CGImageDestinationCopyImageSource (/* CGImageDestinationRef __nonnull */ IntPtr idst, /* CGImageSourceRef __nonnull */ IntPtr image, /* CFDictionaryRef __nullable */ IntPtr options, - /* CFErrorRef* */ out IntPtr err); + /* CFErrorRef* */ IntPtr* err); #if NET [SupportedOSPlatform ("ios")] @@ -371,9 +369,13 @@ public bool CopyImageSource (CGImageSource image, NSDictionary? options, out NSE { if (image is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (image)); - var result = CGImageDestinationCopyImageSource (Handle, image.Handle, options.GetHandle (), out var err); + byte result; + IntPtr err; + unsafe { + result = CGImageDestinationCopyImageSource (Handle, image.Handle, options.GetHandle (), &err); + } error = Runtime.GetNSObject (err); - return result; + return result != 0; } #if NET diff --git a/src/ImageIO/CGImageSource.cs b/src/ImageIO/CGImageSource.cs index 33654f21a123..fd2955c7c403 100644 --- a/src/ImageIO/CGImageSource.cs +++ b/src/ImageIO/CGImageSource.cs @@ -322,25 +322,25 @@ public static CGImageSource CreateIncremental (CGImageOptions? options) [DllImport (Constants.ImageIOLibrary)] extern static void CGImageSourceUpdateData (/* CGImageSourceRef __nonnull */ IntPtr isrc, - /* CFDataRef __nonnull */ IntPtr data, [MarshalAs (UnmanagedType.I1)] bool final); + /* CFDataRef __nonnull */ IntPtr data, byte final); public void UpdateData (NSData data, bool final) { if (data is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (data)); - CGImageSourceUpdateData (Handle, data.Handle, final); + CGImageSourceUpdateData (Handle, data.Handle, final ? (byte) 1 : (byte) 0); } [DllImport (Constants.ImageIOLibrary)] extern static void CGImageSourceUpdateDataProvider (/* CGImageSourceRef __nonnull */ IntPtr handle, /* CGDataProviderRef __nonnull */ IntPtr dataProvider, - [MarshalAs (UnmanagedType.I1)] bool final); + byte final); public void UpdateDataProvider (CGDataProvider provider, bool final) { if (provider is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (provider)); - CGImageSourceUpdateDataProvider (Handle, provider.Handle, final); + CGImageSourceUpdateDataProvider (Handle, provider.Handle, final ? (byte) 1 : (byte) 0); } // note: CGImageSourceStatus is always an int (4 bytes) so it's ok to use in the pinvoke declaration diff --git a/src/ImageIO/CGMutableImageMetadata.cs b/src/ImageIO/CGMutableImageMetadata.cs index dbc6350cd4c3..44d4043e5af4 100644 --- a/src/ImageIO/CGMutableImageMetadata.cs +++ b/src/ImageIO/CGMutableImageMetadata.cs @@ -46,10 +46,9 @@ public CGMutableImageMetadata (CGImageMetadata metadata) } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageMetadataRegisterNamespaceForPrefix ( + unsafe extern static byte CGImageMetadataRegisterNamespaceForPrefix ( /* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, /* CFStringRef __nonnull */ IntPtr xmlns, - /* CFStringRef __nonnull */ IntPtr prefix, /* CFErrorRef __nullable */ out IntPtr error); + /* CFStringRef __nonnull */ IntPtr prefix, /* CFErrorRef __nullable */ IntPtr* error); public bool RegisterNamespace (NSString xmlns, NSString prefix, out NSError? error) { @@ -57,14 +56,17 @@ public bool RegisterNamespace (NSString xmlns, NSString prefix, out NSError? err ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (xmlns)); if (prefix is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (prefix)); - bool result = CGImageMetadataRegisterNamespaceForPrefix (Handle, xmlns.Handle, prefix.Handle, out var err); + byte result; + IntPtr err; + unsafe { + result = CGImageMetadataRegisterNamespaceForPrefix (Handle, xmlns.Handle, prefix.Handle, &err); + } error = Runtime.GetNSObject (err); - return result; + return result != 0; } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageMetadataSetTagWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, + extern static byte CGImageMetadataSetTagWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, /* CGImageMetadataTagRef __nullable */ IntPtr parent, /* CFStringRef __nonnull */ IntPtr path, /* CGImageMetadataTagRef __nonnull */ IntPtr tag); @@ -74,12 +76,11 @@ public bool SetTag (CGImageMetadataTag? parent, NSString path, CGImageMetadataTa ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (path)); if (tag is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (tag)); - return CGImageMetadataSetTagWithPath (Handle, parent.GetHandle (), path.Handle, tag.Handle); + return CGImageMetadataSetTagWithPath (Handle, parent.GetHandle (), path.Handle, tag.Handle) != 0; } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageMetadataSetValueWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, + extern static byte CGImageMetadataSetValueWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, /* CGImageMetadataTagRef __nullable */ IntPtr parent, /* CFStringRef __nonnull */ IntPtr path, /* CFTypeRef __nonnull */ IntPtr value); @@ -99,24 +100,22 @@ bool SetValue (CGImageMetadataTag? parent, NSString path, IntPtr value) { if (path is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (path)); - return CGImageMetadataSetValueWithPath (Handle, parent.GetHandle (), path.Handle, value); + return CGImageMetadataSetValueWithPath (Handle, parent.GetHandle (), path.Handle, value) != 0; } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageMetadataRemoveTagWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, + extern static byte CGImageMetadataRemoveTagWithPath (/* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, /* CGImageMetadataTagRef __nullable */ IntPtr parent, /* CFStringRef __nonnull */ IntPtr path); public bool RemoveTag (CGImageMetadataTag? parent, NSString path) { if (path is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (path)); - return CGImageMetadataRemoveTagWithPath (Handle, parent.GetHandle (), path.Handle); + return CGImageMetadataRemoveTagWithPath (Handle, parent.GetHandle (), path.Handle) != 0; } [DllImport (Constants.ImageIOLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - extern static bool CGImageMetadataSetValueMatchingImageProperty ( + extern static byte CGImageMetadataSetValueMatchingImageProperty ( /* CGMutableImageMetadataRef __nonnull */ IntPtr metadata, /* CFStringRef __nonnull */ IntPtr dictionaryName, /* CFStringRef __nonnull */ IntPtr propertyName, /* CFTypeRef __nonnull */ IntPtr value); @@ -139,7 +138,7 @@ bool SetValueMatchingImageProperty (NSString dictionaryName, NSString propertyNa ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (dictionaryName)); if (propertyName is null) ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (propertyName)); - return CGImageMetadataSetValueMatchingImageProperty (Handle, dictionaryName.Handle, propertyName.Handle, value); + return CGImageMetadataSetValueMatchingImageProperty (Handle, dictionaryName.Handle, propertyName.Handle, value) != 0; } } } diff --git a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs index 25cee30bd483..be4eaaeb17c7 100644 --- a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs +++ b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs @@ -453,13 +453,6 @@ public partial class BlittablePInvokes { "System.Boolean GameController.GCMicroGamepadSnapshot::GCMicroGamepadSnapshotDataFromNSData(GameController.GCMicroGamepadSnapshotData&,System.IntPtr)", "System.Boolean GameController.GCMicroGamepadSnapshot::GCMicroGamepadSnapShotDataV100FromNSData(GameController.GCMicroGamepadSnapShotDataV100&,System.IntPtr)", "System.Boolean HealthKit.HKAppleWalkingSteadiness::HKAppleWalkingSteadinessClassificationForQuantity(System.IntPtr,System.IntPtr&,System.IntPtr&)", - "System.Boolean ImageIO.CGImageDestination::CGImageDestinationCopyImageSource(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)", - "System.Boolean ImageIO.CGImageDestination::CGImageDestinationFinalize(System.IntPtr)", - "System.Boolean ImageIO.CGMutableImageMetadata::CGImageMetadataRegisterNamespaceForPrefix(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)", - "System.Boolean ImageIO.CGMutableImageMetadata::CGImageMetadataRemoveTagWithPath(System.IntPtr,System.IntPtr,System.IntPtr)", - "System.Boolean ImageIO.CGMutableImageMetadata::CGImageMetadataSetTagWithPath(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr)", - "System.Boolean ImageIO.CGMutableImageMetadata::CGImageMetadataSetValueMatchingImageProperty(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr)", - "System.Boolean ImageIO.CGMutableImageMetadata::CGImageMetadataSetValueWithPath(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr)", "System.Boolean MapKit.MKMapRect::Intersects(MapKit.MKMapRect,MapKit.MKMapRect)", "System.Boolean MapKit.MKMapRect::MKMapRectContainsPoint(MapKit.MKMapRect,MapKit.MKMapPoint)", "System.Boolean MapKit.MKMapRect::MKMapRectContainsRect(MapKit.MKMapRect,MapKit.MKMapRect)", @@ -796,8 +789,6 @@ public partial class BlittablePInvokes { "System.Void CoreVideo.CVPixelBuffer::CVPixelBufferGetExtendedPixels(System.IntPtr,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&,System.UIntPtr&)", "System.Void Foundation.NSDecimal::NSDecimalRound(Foundation.NSDecimal&,Foundation.NSDecimal&,System.IntPtr,System.UIntPtr)", "System.Void Foundation.NSObject::xamarin_release_managed_ref(System.IntPtr,System.Boolean)", - "System.Void ImageIO.CGImageSource::CGImageSourceUpdateData(System.IntPtr,System.IntPtr,System.Boolean)", - "System.Void ImageIO.CGImageSource::CGImageSourceUpdateDataProvider(System.IntPtr,System.IntPtr,System.Boolean)", "System.Void MapKit.MKMapRect::MKMapRectDivide(MapKit.MKMapRect,MapKit.MKMapRect&,MapKit.MKMapRect&,System.Double,CoreGraphics.CGRectEdge)", "System.Void Network.NWAdvertiseDescriptor::nw_advertise_descriptor_set_no_auto_rename(System.IntPtr,System.Boolean)", "System.Void Network.NWBrowserDescriptor::nw_browse_descriptor_set_include_txt_record(System.IntPtr,System.Boolean)",