diff --git a/src/Avalonia.Controls/Platform/ITopLevelRenderer.cs b/src/Avalonia.Controls/Platform/ITopLevelRenderer.cs index d81ff6efd1..1653ff0a6d 100644 --- a/src/Avalonia.Controls/Platform/ITopLevelRenderer.cs +++ b/src/Avalonia.Controls/Platform/ITopLevelRenderer.cs @@ -38,7 +38,19 @@ namespace Avalonia.Controls.Platform topLevel.PlatformImpl.Paint = rect => { - viewport.Render(topLevel); + try + { + viewport.Render(topLevel); + } + catch (RenderTargetCorruptedException ex) + { + Logging.Logger.Error("Renderer", this, "Render target was corrupted. Exception: {0}", ex); + viewport.Dispose(); + resources.Remove(viewport); + viewport = PlatformManager.CreateRenderTarget(topLevel.PlatformImpl); + resources.Add(viewport); + topLevel.PlatformImpl.Paint(rect); // Retry painting + } queueManager.RenderFinished(); }; diff --git a/src/Avalonia.SceneGraph/Avalonia.SceneGraph.csproj b/src/Avalonia.SceneGraph/Avalonia.SceneGraph.csproj index f6dc1c4f3c..25a79307e6 100644 --- a/src/Avalonia.SceneGraph/Avalonia.SceneGraph.csproj +++ b/src/Avalonia.SceneGraph/Avalonia.SceneGraph.csproj @@ -100,6 +100,7 @@ + diff --git a/src/Avalonia.SceneGraph/RenderTargetCorruptedException.cs b/src/Avalonia.SceneGraph/RenderTargetCorruptedException.cs new file mode 100644 index 0000000000..f67a866be2 --- /dev/null +++ b/src/Avalonia.SceneGraph/RenderTargetCorruptedException.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Avalonia +{ + public class RenderTargetCorruptedException : Exception + { + public RenderTargetCorruptedException() + { + } + + public RenderTargetCorruptedException(string message) + : base(message) + { + } + + public RenderTargetCorruptedException(Exception innerException) + : base(null, innerException) + { + } + + public RenderTargetCorruptedException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index ff0bebd359..6ad64aced1 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -26,8 +26,12 @@ namespace Avalonia.Direct2D1 { private static readonly Direct2D1Platform s_instance = new Direct2D1Platform(); - private static readonly SharpDX.Direct2D1.Factory s_d2D1Factory = new SharpDX.Direct2D1.Factory(); - + private static readonly SharpDX.Direct2D1.Factory s_d2D1Factory = +#if DEBUG + new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded, SharpDX.Direct2D1.DebugLevel.Error); +#else + new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded, SharpDX.Direct2D1.DebugLevel.None); +#endif private static readonly SharpDX.DirectWrite.Factory s_dwfactory = new SharpDX.DirectWrite.Factory(); private static readonly SharpDX.WIC.ImagingFactory s_imagingFactory = new SharpDX.WIC.ImagingFactory(); diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs index 61ba43123b..75a0f43d9f 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContext.cs @@ -57,7 +57,14 @@ namespace Avalonia.Direct2D1.Media { foreach (var layer in _layerPool) layer.Dispose(); - _renderTarget.EndDraw(); + try + { + _renderTarget.EndDraw(); + } + catch (SharpDXException ex) when((uint)ex.HResult == 0x8899000C) // D2DERR_RECREATE_TARGET + { + throw new RenderTargetCorruptedException(ex); + } } ///