A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 lines
3.1 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Media;
using Avalonia.Platform;
using SkiaSharp;
namespace Avalonia.Skia
{
public partial class PlatformRenderInterface : IPlatformRenderInterface
{
public IBitmapImpl CreateBitmap(int width, int height)
{
return CreateRenderTargetBitmap(width, height, 96, 96);
}
public IFormattedTextImpl CreateFormattedText(
string text,
Typeface typeface,
TextAlignment textAlignment,
TextWrapping wrapping,
Size constraint,
IReadOnlyList<FormattedTextStyleSpan> spans)
{
return new FormattedTextImpl(text, typeface, textAlignment, wrapping, constraint, spans);
}
public IStreamGeometryImpl CreateStreamGeometry()
{
return new StreamGeometryImpl();
}
public IBitmapImpl LoadBitmap(System.IO.Stream stream)
{
using (var s = new SKManagedStream(stream))
{
var bitmap = SKBitmap.Decode(s);
if (bitmap != null)
{
return new BitmapImpl(bitmap);
}
else
{
throw new ArgumentException("Unable to load bitmap from provided data");
}
}
}
public IBitmapImpl LoadBitmap(string fileName)
{
using (var stream = File.OpenRead(fileName))
{
return LoadBitmap(stream);
}
}
public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, int width, int height, int stride)
{
using (var tmp = new SKBitmap())
{
tmp.InstallPixels(new SKImageInfo(width, height, format.ToSkColorType(), SKAlphaType.Premul)
, data, stride);
return new BitmapImpl(tmp.Copy());
}
}
public IRenderTargetBitmapImpl CreateRenderTargetBitmap(
int width,
int height,
double dpiX,
double dpiY)
{
if (width < 1)
throw new ArgumentException("Width can't be less than 1", nameof(width));
if (height < 1)
throw new ArgumentException("Height can't be less than 1", nameof(height));
return new BitmapImpl(width, height, new Vector(dpiX, dpiY));
}
public virtual IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
{
var fb = surfaces?.OfType<IFramebufferPlatformSurface>().FirstOrDefault();
if (fb == null)
throw new Exception("Skia backend currently only supports framebuffer render target");
return new FramebufferRenderTarget(fb);
}
public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? format = null)
{
return new BitmapImpl(width, height, new Vector(96, 96), format);
}
}
}