2
0

RenderTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Avalonia.Headless.XUnit;
  2. using Avalonia.Threading;
  3. using ChunkyImageLib;
  4. using ChunkyImageLib.DataHolders;
  5. using Drawie.Backend.Core;
  6. using Drawie.Backend.Core.Bridge;
  7. using Drawie.Backend.Core.ColorsImpl;
  8. using Drawie.Backend.Core.Numerics;
  9. using Drawie.Backend.Core.Surfaces;
  10. using Drawie.Numerics;
  11. using PixiEditor.Models.IO;
  12. using PixiEditor.Models.Position;
  13. using Xunit.Abstractions;
  14. using Color = Drawie.Backend.Core.ColorsImpl.Color;
  15. namespace PixiEditor.Tests;
  16. public class RenderTests : FullPixiEditorTest
  17. {
  18. private readonly ITestOutputHelper _testOutputHelper;
  19. public RenderTests(ITestOutputHelper testOutputHelper)
  20. {
  21. _testOutputHelper = testOutputHelper;
  22. }
  23. [AvaloniaTheory]
  24. [InlineData("Fibi")]
  25. [InlineData("Pond")]
  26. [InlineData("SmlPxlCircShadWithMask")]
  27. [InlineData("SmallPixelArtCircleShadow")]
  28. [InlineData("SmlPxlCircShadWithMaskClipped")]
  29. [InlineData("SmlPxlCircShadWithMaskClippedInFolder")]
  30. [InlineData("VectorRectangleClippedToCircle")]
  31. [InlineData("VectorRectangleClippedToCircleShadowFilter")]
  32. [InlineData("VectorRectangleClippedToCircleMasked")]
  33. [InlineData("BlendingLinearSrgb")]
  34. [InlineData("BlendingSrgb")]
  35. [InlineData("VectorWithSepiaFilter")]
  36. [InlineData("VectorWithSepiaFilterSrgb")]
  37. [InlineData("VectorWithSepiaFilterChained")]
  38. [InlineData("Offset")]
  39. [InlineData("Scale")]
  40. [InlineData("Skew")]
  41. [InlineData("Rotation")]
  42. [InlineData("MatrixChain")]
  43. [InlineData("GpuOffset", "Offset")]
  44. [InlineData("GpuScale")]
  45. [InlineData("GpuSkew")]
  46. [InlineData("GpuMatrixChain")]
  47. public void TestThatPixiFilesRenderTheSameResultAsSavedPng(string fileName, string? resultName = null)
  48. {
  49. if (!DrawingBackendApi.Current.IsHardwareAccelerated)
  50. {
  51. _testOutputHelper.WriteLine("Skipping the test because hardware acceleration is not enabled.");
  52. return;
  53. }
  54. string pixiFile = Path.Combine("TestFiles", "RenderTests", fileName + ".pixi");
  55. string pngFile = Path.Combine("TestFiles", "RenderTests", (resultName ?? fileName) + ".png");
  56. var document = Importer.ImportDocument(pixiFile);
  57. Assert.NotNull(pngFile);
  58. var result = document.TryRenderWholeImage(0);
  59. Assert.True(result is { IsT1: true, AsT1: not null }); // Check if rendering was successful
  60. using var image = result.AsT1;
  61. using var snapshot = image.DrawingSurface.Snapshot();
  62. using var encoded = snapshot.Encode();
  63. using var renderedToCompare = Surface.Load(encoded.AsSpan().ToArray());
  64. using var toCompareTo = Importer.ImportImage(pngFile, document.SizeBindable);
  65. Assert.NotNull(toCompareTo);
  66. Assert.True(PixelCompare(renderedToCompare, toCompareTo));
  67. }
  68. [AvaloniaTheory]
  69. [InlineData("SingleLayer")]
  70. [InlineData("SingleLayerWithMask")]
  71. [InlineData("LayerWithMaskClipped")]
  72. [InlineData("LayerWithMaskClippedHighDpiPresent")]
  73. [InlineData("LayerWithMaskClippedInFolder")]
  74. [InlineData("LayerWithMaskClippedInFolderWithMask")]
  75. public void TestThatHalfResolutionScalesRenderCorrectly(string pixiName)
  76. {
  77. string pixiFile = Path.Combine("TestFiles", "ResolutionTests", pixiName + ".pixi");
  78. var document = Importer.ImportDocument(pixiFile);
  79. ViewportInfo info = new ViewportInfo(
  80. 0,
  81. document.SizeBindable / 2f,
  82. document.SizeBindable,
  83. Matrix3X3.Identity, null, "DEFAULT", SamplingOptions.Default, document.SizeBindable, ChunkResolution.Half,
  84. Guid.NewGuid(), false, false, () => { });
  85. using var output = document.SceneRenderer.RenderScene(info, new AffectedArea(), null);
  86. Color expectedColor = Colors.Yellow;
  87. using Surface surface = Surface.ForDisplay(document.SizeBindable);
  88. surface.DrawingSurface.Canvas.DrawSurface(output.DrawingSurface, 0, 0);
  89. Assert.True(AllPixelsAreColor(surface, expectedColor));
  90. }
  91. private static bool PixelCompare(Surface image, Surface compareTo)
  92. {
  93. if (image.Size != compareTo.Size)
  94. {
  95. return false;
  96. }
  97. using Surface compareSurface1 = new Surface(image.Size);
  98. using Surface compareSurface2 = new Surface(image.Size);
  99. compareSurface1.DrawingSurface.Canvas.DrawSurface(image.DrawingSurface, 0, 0);
  100. compareSurface2.DrawingSurface.Canvas.DrawSurface(compareTo.DrawingSurface, 0, 0);
  101. var imageData1 = compareSurface1.PeekPixels();
  102. var imageData2 = compareSurface2.PeekPixels();
  103. if (imageData1.Width != imageData2.Width || imageData1.Height != imageData2.Height)
  104. {
  105. return false;
  106. }
  107. for (int y = 0; y < imageData1.Height; y++)
  108. {
  109. for (int x = 0; x < imageData1.Width; x++)
  110. {
  111. var pixel1 = imageData1.GetPixelColor(x, y);
  112. var pixel2 = imageData2.GetPixelColor(x, y);
  113. if (pixel1 != pixel2)
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. private static bool AllPixelsAreColor(Surface image, Color color)
  122. {
  123. var imageData = image.PeekPixels();
  124. for (int y = 0; y < imageData.Height; y++)
  125. {
  126. for (int x = 0; x < imageData.Width; x++)
  127. {
  128. var pixel = imageData.GetPixelColor(x, y);
  129. if (pixel != color)
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. return true;
  136. }
  137. }