Browse Source

ImageBrush implemented on cairo.

pull/148/head
Steven Kirk 11 years ago
parent
commit
43086960c4
  1. 48
      Tests/Perspex.RenderTests/Media/ImageBrushTests.cs
  2. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_Fill_NoTile.expected.png
  3. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipXY_TopLeftDest.expected.png
  4. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipX_TopLeftDest.expected.png
  5. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipY_TopLeftDest.expected.png
  6. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_BottomRight.expected.png
  7. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_Center.expected.png
  8. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_TopLeft.expected.png
  9. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterDest.expected.png
  10. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterSource.expected.png
  11. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterSource_BottomRightQuarterDest.expected.png
  12. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_Tile_BottomRightQuarterSource_CenterQuarterDest.expected.png
  13. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_UniformToFill_NoTile.expected.png
  14. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_Uniform_NoTile.expected.png
  15. BIN
      Tests/TestFiles/Cairo/Media/ImageBrush/github_icon.png
  16. 7
      src/Gtk/Perspex.Cairo/Media/DrawingContext.cs
  17. 152
      src/Gtk/Perspex.Cairo/Media/TileBrushes.cs
  18. 1
      src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj
  19. 1
      tests/Perspex.RenderTests/Perspex.Cairo.RenderTests.csproj

48
Tests/Perspex.RenderTests/Media/ImageBrushTests.cs

@ -26,11 +26,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
get { return System.IO.Path.Combine(OutputPath, "github_icon.png"); }
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_Alignment_TopLeft()
{
Decorator target = new Decorator
@ -55,11 +51,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_Alignment_Center()
{
Decorator target = new Decorator
@ -84,11 +76,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_Alignment_BottomRight()
{
Decorator target = new Decorator
@ -113,11 +101,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_Fill_NoTile()
{
Decorator target = new Decorator
@ -140,11 +124,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_Uniform_NoTile()
{
Decorator target = new Decorator
@ -167,11 +147,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_UniformToFill_NoTile()
{
Decorator target = new Decorator
@ -194,11 +170,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_BottomRightQuarterSource()
{
Decorator target = new Decorator
@ -222,11 +194,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_BottomRightQuarterDest()
{
Decorator target = new Decorator
@ -250,11 +218,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_NoTile_BottomRightQuarterSource_BottomRightQuarterDest()
{
Decorator target = new Decorator
@ -279,11 +243,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_Tile_BottomRightQuarterSource_CenterQuarterDest()
{
Decorator target = new Decorator
@ -309,7 +269,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
[Fact(Skip = "TileMode.FlipX not yet supported on cairo")]
#else
[Fact]
#endif
@ -337,7 +297,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
[Fact(Skip = "TileMode.FlipY not yet supported on cairo")]
#else
[Fact]
#endif
@ -364,11 +324,7 @@ namespace Perspex.Direct2D1.RenderTests.Media
CompareImages();
}
#if PERSPEX_CAIRO
[Fact(Skip = "ImageBrush not yet implemented on Cairo")]
#else
[Fact]
#endif
public void ImageBrush_NoStretch_FlipXY_TopLeftDest()
{
Decorator target = new Decorator

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_Fill_NoTile.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipXY_TopLeftDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipX_TopLeftDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_FlipY_TopLeftDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_BottomRight.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_Center.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_Alignment_TopLeft.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterSource.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_NoTile_BottomRightQuarterSource_BottomRightQuarterDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 930 B

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_NoStretch_Tile_BottomRightQuarterSource_CenterQuarterDest.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_UniformToFill_NoTile.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/ImageBrush_Uniform_NoTile.expected.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
Tests/TestFiles/Cairo/Media/ImageBrush/github_icon.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

7
src/Gtk/Perspex.Cairo/Media/DrawingContext.cs

@ -216,6 +216,7 @@ namespace Perspex.Cairo.Media
{
var solid = brush as SolidColorBrush;
var linearGradientBrush = brush as LinearGradientBrush;
var imageBrush = brush as ImageBrush;
if (solid != null)
{
@ -225,6 +226,10 @@ namespace Perspex.Cairo.Media
solid.Color.B / 255.0,
solid.Color.A / 255.0);
}
else if (imageBrush != null)
{
_context.SetSource(TileBrushes.CreateImageBrush(imageBrush, destinationSize));
}
else if (linearGradientBrush != null)
{
var start = linearGradientBrush.StartPoint.ToPixels(destinationSize);
@ -236,7 +241,7 @@ namespace Perspex.Cairo.Media
g.AddColorStop(s.Offset, s.Color.ToCairo());
g.Extend = Cairo.Extend.Pad;
_context.SetSource(g);
}
}

152
src/Gtk/Perspex.Cairo/Media/TileBrushes.cs

@ -0,0 +1,152 @@
// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using Cairo;
using Perspex.Cairo.Media.Imaging;
using Perspex.Media;
namespace Perspex.Cairo.Media
{
internal static class TileBrushes
{
public static SurfacePattern CreateImageBrush(ImageBrush brush, Size targetSize)
{
if (brush.Source == null)
{
return null;
}
// TODO: This is directly ported from Direct2D and could probably be made more
// efficient on cairo by taking advantage of the fact that cairo has Extend.None.
var image = ((BitmapImpl)brush.Source.PlatformImpl).Surface;
var imageSize = new Size(brush.Source.PixelWidth, brush.Source.PixelHeight);
var tileMode = brush.TileMode;
var sourceRect = brush.SourceRect.ToPixels(imageSize);
var destinationRect = brush.DestinationRect.ToPixels(targetSize);
var scale = brush.Stretch.CalculateScaling(destinationRect.Size, sourceRect.Size);
var translate = CalculateTranslate(brush, sourceRect, destinationRect, scale);
var intermediateSize = CalculateIntermediateSize(tileMode, targetSize, destinationRect.Size);
var intermediate = new ImageSurface(Format.ARGB32, (int)intermediateSize.Width, (int)intermediateSize.Height);
using (var context = new Context(intermediate))
{
Rect drawRect;
var transform = CalculateIntermediateTransform(
tileMode,
sourceRect,
destinationRect,
scale,
translate,
out drawRect);
context.Rectangle(drawRect.ToCairo());
context.Clip();
context.Transform(transform.ToCairo());
Gdk.CairoHelper.SetSourcePixbuf(context, image, 0, 0);
context.Rectangle(0, 0, imageSize.Width, imageSize.Height);
context.Fill();
var result = new SurfacePattern(intermediate);
if ((brush.TileMode & TileMode.FlipXY) != 0)
{
// TODO: Currently always FlipXY as that's all cairo supports natively.
// Support separate FlipX and FlipY by drawing flipped images to intermediate
// surface.
result.Extend = Extend.Reflect;
}
else
{
result.Extend = Extend.Repeat;
}
if (brush.TileMode != TileMode.None)
{
var matrix = result.Matrix;
matrix.InitTranslate(-destinationRect.X, -destinationRect.Y);
result.Matrix = matrix;
}
return result;
}
}
/// <summary>
/// Calculates a translate based on a <see cref="TileBrush"/>, a source and destination
/// rectangle and a scale.
/// </summary>
/// <param name="brush">The brush.</param>
/// <param name="sourceRect">The source rectangle.</param>
/// <param name="destinationRect">The destination rectangle.</param>
/// <param name="scale">The scale factor.</param>
/// <returns>A vector with the X and Y translate.</returns>
private static Vector CalculateTranslate(
TileBrush brush,
Rect sourceRect,
Rect destinationRect,
Vector scale)
{
var x = 0.0;
var y = 0.0;
var size = sourceRect.Size * scale;
switch (brush.AlignmentX)
{
case AlignmentX.Center:
x += (destinationRect.Width - size.Width) / 2;
break;
case AlignmentX.Right:
x += destinationRect.Width - size.Width;
break;
}
switch (brush.AlignmentY)
{
case AlignmentY.Center:
y += (destinationRect.Height - size.Height) / 2;
break;
case AlignmentY.Bottom:
y += destinationRect.Height - size.Height;
break;
}
return new Vector(x, y);
}
private static Size CalculateIntermediateSize(
TileMode tileMode,
Size targetSize,
Size destinationSize)
{
var result = tileMode == TileMode.None ? targetSize : destinationSize;
return result;
}
private static Matrix CalculateIntermediateTransform(
TileMode tileMode,
Rect sourceRect,
Rect destinationRect,
Vector scale,
Vector translate,
out Rect drawRect)
{
var transform = Matrix.CreateTranslation(-sourceRect.Position) *
Matrix.CreateScale(scale) *
Matrix.CreateTranslation(translate);
Rect dr;
if (tileMode == TileMode.None)
{
dr = destinationRect;
transform *= Matrix.CreateTranslation(destinationRect.Position);
}
else
{
dr = new Rect(destinationRect.Size);
}
drawRect = dr;
return transform;
}
}
}

1
src/Gtk/Perspex.Cairo/Perspex.Cairo.csproj

@ -72,6 +72,7 @@
<Compile Include="Media\FormattedTextImpl.cs" />
<Compile Include="Media\Imaging\BitmapImpl.cs" />
<Compile Include="Media\Imaging\RenderTargetBitmapImpl.cs" />
<Compile Include="Media\TileBrushes.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderer.cs" />
<Compile Include="CairoExtensions.cs" />

1
tests/Perspex.RenderTests/Perspex.Cairo.RenderTests.csproj

@ -74,6 +74,7 @@
<ItemGroup>
<Compile Include="Controls\ImageTests.cs" />
<Compile Include="Controls\BorderTests.cs" />
<Compile Include="Media\ImageBrushTests.cs" />
<Compile Include="Media\LinearGradientBrushTests.cs" />
<Compile Include="Media\VisualBrushTests.cs" />
<Compile Include="Shapes\PathTests.cs" />

Loading…
Cancel
Save