This commit is contained in:
Matthew Leibowitz 2016-09-09 07:04:15 +02:00
Родитель bc3fbfc9e5
Коммит 8b3d47c211
6 изменённых файлов: 215 добавлений и 7 удалений

Просмотреть файл

@ -0,0 +1,83 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace SkiaSharp.Views
{
public class SKControl : Control
{
private readonly bool designMode;
private Bitmap bitmap;
public SKControl()
{
DoubleBuffered = true;
SetStyle(ControlStyles.ResizeRedraw, true);
designMode = DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;
}
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
protected override void OnPaint(PaintEventArgs e)
{
if (designMode)
return;
base.OnPaint(e);
// get the bitmap
CreateBitmap();
var data = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
// create the surface
var info = new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
using (var surface = SKSurface.Create(info, data.Scan0, data.Stride))
{
// start drawing
OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info));
surface.Canvas.Flush();
}
// write the bitmap to the graphics
bitmap.UnlockBits(data);
e.Graphics.DrawImage(bitmap, 0, 0);
}
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
// invoke the event
PaintSurface?.Invoke(this, e);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
FreeBitmap();
}
private void CreateBitmap()
{
if (bitmap == null || bitmap.Width != Width || bitmap.Height != Height)
{
FreeBitmap();
bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppPArgb);
}
}
private void FreeBitmap()
{
if (bitmap != null)
{
bitmap.Dispose();
bitmap = null;
}
}
}
}

Просмотреть файл

@ -0,0 +1,75 @@
using System;
using System.ComponentModel;
using System.Windows.Forms;
using OpenTK;
namespace SkiaSharp.Views
{
public class SKGLControl : GLControl
{
private readonly bool designMode;
private GRContext grContext;
private GRBackendRenderTargetDesc renderTarget;
public SKGLControl()
{
designMode = DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;
ResizeRedraw = true;
}
public event EventHandler<SKPaintGLSurfaceEventArgs> PaintSurface;
protected override void OnPaint(PaintEventArgs e)
{
if (designMode)
{
e.Graphics.Clear(BackColor);
return;
}
base.OnPaint(e);
// create the contexts if not done already
if (grContext == null)
{
var glInterface = GRGlInterface.CreateNativeInterface();
grContext = GRContext.Create(GRBackend.OpenGL, glInterface);
// get initial details
renderTarget = SKGLDrawable.CreateRenderTarget();
}
// update to the latest dimensions
renderTarget.Width = Width;
renderTarget.Height = Height;
// create the surface
using (var surface = SKSurface.Create(grContext, renderTarget))
{
// start drawing
OnPaintSurface(new SKPaintGLSurfaceEventArgs(surface, renderTarget));
surface.Canvas.Flush();
}
// update the control
SwapBuffers();
}
protected virtual void OnPaintSurface(SKPaintGLSurfaceEventArgs e)
{
// invoke the event
PaintSurface?.Invoke(this, e);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
// clean up
grContext.Dispose();
}
}
}

Просмотреть файл

@ -0,0 +1,17 @@
using System;
namespace SkiaSharp.Views
{
public class SKPaintGLSurfaceEventArgs : EventArgs
{
public SKPaintGLSurfaceEventArgs(SKSurface surface, GRBackendRenderTargetDesc renderTarget)
{
Surface = surface;
RenderTarget = renderTarget;
}
public SKSurface Surface { get; private set; }
public GRBackendRenderTargetDesc RenderTarget { get; private set; }
}
}

Просмотреть файл

@ -0,0 +1,17 @@
using System;
namespace SkiaSharp.Views
{
public class SKPaintSurfaceEventArgs : EventArgs
{
public SKPaintSurfaceEventArgs(SKSurface surface, SKImageInfo info)
{
Surface = surface;
Info = info;
}
public SKSurface Surface { get; private set; }
public SKImageInfo Info { get; private set; }
}
}

Просмотреть файл

@ -20,7 +20,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;__DESKTOP__</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
@ -28,30 +28,44 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;__DESKTOP__</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.1.1.2349.61993\lib\NET40\OpenTK.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="OpenTK.GLControl, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.GLControl.1.1.2349.61993\lib\NET40\OpenTK.GLControl.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SkiaSharp, Version=1.54.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
<HintPath>..\packages\SkiaSharp.1.54.0\lib\net45\SkiaSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SKControl.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SKGLControl.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="SKPaintGLSurfaceEventArgs.cs" />
<Compile Include="SKPaintSurfaceEventArgs.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="..\SkiaSharp.Views.OpenTK\SkiaSharp.Views.OpenTK.projitems" Label="Shared" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\SkiaSharp.1.54.0\build\net45\SkiaSharp.targets" Condition="Exists('..\packages\SkiaSharp.1.54.0\build\net45\SkiaSharp.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

Просмотреть файл

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="1.1.2349.61993" targetFramework="net45" />
<package id="OpenTK.GLControl" version="1.1.2349.61993" targetFramework="net45" />
<package id="SkiaSharp" version="1.54.0" targetFramework="net45" />
</packages>