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

[CoreVideo] Make P/Invokes in CVBuffer and CVDisplayLink have blittable signatures. #20494

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
22 changes: 17 additions & 5 deletions src/CoreVideo/CVBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CoreFoundation;
using ObjCRuntime;
Expand Down Expand Up @@ -109,7 +110,13 @@ public void RemoveAttachment (NSString key)
[Deprecated (PlatformName.WatchOS, 8, 0)]
#endif
[DllImport (Constants.CoreVideoLibrary)]
extern static /* CFTypeRef */ IntPtr CVBufferGetAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key, out CVAttachmentMode attachmentMode);
unsafe extern static /* CFTypeRef */ IntPtr CVBufferGetAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key, CVAttachmentMode* attachmentMode);

unsafe static /* CFTypeRef */ IntPtr CVBufferGetAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key, out CVAttachmentMode attachmentMode)
{
attachmentMode = default;
return CVBufferGetAttachment (buffer, key, (CVAttachmentMode*) Unsafe.AsPointer<CVAttachmentMode> (ref attachmentMode));
}

// The new method is the same as the old one but changing the ownership from Get to Copy, so we will use the new version if possible since the
// older method has been deprecatd.
Expand All @@ -126,7 +133,13 @@ public void RemoveAttachment (NSString key)
[MacCatalyst (15, 0)]
#endif
[DllImport (Constants.CoreVideoLibrary)]
extern static /* CFTypeRef */ IntPtr CVBufferCopyAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key, out CVAttachmentMode attachmentMode);
unsafe extern static /* CFTypeRef */ IntPtr CVBufferCopyAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key, CVAttachmentMode* attachmentMode);

unsafe static IntPtr CVBufferCopyAttachment (IntPtr buffer, IntPtr key, out CVAttachmentMode attachmentMode)
{
attachmentMode = default;
return CVBufferCopyAttachment (buffer, key, (CVAttachmentMode*) Unsafe.AsPointer<CVAttachmentMode> (ref attachmentMode));
}

// FIXME: we need to bring the new API to xamcore
#if !MONOMAC
Expand Down Expand Up @@ -257,8 +270,7 @@ public void SetAttachments (NSDictionary theAttachments, CVAttachmentMode attach
[Watch (8, 0)]
#endif
[DllImport (Constants.CoreVideoLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
static extern bool CVBufferHasAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key);
static extern byte CVBufferHasAttachment (/* CVBufferRef */ IntPtr buffer, /* CFStringRef */ IntPtr key);

#if NET
[SupportedOSPlatform ("ios15.0")]
Expand All @@ -276,7 +288,7 @@ public bool HasAttachment (NSString key)
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));
return CVBufferHasAttachment (Handle, key.Handle);
return CVBufferHasAttachment (Handle, key.Handle) != 0;
}

#endif // !COREBUILD
Expand Down
51 changes: 34 additions & 17 deletions src/CoreVideo/CVDisplayLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

using CoreFoundation;
Expand Down Expand Up @@ -72,7 +73,7 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
[NoMacCatalyst]
#endif
[DllImport (Constants.CoreVideoLibrary)]
static extern CVReturn CVDisplayLinkCreateWithCGDisplay (uint displayId, out IntPtr displayLink);
unsafe static extern CVReturn CVDisplayLinkCreateWithCGDisplay (uint displayId, IntPtr* displayLink);

#if NET
[SupportedOSPlatform ("macos12.0")]
Expand All @@ -87,7 +88,10 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
#endif
public static CVDisplayLink? CreateFromDisplayId (uint displayId, out CVReturn error)
{
error = CVDisplayLinkCreateWithCGDisplay (displayId, out IntPtr handle);
IntPtr handle;
unsafe {
error = CVDisplayLinkCreateWithCGDisplay (displayId, &handle);
}
if (error != 0)
return null;

Expand Down Expand Up @@ -120,7 +124,7 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
[NoMacCatalyst]
#endif
[DllImport (Constants.CoreVideoLibrary)]
static extern CVReturn CVDisplayLinkCreateWithCGDisplays (uint[] displayArray, nint count, out IntPtr displayLink);
unsafe static extern CVReturn CVDisplayLinkCreateWithCGDisplays (uint* displayArray, nint count, IntPtr* displayLink);

#if NET
[SupportedOSPlatform ("macos12.0")]
Expand All @@ -139,7 +143,11 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (displayIds));
error = 0;
IntPtr handle = IntPtr.Zero;
error = CVDisplayLinkCreateWithCGDisplays (displayIds, displayIds.Length, out handle);
unsafe {
fixed (uint* displayArrayPtrs = displayIds) {
error = CVDisplayLinkCreateWithCGDisplays (displayArrayPtrs, displayIds.Length, &handle);
}
}

if (error != 0)
return null;
Expand Down Expand Up @@ -173,7 +181,7 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
[NoMacCatalyst]
#endif
[DllImport (Constants.CoreVideoLibrary)]
static extern CVReturn CVDisplayLinkCreateWithOpenGLDisplayMask (uint mask, out IntPtr displayLinkOut);
unsafe static extern CVReturn CVDisplayLinkCreateWithOpenGLDisplayMask (uint mask, IntPtr* displayLinkOut);

#if NET
[SupportedOSPlatform ("macos12.0")]
Expand All @@ -188,7 +196,10 @@ internal CVDisplayLink (NativeHandle handle, bool owns)
#endif
public static CVDisplayLink? CreateFromOpenGLMask (uint mask, out CVReturn error)
{
error = CVDisplayLinkCreateWithOpenGLDisplayMask (mask, out IntPtr handle);
IntPtr handle;
unsafe {
error = CVDisplayLinkCreateWithOpenGLDisplayMask (mask, &handle);
}
if (error != 0)
return null;
return new CVDisplayLink (handle, true);
Expand Down Expand Up @@ -234,10 +245,14 @@ protected override void Dispose (bool disposing)
}

[DllImport (Constants.CoreVideoLibrary)]
extern static CVReturn CVDisplayLinkCreateWithActiveCGDisplays (out IntPtr displayLinkOut);
unsafe extern static CVReturn CVDisplayLinkCreateWithActiveCGDisplays (IntPtr* displayLinkOut);
static IntPtr Create ()
{
var ret = CVDisplayLinkCreateWithActiveCGDisplays (out var handle);
CVReturn ret;
IntPtr handle;
unsafe {
ret = CVDisplayLinkCreateWithActiveCGDisplays (&handle);
}

if (ret != CVReturn.Success)
throw new Exception ("CVDisplayLink returned: " + ret);
Expand Down Expand Up @@ -310,19 +325,22 @@ public CVReturn Stop ()
}

[DllImport (Constants.CoreVideoLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool CVDisplayLinkIsRunning (IntPtr displayLink);
extern static byte CVDisplayLinkIsRunning (IntPtr displayLink);
public bool IsRunning {
get {
return CVDisplayLinkIsRunning (Handle);
return CVDisplayLinkIsRunning (Handle) != 0;
}
}

[DllImport (Constants.CoreVideoLibrary)]
extern static CVReturn CVDisplayLinkGetCurrentTime (IntPtr displayLink, out CVTimeStamp outTime);
unsafe extern static CVReturn CVDisplayLinkGetCurrentTime (IntPtr displayLink, CVTimeStamp* outTime);
public CVReturn GetCurrentTime (out CVTimeStamp outTime)
{
CVReturn ret = CVDisplayLinkGetCurrentTime (this.Handle, out outTime);
CVReturn ret;
outTime = default;
unsafe {
ret = CVDisplayLinkGetCurrentTime (this.Handle, (CVTimeStamp *) Unsafe.AsPointer<CVTimeStamp> (ref outTime));
}

return ret;
}
Expand Down Expand Up @@ -416,7 +434,7 @@ public static nuint GetTypeId ()
[NoMacCatalyst]
#endif
[DllImport (Constants.CoreVideoLibrary)]
static extern int CVDisplayLinkTranslateTime (IntPtr displayLink, CVTimeStamp inTime, ref CVTimeStamp outTime);
unsafe static extern int CVDisplayLinkTranslateTime (IntPtr displayLink, CVTimeStamp inTime, CVTimeStamp* outTime);

#if NET
[SupportedOSPlatform ("macos12.0")]
Expand All @@ -431,10 +449,9 @@ public static nuint GetTypeId ()
#endif
public bool TryTranslateTime (CVTimeStamp inTime, ref CVTimeStamp outTime)
{
if (CVDisplayLinkTranslateTime (this.Handle, inTime, ref outTime) == 0) {
return true;
unsafe {
return CVDisplayLinkTranslateTime (this.Handle, inTime, (CVTimeStamp *) Unsafe.AsPointer<CVTimeStamp> (ref outTime)) == 0;
}
return false;
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ public partial class BlittablePInvokes {
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSend(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithActiveCGDisplays(System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplay(System.UInt32,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithCGDisplays(System.UInt32[],System.IntPtr,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkCreateWithOpenGLDisplayMask(System.UInt32,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVDisplayLink::CVDisplayLinkGetCurrentTime(System.IntPtr,CoreVideo.CVTimeStamp&)",
"CoreVideo.CVReturn CoreVideo.CVMetalTextureCache::CVMetalTextureCacheCreateTextureFromImage(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.UIntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVPixelBuffer::CVPixelBufferCreate(System.IntPtr,System.IntPtr,System.IntPtr,CoreVideo.CVPixelFormatType,System.IntPtr,System.IntPtr&)",
"CoreVideo.CVReturn CoreVideo.CVPixelBuffer::CVPixelBufferCreateResolvedAttributesDictionary(System.IntPtr,System.IntPtr,System.IntPtr&)",
Expand Down Expand Up @@ -158,8 +153,6 @@ public partial class BlittablePInvokes {
"Security.SslStatus Security.SslContext::SSLRead(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"Security.SslStatus Security.SslContext::SSLSetSessionOption(System.IntPtr,Security.SslSessionOption,System.Boolean)",
"Security.SslStatus Security.SslContext::SSLWrite(System.IntPtr,System.Byte*,System.IntPtr,System.IntPtr&)",
"System.Boolean CoreVideo.CVBuffer::CVBufferHasAttachment(System.IntPtr,System.IntPtr)",
"System.Boolean CoreVideo.CVDisplayLink::CVDisplayLinkIsRunning(System.IntPtr)",
"System.Boolean CoreVideo.CVImageBuffer::CVImageBufferIsFlipped(System.IntPtr)",
"System.Boolean CoreVideo.CVMetalTexture::CVMetalTextureIsFlipped(System.IntPtr)",
"System.Boolean CoreVideo.CVPixelBuffer::CVPixelBufferIsPlanar(System.IntPtr)",
Expand Down Expand Up @@ -248,16 +241,13 @@ public partial class BlittablePInvokes {
"System.Int32 AudioUnit.AudioUnit::AudioUnitGetProperty(System.IntPtr,AudioUnit.AudioUnitPropertyIDType,AudioUnit.AudioUnitScopeType,System.UInt32,System.UInt32&,System.UInt32&)",
"System.Int32 AudioUnit.AudioUnit::AudioUnitSetProperty(System.IntPtr,AudioUnit.AudioUnitPropertyIDType,AudioUnit.AudioUnitScopeType,System.UInt32,AudioToolbox.AudioStreamBasicDescription&,System.UInt32)",
"System.Int32 AudioUnit.AUGraph::NewAUGraph(System.IntPtr&)",
"System.Int32 CoreVideo.CVDisplayLink::CVDisplayLinkTranslateTime(System.IntPtr,CoreVideo.CVTimeStamp,CoreVideo.CVTimeStamp&)",
"System.Int32 CoreVideo.CVMetalTextureCache::CVMetalTextureCacheCreate(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)",
"System.Int32 ObjCRuntime.Runtime::_NSGetExecutablePath(System.Byte[],System.Int32&)",
"System.Int32 Security.Authorization::AuthorizationCreate(Security.AuthorizationItemSet*,Security.AuthorizationItemSet*,Security.AuthorizationFlags,System.IntPtr&)",
"System.Int32 Security.SecCertificate::SecCertificateCopyCommonName(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SecCertificate::SecCertificateCopyEmailAddresses(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferCopyAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr CoreVideo.CVBuffer::CVBufferGetAttachment(System.IntPtr,System.IntPtr,CoreVideo.CVAttachmentMode&)",
"System.IntPtr Foundation.NSSearchPath::NSSearchPathForDirectoriesInDomains(System.UIntPtr,System.UIntPtr,System.Boolean)",
"System.IntPtr Foundation.NSThread::xamarin_init_nsthread(System.IntPtr,System.Boolean,System.IntPtr,System.IntPtr,System.IntPtr)",
"System.IntPtr GameController.GCExtendedGamepadSnapshotData::NSDataFromGCExtendedGamepadSnapshotData(GameController.GCExtendedGamepadSnapshotData&)",
Expand Down
Loading