xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.49k stars 515 forks source link

Can't share or open `IOSurface` cross process #18389

Open thenextman opened 1 year ago

thenextman commented 1 year ago

Steps to Reproduce

  1. Try to use any function to share an IOSurface cross-process on macOS

Expected Behavior

The functions to share and lookup IOSurface should be exported; e.g.

IOSurfaceCreateMachPort IOSurfaceCreateXPCObject IOSurfaceLookupFromMachPort IOSurfaceLookupFromXPCObject

Ideally, I'd also expect to be able to call the deprecated function IOSurfaceLookup. I can manually use IOSurfaceIsGlobal and IOSurfaceGetID, but how do I call the lookup? P/invoking it with a return type of IntPtr isn't helpful when I need an IOSurface to pass to other functions.

Actual Behavior

The functions are not available.

Environment

Visual Studio Professional 2022 for Mac Version 17.5.3 (build 15) Installation UUID: 4693e2b8-e552-4d2a-9994-cef0d37548ad

Runtime .NET 7.0.1 (64-bit) Architecture: Arm64 Microsoft.macOS.Sdk 12.3.2372; git-rev-head:754abbf6a3563f6267e5717ae832b4ac25b1f2fb; git-branch:release/7.0.1xx-xcode13.3

Roslyn (Language Service) 4.5.0-3.23056.2+97881342e427ff5cdcba8f12b12ff8e6f3564431

NuGet Version: 6.4.0.117

.NET SDK (Arm64) SDK: /usr/local/share/dotnet/sdk/7.0.202/Sdks SDK Versions: 7.0.202 7.0.200 7.0.102 7.0.101 7.0.100 6.0.407 6.0.406 6.0.405 6.0.404 6.0.403 6.0.402 6.0.401 6.0.302 6.0.300 6.0.101 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET SDK (x64) SDK Versions: 6.0.407 6.0.406 6.0.405 6.0.404 6.0.403 6.0.402 6.0.401 6.0.302 6.0.105 6.0.102 6.0.101 5.0.408 5.0.405 5.0.404 3.1.426 3.1.425 3.1.424 3.1.423 3.1.421 3.1.419 3.1.416

.NET Runtime (Arm64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.4 7.0.3 7.0.2 7.0.1 7.0.0 6.0.15 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.7 6.0.5 6.0.1

.NET Runtime (x64) Runtime: /usr/local/share/dotnet/x64/dotnet Runtime Versions: 6.0.15 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.7 6.0.5 6.0.2 6.0.1 5.0.17 5.0.14 5.0.13 3.1.32 3.1.31 3.1.30 3.1.29 3.1.27 3.1.25 3.1.22

Xamarin.Profiler Version: 1.8.0.49 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

Updater Version: 11

Apple Developer Tools Xcode: 14.2 21534 Build: 14C18

Xamarin.Mac Version: 9.1.0.5 Visual Studio Professional Hash: 7738c90c9 Branch: xcode14.2 Build date: 2023-01-25 15:56:14-0500

Xamarin.iOS Version: 16.2.0.5 Visual Studio Professional Hash: 7738c90c9 Branch: xcode14.2 Build date: 2023-01-25 15:56:15-0500

Xamarin Designer Version: 17.5.3.47 Hash: e8b5d371c3 Branch: remotes/origin/d17-5 Build date: 2023-03-23 18:30:22 UTC

Xamarin.Android Version: 13.2.0.0 (Visual Studio Professional) Commit: xamarin-android/d17-5/797e2e1 Android SDK: /Users/rmarkiewicz/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 12.0 (API level 31) 11.0 (API level 30) 10.0 (API level 29) 9.0 (API level 28) 13.0 (API level 33)

SDK Command-line Tools Version: 7.0 SDK Platform Tools Version: 33.0.2 SDK Build Tools Version: 32.0.0

Build Information: Mono: 6dd9def Java.Interop: xamarin/java.interop/main@149d70fe SQLite: xamarin/sqlite/3.40.0@fdc1e34 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@9f02d77

Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.16.1 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager Version: 17.5.0.33 Hash: f0c0c52 Branch: remotes/origin/d17-5~2 Build date: 2023-03-23 18:30:27 UTC

Android Device Manager Version: 0.0.0.1245 Hash: 7f8a990 Branch: 7f8a990 Build date: 2023-03-23 18:30:27 UTC

Build Information Release ID: 1705030015 Git revision: c7ec3826dd11045a53fa7f2c08ddbd6bd475c05b Build date: 2023-03-23 18:28:33+00 Build branch: release-17.5 Build lane: release-17.5

Operating System Mac OS X 13.4.0 Darwin 22.5.0 Darwin Kernel Version 22.5.0 Mon Apr 24 20:52:24 PDT 2023 root:xnu-8796.121.2~5/RELEASE_ARM64_T6000 arm64

Enabled user installed extensions Project System Tools 0.12

rolfbjarne commented 1 year ago

The DllImports and helper functions would be something like this:

using System.Runtime.InteropServices:
using ObjCRuntime;

static class IOSurfaceHelpers {
    [DllImport ("/System/Library/Frameworks/IOSurface.framework/IOSurface")]
    // mach_port_t IOSurfaceCreateMachPort(IOSurfaceRef buffer);
    static extern /* mach_port_t */ uint IOSurfaceCreateMachPort (/* IOSurfaceRef */ IntPtr buffer);

    [DllImport ("/System/Library/Frameworks/IOSurface.framework/IOSurface")]
    // xpc_object_t IOSurfaceCreateXPCObject(IOSurfaceRef aSurface);
    static extern /* xpc_object_t */ IntPtr IOSurfaceCreateXPCObject (/* IOSurfaceRef */ aSurface);

    [DllImport ("/System/Library/Frameworks/IOSurface.framework/IOSurface")]
    // IOSurfaceRef IOSurfaceLookupFromMachPort(mach_port_t port);
    static extern /* IOSurfaceRef */ IntPtr IOSurfaceLookupFromMachPort (/* mach_port_t */ uint port);

    [DllImport ("/System/Library/Frameworks/IOSurface.framework/IOSurface")]
    // IOSurfaceRef IOSurfaceLookupFromXPCObject(xpc_object_t xobj);
    static extern /* IOSurfaceRef */ IntPtr IOSurfaceLookupFromXPCObject (/* xpc_object_t */ IntPtr xobj);

    [DllImport ("/System/Library/Frameworks/IOSurface.framework/IOSurface")]
    // IOSurfaceRef IOSurfaceLookup(IOSurfaceID csid);
    static extern /*  IOSurfaceRef */ IntPtr IOSurfaceLookup (/* IOSurfaceID */ int csid);

    public static uint CreateMachPort (this IOSurface buffer)
    {
        return IOSurfaceCreateMachPort (buffer.Handle);
    }

    public static NSObject CreateXpcObject (this IOSurface aSurface)
    {
        var rv = IOSurfaceCreateXPCObject (aSurface.Handle);
        return Runtime.GetNSObject (rv);
    }

    public static IOSurface LookupFromMachPort (uint port)
    {
        var rv = IOSurfaceLookupFromMachPort (port);
        return (IOSurface) Runtime.GetNSObject (rv);
    }

    public static IOSurface LookupFromXpcObject (NSObject xobj)
    {
        var rv = IOSurfaceCreateXPCObject (xobj.Handle);
        return (IOSurface) Runtime.GetNSObject (rv);
    }

    public static IOSurface Lookup (int csid)
    {
        var rv = IOSurfaceLookup (csid);
        return (IOSurface) Runtime.GetNSObject (rv);
    }
}

Can you try these and see if it works for you?

thenextman commented 1 year ago

Hi @rolfbjarne

This is working great, thank you!

rolfbjarne commented 1 year ago

Reopening, since we'll want to add these APIs at some point.