diff --git a/Tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_No_Visual.expected.png b/Tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_No_Visual.expected.png new file mode 100644 index 0000000000..31d16498cc Binary files /dev/null and b/Tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_No_Visual.expected.png differ diff --git a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs index 8fdcb42b82..53f5ff3d5c 100644 --- a/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs +++ b/src/Windows/Perspex.Direct2D1/Media/DrawingContext.cs @@ -90,12 +90,15 @@ namespace Perspex.Direct2D1.Media using (var d2dBrush = CreateBrush(pen.Brush, size)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) { - _renderTarget.DrawLine( - p1.ToSharpDX(), - p2.ToSharpDX(), - d2dBrush.PlatformBrush, - (float)pen.Thickness, - d2dStroke); + if (d2dBrush.PlatformBrush != null) + { + _renderTarget.DrawLine( + p1.ToSharpDX(), + p2.ToSharpDX(), + d2dBrush.PlatformBrush, + (float)pen.Thickness, + d2dStroke); + } } } } @@ -112,8 +115,11 @@ namespace Perspex.Direct2D1.Media { using (var d2dBrush = CreateBrush(brush, geometry.Bounds.Size)) { - GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; - _renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); + if (d2dBrush.PlatformBrush != null) + { + var impl = (GeometryImpl)geometry.PlatformImpl; + _renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); + } } } @@ -122,8 +128,11 @@ namespace Perspex.Direct2D1.Media using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen.Thickness).Size)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) { - GeometryImpl impl = (GeometryImpl)geometry.PlatformImpl; - _renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); + if (d2dBrush.PlatformBrush != null) + { + var impl = (GeometryImpl)geometry.PlatformImpl; + _renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); + } } } } @@ -139,21 +148,24 @@ namespace Perspex.Direct2D1.Media using (var brush = CreateBrush(pen.Brush, rect.Size)) using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) { - if (cornerRadius == 0) + if (brush.PlatformBrush != null) { - _renderTarget.DrawRectangle( - rect.ToDirect2D(), - brush.PlatformBrush, - (float)pen.Thickness, - d2dStroke); - } - else - { - _renderTarget.DrawRoundedRectangle( - new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius }, - brush.PlatformBrush, - (float)pen.Thickness, - d2dStroke); + if (cornerRadius == 0) + { + _renderTarget.DrawRectangle( + rect.ToDirect2D(), + brush.PlatformBrush, + (float)pen.Thickness, + d2dStroke); + } + else + { + _renderTarget.DrawRoundedRectangle( + new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius }, + brush.PlatformBrush, + (float)pen.Thickness, + d2dStroke); + } } } } @@ -173,7 +185,10 @@ namespace Perspex.Direct2D1.Media using (var brush = CreateBrush(foreground, impl.Measure())) using (var renderer = new PerspexTextRenderer(this, _renderTarget, brush.PlatformBrush)) { - impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y); + if (brush.PlatformBrush != null) + { + impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y); + } } } } @@ -188,24 +203,27 @@ namespace Perspex.Direct2D1.Media { using (var b = CreateBrush(brush, rect.Size)) { - if (cornerRadius == 0) - { - _renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush); - } - else + if (b.PlatformBrush != null) { - _renderTarget.FillRoundedRectangle( - new RoundedRectangle - { - Rect = new RectangleF( - (float)rect.X, - (float)rect.Y, - (float)rect.Width, - (float)rect.Height), - RadiusX = cornerRadius, - RadiusY = cornerRadius - }, - b.PlatformBrush); + if (cornerRadius == 0) + { + _renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush); + } + else + { + _renderTarget.FillRoundedRectangle( + new RoundedRectangle + { + Rect = new RectangleF( + (float)rect.X, + (float)rect.Y, + (float)rect.Width, + (float)rect.Height), + RadiusX = cornerRadius, + RadiusY = cornerRadius + }, + b.PlatformBrush); + } } } } diff --git a/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs b/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs index 48b349edad..f7e12187f6 100644 --- a/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs +++ b/src/Windows/Perspex.Direct2D1/Media/VisualBrushImpl.cs @@ -16,6 +16,12 @@ namespace Perspex.Direct2D1.Media Size targetSize) { var visual = brush.Visual; + + if (visual == null) + { + return; + } + var layoutable = visual as ILayoutable; if (layoutable?.IsArrangeValid == false) @@ -102,7 +108,7 @@ namespace Perspex.Direct2D1.Media public override void Dispose() { - ((BitmapBrush)PlatformBrush).Bitmap.Dispose(); + ((BitmapBrush)PlatformBrush)?.Bitmap.Dispose(); base.Dispose(); } } diff --git a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs index 10fbef3e79..499f8207c3 100644 --- a/tests/Perspex.RenderTests/Media/VisualBrushTests.cs +++ b/tests/Perspex.RenderTests/Media/VisualBrushTests.cs @@ -650,6 +650,28 @@ namespace Perspex.Direct2D1.RenderTests.Media } }; + RenderToFile(target); + CompareImages(); + } + +#if PERSPEX_CAIRO + [Fact(Skip = "VisualBrush not yet implemented on Cairo")] +#else + [Fact] +#endif + public void VisualBrush_No_Visual() + { + Decorator target = new Decorator + { + Padding = new Thickness(8), + Width = 200, + Height = 200, + Child = new Rectangle + { + Fill = new VisualBrush(), + } + }; + RenderToFile(target); CompareImages(); }