diff --git a/binding/SkiaSharp/GRBackendTexture.cs b/binding/SkiaSharp/GRBackendTexture.cs index 603731f457..254cc3c708 100644 --- a/binding/SkiaSharp/GRBackendTexture.cs +++ b/binding/SkiaSharp/GRBackendTexture.cs @@ -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) { @@ -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); diff --git a/binding/SkiaSharp/GRContext.cs b/binding/SkiaSharp/GRContext.cs index 70a50e67a4..55071e4187 100644 --- a/binding/SkiaSharp/GRContext.cs +++ b/binding/SkiaSharp/GRContext.cs @@ -63,8 +63,6 @@ public static GRContext CreateVulkan (GRVkBackendContext backendContext, GRConte } } -#if __IOS__ || __MACOS__ - // CreateMetal public static GRContext CreateMetal (GRMtlBackendContext backendContext) => @@ -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; diff --git a/binding/SkiaSharp/GRDefinitions.cs b/binding/SkiaSharp/GRDefinitions.cs index 54c9a46ad9..304f479386 100644 --- a/binding/SkiaSharp/GRDefinitions.cs +++ b/binding/SkiaSharp/GRDefinitions.cs @@ -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); @@ -105,13 +129,11 @@ public readonly override bool Equals (object obj) => 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) => diff --git a/binding/SkiaSharp/GRMtlBackendContext.cs b/binding/SkiaSharp/GRMtlBackendContext.cs index aba9670b9a..9c2aa28112 100644 --- a/binding/SkiaSharp/GRMtlBackendContext.cs +++ b/binding/SkiaSharp/GRMtlBackendContext.cs @@ -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) { @@ -23,4 +63,3 @@ public void Dispose () } } } -#endif