Skip to content

Commit 5d4bff5

Browse files
authored
[AppKit] Make NSGraphics.NSBestDepth P/Invoke blittable. (#19614)
Also introduce a few other improvements: * Add an varation that takes an 'out bool' instead of a 'ref bool'. According to the documentation the value is out-only. * Name this variation according to our guidelines (with a verb). * Deprecate the old version. Contributes towards #15684.
1 parent 8eb8788 commit 5d4bff5

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/AppKit/NSGraphics.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#if !__MACCATALYST__
2525

2626
using System;
27+
using System.ComponentModel;
2728
using System.Runtime.InteropServices;
2829

2930
using ObjCRuntime;
@@ -47,14 +48,38 @@ public static class NSGraphics {
4748
public static readonly float DarkGray = (float) 1 / 3.0f;
4849

4950
[DllImport (Constants.AppKitLibrary)]
50-
extern static NSWindowDepth NSBestDepth (IntPtr colorspaceHandle, nint bitsPerSample, nint bitsPerPixel, [MarshalAs (UnmanagedType.I1)] bool planar, [MarshalAs (UnmanagedType.I1)] ref bool exactMatch);
51+
extern unsafe static NSWindowDepth NSBestDepth (IntPtr colorspaceHandle, nint bitsPerSample, nint bitsPerPixel, byte planar, byte* exactMatch);
5152

52-
public static NSWindowDepth BestDepth (NSString colorspace, nint bitsPerSample, nint bitsPerPixel, [MarshalAs (UnmanagedType.I1)] bool planar, [MarshalAs (UnmanagedType.I1)] ref bool exactMatch)
53+
#if !XAMCORE_5_0
54+
[EditorBrowsable (EditorBrowsableState.Never)]
55+
[Obsolete ("Call 'GetBestDepth' instead.")]
56+
public static NSWindowDepth BestDepth (NSString colorspace, nint bitsPerSample, nint bitsPerPixel, bool planar, ref bool exactMatch)
5357
{
5458
if (colorspace is null)
55-
throw new ArgumentNullException ("colorspace");
59+
throw new ArgumentNullException (nameof (colorspace));
5660

57-
return NSBestDepth (colorspace.Handle, bitsPerSample, bitsPerPixel, planar, ref exactMatch);
61+
var exactMatchValue = (byte) (exactMatch ? 1 : 0);
62+
NSWindowDepth rv;
63+
unsafe {
64+
rv = NSBestDepth (colorspace.Handle, bitsPerSample, bitsPerPixel, (byte) (planar ? 1 : 0), &exactMatchValue);
65+
}
66+
exactMatch = exactMatchValue != 0;
67+
return rv;
68+
}
69+
#endif
70+
71+
public static NSWindowDepth GetBestDepth (NSString colorspace, nint bitsPerSample, nint bitsPerPixel, bool planar, out bool exactMatch)
72+
{
73+
if (colorspace is null)
74+
throw new ArgumentNullException (nameof (colorspace));
75+
76+
byte exactMatchValue = 0;
77+
NSWindowDepth rv;
78+
unsafe {
79+
rv = NSBestDepth (colorspace.Handle, bitsPerSample, bitsPerPixel, (byte) (planar ? 1 : 0), &exactMatchValue);
80+
}
81+
exactMatch = exactMatchValue != 0;
82+
return rv;
5883
}
5984

6085
[DllImport (Constants.AppKitLibrary)]

tests/cecil-tests/BlittablePInvokes.KnownFailures.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
namespace Cecil.Tests {
1818
public partial class BlittablePInvokes {
1919
static HashSet<string> knownFailuresPInvokes = new HashSet<string> {
20-
"AppKit.NSWindowDepth AppKit.NSGraphics::NSBestDepth(System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean,System.Boolean&)",
2120
"AudioToolbox.AudioConverterError AudioToolbox.AudioConverter::AudioConverterGetPropertyInfo(System.IntPtr,AudioToolbox.AudioConverterPropertyID,System.Int32&,System.Boolean&)",
2221
"AudioToolbox.AudioFileError AudioToolbox.AudioFile::AudioFileWritePackets(System.IntPtr,System.Boolean,System.Int32,AudioToolbox.AudioStreamPacketDescription[],System.Int64,System.Int32&,System.IntPtr)",
2322
"AudioToolbox.AudioFileStreamStatus AudioToolbox.AudioFileStream::AudioFileStreamGetPropertyInfo(System.IntPtr,AudioToolbox.AudioFileStreamProperty,System.Int32&,System.Boolean&)",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if __MACOS__
2+
using System;
3+
using System.Threading.Tasks;
4+
using NUnit.Framework;
5+
6+
using AppKit;
7+
using Foundation;
8+
9+
namespace Xamarin.Mac.Tests {
10+
[TestFixture]
11+
[Preserve (AllMembers = true)]
12+
public class NSGraphicsTest {
13+
#if !XAMCORE_5_0
14+
[Test]
15+
public void BestDepth ()
16+
{
17+
bool exactMatch = false;
18+
var rv = NSGraphics.BestDepth (NSColorSpace.DeviceRGB, 8, 8, false, ref exactMatch);
19+
Assert.AreEqual (NSWindowDepth.TwentyfourBitRgb, rv, "BestDepth");
20+
Assert.IsTrue (exactMatch, "ExactMatch");
21+
}
22+
#endif
23+
24+
[Test]
25+
public void GetBestDepth ()
26+
{
27+
var rv = NSGraphics.GetBestDepth (NSColorSpace.DeviceRGB, 8, 8, false, out var exactMatch);
28+
Assert.AreEqual (NSWindowDepth.TwentyfourBitRgb, rv, "GetBestDepth");
29+
Assert.IsTrue (exactMatch, "ExactMatch");
30+
}
31+
}
32+
}
33+
34+
#endif // __MACOS__

0 commit comments

Comments
 (0)