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

[ImageIO] Make P/Invokes have blittable signatures. #19701

Merged
merged 1 commit into from
Jan 2, 2024
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
18 changes: 10 additions & 8 deletions src/ImageIO/CGImageDestination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")]
Expand All @@ -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<NSError> (err);
return result;
return result != 0;
}

#if NET
Expand Down
8 changes: 4 additions & 4 deletions src/ImageIO/CGImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 16 additions & 17 deletions src/ImageIO/CGMutableImageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,27 @@ 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)
{
if (xmlns is null)
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<NSError> (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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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;
}
}
}
9 changes: 0 additions & 9 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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)",
Expand Down