Browse Source

Fix grip visualbrush tests.

Steven Kirk 7 years ago
parent
commit
41f201bec8

+ 2 - 2
src/Avalonia.Visuals/Media/PixelSize.cs

@@ -159,8 +159,8 @@ namespace Avalonia
         /// <param name="dpi">The dots per inch.</param>
         /// <returns>The device-independent size.</returns>
         public static PixelSize FromSize(Size size, Vector dpi) => new PixelSize(
-            (int)(size.Width * (dpi.X / 96)),
-            (int)(size.Height * (dpi.Y / 96)));
+            (int)Math.Ceiling(size.Width * (dpi.X / 96)),
+            (int)Math.Ceiling(size.Height * (dpi.Y / 96)));
 
         /// <summary>
         /// Returns the string representation of the size.

+ 2 - 2
src/Avalonia.Visuals/Rendering/Utilities/TileBrushCalculator.cs

@@ -116,8 +116,8 @@ namespace Avalonia.Rendering.Utilities
                     return true;
                 if (SourceRect.Size.AspectRatio == _imageSize.AspectRatio)
                     return false;
-                if ((int)SourceRect.Width != _imageSize.Width ||
-                    (int)SourceRect.Height != _imageSize.Height)
+                if (SourceRect.Width != _imageSize.Width ||
+                    SourceRect.Height != _imageSize.Height)
                     return true;
                 return false;
             }

+ 7 - 1
src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs

@@ -434,10 +434,16 @@ namespace Avalonia.Direct2D1.Media
 
                     if (intermediateSize.Width >= 1 && intermediateSize.Height >= 1)
                     {
+                        // We need to ensure the size we're requesting is an integer pixel size, otherwise
+                        // D2D alters the DPI of the render target, which messes stuff up. PixelSize.FromSize
+                        // will do the rounding for us.
+                        var dpi = new Vector(_deviceContext.DotsPerInch.Width, _deviceContext.DotsPerInch.Height);
+                        var pixelSize = PixelSize.FromSize(intermediateSize, dpi);
+
                         using (var intermediate = new BitmapRenderTarget(
                             _deviceContext,
                             CompatibleRenderTargetOptions.None,
-                            intermediateSize.ToSharpDX()))
+                            pixelSize.ToSize(dpi).ToSharpDX()))
                         {
                             using (var ctx = new RenderTarget(intermediate).CreateDrawingContext(_visualBrushRenderer))
                             {

+ 2 - 1
src/Windows/Avalonia.Direct2D1/Media/ImageBrushImpl.cs

@@ -100,7 +100,8 @@ namespace Avalonia.Direct2D1.Media
 
             using (var context = new RenderTarget(result).CreateDrawingContext(null))
             {
-                var rect = new Rect(0, 0, bitmap.PixelSize.Width, bitmap.PixelSize.Height);
+                var dpi = new Vector(target.DotsPerInch.Width, target.DotsPerInch.Height);
+                var rect = new Rect(bitmap.PixelSize.ToSize(dpi));
 
                 context.Clear(Colors.Transparent);
                 context.PushClip(calc.IntermediateClip);

BIN
tests/TestFiles/Direct2D1/Media/VisualBrush/VisualBrush_Grip_144_Dpi.expected.png