Skip to content

Commit

Permalink
Merge Metal APIs from main branch (#1598)
Browse files Browse the repository at this point in the history
* Remove the externals/harfbuzz submodule (#1599)
* Add Metal APIs for macOS and iOS (#1394)
* docs: Add unoplatform references (#1602)
* Add a symlink for a "missing" file
  • Loading branch information
mattleibow authored Feb 2, 2021
1 parent 8450b68 commit 0cd7fe5
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 51 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SkiaSharp

[![SkiaSharp](https://img.shields.io/nuget/vpre/SkiaSharp.svg?maxAge=2592000&label=SkiaSharp%20nuget)](https://www.nuget.org/packages/SkiaSharp) [![SkiaSharp.Views](https://img.shields.io/nuget/vpre/SkiaSharp.Views.svg?maxAge=2592000&label=SkiaSharp.Views%20nuget)](https://www.nuget.org/packages/SkiaSharp.Views) [![SkiaSharp.Views.Forms](https://img.shields.io/nuget/vpre/SkiaSharp.Views.Forms.svg?maxAge=2592000&label=SkiaSharp.Views.Forms%20nuget)](https://www.nuget.org/packages/SkiaSharp.Views.Forms) [![HarfBuzzSharp](https://img.shields.io/nuget/vpre/HarfBuzzSharp.svg?maxAge=2592000&label=HarfBuzzSharp%20nuget)](https://www.nuget.org/packages/HarfBuzzSharp)
[![SkiaSharp](https://img.shields.io/nuget/vpre/SkiaSharp.svg?maxAge=2592000&label=SkiaSharp%20nuget)](https://www.nuget.org/packages/SkiaSharp) [![SkiaSharp.Views](https://img.shields.io/nuget/vpre/SkiaSharp.Views.svg?maxAge=2592000&label=SkiaSharp.Views%20nuget)](https://www.nuget.org/packages/SkiaSharp.Views) [![SkiaSharp.Views.Forms](https://img.shields.io/nuget/vpre/SkiaSharp.Views.Forms.svg?maxAge=2592000&label=SkiaSharp.Views.Forms%20nuget)](https://www.nuget.org/packages/SkiaSharp.Views.Forms) [![HarfBuzzSharp](https://img.shields.io/nuget/vpre/HarfBuzzSharp.svg?maxAge=2592000&label=HarfBuzzSharp%20nuget)](https://www.nuget.org/packages/HarfBuzzSharp) [![SkiaSharp.Views.Uno](https://img.shields.io/nuget/vpre/SkiaSharp.Views.Uno.svg?maxAge=2592000&label=SkiaSharp.Views.Uno%20nuget)](https://www.nuget.org/packages/SkiaSharp.Views.Uno)
[![chat](https://img.shields.io/badge/chat-xamarin%2FXamarinComponents-E60256.svg)](https://gitter.im/xamarin/XamarinComponents) [![SkiaSharp API Docs](https://img.shields.io/badge/docs-skiasharp-1faece.svg)](https://docs.microsoft.com/dotnet/api/SkiaSharp) [![HarfBuzzSharp API Docs](https://img.shields.io/badge/docs-harfbuzzsharp-1faece.svg)](https://docs.microsoft.com/dotnet/api/SkiaSharp) [![SkiaSharp Guides](https://img.shields.io/badge/docs-guides-1faece.svg)](https://docs.microsoft.com/xamarin/graphics-games/skiasharp/)
[![Build Status](https://dev.azure.com/devdiv/DevDiv/_apis/build/status/Xamarin/Components/SkiaSharp?branchName=master)](https://dev.azure.com/devdiv/DevDiv/_build/latest?definitionId=10789&branchName=master) [![Build Status](https://dev.azure.com/xamarin/public/_apis/build/status/mono/SkiaSharp/SkiaSharp%20(Public)?branchName=master)](https://dev.azure.com/xamarin/public/_build/latest?definitionId=4&branchName=master)

Expand All @@ -21,6 +21,7 @@ SkiaSharp provides cross-platform bindings for:
- Windows Classic Desktop (Windows.Forms / WPF)
- Windows UWP (Desktop / Mobile / Xbox / HoloLens)
- Web Assembly (WASM)
- Uno Platform (iOS / macOS / Android / WebAssembly / UWP)

The [API Documentation](https://docs.microsoft.com/en-us/dotnet/api/SkiaSharp/) is
available on the web to browse.
Expand Down
2 changes: 2 additions & 0 deletions binding/Binding/Definitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public enum SKColorType
Alpha16 = 16,
Rg1616 = 17,
Rgba16161616 = 18,
Bgra1010102 = 19,
Bgr101010x = 20,
}

public static partial class SkiaExtensions
Expand Down
7 changes: 7 additions & 0 deletions binding/Binding/EnumMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum GRBackend
OpenGL = 1,
Vulkan = 2,
Dawn = 3,
Direct3D = 4,
}

[EditorBrowsable (EditorBrowsableState.Never)]
Expand Down Expand Up @@ -61,6 +62,7 @@ internal static GRBackendNative ToNative (this GRBackend backend) =>
GRBackend.OpenGL => GRBackendNative.OpenGL,
GRBackend.Vulkan => GRBackendNative.Vulkan,
GRBackend.Dawn => GRBackendNative.Dawn,
GRBackend.Direct3D => GRBackendNative.Direct3D,
_ => throw new ArgumentOutOfRangeException (nameof (backend)),
};

Expand All @@ -71,6 +73,7 @@ internal static GRBackend FromNative (this GRBackendNative backend) =>
GRBackendNative.OpenGL => GRBackend.OpenGL,
GRBackendNative.Vulkan => GRBackend.Vulkan,
GRBackendNative.Dawn => GRBackend.Dawn,
GRBackendNative.Direct3D => GRBackend.Direct3D,
_ => throw new ArgumentOutOfRangeException (nameof (backend)),
};

Expand All @@ -96,6 +99,8 @@ internal static SKColorTypeNative ToNative (this SKColorType colorType) =>
SKColorType.Alpha16 => SKColorTypeNative.A16Unorm,
SKColorType.Rg1616 => SKColorTypeNative.R16g16Unorm,
SKColorType.Rgba16161616 => SKColorTypeNative.R16g16b16a16Unorm,
SKColorType.Bgra1010102 => SKColorTypeNative.Bgra1010102,
SKColorType.Bgr101010x => SKColorTypeNative.Bgr101010x,
_ => throw new ArgumentOutOfRangeException (nameof (colorType)),
};

Expand All @@ -121,6 +126,8 @@ internal static SKColorType FromNative (this SKColorTypeNative colorType) =>
SKColorTypeNative.A16Unorm => SKColorType.Alpha16,
SKColorTypeNative.R16g16Unorm => SKColorType.Rg1616,
SKColorTypeNative.R16g16b16a16Unorm => SKColorType.Rgba16161616,
SKColorTypeNative.Bgra1010102 => SKColorType.Bgra1010102,
SKColorTypeNative.Bgr101010x => SKColorType.Bgr101010x,
_ => throw new ArgumentOutOfRangeException (nameof (colorType)),
};
}
Expand Down
15 changes: 15 additions & 0 deletions binding/Binding/GRBackendRenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public GRBackendRenderTarget (int width, int height, int sampleCount, GRVkImageI
CreateVulkan (width, height, sampleCount, vkImageInfo);
}

#if __IOS__ || __MACOS__

public GRBackendRenderTarget (int width, int height, int sampleCount, GRMetalTextureInfo mtlInfo)
: this (IntPtr.Zero, true)
{
var info = mtlInfo.ToNative ();
Handle = SkiaApi.gr_backendrendertarget_new_metal (width, height, sampleCount, &info);

if (Handle == IntPtr.Zero) {
throw new InvalidOperationException ("Unable to create a new GRBackendRenderTarget instance.");
}
}

#endif

private void CreateGl (int width, int height, int sampleCount, int stencilBits, GRGlFramebufferInfo glInfo)
{
Handle = SkiaApi.gr_backendrendertarget_new_gl (width, height, sampleCount, stencilBits, &glInfo);
Expand Down
15 changes: 15 additions & 0 deletions binding/Binding/GRBackendTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public GRBackendTexture (int width, int height, GRVkImageInfo vkInfo)
CreateVulkan (width, height, vkInfo);
}

#if __IOS__ || __MACOS__

public GRBackendTexture (int width, int height, bool mipmapped, GRMetalTextureInfo mtlInfo)
: this (IntPtr.Zero, true)
{
var info = mtlInfo.ToNative ();
Handle = SkiaApi.gr_backendtexture_new_metal (width, height, mipmapped, &info);

if (Handle == IntPtr.Zero) {
throw new InvalidOperationException ("Unable to create a new GRBackendTexture instance.");
}
}

#endif

private void CreateGl (int width, int height, bool mipmapped, GRGlTextureInfo glInfo)
{
Handle = SkiaApi.gr_backendtexture_new_gl (width, height, mipmapped, &glInfo);
Expand Down
16 changes: 16 additions & 0 deletions binding/Binding/GRContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ public static GRContext CreateVulkan (GRVkBackendContext backendContext)
return GetObject (SkiaApi.gr_direct_context_make_vulkan (backendContext.ToNative ()));
}

#if __IOS__ || __MACOS__

// CreateMetal

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

return GetObject (SkiaApi.gr_direct_context_make_metal ((void*)device.Handle, (void*)queue.Handle));
}

#endif

//

public GRBackend Backend => SkiaApi.gr_direct_context_get_backend (Handle).FromNative ();
Expand Down
38 changes: 38 additions & 0 deletions binding/Binding/GRDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,44 @@ public GRGlTextureInfo (uint target, uint id, uint format)
}
}

#if __IOS__ || __MACOS__

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

public Metal.IMTLTexture Texture { get; set; }

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

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

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

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

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

public readonly override int GetHashCode ()
{
var hash = new HashCode ();
hash.Add (Texture);
return hash.ToHashCode ();
}
}

#endif

[EditorBrowsable (EditorBrowsableState.Never)]
[Flags]
[Obsolete]
Expand Down
21 changes: 21 additions & 0 deletions binding/Binding/SKSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,24 @@ public static SKSurface Create (GRContext context, bool budgeted, SKImageInfo in
return GetObject (SkiaApi.sk_surface_new_render_target (context.Handle, budgeted, &cinfo, sampleCount, origin, props?.Handle ?? IntPtr.Zero, shouldCreateWithMips));
}

#if __MACOS__ || __IOS__

public static SKSurface Create (GRContext context, CoreAnimation.CAMetalLayer layer, GRSurfaceOrigin origin, int sampleCount, SKColorType colorType, out CoreAnimation.ICAMetalDrawable drawable) =>
Create (context, layer, origin, sampleCount, colorType, null, null, out drawable);

public static SKSurface Create (GRContext context, CoreAnimation.CAMetalLayer layer, GRSurfaceOrigin origin, int sampleCount, SKColorType colorType, SKColorSpace colorspace, out CoreAnimation.ICAMetalDrawable drawable) =>
Create (context, layer, origin, sampleCount, colorType, colorspace, null, out drawable);

public static SKSurface Create (GRContext context, CoreAnimation.CAMetalLayer layer, GRSurfaceOrigin origin, int sampleCount, SKColorType colorType, SKColorSpace colorspace, SKSurfaceProperties props, out CoreAnimation.ICAMetalDrawable drawable)
{
void* drawablePtr;
var surface = GetObject (SkiaApi.sk_surface_new_metal_layer (context.Handle, (void*)layer.Handle, origin, sampleCount, colorType.ToNative (), colorspace?.Handle ?? IntPtr.Zero, props?.Handle ?? IntPtr.Zero, &drawablePtr));
drawable = ObjCRuntime.Runtime.GetINativeObject<CoreAnimation.ICAMetalDrawable> ((IntPtr)drawablePtr, true);
return surface;
}

#endif

// NULL surface

public static SKSurface CreateNull (int width, int height) =>
Expand Down Expand Up @@ -363,6 +381,9 @@ public bool ReadPixels (SKImageInfo dstInfo, IntPtr dstPixels, int dstRowBytes,
return result;
}

public void Flush () =>
SkiaApi.sk_surface_flush (Handle);

internal static SKSurface GetObject (IntPtr handle) =>
handle == IntPtr.Zero ? null : new SKSurface (handle, true);
}
Expand Down
Loading

0 comments on commit 0cd7fe5

Please sign in to comment.