|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Runtime.InteropServices; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Drawing.Processing.Backends; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Attaches a Core Animation metal layer to a Cocoa window for WebGPU presentation. |
| 10 | +/// </summary> |
| 11 | +internal static partial class MacOSMetalLayerFactory |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// The Objective-C runtime library used to construct and attach the Core Animation layer. |
| 15 | + /// </summary> |
| 16 | + private const string ObjectiveCLibrary = "/usr/lib/libobjc.A.dylib"; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Creates and attaches a metal layer to a Cocoa window. |
| 20 | + /// </summary> |
| 21 | + /// <param name="nsWindow">The Cocoa <c>NSWindow*</c>.</param> |
| 22 | + /// <returns>The attached <c>CAMetalLayer*</c>.</returns> |
| 23 | + /// <remarks> |
| 24 | + /// The returned layer carries the ownership acquired by <c>alloc/init</c>. Call <see cref="Release"/> after |
| 25 | + /// wgpu-native has created its surface; the AppKit content view retains the attached layer independently. |
| 26 | + /// </remarks> |
| 27 | + public static nint Create(nint nsWindow) |
| 28 | + { |
| 29 | + nint metalLayerClass = GetClass("CAMetalLayer"); |
| 30 | + nint metalLayer = SendPointer(SendPointer(metalLayerClass, RegisterSelector("alloc")), RegisterSelector("init")); |
| 31 | + nint contentView = SendPointer(nsWindow, RegisterSelector("contentView")); |
| 32 | + |
| 33 | + // WebGPU presents through a CAMetalLayer. AppKit creates a default backing layer only |
| 34 | + // after wantsLayer is enabled, so install the explicitly-created metal layer afterwards. |
| 35 | + SendByte(contentView, RegisterSelector("setWantsLayer:"), 1); |
| 36 | + SendPointer(contentView, RegisterSelector("setLayer:"), metalLayer); |
| 37 | + return metalLayer; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Releases the factory's ownership of a metal layer after its AppKit view has retained it. |
| 42 | + /// </summary> |
| 43 | + /// <param name="metalLayer">The <c>CAMetalLayer*</c> to release.</param> |
| 44 | + public static void Release(nint metalLayer) |
| 45 | + => SendVoid(metalLayer, RegisterSelector("release")); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Resolves an Objective-C class by name. |
| 49 | + /// </summary> |
| 50 | + /// <param name="name">The Objective-C class name.</param> |
| 51 | + /// <returns>The Objective-C class object.</returns> |
| 52 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "objc_getClass", StringMarshalling = StringMarshalling.Utf8)] |
| 53 | + private static partial nint GetClass(string name); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Registers an Objective-C selector by name. |
| 57 | + /// </summary> |
| 58 | + /// <param name="name">The selector name.</param> |
| 59 | + /// <returns>The registered selector.</returns> |
| 60 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "sel_registerName", StringMarshalling = StringMarshalling.Utf8)] |
| 61 | + private static partial nint RegisterSelector(string name); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Sends a parameterless Objective-C message that returns an object pointer. |
| 65 | + /// </summary> |
| 66 | + /// <param name="receiver">The receiving object or class.</param> |
| 67 | + /// <param name="selector">The selector to invoke.</param> |
| 68 | + /// <returns>The returned object pointer.</returns> |
| 69 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "objc_msgSend")] |
| 70 | + private static partial nint SendPointer(nint receiver, nint selector); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Sends an Objective-C message with one object-pointer argument and no return value. |
| 74 | + /// </summary> |
| 75 | + /// <param name="receiver">The receiving object.</param> |
| 76 | + /// <param name="selector">The selector to invoke.</param> |
| 77 | + /// <param name="value">The object pointer passed to the selector.</param> |
| 78 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "objc_msgSend")] |
| 79 | + private static partial void SendPointer(nint receiver, nint selector, nint value); |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Sends an Objective-C message with one Boolean-sized argument and no return value. |
| 83 | + /// </summary> |
| 84 | + /// <param name="receiver">The receiving object.</param> |
| 85 | + /// <param name="selector">The selector to invoke.</param> |
| 86 | + /// <param name="value">The Objective-C <c>BOOL</c> value passed to the selector.</param> |
| 87 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "objc_msgSend")] |
| 88 | + private static partial void SendByte(nint receiver, nint selector, byte value); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Sends a parameterless Objective-C message with no return value. |
| 92 | + /// </summary> |
| 93 | + /// <param name="receiver">The receiving object.</param> |
| 94 | + /// <param name="selector">The selector to invoke.</param> |
| 95 | + [LibraryImport(ObjectiveCLibrary, EntryPoint = "objc_msgSend")] |
| 96 | + private static partial void SendVoid(nint receiver, nint selector); |
| 97 | +} |
0 commit comments