Skip to content

Commit

Permalink
Add the GPU views for Uno Platform (#1429)
Browse files Browse the repository at this point in the history
* Add the GPU views for the platforms
* Add the render loop for the platforms
* Fix the samples for infinite recursion
* Added the GL views to the gallery
  • Loading branch information
mattleibow authored Jul 27, 2020
1 parent 1fa3dad commit b3f5975
Show file tree
Hide file tree
Showing 22 changed files with 828 additions and 14 deletions.
3 changes: 1 addition & 2 deletions samples/Gallery/Shared/Samples/CreatePdfSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void OnDrawSample(SKCanvas canvas, int width, int height)

private void GenerateDocument()
{
if (isSupported && File.Exists(path))
if (!isSupported || (isSupported && File.Exists(path)))
return;

var metadata = new SKDocumentPdfMetadata
Expand All @@ -70,7 +70,6 @@ private void GenerateDocument()
if (document == null)
{
isSupported = false;
Refresh();
return;
}

Expand Down
3 changes: 1 addition & 2 deletions samples/Gallery/Shared/Samples/CreateXpsSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ protected override void OnDrawSample(SKCanvas canvas, int width, int height)

private void GenerateDocument()
{
if (isSupported && File.Exists(path))
if (!isSupported || (isSupported && File.Exists(path)))
return;

using var document = SKDocument.CreateXps(path);

if (document == null)
{
isSupported = false;
Refresh();
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" package="com.companyname.skiasharpsample" android:versionCode="1">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" />
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:label="@string/app_name" android:theme="@style/Theme.AppCompat" android:hardwareAccelerated="true"></application>
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:label="@string/app_name" android:theme="@style/AppTheme" android:hardwareAccelerated="true"></application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light">

<!-- This removes the ActionBar -->
<item name="windowActionBar">false</item>
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>

</style>
</resources>
9 changes: 9 additions & 0 deletions samples/Gallery/Uno/SkiaSharpSample.Shared/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<!-- the samples canvas -->
<Grid Tapped="OnSampleTapped" Background="White">
<views:SKXamlCanvas x:Name="canvas" Grid.Row="1" PaintSurface="OnPaintCanvas" />
<views:SKSwapChainPanel x:Name="glview" Grid.Row="1" PaintSurface="OnPaintGL" Visibility="Collapsed" />
</Grid>
</SplitView.Content>
</SplitView>
Expand All @@ -54,6 +55,14 @@
OverflowButtonVisibility="Collapsed"
Foreground="White">
<!-- the toolbar items -->
<AppBarButton Icon="Repair" Label="Backend" IsCompact="True" Foreground="White">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem Text="Memory" Tag="Memory" Click="OnBackendSelected" />
<MenuFlyoutItem Text="OpenGL" Tag="OpenGL" Click="OnBackendSelected" />
</MenuFlyout>
</Button.Flyout>
</AppBarButton>
<AppBarButton
Click="OnToggleSlideshow"
Foreground="White"
Expand Down
30 changes: 29 additions & 1 deletion samples/Gallery/Uno/SkiaSharpSample.Shared/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ private void OnToggleSplitView(object sender, RoutedEventArgs e)
splitView.IsPaneOpen = menuButton.IsChecked == true;
}

private void OnBackendSelected(object sender, RoutedEventArgs e)
{
var menu = sender as MenuFlyoutItem;

var backend = (SampleBackends)Enum.Parse(typeof(SampleBackends), menu.Tag.ToString());
switch (backend)
{
case SampleBackends.Memory:
glview.Visibility = Visibility.Collapsed;
canvas.Visibility = Visibility.Visible;
canvas.Invalidate();
break;
case SampleBackends.OpenGL:
glview.Visibility = Visibility.Visible;
canvas.Visibility = Visibility.Collapsed;
glview.Invalidate();
break;
}
}

private void OnToggleSlideshow(object sender, RoutedEventArgs e)
{
if (cancellations != null)
Expand Down Expand Up @@ -107,6 +127,11 @@ private void OnPaintCanvas(object sender, SKPaintSurfaceEventArgs e)
OnPaintSurface(e.Surface.Canvas, e.Info.Width, e.Info.Height);
}

private void OnPaintGL(object sender, SKPaintGLSurfaceEventArgs e)
{
OnPaintSurface(e.Surface.Canvas, e.BackendRenderTarget.Width, e.BackendRenderTarget.Height);
}

private void SetSample(SampleBase newSample)
{
// clean up the old sample
Expand Down Expand Up @@ -145,7 +170,10 @@ private void SetSample(SampleBase newSample)

private void OnRefreshRequested(object sender, EventArgs e)
{
canvas.Invalidate();
if (canvas.Visibility == Visibility.Visible)
canvas.Invalidate();
if (glview.Visibility == Visibility.Visible)
glview.Invalidate();
}

private void OnPaintSurface(SKCanvas canvas, int width, int height)
Expand Down
1 change: 1 addition & 0 deletions scripts/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ stages:
- task: TSAUpload@1
displayName: Publish TSA logs
condition: always()
continueOnError: true
inputs:
tsaVersion: 'TsaV2'
codebase: 'NewOrUpdate'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Android.Opengl;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace SkiaSharp.Views.UWP
{
public partial class SKSwapChainPanel : FrameworkElement
{
private SKGLTextureView glTextureView;

public SKSwapChainPanel()
{
Initialize();
}

private SKSize GetCanvasSize() =>
glTextureView?.CanvasSize ?? SKSize.Empty;

private GRContext GetGRContext() =>
glTextureView?.GRContext;

partial void DoLoaded()
{
glTextureView = new SKGLTextureView(Context);
DoEnableRenderLoop(EnableRenderLoop);
glTextureView.PaintSurface += OnPaintSurface;
AddView(glTextureView);
}

partial void DoUnloaded()
{
if (glTextureView == null)
return;

RemoveView(glTextureView);
glTextureView.PaintSurface -= OnPaintSurface;
glTextureView.Dispose();
glTextureView = null;
}

partial void DoEnableRenderLoop(bool enable)
{
if (glTextureView == null)
return;

glTextureView.RenderMode = enable
? Rendermode.Continuously
: Rendermode.WhenDirty;
}

private void DoInvalidate() =>
glTextureView?.RequestRender();

private void OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e) =>
OnPaintSurface(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Shared\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Android\GLTextureView.cs" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Android\SKGLTextureView.cs" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Android\SKGLTextureViewRenderer.cs" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.UWP\UWPExtensions.cs" />
<Compile Include="..\SkiaSharp.Views.Uno\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using CoreVideo;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace SkiaSharp.Views.UWP
{
public partial class SKSwapChainPanel : FrameworkElement
{
private SKGLView glView;
private CVDisplayLink displayLink;

public SKSwapChainPanel()
{
Initialize();
}

private SKSize GetCanvasSize() =>
glView?.CanvasSize ?? SKSize.Empty;

private GRContext GetGRContext() =>
glView?.GRContext;

partial void DoLoaded()
{
glView = new SKGLView(Bounds);
glView.PaintSurface += OnPaintSurface;
AddSubview(glView);
}

partial void DoUnloaded()
{
DoEnableRenderLoop(false);

if (glView != null)
{
glView.RemoveFromSuperview();
glView.PaintSurface -= OnPaintSurface;
glView.Dispose();
glView = null;
}
}

private void DoInvalidate() =>
DoEnableRenderLoop(true);

private void OnPaintSurface(object sender, SKPaintGLSurfaceEventArgs e) =>
OnPaintSurface(e);

partial void DoEnableRenderLoop(bool enable)
{
// stop the render loop
if (!enable)
{
if (displayLink != null)
{
displayLink.Stop();
displayLink.Dispose();
displayLink = null;
}
return;
}

// only start if we haven't already
if (displayLink != null)
return;

// create the loop
displayLink = new CVDisplayLink();
displayLink.SetOutputCallback(delegate
{
// redraw the view
glView?.BeginInvokeOnMainThread(() => glView?.Display());

// stop the render loop if it has been disabled or the views are disposed
if (glView == null || !EnableRenderLoop)
DoEnableRenderLoop(false);

return CVReturn.Success;
});
displayLink.Start();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Shared\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Apple\SKCGSurfaceFactory.cs" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.Mac\SKGLView.cs" />
<Compile Include="..\..\SkiaSharp.Views\SkiaSharp.Views.UWP\UWPExtensions.cs" />
<Compile Include="..\SkiaSharp.Views.Uno\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>
Expand Down
Loading

0 comments on commit b3f5975

Please sign in to comment.