Skip to content

Commit

Permalink
Bring Metal APIs to a common .NET TFM (#2788)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
maxkatz6 and mattleibow committed Apr 8, 2024
1 parent 647403d commit 63141bf
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
4 changes: 0 additions & 4 deletions binding/SkiaSharp/GRBackendTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public GRBackendTexture (int width, int height, GRVkImageInfo vkInfo)
CreateVulkan (width, height, vkInfo);
}

#if __IOS__ || __MACOS__

public GRBackendTexture (int width, int height, bool mipmapped, GRMtlTextureInfo mtlInfo)
: this (IntPtr.Zero, true)
{
Expand All @@ -38,8 +36,6 @@ public GRBackendTexture (int width, int height, bool mipmapped, GRMtlTextureInfo
}
}

#endif

private void CreateGl (int width, int height, bool mipmapped, GRGlTextureInfo glInfo)
{
Handle = SkiaApi.gr_backendtexture_new_gl (width, height, mipmapped, &glInfo);
Expand Down
12 changes: 4 additions & 8 deletions binding/SkiaSharp/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ public static GRContext CreateVulkan (GRVkBackendContext backendContext, GRConte
}
}

#if __IOS__ || __MACOS__

// CreateMetal

public static GRContext CreateMetal (GRMtlBackendContext backendContext) =>
Expand All @@ -75,19 +73,17 @@ public static GRContext CreateMetal (GRMtlBackendContext backendContext, GRConte
if (backendContext == null)
throw new ArgumentNullException (nameof (backendContext));

var device = backendContext.Device;
var queue = backendContext.Queue;
var device = backendContext.DeviceHandle;
var queue = backendContext.QueueHandle;

if (options == null) {
return GetObject (SkiaApi.gr_direct_context_make_metal ((void*)(IntPtr)device.Handle, (void*)(IntPtr)queue.Handle));
return GetObject (SkiaApi.gr_direct_context_make_metal ((void*)device, (void*)queue));
} else {
var opts = options.ToNative ();
return GetObject (SkiaApi.gr_direct_context_make_metal_with_options ((void*)(IntPtr)device.Handle, (void*)(IntPtr)queue.Handle, &opts));
return GetObject (SkiaApi.gr_direct_context_make_metal_with_options ((void*)device, (void*)queue, &opts));
}
}

#endif

//

public override GRBackend Backend => base.Backend;
Expand Down
38 changes: 30 additions & 8 deletions binding/SkiaSharp/GRDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,48 @@ public GRGlTextureInfo (uint target, uint id, uint format)
}
}

#if __IOS__ || __MACOS__

public unsafe partial struct GRMtlTextureInfo
{
private IntPtr _textureHandle;

public GRMtlTextureInfo (IntPtr textureHandle)
{
TextureHandle = textureHandle;
}

public IntPtr TextureHandle {
get => _textureHandle;
set {
_textureHandle = value;
#if __IOS__ || __MACOS__
_texture = null;
#endif
}
}

#if __IOS__ || __MACOS__
private Metal.IMTLTexture _texture;
public GRMtlTextureInfo (Metal.IMTLTexture texture)
{
Texture = texture;
}

public Metal.IMTLTexture Texture { get; set; }
public Metal.IMTLTexture Texture {
get => _texture;
set {
_texture = value;
_textureHandle = _texture.Handle;
}
}
#endif

internal GRMtlTextureInfoNative ToNative () =>
new GRMtlTextureInfoNative {
fTexture = (void*)(IntPtr)Texture.Handle
fTexture = (void*)TextureHandle
};

public readonly bool Equals (GRMtlTextureInfo obj) =>
Texture == obj.Texture;
TextureHandle == obj.TextureHandle;

public readonly override bool Equals (object obj) =>
obj is GRMtlTextureInfo f && Equals (f);
Expand All @@ -105,13 +129,11 @@ public GRMtlTextureInfo (Metal.IMTLTexture texture)
public readonly override int GetHashCode ()
{
var hash = new HashCode ();
hash.Add (Texture);
hash.Add (TextureHandle);
return hash.ToHashCode ();
}
}

#endif

public static partial class SkiaExtensions
{
public static uint ToGlSizedFormat (this SKColorType colorType) =>
Expand Down
47 changes: 43 additions & 4 deletions binding/SkiaSharp/GRMtlBackendContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
#nullable disable

#if __IOS__ || __MACOS__
using System;
#if __IOS__ || __MACOS__
using Metal;
#endif

namespace SkiaSharp
{
public class GRMtlBackendContext : IDisposable
{
public IMTLDevice Device { get; set; }
private IntPtr _deviceHandle, _queueHandle;

public IntPtr DeviceHandle {
get => _deviceHandle;
set {
_deviceHandle = value;
#if __IOS__ || __MACOS__
_device = null;
#endif
}
}

public IntPtr QueueHandle {
get => _queueHandle;
set {
_queueHandle = value;
#if __IOS__ || __MACOS__
_queue = null;
#endif
}
}

public IMTLCommandQueue Queue { get; set; }
#if __IOS__ || __MACOS__
private IMTLDevice _device;
private IMTLCommandQueue _queue;

public IMTLDevice Device {
get => _device;
set {
_device = value;
_deviceHandle = _device.Handle;
}
}

public IMTLCommandQueue Queue {
get => _queue;
set {
_queue = value;
_queueHandle = _queue.Handle;
}
}
#endif

protected virtual void Dispose (bool disposing)
{
Expand All @@ -23,4 +63,3 @@ public void Dispose ()
}
}
}
#endif

0 comments on commit 63141bf

Please sign in to comment.