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

Contributes towards #15684.
This commit is contained in:
Rolf Bjarne Kvinge 2024-04-24 12:01:43 +02:00 коммит произвёл GitHub
Родитель 45449a0c4b
Коммит cb34348455
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 51 добавлений и 32 удалений

Просмотреть файл

@ -29,6 +29,7 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CoreFoundation;
using ObjCRuntime;
@ -109,7 +110,13 @@ namespace CoreVideo {
[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.
@ -126,7 +133,13 @@ namespace CoreVideo {
[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
@ -257,8 +270,7 @@ namespace CoreVideo {
[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")]
@ -276,7 +288,7 @@ namespace CoreVideo {
{
if (key is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (key));
return CVBufferHasAttachment (Handle, key.Handle);
return CVBufferHasAttachment (Handle, key.Handle) != 0;
}
#endif // !COREBUILD

Просмотреть файл

@ -27,6 +27,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using CoreFoundation;
@ -72,7 +73,7 @@ namespace CoreVideo {
[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")]
@ -87,7 +88,10 @@ namespace CoreVideo {
#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;
@ -120,7 +124,7 @@ namespace CoreVideo {
[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")]
@ -139,7 +143,11 @@ namespace CoreVideo {
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;
@ -173,7 +181,7 @@ namespace CoreVideo {
[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")]
@ -188,7 +196,10 @@ namespace CoreVideo {
#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);
@ -234,10 +245,14 @@ namespace CoreVideo {
}
[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);
@ -310,19 +325,22 @@ namespace CoreVideo {
}
[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;
}
@ -416,7 +434,7 @@ namespace CoreVideo {
[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")]
@ -431,10 +449,9 @@ namespace CoreVideo {
#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;
}
}
}

Просмотреть файл

@ -64,11 +64,6 @@ namespace Cecil.Tests {
"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&)",
@ -158,8 +153,6 @@ namespace Cecil.Tests {
"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)",
@ -248,7 +241,6 @@ namespace Cecil.Tests {
"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&)",
@ -256,8 +248,6 @@ namespace Cecil.Tests {
"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&)",