Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#7605 from AvaloniaUI/feature/skia-layer…
Browse files Browse the repository at this point in the history
…ing-extensions

Add Skia Helper Methods to allow applying Skia Filter Effects (Blur, DropShadow, Lighting) to DC content
  • Loading branch information
danwalmsley committed Feb 15, 2022
1 parent 50e2085 commit d22e627
Showing 1 changed file with 69 additions and 2 deletions.
71 changes: 69 additions & 2 deletions src/Skia/Avalonia.Skia/Helpers/DrawingContextHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Avalonia.Platform;
using System;
using Avalonia.Platform;
using Avalonia.Rendering;
using SkiaSharp;

namespace Avalonia.Skia.Helpers
{
public class DrawingContextHelper
public static class DrawingContextHelper
{
/// <summary>
/// Wrap Skia canvas in drawing context so we can use Avalonia api to render to external skia canvas
Expand All @@ -27,5 +28,71 @@ public static IDrawingContextImpl WrapSkiaCanvas(SKCanvas canvas, Vector dpi, IV

return new DrawingContextImpl(createInfo);
}

/// <summary>
/// Unsupported - Wraps a GPU Backed SkiaSurface in an Avalonia DrawingContext.
/// </summary>
[Obsolete]
public static ISkiaDrawingContextImpl WrapSkiaSurface(this SKSurface surface, GRContext grContext, Vector dpi, params IDisposable[] disposables)
{
var createInfo = new DrawingContextImpl.CreateInfo
{
GrContext = grContext,
Surface = surface,
Dpi = dpi,
DisableTextLcdRendering = false,
};

return new DrawingContextImpl(createInfo, disposables);
}

/// <summary>
/// Unsupported - Wraps a non-GPU Backed SkiaSurface in an Avalonia DrawingContext.
/// </summary>
[Obsolete]
public static ISkiaDrawingContextImpl WrapSkiaSurface(this SKSurface surface, Vector dpi, params IDisposable[] disposables)
{
var createInfo = new DrawingContextImpl.CreateInfo
{
Surface = surface,
Dpi = dpi,
DisableTextLcdRendering = false,
};

return new DrawingContextImpl(createInfo, disposables);
}

[Obsolete]
public static ISkiaDrawingContextImpl CreateDrawingContext(Size size, Vector dpi, GRContext grContext = null)
{
if (grContext is null)
{
var surface = SKSurface.Create(
new SKImageInfo(
(int)Math.Ceiling(size.Width),
(int)Math.Ceiling(size.Height),
SKImageInfo.PlatformColorType,
SKAlphaType.Premul));

return WrapSkiaSurface(surface, dpi, surface);
}
else
{
var surface = SKSurface.Create(grContext, false,
new SKImageInfo(
(int)Math.Ceiling(size.Width),
(int)Math.Ceiling(size.Height),
SKImageInfo.PlatformColorType,
SKAlphaType.Premul));

return WrapSkiaSurface(surface, grContext, dpi, surface);
}
}

[Obsolete]
public static void DrawTo(this ISkiaDrawingContextImpl source, ISkiaDrawingContextImpl destination, SKPaint paint = null)
{
destination.SkCanvas.DrawSurface(source.SkSurface, new SKPoint(0, 0), paint);
}
}
}

0 comments on commit d22e627

Please sign in to comment.