Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce some metal consistency #1615

Merged
merged 1 commit into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binding/Binding/GRBackendRenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GRBackendRenderTarget (int width, int height, int sampleCount, GRVkImageI

#if __IOS__ || __MACOS__

public GRBackendRenderTarget (int width, int height, int sampleCount, GRMetalTextureInfo mtlInfo)
public GRBackendRenderTarget (int width, int height, int sampleCount, GRMtlTextureInfo mtlInfo)
: this (IntPtr.Zero, true)
{
var info = mtlInfo.ToNative ();
Expand Down
2 changes: 1 addition & 1 deletion binding/Binding/GRBackendTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public GRBackendTexture (int width, int height, GRVkImageInfo vkInfo)

#if __IOS__ || __MACOS__

public GRBackendTexture (int width, int height, bool mipmapped, GRMetalTextureInfo mtlInfo)
public GRBackendTexture (int width, int height, bool mipmapped, GRMtlTextureInfo mtlInfo)
: this (IntPtr.Zero, true)
{
var info = mtlInfo.ToNative ();
Expand Down
15 changes: 8 additions & 7 deletions binding/Binding/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ public static GRContext CreateVulkan (GRVkBackendContext backendContext, GRConte

// CreateMetal

public static GRContext CreateMetal (Metal.IMTLDevice device, Metal.IMTLCommandQueue queue) =>
CreateMetal (device, queue, null);
public static GRContext CreateMetal (GRMtlBackendContext backendContext) =>
CreateMetal (backendContext, null);

public static GRContext CreateMetal (Metal.IMTLDevice device, Metal.IMTLCommandQueue queue, GRContextOptions options)
public static GRContext CreateMetal (GRMtlBackendContext backendContext, GRContextOptions options)
{
if (device == null)
throw new ArgumentNullException (nameof (device));
if (queue == null)
throw new ArgumentNullException (nameof (queue));
if (backendContext == null)
throw new ArgumentNullException (nameof (backendContext));

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

if (options == null) {
return GetObject (SkiaApi.gr_context_make_metal ((void*)device.Handle, (void*)queue.Handle));
Expand Down
16 changes: 8 additions & 8 deletions binding/Binding/GRDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,30 @@ public GRGlTextureInfo (uint target, uint id, uint format)

#if __IOS__ || __MACOS__

public unsafe partial struct GRMetalTextureInfo
public unsafe partial struct GRMtlTextureInfo
{
public GRMetalTextureInfo (Metal.IMTLTexture texture)
public GRMtlTextureInfo (Metal.IMTLTexture texture)
{
Texture = texture;
}

public Metal.IMTLTexture Texture { get; set; }

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

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

public readonly override bool Equals (object obj) =>
obj is GRMetalTextureInfo f && Equals (f);
obj is GRMtlTextureInfo f && Equals (f);

public static bool operator == (GRMetalTextureInfo left, GRMetalTextureInfo right) =>
public static bool operator == (GRMtlTextureInfo left, GRMtlTextureInfo right) =>
left.Equals (right);

public static bool operator != (GRMetalTextureInfo left, GRMetalTextureInfo right) =>
public static bool operator != (GRMtlTextureInfo left, GRMtlTextureInfo right) =>
!left.Equals (right);

public readonly override int GetHashCode ()
Expand Down
24 changes: 24 additions & 0 deletions binding/Binding/GRMtlBackendContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if __IOS__ || __MACOS__
using System;
using Metal;

namespace SkiaSharp
{
public class GRMtlBackendContext : IDisposable
{
public IMTLDevice Device { get; set; }

public IMTLCommandQueue Queue { get; set; }

protected virtual void Dispose (bool disposing)
{
}

public void Dispose ()
{
Dispose (disposing: true);
GC.SuppressFinalize (this);
}
}
}
#endif
22 changes: 11 additions & 11 deletions binding/Binding/SkiaApi.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ private partial class Delegates {
// gr_backendrendertarget_t* gr_backendrendertarget_new_metal(int width, int height, int samples, const gr_mtl_textureinfo_t* mtlInfo)
#if !USE_DELEGATES
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
internal static extern gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMetalTextureInfoNative* mtlInfo);
internal static extern gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMtlTextureInfoNative* mtlInfo);
#else
private partial class Delegates {
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
internal delegate gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMetalTextureInfoNative* mtlInfo);
internal delegate gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMtlTextureInfoNative* mtlInfo);
}
private static Delegates.gr_backendrendertarget_new_metal gr_backendrendertarget_new_metal_delegate;
internal static gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMetalTextureInfoNative* mtlInfo) =>
internal static gr_backendrendertarget_t gr_backendrendertarget_new_metal (Int32 width, Int32 height, Int32 samples, GRMtlTextureInfoNative* mtlInfo) =>
(gr_backendrendertarget_new_metal_delegate ??= GetSymbol<Delegates.gr_backendrendertarget_new_metal> ("gr_backendrendertarget_new_metal")).Invoke (width, height, samples, mtlInfo);
#endif

Expand Down Expand Up @@ -370,14 +370,14 @@ private partial class Delegates {
// gr_backendtexture_t* gr_backendtexture_new_metal(int width, int height, bool mipmapped, const gr_mtl_textureinfo_t* mtlInfo)
#if !USE_DELEGATES
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
internal static extern gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMetalTextureInfoNative* mtlInfo);
internal static extern gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMtlTextureInfoNative* mtlInfo);
#else
private partial class Delegates {
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
internal delegate gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMetalTextureInfoNative* mtlInfo);
internal delegate gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMtlTextureInfoNative* mtlInfo);
}
private static Delegates.gr_backendtexture_new_metal gr_backendtexture_new_metal_delegate;
internal static gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMetalTextureInfoNative* mtlInfo) =>
internal static gr_backendtexture_t gr_backendtexture_new_metal (Int32 width, Int32 height, [MarshalAs (UnmanagedType.I1)] bool mipmapped, GRMtlTextureInfoNative* mtlInfo) =>
(gr_backendtexture_new_metal_delegate ??= GetSymbol<Delegates.gr_backendtexture_new_metal> ("gr_backendtexture_new_metal")).Invoke (width, height, mipmapped, mtlInfo);
#endif

Expand Down Expand Up @@ -13423,20 +13423,20 @@ public unsafe partial struct GRGlTextureInfo : IEquatable<GRGlTextureInfo> {

// gr_mtl_textureinfo_t
[StructLayout (LayoutKind.Sequential)]
internal unsafe partial struct GRMetalTextureInfoNative : IEquatable<GRMetalTextureInfoNative> {
internal unsafe partial struct GRMtlTextureInfoNative : IEquatable<GRMtlTextureInfoNative> {
// public const void* fTexture
public void* fTexture;

public readonly bool Equals (GRMetalTextureInfoNative obj) =>
public readonly bool Equals (GRMtlTextureInfoNative obj) =>
fTexture == obj.fTexture;

public readonly override bool Equals (object obj) =>
obj is GRMetalTextureInfoNative f && Equals (f);
obj is GRMtlTextureInfoNative f && Equals (f);

public static bool operator == (GRMetalTextureInfoNative left, GRMetalTextureInfoNative right) =>
public static bool operator == (GRMtlTextureInfoNative left, GRMtlTextureInfoNative right) =>
left.Equals (right);

public static bool operator != (GRMetalTextureInfoNative left, GRMetalTextureInfoNative right) =>
public static bool operator != (GRMtlTextureInfoNative left, GRMtlTextureInfoNative right) =>
!left.Equals (right);

public readonly override int GetHashCode ()
Expand Down
2 changes: 1 addition & 1 deletion binding/libSkiaSharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"internal": true
},
"gr_mtl_textureinfo_t": {
"cs": "GRMetalTextureInfoNative",
"cs": "GRMtlTextureInfoNative",
"internal": true
},
"gr_context_options_t": {
Expand Down
18 changes: 11 additions & 7 deletions source/SkiaSharp.Views/SkiaSharp.Views.Apple/SKMetalView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SKMetalView : MTKView, IMTKViewDelegate, IComponent

private bool designMode;

private IMTLCommandQueue commandQueue;
private GRMtlBackendContext backendContext;
private GRContext context;

// created in code
Expand Down Expand Up @@ -84,9 +84,13 @@ private void Initialize()
DepthStencilPixelFormat = MTLPixelFormat.Depth32Float_Stencil8;
SampleCount = 1;
Device = device;
commandQueue = device.CreateCommandQueue();
backendContext = new GRMtlBackendContext
{
Device = device,
Queue = device.CreateCommandQueue(),
};

// hook up the drawing
// hook up the drawing
Delegate = this;
}

Expand All @@ -111,7 +115,7 @@ void IMTKViewDelegate.Draw(MTKView view)
if (designMode)
return;

if (Device == null || commandQueue == null || CurrentDrawable?.Texture == null)
if (backendContext.Device == null || backendContext.Queue == null || CurrentDrawable?.Texture == null)
return;

CanvasSize = DrawableSize.ToSKSize();
Expand All @@ -120,13 +124,13 @@ void IMTKViewDelegate.Draw(MTKView view)
return;

// create the contexts if not done already
context ??= GRContext.CreateMetal(Device, commandQueue);
context ??= GRContext.CreateMetal(backendContext);

const SKColorType colorType = SKColorType.Bgra8888;
const GRSurfaceOrigin surfaceOrigin = GRSurfaceOrigin.TopLeft;

// create the render target
var metalInfo = new GRMetalTextureInfo(CurrentDrawable.Texture);
var metalInfo = new GRMtlTextureInfo(CurrentDrawable.Texture);
using var renderTarget = new GRBackendRenderTarget((int)CanvasSize.Width, (int)CanvasSize.Height, (int)SampleCount, metalInfo);

// create the surface
Expand All @@ -143,7 +147,7 @@ void IMTKViewDelegate.Draw(MTKView view)
context.Flush();

// present
using var commandBuffer = commandQueue.CommandBuffer();
using var commandBuffer = backendContext.Queue.CommandBuffer();
commandBuffer.PresentDrawable(CurrentDrawable);
commandBuffer.Commit();
}
Expand Down