// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) 2020 OxyPlot contributors
//
// --------------------------------------------------------------------------------------------------------------------
namespace OxyPlot.SkiaSharp.Wpf
{
using global::SkiaSharp;
using global::SkiaSharp.Views.Desktop;
using OxyPlot;
using OxyPlot.SkiaSharp;
using OxyPlot.Wpf;
using System.Windows;
///
/// Represents a control that displays a . This is based on .
///
public partial class PlotView : PlotViewBase
{
///
/// Gets the SkiaRenderContext.
///
private SkiaRenderContext SkiaRenderContext => (SkiaRenderContext)this.renderContext;
///
/// Gets the OxySKElement.
///
private OxySKElement OxySKElement => (OxySKElement)this.plotPresenter;
///
protected override void ClearBackground()
{
var color = this.ActualModel?.Background.IsVisible() == true
? this.ActualModel.Background.ToSKColor()
: SKColors.Empty;
this.SkiaRenderContext.SkCanvas.Clear(color);
}
///
protected override FrameworkElement CreatePlotPresenter()
{
var skElement = new OxySKElement();
skElement.PaintSurface += this.SkElement_PaintSurface;
return skElement;
}
///
protected override IRenderContext CreateRenderContext()
{
return new SkiaRenderContext();
}
///
protected override void RenderOverride()
{
// Instead of rendering directly, invalidate the plot presenter.
// Actual rendering is done in SkElement_PaintSurface.
this.plotPresenter.InvalidateVisual();
}
///
protected override double UpdateDpi()
{
var dpiScale = base.UpdateDpi();
var renderScale = this.OxySKElement.GetRenderScale();
var skiaScale = (float)(dpiScale * renderScale);
this.SkiaRenderContext.DpiScale = skiaScale;
return dpiScale;
}
///
/// This is called when the SKElement paints its surface.
///
/// The sender.
/// The surface paint event args.
private void SkElement_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
if (this.plotPresenter == null || this.renderContext == null)
{
return;
}
this.SkiaRenderContext.SkCanvas = e.Surface.Canvas;
base.RenderOverride();
this.SkiaRenderContext.SkCanvas = null;
}
}
}