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

Merge Metal APIs from main branch #1598

Merged
merged 28 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f3f0d44
Add Metal
mattleibow Jul 10, 2020
2115f62
Update externals
mattleibow Jul 10, 2020
c5eb1c6
Changes
mattleibow Jul 11, 2020
a0e2c59
undos
mattleibow Jul 11, 2020
ded3586
dsfgdsf 
mattleibow Jul 11, 2020
488cbde
Merge branch 'master' into dev/metal
mattleibow Jul 19, 2020
e14b92b
Merge branch 'master' into dev/metal
mattleibow Jul 19, 2020
cba19ad
Merge branch 'master' into dev/metal
mattleibow Jul 21, 2020
a1a7b18
regen
mattleibow Jul 21, 2020
96f4a26
sdfasdf
mattleibow Jul 21, 2020
6d7d0d9
SKMetalView
mattleibow Jul 21, 2020
719368a
Merge master into dev/metal
mattleibow Oct 10, 2020
1fd0a1d
Update PR
mattleibow Feb 1, 2021
951333d
Merge branch 'master' into dev/metal
mattleibow Feb 1, 2021
753d395
Merge branch 'dev/skia-update' into dev/metal-update
mattleibow Feb 1, 2021
6b6213f
Some fixes
mattleibow Feb 1, 2021
daf2657
Fixing things up
mattleibow Feb 1, 2021
fc244e3
Fix changed enums
mattleibow Feb 1, 2021
7d087c9
Remove the externals/harfbuzz submodule (#1599)
mattleibow Feb 1, 2021
2a87a0d
Add Metal APIs for macOS and iOS (#1394)
mattleibow Feb 2, 2021
112025b
Revert "Add Metal APIs for macOS and iOS (#1394)"
mattleibow Feb 2, 2021
29e8f26
Add Metal APIs for macOS and iOS (#1394)
mattleibow Feb 2, 2021
d966ef2
docs: Add unoplatform references (#1602)
jeromelaban Feb 2, 2021
97efda8
Merge branch 'dev/skia-update' into dev/metal-update
mattleibow Feb 2, 2021
fa6fbbe
Merge branch 'master' into dev/metal-update
mattleibow Feb 2, 2021
abc73fe
Merge branch 'master' into dev/metal-update
mattleibow Feb 2, 2021
e3a2414
Add a symlink for a "missing" file
mattleibow Feb 2, 2021
f66ea3b
Merge branch 'master' into dev/metal-update
mattleibow Feb 2, 2021
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
Prev Previous commit
Next Next commit
SKMetalView
  • Loading branch information
mattleibow committed Jul 21, 2020
commit 6d7d0d96ea57bb6c87d1c0d718f50a5166a3c7c2
6 changes: 3 additions & 3 deletions binding/Binding/SKSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ public static SKSurface Create (GRContext context, CoreAnimation.CAMetalLayer la

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

Expand Down
12 changes: 11 additions & 1 deletion samples/Basic/iOS/SkiaSharpSample/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<subviews>
<view contentMode="scaleToFill" id="199" customClass="SKCanvasView" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<autoresizingMask key="autoresizingMask" heightSizable="YES" widthSizable="YES"/>
</view>
</subviews>
</view>
<navigationItem title="SkiaSharp Sample" id="287" key="navigationItem"/>
<connections>
<outlet property="skiaView" destination="199" id="name-outlet-199"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
Expand All @@ -31,7 +41,7 @@
<objects>
<navigationController id="216" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="218">
<rect key="frame" x="0.0" y="20" width="375" height="50"/>
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
Expand Down
107 changes: 0 additions & 107 deletions samples/Basic/iOS/SkiaSharpSample/ViewController.cs
Original file line number Diff line number Diff line change
@@ -1,111 +1,6 @@
using System;
using UIKit;

using SkiaSharp;
using SkiaSharp.Views.iOS;
using Metal;
using MetalKit;
using CoreGraphics;

namespace SkiaSharpSample
{
public partial class ViewController : UIViewController, IMTKViewDelegate
{
protected ViewController(IntPtr handle)
: base(handle)
{
}

MTKView mtkView;
GRContext grContext;
IMTLDevice metalDevice;
IMTLCommandQueue metalQueue;

public override void LoadView()
{
View = new MTKView(UIScreen.MainScreen.Bounds, null);
}

public override void ViewDidLoad()
{
base.ViewDidLoad();

if (grContext == null)
{
metalDevice = MTLDevice.SystemDefault;
metalQueue = metalDevice.CreateCommandQueue();
grContext = GRContext.CreateMetal(metalDevice, metalQueue);
}

if (View == null || metalDevice == null)
{
Console.WriteLine("Metal is not supported on this device.");
return;
}

mtkView = (MTKView)View;
mtkView.BackgroundColor = UIColor.Black;
mtkView.ColorPixelFormat = MTLPixelFormat.BGRA8Unorm;
mtkView.DepthStencilPixelFormat = MTLPixelFormat.Depth32Float_Stencil8;
mtkView.SampleCount = 1;
mtkView.Device = metalDevice;
mtkView.Delegate = this;
}

public void DrawableSizeWillChange(MTKView view, CGSize size)
{
}

public void Draw(MTKView view)
{
if (grContext == null || view == null)
return;

var colorType = SKColorType.Bgra8888;
var origin = GRSurfaceOrigin.TopLeft;
var sampleCount = (int)mtkView.SampleCount;
var size = mtkView.DrawableSize;
var width = (int)size.Width;
var height = (int)size.Height;

var fbInfo = new GRMetalTextureInfo(mtkView.CurrentDrawable.Texture);

using (var backendRT = new GRBackendRenderTarget(width, height, 1, fbInfo))
using (var surface = SKSurface.Create(grContext, backendRT, origin, colorType))
{
if (surface == null)
{
Console.WriteLine("Unable to create SKSurface.");
return;
}

OnPaintSurface(mtkView, new SKPaintGLSurfaceEventArgs(surface, backendRT));

// Must flush *and* present for this to work!
surface.Flush();
}

var commandBuffer = metalQueue.CommandBuffer();
commandBuffer.PresentDrawable(view.CurrentDrawable);
commandBuffer.Commit();
}

private void OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
{
var canvas = e.Surface.Canvas;

canvas.Clear(SKColors.Red);
canvas.DrawRect(SKRect.Create(100, 400, 400, 100), new SKPaint { Color = SKColors.Violet });
canvas.DrawText("This is METAL! " + DateTime.Now.ToString("mm:ss.fff"), 50, 550, new SKPaint { TextSize = 50 });

}
}
}

/*
using System;
using UIKit;

using SkiaSharp;
using SkiaSharp.Views.iOS;

Expand Down Expand Up @@ -153,5 +48,3 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
}
}
}

*/
8 changes: 8 additions & 0 deletions samples/Basic/iOS/SkiaSharpSample/ViewController.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion samples/Basic/macOS/SkiaSharpSample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>10.11</string>
<string>10.10</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleInfoDictionaryVersion</key>
Expand Down
152 changes: 152 additions & 0 deletions source/SkiaSharp.Views/SkiaSharp.Views.Apple/SKMetalView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#if __IOS__ || __MACOS__
using System;
using System.ComponentModel;
using System.Diagnostics;
using CoreGraphics;
using Foundation;
using Metal;
using MetalKit;

#if __IOS__
namespace SkiaSharp.Views.iOS
#elif __MACOS__
namespace SkiaSharp.Views.Mac
#endif
{
[Register(nameof(SKMetalView))]
[DesignTimeVisible(true)]
public class SKMetalView : MTKView, IMTKViewDelegate, IComponent
{
// for IComponent
#pragma warning disable 67
private event EventHandler DisposedInternal;
#pragma warning restore 67
ISite IComponent.Site { get; set; }
event EventHandler IComponent.Disposed
{
add { DisposedInternal += value; }
remove { DisposedInternal -= value; }
}

private bool designMode;

private IMTLCommandQueue commandQueue;
private GRContext context;

// created in code
public SKMetalView()
: this(CGRect.Empty)
{
Initialize();
}

// created in code
public SKMetalView(CGRect frame)
: base(frame, null)
{
Initialize();
}

// created in code
public SKMetalView(CGRect frame, IMTLDevice device)
: base(frame, device)
{
Initialize();
}

// created via designer
public SKMetalView(IntPtr p)
: base(p)
{
}

// created via designer
public override void AwakeFromNib()
{
Initialize();
}

private void Initialize()
{
designMode = ((IComponent)this).Site?.DesignMode == true || !Extensions.IsValidEnvironment;

if (designMode)
return;

var device = MTLDevice.SystemDefault;
if (device == null)
{
Console.WriteLine("Metal is not supported on this device.");
return;
}

ColorPixelFormat = MTLPixelFormat.BGRA8Unorm;
DepthStencilPixelFormat = MTLPixelFormat.Depth32Float_Stencil8;
SampleCount = 1;
Device = device;
commandQueue = device.CreateCommandQueue();

// hook up the drawing
Delegate = this;
}

public SKSize CanvasSize { get; private set; }

public GRContext GRContext => context;

void IMTKViewDelegate.DrawableSizeWillChange(MTKView view, CGSize size)
{
CanvasSize = size.ToSKSize();
}

void IMTKViewDelegate.Draw(MTKView view)
{
if (designMode)
return;

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

CanvasSize = DrawableSize.ToSKSize();

if (CanvasSize.Width <= 0 || CanvasSize.Height <= 0)
return;

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

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

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

// create the surface
using var surface = SKSurface.Create(context, renderTarget, surfaceOrigin, colorType);
using var canvas = surface.Canvas;

// start drawing
var e = new SKPaintMetalSurfaceEventArgs(surface, renderTarget, surfaceOrigin, colorType);
OnPaintSurface(e);

// flush the SkiaSharp contents
canvas.Flush();
surface.Flush();
context.Flush();

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

public event EventHandler<SKPaintMetalSurfaceEventArgs> PaintSurface;

protected virtual void OnPaintSurface(SKPaintMetalSurfaceEventArgs e)
{
PaintSurface?.Invoke(this, e);
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if __IOS__ || __MACOS__
using System;

#if __IOS__
namespace SkiaSharp.Views.iOS
#elif __MACOS__
namespace SkiaSharp.Views.Mac
#endif
{
public class SKPaintMetalSurfaceEventArgs : EventArgs
{
public SKPaintMetalSurfaceEventArgs(SKSurface surface, GRBackendRenderTarget renderTarget)
: this(surface, renderTarget, GRSurfaceOrigin.TopLeft, SKColorType.Rgba8888)
{
}

public SKPaintMetalSurfaceEventArgs(SKSurface surface, GRBackendRenderTarget renderTarget, GRSurfaceOrigin origin, SKColorType colorType)
{
Surface = surface;
BackendRenderTarget = renderTarget;
ColorType = colorType;
Origin = origin;
}

public SKSurface Surface { get; private set; }

public GRBackendRenderTarget BackendRenderTarget { get; private set; }

public SKColorType ColorType { get; private set; }

public GRSurfaceOrigin Origin { get; private set; }
}
}
#endif