Browse Source
Craetes a compatible render target depending on the type of `IRenderTargetBitmapImpl`.pull/1184/head
14 changed files with 225 additions and 121 deletions
@ -0,0 +1,10 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Platform; |
||||
|
|
||||
|
namespace Avalonia.Direct2D1 |
||||
|
{ |
||||
|
internal interface ICreateLayer |
||||
|
{ |
||||
|
IRenderTargetBitmapImpl CreateLayer(int pixelWidth, int pixelHeight); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Platform; |
||||
|
using Avalonia.Rendering; |
||||
|
using SharpDX; |
||||
|
using SharpDX.Direct2D1; |
||||
|
using SharpDX.WIC; |
||||
|
using D2DBitmap = SharpDX.Direct2D1.Bitmap; |
||||
|
using DirectWriteFactory = SharpDX.DirectWrite.Factory; |
||||
|
|
||||
|
namespace Avalonia.Direct2D1.Media.Imaging |
||||
|
{ |
||||
|
public class D2DRenderTargetBitmapImpl : D2DBitmapImpl, IRenderTargetBitmapImpl, ICreateLayer |
||||
|
{ |
||||
|
private readonly DirectWriteFactory _dwriteFactory; |
||||
|
private readonly BitmapRenderTarget _target; |
||||
|
|
||||
|
public D2DRenderTargetBitmapImpl( |
||||
|
ImagingFactory imagingFactory, |
||||
|
DirectWriteFactory dwriteFactory, |
||||
|
BitmapRenderTarget target) |
||||
|
: base(imagingFactory, target.Bitmap) |
||||
|
{ |
||||
|
_dwriteFactory = dwriteFactory; |
||||
|
_target = target; |
||||
|
} |
||||
|
|
||||
|
public override int PixelWidth => _target.PixelSize.Width; |
||||
|
public override int PixelHeight => _target.PixelSize.Height; |
||||
|
|
||||
|
public static D2DRenderTargetBitmapImpl CreateCompatible( |
||||
|
ImagingFactory imagingFactory, |
||||
|
DirectWriteFactory dwriteFactory, |
||||
|
SharpDX.Direct2D1.RenderTarget renderTarget, |
||||
|
int pixelWidth, |
||||
|
int pixelHeight) |
||||
|
{ |
||||
|
var bitmapRenderTarget = new BitmapRenderTarget( |
||||
|
renderTarget, |
||||
|
CompatibleRenderTargetOptions.None, |
||||
|
new Size2F(pixelWidth, pixelHeight)); |
||||
|
return new D2DRenderTargetBitmapImpl(imagingFactory, dwriteFactory, bitmapRenderTarget); |
||||
|
} |
||||
|
|
||||
|
public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) |
||||
|
{ |
||||
|
return new DrawingContextImpl( |
||||
|
visualBrushRenderer, |
||||
|
_target, |
||||
|
_dwriteFactory, |
||||
|
WicImagingFactory); |
||||
|
} |
||||
|
|
||||
|
public IRenderTargetBitmapImpl CreateLayer(int pixelWidth, int pixelHeight) |
||||
|
{ |
||||
|
return CreateCompatible(WicImagingFactory, _dwriteFactory, _target, pixelWidth, pixelHeight); |
||||
|
} |
||||
|
|
||||
|
public override void Dispose() |
||||
|
{ |
||||
|
_target.Dispose(); |
||||
|
} |
||||
|
|
||||
|
public override OptionalDispose<D2DBitmap> GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget target) |
||||
|
{ |
||||
|
return new OptionalDispose<D2DBitmap>(_target.Bitmap, false); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Avalonia.Direct2D1 |
||||
|
{ |
||||
|
public struct OptionalDispose<T> : IDisposable where T : IDisposable |
||||
|
{ |
||||
|
private readonly bool _dispose; |
||||
|
|
||||
|
public OptionalDispose(T value, bool dispose) |
||||
|
{ |
||||
|
Value = value; |
||||
|
_dispose = dispose; |
||||
|
} |
||||
|
|
||||
|
public T Value { get; } |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
if (_dispose) Value?.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue