Skip to content

Commit

Permalink
[CoreGraphics] Xcode 14 Beta 1-5 (#15831)
Browse files Browse the repository at this point in the history
**Note:** This PR is almost entirely of manual (but simple) bindings, and the documentation of these CG methods is non-existent. I had to add add MarshalAs and change pointers to IntPtr by hand, so please review carefully. 

I wrote "did not crash/return null" manual tests, which I could improve upon if desired, but I'd have to re-learn some matrix math and figure out what each method does under the hood (Alex thought this would likely be fine).

A few important notes:

- CGAffineTransformComponents is in CoreFoundation as it was defined (but not used) in those API diffs. There is a define `CF_DEFINES_CGAFFINETRANSFORMCOMPONENTS` which allows CoreGraphics to declare it in the headers, but I'm assuming that is a hack for Apple.
- The headers/docs for kCGColorSpaceITUR_709_HLG lie and claim it's in older OS than it seems to be, so I assumed latest instead.

Co-authored-by: Rolf Bjarne Kvinge <[email protected]>
  • Loading branch information
chamons and rolfbjarne authored Sep 2, 2022
1 parent 2170040 commit e43e05c
Show file tree
Hide file tree
Showing 19 changed files with 477 additions and 118 deletions.
31 changes: 31 additions & 0 deletions src/CoreFoundation/CGAffineTransformComponents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 Microsoft Corporation.

#nullable enable

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

using CoreGraphics;

namespace CoreFoundation {
#if NET
[SupportedOSPlatform ("ios")]
[SupportedOSPlatform ("maccatalyst")]
[SupportedOSPlatform ("macos")]
[SupportedOSPlatform ("tvos")]
#endif
[StructLayout (LayoutKind.Sequential)]
// The name prefix suggests CoreGraphics and based on CF_DEFINES_CGAFFINETRANSFORMCOMPONENTS
// it could be defined in CoreGraphics but documented as CoreFoundation type
public struct CGAffineTransformComponents
{
public CGSize Scale;

public nfloat HorizontalShear;

public nfloat Rotation;

public CGVector Translation;
}
}
32 changes: 32 additions & 0 deletions src/CoreGraphics/CGAffineTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,38 @@ public CGAffineTransform Invert ()
{
return CGAffineTransformInvert (this);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern CGAffineTransformComponents CGAffineTransformDecompose (CGAffineTransform transform);

public CGAffineTransformComponents Decompose ()
{
return CGAffineTransformDecompose (this);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern CGAffineTransform CGAffineTransformMakeWithComponents (CGAffineTransformComponents components);

public static CGAffineTransform MakeWithComponents (CGAffineTransformComponents components)
{
return CGAffineTransformMakeWithComponents (components);
}
#endif // !COREBUILD
}
}
27 changes: 27 additions & 0 deletions src/CoreGraphics/CGColorSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,33 @@ public bool UsesExtendedRange {
#endif
public CGColorSpace? CreateExtendedLinearized () => Runtime.GetINativeObject<CGColorSpace> (CGColorSpaceCreateExtendedLinearized (Handle), owns: true);

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("tvos16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
#else
[iOS (16,0)]
[TV (16,0)]
[Mac (13,0)]
[MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGColorSpaceCreateCopyWithStandardRange (/* CGColorSpaceRef */ IntPtr s);

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("tvos16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
#else
[iOS (16,0)]
[TV (16,0)]
[Mac (13,0)]
[MacCatalyst (16,0)]
#endif
public CGColorSpace? CreateCopyWithStandardRange () => Runtime.GetINativeObject<CGColorSpace> (CGColorSpaceCreateCopyWithStandardRange (Handle), owns: true);

#if NET
[SupportedOSPlatform ("macos12.0")]
[SupportedOSPlatform ("ios15.0")]
Expand Down
24 changes: 24 additions & 0 deletions src/CoreGraphics/CGPDFScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,29 @@ public bool TryPop (out CGPDFStream? value)
return false;
}
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
extern static void CGPDFScannerStop (/* CGPDFScannerRef */ IntPtr scanner);

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
public void Stop ()
{
CGPDFScannerStop (Handle);
}
}
}
167 changes: 167 additions & 0 deletions src/CoreGraphics/CGPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#nullable enable

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

using CoreFoundation;
Expand Down Expand Up @@ -529,6 +532,170 @@ public void Apply (ApplierFunction func)
gch.Free ();
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByNormalizing (IntPtr path, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateByNormalizing (bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyByNormalizing (Handle, evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByUnioningPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateByUnioningPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyByUnioningPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByIntersectingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateByIntersectingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyBySubtractingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateBySubtractingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyBySymmetricDifferenceOfPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateBySymmetricDifferenceOfPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyBySymmetricDifferenceOfPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyOfLineBySubtractingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateLineBySubtractingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyOfLineBySubtractingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyOfLineByIntersectingPath (IntPtr path, IntPtr maskPath, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath? CreateLineByIntersectingPath (CGPath? maskPath, bool evenOddFillRule)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyOfLineByIntersectingPath (Handle, maskPath.GetHandle (), evenOddFillRule), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern unsafe /* CFArrayRef __nullable */ IntPtr CGPathCreateSeparateComponents (IntPtr path, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public CGPath[] GetSeparateComponents (bool evenOddFillRule)
{
var cfArrayRef = CGPathCreateSeparateComponents (Handle, evenOddFillRule);
if (cfArrayRef == IntPtr.Zero)
return Array.Empty<CGPath> ();
return NSArray.ArrayFromHandle<CGPath> (cfArrayRef);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
static extern IntPtr CGPathCreateCopyByFlattening (IntPtr path, nfloat flatteningThreshold);

public CGPath? CreateByFlattening (nfloat flatteningThreshold)
{
return Runtime.GetINativeObject<CGPath>(CGPathCreateCopyByFlattening (Handle, flatteningThreshold), owns: true);
}

#if NET
[SupportedOSPlatform ("ios16.0")]
[SupportedOSPlatform ("maccatalyst16.0")]
[SupportedOSPlatform ("macos13.0")]
[SupportedOSPlatform ("tvos16.0")]
#else
[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
#endif
[DllImport (Constants.CoreGraphicsLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool CGPathIntersectsPath (IntPtr path1, IntPtr path2, [MarshalAs (UnmanagedType.I1)] bool evenOddFillRule);

public bool DoesIntersect (CGPath? maskPath, bool evenOddFillRule)
{
return CGPathIntersectsPath (Handle, maskPath.GetHandle (), evenOddFillRule);
}

static CGPath MakeMutable (IntPtr source, bool owns)
{
var mutable = CGPathCreateMutableCopy (source);
Expand Down
4 changes: 4 additions & 0 deletions src/coregraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ interface CGColorSpaceNames {
[Field ("kCGColorSpaceITUR_709_PQ")]
NSString ItuR_709_PQ { get; }

[Mac (13,0), iOS (16,0), TV (16,0), MacCatalyst (16,0)]
[Field ("kCGColorSpaceITUR_709_HLG")]
NSString ItuR_709_Hlg { get; }

[Mac (10,11)]
[Field ("kCGColorSpaceITUR_2020")]
NSString ItuR_2020 { get; }
Expand Down
1 change: 1 addition & 0 deletions src/frameworks.sources
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ COREFOUNDATION_CORE_SOURCES = \
CoreFoundation/DispatchData.cs \
CoreFoundation/NativeObject.cs \
CoreFoundation/Architecture.cs \
CoreFoundation/CGAffineTransformComponents.cs \

COREFOUNDATION_SOURCES = \
CoreFoundation/CFAllocator.cs \
Expand Down
33 changes: 33 additions & 0 deletions tests/monotouch-test/CoreGraphics/AffineTransformTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.InteropServices;
using Foundation;
using CoreGraphics;
using CoreFoundation;
using ObjCRuntime;

using NUnit.Framework;
Expand Down Expand Up @@ -499,6 +500,38 @@ public void Invert ()
#endif
}

[Test]
public void Decompose ()
{
TestRuntime.AssertXcodeVersion (14, 0);

var components = new CGAffineTransform (1, 2, 3, 4, 5, 6).Decompose ();
Assert.AreNotEqual (0.0, components.Scale);
Assert.AreNotEqual (0.0, components.HorizontalShear);
Assert.AreNotEqual (0.0, components.Rotation);
Assert.AreNotEqual (new CGVector ((nfloat)0, (nfloat)0), components.Translation);
}

[Test]
public void MakeWithComponents ()
{
TestRuntime.AssertXcodeVersion (14, 0);

var components = new CGAffineTransformComponents () {
Scale = new CGSize (1.0, 2.0),
HorizontalShear = (nfloat)3.0,
Rotation = (nfloat)4.0,
Translation = new CGVector ((nfloat)5.0, (nfloat)6.0),
};
var transform = CGAffineTransform.MakeWithComponents (components);
Assert.AreNotEqual (0.0, transform.A);
Assert.AreNotEqual (0.0, transform.B);
Assert.AreNotEqual (0.0, transform.C);
Assert.AreNotEqual (0.0, transform.D);
Assert.AreNotEqual (0.0, transform.Tx);
Assert.AreNotEqual (0.0, transform.Ty);
}

[Test]
public void NSValueRoundtrip ()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/monotouch-test/CoreGraphics/ColorSpaceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ public void CreateExtendedLinearizedTest ()
Assert.That ((nint) TestRuntime.CFGetRetainCount (csl.Handle), Is.EqualTo ((nint) 1).Or.EqualTo ((nint) 2));
}
}

[Test]
public void CreateCopyWithStandardRange ()
{
TestRuntime.AssertXcodeVersion (14, 0);
using var cs = CGColorSpace.CreateWithName (CGColorSpaceNames.GenericRgb);
Assert.NotNull (cs.CreateCopyWithStandardRange());
}

[Test]
public void IsHlgBasedTest ()
Expand Down
Loading

3 comments on commit e43e05c

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Build failed 🔥

Build failed for the job 'Build packages'

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 [CI Build] Build failed 🔥

Build failed for the job 'Detect API changes'

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: e43e05c298f482a3a708ee56fe9a802e507bdc0f [CI build]

Please sign in to comment.