Skip to content

Commit

Permalink
[src] Remove a few MarshalAs attributes in various APIs. (#20704)
Browse files Browse the repository at this point in the history
In these cases the APIs in question aren't used in P/Invokes at the moment,
but that may change, so just make as much as we can blittable by removing any
MarshalAs attributes.
  • Loading branch information
rolfbjarne committed Jun 10, 2024
1 parent acf63db commit 9c2bd68
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/AVFoundation/AVSampleBufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ public static class AVSampleBufferExtensions {

[iOS (14, 5), Mac (11, 3), TV (14, 5), Watch (7,4)]
[DllImport (Constants.AVFoundationLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern /* BOOL */ bool AVSampleBufferAttachContentKey (
unsafe static extern /* BOOL */ byte AVSampleBufferAttachContentKey (
/* CMSampleBufferRef */ CMSampleBufferRef sbuf,
/* AVContentKey */ AVContentKey contentKey,
/* NSError * _Nullable * _Nullable */ out NSErrorPtr outError);
/* NSError * _Nullable * _Nullable */ IntPtr* outError);

[iOS (14, 5), Mac (11, 3), TV (14, 5), Watch (7,4)]
public static bool AttachContentKey (this CMSampleBuffer sampleBuffer, AVContentKey contentKey, out NSError error)
Expand All @@ -39,9 +38,12 @@ public static bool AttachContentKey (this CMSampleBuffer sampleBuffer, AVContent
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (contentKey));

IntPtr outerr;
var retVal = AVSampleBufferAttachContentKey (sampleBuffer.Handle, contentKey.Handle, out outerr);
byte retVal;
unsafe {
retVal = AVSampleBufferAttachContentKey (sampleBuffer.Handle, contentKey.Handle, &outerr);
}
error = Runtime.GetNSObject<NSError> (outerr);
return retVal;
return retVal != 0;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ImageIO/CGImageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ internal NSMutableDictionary ToDictionary ()
}
}

[return: MarshalAs (UnmanagedType.I1)]
public delegate bool CGImageMetadataTagBlock (NSString path, CGImageMetadataTag tag);

// CGImageMetadata.h
Expand Down
72 changes: 72 additions & 0 deletions src/Metal/Defs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,62 @@ public MTLDrawPatchIndirectArguments (uint pathCount, uint instanceCount, uint p
#endif
[StructLayout (LayoutKind.Sequential)]
public struct MTLQuadTessellationFactorsHalf {
#if XAMCORE_5_0
ushort edgeTessellationFactor0;
ushort edgeTessellationFactor1;
ushort edgeTessellationFactor2;
ushort edgeTessellationFactor3;
ushort insideTessellationFactor0;
ushort insideTessellationFactor1;

public ushort [] EdgeTessellationFactor {
get => new ushort [] { edgeTessellationFactor0, edgeTessellationFactor1, edgeTessellationFactor2, edgeTessellationFactor3 };
set {
if (value.Length > 4)
throw new ArgumentOutOfRangeException ($"The '{nameof (value)}' array length can't be greater than 4.");

edgeTessellationFactor0 = value.Length >= 1 ? value [0] : 0;
edgeTessellationFactor1 = value.Length >= 2 ? value [1] : 0;
edgeTessellationFactor2 = value.Length >= 3 ? value [2] : 0;
edgeTessellationFactor3 = value.Length >= 4 ? value [3] : 0;
}
}

public ushort [] InsideTessellationFactor {
get => new ushort [] { insideTessellationFactor0, insideTessellationFactor1 };
set {
if (value.Length > 2)
throw new ArgumentOutOfRangeException ($"The '{nameof (value)}' array length can't be greater than 2.");

insideTessellationFactor0 = value.Length >= 1 ? value [0] : 0;
insideTessellationFactor1 = value.Length >= 2 ? value [1] : 0;
}
}
#else
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 4)]
public ushort [] EdgeTessellationFactor;
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 2)]
public ushort [] InsideTessellationFactor;
#endif

public MTLQuadTessellationFactorsHalf (ushort [] edgeTessellationFactor, ushort [] insideTessellationFactor)
{
if (edgeTessellationFactor.Length > 4)
throw new ArgumentOutOfRangeException ($"The '{nameof (edgeTessellationFactor)}' array length can't be greater than 4.");

if (insideTessellationFactor.Length > 2)
throw new ArgumentOutOfRangeException ($"The '{nameof (insideTessellationFactor)}' array length can't be greater than 2.");
#if XAMCORE_5_0
edgeTessellationFactor0 = edgeTessellationFactor.Length >= 1 ? edgeTessellationFactor [0] : 0;
edgeTessellationFactor1 = edgeTessellationFactor.Length >= 2 ? edgeTessellationFactor [1] : 0;
edgeTessellationFactor2 = edgeTessellationFactor.Length >= 3 ? edgeTessellationFactor [2] : 0;
edgeTessellationFactor3 = edgeTessellationFactor.Length >= 4 ? edgeTessellationFactor [3] : 0;
insideTessellationFactor0 = insideTessellationFactor.Length >= 1 ? insideTessellationFactor [0] : 0;
insideTessellationFactor1 = insideTessellationFactor.Length >= 2 ? insideTessellationFactor [1] : 0;
#else
EdgeTessellationFactor = edgeTessellationFactor;
InsideTessellationFactor = insideTessellationFactor;
#endif
}

}
Expand All @@ -441,13 +488,38 @@ public MTLQuadTessellationFactorsHalf (ushort [] edgeTessellationFactor, ushort
#endif
[StructLayout (LayoutKind.Sequential)]
public struct MTLTriangleTessellationFactorsHalf {
#if XAMCORE_5_0
ushort edgeTessellationFactor0;
ushort edgeTessellationFactor1;
ushort edgeTessellationFactor2;

public ushort [] EdgeTessellationFactor {
get => new ushort [] { edgeTessellationFactor0, edgeTessellationFactor1, edgeTessellationFactor2 };
set {
if (value.Length > 3)
throw new ArgumentOutOfRangeException ($"The '{nameof (value)}' array length can't be greater than 3.");
edgeTessellationFactor0 = value.Length >= 1 ? value [0] : 0;
edgeTessellationFactor1 = value.Length >= 2 ? value [1] : 0;
edgeTessellationFactor2 = value.Length >= 3 ? value [2] : 0;
}
}
#else
[MarshalAs (UnmanagedType.ByValArray, SizeConst = 3)]
public ushort [] EdgeTessellationFactor;
#endif
public ushort InsideTessellationFactor;

public MTLTriangleTessellationFactorsHalf (ushort [] edgeTessellationFactor, ushort insideTessellationFactor)
{
if (edgeTessellationFactor.Length > 3)
throw new ArgumentOutOfRangeException ($"The '{nameof (edgeTessellationFactor)}' array length can't be greater than 3.");
#if XAMCORE_5_0
edgeTessellationFactor0 = edgeTessellationFactor.Length >= 1 ? edgeTessellationFactor [0] : 0;
edgeTessellationFactor1 = edgeTessellationFactor.Length >= 2 ? edgeTessellationFactor [1] : 0;
edgeTessellationFactor2 = edgeTessellationFactor.Length >= 3 ? edgeTessellationFactor [2] : 0;
#else
EdgeTessellationFactor = edgeTessellationFactor;
#endif
InsideTessellationFactor = insideTessellationFactor;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Network/NWFramer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace Network {

public delegate nuint NWFramerParseCompletionDelegate (Memory<byte> buffer, [MarshalAs (UnmanagedType.I1)] bool isCompleted);
public delegate nuint NWFramerParseCompletionDelegate (Memory<byte> buffer, bool isCompleted);
public delegate nuint NWFramerInputDelegate (NWFramer framer);

#if NET
Expand Down
5 changes: 2 additions & 3 deletions src/ObjCRuntime/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,9 @@ internal static void FreeStringPtrs (IntPtr [] ptrs)
}
}

[StructLayout (LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct objc_attribute_prop {
[MarshalAs (UnmanagedType.LPStr)] internal string name;
[MarshalAs (UnmanagedType.LPStr)] internal string value;
internal string name;
internal string value;
}
#endif // !COREBUILD
}
Expand Down
6 changes: 0 additions & 6 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public partial class BlittablePInvokes {
static HashSet<string> knownFailuresMarshalAs = new (); // shouldn't have any failures here in XAMCORE_5_0.
#else
static HashSet<string> knownFailuresMarshalAs = new HashSet<string> {
"For the method ImageIO.CGImageMetadataTagBlock::EndInvoke(System.IAsyncResult) the return type has a [MarshalAs] attribute",
"For the method ImageIO.CGImageMetadataTagBlock::Invoke(Foundation.NSString,ImageIO.CGImageMetadataTag) the return type has a [MarshalAs] attribute",
"For the method Network.NWFramerParseCompletionDelegate::BeginInvoke(System.Memory`1<System.Byte>,System.Boolean,System.AsyncCallback,System.Object) the parameter #1 (isCompleted) has a [MarshalAs] attribute",
"For the method Network.NWFramerParseCompletionDelegate::Invoke(System.Memory`1<System.Byte>,System.Boolean) the parameter #1 (isCompleted) has a [MarshalAs] attribute",
"The field AVFoundation.AVSampleCursorAudioDependencyInfo.IsIndependentlyDecodable has a [MarshalAs] attribute",
"The field AVFoundation.AVSampleCursorChunkInfo.HasUniformFormatDescriptions has a [MarshalAs] attribute",
"The field AVFoundation.AVSampleCursorChunkInfo.HasUniformSampleDurations has a [MarshalAs] attribute",
Expand All @@ -49,8 +45,6 @@ public partial class BlittablePInvokes {
"The field Metal.MTLQuadTessellationFactorsHalf.EdgeTessellationFactor has a [MarshalAs] attribute",
"The field Metal.MTLQuadTessellationFactorsHalf.InsideTessellationFactor has a [MarshalAs] attribute",
"The field Metal.MTLTriangleTessellationFactorsHalf.EdgeTessellationFactor has a [MarshalAs] attribute",
"The field ObjCRuntime.Class/objc_attribute_prop.name has a [MarshalAs] attribute",
"The field ObjCRuntime.Class/objc_attribute_prop.value has a [MarshalAs] attribute",
};
#endif
static HashSet<string> knownFailuresPInvokes = new HashSet<string> {
Expand Down

8 comments on commit 9c2bd68

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