Browse Source

Introduce ResizeBuffers to prevent SwapChain recreation

pull/1861/head
Benedikt Schroeder 8 years ago
parent
commit
0646106f8f
  1. 105
      src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs

105
src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs

@ -7,11 +7,13 @@ using SharpDX;
using SharpDX.Direct2D1; using SharpDX.Direct2D1;
using SharpDX.DXGI; using SharpDX.DXGI;
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using PixelFormat = SharpDX.Direct2D1.PixelFormat;
namespace Avalonia.Direct2D1 namespace Avalonia.Direct2D1
{ {
using AlphaMode = SharpDX.Direct2D1.AlphaMode;
using DeviceContext = SharpDX.Direct2D1.DeviceContext;
using PixelFormat = SharpDX.Direct2D1.PixelFormat;
public abstract class SwapChainRenderTarget : IRenderTarget, ILayerFactory public abstract class SwapChainRenderTarget : IRenderTarget, ILayerFactory
{ {
private Size2 _savedSize; private Size2 _savedSize;
@ -32,7 +34,8 @@ namespace Avalonia.Direct2D1
{ {
_savedSize = size; _savedSize = size;
_savedDpi = dpi; _savedDpi = dpi;
CreateSwapChain();
Resize();
} }
return new DrawingContextImpl(visualBrushRenderer, this, _deviceContext, _swapChain); return new DrawingContextImpl(visualBrushRenderer, this, _deviceContext, _swapChain);
@ -42,7 +45,7 @@ namespace Avalonia.Direct2D1
{ {
if (_deviceContext == null) if (_deviceContext == null)
{ {
CreateSwapChain(); CreateDeviceContext();
} }
return D2DRenderTargetBitmapImpl.CreateCompatible(_deviceContext, size); return D2DRenderTargetBitmapImpl.CreateCompatible(_deviceContext, size);
@ -51,54 +54,68 @@ namespace Avalonia.Direct2D1
public void Dispose() public void Dispose()
{ {
_deviceContext?.Dispose(); _deviceContext?.Dispose();
_swapChain?.Dispose(); _swapChain?.Dispose();
} }
private void Resize()
{
_deviceContext?.Dispose();
_deviceContext = null;
_swapChain?.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.None);
CreateDeviceContext();
}
private void CreateSwapChain() private void CreateSwapChain()
{ {
using (var dxgiAdaptor = Direct2D1Platform.DxgiDevice.Adapter) var swapChainDescription = new SwapChainDescription1
using (var dxgiFactory = dxgiAdaptor.GetParent<SharpDX.DXGI.Factory2>())
{ {
_deviceContext?.Dispose(); Width = _savedSize.Width,
_deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; Height = _savedSize.Height,
Format = Format.B8G8R8A8_UNorm,
var swapChainDesc = new SwapChainDescription1 SampleDescription = new SampleDescription
{ {
Width = _savedSize.Width, Count = 1,
Height = _savedSize.Height, Quality = 0,
Format = Format.B8G8R8A8_UNorm, },
Stereo = false, Usage = Usage.RenderTargetOutput,
SampleDescription = new SampleDescription BufferCount = 1,
SwapEffect = SwapEffect.Discard,
};
using (var dxgiAdapter = Direct2D1Platform.DxgiDevice.Adapter)
using (var dxgiFactory = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>())
{
_swapChain = CreateSwapChain(dxgiFactory, swapChainDescription);
}
}
private void CreateDeviceContext()
{
_deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi };
if (_swapChain == null)
{
CreateSwapChain();
}
using (var dxgiBackBuffer = _swapChain.GetBackBuffer<Surface>(0))
using (var d2dBackBuffer = new Bitmap1(
_deviceContext,
dxgiBackBuffer,
new BitmapProperties1(
new PixelFormat
{ {
Count = 1, AlphaMode = AlphaMode.Premultiplied,
Quality = 0, Format = Format.B8G8R8A8_UNorm
}, },
Usage = Usage.RenderTargetOutput, _savedSize.Width,
BufferCount = 1, _savedSize.Height,
Scaling = Scaling.Stretch, BitmapOptions.Target | BitmapOptions.CannotDraw)))
SwapEffect = SwapEffect.Discard, {
Flags = 0, _deviceContext.Target = d2dBackBuffer;
};
_swapChain?.Dispose();
_swapChain = CreateSwapChain(dxgiFactory, swapChainDesc);
using (var dxgiBackBuffer = _swapChain.GetBackBuffer<Surface>(0))
using (var d2dBackBuffer = new Bitmap1(
_deviceContext,
dxgiBackBuffer,
new BitmapProperties1(
new PixelFormat
{
AlphaMode = AlphaMode.Premultiplied,
Format = Format.B8G8R8A8_UNorm
},
_savedDpi.Width,
_savedDpi.Height,
BitmapOptions.Target | BitmapOptions.CannotDraw)))
{
_deviceContext.Target = d2dBackBuffer;
}
} }
} }

Loading…
Cancel
Save